Home | History | Annotate | Line # | Download | only in locale
setlocale.c revision 1.64.20.1
      1  1.64.20.1  perseant /* $NetBSD: setlocale.c,v 1.64.20.1 2017/07/14 15:53:08 perseant Exp $ */
      2       1.10    kleink 
      3       1.55   tnozaki /*-
      4       1.55   tnozaki  * Copyright (c)2008 Citrus Project,
      5       1.55   tnozaki  * All rights reserved.
      6        1.1       cgd  *
      7        1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8        1.1       cgd  * modification, are permitted provided that the following conditions
      9        1.1       cgd  * are met:
     10        1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11        1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12        1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14        1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15        1.1       cgd  *
     16       1.55   tnozaki  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19       1.55   tnozaki  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26        1.1       cgd  * SUCH DAMAGE.
     27        1.1       cgd  */
     28        1.1       cgd 
     29       1.12  christos #include <sys/cdefs.h>
     30        1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     31  1.64.20.1  perseant __RCSID("$NetBSD: setlocale.c,v 1.64.20.1 2017/07/14 15:53:08 perseant Exp $");
     32        1.1       cgd #endif /* LIBC_SCCS and not lint */
     33        1.1       cgd 
     34       1.37      yamt #include <sys/types.h>
     35       1.64     joerg #include <sys/localedef.h>
     36        1.1       cgd #include <locale.h>
     37       1.58   tnozaki #include <limits.h>
     38       1.11    kleink #include <paths.h>
     39        1.5       jtc #include <stdlib.h>
     40        1.1       cgd #include <string.h>
     41       1.21     veego #include <unistd.h>
     42       1.44  tshiozak 
     43       1.55   tnozaki #include "setlocale_local.h"
     44       1.55   tnozaki 
     45       1.55   tnozaki const char *_PathLocale = NULL;
     46       1.44  tshiozak 
     47       1.60   tnozaki static _locale_set_t all_categories[_LC_LAST] = {
     48       1.60   tnozaki 	[LC_ALL     ] = &_generic_LC_ALL_setlocale,
     49  1.64.20.1  perseant 	[LC_COLLATE ] = &_citrus_LC_COLLATE_setlocale,
     50       1.60   tnozaki 	[LC_CTYPE   ] = &_citrus_LC_CTYPE_setlocale,
     51       1.60   tnozaki 	[LC_MONETARY] = &_citrus_LC_MONETARY_setlocale,
     52       1.60   tnozaki 	[LC_NUMERIC ] = &_citrus_LC_NUMERIC_setlocale,
     53       1.60   tnozaki 	[LC_TIME    ] = &_citrus_LC_TIME_setlocale,
     54       1.60   tnozaki 	[LC_MESSAGES] = &_citrus_LC_MESSAGES_setlocale,
     55       1.60   tnozaki };
     56        1.1       cgd 
     57       1.64     joerg /* XXX Consider locking the list. Race condition leaks memory. */
     58       1.64     joerg static SLIST_HEAD(, _locale_cache_t) caches = {
     59       1.64     joerg     __UNCONST(&_C_cache)
     60       1.64     joerg };
     61       1.64     joerg 
     62       1.64     joerg int
     63       1.64     joerg _setlocale_cache(locale_t loc, struct _locale_cache_t *cache)
     64       1.64     joerg {
     65       1.64     joerg 	const char *monetary_name = loc->part_name[LC_MONETARY];
     66       1.64     joerg 	const char *numeric_name = loc->part_name[LC_NUMERIC];
     67       1.64     joerg 	_NumericLocale *numeric = loc->part_impl[LC_NUMERIC];
     68       1.64     joerg 	_MonetaryLocale *monetary = loc->part_impl[LC_MONETARY];
     69       1.64     joerg 	struct lconv *ldata;
     70       1.64     joerg 
     71       1.64     joerg 	struct _locale_cache_t *old_cache;
     72       1.64     joerg 
     73       1.64     joerg 	SLIST_FOREACH(old_cache, &caches, cache_link) {
     74       1.64     joerg 		if (monetary_name != old_cache->monetary_name &&
     75       1.64     joerg 		    strcmp(monetary_name, old_cache->monetary_name) != 0)
     76       1.64     joerg 			continue;
     77       1.64     joerg 		if (numeric_name != old_cache->numeric_name &&
     78       1.64     joerg 		    strcmp(numeric_name, old_cache->numeric_name) != 0)
     79       1.64     joerg 			continue;
     80       1.64     joerg 		loc->cache = old_cache;
     81       1.64     joerg 		free(cache);
     82       1.64     joerg 		return 0;
     83       1.64     joerg 	}
     84       1.64     joerg 
     85       1.64     joerg 	if (cache == NULL) {
     86       1.64     joerg 		cache = malloc(sizeof(*cache));
     87       1.64     joerg 		if (cache == NULL)
     88       1.64     joerg 			return -1;
     89       1.64     joerg 	}
     90       1.64     joerg 
     91       1.64     joerg 	cache->monetary_name = monetary_name;
     92       1.64     joerg 	cache->numeric_name = numeric_name;
     93       1.64     joerg 	ldata = &cache->ldata;
     94       1.64     joerg 
     95       1.64     joerg 	ldata->decimal_point = __UNCONST(numeric->decimal_point);
     96       1.64     joerg 	ldata->thousands_sep = __UNCONST(numeric->thousands_sep);
     97       1.64     joerg 	ldata->grouping      = __UNCONST(numeric->grouping);
     98       1.64     joerg 
     99       1.64     joerg 	ldata->int_curr_symbol   = __UNCONST(monetary->int_curr_symbol);
    100       1.64     joerg 	ldata->currency_symbol   = __UNCONST(monetary->currency_symbol);
    101       1.64     joerg 	ldata->mon_decimal_point = __UNCONST(monetary->mon_decimal_point);
    102       1.64     joerg 	ldata->mon_thousands_sep = __UNCONST(monetary->mon_thousands_sep);
    103       1.64     joerg 	ldata->mon_grouping      = __UNCONST(monetary->mon_grouping);
    104       1.64     joerg 	ldata->positive_sign     = __UNCONST(monetary->positive_sign);
    105       1.64     joerg 	ldata->negative_sign     = __UNCONST(monetary->negative_sign);
    106       1.64     joerg 
    107       1.64     joerg 	ldata->int_frac_digits    = monetary->int_frac_digits;
    108       1.64     joerg 	ldata->frac_digits        = monetary->frac_digits;
    109       1.64     joerg 	ldata->p_cs_precedes      = monetary->p_cs_precedes;
    110       1.64     joerg 	ldata->p_sep_by_space     = monetary->p_sep_by_space;
    111       1.64     joerg 	ldata->n_cs_precedes      = monetary->n_cs_precedes;
    112       1.64     joerg 	ldata->n_sep_by_space     = monetary->n_sep_by_space;
    113       1.64     joerg 	ldata->p_sign_posn        = monetary->p_sign_posn;
    114       1.64     joerg 	ldata->n_sign_posn        = monetary->n_sign_posn;
    115       1.64     joerg 	ldata->int_p_cs_precedes  = monetary->int_p_cs_precedes;
    116       1.64     joerg 	ldata->int_n_cs_precedes  = monetary->int_n_cs_precedes;
    117       1.64     joerg 	ldata->int_p_sep_by_space = monetary-> int_p_sep_by_space;
    118       1.64     joerg 	ldata->int_n_sep_by_space = monetary->int_n_sep_by_space;
    119       1.64     joerg 	ldata->int_p_sign_posn    = monetary->int_p_sign_posn;
    120       1.64     joerg 	ldata->int_n_sign_posn    = monetary->int_n_sign_posn;
    121       1.64     joerg 	SLIST_INSERT_HEAD(&caches, cache, cache_link);
    122       1.64     joerg 
    123       1.64     joerg 	loc->cache = cache;
    124       1.64     joerg 	return 0;
    125       1.64     joerg }
    126       1.64     joerg 
    127       1.60   tnozaki _locale_set_t
    128       1.55   tnozaki _find_category(int category)
    129       1.24    itojun {
    130       1.62     joerg 	static int initialised;
    131       1.62     joerg 
    132       1.62     joerg 	if (!initialised) {
    133       1.62     joerg 		if (issetugid() || ((_PathLocale == NULL &&
    134       1.62     joerg 		    (_PathLocale = getenv("PATH_LOCALE")) == NULL) ||
    135       1.62     joerg 		    *_PathLocale == '\0'))
    136       1.62     joerg 			_PathLocale = _PATH_LOCALE;
    137       1.62     joerg 		initialised = 1;
    138       1.62     joerg 	}
    139       1.62     joerg 
    140       1.60   tnozaki 	if (category >= LC_ALL && category < _LC_LAST)
    141       1.60   tnozaki 		return all_categories[category];
    142       1.55   tnozaki 	return NULL;
    143        1.5       jtc }
    144        1.5       jtc 
    145       1.55   tnozaki const char *
    146       1.55   tnozaki _get_locale_env(const char *category)
    147        1.5       jtc {
    148       1.55   tnozaki 	const char *name;
    149        1.5       jtc 
    150       1.55   tnozaki 	/* 1. check LC_ALL */
    151       1.55   tnozaki 	name = (const char *)getenv("LC_ALL");
    152       1.55   tnozaki 	if (name == NULL || *name == '\0') {
    153       1.55   tnozaki 		/* 2. check LC_* */
    154       1.55   tnozaki 		name = (const char *)getenv(category);
    155       1.55   tnozaki 		if (name == NULL || *name == '\0') {
    156       1.55   tnozaki 			/* 3. check LANG */
    157       1.55   tnozaki 			name = getenv("LANG");
    158       1.51      manu 		}
    159       1.46  tshiozak 	}
    160       1.55   tnozaki 	if (name == NULL || *name == '\0' || strchr(name, '/'))
    161       1.55   tnozaki 		/* 4. if none is set, fall to "C" */
    162       1.55   tnozaki 		name = _C_LOCALE;
    163       1.55   tnozaki 	return name;
    164       1.46  tshiozak }
    165       1.46  tshiozak 
    166       1.55   tnozaki char *
    167       1.55   tnozaki __setlocale(int category, const char *name)
    168       1.44  tshiozak {
    169       1.60   tnozaki 	_locale_set_t sl;
    170       1.64     joerg 	locale_t loc;
    171       1.64     joerg 	struct _locale_cache_t *cache;
    172       1.64     joerg 	const char *result;
    173       1.44  tshiozak 
    174       1.60   tnozaki 	sl = _find_category(category);
    175       1.60   tnozaki 	if (sl == NULL)
    176       1.60   tnozaki 		return NULL;
    177       1.64     joerg 	cache = malloc(sizeof(*cache));
    178       1.64     joerg 	if (cache == NULL)
    179       1.64     joerg 		return NULL;
    180       1.64     joerg 	loc = _current_locale();
    181       1.64     joerg 	result = (*sl)(name, loc);
    182       1.64     joerg 	_setlocale_cache(loc, cache);
    183       1.64     joerg 	return __UNCONST(result);
    184        1.1       cgd }
    185       1.58   tnozaki 
    186       1.58   tnozaki char *
    187       1.58   tnozaki setlocale(int category, const char *locale)
    188       1.58   tnozaki {
    189       1.58   tnozaki 
    190       1.58   tnozaki 	/* locale may be NULL */
    191       1.58   tnozaki 
    192       1.58   tnozaki 	__mb_len_max_runtime = MB_LEN_MAX;
    193       1.58   tnozaki 	return __setlocale(category, locale);
    194       1.58   tnozaki }
    195