]> git.saurik.com Git - apple/libc.git/blob - locale/setlocale-fbsd.c
61909b958a3e8e5ed185b55a1db19aa1a459f087
[apple/libc.git] / locale / setlocale-fbsd.c
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 #define UNLOCK_AND_RETURN(x) {XL_UNLOCK(&__global_locale); return (x);}
107
108 char *
109 setlocale(category, locale)
110 int category;
111 const char *locale;
112 {
113 int i, j, len, saverr, save__numeric_fp_cvt;
114 const char *env, *r;
115 locale_t save__lc_numeric_loc;
116
117 if (category < LC_ALL || category >= _LC_LAST) {
118 errno = EINVAL;
119 return (NULL);
120 }
121
122 if (locale == NULL)
123 return (category != LC_ALL ?
124 current_categories[category] : currentlocale());
125
126 XL_LOCK(&__global_locale);
127 /*
128 * Default to the current locale for everything.
129 */
130 for (i = 1; i < _LC_LAST; ++i)
131 (void)strcpy(new_categories[i], current_categories[i]);
132
133 /*
134 * Now go fill up new_categories from the locale argument
135 */
136 if (!*locale) {
137 if (category == LC_ALL) {
138 for (i = 1; i < _LC_LAST; ++i) {
139 env = __get_locale_env(i);
140 if (strlen(env) > ENCODING_LEN) {
141 errno = EINVAL;
142 UNLOCK_AND_RETURN (NULL);
143 }
144 (void)strcpy(new_categories[i], env);
145 }
146 } else {
147 env = __get_locale_env(category);
148 if (strlen(env) > ENCODING_LEN) {
149 errno = EINVAL;
150 UNLOCK_AND_RETURN (NULL);
151 }
152 (void)strcpy(new_categories[category], env);
153 }
154 } else if (category != LC_ALL) {
155 if (strlen(locale) > ENCODING_LEN) {
156 errno = EINVAL;
157 UNLOCK_AND_RETURN (NULL);
158 }
159 (void)strcpy(new_categories[category], locale);
160 } else {
161 if ((r = strchr(locale, '/')) == NULL) {
162 if (strlen(locale) > ENCODING_LEN) {
163 errno = EINVAL;
164 UNLOCK_AND_RETURN (NULL);
165 }
166 for (i = 1; i < _LC_LAST; ++i)
167 (void)strcpy(new_categories[i], locale);
168 } else {
169 for (i = 1; r[1] == '/'; ++r)
170 ;
171 if (!r[1]) {
172 errno = EINVAL;
173 UNLOCK_AND_RETURN (NULL); /* Hmm, just slashes... */
174 }
175 do {
176 if (i == _LC_LAST)
177 break; /* Too many slashes... */
178 if ((len = r - locale) > ENCODING_LEN) {
179 errno = EINVAL;
180 UNLOCK_AND_RETURN (NULL);
181 }
182 (void)strlcpy(new_categories[i], locale,
183 len + 1);
184 i++;
185 while (*r == '/')
186 r++;
187 locale = r;
188 while (*r && *r != '/')
189 r++;
190 } while (*locale);
191 while (i < _LC_LAST) {
192 (void)strcpy(new_categories[i],
193 new_categories[i-1]);
194 i++;
195 }
196 }
197 }
198
199 if (category != LC_ALL)
200 UNLOCK_AND_RETURN (loadlocale(category));
201
202 save__numeric_fp_cvt = __global_locale.__numeric_fp_cvt;
203 save__lc_numeric_loc = __global_locale.__lc_numeric_loc;
204 XL_RETAIN(save__lc_numeric_loc);
205 for (i = 1; i < _LC_LAST; ++i) {
206 (void)strcpy(saved_categories[i], current_categories[i]);
207 if (loadlocale(i) == NULL) {
208 saverr = errno;
209 for (j = 1; j < i; j++) {
210 (void)strcpy(new_categories[j],
211 saved_categories[j]);
212 if (loadlocale(j) == NULL) {
213 (void)strcpy(new_categories[j], "C");
214 (void)loadlocale(j);
215 }
216 }
217 __global_locale.__numeric_fp_cvt = save__numeric_fp_cvt;
218 __global_locale.__lc_numeric_loc = save__lc_numeric_loc;
219 XL_RELEASE(save__lc_numeric_loc);
220 errno = saverr;
221 UNLOCK_AND_RETURN (NULL);
222 }
223 }
224 XL_RELEASE(save__lc_numeric_loc);
225 UNLOCK_AND_RETURN (currentlocale());
226 }
227
228 static char *
229 currentlocale()
230 {
231 int i;
232
233 (void)strcpy(current_locale_string, current_categories[1]);
234
235 for (i = 2; i < _LC_LAST; ++i)
236 if (strcmp(current_categories[1], current_categories[i])) {
237 for (i = 2; i < _LC_LAST; ++i) {
238 (void)strcat(current_locale_string, "/");
239 (void)strcat(current_locale_string,
240 current_categories[i]);
241 }
242 break;
243 }
244 return (current_locale_string);
245 }
246
247 static char *
248 loadlocale(category)
249 int category;
250 {
251 char *new = new_categories[category];
252 char *old = current_categories[category];
253 int (*func)(const char *, locale_t);
254 int saved_errno;
255
256 if ((new[0] == '.' &&
257 (new[1] == '\0' || (new[1] == '.' && new[2] == '\0'))) ||
258 strchr(new, '/') != NULL) {
259 errno = EINVAL;
260 return (NULL);
261 }
262
263 saved_errno = errno;
264 errno = __detect_path_locale();
265 if (errno != 0)
266 return (NULL);
267 errno = saved_errno;
268
269 switch (category) {
270 case LC_CTYPE:
271 func = __wrap_setrunelocale;
272 break;
273 case LC_COLLATE:
274 func = __collate_load_tables;
275 break;
276 case LC_TIME:
277 func = __time_load_locale;
278 break;
279 case LC_NUMERIC:
280 func = __numeric_load_locale;
281 break;
282 case LC_MONETARY:
283 func = __monetary_load_locale;
284 break;
285 case LC_MESSAGES:
286 func = __messages_load_locale;
287 break;
288 default:
289 errno = EINVAL;
290 return (NULL);
291 }
292
293 if (strcmp(new, old) == 0)
294 return (old);
295
296 if (func(new, &__global_locale) != _LDP_ERROR) {
297 (void)strcpy(old, new);
298 switch (category) {
299 case LC_CTYPE:
300 if (__global_locale.__numeric_fp_cvt == LC_NUMERIC_FP_SAME_LOCALE)
301 __global_locale.__numeric_fp_cvt = LC_NUMERIC_FP_UNINITIALIZED;
302 break;
303 case LC_NUMERIC:
304 __global_locale.__numeric_fp_cvt = LC_NUMERIC_FP_UNINITIALIZED;
305 XL_RELEASE(__global_locale.__lc_numeric_loc);
306 __global_locale.__lc_numeric_loc = NULL;
307 break;
308 }
309 return (old);
310 }
311
312 return (NULL);
313 }
314
315 __private_extern__ const char *
316 __get_locale_env(category)
317 int category;
318 {
319 const char *env;
320
321 /* 1. check LC_ALL. */
322 env = getenv(categories[0]);
323
324 /* 2. check LC_* */
325 if (env == NULL || !*env)
326 env = getenv(categories[category]);
327
328 /* 3. check LANG */
329 if (env == NULL || !*env)
330 env = getenv("LANG");
331
332 /* 4. if none is set, fall to "C" */
333 if (env == NULL || !*env)
334 env = "C";
335
336 return (env);
337 }
338
339 /*
340 * Detect locale storage location and store its value to _PathLocale variable
341 */
342 __private_extern__ int
343 __detect_path_locale(void)
344 {
345 if (_PathLocale == NULL) {
346 char *p = getenv("PATH_LOCALE");
347
348 if (p != NULL && !issetugid()) {
349 if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
350 1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
351 return (ENAMETOOLONG);
352 _PathLocale = strdup(p);
353 if (_PathLocale == NULL)
354 return (errno == 0 ? ENOMEM : errno);
355 } else
356 _PathLocale = _PATH_LOCALE;
357 }
358 return (0);
359 }
360