]> sourceware.org Git - glibc.git/blame - locale/setlocale.c
update from main archive
[glibc.git] / locale / setlocale.c
CommitLineData
19bc17a9 1/* Copyright (C) 1991, 1992, 1995, 1996 Free Software Foundation, Inc.
28f540f4
RM
2This file is part of the GNU C Library.
3
4The GNU C Library is free software; you can redistribute it and/or
5modify it under the terms of the GNU Library General Public License as
6published by the Free Software Foundation; either version 2 of the
7License, or (at your option) any later version.
8
9The GNU C Library is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12Library General Public License for more details.
13
14You should have received a copy of the GNU Library General Public
15License along with the GNU C Library; see the file COPYING.LIB. If
7a12c6bb
RM
16not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17Boston, MA 02111-1307, USA. */
28f540f4 18
7a12c6bb
RM
19#include <alloca.h>
20#include <argz.h>
28f540f4 21#include <errno.h>
a5113b14 22#include <libc-lock.h>
933e73fa 23#include <locale.h>
7a12c6bb
RM
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27
933e73fa
RM
28#include "localeinfo.h"
29
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) \
38extern const struct locale_data *_nl_current_##category; \
39extern const struct locale_data _nl_C_##category; \
0676b5fd 40weak_extern (_nl_current_##category) weak_extern (_nl_C_##category)
933e73fa
RM
41#include "categories.def"
42#undef DEFINE_CATEGORY
43
44/* Array indexed by category of pointers to _nl_current_CATEGORY slots.
45 Elements are zero for categories whose data is never used. */
7a12c6bb
RM
46static const struct locale_data * *const _nl_current[] =
47 {
933e73fa 48#define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
7a12c6bb 49 [category] = &_nl_current_##category,
933e73fa
RM
50#include "categories.def"
51#undef DEFINE_CATEGORY
7a12c6bb 52 };
933e73fa
RM
53
54/* Array indexed by category of pointers to _nl_C_CATEGORY slots.
55 Elements are zero for categories whose data is never used. */
56const struct locale_data *const _nl_C[] =
036cc82f 57 {
933e73fa 58#define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
036cc82f 59 [category] = &_nl_C_##category,
933e73fa
RM
60#include "categories.def"
61#undef DEFINE_CATEGORY
036cc82f 62 };
933e73fa
RM
63
64
65/* Define an array of category names (also the environment variable names),
66 indexed by integral category. */
67const char *const _nl_category_names[] =
68 {
69#define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
70 [category] = category_name,
71#include "categories.def"
72#undef DEFINE_CATEGORY
7a12c6bb 73 [LC_ALL] = "LC_ALL"
933e73fa
RM
74 };
75/* An array of their lengths, for convenience. */
76const size_t _nl_category_name_sizes[] =
77 {
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
7a12c6bb 82 [LC_ALL] = sizeof ("LC_ALL") - 1
933e73fa 83 };
28f540f4
RM
84
85
933e73fa
RM
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) \
90extern void postload (void);
91#include "categories.def"
92#undef DEFINE_CATEGORY
93#undef NO_POSTLOAD
94
95/* Define an array indexed by category of postload functions to call after
96 loading and installing that category's data. */
a5113b14 97static void (*const _nl_category_postload[]) (void) =
933e73fa
RM
98 {
99#define DEFINE_CATEGORY(category, category_name, items, postload, b, c, d) \
100 [category] = postload,
101#include "categories.def"
102#undef DEFINE_CATEGORY
103 };
104
105
933e73fa
RM
106/* Name of current locale for each individual category.
107 Each is malloc'd unless it is nl_C_name. */
7a12c6bb 108static const char *_nl_current_names[] =
933e73fa
RM
109 {
110#define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
7a12c6bb 111 [category] = _nl_C_name,
933e73fa
RM
112#include "categories.def"
113#undef DEFINE_CATEGORY
7a12c6bb 114 [LC_ALL] = _nl_C_name /* For LC_ALL. */
933e73fa
RM
115 };
116
933e73fa 117
a5113b14 118/* Lock for protecting global data. */
c4029823 119__libc_lock_define_initialized (, __libc_setlocale_lock)
a5113b14 120
933e73fa 121
7a12c6bb
RM
122/* Use this when we come along an error. */
123#define ERROR_RETURN \
124 do { \
c4029823 125 __set_errno (EINVAL); \
7a12c6bb
RM
126 return NULL; \
127 } while (0)
933e73fa 128
7a12c6bb
RM
129
130static inline char *
131clever_copy (const char *string)
28f540f4 132{
7a12c6bb
RM
133 size_t len;
134 char *new;
135
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;
141
142 len = strlen (string) + 1;
143 new = (char *) malloc (len);
144 return new != NULL ? memcpy (new, string, len) : NULL;
145}
933e73fa 146
933e73fa 147
7a12c6bb
RM
148/* Construct a new composite name. */
149static inline char *
150new_composite_name (int category, char *newnames[LC_ALL])
151{
152 size_t last_len;
153 size_t cumlen = 0;
154 int i;
155 char *new, *p;
156 int same = 1;
157
158 for (i = 0; i < LC_ALL; ++i)
933e73fa 159 {
7a12c6bb
RM
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;
933e73fa 167 }
7a12c6bb
RM
168
169 if (same)
933e73fa 170 {
7a12c6bb
RM
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;
174
175 new = malloc (last_len + 1);
7a12c6bb 176
036cc82f 177 return new == NULL ? NULL : memcpy (new, newnames[0], last_len + 1);
933e73fa 178 }
7a12c6bb
RM
179
180 new = malloc (cumlen);
181 if (new == NULL)
182 return NULL;
183 p = new;
184 for (i = 0; i < LC_ALL; ++i)
933e73fa 185 {
7a12c6bb
RM
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++ = ';';
933e73fa 194 }
7a12c6bb
RM
195 p[-1] = '\0'; /* Clobber the last ';'. */
196 return new;
197}
933e73fa 198
933e73fa 199
7a12c6bb
RM
200/* Put NAME in _nl_current_names. */
201static inline void
202setname (int category, const char *name)
203{
204 if (_nl_current[category] == NULL
205 && _nl_current_names[category] != _nl_C_name)
206 free ((void *) _nl_current_names[category]);
207
208 _nl_current_names[category] = name;
209}
210
211
212/* Put DATA in *_nl_current[CATEGORY]. */
213static inline void
214setdata (int category, const struct locale_data *data)
215{
216 if (_nl_current[category] != NULL)
933e73fa 217 {
7a12c6bb
RM
218 *_nl_current[category] = data;
219 if (_nl_category_postload[category])
220 (*_nl_category_postload[category]) ();
933e73fa 221 }
7a12c6bb 222}
933e73fa 223
933e73fa 224
7a12c6bb
RM
225char *
226setlocale (int category, const char *locale)
227{
7a12c6bb
RM
228 char *locale_path;
229 size_t locale_path_len;
230 char *composite;
933e73fa 231
7a12c6bb
RM
232 /* Sanity check for CATEGORY argument. */
233 if (category < 0 || category > LC_ALL)
234 ERROR_RETURN;
235
236 /* Does user want name of current locale? */
237 if (locale == NULL)
238 return (char *) _nl_current_names[category];
239
240 if (strcmp (locale, _nl_current_names[category]) == 0)
933e73fa 241 /* Changing to the same thing. */
7a12c6bb
RM
242 return (char *) _nl_current_names[category];
243
244 /* We perhaps really have to load some data. So we determine the
a5113b14
UD
245 path in which to look for the data now. The environment variable
246 `LOCPATH' must only be used when the binary has no SUID or SGID
7a12c6bb
RM
247 bit set. */
248 locale_path = NULL;
249 locale_path_len = 0;
250
23396375
UD
251 if (!__libc_enable_secure)
252 {
253 char *locpath_var = getenv ("LOCPATH");
254
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 }
933e73fa 260
7a12c6bb
RM
261 if (__argz_append (&locale_path, &locale_path_len,
262 LOCALE_PATH, sizeof (LOCALE_PATH)) != 0)
263 return NULL;
db2286f6 264
933e73fa
RM
265 if (category == LC_ALL)
266 {
7a12c6bb
RM
267 /* The user wants to set all categories. The desired locales
268 for the individual categories can be selected by using a
269 composite locale name. This is a semi-colon separated list
270 of entries of the form `CATEGORY=VALUE'. */
933e73fa 271 char *newnames[LC_ALL];
7a12c6bb 272 const struct locale_data *newdata[LC_ALL];
933e73fa
RM
273
274 /* Set all name pointers to the argument name. */
275 for (category = 0; category < LC_ALL; ++category)
7a12c6bb 276 newnames[category] = (char *) locale;
db2286f6 277
7a12c6bb 278 if (strchr (locale, ';') != NULL)
933e73fa 279 {
7a12c6bb
RM
280 /* This is a composite name. Make a copy and split it up. */
281 char *np = strdupa (locale);
282 char *cp;
283 int cnt;
933e73fa 284
7a12c6bb 285 while ((cp = strchr (np, '=')) != NULL)
933e73fa 286 {
7a12c6bb 287 for (cnt = 0; cnt < LC_ALL; ++cnt)
ce7a5ef4 288 if ((size_t) (cp - np) == _nl_category_name_sizes[cnt]
7a12c6bb 289 && memcmp (np, _nl_category_names[cnt], cp - np) == 0)
933e73fa 290 break;
7a12c6bb
RM
291
292 if (cnt == LC_ALL)
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)
933e73fa 300 {
7a12c6bb
RM
301 /* Examine the next clause. */
302 *cp = '\0';
303 np = cp + 1;
933e73fa 304 }
7a12c6bb
RM
305 else
306 /* This was the last clause. We are done. */
307 break;
933e73fa
RM
308 }
309
7a12c6bb
RM
310 for (cnt = 0; cnt < LC_ALL; ++cnt)
311 if (newnames[cnt] == locale)
933e73fa 312 /* The composite name did not specify all categories. */
7a12c6bb 313 ERROR_RETURN;
933e73fa 314 }
19bc17a9 315
a5113b14 316 /* Protect global data. */
c4029823 317 __libc_lock_lock (__libc_setlocale_lock);
a5113b14 318
933e73fa
RM
319 /* Load the new data for each category. */
320 while (category-- > 0)
321 /* Only actually load the data if anything will use it. */
7a12c6bb 322 if (_nl_current[category] != NULL)
933e73fa 323 {
7a12c6bb
RM
324 newdata[category] = _nl_find_locale (locale_path, locale_path_len,
325 category,
933e73fa 326 &newnames[category]);
7a12c6bb
RM
327
328 if (newdata[category] == NULL)
845dcb57 329 break;
933e73fa
RM
330 }
331 else
332 {
333 /* The data is never used; just change the name. */
7a12c6bb
RM
334 newnames[category] = clever_copy (newnames[category]);
335 if (newnames[category] == NULL)
845dcb57 336 break;
933e73fa
RM
337 }
338
7a12c6bb 339 /* Create new composite name. */
845dcb57
UD
340 if (category >= 0
341 || (composite = new_composite_name (LC_ALL, newnames)) == NULL)
342 /* Loading this part of the locale failed. Abort the
343 composite load. */
344 composite = NULL;
a5113b14 345 else
933e73fa 346 {
a5113b14
UD
347 /* Now we have loaded all the new data. Put it in place. */
348 for (category = 0; category < LC_ALL; ++category)
349 {
350 setdata (category, newdata[category]);
351 setname (category, newnames[category]);
352 }
353 setname (LC_ALL, composite);
933e73fa 354 }
a5113b14
UD
355
356 /* Critical section left. */
c4029823 357 __libc_lock_unlock (__libc_setlocale_lock);
933e73fa
RM
358
359 return composite;
360 }
361 else
362 {
6990326c 363 const struct locale_data *newdata = NULL;
845dcb57 364 char *newname = (char *) locale;
7a12c6bb 365
a5113b14 366 /* Protect global data. */
c4029823 367 __libc_lock_lock (__libc_setlocale_lock);
a5113b14 368
7a12c6bb 369 if (_nl_current[category] != NULL)
933e73fa 370 {
7a12c6bb 371 /* Only actually load the data if anything will use it. */
7a12c6bb
RM
372 newdata = _nl_find_locale (locale_path, locale_path_len, category,
373 (char **) &newname);
374 if (newdata == NULL)
a5113b14 375 goto abort_single;
933e73fa
RM
376 }
377
7a12c6bb 378 /* Create new composite name. */
933e73fa 379 composite = new_composite_name (category, &newname);
7a12c6bb 380 if (composite == NULL)
933e73fa 381 {
845dcb57 382 /* Say that we don't have any data loaded. */
a5113b14
UD
383 abort_single:
384 newname = NULL;
933e73fa 385 }
a5113b14
UD
386 else
387 {
388 if (_nl_current[category] != NULL)
389 setdata (category, newdata);
28f540f4 390
a5113b14
UD
391 setname (category, newname);
392 setname (LC_ALL, composite);
393 }
28f540f4 394
a5113b14 395 /* Critical section left. */
c4029823 396 __libc_lock_unlock (__libc_setlocale_lock);
28f540f4 397
933e73fa
RM
398 return newname;
399 }
28f540f4 400}
This page took 0.099769 seconds and 5 git commands to generate.