| 1 |
/* Determine name of the currently selected locale. |
| 2 |
Copyright (C) 1995-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is free software: you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU Lesser General Public License as |
| 6 |
published by the Free Software Foundation; either version 2.1 of the |
| 7 |
License, or (at your option) any later version. |
| 8 |
|
| 9 |
This file is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU Lesser General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU Lesser General Public License |
| 15 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
/* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc |
| 18 |
optimizes away the locale == NULL tests below in duplocale() and freelocale(), |
| 19 |
or xlclang reports -Wtautological-pointer-compare warnings for these tests. |
| 20 |
*/ |
| 21 |
#define _GL_ARG_NONNULL(params) |
| 22 |
|
| 23 |
#include <config.h> |
| 24 |
|
| 25 |
/* Specification. */ |
| 26 |
#include "localename.h" |
| 27 |
|
| 28 |
#include <limits.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <locale.h> |
| 31 |
#include <string.h> |
| 32 |
|
| 33 |
#include "flexmember.h" |
| 34 |
#include "glthread/lock.h" |
| 35 |
#include "thread-optim.h" |
| 36 |
|
| 37 |
|
| 38 |
/* Define a local struniq() function. */ |
| 39 |
#include "struniq.h" |
| 40 |
|
| 41 |
const char * |
| 42 |
gl_locale_name_thread (int category, const char *categoryname) |
| 43 |
{ |
| 44 |
if (category == LC_ALL) |
| 45 |
/* Invalid argument. */ |
| 46 |
abort (); |
| 47 |
const char *name = gl_locale_name_thread_unsafe (category, categoryname); |
| 48 |
if (name != NULL) |
| 49 |
return struniq (name); |
| 50 |
return NULL; |
| 51 |
} |
| 52 |
|
| 53 |
const char * |
| 54 |
gl_locale_name_posix (int category, const char *categoryname) |
| 55 |
{ |
| 56 |
if (category == LC_ALL) |
| 57 |
/* Invalid argument. */ |
| 58 |
abort (); |
| 59 |
const char *name = gl_locale_name_posix_unsafe (category, categoryname); |
| 60 |
if (name != NULL) |
| 61 |
return struniq (name); |
| 62 |
return NULL; |
| 63 |
} |
| 64 |
|
| 65 |
/* Determine the current locale's name, and canonicalize it into XPG syntax |
| 66 |
language[_territory][.codeset][@modifier] |
| 67 |
The codeset part in the result is not reliable; the locale_charset() |
| 68 |
should be used for codeset information instead. |
| 69 |
The result must not be freed; it is statically allocated. */ |
| 70 |
|
| 71 |
const char * |
| 72 |
gl_locale_name (int category, const char *categoryname) |
| 73 |
{ |
| 74 |
const char *retval; |
| 75 |
|
| 76 |
if (category == LC_ALL) |
| 77 |
/* Invalid argument. */ |
| 78 |
abort (); |
| 79 |
|
| 80 |
retval = gl_locale_name_thread (category, categoryname); |
| 81 |
if (retval != NULL) |
| 82 |
return retval; |
| 83 |
|
| 84 |
retval = gl_locale_name_posix (category, categoryname); |
| 85 |
if (retval != NULL) |
| 86 |
return retval; |
| 87 |
|
| 88 |
return gl_locale_name_default (); |
| 89 |
} |