1994-05-27 05:00:24 +00:00
|
|
|
/*
|
2002-01-24 15:38:59 +00:00
|
|
|
* Copyright (c) 1996 - 2002 FreeBSD Project
|
1994-05-27 05:00:24 +00:00
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Paul Borman at Krystal Technologies.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1996-11-27 22:30:44 +00:00
|
|
|
#ifdef LIBC_RCS
|
|
|
|
static const char rcsid[] =
|
1999-08-28 00:22:10 +00:00
|
|
|
"$FreeBSD$";
|
1996-11-27 22:30:44 +00:00
|
|
|
#endif
|
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
|
|
|
static char sccsid[] = "@(#)setlocale.c 8.1 (Berkeley) 7/4/93";
|
|
|
|
#endif /* LIBC_SCCS and not lint */
|
|
|
|
|
1996-11-27 22:30:44 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
1994-05-27 05:00:24 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <rune.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1996-11-27 22:30:44 +00:00
|
|
|
#include <unistd.h>
|
1995-10-23 01:34:17 +00:00
|
|
|
#include "collate.h"
|
2001-02-08 16:58:53 +00:00
|
|
|
#include "lmonetary.h" /* for __monetary_load_locale() */
|
|
|
|
#include "lnumeric.h" /* for __numeric_load_locale() */
|
|
|
|
#include "lmessages.h" /* for __messages_load_locale() */
|
1997-02-06 09:11:06 +00:00
|
|
|
#include "setlocale.h"
|
2002-01-24 15:38:59 +00:00
|
|
|
#include "../stdtime/timelocal.h" /* for __time_load_locale() */
|
1994-05-27 05:00:24 +00:00
|
|
|
|
1995-10-23 01:34:17 +00:00
|
|
|
/*
|
|
|
|
* Category names for getenv()
|
|
|
|
*/
|
|
|
|
static char *categories[_LC_LAST] = {
|
|
|
|
"LC_ALL",
|
|
|
|
"LC_COLLATE",
|
|
|
|
"LC_CTYPE",
|
|
|
|
"LC_MONETARY",
|
|
|
|
"LC_NUMERIC",
|
|
|
|
"LC_TIME",
|
1998-04-29 22:39:56 +00:00
|
|
|
"LC_MESSAGES",
|
1995-10-23 01:34:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Current locales for each category
|
|
|
|
*/
|
1997-02-06 09:11:06 +00:00
|
|
|
static char current_categories[_LC_LAST][ENCODING_LEN + 1] = {
|
1995-10-23 01:34:17 +00:00
|
|
|
"C",
|
|
|
|
"C",
|
|
|
|
"C",
|
|
|
|
"C",
|
|
|
|
"C",
|
|
|
|
"C",
|
1998-04-29 22:39:56 +00:00
|
|
|
"C",
|
1995-10-23 01:34:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The locales we are going to try and load
|
|
|
|
*/
|
1997-02-06 09:11:06 +00:00
|
|
|
static char new_categories[_LC_LAST][ENCODING_LEN + 1];
|
|
|
|
static char saved_categories[_LC_LAST][ENCODING_LEN + 1];
|
1995-10-23 01:34:17 +00:00
|
|
|
|
1997-02-06 09:11:06 +00:00
|
|
|
static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)];
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2002-03-21 22:49:10 +00:00
|
|
|
static char *currentlocale(void);
|
|
|
|
static char *loadlocale(int);
|
1994-05-27 05:00:24 +00:00
|
|
|
|
|
|
|
char *
|
|
|
|
setlocale(category, locale)
|
|
|
|
int category;
|
|
|
|
const char *locale;
|
|
|
|
{
|
1996-11-26 02:49:53 +00:00
|
|
|
int i, j, len;
|
1994-05-27 05:00:24 +00:00
|
|
|
char *env, *r;
|
|
|
|
|
1996-11-26 02:49:53 +00:00
|
|
|
if (category < LC_ALL || category >= _LC_LAST)
|
1994-05-27 05:00:24 +00:00
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (!locale)
|
1996-11-26 02:49:53 +00:00
|
|
|
return (category != LC_ALL ?
|
1994-05-27 05:00:24 +00:00
|
|
|
current_categories[category] : currentlocale());
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Default to the current locale for everything.
|
|
|
|
*/
|
1996-11-26 08:00:17 +00:00
|
|
|
for (i = 1; i < _LC_LAST; ++i)
|
1994-05-27 05:00:24 +00:00
|
|
|
(void)strcpy(new_categories[i], current_categories[i]);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now go fill up new_categories from the locale argument
|
|
|
|
*/
|
|
|
|
if (!*locale) {
|
Fix setlocale() to conform to the ISO C and POSIX standards.
The below text is quoted from the latest POSIX draft:
: The values of locale categories shall be determined by a precedence
: order; the first condition met below determines the value:
:
: 1. If the LC_ALL environment variable is defined and is not null,
: the value of LC_ALL shall be used.
: 2. If the LC_* environment variable (LC_COLLATE, LC_CTYPE, LC_MESSAGES,
: LC_MONETARY, LC_NUMERIC, LC_TIME) is defined and is not null, the
: value of the environment variable shall be used to initialize the
: category that corresponds to the environment variable.
: 3. If the LANG environment variable is defined and is not null, the
: value of the LANG environment variable shall be used.
: 4. If the LANG environment variable is not set or is set to the empty
: string, the implementation-defined default locale shall be used.
The conditions 1 and 2 were interchanged, i.e., LC_* were looked first,
then LC_ALL, then LANG (note that LC_ALL and LANG were essentially the
same, providing the default, with LC_ALL taking precedence over LANG).
Now, LC_ALL and LANG serve the different purposes. LC_ALL overrides
any LC_*, and LANG provides the default fallback.
Testcase:
/usr/bin/env LC_ALL=C LC_TIME=de_DE.ISO_8859-1 /bin/date
Should return date in the "C" locale format.
Inspired by: date(1) reference page in the Draft
2001-03-02 12:45:52 +00:00
|
|
|
env = getenv("LC_ALL");
|
1994-05-27 05:00:24 +00:00
|
|
|
|
1996-11-26 02:49:53 +00:00
|
|
|
if (category != LC_ALL && (!env || !*env))
|
Fix setlocale() to conform to the ISO C and POSIX standards.
The below text is quoted from the latest POSIX draft:
: The values of locale categories shall be determined by a precedence
: order; the first condition met below determines the value:
:
: 1. If the LC_ALL environment variable is defined and is not null,
: the value of LC_ALL shall be used.
: 2. If the LC_* environment variable (LC_COLLATE, LC_CTYPE, LC_MESSAGES,
: LC_MONETARY, LC_NUMERIC, LC_TIME) is defined and is not null, the
: value of the environment variable shall be used to initialize the
: category that corresponds to the environment variable.
: 3. If the LANG environment variable is defined and is not null, the
: value of the LANG environment variable shall be used.
: 4. If the LANG environment variable is not set or is set to the empty
: string, the implementation-defined default locale shall be used.
The conditions 1 and 2 were interchanged, i.e., LC_* were looked first,
then LC_ALL, then LANG (note that LC_ALL and LANG were essentially the
same, providing the default, with LC_ALL taking precedence over LANG).
Now, LC_ALL and LANG serve the different purposes. LC_ALL overrides
any LC_*, and LANG provides the default fallback.
Testcase:
/usr/bin/env LC_ALL=C LC_TIME=de_DE.ISO_8859-1 /bin/date
Should return date in the "C" locale format.
Inspired by: date(1) reference page in the Draft
2001-03-02 12:45:52 +00:00
|
|
|
env = getenv(categories[category]);
|
1994-05-27 05:00:24 +00:00
|
|
|
|
1996-11-26 02:49:53 +00:00
|
|
|
if (!env || !*env)
|
1994-05-27 05:00:24 +00:00
|
|
|
env = getenv("LANG");
|
|
|
|
|
2000-09-08 07:29:48 +00:00
|
|
|
if (!env || !*env || strchr(env, '/'))
|
1994-05-27 05:00:24 +00:00
|
|
|
env = "C";
|
|
|
|
|
1997-02-06 09:11:06 +00:00
|
|
|
(void) strncpy(new_categories[category], env, ENCODING_LEN);
|
|
|
|
new_categories[category][ENCODING_LEN] = '\0';
|
1996-11-26 02:49:53 +00:00
|
|
|
if (category == LC_ALL) {
|
1994-05-27 05:00:24 +00:00
|
|
|
for (i = 1; i < _LC_LAST; ++i) {
|
1996-11-26 02:49:53 +00:00
|
|
|
if (!(env = getenv(categories[i])) || !*env)
|
|
|
|
env = new_categories[LC_ALL];
|
1997-02-06 09:11:06 +00:00
|
|
|
(void)strncpy(new_categories[i], env, ENCODING_LEN);
|
|
|
|
new_categories[i][ENCODING_LEN] = '\0';
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
}
|
1996-11-26 02:49:53 +00:00
|
|
|
} else if (category != LC_ALL) {
|
1997-02-06 09:11:06 +00:00
|
|
|
(void)strncpy(new_categories[category], locale, ENCODING_LEN);
|
|
|
|
new_categories[category][ENCODING_LEN] = '\0';
|
1994-05-27 05:00:24 +00:00
|
|
|
} else {
|
1997-02-06 09:11:06 +00:00
|
|
|
if ((r = strchr(locale, '/')) == NULL) {
|
1994-05-27 05:00:24 +00:00
|
|
|
for (i = 1; i < _LC_LAST; ++i) {
|
1997-02-06 09:11:06 +00:00
|
|
|
(void)strncpy(new_categories[i], locale, ENCODING_LEN);
|
|
|
|
new_categories[i][ENCODING_LEN] = '\0';
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 1; r[1] == '/'; ++r);
|
|
|
|
if (!r[1])
|
|
|
|
return (NULL); /* Hmm, just slashes... */
|
|
|
|
do {
|
1997-02-06 09:11:06 +00:00
|
|
|
len = r - locale > ENCODING_LEN ? ENCODING_LEN : r - locale;
|
1997-02-06 08:31:42 +00:00
|
|
|
(void)strncpy(new_categories[i], locale, len);
|
1997-02-06 09:11:06 +00:00
|
|
|
new_categories[i][len] = '\0';
|
|
|
|
i++;
|
1994-05-27 05:00:24 +00:00
|
|
|
locale = r;
|
|
|
|
while (*locale == '/')
|
|
|
|
++locale;
|
|
|
|
while (*++r && *r != '/');
|
|
|
|
} while (*locale);
|
1999-11-09 11:09:16 +00:00
|
|
|
while (i < _LC_LAST) {
|
1994-05-27 05:00:24 +00:00
|
|
|
(void)strcpy(new_categories[i],
|
|
|
|
new_categories[i-1]);
|
1999-11-09 11:09:16 +00:00
|
|
|
i++;
|
|
|
|
}
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-09-04 03:43:24 +00:00
|
|
|
if (category != LC_ALL)
|
1996-11-26 08:00:17 +00:00
|
|
|
return (loadlocale(category));
|
1994-05-27 05:00:24 +00:00
|
|
|
|
1996-11-26 08:00:17 +00:00
|
|
|
for (i = 1; i < _LC_LAST; ++i) {
|
|
|
|
(void)strcpy(saved_categories[i], current_categories[i]);
|
1996-11-26 02:49:53 +00:00
|
|
|
if (loadlocale(i) == NULL) {
|
|
|
|
for (j = 1; j < i; j++) {
|
|
|
|
(void)strcpy(new_categories[j],
|
|
|
|
saved_categories[j]);
|
|
|
|
/* XXX can fail too */
|
|
|
|
(void)loadlocale(j);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
1996-11-26 08:00:17 +00:00
|
|
|
}
|
1996-11-26 02:49:53 +00:00
|
|
|
return (currentlocale());
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
1995-10-23 01:34:17 +00:00
|
|
|
static char *
|
|
|
|
currentlocale()
|
|
|
|
{
|
1996-11-26 02:49:53 +00:00
|
|
|
int i;
|
1995-10-23 01:34:17 +00:00
|
|
|
|
|
|
|
(void)strcpy(current_locale_string, current_categories[1]);
|
|
|
|
|
|
|
|
for (i = 2; i < _LC_LAST; ++i)
|
|
|
|
if (strcmp(current_categories[1], current_categories[i])) {
|
1998-04-29 22:39:56 +00:00
|
|
|
for (i = 2; i < _LC_LAST; ++i) {
|
|
|
|
(void) strcat(current_locale_string, "/");
|
|
|
|
(void) strcat(current_locale_string, current_categories[i]);
|
|
|
|
}
|
1995-10-23 01:34:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (current_locale_string);
|
|
|
|
}
|
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
static char *
|
|
|
|
loadlocale(category)
|
|
|
|
int category;
|
|
|
|
{
|
1996-11-26 08:00:17 +00:00
|
|
|
char *ret;
|
|
|
|
char *new = new_categories[category];
|
|
|
|
char *old = current_categories[category];
|
1996-11-26 02:49:53 +00:00
|
|
|
|
1997-04-07 08:54:38 +00:00
|
|
|
if (_PathLocale == NULL) {
|
|
|
|
char *p = getenv("PATH_LOCALE");
|
|
|
|
|
1998-01-15 09:58:08 +00:00
|
|
|
if (p != NULL
|
|
|
|
#ifndef __NETBSD_SYSCALLS
|
|
|
|
&& !issetugid()
|
|
|
|
#endif
|
|
|
|
) {
|
1997-04-07 08:54:38 +00:00
|
|
|
if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
|
|
|
|
1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
|
|
|
|
return (NULL);
|
|
|
|
_PathLocale = strdup(p);
|
|
|
|
if (_PathLocale == NULL)
|
|
|
|
return (NULL);
|
|
|
|
} else
|
|
|
|
_PathLocale = _PATH_LOCALE;
|
|
|
|
}
|
1996-11-26 02:49:53 +00:00
|
|
|
|
1996-11-27 22:30:44 +00:00
|
|
|
if (strcmp(new, old) == 0)
|
|
|
|
return (old);
|
|
|
|
|
1994-05-27 05:00:24 +00:00
|
|
|
if (category == LC_CTYPE) {
|
1996-11-26 08:00:17 +00:00
|
|
|
ret = setrunelocale(new) ? NULL : new;
|
2000-06-03 12:24:08 +00:00
|
|
|
if (!ret)
|
1996-11-26 08:00:17 +00:00
|
|
|
(void)setrunelocale(old);
|
2000-06-03 12:24:08 +00:00
|
|
|
else
|
1996-11-26 08:00:17 +00:00
|
|
|
(void)strcpy(old, new);
|
|
|
|
return (ret);
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
2001-02-08 16:58:53 +00:00
|
|
|
#define LOAD_CATEGORY(CAT, FUNC) \
|
|
|
|
if (category == CAT) { \
|
|
|
|
ret = (FUNC(new) < 0) ? NULL : new; \
|
|
|
|
if (!ret) \
|
|
|
|
(void)FUNC(old); \
|
|
|
|
else \
|
|
|
|
(void)strcpy(old, new); \
|
|
|
|
return (ret); \
|
1995-02-16 04:24:39 +00:00
|
|
|
}
|
|
|
|
|
2001-02-08 16:58:53 +00:00
|
|
|
LOAD_CATEGORY(LC_COLLATE, __collate_load_tables);
|
|
|
|
LOAD_CATEGORY(LC_TIME, __time_load_locale);
|
|
|
|
LOAD_CATEGORY(LC_NUMERIC, __numeric_load_locale);
|
|
|
|
LOAD_CATEGORY(LC_MONETARY, __monetary_load_locale);
|
|
|
|
LOAD_CATEGORY(LC_MESSAGES, __messages_load_locale);
|
1996-11-26 02:49:53 +00:00
|
|
|
|
|
|
|
/* Just in case...*/
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|