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