xref: /freebsd/lib/libc/locale/setlocale.c (revision 1ca63a8219b88b752b064d19bd3428c61dbcf1f9)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1996 - 2002 FreeBSD Project
5  * Copyright (c) 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Paul Borman at Krystal Technologies.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #if defined(LIBC_SCCS) && !defined(lint)
37 static char sccsid[] = "@(#)setlocale.c	8.1 (Berkeley) 7/4/93";
38 #endif /* LIBC_SCCS and not lint */
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <errno.h>
42 #include <limits.h>
43 #include <locale.h>
44 #include <paths.h>			/* for _PATH_LOCALE */
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include "collate.h"
49 #include "lmonetary.h"			/* for __monetary_load_locale() */
50 #include "lnumeric.h"			/* for __numeric_load_locale() */
51 #include "lmessages.h"			/* for __messages_load_locale() */
52 #include "setlocale.h"
53 #include "ldpart.h"
54 #include "../stdtime/timelocal.h"	/* for __time_load_locale() */
55 
56 /*
57  * Category names for getenv()
58  */
59 static const char categories[_LC_LAST][12] = {
60 	"LC_ALL",
61 	"LC_COLLATE",
62 	"LC_CTYPE",
63 	"LC_MONETARY",
64 	"LC_NUMERIC",
65 	"LC_TIME",
66 	"LC_MESSAGES",
67 };
68 
69 /*
70  * Current locales for each category
71  */
72 static char current_categories[_LC_LAST][ENCODING_LEN + 1] = {
73 	"C",
74 	"C",
75 	"C",
76 	"C",
77 	"C",
78 	"C",
79 	"C",
80 };
81 
82 /*
83  * Path to locale storage directory
84  */
85 char   *_PathLocale;
86 
87 /*
88  * The locales we are going to try and load
89  */
90 static char new_categories[_LC_LAST][ENCODING_LEN + 1];
91 static char saved_categories[_LC_LAST][ENCODING_LEN + 1];
92 
93 static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)];
94 
95 static char *currentlocale(void);
96 static char *loadlocale(int);
97 const char *__get_locale_env(int);
98 
99 char *
100 setlocale(int category, const char *locale)
101 {
102 	int i, j, len, saverr;
103 	const char *env, *r;
104 
105 	if (category < LC_ALL || category >= _LC_LAST) {
106 		errno = EINVAL;
107 		return (NULL);
108 	}
109 	if (locale == NULL)
110 		return (category != LC_ALL ?
111 		    current_categories[category] : currentlocale());
112 
113 	/*
114 	 * Default to the current locale for everything.
115 	 */
116 	for (i = 1; i < _LC_LAST; ++i)
117 		(void)strcpy(new_categories[i], current_categories[i]);
118 
119 	/*
120 	 * Now go fill up new_categories from the locale argument
121 	 */
122 	if (!*locale) {
123 		if (category == LC_ALL) {
124 			for (i = 1; i < _LC_LAST; ++i) {
125 				env = __get_locale_env(i);
126 				if (strlen(env) > ENCODING_LEN) {
127 					errno = EINVAL;
128 					return (NULL);
129 				}
130 				(void)strcpy(new_categories[i], env);
131 			}
132 		} else {
133 			env = __get_locale_env(category);
134 			if (strlen(env) > ENCODING_LEN) {
135 				errno = EINVAL;
136 				return (NULL);
137 			}
138 			(void)strcpy(new_categories[category], env);
139 		}
140 	} else if (category != LC_ALL) {
141 		if (strlen(locale) > ENCODING_LEN) {
142 			errno = EINVAL;
143 			return (NULL);
144 		}
145 		(void)strcpy(new_categories[category], locale);
146 	} else {
147 		if ((r = strchr(locale, '/')) == NULL) {
148 			if (strlen(locale) > ENCODING_LEN) {
149 				errno = EINVAL;
150 				return (NULL);
151 			}
152 			for (i = 1; i < _LC_LAST; ++i)
153 				(void)strcpy(new_categories[i], locale);
154 		} else {
155 			for (i = 1; r[1] == '/'; ++r)
156 				;
157 			if (!r[1]) {
158 				errno = EINVAL;
159 				return (NULL);	/* Hmm, just slashes... */
160 			}
161 			do {
162 				if (i == _LC_LAST)
163 					break;	/* Too many slashes... */
164 				if ((len = r - locale) > ENCODING_LEN) {
165 					errno = EINVAL;
166 					return (NULL);
167 				}
168 				(void)strlcpy(new_categories[i], locale,
169 				    len + 1);
170 				i++;
171 				while (*r == '/')
172 					r++;
173 				locale = r;
174 				while (*r && *r != '/')
175 					r++;
176 			} while (*locale);
177 			while (i < _LC_LAST) {
178 				(void)strcpy(new_categories[i],
179 				    new_categories[i - 1]);
180 				i++;
181 			}
182 		}
183 	}
184 
185 	if (category != LC_ALL)
186 		return (loadlocale(category));
187 
188 	for (i = 1; i < _LC_LAST; ++i) {
189 		(void)strcpy(saved_categories[i], current_categories[i]);
190 		if (loadlocale(i) == NULL) {
191 			saverr = errno;
192 			for (j = 1; j < i; j++) {
193 				(void)strcpy(new_categories[j],
194 				    saved_categories[j]);
195 				if (loadlocale(j) == NULL) {
196 					(void)strcpy(new_categories[j], "C");
197 					(void)loadlocale(j);
198 				}
199 			}
200 			errno = saverr;
201 			return (NULL);
202 		}
203 	}
204 	return (currentlocale());
205 }
206 
207 static char *
208 currentlocale(void)
209 {
210 	int i;
211 
212 	(void)strcpy(current_locale_string, current_categories[1]);
213 
214 	for (i = 2; i < _LC_LAST; ++i)
215 		if (strcmp(current_categories[1], current_categories[i])) {
216 			for (i = 2; i < _LC_LAST; ++i) {
217 				(void)strcat(current_locale_string, "/");
218 				(void)strcat(current_locale_string,
219 				    current_categories[i]);
220 			}
221 			break;
222 		}
223 	return (current_locale_string);
224 }
225 
226 static char *
227 loadlocale(int category)
228 {
229 	char *new = new_categories[category];
230 	char *old = current_categories[category];
231 	int (*func) (const char *);
232 	int saved_errno;
233 
234 	if ((new[0] == '.' &&
235 	    (new[1] == '\0' || (new[1] == '.' && new[2] == '\0'))) ||
236 	    strchr(new, '/') != NULL) {
237 		errno = EINVAL;
238 		return (NULL);
239 	}
240 	saved_errno = errno;
241 	errno = __detect_path_locale();
242 	if (errno != 0)
243 		return (NULL);
244 	errno = saved_errno;
245 
246 	switch (category) {
247 	case LC_CTYPE:
248 		func = __wrap_setrunelocale;
249 		break;
250 	case LC_COLLATE:
251 		func = __collate_load_tables;
252 		break;
253 	case LC_TIME:
254 		func = __time_load_locale;
255 		break;
256 	case LC_NUMERIC:
257 		func = __numeric_load_locale;
258 		break;
259 	case LC_MONETARY:
260 		func = __monetary_load_locale;
261 		break;
262 	case LC_MESSAGES:
263 		func = __messages_load_locale;
264 		break;
265 	default:
266 		errno = EINVAL;
267 		return (NULL);
268 	}
269 
270 	if (strcmp(new, old) == 0)
271 		return (old);
272 
273 	if (func(new) != _LDP_ERROR) {
274 		(void)strcpy(old, new);
275 		(void)strcpy(__xlocale_global_locale.components[category-1]->locale, new);
276 		return (old);
277 	}
278 
279 	return (NULL);
280 }
281 
282 const char *
283 __get_locale_env(int category)
284 {
285 	const char *env;
286 
287 	/* 1. check LC_ALL. */
288 	env = getenv(categories[0]);
289 
290 	/* 2. check LC_* */
291 	if (env == NULL || !*env)
292 		env = getenv(categories[category]);
293 
294 	/* 3. check LANG */
295 	if (env == NULL || !*env)
296 		env = getenv("LANG");
297 
298 	/* 4. if none is set, fall to "C" */
299 	if (env == NULL || !*env)
300 		env = "C";
301 
302 	return (env);
303 }
304 
305 /*
306  * Detect locale storage location and store its value to _PathLocale variable
307  */
308 int
309 __detect_path_locale(void)
310 {
311 	if (_PathLocale == NULL) {
312 		char *p = secure_getenv("PATH_LOCALE");
313 
314 		if (p != NULL) {
315 			if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
316 			    1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
317 				return (ENAMETOOLONG);
318 			_PathLocale = strdup(p);
319 			if (_PathLocale == NULL)
320 				return (errno == 0 ? ENOMEM : errno);
321 		} else
322 			_PathLocale = _PATH_LOCALE;
323 	}
324 	return (0);
325 }
326