]> git.proxmox.com Git - wasi-libc.git/blob - libc-top-half/musl/src/locale/newlocale.c
Update to musl 1.2.2.
[wasi-libc.git] / libc-top-half / musl / src / locale / newlocale.c
1 #include <stdlib.h>
2 #include <string.h>
3 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
4 #include <pthread.h>
5 #endif
6 #include "locale_impl.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 static int default_locale_init_done;
15 static struct __locale_struct default_locale, default_ctype_locale;
16
17 int __loc_is_allocated(locale_t loc)
18 {
19 return loc && loc != C_LOCALE && loc != UTF8_LOCALE
20 && loc != &default_locale && loc != &default_ctype_locale;
21 }
22
23 static locale_t do_newlocale(int mask, const char *name, locale_t loc)
24 {
25 struct __locale_struct tmp;
26
27 for (int i=0; i<LC_ALL; i++) {
28 tmp.cat[i] = (!(mask & (1<<i)) && loc) ? loc->cat[i] :
29 __get_locale(i, (mask & (1<<i)) ? name : "");
30 if (tmp.cat[i] == LOC_MAP_FAILED)
31 return 0;
32 }
33
34 /* For locales with allocated storage, modify in-place. */
35 if (__loc_is_allocated(loc)) {
36 *loc = tmp;
37 return loc;
38 }
39
40 /* Otherwise, first see if we can use one of the builtin locales.
41 * This makes the common usage case for newlocale, getting a C locale
42 * with predictable behavior, very fast, and more importantly, fail-safe. */
43 if (!memcmp(&tmp, C_LOCALE, sizeof tmp)) return C_LOCALE;
44 if (!memcmp(&tmp, UTF8_LOCALE, sizeof tmp)) return UTF8_LOCALE;
45
46 /* And provide builtins for the initial default locale, and a
47 * variant of the C locale honoring the default locale's encoding. */
48 if (!default_locale_init_done) {
49 for (int i=0; i<LC_ALL; i++)
50 default_locale.cat[i] = __get_locale(i, "");
51 default_ctype_locale.cat[LC_CTYPE] = default_locale.cat[LC_CTYPE];
52 default_locale_init_done = 1;
53 }
54 if (!memcmp(&tmp, &default_locale, sizeof tmp)) return &default_locale;
55 if (!memcmp(&tmp, &default_ctype_locale, sizeof tmp))
56 return &default_ctype_locale;
57
58 /* If no builtin locale matched, attempt to allocate and copy. */
59 if ((loc = malloc(sizeof *loc))) *loc = tmp;
60
61 return loc;
62 }
63
64 locale_t __newlocale(int mask, const char *name, locale_t loc)
65 {
66 LOCK(__locale_lock);
67 loc = do_newlocale(mask, name, loc);
68 UNLOCK(__locale_lock);
69 return loc;
70 }
71
72 weak_alias(__newlocale, newlocale);