setlocale.c revision 1.40 1 /* $NetBSD: setlocale.c,v 1.40 2002/08/02 12:27:32 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)setlocale.c 8.1 (Berkeley) 7/4/93";
43 #else
44 __RCSID("$NetBSD: setlocale.c,v 1.40 2002/08/02 12:27:32 tshiozak Exp $");
45 #endif
46 #endif /* LIBC_SCCS and not lint */
47
48 #define _CTYPE_PRIVATE
49
50 #include "namespace.h"
51 #include <sys/localedef.h>
52 #include <sys/types.h>
53 #include <sys/stat.h>
54 #include <assert.h>
55 #include <limits.h>
56 #include <ctype.h>
57 #define __SETLOCALE_SOURCE__
58 #include <locale.h>
59 #include <paths.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64 #ifdef WITH_RUNE
65 #include "rune.h"
66 #include "rune_local.h"
67 #else
68 #include "ctypeio.h"
69 #endif
70
71 /*
72 * Category names for getenv()
73 */
74 static const char *const categories[_LC_LAST] = {
75 "LC_ALL",
76 "LC_COLLATE",
77 "LC_CTYPE",
78 "LC_MONETARY",
79 "LC_NUMERIC",
80 "LC_TIME",
81 "LC_MESSAGES"
82 };
83
84 /*
85 * Current locales for each category
86 */
87 static char current_categories[_LC_LAST][32] = {
88 "C",
89 "C",
90 "C",
91 "C",
92 "C",
93 "C",
94 "C"
95 };
96
97 /*
98 * The locales we are going to try and load
99 */
100 static char new_categories[_LC_LAST][32];
101
102 /*
103 * Backup area to back out changes on failure
104 */
105 static char saved_categories[_LC_LAST][32];
106
107 static char current_locale_string[_LC_LAST * 33];
108 char *_PathLocale;
109
110 static char *currentlocale __P((void));
111 static char *loadlocale __P((int));
112 static const char *__get_locale_env __P((int));
113
114 char *
115 __setlocale(category, locale)
116 int category;
117 const char *locale;
118 {
119 int i, loadlocale_success;
120 size_t len;
121 const char *env, *r;
122
123 if (issetugid() ||
124 (!_PathLocale && !(_PathLocale = getenv("PATH_LOCALE"))))
125 _PathLocale = _PATH_LOCALE;
126
127 if (category < 0 || category >= _LC_LAST)
128 return (NULL);
129
130 if (!locale)
131 return (category ?
132 current_categories[category] : currentlocale());
133
134 /*
135 * Default to the current locale for everything.
136 */
137 for (i = 1; i < _LC_LAST; ++i)
138 (void)strlcpy(new_categories[i], current_categories[i],
139 sizeof(new_categories[i]));
140
141 /*
142 * Now go fill up new_categories from the locale argument
143 */
144 if (!*locale) {
145 if (category == LC_ALL) {
146 for (i = 1; i < _LC_LAST; ++i) {
147 env = __get_locale_env(i);
148 (void)strlcpy(new_categories[i], env,
149 sizeof(new_categories[i]));
150 }
151 }
152 else {
153 env = __get_locale_env(category);
154 (void)strlcpy(new_categories[category], env,
155 sizeof(new_categories[category]));
156 }
157 } else if (category) {
158 (void)strlcpy(new_categories[category], locale,
159 sizeof(new_categories[category]));
160 } else {
161 if ((r = strchr(locale, '/')) == 0) {
162 for (i = 1; i < _LC_LAST; ++i) {
163 (void)strlcpy(new_categories[i], locale,
164 sizeof(new_categories[i]));
165 }
166 } else {
167 for (i = 1; r[1] == '/'; ++r)
168 ;
169 if (!r[1])
170 return (NULL); /* Hmm, just slashes... */
171 do {
172 if (i == _LC_LAST)
173 return (NULL); /* too many slashes. */
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 i++;
180 locale = r;
181 while (*locale == '/')
182 ++locale;
183 while (*++r && *r != '/');
184 } while (*locale);
185 while (i < _LC_LAST) {
186 (void)strlcpy(new_categories[i],
187 new_categories[i - 1],
188 sizeof(new_categories[i]));
189 i++;
190 }
191 }
192 }
193
194 if (category)
195 return (loadlocale(category));
196
197 loadlocale_success = 0;
198 for (i = 1; i < _LC_LAST; ++i) {
199 (void)strlcpy(saved_categories[i], current_categories[i],
200 sizeof(saved_categories[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 char *
236 loadlocale(category)
237 int category;
238 {
239 char name[PATH_MAX];
240
241 _DIAGASSERT(0 < category && category < _LC_LAST);
242
243 if (strcmp(new_categories[category], current_categories[category]) == 0)
244 return (current_categories[category]);
245
246 if (!strcmp(new_categories[category], "C") ||
247 !strcmp(new_categories[category], "POSIX")) {
248
249 switch (category) {
250 case LC_CTYPE:
251 #ifdef WITH_RUNE
252 (void)_xpg4_setrunelocale("C");
253 (void)__runetable_to_netbsd_ctype("C");
254 #else
255 if (_ctype_ != _C_ctype_) {
256 /* LINTED const castaway */
257 free((void *)_ctype_);
258 _ctype_ = _C_ctype_;
259 }
260 if (_toupper_tab_ != _C_toupper_) {
261 /* LINTED const castaway */
262 free((void *)_toupper_tab_);
263 _toupper_tab_ = _C_toupper_;
264 }
265 if (_tolower_tab_ != _C_tolower_) {
266 /* LINTED const castaway */
267 free((void *)_tolower_tab_);
268 _tolower_tab_ = _C_tolower_;
269 }
270 #endif
271 }
272
273 (void)strlcpy(current_categories[category],
274 new_categories[category],
275 sizeof(current_categories[category]));
276 return current_categories[category];
277 }
278
279 (void)snprintf(name, sizeof(name), "%s/%s/%s",
280 _PathLocale, new_categories[category], categories[category]);
281
282 switch (category) {
283 case LC_CTYPE:
284 #ifdef WITH_RUNE
285 if (_xpg4_setrunelocale(new_categories[category]))
286 return NULL;
287 if (__runetable_to_netbsd_ctype(new_categories[category])) {
288 /* very unfortunate, but need to go to "C" locale */
289 (void)_xpg4_setrunelocale("C");
290 (void)__runetable_to_netbsd_ctype("C");
291 return NULL;
292 }
293 #else
294 if (!__loadctype(name))
295 return NULL;
296 #endif
297 break;
298
299 case LC_MESSAGES:
300 /*
301 * XXX we don't have LC_MESSAGES support yet,
302 * but catopen may use the value of LC_MESSAGES category.
303 * so return successfully if locale directory is present.
304 */
305 (void)snprintf(name, sizeof(name), "%s/%s",
306 _PathLocale, new_categories[category]);
307 /* local */
308 {
309 struct stat st;
310 if (stat(name, &st) < 0)
311 return NULL;
312 if (!S_ISDIR(st.st_mode))
313 return NULL;
314 }
315 break;
316
317 case LC_COLLATE:
318 case LC_MONETARY:
319 case LC_NUMERIC:
320 case LC_TIME:
321 return NULL;
322 }
323
324 (void)strlcpy(current_categories[category],
325 new_categories[category],
326 sizeof(current_categories[category]));
327 return current_categories[category];
328 }
329
330 static const char *
331 __get_locale_env(category)
332 int category;
333 {
334 const char *env;
335
336 _DIAGASSERT(category != LC_ALL);
337
338 /* 1. check LC_ALL. */
339 env = getenv(categories[0]);
340
341 /* 2. check LC_* */
342 if (!env || !*env)
343 env = getenv(categories[category]);
344
345 /* 3. check LANG */
346 if (!env || !*env)
347 env = getenv("LANG");
348
349 /* 4. if none is set, fall to "C" */
350 if (!env || !*env || strchr(env, '/'))
351 env = "C";
352
353 return env;
354 }
355