setlocale.c revision 92905
133965Sjdp/*
233965Sjdp * Copyright (c) 1996 - 2002 FreeBSD Project
333965Sjdp * Copyright (c) 1991, 1993
433965Sjdp *	The Regents of the University of California.  All rights reserved.
533965Sjdp *
633965Sjdp * This code is derived from software contributed to Berkeley by
733965Sjdp * Paul Borman at Krystal Technologies.
833965Sjdp *
933965Sjdp * Redistribution and use in source and binary forms, with or without
1033965Sjdp * modification, are permitted provided that the following conditions
1133965Sjdp * are met:
1233965Sjdp * 1. Redistributions of source code must retain the above copyright
1333965Sjdp *    notice, this list of conditions and the following disclaimer.
1433965Sjdp * 2. Redistributions in binary form must reproduce the above copyright
1533965Sjdp *    notice, this list of conditions and the following disclaimer in the
1633965Sjdp *    documentation and/or other materials provided with the distribution.
1733965Sjdp * 3. All advertising materials mentioning features or use of this software
1838889Sjdp *    must display the following acknowledgement:
1933965Sjdp *	This product includes software developed by the University of
2033965Sjdp *	California, Berkeley and its contributors.
2133965Sjdp * 4. Neither the name of the University nor the names of its contributors
2238889Sjdp *    may be used to endorse or promote products derived from this software
2333965Sjdp *    without specific prior written permission.
2433965Sjdp *
2533965Sjdp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2660484Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2733965Sjdp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2833965Sjdp * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2933965Sjdp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3077298Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3177298Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3233965Sjdp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3333965Sjdp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3433965Sjdp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3533965Sjdp * SUCH DAMAGE.
3633965Sjdp */
3733965Sjdp
3833965Sjdp#ifdef LIBC_RCS
3933965Sjdpstatic const char rcsid[] =
4033965Sjdp  "$FreeBSD: head/lib/libc/locale/setlocale.c 92905 2002-03-21 22:49:10Z obrien $";
4133965Sjdp#endif
4233965Sjdp
4333965Sjdp#if defined(LIBC_SCCS) && !defined(lint)
4433965Sjdpstatic char sccsid[] = "@(#)setlocale.c	8.1 (Berkeley) 7/4/93";
4533965Sjdp#endif /* LIBC_SCCS and not lint */
4633965Sjdp
4733965Sjdp#include <sys/types.h>
4833965Sjdp#include <sys/stat.h>
4933965Sjdp#include <limits.h>
5033965Sjdp#include <locale.h>
5177298Sobrien#include <rune.h>
5277298Sobrien#include <stdlib.h>
5377298Sobrien#include <string.h>
5477298Sobrien#include <unistd.h>
5533965Sjdp#include "collate.h"
5633965Sjdp#include "lmonetary.h"	/* for __monetary_load_locale() */
5733965Sjdp#include "lnumeric.h"	/* for __numeric_load_locale() */
5833965Sjdp#include "lmessages.h"	/* for __messages_load_locale() */
5933965Sjdp#include "setlocale.h"
6033965Sjdp#include "../stdtime/timelocal.h" /* for __time_load_locale() */
6133965Sjdp
6233965Sjdp/*
6333965Sjdp * Category names for getenv()
6433965Sjdp */
6533965Sjdpstatic char *categories[_LC_LAST] = {
6633965Sjdp    "LC_ALL",
6733965Sjdp    "LC_COLLATE",
6833965Sjdp    "LC_CTYPE",
6933965Sjdp    "LC_MONETARY",
7033965Sjdp    "LC_NUMERIC",
7133965Sjdp    "LC_TIME",
7260484Sobrien    "LC_MESSAGES",
7360484Sobrien};
7433965Sjdp
7533965Sjdp/*
7633965Sjdp * Current locales for each category
7733965Sjdp */
7833965Sjdpstatic char current_categories[_LC_LAST][ENCODING_LEN + 1] = {
79    "C",
80    "C",
81    "C",
82    "C",
83    "C",
84    "C",
85    "C",
86};
87
88/*
89 * The locales we are going to try and load
90 */
91static char new_categories[_LC_LAST][ENCODING_LEN + 1];
92static char saved_categories[_LC_LAST][ENCODING_LEN + 1];
93
94static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)];
95
96static char	*currentlocale(void);
97static char	*loadlocale(int);
98
99char *
100setlocale(category, locale)
101	int category;
102	const char *locale;
103{
104	int i, j, len;
105	char *env, *r;
106
107	if (category < LC_ALL || category >= _LC_LAST)
108		return (NULL);
109
110	if (!locale)
111		return (category != LC_ALL ?
112		    current_categories[category] : currentlocale());
113
114	/*
115	 * Default to the current locale for everything.
116	 */
117	for (i = 1; i < _LC_LAST; ++i)
118		(void)strcpy(new_categories[i], current_categories[i]);
119
120	/*
121	 * Now go fill up new_categories from the locale argument
122	 */
123	if (!*locale) {
124		env = getenv("LC_ALL");
125
126		if (category != LC_ALL && (!env || !*env))
127			env = getenv(categories[category]);
128
129		if (!env || !*env)
130			env = getenv("LANG");
131
132		if (!env || !*env || strchr(env, '/'))
133			env = "C";
134
135		(void) strncpy(new_categories[category], env, ENCODING_LEN);
136		new_categories[category][ENCODING_LEN] = '\0';
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)strncpy(new_categories[i], env, ENCODING_LEN);
142				new_categories[i][ENCODING_LEN] = '\0';
143			}
144		}
145	} else if (category != LC_ALL)  {
146		(void)strncpy(new_categories[category], locale, ENCODING_LEN);
147		new_categories[category][ENCODING_LEN] = '\0';
148	} else {
149		if ((r = strchr(locale, '/')) == NULL) {
150			for (i = 1; i < _LC_LAST; ++i) {
151				(void)strncpy(new_categories[i], locale, ENCODING_LEN);
152				new_categories[i][ENCODING_LEN] = '\0';
153			}
154		} else {
155			for (i = 1; r[1] == '/'; ++r);
156			if (!r[1])
157				return (NULL);	/* Hmm, just slashes... */
158			do {
159				len = r - locale > ENCODING_LEN ? ENCODING_LEN : r - locale;
160				(void)strncpy(new_categories[i], locale, len);
161				new_categories[i][len] = '\0';
162				i++;
163				locale = r;
164				while (*locale == '/')
165				    ++locale;
166				while (*++r && *r != '/');
167			} while (*locale);
168			while (i < _LC_LAST) {
169				(void)strcpy(new_categories[i],
170				    new_categories[i-1]);
171				i++;
172			}
173		}
174	}
175
176	if (category != LC_ALL)
177		return (loadlocale(category));
178
179	for (i = 1; i < _LC_LAST; ++i) {
180		(void)strcpy(saved_categories[i], current_categories[i]);
181		if (loadlocale(i) == NULL) {
182			for (j = 1; j < i; j++) {
183				(void)strcpy(new_categories[j],
184				     saved_categories[j]);
185				/* XXX can fail too */
186				(void)loadlocale(j);
187			}
188			return (NULL);
189		}
190	}
191	return (currentlocale());
192}
193
194static char *
195currentlocale()
196{
197	int i;
198
199	(void)strcpy(current_locale_string, current_categories[1]);
200
201	for (i = 2; i < _LC_LAST; ++i)
202		if (strcmp(current_categories[1], current_categories[i])) {
203			for (i = 2; i < _LC_LAST; ++i) {
204				(void) strcat(current_locale_string, "/");
205				(void) strcat(current_locale_string, current_categories[i]);
206			}
207			break;
208		}
209	return (current_locale_string);
210}
211
212static char *
213loadlocale(category)
214	int category;
215{
216	char *ret;
217	char *new = new_categories[category];
218	char *old = current_categories[category];
219
220	if (_PathLocale == NULL) {
221		char *p = getenv("PATH_LOCALE");
222
223		if (p != NULL
224#ifndef __NETBSD_SYSCALLS
225			&& !issetugid()
226#endif
227			) {
228			if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
229			    1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
230				return (NULL);
231			_PathLocale = strdup(p);
232			if (_PathLocale == NULL)
233				return (NULL);
234		} else
235			_PathLocale = _PATH_LOCALE;
236	}
237
238	if (strcmp(new, old) == 0)
239		return (old);
240
241	if (category == LC_CTYPE) {
242		ret = setrunelocale(new) ? NULL : new;
243		if (!ret)
244			(void)setrunelocale(old);
245		else
246			(void)strcpy(old, new);
247		return (ret);
248	}
249
250#define LOAD_CATEGORY(CAT, FUNC)			\
251	if (category == CAT) {				\
252		ret = (FUNC(new) < 0) ? NULL : new;	\
253		if (!ret)				\
254			(void)FUNC(old);		\
255		else					\
256			(void)strcpy(old, new);		\
257		return (ret);				\
258	}
259
260	LOAD_CATEGORY(LC_COLLATE, __collate_load_tables);
261	LOAD_CATEGORY(LC_TIME, __time_load_locale);
262	LOAD_CATEGORY(LC_NUMERIC, __numeric_load_locale);
263	LOAD_CATEGORY(LC_MONETARY, __monetary_load_locale);
264	LOAD_CATEGORY(LC_MESSAGES, __messages_load_locale);
265
266	/* Just in case...*/
267	return (NULL);
268}
269
270