setlocale.c revision 53050
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 53050 1999-11-09 11:09:16Z ache $
37 */
38
39#ifdef LIBC_RCS
40static const char rcsid[] =
41  "$FreeBSD: head/lib/libc/locale/setlocale.c 53050 1999-11-09 11:09:16Z ache $";
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#include "setlocale.h"
58
59/*
60 * Category names for getenv()
61 */
62static char *categories[_LC_LAST] = {
63    "LC_ALL",
64    "LC_COLLATE",
65    "LC_CTYPE",
66    "LC_MONETARY",
67    "LC_NUMERIC",
68    "LC_TIME",
69    "LC_MESSAGES",
70};
71
72/*
73 * Current locales for each category
74 */
75static char current_categories[_LC_LAST][ENCODING_LEN + 1] = {
76    "C",
77    "C",
78    "C",
79    "C",
80    "C",
81    "C",
82    "C",
83};
84
85/*
86 * The locales we are going to try and load
87 */
88static char new_categories[_LC_LAST][ENCODING_LEN + 1];
89static char saved_categories[_LC_LAST][ENCODING_LEN + 1];
90
91static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)];
92
93static char	*currentlocale __P((void));
94static char	*loadlocale __P((int));
95static int      stub_load_locale __P((const char *));
96
97extern int __time_load_locale __P((const char *)); /* strftime.c */
98
99#ifdef XPG4
100extern int _xpg4_setrunelocale __P((char *));
101#endif
102
103char *
104setlocale(category, locale)
105	int category;
106	const char *locale;
107{
108	int i, j, len;
109	char *env, *r;
110
111	if (category < LC_ALL || category >= _LC_LAST)
112		return (NULL);
113
114	if (!locale)
115		return (category != LC_ALL ?
116		    current_categories[category] : currentlocale());
117
118	/*
119	 * Default to the current locale for everything.
120	 */
121	for (i = 1; i < _LC_LAST; ++i)
122		(void)strcpy(new_categories[i], current_categories[i]);
123
124	/*
125	 * Now go fill up new_categories from the locale argument
126	 */
127	if (!*locale) {
128		env = getenv(categories[category]);
129
130		if (category != LC_ALL && (!env || !*env))
131			env = getenv(categories[LC_ALL]);
132
133		if (!env || !*env)
134			env = getenv("LANG");
135
136		if (!env || !*env)
137			env = "C";
138
139		(void) strncpy(new_categories[category], env, ENCODING_LEN);
140		new_categories[category][ENCODING_LEN] = '\0';
141		if (category == LC_ALL) {
142			for (i = 1; i < _LC_LAST; ++i) {
143				if (!(env = getenv(categories[i])) || !*env)
144					env = new_categories[LC_ALL];
145				(void)strncpy(new_categories[i], env, ENCODING_LEN);
146				new_categories[i][ENCODING_LEN] = '\0';
147			}
148		}
149	} else if (category != LC_ALL)  {
150		(void)strncpy(new_categories[category], locale, ENCODING_LEN);
151		new_categories[category][ENCODING_LEN] = '\0';
152	} else {
153		if ((r = strchr(locale, '/')) == NULL) {
154			for (i = 1; i < _LC_LAST; ++i) {
155				(void)strncpy(new_categories[i], locale, ENCODING_LEN);
156				new_categories[i][ENCODING_LEN] = '\0';
157			}
158		} else {
159			for (i = 1; r[1] == '/'; ++r);
160			if (!r[1])
161				return (NULL);	/* Hmm, just slashes... */
162			do {
163				len = r - locale > ENCODING_LEN ? ENCODING_LEN : r - locale;
164				(void)strncpy(new_categories[i], locale, len);
165				new_categories[i][len] = '\0';
166				i++;
167				locale = r;
168				while (*locale == '/')
169				    ++locale;
170				while (*++r && *r != '/');
171			} while (*locale);
172			while (i < _LC_LAST) {
173				(void)strcpy(new_categories[i],
174				    new_categories[i-1]);
175				i++;
176			}
177		}
178	}
179
180	if (category)
181		return (loadlocale(category));
182
183	for (i = 1; i < _LC_LAST; ++i) {
184		(void)strcpy(saved_categories[i], current_categories[i]);
185		if (loadlocale(i) == NULL) {
186			for (j = 1; j < i; j++) {
187				(void)strcpy(new_categories[j],
188				     saved_categories[j]);
189				/* XXX can fail too */
190				(void)loadlocale(j);
191			}
192			return (NULL);
193		}
194	}
195	return (currentlocale());
196}
197
198static char *
199currentlocale()
200{
201	int i;
202
203	(void)strcpy(current_locale_string, current_categories[1]);
204
205	for (i = 2; i < _LC_LAST; ++i)
206		if (strcmp(current_categories[1], current_categories[i])) {
207			for (i = 2; i < _LC_LAST; ++i) {
208				(void) strcat(current_locale_string, "/");
209				(void) strcat(current_locale_string, current_categories[i]);
210			}
211			break;
212		}
213	return (current_locale_string);
214}
215
216static char *
217loadlocale(category)
218	int category;
219{
220	char *ret;
221	char *new = new_categories[category];
222	char *old = current_categories[category];
223
224	if (_PathLocale == NULL) {
225		char *p = getenv("PATH_LOCALE");
226
227		if (p != NULL
228#ifndef __NETBSD_SYSCALLS
229			&& !issetugid()
230#endif
231			) {
232			if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
233			    1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
234				return (NULL);
235			_PathLocale = strdup(p);
236			if (_PathLocale == NULL)
237				return (NULL);
238		} else
239			_PathLocale = _PATH_LOCALE;
240	}
241
242	if (strcmp(new, old) == 0)
243		return (old);
244
245	if (category == LC_CTYPE) {
246#ifdef XPG4
247		ret = _xpg4_setrunelocale(new) ? NULL : new;
248#else
249		ret = setrunelocale(new) ? NULL : new;
250#endif
251		if (!ret) {
252#ifdef XPG4
253			(void)_xpg4_setrunelocale(old);
254#else
255			(void)setrunelocale(old);
256#endif
257		} else
258			(void)strcpy(old, new);
259		return (ret);
260	}
261
262	if (category == LC_COLLATE) {
263		ret = (__collate_load_tables(new) < 0) ? NULL : new;
264		if (!ret)
265			(void)__collate_load_tables(old);
266		else
267			(void)strcpy(old, new);
268		return (ret);
269	}
270
271	if (category == LC_TIME) {
272		ret = (__time_load_locale(new) < 0) ? NULL : new;
273		if (!ret)
274			(void)__time_load_locale(old);
275		else
276			(void)strcpy(old, new);
277		return (ret);
278	}
279
280	if (category == LC_MONETARY ||
281	    category == LC_MESSAGES ||
282	    category == LC_NUMERIC) {
283		ret = stub_load_locale(new) ? NULL : new;
284		if (!ret)
285			(void)stub_load_locale(old);
286		else
287			(void)strcpy(old, new);
288		return (ret);
289	}
290
291	/* Just in case...*/
292	return (NULL);
293}
294
295static int
296stub_load_locale(encoding)
297const char *encoding;
298{
299	char name[PATH_MAX];
300	struct stat st;
301
302	if (!encoding)
303		return(1);
304	/*
305	 * The "C" and "POSIX" locale are always here.
306	 */
307	if (!strcmp(encoding, "C") || !strcmp(encoding, "POSIX"))
308		return(0);
309	if (!_PathLocale)
310		return(1);
311	/* Range checking not needed, encoding has fixed size */
312	strcpy(name, _PathLocale);
313	strcat(name, "/");
314	strcat(name, encoding);
315#if 0
316	/*
317	 * Some day we will actually look at this file.
318	 */
319#endif
320	return (stat(name, &st) != 0 || !S_ISDIR(st.st_mode));
321}
322