xref: /netbsd-src/lib/libc/locale/setlocale.c (revision d1f43e70cb5544bfeaa3cb4776119ec76b02d3f5)
1 /* $NetBSD: setlocale.c,v 1.66 2024/06/08 21:35:18 joerg 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.66 2024/06/08 21:35:18 joerg 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
_setlocale_cache(locale_t loc,struct _locale_cache_t * cache)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 	const char *message_name = loc->part_name[LC_MESSAGES];
69 	_NumericLocale *numeric = loc->part_impl[LC_NUMERIC];
70 	_MonetaryLocale *monetary = loc->part_impl[LC_MONETARY];
71 	struct lconv *ldata;
72 
73 	struct _locale_cache_t *old_cache;
74 
75 	SLIST_FOREACH(old_cache, &caches, cache_link) {
76 		if (monetary_name != old_cache->monetary_name &&
77 		    strcmp(monetary_name, old_cache->monetary_name) != 0)
78 			continue;
79 		if (numeric_name != old_cache->numeric_name &&
80 		    strcmp(numeric_name, old_cache->numeric_name) != 0)
81 			continue;
82 		if (message_name != old_cache->message_name &&
83 		    strcmp(message_name, old_cache->message_name) != 0)
84 			continue;
85 		loc->cache = old_cache;
86 		free(cache);
87 		return 0;
88 	}
89 
90 	if (cache == NULL) {
91 		cache = malloc(sizeof(*cache));
92 		if (cache == NULL)
93 			return -1;
94 	}
95 
96 	cache->monetary_name = monetary_name;
97 	cache->numeric_name = numeric_name;
98 	cache->message_name = message_name;
99 	cache->errlist = NULL;
100 	cache->errlistp = &cache->errlist;
101 	cache->errlist_prefix = NULL;
102 	ldata = &cache->ldata;
103 
104 	ldata->decimal_point = __UNCONST(numeric->decimal_point);
105 	ldata->thousands_sep = __UNCONST(numeric->thousands_sep);
106 	ldata->grouping      = __UNCONST(numeric->grouping);
107 
108 	ldata->int_curr_symbol   = __UNCONST(monetary->int_curr_symbol);
109 	ldata->currency_symbol   = __UNCONST(monetary->currency_symbol);
110 	ldata->mon_decimal_point = __UNCONST(monetary->mon_decimal_point);
111 	ldata->mon_thousands_sep = __UNCONST(monetary->mon_thousands_sep);
112 	ldata->mon_grouping      = __UNCONST(monetary->mon_grouping);
113 	ldata->positive_sign     = __UNCONST(monetary->positive_sign);
114 	ldata->negative_sign     = __UNCONST(monetary->negative_sign);
115 
116 	ldata->int_frac_digits    = monetary->int_frac_digits;
117 	ldata->frac_digits        = monetary->frac_digits;
118 	ldata->p_cs_precedes      = monetary->p_cs_precedes;
119 	ldata->p_sep_by_space     = monetary->p_sep_by_space;
120 	ldata->n_cs_precedes      = monetary->n_cs_precedes;
121 	ldata->n_sep_by_space     = monetary->n_sep_by_space;
122 	ldata->p_sign_posn        = monetary->p_sign_posn;
123 	ldata->n_sign_posn        = monetary->n_sign_posn;
124 	ldata->int_p_cs_precedes  = monetary->int_p_cs_precedes;
125 	ldata->int_n_cs_precedes  = monetary->int_n_cs_precedes;
126 	ldata->int_p_sep_by_space = monetary-> int_p_sep_by_space;
127 	ldata->int_n_sep_by_space = monetary->int_n_sep_by_space;
128 	ldata->int_p_sign_posn    = monetary->int_p_sign_posn;
129 	ldata->int_n_sign_posn    = monetary->int_n_sign_posn;
130 	SLIST_INSERT_HEAD(&caches, cache, cache_link);
131 
132 	loc->cache = cache;
133 	return 0;
134 }
135 
136 _locale_set_t
_find_category(int category)137 _find_category(int category)
138 {
139 	static int initialised;
140 
141 	if (!initialised) {
142 		if (issetugid() || ((_PathLocale == NULL &&
143 		    (_PathLocale = getenv("PATH_LOCALE")) == NULL) ||
144 		    *_PathLocale == '\0'))
145 			_PathLocale = _PATH_LOCALE;
146 		initialised = 1;
147 	}
148 
149 	if (category >= LC_ALL && category < _LC_LAST)
150 		return all_categories[category];
151 	return NULL;
152 }
153 
154 const char *
_get_locale_env(const char * category)155 _get_locale_env(const char *category)
156 {
157 	const char *name;
158 
159 	/* 1. check LC_ALL */
160 	name = (const char *)getenv("LC_ALL");
161 	if (name == NULL || *name == '\0') {
162 		/* 2. check LC_* */
163 		name = (const char *)getenv(category);
164 		if (name == NULL || *name == '\0') {
165 			/* 3. check LANG */
166 			name = getenv("LANG");
167 		}
168 	}
169 	if (name == NULL || *name == '\0' || strchr(name, '/'))
170 		/* 4. if none is set, fall to "C" */
171 		name = _C_LOCALE;
172 	return name;
173 }
174 
175 char *
__setlocale(int category,const char * name)176 __setlocale(int category, const char *name)
177 {
178 	_locale_set_t sl;
179 	locale_t loc;
180 	struct _locale_cache_t *cache;
181 	const char *result;
182 
183 	sl = _find_category(category);
184 	if (sl == NULL)
185 		return NULL;
186 	cache = malloc(sizeof(*cache));
187 	if (cache == NULL)
188 		return NULL;
189 	loc = _current_locale();
190 	result = (*sl)(name, loc);
191 	_setlocale_cache(loc, cache);
192 	return __UNCONST(result);
193 }
194 
195 char *
setlocale(int category,const char * locale)196 setlocale(int category, const char *locale)
197 {
198 
199 	/* locale may be NULL */
200 
201 	__mb_len_max_runtime = MB_LEN_MAX;
202 	return __setlocale(category, locale);
203 }
204