]> git.ipfire.org Git - thirdparty/glibc.git/blame - locale/setlocale.c
Use glibc_likely instead __builtin_expect.
[thirdparty/glibc.git] / locale / setlocale.c
CommitLineData
d4697bc9 1/* Copyright (C) 1991-2014 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>
5107cf1d 21#include <bits/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];
933e73fa
RM
275
276 /* Set all name pointers to the argument name. */
4b10dd6c
UD
277 for (category = 0; category < __LC_LAST; ++category)
278 if (category != LC_ALL)
279 newnames[category] = (char *) locale;
db2286f6 280
a1ffb40e 281 if (__glibc_unlikely (strchr (locale, ';') != NULL))
933e73fa 282 {
7a12c6bb
RM
283 /* This is a composite name. Make a copy and split it up. */
284 char *np = strdupa (locale);
285 char *cp;
286 int cnt;
933e73fa 287
7a12c6bb 288 while ((cp = strchr (np, '=')) != NULL)
933e73fa 289 {
4b10dd6c
UD
290 for (cnt = 0; cnt < __LC_LAST; ++cnt)
291 if (cnt != LC_ALL
292 && (size_t) (cp - np) == _nl_category_name_sizes[cnt]
9446614c
UD
293 && (memcmp (np, (_nl_category_names.str
294 + _nl_category_name_idxs[cnt]), cp - np)
295 == 0))
933e73fa 296 break;
7a12c6bb 297
4b10dd6c 298 if (cnt == __LC_LAST)
9f558b80
UD
299 {
300 error_return:
301 __libc_rwlock_unlock (__libc_setlocale_lock);
302
303 /* Bogus category name. */
304 ERROR_RETURN;
305 }
7a12c6bb
RM
306
307 /* Found the category this clause sets. */
308 newnames[cnt] = ++cp;
309 cp = strchr (cp, ';');
310 if (cp != NULL)
933e73fa 311 {
7a12c6bb
RM
312 /* Examine the next clause. */
313 *cp = '\0';
314 np = cp + 1;
933e73fa 315 }
7a12c6bb
RM
316 else
317 /* This was the last clause. We are done. */
318 break;
933e73fa
RM
319 }
320
4b10dd6c
UD
321 for (cnt = 0; cnt < __LC_LAST; ++cnt)
322 if (cnt != LC_ALL && newnames[cnt] == locale)
933e73fa 323 /* The composite name did not specify all categories. */
9f558b80 324 goto error_return;
933e73fa 325 }
19bc17a9 326
933e73fa
RM
327 /* Load the new data for each category. */
328 while (category-- > 0)
ef5d6645
UD
329 if (category != LC_ALL)
330 {
ef5d6645
UD
331 newdata[category] = _nl_find_locale (locale_path, locale_path_len,
332 category,
333 &newnames[category]);
1fb05e3d 334
ef5d6645 335 if (newdata[category] == NULL)
9a411bf5
RM
336 {
337#ifdef NL_CURRENT_INDIRECT
338 if (newnames[category] == _nl_C_name)
339 /* Null because it's the weak value of _nl_C_LC_FOO. */
340 continue;
341#endif
342 break;
343 }
1fb05e3d 344
a31f867a
UD
345 /* We must not simply free a global locale since we have
346 no control over the usage. So we mark it as
347 un-deletable. And yes, the 'if' is needed, the data
348 might be in read-only memory. */
ef5d6645
UD
349 if (newdata[category]->usage_count != UNDELETABLE)
350 newdata[category]->usage_count = UNDELETABLE;
411adb10
UD
351
352 /* Make a copy of locale name. */
353 if (newnames[category] != _nl_C_name)
354 {
a31f867a
UD
355 if (strcmp (newnames[category],
356 _nl_global_locale.__names[category]) == 0)
357 newnames[category] = _nl_global_locale.__names[category];
358 else
359 {
360 newnames[category] = __strdup (newnames[category]);
361 if (newnames[category] == NULL)
362 break;
363 }
411adb10 364 }
ef5d6645 365 }
933e73fa 366
7a12c6bb 367 /* Create new composite name. */
d4c5cf80
UD
368 composite = (category >= 0
369 ? NULL : new_composite_name (LC_ALL, newnames));
370 if (composite != NULL)
933e73fa 371 {
a5113b14 372 /* Now we have loaded all the new data. Put it in place. */
4b10dd6c
UD
373 for (category = 0; category < __LC_LAST; ++category)
374 if (category != LC_ALL)
375 {
376 setdata (category, newdata[category]);
377 setname (category, newnames[category]);
378 }
a5113b14 379 setname (LC_ALL, composite);
111bb972
UD
380
381 /* We successfully loaded a new locale. Let the message catalog
382 functions know about this. */
383 ++_nl_msg_cat_cntr;
933e73fa 384 }
411adb10
UD
385 else
386 for (++category; category < __LC_LAST; ++category)
a31f867a
UD
387 if (category != LC_ALL && newnames[category] != _nl_C_name
388 && newnames[category] != _nl_global_locale.__names[category])
411adb10 389 free ((char *) newnames[category]);
a5113b14
UD
390
391 /* Critical section left. */
9a69db29 392 __libc_rwlock_unlock (__libc_setlocale_lock);
933e73fa 393
e77fa918 394 /* Free the resources (the locale path variable). */
714a562f
UD
395 free (locale_path);
396
933e73fa
RM
397 return composite;
398 }
399 else
400 {
f095bb72 401 struct __locale_data *newdata = NULL;
650425ce 402 const char *newname[1] = { locale };
7a12c6bb 403
30c14c31 404 if (CATEGORY_USED (category))
933e73fa 405 {
7a12c6bb 406 /* Only actually load the data if anything will use it. */
7a12c6bb 407 newdata = _nl_find_locale (locale_path, locale_path_len, category,
650425ce 408 &newname[0]);
7a12c6bb 409 if (newdata == NULL)
a5113b14 410 goto abort_single;
c84142e8
UD
411
412 /* We must not simply free a global locale since we have no
a2b08ee5
UD
413 control over the usage. So we mark it as un-deletable.
414
9cd83302 415 Note: do not remove the `if', it's necessary to cope with
a2b08ee5 416 the builtin locale data. */
9756dfe1
UD
417 if (newdata->usage_count != UNDELETABLE)
418 newdata->usage_count = UNDELETABLE;
933e73fa
RM
419 }
420
411adb10
UD
421 /* Make a copy of locale name. */
422 if (newname[0] != _nl_C_name)
423 {
1ce8aaae 424 newname[0] = __strdup (newname[0]);
411adb10
UD
425 if (newname[0] == NULL)
426 goto abort_single;
427 }
428
7a12c6bb 429 /* Create new composite name. */
650425ce 430 composite = new_composite_name (category, newname);
7a12c6bb 431 if (composite == NULL)
933e73fa 432 {
411adb10
UD
433 if (newname[0] != _nl_C_name)
434 free ((char *) newname[0]);
435
845dcb57 436 /* Say that we don't have any data loaded. */
a5113b14 437 abort_single:
650425ce 438 newname[0] = NULL;
933e73fa 439 }
a5113b14
UD
440 else
441 {
30c14c31 442 if (CATEGORY_USED (category))
a5113b14 443 setdata (category, newdata);
28f540f4 444
650425ce 445 setname (category, newname[0]);
a5113b14 446 setname (LC_ALL, composite);
111bb972
UD
447
448 /* We successfully loaded a new locale. Let the message catalog
449 functions know about this. */
450 ++_nl_msg_cat_cntr;
a5113b14 451 }
28f540f4 452
a5113b14 453 /* Critical section left. */
9a69db29 454 __libc_rwlock_unlock (__libc_setlocale_lock);
28f540f4 455
714a562f
UD
456 /* Free the resources (the locale path variable. */
457 free (locale_path);
458
650425ce 459 return (char *) newname[0];
933e73fa 460 }
28f540f4 461}
30c14c31 462libc_hidden_def (setlocale)
f84ad0b1 463
7c11c4a1 464static void __libc_freeres_fn_section
1a0d874e 465free_category (int category,
6e9331df 466 struct __locale_data *here, struct __locale_data *c_data)
1a0d874e
RM
467{
468 struct loaded_l10nfile *runp = _nl_locale_file_list[category];
469
470 /* If this category is already "C" don't do anything. */
471 if (here != c_data)
472 {
473 /* We have to be prepared that sometime later we still
474 might need the locale information. */
475 setdata (category, c_data);
476 setname (category, _nl_C_name);
477 }
478
479 while (runp != NULL)
480 {
481 struct loaded_l10nfile *curr = runp;
6e9331df 482 struct __locale_data *data = (struct __locale_data *) runp->data;
1a0d874e
RM
483
484 if (data != NULL && data != c_data)
485 _nl_unload_locale (data);
486 runp = runp->next;
487 free ((char *) curr->filename);
488 free (curr);
489 }
490}
491
7c11c4a1
UD
492/* This is called from iconv/gconv_db.c's free_mem, as locales must
493 be freed before freeing gconv steps arrays. */
494void __libc_freeres_fn_section
495_nl_locale_subfreeres (void)
f84ad0b1 496{
1a0d874e
RM
497#ifdef NL_CURRENT_INDIRECT
498 /* We don't use the loop because we want to have individual weak
499 symbol references here. */
500# define DEFINE_CATEGORY(category, category_name, items, a) \
501 if (CATEGORY_USED (category)) \
502 { \
f095bb72 503 extern struct __locale_data _nl_C_##category; \
1a0d874e
RM
504 weak_extern (_nl_C_##category) \
505 free_category (category, *_nl_current_##category, &_nl_C_##category); \
506 }
507# include "categories.def"
508# undef DEFINE_CATEGORY
509#else
f84ad0b1
UD
510 int category;
511
4b10dd6c
UD
512 for (category = 0; category < __LC_LAST; ++category)
513 if (category != LC_ALL)
1a0d874e
RM
514 free_category (category, _NL_CURRENT_DATA (category),
515 _nl_C_locobj.__locales[category]);
516#endif
f84ad0b1
UD
517
518 setname (LC_ALL, _nl_C_name);
a89a3dab
RM
519
520 /* This frees the data structures associated with the locale archive.
521 The locales from the archive are not in the file list, so we have
522 not called _nl_unload_locale on them above. */
523 _nl_archive_subfreeres ();
f84ad0b1 524}