Home | History | Annotate | Line # | Download | only in locale
setlocale.c revision 1.6
      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 found, i, len;
     92 	char *env, *r;
     93 
     94 	if (!PathLocale && !(PathLocale = getenv("PATH_LOCALE")))
     95 		PathLocale = _PATH_LOCALE;
     96 
     97 	if (category < 0 || category >= _LC_LAST)
     98 		return (NULL);
     99 
    100 	if (!locale)
    101 		return (category ?
    102 		    current_categories[category] : currentlocale());
    103 
    104 	/*
    105 	 * Default to the current locale for everything.
    106 	 */
    107 	for (i = 1; i < _LC_LAST; ++i)
    108 		(void)strcpy(new_categories[i], current_categories[i]);
    109 
    110 	/*
    111 	 * Now go fill up new_categories from the locale argument
    112 	 */
    113 	if (!*locale) {
    114 		env = getenv(categories[category]);
    115 
    116 		if (!env)
    117 			env = getenv(categories[0]);
    118 
    119 		if (!env)
    120 			env = getenv("LANG");
    121 
    122 		if (!env)
    123 			env = "C";
    124 
    125 		(void) strncpy(new_categories[category], env, 31);
    126 		new_categories[category][31] = 0;
    127 		if (!category) {
    128 			for (i = 1; i < _LC_LAST; ++i) {
    129 				if (!(env = getenv(categories[i])))
    130 					env = new_categories[0];
    131 				(void)strncpy(new_categories[i], env, 31);
    132 				new_categories[i][31] = 0;
    133 			}
    134 		}
    135 	} else if (category)  {
    136 		(void)strncpy(new_categories[category], locale, 31);
    137 		new_categories[category][31] = 0;
    138 	} else {
    139 		if ((r = strchr(locale, '/')) == 0) {
    140 			for (i = 1; i < _LC_LAST; ++i) {
    141 				(void)strncpy(new_categories[i], locale, 31);
    142 				new_categories[i][31] = 0;
    143 			}
    144 		} else {
    145 			for (i = 1; r[1] == '/'; ++r);
    146 			if (!r[1])
    147 				return (NULL);	/* Hmm, just slashes... */
    148 			do {
    149 				len = r - locale > 31 ? 31 : r - locale;
    150 				(void)strncpy(new_categories[i++], locale, len);
    151 				new_categories[i++][len] = 0;
    152 				locale = r;
    153 				while (*locale == '/')
    154 				    ++locale;
    155 				while (*++r && *r != '/');
    156 			} while (*locale);
    157 			while (i < _LC_LAST)
    158 				(void)strcpy(new_categories[i],
    159 				    new_categories[i-1]);
    160 		}
    161 	}
    162 
    163 	if (category)
    164 		return (loadlocale(category));
    165 
    166 	found = 0;
    167 	for (i = 1; i < _LC_LAST; ++i)
    168 		if (loadlocale(i) != NULL)
    169 			found = 1;
    170 	if (found)
    171 	    return (currentlocale());
    172 	return (NULL);
    173 }
    174 
    175 static char *
    176 currentlocale()
    177 {
    178 	int i;
    179 
    180 	(void)strcpy(current_locale_string, current_categories[1]);
    181 
    182 	for (i = 2; i < _LC_LAST; ++i)
    183 		if (strcmp(current_categories[1], current_categories[i])) {
    184 			(void)snprintf(current_locale_string,
    185 			    sizeof(current_locale_string), "%s/%s/%s/%s/%s",
    186 			    current_categories[1], current_categories[2],
    187 			    current_categories[3], current_categories[4],
    188 			    current_categories[5]);
    189 			break;
    190 		}
    191 	return (current_locale_string);
    192 }
    193 
    194 static char *
    195 loadlocale(category)
    196 	int category;
    197 {
    198 	char name[PATH_MAX];
    199 
    200 	if (strcmp(new_categories[category],
    201 	    current_categories[category]) == 0)
    202 		return (current_categories[category]);
    203 
    204 	if (!strcmp(new_categories[category], "C") ||
    205 		!strcmp(new_categories[category], "POSIX")) {
    206 
    207 		/*
    208 		 * Some day this will need to reset the locale to the default
    209 		 * C locale.  Since we have no way to change them as of yet,
    210 		 * there is no need to reset them.
    211 		 */
    212 		(void)strcpy(current_categories[category],
    213 		    new_categories[category]);
    214 		return (current_categories[category]);
    215 	}
    216 
    217 	/*
    218 	 * Some day we will actually look at this file.
    219 	 */
    220 	(void)snprintf(name, sizeof(name), "%s/%s/%s",
    221 	    PathLocale, new_categories[category], categories[category]);
    222 
    223 	switch (category) {
    224 		case LC_CTYPE:
    225 		case LC_COLLATE:
    226 		case LC_MESSAGES:
    227 		case LC_MONETARY:
    228 		case LC_NUMERIC:
    229 		case LC_TIME:
    230 			return (NULL);
    231 	}
    232 }
    233