]> git.saurik.com Git - apple/libc.git/blob - locale/FreeBSD/setlocale.c
Libc-498.tar.gz
[apple/libc.git] / locale / FreeBSD / setlocale.c
1 /*
2 * Copyright (c) 1996 - 2002 FreeBSD Project
3 * Copyright (c) 1991, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Paul Borman at Krystal Technologies.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid[] = "@(#)setlocale.c 8.1 (Berkeley) 7/4/93";
40 #endif /* LIBC_SCCS and not lint */
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD: src/lib/libc/locale/setlocale.c,v 1.50 2004/01/31 19:15:32 ache Exp $");
43
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <errno.h>
47 #include <limits.h>
48 #include <locale.h>
49 #include <paths.h> /* for _PATH_LOCALE */
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include "collate.h"
54 #include "lmonetary.h" /* for __monetary_load_locale() */
55 #include "lnumeric.h" /* for __numeric_load_locale() */
56 #include "lmessages.h" /* for __messages_load_locale() */
57 #include "setlocale.h"
58 #include "ldpart.h"
59 #include "../stdtime/timelocal.h" /* for __time_load_locale() */
60
61 /*
62 * Category names for getenv()
63 */
64 static char *categories[_LC_LAST] = {
65 "LC_ALL",
66 "LC_COLLATE",
67 "LC_CTYPE",
68 "LC_MONETARY",
69 "LC_NUMERIC",
70 "LC_TIME",
71 "LC_MESSAGES",
72 };
73
74 /*
75 * Current locales for each category
76 */
77 static char current_categories[_LC_LAST][ENCODING_LEN + 1] = {
78 "C",
79 "C",
80 "C",
81 "C",
82 "C",
83 "C",
84 "C",
85 };
86
87 /*
88 * Path to locale storage directory
89 */
90 char *_PathLocale;
91
92 /*
93 * The locales we are going to try and load
94 */
95 static char new_categories[_LC_LAST][ENCODING_LEN + 1];
96 static char saved_categories[_LC_LAST][ENCODING_LEN + 1];
97
98 static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)];
99
100 static char *currentlocale(void);
101 static char *loadlocale(int);
102 static const char *__get_locale_env(int);
103
104 char *
105 setlocale(category, locale)
106 int category;
107 const char *locale;
108 {
109 int i, j, len, saverr;
110 const char *env, *r;
111
112 if (category < LC_ALL || category >= _LC_LAST) {
113 errno = EINVAL;
114 return (NULL);
115 }
116
117 if (locale == NULL)
118 return (category != LC_ALL ?
119 current_categories[category] : currentlocale());
120
121 /*
122 * Default to the current locale for everything.
123 */
124 for (i = 1; i < _LC_LAST; ++i)
125 (void)strcpy(new_categories[i], current_categories[i]);
126
127 /*
128 * Now go fill up new_categories from the locale argument
129 */
130 if (!*locale) {
131 if (category == LC_ALL) {
132 for (i = 1; i < _LC_LAST; ++i) {
133 env = __get_locale_env(i);
134 if (strlen(env) > ENCODING_LEN) {
135 errno = EINVAL;
136 return (NULL);
137 }
138 (void)strcpy(new_categories[i], env);
139 }
140 } else {
141 env = __get_locale_env(category);
142 if (strlen(env) > ENCODING_LEN) {
143 errno = EINVAL;
144 return (NULL);
145 }
146 (void)strcpy(new_categories[category], env);
147 }
148 } else if (category != LC_ALL) {
149 if (strlen(locale) > ENCODING_LEN) {
150 errno = EINVAL;
151 return (NULL);
152 }
153 (void)strcpy(new_categories[category], locale);
154 } else {
155 if ((r = strchr(locale, '/')) == NULL) {
156 if (strlen(locale) > ENCODING_LEN) {
157 errno = EINVAL;
158 return (NULL);
159 }
160 for (i = 1; i < _LC_LAST; ++i)
161 (void)strcpy(new_categories[i], locale);
162 } else {
163 for (i = 1; r[1] == '/'; ++r)
164 ;
165 if (!r[1]) {
166 errno = EINVAL;
167 return (NULL); /* Hmm, just slashes... */
168 }
169 do {
170 if (i == _LC_LAST)
171 break; /* Too many slashes... */
172 if ((len = r - locale) > ENCODING_LEN) {
173 errno = EINVAL;
174 return (NULL);
175 }
176 (void)strlcpy(new_categories[i], locale,
177 len + 1);
178 i++;
179 while (*r == '/')
180 r++;
181 locale = r;
182 while (*r && *r != '/')
183 r++;
184 } while (*locale);
185 while (i < _LC_LAST) {
186 (void)strcpy(new_categories[i],
187 new_categories[i-1]);
188 i++;
189 }
190 }
191 }
192
193 if (category != LC_ALL)
194 return (loadlocale(category));
195
196 for (i = 1; i < _LC_LAST; ++i) {
197 (void)strcpy(saved_categories[i], current_categories[i]);
198 if (loadlocale(i) == NULL) {
199 saverr = errno;
200 for (j = 1; j < i; j++) {
201 (void)strcpy(new_categories[j],
202 saved_categories[j]);
203 if (loadlocale(j) == NULL) {
204 (void)strcpy(new_categories[j], "C");
205 (void)loadlocale(j);
206 }
207 }
208 errno = saverr;
209 return (NULL);
210 }
211 }
212 return (currentlocale());
213 }
214
215 static char *
216 currentlocale()
217 {
218 int i;
219
220 (void)strcpy(current_locale_string, current_categories[1]);
221
222 for (i = 2; i < _LC_LAST; ++i)
223 if (strcmp(current_categories[1], current_categories[i])) {
224 for (i = 2; i < _LC_LAST; ++i) {
225 (void)strcat(current_locale_string, "/");
226 (void)strcat(current_locale_string,
227 current_categories[i]);
228 }
229 break;
230 }
231 return (current_locale_string);
232 }
233
234 static char *
235 loadlocale(category)
236 int category;
237 {
238 char *new = new_categories[category];
239 char *old = current_categories[category];
240 int (*func)(const char *);
241 int saved_errno;
242
243 if ((new[0] == '.' &&
244 (new[1] == '\0' || (new[1] == '.' && new[2] == '\0'))) ||
245 strchr(new, '/') != NULL) {
246 errno = EINVAL;
247 return (NULL);
248 }
249
250 saved_errno = errno;
251 errno = __detect_path_locale();
252 if (errno != 0)
253 return (NULL);
254 errno = saved_errno;
255
256 switch (category) {
257 case LC_CTYPE:
258 func = __wrap_setrunelocale;
259 break;
260 case LC_COLLATE:
261 func = __collate_load_tables;
262 break;
263 case LC_TIME:
264 func = __time_load_locale;
265 break;
266 case LC_NUMERIC:
267 func = __numeric_load_locale;
268 break;
269 case LC_MONETARY:
270 func = __monetary_load_locale;
271 break;
272 case LC_MESSAGES:
273 func = __messages_load_locale;
274 break;
275 default:
276 errno = EINVAL;
277 return (NULL);
278 }
279
280 if (strcmp(new, old) == 0)
281 return (old);
282
283 if (func(new) != _LDP_ERROR) {
284 (void)strcpy(old, new);
285 return (old);
286 }
287
288 return (NULL);
289 }
290
291 static const char *
292 __get_locale_env(category)
293 int category;
294 {
295 const char *env;
296
297 /* 1. check LC_ALL. */
298 env = getenv(categories[0]);
299
300 /* 2. check LC_* */
301 if (env == NULL || !*env)
302 env = getenv(categories[category]);
303
304 /* 3. check LANG */
305 if (env == NULL || !*env)
306 env = getenv("LANG");
307
308 /* 4. if none is set, fall to "C" */
309 if (env == NULL || !*env)
310 env = "C";
311
312 return (env);
313 }
314
315 /*
316 * Detect locale storage location and store its value to _PathLocale variable
317 */
318 int
319 __detect_path_locale(void)
320 {
321 if (_PathLocale == NULL) {
322 char *p = getenv("PATH_LOCALE");
323
324 if (p != NULL && !issetugid()) {
325 if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
326 1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
327 return (ENAMETOOLONG);
328 _PathLocale = strdup(p);
329 if (_PathLocale == NULL)
330 return (errno == 0 ? ENOMEM : errno);
331 } else
332 _PathLocale = _PATH_LOCALE;
333 }
334 return (0);
335 }
336