xref: /illumos-gate/usr/src/lib/libc/port/locale/setlocale.c (revision 6da5aa940d1e163c6f244b5523c22d545bfb954b)
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  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 /*
35  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
36  */
37 
38 #include "lint.h"
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <errno.h>
42 #include <limits.h>
43 #include <locale.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <alloca.h>
48 #include "collate.h"
49 #include "lmonetary.h"	/* for __monetary_load_locale() */
50 #include "lnumeric.h"	/* for __numeric_load_locale() */
51 #include "lmessages.h"	/* for __messages_load_locale() */
52 #include "setlocale.h"
53 #include "ldpart.h"
54 #include "timelocal.h" /* for __time_load_locale() */
55 #include "../i18n/_loc_path.h"
56 
57 #define	NUM_CATS	7
58 /*
59  * Category names for getenv()  Note that this was modified
60  * for Solaris.  See <iso/locale_iso.h>.
61  */
62 static char *categories[NUM_CATS] = {
63 	"LC_CTYPE",
64 	"LC_NUMERIC",
65 	"LC_TIME",
66 	"LC_COLLATE",
67 	"LC_MONETARY",
68 	"LC_MESSAGES",
69 	"LC_ALL",
70 };
71 
72 /*
73  * Current locales for each category
74  */
75 static char current_categories[NUM_CATS][ENCODING_LEN + 1] = {
76 	"C",
77 	"C",
78 	"C",
79 	"C",
80 	"C",
81 	"C",
82 	"C",
83 };
84 
85 /*
86  * Path to locale storage directory.  See ../i18n/_loc_path.h
87  */
88 char	*_PathLocale = _DFLT_LOC_PATH;
89 
90 /*
91  * The locales we are going to try and load
92  */
93 static char new_categories[NUM_CATS][ENCODING_LEN + 1];
94 static char saved_categories[NUM_CATS][ENCODING_LEN + 1];
95 static char current_locale_string[NUM_CATS * (ENCODING_LEN + 1 + 1)];
96 
97 static char	*currentlocale(void);
98 static char	*loadlocale(int);
99 static const char *__get_locale_env(int);
100 
101 char *
102 setlocale(int category, const char *locale)
103 {
104 	int i, j, saverr;
105 	const char *env, *r;
106 
107 	if (category < 0 || category >= NUM_CATS) {
108 		errno = EINVAL;
109 		return (NULL);
110 	}
111 
112 	if (locale == NULL)
113 		return (category != LC_ALL ?
114 		    current_categories[category] : currentlocale());
115 
116 	/*
117 	 * Default to the current locale for everything.
118 	 */
119 	for (i = 0; i < NUM_CATS; ++i)
120 		(void) strcpy(new_categories[i], current_categories[i]);
121 
122 	/*
123 	 * Now go fill up new_categories from the locale argument
124 	 */
125 	if (!*locale) {
126 		if (category == LC_ALL) {
127 			for (i = 0; i < NUM_CATS; ++i) {
128 				env = __get_locale_env(i);
129 				if (strlen(env) > ENCODING_LEN) {
130 					errno = EINVAL;
131 					return (NULL);
132 				}
133 				(void) strcpy(new_categories[i], env);
134 			}
135 		} else {
136 			env = __get_locale_env(category);
137 			if (strlen(env) > ENCODING_LEN) {
138 				errno = EINVAL;
139 				return (NULL);
140 			}
141 			(void) strcpy(new_categories[category], env);
142 		}
143 	} else if (category != LC_ALL) {
144 		if (strlen(locale) > ENCODING_LEN) {
145 			errno = EINVAL;
146 			return (NULL);
147 		}
148 		(void) strcpy(new_categories[category], locale);
149 	} else {
150 		if ((r = strchr(locale, '/')) == NULL) {
151 			if (strlen(locale) > ENCODING_LEN) {
152 				errno = EINVAL;
153 				return (NULL);
154 			}
155 			for (i = 1; i < NUM_CATS; ++i)
156 				(void) strcpy(new_categories[i], locale);
157 		} else {
158 			char	*buf;
159 			char	*save;
160 
161 			buf = alloca(strlen(locale) + 1);
162 
163 			for (i = 0, save = NULL; i <= LC_ALL; i++) {
164 				r = strtok_r(buf, "/", &save);
165 				if (r == NULL) {
166 					if (i == LC_ALL) {
167 						/* Good!  Fully specified! */
168 						break;
169 					}
170 					/*
171 					 * Composite Locale is inadequately
172 					 * specified!   (Or with empty fields.)
173 					 * The old code would fill fields
174 					 * out from the last one, but I think
175 					 * this is suboptimal.
176 					 */
177 					errno = EINVAL;
178 					return (NULL);
179 				}
180 				if (i == LC_ALL) {
181 					/* Too many components */
182 					errno = EINVAL;
183 					return (NULL);
184 				}
185 				(void) strlcpy(new_categories[i], r,
186 				    ENCODING_LEN);
187 				buf = NULL;	/* for strtok's benefit */
188 			}
189 		}
190 	}
191 
192 	if (category != LC_ALL)
193 		return (loadlocale(category));
194 
195 	for (i = 0; i < LC_ALL; ++i) {
196 		(void) strcpy(saved_categories[i], current_categories[i]);
197 		if (loadlocale(i) == NULL) {
198 			saverr = errno;
199 			for (j = 1; j < i; j++) {
200 				(void) strcpy(new_categories[j],
201 				    saved_categories[j]);
202 				if (loadlocale(j) == NULL) {
203 					(void) strcpy(new_categories[j], "C");
204 					(void) loadlocale(j);
205 				}
206 			}
207 			errno = saverr;
208 			return (NULL);
209 		}
210 	}
211 	return (currentlocale());
212 }
213 
214 static char *
215 currentlocale(void)
216 {
217 	int i;
218 
219 	(void) strcpy(current_locale_string, current_categories[0]);
220 
221 	for (i = 1; i < LC_ALL; ++i)
222 		if (strcmp(current_categories[1], current_categories[i])) {
223 			for (i = 1; i < LC_ALL; ++i) {
224 				(void) strcat(current_locale_string, "/");
225 				(void) strcat(current_locale_string,
226 				    current_categories[i]);
227 			}
228 			break;
229 		}
230 	return (current_locale_string);
231 }
232 
233 static char *
234 loadlocale(int category)
235 {
236 	char *new = new_categories[category];
237 	char *old = current_categories[category];
238 	int (*func)(const char *);
239 
240 	if ((new[0] == '.' &&
241 	    (new[1] == '\0' || (new[1] == '.' && new[2] == '\0'))) ||
242 	    strchr(new, '/') != NULL) {
243 		errno = EINVAL;
244 		return (NULL);
245 	}
246 
247 	switch (category) {
248 	case LC_CTYPE:
249 		func = __wrap_setrunelocale;
250 		break;
251 	case LC_COLLATE:
252 		func = __collate_load_tables;
253 		break;
254 	case LC_TIME:
255 		func = __time_load_locale;
256 		break;
257 	case LC_NUMERIC:
258 		func = __numeric_load_locale;
259 		break;
260 	case LC_MONETARY:
261 		func = __monetary_load_locale;
262 		break;
263 	case LC_MESSAGES:
264 		func = __messages_load_locale;
265 		break;
266 	default:
267 		errno = EINVAL;
268 		return (NULL);
269 	}
270 
271 	if (strcmp(new, old) == 0)
272 		return (old);
273 
274 	if (func(new) != _LDP_ERROR) {
275 		(void) strcpy(old, new);
276 		return (old);
277 	}
278 
279 	return (NULL);
280 }
281 
282 static const char *
283 __get_locale_env(int category)
284 {
285 	const char *env;
286 
287 	/* 1. check LC_ALL. */
288 	env = getenv(categories[0]);
289 
290 	/* 2. check LC_* */
291 	if (env == NULL || !*env)
292 		env = getenv(categories[category]);
293 
294 	/* 3. check LANG */
295 	if (env == NULL || !*env)
296 		env = getenv("LANG");
297 
298 	/* 4. if none is set, fall to "C" */
299 	if (env == NULL || !*env)
300 		env = "C";
301 
302 	return (env);
303 }
304