]> git.ipfire.org Git - thirdparty/glibc.git/blob - locale/setlocale.c
Replace FSF snail mail address with URLs.
[thirdparty/glibc.git] / locale / setlocale.c
1 /* Copyright (C) 1991, 1992, 1995-2000, 2002, 2003, 2004, 2006, 2008, 2010, 2011
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
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
13 Lesser General Public License for more details.
14
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 <http://www.gnu.org/licenses/>. */
18
19 #include <alloca.h>
20 #include <argz.h>
21 #include <errno.h>
22 #include <bits/libc-lock.h>
23 #include <locale.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "localeinfo.h"
29
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. */
48 static char *const _nl_current_used[] =
49 {
50 # define DEFINE_CATEGORY(category, category_name, items, a) \
51 [category] = &_nl_current_##category##_used,
52 # include "categories.def"
53 # undef DEFINE_CATEGORY
54 };
55
56 # define CATEGORY_USED(category) (_nl_current_used[category] != 0)
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
63 # define CATEGORY_USED(category) (1)
64
65 #endif
66
67
68 /* Define an array of category names (also the environment variable names). */
69 const union catnamestr_t _nl_category_names attribute_hidden =
70 {
71 {
72 #define DEFINE_CATEGORY(category, category_name, items, a) \
73 category_name,
74 #include "categories.def"
75 #undef DEFINE_CATEGORY
76 }
77 };
78
79 const 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
87 /* An array of their lengths, for convenience. */
88 const uint8_t _nl_category_name_sizes[] attribute_hidden =
89 {
90 #define DEFINE_CATEGORY(category, category_name, items, a) \
91 [category] = sizeof (category_name) - 1,
92 #include "categories.def"
93 #undef DEFINE_CATEGORY
94 [LC_ALL] = sizeof ("LC_ALL") - 1
95 };
96
97
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
104 /* Declare the postload functions used below. */
105 #undef NO_POSTLOAD
106 #define NO_POSTLOAD _nl_postload_ctype /* Harmless thing known to exist. */
107 #define DEFINE_CATEGORY(category, category_name, items, postload) \
108 extern void postload (void); WEAK_POSTLOAD (postload)
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. */
115 static void (*const _nl_category_postload[]) (void) =
116 {
117 #define DEFINE_CATEGORY(category, category_name, items, postload) \
118 [category] = postload,
119 #include "categories.def"
120 #undef DEFINE_CATEGORY
121 };
122
123
124 /* Lock for protecting global data. */
125 __libc_rwlock_define_initialized (, __libc_setlocale_lock attribute_hidden)
126
127 /* Defined in loadmsgcat.c. */
128 extern int _nl_msg_cat_cntr;
129
130
131 /* Use this when we come along an error. */
132 #define ERROR_RETURN \
133 do { \
134 __set_errno (EINVAL); \
135 return NULL; \
136 } while (0)
137
138
139 /* Construct a new composite name. */
140 static char *
141 new_composite_name (int category, const char *newnames[__LC_LAST])
142 {
143 size_t last_len = 0;
144 size_t cumlen = 0;
145 int i;
146 char *new, *p;
147 int same = 1;
148
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] :
154 _nl_global_locale.__names[i]);
155 last_len = strlen (name);
156 cumlen += _nl_category_name_sizes[i] + 1 + last_len + 1;
157 if (same && name != newnames[0] && strcmp (name, newnames[0]) != 0)
158 same = 0;
159 }
160
161 if (same)
162 {
163 /* All the categories use the same name. */
164 if (strcmp (newnames[0], _nl_C_name) == 0
165 || strcmp (newnames[0], _nl_POSIX_name) == 0)
166 return (char *) _nl_C_name;
167
168 new = malloc (last_len + 1);
169
170 return new == NULL ? NULL : memcpy (new, newnames[0], last_len + 1);
171 }
172
173 new = malloc (cumlen);
174 if (new == NULL)
175 return NULL;
176 p = new;
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] :
183 _nl_global_locale.__names[i]);
184 p = __stpcpy (p, _nl_category_names.str + _nl_category_name_idxs[i]);
185 *p++ = '=';
186 p = __stpcpy (p, name);
187 *p++ = ';';
188 }
189 p[-1] = '\0'; /* Clobber the last ';'. */
190 return new;
191 }
192
193
194 /* Put NAME in _nl_global_locale.__names. */
195 static void
196 setname (int category, const char *name)
197 {
198 if (_nl_global_locale.__names[category] == name)
199 return;
200
201 if (_nl_global_locale.__names[category] != _nl_C_name)
202 free ((char *) _nl_global_locale.__names[category]);
203
204 _nl_global_locale.__names[category] = name;
205 }
206
207 /* Put DATA in *_nl_current[CATEGORY]. */
208 static inline void
209 setdata (int category, struct __locale_data *data)
210 {
211 if (CATEGORY_USED (category))
212 {
213 _nl_global_locale.__locales[category] = data;
214 if (_nl_category_postload[category])
215 (*_nl_category_postload[category]) ();
216 }
217 }
218
219 char *
220 setlocale (int category, const char *locale)
221 {
222 char *locale_path;
223 size_t locale_path_len;
224 const char *locpath_var;
225 char *composite;
226
227 /* Sanity check for CATEGORY argument. */
228 if (__builtin_expect (category, 0) < 0
229 || __builtin_expect (category, 0) >= __LC_LAST)
230 ERROR_RETURN;
231
232 /* Does user want name of current locale? */
233 if (locale == NULL)
234 return (char *) _nl_global_locale.__names[category];
235
236 /* Protect global data. */
237 __libc_rwlock_wrlock (__libc_setlocale_lock);
238
239 if (strcmp (locale, _nl_global_locale.__names[category]) == 0)
240 {
241 /* Changing to the same thing. */
242 __libc_rwlock_unlock (__libc_setlocale_lock);
243
244 return (char *) _nl_global_locale.__names[category];
245 }
246
247 /* We perhaps really have to load some data. So we determine the
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
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. */
252 locale_path = NULL;
253 locale_path_len = 0;
254
255 locpath_var = getenv ("LOCPATH");
256 if (locpath_var != NULL && locpath_var[0] != '\0')
257 {
258 if (__argz_create_sep (locpath_var, ':',
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 }
266 }
267
268 if (category == LC_ALL)
269 {
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'. */
274 const char *newnames[__LC_LAST];
275 struct __locale_data *newdata[__LC_LAST];
276
277 /* Set all name pointers to the argument name. */
278 for (category = 0; category < __LC_LAST; ++category)
279 if (category != LC_ALL)
280 newnames[category] = (char *) locale;
281
282 if (__builtin_expect (strchr (locale, ';') != NULL, 0))
283 {
284 /* This is a composite name. Make a copy and split it up. */
285 char *np = strdupa (locale);
286 char *cp;
287 int cnt;
288
289 while ((cp = strchr (np, '=')) != NULL)
290 {
291 for (cnt = 0; cnt < __LC_LAST; ++cnt)
292 if (cnt != LC_ALL
293 && (size_t) (cp - np) == _nl_category_name_sizes[cnt]
294 && (memcmp (np, (_nl_category_names.str
295 + _nl_category_name_idxs[cnt]), cp - np)
296 == 0))
297 break;
298
299 if (cnt == __LC_LAST)
300 {
301 error_return:
302 __libc_rwlock_unlock (__libc_setlocale_lock);
303
304 /* Bogus category name. */
305 ERROR_RETURN;
306 }
307
308 /* Found the category this clause sets. */
309 newnames[cnt] = ++cp;
310 cp = strchr (cp, ';');
311 if (cp != NULL)
312 {
313 /* Examine the next clause. */
314 *cp = '\0';
315 np = cp + 1;
316 }
317 else
318 /* This was the last clause. We are done. */
319 break;
320 }
321
322 for (cnt = 0; cnt < __LC_LAST; ++cnt)
323 if (cnt != LC_ALL && newnames[cnt] == locale)
324 /* The composite name did not specify all categories. */
325 goto error_return;
326 }
327
328 /* Load the new data for each category. */
329 while (category-- > 0)
330 if (category != LC_ALL)
331 {
332 newdata[category] = _nl_find_locale (locale_path, locale_path_len,
333 category,
334 &newnames[category]);
335
336 if (newdata[category] == NULL)
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 }
345
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. */
350 if (newdata[category]->usage_count != UNDELETABLE)
351 newdata[category]->usage_count = UNDELETABLE;
352
353 /* Make a copy of locale name. */
354 if (newnames[category] != _nl_C_name)
355 {
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 }
365 }
366 }
367
368 /* Create new composite name. */
369 composite = (category >= 0
370 ? NULL : new_composite_name (LC_ALL, newnames));
371 if (composite != NULL)
372 {
373 /* Now we have loaded all the new data. Put it in place. */
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 }
380 setname (LC_ALL, composite);
381
382 /* We successfully loaded a new locale. Let the message catalog
383 functions know about this. */
384 ++_nl_msg_cat_cntr;
385 }
386 else
387 for (++category; category < __LC_LAST; ++category)
388 if (category != LC_ALL && newnames[category] != _nl_C_name
389 && newnames[category] != _nl_global_locale.__names[category])
390 free ((char *) newnames[category]);
391
392 /* Critical section left. */
393 __libc_rwlock_unlock (__libc_setlocale_lock);
394
395 /* Free the resources (the locale path variable). */
396 free (locale_path);
397
398 return composite;
399 }
400 else
401 {
402 struct __locale_data *newdata = NULL;
403 const char *newname[1] = { locale };
404
405 if (CATEGORY_USED (category))
406 {
407 /* Only actually load the data if anything will use it. */
408 newdata = _nl_find_locale (locale_path, locale_path_len, category,
409 &newname[0]);
410 if (newdata == NULL)
411 goto abort_single;
412
413 /* We must not simply free a global locale since we have no
414 control over the usage. So we mark it as un-deletable.
415
416 Note: do not remove the `if', it's necessary to copy with
417 the builtin locale data. */
418 if (newdata->usage_count != UNDELETABLE)
419 newdata->usage_count = UNDELETABLE;
420 }
421
422 /* Make a copy of locale name. */
423 if (newname[0] != _nl_C_name)
424 {
425 newname[0] = __strdup (newname[0]);
426 if (newname[0] == NULL)
427 goto abort_single;
428 }
429
430 /* Create new composite name. */
431 composite = new_composite_name (category, newname);
432 if (composite == NULL)
433 {
434 if (newname[0] != _nl_C_name)
435 free ((char *) newname[0]);
436
437 /* Say that we don't have any data loaded. */
438 abort_single:
439 newname[0] = NULL;
440 }
441 else
442 {
443 if (CATEGORY_USED (category))
444 setdata (category, newdata);
445
446 setname (category, newname[0]);
447 setname (LC_ALL, composite);
448
449 /* We successfully loaded a new locale. Let the message catalog
450 functions know about this. */
451 ++_nl_msg_cat_cntr;
452 }
453
454 /* Critical section left. */
455 __libc_rwlock_unlock (__libc_setlocale_lock);
456
457 /* Free the resources (the locale path variable. */
458 free (locale_path);
459
460 return (char *) newname[0];
461 }
462 }
463 libc_hidden_def (setlocale)
464
465 static void __libc_freeres_fn_section
466 free_category (int category,
467 struct __locale_data *here, struct __locale_data *c_data)
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;
483 struct __locale_data *data = (struct __locale_data *) runp->data;
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
493 /* This is called from iconv/gconv_db.c's free_mem, as locales must
494 be freed before freeing gconv steps arrays. */
495 void __libc_freeres_fn_section
496 _nl_locale_subfreeres (void)
497 {
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 { \
504 extern struct __locale_data _nl_C_##category; \
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
511 int category;
512
513 for (category = 0; category < __LC_LAST; ++category)
514 if (category != LC_ALL)
515 free_category (category, _NL_CURRENT_DATA (category),
516 _nl_C_locobj.__locales[category]);
517 #endif
518
519 setname (LC_ALL, _nl_C_name);
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 ();
525 }