]> git.saurik.com Git - apple/libc.git/blob - locale/setlocale-fbsd.c
Libc-763.11.tar.gz
[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 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)setlocale.c 8.1 (Berkeley) 7/4/93";
36 #endif /* LIBC_SCCS and not lint */
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD: src/lib/libc/locale/setlocale.c,v 1.51 2007/01/09 00:28:00 imp Exp $");
39
40 #include "xlocale_private.h"
41
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <errno.h>
45 #include <limits.h>
46 #include <locale.h>
47 #include <paths.h> /* for _PATH_LOCALE */
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include "collate.h"
52 #include "lmonetary.h" /* for __monetary_load_locale() */
53 #include "lnumeric.h" /* for __numeric_load_locale() */
54 #include "lmessages.h" /* for __messages_load_locale() */
55 #include "setlocale.h"
56 #include "ldpart.h"
57 #include "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 * Path to locale storage directory
87 */
88 char *_PathLocale;
89
90 /*
91 * The locales we are going to try and load
92 */
93 static char new_categories[_LC_LAST][ENCODING_LEN + 1];
94 static char saved_categories[_LC_LAST][ENCODING_LEN + 1];
95
96 static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)];
97
98 static char *currentlocale(void);
99 static char *loadlocale(int);
100 __private_extern__ const char *__get_locale_env(int);
101
102 #define UNLOCK_AND_RETURN(x) {XL_UNLOCK(&__global_locale); return (x);}
103
104 char *
105 setlocale(category, locale)
106 int category;
107 const char *locale;
108 {
109 int i, j, len, saverr, save__numeric_fp_cvt;
110 const char *env, *r;
111 locale_t save__lc_numeric_loc;
112
113 if (category < LC_ALL || category >= _LC_LAST) {
114 errno = EINVAL;
115 return (NULL);
116 }
117
118 if (locale == NULL)
119 return (category != LC_ALL ?
120 current_categories[category] : currentlocale());
121
122 XL_LOCK(&__global_locale);
123 /*
124 * Default to the current locale for everything.
125 */
126 for (i = 1; i < _LC_LAST; ++i)
127 (void)strcpy(new_categories[i], current_categories[i]);
128
129 /*
130 * Now go fill up new_categories from the locale argument
131 */
132 if (!*locale) {
133 if (category == LC_ALL) {
134 for (i = 1; i < _LC_LAST; ++i) {
135 env = __get_locale_env(i);
136 if (strlen(env) > ENCODING_LEN) {
137 errno = EINVAL;
138 UNLOCK_AND_RETURN (NULL);
139 }
140 (void)strcpy(new_categories[i], env);
141 }
142 } else {
143 env = __get_locale_env(category);
144 if (strlen(env) > ENCODING_LEN) {
145 errno = EINVAL;
146 UNLOCK_AND_RETURN (NULL);
147 }
148 (void)strcpy(new_categories[category], env);
149 }
150 } else if (category != LC_ALL) {
151 if (strlen(locale) > ENCODING_LEN) {
152 errno = EINVAL;
153 UNLOCK_AND_RETURN (NULL);
154 }
155 (void)strcpy(new_categories[category], locale);
156 } else {
157 if ((r = strchr(locale, '/')) == NULL) {
158 if (strlen(locale) > ENCODING_LEN) {
159 errno = EINVAL;
160 UNLOCK_AND_RETURN (NULL);
161 }
162 for (i = 1; i < _LC_LAST; ++i)
163 (void)strcpy(new_categories[i], locale);
164 } else {
165 for (i = 1; r[1] == '/'; ++r)
166 ;
167 if (!r[1]) {
168 errno = EINVAL;
169 UNLOCK_AND_RETURN (NULL); /* Hmm, just slashes... */
170 }
171 do {
172 if (i == _LC_LAST)
173 break; /* Too many slashes... */
174 if ((len = r - locale) > ENCODING_LEN) {
175 errno = EINVAL;
176 UNLOCK_AND_RETURN (NULL);
177 }
178 (void)strlcpy(new_categories[i], locale,
179 len + 1);
180 i++;
181 while (*r == '/')
182 r++;
183 locale = r;
184 while (*r && *r != '/')
185 r++;
186 } while (*locale);
187 while (i < _LC_LAST) {
188 (void)strcpy(new_categories[i],
189 new_categories[i-1]);
190 i++;
191 }
192 }
193 }
194
195 if (category != LC_ALL)
196 UNLOCK_AND_RETURN (loadlocale(category));
197
198 save__numeric_fp_cvt = __global_locale.__numeric_fp_cvt;
199 save__lc_numeric_loc = __global_locale.__lc_numeric_loc;
200 XL_RETAIN(save__lc_numeric_loc);
201 for (i = 1; i < _LC_LAST; ++i) {
202 (void)strcpy(saved_categories[i], current_categories[i]);
203 if (loadlocale(i) == NULL) {
204 saverr = errno;
205 for (j = 1; j < i; j++) {
206 (void)strcpy(new_categories[j],
207 saved_categories[j]);
208 if (loadlocale(j) == NULL) {
209 (void)strcpy(new_categories[j], "C");
210 (void)loadlocale(j);
211 }
212 }
213 __global_locale.__numeric_fp_cvt = save__numeric_fp_cvt;
214 __global_locale.__lc_numeric_loc = save__lc_numeric_loc;
215 XL_RELEASE(save__lc_numeric_loc);
216 errno = saverr;
217 UNLOCK_AND_RETURN (NULL);
218 }
219 }
220 XL_RELEASE(save__lc_numeric_loc);
221 UNLOCK_AND_RETURN (currentlocale());
222 }
223
224 static char *
225 currentlocale()
226 {
227 int i;
228
229 (void)strcpy(current_locale_string, current_categories[1]);
230
231 for (i = 2; i < _LC_LAST; ++i)
232 if (strcmp(current_categories[1], current_categories[i])) {
233 for (i = 2; i < _LC_LAST; ++i) {
234 (void)strcat(current_locale_string, "/");
235 (void)strcat(current_locale_string,
236 current_categories[i]);
237 }
238 break;
239 }
240 return (current_locale_string);
241 }
242
243 static char *
244 loadlocale(category)
245 int category;
246 {
247 char *new = new_categories[category];
248 char *old = current_categories[category];
249 int (*func)(const char *, locale_t);
250 int saved_errno;
251
252 if ((new[0] == '.' &&
253 (new[1] == '\0' || (new[1] == '.' && new[2] == '\0'))) ||
254 strchr(new, '/') != NULL) {
255 errno = EINVAL;
256 return (NULL);
257 }
258
259 saved_errno = errno;
260 errno = __detect_path_locale();
261 if (errno != 0)
262 return (NULL);
263 errno = saved_errno;
264
265 switch (category) {
266 case LC_CTYPE:
267 func = __wrap_setrunelocale;
268 break;
269 case LC_COLLATE:
270 func = __collate_load_tables;
271 break;
272 case LC_TIME:
273 func = __time_load_locale;
274 break;
275 case LC_NUMERIC:
276 func = __numeric_load_locale;
277 break;
278 case LC_MONETARY:
279 func = __monetary_load_locale;
280 break;
281 case LC_MESSAGES:
282 func = __messages_load_locale;
283 break;
284 default:
285 errno = EINVAL;
286 return (NULL);
287 }
288
289 if (strcmp(new, old) == 0)
290 return (old);
291
292 if (func(new, &__global_locale) != _LDP_ERROR) {
293 (void)strcpy(old, new);
294 switch (category) {
295 case LC_CTYPE:
296 if (__global_locale.__numeric_fp_cvt == LC_NUMERIC_FP_SAME_LOCALE)
297 __global_locale.__numeric_fp_cvt = LC_NUMERIC_FP_UNINITIALIZED;
298 break;
299 case LC_NUMERIC:
300 __global_locale.__numeric_fp_cvt = LC_NUMERIC_FP_UNINITIALIZED;
301 XL_RELEASE(__global_locale.__lc_numeric_loc);
302 __global_locale.__lc_numeric_loc = NULL;
303 break;
304 }
305 return (old);
306 }
307
308 return (NULL);
309 }
310
311 __private_extern__ const char *
312 __get_locale_env(category)
313 int category;
314 {
315 const char *env;
316
317 /* 1. check LC_ALL. */
318 env = getenv(categories[0]);
319
320 /* 2. check LC_* */
321 if (env == NULL || !*env)
322 env = getenv(categories[category]);
323
324 /* 3. check LANG */
325 if (env == NULL || !*env)
326 env = getenv("LANG");
327
328 /* 4. if none is set, fall to "C" */
329 if (env == NULL || !*env)
330 env = "C";
331
332 return (env);
333 }
334
335 /*
336 * Detect locale storage location and store its value to _PathLocale variable
337 */
338 __private_extern__ int
339 __detect_path_locale(void)
340 {
341 if (_PathLocale == NULL) {
342 char *p = getenv("PATH_LOCALE");
343
344 if (p != NULL && !issetugid()) {
345 if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
346 1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
347 return (ENAMETOOLONG);
348 _PathLocale = strdup(p);
349 if (_PathLocale == NULL)
350 return (errno == 0 ? ENOMEM : errno);
351 } else
352 _PathLocale = _PATH_LOCALE;
353 }
354 return (0);
355 }
356