Merge from vendor branch BINUTILS:
[dragonfly.git] / lib / libc / locale / setlocale.c
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  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by the University of
20  *      California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  * @(#)setlocale.c      8.1 (Berkeley) 7/4/93
38  * $FreeBSD: src/lib/libc/locale/setlocale.c,v 1.25.2.8 2002/08/12 11:17:38 ache Exp $
39  * $DragonFly: src/lib/libc/locale/setlocale.c,v 1.2 2003/06/17 04:26:44 dillon Exp $
40  */
41
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <errno.h>
45 #include <limits.h>
46 #include <locale.h>
47 #include <rune.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include "collate.h"
52 #include "lmonetary.h"  /* for __monetary_load_locale() */
53 #include "lnumeric.h"   /* for __numeric_load_locale() */
54 #include "lmessages.h"  /* for __messages_load_locale() */
55 #include "setlocale.h"
56 #include "ldpart.h"
57 #include "../stdtime/timelocal.h" /* for __time_load_locale() */
58
59 /*
60  * Category names for getenv()
61  */
62 static char *categories[_LC_LAST] = {
63     "LC_ALL",
64     "LC_COLLATE",
65     "LC_CTYPE",
66     "LC_MONETARY",
67     "LC_NUMERIC",
68     "LC_TIME",
69     "LC_MESSAGES",
70 };
71
72 /*
73  * Current locales for each category
74  */
75 static char current_categories[_LC_LAST][ENCODING_LEN + 1] = {
76     "C",
77     "C",
78     "C",
79     "C",
80     "C",
81     "C",
82     "C",
83 };
84
85 /*
86  * The locales we are going to try and load
87  */
88 static char new_categories[_LC_LAST][ENCODING_LEN + 1];
89 static char saved_categories[_LC_LAST][ENCODING_LEN + 1];
90
91 static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)];
92
93 static char     *currentlocale(void);
94 static int      wrap_setrunelocale(const char *);
95 static char     *loadlocale(int);
96
97 char *
98 setlocale(category, locale)
99         int category;
100         const char *locale;
101 {
102         int i, j, len, saverr;
103         char *env, *r;
104
105         if (category < LC_ALL || category >= _LC_LAST) {
106                 errno = EINVAL;
107                 return (NULL);
108         }
109
110         if (locale == NULL)
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 == NULL || !*env))
127                         env = getenv(categories[category]);
128
129                 if (env == NULL || !*env)
130                         env = getenv("LANG");
131
132                 if (env == NULL || !*env)
133                         env = "C";
134
135                 if (strlen(env) > ENCODING_LEN) {
136                         errno = EINVAL;
137                         return (NULL);
138                 }
139                 (void)strcpy(new_categories[category], env);
140
141                 if (category == LC_ALL) {
142                         for (i = 1; i < _LC_LAST; ++i) {
143                                 if ((env = getenv(categories[i])) == NULL ||
144                                     !*env)
145                                         env = new_categories[LC_ALL];
146                                 else if (strlen(env) > ENCODING_LEN) {
147                                         errno = EINVAL;
148                                         return (NULL);
149                                 }
150                                 (void)strcpy(new_categories[i], env);
151                         }
152                 }
153         } else if (category != LC_ALL) {
154                 if (strlen(locale) > ENCODING_LEN) {
155                         errno = EINVAL;
156                         return (NULL);
157                 }
158                 (void)strcpy(new_categories[category], locale);
159         } else {
160                 if ((r = strchr(locale, '/')) == NULL) {
161                         if (strlen(locale) > ENCODING_LEN) {
162                                 errno = EINVAL;
163                                 return (NULL);
164                         }
165                         for (i = 1; i < _LC_LAST; ++i)
166                                 (void)strcpy(new_categories[i], locale);
167                 } else {
168                         for (i = 1; r[1] == '/'; ++r)
169                                 ;
170                         if (!r[1]) {
171                                 errno = EINVAL;
172                                 return (NULL);  /* Hmm, just slashes... */
173                         }
174                         do {
175                                 if (i == _LC_LAST)
176                                         break;  /* Too many slashes... */
177                                 if ((len = r - locale) > ENCODING_LEN) {
178                                         errno = EINVAL;
179                                         return (NULL);
180                                 }
181                                 (void)strlcpy(new_categories[i], locale,
182                                               len + 1);
183                                 i++;
184                                 locale = r;
185                                 while (*locale == '/')
186                                         ++locale;
187                                 while (*++r && *r != '/')
188                                         ;
189                         } while (*locale);
190                         while (i < _LC_LAST) {
191                                 (void)strcpy(new_categories[i],
192                                              new_categories[i-1]);
193                                 i++;
194                         }
195                 }
196         }
197
198         if (category != LC_ALL)
199                 return (loadlocale(category));
200
201         for (i = 1; i < _LC_LAST; ++i) {
202                 (void)strcpy(saved_categories[i], current_categories[i]);
203                 if (loadlocale(i) == NULL) {
204                         saverr = errno;
205                         for (j = 1; j < i; j++) {
206                                 (void)strcpy(new_categories[j],
207                                              saved_categories[j]);
208                                 if (loadlocale(j) == NULL) {
209                                         (void)strcpy(new_categories[j], "C");
210                                         (void)loadlocale(j);
211                                 }
212                         }
213                         errno = saverr;
214                         return (NULL);
215                 }
216         }
217         return (currentlocale());
218 }
219
220 static char *
221 currentlocale()
222 {
223         int i;
224
225         (void)strcpy(current_locale_string, current_categories[1]);
226
227         for (i = 2; i < _LC_LAST; ++i)
228                 if (strcmp(current_categories[1], current_categories[i])) {
229                         for (i = 2; i < _LC_LAST; ++i) {
230                                 (void)strcat(current_locale_string, "/");
231                                 (void)strcat(current_locale_string,
232                                              current_categories[i]);
233                         }
234                         break;
235                 }
236         return (current_locale_string);
237 }
238
239 static int
240 wrap_setrunelocale(const char *locale)
241 {
242         int ret = setrunelocale((char *)locale);
243
244         if (ret != 0) {
245                 errno = ret;
246                 return (_LDP_ERROR);
247         }
248         return (_LDP_LOADED);
249 }
250
251 static char *
252 loadlocale(category)
253         int category;
254 {
255         char *new = new_categories[category];
256         char *old = current_categories[category];
257         int (*func)(const char *);
258
259         if ((new[0] == '.' &&
260              (new[1] == '\0' || (new[1] == '.' && new[2] == '\0'))) ||
261             strchr(new, '/') != NULL) {
262                 errno = EINVAL;
263                 return (NULL);
264         }
265
266         if (_PathLocale == NULL) {
267                 char *p = getenv("PATH_LOCALE");
268
269                 if (p != NULL
270 #ifndef __NETBSD_SYSCALLS
271                         && !issetugid()
272 #endif
273                         ) {
274                         if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
275                             1/*"/"*/ + CATEGORY_LEN >= PATH_MAX) {
276                                 errno = ENAMETOOLONG;
277                                 return (NULL);
278                         }
279                         _PathLocale = strdup(p);
280                         if (_PathLocale == NULL) {
281                                 errno = ENOMEM;
282                                 return (NULL);
283                         }
284                 } else
285                         _PathLocale = _PATH_LOCALE;
286         }
287
288         switch (category) {
289         case LC_CTYPE:
290                 func = wrap_setrunelocale;
291                 break;
292         case LC_COLLATE:
293                 func = __collate_load_tables;
294                 break;
295         case LC_TIME:
296                 func = __time_load_locale;
297                 break;
298         case LC_NUMERIC:
299                 func = __numeric_load_locale;
300                 break;
301         case LC_MONETARY:
302                 func = __monetary_load_locale;
303                 break;
304         case LC_MESSAGES:
305                 func = __messages_load_locale;
306                 break;
307         default:
308                 errno = EINVAL;
309                 return (NULL);
310         }
311
312         if (strcmp(new, old) == 0)
313                 return (old);
314
315         if (func(new) != _LDP_ERROR) {
316                 (void)strcpy(old, new);
317                 return (old);
318         }
319
320         return (NULL);
321 }
322