compiler/clib/regex: Switched to NetBSD version of the regex functions.
[AROS.git] / compiler / clib / locale / setlocale.c
blob084749ae933cf32d219d537021f249aa86bccc8d
1 /*
2 * Copyright (c) 1996 - 2002 FreeBSD Project
3 * Copyright (c) 1991, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Paul Borman at Krystal Technologies.
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 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)setlocale.c 8.1 (Berkeley) 7/4/93";
36 #endif /* LIBC_SCCS and not lint */
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD: src/lib/libc/locale/setlocale.c,v 1.51 2007/01/09 00:28:00 imp Exp $");
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <errno.h>
43 #include <limits.h>
44 #include <locale.h>
45 #ifdef __AROS__
46 #define _PATH_LOCALE "/Locale"
47 #else
48 #include <paths.h> /* for _PATH_LOCALE */
49 #endif
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() */
61 #ifdef __AROS__
62 #define issetugid() (0)
63 #endif
66 * Category names for getenv()
68 static char *categories[_LC_LAST] = {
69 "LC_ALL",
70 "LC_COLLATE",
71 "LC_CTYPE",
72 "LC_MONETARY",
73 "LC_NUMERIC",
74 "LC_TIME",
75 "LC_MESSAGES",
79 * Current locales for each category
81 static char current_categories[_LC_LAST][ENCODING_LEN + 1] = {
82 "C",
83 "C",
84 "C",
85 "C",
86 "C",
87 "C",
88 "C",
92 * Path to locale storage directory
94 char *_PathLocale;
97 * The locales we are going to try and load
99 static char new_categories[_LC_LAST][ENCODING_LEN + 1];
100 static char saved_categories[_LC_LAST][ENCODING_LEN + 1];
102 static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)];
104 static char *currentlocale(void);
105 static char *loadlocale(int);
106 static const char *__get_locale_env(int);
108 char *
109 setlocale(category, locale)
110 int category;
111 const char *locale;
113 int i, j, len, saverr;
114 const char *env, *r;
116 if (category < LC_ALL || category >= _LC_LAST) {
117 errno = EINVAL;
118 return (NULL);
121 if (locale == NULL)
122 return (category != LC_ALL ?
123 current_categories[category] : currentlocale());
126 * Default to the current locale for everything.
128 for (i = 1; i < _LC_LAST; ++i)
129 (void)strcpy(new_categories[i], current_categories[i]);
132 * Now go fill up new_categories from the locale argument
134 if (!*locale) {
135 if (category == LC_ALL) {
136 for (i = 1; i < _LC_LAST; ++i) {
137 env = __get_locale_env(i);
138 if (strlen(env) > ENCODING_LEN) {
139 errno = EINVAL;
140 return (NULL);
142 (void)strcpy(new_categories[i], env);
144 } else {
145 env = __get_locale_env(category);
146 if (strlen(env) > ENCODING_LEN) {
147 errno = EINVAL;
148 return (NULL);
150 (void)strcpy(new_categories[category], env);
152 } else if (category != LC_ALL) {
153 if (strlen(locale) > ENCODING_LEN) {
154 errno = EINVAL;
155 return (NULL);
157 (void)strcpy(new_categories[category], locale);
158 } else {
159 if ((r = strchr(locale, '/')) == NULL) {
160 if (strlen(locale) > ENCODING_LEN) {
161 errno = EINVAL;
162 return (NULL);
164 for (i = 1; i < _LC_LAST; ++i)
165 (void)strcpy(new_categories[i], locale);
166 } else {
167 for (i = 1; r[1] == '/'; ++r)
169 if (!r[1]) {
170 errno = EINVAL;
171 return (NULL); /* Hmm, just slashes... */
173 do {
174 if (i == _LC_LAST)
175 break; /* Too many slashes... */
176 if ((len = r - locale) > ENCODING_LEN) {
177 errno = EINVAL;
178 return (NULL);
180 (void)strlcpy(new_categories[i], locale,
181 len + 1);
182 i++;
183 while (*r == '/')
184 r++;
185 locale = r;
186 while (*r && *r != '/')
187 r++;
188 } while (*locale);
189 while (i < _LC_LAST) {
190 (void)strcpy(new_categories[i],
191 new_categories[i-1]);
192 i++;
197 if (category != LC_ALL)
198 return (loadlocale(category));
200 for (i = 1; i < _LC_LAST; ++i) {
201 (void)strcpy(saved_categories[i], current_categories[i]);
202 if (loadlocale(i) == NULL) {
203 saverr = errno;
204 for (j = 1; j < i; j++) {
205 (void)strcpy(new_categories[j],
206 saved_categories[j]);
207 if (loadlocale(j) == NULL) {
208 (void)strcpy(new_categories[j], "C");
209 (void)loadlocale(j);
212 errno = saverr;
213 return (NULL);
216 return (currentlocale());
219 static char *
220 currentlocale()
222 int i;
224 (void)strcpy(current_locale_string, current_categories[1]);
226 for (i = 2; i < _LC_LAST; ++i)
227 if (strcmp(current_categories[1], current_categories[i])) {
228 for (i = 2; i < _LC_LAST; ++i) {
229 (void)strcat(current_locale_string, "/");
230 (void)strcat(current_locale_string,
231 current_categories[i]);
233 break;
235 return (current_locale_string);
238 static char *
239 loadlocale(category)
240 int category;
242 char *new = new_categories[category];
243 char *old = current_categories[category];
244 int (*func)(const char *);
245 int saved_errno;
247 if ((new[0] == '.' &&
248 (new[1] == '\0' || (new[1] == '.' && new[2] == '\0'))) ||
249 strchr(new, '/') != NULL) {
250 errno = EINVAL;
251 return (NULL);
254 saved_errno = errno;
255 errno = __detect_path_locale();
256 if (errno != 0)
257 return (NULL);
258 errno = saved_errno;
260 switch (category) {
261 case LC_CTYPE:
262 func = __wrap_setrunelocale;
263 break;
264 case LC_COLLATE:
265 func = __collate_load_tables;
266 break;
267 case LC_TIME:
268 func = __time_load_locale;
269 break;
270 case LC_NUMERIC:
271 func = __numeric_load_locale;
272 break;
273 case LC_MONETARY:
274 func = __monetary_load_locale;
275 break;
276 case LC_MESSAGES:
277 func = __messages_load_locale;
278 break;
279 default:
280 errno = EINVAL;
281 return (NULL);
284 if (strcmp(new, old) == 0)
285 return (old);
287 if (func(new) != _LDP_ERROR) {
288 (void)strcpy(old, new);
289 return (old);
292 return (NULL);
295 static const char *
296 __get_locale_env(category)
297 int category;
299 const char *env;
301 /* 1. check LC_ALL. */
302 env = getenv(categories[0]);
304 /* 2. check LC_* */
305 if (env == NULL || !*env)
306 env = getenv(categories[category]);
308 /* 3. check LANG */
309 if (env == NULL || !*env)
310 env = getenv("LANG");
312 /* 4. if none is set, fall to "C" */
313 if (env == NULL || !*env)
314 env = "C";
316 return (env);
320 * Detect locale storage location and store its value to _PathLocale variable
323 __detect_path_locale(void)
325 if (_PathLocale == NULL) {
326 char *p = getenv("PATH_LOCALE");
328 if (p != NULL && !issetugid()) {
329 if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
330 1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
331 return (ENAMETOOLONG);
332 _PathLocale = strdup(p);
333 if (_PathLocale == NULL)
334 return (errno == 0 ? ENOMEM : errno);
335 } else
336 _PathLocale = _PATH_LOCALE;
338 return (0);