setlocale.c revision 73340
1179187Sjb/*
2179187Sjb * Copyright (c) 1996 - 2001 FreeBSD Project
3179187Sjb * Copyright (c) 1991, 1993
4179187Sjb *	The Regents of the University of California.  All rights reserved.
5179187Sjb *
6179187Sjb * This code is derived from software contributed to Berkeley by
7179187Sjb * Paul Borman at Krystal Technologies.
8179187Sjb *
9179187Sjb * Redistribution and use in source and binary forms, with or without
10179187Sjb * modification, are permitted provided that the following conditions
11179187Sjb * are met:
12179187Sjb * 1. Redistributions of source code must retain the above copyright
13179187Sjb *    notice, this list of conditions and the following disclaimer.
14179187Sjb * 2. Redistributions in binary form must reproduce the above copyright
15179187Sjb *    notice, this list of conditions and the following disclaimer in the
16179187Sjb *    documentation and/or other materials provided with the distribution.
17179187Sjb * 3. All advertising materials mentioning features or use of this software
18179187Sjb *    must display the following acknowledgement:
19179187Sjb *	This product includes software developed by the University of
20179187Sjb *	California, Berkeley and its contributors.
21179187Sjb * 4. Neither the name of the University nor the names of its contributors
22179187Sjb *    may be used to endorse or promote products derived from this software
23179187Sjb *    without specific prior written permission.
24179187Sjb *
25179187Sjb * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26179187Sjb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27179187Sjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28179187Sjb * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29179187Sjb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30179187Sjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31179187Sjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32179187Sjb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33179187Sjb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34179187Sjb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35179187Sjb * SUCH DAMAGE.
36179187Sjb *
37179187Sjb * $FreeBSD: head/lib/libc/locale/setlocale.c 73340 2001-03-02 12:45:52Z ru $
38179187Sjb */
39179187Sjb
40179187Sjb#ifdef LIBC_RCS
41179187Sjbstatic const char rcsid[] =
42179187Sjb  "$FreeBSD: head/lib/libc/locale/setlocale.c 73340 2001-03-02 12:45:52Z ru $";
43179187Sjb#endif
44179187Sjb
45179187Sjb#if defined(LIBC_SCCS) && !defined(lint)
46179187Sjbstatic char sccsid[] = "@(#)setlocale.c	8.1 (Berkeley) 7/4/93";
47179187Sjb#endif /* LIBC_SCCS and not lint */
48179187Sjb
49179187Sjb#include <sys/types.h>
50179187Sjb#include <sys/stat.h>
51179187Sjb#include <limits.h>
52179187Sjb#include <locale.h>
53179187Sjb#include <rune.h>
54179187Sjb#include <stdlib.h>
55179187Sjb#include <string.h>
56179187Sjb#include <unistd.h>
57179187Sjb#include "collate.h"
58179187Sjb#include "lmonetary.h"	/* for __monetary_load_locale() */
59179187Sjb#include "lnumeric.h"	/* for __numeric_load_locale() */
60179187Sjb#include "lmessages.h"	/* for __messages_load_locale() */
61179187Sjb#include "setlocale.h"
62179187Sjb
63179187Sjb/*
64179187Sjb * Category names for getenv()
65179187Sjb */
66179187Sjbstatic char *categories[_LC_LAST] = {
67179187Sjb    "LC_ALL",
68179187Sjb    "LC_COLLATE",
69179187Sjb    "LC_CTYPE",
70179187Sjb    "LC_MONETARY",
71179187Sjb    "LC_NUMERIC",
72179187Sjb    "LC_TIME",
73179187Sjb    "LC_MESSAGES",
74179187Sjb};
75179187Sjb
76179187Sjb/*
77179187Sjb * Current locales for each category
78179187Sjb */
79179187Sjbstatic char current_categories[_LC_LAST][ENCODING_LEN + 1] = {
80179187Sjb    "C",
81179187Sjb    "C",
82179187Sjb    "C",
83179187Sjb    "C",
84179187Sjb    "C",
85179187Sjb    "C",
86179187Sjb    "C",
87179187Sjb};
88179187Sjb
89179187Sjb/*
90179187Sjb * The locales we are going to try and load
91179187Sjb */
92179187Sjbstatic char new_categories[_LC_LAST][ENCODING_LEN + 1];
93179187Sjbstatic char saved_categories[_LC_LAST][ENCODING_LEN + 1];
94179187Sjb
95179187Sjbstatic char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)];
96179187Sjb
97179187Sjbstatic char	*currentlocale __P((void));
98179187Sjbstatic char	*loadlocale __P((int));
99
100extern int __time_load_locale __P((const char *)); /* strftime.c */
101
102char *
103setlocale(category, locale)
104	int category;
105	const char *locale;
106{
107	int i, j, len;
108	char *env, *r;
109
110	if (category < LC_ALL || category >= _LC_LAST)
111		return (NULL);
112
113	if (!locale)
114		return (category != LC_ALL ?
115		    current_categories[category] : currentlocale());
116
117	/*
118	 * Default to the current locale for everything.
119	 */
120	for (i = 1; i < _LC_LAST; ++i)
121		(void)strcpy(new_categories[i], current_categories[i]);
122
123	/*
124	 * Now go fill up new_categories from the locale argument
125	 */
126	if (!*locale) {
127		env = getenv("LC_ALL");
128
129		if (category != LC_ALL && (!env || !*env))
130			env = getenv(categories[category]);
131
132		if (!env || !*env)
133			env = getenv("LANG");
134
135		if (!env || !*env || strchr(env, '/'))
136			env = "C";
137
138		(void) strncpy(new_categories[category], env, ENCODING_LEN);
139		new_categories[category][ENCODING_LEN] = '\0';
140		if (category == LC_ALL) {
141			for (i = 1; i < _LC_LAST; ++i) {
142				if (!(env = getenv(categories[i])) || !*env)
143					env = new_categories[LC_ALL];
144				(void)strncpy(new_categories[i], env, ENCODING_LEN);
145				new_categories[i][ENCODING_LEN] = '\0';
146			}
147		}
148	} else if (category != LC_ALL)  {
149		(void)strncpy(new_categories[category], locale, ENCODING_LEN);
150		new_categories[category][ENCODING_LEN] = '\0';
151	} else {
152		if ((r = strchr(locale, '/')) == NULL) {
153			for (i = 1; i < _LC_LAST; ++i) {
154				(void)strncpy(new_categories[i], locale, ENCODING_LEN);
155				new_categories[i][ENCODING_LEN] = '\0';
156			}
157		} else {
158			for (i = 1; r[1] == '/'; ++r);
159			if (!r[1])
160				return (NULL);	/* Hmm, just slashes... */
161			do {
162				len = r - locale > ENCODING_LEN ? ENCODING_LEN : r - locale;
163				(void)strncpy(new_categories[i], locale, len);
164				new_categories[i][len] = '\0';
165				i++;
166				locale = r;
167				while (*locale == '/')
168				    ++locale;
169				while (*++r && *r != '/');
170			} while (*locale);
171			while (i < _LC_LAST) {
172				(void)strcpy(new_categories[i],
173				    new_categories[i-1]);
174				i++;
175			}
176		}
177	}
178
179	if (category != LC_ALL)
180		return (loadlocale(category));
181
182	for (i = 1; i < _LC_LAST; ++i) {
183		(void)strcpy(saved_categories[i], current_categories[i]);
184		if (loadlocale(i) == NULL) {
185			for (j = 1; j < i; j++) {
186				(void)strcpy(new_categories[j],
187				     saved_categories[j]);
188				/* XXX can fail too */
189				(void)loadlocale(j);
190			}
191			return (NULL);
192		}
193	}
194	return (currentlocale());
195}
196
197static char *
198currentlocale()
199{
200	int i;
201
202	(void)strcpy(current_locale_string, current_categories[1]);
203
204	for (i = 2; i < _LC_LAST; ++i)
205		if (strcmp(current_categories[1], current_categories[i])) {
206			for (i = 2; i < _LC_LAST; ++i) {
207				(void) strcat(current_locale_string, "/");
208				(void) strcat(current_locale_string, current_categories[i]);
209			}
210			break;
211		}
212	return (current_locale_string);
213}
214
215static char *
216loadlocale(category)
217	int category;
218{
219	char *ret;
220	char *new = new_categories[category];
221	char *old = current_categories[category];
222
223	if (_PathLocale == NULL) {
224		char *p = getenv("PATH_LOCALE");
225
226		if (p != NULL
227#ifndef __NETBSD_SYSCALLS
228			&& !issetugid()
229#endif
230			) {
231			if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
232			    1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
233				return (NULL);
234			_PathLocale = strdup(p);
235			if (_PathLocale == NULL)
236				return (NULL);
237		} else
238			_PathLocale = _PATH_LOCALE;
239	}
240
241	if (strcmp(new, old) == 0)
242		return (old);
243
244	if (category == LC_CTYPE) {
245		ret = setrunelocale(new) ? NULL : new;
246		if (!ret)
247			(void)setrunelocale(old);
248		else
249			(void)strcpy(old, new);
250		return (ret);
251	}
252
253#define LOAD_CATEGORY(CAT, FUNC)			\
254	if (category == CAT) {				\
255		ret = (FUNC(new) < 0) ? NULL : new;	\
256		if (!ret)				\
257			(void)FUNC(old);		\
258		else					\
259			(void)strcpy(old, new);		\
260		return (ret);				\
261	}
262
263	LOAD_CATEGORY(LC_COLLATE, __collate_load_tables);
264	LOAD_CATEGORY(LC_TIME, __time_load_locale);
265	LOAD_CATEGORY(LC_NUMERIC, __numeric_load_locale);
266	LOAD_CATEGORY(LC_MONETARY, __monetary_load_locale);
267	LOAD_CATEGORY(LC_MESSAGES, __messages_load_locale);
268
269	/* Just in case...*/
270	return (NULL);
271}
272
273