Update.
[glibc.git] / locale / setlocale.c
blobe6db70d4c75719685835786a14278271e11844cd
1 /* Copyright (C) 1991, 1992, 1995-2000, 2002, 2003, 2004
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 indexed by integral category. */
71 const char *const _nl_category_names[] =
73 #define DEFINE_CATEGORY(category, category_name, items, a) \
74 [category] = category_name,
75 #include "categories.def"
76 #undef DEFINE_CATEGORY
77 [LC_ALL] = "LC_ALL"
79 /* An array of their lengths, for convenience. */
80 const size_t _nl_category_name_sizes[] =
82 #define DEFINE_CATEGORY(category, category_name, items, a) \
83 [category] = sizeof (category_name) - 1,
84 #include "categories.def"
85 #undef DEFINE_CATEGORY
86 [LC_ALL] = sizeof ("LC_ALL") - 1
90 #ifdef NL_CURRENT_INDIRECT
91 # define WEAK_POSTLOAD(postload) weak_extern (postload)
92 #else
93 # define WEAK_POSTLOAD(postload) /* Need strong refs in static linking. */
94 #endif
96 /* Declare the postload functions used below. */
97 #undef NO_POSTLOAD
98 #define NO_POSTLOAD _nl_postload_ctype /* Harmless thing known to exist. */
99 #define DEFINE_CATEGORY(category, category_name, items, postload) \
100 extern void postload (void); WEAK_POSTLOAD (postload)
101 #include "categories.def"
102 #undef DEFINE_CATEGORY
103 #undef NO_POSTLOAD
105 /* Define an array indexed by category of postload functions to call after
106 loading and installing that category's data. */
107 static void (*const _nl_category_postload[]) (void) =
109 #define DEFINE_CATEGORY(category, category_name, items, postload) \
110 [category] = postload,
111 #include "categories.def"
112 #undef DEFINE_CATEGORY
116 /* Lock for protecting global data. */
117 __libc_lock_define_initialized (, __libc_setlocale_lock attribute_hidden)
119 /* Defined in loadmsgcat.c. */
120 extern int _nl_msg_cat_cntr;
123 /* Use this when we come along an error. */
124 #define ERROR_RETURN \
125 do { \
126 __set_errno (EINVAL); \
127 return NULL; \
128 } while (0)
131 /* Construct a new composite name. */
132 static char *
133 new_composite_name (int category, const char *newnames[__LC_LAST])
135 size_t last_len = 0;
136 size_t cumlen = 0;
137 int i;
138 char *new, *p;
139 int same = 1;
141 for (i = 0; i < __LC_LAST; ++i)
142 if (i != LC_ALL)
144 const char *name = (category == LC_ALL ? newnames[i] :
145 category == i ? newnames[0] :
146 _nl_global_locale.__names[i]);
147 last_len = strlen (name);
148 cumlen += _nl_category_name_sizes[i] + 1 + last_len + 1;
149 if (i > 0 && same && strcmp (name, newnames[0]) != 0)
150 same = 0;
153 if (same)
155 /* All the categories use the same name. */
156 if (strcmp (newnames[0], _nl_C_name) == 0
157 || strcmp (newnames[0], _nl_POSIX_name) == 0)
158 return (char *) _nl_C_name;
160 new = malloc (last_len + 1);
162 return new == NULL ? NULL : memcpy (new, newnames[0], last_len + 1);
165 new = malloc (cumlen);
166 if (new == NULL)
167 return NULL;
168 p = new;
169 for (i = 0; i < __LC_LAST; ++i)
170 if (i != LC_ALL)
172 /* Add "CATEGORY=NAME;" to the string. */
173 const char *name = (category == LC_ALL ? newnames[i] :
174 category == i ? newnames[0] :
175 _nl_global_locale.__names[i]);
176 p = __stpcpy (p, _nl_category_names[i]);
177 *p++ = '=';
178 p = __stpcpy (p, name);
179 *p++ = ';';
181 p[-1] = '\0'; /* Clobber the last ';'. */
182 return new;
186 /* Put NAME in _nl_global_locale.__names. */
187 static inline void
188 setname (int category, const char *name)
190 if (_nl_global_locale.__names[category] == name)
191 return;
193 if (_nl_global_locale.__names[category] != _nl_C_name)
194 free ((char *) _nl_global_locale.__names[category]);
196 _nl_global_locale.__names[category] = name;
199 /* Put DATA in *_nl_current[CATEGORY]. */
200 static inline void
201 setdata (int category, struct locale_data *data)
203 if (CATEGORY_USED (category))
205 _nl_global_locale.__locales[category] = data;
206 if (_nl_category_postload[category])
207 (*_nl_category_postload[category]) ();
211 char *
212 setlocale (int category, const char *locale)
214 char *locale_path;
215 size_t locale_path_len;
216 const char *locpath_var;
217 char *composite;
219 /* Sanity check for CATEGORY argument. */
220 if (__builtin_expect (category, 0) < 0
221 || __builtin_expect (category, 0) >= __LC_LAST)
222 ERROR_RETURN;
224 /* Does user want name of current locale? */
225 if (locale == NULL)
226 return (char *) _nl_global_locale.__names[category];
228 if (strcmp (locale, _nl_global_locale.__names[category]) == 0)
229 /* Changing to the same thing. */
230 return (char *) _nl_global_locale.__names[category];
232 /* We perhaps really have to load some data. So we determine the
233 path in which to look for the data now. The environment variable
234 `LOCPATH' must only be used when the binary has no SUID or SGID
235 bit set. If using the default path, we tell _nl_find_locale
236 by passing null and it can check the canonical locale archive. */
237 locale_path = NULL;
238 locale_path_len = 0;
240 locpath_var = getenv ("LOCPATH");
241 if (locpath_var != NULL && locpath_var[0] != '\0')
243 if (__argz_create_sep (locpath_var, ':',
244 &locale_path, &locale_path_len) != 0)
245 return NULL;
247 if (__argz_add_sep (&locale_path, &locale_path_len,
248 _nl_default_locale_path, ':') != 0)
249 return NULL;
252 if (category == LC_ALL)
254 /* The user wants to set all categories. The desired locales
255 for the individual categories can be selected by using a
256 composite locale name. This is a semi-colon separated list
257 of entries of the form `CATEGORY=VALUE'. */
258 const char *newnames[__LC_LAST];
259 struct locale_data *newdata[__LC_LAST];
261 /* Set all name pointers to the argument name. */
262 for (category = 0; category < __LC_LAST; ++category)
263 if (category != LC_ALL)
264 newnames[category] = (char *) locale;
266 if (__builtin_expect (strchr (locale, ';') != NULL, 0))
268 /* This is a composite name. Make a copy and split it up. */
269 char *np = strdupa (locale);
270 char *cp;
271 int cnt;
273 while ((cp = strchr (np, '=')) != NULL)
275 for (cnt = 0; cnt < __LC_LAST; ++cnt)
276 if (cnt != LC_ALL
277 && (size_t) (cp - np) == _nl_category_name_sizes[cnt]
278 && memcmp (np, _nl_category_names[cnt], cp - np) == 0)
279 break;
281 if (cnt == __LC_LAST)
282 /* Bogus category name. */
283 ERROR_RETURN;
285 /* Found the category this clause sets. */
286 newnames[cnt] = ++cp;
287 cp = strchr (cp, ';');
288 if (cp != NULL)
290 /* Examine the next clause. */
291 *cp = '\0';
292 np = cp + 1;
294 else
295 /* This was the last clause. We are done. */
296 break;
299 for (cnt = 0; cnt < __LC_LAST; ++cnt)
300 if (cnt != LC_ALL && newnames[cnt] == locale)
301 /* The composite name did not specify all categories. */
302 ERROR_RETURN;
305 /* Protect global data. */
306 __libc_lock_lock (__libc_setlocale_lock);
308 /* Load the new data for each category. */
309 while (category-- > 0)
310 if (category != LC_ALL)
312 newdata[category] = _nl_find_locale (locale_path, locale_path_len,
313 category,
314 &newnames[category]);
316 if (newdata[category] == NULL)
318 #ifdef NL_CURRENT_INDIRECT
319 if (newnames[category] == _nl_C_name)
320 /* Null because it's the weak value of _nl_C_LC_FOO. */
321 continue;
322 #endif
323 break;
326 /* We must not simply free a global locale since we have
327 no control over the usage. So we mark it as
328 un-deletable. And yes, the 'if' is needed, the data
329 might be in read-only memory. */
330 if (newdata[category]->usage_count != UNDELETABLE)
331 newdata[category]->usage_count = UNDELETABLE;
333 /* Make a copy of locale name. */
334 if (newnames[category] != _nl_C_name)
336 if (strcmp (newnames[category],
337 _nl_global_locale.__names[category]) == 0)
338 newnames[category] = _nl_global_locale.__names[category];
339 else
341 newnames[category] = __strdup (newnames[category]);
342 if (newnames[category] == NULL)
343 break;
348 /* Create new composite name. */
349 composite = (category >= 0
350 ? NULL : new_composite_name (LC_ALL, newnames));
351 if (composite != NULL)
353 /* Now we have loaded all the new data. Put it in place. */
354 for (category = 0; category < __LC_LAST; ++category)
355 if (category != LC_ALL)
357 setdata (category, newdata[category]);
358 setname (category, newnames[category]);
360 setname (LC_ALL, composite);
362 /* We successfully loaded a new locale. Let the message catalog
363 functions know about this. */
364 ++_nl_msg_cat_cntr;
366 else
367 for (++category; category < __LC_LAST; ++category)
368 if (category != LC_ALL && newnames[category] != _nl_C_name
369 && newnames[category] != _nl_global_locale.__names[category])
370 free ((char *) newnames[category]);
372 /* Critical section left. */
373 __libc_lock_unlock (__libc_setlocale_lock);
375 /* Free the resources (the locale path variable. */
376 free (locale_path);
378 return composite;
380 else
382 struct locale_data *newdata = NULL;
383 const char *newname[1] = { locale };
385 /* Protect global data. */
386 __libc_lock_lock (__libc_setlocale_lock);
388 if (CATEGORY_USED (category))
390 /* Only actually load the data if anything will use it. */
391 newdata = _nl_find_locale (locale_path, locale_path_len, category,
392 &newname[0]);
393 if (newdata == NULL)
394 goto abort_single;
396 /* We must not simply free a global locale since we have no
397 control over the usage. So we mark it as un-deletable.
399 Note: do not remove the `if', it's necessary to copy with
400 the builtin locale data. */
401 if (newdata->usage_count != UNDELETABLE)
402 newdata->usage_count = UNDELETABLE;
405 /* Make a copy of locale name. */
406 if (newname[0] != _nl_C_name)
408 newname[0] = __strdup (newname[0]);
409 if (newname[0] == NULL)
410 goto abort_single;
413 /* Create new composite name. */
414 composite = new_composite_name (category, newname);
415 if (composite == NULL)
417 if (newname[0] != _nl_C_name)
418 free ((char *) newname[0]);
420 /* Say that we don't have any data loaded. */
421 abort_single:
422 newname[0] = NULL;
424 else
426 if (CATEGORY_USED (category))
427 setdata (category, newdata);
429 setname (category, newname[0]);
430 setname (LC_ALL, composite);
432 /* We successfully loaded a new locale. Let the message catalog
433 functions know about this. */
434 ++_nl_msg_cat_cntr;
437 /* Critical section left. */
438 __libc_lock_unlock (__libc_setlocale_lock);
440 /* Free the resources (the locale path variable. */
441 free (locale_path);
443 return (char *) newname[0];
446 libc_hidden_def (setlocale)
448 static void __libc_freeres_fn_section
449 free_category (int category,
450 struct locale_data *here, struct locale_data *c_data)
452 struct loaded_l10nfile *runp = _nl_locale_file_list[category];
454 /* If this category is already "C" don't do anything. */
455 if (here != c_data)
457 /* We have to be prepared that sometime later we still
458 might need the locale information. */
459 setdata (category, c_data);
460 setname (category, _nl_C_name);
463 while (runp != NULL)
465 struct loaded_l10nfile *curr = runp;
466 struct locale_data *data = (struct locale_data *) runp->data;
468 if (data != NULL && data != c_data)
469 _nl_unload_locale (data);
470 runp = runp->next;
471 free ((char *) curr->filename);
472 free (curr);
476 /* This is called from iconv/gconv_db.c's free_mem, as locales must
477 be freed before freeing gconv steps arrays. */
478 void __libc_freeres_fn_section
479 _nl_locale_subfreeres (void)
481 #ifdef NL_CURRENT_INDIRECT
482 /* We don't use the loop because we want to have individual weak
483 symbol references here. */
484 # define DEFINE_CATEGORY(category, category_name, items, a) \
485 if (CATEGORY_USED (category)) \
487 extern struct locale_data _nl_C_##category; \
488 weak_extern (_nl_C_##category) \
489 free_category (category, *_nl_current_##category, &_nl_C_##category); \
491 # include "categories.def"
492 # undef DEFINE_CATEGORY
493 #else
494 int category;
496 for (category = 0; category < __LC_LAST; ++category)
497 if (category != LC_ALL)
498 free_category (category, _NL_CURRENT_DATA (category),
499 _nl_C_locobj.__locales[category]);
500 #endif
502 setname (LC_ALL, _nl_C_name);
504 /* This frees the data structures associated with the locale archive.
505 The locales from the archive are not in the file list, so we have
506 not called _nl_unload_locale on them above. */
507 _nl_archive_subfreeres ();