diff options
Diffstat (limited to 'src/locale/newlocale.c')
-rw-r--r-- | src/locale/newlocale.c | 70 |
1 files changed, 46 insertions, 24 deletions
diff --git a/src/locale/newlocale.c b/src/locale/newlocale.c index 8fb006a7..9ac3cd38 100644 --- a/src/locale/newlocale.c +++ b/src/locale/newlocale.c @@ -1,48 +1,70 @@ #include <stdlib.h> #include <string.h> +#include <pthread.h> #include "locale_impl.h" +#include "lock.h" + +#define malloc __libc_malloc +#define calloc undef +#define realloc undef +#define free undef + +static int default_locale_init_done; +static struct __locale_struct default_locale, default_ctype_locale; int __loc_is_allocated(locale_t loc) { - return loc && loc != C_LOCALE && loc != UTF8_LOCALE; + return loc && loc != C_LOCALE && loc != UTF8_LOCALE + && loc != &default_locale && loc != &default_ctype_locale; } -locale_t __newlocale(int mask, const char *name, locale_t loc) +static locale_t do_newlocale(int mask, const char *name, locale_t loc) { - int i, j; struct __locale_struct tmp; - const struct __locale_map *lm; + + for (int i=0; i<LC_ALL; i++) { + tmp.cat[i] = (!(mask & (1<<i)) && loc) ? loc->cat[i] : + __get_locale(i, (mask & (1<<i)) ? name : ""); + if (tmp.cat[i] == LOC_MAP_FAILED) + return 0; + } /* For locales with allocated storage, modify in-place. */ if (__loc_is_allocated(loc)) { - for (i=0; i<LC_ALL; i++) - if (mask & (1<<i)) - loc->cat[i] = __get_locale(i, name); + *loc = tmp; return loc; } - /* Otherwise, build a temporary locale object, which will only - * be instantiated in allocated storage if it does not match - * one of the built-in static locales. This makes the common - * usage case for newlocale, getting a C locale with predictable - * behavior, very fast, and more importantly, fail-safe. */ - for (j=i=0; i<LC_ALL; i++) { - if (loc && !(mask & (1<<i))) - lm = loc->cat[i]; - else - lm = __get_locale(i, mask & (1<<i) ? name : ""); - if (lm) j++; - tmp.cat[i] = lm; - } + /* Otherwise, first see if we can use one of the builtin locales. + * This makes the common usage case for newlocale, getting a C locale + * with predictable behavior, very fast, and more importantly, fail-safe. */ + if (!memcmp(&tmp, C_LOCALE, sizeof tmp)) return C_LOCALE; + if (!memcmp(&tmp, UTF8_LOCALE, sizeof tmp)) return UTF8_LOCALE; - if (!j) - return C_LOCALE; - if (j==1 && tmp.cat[LC_CTYPE]==&__c_dot_utf8) - return UTF8_LOCALE; + /* And provide builtins for the initial default locale, and a + * variant of the C locale honoring the default locale's encoding. */ + if (!default_locale_init_done) { + for (int i=0; i<LC_ALL; i++) + default_locale.cat[i] = __get_locale(i, ""); + default_ctype_locale.cat[LC_CTYPE] = default_locale.cat[LC_CTYPE]; + default_locale_init_done = 1; + } + if (!memcmp(&tmp, &default_locale, sizeof tmp)) return &default_locale; + if (!memcmp(&tmp, &default_ctype_locale, sizeof tmp)) + return &default_ctype_locale; + /* If no builtin locale matched, attempt to allocate and copy. */ if ((loc = malloc(sizeof *loc))) *loc = tmp; return loc; } +locale_t __newlocale(int mask, const char *name, locale_t loc) +{ + LOCK(__locale_lock); + loc = do_newlocale(mask, name, loc); + UNLOCK(__locale_lock); + return loc; +} + weak_alias(__newlocale, newlocale); |