]> git.ipfire.org Git - thirdparty/glibc.git/blame - locale/setlocale.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / locale / setlocale.c
CommitLineData
f7a9f785 1/* Copyright (C) 1991-2016 Free Software Foundation, Inc.
c84142e8 2 This file is part of the GNU C Library.
28f540f4 3
c84142e8 4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
28f540f4 8
c84142e8
UD
9 The GNU C Library 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 GNU
41bdb6e2 12 Lesser General Public License for more details.
28f540f4 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
28f540f4 17
7a12c6bb
RM
18#include <alloca.h>
19#include <argz.h>
28f540f4 20#include <errno.h>
ec999b8e 21#include <libc-lock.h>
933e73fa 22#include <locale.h>
7a12c6bb
RM
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26
933e73fa
RM
27#include "localeinfo.h"
28
1a0d874e
RM
29#ifdef NL_CURRENT_INDIRECT
30
31/* For each category declare a special external symbol
32 _nl_current_CATEGORY_used with a weak reference.
33 This symbol will is defined in lc-CATEGORY.c and will be linked in
34 if anything uses _nl_current_CATEGORY (also defined in that module).
35 Also use a weak reference for the _nl_current_CATEGORY thread variable. */
36
37# define DEFINE_CATEGORY(category, category_name, items, a) \
38 extern char _nl_current_##category##_used; \
39 weak_extern (_nl_current_##category##_used) \
40 weak_extern (_nl_current_##category)
41# include "categories.def"
42# undef DEFINE_CATEGORY
43
44/* Now define a table of flags based on those special weak symbols' values.
45 _nl_current_used[CATEGORY] will be zero if _nl_current_CATEGORY is not
46 linked in. */
47static char *const _nl_current_used[] =
036cc82f 48 {
1a0d874e
RM
49# define DEFINE_CATEGORY(category, category_name, items, a) \
50 [category] = &_nl_current_##category##_used,
51# include "categories.def"
52# undef DEFINE_CATEGORY
036cc82f 53 };
933e73fa 54
1a0d874e 55# define CATEGORY_USED(category) (_nl_current_used[category] != 0)
30c14c31
RM
56
57#else
58
59/* The shared library always loads all the categories,
60 and the current global settings are kept in _nl_global_locale. */
61
30c14c31
RM
62# define CATEGORY_USED(category) (1)
63
64#endif
65
933e73fa 66
9446614c
UD
67/* Define an array of category names (also the environment variable names). */
68const union catnamestr_t _nl_category_names attribute_hidden =
933e73fa 69 {
9446614c 70 {
4b10dd6c 71#define DEFINE_CATEGORY(category, category_name, items, a) \
9446614c 72 category_name,
933e73fa 73#include "categories.def"
9446614c
UD
74#undef DEFINE_CATEGORY
75 }
0ecb606c 76 };
9446614c
UD
77
78const uint8_t _nl_category_name_idxs[__LC_LAST] attribute_hidden =
79 {
80#define DEFINE_CATEGORY(category, category_name, items, a) \
81 [category] = offsetof (union catnamestr_t, CATNAMEMF (__LINE__)),
82#include "categories.def"
83#undef DEFINE_CATEGORY
84 };
85
933e73fa 86/* An array of their lengths, for convenience. */
9446614c 87const uint8_t _nl_category_name_sizes[] attribute_hidden =
933e73fa 88 {
4b10dd6c 89#define DEFINE_CATEGORY(category, category_name, items, a) \
933e73fa
RM
90 [category] = sizeof (category_name) - 1,
91#include "categories.def"
92#undef DEFINE_CATEGORY
7a12c6bb 93 [LC_ALL] = sizeof ("LC_ALL") - 1
933e73fa 94 };
28f540f4
RM
95
96
0ba454fc
RM
97#ifdef NL_CURRENT_INDIRECT
98# define WEAK_POSTLOAD(postload) weak_extern (postload)
99#else
100# define WEAK_POSTLOAD(postload) /* Need strong refs in static linking. */
101#endif
102
933e73fa
RM
103/* Declare the postload functions used below. */
104#undef NO_POSTLOAD
105#define NO_POSTLOAD _nl_postload_ctype /* Harmless thing known to exist. */
4b10dd6c 106#define DEFINE_CATEGORY(category, category_name, items, postload) \
0ba454fc 107extern void postload (void); WEAK_POSTLOAD (postload)
933e73fa
RM
108#include "categories.def"
109#undef DEFINE_CATEGORY
110#undef NO_POSTLOAD
111
112/* Define an array indexed by category of postload functions to call after
113 loading and installing that category's data. */
a5113b14 114static void (*const _nl_category_postload[]) (void) =
933e73fa 115 {
4b10dd6c 116#define DEFINE_CATEGORY(category, category_name, items, postload) \
933e73fa
RM
117 [category] = postload,
118#include "categories.def"
119#undef DEFINE_CATEGORY
120 };
121
122
a5113b14 123/* Lock for protecting global data. */
9a69db29 124__libc_rwlock_define_initialized (, __libc_setlocale_lock attribute_hidden)
a5113b14 125
111bb972
UD
126/* Defined in loadmsgcat.c. */
127extern int _nl_msg_cat_cntr;
128
933e73fa 129
7a12c6bb
RM
130/* Use this when we come along an error. */
131#define ERROR_RETURN \
132 do { \
c4029823 133 __set_errno (EINVAL); \
7a12c6bb
RM
134 return NULL; \
135 } while (0)
933e73fa 136
7a12c6bb 137
7a12c6bb 138/* Construct a new composite name. */
dd9423a6 139static char *
4b10dd6c 140new_composite_name (int category, const char *newnames[__LC_LAST])
7a12c6bb 141{
e918a7fe 142 size_t last_len = 0;
7a12c6bb
RM
143 size_t cumlen = 0;
144 int i;
145 char *new, *p;
146 int same = 1;
147
4b10dd6c
UD
148 for (i = 0; i < __LC_LAST; ++i)
149 if (i != LC_ALL)
150 {
151 const char *name = (category == LC_ALL ? newnames[i] :
152 category == i ? newnames[0] :
1ce8aaae 153 _nl_global_locale.__names[i]);
4b10dd6c
UD
154 last_len = strlen (name);
155 cumlen += _nl_category_name_sizes[i] + 1 + last_len + 1;
cc9e536d 156 if (same && name != newnames[0] && strcmp (name, newnames[0]) != 0)
4b10dd6c
UD
157 same = 0;
158 }
7a12c6bb
RM
159
160 if (same)
933e73fa 161 {
7a12c6bb 162 /* All the categories use the same name. */
72c74375
UD
163 if (strcmp (newnames[0], _nl_C_name) == 0
164 || strcmp (newnames[0], _nl_POSIX_name) == 0)
7a12c6bb
RM
165 return (char *) _nl_C_name;
166
167 new = malloc (last_len + 1);
7a12c6bb 168
036cc82f 169 return new == NULL ? NULL : memcpy (new, newnames[0], last_len + 1);
933e73fa 170 }
7a12c6bb
RM
171
172 new = malloc (cumlen);
173 if (new == NULL)
174 return NULL;
175 p = new;
4b10dd6c
UD
176 for (i = 0; i < __LC_LAST; ++i)
177 if (i != LC_ALL)
178 {
179 /* Add "CATEGORY=NAME;" to the string. */
180 const char *name = (category == LC_ALL ? newnames[i] :
181 category == i ? newnames[0] :
1ce8aaae 182 _nl_global_locale.__names[i]);
9446614c 183 p = __stpcpy (p, _nl_category_names.str + _nl_category_name_idxs[i]);
4b10dd6c
UD
184 *p++ = '=';
185 p = __stpcpy (p, name);
186 *p++ = ';';
187 }
7a12c6bb
RM
188 p[-1] = '\0'; /* Clobber the last ';'. */
189 return new;
190}
933e73fa 191
933e73fa 192
1ce8aaae 193/* Put NAME in _nl_global_locale.__names. */
2e79213a 194static void
7a12c6bb
RM
195setname (int category, const char *name)
196{
1ce8aaae 197 if (_nl_global_locale.__names[category] == name)
762a2918
UD
198 return;
199
1ce8aaae
RM
200 if (_nl_global_locale.__names[category] != _nl_C_name)
201 free ((char *) _nl_global_locale.__names[category]);
7a12c6bb 202
1ce8aaae 203 _nl_global_locale.__names[category] = name;
7a12c6bb
RM
204}
205
7a12c6bb 206/* Put DATA in *_nl_current[CATEGORY]. */
f1d70dad 207static void
f095bb72 208setdata (int category, struct __locale_data *data)
7a12c6bb 209{
30c14c31 210 if (CATEGORY_USED (category))
933e73fa 211 {
30c14c31 212 _nl_global_locale.__locales[category] = data;
7a12c6bb
RM
213 if (_nl_category_postload[category])
214 (*_nl_category_postload[category]) ();
933e73fa 215 }
7a12c6bb 216}
933e73fa 217
7a12c6bb
RM
218char *
219setlocale (int category, const char *locale)
220{
7a12c6bb
RM
221 char *locale_path;
222 size_t locale_path_len;
d68171ed 223 const char *locpath_var;
7a12c6bb 224 char *composite;
933e73fa 225
7a12c6bb 226 /* Sanity check for CATEGORY argument. */
323fb88d
UD
227 if (__builtin_expect (category, 0) < 0
228 || __builtin_expect (category, 0) >= __LC_LAST)
7a12c6bb
RM
229 ERROR_RETURN;
230
231 /* Does user want name of current locale? */
232 if (locale == NULL)
1ce8aaae 233 return (char *) _nl_global_locale.__names[category];
7a12c6bb 234
9f558b80
UD
235 /* Protect global data. */
236 __libc_rwlock_wrlock (__libc_setlocale_lock);
237
1ce8aaae 238 if (strcmp (locale, _nl_global_locale.__names[category]) == 0)
9f558b80
UD
239 {
240 /* Changing to the same thing. */
241 __libc_rwlock_unlock (__libc_setlocale_lock);
242
243 return (char *) _nl_global_locale.__names[category];
244 }
7a12c6bb
RM
245
246 /* We perhaps really have to load some data. So we determine the
a5113b14
UD
247 path in which to look for the data now. The environment variable
248 `LOCPATH' must only be used when the binary has no SUID or SGID
cb09a2cd
RM
249 bit set. If using the default path, we tell _nl_find_locale
250 by passing null and it can check the canonical locale archive. */
7a12c6bb
RM
251 locale_path = NULL;
252 locale_path_len = 0;
253
74955460 254 locpath_var = getenv ("LOCPATH");
d68171ed 255 if (locpath_var != NULL && locpath_var[0] != '\0')
cb09a2cd
RM
256 {
257 if (__argz_create_sep (locpath_var, ':',
9f558b80
UD
258 &locale_path, &locale_path_len) != 0
259 || __argz_add_sep (&locale_path, &locale_path_len,
260 _nl_default_locale_path, ':') != 0)
261 {
262 __libc_rwlock_unlock (__libc_setlocale_lock);
263 return NULL;
264 }
cb09a2cd 265 }
db2286f6 266
933e73fa
RM
267 if (category == LC_ALL)
268 {
7a12c6bb
RM
269 /* The user wants to set all categories. The desired locales
270 for the individual categories can be selected by using a
271 composite locale name. This is a semi-colon separated list
272 of entries of the form `CATEGORY=VALUE'. */
4b10dd6c 273 const char *newnames[__LC_LAST];
f095bb72 274 struct __locale_data *newdata[__LC_LAST];
d1836456
FW
275 /* Copy of the locale argument, for in-place splitting. */
276 char *locale_copy = NULL;
933e73fa
RM
277
278 /* Set all name pointers to the argument name. */
4b10dd6c
UD
279 for (category = 0; category < __LC_LAST; ++category)
280 if (category != LC_ALL)
281 newnames[category] = (char *) locale;
db2286f6 282
a1ffb40e 283 if (__glibc_unlikely (strchr (locale, ';') != NULL))
933e73fa 284 {
7a12c6bb 285 /* This is a composite name. Make a copy and split it up. */
d1836456
FW
286 locale_copy = strdup (locale);
287 if (__glibc_unlikely (locale_copy == NULL))
288 {
289 __libc_rwlock_unlock (__libc_setlocale_lock);
290 return NULL;
291 }
292 char *np = locale_copy;
7a12c6bb
RM
293 char *cp;
294 int cnt;
933e73fa 295
7a12c6bb 296 while ((cp = strchr (np, '=')) != NULL)
933e73fa 297 {
4b10dd6c
UD
298 for (cnt = 0; cnt < __LC_LAST; ++cnt)
299 if (cnt != LC_ALL
300 && (size_t) (cp - np) == _nl_category_name_sizes[cnt]
9446614c
UD
301 && (memcmp (np, (_nl_category_names.str
302 + _nl_category_name_idxs[cnt]), cp - np)
303 == 0))
933e73fa 304 break;
7a12c6bb 305
4b10dd6c 306 if (cnt == __LC_LAST)
9f558b80
UD
307 {
308 error_return:
309 __libc_rwlock_unlock (__libc_setlocale_lock);
d1836456 310 free (locale_copy);
9f558b80
UD
311
312 /* Bogus category name. */
313 ERROR_RETURN;
314 }
7a12c6bb
RM
315
316 /* Found the category this clause sets. */
317 newnames[cnt] = ++cp;
318 cp = strchr (cp, ';');
319 if (cp != NULL)
933e73fa 320 {
7a12c6bb
RM
321 /* Examine the next clause. */
322 *cp = '\0';
323 np = cp + 1;
933e73fa 324 }
7a12c6bb
RM
325 else
326 /* This was the last clause. We are done. */
327 break;
933e73fa
RM
328 }
329
4b10dd6c
UD
330 for (cnt = 0; cnt < __LC_LAST; ++cnt)
331 if (cnt != LC_ALL && newnames[cnt] == locale)
933e73fa 332 /* The composite name did not specify all categories. */
9f558b80 333 goto error_return;
933e73fa 334 }
19bc17a9 335
933e73fa
RM
336 /* Load the new data for each category. */
337 while (category-- > 0)
ef5d6645
UD
338 if (category != LC_ALL)
339 {
ef5d6645
UD
340 newdata[category] = _nl_find_locale (locale_path, locale_path_len,
341 category,
342 &newnames[category]);
1fb05e3d 343
ef5d6645 344 if (newdata[category] == NULL)
9a411bf5
RM
345 {
346#ifdef NL_CURRENT_INDIRECT
347 if (newnames[category] == _nl_C_name)
348 /* Null because it's the weak value of _nl_C_LC_FOO. */
349 continue;
350#endif
351 break;
352 }
1fb05e3d 353
a31f867a
UD
354 /* We must not simply free a global locale since we have
355 no control over the usage. So we mark it as
356 un-deletable. And yes, the 'if' is needed, the data
357 might be in read-only memory. */
ef5d6645
UD
358 if (newdata[category]->usage_count != UNDELETABLE)
359 newdata[category]->usage_count = UNDELETABLE;
411adb10
UD
360
361 /* Make a copy of locale name. */
362 if (newnames[category] != _nl_C_name)
363 {
a31f867a
UD
364 if (strcmp (newnames[category],
365 _nl_global_locale.__names[category]) == 0)
366 newnames[category] = _nl_global_locale.__names[category];
367 else
368 {
369 newnames[category] = __strdup (newnames[category]);
370 if (newnames[category] == NULL)
371 break;
372 }
411adb10 373 }
ef5d6645 374 }
933e73fa 375
7a12c6bb 376 /* Create new composite name. */
d4c5cf80
UD
377 composite = (category >= 0
378 ? NULL : new_composite_name (LC_ALL, newnames));
379 if (composite != NULL)
933e73fa 380 {
a5113b14 381 /* Now we have loaded all the new data. Put it in place. */
4b10dd6c
UD
382 for (category = 0; category < __LC_LAST; ++category)
383 if (category != LC_ALL)
384 {
385 setdata (category, newdata[category]);
386 setname (category, newnames[category]);
387 }
a5113b14 388 setname (LC_ALL, composite);
111bb972
UD
389
390 /* We successfully loaded a new locale. Let the message catalog
391 functions know about this. */
392 ++_nl_msg_cat_cntr;
933e73fa 393 }
411adb10
UD
394 else
395 for (++category; category < __LC_LAST; ++category)
a31f867a
UD
396 if (category != LC_ALL && newnames[category] != _nl_C_name
397 && newnames[category] != _nl_global_locale.__names[category])
411adb10 398 free ((char *) newnames[category]);
a5113b14
UD
399
400 /* Critical section left. */
9a69db29 401 __libc_rwlock_unlock (__libc_setlocale_lock);
933e73fa 402
d1836456 403 /* Free the resources. */
714a562f 404 free (locale_path);
d1836456 405 free (locale_copy);
714a562f 406
933e73fa
RM
407 return composite;
408 }
409 else
410 {
f095bb72 411 struct __locale_data *newdata = NULL;
650425ce 412 const char *newname[1] = { locale };
7a12c6bb 413
30c14c31 414 if (CATEGORY_USED (category))
933e73fa 415 {
7a12c6bb 416 /* Only actually load the data if anything will use it. */
7a12c6bb 417 newdata = _nl_find_locale (locale_path, locale_path_len, category,
650425ce 418 &newname[0]);
7a12c6bb 419 if (newdata == NULL)
a5113b14 420 goto abort_single;
c84142e8
UD
421
422 /* We must not simply free a global locale since we have no
a2b08ee5
UD
423 control over the usage. So we mark it as un-deletable.
424
9cd83302 425 Note: do not remove the `if', it's necessary to cope with
a2b08ee5 426 the builtin locale data. */
9756dfe1
UD
427 if (newdata->usage_count != UNDELETABLE)
428 newdata->usage_count = UNDELETABLE;
933e73fa
RM
429 }
430
411adb10
UD
431 /* Make a copy of locale name. */
432 if (newname[0] != _nl_C_name)
433 {
1ce8aaae 434 newname[0] = __strdup (newname[0]);
411adb10
UD
435 if (newname[0] == NULL)
436 goto abort_single;
437 }
438
7a12c6bb 439 /* Create new composite name. */
650425ce 440 composite = new_composite_name (category, newname);
7a12c6bb 441 if (composite == NULL)
933e73fa 442 {
411adb10
UD
443 if (newname[0] != _nl_C_name)
444 free ((char *) newname[0]);
445
845dcb57 446 /* Say that we don't have any data loaded. */
a5113b14 447 abort_single:
650425ce 448 newname[0] = NULL;
933e73fa 449 }
a5113b14
UD
450 else
451 {
30c14c31 452 if (CATEGORY_USED (category))
a5113b14 453 setdata (category, newdata);
28f540f4 454
650425ce 455 setname (category, newname[0]);
a5113b14 456 setname (LC_ALL, composite);
111bb972
UD
457
458 /* We successfully loaded a new locale. Let the message catalog
459 functions know about this. */
460 ++_nl_msg_cat_cntr;
a5113b14 461 }
28f540f4 462
a5113b14 463 /* Critical section left. */
9a69db29 464 __libc_rwlock_unlock (__libc_setlocale_lock);
28f540f4 465
714a562f
UD
466 /* Free the resources (the locale path variable. */
467 free (locale_path);
468
650425ce 469 return (char *) newname[0];
933e73fa 470 }
28f540f4 471}
30c14c31 472libc_hidden_def (setlocale)
f84ad0b1 473
7c11c4a1 474static void __libc_freeres_fn_section
1a0d874e 475free_category (int category,
6e9331df 476 struct __locale_data *here, struct __locale_data *c_data)
1a0d874e
RM
477{
478 struct loaded_l10nfile *runp = _nl_locale_file_list[category];
479
480 /* If this category is already "C" don't do anything. */
481 if (here != c_data)
482 {
483 /* We have to be prepared that sometime later we still
484 might need the locale information. */
485 setdata (category, c_data);
486 setname (category, _nl_C_name);
487 }
488
489 while (runp != NULL)
490 {
491 struct loaded_l10nfile *curr = runp;
6e9331df 492 struct __locale_data *data = (struct __locale_data *) runp->data;
1a0d874e
RM
493
494 if (data != NULL && data != c_data)
495 _nl_unload_locale (data);
496 runp = runp->next;
497 free ((char *) curr->filename);
498 free (curr);
499 }
500}
501
7c11c4a1
UD
502/* This is called from iconv/gconv_db.c's free_mem, as locales must
503 be freed before freeing gconv steps arrays. */
504void __libc_freeres_fn_section
505_nl_locale_subfreeres (void)
f84ad0b1 506{
1a0d874e
RM
507#ifdef NL_CURRENT_INDIRECT
508 /* We don't use the loop because we want to have individual weak
509 symbol references here. */
510# define DEFINE_CATEGORY(category, category_name, items, a) \
511 if (CATEGORY_USED (category)) \
512 { \
f095bb72 513 extern struct __locale_data _nl_C_##category; \
1a0d874e
RM
514 weak_extern (_nl_C_##category) \
515 free_category (category, *_nl_current_##category, &_nl_C_##category); \
516 }
517# include "categories.def"
518# undef DEFINE_CATEGORY
519#else
f84ad0b1
UD
520 int category;
521
4b10dd6c
UD
522 for (category = 0; category < __LC_LAST; ++category)
523 if (category != LC_ALL)
1a0d874e
RM
524 free_category (category, _NL_CURRENT_DATA (category),
525 _nl_C_locobj.__locales[category]);
526#endif
f84ad0b1
UD
527
528 setname (LC_ALL, _nl_C_name);
a89a3dab
RM
529
530 /* This frees the data structures associated with the locale archive.
531 The locales from the archive are not in the file list, so we have
532 not called _nl_unload_locale on them above. */
533 _nl_archive_subfreeres ();
f84ad0b1 534}