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
37 * @(#)setlocale.c 8.1 (Berkeley) 7/4/93
38 * $FreeBSD: src/lib/libc/locale/setlocale.c,v 1.25.2.8 2002/08/12 11:17:38 ache Exp $
39 * $DragonFly: src/lib/libc/locale/setlocale.c,v 1.2 2003/06/17 04:26:44 dillon Exp $
42 #include <sys/types.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"
57 #include "../stdtime/timelocal.h" /* for __time_load_locale() */
60 * Category names for getenv()
62 static char *categories[_LC_LAST] = {
73 * Current locales for each category
75 static char current_categories[_LC_LAST][ENCODING_LEN + 1] = {
86 * The locales we are going to try and load
88 static char new_categories[_LC_LAST][ENCODING_LEN + 1];
89 static char saved_categories[_LC_LAST][ENCODING_LEN + 1];
91 static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)];
93 static char *currentlocale(void);
94 static int wrap_setrunelocale(const char *);
95 static char *loadlocale(int);
98 setlocale(category, locale)
102 int i, j, len, saverr;
105 if (category < LC_ALL || category >= _LC_LAST) {
111 return (category != LC_ALL ?
112 current_categories[category] : currentlocale());
115 * Default to the current locale for everything.
117 for (i = 1; i < _LC_LAST; ++i)
118 (void)strcpy(new_categories[i], current_categories[i]);
121 * Now go fill up new_categories from the locale argument
124 env = getenv("LC_ALL");
126 if (category != LC_ALL && (env == NULL || !*env))
127 env = getenv(categories[category]);
129 if (env == NULL || !*env)
130 env = getenv("LANG");
132 if (env == NULL || !*env)
135 if (strlen(env) > ENCODING_LEN) {
139 (void)strcpy(new_categories[category], env);
141 if (category == LC_ALL) {
142 for (i = 1; i < _LC_LAST; ++i) {
143 if ((env = getenv(categories[i])) == NULL ||
145 env = new_categories[LC_ALL];
146 else if (strlen(env) > ENCODING_LEN) {
150 (void)strcpy(new_categories[i], env);
153 } else if (category != LC_ALL) {
154 if (strlen(locale) > ENCODING_LEN) {
158 (void)strcpy(new_categories[category], locale);
160 if ((r = strchr(locale, '/')) == NULL) {
161 if (strlen(locale) > ENCODING_LEN) {
165 for (i = 1; i < _LC_LAST; ++i)
166 (void)strcpy(new_categories[i], locale);
168 for (i = 1; r[1] == '/'; ++r)
172 return (NULL); /* Hmm, just slashes... */
176 break; /* Too many slashes... */
177 if ((len = r - locale) > ENCODING_LEN) {
181 (void)strlcpy(new_categories[i], locale,
185 while (*locale == '/')
187 while (*++r && *r != '/')
190 while (i < _LC_LAST) {
191 (void)strcpy(new_categories[i],
192 new_categories[i-1]);
198 if (category != LC_ALL)
199 return (loadlocale(category));
201 for (i = 1; i < _LC_LAST; ++i) {
202 (void)strcpy(saved_categories[i], current_categories[i]);
203 if (loadlocale(i) == NULL) {
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");
217 return (currentlocale());
225 (void)strcpy(current_locale_string, current_categories[1]);
227 for (i = 2; i < _LC_LAST; ++i)
228 if (strcmp(current_categories[1], current_categories[i])) {
229 for (i = 2; i < _LC_LAST; ++i) {
230 (void)strcat(current_locale_string, "/");
231 (void)strcat(current_locale_string,
232 current_categories[i]);
236 return (current_locale_string);
240 wrap_setrunelocale(const char *locale)
242 int ret = setrunelocale((char *)locale);
248 return (_LDP_LOADED);
255 char *new = new_categories[category];
256 char *old = current_categories[category];
257 int (*func)(const char *);
259 if ((new[0] == '.' &&
260 (new[1] == '\0' || (new[1] == '.' && new[2] == '\0'))) ||
261 strchr(new, '/') != NULL) {
266 if (_PathLocale == NULL) {
267 char *p = getenv("PATH_LOCALE");
270 #ifndef __NETBSD_SYSCALLS
274 if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
275 1/*"/"*/ + CATEGORY_LEN >= PATH_MAX) {
276 errno = ENAMETOOLONG;
279 _PathLocale = strdup(p);
280 if (_PathLocale == NULL) {
285 _PathLocale = _PATH_LOCALE;
290 func = wrap_setrunelocale;
293 func = __collate_load_tables;
296 func = __time_load_locale;
299 func = __numeric_load_locale;
302 func = __monetary_load_locale;
305 func = __messages_load_locale;
312 if (strcmp(new, old) == 0)
315 if (func(new) != _LDP_ERROR) {
316 (void)strcpy(old, new);