update from main archive 961208
[glibc.git] / locale / setlocale.c
blob094664216ef028c181af02803ef32ae4fd3e1d9c
1 /* Copyright (C) 1991, 1992, 1995, 1996 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
16 not, 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 <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 const struct locale_data *_nl_current_##category; \
39 extern const struct locale_data _nl_C_##category; \
40 weak_extern (_nl_current_##category) weak_extern (_nl_C_##category)
41 #include "categories.def"
42 #undef DEFINE_CATEGORY
44 /* Array indexed by category of pointers to _nl_current_CATEGORY slots.
45 Elements are zero for categories whose data is never used. */
46 static const struct locale_data * *const _nl_current[] =
48 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
49 [category] = &_nl_current_##category,
50 #include "categories.def"
51 #undef DEFINE_CATEGORY
54 /* Array indexed by category of pointers to _nl_C_CATEGORY slots.
55 Elements are zero for categories whose data is never used. */
56 const struct locale_data *const _nl_C[] =
58 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
59 [category] = &_nl_C_##category,
60 #include "categories.def"
61 #undef DEFINE_CATEGORY
65 /* Define an array of category names (also the environment variable names),
66 indexed by integral category. */
67 const char *const _nl_category_names[] =
69 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
70 [category] = category_name,
71 #include "categories.def"
72 #undef DEFINE_CATEGORY
73 [LC_ALL] = "LC_ALL"
75 /* An array of their lengths, for convenience. */
76 const size_t _nl_category_name_sizes[] =
78 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
79 [category] = sizeof (category_name) - 1,
80 #include "categories.def"
81 #undef DEFINE_CATEGORY
82 [LC_ALL] = sizeof ("LC_ALL") - 1
86 /* Declare the postload functions used below. */
87 #undef NO_POSTLOAD
88 #define NO_POSTLOAD _nl_postload_ctype /* Harmless thing known to exist. */
89 #define DEFINE_CATEGORY(category, category_name, items, postload, b, c, d) \
90 extern void postload (void);
91 #include "categories.def"
92 #undef DEFINE_CATEGORY
93 #undef NO_POSTLOAD
95 /* Define an array indexed by category of postload functions to call after
96 loading and installing that category's data. */
97 static void (*const _nl_category_postload[]) (void) =
99 #define DEFINE_CATEGORY(category, category_name, items, postload, b, c, d) \
100 [category] = postload,
101 #include "categories.def"
102 #undef DEFINE_CATEGORY
106 /* Name of current locale for each individual category.
107 Each is malloc'd unless it is nl_C_name. */
108 static const char *_nl_current_names[] =
110 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
111 [category] = _nl_C_name,
112 #include "categories.def"
113 #undef DEFINE_CATEGORY
114 [LC_ALL] = _nl_C_name /* For LC_ALL. */
118 /* Lock for protecting global data. */
119 __libc_lock_define_initialized (, __libc_setlocale_lock)
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)
130 static inline char *
131 clever_copy (const char *string)
133 size_t len;
134 char *new;
136 if (strcmp (string, "C") == 0 || strcmp (string, "POSIX") == 0)
137 /* This return is dangerous because the returned string might be
138 placed in read-only memory. But everything should be set up to
139 handle this case. */
140 return (char *) _nl_C_name;
142 len = strlen (string) + 1;
143 new = (char *) malloc (len);
144 return new != NULL ? memcpy (new, string, len) : NULL;
148 /* Construct a new composite name. */
149 static inline char *
150 new_composite_name (int category, char *newnames[LC_ALL])
152 size_t last_len;
153 size_t cumlen = 0;
154 int i;
155 char *new, *p;
156 int same = 1;
158 for (i = 0; i < LC_ALL; ++i)
160 char *name = (category == LC_ALL ? newnames[i] :
161 category == i ? newnames[0] :
162 (char *) _nl_current_names[i]);
163 last_len = strlen (name);
164 cumlen += _nl_category_name_sizes[i] + 1 + last_len + 1;
165 if (i > 0 && same && strcmp (name, newnames[0]) != 0)
166 same = 0;
169 if (same)
171 /* All the categories use the same name. */
172 if (strcmp (newnames[0], "C") == 0 || strcmp (newnames[0], "POSIX") == 0)
173 return (char *) _nl_C_name;
175 new = malloc (last_len + 1);
177 return new == NULL ? NULL : memcpy (new, newnames[0], last_len + 1);
180 new = malloc (cumlen);
181 if (new == NULL)
182 return NULL;
183 p = new;
184 for (i = 0; i < LC_ALL; ++i)
186 /* Add "CATEGORY=NAME;" to the string. */
187 char *name = (category == LC_ALL ? newnames[i] :
188 category == i ? newnames[0] :
189 (char *) _nl_current_names[i]);
190 p = __stpcpy (p, _nl_category_names[i]);
191 *p++ = '=';
192 p = __stpcpy (p, name);
193 *p++ = ';';
195 p[-1] = '\0'; /* Clobber the last ';'. */
196 return new;
200 /* Put NAME in _nl_current_names. */
201 static inline void
202 setname (int category, const char *name)
204 if (_nl_current[category] == NULL
205 && _nl_current_names[category] != _nl_C_name)
206 free ((void *) _nl_current_names[category]);
208 _nl_current_names[category] = name;
212 /* Put DATA in *_nl_current[CATEGORY]. */
213 static inline void
214 setdata (int category, const struct locale_data *data)
216 if (_nl_current[category] != NULL)
218 *_nl_current[category] = data;
219 if (_nl_category_postload[category])
220 (*_nl_category_postload[category]) ();
225 char *
226 setlocale (int category, const char *locale)
228 char *locale_path;
229 size_t locale_path_len;
230 const char *locpath_var;
231 char *composite;
233 /* Sanity check for CATEGORY argument. */
234 if (category < 0 || category > LC_ALL)
235 ERROR_RETURN;
237 /* Does user want name of current locale? */
238 if (locale == NULL)
239 return (char *) _nl_current_names[category];
241 if (strcmp (locale, _nl_current_names[category]) == 0)
242 /* Changing to the same thing. */
243 return (char *) _nl_current_names[category];
245 /* We perhaps really have to load some data. So we determine the
246 path in which to look for the data now. The environment variable
247 `LOCPATH' must only be used when the binary has no SUID or SGID
248 bit set. */
249 locale_path = NULL;
250 locale_path_len = 0;
252 locpath_var = __secure_getenv ("LOCPATH");
253 if (locpath_var != NULL && locpath_var[0] != '\0')
254 if (__argz_create_sep (locpath_var, ':',
255 &locale_path, &locale_path_len) != 0)
256 return NULL;
258 if (__argz_add_sep (&locale_path, &locale_path_len, LOCALE_PATH, ':') != 0)
259 return NULL;
261 if (category == LC_ALL)
263 /* The user wants to set all categories. The desired locales
264 for the individual categories can be selected by using a
265 composite locale name. This is a semi-colon separated list
266 of entries of the form `CATEGORY=VALUE'. */
267 char *newnames[LC_ALL];
268 const struct locale_data *newdata[LC_ALL];
270 /* Set all name pointers to the argument name. */
271 for (category = 0; category < LC_ALL; ++category)
272 newnames[category] = (char *) locale;
274 if (strchr (locale, ';') != NULL)
276 /* This is a composite name. Make a copy and split it up. */
277 char *np = strdupa (locale);
278 char *cp;
279 int cnt;
281 while ((cp = strchr (np, '=')) != NULL)
283 for (cnt = 0; cnt < LC_ALL; ++cnt)
284 if ((size_t) (cp - np) == _nl_category_name_sizes[cnt]
285 && memcmp (np, _nl_category_names[cnt], cp - np) == 0)
286 break;
288 if (cnt == LC_ALL)
289 /* Bogus category name. */
290 ERROR_RETURN;
292 /* Found the category this clause sets. */
293 newnames[cnt] = ++cp;
294 cp = strchr (cp, ';');
295 if (cp != NULL)
297 /* Examine the next clause. */
298 *cp = '\0';
299 np = cp + 1;
301 else
302 /* This was the last clause. We are done. */
303 break;
306 for (cnt = 0; cnt < LC_ALL; ++cnt)
307 if (newnames[cnt] == locale)
308 /* The composite name did not specify all categories. */
309 ERROR_RETURN;
312 /* Protect global data. */
313 __libc_lock_lock (__libc_setlocale_lock);
315 /* Load the new data for each category. */
316 while (category-- > 0)
317 /* Only actually load the data if anything will use it. */
318 if (_nl_current[category] != NULL)
320 newdata[category] = _nl_find_locale (locale_path, locale_path_len,
321 category,
322 &newnames[category]);
324 if (newdata[category] == NULL)
325 break;
327 else
329 /* The data is never used; just change the name. */
330 newnames[category] = clever_copy (newnames[category]);
331 if (newnames[category] == NULL)
332 break;
335 /* Create new composite name. */
336 if (category >= 0
337 || (composite = new_composite_name (LC_ALL, newnames)) == NULL)
338 /* Loading this part of the locale failed. Abort the
339 composite load. */
340 composite = NULL;
341 else
343 /* Now we have loaded all the new data. Put it in place. */
344 for (category = 0; category < LC_ALL; ++category)
346 setdata (category, newdata[category]);
347 setname (category, newnames[category]);
349 setname (LC_ALL, composite);
352 /* Critical section left. */
353 __libc_lock_unlock (__libc_setlocale_lock);
355 return composite;
357 else
359 const struct locale_data *newdata = NULL;
360 char *newname = (char *) locale;
362 /* Protect global data. */
363 __libc_lock_lock (__libc_setlocale_lock);
365 if (_nl_current[category] != NULL)
367 /* Only actually load the data if anything will use it. */
368 newdata = _nl_find_locale (locale_path, locale_path_len, category,
369 (char **) &newname);
370 if (newdata == NULL)
371 goto abort_single;
374 /* Create new composite name. */
375 composite = new_composite_name (category, &newname);
376 if (composite == NULL)
378 /* Say that we don't have any data loaded. */
379 abort_single:
380 newname = NULL;
382 else
384 if (_nl_current[category] != NULL)
385 setdata (category, newdata);
387 setname (category, newname);
388 setname (LC_ALL, composite);
391 /* Critical section left. */
392 __libc_lock_unlock (__libc_setlocale_lock);
394 return newname;