xref: /freebsd/lib/libc/locale/setlocale.c (revision b52b9d56d4e96089873a75f9e29062eec19fabba)
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 <limits.h>
47 #include <locale.h>
48 #include <rune.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include "collate.h"
53 #include "lmonetary.h"	/* for __monetary_load_locale() */
54 #include "lnumeric.h"	/* for __numeric_load_locale() */
55 #include "lmessages.h"	/* for __messages_load_locale() */
56 #include "setlocale.h"
57 #include "../stdtime/timelocal.h" /* for __time_load_locale() */
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     "LC_MESSAGES",
70 };
71 
72 /*
73  * Current locales for each category
74  */
75 static char current_categories[_LC_LAST][ENCODING_LEN + 1] = {
76     "C",
77     "C",
78     "C",
79     "C",
80     "C",
81     "C",
82     "C",
83 };
84 
85 /*
86  * The locales we are going to try and load
87  */
88 static char new_categories[_LC_LAST][ENCODING_LEN + 1];
89 static char saved_categories[_LC_LAST][ENCODING_LEN + 1];
90 
91 static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)];
92 
93 static char	*currentlocale(void);
94 static char	*loadlocale(int);
95 
96 char *
97 setlocale(category, locale)
98 	int category;
99 	const char *locale;
100 {
101 	int i, j, len;
102 	char *env, *r;
103 
104 	if (category < LC_ALL || category >= _LC_LAST)
105 		return (NULL);
106 
107 	if (!locale)
108 		return (category != LC_ALL ?
109 		    current_categories[category] : currentlocale());
110 
111 	/*
112 	 * Default to the current locale for everything.
113 	 */
114 	for (i = 1; i < _LC_LAST; ++i)
115 		(void)strcpy(new_categories[i], current_categories[i]);
116 
117 	/*
118 	 * Now go fill up new_categories from the locale argument
119 	 */
120 	if (!*locale) {
121 		env = getenv("LC_ALL");
122 
123 		if (category != LC_ALL && (!env || !*env))
124 			env = getenv(categories[category]);
125 
126 		if (!env || !*env)
127 			env = getenv("LANG");
128 
129 		if (!env || !*env || strchr(env, '/'))
130 			env = "C";
131 
132 		(void) strncpy(new_categories[category], env, ENCODING_LEN);
133 		new_categories[category][ENCODING_LEN] = '\0';
134 		if (category == LC_ALL) {
135 			for (i = 1; i < _LC_LAST; ++i) {
136 				if (!(env = getenv(categories[i])) || !*env)
137 					env = new_categories[LC_ALL];
138 				(void)strncpy(new_categories[i], env, ENCODING_LEN);
139 				new_categories[i][ENCODING_LEN] = '\0';
140 			}
141 		}
142 	} else if (category != LC_ALL)  {
143 		(void)strncpy(new_categories[category], locale, ENCODING_LEN);
144 		new_categories[category][ENCODING_LEN] = '\0';
145 	} else {
146 		if ((r = strchr(locale, '/')) == NULL) {
147 			for (i = 1; i < _LC_LAST; ++i) {
148 				(void)strncpy(new_categories[i], locale, ENCODING_LEN);
149 				new_categories[i][ENCODING_LEN] = '\0';
150 			}
151 		} else {
152 			for (i = 1; r[1] == '/'; ++r);
153 			if (!r[1])
154 				return (NULL);	/* Hmm, just slashes... */
155 			do {
156 				len = r - locale > ENCODING_LEN ? ENCODING_LEN : r - locale;
157 				(void)strncpy(new_categories[i], locale, len);
158 				new_categories[i][len] = '\0';
159 				i++;
160 				locale = r;
161 				while (*locale == '/')
162 				    ++locale;
163 				while (*++r && *r != '/');
164 			} while (*locale);
165 			while (i < _LC_LAST) {
166 				(void)strcpy(new_categories[i],
167 				    new_categories[i-1]);
168 				i++;
169 			}
170 		}
171 	}
172 
173 	if (category != LC_ALL)
174 		return (loadlocale(category));
175 
176 	for (i = 1; i < _LC_LAST; ++i) {
177 		(void)strcpy(saved_categories[i], current_categories[i]);
178 		if (loadlocale(i) == NULL) {
179 			for (j = 1; j < i; j++) {
180 				(void)strcpy(new_categories[j],
181 				     saved_categories[j]);
182 				/* XXX can fail too */
183 				(void)loadlocale(j);
184 			}
185 			return (NULL);
186 		}
187 	}
188 	return (currentlocale());
189 }
190 
191 static char *
192 currentlocale()
193 {
194 	int i;
195 
196 	(void)strcpy(current_locale_string, current_categories[1]);
197 
198 	for (i = 2; i < _LC_LAST; ++i)
199 		if (strcmp(current_categories[1], current_categories[i])) {
200 			for (i = 2; i < _LC_LAST; ++i) {
201 				(void) strcat(current_locale_string, "/");
202 				(void) strcat(current_locale_string, current_categories[i]);
203 			}
204 			break;
205 		}
206 	return (current_locale_string);
207 }
208 
209 static char *
210 loadlocale(category)
211 	int category;
212 {
213 	char *ret;
214 	char *new = new_categories[category];
215 	char *old = current_categories[category];
216 
217 	if (_PathLocale == NULL) {
218 		char *p = getenv("PATH_LOCALE");
219 
220 		if (p != NULL
221 #ifndef __NETBSD_SYSCALLS
222 			&& !issetugid()
223 #endif
224 			) {
225 			if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
226 			    1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
227 				return (NULL);
228 			_PathLocale = strdup(p);
229 			if (_PathLocale == NULL)
230 				return (NULL);
231 		} else
232 			_PathLocale = _PATH_LOCALE;
233 	}
234 
235 	if (strcmp(new, old) == 0)
236 		return (old);
237 
238 	if (category == LC_CTYPE) {
239 		ret = setrunelocale(new) ? NULL : new;
240 		if (!ret)
241 			(void)setrunelocale(old);
242 		else
243 			(void)strcpy(old, new);
244 		return (ret);
245 	}
246 
247 #define LOAD_CATEGORY(CAT, FUNC)			\
248 	if (category == CAT) {				\
249 		ret = (FUNC(new) < 0) ? NULL : new;	\
250 		if (!ret)				\
251 			(void)FUNC(old);		\
252 		else					\
253 			(void)strcpy(old, new);		\
254 		return (ret);				\
255 	}
256 
257 	LOAD_CATEGORY(LC_COLLATE, __collate_load_tables);
258 	LOAD_CATEGORY(LC_TIME, __time_load_locale);
259 	LOAD_CATEGORY(LC_NUMERIC, __numeric_load_locale);
260 	LOAD_CATEGORY(LC_MONETARY, __monetary_load_locale);
261 	LOAD_CATEGORY(LC_MESSAGES, __messages_load_locale);
262 
263 	/* Just in case...*/
264 	return (NULL);
265 }
266 
267