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