Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/netbsd-1-5]: src/lib/libc/locale Pull up revisions 1.23, 1.25-1.27, 1.31...



details:   https://anonhg.NetBSD.org/src/rev/244ad476f422
branches:  netbsd-1-5
changeset: 490511:244ad476f422
user:      jhawk <jhawk%NetBSD.org@localhost>
date:      Thu Jan 25 18:05:11 2001 +0000

description:
Pull up revisions 1.23, 1.25-1.27, 1.31 via patch (requested by itojun):
  Updates to setlocale(): return NULL for nonexistant locales, ignore
  some errors, and handle environment vars consistently (no slashes).

diffstat:

 lib/libc/locale/setlocale.c |  75 ++++++++++++++++++++++++++++----------------
 1 files changed, 48 insertions(+), 27 deletions(-)

diffs (167 lines):

diff -r caefe73d5334 -r 244ad476f422 lib/libc/locale/setlocale.c
--- a/lib/libc/locale/setlocale.c       Thu Jan 25 17:54:21 2001 +0000
+++ b/lib/libc/locale/setlocale.c       Thu Jan 25 18:05:11 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: setlocale.c,v 1.17.6.3 2000/10/26 16:17:27 sommerfeld Exp $    */
+/*     $NetBSD: setlocale.c,v 1.17.6.4 2001/01/25 18:05:11 jhawk Exp $ */
 
 /*
  * Copyright (c) 1991, 1993
@@ -41,7 +41,7 @@
 #if 0
 static char sccsid[] = "@(#)setlocale.c        8.1 (Berkeley) 7/4/93";
 #else
-__RCSID("$NetBSD: setlocale.c,v 1.17.6.3 2000/10/26 16:17:27 sommerfeld Exp $");
+__RCSID("$NetBSD: setlocale.c,v 1.17.6.4 2001/01/25 18:05:11 jhawk Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -93,6 +93,11 @@
  */
 static char new_categories[_LC_LAST][32];
 
+/*
+ * Backup area to back out changes on failure
+ */
+static char saved_categories[_LC_LAST][32];
+
 static char current_locale_string[_LC_LAST * 33];
 static char *PathLocale;
 
@@ -104,7 +109,7 @@
        int category;
        const char *locale;
 {
-       int i;
+       int i, loadlocale_success;
        size_t len;
        char *env, *r;
 
@@ -123,8 +128,8 @@
         * Default to the current locale for everything.
         */
        for (i = 1; i < _LC_LAST; ++i)
-               (void)strncpy(new_categories[i], current_categories[i],
-                   sizeof(new_categories[i]) - 1);
+               (void)strlcpy(new_categories[i], current_categories[i],
+                   sizeof(new_categories[i]));
 
        /*
         * Now go fill up new_categories from the locale argument
@@ -141,31 +146,35 @@
                if (!env || !*env || strchr(env, '/'))
                        env = "C";
 
-               (void)strncpy(new_categories[category], env, 31);
-               new_categories[category][31] = 0;
+               (void)strlcpy(new_categories[category], env,
+                   sizeof(new_categories[category]));
                if (!category) {
                        for (i = 1; i < _LC_LAST; ++i) {
-                               if (!(env = getenv(categories[i])) || !*env)
+                               env = getenv(categories[i]);
+                               if (!env || !*env || strchr(env, '/'))
                                        env = new_categories[0];
-                               (void)strncpy(new_categories[i], env, 31);
-                               new_categories[i][31] = 0;
+                               (void)strlcpy(new_categories[i], env,
+                                   sizeof(new_categories[i]));
                        }
                }
        } else if (category) {
-               (void)strncpy(new_categories[category], locale, 31);
-               new_categories[category][31] = 0;
+               (void)strlcpy(new_categories[category], locale,
+                   sizeof(new_categories[category]));
        } else {
                if ((r = strchr(locale, '/')) == 0) {
                        for (i = 1; i < _LC_LAST; ++i) {
-                               (void)strncpy(new_categories[i], locale, 31);
-                               new_categories[i][31] = 0;
+                               (void)strlcpy(new_categories[i], locale,
+                                   sizeof(new_categories[i]));
                        }
                } else {
-                       for (i = 1; r[1] == '/'; ++r);
+                       for (i = 1; r[1] == '/'; ++r)
+                               ;
                        if (!r[1])
                                return (NULL);  /* Hmm, just slashes... */
                        do {
-                               len = r - locale > 31 ? 31 : r - locale;
+                               len = r - locale > sizeof(new_categories[i]) - 1
+                                       ? sizeof(new_categories[i]) - 1
+                                       : r - locale;
                                (void)strncpy(new_categories[i++], locale, len);
                                new_categories[i++][len] = 0;
                                locale = r;
@@ -174,17 +183,29 @@
                                while (*++r && *r != '/');
                        } while (*locale);
                        while (i < _LC_LAST)
-                               (void)strncpy(new_categories[i],
-                                   new_categories[i-1],
-                                   sizeof(new_categories[i]) - 1);
+                               (void)strlcpy(new_categories[i],
+                                   new_categories[i - 1],
+                                   sizeof(new_categories[i]));
                }
        }
 
        if (category)
                return (loadlocale(category));
 
-       for (i = 1; i < _LC_LAST; ++i)
-               (void) loadlocale(i);
+       loadlocale_success = 0;
+       for (i = 1; i < _LC_LAST; ++i) {
+               (void)strlcpy(saved_categories[i], current_categories[i],
+                   sizeof(saved_categories[i]));
+               if (loadlocale(i) != NULL)
+                       loadlocale_success = 1;
+       }
+
+       /*
+        * If all categories failed, return NULL; we don't need to back
+        * changes off, since none happened.
+        */
+       if (!loadlocale_success)
+               return NULL;
 
        return (currentlocale());
 }
@@ -194,8 +215,8 @@
 {
        int i;
 
-       (void)strncpy(current_locale_string, current_categories[1],
-           sizeof(current_locale_string) - 1);
+       (void)strlcpy(current_locale_string, current_categories[1],
+           sizeof(current_locale_string));
 
        for (i = 2; i < _LC_LAST; ++i)
                if (strcmp(current_categories[1], current_categories[i])) {
@@ -240,9 +261,9 @@
                        }
                }
 
-               (void)strncpy(current_categories[category],
+               (void)strlcpy(current_categories[category],
                    new_categories[category],
-                   sizeof(current_categories[category]) - 1);
+                   sizeof(current_categories[category]));
                return current_categories[category];
        }
 
@@ -255,9 +276,9 @@
        switch (category) {
        case LC_CTYPE:
                if (__loadctype(name)) {
-                       (void)strncpy(current_categories[category],
+                       (void)strlcpy(current_categories[category],
                            new_categories[category],
-                           sizeof(current_categories[category]) - 1);
+                           sizeof(current_categories[category]));
                        return current_categories[category];
                }
                return NULL;



Home | Main Index | Thread Index | Old Index