setlocale.c revision 1.47 1 /* $NetBSD: setlocale.c,v 1.47 2004/07/21 20:27:46 tshiozak Exp $ */
2
3 /*
4 * Copyright (c) 1991, 1993
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.47 2004/07/21 20:27:46 tshiozak 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
67 #ifdef CITRUS
68 #include <citrus/citrus_namespace.h>
69 #include <citrus/citrus_region.h>
70 #include <citrus/citrus_lookup.h>
71 #include <citrus/citrus_bcs.h>
72 #else
73 #include <locale/aliasname_local.h>
74 #define _lookup_alias(p, a, b, s, c) __unaliasname((p), (a), (b), (s))
75 #define _bcs_strcasecmp(a, b) strcasecmp((a), (b))
76 #endif
77
78 #define _LOCALE_ALIAS_NAME "locale.alias"
79 #define _LOCALE_SYM_FORCE "/force"
80
81 /*
82 * Category names for getenv()
83 */
84 static const char *const categories[_LC_LAST] = {
85 "LC_ALL",
86 "LC_COLLATE",
87 "LC_CTYPE",
88 "LC_MONETARY",
89 "LC_NUMERIC",
90 "LC_TIME",
91 "LC_MESSAGES"
92 };
93
94 /*
95 * Current locales for each category
96 */
97 static char current_categories[_LC_LAST][32] = {
98 "C",
99 "C",
100 "C",
101 "C",
102 "C",
103 "C",
104 "C"
105 };
106
107 /*
108 * The locales we are going to try and load
109 */
110 static char new_categories[_LC_LAST][32];
111
112 static char current_locale_string[_LC_LAST * 33];
113 char *_PathLocale;
114
115 static char *currentlocale __P((void));
116 static void revert_to_default __P((int));
117 static int force_locale_enable __P((int));
118 static int load_locale_sub __P((int, const char *, int));
119 static char *loadlocale __P((int));
120 static const char *__get_locale_env __P((int));
121
122 char *
123 __setlocale(category, locale)
124 int category;
125 const char *locale;
126 {
127 int i, loadlocale_success;
128 size_t len;
129 const char *env, *r;
130
131 if (issetugid() ||
132 (!_PathLocale && !(_PathLocale = getenv("PATH_LOCALE"))))
133 _PathLocale = _PATH_LOCALE;
134
135 if (category < 0 || category >= _LC_LAST)
136 return (NULL);
137
138 if (!locale)
139 return (category ?
140 current_categories[category] : currentlocale());
141
142 /*
143 * Default to the current locale for everything.
144 */
145 for (i = 1; i < _LC_LAST; ++i)
146 (void)strlcpy(new_categories[i], current_categories[i],
147 sizeof(new_categories[i]));
148
149 /*
150 * Now go fill up new_categories from the locale argument
151 */
152 if (!*locale) {
153 if (category == LC_ALL) {
154 for (i = 1; i < _LC_LAST; ++i) {
155 env = __get_locale_env(i);
156 (void)strlcpy(new_categories[i], env,
157 sizeof(new_categories[i]));
158 }
159 }
160 else {
161 env = __get_locale_env(category);
162 (void)strlcpy(new_categories[category], env,
163 sizeof(new_categories[category]));
164 }
165 } else if (category) {
166 (void)strlcpy(new_categories[category], locale,
167 sizeof(new_categories[category]));
168 } else {
169 if ((r = strchr(locale, '/')) == 0) {
170 for (i = 1; i < _LC_LAST; ++i) {
171 (void)strlcpy(new_categories[i], locale,
172 sizeof(new_categories[i]));
173 }
174 } else {
175 for (i = 1;;) {
176 _DIAGASSERT(*r == '/' || *r == 0);
177 _DIAGASSERT(*locale != 0);
178 if (*locale == '/')
179 return (NULL); /* invalid format. */
180 len = r - locale;
181 if (len + 1 > sizeof(new_categories[i]))
182 return (NULL); /* too long */
183 (void)memcpy(new_categories[i], locale, len);
184 new_categories[i][len] = '\0';
185 if (*r == 0)
186 break;
187 _DIAGASSERT(*r == '/');
188 if (*(locale = ++r) == 0)
189 /* slash followed by NUL */
190 return (NULL);
191 /* skip until NUL or '/' */
192 while (*r && *r != '/')
193 r++;
194 if (++i == _LC_LAST)
195 return (NULL); /* too many slashes. */
196 }
197 if (i + 1 != _LC_LAST)
198 return (NULL); /* too few slashes. */
199 }
200 }
201
202 if (category)
203 return (loadlocale(category));
204
205 loadlocale_success = 0;
206 for (i = 1; i < _LC_LAST; ++i) {
207 if (loadlocale(i) != NULL)
208 loadlocale_success = 1;
209 }
210
211 /*
212 * If all categories failed, return NULL; we don't need to back
213 * changes off, since none happened.
214 */
215 if (!loadlocale_success)
216 return NULL;
217
218 return (currentlocale());
219 }
220
221 static char *
222 currentlocale()
223 {
224 int i;
225
226 (void)strlcpy(current_locale_string, current_categories[1],
227 sizeof(current_locale_string));
228
229 for (i = 2; i < _LC_LAST; ++i)
230 if (strcmp(current_categories[1], current_categories[i])) {
231 (void)snprintf(current_locale_string,
232 sizeof(current_locale_string), "%s/%s/%s/%s/%s/%s",
233 current_categories[1], current_categories[2],
234 current_categories[3], current_categories[4],
235 current_categories[5], current_categories[6]);
236 break;
237 }
238 return (current_locale_string);
239 }
240
241 static void
242 revert_to_default(category)
243 int category;
244 {
245 switch (category) {
246 case LC_CTYPE:
247 #ifdef WITH_RUNE
248 (void)_xpg4_setrunelocale("C");
249 (void)__runetable_to_netbsd_ctype("C");
250 #else
251 if (_ctype_ != _C_ctype_) {
252 /* LINTED const castaway */
253 free((void *)_ctype_);
254 _ctype_ = _C_ctype_;
255 }
256 if (_toupper_tab_ != _C_toupper_) {
257 /* LINTED const castaway */
258 free((void *)_toupper_tab_);
259 _toupper_tab_ = _C_toupper_;
260 }
261 if (_tolower_tab_ != _C_tolower_) {
262 /* LINTED const castaway */
263 free((void *)_tolower_tab_);
264 _tolower_tab_ = _C_tolower_;
265 }
266 #endif
267 break;
268 case LC_MESSAGES:
269 case LC_COLLATE:
270 case LC_MONETARY:
271 case LC_NUMERIC:
272 case LC_TIME:
273 break;
274 }
275 }
276
277 static int
278 force_locale_enable(category)
279 int category;
280 {
281 revert_to_default(category);
282
283 return 0;
284 }
285
286 static int
287 load_locale_sub(category, locname, isspecial)
288 int category;
289 const char *locname;
290 int isspecial;
291 {
292 char name[PATH_MAX];
293
294 /* check for the default locales */
295 if (!strcmp(new_categories[category], "C") ||
296 !strcmp(new_categories[category], "POSIX")) {
297 revert_to_default(category);
298 return 0;
299 }
300
301 /* check whether special symbol */
302 if (isspecial && _bcs_strcasecmp(locname, _LOCALE_SYM_FORCE) == 0)
303 return force_locale_enable(category);
304
305 /* sanity check */
306 if (strchr(locname, '/') != NULL)
307 return -1;
308
309 (void)snprintf(name, sizeof(name), "%s/%s/%s",
310 _PathLocale, locname, categories[category]);
311
312 switch (category) {
313 case LC_CTYPE:
314 #ifdef WITH_RUNE
315 if (_xpg4_setrunelocale(__UNCONST(locname)))
316 return -1;
317 if (__runetable_to_netbsd_ctype(locname)) {
318 /* very unfortunate, but need to go to "C" locale */
319 revert_to_default(category);
320 return -1;
321 }
322 #else
323 if (!__loadctype(name))
324 return -1;
325 #endif
326 break;
327
328 case LC_MESSAGES:
329 /*
330 * XXX we don't have LC_MESSAGES support yet,
331 * but catopen may use the value of LC_MESSAGES category.
332 * so return successfully if locale directory is present.
333 */
334 (void)snprintf(name, sizeof(name), "%s/%s",
335 _PathLocale, locname);
336 /* local */
337 {
338 struct stat st;
339 if (stat(name, &st) < 0)
340 return -1;
341 if (!S_ISDIR(st.st_mode))
342 return -1;
343 }
344 break;
345
346 case LC_COLLATE:
347 case LC_MONETARY:
348 case LC_NUMERIC:
349 case LC_TIME:
350 return -1;
351 }
352
353 return 0;
354 }
355
356 static char *
357 loadlocale(category)
358 int category;
359 {
360 char aliaspath[PATH_MAX], loccat[PATH_MAX], buf[PATH_MAX];
361 const char *alias;
362
363 _DIAGASSERT(0 < category && category < _LC_LAST);
364
365 if (strcmp(new_categories[category], current_categories[category]) == 0)
366 return (current_categories[category]);
367
368 /* (1) non-aliased file */
369 if (!load_locale_sub(category, new_categories[category], 0))
370 goto success;
371
372 /* (2) lookup locname/catname type alias */
373 (void)snprintf(aliaspath, sizeof(aliaspath),
374 "%s/" _LOCALE_ALIAS_NAME, _PathLocale);
375 (void)snprintf(loccat, sizeof(loccat), "%s/%s",
376 new_categories[category], categories[category]);
377 alias = _lookup_alias(aliaspath, loccat, buf, sizeof(buf),
378 _LOOKUP_CASE_SENSITIVE);
379 if (!load_locale_sub(category, alias, 1))
380 goto success;
381
382 /* (3) lookup locname type alias */
383 alias = _lookup_alias(aliaspath, new_categories[category],
384 buf, sizeof(buf), _LOOKUP_CASE_SENSITIVE);
385 if (!load_locale_sub(category, alias, 1))
386 goto success;
387
388 return NULL;
389
390 success:
391 (void)strlcpy(current_categories[category],
392 new_categories[category],
393 sizeof(current_categories[category]));
394 return current_categories[category];
395 }
396
397 static const char *
398 __get_locale_env(category)
399 int category;
400 {
401 const char *env;
402
403 _DIAGASSERT(category != LC_ALL);
404
405 /* 1. check LC_ALL. */
406 env = getenv(categories[0]);
407
408 /* 2. check LC_* */
409 if (!env || !*env)
410 env = getenv(categories[category]);
411
412 /* 3. check LANG */
413 if (!env || !*env)
414 env = getenv("LANG");
415
416 /* 4. if none is set, fall to "C" */
417 if (!env || !*env || strchr(env, '/'))
418 env = "C";
419
420 return env;
421 }
422