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 struct __locale_map *findlocale(const char *name, size_t n)
20 static void *loc_head;
21 struct __locale_map *p, *new, *old_head;
22 const char *path = 0, *z;
28 for (p=loc_head; p; p=p->next)
29 if (!strcmp(name, p->name)) return p;
31 if (strchr(name, '/')) return 0;
33 if (!libc.secure) path = getenv("MUSL_LOCPATH");
34 /* FIXME: add a default path? */
37 for (; *path; path=z+!!*z) {
38 z = __strchrnul(path, ':');
40 if (l >= sizeof buf - n - 2) continue;
43 memcpy(buf+l+1, name, n);
45 map = __map_file(buf, &map_size);
47 new = malloc(sizeof *new);
49 __munmap((void *)map, map_size);
53 new->map_size = map_size;
54 memcpy(new->name, name, n);
59 } while (a_cas_p(&loc_head, old_head, new) != old_head);
66 static const char envvars[][12] = {
75 int __setlocalecat(locale_t loc, int cat, const char *val)
78 (val = getenv("LC_ALL")) && *val ||
79 (val = getenv(envvars[cat])) && *val ||
80 (val = getenv("LANG")) && *val ||
84 size_t n = strnlen(val, LOCALE_NAME_MAX);
85 int builtin = (val[0]=='C' && !val[1])
86 || !strcmp(val, "C.UTF-8")
87 || !strcmp(val, "POSIX");
88 struct __locale_map *data, *old;
92 a_store(&loc->ctype_utf8, !builtin || val[1]=='.');
96 loc->messages_name[0] = 0;
98 memcpy(loc->messages_name, val, n);
99 loc->messages_name[n] = 0;
103 data = builtin ? 0 : findlocale(val, n);
104 if (data == loc->cat[cat-2]) break;
105 do old = loc->cat[cat-2];
106 while (a_cas_p(&loc->cat[cat-2], old, data) != old);