setlocale.c revision 1.33 1 1.33 itojun /* $NetBSD: setlocale.c,v 1.33 2001/01/22 00:29:46 itojun Exp $ */
2 1.10 kleink
3 1.1 cgd /*
4 1.5 jtc * Copyright (c) 1991, 1993
5 1.5 jtc * The Regents of the University of California. All rights reserved.
6 1.5 jtc *
7 1.5 jtc * This code is derived from software contributed to Berkeley by
8 1.5 jtc * Paul Borman at Krystal Technologies.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd */
38 1.1 cgd
39 1.12 christos #include <sys/cdefs.h>
40 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
41 1.10 kleink #if 0
42 1.5 jtc static char sccsid[] = "@(#)setlocale.c 8.1 (Berkeley) 7/4/93";
43 1.10 kleink #else
44 1.33 itojun __RCSID("$NetBSD: setlocale.c,v 1.33 2001/01/22 00:29:46 itojun Exp $");
45 1.10 kleink #endif
46 1.1 cgd #endif /* LIBC_SCCS and not lint */
47 1.1 cgd
48 1.11 kleink #define _CTYPE_PRIVATE
49 1.11 kleink
50 1.14 kleink #include "namespace.h"
51 1.5 jtc #include <sys/localedef.h>
52 1.11 kleink #include <ctype.h>
53 1.11 kleink #include <limits.h>
54 1.18 tshiozak #define __SETLOCALE_SOURCE__
55 1.1 cgd #include <locale.h>
56 1.11 kleink #include <paths.h>
57 1.6 jtc #include <stdio.h>
58 1.5 jtc #include <stdlib.h>
59 1.1 cgd #include <string.h>
60 1.21 veego #include <unistd.h>
61 1.32 itojun #ifdef WITH_RUNE
62 1.24 itojun #include "rune.h"
63 1.24 itojun #include "rune_local.h"
64 1.32 itojun #else
65 1.32 itojun #include "ctypeio.h"
66 1.32 itojun #endif
67 1.1 cgd
68 1.5 jtc /*
69 1.5 jtc * Category names for getenv()
70 1.5 jtc */
71 1.13 mycroft static const char *const categories[_LC_LAST] = {
72 1.5 jtc "LC_ALL",
73 1.5 jtc "LC_COLLATE",
74 1.5 jtc "LC_CTYPE",
75 1.5 jtc "LC_MONETARY",
76 1.5 jtc "LC_NUMERIC",
77 1.5 jtc "LC_TIME",
78 1.5 jtc "LC_MESSAGES"
79 1.5 jtc };
80 1.1 cgd
81 1.1 cgd /*
82 1.5 jtc * Current locales for each category
83 1.5 jtc */
84 1.5 jtc static char current_categories[_LC_LAST][32] = {
85 1.5 jtc "C",
86 1.5 jtc "C",
87 1.5 jtc "C",
88 1.5 jtc "C",
89 1.5 jtc "C",
90 1.5 jtc "C",
91 1.5 jtc "C"
92 1.5 jtc };
93 1.19 kleink
94 1.24 itojun size_t __mb_len_max_runtime = MB_LEN_MAX;
95 1.5 jtc
96 1.5 jtc /*
97 1.5 jtc * The locales we are going to try and load
98 1.1 cgd */
99 1.5 jtc static char new_categories[_LC_LAST][32];
100 1.5 jtc
101 1.22 itojun /*
102 1.22 itojun * Backup area to back out changes on failure
103 1.22 itojun */
104 1.22 itojun static char saved_categories[_LC_LAST][32];
105 1.22 itojun
106 1.5 jtc static char current_locale_string[_LC_LAST * 33];
107 1.24 itojun char *_PathLocale;
108 1.5 jtc
109 1.5 jtc static char *currentlocale __P((void));
110 1.5 jtc static char *loadlocale __P((int));
111 1.5 jtc
112 1.1 cgd char *
113 1.18 tshiozak __setlocale_mb_len_max_32(category, locale)
114 1.1 cgd int category;
115 1.1 cgd const char *locale;
116 1.1 cgd {
117 1.32 itojun #ifdef WITH_RUNE
118 1.24 itojun __mb_len_max_runtime = 32;
119 1.32 itojun #else
120 1.32 itojun __mb_len_max_runtime = 1;
121 1.32 itojun #endif
122 1.24 itojun return __setlocale(category, locale);
123 1.24 itojun }
124 1.24 itojun
125 1.24 itojun char *
126 1.24 itojun __setlocale(category, locale)
127 1.24 itojun int category;
128 1.24 itojun const char *locale;
129 1.24 itojun {
130 1.27 jdolecek int i, loadlocale_success;
131 1.16 christos size_t len;
132 1.5 jtc char *env, *r;
133 1.5 jtc
134 1.20 tshiozak if (issetugid() ||
135 1.24 itojun (!_PathLocale && !(_PathLocale = getenv("PATH_LOCALE"))))
136 1.24 itojun _PathLocale = _PATH_LOCALE;
137 1.5 jtc
138 1.5 jtc if (category < 0 || category >= _LC_LAST)
139 1.1 cgd return (NULL);
140 1.5 jtc
141 1.5 jtc if (!locale)
142 1.5 jtc return (category ?
143 1.5 jtc current_categories[category] : currentlocale());
144 1.5 jtc
145 1.5 jtc /*
146 1.5 jtc * Default to the current locale for everything.
147 1.5 jtc */
148 1.5 jtc for (i = 1; i < _LC_LAST; ++i)
149 1.23 itojun (void)strlcpy(new_categories[i], current_categories[i],
150 1.23 itojun sizeof(new_categories[i]));
151 1.5 jtc
152 1.5 jtc /*
153 1.5 jtc * Now go fill up new_categories from the locale argument
154 1.5 jtc */
155 1.5 jtc if (!*locale) {
156 1.5 jtc env = getenv(categories[category]);
157 1.5 jtc
158 1.9 kleink if (!env || !*env)
159 1.5 jtc env = getenv(categories[0]);
160 1.5 jtc
161 1.9 kleink if (!env || !*env)
162 1.5 jtc env = getenv("LANG");
163 1.5 jtc
164 1.20 tshiozak if (!env || !*env || strchr(env, '/'))
165 1.5 jtc env = "C";
166 1.5 jtc
167 1.23 itojun (void)strlcpy(new_categories[category], env,
168 1.23 itojun sizeof(new_categories[category]));
169 1.5 jtc if (!category) {
170 1.5 jtc for (i = 1; i < _LC_LAST; ++i) {
171 1.25 itojun env = getenv(categories[i]);
172 1.25 itojun if (!env || !*env || strchr(env, '/'))
173 1.5 jtc env = new_categories[0];
174 1.23 itojun (void)strlcpy(new_categories[i], env,
175 1.23 itojun sizeof(new_categories[i]));
176 1.5 jtc }
177 1.5 jtc }
178 1.8 mrg } else if (category) {
179 1.23 itojun (void)strlcpy(new_categories[category], locale,
180 1.23 itojun sizeof(new_categories[category]));
181 1.5 jtc } else {
182 1.5 jtc if ((r = strchr(locale, '/')) == 0) {
183 1.5 jtc for (i = 1; i < _LC_LAST; ++i) {
184 1.23 itojun (void)strlcpy(new_categories[i], locale,
185 1.23 itojun sizeof(new_categories[i]));
186 1.5 jtc }
187 1.5 jtc } else {
188 1.23 itojun for (i = 1; r[1] == '/'; ++r)
189 1.23 itojun ;
190 1.5 jtc if (!r[1])
191 1.5 jtc return (NULL); /* Hmm, just slashes... */
192 1.5 jtc do {
193 1.23 itojun len = r - locale > sizeof(new_categories[i]) - 1
194 1.23 itojun ? sizeof(new_categories[i]) - 1
195 1.23 itojun : r - locale;
196 1.5 jtc (void)strncpy(new_categories[i++], locale, len);
197 1.5 jtc new_categories[i++][len] = 0;
198 1.5 jtc locale = r;
199 1.5 jtc while (*locale == '/')
200 1.5 jtc ++locale;
201 1.5 jtc while (*++r && *r != '/');
202 1.5 jtc } while (*locale);
203 1.5 jtc while (i < _LC_LAST)
204 1.23 itojun (void)strlcpy(new_categories[i],
205 1.23 itojun new_categories[i - 1],
206 1.23 itojun sizeof(new_categories[i]));
207 1.5 jtc }
208 1.5 jtc }
209 1.5 jtc
210 1.5 jtc if (category)
211 1.5 jtc return (loadlocale(category));
212 1.5 jtc
213 1.27 jdolecek loadlocale_success = 0;
214 1.22 itojun for (i = 1; i < _LC_LAST; ++i) {
215 1.22 itojun (void)strlcpy(saved_categories[i], current_categories[i],
216 1.22 itojun sizeof(saved_categories[i]));
217 1.27 jdolecek if (loadlocale(i) != NULL)
218 1.27 jdolecek loadlocale_success = 1;
219 1.22 itojun }
220 1.27 jdolecek
221 1.27 jdolecek /*
222 1.27 jdolecek * If all categories failed, return NULL; we don't need to back
223 1.27 jdolecek * changes off, since none happened.
224 1.27 jdolecek */
225 1.27 jdolecek if (!loadlocale_success)
226 1.27 jdolecek return NULL;
227 1.27 jdolecek
228 1.9 kleink return (currentlocale());
229 1.5 jtc }
230 1.5 jtc
231 1.5 jtc static char *
232 1.5 jtc currentlocale()
233 1.5 jtc {
234 1.5 jtc int i;
235 1.5 jtc
236 1.23 itojun (void)strlcpy(current_locale_string, current_categories[1],
237 1.23 itojun sizeof(current_locale_string));
238 1.5 jtc
239 1.5 jtc for (i = 2; i < _LC_LAST; ++i)
240 1.5 jtc if (strcmp(current_categories[1], current_categories[i])) {
241 1.5 jtc (void)snprintf(current_locale_string,
242 1.5 jtc sizeof(current_locale_string), "%s/%s/%s/%s/%s",
243 1.5 jtc current_categories[1], current_categories[2],
244 1.5 jtc current_categories[3], current_categories[4],
245 1.5 jtc current_categories[5]);
246 1.5 jtc break;
247 1.5 jtc }
248 1.5 jtc return (current_locale_string);
249 1.5 jtc }
250 1.5 jtc
251 1.15 kleink static char *
252 1.5 jtc loadlocale(category)
253 1.5 jtc int category;
254 1.5 jtc {
255 1.5 jtc char name[PATH_MAX];
256 1.5 jtc
257 1.11 kleink if (strcmp(new_categories[category], current_categories[category]) == 0)
258 1.5 jtc return (current_categories[category]);
259 1.5 jtc
260 1.5 jtc if (!strcmp(new_categories[category], "C") ||
261 1.11 kleink !strcmp(new_categories[category], "POSIX")) {
262 1.11 kleink
263 1.11 kleink switch (category) {
264 1.11 kleink case LC_CTYPE:
265 1.32 itojun #ifdef WITH_RUNE
266 1.24 itojun (void)_xpg4_setrunelocale("C");
267 1.29 itojun (void)__runetable_to_netbsd_ctype("C");
268 1.32 itojun #else
269 1.32 itojun if (_ctype_ != _C_ctype_) {
270 1.32 itojun /* LINTED const castaway */
271 1.32 itojun free((void *)_ctype_);
272 1.32 itojun _ctype_ = _C_ctype_;
273 1.32 itojun }
274 1.32 itojun if (_toupper_tab_ != _C_toupper_) {
275 1.32 itojun /* LINTED const castaway */
276 1.32 itojun free((void *)_toupper_tab_);
277 1.32 itojun _toupper_tab_ = _C_toupper_;
278 1.32 itojun }
279 1.32 itojun if (_tolower_tab_ != _C_tolower_) {
280 1.32 itojun /* LINTED const castaway */
281 1.32 itojun free((void *)_tolower_tab_);
282 1.32 itojun _tolower_tab_ = _C_tolower_;
283 1.32 itojun }
284 1.32 itojun #endif
285 1.11 kleink }
286 1.5 jtc
287 1.23 itojun (void)strlcpy(current_categories[category],
288 1.7 mrg new_categories[category],
289 1.23 itojun sizeof(current_categories[category]));
290 1.11 kleink return current_categories[category];
291 1.5 jtc }
292 1.5 jtc
293 1.5 jtc /*
294 1.5 jtc * Some day we will actually look at this file.
295 1.5 jtc */
296 1.5 jtc (void)snprintf(name, sizeof(name), "%s/%s/%s",
297 1.24 itojun _PathLocale, new_categories[category], categories[category]);
298 1.5 jtc
299 1.5 jtc switch (category) {
300 1.11 kleink case LC_CTYPE:
301 1.32 itojun #ifdef WITH_RUNE
302 1.31 itojun if (_xpg4_setrunelocale(new_categories[category]))
303 1.29 itojun return NULL;
304 1.31 itojun if (__runetable_to_netbsd_ctype(new_categories[category])) {
305 1.29 itojun /* very unfortunate, but need to go to "C" locale */
306 1.29 itojun (void)_xpg4_setrunelocale("C");
307 1.29 itojun (void)__runetable_to_netbsd_ctype("C");
308 1.28 itojun return NULL;
309 1.28 itojun }
310 1.32 itojun #else
311 1.32 itojun if (!__loadctype(name))
312 1.32 itojun return NULL;
313 1.32 itojun #endif
314 1.29 itojun
315 1.29 itojun (void)strlcpy(current_categories[category],
316 1.29 itojun new_categories[category],
317 1.29 itojun sizeof(current_categories[category]));
318 1.29 itojun return current_categories[category];
319 1.11 kleink
320 1.11 kleink case LC_COLLATE:
321 1.11 kleink case LC_MESSAGES:
322 1.11 kleink case LC_MONETARY:
323 1.11 kleink case LC_NUMERIC:
324 1.11 kleink case LC_TIME:
325 1.11 kleink return NULL;
326 1.5 jtc }
327 1.11 kleink
328 1.11 kleink return NULL;
329 1.1 cgd }
330