setlocale.c revision 11695
1/*
2 * Copyright (c) 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Paul Borman at Krystal Technologies.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char sccsid[] = "@(#)setlocale.c	8.1 (Berkeley) 7/4/93";
39#endif /* LIBC_SCCS and not lint */
40
41#include <limits.h>
42#include <locale.h>
43#include <rune.h>
44#include <stdlib.h>
45#include <string.h>
46#include "collate.h"
47
48/*
49 * Category names for getenv()
50 */
51static char *categories[_LC_LAST] = {
52    "LC_ALL",
53    "LC_COLLATE",
54    "LC_CTYPE",
55    "LC_MONETARY",
56    "LC_NUMERIC",
57    "LC_TIME",
58};
59
60/*
61 * Current locales for each category
62 */
63static char current_categories[_LC_LAST][32] = {
64    "C",
65    "C",
66    "C",
67    "C",
68    "C",
69    "C",
70};
71
72/*
73 * The locales we are going to try and load
74 */
75static char new_categories[_LC_LAST][32];
76
77static char current_locale_string[_LC_LAST * 33];
78char *_PathLocale;
79
80static char	*currentlocale __P((void));
81static char	*loadlocale __P((int));
82
83extern int __time_load_locale __P((const char *)); /* strftime.c */
84
85#ifdef XPG4
86extern int _xpg4_setrunelocale __P((char *));
87#endif
88
89char *
90setlocale(category, locale)
91	int category;
92	const char *locale;
93{
94	int found, i, len;
95	char *env, *r;
96
97	if (!_PathLocale && !(_PathLocale = getenv("PATH_LOCALE")))
98		_PathLocale = _PATH_LOCALE;
99
100	if (category < 0 || category >= _LC_LAST)
101		return (NULL);
102
103	if (!locale)
104		return (category ?
105		    current_categories[category] : currentlocale());
106
107	/*
108	 * Default to the current locale for everything.
109	 */
110	for (i = 1; i < _LC_LAST; ++i)
111		(void)strcpy(new_categories[i], current_categories[i]);
112
113	/*
114	 * Now go fill up new_categories from the locale argument
115	 */
116	if (!*locale) {
117		env = getenv(categories[category]);
118
119		if (!env)
120			env = getenv(categories[0]);
121
122		if (!env)
123			env = getenv("LANG");
124
125		if (!env)
126			env = "C";
127
128		(void) strncpy(new_categories[category], env, 31);
129		new_categories[category][31] = 0;
130		if (!category) {
131			for (i = 1; i < _LC_LAST; ++i) {
132				if (!(env = getenv(categories[i])))
133					env = new_categories[0];
134				(void)strncpy(new_categories[i], env, 31);
135				new_categories[i][31] = 0;
136			}
137		}
138	} else if (category)  {
139		(void)strncpy(new_categories[category], locale, 31);
140		new_categories[category][31] = 0;
141	} else {
142		if ((r = strchr(locale, '/')) == 0) {
143			for (i = 1; i < _LC_LAST; ++i) {
144				(void)strncpy(new_categories[i], locale, 31);
145				new_categories[i][31] = 0;
146			}
147		} else {
148			for (i = 1; r[1] == '/'; ++r);
149			if (!r[1])
150				return (NULL);	/* Hmm, just slashes... */
151			do {
152				len = r - locale > 31 ? 31 : r - locale;
153				(void)strncpy(new_categories[i++], locale, len);
154				new_categories[i++][len] = 0;
155				locale = r;
156				while (*locale == '/')
157				    ++locale;
158				while (*++r && *r != '/');
159			} while (*locale);
160			while (i < _LC_LAST)
161				(void)strcpy(new_categories[i],
162				    new_categories[i-1]);
163		}
164	}
165
166	if (category)
167		return (loadlocale(category));
168
169	found = 0;
170	for (i = 1; i < _LC_LAST; ++i)
171		if (loadlocale(i) != NULL)
172			found = 1;
173	if (found)
174	    return (currentlocale());
175	return (NULL);
176}
177
178#ifndef XPG4
179/* To be compatible with old binaries */
180void
181_startup_setlocale(category, locale)
182	int category;
183	const char *locale;
184{
185	(void) setlocale(category, locale);
186}
187#endif
188
189static char *
190currentlocale()
191{
192	int i, len;
193
194	(void)strcpy(current_locale_string, current_categories[1]);
195
196	for (i = 2; i < _LC_LAST; ++i)
197		if (strcmp(current_categories[1], current_categories[i])) {
198			len = strlen(current_categories[1]) + 1 +
199			      strlen(current_categories[2]) + 1 +
200			      strlen(current_categories[3]) + 1 +
201			      strlen(current_categories[4]) + 1 +
202			      strlen(current_categories[5]) + 1;
203			if (len > sizeof(current_locale_string))
204				return NULL;
205			(void) strcpy(current_locale_string, current_categories[1]);
206			(void) strcat(current_locale_string, "/");
207			(void) strcat(current_locale_string, current_categories[2]);
208			(void) strcat(current_locale_string, "/");
209			(void) strcat(current_locale_string, current_categories[3]);
210			(void) strcat(current_locale_string, "/");
211			(void) strcat(current_locale_string, current_categories[4]);
212			(void) strcat(current_locale_string, "/");
213			(void) strcat(current_locale_string, current_categories[5]);
214			break;
215		}
216	return (current_locale_string);
217}
218
219static char *
220loadlocale(category)
221	int category;
222{
223#if 0
224	char name[PATH_MAX];
225#endif
226	if (strcmp(new_categories[category],
227	    current_categories[category]) == 0)
228		return (current_categories[category]);
229
230	if (category == LC_CTYPE) {
231#ifdef XPG4
232		if (_xpg4_setrunelocale(new_categories[LC_CTYPE]))
233#else
234		if (setrunelocale(new_categories[LC_CTYPE]))
235#endif
236			return (NULL);
237		(void)strcpy(current_categories[LC_CTYPE],
238		    new_categories[LC_CTYPE]);
239		return (current_categories[LC_CTYPE]);
240	}
241
242	if (category == LC_COLLATE) {
243		if (__collate_load_tables(new_categories[LC_COLLATE]) < 0)
244			return (NULL);
245		(void)strcpy(current_categories[LC_COLLATE],
246		    new_categories[LC_COLLATE]);
247		return (current_categories[LC_COLLATE]);
248	}
249
250	if (category == LC_TIME) {
251		if (__time_load_locale(new_categories[LC_TIME]) < 0)
252			return (NULL);
253		(void)strcpy(current_categories[LC_TIME],
254		       new_categories[LC_TIME]);
255		return (current_categories[LC_TIME]);
256	}
257
258	if (!strcmp(new_categories[category], "C") ||
259		!strcmp(new_categories[category], "POSIX")) {
260
261		/*
262		 * Some day this will need to reset the locale to the default
263		 * C locale.  Since we have no way to change them as of yet,
264		 * there is no need to reset them.
265		 */
266		(void)strcpy(current_categories[category],
267		    new_categories[category]);
268		return (current_categories[category]);
269	}
270#if 0
271	/*
272	 * Some day we will actually look at this file.
273	 */
274	(void)snprintf(name, sizeof(name), "%s/%s/%s",
275	    _PathLocale, new_categories[category], categories[category]);
276#endif
277	switch (category) {
278		case LC_MONETARY:
279		case LC_NUMERIC:
280			return (NULL);
281	}
282	/* Just in case...*/
283	return (NULL);
284}
285