[BZ #2792]
[glibc.git] / locale / setlocale.c
blobc1b8c3faeceef8cc772a05c77f8cb2e4d7e1ff93
1 /* Copyright (C) 1991, 1992, 1995-2000, 2002, 2003, 2004, 2006
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_lock_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 inline 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 if (strcmp (locale, _nl_global_locale.__names[category]) == 0)
238 /* Changing to the same thing. */
239 return (char *) _nl_global_locale.__names[category];
241 /* We perhaps really have to load some data. So we determine the
242 path in which to look for the data now. The environment variable
243 `LOCPATH' must only be used when the binary has no SUID or SGID
244 bit set. If using the default path, we tell _nl_find_locale
245 by passing null and it can check the canonical locale archive. */
246 locale_path = NULL;
247 locale_path_len = 0;
249 locpath_var = getenv ("LOCPATH");
250 if (locpath_var != NULL && locpath_var[0] != '\0')
252 if (__argz_create_sep (locpath_var, ':',
253 &locale_path, &locale_path_len) != 0)
254 return NULL;
256 if (__argz_add_sep (&locale_path, &locale_path_len,
257 _nl_default_locale_path, ':') != 0)
258 return NULL;
261 if (category == LC_ALL)
263 /* The user wants to set all categories. The desired locales
264 for the individual categories can be selected by using a
265 composite locale name. This is a semi-colon separated list
266 of entries of the form `CATEGORY=VALUE'. */
267 const char *newnames[__LC_LAST];
268 struct locale_data *newdata[__LC_LAST];
270 /* Set all name pointers to the argument name. */
271 for (category = 0; category < __LC_LAST; ++category)
272 if (category != LC_ALL)
273 newnames[category] = (char *) locale;
275 if (__builtin_expect (strchr (locale, ';') != NULL, 0))
277 /* This is a composite name. Make a copy and split it up. */
278 char *np = strdupa (locale);
279 char *cp;
280 int cnt;
282 while ((cp = strchr (np, '=')) != NULL)
284 for (cnt = 0; cnt < __LC_LAST; ++cnt)
285 if (cnt != LC_ALL
286 && (size_t) (cp - np) == _nl_category_name_sizes[cnt]
287 && (memcmp (np, (_nl_category_names.str
288 + _nl_category_name_idxs[cnt]), cp - np)
289 == 0))
290 break;
292 if (cnt == __LC_LAST)
293 /* Bogus category name. */
294 ERROR_RETURN;
296 /* Found the category this clause sets. */
297 newnames[cnt] = ++cp;
298 cp = strchr (cp, ';');
299 if (cp != NULL)
301 /* Examine the next clause. */
302 *cp = '\0';
303 np = cp + 1;
305 else
306 /* This was the last clause. We are done. */
307 break;
310 for (cnt = 0; cnt < __LC_LAST; ++cnt)
311 if (cnt != LC_ALL && newnames[cnt] == locale)
312 /* The composite name did not specify all categories. */
313 ERROR_RETURN;
316 /* Protect global data. */
317 __libc_lock_lock (__libc_setlocale_lock);
319 /* Load the new data for each category. */
320 while (category-- > 0)
321 if (category != LC_ALL)
323 newdata[category] = _nl_find_locale (locale_path, locale_path_len,
324 category,
325 &newnames[category]);
327 if (newdata[category] == NULL)
329 #ifdef NL_CURRENT_INDIRECT
330 if (newnames[category] == _nl_C_name)
331 /* Null because it's the weak value of _nl_C_LC_FOO. */
332 continue;
333 #endif
334 break;
337 /* We must not simply free a global locale since we have
338 no control over the usage. So we mark it as
339 un-deletable. And yes, the 'if' is needed, the data
340 might be in read-only memory. */
341 if (newdata[category]->usage_count != UNDELETABLE)
342 newdata[category]->usage_count = UNDELETABLE;
344 /* Make a copy of locale name. */
345 if (newnames[category] != _nl_C_name)
347 if (strcmp (newnames[category],
348 _nl_global_locale.__names[category]) == 0)
349 newnames[category] = _nl_global_locale.__names[category];
350 else
352 newnames[category] = __strdup (newnames[category]);
353 if (newnames[category] == NULL)
354 break;
359 /* Create new composite name. */
360 composite = (category >= 0
361 ? NULL : new_composite_name (LC_ALL, newnames));
362 if (composite != NULL)
364 /* Now we have loaded all the new data. Put it in place. */
365 for (category = 0; category < __LC_LAST; ++category)
366 if (category != LC_ALL)
368 setdata (category, newdata[category]);
369 setname (category, newnames[category]);
371 setname (LC_ALL, composite);
373 /* We successfully loaded a new locale. Let the message catalog
374 functions know about this. */
375 ++_nl_msg_cat_cntr;
377 else
378 for (++category; category < __LC_LAST; ++category)
379 if (category != LC_ALL && newnames[category] != _nl_C_name
380 && newnames[category] != _nl_global_locale.__names[category])
381 free ((char *) newnames[category]);
383 /* Critical section left. */
384 __libc_lock_unlock (__libc_setlocale_lock);
386 /* Free the resources (the locale path variable). */
387 free (locale_path);
389 return composite;
391 else
393 struct locale_data *newdata = NULL;
394 const char *newname[1] = { locale };
396 /* Protect global data. */
397 __libc_lock_lock (__libc_setlocale_lock);
399 if (CATEGORY_USED (category))
401 /* Only actually load the data if anything will use it. */
402 newdata = _nl_find_locale (locale_path, locale_path_len, category,
403 &newname[0]);
404 if (newdata == NULL)
405 goto abort_single;
407 /* We must not simply free a global locale since we have no
408 control over the usage. So we mark it as un-deletable.
410 Note: do not remove the `if', it's necessary to copy with
411 the builtin locale data. */
412 if (newdata->usage_count != UNDELETABLE)
413 newdata->usage_count = UNDELETABLE;
416 /* Make a copy of locale name. */
417 if (newname[0] != _nl_C_name)
419 newname[0] = __strdup (newname[0]);
420 if (newname[0] == NULL)
421 goto abort_single;
424 /* Create new composite name. */
425 composite = new_composite_name (category, newname);
426 if (composite == NULL)
428 if (newname[0] != _nl_C_name)
429 free ((char *) newname[0]);
431 /* Say that we don't have any data loaded. */
432 abort_single:
433 newname[0] = NULL;
435 else
437 if (CATEGORY_USED (category))
438 setdata (category, newdata);
440 setname (category, newname[0]);
441 setname (LC_ALL, composite);
443 /* We successfully loaded a new locale. Let the message catalog
444 functions know about this. */
445 ++_nl_msg_cat_cntr;
448 /* Critical section left. */
449 __libc_lock_unlock (__libc_setlocale_lock);
451 /* Free the resources (the locale path variable. */
452 free (locale_path);
454 return (char *) newname[0];
457 libc_hidden_def (setlocale)
459 static void __libc_freeres_fn_section
460 free_category (int category,
461 struct locale_data *here, struct locale_data *c_data)
463 struct loaded_l10nfile *runp = _nl_locale_file_list[category];
465 /* If this category is already "C" don't do anything. */
466 if (here != c_data)
468 /* We have to be prepared that sometime later we still
469 might need the locale information. */
470 setdata (category, c_data);
471 setname (category, _nl_C_name);
474 while (runp != NULL)
476 struct loaded_l10nfile *curr = runp;
477 struct locale_data *data = (struct locale_data *) runp->data;
479 if (data != NULL && data != c_data)
480 _nl_unload_locale (data);
481 runp = runp->next;
482 free ((char *) curr->filename);
483 free (curr);
487 /* This is called from iconv/gconv_db.c's free_mem, as locales must
488 be freed before freeing gconv steps arrays. */
489 void __libc_freeres_fn_section
490 _nl_locale_subfreeres (void)
492 #ifdef NL_CURRENT_INDIRECT
493 /* We don't use the loop because we want to have individual weak
494 symbol references here. */
495 # define DEFINE_CATEGORY(category, category_name, items, a) \
496 if (CATEGORY_USED (category)) \
498 extern struct locale_data _nl_C_##category; \
499 weak_extern (_nl_C_##category) \
500 free_category (category, *_nl_current_##category, &_nl_C_##category); \
502 # include "categories.def"
503 # undef DEFINE_CATEGORY
504 #else
505 int category;
507 for (category = 0; category < __LC_LAST; ++category)
508 if (category != LC_ALL)
509 free_category (category, _NL_CURRENT_DATA (category),
510 _nl_C_locobj.__locales[category]);
511 #endif
513 setname (LC_ALL, _nl_C_name);
515 /* This frees the data structures associated with the locale archive.
516 The locales from the archive are not in the file list, so we have
517 not called _nl_unload_locale on them above. */
518 _nl_archive_subfreeres ();