exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / localename.c
blob203b829b733b89d77f35d42b9ec63e7ecec5dbb7
1 /* Determine name of the currently selected locale.
2 Copyright (C) 1995-2024 Free Software Foundation, Inc.
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.
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.
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/>. */
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.
21 #define _GL_ARG_NONNULL(params)
23 #include <config.h>
25 /* Specification. */
26 #include "localename.h"
28 #include <limits.h>
29 #include <stdlib.h>
30 #include <locale.h>
31 #include <string.h>
33 #include "flexmember.h"
34 #include "glthread/lock.h"
35 #include "thread-optim.h"
38 /* Define a local struniq() function. */
39 #include "struniq.h"
41 const char *
42 gl_locale_name_thread (int category, const char *categoryname)
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;
53 const char *
54 gl_locale_name_posix (int category, const char *categoryname)
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;
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. */
71 const char *
72 gl_locale_name (int category, const char *categoryname)
74 const char *retval;
76 if (category == LC_ALL)
77 /* Invalid argument. */
78 abort ();
80 retval = gl_locale_name_thread (category, categoryname);
81 if (retval != NULL)
82 return retval;
84 retval = gl_locale_name_posix (category, categoryname);
85 if (retval != NULL)
86 return retval;
88 return gl_locale_name_default ();