xref: /freebsd/lib/libc/locale/setlocale.c (revision a316b26e50bbed7cf655fbba726ab87d8ab7599d)
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 #include "common_setlocale.h"
47 #include "common_rune.h"
48 
49 char *_PathLocale;
50 
51 static char	*loadlocale __P((int));
52 
53 char *
54 setlocale(category, locale)
55 	int category;
56 	const char *locale;
57 {
58 	int found, i, len;
59 	char *env, *r;
60 
61 	if (!PathLocale && !(PathLocale = getenv("PATH_LOCALE")))
62 		PathLocale = _PATH_LOCALE;
63 
64 	if (category < 0 || category >= _LC_LAST)
65 		return (NULL);
66 
67 	if (!locale)
68 		return (category ?
69 		    current_categories[category] : currentlocale());
70 
71 	/*
72 	 * Default to the current locale for everything.
73 	 */
74 	for (i = 1; i < _LC_LAST; ++i)
75 		(void)strcpy(new_categories[i], current_categories[i]);
76 
77 	/*
78 	 * Now go fill up new_categories from the locale argument
79 	 */
80 	if (!*locale) {
81 		env = getenv(categories[category]);
82 
83 		if (!env)
84 			env = getenv(categories[0]);
85 
86 		if (!env)
87 			env = getenv("LANG");
88 
89 		if (!env)
90 			env = "C";
91 
92 		(void) strncpy(new_categories[category], env, 31);
93 		new_categories[category][31] = 0;
94 		if (!category) {
95 			for (i = 1; i < _LC_LAST; ++i) {
96 				if (!(env = getenv(categories[i])))
97 					env = new_categories[0];
98 				(void)strncpy(new_categories[i], env, 31);
99 				new_categories[i][31] = 0;
100 			}
101 		}
102 	} else if (category)  {
103 		(void)strncpy(new_categories[category], locale, 31);
104 		new_categories[category][31] = 0;
105 	} else {
106 		if ((r = strchr(locale, '/')) == 0) {
107 			for (i = 1; i < _LC_LAST; ++i) {
108 				(void)strncpy(new_categories[i], locale, 31);
109 				new_categories[i][31] = 0;
110 			}
111 		} else {
112 			for (i = 1; r[1] == '/'; ++r);
113 			if (!r[1])
114 				return (NULL);	/* Hmm, just slashes... */
115 			do {
116 				len = r - locale > 31 ? 31 : r - locale;
117 				(void)strncpy(new_categories[i++], locale, len);
118 				new_categories[i++][len] = 0;
119 				locale = r;
120 				while (*locale == '/')
121 				    ++locale;
122 				while (*++r && *r != '/');
123 			} while (*locale);
124 			while (i < _LC_LAST)
125 				(void)strcpy(new_categories[i],
126 				    new_categories[i-1]);
127 		}
128 	}
129 
130 	if (category)
131 		return (loadlocale(category));
132 
133 	found = 0;
134 	for (i = 1; i < _LC_LAST; ++i)
135 		if (loadlocale(i) != NULL)
136 			found = 1;
137 	if (found)
138 	    return (currentlocale());
139 	return (NULL);
140 }
141 
142 static char *
143 loadlocale(category)
144 	int category;
145 {
146 #if 0
147 	char name[PATH_MAX];
148 #endif
149 	if (strcmp(new_categories[category],
150 	    current_categories[category]) == 0)
151 		return (current_categories[category]);
152 
153 	if (category == LC_CTYPE) {
154 		if (setrunelocale(new_categories[LC_CTYPE]))
155 			return (NULL);
156 		(void)strcpy(current_categories[LC_CTYPE],
157 		    new_categories[LC_CTYPE]);
158 		return (current_categories[LC_CTYPE]);
159 	}
160 
161 	if (!strcmp(new_categories[category], "C") ||
162 		!strcmp(new_categories[category], "POSIX")) {
163 
164 		/*
165 		 * Some day this will need to reset the locale to the default
166 		 * C locale.  Since we have no way to change them as of yet,
167 		 * there is no need to reset them.
168 		 */
169 		(void)strcpy(current_categories[category],
170 		    new_categories[category]);
171 		return (current_categories[category]);
172 	}
173 #if 0
174 	/*
175 	 * Some day we will actually look at this file.
176 	 */
177 	(void)snprintf(name, sizeof(name), "%s/%s/%s",
178 	    PathLocale, new_categories[category], categories[category]);
179 #endif
180 	switch (category) {
181 		case LC_COLLATE:
182 		case LC_MONETARY:
183 		case LC_NUMERIC:
184 		case LC_TIME:
185 			return (NULL);
186 	}
187 }
188