X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Flocale%2Fnewlocale.c;h=8fb006a7fcdb934ec7031942f5fa1cb6eda6888c;hb=11ce1b133d594b6a454d3e8d5941e7a6a432c42b;hp=4e0cbd34072f23675cda75cf047dfc9fbace184d;hpb=61a3364d246e72b903da8b76c2e27a225a51351e;p=musl diff --git a/src/locale/newlocale.c b/src/locale/newlocale.c index 4e0cbd34..8fb006a7 100644 --- a/src/locale/newlocale.c +++ b/src/locale/newlocale.c @@ -1,23 +1,46 @@ #include #include #include "locale_impl.h" -#include "libc.h" + +int __loc_is_allocated(locale_t loc) +{ + return loc && loc != C_LOCALE && loc != UTF8_LOCALE; +} locale_t __newlocale(int mask, const char *name, locale_t loc) { - int i; + int i, j; + struct __locale_struct tmp; + const struct __locale_map *lm; - if (!loc) { - loc = malloc(sizeof *loc); - if (!loc) return 0; + /* For locales with allocated storage, modify in-place. */ + if (__loc_is_allocated(loc)) { for (i=0; icat[i] = __get_locale(i, ""); + if (mask & (1<cat[i] = __get_locale(i, name); + return loc; } - for (i=0; icat[i] = __get_locale(i, name); + /* 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; icat[i]; + else + lm = __get_locale(i, mask & (1<