3 #include "locale_impl.h"
5 int __loc_is_allocated(locale_t loc)
7 return loc && loc != C_LOCALE && loc != UTF8_LOCALE;
10 locale_t __newlocale(int mask, const char *name, locale_t loc)
12 struct __locale_struct tmp;
14 for (int i=0; i<LC_ALL; i++) {
15 tmp.cat[i] = (!(mask & (1<<i)) && loc) ? loc->cat[i] :
16 __get_locale(i, (mask & (1<<i)) ? name : "");
17 if (tmp.cat[i] == LOC_MAP_FAILED)
21 /* For locales with allocated storage, modify in-place. */
22 if (__loc_is_allocated(loc)) {
27 /* Otherwise, first see if we can use one of the builtin locales.
28 * This makes the common usage case for newlocale, getting a C locale
29 * with predictable behavior, very fast, and more importantly, fail-safe. */
30 if (!memcmp(&tmp, C_LOCALE, sizeof tmp)) return C_LOCALE;
31 if (!memcmp(&tmp, UTF8_LOCALE, sizeof tmp)) return UTF8_LOCALE;
33 /* If no builtin locale matched, attempt to allocate and copy. */
34 if ((loc = malloc(sizeof *loc))) *loc = tmp;
39 weak_alias(__newlocale, newlocale);