]> sourceware.org Git - glibc.git/blob - locale/setlocale.c
Update.
[glibc.git] / locale / setlocale.c
1 /* Copyright (C) 1991, 92, 1995-2000, 2002, 2003 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 #ifdef NL_CURRENT_INDIRECT
31
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. */
37
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
44
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[] =
49 {
50 # define DEFINE_CATEGORY(category, category_name, items, a) \
51 [category] = &_nl_current_##category##_used,
52 # include "categories.def"
53 # undef DEFINE_CATEGORY
54 };
55
56 # define CATEGORY_USED(category) (_nl_current_used[category] != 0)
57
58 #else
59
60 /* The shared library always loads all the categories,
61 and the current global settings are kept in _nl_global_locale. */
62
63 # define CATEGORY_USED(category) (1)
64
65 #endif
66
67
68 /* Define an array of category names (also the environment variable names),
69 indexed by integral category. */
70 const char *const _nl_category_names[] =
71 {
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"
77 };
78 /* An array of their lengths, for convenience. */
79 const size_t _nl_category_name_sizes[] =
80 {
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
86 };
87
88
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
94
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
103
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) =
107 {
108 #define DEFINE_CATEGORY(category, category_name, items, postload) \
109 [category] = postload,
110 #include "categories.def"
111 #undef DEFINE_CATEGORY
112 };
113
114
115 /* Lock for protecting global data. */
116 __libc_lock_define_initialized (, __libc_setlocale_lock attribute_hidden)
117
118 /* Defined in loadmsgcat.c. */
119 extern int _nl_msg_cat_cntr;
120
121
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)
128
129
130 /* Construct a new composite name. */
131 static char *
132 new_composite_name (int category, const char *newnames[__LC_LAST])
133 {
134 size_t last_len = 0;
135 size_t cumlen = 0;
136 int i;
137 char *new, *p;
138 int same = 1;
139
140 for (i = 0; i < __LC_LAST; ++i)
141 if (i != LC_ALL)
142 {
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;
150 }
151
152 if (same)
153 {
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;
158
159 new = malloc (last_len + 1);
160
161 return new == NULL ? NULL : memcpy (new, newnames[0], last_len + 1);
162 }
163
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)
170 {
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++ = ';';
179 }
180 p[-1] = '\0'; /* Clobber the last ';'. */
181 return new;
182 }
183
184
185 /* Put NAME in _nl_global_locale.__names. */
186 static inline void
187 setname (int category, const char *name)
188 {
189 if (_nl_global_locale.__names[category] == name)
190 return;
191
192 if (_nl_global_locale.__names[category] != _nl_C_name)
193 free ((char *) _nl_global_locale.__names[category]);
194
195 _nl_global_locale.__names[category] = name;
196 }
197
198 /* Put DATA in *_nl_current[CATEGORY]. */
199 static inline void
200 setdata (int category, struct locale_data *data)
201 {
202 if (CATEGORY_USED (category))
203 {
204 _nl_global_locale.__locales[category] = data;
205 if (_nl_category_postload[category])
206 (*_nl_category_postload[category]) ();
207 }
208 }
209
210 char *
211 setlocale (int category, const char *locale)
212 {
213 char *locale_path;
214 size_t locale_path_len;
215 const char *locpath_var;
216 char *composite;
217
218 /* Sanity check for CATEGORY argument. */
219 if (__builtin_expect (category, 0) < 0
220 || __builtin_expect (category, 0) >= __LC_LAST)
221 ERROR_RETURN;
222
223 /* Does user want name of current locale? */
224 if (locale == NULL)
225 return (char *) _nl_global_locale.__names[category];
226
227 if (strcmp (locale, _nl_global_locale.__names[category]) == 0)
228 /* Changing to the same thing. */
229 return (char *) _nl_global_locale.__names[category];
230
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;
238
239 locpath_var = getenv ("LOCPATH");
240 if (locpath_var != NULL && locpath_var[0] != '\0')
241 {
242 if (__argz_create_sep (locpath_var, ':',
243 &locale_path, &locale_path_len) != 0)
244 return NULL;
245
246 if (__argz_add_sep (&locale_path, &locale_path_len,
247 _nl_default_locale_path, ':') != 0)
248 return NULL;
249 }
250
251 if (category == LC_ALL)
252 {
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];
259
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;
264
265 if (__builtin_expect (strchr (locale, ';') != NULL, 0))
266 {
267 /* This is a composite name. Make a copy and split it up. */
268 char *np = strdupa (locale);
269 char *cp;
270 int cnt;
271
272 while ((cp = strchr (np, '=')) != NULL)
273 {
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;
279
280 if (cnt == __LC_LAST)
281 /* Bogus category name. */
282 ERROR_RETURN;
283
284 /* Found the category this clause sets. */
285 newnames[cnt] = ++cp;
286 cp = strchr (cp, ';');
287 if (cp != NULL)
288 {
289 /* Examine the next clause. */
290 *cp = '\0';
291 np = cp + 1;
292 }
293 else
294 /* This was the last clause. We are done. */
295 break;
296 }
297
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;
302 }
303
304 /* Protect global data. */
305 __libc_lock_lock (__libc_setlocale_lock);
306
307 /* Load the new data for each category. */
308 while (category-- > 0)
309 if (category != LC_ALL)
310 {
311 newdata[category] = _nl_find_locale (locale_path, locale_path_len,
312 category,
313 &newnames[category]);
314
315 if (newdata[category] == NULL)
316 {
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;
323 }
324
325 /* We must not simply free a global locale since we have
326 no control over the usage. So we mark it as
327 un-deletable. And yes, the 'if' is needed, the data
328 might be in read-only memory. */
329 if (newdata[category]->usage_count != UNDELETABLE)
330 newdata[category]->usage_count = UNDELETABLE;
331
332 /* Make a copy of locale name. */
333 if (newnames[category] != _nl_C_name)
334 {
335 if (strcmp (newnames[category],
336 _nl_global_locale.__names[category]) == 0)
337 newnames[category] = _nl_global_locale.__names[category];
338 else
339 {
340 newnames[category] = __strdup (newnames[category]);
341 if (newnames[category] == NULL)
342 break;
343 }
344 }
345 }
346
347 /* Create new composite name. */
348 composite = (category >= 0
349 ? NULL : new_composite_name (LC_ALL, newnames));
350 if (composite != NULL)
351 {
352 /* Now we have loaded all the new data. Put it in place. */
353 for (category = 0; category < __LC_LAST; ++category)
354 if (category != LC_ALL)
355 {
356 setdata (category, newdata[category]);
357 setname (category, newnames[category]);
358 }
359 setname (LC_ALL, composite);
360
361 /* We successfully loaded a new locale. Let the message catalog
362 functions know about this. */
363 ++_nl_msg_cat_cntr;
364 }
365 else
366 for (++category; category < __LC_LAST; ++category)
367 if (category != LC_ALL && newnames[category] != _nl_C_name
368 && newnames[category] != _nl_global_locale.__names[category])
369 free ((char *) newnames[category]);
370
371 /* Critical section left. */
372 __libc_lock_unlock (__libc_setlocale_lock);
373
374 /* Free the resources (the locale path variable. */
375 free (locale_path);
376
377 return composite;
378 }
379 else
380 {
381 struct locale_data *newdata = NULL;
382 const char *newname[1] = { locale };
383
384 /* Protect global data. */
385 __libc_lock_lock (__libc_setlocale_lock);
386
387 if (CATEGORY_USED (category))
388 {
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;
394
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.
397
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;
402 }
403
404 /* Make a copy of locale name. */
405 if (newname[0] != _nl_C_name)
406 {
407 newname[0] = __strdup (newname[0]);
408 if (newname[0] == NULL)
409 goto abort_single;
410 }
411
412 /* Create new composite name. */
413 composite = new_composite_name (category, newname);
414 if (composite == NULL)
415 {
416 if (newname[0] != _nl_C_name)
417 free ((char *) newname[0]);
418
419 /* Say that we don't have any data loaded. */
420 abort_single:
421 newname[0] = NULL;
422 }
423 else
424 {
425 if (CATEGORY_USED (category))
426 setdata (category, newdata);
427
428 setname (category, newname[0]);
429 setname (LC_ALL, composite);
430
431 /* We successfully loaded a new locale. Let the message catalog
432 functions know about this. */
433 ++_nl_msg_cat_cntr;
434 }
435
436 /* Critical section left. */
437 __libc_lock_unlock (__libc_setlocale_lock);
438
439 /* Free the resources (the locale path variable. */
440 free (locale_path);
441
442 return (char *) newname[0];
443 }
444 }
445 libc_hidden_def (setlocale)
446
447 static void
448 free_category (int category,
449 struct locale_data *here, struct locale_data *c_data)
450 {
451 struct loaded_l10nfile *runp = _nl_locale_file_list[category];
452
453 /* If this category is already "C" don't do anything. */
454 if (here != c_data)
455 {
456 /* We have to be prepared that sometime later we still
457 might need the locale information. */
458 setdata (category, c_data);
459 setname (category, _nl_C_name);
460 }
461
462 while (runp != NULL)
463 {
464 struct loaded_l10nfile *curr = runp;
465 struct locale_data *data = (struct locale_data *) runp->data;
466
467 if (data != NULL && data != c_data)
468 _nl_unload_locale (data);
469 runp = runp->next;
470 free ((char *) curr->filename);
471 free (curr);
472 }
473 }
474
475 libc_freeres_fn (free_mem)
476 {
477 #ifdef NL_CURRENT_INDIRECT
478 /* We don't use the loop because we want to have individual weak
479 symbol references here. */
480 # define DEFINE_CATEGORY(category, category_name, items, a) \
481 if (CATEGORY_USED (category)) \
482 { \
483 extern struct locale_data _nl_C_##category; \
484 weak_extern (_nl_C_##category) \
485 free_category (category, *_nl_current_##category, &_nl_C_##category); \
486 }
487 # include "categories.def"
488 # undef DEFINE_CATEGORY
489 #else
490 int category;
491
492 for (category = 0; category < __LC_LAST; ++category)
493 if (category != LC_ALL)
494 free_category (category, _NL_CURRENT_DATA (category),
495 _nl_C_locobj.__locales[category]);
496 #endif
497
498 setname (LC_ALL, _nl_C_name);
499
500 /* This frees the data structures associated with the locale archive.
501 The locales from the archive are not in the file list, so we have
502 not called _nl_unload_locale on them above. */
503 _nl_archive_subfreeres ();
504 }
This page took 0.056338 seconds and 5 git commands to generate.