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