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