Home | History | Annotate | Line # | Download | only in locale
setlocale.c revision 1.42
      1  1.42     enami /*	$NetBSD: setlocale.c,v 1.42 2002/08/07 04:42:42 enami 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.1       cgd  * 3. All advertising materials mentioning features or use of this software
     19   1.1       cgd  *    must display the following acknowledgement:
     20   1.1       cgd  *	This product includes software developed by the University of
     21   1.1       cgd  *	California, Berkeley and its contributors.
     22   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     23   1.1       cgd  *    may be used to endorse or promote products derived from this software
     24   1.1       cgd  *    without specific prior written permission.
     25   1.1       cgd  *
     26   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1       cgd  * SUCH DAMAGE.
     37   1.1       cgd  */
     38   1.1       cgd 
     39  1.12  christos #include <sys/cdefs.h>
     40   1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     41  1.10    kleink #if 0
     42   1.5       jtc static char sccsid[] = "@(#)setlocale.c	8.1 (Berkeley) 7/4/93";
     43  1.10    kleink #else
     44  1.42     enami __RCSID("$NetBSD: setlocale.c,v 1.42 2002/08/07 04:42:42 enami Exp $");
     45  1.10    kleink #endif
     46   1.1       cgd #endif /* LIBC_SCCS and not lint */
     47   1.1       cgd 
     48  1.11    kleink #define _CTYPE_PRIVATE
     49  1.11    kleink 
     50  1.14    kleink #include "namespace.h"
     51   1.5       jtc #include <sys/localedef.h>
     52  1.37      yamt #include <sys/types.h>
     53  1.37      yamt #include <sys/stat.h>
     54  1.37      yamt #include <assert.h>
     55  1.35    kleink #include <limits.h>
     56  1.11    kleink #include <ctype.h>
     57  1.18  tshiozak #define __SETLOCALE_SOURCE__
     58   1.1       cgd #include <locale.h>
     59  1.11    kleink #include <paths.h>
     60   1.6       jtc #include <stdio.h>
     61   1.5       jtc #include <stdlib.h>
     62   1.1       cgd #include <string.h>
     63  1.21     veego #include <unistd.h>
     64  1.32    itojun #ifdef WITH_RUNE
     65  1.24    itojun #include "rune.h"
     66  1.24    itojun #include "rune_local.h"
     67  1.32    itojun #else
     68  1.32    itojun #include "ctypeio.h"
     69  1.32    itojun #endif
     70   1.1       cgd 
     71   1.5       jtc /*
     72   1.5       jtc  * Category names for getenv()
     73   1.5       jtc  */
     74  1.13   mycroft static const char *const categories[_LC_LAST] = {
     75   1.5       jtc     "LC_ALL",
     76   1.5       jtc     "LC_COLLATE",
     77   1.5       jtc     "LC_CTYPE",
     78   1.5       jtc     "LC_MONETARY",
     79   1.5       jtc     "LC_NUMERIC",
     80   1.5       jtc     "LC_TIME",
     81   1.5       jtc     "LC_MESSAGES"
     82   1.5       jtc };
     83   1.1       cgd 
     84   1.1       cgd /*
     85   1.5       jtc  * Current locales for each category
     86   1.5       jtc  */
     87   1.5       jtc static char current_categories[_LC_LAST][32] = {
     88   1.5       jtc     "C",
     89   1.5       jtc     "C",
     90   1.5       jtc     "C",
     91   1.5       jtc     "C",
     92   1.5       jtc     "C",
     93   1.5       jtc     "C",
     94   1.5       jtc     "C"
     95   1.5       jtc };
     96  1.19    kleink 
     97   1.5       jtc /*
     98   1.5       jtc  * The locales we are going to try and load
     99   1.1       cgd  */
    100   1.5       jtc static char new_categories[_LC_LAST][32];
    101   1.5       jtc 
    102   1.5       jtc static char current_locale_string[_LC_LAST * 33];
    103  1.24    itojun char *_PathLocale;
    104   1.5       jtc 
    105  1.34    itojun static char *currentlocale __P((void));
    106  1.34    itojun static char *loadlocale __P((int));
    107  1.37      yamt static const char *__get_locale_env __P((int));
    108  1.24    itojun 
    109  1.24    itojun char *
    110  1.24    itojun __setlocale(category, locale)
    111  1.24    itojun 	int category;
    112  1.24    itojun 	const char *locale;
    113  1.24    itojun {
    114  1.27  jdolecek 	int i, loadlocale_success;
    115  1.16  christos 	size_t len;
    116  1.37      yamt 	const char *env, *r;
    117   1.5       jtc 
    118  1.20  tshiozak 	if (issetugid() ||
    119  1.24    itojun 	    (!_PathLocale && !(_PathLocale = getenv("PATH_LOCALE"))))
    120  1.24    itojun 		_PathLocale = _PATH_LOCALE;
    121   1.5       jtc 
    122   1.5       jtc 	if (category < 0 || category >= _LC_LAST)
    123   1.1       cgd 		return (NULL);
    124   1.5       jtc 
    125   1.5       jtc 	if (!locale)
    126   1.5       jtc 		return (category ?
    127   1.5       jtc 		    current_categories[category] : currentlocale());
    128   1.5       jtc 
    129   1.5       jtc 	/*
    130   1.5       jtc 	 * Default to the current locale for everything.
    131   1.5       jtc 	 */
    132   1.5       jtc 	for (i = 1; i < _LC_LAST; ++i)
    133  1.23    itojun 		(void)strlcpy(new_categories[i], current_categories[i],
    134  1.23    itojun 		    sizeof(new_categories[i]));
    135   1.5       jtc 
    136   1.5       jtc 	/*
    137   1.5       jtc 	 * Now go fill up new_categories from the locale argument
    138   1.5       jtc 	 */
    139   1.5       jtc 	if (!*locale) {
    140  1.37      yamt 		if (category == LC_ALL) {
    141   1.5       jtc 			for (i = 1; i < _LC_LAST; ++i) {
    142  1.37      yamt 				env = __get_locale_env(i);
    143  1.23    itojun 				(void)strlcpy(new_categories[i], env,
    144  1.23    itojun 				    sizeof(new_categories[i]));
    145   1.5       jtc 			}
    146   1.5       jtc 		}
    147  1.37      yamt 		else {
    148  1.37      yamt 			env = __get_locale_env(category);
    149  1.37      yamt 			(void)strlcpy(new_categories[category], env,
    150  1.37      yamt 				sizeof(new_categories[category]));
    151  1.37      yamt 		}
    152   1.8       mrg 	} else if (category) {
    153  1.23    itojun 		(void)strlcpy(new_categories[category], locale,
    154  1.23    itojun 		    sizeof(new_categories[category]));
    155   1.5       jtc 	} else {
    156   1.5       jtc 		if ((r = strchr(locale, '/')) == 0) {
    157   1.5       jtc 			for (i = 1; i < _LC_LAST; ++i) {
    158  1.23    itojun 				(void)strlcpy(new_categories[i], locale,
    159  1.23    itojun 				    sizeof(new_categories[i]));
    160   1.5       jtc 			}
    161   1.5       jtc 		} else {
    162  1.42     enami 			for (i = 1;;) {
    163  1.42     enami 				_DIAGASSERT(*r == '/' || *r == 0);
    164  1.42     enami 				_DIAGASSERT(*locale != 0);
    165  1.42     enami 				if (*locale == '/')
    166  1.42     enami 					return (NULL);	/* invalid format. */
    167  1.40  tshiozak 				len = r - locale;
    168  1.39    itojun 				if (len + 1 > sizeof(new_categories[i]))
    169  1.39    itojun 					return (NULL);	/* too long */
    170  1.39    itojun 				(void)memcpy(new_categories[i], locale, len);
    171  1.39    itojun 				new_categories[i][len] = '\0';
    172  1.42     enami 				if (*r == 0)
    173  1.42     enami 					break;
    174  1.42     enami 				_DIAGASSERT(*r == '/');
    175  1.42     enami 				if (*(locale = ++r) == 0)
    176  1.42     enami 					/* slash followed by NUL */
    177  1.42     enami 					return (NULL);
    178  1.42     enami 				/* skip until NUL or '/' */
    179  1.42     enami 				while (*r && *r != '/')
    180  1.42     enami 					r++;
    181  1.42     enami 				if (++i == _LC_LAST)
    182  1.42     enami 					return (NULL);	/* too many slashes. */
    183  1.38  tshiozak 			}
    184  1.42     enami 			if (i + 1 != _LC_LAST)
    185  1.42     enami 				return (NULL);	/* too few slashes. */
    186   1.5       jtc 		}
    187   1.5       jtc 	}
    188   1.5       jtc 
    189   1.5       jtc 	if (category)
    190   1.5       jtc 		return (loadlocale(category));
    191   1.5       jtc 
    192  1.27  jdolecek 	loadlocale_success = 0;
    193  1.22    itojun 	for (i = 1; i < _LC_LAST; ++i) {
    194  1.27  jdolecek 		if (loadlocale(i) != NULL)
    195  1.27  jdolecek 			loadlocale_success = 1;
    196  1.22    itojun 	}
    197  1.27  jdolecek 
    198  1.27  jdolecek 	/*
    199  1.27  jdolecek 	 * If all categories failed, return NULL; we don't need to back
    200  1.27  jdolecek 	 * changes off, since none happened.
    201  1.27  jdolecek 	 */
    202  1.27  jdolecek 	if (!loadlocale_success)
    203  1.27  jdolecek 		return NULL;
    204  1.27  jdolecek 
    205   1.9    kleink 	return (currentlocale());
    206   1.5       jtc }
    207   1.5       jtc 
    208   1.5       jtc static char *
    209   1.5       jtc currentlocale()
    210   1.5       jtc {
    211   1.5       jtc 	int i;
    212   1.5       jtc 
    213  1.23    itojun 	(void)strlcpy(current_locale_string, current_categories[1],
    214  1.23    itojun 	    sizeof(current_locale_string));
    215   1.5       jtc 
    216   1.5       jtc 	for (i = 2; i < _LC_LAST; ++i)
    217   1.5       jtc 		if (strcmp(current_categories[1], current_categories[i])) {
    218   1.5       jtc 			(void)snprintf(current_locale_string,
    219  1.36      yamt 			    sizeof(current_locale_string), "%s/%s/%s/%s/%s/%s",
    220   1.5       jtc 			    current_categories[1], current_categories[2],
    221   1.5       jtc 			    current_categories[3], current_categories[4],
    222  1.36      yamt 			    current_categories[5], current_categories[6]);
    223   1.5       jtc 			break;
    224   1.5       jtc 		}
    225   1.5       jtc 	return (current_locale_string);
    226   1.5       jtc }
    227   1.5       jtc 
    228  1.15    kleink static char *
    229   1.5       jtc loadlocale(category)
    230   1.5       jtc 	int category;
    231   1.5       jtc {
    232   1.5       jtc 	char name[PATH_MAX];
    233   1.5       jtc 
    234  1.37      yamt 	_DIAGASSERT(0 < category && category < _LC_LAST);
    235  1.37      yamt 
    236  1.11    kleink 	if (strcmp(new_categories[category], current_categories[category]) == 0)
    237   1.5       jtc 		return (current_categories[category]);
    238   1.5       jtc 
    239   1.5       jtc 	if (!strcmp(new_categories[category], "C") ||
    240  1.11    kleink 	    !strcmp(new_categories[category], "POSIX")) {
    241  1.11    kleink 
    242  1.11    kleink 		switch (category) {
    243  1.11    kleink 		case LC_CTYPE:
    244  1.32    itojun #ifdef WITH_RUNE
    245  1.24    itojun 			(void)_xpg4_setrunelocale("C");
    246  1.29    itojun 			(void)__runetable_to_netbsd_ctype("C");
    247  1.32    itojun #else
    248  1.32    itojun 			if (_ctype_ != _C_ctype_) {
    249  1.32    itojun 				/* LINTED const castaway */
    250  1.32    itojun 				free((void *)_ctype_);
    251  1.32    itojun 				_ctype_ = _C_ctype_;
    252  1.32    itojun 			}
    253  1.32    itojun 			if (_toupper_tab_ != _C_toupper_) {
    254  1.32    itojun 				/* LINTED const castaway */
    255  1.32    itojun 				free((void *)_toupper_tab_);
    256  1.32    itojun 				_toupper_tab_ = _C_toupper_;
    257  1.32    itojun 			}
    258  1.32    itojun 			if (_tolower_tab_ != _C_tolower_) {
    259  1.32    itojun 				/* LINTED const castaway */
    260  1.32    itojun 				free((void *)_tolower_tab_);
    261  1.32    itojun 				_tolower_tab_ = _C_tolower_;
    262  1.32    itojun 			}
    263  1.32    itojun #endif
    264  1.11    kleink 		}
    265   1.5       jtc 
    266  1.23    itojun 		(void)strlcpy(current_categories[category],
    267   1.7       mrg 		    new_categories[category],
    268  1.23    itojun 		    sizeof(current_categories[category]));
    269  1.11    kleink 		return current_categories[category];
    270   1.5       jtc 	}
    271   1.5       jtc 
    272   1.5       jtc 	(void)snprintf(name, sizeof(name), "%s/%s/%s",
    273  1.24    itojun 	    _PathLocale, new_categories[category], categories[category]);
    274   1.5       jtc 
    275   1.5       jtc 	switch (category) {
    276  1.11    kleink 	case LC_CTYPE:
    277  1.32    itojun #ifdef WITH_RUNE
    278  1.31    itojun 		if (_xpg4_setrunelocale(new_categories[category]))
    279  1.29    itojun 			return NULL;
    280  1.31    itojun 		if (__runetable_to_netbsd_ctype(new_categories[category])) {
    281  1.29    itojun 			/* very unfortunate, but need to go to "C" locale */
    282  1.29    itojun 			(void)_xpg4_setrunelocale("C");
    283  1.29    itojun 			(void)__runetable_to_netbsd_ctype("C");
    284  1.28    itojun 			return NULL;
    285  1.28    itojun 		}
    286  1.32    itojun #else
    287  1.32    itojun 		if (!__loadctype(name))
    288  1.32    itojun 			return NULL;
    289  1.32    itojun #endif
    290  1.37      yamt 		break;
    291  1.29    itojun 
    292  1.37      yamt 	case LC_MESSAGES:
    293  1.37      yamt 		/*
    294  1.37      yamt 		 * XXX we don't have LC_MESSAGES support yet,
    295  1.37      yamt 		 * but catopen may use the value of LC_MESSAGES category.
    296  1.37      yamt 		 * so return successfully if locale directory is present.
    297  1.37      yamt 		 */
    298  1.37      yamt 		(void)snprintf(name, sizeof(name), "%s/%s",
    299  1.37      yamt 			_PathLocale, new_categories[category]);
    300  1.37      yamt 		/* local */
    301  1.37      yamt 		{
    302  1.37      yamt 			struct stat st;
    303  1.37      yamt 			if (stat(name, &st) < 0)
    304  1.37      yamt 				return NULL;
    305  1.37      yamt 			if (!S_ISDIR(st.st_mode))
    306  1.37      yamt 				return NULL;
    307  1.37      yamt 		}
    308  1.37      yamt 		break;
    309  1.11    kleink 
    310  1.11    kleink 	case LC_COLLATE:
    311  1.11    kleink 	case LC_MONETARY:
    312  1.11    kleink 	case LC_NUMERIC:
    313  1.11    kleink 	case LC_TIME:
    314  1.11    kleink 		return NULL;
    315   1.5       jtc 	}
    316  1.11    kleink 
    317  1.37      yamt 	(void)strlcpy(current_categories[category],
    318  1.37      yamt 		new_categories[category],
    319  1.37      yamt 		sizeof(current_categories[category]));
    320  1.37      yamt 	return current_categories[category];
    321  1.37      yamt }
    322  1.37      yamt 
    323  1.37      yamt static const char *
    324  1.37      yamt __get_locale_env(category)
    325  1.37      yamt 	int category;
    326  1.37      yamt {
    327  1.37      yamt 	const char *env;
    328  1.37      yamt 
    329  1.37      yamt 	_DIAGASSERT(category != LC_ALL);
    330  1.37      yamt 
    331  1.37      yamt 	/* 1. check LC_ALL. */
    332  1.37      yamt 	env = getenv(categories[0]);
    333  1.37      yamt 
    334  1.37      yamt 	/* 2. check LC_* */
    335  1.37      yamt 	if (!env || !*env)
    336  1.37      yamt 		env = getenv(categories[category]);
    337  1.37      yamt 
    338  1.37      yamt 	/* 3. check LANG */
    339  1.37      yamt 	if (!env || !*env)
    340  1.37      yamt 		env = getenv("LANG");
    341  1.37      yamt 
    342  1.37      yamt 	/* 4. if none is set, fall to "C" */
    343  1.37      yamt 	if (!env || !*env || strchr(env, '/'))
    344  1.37      yamt 		env = "C";
    345  1.37      yamt 
    346  1.37      yamt 	return env;
    347   1.1       cgd }
    348