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