aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAndrey A. Chernov <ache@FreeBSD.org>2004-01-31 21:49:00 +0000
committerAndrey A. Chernov <ache@FreeBSD.org>2004-01-31 21:49:00 +0000
commitd6a6a136c94658ed018d254f2f8dff0861f489da (patch)
treedb909fec04ff3ef951b41dce8f0eaf3dbc7dcb1e /lib
parenteb3f8f2b65428834a9877df6f4d6bff88983bd6d (diff)
Notes
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/locale/setlocale.c56
1 files changed, 34 insertions, 22 deletions
diff --git a/lib/libc/locale/setlocale.c b/lib/libc/locale/setlocale.c
index f2c67e470008..874c09c9ac4b 100644
--- a/lib/libc/locale/setlocale.c
+++ b/lib/libc/locale/setlocale.c
@@ -95,6 +95,7 @@ static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)];
static char *currentlocale(void);
static int wrap_setrunelocale(const char *);
static char *loadlocale(int);
+static const char *__get_locale_env(int);
char *
setlocale(category, locale)
@@ -102,7 +103,7 @@ setlocale(category, locale)
const char *locale;
{
int i, j, len, saverr;
- char *env, *r;
+ const char *env, *r;
if (category < LC_ALL || category >= _LC_LAST) {
errno = EINVAL;
@@ -123,34 +124,22 @@ setlocale(category, locale)
* Now go fill up new_categories from the locale argument
*/
if (!*locale) {
- env = getenv("LC_ALL");
-
- if (category != LC_ALL && (env == NULL || !*env))
- env = getenv(categories[category]);
-
- if (env == NULL || !*env)
- env = getenv("LANG");
-
- if (env == NULL || !*env)
- env = "C";
-
- if (strlen(env) > ENCODING_LEN) {
- errno = EINVAL;
- return (NULL);
- }
- (void)strcpy(new_categories[category], env);
-
if (category == LC_ALL) {
for (i = 1; i < _LC_LAST; ++i) {
- if ((env = getenv(categories[i])) == NULL ||
- !*env)
- env = new_categories[LC_ALL];
- else if (strlen(env) > ENCODING_LEN) {
+ env = __get_locale_env(i);
+ if (strlen(env) > ENCODING_LEN) {
errno = EINVAL;
return (NULL);
}
(void)strcpy(new_categories[i], env);
}
+ } else {
+ env = __get_locale_env(category);
+ if (strlen(env) > ENCODING_LEN) {
+ errno = EINVAL;
+ return (NULL);
+ }
+ (void)strcpy(new_categories[category], env);
}
} else if (category != LC_ALL) {
if (strlen(locale) > ENCODING_LEN) {
@@ -322,3 +311,26 @@ loadlocale(category)
return (NULL);
}
+static const char *
+__get_locale_env(category)
+ int category;
+{
+ const char *env;
+
+ /* 1. check LC_ALL. */
+ env = getenv(categories[0]);
+
+ /* 2. check LC_* */
+ if (env == NULL || !*env)
+ env = getenv(categories[category]);
+
+ /* 3. check LANG */
+ if (env == NULL || !*env)
+ env = getenv("LANG");
+
+ /* 4. if none is set, fall to "C" */
+ if (env == NULL || !*env)
+ env = "C";
+
+ return (env);
+}