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