setlocale.c revision 1.53 1 /* $NetBSD: setlocale.c,v 1.53 2008/05/17 03:49:54 ginsbach Exp $ */
2
3 /*
4 * Copyright (c) 1991, 1993, 2008
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Paul Borman at Krystal Technologies.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
37 #if 0
38 static char sccsid[] = "@(#)setlocale.c 8.1 (Berkeley) 7/4/93";
39 #else
40 __RCSID("$NetBSD: setlocale.c,v 1.53 2008/05/17 03:49:54 ginsbach Exp $");
41 #endif
42 #endif /* LIBC_SCCS and not lint */
43
44 #define _CTYPE_PRIVATE
45
46 #include "namespace.h"
47 #include <sys/localedef.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <assert.h>
51 #include <limits.h>
52 #include <ctype.h>
53 #define __SETLOCALE_SOURCE__
54 #include <locale.h>
55 #include <paths.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60 #ifdef WITH_RUNE
61 #include "rune.h"
62 #include "rune_local.h"
63 #else
64 #include "ctypeio.h"
65 #endif
66 #include "lcmessages.h"
67 #include "lcmonetary.h"
68 #include "lcnumeric.h"
69 #include "lctime.h"
70
71 #ifdef CITRUS
72 #include <citrus/citrus_namespace.h>
73 #include <citrus/citrus_region.h>
74 #include <citrus/citrus_lookup.h>
75 #include <citrus/citrus_bcs.h>
76 #else
77 #include <locale/aliasname_local.h>
78 #define _lookup_alias(p, a, b, s, c) __unaliasname((p), (a), (b), (s))
79 #define _bcs_strcasecmp(a, b) strcasecmp((a), (b))
80 #endif
81
82 #define _LOCALE_SYM_FORCE "/force"
83
84 /*
85 * Category names for getenv()
86 */
87 static const char *const categories[_LC_LAST] = {
88 "LC_ALL",
89 "LC_COLLATE",
90 "LC_CTYPE",
91 "LC_MONETARY",
92 "LC_NUMERIC",
93 "LC_TIME",
94 "LC_MESSAGES"
95 };
96
97 /*
98 * Current locales for each category
99 */
100 static char current_categories[_LC_LAST][32] = {
101 "C",
102 "C",
103 "C",
104 "C",
105 "C",
106 "C",
107 "C"
108 };
109
110 /*
111 * The locales we are going to try and load
112 */
113 static char new_categories[_LC_LAST][32];
114
115 static char current_locale_string[_LC_LAST * 33];
116
117 static char *currentlocale __P((void));
118 static void revert_to_default __P((int));
119 static int force_locale_enable __P((int));
120 static int load_locale_sub __P((int, const char *, int));
121 static char *loadlocale __P((int));
122 static const char *__get_locale_env __P((int));
123
124 char *
125 __setlocale(category, locale)
126 int category;
127 const char *locale;
128 {
129 int i, loadlocale_success;
130 size_t len;
131 const char *env, *r;
132
133 if (issetugid() ||
134 (!_PathLocale && !(_PathLocale = getenv("PATH_LOCALE"))))
135 _PathLocale = _PATH_LOCALE;
136
137 if (category < 0 || category >= _LC_LAST)
138 return (NULL);
139
140 if (!locale)
141 return (category ?
142 current_categories[category] : currentlocale());
143
144 /*
145 * Default to the current locale for everything.
146 */
147 for (i = 1; i < _LC_LAST; ++i)
148 (void)strlcpy(new_categories[i], current_categories[i],
149 sizeof(new_categories[i]));
150
151 /*
152 * Now go fill up new_categories from the locale argument
153 */
154 if (!*locale) {
155 if (category == LC_ALL) {
156 for (i = 1; i < _LC_LAST; ++i) {
157 env = __get_locale_env(i);
158 (void)strlcpy(new_categories[i], env,
159 sizeof(new_categories[i]));
160 }
161 }
162 else {
163 env = __get_locale_env(category);
164 (void)strlcpy(new_categories[category], env,
165 sizeof(new_categories[category]));
166 }
167 } else if (category) {
168 (void)strlcpy(new_categories[category], locale,
169 sizeof(new_categories[category]));
170 } else {
171 if ((r = strchr(locale, '/')) == 0) {
172 for (i = 1; i < _LC_LAST; ++i) {
173 (void)strlcpy(new_categories[i], locale,
174 sizeof(new_categories[i]));
175 }
176 } else {
177 for (i = 1;;) {
178 _DIAGASSERT(*r == '/' || *r == 0);
179 _DIAGASSERT(*locale != 0);
180 if (*locale == '/')
181 return (NULL); /* invalid format. */
182 len = r - locale;
183 if (len + 1 > sizeof(new_categories[i]))
184 return (NULL); /* too long */
185 (void)memcpy(new_categories[i], locale, len);
186 new_categories[i][len] = '\0';
187 if (*r == 0)
188 break;
189 _DIAGASSERT(*r == '/');
190 if (*(locale = ++r) == 0)
191 /* slash followed by NUL */
192 return (NULL);
193 /* skip until NUL or '/' */
194 while (*r && *r != '/')
195 r++;
196 if (++i == _LC_LAST)
197 return (NULL); /* too many slashes. */
198 }
199 if (i + 1 != _LC_LAST)
200 return (NULL); /* too few slashes. */
201 }
202 }
203
204 if (category)
205 return (loadlocale(category));
206
207 loadlocale_success = 0;
208 for (i = 1; i < _LC_LAST; ++i) {
209 if (loadlocale(i) != NULL)
210 loadlocale_success = 1;
211 }
212
213 /*
214 * If all categories failed, return NULL; we don't need to back
215 * changes off, since none happened.
216 */
217 if (!loadlocale_success)
218 return NULL;
219
220 return (currentlocale());
221 }
222
223 static char *
224 currentlocale()
225 {
226 int i;
227
228 (void)strlcpy(current_locale_string, current_categories[1],
229 sizeof(current_locale_string));
230
231 for (i = 2; i < _LC_LAST; ++i)
232 if (strcmp(current_categories[1], current_categories[i])) {
233 (void)snprintf(current_locale_string,
234 sizeof(current_locale_string), "%s/%s/%s/%s/%s/%s",
235 current_categories[1], current_categories[2],
236 current_categories[3], current_categories[4],
237 current_categories[5], current_categories[6]);
238 break;
239 }
240 return (current_locale_string);
241 }
242
243 static void
244 revert_to_default(category)
245 int category;
246 {
247 switch (category) {
248 case LC_CTYPE:
249 #ifdef WITH_RUNE
250 (void)_xpg4_setrunelocale("C");
251 #else
252 if (_ctype_ != _C_ctype_) {
253 /* LINTED const castaway */
254 free((void *)_ctype_);
255 _ctype_ = _C_ctype_;
256 }
257 if (_toupper_tab_ != _C_toupper_) {
258 /* LINTED const castaway */
259 free((void *)_toupper_tab_);
260 _toupper_tab_ = _C_toupper_;
261 }
262 if (_tolower_tab_ != _C_tolower_) {
263 /* LINTED const castaway */
264 free((void *)_tolower_tab_);
265 _tolower_tab_ = _C_tolower_;
266 }
267 #endif
268 break;
269 case LC_TIME:
270 if (_CurrentTimeLocale != &_DefaultTimeLocale) {
271 free(__UNCONST(_CurrentTimeLocale));
272 _CurrentTimeLocale = &_DefaultTimeLocale;
273 }
274 break;
275 case LC_MESSAGES:
276 if (_CurrentMessagesLocale != &_DefaultMessagesLocale) {
277 free(__UNCONST(_CurrentMessagesLocale));
278 _CurrentMessagesLocale = &_DefaultMessagesLocale;
279 }
280 break;
281 case LC_COLLATE:
282 break;
283 case LC_MONETARY:
284 if (_CurrentMonetaryLocale != &_DefaultMonetaryLocale) {
285 free(__UNCONST(_CurrentMonetaryLocale));
286 _CurrentMonetaryLocale = &_DefaultMonetaryLocale;
287 }
288 break;
289 case LC_NUMERIC:
290 if (_CurrentNumericLocale != &_DefaultNumericLocale) {
291 free(__UNCONST(_CurrentNumericLocale));
292 _CurrentNumericLocale = &_DefaultNumericLocale;
293 }
294 break;
295 }
296 }
297
298 static int
299 force_locale_enable(category)
300 int category;
301 {
302 revert_to_default(category);
303
304 return 0;
305 }
306
307 static int
308 load_locale_sub(category, locname, isspecial)
309 int category;
310 const char *locname;
311 int isspecial;
312 {
313 char name[PATH_MAX];
314
315 /* check for the default locales */
316 if (!strcmp(new_categories[category], "C") ||
317 !strcmp(new_categories[category], "POSIX")) {
318 revert_to_default(category);
319 return 0;
320 }
321
322 /* check whether special symbol */
323 if (isspecial && _bcs_strcasecmp(locname, _LOCALE_SYM_FORCE) == 0)
324 return force_locale_enable(category);
325
326 /* sanity check */
327 if (strchr(locname, '/') != NULL)
328 return -1;
329
330 (void)snprintf(name, sizeof(name), "%s/%s/%s",
331 _PathLocale, locname, categories[category]);
332
333 switch (category) {
334 case LC_CTYPE:
335 #ifdef WITH_RUNE
336 if (_xpg4_setrunelocale(__UNCONST(locname)))
337 return -1;
338 #else
339 if (!__loadctype(name))
340 return -1;
341 #endif
342 break;
343
344 case LC_MESSAGES:
345 #ifdef OLD_NO_LC_MESSAGES
346 /*
347 * XXX we don't have LC_MESSAGES support yet,
348 * but catopen may use the value of LC_MESSAGES category.
349 * so return successfully if locale directory is present.
350 */
351 (void)snprintf(name, sizeof(name), "%s/%s",
352 _PathLocale, locname);
353 /* local */
354 {
355 struct stat st;
356 if (stat(name, &st) < 0)
357 return -1;
358 if (!S_ISDIR(st.st_mode))
359 return -1;
360 }
361 #else
362 if (!__loadmessages(name))
363 return -1;
364 #endif
365 break;
366
367 case LC_TIME:
368 if (!__loadtime(name))
369 return -1;
370 break;
371 case LC_COLLATE:
372 return -1;
373 case LC_MONETARY:
374 if (!__loadmonetary(name))
375 return -1;
376 break;
377 case LC_NUMERIC:
378 if (!__loadnumeric(name))
379 return -1;
380 break;
381 }
382
383 return 0;
384 }
385
386 static char *
387 loadlocale(category)
388 int category;
389 {
390 char aliaspath[PATH_MAX], loccat[PATH_MAX], buf[PATH_MAX];
391 const char *alias;
392
393 _DIAGASSERT(0 < category && category < _LC_LAST);
394
395 if (strcmp(new_categories[category], current_categories[category]) == 0)
396 return (current_categories[category]);
397
398 /* (1) non-aliased file */
399 if (!load_locale_sub(category, new_categories[category], 0))
400 goto success;
401
402 /* (2) lookup locname/catname type alias */
403 (void)snprintf(aliaspath, sizeof(aliaspath),
404 "%s/" _LOCALE_ALIAS_NAME, _PathLocale);
405 (void)snprintf(loccat, sizeof(loccat), "%s/%s",
406 new_categories[category], categories[category]);
407 alias = _lookup_alias(aliaspath, loccat, buf, sizeof(buf),
408 _LOOKUP_CASE_SENSITIVE);
409 if (!load_locale_sub(category, alias, 1))
410 goto success;
411
412 /* (3) lookup locname type alias */
413 alias = _lookup_alias(aliaspath, new_categories[category],
414 buf, sizeof(buf), _LOOKUP_CASE_SENSITIVE);
415 if (!load_locale_sub(category, alias, 1))
416 goto success;
417
418 return NULL;
419
420 success:
421 (void)strlcpy(current_categories[category],
422 new_categories[category],
423 sizeof(current_categories[category]));
424 return current_categories[category];
425 }
426
427 static const char *
428 __get_locale_env(category)
429 int category;
430 {
431 const char *env;
432
433 _DIAGASSERT(category != LC_ALL);
434
435 /* 1. check LC_ALL. */
436 env = getenv(categories[0]);
437
438 /* 2. check LC_* */
439 if (!env || !*env)
440 env = getenv(categories[category]);
441
442 /* 3. check LANG */
443 if (!env || !*env)
444 env = getenv("LANG");
445
446 /* 4. if none is set, fall to "C" */
447 if (!env || !*env || strchr(env, '/'))
448 env = "C";
449
450 return env;
451 }
452