5 #include "locale_impl.h"
9 #define malloc __libc_malloc
14 const char *__lctrans_impl(const char *msg, const struct __locale_map *lm)
16 const char *trans = 0;
17 if (lm) trans = __mo_lookup(lm->map, lm->map_size, msg);
18 return trans ? trans : msg;
21 static const char envvars[][12] = {
30 const struct __locale_map *__get_locale(int cat, const char *val)
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;
41 (val = getenv("LC_ALL")) && *val ||
42 (val = getenv(envvars[cat])) && *val ||
43 (val = getenv("LANG")) && *val ||
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");
55 if (cat == LC_CTYPE && val[1]=='.')
56 return (void *)&__c_dot_utf8;
60 for (p=loc_head; p; p=p->next)
61 if (!strcmp(val, p->name)) return p;
65 for (p=loc_head; p; p=p->next)
66 if (!strcmp(val, p->name)) {
71 if (!libc.secure) path = getenv("MUSL_LOCPATH");
72 /* FIXME: add a default path? */
74 if (path) for (; *path; path=z+!!*z) {
75 z = __strchrnul(path, ':');
77 if (l >= sizeof buf - n - 2) continue;
80 memcpy(buf+l+1, val, n);
83 const void *map = __map_file(buf, &map_size);
85 new = malloc(sizeof *new);
87 __munmap((void *)map, map_size);
91 new->map_size = map_size;
92 memcpy(new->name, val, n);
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);
109 new->next = loc_head;
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;