diff options
author | Rich Felker <dalias@aerifal.cx> | 2018-10-22 00:22:33 -0400 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2018-10-22 00:22:33 -0400 |
commit | 5af1f5942b2068d99182991dc212bdb3f5e9973d (patch) | |
tree | c804d4ca66f5ed7ef6a85f708937fe0e6e05aa98 /src/locale/newlocale.c | |
parent | 74e704006a0004058fc38806a19c1552b1e2463d (diff) | |
download | musl-5af1f5942b2068d99182991dc212bdb3f5e9973d.tar.gz musl-5af1f5942b2068d99182991dc212bdb3f5e9973d.tar.bz2 musl-5af1f5942b2068d99182991dc212bdb3f5e9973d.tar.xz musl-5af1f5942b2068d99182991dc212bdb3f5e9973d.zip |
make the default locale (& a variant) failure-free cases for newlocale
commit aeeac9ca5490d7d90fe061ab72da446c01ddf746 introduced fail-safe
invariants that creating a locale_t object for the C locale or C.UTF-8
locale will always succeed. extend the guarantee to also cover the
following:
- newlocale(LC_ALL_MASK, "", 0)
- newlocale(LC_ALL_MASK-LC_CTYPE_MASK, "C", 0)
provided that the LANG/LC_* environment variables have not been
changed by the program. these usages are idiomatic for getting the
default locale, and for getting a locale that behaves as the C locale
except for honoring the default locale's character encoding.
Diffstat (limited to 'src/locale/newlocale.c')
-rw-r--r-- | src/locale/newlocale.c | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/src/locale/newlocale.c b/src/locale/newlocale.c index 68574605..d20a8489 100644 --- a/src/locale/newlocale.c +++ b/src/locale/newlocale.c @@ -1,10 +1,22 @@ #include <stdlib.h> #include <string.h> +#include <pthread.h> #include "locale_impl.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; i<LC_ALL; i++) + default_locale.cat[i] = __get_locale(i, ""); + default_ctype_locale.cat[LC_CTYPE] = default_locale.cat[LC_CTYPE]; +} + 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) @@ -30,6 +42,13 @@ locale_t __newlocale(int mask, const char *name, locale_t loc) 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; |