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);
107 setlocale(category
, locale
)
111 int i
, j
, len
, saverr
, save__numeric_fp_cvt
;
113 locale_t save__lc_numeric_loc
;
115 if (category
< LC_ALL
|| category
>= _LC_LAST
) {
121 return (category
!= LC_ALL
?
122 current_categories
[category
] : currentlocale());
125 * Default to the current locale for everything.
127 for (i
= 1; i
< _LC_LAST
; ++i
)
128 (void)strcpy(new_categories
[i
], current_categories
[i
]);
131 * Now go fill up new_categories from the locale argument
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
) {
141 (void)strcpy(new_categories
[i
], env
);
144 env
= __get_locale_env(category
);
145 if (strlen(env
) > ENCODING_LEN
) {
149 (void)strcpy(new_categories
[category
], env
);
151 } else if (category
!= LC_ALL
) {
152 if (strlen(locale
) > ENCODING_LEN
) {
156 (void)strcpy(new_categories
[category
], locale
);
158 if ((r
= strchr(locale
, '/')) == NULL
) {
159 if (strlen(locale
) > ENCODING_LEN
) {
163 for (i
= 1; i
< _LC_LAST
; ++i
)
164 (void)strcpy(new_categories
[i
], locale
);
166 for (i
= 1; r
[1] == '/'; ++r
)
170 return (NULL
); /* Hmm, just slashes... */
174 break; /* Too many slashes... */
175 if ((len
= r
- locale
) > ENCODING_LEN
) {
179 (void)strlcpy(new_categories
[i
], locale
,
185 while (*r
&& *r
!= '/')
188 while (i
< _LC_LAST
) {
189 (void)strcpy(new_categories
[i
],
190 new_categories
[i
-1]);
196 if (category
!= LC_ALL
)
197 return (loadlocale(category
));
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
) {
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");
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
);
221 XL_RELEASE(save__lc_numeric_loc
);
222 return (currentlocale());
230 (void)strcpy(current_locale_string
, current_categories
[1]);
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
]);
241 return (current_locale_string
);
248 char *new = new_categories
[category
];
249 char *old
= current_categories
[category
];
250 int (*func
)(const char *, locale_t
);
253 if ((new[0] == '.' &&
254 (new[1] == '\0' || (new[1] == '.' && new[2] == '\0'))) ||
255 strchr(new, '/') != NULL
) {
261 errno
= __detect_path_locale();
268 func
= __wrap_setrunelocale
;
271 func
= __collate_load_tables
;
274 func
= __time_load_locale
;
277 func
= __numeric_load_locale
;
280 func
= __monetary_load_locale
;
283 func
= __messages_load_locale
;
290 if (strcmp(new, old
) == 0)
293 if (func(new, &__global_locale
) != _LDP_ERROR
) {
294 (void)strcpy(old
, new);
297 if (__global_locale
.__numeric_fp_cvt
== LC_NUMERIC_FP_SAME_LOCALE
)
298 __global_locale
.__numeric_fp_cvt
= LC_NUMERIC_FP_UNINITIALIZED
;
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
;
312 __private_extern__
const char *
313 __get_locale_env(category
)
318 /* 1. check LC_ALL. */
319 env
= getenv(categories
[0]);
322 if (env
== NULL
|| !*env
)
323 env
= getenv(categories
[category
]);
326 if (env
== NULL
|| !*env
)
327 env
= getenv("LANG");
329 /* 4. if none is set, fall to "C" */
330 if (env
== NULL
|| !*env
)
337 * Detect locale storage location and store its value to _PathLocale variable
339 __private_extern__
int
340 __detect_path_locale(void)
342 if (_PathLocale
== NULL
) {
343 char *p
= getenv("PATH_LOCALE");
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
);
353 _PathLocale
= _PATH_LOCALE
;