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