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