Remove handling of static-only-routines.
[glibc.git] / locale / setlocale.c
blob1f09eaf4eada5ac9dca8cc9a83720abb086c5a0f
1 /* Copyright (C) 1991, 1992, 1995, 1996, 1997 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 not,
16 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
52 /* We need this additional element to simplify the code. It must
53 simply be != NULL. */
54 [LC_ALL] = (const struct locale_data **) ~0ul
57 /* Array indexed by category of pointers to _nl_C_CATEGORY slots.
58 Elements are zero for categories whose data is never used. */
59 const struct locale_data *const _nl_C[] =
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
68 /* Define an array of category names (also the environment variable names),
69 indexed by integral category. */
70 const char *const _nl_category_names[] =
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"
78 /* An array of their lengths, for convenience. */
79 const size_t _nl_category_name_sizes[] =
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
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
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) =
102 #define DEFINE_CATEGORY(category, category_name, items, postload, b, c, d) \
103 [category] = postload,
104 #include "categories.def"
105 #undef DEFINE_CATEGORY
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[] =
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. */
121 /* Lock for protecting global data. */
122 __libc_lock_define_initialized (, __libc_setlocale_lock)
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)
133 static inline char *
134 clever_copy (const char *string)
136 size_t len;
137 char *new;
139 if (strcmp (string, "C") == 0 || strcmp (string, "POSIX") == 0)
140 /* This return is dangerous because the returned string might be
141 placed in read-only memory. But everything should be set up to
142 handle this case. */
143 return (char *) _nl_C_name;
145 len = strlen (string) + 1;
146 new = (char *) malloc (len);
147 return new != NULL ? memcpy (new, string, len) : NULL;
151 /* Construct a new composite name. */
152 static inline char *
153 new_composite_name (int category, char *newnames[LC_ALL])
155 size_t last_len;
156 size_t cumlen = 0;
157 int i;
158 char *new, *p;
159 int same = 1;
161 for (i = 0; i < LC_ALL; ++i)
163 char *name = (category == LC_ALL ? newnames[i] :
164 category == i ? newnames[0] :
165 (char *) _nl_current_names[i]);
166 last_len = strlen (name);
167 cumlen += _nl_category_name_sizes[i] + 1 + last_len + 1;
168 if (i > 0 && same && strcmp (name, newnames[0]) != 0)
169 same = 0;
172 if (same)
174 /* All the categories use the same name. */
175 if (strcmp (newnames[0], "C") == 0 || strcmp (newnames[0], "POSIX") == 0)
176 return (char *) _nl_C_name;
178 new = malloc (last_len + 1);
180 return new == NULL ? NULL : memcpy (new, newnames[0], last_len + 1);
183 new = malloc (cumlen);
184 if (new == NULL)
185 return NULL;
186 p = new;
187 for (i = 0; i < LC_ALL; ++i)
189 /* Add "CATEGORY=NAME;" to the string. */
190 char *name = (category == LC_ALL ? newnames[i] :
191 category == i ? newnames[0] :
192 (char *) _nl_current_names[i]);
193 p = __stpcpy (p, _nl_category_names[i]);
194 *p++ = '=';
195 p = __stpcpy (p, name);
196 *p++ = ';';
198 p[-1] = '\0'; /* Clobber the last ';'. */
199 return new;
203 /* Put NAME in _nl_current_names. */
204 static inline void
205 setname (int category, const char *name)
207 if (_nl_current_names[category] == name)
208 return;
210 if (category == LC_ALL && _nl_current_names[category] != _nl_C_name)
211 free ((void *) _nl_current_names[category]);
213 _nl_current_names[category] = name;
217 /* Put DATA in *_nl_current[CATEGORY]. */
218 static inline void
219 setdata (int category, const struct locale_data *data)
221 if (_nl_current[category] != NULL)
223 *_nl_current[category] = data;
224 if (_nl_category_postload[category])
225 (*_nl_category_postload[category]) ();
230 char *
231 setlocale (int category, const char *locale)
233 char *locale_path;
234 size_t locale_path_len;
235 const char *locpath_var;
236 char *composite;
238 /* Sanity check for CATEGORY argument. */
239 if (category < 0 || category > LC_ALL)
240 ERROR_RETURN;
242 /* Does user want name of current locale? */
243 if (locale == NULL)
244 return (char *) _nl_current_names[category];
246 if (strcmp (locale, _nl_current_names[category]) == 0)
247 /* Changing to the same thing. */
248 return (char *) _nl_current_names[category];
250 /* We perhaps really have to load some data. So we determine the
251 path in which to look for the data now. The environment variable
252 `LOCPATH' must only be used when the binary has no SUID or SGID
253 bit set. */
254 locale_path = NULL;
255 locale_path_len = 0;
257 locpath_var = __secure_getenv ("LOCPATH");
258 if (locpath_var != NULL && locpath_var[0] != '\0')
259 if (__argz_create_sep (locpath_var, ':',
260 &locale_path, &locale_path_len) != 0)
261 return NULL;
263 if (__argz_add_sep (&locale_path, &locale_path_len, LOCALE_PATH, ':') != 0)
264 return NULL;
266 if (category == LC_ALL)
268 /* The user wants to set all categories. The desired locales
269 for the individual categories can be selected by using a
270 composite locale name. This is a semi-colon separated list
271 of entries of the form `CATEGORY=VALUE'. */
272 char *newnames[LC_ALL];
273 const struct locale_data *newdata[LC_ALL];
275 /* Set all name pointers to the argument name. */
276 for (category = 0; category < LC_ALL; ++category)
277 newnames[category] = (char *) locale;
279 if (strchr (locale, ';') != NULL)
281 /* This is a composite name. Make a copy and split it up. */
282 char *np = strdupa (locale);
283 char *cp;
284 int cnt;
286 while ((cp = strchr (np, '=')) != NULL)
288 for (cnt = 0; cnt < LC_ALL; ++cnt)
289 if ((size_t) (cp - np) == _nl_category_name_sizes[cnt]
290 && memcmp (np, _nl_category_names[cnt], cp - np) == 0)
291 break;
293 if (cnt == LC_ALL)
294 /* Bogus category name. */
295 ERROR_RETURN;
297 /* Found the category this clause sets. */
298 newnames[cnt] = ++cp;
299 cp = strchr (cp, ';');
300 if (cp != NULL)
302 /* Examine the next clause. */
303 *cp = '\0';
304 np = cp + 1;
306 else
307 /* This was the last clause. We are done. */
308 break;
311 for (cnt = 0; cnt < LC_ALL; ++cnt)
312 if (newnames[cnt] == locale)
313 /* The composite name did not specify all categories. */
314 ERROR_RETURN;
317 /* Protect global data. */
318 __libc_lock_lock (__libc_setlocale_lock);
320 /* Load the new data for each category. */
321 while (category-- > 0)
322 /* Only actually load the data if anything will use it. */
323 if (_nl_current[category] != NULL)
325 newdata[category] = _nl_find_locale (locale_path, locale_path_len,
326 category,
327 &newnames[category]);
329 if (newdata[category] == NULL)
330 break;
332 else
334 /* The data is never used; just change the name. */
335 newnames[category] = clever_copy (newnames[category]);
336 if (newnames[category] == NULL)
337 break;
340 /* Create new composite name. */
341 if (category >= 0
342 || (composite = new_composite_name (LC_ALL, newnames)) == NULL)
343 /* Loading this part of the locale failed. Abort the
344 composite load. */
345 composite = NULL;
346 else
348 /* Now we have loaded all the new data. Put it in place. */
349 for (category = 0; category < LC_ALL; ++category)
351 setdata (category, newdata[category]);
352 setname (category, newnames[category]);
354 setname (LC_ALL, composite);
357 /* Critical section left. */
358 __libc_lock_unlock (__libc_setlocale_lock);
360 /* Free the resources (the locale path variable. */
361 free (locale_path);
363 return composite;
365 else
367 const struct locale_data *newdata = NULL;
368 char *newname = (char *) locale;
370 /* Protect global data. */
371 __libc_lock_lock (__libc_setlocale_lock);
373 if (_nl_current[category] != NULL)
375 /* Only actually load the data if anything will use it. */
376 newdata = _nl_find_locale (locale_path, locale_path_len, category,
377 (char **) &newname);
378 if (newdata == NULL)
379 goto abort_single;
382 /* Create new composite name. */
383 composite = new_composite_name (category, &newname);
384 if (composite == NULL)
386 /* Say that we don't have any data loaded. */
387 abort_single:
388 newname = NULL;
390 else
392 if (_nl_current[category] != NULL)
393 setdata (category, newdata);
395 setname (category, newname);
396 setname (LC_ALL, composite);
399 /* Critical section left. */
400 __libc_lock_unlock (__libc_setlocale_lock);
402 /* Free the resources (the locale path variable. */
403 free (locale_path);
405 return newname;