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 * 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.
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
33 * @(#)setlocale.c 8.1 (Berkeley) 7/4/93
34 * $FreeBSD: head/lib/libc/locale/setlocale.c 228921 2011-12-27 23:28:01Z jilles $
38 #include <sys/types.h>
43 #include <paths.h> /* for _PATH_LOCALE */
48 #include "lmonetary.h" /* for __monetary_load_locale() */
49 #include "lnumeric.h" /* for __numeric_load_locale() */
50 #include "lmessages.h" /* for __messages_load_locale() */
51 #include "setlocale.h"
53 #include "../stdtime/timelocal.h" /* for __time_load_locale() */
56 * Category names for getenv()
58 static const char categories[_LC_LAST][12] = {
69 * Current locales for each category
71 static char current_categories[_LC_LAST][ENCODING_LEN + 1] = {
82 * Path to locale storage directory
87 * The locales we are going to try and load
89 static char new_categories[_LC_LAST][ENCODING_LEN + 1];
90 static char saved_categories[_LC_LAST][ENCODING_LEN + 1];
92 static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)];
94 static char *currentlocale(void);
95 static char *loadlocale(int);
96 const char *__get_locale_env(int);
99 setlocale(int category, const char *locale)
101 int i, j, len, saverr;
104 if (category < LC_ALL || category >= _LC_LAST) {
110 return (category != LC_ALL ?
111 current_categories[category] : currentlocale());
114 * Default to the current locale for everything.
116 for (i = 1; i < _LC_LAST; ++i)
117 (void)strcpy(new_categories[i], current_categories[i]);
120 * Now go fill up new_categories from the locale argument
123 if (category == LC_ALL) {
124 for (i = 1; i < _LC_LAST; ++i) {
125 env = __get_locale_env(i);
126 if (strlen(env) > ENCODING_LEN) {
130 (void)strcpy(new_categories[i], env);
133 env = __get_locale_env(category);
134 if (strlen(env) > ENCODING_LEN) {
138 (void)strcpy(new_categories[category], env);
140 } else if (category != LC_ALL) {
141 if (strlen(locale) > ENCODING_LEN) {
145 (void)strcpy(new_categories[category], locale);
147 if ((r = strchr(locale, '/')) == NULL) {
148 if (strlen(locale) > ENCODING_LEN) {
152 for (i = 1; i < _LC_LAST; ++i)
153 (void)strcpy(new_categories[i], locale);
155 for (i = 1; r[1] == '/'; ++r)
159 return (NULL); /* Hmm, just slashes... */
163 break; /* Too many slashes... */
164 if ((len = r - locale) > ENCODING_LEN) {
168 (void)strlcpy(new_categories[i], locale,
174 while (*r && *r != '/')
177 while (i < _LC_LAST) {
178 (void)strcpy(new_categories[i],
179 new_categories[i-1]);
185 if (category != LC_ALL)
186 return (loadlocale(category));
188 for (i = 1; i < _LC_LAST; ++i) {
189 (void)strcpy(saved_categories[i], current_categories[i]);
190 if (loadlocale(i) == NULL) {
192 for (j = 1; j < i; j++) {
193 (void)strcpy(new_categories[j],
194 saved_categories[j]);
195 if (loadlocale(j) == NULL) {
196 (void)strcpy(new_categories[j], "C");
204 return (currentlocale());
212 (void)strcpy(current_locale_string, current_categories[1]);
214 for (i = 2; i < _LC_LAST; ++i)
215 if (strcmp(current_categories[1], current_categories[i])) {
216 for (i = 2; i < _LC_LAST; ++i) {
217 (void)strcat(current_locale_string, "/");
218 (void)strcat(current_locale_string,
219 current_categories[i]);
223 return (current_locale_string);
227 loadlocale(int category)
229 char *new = new_categories[category];
230 char *old = current_categories[category];
231 int (*func)(const char *);
234 if ((new[0] == '.' &&
235 (new[1] == '\0' || (new[1] == '.' && new[2] == '\0'))) ||
236 strchr(new, '/') != NULL) {
242 errno = __detect_path_locale();
249 func = __wrap_setrunelocale;
252 func = __collate_load_tables;
255 func = __time_load_locale;
258 func = __numeric_load_locale;
261 func = __monetary_load_locale;
264 func = __messages_load_locale;
271 if (strcmp(new, old) == 0)
274 if (func(new) != _LDP_ERROR) {
275 (void)strcpy(old, new);
276 (void)strcpy(__xlocale_global_locale.components[category-1]->locale, new);
284 __get_locale_env(int category)
288 /* 1. check LC_ALL. */
289 env = getenv(categories[0]);
292 if (env == NULL || !*env)
293 env = getenv(categories[category]);
296 if (env == NULL || !*env)
297 env = getenv("LANG");
299 /* 4. if none is set, fall to "C" */
300 if (env == NULL || !*env)
307 * Detect locale storage location and store its value to _PathLocale variable
310 __detect_path_locale(void)
312 if (_PathLocale == NULL) {
313 char *p = getenv("PATH_LOCALE");
315 if (p != NULL && !issetugid()) {
316 if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
317 1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
318 return (ENAMETOOLONG);
319 _PathLocale = strdup(p);
320 if (_PathLocale == NULL)
321 return (errno == 0 ? ENOMEM : errno);
323 _PathLocale = _PATH_LOCALE;