2 * Copyright (c) 1996 - 2002 FreeBSD Project
3 * Copyright (c) 1991, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Paul Borman at Krystal Technologies.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
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 $");
44 #include "xlocale_private.h"
46 #include <sys/types.h>
51 #include <paths.h> /* for _PATH_LOCALE */
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"
61 #include "timelocal.h" /* for __time_load_locale() */
64 * Category names for getenv()
66 static char *categories
[_LC_LAST
] = {
77 * Current locales for each category
79 static char current_categories
[_LC_LAST
][ENCODING_LEN
+ 1] = {
90 * Path to locale storage directory
95 * The locales we are going to try and load
97 static char new_categories
[_LC_LAST
][ENCODING_LEN
+ 1];
98 static char saved_categories
[_LC_LAST
][ENCODING_LEN
+ 1];
100 static char current_locale_string
[_LC_LAST
* (ENCODING_LEN
+ 1/*"/"*/ + 1)];
102 static char *currentlocale(void);
103 static char *loadlocale(int);
104 __private_extern__
const char *__get_locale_env(int);
106 #define UNLOCK_AND_RETURN(x) {XL_UNLOCK(&__global_locale); return (x);}
109 setlocale(category
, locale
)
113 int i
, j
, len
, saverr
, save__numeric_fp_cvt
;
115 locale_t save__lc_numeric_loc
;
117 if (category
< LC_ALL
|| category
>= _LC_LAST
) {
123 return (category
!= LC_ALL
?
124 current_categories
[category
] : currentlocale());
126 XL_LOCK(&__global_locale
);
128 * Default to the current locale for everything.
130 for (i
= 1; i
< _LC_LAST
; ++i
)
131 (void)strcpy(new_categories
[i
], current_categories
[i
]);
134 * Now go fill up new_categories from the locale argument
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
) {
142 UNLOCK_AND_RETURN (NULL
);
144 (void)strcpy(new_categories
[i
], env
);
147 env
= __get_locale_env(category
);
148 if (strlen(env
) > ENCODING_LEN
) {
150 UNLOCK_AND_RETURN (NULL
);
152 (void)strcpy(new_categories
[category
], env
);
154 } else if (category
!= LC_ALL
) {
155 if (strlen(locale
) > ENCODING_LEN
) {
157 UNLOCK_AND_RETURN (NULL
);
159 (void)strcpy(new_categories
[category
], locale
);
161 if ((r
= strchr(locale
, '/')) == NULL
) {
162 if (strlen(locale
) > ENCODING_LEN
) {
164 UNLOCK_AND_RETURN (NULL
);
166 for (i
= 1; i
< _LC_LAST
; ++i
)
167 (void)strcpy(new_categories
[i
], locale
);
169 for (i
= 1; r
[1] == '/'; ++r
)
173 UNLOCK_AND_RETURN (NULL
); /* Hmm, just slashes... */
177 break; /* Too many slashes... */
178 if ((len
= r
- locale
) > ENCODING_LEN
) {
180 UNLOCK_AND_RETURN (NULL
);
182 (void)strlcpy(new_categories
[i
], locale
,
188 while (*r
&& *r
!= '/')
191 while (i
< _LC_LAST
) {
192 (void)strcpy(new_categories
[i
],
193 new_categories
[i
-1]);
199 if (category
!= LC_ALL
)
200 UNLOCK_AND_RETURN (loadlocale(category
));
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
) {
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");
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
);
221 UNLOCK_AND_RETURN (NULL
);
224 XL_RELEASE(save__lc_numeric_loc
);
225 UNLOCK_AND_RETURN (currentlocale());
233 (void)strcpy(current_locale_string
, current_categories
[1]);
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
]);
244 return (current_locale_string
);
251 char *new = new_categories
[category
];
252 char *old
= current_categories
[category
];
253 int (*func
)(const char *, locale_t
);
256 if ((new[0] == '.' &&
257 (new[1] == '\0' || (new[1] == '.' && new[2] == '\0'))) ||
258 strchr(new, '/') != NULL
) {
264 errno
= __detect_path_locale();
271 func
= __wrap_setrunelocale
;
274 func
= __collate_load_tables
;
277 func
= __time_load_locale
;
280 func
= __numeric_load_locale
;
283 func
= __monetary_load_locale
;
286 func
= __messages_load_locale
;
293 if (strcmp(new, old
) == 0)
296 if (func(new, &__global_locale
) != _LDP_ERROR
) {
297 (void)strcpy(old
, new);
300 if (__global_locale
.__numeric_fp_cvt
== LC_NUMERIC_FP_SAME_LOCALE
)
301 __global_locale
.__numeric_fp_cvt
= LC_NUMERIC_FP_UNINITIALIZED
;
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
;
315 __private_extern__
const char *
316 __get_locale_env(category
)
321 /* 1. check LC_ALL. */
322 env
= getenv(categories
[0]);
325 if (env
== NULL
|| !*env
)
326 env
= getenv(categories
[category
]);
329 if (env
== NULL
|| !*env
)
330 env
= getenv("LANG");
332 /* 4. if none is set, fall to "C" */
333 if (env
== NULL
|| !*env
)
340 * Detect locale storage location and store its value to _PathLocale variable
342 __private_extern__
int
343 __detect_path_locale(void)
345 if (_PathLocale
== NULL
) {
346 char *p
= getenv("PATH_LOCALE");
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
);
356 _PathLocale
= _PATH_LOCALE
;