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