LoongArch: Fix inconsistency in SHMLBA macro values between glibc and kernel
[glibc.git] / locale / newlocale.c
blob108d2428bf34c79180db8b21874d541551a359b8
1 /* Return a reference to locale information record.
2 Copyright (C) 1996-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <argz.h>
20 #include <libc-lock.h>
21 #include <errno.h>
22 #include <locale.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "localeinfo.h"
29 /* Lock for protecting global data. */
30 __libc_rwlock_define (extern , __libc_setlocale_lock attribute_hidden)
33 /* Use this when we come along an error. */
34 #define ERROR_RETURN \
35 do { \
36 __set_errno (EINVAL); \
37 return NULL; \
38 } while (0)
41 locale_t
42 __newlocale (int category_mask, const char *locale, locale_t base)
44 /* Intermediate memory for result. */
45 const char *newnames[__LC_LAST];
46 struct __locale_struct result;
47 locale_t result_ptr;
48 char *locale_path;
49 size_t locale_path_len;
50 const char *locpath_var;
51 int cnt;
52 size_t names_len;
54 /* We treat LC_ALL in the same way as if all bits were set. */
55 if (category_mask == 1 << LC_ALL)
56 category_mask = (1 << __LC_LAST) - 1 - (1 << LC_ALL);
58 /* Sanity check for CATEGORY argument. */
59 if ((category_mask & ~((1 << __LC_LAST) - 1 - (1 << LC_ALL))) != 0)
60 ERROR_RETURN;
62 /* `newlocale' does not support asking for the locale name. */
63 if (locale == NULL)
64 ERROR_RETURN;
66 if (base == _nl_C_locobj_ptr)
67 /* We're to modify BASE, returned for a previous call with "C".
68 We can't really modify the read-only structure, so instead
69 start over by copying it. */
70 base = NULL;
72 if ((base == NULL || category_mask == (1 << __LC_LAST) - 1 - (1 << LC_ALL))
73 && (category_mask == 0 || !strcmp (locale, "C")))
74 /* Asking for the "C" locale needn't allocate a new object. */
75 return _nl_C_locobj_ptr;
77 /* Allocate memory for the result. */
78 if (base != NULL)
79 result = *base;
80 else
81 /* Fill with pointers to C locale data. */
82 result = _nl_C_locobj;
84 /* If no category is to be set we return BASE if available or a
85 dataset using the C locale data. */
86 if (category_mask == 0)
88 result_ptr = (locale_t) malloc (sizeof (struct __locale_struct));
89 if (result_ptr == NULL)
90 return NULL;
91 *result_ptr = result;
93 goto update;
96 /* We perhaps really have to load some data. So we determine the
97 path in which to look for the data now. The environment variable
98 `LOCPATH' must only be used when the binary has no SUID or SGID
99 bit set. If using the default path, we tell _nl_find_locale
100 by passing null and it can check the canonical locale archive. */
101 locale_path = NULL;
102 locale_path_len = 0;
104 locpath_var = getenv ("LOCPATH");
105 if (locpath_var != NULL && locpath_var[0] != '\0')
107 if (__argz_create_sep (locpath_var, ':',
108 &locale_path, &locale_path_len) != 0)
109 return NULL;
111 if (__argz_add_sep (&locale_path, &locale_path_len,
112 _nl_default_locale_path, ':') != 0)
113 return NULL;
116 /* Get the names for the locales we are interested in. We either
117 allow a composite name or a single name. */
118 for (cnt = 0; cnt < __LC_LAST; ++cnt)
119 if (cnt != LC_ALL)
120 newnames[cnt] = locale;
121 if (strchr (locale, ';') != NULL)
123 /* This is a composite name. Make a copy and split it up. */
124 char *np = strdupa (locale);
125 char *cp;
126 int specified_mask = 0;
128 while ((cp = strchr (np, '=')) != NULL)
130 for (cnt = 0; cnt < __LC_LAST; ++cnt)
131 if (cnt != LC_ALL
132 && (size_t) (cp - np) == _nl_category_name_sizes[cnt]
133 && memcmp (np, (_nl_category_names_get (cnt)), cp - np) == 0)
134 break;
136 if (cnt == __LC_LAST)
137 /* Bogus category name. */
138 ERROR_RETURN;
140 /* Found the category this clause sets. */
141 specified_mask |= 1 << cnt;
142 newnames[cnt] = ++cp;
143 cp = strchr (cp, ';');
144 if (cp != NULL)
146 /* Examine the next clause. */
147 *cp = '\0';
148 np = cp + 1;
150 else
151 /* This was the last clause. We are done. */
152 break;
155 if (category_mask &~ specified_mask)
156 /* The composite name did not specify all categories we need. */
157 ERROR_RETURN;
160 /* Protect global data. */
161 __libc_rwlock_wrlock (__libc_setlocale_lock);
163 /* Now process all categories we are interested in. */
164 names_len = 0;
165 for (cnt = 0; cnt < __LC_LAST; ++cnt)
167 if ((category_mask & 1 << cnt) != 0)
169 result.__locales[cnt] = _nl_find_locale (locale_path,
170 locale_path_len,
171 cnt, &newnames[cnt]);
172 if (result.__locales[cnt] == NULL)
174 free_cnt_data_and_exit:
175 while (cnt-- > 0)
176 if (((category_mask & 1 << cnt) != 0)
177 && result.__locales[cnt]->usage_count != UNDELETABLE)
178 /* We can remove the data. */
179 _nl_remove_locale (cnt, result.__locales[cnt]);
181 /* Critical section left. */
182 __libc_rwlock_unlock (__libc_setlocale_lock);
183 return NULL;
186 if (newnames[cnt] != _nl_C_name)
187 names_len += strlen (newnames[cnt]) + 1;
189 else if (cnt != LC_ALL && result.__names[cnt] != _nl_C_name)
190 /* Tally up the unchanged names from BASE as well. */
191 names_len += strlen (result.__names[cnt]) + 1;
194 /* We successfully loaded all required data. Allocate a new structure.
195 We can't just reuse the BASE pointer, because the name strings are
196 changing and we need the old name string area intact so we can copy
197 out of it into the new one without overlap problems should some
198 category's name be getting longer. */
199 result_ptr = malloc (sizeof (struct __locale_struct) + names_len);
200 if (result_ptr == NULL)
202 cnt = __LC_LAST;
203 goto free_cnt_data_and_exit;
206 if (base == NULL)
208 /* Fill in this new structure from scratch. */
210 char *namep = (char *) (result_ptr + 1);
212 /* Install copied new names in the new structure's __names array.
213 If resolved to "C", that is already in RESULT.__names to start. */
214 for (cnt = 0; cnt < __LC_LAST; ++cnt)
215 if ((category_mask & 1 << cnt) != 0 && newnames[cnt] != _nl_C_name)
217 result.__names[cnt] = namep;
218 namep = __stpcpy (namep, newnames[cnt]) + 1;
221 *result_ptr = result;
223 else
225 /* We modify the base structure. */
227 char *namep = (char *) (result_ptr + 1);
229 for (cnt = 0; cnt < __LC_LAST; ++cnt)
230 if ((category_mask & 1 << cnt) != 0)
232 if (base->__locales[cnt]->usage_count != UNDELETABLE)
233 /* We can remove the old data. */
234 _nl_remove_locale (cnt, base->__locales[cnt]);
235 result_ptr->__locales[cnt] = result.__locales[cnt];
237 if (newnames[cnt] == _nl_C_name)
238 result_ptr->__names[cnt] = _nl_C_name;
239 else
241 result_ptr->__names[cnt] = namep;
242 namep = __stpcpy (namep, newnames[cnt]) + 1;
245 else if (cnt != LC_ALL)
247 /* The RESULT members point into the old BASE structure. */
248 result_ptr->__locales[cnt] = result.__locales[cnt];
249 if (result.__names[cnt] == _nl_C_name)
250 result_ptr->__names[cnt] = _nl_C_name;
251 else
253 result_ptr->__names[cnt] = namep;
254 namep = __stpcpy (namep, result.__names[cnt]) + 1;
258 free (base);
261 /* Critical section left. */
262 __libc_rwlock_unlock (__libc_setlocale_lock);
264 /* Update the special members. */
265 update:
267 union locale_data_value *ctypes = result_ptr->__locales[LC_CTYPE]->values;
268 result_ptr->__ctype_b = (const unsigned short int *)
269 ctypes[_NL_ITEM_INDEX (_NL_CTYPE_CLASS)].string + 128;
270 result_ptr->__ctype_tolower = (const int *)
271 ctypes[_NL_ITEM_INDEX (_NL_CTYPE_TOLOWER)].string + 128;
272 result_ptr->__ctype_toupper = (const int *)
273 ctypes[_NL_ITEM_INDEX (_NL_CTYPE_TOUPPER)].string + 128;
276 return result_ptr;
278 weak_alias (__newlocale, newlocale)