From 8f68ea49ce3b06b48d083bd3093f66b3f2ca2388 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sun, 23 Jun 2024 14:07:21 +0200 Subject: [PATCH] Add getlocalename_l(3). --- libc/include/locale.h | 17 +++++++++++++++++ libc/locale/getlocalename_l.c | 29 +++++++++++++++++++++++++++++ libc/locale/setlocale.c | 10 +++++----- 3 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 libc/locale/getlocalename_l.c diff --git a/libc/include/locale.h b/libc/include/locale.h index a42bed76..2a962bab 100644 --- a/libc/include/locale.h +++ b/libc/include/locale.h @@ -64,12 +64,29 @@ struct lconv #define LC_ALL 6 #define LC_NUM_CATEGORIES LC_ALL +#if __USE_SORTIX || __USE_POSIX +#define LC_GLOBAL_LOCALE ((locale_t) -1) +#endif + +#if __USE_SORTIX || __USE_POSIX +#ifndef __locale_t_defined +#define __locale_t_defined +/* TODO: figure out what this does and typedef it properly. This is just a + temporary assignment. */ +typedef int __locale_t; +typedef __locale_t locale_t; +#endif +#endif + #ifdef __cplusplus extern "C" { #endif char* setlocale(int category, const char* locale); struct lconv* localeconv(void); +#if __USE_SORTIX || 202405L <= __USE_POSIX +const char* getlocalename_l(int, locale_t); +#endif #ifdef __cplusplus } /* extern "C" */ diff --git a/libc/locale/getlocalename_l.c b/libc/locale/getlocalename_l.c new file mode 100644 index 00000000..d7a95fa0 --- /dev/null +++ b/libc/locale/getlocalename_l.c @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2024 Jonas 'Sortie' Termansen. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * locale/getlocalename_l.c + * Get locale name. + */ + +#include +#include + +extern char* __current_locales[LC_NUM_CATEGORIES]; + +const char* getlocalename_l(int cat, locale_t locale) +{ + assert(locale == LC_GLOBAL_LOCALE); + return __current_locales[cat] ? __current_locales[cat] : (char*) "C"; +} diff --git a/libc/locale/setlocale.c b/libc/locale/setlocale.c index beb9d8d0..53d0009d 100644 --- a/libc/locale/setlocale.c +++ b/libc/locale/setlocale.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2014, 2015 Jonas 'Sortie' Termansen. + * Copyright (c) 2012, 2014, 2015, 2024 Jonas 'Sortie' Termansen. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -22,7 +22,7 @@ #include #include -static char* current_locales[LC_NUM_CATEGORIES] = { NULL }; +char* __current_locales[LC_NUM_CATEGORIES] = { NULL }; char* setlocale(int category, const char* locale) { @@ -32,7 +32,7 @@ char* setlocale(int category, const char* locale) int from = category != LC_ALL ? category : 0; int to = category != LC_ALL ? category : LC_NUM_CATEGORIES - 1; if ( !locale ) - return current_locales[to] ? current_locales[to] : (char*) "C"; + return __current_locales[to] ? __current_locales[to] : (char*) "C"; for ( int i = from; i <= to; i++ ) { if ( !(new_strings[i] = strdup(locale)) ) @@ -44,8 +44,8 @@ char* setlocale(int category, const char* locale) } for ( int i = from; i <= to; i++ ) { - free(current_locales[i]); - current_locales[i] = new_strings[i]; + free(__current_locales[i]); + __current_locales[i] = new_strings[i]; } return (char*) locale; }