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 #if defined(LIBC_SCCS) && !defined(lint)
37 static char sccsid[] = "@(#)setlocale.c 8.1 (Berkeley) 7/4/93";
38 #endif /* LIBC_SCCS and not lint */
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/types.h>
47 #include <paths.h> /* for _PATH_LOCALE */
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 const char categories[_LC_LAST][12] = {
73 * Current locales for each category
75 static char current_categories[_LC_LAST][ENCODING_LEN + 1] = {
86 * Path to locale storage directory
91 * The locales we are going to try and load
93 static char new_categories[_LC_LAST][ENCODING_LEN + 1];
94 static char saved_categories[_LC_LAST][ENCODING_LEN + 1];
96 static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)];
98 static char *currentlocale(void);
99 static char *loadlocale(int);
100 const char *__get_locale_env(int);
103 setlocale(int category, const char *locale)
105 int i, j, len, saverr;
108 if (category < LC_ALL || category >= _LC_LAST) {
113 return (category != LC_ALL ?
114 current_categories[category] : currentlocale());
117 * Default to the current locale for everything.
119 for (i = 1; i < _LC_LAST; ++i)
120 (void)strcpy(new_categories[i], current_categories[i]);
123 * Now go fill up new_categories from the locale argument
126 if (category == LC_ALL) {
127 for (i = 1; i < _LC_LAST; ++i) {
128 env = __get_locale_env(i);
129 if (strlen(env) > ENCODING_LEN) {
133 (void)strcpy(new_categories[i], env);
136 env = __get_locale_env(category);
137 if (strlen(env) > ENCODING_LEN) {
141 (void)strcpy(new_categories[category], env);
143 } else if (category != LC_ALL) {
144 if (strlen(locale) > ENCODING_LEN) {
148 (void)strcpy(new_categories[category], locale);
150 if ((r = strchr(locale, '/')) == NULL) {
151 if (strlen(locale) > ENCODING_LEN) {
155 for (i = 1; i < _LC_LAST; ++i)
156 (void)strcpy(new_categories[i], locale);
158 for (i = 1; r[1] == '/'; ++r)
162 return (NULL); /* Hmm, just slashes... */
166 break; /* Too many slashes... */
167 if ((len = r - locale) > ENCODING_LEN) {
171 (void)strlcpy(new_categories[i], locale,
177 while (*r && *r != '/')
180 while (i < _LC_LAST) {
181 (void)strcpy(new_categories[i],
182 new_categories[i - 1]);
188 if (category != LC_ALL)
189 return (loadlocale(category));
191 for (i = 1; i < _LC_LAST; ++i) {
192 (void)strcpy(saved_categories[i], current_categories[i]);
193 if (loadlocale(i) == NULL) {
195 for (j = 1; j < i; j++) {
196 (void)strcpy(new_categories[j],
197 saved_categories[j]);
198 if (loadlocale(j) == NULL) {
199 (void)strcpy(new_categories[j], "C");
207 return (currentlocale());
215 (void)strcpy(current_locale_string, current_categories[1]);
217 for (i = 2; i < _LC_LAST; ++i)
218 if (strcmp(current_categories[1], current_categories[i])) {
219 for (i = 2; i < _LC_LAST; ++i) {
220 (void)strcat(current_locale_string, "/");
221 (void)strcat(current_locale_string,
222 current_categories[i]);
226 return (current_locale_string);
230 loadlocale(int category)
232 char *new = new_categories[category];
233 char *old = current_categories[category];
234 int (*func) (const char *);
237 if ((new[0] == '.' &&
238 (new[1] == '\0' || (new[1] == '.' && new[2] == '\0'))) ||
239 strchr(new, '/') != NULL) {
244 errno = __detect_path_locale();
251 func = __wrap_setrunelocale;
254 func = __collate_load_tables;
257 func = __time_load_locale;
260 func = __numeric_load_locale;
263 func = __monetary_load_locale;
266 func = __messages_load_locale;
273 if (strcmp(new, old) == 0)
276 if (func(new) != _LDP_ERROR) {
277 (void)strcpy(old, new);
278 (void)strcpy(__xlocale_global_locale.components[category-1]->locale, new);
286 __get_locale_env(int category)
290 /* 1. check LC_ALL. */
291 env = getenv(categories[0]);
294 if (env == NULL || !*env)
295 env = getenv(categories[category]);
298 if (env == NULL || !*env)
299 env = getenv("LANG");
301 /* 4. if none is set, fall to "C" */
302 if (env == NULL || !*env)
309 * Detect locale storage location and store its value to _PathLocale variable
312 __detect_path_locale(void)
314 if (_PathLocale == NULL) {
315 char *p = getenv("PATH_LOCALE");
317 if (p != NULL && !issetugid()) {
318 if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
319 1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
320 return (ENAMETOOLONG);
321 _PathLocale = strdup(p);
322 if (_PathLocale == NULL)
323 return (errno == 0 ? ENOMEM : errno);
325 _PathLocale = _PATH_LOCALE;