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-26 08:00:17 +00:00
|
|
|
*
|
1999-08-28 00:22:10 +00:00
|
|
|
* $FreeBSD$
|
1994-05-27 05:00:24 +00:00
|
|
|
*/
|
|
|
|
|
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"
|
1997-02-06 09:11:06 +00:00
|
|
|
#include "setlocale.h"
|
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
|
|
|
|
1995-10-23 01:34:17 +00:00
|
|
|
static char *currentlocale __P((void));
|
1994-05-27 05:00:24 +00:00
|
|
|
static char *loadlocale __P((int));
|
1996-11-26 02:49:53 +00:00
|
|
|
static int stub_load_locale __P((const char *));
|
1994-05-27 05:00:24 +00:00
|
|
|
|
1995-10-23 01:34:17 +00:00
|
|
|
extern int __time_load_locale __P((const char *)); /* strftime.c */
|
|
|
|
|
|
|
|
#ifdef XPG4
|
|
|
|
extern int _xpg4_setrunelocale __P((char *));
|
|
|
|
#endif
|
|
|
|
|
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) {
|
|
|
|
env = getenv(categories[category]);
|
|
|
|
|
1996-11-26 02:49:53 +00:00
|
|
|
if (category != LC_ALL && (!env || !*env))
|
|
|
|
env = getenv(categories[LC_ALL]);
|
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");
|
|
|
|
|
1996-11-26 02:49:53 +00:00
|
|
|
if (!env || !*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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-11-26 08:00:17 +00:00
|
|
|
if (category)
|
|
|
|
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) {
|
1995-10-23 01:34:17 +00:00
|
|
|
#ifdef XPG4
|
1996-11-26 08:00:17 +00:00
|
|
|
ret = _xpg4_setrunelocale(new) ? NULL : new;
|
1995-10-23 01:34:17 +00:00
|
|
|
#else
|
1996-11-26 08:00:17 +00:00
|
|
|
ret = setrunelocale(new) ? NULL : new;
|
1995-10-23 01:34:17 +00:00
|
|
|
#endif
|
1996-11-26 08:00:17 +00:00
|
|
|
if (!ret) {
|
|
|
|
#ifdef XPG4
|
|
|
|
(void)_xpg4_setrunelocale(old);
|
|
|
|
#else
|
|
|
|
(void)setrunelocale(old);
|
|
|
|
#endif
|
|
|
|
} else
|
|
|
|
(void)strcpy(old, new);
|
|
|
|
return (ret);
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
1995-02-16 04:24:39 +00:00
|
|
|
if (category == LC_COLLATE) {
|
1996-11-26 08:00:17 +00:00
|
|
|
ret = (__collate_load_tables(new) < 0) ? NULL : new;
|
|
|
|
if (!ret)
|
|
|
|
(void)__collate_load_tables(old);
|
|
|
|
else
|
|
|
|
(void)strcpy(old, new);
|
|
|
|
return (ret);
|
1995-02-16 04:24:39 +00:00
|
|
|
}
|
|
|
|
|
1995-08-04 18:43:01 +00:00
|
|
|
if (category == LC_TIME) {
|
1996-11-26 08:00:17 +00:00
|
|
|
ret = (__time_load_locale(new) < 0) ? NULL : new;
|
|
|
|
if (!ret)
|
|
|
|
(void)__time_load_locale(old);
|
|
|
|
else
|
|
|
|
(void)strcpy(old, new);
|
|
|
|
return (ret);
|
1995-08-04 18:43:01 +00:00
|
|
|
}
|
|
|
|
|
1998-04-29 22:39:56 +00:00
|
|
|
if (category == LC_MONETARY ||
|
|
|
|
category == LC_MESSAGES ||
|
|
|
|
category == LC_NUMERIC) {
|
1996-11-26 08:00:17 +00:00
|
|
|
ret = stub_load_locale(new) ? NULL : new;
|
|
|
|
if (!ret)
|
|
|
|
(void)stub_load_locale(old);
|
|
|
|
else
|
|
|
|
(void)strcpy(old, new);
|
|
|
|
return (ret);
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
1996-11-26 02:49:53 +00:00
|
|
|
|
|
|
|
/* Just in case...*/
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
stub_load_locale(encoding)
|
|
|
|
const char *encoding;
|
|
|
|
{
|
|
|
|
char name[PATH_MAX];
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (!encoding)
|
|
|
|
return(1);
|
|
|
|
/*
|
|
|
|
* The "C" and "POSIX" locale are always here.
|
|
|
|
*/
|
|
|
|
if (!strcmp(encoding, "C") || !strcmp(encoding, "POSIX"))
|
|
|
|
return(0);
|
|
|
|
if (!_PathLocale)
|
|
|
|
return(1);
|
1997-02-05 19:17:10 +00:00
|
|
|
/* Range checking not needed, encoding has fixed size */
|
1996-11-26 02:49:53 +00:00
|
|
|
strcpy(name, _PathLocale);
|
|
|
|
strcat(name, "/");
|
|
|
|
strcat(name, encoding);
|
1994-09-24 15:59:33 +00:00
|
|
|
#if 0
|
1994-05-27 05:00:24 +00:00
|
|
|
/*
|
|
|
|
* Some day we will actually look at this file.
|
|
|
|
*/
|
1994-09-24 15:59:33 +00:00
|
|
|
#endif
|
1996-11-26 02:49:53 +00:00
|
|
|
return (stat(name, &st) != 0 || !S_ISDIR(st.st_mode));
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|