rename internal locale file handling locale maps
[musl] / src / locale / locale_map.c
1 #include <locale.h>
2 #include <string.h>
3 #include "locale_impl.h"
4 #include "libc.h"
5 #include "atomic.h"
6
7 const char *__lctrans_impl(const char *msg, const struct __locale_map *lm)
8 {
9         const char *trans = 0;
10         if (lm) trans = __mo_lookup(lm->map, lm->map_size, msg);
11         return trans ? trans : msg;
12 }
13
14 const unsigned char *__map_file(const char *, size_t *);
15 int __munmap(void *, size_t);
16 char *__strchrnul(const char *, int);
17
18 static const char envvars[][12] = {
19         "LC_CTYPE",
20         "LC_NUMERIC",
21         "LC_TIME",
22         "LC_COLLATE",
23         "LC_MONETARY",
24         "LC_MESSAGES",
25 };
26
27 static const uint32_t empty_mo[] = { 0x950412de, 0, -1, -1, -1 };
28
29 static const struct __locale_map c_dot_utf8 = {
30         .map = empty_mo,
31         .map_size = sizeof empty_mo,
32         .name = "C.UTF-8"
33 };
34
35 const struct __locale_map *__get_locale(int cat, const char *val)
36 {
37         static int lock[2];
38         static void *volatile loc_head;
39         const struct __locale_map *p;
40         struct __locale_map *new = 0;
41         const char *path = 0, *z;
42         char buf[256];
43         size_t l, n;
44
45         if (!*val) {
46                 (val = getenv("LC_ALL")) && *val ||
47                 (val = getenv(envvars[cat])) && *val ||
48                 (val = getenv("LANG")) && *val ||
49                 (val = "C.UTF-8");
50         }
51
52         /* Limit name length and forbid leading dot or any slashes. */
53         for (n=0; n<LOCALE_NAME_MAX && val[n] && val[n]!='/'; n++);
54         if (val[0]=='.' || val[n]) val = "C.UTF-8";
55         int builtin = (val[0]=='C' && !val[1])
56                 || !strcmp(val, "C.UTF-8")
57                 || !strcmp(val, "POSIX");
58
59         if (builtin) {
60                 if (cat == LC_CTYPE && val[1]=='.')
61                         return (void *)&c_dot_utf8;
62                 return 0;
63         }
64
65         for (p=loc_head; p; p=p->next)
66                 if (!strcmp(val, p->name)) return p;
67
68         LOCK(lock);
69
70         for (p=loc_head; p; p=p->next)
71                 if (!strcmp(val, p->name)) {
72                         UNLOCK(lock);
73                         return p;
74                 }
75
76         if (!libc.secure) path = getenv("MUSL_LOCPATH");
77         /* FIXME: add a default path? */
78
79         if (path) for (; *path; path=z+!!*z) {
80                 z = __strchrnul(path, ':');
81                 l = z - path - !!*z;
82                 if (l >= sizeof buf - n - 2) continue;
83                 memcpy(buf, path, l);
84                 buf[l] = '/';
85                 memcpy(buf+l+1, val, n);
86                 buf[l+1+n] = 0;
87                 size_t map_size;
88                 const void *map = __map_file(buf, &map_size);
89                 if (map) {
90                         new = malloc(sizeof *new);
91                         if (!new) {
92                                 __munmap((void *)map, map_size);
93                                 break;
94                         }
95                         new->map = map;
96                         new->map_size = map_size;
97                         memcpy(new->name, val, n);
98                         new->name[n] = 0;
99                         new->next = loc_head;
100                         loc_head = new;
101                         break;
102                 }
103         }
104
105         /* If no locale definition was found, make a locale map
106          * object anyway to store the name, which is kept for the
107          * sake of being able to do message translations at the
108          * application level. */
109         if (!new && (new = malloc(sizeof *new))) {
110                 new->map = empty_mo;
111                 new->map_size = sizeof empty_mo;
112                 memcpy(new->name, val, n);
113                 new->name[n] = 0;
114                 new->next = loc_head;
115                 loc_head = new;
116         }
117
118         /* For LC_CTYPE, never return a null pointer unless the
119          * requested name was "C" or "POSIX". */
120         if (!new && cat == LC_CTYPE) new = (void *)&c_dot_utf8;
121
122         UNLOCK(lock);
123         return new;
124 }