Update.
[glibc.git] / locale / setlocale.c
blobcde5b6a4fc360da645094d1ca3db4758581b4727
1 /* Copyright (C) 1991, 92, 95, 96, 97, 98 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, b, c, d) \
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 static struct locale_data * *const _nl_current[] =
47 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
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, b, c, d) \
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, b, c, d) \
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, b, c, d) \
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, b, c, d) \
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, b, c, d) \
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, b, c, d) \
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_ALL])
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_ALL; ++i)
144 const char *name = (category == LC_ALL ? newnames[i] :
145 category == i ? newnames[0] :
146 _nl_current_names[i]);
147 last_len = strlen (name);
148 cumlen += _nl_category_name_sizes[i] + 1 + last_len + 1;
149 if (i > 0 && same && strcmp (name, newnames[0]) != 0)
150 same = 0;
153 if (same)
155 /* All the categories use the same name. */
156 if (strcmp (newnames[0], _nl_C_name) == 0
157 || strcmp (newnames[0], _nl_POSIX_name) == 0)
158 return (char *) _nl_C_name;
160 new = malloc (last_len + 1);
162 return new == NULL ? NULL : memcpy (new, newnames[0], last_len + 1);
165 new = malloc (cumlen);
166 if (new == NULL)
167 return NULL;
168 p = new;
169 for (i = 0; i < LC_ALL; ++i)
171 /* Add "CATEGORY=NAME;" to the string. */
172 const char *name = (category == LC_ALL ? newnames[i] :
173 category == i ? newnames[0] :
174 _nl_current_names[i]);
175 p = __stpcpy (p, _nl_category_names[i]);
176 *p++ = '=';
177 p = __stpcpy (p, name);
178 *p++ = ';';
180 p[-1] = '\0'; /* Clobber the last ';'. */
181 return new;
185 /* Put NAME in _nl_current_names. */
186 static inline void
187 setname (int category, const char *name)
189 if (_nl_current_names[category] == name)
190 return;
192 if (category == LC_ALL && _nl_current_names[category] != _nl_C_name)
193 free ((char *) _nl_current_names[category]);
195 _nl_current_names[category] = name;
199 /* Put DATA in *_nl_current[CATEGORY]. */
200 static inline void
201 setdata (int category, struct locale_data *data)
203 if (_nl_current[category] != NULL)
205 *_nl_current[category] = data;
206 if (_nl_category_postload[category])
207 (*_nl_category_postload[category]) ();
212 char *
213 setlocale (int category, const char *locale)
215 char *locale_path;
216 size_t locale_path_len;
217 const char *locpath_var;
218 char *composite;
220 /* Sanity check for CATEGORY argument. */
221 if (category < 0 || category > LC_ALL)
222 ERROR_RETURN;
224 /* Does user want name of current locale? */
225 if (locale == NULL)
226 return (char *) _nl_current_names[category];
228 if (strcmp (locale, _nl_current_names[category]) == 0)
229 /* Changing to the same thing. */
230 return (char *) _nl_current_names[category];
232 /* We perhaps really have to load some data. So we determine the
233 path in which to look for the data now. The environment variable
234 `LOCPATH' must only be used when the binary has no SUID or SGID
235 bit set. */
236 locale_path = NULL;
237 locale_path_len = 0;
239 locpath_var = __secure_getenv ("LOCPATH");
240 if (locpath_var != NULL && locpath_var[0] != '\0')
241 if (__argz_create_sep (locpath_var, ':',
242 &locale_path, &locale_path_len) != 0)
243 return NULL;
245 if (__argz_add_sep (&locale_path, &locale_path_len, LOCALE_PATH, ':') != 0)
246 return NULL;
248 if (category == LC_ALL)
250 /* The user wants to set all categories. The desired locales
251 for the individual categories can be selected by using a
252 composite locale name. This is a semi-colon separated list
253 of entries of the form `CATEGORY=VALUE'. */
254 const char *newnames[LC_ALL];
255 struct locale_data *newdata[LC_ALL];
257 /* Set all name pointers to the argument name. */
258 for (category = 0; category < LC_ALL; ++category)
259 newnames[category] = (char *) locale;
261 if (strchr (locale, ';') != NULL)
263 /* This is a composite name. Make a copy and split it up. */
264 char *np = strdupa (locale);
265 char *cp;
266 int cnt;
268 while ((cp = strchr (np, '=')) != NULL)
270 for (cnt = 0; cnt < LC_ALL; ++cnt)
271 if ((size_t) (cp - np) == _nl_category_name_sizes[cnt]
272 && memcmp (np, _nl_category_names[cnt], cp - np) == 0)
273 break;
275 if (cnt == LC_ALL)
276 /* Bogus category name. */
277 ERROR_RETURN;
279 /* Found the category this clause sets. */
280 newnames[cnt] = ++cp;
281 cp = strchr (cp, ';');
282 if (cp != NULL)
284 /* Examine the next clause. */
285 *cp = '\0';
286 np = cp + 1;
288 else
289 /* This was the last clause. We are done. */
290 break;
293 for (cnt = 0; cnt < LC_ALL; ++cnt)
294 if (newnames[cnt] == locale)
295 /* The composite name did not specify all categories. */
296 ERROR_RETURN;
299 /* Protect global data. */
300 __libc_lock_lock (__libc_setlocale_lock);
302 /* Load the new data for each category. */
303 while (category-- > 0)
305 newdata[category] = _nl_find_locale (locale_path, locale_path_len,
306 category,
307 &newnames[category]);
309 if (newdata[category] == NULL)
310 break;
312 /* We must not simply free a global locale since we have no
313 control over the usage. So we mark it as un-deletable. */
314 if (newdata[category]->usage_count != UNDELETABLE)
315 newdata[category]->usage_count = UNDELETABLE;
318 /* Create new composite name. */
319 if (category >= 0
320 || (composite = new_composite_name (LC_ALL, newnames)) == NULL)
321 /* Loading this part of the locale failed. Abort the
322 composite load. */
323 composite = NULL;
324 else
326 /* Now we have loaded all the new data. Put it in place. */
327 for (category = 0; category < LC_ALL; ++category)
329 setdata (category, newdata[category]);
330 setname (category, newnames[category]);
332 setname (LC_ALL, composite);
335 /* Critical section left. */
336 __libc_lock_unlock (__libc_setlocale_lock);
338 /* Free the resources (the locale path variable. */
339 free (locale_path);
341 return composite;
343 else
345 struct locale_data *newdata = NULL;
346 const char *newname[1] = { locale };
348 /* Protect global data. */
349 __libc_lock_lock (__libc_setlocale_lock);
351 if (_nl_current[category] != NULL)
353 /* Only actually load the data if anything will use it. */
354 newdata = _nl_find_locale (locale_path, locale_path_len, category,
355 &newname[0]);
356 if (newdata == NULL)
357 goto abort_single;
359 /* We must not simply free a global locale since we have no
360 control over the usage. So we mark it as un-deletable.
362 Note: do not remove the `if', it's necessary to copy with
363 the builtin locale data. */
364 if (newdata->usage_count != UNDELETABLE)
365 newdata->usage_count = UNDELETABLE;
368 /* Create new composite name. */
369 composite = new_composite_name (category, newname);
370 if (composite == NULL)
372 /* Say that we don't have any data loaded. */
373 abort_single:
374 newname[0] = NULL;
376 else
378 if (_nl_current[category] != NULL)
379 setdata (category, newdata);
381 setname (category, newname[0]);
382 setname (LC_ALL, composite);
385 /* Critical section left. */
386 __libc_lock_unlock (__libc_setlocale_lock);
388 /* Free the resources (the locale path variable. */
389 free (locale_path);
391 return (char *) newname[0];