setlocale.c revision 101259
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)
39static 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: head/lib/libc/locale/setlocale.c 101259 2002-08-03 09:04:44Z ache $");
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 <rune.h>
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 "../stdtime/timelocal.h" /* for __time_load_locale() */
59
60/*
61 * Category names for getenv()
62 */
63static char *categories[_LC_LAST] = {
64    "LC_ALL",
65    "LC_COLLATE",
66    "LC_CTYPE",
67    "LC_MONETARY",
68    "LC_NUMERIC",
69    "LC_TIME",
70    "LC_MESSAGES",
71};
72
73/*
74 * Current locales for each category
75 */
76static char current_categories[_LC_LAST][ENCODING_LEN + 1] = {
77    "C",
78    "C",
79    "C",
80    "C",
81    "C",
82    "C",
83    "C",
84};
85
86/*
87 * The locales we are going to try and load
88 */
89static char new_categories[_LC_LAST][ENCODING_LEN + 1];
90static char saved_categories[_LC_LAST][ENCODING_LEN + 1];
91
92static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)];
93
94static char	*currentlocale(void);
95static int      wrap_setrunelocale(char *);
96static char	*loadlocale(int);
97
98char *
99setlocale(category, locale)
100	int category;
101	const char *locale;
102{
103	int i, j, len;
104	char *env, *r;
105
106	if (category < LC_ALL || category >= _LC_LAST) {
107		errno = EINVAL;
108		return (NULL);
109	}
110
111	if (!locale)
112		return (category != LC_ALL ?
113		    current_categories[category] : currentlocale());
114
115	/*
116	 * Default to the current locale for everything.
117	 */
118	for (i = 1; i < _LC_LAST; ++i)
119		(void)strcpy(new_categories[i], current_categories[i]);
120
121	/*
122	 * Now go fill up new_categories from the locale argument
123	 */
124	if (!*locale) {
125		env = getenv("LC_ALL");
126
127		if (category != LC_ALL && (!env || !*env))
128			env = getenv(categories[category]);
129
130		if (!env || !*env)
131			env = getenv("LANG");
132
133		if (!env || !*env)
134			env = "C";
135
136		(void)strlcpy(new_categories[category], env, ENCODING_LEN + 1);
137		if (category == LC_ALL) {
138			for (i = 1; i < _LC_LAST; ++i) {
139				if (!(env = getenv(categories[i])) || !*env)
140					env = new_categories[LC_ALL];
141				(void)strlcpy(new_categories[i], env,
142					      ENCODING_LEN + 1);
143			}
144		}
145	} else if (category != LC_ALL)
146		(void)strlcpy(new_categories[category], locale,
147			      ENCODING_LEN + 1);
148	else {
149		if ((r = strchr(locale, '/')) == NULL) {
150			for (i = 1; i < _LC_LAST; ++i)
151				(void)strlcpy(new_categories[i], locale,
152					      ENCODING_LEN + 1);
153		} else {
154			for (i = 1; r[1] == '/'; ++r)
155				;
156			if (!r[1]) {
157				errno = EINVAL;
158				return (NULL);	/* Hmm, just slashes... */
159			}
160			do {
161				if (i == _LC_LAST)
162					break;  /* Too many slashes... */
163				len = r - locale > ENCODING_LEN ?
164				      ENCODING_LEN : r - locale;
165				(void)strlcpy(new_categories[i], locale,
166					      len + 1);
167				i++;
168				locale = r;
169				while (*locale == '/')
170					++locale;
171				while (*++r && *r != '/')
172					;
173			} while (*locale);
174			while (i < _LC_LAST) {
175				(void)strcpy(new_categories[i],
176					     new_categories[i-1]);
177				i++;
178			}
179		}
180	}
181
182	if (category != LC_ALL)
183		return (loadlocale(category));
184
185	for (i = 1; i < _LC_LAST; ++i) {
186		(void)strcpy(saved_categories[i], current_categories[i]);
187		if (loadlocale(i) == NULL) {
188			int saverr = errno;
189
190			for (j = 1; j < i; j++) {
191				(void)strcpy(new_categories[j],
192					     saved_categories[j]);
193				(void)loadlocale(j);
194			}
195			errno = saverr;
196			return (NULL);
197		}
198	}
199	return (currentlocale());
200}
201
202static char *
203currentlocale()
204{
205	int i;
206
207	(void)strcpy(current_locale_string, current_categories[1]);
208
209	for (i = 2; i < _LC_LAST; ++i)
210		if (strcmp(current_categories[1], current_categories[i])) {
211			for (i = 2; i < _LC_LAST; ++i) {
212				(void)strcat(current_locale_string, "/");
213				(void)strcat(current_locale_string,
214					     current_categories[i]);
215			}
216			break;
217		}
218	return (current_locale_string);
219}
220
221
222static int
223wrap_setrunelocale(locale)
224	char *locale;
225{
226	int ret = setrunelocale(locale);
227
228	if (ret != 0) {
229		errno = ret;
230		return (-1);
231	}
232	return (0);
233}
234
235static char *
236loadlocale(category)
237	int category;
238{
239	char *ret;
240	char *new = new_categories[category];
241	char *old = current_categories[category];
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	if (_PathLocale == NULL) {
251		char *p = getenv("PATH_LOCALE");
252
253		if (p != NULL
254#ifndef __NETBSD_SYSCALLS
255			&& !issetugid()
256#endif
257			) {
258			if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
259			    1/*"/"*/ + CATEGORY_LEN >= PATH_MAX) {
260				errno = ENAMETOOLONG;
261				return (NULL);
262			}
263			_PathLocale = strdup(p);
264			if (_PathLocale == NULL)
265				return (NULL);
266		} else
267			_PathLocale = _PATH_LOCALE;
268	}
269
270#define LOAD_CATEGORY(FUNC)                                   \
271	{                                                     \
272		if (strcmp(new, old) == 0)                    \
273			return (old);                         \
274		ret = FUNC(new) != 0 ? NULL : new;            \
275		if (ret == NULL) {                            \
276			if (FUNC(old) != 0 && FUNC("C") == 0) \
277				(void)strcpy(old, "C");       \
278		} else                                        \
279			(void)strcpy(old, new);               \
280		return (ret);                                 \
281	}
282
283	switch (category) {
284	case LC_CTYPE:
285		LOAD_CATEGORY(wrap_setrunelocale);
286		/* NOTREACHED */
287	case LC_COLLATE:
288		LOAD_CATEGORY(__collate_load_tables);
289		/* NOTREACHED */
290	case LC_TIME:
291		LOAD_CATEGORY(__time_load_locale);
292		/* NOTREACHED */
293	case LC_NUMERIC:
294		LOAD_CATEGORY(__numeric_load_locale);
295		/* NOTREACHED */
296	case LC_MONETARY:
297		LOAD_CATEGORY(__monetary_load_locale);
298		/* NOTREACHED */
299	case LC_MESSAGES:
300		LOAD_CATEGORY(__messages_load_locale);
301		/* NOTREACHED */
302	default:
303		errno = EINVAL;
304		return (NULL);
305	}
306}
307
308