Home | History | Annotate | Line # | Download | only in locale
setlocale.c revision 1.50
      1 /*	$NetBSD: setlocale.c,v 1.50 2006/02/16 19:19:49 tnozaki 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. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 #if defined(LIBC_SCCS) && !defined(lint)
     37 #if 0
     38 static char sccsid[] = "@(#)setlocale.c	8.1 (Berkeley) 7/4/93";
     39 #else
     40 __RCSID("$NetBSD: setlocale.c,v 1.50 2006/02/16 19:19:49 tnozaki Exp $");
     41 #endif
     42 #endif /* LIBC_SCCS and not lint */
     43 
     44 #define _CTYPE_PRIVATE
     45 
     46 #include "namespace.h"
     47 #include <sys/localedef.h>
     48 #include <sys/types.h>
     49 #include <sys/stat.h>
     50 #include <assert.h>
     51 #include <limits.h>
     52 #include <ctype.h>
     53 #define __SETLOCALE_SOURCE__
     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 #ifdef WITH_RUNE
     61 #include "rune.h"
     62 #include "rune_local.h"
     63 #else
     64 #include "ctypeio.h"
     65 #endif
     66 
     67 #ifdef CITRUS
     68 #include <citrus/citrus_namespace.h>
     69 #include <citrus/citrus_region.h>
     70 #include <citrus/citrus_lookup.h>
     71 #include <citrus/citrus_bcs.h>
     72 #else
     73 #include <locale/aliasname_local.h>
     74 #define _lookup_alias(p, a, b, s, c)	__unaliasname((p), (a), (b), (s))
     75 #define _bcs_strcasecmp(a, b)		strcasecmp((a), (b))
     76 #endif
     77 
     78 #define _LOCALE_SYM_FORCE	"/force"
     79 
     80 /*
     81  * Category names for getenv()
     82  */
     83 static const char *const categories[_LC_LAST] = {
     84     "LC_ALL",
     85     "LC_COLLATE",
     86     "LC_CTYPE",
     87     "LC_MONETARY",
     88     "LC_NUMERIC",
     89     "LC_TIME",
     90     "LC_MESSAGES"
     91 };
     92 
     93 /*
     94  * Current locales for each category
     95  */
     96 static char current_categories[_LC_LAST][32] = {
     97     "C",
     98     "C",
     99     "C",
    100     "C",
    101     "C",
    102     "C",
    103     "C"
    104 };
    105 
    106 /*
    107  * The locales we are going to try and load
    108  */
    109 static char new_categories[_LC_LAST][32];
    110 
    111 static char current_locale_string[_LC_LAST * 33];
    112 
    113 static char *currentlocale __P((void));
    114 static void revert_to_default __P((int));
    115 static int force_locale_enable __P((int));
    116 static int load_locale_sub __P((int, const char *, int));
    117 static char *loadlocale __P((int));
    118 static const char *__get_locale_env __P((int));
    119 
    120 char *
    121 __setlocale(category, locale)
    122 	int category;
    123 	const char *locale;
    124 {
    125 	int i, loadlocale_success;
    126 	size_t len;
    127 	const char *env, *r;
    128 
    129 	if (issetugid() ||
    130 	    (!_PathLocale && !(_PathLocale = getenv("PATH_LOCALE"))))
    131 		_PathLocale = _PATH_LOCALE;
    132 
    133 	if (category < 0 || category >= _LC_LAST)
    134 		return (NULL);
    135 
    136 	if (!locale)
    137 		return (category ?
    138 		    current_categories[category] : currentlocale());
    139 
    140 	/*
    141 	 * Default to the current locale for everything.
    142 	 */
    143 	for (i = 1; i < _LC_LAST; ++i)
    144 		(void)strlcpy(new_categories[i], current_categories[i],
    145 		    sizeof(new_categories[i]));
    146 
    147 	/*
    148 	 * Now go fill up new_categories from the locale argument
    149 	 */
    150 	if (!*locale) {
    151 		if (category == LC_ALL) {
    152 			for (i = 1; i < _LC_LAST; ++i) {
    153 				env = __get_locale_env(i);
    154 				(void)strlcpy(new_categories[i], env,
    155 				    sizeof(new_categories[i]));
    156 			}
    157 		}
    158 		else {
    159 			env = __get_locale_env(category);
    160 			(void)strlcpy(new_categories[category], env,
    161 				sizeof(new_categories[category]));
    162 		}
    163 	} else if (category) {
    164 		(void)strlcpy(new_categories[category], locale,
    165 		    sizeof(new_categories[category]));
    166 	} else {
    167 		if ((r = strchr(locale, '/')) == 0) {
    168 			for (i = 1; i < _LC_LAST; ++i) {
    169 				(void)strlcpy(new_categories[i], locale,
    170 				    sizeof(new_categories[i]));
    171 			}
    172 		} else {
    173 			for (i = 1;;) {
    174 				_DIAGASSERT(*r == '/' || *r == 0);
    175 				_DIAGASSERT(*locale != 0);
    176 				if (*locale == '/')
    177 					return (NULL);	/* invalid format. */
    178 				len = r - locale;
    179 				if (len + 1 > sizeof(new_categories[i]))
    180 					return (NULL);	/* too long */
    181 				(void)memcpy(new_categories[i], locale, len);
    182 				new_categories[i][len] = '\0';
    183 				if (*r == 0)
    184 					break;
    185 				_DIAGASSERT(*r == '/');
    186 				if (*(locale = ++r) == 0)
    187 					/* slash followed by NUL */
    188 					return (NULL);
    189 				/* skip until NUL or '/' */
    190 				while (*r && *r != '/')
    191 					r++;
    192 				if (++i == _LC_LAST)
    193 					return (NULL);	/* too many slashes. */
    194 			}
    195 			if (i + 1 != _LC_LAST)
    196 				return (NULL);	/* too few slashes. */
    197 		}
    198 	}
    199 
    200 	if (category)
    201 		return (loadlocale(category));
    202 
    203 	loadlocale_success = 0;
    204 	for (i = 1; i < _LC_LAST; ++i) {
    205 		if (loadlocale(i) != NULL)
    206 			loadlocale_success = 1;
    207 	}
    208 
    209 	/*
    210 	 * If all categories failed, return NULL; we don't need to back
    211 	 * changes off, since none happened.
    212 	 */
    213 	if (!loadlocale_success)
    214 		return NULL;
    215 
    216 	return (currentlocale());
    217 }
    218 
    219 static char *
    220 currentlocale()
    221 {
    222 	int i;
    223 
    224 	(void)strlcpy(current_locale_string, current_categories[1],
    225 	    sizeof(current_locale_string));
    226 
    227 	for (i = 2; i < _LC_LAST; ++i)
    228 		if (strcmp(current_categories[1], current_categories[i])) {
    229 			(void)snprintf(current_locale_string,
    230 			    sizeof(current_locale_string), "%s/%s/%s/%s/%s/%s",
    231 			    current_categories[1], current_categories[2],
    232 			    current_categories[3], current_categories[4],
    233 			    current_categories[5], current_categories[6]);
    234 			break;
    235 		}
    236 	return (current_locale_string);
    237 }
    238 
    239 static void
    240 revert_to_default(category)
    241 	int category;
    242 {
    243 	switch (category) {
    244 	case LC_CTYPE:
    245 #ifdef WITH_RUNE
    246 		(void)_xpg4_setrunelocale("C");
    247 		(void)__runetable_to_netbsd_ctype("C");
    248 #else
    249 		if (_ctype_ != _C_ctype_) {
    250 			/* LINTED const castaway */
    251 			free((void *)_ctype_);
    252 			_ctype_ = _C_ctype_;
    253 		}
    254 		if (_toupper_tab_ != _C_toupper_) {
    255 			/* LINTED const castaway */
    256 			free((void *)_toupper_tab_);
    257 			_toupper_tab_ = _C_toupper_;
    258 		}
    259 		if (_tolower_tab_ != _C_tolower_) {
    260 			/* LINTED const castaway */
    261 			free((void *)_tolower_tab_);
    262 			_tolower_tab_ = _C_tolower_;
    263 		}
    264 #endif
    265 		break;
    266 	case LC_MESSAGES:
    267 	case LC_COLLATE:
    268 	case LC_MONETARY:
    269 	case LC_NUMERIC:
    270 	case LC_TIME:
    271 		break;
    272 	}
    273 }
    274 
    275 static int
    276 force_locale_enable(category)
    277 	int category;
    278 {
    279 	revert_to_default(category);
    280 
    281 	return 0;
    282 }
    283 
    284 static int
    285 load_locale_sub(category, locname, isspecial)
    286 	int category;
    287 	const char *locname;
    288 	int isspecial;
    289 {
    290 	char name[PATH_MAX];
    291 
    292 	/* check for the default locales */
    293 	if (!strcmp(new_categories[category], "C") ||
    294 	    !strcmp(new_categories[category], "POSIX")) {
    295 		revert_to_default(category);
    296 		return 0;
    297 	}
    298 
    299 	/* check whether special symbol */
    300 	if (isspecial && _bcs_strcasecmp(locname, _LOCALE_SYM_FORCE) == 0)
    301 		return force_locale_enable(category);
    302 
    303 	/* sanity check */
    304 	if (strchr(locname, '/') != NULL)
    305 		return -1;
    306 
    307 	(void)snprintf(name, sizeof(name), "%s/%s/%s",
    308 		       _PathLocale, locname, categories[category]);
    309 
    310 	switch (category) {
    311 	case LC_CTYPE:
    312 #ifdef WITH_RUNE
    313 		if (_xpg4_setrunelocale(__UNCONST(locname)))
    314 			return -1;
    315 		if (__runetable_to_netbsd_ctype(locname)) {
    316 			/* very unfortunate, but need to go to "C" locale */
    317 			revert_to_default(category);
    318 			return -1;
    319 		}
    320 #else
    321 		if (!__loadctype(name))
    322 			return -1;
    323 #endif
    324 		break;
    325 
    326 	case LC_MESSAGES:
    327 		/*
    328 		 * XXX we don't have LC_MESSAGES support yet,
    329 		 * but catopen may use the value of LC_MESSAGES category.
    330 		 * so return successfully if locale directory is present.
    331 		 */
    332 		(void)snprintf(name, sizeof(name), "%s/%s",
    333 			_PathLocale, locname);
    334 		/* local */
    335 		{
    336 			struct stat st;
    337 			if (stat(name, &st) < 0)
    338 				return -1;
    339 			if (!S_ISDIR(st.st_mode))
    340 				return -1;
    341 		}
    342 		break;
    343 
    344 	case LC_COLLATE:
    345 	case LC_MONETARY:
    346 	case LC_NUMERIC:
    347 	case LC_TIME:
    348 		return -1;
    349 	}
    350 
    351 	return 0;
    352 }
    353 
    354 static char *
    355 loadlocale(category)
    356 	int category;
    357 {
    358 	char aliaspath[PATH_MAX], loccat[PATH_MAX], buf[PATH_MAX];
    359 	const char *alias;
    360 
    361 	_DIAGASSERT(0 < category && category < _LC_LAST);
    362 
    363 	if (strcmp(new_categories[category], current_categories[category]) == 0)
    364 		return (current_categories[category]);
    365 
    366 	/* (1) non-aliased file */
    367 	if (!load_locale_sub(category, new_categories[category], 0))
    368 		goto success;
    369 
    370 	/* (2) lookup locname/catname type alias */
    371 	(void)snprintf(aliaspath, sizeof(aliaspath),
    372 		       "%s/" _LOCALE_ALIAS_NAME, _PathLocale);
    373 	(void)snprintf(loccat, sizeof(loccat), "%s/%s",
    374 		       new_categories[category], categories[category]);
    375 	alias = _lookup_alias(aliaspath, loccat, buf, sizeof(buf),
    376 			      _LOOKUP_CASE_SENSITIVE);
    377 	if (!load_locale_sub(category, alias, 1))
    378 		goto success;
    379 
    380 	/* (3) lookup locname type alias */
    381 	alias = _lookup_alias(aliaspath, new_categories[category],
    382 			      buf, sizeof(buf), _LOOKUP_CASE_SENSITIVE);
    383 	if (!load_locale_sub(category, alias, 1))
    384 		goto success;
    385 
    386 	return NULL;
    387 
    388 success:
    389 	(void)strlcpy(current_categories[category],
    390 		new_categories[category],
    391 		sizeof(current_categories[category]));
    392 	return current_categories[category];
    393 }
    394 
    395 static const char *
    396 __get_locale_env(category)
    397 	int category;
    398 {
    399 	const char *env;
    400 
    401 	_DIAGASSERT(category != LC_ALL);
    402 
    403 	/* 1. check LC_ALL. */
    404 	env = getenv(categories[0]);
    405 
    406 	/* 2. check LC_* */
    407 	if (!env || !*env)
    408 		env = getenv(categories[category]);
    409 
    410 	/* 3. check LANG */
    411 	if (!env || !*env)
    412 		env = getenv("LANG");
    413 
    414 	/* 4. if none is set, fall to "C" */
    415 	if (!env || !*env || strchr(env, '/'))
    416 		env = "C";
    417 
    418 	return env;
    419 }
    420