]> git.ipfire.org Git - thirdparty/glibc.git/blame - locale/newlocale.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / locale / newlocale.c
CommitLineData
c84142e8 1/* Return a reference to locale information record.
04277e02 2 Copyright (C) 1996-2019 Free Software Foundation, Inc.
c84142e8
UD
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5
6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
c84142e8
UD
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 14 Lesser General Public License for more details.
c84142e8 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6 17 License along with the GNU C Library; if not, see
5a82c748 18 <https://www.gnu.org/licenses/>. */
c84142e8
UD
19
20#include <argz.h>
ec999b8e 21#include <libc-lock.h>
c84142e8
UD
22#include <errno.h>
23#include <locale.h>
24#include <stdlib.h>
26e40124 25#include <string.h>
c84142e8
UD
26
27#include "localeinfo.h"
28
29
531b71dd 30/* Lock for protecting global data. */
9a69db29 31__libc_rwlock_define (extern , __libc_setlocale_lock attribute_hidden)
531b71dd
UD
32
33
c84142e8
UD
34/* Use this when we come along an error. */
35#define ERROR_RETURN \
36 do { \
37 __set_errno (EINVAL); \
38 return NULL; \
39 } while (0)
40
41
af85385f
ZW
42locale_t
43__newlocale (int category_mask, const char *locale, locale_t base)
c84142e8
UD
44{
45 /* Intermediate memory for result. */
4b10dd6c 46 const char *newnames[__LC_LAST];
4cca6b86 47 struct __locale_struct result;
af85385f 48 locale_t result_ptr;
c84142e8
UD
49 char *locale_path;
50 size_t locale_path_len;
51 const char *locpath_var;
52 int cnt;
a0fc81e1 53 size_t names_len;
c84142e8
UD
54
55 /* We treat LC_ALL in the same way as if all bits were set. */
f7719a9a 56 if (category_mask == 1 << LC_ALL)
4b10dd6c 57 category_mask = (1 << __LC_LAST) - 1 - (1 << LC_ALL);
c84142e8
UD
58
59 /* Sanity check for CATEGORY argument. */
5bef2820 60 if ((category_mask & ~((1 << __LC_LAST) - 1 - (1 << LC_ALL))) != 0)
c84142e8
UD
61 ERROR_RETURN;
62
63 /* `newlocale' does not support asking for the locale name. */
64 if (locale == NULL)
65 ERROR_RETURN;
66
4b5b009c 67 if (base == _nl_C_locobj_ptr)
679e4c43
RM
68 /* We're to modify BASE, returned for a previous call with "C".
69 We can't really modify the read-only structure, so instead
70 start over by copying it. */
71 base = NULL;
72
73 if ((base == NULL || category_mask == (1 << __LC_LAST) - 1 - (1 << LC_ALL))
74 && (category_mask == 0 || !strcmp (locale, "C")))
75 /* Asking for the "C" locale needn't allocate a new object. */
4b5b009c 76 return _nl_C_locobj_ptr;
679e4c43 77
c84142e8
UD
78 /* Allocate memory for the result. */
79 if (base != NULL)
5bef2820 80 result = *base;
c84142e8 81 else
30c14c31
RM
82 /* Fill with pointers to C locale data. */
83 result = _nl_C_locobj;
c84142e8 84
5bef2820
UD
85 /* If no category is to be set we return BASE if available or a
86 dataset using the C locale data. */
87 if (category_mask == 0)
88 {
af85385f 89 result_ptr = (locale_t) malloc (sizeof (struct __locale_struct));
5bef2820
UD
90 if (result_ptr == NULL)
91 return NULL;
92 *result_ptr = result;
c84142e8 93
5bef2820 94 goto update;
c84142e8
UD
95 }
96
97 /* We perhaps really have to load some data. So we determine the
98 path in which to look for the data now. The environment variable
99 `LOCPATH' must only be used when the binary has no SUID or SGID
cb09a2cd
RM
100 bit set. If using the default path, we tell _nl_find_locale
101 by passing null and it can check the canonical locale archive. */
c84142e8
UD
102 locale_path = NULL;
103 locale_path_len = 0;
104
74955460 105 locpath_var = getenv ("LOCPATH");
c84142e8 106 if (locpath_var != NULL && locpath_var[0] != '\0')
cb09a2cd
RM
107 {
108 if (__argz_create_sep (locpath_var, ':',
109 &locale_path, &locale_path_len) != 0)
110 return NULL;
c84142e8 111
cb09a2cd
RM
112 if (__argz_add_sep (&locale_path, &locale_path_len,
113 _nl_default_locale_path, ':') != 0)
114 return NULL;
115 }
c84142e8
UD
116
117 /* Get the names for the locales we are interested in. We either
118 allow a composite name or a single name. */
4b10dd6c
UD
119 for (cnt = 0; cnt < __LC_LAST; ++cnt)
120 if (cnt != LC_ALL)
121 newnames[cnt] = locale;
c84142e8
UD
122 if (strchr (locale, ';') != NULL)
123 {
124 /* This is a composite name. Make a copy and split it up. */
125 char *np = strdupa (locale);
126 char *cp;
78323b5b 127 int specified_mask = 0;
c84142e8
UD
128
129 while ((cp = strchr (np, '=')) != NULL)
130 {
4b10dd6c
UD
131 for (cnt = 0; cnt < __LC_LAST; ++cnt)
132 if (cnt != LC_ALL
133 && (size_t) (cp - np) == _nl_category_name_sizes[cnt]
de18a706 134 && memcmp (np, (_nl_category_names_get (cnt)), cp - np) == 0)
c84142e8
UD
135 break;
136
4b10dd6c 137 if (cnt == __LC_LAST)
c84142e8
UD
138 /* Bogus category name. */
139 ERROR_RETURN;
140
141 /* Found the category this clause sets. */
78323b5b 142 specified_mask |= 1 << cnt;
c84142e8
UD
143 newnames[cnt] = ++cp;
144 cp = strchr (cp, ';');
145 if (cp != NULL)
146 {
147 /* Examine the next clause. */
148 *cp = '\0';
149 np = cp + 1;
150 }
151 else
152 /* This was the last clause. We are done. */
153 break;
154 }
155
78323b5b
RM
156 if (category_mask &~ specified_mask)
157 /* The composite name did not specify all categories we need. */
158 ERROR_RETURN;
c84142e8
UD
159 }
160
531b71dd 161 /* Protect global data. */
9a69db29 162 __libc_rwlock_wrlock (__libc_setlocale_lock);
531b71dd 163
c84142e8 164 /* Now process all categories we are interested in. */
a0fc81e1 165 names_len = 0;
4b10dd6c 166 for (cnt = 0; cnt < __LC_LAST; ++cnt)
a0fc81e1
RM
167 {
168 if ((category_mask & 1 << cnt) != 0)
169 {
170 result.__locales[cnt] = _nl_find_locale (locale_path,
171 locale_path_len,
172 cnt, &newnames[cnt]);
173 if (result.__locales[cnt] == NULL)
174 {
175 free_cnt_data_and_exit:
176 while (cnt-- > 0)
177 if (((category_mask & 1 << cnt) != 0)
178 && result.__locales[cnt]->usage_count != UNDELETABLE)
179 /* We can remove the data. */
180 _nl_remove_locale (cnt, result.__locales[cnt]);
531b71dd
UD
181
182 /* Critical section left. */
9a69db29 183 __libc_rwlock_unlock (__libc_setlocale_lock);
a0fc81e1
RM
184 return NULL;
185 }
186
187 if (newnames[cnt] != _nl_C_name)
188 names_len += strlen (newnames[cnt]) + 1;
189 }
190 else if (cnt != LC_ALL && result.__names[cnt] != _nl_C_name)
191 /* Tally up the unchanged names from BASE as well. */
192 names_len += strlen (result.__names[cnt]) + 1;
193 }
194
195 /* We successfully loaded all required data. Allocate a new structure.
196 We can't just reuse the BASE pointer, because the name strings are
197 changing and we need the old name string area intact so we can copy
198 out of it into the new one without overlap problems should some
199 category's name be getting longer. */
200 result_ptr = malloc (sizeof (struct __locale_struct) + names_len);
201 if (result_ptr == NULL)
202 {
203 cnt = __LC_LAST;
204 goto free_cnt_data_and_exit;
205 }
c84142e8 206
c84142e8
UD
207 if (base == NULL)
208 {
a0fc81e1
RM
209 /* Fill in this new structure from scratch. */
210
211 char *namep = (char *) (result_ptr + 1);
c84142e8 212
a0fc81e1 213 /* Install copied new names in the new structure's __names array.
252e7983 214 If resolved to "C", that is already in RESULT.__names to start. */
26e40124 215 for (cnt = 0; cnt < __LC_LAST; ++cnt)
252e7983 216 if ((category_mask & 1 << cnt) != 0 && newnames[cnt] != _nl_C_name)
26e40124 217 {
a0fc81e1
RM
218 result.__names[cnt] = namep;
219 namep = __stpcpy (namep, newnames[cnt]) + 1;
26e40124 220 }
252e7983
RM
221
222 *result_ptr = result;
c84142e8
UD
223 }
224 else
26e40124 225 {
a0fc81e1 226 /* We modify the base structure. */
26e40124 227
a0fc81e1 228 char *namep = (char *) (result_ptr + 1);
26e40124 229
26e40124 230 for (cnt = 0; cnt < __LC_LAST; ++cnt)
252e7983 231 if ((category_mask & 1 << cnt) != 0)
26e40124 232 {
252e7983
RM
233 if (base->__locales[cnt]->usage_count != UNDELETABLE)
234 /* We can remove the old data. */
235 _nl_remove_locale (cnt, base->__locales[cnt]);
a0fc81e1 236 result_ptr->__locales[cnt] = result.__locales[cnt];
252e7983 237
a0fc81e1
RM
238 if (newnames[cnt] == _nl_C_name)
239 result_ptr->__names[cnt] = _nl_C_name;
240 else
241 {
242 result_ptr->__names[cnt] = namep;
243 namep = __stpcpy (namep, newnames[cnt]) + 1;
244 }
245 }
246 else if (cnt != LC_ALL)
247 {
248 /* The RESULT members point into the old BASE structure. */
249 result_ptr->__locales[cnt] = result.__locales[cnt];
250 if (result.__names[cnt] == _nl_C_name)
251 result_ptr->__names[cnt] = _nl_C_name;
252 else
253 {
254 result_ptr->__names[cnt] = namep;
255 namep = __stpcpy (namep, result.__names[cnt]) + 1;
256 }
26e40124
RM
257 }
258
a0fc81e1 259 free (base);
26e40124 260 }
5db91571 261
531b71dd 262 /* Critical section left. */
9a69db29 263 __libc_rwlock_unlock (__libc_setlocale_lock);
531b71dd 264
c84142e8
UD
265 /* Update the special members. */
266 update:
267 {
268 union locale_data_value *ctypes = result_ptr->__locales[LC_CTYPE]->values;
5bef2820 269 result_ptr->__ctype_b = (const unsigned short int *)
671ab00d 270 ctypes[_NL_ITEM_INDEX (_NL_CTYPE_CLASS)].string + 128;
5bef2820 271 result_ptr->__ctype_tolower = (const int *)
671ab00d 272 ctypes[_NL_ITEM_INDEX (_NL_CTYPE_TOLOWER)].string + 128;
5bef2820 273 result_ptr->__ctype_toupper = (const int *)
671ab00d 274 ctypes[_NL_ITEM_INDEX (_NL_CTYPE_TOUPPER)].string + 128;
c84142e8
UD
275 }
276
277 return result_ptr;
278}
30c14c31 279weak_alias (__newlocale, newlocale)