]> git.ipfire.org Git - thirdparty/glibc.git/blob - locale/setlocale.c
* locale/setlocale.c (_nl_current_names): Variable moved ...
[thirdparty/glibc.git] / locale / setlocale.c
1 /* Copyright (C) 1991, 92, 95-99, 2000, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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. */
18
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>
27
28 #include "localeinfo.h"
29
30 #ifndef SHARED
31
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
46
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[] =
50 {
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
58 };
59
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 =
63 {
64 #define DEFINE_CATEGORY(category, category_name, items, a) \
65 [category] = &_nl_C_##category,
66 #include "categories.def"
67 #undef DEFINE_CATEGORY
68 };
69
70 # define CATEGORY_USED(category) (_nl_current[category] != NULL)
71
72 #else
73
74 /* The shared library always loads all the categories,
75 and the current global settings are kept in _nl_global_locale. */
76
77 # define _nl_C (_nl_C_locobj.__locales)
78
79 # define CATEGORY_USED(category) (1)
80
81 #endif
82
83
84 /* Define an array of category names (also the environment variable names),
85 indexed by integral category. */
86 const char *const _nl_category_names[] =
87 {
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"
93 };
94 /* An array of their lengths, for convenience. */
95 const size_t _nl_category_name_sizes[] =
96 {
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
102 };
103
104
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
113
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) =
117 {
118 #define DEFINE_CATEGORY(category, category_name, items, postload) \
119 [category] = postload,
120 #include "categories.def"
121 #undef DEFINE_CATEGORY
122 };
123
124
125 /* Lock for protecting global data. */
126 __libc_lock_define_initialized (, __libc_setlocale_lock attribute_hidden)
127
128 /* Defined in loadmsgcat.c. */
129 extern int _nl_msg_cat_cntr;
130
131
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)
138
139
140 /* Construct a new composite name. */
141 static inline char *
142 new_composite_name (int category, const char *newnames[__LC_LAST])
143 {
144 size_t last_len = 0;
145 size_t cumlen = 0;
146 int i;
147 char *new, *p;
148 int same = 1;
149
150 for (i = 0; i < __LC_LAST; ++i)
151 if (i != LC_ALL)
152 {
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;
160 }
161
162 if (same)
163 {
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;
168
169 new = malloc (last_len + 1);
170
171 return new == NULL ? NULL : memcpy (new, newnames[0], last_len + 1);
172 }
173
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)
180 {
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++ = ';';
189 }
190 p[-1] = '\0'; /* Clobber the last ';'. */
191 return new;
192 }
193
194
195 /* Put NAME in _nl_current_names. */
196 static inline void
197 setname (int category, const char *name)
198 {
199 if (_nl_current_names[category] == name)
200 return;
201
202 if (_nl_current_names[category] != _nl_C_name)
203 free ((char *) _nl_current_names[category]);
204
205 _nl_current_names[category] = name;
206 }
207
208 /* Put DATA in *_nl_current[CATEGORY]. */
209 static inline void
210 setdata (int category, struct locale_data *data)
211 {
212 if (CATEGORY_USED (category))
213 {
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]) ();
223 }
224 }
225
226 char *
227 setlocale (int category, const char *locale)
228 {
229 char *locale_path;
230 size_t locale_path_len;
231 const char *locpath_var;
232 char *composite;
233
234 /* Sanity check for CATEGORY argument. */
235 if (__builtin_expect (category, 0) < 0
236 || __builtin_expect (category, 0) >= __LC_LAST)
237 ERROR_RETURN;
238
239 /* Does user want name of current locale? */
240 if (locale == NULL)
241 return (char *) _nl_current_names[category];
242
243 if (strcmp (locale, _nl_current_names[category]) == 0)
244 /* Changing to the same thing. */
245 return (char *) _nl_current_names[category];
246
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. */
251 locale_path = NULL;
252 locale_path_len = 0;
253
254 locpath_var = getenv ("LOCPATH");
255 if (locpath_var != NULL && locpath_var[0] != '\0')
256 if (__argz_create_sep (locpath_var, ':',
257 &locale_path, &locale_path_len) != 0)
258 return NULL;
259
260 if (__argz_add_sep (&locale_path, &locale_path_len, LOCALEDIR, ':') != 0)
261 return NULL;
262
263 if (category == LC_ALL)
264 {
265 /* The user wants to set all categories. The desired locales
266 for the individual categories can be selected by using a
267 composite locale name. This is a semi-colon separated list
268 of entries of the form `CATEGORY=VALUE'. */
269 const char *newnames[__LC_LAST];
270 struct locale_data *newdata[__LC_LAST];
271
272 /* Set all name pointers to the argument name. */
273 for (category = 0; category < __LC_LAST; ++category)
274 if (category != LC_ALL)
275 newnames[category] = (char *) locale;
276
277 if (__builtin_expect (strchr (locale, ';') != NULL, 0))
278 {
279 /* This is a composite name. Make a copy and split it up. */
280 char *np = strdupa (locale);
281 char *cp;
282 int cnt;
283
284 while ((cp = strchr (np, '=')) != NULL)
285 {
286 for (cnt = 0; cnt < __LC_LAST; ++cnt)
287 if (cnt != LC_ALL
288 && (size_t) (cp - np) == _nl_category_name_sizes[cnt]
289 && memcmp (np, _nl_category_names[cnt], cp - np) == 0)
290 break;
291
292 if (cnt == __LC_LAST)
293 /* Bogus category name. */
294 ERROR_RETURN;
295
296 /* Found the category this clause sets. */
297 newnames[cnt] = ++cp;
298 cp = strchr (cp, ';');
299 if (cp != NULL)
300 {
301 /* Examine the next clause. */
302 *cp = '\0';
303 np = cp + 1;
304 }
305 else
306 /* This was the last clause. We are done. */
307 break;
308 }
309
310 for (cnt = 0; cnt < __LC_LAST; ++cnt)
311 if (cnt != LC_ALL && newnames[cnt] == locale)
312 /* The composite name did not specify all categories. */
313 ERROR_RETURN;
314 }
315
316 /* Protect global data. */
317 __libc_lock_lock (__libc_setlocale_lock);
318
319 /* Load the new data for each category. */
320 while (category-- > 0)
321 if (category != LC_ALL)
322 {
323 newdata[category] = _nl_find_locale (locale_path, locale_path_len,
324 category,
325 &newnames[category]);
326
327 if (newdata[category] == NULL)
328 break;
329
330 /* We must not simply free a global locale since we have no
331 control over the usage. So we mark it as un-deletable. */
332 if (newdata[category]->usage_count != UNDELETABLE)
333 newdata[category]->usage_count = UNDELETABLE;
334
335 /* Make a copy of locale name. */
336 if (newnames[category] != _nl_C_name)
337 {
338 newnames[category] = strdup (newnames[category]);
339 if (newnames[category] == NULL)
340 break;
341 }
342 }
343
344 /* Create new composite name. */
345 composite = (category >= 0
346 ? NULL : new_composite_name (LC_ALL, newnames));
347 if (composite != NULL)
348 {
349 /* Now we have loaded all the new data. Put it in place. */
350 for (category = 0; category < __LC_LAST; ++category)
351 if (category != LC_ALL)
352 {
353 setdata (category, newdata[category]);
354 setname (category, newnames[category]);
355 }
356 setname (LC_ALL, composite);
357
358 /* We successfully loaded a new locale. Let the message catalog
359 functions know about this. */
360 ++_nl_msg_cat_cntr;
361 }
362 else
363 for (++category; category < __LC_LAST; ++category)
364 if (category != LC_ALL && newnames[category] != _nl_C_name)
365 free ((char *) newnames[category]);
366
367 /* Critical section left. */
368 __libc_lock_unlock (__libc_setlocale_lock);
369
370 /* Free the resources (the locale path variable. */
371 free (locale_path);
372
373 return composite;
374 }
375 else
376 {
377 struct locale_data *newdata = NULL;
378 const char *newname[1] = { locale };
379
380 /* Protect global data. */
381 __libc_lock_lock (__libc_setlocale_lock);
382
383 if (CATEGORY_USED (category))
384 {
385 /* Only actually load the data if anything will use it. */
386 newdata = _nl_find_locale (locale_path, locale_path_len, category,
387 &newname[0]);
388 if (newdata == NULL)
389 goto abort_single;
390
391 /* We must not simply free a global locale since we have no
392 control over the usage. So we mark it as un-deletable.
393
394 Note: do not remove the `if', it's necessary to copy with
395 the builtin locale data. */
396 if (newdata->usage_count != UNDELETABLE)
397 newdata->usage_count = UNDELETABLE;
398 }
399
400 /* Make a copy of locale name. */
401 if (newname[0] != _nl_C_name)
402 {
403 newname[0] = strdup (newname[0]);
404 if (newname[0] == NULL)
405 goto abort_single;
406 }
407
408 /* Create new composite name. */
409 composite = new_composite_name (category, newname);
410 if (composite == NULL)
411 {
412 if (newname[0] != _nl_C_name)
413 free ((char *) newname[0]);
414
415 /* Say that we don't have any data loaded. */
416 abort_single:
417 newname[0] = NULL;
418 }
419 else
420 {
421 if (CATEGORY_USED (category))
422 setdata (category, newdata);
423
424 setname (category, newname[0]);
425 setname (LC_ALL, composite);
426
427 /* We successfully loaded a new locale. Let the message catalog
428 functions know about this. */
429 ++_nl_msg_cat_cntr;
430 }
431
432 /* Critical section left. */
433 __libc_lock_unlock (__libc_setlocale_lock);
434
435 /* Free the resources (the locale path variable. */
436 free (locale_path);
437
438 return (char *) newname[0];
439 }
440 }
441 libc_hidden_def (setlocale)
442
443 static void __attribute__ ((unused))
444 free_mem (void)
445 {
446 int category;
447
448 for (category = 0; category < __LC_LAST; ++category)
449 if (category != LC_ALL)
450 {
451 struct locale_data *here = _NL_CURRENT_DATA (category);
452 struct loaded_l10nfile *runp = _nl_locale_file_list[category];
453
454 /* If this category is already "C" don't do anything. */
455 if (here != _nl_C[category])
456 {
457 /* We have to be prepared that sometime later me still
458 might need the locale information. */
459 setdata (category, _nl_C[category]);
460 setname (category, _nl_C_name);
461
462 _nl_unload_locale (here);
463 }
464
465 while (runp != NULL)
466 {
467 struct loaded_l10nfile *curr = runp;
468 struct locale_data *data = (struct locale_data *) runp->data;
469
470 if (data != NULL && data != here && data != _nl_C[category])
471 _nl_unload_locale (data);
472 runp = runp->next;
473 free ((char *) curr->filename);
474 free (curr);
475 }
476 }
477
478 setname (LC_ALL, _nl_C_name);
479 }
480 text_set_element (__libc_subfreeres, free_mem);