From 767b23c4aa599ebefe0e344608e0c255cc32e45a Mon Sep 17 00:00:00 2001 From: implicitfield <114500360+implicitfield@users.noreply.github.com> Date: Wed, 28 Sep 2022 22:01:54 +0300 Subject: [PATCH] LibC: Allow detection of supported locales through setlocale Closes #14690 --- Userland/Libraries/LibC/locale.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibC/locale.cpp b/Userland/Libraries/LibC/locale.cpp index 06cbcdb6ce..36ff8010d4 100644 --- a/Userland/Libraries/LibC/locale.cpp +++ b/Userland/Libraries/LibC/locale.cpp @@ -45,11 +45,19 @@ static struct lconv default_locale = { default_empty_value }; -char* setlocale(int, char const*) +char* setlocale(int, char const* locale) { - static char locale[2]; - memcpy(locale, "C", 2); - return locale; + static char c_locale_string[2]; + memcpy(c_locale_string, "C", 2); + + // If we get a null pointer, return the current locale as per POSIX spec. + if (locale == nullptr) + return c_locale_string; + + if (strcmp(locale, "POSIX") == 0 || strcmp(locale, "C") == 0 || strcmp(locale, "") == 0) + return c_locale_string; + + return nullptr; } struct lconv* localeconv()