update from main archive 961201
[platform/upstream/glibc.git] / locale / setlocale.c
1 /* Copyright (C) 1991, 1992, 1995, 1996 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
16 not, 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 <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 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
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 const 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   };
53
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[] =
57   {
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
62   };
63
64
65 /* Define an array of category names (also the environment variable names),
66    indexed by integral category.  */
67 const 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
73     [LC_ALL] = "LC_ALL"
74   };
75 /* An array of their lengths, for convenience.  */
76 const 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
82     [LC_ALL] = sizeof ("LC_ALL") - 1
83   };
84
85
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
94
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) =
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
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[] =
109   {
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.  */
115   };
116
117
118 /* Lock for protecting global data.  */
119 __libc_lock_define_initialized (, __libc_setlocale_lock)
120
121
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)
128
129
130 static inline char *
131 clever_copy (const char *string)
132 {
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 }
146
147
148 /* Construct a new composite name.  */
149 static inline char *
150 new_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)
159     {
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;
167     }
168
169   if (same)
170     {
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);
176
177       return new == NULL ? NULL : memcpy (new, newnames[0], last_len + 1);
178     }
179
180   new = malloc (cumlen);
181   if (new == NULL)
182     return NULL;
183   p = new;
184   for (i = 0; i < LC_ALL; ++i)
185     {
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++ = ';';
194     }
195   p[-1] = '\0';         /* Clobber the last ';'.  */
196   return new;
197 }
198
199
200 /* Put NAME in _nl_current_names.  */
201 static inline void
202 setname (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].  */
213 static inline void
214 setdata (int category, const struct locale_data *data)
215 {
216   if (_nl_current[category] != NULL)
217     {
218       *_nl_current[category] = data;
219       if (_nl_category_postload[category])
220         (*_nl_category_postload[category]) ();
221     }
222 }
223
224
225 char *
226 setlocale (int category, const char *locale)
227 {
228   char *locale_path;
229   size_t locale_path_len;
230   const char *locpath_var;
231   char *composite;
232
233   /* Sanity check for CATEGORY argument.  */
234   if (category < 0 || category > LC_ALL)
235     ERROR_RETURN;
236
237   /* Does user want name of current locale?  */
238   if (locale == NULL)
239     return (char *) _nl_current_names[category];
240
241   if (strcmp (locale, _nl_current_names[category]) == 0)
242     /* Changing to the same thing.  */
243     return (char *) _nl_current_names[category];
244
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;
251
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;
257
258   if (__argz_add_sep (&locale_path, &locale_path_len, LOCALE_PATH, ':') != 0)
259     return NULL;
260
261   if (category == LC_ALL)
262     {
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];
269
270       /* Set all name pointers to the argument name.  */
271       for (category = 0; category < LC_ALL; ++category)
272         newnames[category] = (char *) locale;
273
274       if (strchr (locale, ';') != NULL)
275         {
276           /* This is a composite name.  Make a copy and split it up.  */
277           char *np = strdupa (locale);
278           char *cp;
279           int cnt;
280
281           while ((cp = strchr (np, '=')) != NULL)
282             {
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;
287
288               if (cnt == LC_ALL)
289                 /* Bogus category name.  */
290                 ERROR_RETURN;
291
292               /* Found the category this clause sets.  */
293               newnames[cnt] = ++cp;
294               cp = strchr (cp, ';');
295               if (cp != NULL)
296                 {
297                   /* Examine the next clause.  */
298                   *cp = '\0';
299                   np = cp + 1;
300                 }
301               else
302                 /* This was the last clause.  We are done.  */
303                 break;
304             }
305
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;
310         }
311
312       /* Protect global data.  */
313       __libc_lock_lock (__libc_setlocale_lock);
314
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)
319           {
320             newdata[category] = _nl_find_locale (locale_path, locale_path_len,
321                                                  category,
322                                                  &newnames[category]);
323
324             if (newdata[category] == NULL)
325               break;
326           }
327         else
328           {
329             /* The data is never used; just change the name.  */
330             newnames[category] = clever_copy (newnames[category]);
331             if (newnames[category] == NULL)
332               break;
333           }
334
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
342         {
343           /* Now we have loaded all the new data.  Put it in place.  */
344           for (category = 0; category < LC_ALL; ++category)
345             {
346               setdata (category, newdata[category]);
347               setname (category, newnames[category]);
348             }
349           setname (LC_ALL, composite);
350         }
351
352       /* Critical section left.  */
353       __libc_lock_unlock (__libc_setlocale_lock);
354
355       return composite;
356     }
357   else
358     {
359       const struct locale_data *newdata = NULL;
360       char *newname = (char *) locale;
361
362       /* Protect global data.  */
363       __libc_lock_lock (__libc_setlocale_lock);
364
365       if (_nl_current[category] != NULL)
366         {
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;
372         }
373
374       /* Create new composite name.  */
375       composite = new_composite_name (category, &newname);
376       if (composite == NULL)
377         {
378           /* Say that we don't have any data loaded.  */
379         abort_single:
380           newname = NULL;
381         }
382       else
383         {
384           if (_nl_current[category] != NULL)
385             setdata (category, newdata);
386
387           setname (category, newname);
388           setname (LC_ALL, composite);
389         }
390
391       /* Critical section left.  */
392       __libc_lock_unlock (__libc_setlocale_lock);
393
394       return newname;
395     }
396 }