X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;ds=inline;f=src%2Flocale%2Fnewlocale.c;h=f50bbe9132df39445c642ea3215428b4b2b960d5;hb=55a1c9c89028f8930e5f65fe5484fa7ba0e18853;hp=4e0cbd34072f23675cda75cf047dfc9fbace184d;hpb=61a3364d246e72b903da8b76c2e27a225a51351e;p=musl diff --git a/src/locale/newlocale.c b/src/locale/newlocale.c index 4e0cbd34..f50bbe91 100644 --- a/src/locale/newlocale.c +++ b/src/locale/newlocale.c @@ -3,21 +3,45 @@ #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<