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