Add getlocalename_l(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2024-06-23 14:07:21 +02:00
parent 8618c1adf5
commit 8f68ea49ce
3 changed files with 51 additions and 5 deletions

View File

@ -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" */

View File

@ -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 <assert.h>
#include <locale.h>
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";
}

View File

@ -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 <stdlib.h>
#include <string.h>
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;
}