Update.
[glibc.git] / locale / setlocale.c
blobc7848b9a5c275d8105663c7b8d8db2ab751d82a7
1 /* Copyright (C) 1991, 92, 95-99, 2000 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 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 /* For each category declare two external variables (with weak references):
31 extern const struct locale_data *_nl_current_CATEGORY;
32 This points to the current locale's in-core data for CATEGORY.
33 extern const struct locale_data _nl_C_CATEGORY;
34 This contains the built-in "C"/"POSIX" locale's data for CATEGORY.
35 Both are weak references; if &_nl_current_CATEGORY is zero,
36 then nothing is using the locale data. */
37 #define DEFINE_CATEGORY(category, category_name, items, a) \
38 extern struct locale_data *_nl_current_##category; \
39 extern struct locale_data _nl_C_##category;
40 #include "categories.def"
41 #undef DEFINE_CATEGORY
43 /* Array indexed by category of pointers to _nl_current_CATEGORY slots.
44 Elements are zero for categories whose data is never used. */
45 struct locale_data * *const _nl_current[] =
47 #define DEFINE_CATEGORY(category, category_name, items, a) \
48 [category] = &_nl_current_##category,
49 #include "categories.def"
50 #undef DEFINE_CATEGORY
51 /* We need this additional element to simplify the code. It must
52 simply be != NULL. */
53 [LC_ALL] = (struct locale_data **) ~0ul
56 /* Array indexed by category of pointers to _nl_C_CATEGORY slots.
57 Elements are zero for categories whose data is never used. */
58 struct locale_data *const _nl_C[] =
60 #define DEFINE_CATEGORY(category, category_name, items, a) \
61 [category] = &_nl_C_##category,
62 #include "categories.def"
63 #undef DEFINE_CATEGORY
67 /* Define an array of category names (also the environment variable names),
68 indexed by integral category. */
69 const char *const _nl_category_names[] =
71 #define DEFINE_CATEGORY(category, category_name, items, a) \
72 [category] = category_name,
73 #include "categories.def"
74 #undef DEFINE_CATEGORY
75 [LC_ALL] = "LC_ALL"
77 /* An array of their lengths, for convenience. */
78 const size_t _nl_category_name_sizes[] =
80 #define DEFINE_CATEGORY(category, category_name, items, a) \
81 [category] = sizeof (category_name) - 1,
82 #include "categories.def"
83 #undef DEFINE_CATEGORY
84 [LC_ALL] = sizeof ("LC_ALL") - 1
88 /* Declare the postload functions used below. */
89 #undef NO_POSTLOAD
90 #define NO_POSTLOAD _nl_postload_ctype /* Harmless thing known to exist. */
91 #define DEFINE_CATEGORY(category, category_name, items, postload) \
92 extern void postload (void);
93 #include "categories.def"
94 #undef DEFINE_CATEGORY
95 #undef NO_POSTLOAD
97 /* Define an array indexed by category of postload functions to call after
98 loading and installing that category's data. */
99 static void (*const _nl_category_postload[]) (void) =
101 #define DEFINE_CATEGORY(category, category_name, items, postload) \
102 [category] = postload,
103 #include "categories.def"
104 #undef DEFINE_CATEGORY
108 /* Name of current locale for each individual category.
109 Each is malloc'd unless it is nl_C_name. */
110 static const char *_nl_current_names[] =
112 #define DEFINE_CATEGORY(category, category_name, items, a) \
113 [category] = _nl_C_name,
114 #include "categories.def"
115 #undef DEFINE_CATEGORY
116 [LC_ALL] = _nl_C_name /* For LC_ALL. */
120 /* Lock for protecting global data. */
121 __libc_lock_define_initialized (, __libc_setlocale_lock)
123 /* Defined in loadmsgcat.c. */
124 extern int _nl_msg_cat_cntr;
127 /* Use this when we come along an error. */
128 #define ERROR_RETURN \
129 do { \
130 __set_errno (EINVAL); \
131 return NULL; \
132 } while (0)
135 /* Construct a new composite name. */
136 static inline char *
137 new_composite_name (int category, const char *newnames[__LC_LAST])
139 size_t last_len = 0;
140 size_t cumlen = 0;
141 int i;
142 char *new, *p;
143 int same = 1;
145 for (i = 0; i < __LC_LAST; ++i)
146 if (i != LC_ALL)
148 const char *name = (category == LC_ALL ? newnames[i] :
149 category == i ? newnames[0] :
150 _nl_current_names[i]);
151 last_len = strlen (name);
152 cumlen += _nl_category_name_sizes[i] + 1 + last_len + 1;
153 if (i > 0 && same && strcmp (name, newnames[0]) != 0)
154 same = 0;
157 if (same)
159 /* All the categories use the same name. */
160 if (strcmp (newnames[0], _nl_C_name) == 0
161 || strcmp (newnames[0], _nl_POSIX_name) == 0)
162 return (char *) _nl_C_name;
164 new = malloc (last_len + 1);
166 return new == NULL ? NULL : memcpy (new, newnames[0], last_len + 1);
169 new = malloc (cumlen);
170 if (new == NULL)
171 return NULL;
172 p = new;
173 for (i = 0; i < __LC_LAST; ++i)
174 if (i != LC_ALL)
176 /* Add "CATEGORY=NAME;" to the string. */
177 const char *name = (category == LC_ALL ? newnames[i] :
178 category == i ? newnames[0] :
179 _nl_current_names[i]);
180 p = __stpcpy (p, _nl_category_names[i]);
181 *p++ = '=';
182 p = __stpcpy (p, name);
183 *p++ = ';';
185 p[-1] = '\0'; /* Clobber the last ';'. */
186 return new;
190 /* Put NAME in _nl_current_names. */
191 static inline void
192 setname (int category, const char *name)
194 if (_nl_current_names[category] == name)
195 return;
197 if (_nl_current_names[category] != _nl_C_name)
198 free ((char *) _nl_current_names[category]);
200 _nl_current_names[category] = name;
204 /* Put DATA in *_nl_current[CATEGORY]. */
205 static inline void
206 setdata (int category, struct locale_data *data)
208 if (_nl_current[category] != NULL)
210 *_nl_current[category] = data;
211 if (_nl_category_postload[category])
212 (*_nl_category_postload[category]) ();
217 char *
218 setlocale (int category, const char *locale)
220 char *locale_path;
221 size_t locale_path_len;
222 const char *locpath_var;
223 char *composite;
225 /* Sanity check for CATEGORY argument. */
226 if (__builtin_expect (category, 0) < 0
227 || __builtin_expect (category, 0) >= __LC_LAST)
228 ERROR_RETURN;
230 /* Does user want name of current locale? */
231 if (locale == NULL)
232 return (char *) _nl_current_names[category];
234 if (strcmp (locale, _nl_current_names[category]) == 0)
235 /* Changing to the same thing. */
236 return (char *) _nl_current_names[category];
238 /* We perhaps really have to load some data. So we determine the
239 path in which to look for the data now. The environment variable
240 `LOCPATH' must only be used when the binary has no SUID or SGID
241 bit set. */
242 locale_path = NULL;
243 locale_path_len = 0;
245 locpath_var = getenv ("LOCPATH");
246 if (locpath_var != NULL && locpath_var[0] != '\0')
247 if (__argz_create_sep (locpath_var, ':',
248 &locale_path, &locale_path_len) != 0)
249 return NULL;
251 if (__argz_add_sep (&locale_path, &locale_path_len, LOCALEDIR, ':') != 0)
252 return NULL;
254 if (category == LC_ALL)
256 /* The user wants to set all categories. The desired locales
257 for the individual categories can be selected by using a
258 composite locale name. This is a semi-colon separated list
259 of entries of the form `CATEGORY=VALUE'. */
260 const char *newnames[__LC_LAST];
261 struct locale_data *newdata[__LC_LAST];
263 /* Set all name pointers to the argument name. */
264 for (category = 0; category < __LC_LAST; ++category)
265 if (category != LC_ALL)
266 newnames[category] = (char *) locale;
268 if (__builtin_expect (strchr (locale, ';') != NULL, 0))
270 /* This is a composite name. Make a copy and split it up. */
271 char *np = strdupa (locale);
272 char *cp;
273 int cnt;
275 while ((cp = strchr (np, '=')) != NULL)
277 for (cnt = 0; cnt < __LC_LAST; ++cnt)
278 if (cnt != LC_ALL
279 && (size_t) (cp - np) == _nl_category_name_sizes[cnt]
280 && memcmp (np, _nl_category_names[cnt], cp - np) == 0)
281 break;
283 if (cnt == __LC_LAST)
284 /* Bogus category name. */
285 ERROR_RETURN;
287 /* Found the category this clause sets. */
288 newnames[cnt] = ++cp;
289 cp = strchr (cp, ';');
290 if (cp != NULL)
292 /* Examine the next clause. */
293 *cp = '\0';
294 np = cp + 1;
296 else
297 /* This was the last clause. We are done. */
298 break;
301 for (cnt = 0; cnt < __LC_LAST; ++cnt)
302 if (cnt != LC_ALL && newnames[cnt] == locale)
303 /* The composite name did not specify all categories. */
304 ERROR_RETURN;
307 /* Protect global data. */
308 __libc_lock_lock (__libc_setlocale_lock);
310 /* Load the new data for each category. */
311 while (category-- > 0)
312 if (category != LC_ALL)
314 newdata[category] = _nl_find_locale (locale_path, locale_path_len,
315 category,
316 &newnames[category]);
318 if (newdata[category] == NULL)
319 break;
321 /* We must not simply free a global locale since we have no
322 control over the usage. So we mark it as un-deletable. */
323 if (newdata[category]->usage_count != UNDELETABLE)
324 newdata[category]->usage_count = UNDELETABLE;
326 /* Make a copy of locale name. */
327 if (newnames[category] != _nl_C_name)
329 newnames[category] = strdup (newnames[category]);
330 if (newnames[category] == NULL)
331 break;
335 /* Create new composite name. */
336 composite = (category >= 0
337 ? NULL : new_composite_name (LC_ALL, newnames));
338 if (composite != NULL)
340 /* Now we have loaded all the new data. Put it in place. */
341 for (category = 0; category < __LC_LAST; ++category)
342 if (category != LC_ALL)
344 setdata (category, newdata[category]);
345 setname (category, newnames[category]);
347 setname (LC_ALL, composite);
349 /* We successfully loaded a new locale. Let the message catalog
350 functions know about this. */
351 ++_nl_msg_cat_cntr;
353 else
354 for (++category; category < __LC_LAST; ++category)
355 if (category != LC_ALL && newnames[category] != _nl_C_name)
356 free ((char *) newnames[category]);
358 /* Critical section left. */
359 __libc_lock_unlock (__libc_setlocale_lock);
361 /* Free the resources (the locale path variable. */
362 free (locale_path);
364 return composite;
366 else
368 struct locale_data *newdata = NULL;
369 const char *newname[1] = { locale };
371 /* Protect global data. */
372 __libc_lock_lock (__libc_setlocale_lock);
374 if (_nl_current[category] != NULL)
376 /* Only actually load the data if anything will use it. */
377 newdata = _nl_find_locale (locale_path, locale_path_len, category,
378 &newname[0]);
379 if (newdata == NULL)
380 goto abort_single;
382 /* We must not simply free a global locale since we have no
383 control over the usage. So we mark it as un-deletable.
385 Note: do not remove the `if', it's necessary to copy with
386 the builtin locale data. */
387 if (newdata->usage_count != UNDELETABLE)
388 newdata->usage_count = UNDELETABLE;
391 /* Make a copy of locale name. */
392 if (newname[0] != _nl_C_name)
394 newname[0] = strdup (newname[0]);
395 if (newname[0] == NULL)
396 goto abort_single;
399 /* Create new composite name. */
400 composite = new_composite_name (category, newname);
401 if (composite == NULL)
403 if (newname[0] != _nl_C_name)
404 free ((char *) newname[0]);
406 /* Say that we don't have any data loaded. */
407 abort_single:
408 newname[0] = NULL;
410 else
412 if (_nl_current[category] != NULL)
413 setdata (category, newdata);
415 setname (category, newname[0]);
416 setname (LC_ALL, composite);
418 /* We successfully loaded a new locale. Let the message catalog
419 functions know about this. */
420 ++_nl_msg_cat_cntr;
423 /* Critical section left. */
424 __libc_lock_unlock (__libc_setlocale_lock);
426 /* Free the resources (the locale path variable. */
427 free (locale_path);
429 return (char *) newname[0];
434 static void __attribute__ ((unused))
435 free_mem (void)
437 int category;
439 for (category = 0; category < __LC_LAST; ++category)
440 if (category != LC_ALL)
442 struct locale_data *here = *_nl_current[category];
444 /* If this category is already "C" don't do anything. */
445 if (here == _nl_C[category])
446 continue;
448 /* We have to be prepared that sometime later me still might
449 need the locale information. */
450 setdata (category, _nl_C[category]);
451 setname (category, _nl_C_name);
453 _nl_unload_locale (here);
456 setname (LC_ALL, _nl_C_name);
458 text_set_element (__libc_subfreeres, free_mem);