fix regression in setlocale for LC_ALL with per-category setting
[musl] / src / locale / setlocale.c
index 623660c..2bc7b50 100644 (file)
@@ -3,25 +3,14 @@
 #include <string.h>
 #include "locale_impl.h"
 #include "libc.h"
-#include "atomic.h"
+#include "lock.h"
 
 static char buf[LC_ALL*(LOCALE_NAME_MAX+1)];
 
-static char *setlocale_one_unlocked(int cat, const char *name)
-{
-       const struct __locale_map *lm;
-
-       if (name) libc.global_locale.cat[cat] = lm = __get_locale(cat, name);
-       else lm = libc.global_locale.cat[cat];
-
-       return lm ? (char *)lm->name : "C";
-}
-
-char *__strchrnul(const char *, int);
-
 char *setlocale(int cat, const char *name)
 {
-       static volatile int lock[2];
+       static volatile int lock[1];
+       const struct __locale_map *lm;
 
        if ((unsigned)cat > LC_ALL) return 0;
 
@@ -35,6 +24,7 @@ char *setlocale(int cat, const char *name)
        if (cat == LC_ALL) {
                int i;
                if (name) {
+                       struct __locale_struct tmp_locale;
                        char part[LOCALE_NAME_MAX+1] = "C.UTF-8";
                        const char *p = name;
                        for (i=0; i<LC_ALL; i++) {
@@ -44,8 +34,14 @@ char *setlocale(int cat, const char *name)
                                        part[z-p] = 0;
                                        if (*z) p = z+1;
                                }
-                               setlocale_one_unlocked(i, part);
+                               lm = __get_locale(i, part);
+                               if (lm == LOC_MAP_FAILED) {
+                                       UNLOCK(lock);
+                                       return 0;
+                               }
+                               tmp_locale.cat[i] = lm;
                        }
+                       libc.global_locale = tmp_locale;
                }
                char *s = buf;
                const char *part;
@@ -65,7 +61,17 @@ char *setlocale(int cat, const char *name)
                return same==LC_ALL ? (char *)part : buf;
        }
 
-       char *ret = setlocale_one_unlocked(cat, name);
+       if (name) {
+               lm = __get_locale(cat, name);
+               if (lm == LOC_MAP_FAILED) {
+                       UNLOCK(lock);
+                       return 0;
+               }
+               libc.global_locale.cat[cat] = lm;
+       } else {
+               lm = libc.global_locale.cat[cat];
+       }
+       char *ret = lm ? (char *)lm->name : "C";
 
        UNLOCK(lock);