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