xref: /freebsd/lib/libc/locale/setlocale.c (revision 3047fefe49f57a673de8df152c199de12ec2c6d3)
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 <rune.h>
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  * 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 int	wrap_setrunelocale(const char *);
97 static char	*loadlocale(int);
98 
99 char *
100 setlocale(category, locale)
101 	int category;
102 	const char *locale;
103 {
104 	int i, j, len, saverr;
105 	char *env, *r;
106 
107 	if (category < LC_ALL || category >= _LC_LAST) {
108 		errno = EINVAL;
109 		return (NULL);
110 	}
111 
112 	if (locale == NULL)
113 		return (category != LC_ALL ?
114 		    current_categories[category] : currentlocale());
115 
116 	/*
117 	 * Default to the current locale for everything.
118 	 */
119 	for (i = 1; i < _LC_LAST; ++i)
120 		(void)strcpy(new_categories[i], current_categories[i]);
121 
122 	/*
123 	 * Now go fill up new_categories from the locale argument
124 	 */
125 	if (!*locale) {
126 		env = getenv("LC_ALL");
127 
128 		if (category != LC_ALL && (env == NULL || !*env))
129 			env = getenv(categories[category]);
130 
131 		if (env == NULL || !*env)
132 			env = getenv("LANG");
133 
134 		if (env == NULL || !*env)
135 			env = "C";
136 
137 		if (strlen(env) > ENCODING_LEN) {
138 			errno = EINVAL;
139 			return (NULL);
140 		}
141 		(void)strcpy(new_categories[category], env);
142 
143 		if (category == LC_ALL) {
144 			for (i = 1; i < _LC_LAST; ++i) {
145 				if ((env = getenv(categories[i])) == NULL ||
146 				    !*env)
147 					env = new_categories[LC_ALL];
148 				else if (strlen(env) > ENCODING_LEN) {
149 					errno = EINVAL;
150 					return (NULL);
151 				}
152 				(void)strcpy(new_categories[i], env);
153 			}
154 		}
155 	} else if (category != LC_ALL) {
156 		if (strlen(locale) > ENCODING_LEN) {
157 			errno = EINVAL;
158 			return (NULL);
159 		}
160 		(void)strcpy(new_categories[category], locale);
161 	} else {
162 		if ((r = strchr(locale, '/')) == NULL) {
163 			if (strlen(locale) > ENCODING_LEN) {
164 				errno = EINVAL;
165 				return (NULL);
166 			}
167 			for (i = 1; i < _LC_LAST; ++i)
168 				(void)strcpy(new_categories[i], locale);
169 		} else {
170 			for (i = 1; r[1] == '/'; ++r)
171 				;
172 			if (!r[1]) {
173 				errno = EINVAL;
174 				return (NULL);	/* Hmm, just slashes... */
175 			}
176 			do {
177 				if (i == _LC_LAST)
178 					break;  /* Too many slashes... */
179 				if ((len = r - locale) > ENCODING_LEN) {
180 					errno = EINVAL;
181 					return (NULL);
182 				}
183 				(void)strlcpy(new_categories[i], locale,
184 					      len + 1);
185 				i++;
186 				locale = r;
187 				while (*locale == '/')
188 					++locale;
189 				while (*++r && *r != '/')
190 					;
191 			} while (*locale);
192 			while (i < _LC_LAST) {
193 				(void)strcpy(new_categories[i],
194 					     new_categories[i-1]);
195 				i++;
196 			}
197 		}
198 	}
199 
200 	if (category != LC_ALL)
201 		return (loadlocale(category));
202 
203 	for (i = 1; i < _LC_LAST; ++i) {
204 		(void)strcpy(saved_categories[i], current_categories[i]);
205 		if (loadlocale(i) == NULL) {
206 			saverr = errno;
207 			for (j = 1; j < i; j++) {
208 				(void)strcpy(new_categories[j],
209 					     saved_categories[j]);
210 				if (loadlocale(j) == NULL) {
211 					(void)strcpy(new_categories[j], "C");
212 					(void)loadlocale(j);
213 				}
214 			}
215 			errno = saverr;
216 			return (NULL);
217 		}
218 	}
219 	return (currentlocale());
220 }
221 
222 static char *
223 currentlocale()
224 {
225 	int i;
226 
227 	(void)strcpy(current_locale_string, current_categories[1]);
228 
229 	for (i = 2; i < _LC_LAST; ++i)
230 		if (strcmp(current_categories[1], current_categories[i])) {
231 			for (i = 2; i < _LC_LAST; ++i) {
232 				(void)strcat(current_locale_string, "/");
233 				(void)strcat(current_locale_string,
234 					     current_categories[i]);
235 			}
236 			break;
237 		}
238 	return (current_locale_string);
239 }
240 
241 static int
242 wrap_setrunelocale(const char *locale)
243 {
244 	int ret = setrunelocale((char *)locale);
245 
246 	if (ret != 0) {
247 		errno = ret;
248 		return (_LDP_ERROR);
249 	}
250 	return (_LDP_LOADED);
251 }
252 
253 static char *
254 loadlocale(category)
255 	int category;
256 {
257 	char *new = new_categories[category];
258 	char *old = current_categories[category];
259 	int (*func)(const char *);
260 
261 	if ((new[0] == '.' &&
262 	     (new[1] == '\0' || (new[1] == '.' && new[2] == '\0'))) ||
263 	    strchr(new, '/') != NULL) {
264 		errno = EINVAL;
265 		return (NULL);
266 	}
267 
268 	if (_PathLocale == NULL) {
269 		char *p = getenv("PATH_LOCALE");
270 
271 		if (p != NULL
272 #ifndef __NETBSD_SYSCALLS
273 			&& !issetugid()
274 #endif
275 			) {
276 			if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
277 			    1/*"/"*/ + CATEGORY_LEN >= PATH_MAX) {
278 				errno = ENAMETOOLONG;
279 				return (NULL);
280 			}
281 			_PathLocale = strdup(p);
282 			if (_PathLocale == NULL) {
283 				errno = ENOMEM;
284 				return (NULL);
285 			}
286 		} else
287 			_PathLocale = _PATH_LOCALE;
288 	}
289 
290 	switch (category) {
291 	case LC_CTYPE:
292 		func = wrap_setrunelocale;
293 		break;
294 	case LC_COLLATE:
295 		func = __collate_load_tables;
296 		break;
297 	case LC_TIME:
298 		func = __time_load_locale;
299 		break;
300 	case LC_NUMERIC:
301 		func = __numeric_load_locale;
302 		break;
303 	case LC_MONETARY:
304 		func = __monetary_load_locale;
305 		break;
306 	case LC_MESSAGES:
307 		func = __messages_load_locale;
308 		break;
309 	default:
310 		errno = EINVAL;
311 		return (NULL);
312 	}
313 
314 	if (strcmp(new, old) == 0)
315 		return (old);
316 
317 	if (func(new) != _LDP_ERROR) {
318 		(void)strcpy(old, new);
319 		return (old);
320 	}
321 
322 	return (NULL);
323 }
324 
325