2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1996 - 2002 FreeBSD Project
5 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Paul Borman at Krystal Technologies.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/types.h>
41 #include <paths.h> /* for _PATH_LOCALE */
46 #include "lmonetary.h" /* for __monetary_load_locale() */
47 #include "lnumeric.h" /* for __numeric_load_locale() */
48 #include "lmessages.h" /* for __messages_load_locale() */
49 #include "setlocale.h"
51 #include "../stdtime/timelocal.h" /* for __time_load_locale() */
54 * Category names for getenv()
56 static const char categories[_LC_LAST][12] = {
67 * Current locales for each category
69 static char current_categories[_LC_LAST][ENCODING_LEN + 1] = {
80 * Path to locale storage directory
85 * The locales we are going to try and load
87 static char new_categories[_LC_LAST][ENCODING_LEN + 1];
88 static char saved_categories[_LC_LAST][ENCODING_LEN + 1];
90 static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)];
92 static char *currentlocale(void);
93 static char *loadlocale(int);
94 const char *__get_locale_env(int);
97 setlocale(int category, const char *locale)
99 int i, j, len, saverr;
102 if (category < LC_ALL || category >= _LC_LAST) {
107 return (category != LC_ALL ?
108 current_categories[category] : currentlocale());
111 * Default to the current locale for everything.
113 for (i = 1; i < _LC_LAST; ++i)
114 (void)strcpy(new_categories[i], current_categories[i]);
117 * Now go fill up new_categories from the locale argument
120 if (category == LC_ALL) {
121 for (i = 1; i < _LC_LAST; ++i) {
122 env = __get_locale_env(i);
123 if (strlen(env) > ENCODING_LEN) {
127 (void)strcpy(new_categories[i], env);
130 env = __get_locale_env(category);
131 if (strlen(env) > ENCODING_LEN) {
135 (void)strcpy(new_categories[category], env);
137 } else if (category != LC_ALL) {
138 if (strlen(locale) > ENCODING_LEN) {
142 (void)strcpy(new_categories[category], locale);
144 if ((r = strchr(locale, '/')) == NULL) {
145 if (strlen(locale) > ENCODING_LEN) {
149 for (i = 1; i < _LC_LAST; ++i)
150 (void)strcpy(new_categories[i], locale);
152 for (i = 1; r[1] == '/'; ++r)
156 return (NULL); /* Hmm, just slashes... */
160 break; /* Too many slashes... */
161 if ((len = r - locale) > ENCODING_LEN) {
165 (void)strlcpy(new_categories[i], locale,
171 while (*r && *r != '/')
174 while (i < _LC_LAST) {
175 (void)strcpy(new_categories[i],
176 new_categories[i - 1]);
182 if (category != LC_ALL)
183 return (loadlocale(category));
185 for (i = 1; i < _LC_LAST; ++i) {
186 (void)strcpy(saved_categories[i], current_categories[i]);
187 if (loadlocale(i) == NULL) {
189 for (j = 1; j < i; j++) {
190 (void)strcpy(new_categories[j],
191 saved_categories[j]);
192 if (loadlocale(j) == NULL) {
193 (void)strcpy(new_categories[j], "C");
201 return (currentlocale());
209 (void)strcpy(current_locale_string, current_categories[1]);
211 for (i = 2; i < _LC_LAST; ++i)
212 if (strcmp(current_categories[1], current_categories[i])) {
213 for (i = 2; i < _LC_LAST; ++i) {
214 (void)strcat(current_locale_string, "/");
215 (void)strcat(current_locale_string,
216 current_categories[i]);
220 return (current_locale_string);
224 loadlocale(int category)
226 char *new = new_categories[category];
227 char *old = current_categories[category];
228 int (*func) (const char *);
231 if ((new[0] == '.' &&
232 (new[1] == '\0' || (new[1] == '.' && new[2] == '\0'))) ||
233 strchr(new, '/') != NULL) {
238 errno = __detect_path_locale();
245 func = __wrap_setrunelocale;
248 func = __collate_load_tables;
251 func = __time_load_locale;
254 func = __numeric_load_locale;
257 func = __monetary_load_locale;
260 func = __messages_load_locale;
267 if (strcmp(new, old) == 0)
270 if (func(new) != _LDP_ERROR) {
271 (void)strcpy(old, new);
272 (void)strcpy(__xlocale_global_locale.components[category-1]->locale, new);
280 __get_locale_env(int category)
284 /* 1. check LC_ALL. */
285 env = getenv(categories[0]);
288 if (env == NULL || !*env)
289 env = getenv(categories[category]);
292 if (env == NULL || !*env)
293 env = getenv("LANG");
295 /* 4. if none is set, fall to "C" */
296 if (env == NULL || !*env)
303 * Detect locale storage location and store its value to _PathLocale variable
306 __detect_path_locale(void)
308 if (_PathLocale == NULL) {
309 char *p = secure_getenv("PATH_LOCALE");
312 if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
313 1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
314 return (ENAMETOOLONG);
315 _PathLocale = strdup(p);
316 if (_PathLocale == NULL)
317 return (errno == 0 ? ENOMEM : errno);
319 _PathLocale = _PATH_LOCALE;