replace atomics with locks in locale-setting code
[oweals/musl.git] / src / locale / setlocale.c
1 #include <locale.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "locale_impl.h"
5 #include "libc.h"
6 #include "atomic.h"
7
8 static char buf[2+4*(LOCALE_NAME_MAX+1)];
9
10 static char *setlocale_one_unlocked(int cat, const char *name)
11 {
12         struct __locale_map *lm;
13
14         if (name) __setlocalecat(&libc.global_locale, cat, name);
15
16         switch (cat) {
17         case LC_CTYPE:
18                 return libc.global_locale.ctype_utf8 ? "C.UTF-8" : "C";
19         case LC_NUMERIC:
20                 return "C";
21         case LC_MESSAGES:
22                 return libc.global_locale.messages_name[0]
23                         ? libc.global_locale.messages_name : "C";
24         default:
25                 lm = libc.global_locale.cat[cat-2];
26                 return lm ? lm->name : "C";
27         }
28 }
29
30 char *setlocale(int cat, const char *name)
31 {
32         static volatile int lock[2];
33         struct __locale_map *lm;
34         int i, j;
35
36         if (!libc.global_locale.messages_name) {
37                 libc.global_locale.messages_name =
38                         buf + 2 + 3*(LOCALE_NAME_MAX+1);
39         }
40
41         if ((unsigned)cat > LC_ALL) return 0;
42
43         LOCK(lock);
44
45         /* For LC_ALL, setlocale is required to return a string which
46          * encodes the current setting for all categories. The format of
47          * this string is unspecified, and only the following code, which
48          * performs both the serialization and deserialization, depends
49          * on the format, so it can easily be changed if needed. */
50         if (cat == LC_ALL) {
51                 if (name) {
52                         char part[LOCALE_NAME_MAX+1];
53                         if (name[0] && name[1]==';'
54                             && strlen(name) > 2 + 3*(LOCALE_NAME_MAX+1)) {
55                                 part[0] = name[0];
56                                 part[1] = 0;
57                                 setlocale(LC_CTYPE, part);
58                                 part[LOCALE_NAME_MAX] = 0;
59                                 for (i=LC_TIME; i<LC_MESSAGES; i++) {
60                                         memcpy(part, name + 2 + (i-2)*(LOCALE_NAME_MAX+1), LOCALE_NAME_MAX);
61                                         for (j=LOCALE_NAME_MAX-1; j && part[j]==';'; j--)
62                                                 part[j] = 0;
63                                         setlocale_one_unlocked(i, part);
64                                 }
65                                 setlocale_one_unlocked(LC_MESSAGES, name
66                                         + 2 + 3*(LOCALE_NAME_MAX+1));
67                         } else {
68                                 for (i=0; i<LC_ALL; i++)
69                                         setlocale_one_unlocked(i, name);
70                         }
71                 }
72                 memset(buf, ';', 2 + 3*(LOCALE_NAME_MAX+1));
73                 buf[0] = libc.global_locale.ctype_utf8 ? 'U' : 'C';
74                 for (i=LC_TIME; i<LC_MESSAGES; i++) {
75                         lm = libc.global_locale.cat[i-2];
76                         if (lm) memcpy(buf + 2 + (i-2)*(LOCALE_NAME_MAX+1),
77                                 lm->name, strlen(lm->name));
78                 }
79                 UNLOCK(lock);
80                 return buf;
81         }
82
83         char *ret = setlocale_one_unlocked(cat, name);
84
85         UNLOCK(lock);
86
87         return ret;
88 }