From b09c82377a6bb5dee7ba7250c9474f09b35ebda2 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Wed, 30 Aug 2023 17:57:19 -0400 Subject: [PATCH] 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. --- src/system/libroot/posix/locale/locale_t.cpp | 20 +++++++++++++------ .../libroot/posix/musl/internal/locale_impl.h | 2 ++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/system/libroot/posix/locale/locale_t.cpp b/src/system/libroot/posix/locale/locale_t.cpp index a5c23eb8c5..780c4cb8c6 100644 --- a/src/system/libroot/posix/locale/locale_t.cpp +++ b/src/system/libroot/posix/locale/locale_t.cpp @@ -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; +} diff --git a/src/system/libroot/posix/musl/internal/locale_impl.h b/src/system/libroot/posix/musl/internal/locale_impl.h index a7cd0de87d..51b245bc66 100644 --- a/src/system/libroot/posix/musl/internal/locale_impl.h +++ b/src/system/libroot/posix/musl/internal/locale_impl.h @@ -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