]>
Commit | Line | Data |
---|---|---|
c84142e8 UD |
1 | /* Copyright (C) 1991, 1992, 1995, 1996, 1997 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. | |
28f540f4 | 3 | |
c84142e8 UD |
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. | |
28f540f4 | 8 | |
c84142e8 UD |
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. | |
28f540f4 | 13 | |
c84142e8 UD |
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. */ | |
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) \ | |
c84142e8 UD |
38 | extern struct locale_data *_nl_current_##category; \ |
39 | extern struct locale_data _nl_C_##category; \ | |
0676b5fd | 40 | weak_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. */ | |
c84142e8 | 46 | static struct locale_data * *const _nl_current[] = |
7a12c6bb | 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. */ | |
c84142e8 | 56 | 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. */ | |
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 | |
7a12c6bb | 73 | [LC_ALL] = "LC_ALL" |
933e73fa RM |
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 | |
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) \ | |
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. */ | |
a5113b14 | 97 | static 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 | 108 | static 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 | |
130 | static inline char * | |
131 | clever_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. */ |
149 | static inline char * | |
c84142e8 | 150 | new_composite_name (int category, const char *newnames[LC_ALL]) |
7a12c6bb RM |
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 | { |
c84142e8 UD |
160 | const char *name = (category == LC_ALL ? newnames[i] : |
161 | category == i ? newnames[0] : | |
162 | _nl_current_names[i]); | |
7a12c6bb RM |
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 | 186 | /* Add "CATEGORY=NAME;" to the string. */ |
c84142e8 UD |
187 | const char *name = (category == LC_ALL ? newnames[i] : |
188 | category == i ? newnames[0] : | |
189 | _nl_current_names[i]); | |
7a12c6bb RM |
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. */ |
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 | |
c84142e8 | 214 | setdata (int category, struct locale_data *data) |
7a12c6bb RM |
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 |
225 | char * |
226 | setlocale (int category, const char *locale) | |
227 | { | |
7a12c6bb RM |
228 | char *locale_path; |
229 | size_t locale_path_len; | |
d68171ed | 230 | const char *locpath_var; |
7a12c6bb | 231 | char *composite; |
933e73fa | 232 | |
7a12c6bb RM |
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) | |
933e73fa | 242 | /* Changing to the same thing. */ |
7a12c6bb RM |
243 | return (char *) _nl_current_names[category]; |
244 | ||
245 | /* We perhaps really have to load some data. So we determine the | |
a5113b14 UD |
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 | |
7a12c6bb RM |
248 | bit set. */ |
249 | locale_path = NULL; | |
250 | locale_path_len = 0; | |
251 | ||
d68171ed UD |
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; | |
933e73fa | 257 | |
e4cf5070 | 258 | if (__argz_add_sep (&locale_path, &locale_path_len, LOCALE_PATH, ':') != 0) |
7a12c6bb | 259 | return NULL; |
db2286f6 | 260 | |
933e73fa RM |
261 | if (category == LC_ALL) |
262 | { | |
7a12c6bb RM |
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'. */ | |
c84142e8 UD |
267 | const char *newnames[LC_ALL]; |
268 | struct locale_data *newdata[LC_ALL]; | |
933e73fa RM |
269 | |
270 | /* Set all name pointers to the argument name. */ | |
271 | for (category = 0; category < LC_ALL; ++category) | |
7a12c6bb | 272 | newnames[category] = (char *) locale; |
db2286f6 | 273 | |
7a12c6bb | 274 | if (strchr (locale, ';') != NULL) |
933e73fa | 275 | { |
7a12c6bb RM |
276 | /* This is a composite name. Make a copy and split it up. */ |
277 | char *np = strdupa (locale); | |
278 | char *cp; | |
279 | int cnt; | |
933e73fa | 280 | |
7a12c6bb | 281 | while ((cp = strchr (np, '=')) != NULL) |
933e73fa | 282 | { |
7a12c6bb | 283 | for (cnt = 0; cnt < LC_ALL; ++cnt) |
ce7a5ef4 | 284 | if ((size_t) (cp - np) == _nl_category_name_sizes[cnt] |
7a12c6bb | 285 | && memcmp (np, _nl_category_names[cnt], cp - np) == 0) |
933e73fa | 286 | break; |
7a12c6bb RM |
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) | |
933e73fa | 296 | { |
7a12c6bb RM |
297 | /* Examine the next clause. */ |
298 | *cp = '\0'; | |
299 | np = cp + 1; | |
933e73fa | 300 | } |
7a12c6bb RM |
301 | else |
302 | /* This was the last clause. We are done. */ | |
303 | break; | |
933e73fa RM |
304 | } |
305 | ||
7a12c6bb RM |
306 | for (cnt = 0; cnt < LC_ALL; ++cnt) |
307 | if (newnames[cnt] == locale) | |
933e73fa | 308 | /* The composite name did not specify all categories. */ |
7a12c6bb | 309 | ERROR_RETURN; |
933e73fa | 310 | } |
19bc17a9 | 311 | |
a5113b14 | 312 | /* Protect global data. */ |
c4029823 | 313 | __libc_lock_lock (__libc_setlocale_lock); |
a5113b14 | 314 | |
933e73fa RM |
315 | /* Load the new data for each category. */ |
316 | while (category-- > 0) | |
1fb05e3d UD |
317 | { |
318 | newdata[category] = _nl_find_locale (locale_path, locale_path_len, | |
319 | category, | |
320 | &newnames[category]); | |
321 | ||
322 | if (newdata[category] == NULL) | |
323 | break; | |
324 | ||
325 | /* We must not simply free a global locale since we have no | |
326 | control over the usage. So we mark it as un-deletable. */ | |
327 | if (newdata[category]->usage_count != MAX_USAGE_COUNT) | |
c84142e8 | 328 | newdata[category]->usage_count = MAX_USAGE_COUNT; |
1fb05e3d | 329 | } |
933e73fa | 330 | |
7a12c6bb | 331 | /* Create new composite name. */ |
845dcb57 UD |
332 | if (category >= 0 |
333 | || (composite = new_composite_name (LC_ALL, newnames)) == NULL) | |
334 | /* Loading this part of the locale failed. Abort the | |
335 | composite load. */ | |
336 | composite = NULL; | |
a5113b14 | 337 | else |
933e73fa | 338 | { |
a5113b14 UD |
339 | /* Now we have loaded all the new data. Put it in place. */ |
340 | for (category = 0; category < LC_ALL; ++category) | |
341 | { | |
342 | setdata (category, newdata[category]); | |
343 | setname (category, newnames[category]); | |
344 | } | |
345 | setname (LC_ALL, composite); | |
933e73fa | 346 | } |
a5113b14 UD |
347 | |
348 | /* Critical section left. */ | |
c4029823 | 349 | __libc_lock_unlock (__libc_setlocale_lock); |
933e73fa RM |
350 | |
351 | return composite; | |
352 | } | |
353 | else | |
354 | { | |
c84142e8 UD |
355 | struct locale_data *newdata = NULL; |
356 | const char *newname = locale; | |
7a12c6bb | 357 | |
a5113b14 | 358 | /* Protect global data. */ |
c4029823 | 359 | __libc_lock_lock (__libc_setlocale_lock); |
a5113b14 | 360 | |
7a12c6bb | 361 | if (_nl_current[category] != NULL) |
933e73fa | 362 | { |
7a12c6bb | 363 | /* Only actually load the data if anything will use it. */ |
7a12c6bb | 364 | newdata = _nl_find_locale (locale_path, locale_path_len, category, |
c84142e8 | 365 | &newname); |
7a12c6bb | 366 | if (newdata == NULL) |
a5113b14 | 367 | goto abort_single; |
c84142e8 UD |
368 | |
369 | /* We must not simply free a global locale since we have no | |
370 | control over the usage. So we mark it as un-deletable. */ | |
371 | newdata->usage_count = MAX_USAGE_COUNT; | |
933e73fa RM |
372 | } |
373 | ||
7a12c6bb | 374 | /* Create new composite name. */ |
933e73fa | 375 | composite = new_composite_name (category, &newname); |
7a12c6bb | 376 | if (composite == NULL) |
933e73fa | 377 | { |
845dcb57 | 378 | /* Say that we don't have any data loaded. */ |
a5113b14 UD |
379 | abort_single: |
380 | newname = NULL; | |
933e73fa | 381 | } |
a5113b14 UD |
382 | else |
383 | { | |
384 | if (_nl_current[category] != NULL) | |
385 | setdata (category, newdata); | |
28f540f4 | 386 | |
a5113b14 UD |
387 | setname (category, newname); |
388 | setname (LC_ALL, composite); | |
389 | } | |
28f540f4 | 390 | |
a5113b14 | 391 | /* Critical section left. */ |
c4029823 | 392 | __libc_lock_unlock (__libc_setlocale_lock); |
28f540f4 | 393 | |
c84142e8 | 394 | return (char *) newname; |
933e73fa | 395 | } |
28f540f4 | 396 | } |