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