setlocale.c revision 1.33 1 /* $NetBSD: setlocale.c,v 1.33 2001/01/22 00:29:46 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.33 2001/01/22 00:29:46 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 <ctype.h>
53 #include <limits.h>
54 #define __SETLOCALE_SOURCE__
55 #include <locale.h>
56 #include <paths.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 #ifdef WITH_RUNE
62 #include "rune.h"
63 #include "rune_local.h"
64 #else
65 #include "ctypeio.h"
66 #endif
67
68 /*
69 * Category names for getenv()
70 */
71 static const char *const categories[_LC_LAST] = {
72 "LC_ALL",
73 "LC_COLLATE",
74 "LC_CTYPE",
75 "LC_MONETARY",
76 "LC_NUMERIC",
77 "LC_TIME",
78 "LC_MESSAGES"
79 };
80
81 /*
82 * Current locales for each category
83 */
84 static char current_categories[_LC_LAST][32] = {
85 "C",
86 "C",
87 "C",
88 "C",
89 "C",
90 "C",
91 "C"
92 };
93
94 size_t __mb_len_max_runtime = MB_LEN_MAX;
95
96 /*
97 * The locales we are going to try and load
98 */
99 static char new_categories[_LC_LAST][32];
100
101 /*
102 * Backup area to back out changes on failure
103 */
104 static char saved_categories[_LC_LAST][32];
105
106 static char current_locale_string[_LC_LAST * 33];
107 char *_PathLocale;
108
109 static char *currentlocale __P((void));
110 static char *loadlocale __P((int));
111
112 char *
113 __setlocale_mb_len_max_32(category, locale)
114 int category;
115 const char *locale;
116 {
117 #ifdef WITH_RUNE
118 __mb_len_max_runtime = 32;
119 #else
120 __mb_len_max_runtime = 1;
121 #endif
122 return __setlocale(category, locale);
123 }
124
125 char *
126 __setlocale(category, locale)
127 int category;
128 const char *locale;
129 {
130 int i, loadlocale_success;
131 size_t len;
132 char *env, *r;
133
134 if (issetugid() ||
135 (!_PathLocale && !(_PathLocale = getenv("PATH_LOCALE"))))
136 _PathLocale = _PATH_LOCALE;
137
138 if (category < 0 || category >= _LC_LAST)
139 return (NULL);
140
141 if (!locale)
142 return (category ?
143 current_categories[category] : currentlocale());
144
145 /*
146 * Default to the current locale for everything.
147 */
148 for (i = 1; i < _LC_LAST; ++i)
149 (void)strlcpy(new_categories[i], current_categories[i],
150 sizeof(new_categories[i]));
151
152 /*
153 * Now go fill up new_categories from the locale argument
154 */
155 if (!*locale) {
156 env = getenv(categories[category]);
157
158 if (!env || !*env)
159 env = getenv(categories[0]);
160
161 if (!env || !*env)
162 env = getenv("LANG");
163
164 if (!env || !*env || strchr(env, '/'))
165 env = "C";
166
167 (void)strlcpy(new_categories[category], env,
168 sizeof(new_categories[category]));
169 if (!category) {
170 for (i = 1; i < _LC_LAST; ++i) {
171 env = getenv(categories[i]);
172 if (!env || !*env || strchr(env, '/'))
173 env = new_categories[0];
174 (void)strlcpy(new_categories[i], env,
175 sizeof(new_categories[i]));
176 }
177 }
178 } else if (category) {
179 (void)strlcpy(new_categories[category], locale,
180 sizeof(new_categories[category]));
181 } else {
182 if ((r = strchr(locale, '/')) == 0) {
183 for (i = 1; i < _LC_LAST; ++i) {
184 (void)strlcpy(new_categories[i], locale,
185 sizeof(new_categories[i]));
186 }
187 } else {
188 for (i = 1; r[1] == '/'; ++r)
189 ;
190 if (!r[1])
191 return (NULL); /* Hmm, just slashes... */
192 do {
193 len = r - locale > sizeof(new_categories[i]) - 1
194 ? sizeof(new_categories[i]) - 1
195 : r - locale;
196 (void)strncpy(new_categories[i++], locale, len);
197 new_categories[i++][len] = 0;
198 locale = r;
199 while (*locale == '/')
200 ++locale;
201 while (*++r && *r != '/');
202 } while (*locale);
203 while (i < _LC_LAST)
204 (void)strlcpy(new_categories[i],
205 new_categories[i - 1],
206 sizeof(new_categories[i]));
207 }
208 }
209
210 if (category)
211 return (loadlocale(category));
212
213 loadlocale_success = 0;
214 for (i = 1; i < _LC_LAST; ++i) {
215 (void)strlcpy(saved_categories[i], current_categories[i],
216 sizeof(saved_categories[i]));
217 if (loadlocale(i) != NULL)
218 loadlocale_success = 1;
219 }
220
221 /*
222 * If all categories failed, return NULL; we don't need to back
223 * changes off, since none happened.
224 */
225 if (!loadlocale_success)
226 return NULL;
227
228 return (currentlocale());
229 }
230
231 static char *
232 currentlocale()
233 {
234 int i;
235
236 (void)strlcpy(current_locale_string, current_categories[1],
237 sizeof(current_locale_string));
238
239 for (i = 2; i < _LC_LAST; ++i)
240 if (strcmp(current_categories[1], current_categories[i])) {
241 (void)snprintf(current_locale_string,
242 sizeof(current_locale_string), "%s/%s/%s/%s/%s",
243 current_categories[1], current_categories[2],
244 current_categories[3], current_categories[4],
245 current_categories[5]);
246 break;
247 }
248 return (current_locale_string);
249 }
250
251 static char *
252 loadlocale(category)
253 int category;
254 {
255 char name[PATH_MAX];
256
257 if (strcmp(new_categories[category], current_categories[category]) == 0)
258 return (current_categories[category]);
259
260 if (!strcmp(new_categories[category], "C") ||
261 !strcmp(new_categories[category], "POSIX")) {
262
263 switch (category) {
264 case LC_CTYPE:
265 #ifdef WITH_RUNE
266 (void)_xpg4_setrunelocale("C");
267 (void)__runetable_to_netbsd_ctype("C");
268 #else
269 if (_ctype_ != _C_ctype_) {
270 /* LINTED const castaway */
271 free((void *)_ctype_);
272 _ctype_ = _C_ctype_;
273 }
274 if (_toupper_tab_ != _C_toupper_) {
275 /* LINTED const castaway */
276 free((void *)_toupper_tab_);
277 _toupper_tab_ = _C_toupper_;
278 }
279 if (_tolower_tab_ != _C_tolower_) {
280 /* LINTED const castaway */
281 free((void *)_tolower_tab_);
282 _tolower_tab_ = _C_tolower_;
283 }
284 #endif
285 }
286
287 (void)strlcpy(current_categories[category],
288 new_categories[category],
289 sizeof(current_categories[category]));
290 return current_categories[category];
291 }
292
293 /*
294 * Some day we will actually look at this file.
295 */
296 (void)snprintf(name, sizeof(name), "%s/%s/%s",
297 _PathLocale, new_categories[category], categories[category]);
298
299 switch (category) {
300 case LC_CTYPE:
301 #ifdef WITH_RUNE
302 if (_xpg4_setrunelocale(new_categories[category]))
303 return NULL;
304 if (__runetable_to_netbsd_ctype(new_categories[category])) {
305 /* very unfortunate, but need to go to "C" locale */
306 (void)_xpg4_setrunelocale("C");
307 (void)__runetable_to_netbsd_ctype("C");
308 return NULL;
309 }
310 #else
311 if (!__loadctype(name))
312 return NULL;
313 #endif
314
315 (void)strlcpy(current_categories[category],
316 new_categories[category],
317 sizeof(current_categories[category]));
318 return current_categories[category];
319
320 case LC_COLLATE:
321 case LC_MESSAGES:
322 case LC_MONETARY:
323 case LC_NUMERIC:
324 case LC_TIME:
325 return NULL;
326 }
327
328 return NULL;
329 }
330