Home | History | Annotate | Line # | Download | only in locale
setlocale.c revision 1.17.6.5
      1 /*	$NetBSD: setlocale.c,v 1.17.6.5 2002/09/04 01:05:40 itojun 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.17.6.5 2002/09/04 01:05:40 itojun 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 #define __SETLOCALE_SOURCE__
     55 #include <locale.h>
     56 #include <paths.h>
     57 #include <stdio.h>
     58 #include <stdlib.h>
     59 #include <string.h>
     60 #include <unistd.h>
     61 #include "ctypeio.h"
     62 
     63 /*
     64  * Category names for getenv()
     65  */
     66 static const char *const categories[_LC_LAST] = {
     67     "LC_ALL",
     68     "LC_COLLATE",
     69     "LC_CTYPE",
     70     "LC_MONETARY",
     71     "LC_NUMERIC",
     72     "LC_TIME",
     73     "LC_MESSAGES"
     74 };
     75 
     76 /*
     77  * Current locales for each category
     78  */
     79 static char current_categories[_LC_LAST][32] = {
     80     "C",
     81     "C",
     82     "C",
     83     "C",
     84     "C",
     85     "C",
     86     "C"
     87 };
     88 
     89 size_t __mb_cur_max = 1;
     90 
     91 /*
     92  * The locales we are going to try and load
     93  */
     94 static char new_categories[_LC_LAST][32];
     95 
     96 /*
     97  * Backup area to back out changes on failure
     98  */
     99 static char saved_categories[_LC_LAST][32];
    100 
    101 static char current_locale_string[_LC_LAST * 33];
    102 static char *PathLocale;
    103 
    104 static char	*currentlocale __P((void));
    105 static char	*loadlocale __P((int));
    106 
    107 char *
    108 __setlocale_mb_len_max_32(category, locale)
    109 	int category;
    110 	const char *locale;
    111 {
    112 	int i, loadlocale_success;
    113 	size_t len;
    114 	char *env, *r;
    115 
    116 	if (issetugid() ||
    117 	    (!PathLocale && !(PathLocale = getenv("PATH_LOCALE"))))
    118 		PathLocale = _PATH_LOCALE;
    119 
    120 	if (category < 0 || category >= _LC_LAST)
    121 		return (NULL);
    122 
    123 	if (!locale)
    124 		return (category ?
    125 		    current_categories[category] : currentlocale());
    126 
    127 	/*
    128 	 * Default to the current locale for everything.
    129 	 */
    130 	for (i = 1; i < _LC_LAST; ++i)
    131 		(void)strlcpy(new_categories[i], current_categories[i],
    132 		    sizeof(new_categories[i]));
    133 
    134 	/*
    135 	 * Now go fill up new_categories from the locale argument
    136 	 */
    137 	if (!*locale) {
    138 		env = getenv(categories[category]);
    139 
    140 		if (!env || !*env)
    141 			env = getenv(categories[0]);
    142 
    143 		if (!env || !*env)
    144 			env = getenv("LANG");
    145 
    146 		if (!env || !*env || strchr(env, '/'))
    147 			env = "C";
    148 
    149 		(void)strlcpy(new_categories[category], env,
    150 		    sizeof(new_categories[category]));
    151 		if (!category) {
    152 			for (i = 1; i < _LC_LAST; ++i) {
    153 				env = getenv(categories[i]);
    154 				if (!env || !*env || strchr(env, '/'))
    155 					env = new_categories[0];
    156 				(void)strlcpy(new_categories[i], env,
    157 				    sizeof(new_categories[i]));
    158 			}
    159 		}
    160 	} else if (category) {
    161 		(void)strlcpy(new_categories[category], locale,
    162 		    sizeof(new_categories[category]));
    163 	} else {
    164 		if ((r = strchr(locale, '/')) == 0) {
    165 			for (i = 1; i < _LC_LAST; ++i) {
    166 				(void)strlcpy(new_categories[i], locale,
    167 				    sizeof(new_categories[i]));
    168 			}
    169 		} else {
    170 			for (i = 1; r[1] == '/'; ++r)
    171 				;
    172 			if (!r[1])
    173 				return (NULL);	/* Hmm, just slashes... */
    174 			do {
    175 				if (i == _LC_LAST)
    176 					return (NULL);	/* too many slashes. */
    177 				len = r - locale;
    178 				if (len + 1 > sizeof(new_categories[i]))
    179 					return (NULL);	/* too long */
    180 				(void)memcpy(new_categories[i], locale, len);
    181 				new_categories[i][len] = '\0';
    182 				i++;
    183 				locale = r;
    184 				while (*locale == '/')
    185 					++locale;
    186 				while (*++r && *r != '/');
    187 			} while (*locale);
    188 			while (i < _LC_LAST) {
    189 				(void)strlcpy(new_categories[i],
    190 				    new_categories[i - 1],
    191 				    sizeof(new_categories[i]));
    192 				i++;
    193 			}
    194 		}
    195 	}
    196 
    197 	if (category)
    198 		return (loadlocale(category));
    199 
    200 	loadlocale_success = 0;
    201 	for (i = 1; i < _LC_LAST; ++i) {
    202 		(void)strlcpy(saved_categories[i], current_categories[i],
    203 		    sizeof(saved_categories[i]));
    204 		if (loadlocale(i) != NULL)
    205 			loadlocale_success = 1;
    206 	}
    207 
    208 	/*
    209 	 * If all categories failed, return NULL; we don't need to back
    210 	 * changes off, since none happened.
    211 	 */
    212 	if (!loadlocale_success)
    213 		return NULL;
    214 
    215 	return (currentlocale());
    216 }
    217 
    218 static char *
    219 currentlocale()
    220 {
    221 	int i;
    222 
    223 	(void)strlcpy(current_locale_string, current_categories[1],
    224 	    sizeof(current_locale_string));
    225 
    226 	for (i = 2; i < _LC_LAST; ++i)
    227 		if (strcmp(current_categories[1], current_categories[i])) {
    228 			(void)snprintf(current_locale_string,
    229 			    sizeof(current_locale_string), "%s/%s/%s/%s/%s",
    230 			    current_categories[1], current_categories[2],
    231 			    current_categories[3], current_categories[4],
    232 			    current_categories[5]);
    233 			break;
    234 		}
    235 	return (current_locale_string);
    236 }
    237 
    238 static char *
    239 loadlocale(category)
    240 	int category;
    241 {
    242 	char name[PATH_MAX];
    243 
    244 	if (strcmp(new_categories[category], current_categories[category]) == 0)
    245 		return (current_categories[category]);
    246 
    247 	if (!strcmp(new_categories[category], "C") ||
    248 	    !strcmp(new_categories[category], "POSIX")) {
    249 
    250 		switch (category) {
    251 		case LC_CTYPE:
    252 			if (_ctype_ != _C_ctype_) {
    253 				/* LINTED const castaway */
    254 				free((void *)_ctype_);
    255 				_ctype_ = _C_ctype_;
    256 			}
    257 			if (_toupper_tab_ != _C_toupper_) {
    258 				/* LINTED const castaway */
    259 				free((void *)_toupper_tab_);
    260 				_toupper_tab_ = _C_toupper_;
    261 			}
    262 			if (_tolower_tab_ != _C_tolower_) {
    263 				/* LINTED const castaway */
    264 				free((void *)_tolower_tab_);
    265 				_tolower_tab_ = _C_tolower_;
    266 			}
    267 		}
    268 
    269 		(void)strlcpy(current_categories[category],
    270 		    new_categories[category],
    271 		    sizeof(current_categories[category]));
    272 		return current_categories[category];
    273 	}
    274 
    275 	/*
    276 	 * Some day we will actually look at this file.
    277 	 */
    278 	(void)snprintf(name, sizeof(name), "%s/%s/%s",
    279 	    PathLocale, new_categories[category], categories[category]);
    280 
    281 	switch (category) {
    282 	case LC_CTYPE:
    283 		if (__loadctype(name)) {
    284 			(void)strlcpy(current_categories[category],
    285 			    new_categories[category],
    286 			    sizeof(current_categories[category]));
    287 			return current_categories[category];
    288 		}
    289 		return NULL;
    290 
    291 	case LC_COLLATE:
    292 	case LC_MESSAGES:
    293 	case LC_MONETARY:
    294 	case LC_NUMERIC:
    295 	case LC_TIME:
    296 		return NULL;
    297 	}
    298 
    299 	return NULL;
    300 }
    301