Update.
[platform/upstream/glibc.git] / locale / setlocale.c
1 /* Copyright (C) 1991, 92, 95, 96, 97, 98 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 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.
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    Library General Public License for more details.
13
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.  */
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 /* 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 weak_extern (_nl_current_##category) weak_extern (_nl_C_##category)
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.  */
46 static struct locale_data * *const _nl_current[] =
47   {
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
52     /* We need this additional element to simplify the code.  It must
53        simply be != NULL.  */
54     [LC_ALL] = (struct locale_data **) ~0ul
55   };
56
57 /* Array indexed by category of pointers to _nl_C_CATEGORY slots.
58    Elements are zero for categories whose data is never used.  */
59 struct locale_data *const _nl_C[] =
60   {
61 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
62     [category] = &_nl_C_##category,
63 #include "categories.def"
64 #undef  DEFINE_CATEGORY
65   };
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, b, c, d) \
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, b, c, d) \
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 /* Declare the postload functions used below.  */
90 #undef  NO_POSTLOAD
91 #define NO_POSTLOAD _nl_postload_ctype /* Harmless thing known to exist.  */
92 #define DEFINE_CATEGORY(category, category_name, items, postload, b, c, d) \
93 extern void postload (void);
94 #include "categories.def"
95 #undef  DEFINE_CATEGORY
96 #undef  NO_POSTLOAD
97
98 /* Define an array indexed by category of postload functions to call after
99    loading and installing that category's data.  */
100 static void (*const _nl_category_postload[]) (void) =
101   {
102 #define DEFINE_CATEGORY(category, category_name, items, postload, b, c, d) \
103     [category] = postload,
104 #include "categories.def"
105 #undef  DEFINE_CATEGORY
106   };
107
108
109 /* Name of current locale for each individual category.
110    Each is malloc'd unless it is nl_C_name.  */
111 static const char *_nl_current_names[] =
112   {
113 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
114     [category] = _nl_C_name,
115 #include "categories.def"
116 #undef  DEFINE_CATEGORY
117     [LC_ALL] = _nl_C_name               /* For LC_ALL.  */
118   };
119
120
121 /* Lock for protecting global data.  */
122 __libc_lock_define_initialized (, __libc_setlocale_lock)
123
124
125 /* Use this when we come along an error.  */
126 #define ERROR_RETURN                                                          \
127   do {                                                                        \
128     __set_errno (EINVAL);                                                     \
129     return NULL;                                                              \
130   } while (0)
131
132
133 /* Construct a new composite name.  */
134 static inline char *
135 new_composite_name (int category, const char *newnames[LC_ALL])
136 {
137   size_t last_len;
138   size_t cumlen = 0;
139   int i;
140   char *new, *p;
141   int same = 1;
142
143   for (i = 0; i < LC_ALL; ++i)
144     {
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;
152     }
153
154   if (same)
155     {
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;
160
161       new = malloc (last_len + 1);
162
163       return new == NULL ? NULL : memcpy (new, newnames[0], last_len + 1);
164     }
165
166   new = malloc (cumlen);
167   if (new == NULL)
168     return NULL;
169   p = new;
170   for (i = 0; i < LC_ALL; ++i)
171     {
172       /* Add "CATEGORY=NAME;" to the string.  */
173       const char *name = (category == LC_ALL ? newnames[i] :
174                           category == i ? newnames[0] :
175                           _nl_current_names[i]);
176       p = __stpcpy (p, _nl_category_names[i]);
177       *p++ = '=';
178       p = __stpcpy (p, name);
179       *p++ = ';';
180     }
181   p[-1] = '\0';         /* Clobber the last ';'.  */
182   return new;
183 }
184
185
186 /* Put NAME in _nl_current_names.  */
187 static inline void
188 setname (int category, const char *name)
189 {
190   if (_nl_current_names[category] == name)
191     return;
192
193   if (category == LC_ALL && _nl_current_names[category] != _nl_C_name)
194     free ((char *) _nl_current_names[category]);
195
196   _nl_current_names[category] = name;
197 }
198
199
200 /* Put DATA in *_nl_current[CATEGORY].  */
201 static inline void
202 setdata (int category, struct locale_data *data)
203 {
204   if (_nl_current[category] != NULL)
205     {
206       *_nl_current[category] = data;
207       if (_nl_category_postload[category])
208         (*_nl_category_postload[category]) ();
209     }
210 }
211
212
213 char *
214 setlocale (int category, const char *locale)
215 {
216   char *locale_path;
217   size_t locale_path_len;
218   const char *locpath_var;
219   char *composite;
220
221   /* Sanity check for CATEGORY argument.  */
222   if (category < 0 || category > LC_ALL)
223     ERROR_RETURN;
224
225   /* Does user want name of current locale?  */
226   if (locale == NULL)
227     return (char *) _nl_current_names[category];
228
229   if (strcmp (locale, _nl_current_names[category]) == 0)
230     /* Changing to the same thing.  */
231     return (char *) _nl_current_names[category];
232
233   /* We perhaps really have to load some data.  So we determine the
234      path in which to look for the data now.  The environment variable
235      `LOCPATH' must only be used when the binary has no SUID or SGID
236      bit set.  */
237   locale_path = NULL;
238   locale_path_len = 0;
239
240   locpath_var = __secure_getenv ("LOCPATH");
241   if (locpath_var != NULL && locpath_var[0] != '\0')
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, LOCALE_PATH, ':') != 0)
247     return NULL;
248
249   if (category == LC_ALL)
250     {
251       /* The user wants to set all categories.  The desired locales
252          for the individual categories can be selected by using a
253          composite locale name.  This is a semi-colon separated list
254          of entries of the form `CATEGORY=VALUE'.  */
255       const char *newnames[LC_ALL];
256       struct locale_data *newdata[LC_ALL];
257
258       /* Set all name pointers to the argument name.  */
259       for (category = 0; category < LC_ALL; ++category)
260         newnames[category] = (char *) locale;
261
262       if (strchr (locale, ';') != NULL)
263         {
264           /* This is a composite name.  Make a copy and split it up.  */
265           char *np = strdupa (locale);
266           char *cp;
267           int cnt;
268
269           while ((cp = strchr (np, '=')) != NULL)
270             {
271               for (cnt = 0; cnt < LC_ALL; ++cnt)
272                 if ((size_t) (cp - np) == _nl_category_name_sizes[cnt]
273                     && memcmp (np, _nl_category_names[cnt], cp - np) == 0)
274                   break;
275
276               if (cnt == LC_ALL)
277                 /* Bogus category name.  */
278                 ERROR_RETURN;
279
280               /* Found the category this clause sets.  */
281               newnames[cnt] = ++cp;
282               cp = strchr (cp, ';');
283               if (cp != NULL)
284                 {
285                   /* Examine the next clause.  */
286                   *cp = '\0';
287                   np = cp + 1;
288                 }
289               else
290                 /* This was the last clause.  We are done.  */
291                 break;
292             }
293
294           for (cnt = 0; cnt < LC_ALL; ++cnt)
295             if (newnames[cnt] == locale)
296               /* The composite name did not specify all categories.  */
297               ERROR_RETURN;
298         }
299
300       /* Protect global data.  */
301       __libc_lock_lock (__libc_setlocale_lock);
302
303       /* Load the new data for each category.  */
304       while (category-- > 0)
305         {
306           newdata[category] = _nl_find_locale (locale_path, locale_path_len,
307                                                category,
308                                                &newnames[category]);
309
310           if (newdata[category] == NULL)
311             break;
312
313           /* We must not simply free a global locale since we have no
314              control over the usage.  So we mark it as un-deletable.  */
315           if (newdata[category]->usage_count != UNDELETABLE)
316             newdata[category]->usage_count = UNDELETABLE;
317         }
318
319       /* Create new composite name.  */
320       if (category >= 0
321           || (composite = new_composite_name (LC_ALL, newnames)) == NULL)
322         /* Loading this part of the locale failed.  Abort the
323            composite load.  */
324         composite = NULL;
325       else
326         {
327           /* Now we have loaded all the new data.  Put it in place.  */
328           for (category = 0; category < LC_ALL; ++category)
329             {
330               setdata (category, newdata[category]);
331               setname (category, newnames[category]);
332             }
333           setname (LC_ALL, composite);
334         }
335
336       /* Critical section left.  */
337       __libc_lock_unlock (__libc_setlocale_lock);
338
339       /* Free the resources (the locale path variable.  */
340       free (locale_path);
341
342       return composite;
343     }
344   else
345     {
346       struct locale_data *newdata = NULL;
347       const char *newname[1] = { locale };
348
349       /* Protect global data.  */
350       __libc_lock_lock (__libc_setlocale_lock);
351
352       if (_nl_current[category] != NULL)
353         {
354           /* Only actually load the data if anything will use it.  */
355           newdata = _nl_find_locale (locale_path, locale_path_len, category,
356                                      &newname[0]);
357           if (newdata == NULL)
358             goto abort_single;
359
360           /* We must not simply free a global locale since we have no
361              control over the usage.  So we mark it as un-deletable.
362
363              Note: do not remove the `if', it's necessary to copy with
364              the builtin locale data.  */
365           if (newdata->usage_count != UNDELETABLE)
366             newdata->usage_count = UNDELETABLE;
367         }
368
369       /* Create new composite name.  */
370       composite = new_composite_name (category, newname);
371       if (composite == NULL)
372         {
373           /* Say that we don't have any data loaded.  */
374         abort_single:
375           newname[0] = NULL;
376         }
377       else
378         {
379           if (_nl_current[category] != NULL)
380             setdata (category, newdata);
381
382           setname (category, newname[0]);
383           setname (LC_ALL, composite);
384         }
385
386       /* Critical section left.  */
387       __libc_lock_unlock (__libc_setlocale_lock);
388
389       /* Free the resources (the locale path variable.  */
390       free (locale_path);
391
392       return (char *) newname[0];
393     }
394 }