summaryrefslogtreecommitdiff
path: root/lib/localename.c
blob: 203b829b733b89d77f35d42b9ec63e7ecec5dbb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* Determine name of the currently selected locale.
   Copyright (C) 1995-2024 Free Software Foundation, Inc.

   This file is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation; either version 2.1 of the
   License, or (at your option) any later version.

   This file is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
   optimizes away the locale == NULL tests below in duplocale() and freelocale(),
   or xlclang reports -Wtautological-pointer-compare warnings for these tests.
 */
#define _GL_ARG_NONNULL(params)

#include <config.h>

/* Specification.  */
#include "localename.h"

#include <limits.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>

#include "flexmember.h"
#include "glthread/lock.h"
#include "thread-optim.h"


/* Define a local struniq() function.  */
#include "struniq.h"

const char *
gl_locale_name_thread (int category, const char *categoryname)
{
  if (category == LC_ALL)
    /* Invalid argument.  */
    abort ();
  const char *name = gl_locale_name_thread_unsafe (category, categoryname);
  if (name != NULL)
    return struniq (name);
  return NULL;
}

const char *
gl_locale_name_posix (int category, const char *categoryname)
{
  if (category == LC_ALL)
    /* Invalid argument.  */
    abort ();
  const char *name = gl_locale_name_posix_unsafe (category, categoryname);
  if (name != NULL)
    return struniq (name);
  return NULL;
}

/* Determine the current locale's name, and canonicalize it into XPG syntax
     language[_territory][.codeset][@modifier]
   The codeset part in the result is not reliable; the locale_charset()
   should be used for codeset information instead.
   The result must not be freed; it is statically allocated.  */

const char *
gl_locale_name (int category, const char *categoryname)
{
  const char *retval;

  if (category == LC_ALL)
    /* Invalid argument.  */
    abort ();

  retval = gl_locale_name_thread (category, categoryname);
  if (retval != NULL)
    return retval;

  retval = gl_locale_name_posix (category, categoryname);
  if (retval != NULL)
    return retval;

  return gl_locale_name_default ();
}