]>
Commit | Line | Data |
---|---|---|
c84142e8 | 1 | /* Return a reference to locale information record. |
581c785b | 2 | Copyright (C) 1996-2022 Free Software Foundation, Inc. |
c84142e8 | 3 | This file is part of the GNU C Library. |
c84142e8 UD |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
c84142e8 UD |
9 | |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
c84142e8 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
c84142e8 UD |
18 | |
19 | #include <argz.h> | |
ec999b8e | 20 | #include <libc-lock.h> |
c84142e8 UD |
21 | #include <errno.h> |
22 | #include <locale.h> | |
23 | #include <stdlib.h> | |
26e40124 | 24 | #include <string.h> |
c84142e8 UD |
25 | |
26 | #include "localeinfo.h" | |
27 | ||
28 | ||
531b71dd | 29 | /* Lock for protecting global data. */ |
9a69db29 | 30 | __libc_rwlock_define (extern , __libc_setlocale_lock attribute_hidden) |
531b71dd UD |
31 | |
32 | ||
c84142e8 UD |
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) | |
39 | ||
40 | ||
af85385f ZW |
41 | locale_t |
42 | __newlocale (int category_mask, const char *locale, locale_t base) | |
c84142e8 UD |
43 | { |
44 | /* Intermediate memory for result. */ | |
4b10dd6c | 45 | const char *newnames[__LC_LAST]; |
4cca6b86 | 46 | struct __locale_struct result; |
af85385f | 47 | locale_t result_ptr; |
c84142e8 UD |
48 | char *locale_path; |
49 | size_t locale_path_len; | |
50 | const char *locpath_var; | |
51 | int cnt; | |
a0fc81e1 | 52 | size_t names_len; |
c84142e8 UD |
53 | |
54 | /* We treat LC_ALL in the same way as if all bits were set. */ | |
f7719a9a | 55 | if (category_mask == 1 << LC_ALL) |
4b10dd6c | 56 | category_mask = (1 << __LC_LAST) - 1 - (1 << LC_ALL); |
c84142e8 UD |
57 | |
58 | /* Sanity check for CATEGORY argument. */ | |
5bef2820 | 59 | if ((category_mask & ~((1 << __LC_LAST) - 1 - (1 << LC_ALL))) != 0) |
c84142e8 UD |
60 | ERROR_RETURN; |
61 | ||
62 | /* `newlocale' does not support asking for the locale name. */ | |
63 | if (locale == NULL) | |
64 | ERROR_RETURN; | |
65 | ||
4b5b009c | 66 | if (base == _nl_C_locobj_ptr) |
679e4c43 RM |
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; | |
71 | ||
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. */ | |
4b5b009c | 75 | return _nl_C_locobj_ptr; |
679e4c43 | 76 | |
c84142e8 UD |
77 | /* Allocate memory for the result. */ |
78 | if (base != NULL) | |
5bef2820 | 79 | result = *base; |
c84142e8 | 80 | else |
30c14c31 RM |
81 | /* Fill with pointers to C locale data. */ |
82 | result = _nl_C_locobj; | |
c84142e8 | 83 | |
5bef2820 UD |
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) | |
87 | { | |
af85385f | 88 | result_ptr = (locale_t) malloc (sizeof (struct __locale_struct)); |
5bef2820 UD |
89 | if (result_ptr == NULL) |
90 | return NULL; | |
91 | *result_ptr = result; | |
c84142e8 | 92 | |
5bef2820 | 93 | goto update; |
c84142e8 UD |
94 | } |
95 | ||
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 | |
cb09a2cd RM |
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. */ | |
c84142e8 UD |
101 | locale_path = NULL; |
102 | locale_path_len = 0; | |
103 | ||
74955460 | 104 | locpath_var = getenv ("LOCPATH"); |
c84142e8 | 105 | if (locpath_var != NULL && locpath_var[0] != '\0') |
cb09a2cd RM |
106 | { |
107 | if (__argz_create_sep (locpath_var, ':', | |
108 | &locale_path, &locale_path_len) != 0) | |
109 | return NULL; | |
c84142e8 | 110 | |
cb09a2cd RM |
111 | if (__argz_add_sep (&locale_path, &locale_path_len, |
112 | _nl_default_locale_path, ':') != 0) | |
113 | return NULL; | |
114 | } | |
c84142e8 UD |
115 | |
116 | /* Get the names for the locales we are interested in. We either | |
117 | allow a composite name or a single name. */ | |
4b10dd6c UD |
118 | for (cnt = 0; cnt < __LC_LAST; ++cnt) |
119 | if (cnt != LC_ALL) | |
120 | newnames[cnt] = locale; | |
c84142e8 UD |
121 | if (strchr (locale, ';') != NULL) |
122 | { | |
123 | /* This is a composite name. Make a copy and split it up. */ | |
124 | char *np = strdupa (locale); | |
125 | char *cp; | |
78323b5b | 126 | int specified_mask = 0; |
c84142e8 UD |
127 | |
128 | while ((cp = strchr (np, '=')) != NULL) | |
129 | { | |
4b10dd6c UD |
130 | for (cnt = 0; cnt < __LC_LAST; ++cnt) |
131 | if (cnt != LC_ALL | |
132 | && (size_t) (cp - np) == _nl_category_name_sizes[cnt] | |
de18a706 | 133 | && memcmp (np, (_nl_category_names_get (cnt)), cp - np) == 0) |
c84142e8 UD |
134 | break; |
135 | ||
4b10dd6c | 136 | if (cnt == __LC_LAST) |
c84142e8 UD |
137 | /* Bogus category name. */ |
138 | ERROR_RETURN; | |
139 | ||
140 | /* Found the category this clause sets. */ | |
78323b5b | 141 | specified_mask |= 1 << cnt; |
c84142e8 UD |
142 | newnames[cnt] = ++cp; |
143 | cp = strchr (cp, ';'); | |
144 | if (cp != NULL) | |
145 | { | |
146 | /* Examine the next clause. */ | |
147 | *cp = '\0'; | |
148 | np = cp + 1; | |
149 | } | |
150 | else | |
151 | /* This was the last clause. We are done. */ | |
152 | break; | |
153 | } | |
154 | ||
78323b5b RM |
155 | if (category_mask &~ specified_mask) |
156 | /* The composite name did not specify all categories we need. */ | |
157 | ERROR_RETURN; | |
c84142e8 UD |
158 | } |
159 | ||
531b71dd | 160 | /* Protect global data. */ |
9a69db29 | 161 | __libc_rwlock_wrlock (__libc_setlocale_lock); |
531b71dd | 162 | |
c84142e8 | 163 | /* Now process all categories we are interested in. */ |
a0fc81e1 | 164 | names_len = 0; |
4b10dd6c | 165 | for (cnt = 0; cnt < __LC_LAST; ++cnt) |
a0fc81e1 RM |
166 | { |
167 | if ((category_mask & 1 << cnt) != 0) | |
168 | { | |
169 | result.__locales[cnt] = _nl_find_locale (locale_path, | |
170 | locale_path_len, | |
171 | cnt, &newnames[cnt]); | |
172 | if (result.__locales[cnt] == NULL) | |
173 | { | |
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]); | |
531b71dd UD |
180 | |
181 | /* Critical section left. */ | |
9a69db29 | 182 | __libc_rwlock_unlock (__libc_setlocale_lock); |
a0fc81e1 RM |
183 | return NULL; |
184 | } | |
185 | ||
186 | if (newnames[cnt] != _nl_C_name) | |
187 | names_len += strlen (newnames[cnt]) + 1; | |
188 | } | |
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; | |
192 | } | |
193 | ||
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) | |
201 | { | |
202 | cnt = __LC_LAST; | |
203 | goto free_cnt_data_and_exit; | |
204 | } | |
c84142e8 | 205 | |
c84142e8 UD |
206 | if (base == NULL) |
207 | { | |
a0fc81e1 RM |
208 | /* Fill in this new structure from scratch. */ |
209 | ||
210 | char *namep = (char *) (result_ptr + 1); | |
c84142e8 | 211 | |
a0fc81e1 | 212 | /* Install copied new names in the new structure's __names array. |
252e7983 | 213 | If resolved to "C", that is already in RESULT.__names to start. */ |
26e40124 | 214 | for (cnt = 0; cnt < __LC_LAST; ++cnt) |
252e7983 | 215 | if ((category_mask & 1 << cnt) != 0 && newnames[cnt] != _nl_C_name) |
26e40124 | 216 | { |
a0fc81e1 RM |
217 | result.__names[cnt] = namep; |
218 | namep = __stpcpy (namep, newnames[cnt]) + 1; | |
26e40124 | 219 | } |
252e7983 RM |
220 | |
221 | *result_ptr = result; | |
c84142e8 UD |
222 | } |
223 | else | |
26e40124 | 224 | { |
a0fc81e1 | 225 | /* We modify the base structure. */ |
26e40124 | 226 | |
a0fc81e1 | 227 | char *namep = (char *) (result_ptr + 1); |
26e40124 | 228 | |
26e40124 | 229 | for (cnt = 0; cnt < __LC_LAST; ++cnt) |
252e7983 | 230 | if ((category_mask & 1 << cnt) != 0) |
26e40124 | 231 | { |
252e7983 RM |
232 | if (base->__locales[cnt]->usage_count != UNDELETABLE) |
233 | /* We can remove the old data. */ | |
234 | _nl_remove_locale (cnt, base->__locales[cnt]); | |
a0fc81e1 | 235 | result_ptr->__locales[cnt] = result.__locales[cnt]; |
252e7983 | 236 | |
a0fc81e1 RM |
237 | if (newnames[cnt] == _nl_C_name) |
238 | result_ptr->__names[cnt] = _nl_C_name; | |
239 | else | |
240 | { | |
241 | result_ptr->__names[cnt] = namep; | |
242 | namep = __stpcpy (namep, newnames[cnt]) + 1; | |
243 | } | |
244 | } | |
245 | else if (cnt != LC_ALL) | |
246 | { | |
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 | |
252 | { | |
253 | result_ptr->__names[cnt] = namep; | |
254 | namep = __stpcpy (namep, result.__names[cnt]) + 1; | |
255 | } | |
26e40124 RM |
256 | } |
257 | ||
a0fc81e1 | 258 | free (base); |
26e40124 | 259 | } |
5db91571 | 260 | |
531b71dd | 261 | /* Critical section left. */ |
9a69db29 | 262 | __libc_rwlock_unlock (__libc_setlocale_lock); |
531b71dd | 263 | |
c84142e8 UD |
264 | /* Update the special members. */ |
265 | update: | |
266 | { | |
267 | union locale_data_value *ctypes = result_ptr->__locales[LC_CTYPE]->values; | |
5bef2820 | 268 | result_ptr->__ctype_b = (const unsigned short int *) |
671ab00d | 269 | ctypes[_NL_ITEM_INDEX (_NL_CTYPE_CLASS)].string + 128; |
5bef2820 | 270 | result_ptr->__ctype_tolower = (const int *) |
671ab00d | 271 | ctypes[_NL_ITEM_INDEX (_NL_CTYPE_TOLOWER)].string + 128; |
5bef2820 | 272 | result_ptr->__ctype_toupper = (const int *) |
671ab00d | 273 | ctypes[_NL_ITEM_INDEX (_NL_CTYPE_TOUPPER)].string + 128; |
c84142e8 UD |
274 | } |
275 | ||
276 | return result_ptr; | |
277 | } | |
30c14c31 | 278 | weak_alias (__newlocale, newlocale) |