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