xref: /freebsd/lib/libc/locale/setlocale.c (revision 4a0f765fbf09711e612e86fce8bb09ec43f482d9)
1 /*
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Paul Borman at Krystal Technologies.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * $FreeBSD$
37  */
38 
39 #ifdef LIBC_RCS
40 static const char rcsid[] =
41 	"$FreeBSD$";
42 #endif
43 
44 #if defined(LIBC_SCCS) && !defined(lint)
45 static char sccsid[] = "@(#)setlocale.c	8.1 (Berkeley) 7/4/93";
46 #endif /* LIBC_SCCS and not lint */
47 
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <limits.h>
51 #include <locale.h>
52 #include <rune.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include "collate.h"
57 #include "setlocale.h"
58 
59 /*
60  * Category names for getenv()
61  */
62 static char *categories[_LC_LAST] = {
63     "LC_ALL",
64     "LC_COLLATE",
65     "LC_CTYPE",
66     "LC_MONETARY",
67     "LC_NUMERIC",
68     "LC_TIME",
69 };
70 
71 /*
72  * Current locales for each category
73  */
74 static char current_categories[_LC_LAST][ENCODING_LEN + 1] = {
75     "C",
76     "C",
77     "C",
78     "C",
79     "C",
80     "C",
81 };
82 
83 /*
84  * The locales we are going to try and load
85  */
86 static char new_categories[_LC_LAST][ENCODING_LEN + 1];
87 static char saved_categories[_LC_LAST][ENCODING_LEN + 1];
88 
89 static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)];
90 
91 static char	*currentlocale __P((void));
92 static char	*loadlocale __P((int));
93 static int      stub_load_locale __P((const char *));
94 
95 extern int __time_load_locale __P((const char *)); /* strftime.c */
96 
97 #ifdef XPG4
98 extern int _xpg4_setrunelocale __P((char *));
99 #endif
100 
101 char *
102 setlocale(category, locale)
103 	int category;
104 	const char *locale;
105 {
106 	int i, j, len;
107 	char *env, *r;
108 
109 	if (category < LC_ALL || category >= _LC_LAST)
110 		return (NULL);
111 
112 	if (!locale)
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(categories[category]);
127 
128 		if (category != LC_ALL && (!env || !*env))
129 			env = getenv(categories[LC_ALL]);
130 
131 		if (!env || !*env)
132 			env = getenv("LANG");
133 
134 		if (!env || !*env)
135 			env = "C";
136 
137 		(void) strncpy(new_categories[category], env, ENCODING_LEN);
138 		new_categories[category][ENCODING_LEN] = '\0';
139 		if (category == LC_ALL) {
140 			for (i = 1; i < _LC_LAST; ++i) {
141 				if (!(env = getenv(categories[i])) || !*env)
142 					env = new_categories[LC_ALL];
143 				(void)strncpy(new_categories[i], env, ENCODING_LEN);
144 				new_categories[i][ENCODING_LEN] = '\0';
145 			}
146 		}
147 	} else if (category != LC_ALL)  {
148 		(void)strncpy(new_categories[category], locale, ENCODING_LEN);
149 		new_categories[category][ENCODING_LEN] = '\0';
150 	} else {
151 		if ((r = strchr(locale, '/')) == NULL) {
152 			for (i = 1; i < _LC_LAST; ++i) {
153 				(void)strncpy(new_categories[i], locale, ENCODING_LEN);
154 				new_categories[i][ENCODING_LEN] = '\0';
155 			}
156 		} else {
157 			for (i = 1; r[1] == '/'; ++r);
158 			if (!r[1])
159 				return (NULL);	/* Hmm, just slashes... */
160 			do {
161 				len = r - locale > ENCODING_LEN ? ENCODING_LEN : r - locale;
162 				(void)strncpy(new_categories[i], locale, len);
163 				new_categories[i][len] = '\0';
164 				i++;
165 				locale = r;
166 				while (*locale == '/')
167 				    ++locale;
168 				while (*++r && *r != '/');
169 			} while (*locale);
170 			while (i < _LC_LAST)
171 				(void)strcpy(new_categories[i],
172 				    new_categories[i-1]);
173 		}
174 	}
175 
176 	if (category)
177 		return (loadlocale(category));
178 
179 	for (i = 1; i < _LC_LAST; ++i) {
180 		(void)strcpy(saved_categories[i], current_categories[i]);
181 		if (loadlocale(i) == NULL) {
182 			for (j = 1; j < i; j++) {
183 				(void)strcpy(new_categories[j],
184 				     saved_categories[j]);
185 				/* XXX can fail too */
186 				(void)loadlocale(j);
187 			}
188 			return (NULL);
189 		}
190 	}
191 	return (currentlocale());
192 }
193 
194 static char *
195 currentlocale()
196 {
197 	int i;
198 
199 	(void)strcpy(current_locale_string, current_categories[1]);
200 
201 	for (i = 2; i < _LC_LAST; ++i)
202 		if (strcmp(current_categories[1], current_categories[i])) {
203 			(void) strcpy(current_locale_string, current_categories[1]);
204 			(void) strcat(current_locale_string, "/");
205 			(void) strcat(current_locale_string, current_categories[2]);
206 			(void) strcat(current_locale_string, "/");
207 			(void) strcat(current_locale_string, current_categories[3]);
208 			(void) strcat(current_locale_string, "/");
209 			(void) strcat(current_locale_string, current_categories[4]);
210 			(void) strcat(current_locale_string, "/");
211 			(void) strcat(current_locale_string, current_categories[5]);
212 			break;
213 		}
214 	return (current_locale_string);
215 }
216 
217 static char *
218 loadlocale(category)
219 	int category;
220 {
221 	char *ret;
222 	char *new = new_categories[category];
223 	char *old = current_categories[category];
224 
225 	if (!_PathLocale)
226 		_PathLocale = _PATH_LOCALE;
227 
228 	if (strcmp(new, old) == 0)
229 		return (old);
230 
231 	if (category == LC_CTYPE) {
232 #ifdef XPG4
233 		ret = _xpg4_setrunelocale(new) ? NULL : new;
234 #else
235 		ret = setrunelocale(new) ? NULL : new;
236 #endif
237 		if (!ret) {
238 #ifdef XPG4
239 			(void)_xpg4_setrunelocale(old);
240 #else
241 			(void)setrunelocale(old);
242 #endif
243 		} else
244 			(void)strcpy(old, new);
245 		return (ret);
246 	}
247 
248 	if (category == LC_COLLATE) {
249 		ret = (__collate_load_tables(new) < 0) ? NULL : new;
250 		if (!ret)
251 			(void)__collate_load_tables(old);
252 		else
253 			(void)strcpy(old, new);
254 		return (ret);
255 	}
256 
257 	if (category == LC_TIME) {
258 		ret = (__time_load_locale(new) < 0) ? NULL : new;
259 		if (!ret)
260 			(void)__time_load_locale(old);
261 		else
262 			(void)strcpy(old, new);
263 		return (ret);
264 	}
265 
266 	if (category == LC_MONETARY || category == LC_NUMERIC) {
267 		ret = stub_load_locale(new) ? NULL : new;
268 		if (!ret)
269 			(void)stub_load_locale(old);
270 		else
271 			(void)strcpy(old, new);
272 		return (ret);
273 	}
274 
275 	/* Just in case...*/
276 	return (NULL);
277 }
278 
279 static int
280 stub_load_locale(encoding)
281 const char *encoding;
282 {
283 	char name[PATH_MAX];
284 	struct stat st;
285 
286 	if (!encoding)
287 		return(1);
288 	/*
289 	 * The "C" and "POSIX" locale are always here.
290 	 */
291 	if (!strcmp(encoding, "C") || !strcmp(encoding, "POSIX"))
292 		return(0);
293 	if (!_PathLocale)
294 		return(1);
295 	/* Range checking not needed, encoding has fixed size */
296 	strcpy(name, _PathLocale);
297 	strcat(name, "/");
298 	strcat(name, encoding);
299 #if 0
300 	/*
301 	 * Some day we will actually look at this file.
302 	 */
303 #endif
304 	return (stat(name, &st) != 0 || !S_ISDIR(st.st_mode));
305 }
306