X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Flocale%2Fnewlocale.c;h=d20a8489835f54e26fb4a59ffcb419b5da43f4bf;hb=e4235d70672d9751d7718ddc2b52d0b426430768;hp=4e0cbd34072f23675cda75cf047dfc9fbace184d;hpb=61a3364d246e72b903da8b76c2e27a225a51351e;p=musl diff --git a/src/locale/newlocale.c b/src/locale/newlocale.c index 4e0cbd34..d20a8489 100644 --- a/src/locale/newlocale.c +++ b/src/locale/newlocale.c @@ -1,23 +1,56 @@ #include #include +#include #include "locale_impl.h" -#include "libc.h" + +static pthread_once_t default_locale_once; +static struct __locale_struct default_locale, default_ctype_locale; + +static void default_locale_init(void) +{ + for (int i=0; icat[i] = __get_locale(i, ""); + struct __locale_struct tmp; + + for (int i=0; icat[i] : + __get_locale(i, (mask & (1<cat[i] = __get_locale(i, name); + /* 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; + + /* And provide builtins for the initial default locale, and a + * variant of the C locale honoring the default locale's encoding. */ + pthread_once(&default_locale_once, default_locale_init); + 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; }