Home | History | Annotate | Line # | Download | only in locale
setlocale.c revision 1.52.8.1
      1 /*	$NetBSD: setlocale.c,v 1.52.8.1 2008/06/23 04:29:32 wrstuden Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1991, 1993, 2008
      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.52.8.1 2008/06/23 04:29:32 wrstuden 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 #include "lcmessages.h"
     67 #include "lcmonetary.h"
     68 #include "lcnumeric.h"
     69 #include "lctime.h"
     70 
     71 #ifdef CITRUS
     72 #include <citrus/citrus_namespace.h>
     73 #include <citrus/citrus_region.h>
     74 #include <citrus/citrus_lookup.h>
     75 #include <citrus/citrus_bcs.h>
     76 #else
     77 #include <locale/aliasname_local.h>
     78 #define _lookup_alias(p, a, b, s, c)	__unaliasname((p), (a), (b), (s))
     79 #define _bcs_strcasecmp(a, b)		strcasecmp((a), (b))
     80 #endif
     81 
     82 #define _LOCALE_SYM_FORCE	"/force"
     83 
     84 /*
     85  * Category names for getenv()
     86  */
     87 static const char *const categories[_LC_LAST] = {
     88     "LC_ALL",
     89     "LC_COLLATE",
     90     "LC_CTYPE",
     91     "LC_MONETARY",
     92     "LC_NUMERIC",
     93     "LC_TIME",
     94     "LC_MESSAGES"
     95 };
     96 
     97 /*
     98  * Current locales for each category
     99  */
    100 static char current_categories[_LC_LAST][32] = {
    101     "C",
    102     "C",
    103     "C",
    104     "C",
    105     "C",
    106     "C",
    107     "C"
    108 };
    109 
    110 /*
    111  * The locales we are going to try and load
    112  */
    113 static char new_categories[_LC_LAST][32];
    114 
    115 static char current_locale_string[_LC_LAST * 33];
    116 
    117 static char *currentlocale __P((void));
    118 static void revert_to_default __P((int));
    119 static int force_locale_enable __P((int));
    120 static int load_locale_sub __P((int, const char *, int));
    121 static char *loadlocale __P((int));
    122 static const char *__get_locale_env __P((int));
    123 
    124 char *
    125 __setlocale(category, locale)
    126 	int category;
    127 	const char *locale;
    128 {
    129 	int i, loadlocale_success;
    130 	size_t len;
    131 	const char *env, *r;
    132 
    133 	if (issetugid() ||
    134 	    ((!_PathLocale && !(_PathLocale = getenv("PATH_LOCALE"))) ||
    135 	     !*_PathLocale))
    136 		_PathLocale = _PATH_LOCALE;
    137 
    138 	if (category < 0 || category >= _LC_LAST)
    139 		return (NULL);
    140 
    141 	if (!locale)
    142 		return (category ?
    143 		    current_categories[category] : currentlocale());
    144 
    145 	/*
    146 	 * Default to the current locale for everything.
    147 	 */
    148 	for (i = 1; i < _LC_LAST; ++i)
    149 		(void)strlcpy(new_categories[i], current_categories[i],
    150 		    sizeof(new_categories[i]));
    151 
    152 	/*
    153 	 * Now go fill up new_categories from the locale argument
    154 	 */
    155 	if (!*locale) {
    156 		if (category == LC_ALL) {
    157 			for (i = 1; i < _LC_LAST; ++i) {
    158 				env = __get_locale_env(i);
    159 				(void)strlcpy(new_categories[i], env,
    160 				    sizeof(new_categories[i]));
    161 			}
    162 		}
    163 		else {
    164 			env = __get_locale_env(category);
    165 			(void)strlcpy(new_categories[category], env,
    166 				sizeof(new_categories[category]));
    167 		}
    168 	} else if (category) {
    169 		(void)strlcpy(new_categories[category], locale,
    170 		    sizeof(new_categories[category]));
    171 	} else {
    172 		if ((r = strchr(locale, '/')) == 0) {
    173 			for (i = 1; i < _LC_LAST; ++i) {
    174 				(void)strlcpy(new_categories[i], locale,
    175 				    sizeof(new_categories[i]));
    176 			}
    177 		} else {
    178 			for (i = 1;;) {
    179 				_DIAGASSERT(*r == '/' || *r == 0);
    180 				_DIAGASSERT(*locale != 0);
    181 				if (*locale == '/')
    182 					return (NULL);	/* invalid format. */
    183 				len = r - locale;
    184 				if (len + 1 > sizeof(new_categories[i]))
    185 					return (NULL);	/* too long */
    186 				(void)memcpy(new_categories[i], locale, len);
    187 				new_categories[i][len] = '\0';
    188 				if (*r == 0)
    189 					break;
    190 				_DIAGASSERT(*r == '/');
    191 				if (*(locale = ++r) == 0)
    192 					/* slash followed by NUL */
    193 					return (NULL);
    194 				/* skip until NUL or '/' */
    195 				while (*r && *r != '/')
    196 					r++;
    197 				if (++i == _LC_LAST)
    198 					return (NULL);	/* too many slashes. */
    199 			}
    200 			if (i + 1 != _LC_LAST)
    201 				return (NULL);	/* too few slashes. */
    202 		}
    203 	}
    204 
    205 	if (category)
    206 		return (loadlocale(category));
    207 
    208 	loadlocale_success = 0;
    209 	for (i = 1; i < _LC_LAST; ++i) {
    210 		if (loadlocale(i) != NULL)
    211 			loadlocale_success = 1;
    212 	}
    213 
    214 	/*
    215 	 * If all categories failed, return NULL; we don't need to back
    216 	 * changes off, since none happened.
    217 	 */
    218 	if (!loadlocale_success)
    219 		return NULL;
    220 
    221 	return (currentlocale());
    222 }
    223 
    224 static char *
    225 currentlocale()
    226 {
    227 	int i;
    228 
    229 	(void)strlcpy(current_locale_string, current_categories[1],
    230 	    sizeof(current_locale_string));
    231 
    232 	for (i = 2; i < _LC_LAST; ++i)
    233 		if (strcmp(current_categories[1], current_categories[i])) {
    234 			(void)snprintf(current_locale_string,
    235 			    sizeof(current_locale_string), "%s/%s/%s/%s/%s/%s",
    236 			    current_categories[1], current_categories[2],
    237 			    current_categories[3], current_categories[4],
    238 			    current_categories[5], current_categories[6]);
    239 			break;
    240 		}
    241 	return (current_locale_string);
    242 }
    243 
    244 static void
    245 revert_to_default(category)
    246 	int category;
    247 {
    248 	switch (category) {
    249 	case LC_CTYPE:
    250 #ifdef WITH_RUNE
    251 		(void)_xpg4_setrunelocale("C");
    252 #else
    253 		if (_ctype_ != _C_ctype_) {
    254 			/* LINTED const castaway */
    255 			free((void *)_ctype_);
    256 			_ctype_ = _C_ctype_;
    257 		}
    258 		if (_toupper_tab_ != _C_toupper_) {
    259 			/* LINTED const castaway */
    260 			free((void *)_toupper_tab_);
    261 			_toupper_tab_ = _C_toupper_;
    262 		}
    263 		if (_tolower_tab_ != _C_tolower_) {
    264 			/* LINTED const castaway */
    265 			free((void *)_tolower_tab_);
    266 			_tolower_tab_ = _C_tolower_;
    267 		}
    268 #endif
    269 		break;
    270 	case LC_TIME:
    271 		if (_CurrentTimeLocale != &_DefaultTimeLocale) {
    272 			free(__UNCONST(_CurrentTimeLocale));
    273 			_CurrentTimeLocale = &_DefaultTimeLocale;
    274 		}
    275 		break;
    276 	case LC_MESSAGES:
    277 		if (_CurrentMessagesLocale != &_DefaultMessagesLocale) {
    278 			free(__UNCONST(_CurrentMessagesLocale));
    279 			_CurrentMessagesLocale = &_DefaultMessagesLocale;
    280 		}
    281 		break;
    282 	case LC_COLLATE:
    283 		break;
    284 	case LC_MONETARY:
    285 		if (_CurrentMonetaryLocale != &_DefaultMonetaryLocale) {
    286 			free(__UNCONST(_CurrentMonetaryLocale));
    287 			_CurrentMonetaryLocale = &_DefaultMonetaryLocale;
    288 		}
    289 		break;
    290 	case LC_NUMERIC:
    291 		if (_CurrentNumericLocale != &_DefaultNumericLocale) {
    292 			free(__UNCONST(_CurrentNumericLocale));
    293 			_CurrentNumericLocale = &_DefaultNumericLocale;
    294 		}
    295 		break;
    296 	}
    297 }
    298 
    299 static int
    300 force_locale_enable(category)
    301 	int category;
    302 {
    303 	revert_to_default(category);
    304 
    305 	return 0;
    306 }
    307 
    308 static int
    309 load_locale_sub(category, locname, isspecial)
    310 	int category;
    311 	const char *locname;
    312 	int isspecial;
    313 {
    314 	char name[PATH_MAX];
    315 	int len;
    316 
    317 	/* check for the default locales */
    318 	if (!strcmp(new_categories[category], "C") ||
    319 	    !strcmp(new_categories[category], "POSIX")) {
    320 		revert_to_default(category);
    321 		return 0;
    322 	}
    323 
    324 	/* check whether special symbol */
    325 	if (isspecial && _bcs_strcasecmp(locname, _LOCALE_SYM_FORCE) == 0)
    326 		return force_locale_enable(category);
    327 
    328 	/* sanity check */
    329 	if (strchr(locname, '/') != NULL)
    330 		return -1;
    331 
    332 	len = snprintf(name, sizeof(name), "%s/%s/%s",
    333 		       _PathLocale, locname, categories[category]);
    334 	if (len < 0 || len >= sizeof(name))
    335 		return -1;
    336 
    337 	switch (category) {
    338 	case LC_CTYPE:
    339 #ifdef WITH_RUNE
    340 		if (_xpg4_setrunelocale(__UNCONST(locname)))
    341 			return -1;
    342 #else
    343 		if (!__loadctype(name))
    344 			return -1;
    345 #endif
    346 		break;
    347 
    348 	case LC_MESSAGES:
    349 		len += snprintf(name + len, sizeof(name) - len, "/%s",
    350 				categories[category]);
    351 		if (len >= sizeof(name))
    352 			return -1;
    353 		if (!__loadmessages(name))
    354 #ifdef notyet
    355 			return -1;
    356 #else
    357 		/*
    358 		 * XXX we don't have LC_MESSAGES support yet,
    359 		 * but catopen may use the value of LC_MESSAGES category.
    360 		 * so return successfully if locale directory is present.
    361 		 */
    362 		/* local */
    363 		{
    364 			struct stat st;
    365 
    366 			(void)snprintf(name, sizeof(name), "%s/%s",
    367 				_PathLocale, locname);
    368 			if (stat(name, &st) < 0)
    369 				return -1;
    370 			if (!S_ISDIR(st.st_mode))
    371 				return -1;
    372 		}
    373 #endif
    374 		break;
    375 
    376 	case LC_TIME:
    377 		if (!__loadtime(name))
    378 			return -1;
    379 		break;
    380 	case LC_COLLATE:
    381 		return -1;
    382 	case LC_MONETARY:
    383 		if (!__loadmonetary(name))
    384 			return -1;
    385 		break;
    386 	case LC_NUMERIC:
    387 		if (!__loadnumeric(name))
    388 			return -1;
    389 		break;
    390 	}
    391 
    392 	return 0;
    393 }
    394 
    395 static char *
    396 loadlocale(category)
    397 	int category;
    398 {
    399 	char aliaspath[PATH_MAX], loccat[PATH_MAX], buf[PATH_MAX];
    400 	const char *alias;
    401 
    402 	_DIAGASSERT(0 < category && category < _LC_LAST);
    403 
    404 	if (strcmp(new_categories[category], current_categories[category]) == 0)
    405 		return (current_categories[category]);
    406 
    407 	/* (1) non-aliased file */
    408 	if (!load_locale_sub(category, new_categories[category], 0))
    409 		goto success;
    410 
    411 	/* (2) lookup locname/catname type alias */
    412 	(void)snprintf(aliaspath, sizeof(aliaspath),
    413 		       "%s/" _LOCALE_ALIAS_NAME, _PathLocale);
    414 	(void)snprintf(loccat, sizeof(loccat), "%s/%s",
    415 		       new_categories[category], categories[category]);
    416 	alias = _lookup_alias(aliaspath, loccat, buf, sizeof(buf),
    417 			      _LOOKUP_CASE_SENSITIVE);
    418 	if (!load_locale_sub(category, alias, 1))
    419 		goto success;
    420 
    421 	/* (3) lookup locname type alias */
    422 	alias = _lookup_alias(aliaspath, new_categories[category],
    423 			      buf, sizeof(buf), _LOOKUP_CASE_SENSITIVE);
    424 	if (!load_locale_sub(category, alias, 1))
    425 		goto success;
    426 
    427 	return NULL;
    428 
    429 success:
    430 	(void)strlcpy(current_categories[category],
    431 		new_categories[category],
    432 		sizeof(current_categories[category]));
    433 	return current_categories[category];
    434 }
    435 
    436 static const char *
    437 __get_locale_env(category)
    438 	int category;
    439 {
    440 	const char *env;
    441 
    442 	_DIAGASSERT(category != LC_ALL);
    443 
    444 	/* 1. check LC_ALL. */
    445 	env = getenv(categories[0]);
    446 
    447 	/* 2. check LC_* */
    448 	if (!env || !*env)
    449 		env = getenv(categories[category]);
    450 
    451 	/* 3. check LANG */
    452 	if (!env || !*env)
    453 		env = getenv("LANG");
    454 
    455 	/* 4. if none is set, fall to "C" */
    456 	if (!env || !*env || strchr(env, '/'))
    457 		env = "C";
    458 
    459 	return env;
    460 }
    461