Here a simple patch to move current_categories[][] array, that embed the
current locale names for each categories (LC_CTYPE, LC_MESSAGES, LC_..),
from global static variable in locale/setlocale.c to the struct _locale_t.

All the logic is to move the reference of static global variable to
global state locale, as setlocale(1) function only operate on global
state.

Later patches will change slightly that to reuse code for creating new
locale state.

-- 
Sebastien Marie


Index: b/lib/libc/locale/setlocale.c
===================================================================
--- a/lib/libc/locale/setlocale.c       2015-07-04 12:04:02.003065887 +0200
+++ b/lib/libc/locale/setlocale.c       2015-07-04 14:04:40.894950518 +0200
@@ -42,6 +42,8 @@
 
 #include "rune.h"
 #include "rune_local.h"
+#include "xlocale_private.h"
+
 /*
  * Category names for getenv()
  */
@@ -55,19 +57,6 @@
     "LC_MESSAGES"
 };
 
-/*
- * Current locales for each category
- */
-static char current_categories[_LC_LAST][32] = {
-    "C",
-    "C",
-    "C",
-    "C",
-    "C",
-    "C",
-    "C"
-};
-
 static char current_locale_string[_LC_LAST * 33];
 
 static char    *currentlocale(void);
@@ -89,13 +78,15 @@
 
        if (!locale)
                return (category ?
-                   current_categories[category] : currentlocale());
+                   _lc_global_locale.categories[category]
+                   : currentlocale());
 
        /*
         * Default to the current locale for everything.
         */
        for (i = 1; i < _LC_LAST; ++i)
-               (void)strlcpy(new_categories[i], current_categories[i],
+               (void)strlcpy(new_categories[i],
+                   _lc_global_locale.categories[i],
                    sizeof(new_categories[i]));
 
        /*
@@ -172,16 +163,21 @@
 {
        int i;
 
-       (void)strlcpy(current_locale_string, current_categories[1],
+       (void)strlcpy(current_locale_string,
+           _lc_global_locale.categories[1],
            sizeof(current_locale_string));
 
        for (i = 2; i < _LC_LAST; ++i)
-               if (strcmp(current_categories[1], current_categories[i])) {
+               if (strcmp(_lc_global_locale.categories[1],
+                          _lc_global_locale.categories[i])) {
                        (void)snprintf(current_locale_string,
                            sizeof(current_locale_string), "%s/%s/%s/%s/%s/%s",
-                           current_categories[1], current_categories[2],
-                           current_categories[3], current_categories[4],
-                           current_categories[5], current_categories[6]);
+                           _lc_global_locale.categories[1],
+                           _lc_global_locale.categories[2],
+                           _lc_global_locale.categories[3],
+                           _lc_global_locale.categories[4],
+                           _lc_global_locale.categories[5],
+                           _lc_global_locale.categories[6]);
                        break;
                }
        return (current_locale_string);
@@ -269,13 +265,14 @@
 static char *
 loadlocale(int category, const char *locname)
 {
-       if (strcmp(locname, current_categories[category]) == 0)
-               return (current_categories[category]);
+       if (strcmp(locname, _lc_global_locale.categories[category]) == 0)
+               return (_lc_global_locale.categories[category]);
 
        if (!load_locale_sub(category, locname)) {
-               (void)strlcpy(current_categories[category],
-                   locname, sizeof(current_categories[category]));
-               return current_categories[category];
+               (void)strlcpy(_lc_global_locale.categories[category],
+                   locname,
+                   sizeof(_lc_global_locale.categories[category]));
+               return _lc_global_locale.categories[category];
        } else {
                return NULL;
        }
Index: b/lib/libc/locale/xlocale.c
===================================================================
--- a/lib/libc/locale/xlocale.c 2015-07-04 14:00:24.042939553 +0200
+++ b/lib/libc/locale/xlocale.c 2015-07-04 14:05:26.015229706 +0200
@@ -24,7 +24,8 @@
  * hold the global state
  */
 struct _locale_t _lc_global_locale = {
-       XLOCALE_INSTALLED
+       XLOCALE_INSTALLED,
+       {"C", "C", "C", "C", "C", "C", "C"}
 };
 
 
Index: b/lib/libc/locale/xlocale_private.h
===================================================================
--- a/lib/libc/locale/xlocale_private.h 2015-07-04 12:04:02.003065887 +0200
+++ b/lib/libc/locale/xlocale_private.h 2015-07-04 14:04:40.904950281 +0200
@@ -18,9 +18,11 @@
 #define _XLOCALE_PRIVATE_H_
 
 #include <sys/cdefs.h>
+#include <locale.h>
 
 struct _locale_t {
        int     flags;
+       char    categories[_LC_LAST][32];
 };
 
 #define XLOCALE_INSTALLED    (1 << 0)

Reply via email to