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