Update.
[glibc.git] / locale / setlocale.c
blob0bf9eeef7ba00d2700996489ca978a1239a08722
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)
124 /* Use this when we come along an error. */
125 #define ERROR_RETURN \
126 do { \
127 __set_errno (EINVAL); \
128 return NULL; \
129 } while (0)
132 /* Construct a new composite name. */
133 static inline char *
134 new_composite_name (int category, const char *newnames[__LC_LAST])
136 size_t last_len = 0;
137 size_t cumlen = 0;
138 int i;
139 char *new, *p;
140 int same = 1;
142 for (i = 0; i < __LC_LAST; ++i)
143 if (i != LC_ALL)
145 const char *name = (category == LC_ALL ? newnames[i] :
146 category == i ? newnames[0] :
147 _nl_current_names[i]);
148 last_len = strlen (name);
149 cumlen += _nl_category_name_sizes[i] + 1 + last_len + 1;
150 if (i > 0 && same && strcmp (name, newnames[0]) != 0)
151 same = 0;
154 if (same)
156 /* All the categories use the same name. */
157 if (strcmp (newnames[0], _nl_C_name) == 0
158 || strcmp (newnames[0], _nl_POSIX_name) == 0)
159 return (char *) _nl_C_name;
161 new = malloc (last_len + 1);
163 return new == NULL ? NULL : memcpy (new, newnames[0], last_len + 1);
166 new = malloc (cumlen);
167 if (new == NULL)
168 return NULL;
169 p = new;
170 for (i = 0; i < __LC_LAST; ++i)
171 if (i != LC_ALL)
173 /* Add "CATEGORY=NAME;" to the string. */
174 const char *name = (category == LC_ALL ? newnames[i] :
175 category == i ? newnames[0] :
176 _nl_current_names[i]);
177 p = __stpcpy (p, _nl_category_names[i]);
178 *p++ = '=';
179 p = __stpcpy (p, name);
180 *p++ = ';';
182 p[-1] = '\0'; /* Clobber the last ';'. */
183 return new;
187 /* Put NAME in _nl_current_names. */
188 static inline void
189 setname (int category, const char *name)
191 if (_nl_current_names[category] == name)
192 return;
194 if (category == LC_ALL && _nl_current_names[category] != _nl_C_name)
195 free ((char *) _nl_current_names[category]);
197 _nl_current_names[category] = name;
201 /* Put DATA in *_nl_current[CATEGORY]. */
202 static inline void
203 setdata (int category, struct locale_data *data)
205 if (_nl_current[category] != NULL)
207 *_nl_current[category] = data;
208 if (_nl_category_postload[category])
209 (*_nl_category_postload[category]) ();
214 char *
215 setlocale (int category, const char *locale)
217 char *locale_path;
218 size_t locale_path_len;
219 const char *locpath_var;
220 char *composite;
222 /* Sanity check for CATEGORY argument. */
223 if (category < 0 || category >= __LC_LAST)
224 ERROR_RETURN;
226 /* Does user want name of current locale? */
227 if (locale == NULL)
228 return (char *) _nl_current_names[category];
230 if (strcmp (locale, _nl_current_names[category]) == 0)
231 /* Changing to the same thing. */
232 return (char *) _nl_current_names[category];
234 /* We perhaps really have to load some data. So we determine the
235 path in which to look for the data now. The environment variable
236 `LOCPATH' must only be used when the binary has no SUID or SGID
237 bit set. */
238 locale_path = NULL;
239 locale_path_len = 0;
241 locpath_var = __secure_getenv ("LOCPATH");
242 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, LOCALE_PATH, ':') != 0)
248 return NULL;
250 if (category == LC_ALL)
252 /* The user wants to set all categories. The desired locales
253 for the individual categories can be selected by using a
254 composite locale name. This is a semi-colon separated list
255 of entries of the form `CATEGORY=VALUE'. */
256 const char *newnames[__LC_LAST];
257 struct locale_data *newdata[__LC_LAST];
259 /* Set all name pointers to the argument name. */
260 for (category = 0; category < __LC_LAST; ++category)
261 if (category != LC_ALL)
262 newnames[category] = (char *) locale;
264 if (strchr (locale, ';') != NULL)
266 /* This is a composite name. Make a copy and split it up. */
267 char *np = strdupa (locale);
268 char *cp;
269 int cnt;
271 while ((cp = strchr (np, '=')) != NULL)
273 for (cnt = 0; cnt < __LC_LAST; ++cnt)
274 if (cnt != LC_ALL
275 && (size_t) (cp - np) == _nl_category_name_sizes[cnt]
276 && memcmp (np, _nl_category_names[cnt], cp - np) == 0)
277 break;
279 if (cnt == __LC_LAST)
280 /* Bogus category name. */
281 ERROR_RETURN;
283 /* Found the category this clause sets. */
284 newnames[cnt] = ++cp;
285 cp = strchr (cp, ';');
286 if (cp != NULL)
288 /* Examine the next clause. */
289 *cp = '\0';
290 np = cp + 1;
292 else
293 /* This was the last clause. We are done. */
294 break;
297 for (cnt = 0; cnt < __LC_LAST; ++cnt)
298 if (cnt != LC_ALL && newnames[cnt] == locale)
299 /* The composite name did not specify all categories. */
300 ERROR_RETURN;
303 /* Protect global data. */
304 __libc_lock_lock (__libc_setlocale_lock);
306 /* Load the new data for each category. */
307 while (category-- > 0)
308 if (category != LC_ALL)
310 newdata[category] = _nl_find_locale (locale_path, locale_path_len,
311 category,
312 &newnames[category]);
314 if (newdata[category] == NULL)
315 break;
317 /* We must not simply free a global locale since we have no
318 control over the usage. So we mark it as un-deletable. */
319 if (newdata[category]->usage_count != UNDELETABLE)
320 newdata[category]->usage_count = UNDELETABLE;
323 /* Create new composite name. */
324 composite = (category >= 0
325 ? NULL : new_composite_name (LC_ALL, newnames));
326 if (composite != NULL)
328 /* Now we have loaded all the new data. Put it in place. */
329 for (category = 0; category < __LC_LAST; ++category)
330 if (category != LC_ALL)
332 setdata (category, newdata[category]);
333 setname (category, newnames[category]);
335 setname (LC_ALL, composite);
338 /* Critical section left. */
339 __libc_lock_unlock (__libc_setlocale_lock);
341 /* Free the resources (the locale path variable. */
342 free (locale_path);
344 return composite;
346 else
348 struct locale_data *newdata = NULL;
349 const char *newname[1] = { locale };
351 /* Protect global data. */
352 __libc_lock_lock (__libc_setlocale_lock);
354 if (_nl_current[category] != NULL)
356 /* Only actually load the data if anything will use it. */
357 newdata = _nl_find_locale (locale_path, locale_path_len, category,
358 &newname[0]);
359 if (newdata == NULL)
360 goto abort_single;
362 /* We must not simply free a global locale since we have no
363 control over the usage. So we mark it as un-deletable.
365 Note: do not remove the `if', it's necessary to copy with
366 the builtin locale data. */
367 if (newdata->usage_count != UNDELETABLE)
368 newdata->usage_count = UNDELETABLE;
371 /* Create new composite name. */
372 composite = new_composite_name (category, newname);
373 if (composite == NULL)
375 /* Say that we don't have any data loaded. */
376 abort_single:
377 newname[0] = NULL;
379 else
381 if (_nl_current[category] != NULL)
382 setdata (category, newdata);
384 setname (category, newname[0]);
385 setname (LC_ALL, composite);
388 /* Critical section left. */
389 __libc_lock_unlock (__libc_setlocale_lock);
391 /* Free the resources (the locale path variable. */
392 free (locale_path);
394 return (char *) newname[0];
399 static void __attribute__ ((unused))
400 free_mem (void)
402 int category;
404 for (category = 0; category < __LC_LAST; ++category)
405 if (category != LC_ALL)
407 struct locale_data *here = *_nl_current[category];
409 /* If this category is already "C" don't do anything. */
410 if (here == _nl_C[category])
411 continue;
413 /* We have to be prepared that sometime later me still might
414 need the locale information. */
415 setdata (category, _nl_C[category]);
416 setname (category, _nl_C_name);
418 _nl_unload_locale (here);
421 setname (LC_ALL, _nl_C_name);
423 text_set_element (__libc_subfreeres, free_mem);