Merge remote branch 'origin/roland/hwcap_mask'
[glibc.git] / locale / setlocale.c
blob46372fdcac0cd49e49e2e03b58b6fc9467f30c55
1 /* Copyright (C) 1991, 1992, 1995-2000, 2002, 2003, 2004, 2006, 2008, 2010
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <alloca.h>
21 #include <argz.h>
22 #include <errno.h>
23 #include <bits/libc-lock.h>
24 #include <locale.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "localeinfo.h"
31 #ifdef NL_CURRENT_INDIRECT
33 /* For each category declare a special external symbol
34 _nl_current_CATEGORY_used with a weak reference.
35 This symbol will is defined in lc-CATEGORY.c and will be linked in
36 if anything uses _nl_current_CATEGORY (also defined in that module).
37 Also use a weak reference for the _nl_current_CATEGORY thread variable. */
39 # define DEFINE_CATEGORY(category, category_name, items, a) \
40 extern char _nl_current_##category##_used; \
41 weak_extern (_nl_current_##category##_used) \
42 weak_extern (_nl_current_##category)
43 # include "categories.def"
44 # undef DEFINE_CATEGORY
46 /* Now define a table of flags based on those special weak symbols' values.
47 _nl_current_used[CATEGORY] will be zero if _nl_current_CATEGORY is not
48 linked in. */
49 static char *const _nl_current_used[] =
51 # define DEFINE_CATEGORY(category, category_name, items, a) \
52 [category] = &_nl_current_##category##_used,
53 # include "categories.def"
54 # undef DEFINE_CATEGORY
57 # define CATEGORY_USED(category) (_nl_current_used[category] != 0)
59 #else
61 /* The shared library always loads all the categories,
62 and the current global settings are kept in _nl_global_locale. */
64 # define CATEGORY_USED(category) (1)
66 #endif
69 /* Define an array of category names (also the environment variable names). */
70 const union catnamestr_t _nl_category_names attribute_hidden =
73 #define DEFINE_CATEGORY(category, category_name, items, a) \
74 category_name,
75 #include "categories.def"
76 #undef DEFINE_CATEGORY
80 const uint8_t _nl_category_name_idxs[__LC_LAST] attribute_hidden =
82 #define DEFINE_CATEGORY(category, category_name, items, a) \
83 [category] = offsetof (union catnamestr_t, CATNAMEMF (__LINE__)),
84 #include "categories.def"
85 #undef DEFINE_CATEGORY
88 /* An array of their lengths, for convenience. */
89 const uint8_t _nl_category_name_sizes[] attribute_hidden =
91 #define DEFINE_CATEGORY(category, category_name, items, a) \
92 [category] = sizeof (category_name) - 1,
93 #include "categories.def"
94 #undef DEFINE_CATEGORY
95 [LC_ALL] = sizeof ("LC_ALL") - 1
99 #ifdef NL_CURRENT_INDIRECT
100 # define WEAK_POSTLOAD(postload) weak_extern (postload)
101 #else
102 # define WEAK_POSTLOAD(postload) /* Need strong refs in static linking. */
103 #endif
105 /* Declare the postload functions used below. */
106 #undef NO_POSTLOAD
107 #define NO_POSTLOAD _nl_postload_ctype /* Harmless thing known to exist. */
108 #define DEFINE_CATEGORY(category, category_name, items, postload) \
109 extern void postload (void); WEAK_POSTLOAD (postload)
110 #include "categories.def"
111 #undef DEFINE_CATEGORY
112 #undef NO_POSTLOAD
114 /* Define an array indexed by category of postload functions to call after
115 loading and installing that category's data. */
116 static void (*const _nl_category_postload[]) (void) =
118 #define DEFINE_CATEGORY(category, category_name, items, postload) \
119 [category] = postload,
120 #include "categories.def"
121 #undef DEFINE_CATEGORY
125 /* Lock for protecting global data. */
126 __libc_rwlock_define_initialized (, __libc_setlocale_lock attribute_hidden)
128 /* Defined in loadmsgcat.c. */
129 extern int _nl_msg_cat_cntr;
132 /* Use this when we come along an error. */
133 #define ERROR_RETURN \
134 do { \
135 __set_errno (EINVAL); \
136 return NULL; \
137 } while (0)
140 /* Construct a new composite name. */
141 static char *
142 new_composite_name (int category, const char *newnames[__LC_LAST])
144 size_t last_len = 0;
145 size_t cumlen = 0;
146 int i;
147 char *new, *p;
148 int same = 1;
150 for (i = 0; i < __LC_LAST; ++i)
151 if (i != LC_ALL)
153 const char *name = (category == LC_ALL ? newnames[i] :
154 category == i ? newnames[0] :
155 _nl_global_locale.__names[i]);
156 last_len = strlen (name);
157 cumlen += _nl_category_name_sizes[i] + 1 + last_len + 1;
158 if (i > 0 && same && strcmp (name, newnames[0]) != 0)
159 same = 0;
162 if (same)
164 /* All the categories use the same name. */
165 if (strcmp (newnames[0], _nl_C_name) == 0
166 || strcmp (newnames[0], _nl_POSIX_name) == 0)
167 return (char *) _nl_C_name;
169 new = malloc (last_len + 1);
171 return new == NULL ? NULL : memcpy (new, newnames[0], last_len + 1);
174 new = malloc (cumlen);
175 if (new == NULL)
176 return NULL;
177 p = new;
178 for (i = 0; i < __LC_LAST; ++i)
179 if (i != LC_ALL)
181 /* Add "CATEGORY=NAME;" to the string. */
182 const char *name = (category == LC_ALL ? newnames[i] :
183 category == i ? newnames[0] :
184 _nl_global_locale.__names[i]);
185 p = __stpcpy (p, _nl_category_names.str + _nl_category_name_idxs[i]);
186 *p++ = '=';
187 p = __stpcpy (p, name);
188 *p++ = ';';
190 p[-1] = '\0'; /* Clobber the last ';'. */
191 return new;
195 /* Put NAME in _nl_global_locale.__names. */
196 static void
197 setname (int category, const char *name)
199 if (_nl_global_locale.__names[category] == name)
200 return;
202 if (_nl_global_locale.__names[category] != _nl_C_name)
203 free ((char *) _nl_global_locale.__names[category]);
205 _nl_global_locale.__names[category] = name;
208 /* Put DATA in *_nl_current[CATEGORY]. */
209 static inline void
210 setdata (int category, struct __locale_data *data)
212 if (CATEGORY_USED (category))
214 _nl_global_locale.__locales[category] = data;
215 if (_nl_category_postload[category])
216 (*_nl_category_postload[category]) ();
220 char *
221 setlocale (int category, const char *locale)
223 char *locale_path;
224 size_t locale_path_len;
225 const char *locpath_var;
226 char *composite;
228 /* Sanity check for CATEGORY argument. */
229 if (__builtin_expect (category, 0) < 0
230 || __builtin_expect (category, 0) >= __LC_LAST)
231 ERROR_RETURN;
233 /* Does user want name of current locale? */
234 if (locale == NULL)
235 return (char *) _nl_global_locale.__names[category];
237 /* Protect global data. */
238 __libc_rwlock_wrlock (__libc_setlocale_lock);
240 if (strcmp (locale, _nl_global_locale.__names[category]) == 0)
242 /* Changing to the same thing. */
243 __libc_rwlock_unlock (__libc_setlocale_lock);
245 return (char *) _nl_global_locale.__names[category];
248 /* We perhaps really have to load some data. So we determine the
249 path in which to look for the data now. The environment variable
250 `LOCPATH' must only be used when the binary has no SUID or SGID
251 bit set. If using the default path, we tell _nl_find_locale
252 by passing null and it can check the canonical locale archive. */
253 locale_path = NULL;
254 locale_path_len = 0;
256 locpath_var = getenv ("LOCPATH");
257 if (locpath_var != NULL && locpath_var[0] != '\0')
259 if (__argz_create_sep (locpath_var, ':',
260 &locale_path, &locale_path_len) != 0
261 || __argz_add_sep (&locale_path, &locale_path_len,
262 _nl_default_locale_path, ':') != 0)
264 __libc_rwlock_unlock (__libc_setlocale_lock);
265 return NULL;
269 if (category == LC_ALL)
271 /* The user wants to set all categories. The desired locales
272 for the individual categories can be selected by using a
273 composite locale name. This is a semi-colon separated list
274 of entries of the form `CATEGORY=VALUE'. */
275 const char *newnames[__LC_LAST];
276 struct __locale_data *newdata[__LC_LAST];
278 /* Set all name pointers to the argument name. */
279 for (category = 0; category < __LC_LAST; ++category)
280 if (category != LC_ALL)
281 newnames[category] = (char *) locale;
283 if (__builtin_expect (strchr (locale, ';') != NULL, 0))
285 /* This is a composite name. Make a copy and split it up. */
286 char *np = strdupa (locale);
287 char *cp;
288 int cnt;
290 while ((cp = strchr (np, '=')) != NULL)
292 for (cnt = 0; cnt < __LC_LAST; ++cnt)
293 if (cnt != LC_ALL
294 && (size_t) (cp - np) == _nl_category_name_sizes[cnt]
295 && (memcmp (np, (_nl_category_names.str
296 + _nl_category_name_idxs[cnt]), cp - np)
297 == 0))
298 break;
300 if (cnt == __LC_LAST)
302 error_return:
303 __libc_rwlock_unlock (__libc_setlocale_lock);
305 /* Bogus category name. */
306 ERROR_RETURN;
309 /* Found the category this clause sets. */
310 newnames[cnt] = ++cp;
311 cp = strchr (cp, ';');
312 if (cp != NULL)
314 /* Examine the next clause. */
315 *cp = '\0';
316 np = cp + 1;
318 else
319 /* This was the last clause. We are done. */
320 break;
323 for (cnt = 0; cnt < __LC_LAST; ++cnt)
324 if (cnt != LC_ALL && newnames[cnt] == locale)
325 /* The composite name did not specify all categories. */
326 goto error_return;
329 /* Load the new data for each category. */
330 while (category-- > 0)
331 if (category != LC_ALL)
333 newdata[category] = _nl_find_locale (locale_path, locale_path_len,
334 category,
335 &newnames[category]);
337 if (newdata[category] == NULL)
339 #ifdef NL_CURRENT_INDIRECT
340 if (newnames[category] == _nl_C_name)
341 /* Null because it's the weak value of _nl_C_LC_FOO. */
342 continue;
343 #endif
344 break;
347 /* We must not simply free a global locale since we have
348 no control over the usage. So we mark it as
349 un-deletable. And yes, the 'if' is needed, the data
350 might be in read-only memory. */
351 if (newdata[category]->usage_count != UNDELETABLE)
352 newdata[category]->usage_count = UNDELETABLE;
354 /* Make a copy of locale name. */
355 if (newnames[category] != _nl_C_name)
357 if (strcmp (newnames[category],
358 _nl_global_locale.__names[category]) == 0)
359 newnames[category] = _nl_global_locale.__names[category];
360 else
362 newnames[category] = __strdup (newnames[category]);
363 if (newnames[category] == NULL)
364 break;
369 /* Create new composite name. */
370 composite = (category >= 0
371 ? NULL : new_composite_name (LC_ALL, newnames));
372 if (composite != NULL)
374 /* Now we have loaded all the new data. Put it in place. */
375 for (category = 0; category < __LC_LAST; ++category)
376 if (category != LC_ALL)
378 setdata (category, newdata[category]);
379 setname (category, newnames[category]);
381 setname (LC_ALL, composite);
383 /* We successfully loaded a new locale. Let the message catalog
384 functions know about this. */
385 ++_nl_msg_cat_cntr;
387 else
388 for (++category; category < __LC_LAST; ++category)
389 if (category != LC_ALL && newnames[category] != _nl_C_name
390 && newnames[category] != _nl_global_locale.__names[category])
391 free ((char *) newnames[category]);
393 /* Critical section left. */
394 __libc_rwlock_unlock (__libc_setlocale_lock);
396 /* Free the resources (the locale path variable). */
397 free (locale_path);
399 return composite;
401 else
403 struct __locale_data *newdata = NULL;
404 const char *newname[1] = { locale };
406 if (CATEGORY_USED (category))
408 /* Only actually load the data if anything will use it. */
409 newdata = _nl_find_locale (locale_path, locale_path_len, category,
410 &newname[0]);
411 if (newdata == NULL)
412 goto abort_single;
414 /* We must not simply free a global locale since we have no
415 control over the usage. So we mark it as un-deletable.
417 Note: do not remove the `if', it's necessary to copy with
418 the builtin locale data. */
419 if (newdata->usage_count != UNDELETABLE)
420 newdata->usage_count = UNDELETABLE;
423 /* Make a copy of locale name. */
424 if (newname[0] != _nl_C_name)
426 newname[0] = __strdup (newname[0]);
427 if (newname[0] == NULL)
428 goto abort_single;
431 /* Create new composite name. */
432 composite = new_composite_name (category, newname);
433 if (composite == NULL)
435 if (newname[0] != _nl_C_name)
436 free ((char *) newname[0]);
438 /* Say that we don't have any data loaded. */
439 abort_single:
440 newname[0] = NULL;
442 else
444 if (CATEGORY_USED (category))
445 setdata (category, newdata);
447 setname (category, newname[0]);
448 setname (LC_ALL, composite);
450 /* We successfully loaded a new locale. Let the message catalog
451 functions know about this. */
452 ++_nl_msg_cat_cntr;
455 /* Critical section left. */
456 __libc_rwlock_unlock (__libc_setlocale_lock);
458 /* Free the resources (the locale path variable. */
459 free (locale_path);
461 return (char *) newname[0];
464 libc_hidden_def (setlocale)
466 static void __libc_freeres_fn_section
467 free_category (int category,
468 struct __locale_data *here, struct __locale_data *c_data)
470 struct loaded_l10nfile *runp = _nl_locale_file_list[category];
472 /* If this category is already "C" don't do anything. */
473 if (here != c_data)
475 /* We have to be prepared that sometime later we still
476 might need the locale information. */
477 setdata (category, c_data);
478 setname (category, _nl_C_name);
481 while (runp != NULL)
483 struct loaded_l10nfile *curr = runp;
484 struct __locale_data *data = (struct __locale_data *) runp->data;
486 if (data != NULL && data != c_data)
487 _nl_unload_locale (data);
488 runp = runp->next;
489 free ((char *) curr->filename);
490 free (curr);
494 /* This is called from iconv/gconv_db.c's free_mem, as locales must
495 be freed before freeing gconv steps arrays. */
496 void __libc_freeres_fn_section
497 _nl_locale_subfreeres (void)
499 #ifdef NL_CURRENT_INDIRECT
500 /* We don't use the loop because we want to have individual weak
501 symbol references here. */
502 # define DEFINE_CATEGORY(category, category_name, items, a) \
503 if (CATEGORY_USED (category)) \
505 extern struct __locale_data _nl_C_##category; \
506 weak_extern (_nl_C_##category) \
507 free_category (category, *_nl_current_##category, &_nl_C_##category); \
509 # include "categories.def"
510 # undef DEFINE_CATEGORY
511 #else
512 int category;
514 for (category = 0; category < __LC_LAST; ++category)
515 if (category != LC_ALL)
516 free_category (category, _NL_CURRENT_DATA (category),
517 _nl_C_locobj.__locales[category]);
518 #endif
520 setname (LC_ALL, _nl_C_name);
522 /* This frees the data structures associated with the locale archive.
523 The locales from the archive are not in the file list, so we have
524 not called _nl_unload_locale on them above. */
525 _nl_archive_subfreeres ();