fix localeconv field value for unavailable values
[musl] / src / locale / newlocale.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include "locale_impl.h"
4 #include "libc.h"
5
6 int __loc_is_allocated(locale_t loc)
7 {
8         return loc && loc != C_LOCALE && loc != UTF8_LOCALE;
9 }
10
11 locale_t __newlocale(int mask, const char *name, locale_t loc)
12 {
13         int i, j;
14         struct __locale_struct tmp;
15         const struct __locale_map *lm;
16
17         /* For locales with allocated storage, modify in-place. */
18         if (__loc_is_allocated(loc)) {
19                 for (i=0; i<LC_ALL; i++)
20                         if (mask & (1<<i))
21                                 loc->cat[i] = __get_locale(i, name);
22                 return loc;
23         }
24
25         /* Otherwise, build a temporary locale object, which will only
26          * be instantiated in allocated storage if it does not match
27          * one of the built-in static locales. This makes the common
28          * usage case for newlocale, getting a C locale with predictable
29          * behavior, very fast, and more importantly, fail-safe. */
30         for (j=i=0; i<LC_ALL; i++) {
31                 if (loc && !(mask & (1<<i)))
32                         lm = loc->cat[i];
33                 else
34                         lm = __get_locale(i, mask & (1<<i) ? name : "");
35                 if (lm) j++;
36                 tmp.cat[i] = lm;
37         }
38
39         if (!j)
40                 return C_LOCALE;
41         if (j==1 && tmp.cat[LC_CTYPE]==&__c_dot_utf8)
42                 return UTF8_LOCALE;
43
44         if ((loc = malloc(sizeof *loc))) *loc = tmp;
45
46         return loc;
47 }
48
49 weak_alias(__newlocale, newlocale);