libroot/locale: Minor fixes and expose internal POSIX locale.

* We can return early from duplocale() so set the "magic" flag early.
 * Initialize all fields up front.
 * Add an internal method __posix_locale_t() which returns a locale_t
   usable as a global C/POSIX locale type, and add the appropriate
   definition to the musl internal header.
This commit is contained in:
Augustin Cavalier 2023-08-30 17:57:19 -04:00
parent 2705bc6bdf
commit b09c82377a
2 changed files with 16 additions and 6 deletions

View File

@ -34,14 +34,15 @@ duplocale(locale_t l)
errno = ENOMEM;
return (locale_t)0;
}
newObj->magic = LOCALE_T_MAGIC;
newObj->backend = NULL;
newObj->databridge = NULL;
LocaleBackend* backend = (l == LC_GLOBAL_LOCALE) ?
gGlobalLocaleBackend : (LocaleBackend*)locObj->backend;
if (backend == NULL) {
newObj->backend = NULL;
if (backend == NULL)
return (locale_t)newObj;
}
// Check if everything is set to "C" or "POSIX",
// and avoid making a backend.
@ -49,7 +50,6 @@ duplocale(locale_t l)
if ((strcasecmp(localeDescription, "POSIX") == 0)
|| (strcasecmp(localeDescription, "C") == 0)) {
newObj->backend = NULL;
return (locale_t)newObj;
}
@ -81,8 +81,6 @@ duplocale(locale_t l)
newBackend->SetLocale(lc, backend->SetLocale(lc, NULL));
}
newObj->magic = LOCALE_T_MAGIC;
return (locale_t)newObj;
}
@ -275,3 +273,13 @@ __current_locale_t()
return locale;
}
extern "C" locale_t
__posix_locale_t()
{
static LocaleBackendData posix_locale_t;
posix_locale_t.backend = NULL;
posix_locale_t.databridge = NULL;
return &posix_locale_t;
}

View File

@ -10,6 +10,8 @@
#define __nl_langinfo_l nl_langinfo_l
locale_t __current_locale_t();
locale_t __posix_locale_t();
#define CURRENT_LOCALE (__current_locale_t())
#define C_LOCALE (__posix_locale_t())
#endif