* stdio-common/vfprintf.c (vfprintf): Also set `is_long' if the
[glibc.git] / locale / setlocale.c
blob03456fba712c751dbe264f99007ef67d5c3e433e
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 (__builtin_expect (category, 0) < 0
224 || __builtin_expect (category, 0) >= __LC_LAST)
225 ERROR_RETURN;
227 /* Does user want name of current locale? */
228 if (locale == NULL)
229 return (char *) _nl_current_names[category];
231 if (strcmp (locale, _nl_current_names[category]) == 0)
232 /* Changing to the same thing. */
233 return (char *) _nl_current_names[category];
235 /* We perhaps really have to load some data. So we determine the
236 path in which to look for the data now. The environment variable
237 `LOCPATH' must only be used when the binary has no SUID or SGID
238 bit set. */
239 locale_path = NULL;
240 locale_path_len = 0;
242 locpath_var = __secure_getenv ("LOCPATH");
243 if (locpath_var != NULL && locpath_var[0] != '\0')
244 if (__argz_create_sep (locpath_var, ':',
245 &locale_path, &locale_path_len) != 0)
246 return NULL;
248 if (__argz_add_sep (&locale_path, &locale_path_len, LOCALE_PATH, ':') != 0)
249 return NULL;
251 if (category == LC_ALL)
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];
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;
265 if (__builtin_expect (strchr (locale, ';') != NULL, 0))
267 /* This is a composite name. Make a copy and split it up. */
268 char *np = strdupa (locale);
269 char *cp;
270 int cnt;
272 while ((cp = strchr (np, '=')) != NULL)
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;
280 if (cnt == __LC_LAST)
281 /* Bogus category name. */
282 ERROR_RETURN;
284 /* Found the category this clause sets. */
285 newnames[cnt] = ++cp;
286 cp = strchr (cp, ';');
287 if (cp != NULL)
289 /* Examine the next clause. */
290 *cp = '\0';
291 np = cp + 1;
293 else
294 /* This was the last clause. We are done. */
295 break;
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;
304 /* Protect global data. */
305 __libc_lock_lock (__libc_setlocale_lock);
307 /* Load the new data for each category. */
308 while (category-- > 0)
309 if (category != LC_ALL)
311 newdata[category] = _nl_find_locale (locale_path, locale_path_len,
312 category,
313 &newnames[category]);
315 if (newdata[category] == NULL)
316 break;
318 /* We must not simply free a global locale since we have no
319 control over the usage. So we mark it as un-deletable. */
320 if (newdata[category]->usage_count != UNDELETABLE)
321 newdata[category]->usage_count = UNDELETABLE;
324 /* Create new composite name. */
325 composite = (category >= 0
326 ? NULL : new_composite_name (LC_ALL, newnames));
327 if (composite != NULL)
329 /* Now we have loaded all the new data. Put it in place. */
330 for (category = 0; category < __LC_LAST; ++category)
331 if (category != LC_ALL)
333 setdata (category, newdata[category]);
334 setname (category, newnames[category]);
336 setname (LC_ALL, composite);
339 /* Critical section left. */
340 __libc_lock_unlock (__libc_setlocale_lock);
342 /* Free the resources (the locale path variable. */
343 free (locale_path);
345 return composite;
347 else
349 struct locale_data *newdata = NULL;
350 const char *newname[1] = { locale };
352 /* Protect global data. */
353 __libc_lock_lock (__libc_setlocale_lock);
355 if (_nl_current[category] != NULL)
357 /* Only actually load the data if anything will use it. */
358 newdata = _nl_find_locale (locale_path, locale_path_len, category,
359 &newname[0]);
360 if (newdata == NULL)
361 goto abort_single;
363 /* We must not simply free a global locale since we have no
364 control over the usage. So we mark it as un-deletable.
366 Note: do not remove the `if', it's necessary to copy with
367 the builtin locale data. */
368 if (newdata->usage_count != UNDELETABLE)
369 newdata->usage_count = UNDELETABLE;
372 /* Create new composite name. */
373 composite = new_composite_name (category, newname);
374 if (composite == NULL)
376 /* Say that we don't have any data loaded. */
377 abort_single:
378 newname[0] = NULL;
380 else
382 if (_nl_current[category] != NULL)
383 setdata (category, newdata);
385 setname (category, newname[0]);
386 setname (LC_ALL, composite);
389 /* Critical section left. */
390 __libc_lock_unlock (__libc_setlocale_lock);
392 /* Free the resources (the locale path variable. */
393 free (locale_path);
395 return (char *) newname[0];
400 static void __attribute__ ((unused))
401 free_mem (void)
403 int category;
405 for (category = 0; category < __LC_LAST; ++category)
406 if (category != LC_ALL)
408 struct locale_data *here = *_nl_current[category];
410 /* If this category is already "C" don't do anything. */
411 if (here == _nl_C[category])
412 continue;
414 /* We have to be prepared that sometime later me still might
415 need the locale information. */
416 setdata (category, _nl_C[category]);
417 setname (category, _nl_C_name);
419 _nl_unload_locale (here);
422 setname (LC_ALL, _nl_C_name);
424 text_set_element (__libc_subfreeres, free_mem);