3 #include "locale_impl.h"
7 const char *__lctrans_impl(const char *msg, const struct __locale_map *lm)
10 if (lm) trans = __mo_lookup(lm->map, lm->map_size, msg);
11 return trans ? trans : msg;
14 const unsigned char *__map_file(const char *, size_t *);
15 int __munmap(void *, size_t);
16 char *__strchrnul(const char *, int);
18 static const char envvars[][12] = {
27 static const uint32_t empty_mo[] = { 0x950412de, 0, -1, -1, -1 };
29 const struct __locale_map __c_dot_utf8 = {
31 .map_size = sizeof empty_mo,
35 const struct __locale_map *__get_locale(int cat, const char *val)
38 static void *volatile loc_head;
39 const struct __locale_map *p;
40 struct __locale_map *new = 0;
41 const char *path = 0, *z;
46 (val = getenv("LC_ALL")) && *val ||
47 (val = getenv(envvars[cat])) && *val ||
48 (val = getenv("LANG")) && *val ||
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");
60 if (cat == LC_CTYPE && val[1]=='.')
61 return (void *)&__c_dot_utf8;
65 for (p=loc_head; p; p=p->next)
66 if (!strcmp(val, p->name)) return p;
70 for (p=loc_head; p; p=p->next)
71 if (!strcmp(val, p->name)) {
76 if (!libc.secure) path = getenv("MUSL_LOCPATH");
77 /* FIXME: add a default path? */
79 if (path) for (; *path; path=z+!!*z) {
80 z = __strchrnul(path, ':');
82 if (l >= sizeof buf - n - 2) continue;
85 memcpy(buf+l+1, val, n);
88 const void *map = __map_file(buf, &map_size);
90 new = malloc(sizeof *new);
92 __munmap((void *)map, map_size);
96 new->map_size = map_size;
97 memcpy(new->name, val, n);
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))) {
111 new->map_size = sizeof empty_mo;
112 memcpy(new->name, val, n);
114 new->next = loc_head;
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;