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