]>
git.saurik.com Git - apple/libc.git/blob - locale/FreeBSD/setlocale.c
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
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.
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
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 $");
44 #include <sys/types.h>
49 #include <paths.h> /* for _PATH_LOCALE */
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"
59 #include "../stdtime/timelocal.h" /* for __time_load_locale() */
62 * Category names for getenv()
64 static char *categories
[_LC_LAST
] = {
75 * Current locales for each category
77 static char current_categories
[_LC_LAST
][ENCODING_LEN
+ 1] = {
88 * Path to locale storage directory
93 * The locales we are going to try and load
95 static char new_categories
[_LC_LAST
][ENCODING_LEN
+ 1];
96 static char saved_categories
[_LC_LAST
][ENCODING_LEN
+ 1];
98 static char current_locale_string
[_LC_LAST
* (ENCODING_LEN
+ 1/*"/"*/ + 1)];
100 static char *currentlocale(void);
101 static char *loadlocale(int);
102 static const char *__get_locale_env(int);
105 setlocale(category
, locale
)
109 int i
, j
, len
, saverr
;
112 if (category
< LC_ALL
|| category
>= _LC_LAST
) {
118 return (category
!= LC_ALL
?
119 current_categories
[category
] : currentlocale());
122 * Default to the current locale for everything.
124 for (i
= 1; i
< _LC_LAST
; ++i
)
125 (void)strcpy(new_categories
[i
], current_categories
[i
]);
128 * Now go fill up new_categories from the locale argument
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
) {
138 (void)strcpy(new_categories
[i
], env
);
141 env
= __get_locale_env(category
);
142 if (strlen(env
) > ENCODING_LEN
) {
146 (void)strcpy(new_categories
[category
], env
);
148 } else if (category
!= LC_ALL
) {
149 if (strlen(locale
) > ENCODING_LEN
) {
153 (void)strcpy(new_categories
[category
], locale
);
155 if ((r
= strchr(locale
, '/')) == NULL
) {
156 if (strlen(locale
) > ENCODING_LEN
) {
160 for (i
= 1; i
< _LC_LAST
; ++i
)
161 (void)strcpy(new_categories
[i
], locale
);
163 for (i
= 1; r
[1] == '/'; ++r
)
167 return (NULL
); /* Hmm, just slashes... */
171 break; /* Too many slashes... */
172 if ((len
= r
- locale
) > ENCODING_LEN
) {
176 (void)strlcpy(new_categories
[i
], locale
,
182 while (*r
&& *r
!= '/')
185 while (i
< _LC_LAST
) {
186 (void)strcpy(new_categories
[i
],
187 new_categories
[i
-1]);
193 if (category
!= LC_ALL
)
194 return (loadlocale(category
));
196 for (i
= 1; i
< _LC_LAST
; ++i
) {
197 (void)strcpy(saved_categories
[i
], current_categories
[i
]);
198 if (loadlocale(i
) == NULL
) {
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");
212 return (currentlocale());
220 (void)strcpy(current_locale_string
, current_categories
[1]);
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
]);
231 return (current_locale_string
);
238 char *new = new_categories
[category
];
239 char *old
= current_categories
[category
];
240 int (*func
)(const char *);
243 if ((new[0] == '.' &&
244 (new[1] == '\0' || (new[1] == '.' && new[2] == '\0'))) ||
245 strchr(new, '/') != NULL
) {
251 errno
= __detect_path_locale();
258 func
= __wrap_setrunelocale
;
261 func
= __collate_load_tables
;
264 func
= __time_load_locale
;
267 func
= __numeric_load_locale
;
270 func
= __monetary_load_locale
;
273 func
= __messages_load_locale
;
280 if (strcmp(new, old
) == 0)
283 if (func(new) != _LDP_ERROR
) {
284 (void)strcpy(old
, new);
292 __get_locale_env(category
)
297 /* 1. check LC_ALL. */
298 env
= getenv(categories
[0]);
301 if (env
== NULL
|| !*env
)
302 env
= getenv(categories
[category
]);
305 if (env
== NULL
|| !*env
)
306 env
= getenv("LANG");
308 /* 4. if none is set, fall to "C" */
309 if (env
== NULL
|| !*env
)
316 * Detect locale storage location and store its value to _PathLocale variable
319 __detect_path_locale(void)
321 if (_PathLocale
== NULL
) {
322 char *p
= getenv("PATH_LOCALE");
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
);
332 _PathLocale
= _PATH_LOCALE
;