From 713e7b2c547731577decd78204b94bfdac884e50 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Wed, 7 Jan 2026 16:27:46 -0500 Subject: [PATCH] locale: Implement getlocalename_l. Specified in POSIX-2024. GNUlib currently has a nasty Haiku-specific hack to implement equivalent functionality; after this it should be able to use this function instead. --- headers/posix/locale.h | 3 ++- src/system/libroot/posix/locale/locale_t.cpp | 11 +++++++++++ src/system/libroot/posix/locale/setlocale.cpp | 9 ++------- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/headers/posix/locale.h b/headers/posix/locale.h index 46d6103084..797e99f648 100644 --- a/headers/posix/locale.h +++ b/headers/posix/locale.h @@ -66,13 +66,14 @@ struct lconv { extern "C" { #endif -extern struct lconv *localeconv(void); extern char *setlocale(int category, const char *locale); +extern struct lconv *localeconv(void); extern locale_t duplocale(locale_t); extern void freelocale(locale_t); extern locale_t newlocale(int, const char *, locale_t); extern locale_t uselocale(locale_t); +const char *getlocalename_l(int category, locale_t locobj); #ifdef __cplusplus } diff --git a/src/system/libroot/posix/locale/locale_t.cpp b/src/system/libroot/posix/locale/locale_t.cpp index 780c4cb8c6..416776f7e7 100644 --- a/src/system/libroot/posix/locale/locale_t.cpp +++ b/src/system/libroot/posix/locale/locale_t.cpp @@ -260,6 +260,17 @@ uselocale(locale_t newLoc) } +extern "C" const char* +getlocalename_l(int category, locale_t l) +{ + LocaleBackend* backend = (l == LC_GLOBAL_LOCALE) ? + gGlobalLocaleBackend : (LocaleBackend*)((LocaleBackendData*)l)->backend; + if (backend == NULL) + return "POSIX"; + return backend->SetLocale(category, NULL); +} + + extern "C" locale_t __current_locale_t() { diff --git a/src/system/libroot/posix/locale/setlocale.cpp b/src/system/libroot/posix/locale/setlocale.cpp index 4c1ffccc5b..c77220cb96 100644 --- a/src/system/libroot/posix/locale/setlocale.cpp +++ b/src/system/libroot/posix/locale/setlocale.cpp @@ -28,13 +28,8 @@ setlocale(int category, const char* locale) if (category < 0 || category > LC_LAST) return NULL; - if (locale == NULL) { - // query the locale of the given category - if (gGlobalLocaleBackend != NULL) - return const_cast(gGlobalLocaleBackend->SetLocale(category, NULL)); - else - return const_cast("POSIX"); - } + if (locale == NULL) + return const_cast(getlocalename_l(category, LC_GLOBAL_LOCALE)); // we may have to invoke SetLocale once for each category, so we use an // array to collect the locale per category