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