Home | History | Annotate | Line # | Download | only in locale
setlocale.c revision 1.16.2.2
      1 /*	$NetBSD: setlocale.c,v 1.16.2.2 2000/10/04 14:08:34 he Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Paul Borman at Krystal Technologies.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #if defined(LIBC_SCCS) && !defined(lint)
     41 #if 0
     42 static char sccsid[] = "@(#)setlocale.c	8.1 (Berkeley) 7/4/93";
     43 #else
     44 __RCSID("$NetBSD: setlocale.c,v 1.16.2.2 2000/10/04 14:08:34 he Exp $");
     45 #endif
     46 #endif /* LIBC_SCCS and not lint */
     47 
     48 #define _CTYPE_PRIVATE
     49 
     50 #include "namespace.h"
     51 #include <sys/localedef.h>
     52 #include <ctype.h>
     53 #include <limits.h>
     54 #include <locale.h>
     55 #include <paths.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 #include <unistd.h>
     60 #include "ctypeio.h"
     61 
     62 /*
     63  * Category names for getenv()
     64  */
     65 static const char *const categories[_LC_LAST] = {
     66     "LC_ALL",
     67     "LC_COLLATE",
     68     "LC_CTYPE",
     69     "LC_MONETARY",
     70     "LC_NUMERIC",
     71     "LC_TIME",
     72     "LC_MESSAGES"
     73 };
     74 
     75 /*
     76  * Current locales for each category
     77  */
     78 static char current_categories[_LC_LAST][32] = {
     79     "C",
     80     "C",
     81     "C",
     82     "C",
     83     "C",
     84     "C",
     85     "C"
     86 };
     87 
     88 /*
     89  * The locales we are going to try and load
     90  */
     91 static char new_categories[_LC_LAST][32];
     92 
     93 static char current_locale_string[_LC_LAST * 33];
     94 static char *PathLocale;
     95 
     96 static char	*currentlocale __P((void));
     97 static char	*loadlocale __P((int));
     98 
     99 char *
    100 setlocale(category, locale)
    101 	int category;
    102 	const char *locale;
    103 {
    104 	int i;
    105 	size_t len;
    106 	char *env, *r;
    107 
    108 	if (issetugid() ||
    109 	    (!PathLocale && !(PathLocale = getenv("PATH_LOCALE"))))
    110 		PathLocale = _PATH_LOCALE;
    111 
    112 	if (category < 0 || category >= _LC_LAST)
    113 		return (NULL);
    114 
    115 	if (!locale)
    116 		return (category ?
    117 		    current_categories[category] : currentlocale());
    118 
    119 	/*
    120 	 * Default to the current locale for everything.
    121 	 */
    122 	for (i = 1; i < _LC_LAST; ++i)
    123 		(void)strncpy(new_categories[i], current_categories[i],
    124 		    sizeof(new_categories[i]) - 1);
    125 
    126 	/*
    127 	 * Now go fill up new_categories from the locale argument
    128 	 */
    129 	if (!*locale) {
    130 		env = getenv(categories[category]);
    131 
    132 		if (!env || !*env)
    133 			env = getenv(categories[0]);
    134 
    135 		if (!env || !*env)
    136 			env = getenv("LANG");
    137 
    138 		if (!env || !*env || strchr(env, '/'))
    139 			env = "C";
    140 
    141 		(void)strncpy(new_categories[category], env, 31);
    142 		new_categories[category][31] = 0;
    143 		if (!category) {
    144 			for (i = 1; i < _LC_LAST; ++i) {
    145 				if (!(env = getenv(categories[i])) || !*env)
    146 					env = new_categories[0];
    147 				(void)strncpy(new_categories[i], env, 31);
    148 				new_categories[i][31] = 0;
    149 			}
    150 		}
    151 	} else if (category) {
    152 		(void)strncpy(new_categories[category], locale, 31);
    153 		new_categories[category][31] = 0;
    154 	} else {
    155 		if ((r = strchr(locale, '/')) == 0) {
    156 			for (i = 1; i < _LC_LAST; ++i) {
    157 				(void)strncpy(new_categories[i], locale, 31);
    158 				new_categories[i][31] = 0;
    159 			}
    160 		} else {
    161 			for (i = 1; r[1] == '/'; ++r);
    162 			if (!r[1])
    163 				return (NULL);	/* Hmm, just slashes... */
    164 			do {
    165 				len = r - locale > 31 ? 31 : r - locale;
    166 				(void)strncpy(new_categories[i++], locale, len);
    167 				new_categories[i++][len] = 0;
    168 				locale = r;
    169 				while (*locale == '/')
    170 				    ++locale;
    171 				while (*++r && *r != '/');
    172 			} while (*locale);
    173 			while (i < _LC_LAST)
    174 				(void)strncpy(new_categories[i],
    175 				    new_categories[i-1],
    176 				    sizeof(new_categories[i]) - 1);
    177 		}
    178 	}
    179 
    180 	if (category)
    181 		return (loadlocale(category));
    182 
    183 	for (i = 1; i < _LC_LAST; ++i)
    184 		(void) loadlocale(i);
    185 
    186 	return (currentlocale());
    187 }
    188 
    189 static char *
    190 currentlocale()
    191 {
    192 	int i;
    193 
    194 	(void)strncpy(current_locale_string, current_categories[1],
    195 	    sizeof(current_locale_string) - 1);
    196 
    197 	for (i = 2; i < _LC_LAST; ++i)
    198 		if (strcmp(current_categories[1], current_categories[i])) {
    199 			(void)snprintf(current_locale_string,
    200 			    sizeof(current_locale_string), "%s/%s/%s/%s/%s",
    201 			    current_categories[1], current_categories[2],
    202 			    current_categories[3], current_categories[4],
    203 			    current_categories[5]);
    204 			break;
    205 		}
    206 	return (current_locale_string);
    207 }
    208 
    209 static char *
    210 loadlocale(category)
    211 	int category;
    212 {
    213 	char name[PATH_MAX];
    214 
    215 	if (strcmp(new_categories[category], current_categories[category]) == 0)
    216 		return (current_categories[category]);
    217 
    218 	if (!strcmp(new_categories[category], "C") ||
    219 	    !strcmp(new_categories[category], "POSIX")) {
    220 
    221 		switch (category) {
    222 		case LC_CTYPE:
    223 			if (_ctype_ != _C_ctype_) {
    224 				/* LINTED const castaway */
    225 				free((void *)_ctype_);
    226 				_ctype_ = _C_ctype_;
    227 			}
    228 			if (_toupper_tab_ != _C_toupper_) {
    229 				/* LINTED const castaway */
    230 				free((void *)_toupper_tab_);
    231 				_toupper_tab_ = _C_toupper_;
    232 			}
    233 			if (_tolower_tab_ != _C_tolower_) {
    234 				/* LINTED const castaway */
    235 				free((void *)_tolower_tab_);
    236 				_tolower_tab_ = _C_tolower_;
    237 			}
    238 		}
    239 
    240 		(void)strncpy(current_categories[category],
    241 		    new_categories[category],
    242 		    sizeof(current_categories[category]) - 1);
    243 		return current_categories[category];
    244 	}
    245 
    246 	/*
    247 	 * Some day we will actually look at this file.
    248 	 */
    249 	(void)snprintf(name, sizeof(name), "%s/%s/%s",
    250 	    PathLocale, new_categories[category], categories[category]);
    251 
    252 	switch (category) {
    253 	case LC_CTYPE:
    254 		if (__loadctype(name)) {
    255 			(void)strncpy(current_categories[category],
    256 			    new_categories[category],
    257 			    sizeof(current_categories[category]) - 1);
    258 			return current_categories[category];
    259 		}
    260 		return NULL;
    261 
    262 	case LC_COLLATE:
    263 	case LC_MESSAGES:
    264 	case LC_MONETARY:
    265 	case LC_NUMERIC:
    266 	case LC_TIME:
    267 		return NULL;
    268 	}
    269 
    270 	return NULL;
    271 }
    272