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