]>
Commit | Line | Data |
---|---|---|
224c7076 A |
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: src/lib/libc/locale/setlocale.c,v 1.50 2004/01/31 19:15:32 ache Exp $"); | |
43 | ||
44 | #include "xlocale_private.h" | |
45 | ||
46 | #include <sys/types.h> | |
47 | #include <sys/stat.h> | |
48 | #include <errno.h> | |
49 | #include <limits.h> | |
50 | #include <locale.h> | |
51 | #include <paths.h> /* for _PATH_LOCALE */ | |
52 | #include <stdlib.h> | |
53 | #include <string.h> | |
54 | #include <unistd.h> | |
55 | #include "collate.h" | |
56 | #include "lmonetary.h" /* for __monetary_load_locale() */ | |
57 | #include "lnumeric.h" /* for __numeric_load_locale() */ | |
58 | #include "lmessages.h" /* for __messages_load_locale() */ | |
59 | #include "setlocale.h" | |
60 | #include "ldpart.h" | |
61 | #include "timelocal.h" /* for __time_load_locale() */ | |
62 | ||
63 | /* | |
64 | * Category names for getenv() | |
65 | */ | |
66 | static char *categories[_LC_LAST] = { | |
67 | "LC_ALL", | |
68 | "LC_COLLATE", | |
69 | "LC_CTYPE", | |
70 | "LC_MONETARY", | |
71 | "LC_NUMERIC", | |
72 | "LC_TIME", | |
73 | "LC_MESSAGES", | |
74 | }; | |
75 | ||
76 | /* | |
77 | * Current locales for each category | |
78 | */ | |
79 | static char current_categories[_LC_LAST][ENCODING_LEN + 1] = { | |
80 | "C", | |
81 | "C", | |
82 | "C", | |
83 | "C", | |
84 | "C", | |
85 | "C", | |
86 | "C", | |
87 | }; | |
88 | ||
89 | /* | |
90 | * Path to locale storage directory | |
91 | */ | |
92 | char *_PathLocale; | |
93 | ||
94 | /* | |
95 | * The locales we are going to try and load | |
96 | */ | |
97 | static char new_categories[_LC_LAST][ENCODING_LEN + 1]; | |
98 | static char saved_categories[_LC_LAST][ENCODING_LEN + 1]; | |
99 | ||
100 | static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)]; | |
101 | ||
102 | static char *currentlocale(void); | |
103 | static char *loadlocale(int); | |
104 | __private_extern__ const char *__get_locale_env(int); | |
105 | ||
106 | char * | |
107 | setlocale(category, locale) | |
108 | int category; | |
109 | const char *locale; | |
110 | { | |
111 | int i, j, len, saverr, save__numeric_fp_cvt; | |
112 | const char *env, *r; | |
113 | locale_t save__lc_numeric_loc; | |
114 | ||
115 | if (category < LC_ALL || category >= _LC_LAST) { | |
116 | errno = EINVAL; | |
117 | return (NULL); | |
118 | } | |
119 | ||
120 | if (locale == NULL) | |
121 | return (category != LC_ALL ? | |
122 | current_categories[category] : currentlocale()); | |
123 | ||
124 | /* | |
125 | * Default to the current locale for everything. | |
126 | */ | |
127 | for (i = 1; i < _LC_LAST; ++i) | |
128 | (void)strcpy(new_categories[i], current_categories[i]); | |
129 | ||
130 | /* | |
131 | * Now go fill up new_categories from the locale argument | |
132 | */ | |
133 | if (!*locale) { | |
134 | if (category == LC_ALL) { | |
135 | for (i = 1; i < _LC_LAST; ++i) { | |
136 | env = __get_locale_env(i); | |
137 | if (strlen(env) > ENCODING_LEN) { | |
138 | errno = EINVAL; | |
139 | return (NULL); | |
140 | } | |
141 | (void)strcpy(new_categories[i], env); | |
142 | } | |
143 | } else { | |
144 | env = __get_locale_env(category); | |
145 | if (strlen(env) > ENCODING_LEN) { | |
146 | errno = EINVAL; | |
147 | return (NULL); | |
148 | } | |
149 | (void)strcpy(new_categories[category], env); | |
150 | } | |
151 | } else if (category != LC_ALL) { | |
152 | if (strlen(locale) > ENCODING_LEN) { | |
153 | errno = EINVAL; | |
154 | return (NULL); | |
155 | } | |
156 | (void)strcpy(new_categories[category], locale); | |
157 | } else { | |
158 | if ((r = strchr(locale, '/')) == NULL) { | |
159 | if (strlen(locale) > ENCODING_LEN) { | |
160 | errno = EINVAL; | |
161 | return (NULL); | |
162 | } | |
163 | for (i = 1; i < _LC_LAST; ++i) | |
164 | (void)strcpy(new_categories[i], locale); | |
165 | } else { | |
166 | for (i = 1; r[1] == '/'; ++r) | |
167 | ; | |
168 | if (!r[1]) { | |
169 | errno = EINVAL; | |
170 | return (NULL); /* Hmm, just slashes... */ | |
171 | } | |
172 | do { | |
173 | if (i == _LC_LAST) | |
174 | break; /* Too many slashes... */ | |
175 | if ((len = r - locale) > ENCODING_LEN) { | |
176 | errno = EINVAL; | |
177 | return (NULL); | |
178 | } | |
179 | (void)strlcpy(new_categories[i], locale, | |
180 | len + 1); | |
181 | i++; | |
182 | while (*r == '/') | |
183 | r++; | |
184 | locale = r; | |
185 | while (*r && *r != '/') | |
186 | r++; | |
187 | } while (*locale); | |
188 | while (i < _LC_LAST) { | |
189 | (void)strcpy(new_categories[i], | |
190 | new_categories[i-1]); | |
191 | i++; | |
192 | } | |
193 | } | |
194 | } | |
195 | ||
196 | if (category != LC_ALL) | |
197 | return (loadlocale(category)); | |
198 | ||
199 | save__numeric_fp_cvt = __global_locale.__numeric_fp_cvt; | |
200 | save__lc_numeric_loc = __global_locale.__lc_numeric_loc; | |
201 | XL_RETAIN(save__lc_numeric_loc); | |
202 | for (i = 1; i < _LC_LAST; ++i) { | |
203 | (void)strcpy(saved_categories[i], current_categories[i]); | |
204 | if (loadlocale(i) == NULL) { | |
205 | saverr = errno; | |
206 | for (j = 1; j < i; j++) { | |
207 | (void)strcpy(new_categories[j], | |
208 | saved_categories[j]); | |
209 | if (loadlocale(j) == NULL) { | |
210 | (void)strcpy(new_categories[j], "C"); | |
211 | (void)loadlocale(j); | |
212 | } | |
213 | } | |
214 | __global_locale.__numeric_fp_cvt = save__numeric_fp_cvt; | |
215 | __global_locale.__lc_numeric_loc = save__lc_numeric_loc; | |
216 | XL_RELEASE(save__lc_numeric_loc); | |
217 | errno = saverr; | |
218 | return (NULL); | |
219 | } | |
220 | } | |
221 | XL_RELEASE(save__lc_numeric_loc); | |
222 | return (currentlocale()); | |
223 | } | |
224 | ||
225 | static char * | |
226 | currentlocale() | |
227 | { | |
228 | int i; | |
229 | ||
230 | (void)strcpy(current_locale_string, current_categories[1]); | |
231 | ||
232 | for (i = 2; i < _LC_LAST; ++i) | |
233 | if (strcmp(current_categories[1], current_categories[i])) { | |
234 | for (i = 2; i < _LC_LAST; ++i) { | |
235 | (void)strcat(current_locale_string, "/"); | |
236 | (void)strcat(current_locale_string, | |
237 | current_categories[i]); | |
238 | } | |
239 | break; | |
240 | } | |
241 | return (current_locale_string); | |
242 | } | |
243 | ||
244 | static char * | |
245 | loadlocale(category) | |
246 | int category; | |
247 | { | |
248 | char *new = new_categories[category]; | |
249 | char *old = current_categories[category]; | |
250 | int (*func)(const char *, locale_t); | |
251 | int saved_errno; | |
252 | ||
253 | if ((new[0] == '.' && | |
254 | (new[1] == '\0' || (new[1] == '.' && new[2] == '\0'))) || | |
255 | strchr(new, '/') != NULL) { | |
256 | errno = EINVAL; | |
257 | return (NULL); | |
258 | } | |
259 | ||
260 | saved_errno = errno; | |
261 | errno = __detect_path_locale(); | |
262 | if (errno != 0) | |
263 | return (NULL); | |
264 | errno = saved_errno; | |
265 | ||
266 | switch (category) { | |
267 | case LC_CTYPE: | |
268 | func = __wrap_setrunelocale; | |
269 | break; | |
270 | case LC_COLLATE: | |
271 | func = __collate_load_tables; | |
272 | break; | |
273 | case LC_TIME: | |
274 | func = __time_load_locale; | |
275 | break; | |
276 | case LC_NUMERIC: | |
277 | func = __numeric_load_locale; | |
278 | break; | |
279 | case LC_MONETARY: | |
280 | func = __monetary_load_locale; | |
281 | break; | |
282 | case LC_MESSAGES: | |
283 | func = __messages_load_locale; | |
284 | break; | |
285 | default: | |
286 | errno = EINVAL; | |
287 | return (NULL); | |
288 | } | |
289 | ||
290 | if (strcmp(new, old) == 0) | |
291 | return (old); | |
292 | ||
293 | if (func(new, &__global_locale) != _LDP_ERROR) { | |
294 | (void)strcpy(old, new); | |
295 | switch (category) { | |
296 | case LC_CTYPE: | |
297 | if (__global_locale.__numeric_fp_cvt == LC_NUMERIC_FP_SAME_LOCALE) | |
298 | __global_locale.__numeric_fp_cvt = LC_NUMERIC_FP_UNINITIALIZED; | |
299 | break; | |
300 | case LC_NUMERIC: | |
301 | __global_locale.__numeric_fp_cvt = LC_NUMERIC_FP_UNINITIALIZED; | |
302 | XL_RELEASE(__global_locale.__lc_numeric_loc); | |
303 | __global_locale.__lc_numeric_loc = NULL; | |
304 | break; | |
305 | } | |
306 | return (old); | |
307 | } | |
308 | ||
309 | return (NULL); | |
310 | } | |
311 | ||
312 | __private_extern__ const char * | |
313 | __get_locale_env(category) | |
314 | int category; | |
315 | { | |
316 | const char *env; | |
317 | ||
318 | /* 1. check LC_ALL. */ | |
319 | env = getenv(categories[0]); | |
320 | ||
321 | /* 2. check LC_* */ | |
322 | if (env == NULL || !*env) | |
323 | env = getenv(categories[category]); | |
324 | ||
325 | /* 3. check LANG */ | |
326 | if (env == NULL || !*env) | |
327 | env = getenv("LANG"); | |
328 | ||
329 | /* 4. if none is set, fall to "C" */ | |
330 | if (env == NULL || !*env) | |
331 | env = "C"; | |
332 | ||
333 | return (env); | |
334 | } | |
335 | ||
336 | /* | |
337 | * Detect locale storage location and store its value to _PathLocale variable | |
338 | */ | |
339 | __private_extern__ int | |
340 | __detect_path_locale(void) | |
341 | { | |
342 | if (_PathLocale == NULL) { | |
343 | char *p = getenv("PATH_LOCALE"); | |
344 | ||
345 | if (p != NULL && !issetugid()) { | |
346 | if (strlen(p) + 1/*"/"*/ + ENCODING_LEN + | |
347 | 1/*"/"*/ + CATEGORY_LEN >= PATH_MAX) | |
348 | return (ENAMETOOLONG); | |
349 | _PathLocale = strdup(p); | |
350 | if (_PathLocale == NULL) | |
351 | return (errno == 0 ? ENOMEM : errno); | |
352 | } else | |
353 | _PathLocale = _PATH_LOCALE; | |
354 | } | |
355 | return (0); | |
356 | } | |
357 |