* sysdeps/unix/sysv/linux/alpha/clone.S: Use HIDDEN_JUMPTARGET. ...
[glibc.git] / locale / setlocale.c
blob9f6e96439b1b3f2e7d1d8b83299f656dc3b5df6e
1 /* Copyright (C) 1991, 92, 1995-2000, 2002, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
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>
28 #include "localeinfo.h"
30 #ifdef NL_CURRENT_INDIRECT
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. */
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
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[] =
50 # define DEFINE_CATEGORY(category, category_name, items, a) \
51 [category] = &_nl_current_##category##_used,
52 # include "categories.def"
53 # undef DEFINE_CATEGORY
56 # define CATEGORY_USED(category) (_nl_current_used[category] != 0)
58 #else
60 /* The shared library always loads all the categories,
61 and the current global settings are kept in _nl_global_locale. */
63 # define CATEGORY_USED(category) (1)
65 #endif
68 /* Define an array of category names (also the environment variable names),
69 indexed by integral category. */
70 const char *const _nl_category_names[] =
72 #define DEFINE_CATEGORY(category, category_name, items, a) \
73 [category] = category_name,
74 #include "categories.def"
75 #undef DEFINE_CATEGORY
76 [LC_ALL] = "LC_ALL"
78 /* An array of their lengths, for convenience. */
79 const size_t _nl_category_name_sizes[] =
81 #define DEFINE_CATEGORY(category, category_name, items, a) \
82 [category] = sizeof (category_name) - 1,
83 #include "categories.def"
84 #undef DEFINE_CATEGORY
85 [LC_ALL] = sizeof ("LC_ALL") - 1
89 #ifdef NL_CURRENT_INDIRECT
90 # define WEAK_POSTLOAD(postload) weak_extern (postload)
91 #else
92 # define WEAK_POSTLOAD(postload) /* Need strong refs in static linking. */
93 #endif
95 /* Declare the postload functions used below. */
96 #undef NO_POSTLOAD
97 #define NO_POSTLOAD _nl_postload_ctype /* Harmless thing known to exist. */
98 #define DEFINE_CATEGORY(category, category_name, items, postload) \
99 extern void postload (void); WEAK_POSTLOAD (postload)
100 #include "categories.def"
101 #undef DEFINE_CATEGORY
102 #undef NO_POSTLOAD
104 /* Define an array indexed by category of postload functions to call after
105 loading and installing that category's data. */
106 static void (*const _nl_category_postload[]) (void) =
108 #define DEFINE_CATEGORY(category, category_name, items, postload) \
109 [category] = postload,
110 #include "categories.def"
111 #undef DEFINE_CATEGORY
115 /* Lock for protecting global data. */
116 __libc_lock_define_initialized (, __libc_setlocale_lock attribute_hidden)
118 /* Defined in loadmsgcat.c. */
119 extern int _nl_msg_cat_cntr;
122 /* Use this when we come along an error. */
123 #define ERROR_RETURN \
124 do { \
125 __set_errno (EINVAL); \
126 return NULL; \
127 } while (0)
130 /* Construct a new composite name. */
131 static char *
132 new_composite_name (int category, const char *newnames[__LC_LAST])
134 size_t last_len = 0;
135 size_t cumlen = 0;
136 int i;
137 char *new, *p;
138 int same = 1;
140 for (i = 0; i < __LC_LAST; ++i)
141 if (i != LC_ALL)
143 const char *name = (category == LC_ALL ? newnames[i] :
144 category == i ? newnames[0] :
145 _nl_global_locale.__names[i]);
146 last_len = strlen (name);
147 cumlen += _nl_category_name_sizes[i] + 1 + last_len + 1;
148 if (i > 0 && same && strcmp (name, newnames[0]) != 0)
149 same = 0;
152 if (same)
154 /* All the categories use the same name. */
155 if (strcmp (newnames[0], _nl_C_name) == 0
156 || strcmp (newnames[0], _nl_POSIX_name) == 0)
157 return (char *) _nl_C_name;
159 new = malloc (last_len + 1);
161 return new == NULL ? NULL : memcpy (new, newnames[0], last_len + 1);
164 new = malloc (cumlen);
165 if (new == NULL)
166 return NULL;
167 p = new;
168 for (i = 0; i < __LC_LAST; ++i)
169 if (i != LC_ALL)
171 /* Add "CATEGORY=NAME;" to the string. */
172 const char *name = (category == LC_ALL ? newnames[i] :
173 category == i ? newnames[0] :
174 _nl_global_locale.__names[i]);
175 p = __stpcpy (p, _nl_category_names[i]);
176 *p++ = '=';
177 p = __stpcpy (p, name);
178 *p++ = ';';
180 p[-1] = '\0'; /* Clobber the last ';'. */
181 return new;
185 /* Put NAME in _nl_global_locale.__names. */
186 static inline void
187 setname (int category, const char *name)
189 if (_nl_global_locale.__names[category] == name)
190 return;
192 if (_nl_global_locale.__names[category] != _nl_C_name)
193 free ((char *) _nl_global_locale.__names[category]);
195 _nl_global_locale.__names[category] = name;
198 /* Put DATA in *_nl_current[CATEGORY]. */
199 static inline void
200 setdata (int category, struct locale_data *data)
202 if (CATEGORY_USED (category))
204 _nl_global_locale.__locales[category] = data;
205 if (_nl_category_postload[category])
206 (*_nl_category_postload[category]) ();
210 char *
211 setlocale (int category, const char *locale)
213 char *locale_path;
214 size_t locale_path_len;
215 const char *locpath_var;
216 char *composite;
218 /* Sanity check for CATEGORY argument. */
219 if (__builtin_expect (category, 0) < 0
220 || __builtin_expect (category, 0) >= __LC_LAST)
221 ERROR_RETURN;
223 /* Does user want name of current locale? */
224 if (locale == NULL)
225 return (char *) _nl_global_locale.__names[category];
227 if (strcmp (locale, _nl_global_locale.__names[category]) == 0)
228 /* Changing to the same thing. */
229 return (char *) _nl_global_locale.__names[category];
231 /* We perhaps really have to load some data. So we determine the
232 path in which to look for the data now. The environment variable
233 `LOCPATH' must only be used when the binary has no SUID or SGID
234 bit set. If using the default path, we tell _nl_find_locale
235 by passing null and it can check the canonical locale archive. */
236 locale_path = NULL;
237 locale_path_len = 0;
239 locpath_var = getenv ("LOCPATH");
240 if (locpath_var != NULL && locpath_var[0] != '\0')
242 if (__argz_create_sep (locpath_var, ':',
243 &locale_path, &locale_path_len) != 0)
244 return NULL;
246 if (__argz_add_sep (&locale_path, &locale_path_len,
247 _nl_default_locale_path, ':') != 0)
248 return NULL;
251 if (category == LC_ALL)
253 /* The user wants to set all categories. The desired locales
254 for the individual categories can be selected by using a
255 composite locale name. This is a semi-colon separated list
256 of entries of the form `CATEGORY=VALUE'. */
257 const char *newnames[__LC_LAST];
258 struct locale_data *newdata[__LC_LAST];
260 /* Set all name pointers to the argument name. */
261 for (category = 0; category < __LC_LAST; ++category)
262 if (category != LC_ALL)
263 newnames[category] = (char *) locale;
265 if (__builtin_expect (strchr (locale, ';') != NULL, 0))
267 /* This is a composite name. Make a copy and split it up. */
268 char *np = strdupa (locale);
269 char *cp;
270 int cnt;
272 while ((cp = strchr (np, '=')) != NULL)
274 for (cnt = 0; cnt < __LC_LAST; ++cnt)
275 if (cnt != LC_ALL
276 && (size_t) (cp - np) == _nl_category_name_sizes[cnt]
277 && memcmp (np, _nl_category_names[cnt], cp - np) == 0)
278 break;
280 if (cnt == __LC_LAST)
281 /* Bogus category name. */
282 ERROR_RETURN;
284 /* Found the category this clause sets. */
285 newnames[cnt] = ++cp;
286 cp = strchr (cp, ';');
287 if (cp != NULL)
289 /* Examine the next clause. */
290 *cp = '\0';
291 np = cp + 1;
293 else
294 /* This was the last clause. We are done. */
295 break;
298 for (cnt = 0; cnt < __LC_LAST; ++cnt)
299 if (cnt != LC_ALL && newnames[cnt] == locale)
300 /* The composite name did not specify all categories. */
301 ERROR_RETURN;
304 /* Protect global data. */
305 __libc_lock_lock (__libc_setlocale_lock);
307 /* Load the new data for each category. */
308 while (category-- > 0)
309 if (category != LC_ALL)
311 newdata[category] = _nl_find_locale (locale_path, locale_path_len,
312 category,
313 &newnames[category]);
315 if (newdata[category] == NULL)
317 #ifdef NL_CURRENT_INDIRECT
318 if (newnames[category] == _nl_C_name)
319 /* Null because it's the weak value of _nl_C_LC_FOO. */
320 continue;
321 #endif
322 break;
325 /* We must not simply free a global locale since we have no
326 control over the usage. So we mark it as un-deletable. */
327 if (newdata[category]->usage_count != UNDELETABLE)
328 newdata[category]->usage_count = UNDELETABLE;
330 /* Make a copy of locale name. */
331 if (newnames[category] != _nl_C_name)
333 newnames[category] = __strdup (newnames[category]);
334 if (newnames[category] == NULL)
335 break;
339 /* Create new composite name. */
340 composite = (category >= 0
341 ? NULL : new_composite_name (LC_ALL, newnames));
342 if (composite != NULL)
344 /* Now we have loaded all the new data. Put it in place. */
345 for (category = 0; category < __LC_LAST; ++category)
346 if (category != LC_ALL)
348 setdata (category, newdata[category]);
349 setname (category, newnames[category]);
351 setname (LC_ALL, composite);
353 /* We successfully loaded a new locale. Let the message catalog
354 functions know about this. */
355 ++_nl_msg_cat_cntr;
357 else
358 for (++category; category < __LC_LAST; ++category)
359 if (category != LC_ALL && newnames[category] != _nl_C_name)
360 free ((char *) newnames[category]);
362 /* Critical section left. */
363 __libc_lock_unlock (__libc_setlocale_lock);
365 /* Free the resources (the locale path variable. */
366 free (locale_path);
368 return composite;
370 else
372 struct locale_data *newdata = NULL;
373 const char *newname[1] = { locale };
375 /* Protect global data. */
376 __libc_lock_lock (__libc_setlocale_lock);
378 if (CATEGORY_USED (category))
380 /* Only actually load the data if anything will use it. */
381 newdata = _nl_find_locale (locale_path, locale_path_len, category,
382 &newname[0]);
383 if (newdata == NULL)
384 goto abort_single;
386 /* We must not simply free a global locale since we have no
387 control over the usage. So we mark it as un-deletable.
389 Note: do not remove the `if', it's necessary to copy with
390 the builtin locale data. */
391 if (newdata->usage_count != UNDELETABLE)
392 newdata->usage_count = UNDELETABLE;
395 /* Make a copy of locale name. */
396 if (newname[0] != _nl_C_name)
398 newname[0] = __strdup (newname[0]);
399 if (newname[0] == NULL)
400 goto abort_single;
403 /* Create new composite name. */
404 composite = new_composite_name (category, newname);
405 if (composite == NULL)
407 if (newname[0] != _nl_C_name)
408 free ((char *) newname[0]);
410 /* Say that we don't have any data loaded. */
411 abort_single:
412 newname[0] = NULL;
414 else
416 if (CATEGORY_USED (category))
417 setdata (category, newdata);
419 setname (category, newname[0]);
420 setname (LC_ALL, composite);
422 /* We successfully loaded a new locale. Let the message catalog
423 functions know about this. */
424 ++_nl_msg_cat_cntr;
427 /* Critical section left. */
428 __libc_lock_unlock (__libc_setlocale_lock);
430 /* Free the resources (the locale path variable. */
431 free (locale_path);
433 return (char *) newname[0];
436 libc_hidden_def (setlocale)
438 static void
439 free_category (int category,
440 struct locale_data *here, struct locale_data *c_data)
442 struct loaded_l10nfile *runp = _nl_locale_file_list[category];
444 /* If this category is already "C" don't do anything. */
445 if (here != c_data)
447 /* We have to be prepared that sometime later we still
448 might need the locale information. */
449 setdata (category, c_data);
450 setname (category, _nl_C_name);
453 while (runp != NULL)
455 struct loaded_l10nfile *curr = runp;
456 struct locale_data *data = (struct locale_data *) runp->data;
458 if (data != NULL && data != c_data)
459 _nl_unload_locale (data);
460 runp = runp->next;
461 free ((char *) curr->filename);
462 free (curr);
466 libc_freeres_fn (free_mem)
468 #ifdef NL_CURRENT_INDIRECT
469 /* We don't use the loop because we want to have individual weak
470 symbol references here. */
471 # define DEFINE_CATEGORY(category, category_name, items, a) \
472 if (CATEGORY_USED (category)) \
474 extern struct locale_data _nl_C_##category; \
475 weak_extern (_nl_C_##category) \
476 free_category (category, *_nl_current_##category, &_nl_C_##category); \
478 # include "categories.def"
479 # undef DEFINE_CATEGORY
480 #else
481 int category;
483 for (category = 0; category < __LC_LAST; ++category)
484 if (category != LC_ALL)
485 free_category (category, _NL_CURRENT_DATA (category),
486 _nl_C_locobj.__locales[category]);
487 #endif
489 setname (LC_ALL, _nl_C_name);
491 /* This frees the data structures associated with the locale archive.
492 The locales from the archive are not in the file list, so we have
493 not called _nl_unload_locale on them above. */
494 _nl_archive_subfreeres ();