Update.
[platform/upstream/glibc.git] / locale / setlocale.c
1 /* Copyright (C) 1991, 92, 95-99, 2000 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) \
38 extern struct locale_data *_nl_current_##category;                            \
39 extern struct locale_data _nl_C_##category;
40 #include "categories.def"
41 #undef  DEFINE_CATEGORY
42
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[] =
46   {
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
54   };
55
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[] =
59   {
60 #define DEFINE_CATEGORY(category, category_name, items, a) \
61     [category] = &_nl_C_##category,
62 #include "categories.def"
63 #undef  DEFINE_CATEGORY
64   };
65
66
67 /* Define an array of category names (also the environment variable names),
68    indexed by integral category.  */
69 const char *const _nl_category_names[] =
70   {
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"
76   };
77 /* An array of their lengths, for convenience.  */
78 const size_t _nl_category_name_sizes[] =
79   {
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
85   };
86
87
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
96
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) =
100   {
101 #define DEFINE_CATEGORY(category, category_name, items, postload) \
102     [category] = postload,
103 #include "categories.def"
104 #undef  DEFINE_CATEGORY
105   };
106
107
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[] =
111   {
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.  */
117   };
118
119
120 /* Lock for protecting global data.  */
121 __libc_lock_define_initialized (, __libc_setlocale_lock)
122
123
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)
130
131
132 /* Construct a new composite name.  */
133 static inline char *
134 new_composite_name (int category, const char *newnames[__LC_LAST])
135 {
136   size_t last_len = 0;
137   size_t cumlen = 0;
138   int i;
139   char *new, *p;
140   int same = 1;
141
142   for (i = 0; i < __LC_LAST; ++i)
143     if (i != LC_ALL)
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_LAST; ++i)
171     if (i != LC_ALL)
172       {
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++ = ';';
181       }
182   p[-1] = '\0';         /* Clobber the last ';'.  */
183   return new;
184 }
185
186
187 /* Put NAME in _nl_current_names.  */
188 static inline void
189 setname (int category, const char *name)
190 {
191   if (_nl_current_names[category] == name)
192     return;
193
194   if (category == LC_ALL && _nl_current_names[category] != _nl_C_name)
195     free ((char *) _nl_current_names[category]);
196
197   _nl_current_names[category] = name;
198 }
199
200
201 /* Put DATA in *_nl_current[CATEGORY].  */
202 static inline void
203 setdata (int category, struct locale_data *data)
204 {
205   if (_nl_current[category] != NULL)
206     {
207       *_nl_current[category] = data;
208       if (_nl_category_postload[category])
209         (*_nl_category_postload[category]) ();
210     }
211 }
212
213
214 char *
215 setlocale (int category, const char *locale)
216 {
217   char *locale_path;
218   size_t locale_path_len;
219   const char *locpath_var;
220   char *composite;
221
222   /* Sanity check for CATEGORY argument.  */
223   if (__builtin_expect (category, 0) < 0
224       || __builtin_expect (category, 0) >= __LC_LAST)
225     ERROR_RETURN;
226
227   /* Does user want name of current locale?  */
228   if (locale == NULL)
229     return (char *) _nl_current_names[category];
230
231   if (strcmp (locale, _nl_current_names[category]) == 0)
232     /* Changing to the same thing.  */
233     return (char *) _nl_current_names[category];
234
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;
241
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;
247
248   if (__argz_add_sep (&locale_path, &locale_path_len, LOCALE_PATH, ':') != 0)
249     return NULL;
250
251   if (category == LC_ALL)
252     {
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];
259
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;
264
265       if (__builtin_expect (strchr (locale, ';') != NULL, 0))
266         {
267           /* This is a composite name.  Make a copy and split it up.  */
268           char *np = strdupa (locale);
269           char *cp;
270           int cnt;
271
272           while ((cp = strchr (np, '=')) != NULL)
273             {
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;
279
280               if (cnt == __LC_LAST)
281                 /* Bogus category name.  */
282                 ERROR_RETURN;
283
284               /* Found the category this clause sets.  */
285               newnames[cnt] = ++cp;
286               cp = strchr (cp, ';');
287               if (cp != NULL)
288                 {
289                   /* Examine the next clause.  */
290                   *cp = '\0';
291                   np = cp + 1;
292                 }
293               else
294                 /* This was the last clause.  We are done.  */
295                 break;
296             }
297
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;
302         }
303
304       /* Protect global data.  */
305       __libc_lock_lock (__libc_setlocale_lock);
306
307       /* Load the new data for each category.  */
308       while (category-- > 0)
309         if (category != LC_ALL)
310           {
311             newdata[category] = _nl_find_locale (locale_path, locale_path_len,
312                                                  category,
313                                                  &newnames[category]);
314
315             if (newdata[category] == NULL)
316               break;
317
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;
322           }
323
324       /* Create new composite name.  */
325       composite = (category >= 0
326                    ? NULL : new_composite_name (LC_ALL, newnames));
327       if (composite != NULL)
328         {
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)
332               {
333                 setdata (category, newdata[category]);
334                 setname (category, newnames[category]);
335               }
336           setname (LC_ALL, composite);
337         }
338
339       /* Critical section left.  */
340       __libc_lock_unlock (__libc_setlocale_lock);
341
342       /* Free the resources (the locale path variable.  */
343       free (locale_path);
344
345       return composite;
346     }
347   else
348     {
349       struct locale_data *newdata = NULL;
350       const char *newname[1] = { locale };
351
352       /* Protect global data.  */
353       __libc_lock_lock (__libc_setlocale_lock);
354
355       if (_nl_current[category] != NULL)
356         {
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;
362
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.
365
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;
370         }
371
372       /* Create new composite name.  */
373       composite = new_composite_name (category, newname);
374       if (composite == NULL)
375         {
376           /* Say that we don't have any data loaded.  */
377         abort_single:
378           newname[0] = NULL;
379         }
380       else
381         {
382           if (_nl_current[category] != NULL)
383             setdata (category, newdata);
384
385           setname (category, newname[0]);
386           setname (LC_ALL, composite);
387         }
388
389       /* Critical section left.  */
390       __libc_lock_unlock (__libc_setlocale_lock);
391
392       /* Free the resources (the locale path variable.  */
393       free (locale_path);
394
395       return (char *) newname[0];
396     }
397 }
398
399
400 static void __attribute__ ((unused))
401 free_mem (void)
402 {
403   int category;
404
405   for (category = 0; category < __LC_LAST; ++category)
406     if (category != LC_ALL)
407       {
408         struct locale_data *here = *_nl_current[category];
409
410         /* If this category is already "C" don't do anything.  */
411         if (here == _nl_C[category])
412           continue;
413
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);
418
419         _nl_unload_locale (here);
420       }
421
422   setname (LC_ALL, _nl_C_name);
423 }
424 text_set_element (__libc_subfreeres, free_mem);