xref: /freebsd/lib/libc/locale/setlocale.c (revision afe61c15161c324a7af299a9b8457aba5afc92db)
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)
38 static 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 
47 /*
48  * Category names for getenv()
49  */
50 static char *categories[_LC_LAST] = {
51     "LC_ALL",
52     "LC_COLLATE",
53     "LC_CTYPE",
54     "LC_MONETARY",
55     "LC_NUMERIC",
56     "LC_TIME",
57 };
58 
59 /*
60  * Current locales for each category
61  */
62 static char current_categories[_LC_LAST][32] = {
63     "C",
64     "C",
65     "C",
66     "C",
67     "C",
68     "C",
69 };
70 
71 /*
72  * The locales we are going to try and load
73  */
74 static char new_categories[_LC_LAST][32];
75 
76 static char current_locale_string[_LC_LAST * 33];
77 static char *PathLocale;
78 
79 static char	*currentlocale __P((void));
80 static char	*loadlocale __P((int));
81 
82 char *
83 setlocale(category, locale)
84 	int category;
85 	const char *locale;
86 {
87 	int found, i, len;
88 	char *env, *r;
89 
90 	if (!PathLocale && !(PathLocale = getenv("PATH_LOCALE")))
91 		PathLocale = _PATH_LOCALE;
92 
93 	if (category < 0 || category >= _LC_LAST)
94 		return (NULL);
95 
96 	if (!locale)
97 		return (category ?
98 		    current_categories[category] : currentlocale());
99 
100 	/*
101 	 * Default to the current locale for everything.
102 	 */
103 	for (i = 1; i < _LC_LAST; ++i)
104 		(void)strcpy(new_categories[i], current_categories[i]);
105 
106 	/*
107 	 * Now go fill up new_categories from the locale argument
108 	 */
109 	if (!*locale) {
110 		env = getenv(categories[category]);
111 
112 		if (!env)
113 			env = getenv(categories[0]);
114 
115 		if (!env)
116 			env = getenv("LANG");
117 
118 		if (!env)
119 			env = "C";
120 
121 		(void) strncpy(new_categories[category], env, 31);
122 		new_categories[category][31] = 0;
123 		if (!category) {
124 			for (i = 1; i < _LC_LAST; ++i) {
125 				if (!(env = getenv(categories[i])))
126 					env = new_categories[0];
127 				(void)strncpy(new_categories[i], env, 31);
128 				new_categories[i][31] = 0;
129 			}
130 		}
131 	} else if (category)  {
132 		(void)strncpy(new_categories[category], locale, 31);
133 		new_categories[category][31] = 0;
134 	} else {
135 		if ((r = strchr(locale, '/')) == 0) {
136 			for (i = 1; i < _LC_LAST; ++i) {
137 				(void)strncpy(new_categories[i], locale, 31);
138 				new_categories[i][31] = 0;
139 			}
140 		} else {
141 			for (i = 1; r[1] == '/'; ++r);
142 			if (!r[1])
143 				return (NULL);	/* Hmm, just slashes... */
144 			do {
145 				len = r - locale > 31 ? 31 : r - locale;
146 				(void)strncpy(new_categories[i++], locale, len);
147 				new_categories[i++][len] = 0;
148 				locale = r;
149 				while (*locale == '/')
150 				    ++locale;
151 				while (*++r && *r != '/');
152 			} while (*locale);
153 			while (i < _LC_LAST)
154 				(void)strcpy(new_categories[i],
155 				    new_categories[i-1]);
156 		}
157 	}
158 
159 	if (category)
160 		return (loadlocale(category));
161 
162 	found = 0;
163 	for (i = 1; i < _LC_LAST; ++i)
164 		if (loadlocale(i) != NULL)
165 			found = 1;
166 	if (found)
167 	    return (currentlocale());
168 	return (NULL);
169 }
170 
171 static char *
172 currentlocale()
173 {
174 	int i;
175 
176 	(void)strcpy(current_locale_string, current_categories[1]);
177 
178 	for (i = 2; i < _LC_LAST; ++i)
179 		if (strcmp(current_categories[1], current_categories[i])) {
180 			(void)snprintf(current_locale_string,
181 			    sizeof(current_locale_string), "%s/%s/%s/%s/%s",
182 			    current_categories[1], current_categories[2],
183 			    current_categories[3], current_categories[4],
184 			    current_categories[5]);
185 			break;
186 		}
187 	return (current_locale_string);
188 }
189 
190 static char *
191 loadlocale(category)
192 	int category;
193 {
194 	char name[PATH_MAX];
195 
196 	if (strcmp(new_categories[category],
197 	    current_categories[category]) == 0)
198 		return (current_categories[category]);
199 
200 	if (category == LC_CTYPE) {
201 		if (setrunelocale(new_categories[LC_CTYPE]))
202 			return (NULL);
203 		(void)strcpy(current_categories[LC_CTYPE],
204 		    new_categories[LC_CTYPE]);
205 		return (current_categories[LC_CTYPE]);
206 	}
207 
208 	if (!strcmp(new_categories[category], "C") ||
209 		!strcmp(new_categories[category], "POSIX")) {
210 
211 		/*
212 		 * Some day this will need to reset the locale to the default
213 		 * C locale.  Since we have no way to change them as of yet,
214 		 * there is no need to reset them.
215 		 */
216 		(void)strcpy(current_categories[category],
217 		    new_categories[category]);
218 		return (current_categories[category]);
219 	}
220 
221 	/*
222 	 * Some day we will actually look at this file.
223 	 */
224 	(void)snprintf(name, sizeof(name), "%s/%s/%s",
225 	    PathLocale, new_categories[category], categories[category]);
226 
227 	switch (category) {
228 		case LC_COLLATE:
229 		case LC_MONETARY:
230 		case LC_NUMERIC:
231 		case LC_TIME:
232 			return (NULL);
233 	}
234 }
235