setlocale.c revision 116847
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 116847 2003-06-25 22:42:33Z 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	int saved_errno;
266
267	if ((new[0] == '.' &&
268	     (new[1] == '\0' || (new[1] == '.' && new[2] == '\0'))) ||
269	    strchr(new, '/') != NULL) {
270		errno = EINVAL;
271		return (NULL);
272	}
273
274	saved_errno = errno;
275	errno = __detect_path_locale();
276	if (errno != 0)
277		return (NULL);
278	errno = saved_errno;
279
280	switch (category) {
281	case LC_CTYPE:
282		func = wrap_setrunelocale;
283		break;
284	case LC_COLLATE:
285		func = __collate_load_tables;
286		break;
287	case LC_TIME:
288		func = __time_load_locale;
289		break;
290	case LC_NUMERIC:
291		func = __numeric_load_locale;
292		break;
293	case LC_MONETARY:
294		func = __monetary_load_locale;
295		break;
296	case LC_MESSAGES:
297		func = __messages_load_locale;
298		break;
299	default:
300		errno = EINVAL;
301		return (NULL);
302	}
303
304	if (strcmp(new, old) == 0)
305		return (old);
306
307	if (func(new) != _LDP_ERROR) {
308		(void)strcpy(old, new);
309		return (old);
310	}
311
312	return (NULL);
313}
314
315/*
316 * Detect locale storage location and store its value to _PathLocale variable
317 */
318int
319__detect_path_locale(void)
320{
321	if (_PathLocale == NULL) {
322		char *p = getenv("PATH_LOCALE");
323
324		if (p != NULL
325#ifndef __NETBSD_SYSCALLS
326			&& !issetugid()
327#endif
328			) {
329			if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
330			    1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
331				return (ENAMETOOLONG);
332			_PathLocale = strdup(p);
333			if (_PathLocale == NULL)
334				return (errno == 0 ? ENOMEM : errno);
335		} else
336			_PathLocale = _PATH_LOCALE;
337	}
338	return (0);
339}
340
341