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