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