Home | History | Annotate | Line # | Download | only in locale
setlocale.c revision 1.21
      1 /*	$NetBSD: setlocale.c,v 1.21 2000/09/09 20:50:33 veego 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.21 2000/09/09 20:50:33 veego 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 static char current_locale_string[_LC_LAST * 33];
     97 static char *PathLocale;
     98 
     99 static char	*currentlocale __P((void));
    100 static char	*loadlocale __P((int));
    101 
    102 char *
    103 __setlocale_mb_len_max_32(category, locale)
    104 	int category;
    105 	const char *locale;
    106 {
    107 	int i;
    108 	size_t len;
    109 	char *env, *r;
    110 
    111 	if (issetugid() ||
    112 	    (!PathLocale && !(PathLocale = getenv("PATH_LOCALE"))))
    113 		PathLocale = _PATH_LOCALE;
    114 
    115 	if (category < 0 || category >= _LC_LAST)
    116 		return (NULL);
    117 
    118 	if (!locale)
    119 		return (category ?
    120 		    current_categories[category] : currentlocale());
    121 
    122 	/*
    123 	 * Default to the current locale for everything.
    124 	 */
    125 	for (i = 1; i < _LC_LAST; ++i)
    126 		(void)strncpy(new_categories[i], current_categories[i],
    127 		    sizeof(new_categories[i]) - 1);
    128 
    129 	/*
    130 	 * Now go fill up new_categories from the locale argument
    131 	 */
    132 	if (!*locale) {
    133 		env = getenv(categories[category]);
    134 
    135 		if (!env || !*env)
    136 			env = getenv(categories[0]);
    137 
    138 		if (!env || !*env)
    139 			env = getenv("LANG");
    140 
    141 		if (!env || !*env || strchr(env, '/'))
    142 			env = "C";
    143 
    144 		(void)strncpy(new_categories[category], env, 31);
    145 		new_categories[category][31] = 0;
    146 		if (!category) {
    147 			for (i = 1; i < _LC_LAST; ++i) {
    148 				if (!(env = getenv(categories[i])) || !*env)
    149 					env = new_categories[0];
    150 				(void)strncpy(new_categories[i], env, 31);
    151 				new_categories[i][31] = 0;
    152 			}
    153 		}
    154 	} else if (category) {
    155 		(void)strncpy(new_categories[category], locale, 31);
    156 		new_categories[category][31] = 0;
    157 	} else {
    158 		if ((r = strchr(locale, '/')) == 0) {
    159 			for (i = 1; i < _LC_LAST; ++i) {
    160 				(void)strncpy(new_categories[i], locale, 31);
    161 				new_categories[i][31] = 0;
    162 			}
    163 		} else {
    164 			for (i = 1; r[1] == '/'; ++r);
    165 			if (!r[1])
    166 				return (NULL);	/* Hmm, just slashes... */
    167 			do {
    168 				len = r - locale > 31 ? 31 : r - locale;
    169 				(void)strncpy(new_categories[i++], locale, len);
    170 				new_categories[i++][len] = 0;
    171 				locale = r;
    172 				while (*locale == '/')
    173 				    ++locale;
    174 				while (*++r && *r != '/');
    175 			} while (*locale);
    176 			while (i < _LC_LAST)
    177 				(void)strncpy(new_categories[i],
    178 				    new_categories[i-1],
    179 				    sizeof(new_categories[i]) - 1);
    180 		}
    181 	}
    182 
    183 	if (category)
    184 		return (loadlocale(category));
    185 
    186 	for (i = 1; i < _LC_LAST; ++i)
    187 		(void) loadlocale(i);
    188 
    189 	return (currentlocale());
    190 }
    191 
    192 static char *
    193 currentlocale()
    194 {
    195 	int i;
    196 
    197 	(void)strncpy(current_locale_string, current_categories[1],
    198 	    sizeof(current_locale_string) - 1);
    199 
    200 	for (i = 2; i < _LC_LAST; ++i)
    201 		if (strcmp(current_categories[1], current_categories[i])) {
    202 			(void)snprintf(current_locale_string,
    203 			    sizeof(current_locale_string), "%s/%s/%s/%s/%s",
    204 			    current_categories[1], current_categories[2],
    205 			    current_categories[3], current_categories[4],
    206 			    current_categories[5]);
    207 			break;
    208 		}
    209 	return (current_locale_string);
    210 }
    211 
    212 static char *
    213 loadlocale(category)
    214 	int category;
    215 {
    216 	char name[PATH_MAX];
    217 
    218 	if (strcmp(new_categories[category], current_categories[category]) == 0)
    219 		return (current_categories[category]);
    220 
    221 	if (!strcmp(new_categories[category], "C") ||
    222 	    !strcmp(new_categories[category], "POSIX")) {
    223 
    224 		switch (category) {
    225 		case LC_CTYPE:
    226 			if (_ctype_ != _C_ctype_) {
    227 				/* LINTED const castaway */
    228 				free((void *)_ctype_);
    229 				_ctype_ = _C_ctype_;
    230 			}
    231 			if (_toupper_tab_ != _C_toupper_) {
    232 				/* LINTED const castaway */
    233 				free((void *)_toupper_tab_);
    234 				_toupper_tab_ = _C_toupper_;
    235 			}
    236 			if (_tolower_tab_ != _C_tolower_) {
    237 				/* LINTED const castaway */
    238 				free((void *)_tolower_tab_);
    239 				_tolower_tab_ = _C_tolower_;
    240 			}
    241 		}
    242 
    243 		(void)strncpy(current_categories[category],
    244 		    new_categories[category],
    245 		    sizeof(current_categories[category]) - 1);
    246 		return current_categories[category];
    247 	}
    248 
    249 	/*
    250 	 * Some day we will actually look at this file.
    251 	 */
    252 	(void)snprintf(name, sizeof(name), "%s/%s/%s",
    253 	    PathLocale, new_categories[category], categories[category]);
    254 
    255 	switch (category) {
    256 	case LC_CTYPE:
    257 		if (__loadctype(name)) {
    258 			(void)strncpy(current_categories[category],
    259 			    new_categories[category],
    260 			    sizeof(current_categories[category]) - 1);
    261 			return current_categories[category];
    262 		}
    263 		return NULL;
    264 
    265 	case LC_COLLATE:
    266 	case LC_MESSAGES:
    267 	case LC_MONETARY:
    268 	case LC_NUMERIC:
    269 	case LC_TIME:
    270 		return NULL;
    271 	}
    272 
    273 	return NULL;
    274 }
    275