Fix typo in stdio_lim.h installation rule.
[glibc.git] / locale / setlocale.c
blob3c80379cf99b95cf4381a63b99b9b2a5997671bc
1 /* Copyright (C) 1991, 92, 95-99, 2000, 2002 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 #ifndef SHARED
32 /* For each category declare two external variables (with weak references):
33 extern const struct locale_data *_nl_current_CATEGORY;
34 This points to the current locale's in-core data for CATEGORY.
35 extern const struct locale_data _nl_C_CATEGORY;
36 This contains the built-in "C"/"POSIX" locale's data for CATEGORY.
37 Both are weak references; if &_nl_current_CATEGORY is zero,
38 then nothing is using the locale data. */
39 #define DEFINE_CATEGORY(category, category_name, items, a) \
40 weak_extern (_nl_current_##category) \
41 weak_extern (_nl_C_##category) \
42 extern struct locale_data *_nl_current_##category; \
43 extern struct locale_data _nl_C_##category;
44 #include "categories.def"
45 #undef DEFINE_CATEGORY
47 /* Array indexed by category of pointers to _nl_current_CATEGORY slots.
48 Elements are zero for categories whose data is never used. */
49 struct locale_data * *const _nl_current[] =
51 #define DEFINE_CATEGORY(category, category_name, items, a) \
52 [category] = &_nl_current_##category,
53 #include "categories.def"
54 #undef DEFINE_CATEGORY
55 /* We need this additional element to simplify the code. It must
56 simply be != NULL. */
57 [LC_ALL] = (struct locale_data **) ~0ul
60 /* Array indexed by category of pointers to _nl_C_CATEGORY slots.
61 Elements are zero for categories whose data is never used. */
62 struct locale_data *const _nl_C[] attribute_hidden =
64 #define DEFINE_CATEGORY(category, category_name, items, a) \
65 [category] = &_nl_C_##category,
66 #include "categories.def"
67 #undef DEFINE_CATEGORY
70 # define CATEGORY_USED(category) (_nl_current[category] != NULL)
72 #else
74 /* The shared library always loads all the categories,
75 and the current global settings are kept in _nl_global_locale. */
77 # define _nl_C (_nl_C_locobj.__locales)
79 # define CATEGORY_USED(category) (1)
81 #endif
84 /* Define an array of category names (also the environment variable names),
85 indexed by integral category. */
86 const char *const _nl_category_names[] =
88 #define DEFINE_CATEGORY(category, category_name, items, a) \
89 [category] = category_name,
90 #include "categories.def"
91 #undef DEFINE_CATEGORY
92 [LC_ALL] = "LC_ALL"
94 /* An array of their lengths, for convenience. */
95 const size_t _nl_category_name_sizes[] =
97 #define DEFINE_CATEGORY(category, category_name, items, a) \
98 [category] = sizeof (category_name) - 1,
99 #include "categories.def"
100 #undef DEFINE_CATEGORY
101 [LC_ALL] = sizeof ("LC_ALL") - 1
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);
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 inline 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_current_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_current_names[i]);
185 p = __stpcpy (p, _nl_category_names[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_current_names. */
196 static inline void
197 setname (int category, const char *name)
199 if (_nl_current_names[category] == name)
200 return;
202 if (_nl_current_names[category] != _nl_C_name)
203 free ((char *) _nl_current_names[category]);
205 _nl_current_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 #ifdef SHARED
215 _nl_global_locale.__locales[category] = data;
216 #endif
217 #ifndef SHARED
218 # warning when uselocale exists it will need the line above too
219 *_nl_current[category] = data;
220 #endif
221 if (_nl_category_postload[category])
222 (*_nl_category_postload[category]) ();
226 char *
227 setlocale (int category, const char *locale)
229 char *locale_path;
230 size_t locale_path_len;
231 const char *locpath_var;
232 char *composite;
234 /* Sanity check for CATEGORY argument. */
235 if (__builtin_expect (category, 0) < 0
236 || __builtin_expect (category, 0) >= __LC_LAST)
237 ERROR_RETURN;
239 /* Does user want name of current locale? */
240 if (locale == NULL)
241 return (char *) _nl_current_names[category];
243 if (strcmp (locale, _nl_current_names[category]) == 0)
244 /* Changing to the same thing. */
245 return (char *) _nl_current_names[category];
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;
255 locpath_var = getenv ("LOCPATH");
256 if (locpath_var != NULL && locpath_var[0] != '\0')
258 if (__argz_create_sep (locpath_var, ':',
259 &locale_path, &locale_path_len) != 0)
260 return NULL;
262 if (__argz_add_sep (&locale_path, &locale_path_len,
263 _nl_default_locale_path, ':') != 0)
264 return NULL;
267 if (category == LC_ALL)
269 /* The user wants to set all categories. The desired locales
270 for the individual categories can be selected by using a
271 composite locale name. This is a semi-colon separated list
272 of entries of the form `CATEGORY=VALUE'. */
273 const char *newnames[__LC_LAST];
274 struct locale_data *newdata[__LC_LAST];
276 /* Set all name pointers to the argument name. */
277 for (category = 0; category < __LC_LAST; ++category)
278 if (category != LC_ALL)
279 newnames[category] = (char *) locale;
281 if (__builtin_expect (strchr (locale, ';') != NULL, 0))
283 /* This is a composite name. Make a copy and split it up. */
284 char *np = strdupa (locale);
285 char *cp;
286 int cnt;
288 while ((cp = strchr (np, '=')) != NULL)
290 for (cnt = 0; cnt < __LC_LAST; ++cnt)
291 if (cnt != LC_ALL
292 && (size_t) (cp - np) == _nl_category_name_sizes[cnt]
293 && memcmp (np, _nl_category_names[cnt], cp - np) == 0)
294 break;
296 if (cnt == __LC_LAST)
297 /* Bogus category name. */
298 ERROR_RETURN;
300 /* Found the category this clause sets. */
301 newnames[cnt] = ++cp;
302 cp = strchr (cp, ';');
303 if (cp != NULL)
305 /* Examine the next clause. */
306 *cp = '\0';
307 np = cp + 1;
309 else
310 /* This was the last clause. We are done. */
311 break;
314 for (cnt = 0; cnt < __LC_LAST; ++cnt)
315 if (cnt != LC_ALL && newnames[cnt] == locale)
316 /* The composite name did not specify all categories. */
317 ERROR_RETURN;
320 /* Protect global data. */
321 __libc_lock_lock (__libc_setlocale_lock);
323 /* Load the new data for each category. */
324 while (category-- > 0)
325 if (category != LC_ALL)
327 newdata[category] = _nl_find_locale (locale_path, locale_path_len,
328 category,
329 &newnames[category]);
331 if (newdata[category] == NULL)
332 break;
334 /* We must not simply free a global locale since we have no
335 control over the usage. So we mark it as un-deletable. */
336 if (newdata[category]->usage_count != UNDELETABLE)
337 newdata[category]->usage_count = UNDELETABLE;
339 /* Make a copy of locale name. */
340 if (newnames[category] != _nl_C_name)
342 newnames[category] = strdup (newnames[category]);
343 if (newnames[category] == NULL)
344 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 free ((char *) newnames[category]);
371 /* Critical section left. */
372 __libc_lock_unlock (__libc_setlocale_lock);
374 /* Free the resources (the locale path variable. */
375 free (locale_path);
377 return composite;
379 else
381 struct locale_data *newdata = NULL;
382 const char *newname[1] = { locale };
384 /* Protect global data. */
385 __libc_lock_lock (__libc_setlocale_lock);
387 if (CATEGORY_USED (category))
389 /* Only actually load the data if anything will use it. */
390 newdata = _nl_find_locale (locale_path, locale_path_len, category,
391 &newname[0]);
392 if (newdata == NULL)
393 goto abort_single;
395 /* We must not simply free a global locale since we have no
396 control over the usage. So we mark it as un-deletable.
398 Note: do not remove the `if', it's necessary to copy with
399 the builtin locale data. */
400 if (newdata->usage_count != UNDELETABLE)
401 newdata->usage_count = UNDELETABLE;
404 /* Make a copy of locale name. */
405 if (newname[0] != _nl_C_name)
407 newname[0] = strdup (newname[0]);
408 if (newname[0] == NULL)
409 goto abort_single;
412 /* Create new composite name. */
413 composite = new_composite_name (category, newname);
414 if (composite == NULL)
416 if (newname[0] != _nl_C_name)
417 free ((char *) newname[0]);
419 /* Say that we don't have any data loaded. */
420 abort_single:
421 newname[0] = NULL;
423 else
425 if (CATEGORY_USED (category))
426 setdata (category, newdata);
428 setname (category, newname[0]);
429 setname (LC_ALL, composite);
431 /* We successfully loaded a new locale. Let the message catalog
432 functions know about this. */
433 ++_nl_msg_cat_cntr;
436 /* Critical section left. */
437 __libc_lock_unlock (__libc_setlocale_lock);
439 /* Free the resources (the locale path variable. */
440 free (locale_path);
442 return (char *) newname[0];
445 libc_hidden_def (setlocale)
447 static void __attribute__ ((unused))
448 free_mem (void)
450 int category;
452 for (category = 0; category < __LC_LAST; ++category)
453 if (category != LC_ALL)
455 struct locale_data *here = _NL_CURRENT_DATA (category);
456 struct loaded_l10nfile *runp = _nl_locale_file_list[category];
458 /* If this category is already "C" don't do anything. */
459 if (here != _nl_C[category])
461 /* We have to be prepared that sometime later we still
462 might need the locale information. */
463 setdata (category, _nl_C[category]);
464 setname (category, _nl_C_name);
467 while (runp != NULL)
469 struct loaded_l10nfile *curr = runp;
470 struct locale_data *data = (struct locale_data *) runp->data;
472 if (data != NULL && data != _nl_C[category])
473 _nl_unload_locale (data);
474 runp = runp->next;
475 free ((char *) curr->filename);
476 free (curr);
480 setname (LC_ALL, _nl_C_name);
482 /* This frees the data structures associated with the locale archive.
483 The locales from the archive are not in the file list, so we have
484 not called _nl_unload_locale on them above. */
485 _nl_archive_subfreeres ();
487 text_set_element (__libc_subfreeres, free_mem);