setlocale.c revision 1.39 1 /* $NetBSD: setlocale.c,v 1.39 2002/08/02 08:02:36 itojun 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.39 2002/08/02 08:02:36 itojun 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 > sizeof(new_categories[i]) - 1
175 ? sizeof(new_categories[i]) - 1
176 : r - locale;
177 if (len + 1 > sizeof(new_categories[i]))
178 return (NULL); /* too long */
179 (void)memcpy(new_categories[i], locale, len);
180 new_categories[i][len] = '\0';
181 i++;
182 locale = r;
183 while (*locale == '/')
184 ++locale;
185 while (*++r && *r != '/');
186 } while (*locale);
187 while (i < _LC_LAST) {
188 (void)strlcpy(new_categories[i],
189 new_categories[i - 1],
190 sizeof(new_categories[i]));
191 i++;
192 }
193 }
194 }
195
196 if (category)
197 return (loadlocale(category));
198
199 loadlocale_success = 0;
200 for (i = 1; i < _LC_LAST; ++i) {
201 (void)strlcpy(saved_categories[i], current_categories[i],
202 sizeof(saved_categories[i]));
203 if (loadlocale(i) != NULL)
204 loadlocale_success = 1;
205 }
206
207 /*
208 * If all categories failed, return NULL; we don't need to back
209 * changes off, since none happened.
210 */
211 if (!loadlocale_success)
212 return NULL;
213
214 return (currentlocale());
215 }
216
217 static char *
218 currentlocale()
219 {
220 int i;
221
222 (void)strlcpy(current_locale_string, current_categories[1],
223 sizeof(current_locale_string));
224
225 for (i = 2; i < _LC_LAST; ++i)
226 if (strcmp(current_categories[1], current_categories[i])) {
227 (void)snprintf(current_locale_string,
228 sizeof(current_locale_string), "%s/%s/%s/%s/%s/%s",
229 current_categories[1], current_categories[2],
230 current_categories[3], current_categories[4],
231 current_categories[5], current_categories[6]);
232 break;
233 }
234 return (current_locale_string);
235 }
236
237 static char *
238 loadlocale(category)
239 int category;
240 {
241 char name[PATH_MAX];
242
243 _DIAGASSERT(0 < category && category < _LC_LAST);
244
245 if (strcmp(new_categories[category], current_categories[category]) == 0)
246 return (current_categories[category]);
247
248 if (!strcmp(new_categories[category], "C") ||
249 !strcmp(new_categories[category], "POSIX")) {
250
251 switch (category) {
252 case LC_CTYPE:
253 #ifdef WITH_RUNE
254 (void)_xpg4_setrunelocale("C");
255 (void)__runetable_to_netbsd_ctype("C");
256 #else
257 if (_ctype_ != _C_ctype_) {
258 /* LINTED const castaway */
259 free((void *)_ctype_);
260 _ctype_ = _C_ctype_;
261 }
262 if (_toupper_tab_ != _C_toupper_) {
263 /* LINTED const castaway */
264 free((void *)_toupper_tab_);
265 _toupper_tab_ = _C_toupper_;
266 }
267 if (_tolower_tab_ != _C_tolower_) {
268 /* LINTED const castaway */
269 free((void *)_tolower_tab_);
270 _tolower_tab_ = _C_tolower_;
271 }
272 #endif
273 }
274
275 (void)strlcpy(current_categories[category],
276 new_categories[category],
277 sizeof(current_categories[category]));
278 return current_categories[category];
279 }
280
281 (void)snprintf(name, sizeof(name), "%s/%s/%s",
282 _PathLocale, new_categories[category], categories[category]);
283
284 switch (category) {
285 case LC_CTYPE:
286 #ifdef WITH_RUNE
287 if (_xpg4_setrunelocale(new_categories[category]))
288 return NULL;
289 if (__runetable_to_netbsd_ctype(new_categories[category])) {
290 /* very unfortunate, but need to go to "C" locale */
291 (void)_xpg4_setrunelocale("C");
292 (void)__runetable_to_netbsd_ctype("C");
293 return NULL;
294 }
295 #else
296 if (!__loadctype(name))
297 return NULL;
298 #endif
299 break;
300
301 case LC_MESSAGES:
302 /*
303 * XXX we don't have LC_MESSAGES support yet,
304 * but catopen may use the value of LC_MESSAGES category.
305 * so return successfully if locale directory is present.
306 */
307 (void)snprintf(name, sizeof(name), "%s/%s",
308 _PathLocale, new_categories[category]);
309 /* local */
310 {
311 struct stat st;
312 if (stat(name, &st) < 0)
313 return NULL;
314 if (!S_ISDIR(st.st_mode))
315 return NULL;
316 }
317 break;
318
319 case LC_COLLATE:
320 case LC_MONETARY:
321 case LC_NUMERIC:
322 case LC_TIME:
323 return NULL;
324 }
325
326 (void)strlcpy(current_categories[category],
327 new_categories[category],
328 sizeof(current_categories[category]));
329 return current_categories[category];
330 }
331
332 static const char *
333 __get_locale_env(category)
334 int category;
335 {
336 const char *env;
337
338 _DIAGASSERT(category != LC_ALL);
339
340 /* 1. check LC_ALL. */
341 env = getenv(categories[0]);
342
343 /* 2. check LC_* */
344 if (!env || !*env)
345 env = getenv(categories[category]);
346
347 /* 3. check LANG */
348 if (!env || !*env)
349 env = getenv("LANG");
350
351 /* 4. if none is set, fall to "C" */
352 if (!env || !*env || strchr(env, '/'))
353 env = "C";
354
355 return env;
356 }
357