Home | History | Annotate | Line # | Download | only in locale
setlocale.c revision 1.17.4.1
      1 /*	$NetBSD: setlocale.c,v 1.17.4.1 2000/05/28 22:41:05 minoura 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. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #if defined(LIBC_SCCS) && !defined(lint)
     41 #if 0
     42 static char sccsid[] = "@(#)setlocale.c	8.1 (Berkeley) 7/4/93";
     43 #else
     44 #ifndef __RCSID
     45 #define __RCSID(a)
     46 #endif
     47 __RCSID("$NetBSD: setlocale.c,v 1.17.4.1 2000/05/28 22:41:05 minoura Exp $");
     48 #endif
     49 #endif /* LIBC_SCCS and not lint */
     50 
     51 #define _CTYPE_PRIVATE
     52 
     53 #include "namespace.h"
     54 #include <sys/localedef.h>
     55 #include <ctype.h>
     56 #include <errno.h>
     57 #include <limits.h>
     58 #define __SINGLE_BYTE_LOCALE_SOURCE__
     59 #include <locale.h>
     60 #include <paths.h>
     61 #include <stdio.h>
     62 #include <stdlib.h>
     63 #include <string.h>
     64 #ifndef WITH_RUNE
     65 #include "ctypeio.h"
     66 #else
     67 #include <sys/stat.h>
     68 #include "collate.h"
     69 #include "timelocal.h"
     70 extern void __runetable_to_netbsd_ctype __P((void));
     71 extern int _xpg4_setrunelocale __P((char *));
     72 int __fl_rune_singlebyte = 0;
     73 #ifdef __NetBSD__
     74 #define _NO_NATIVE_ISSETUGID
     75 #endif
     76 #endif
     77 
     78 /*
     79  * Category names for getenv()
     80  */
     81 static const char *const categories[_LC_LAST] = {
     82     "LC_ALL",
     83     "LC_COLLATE",
     84     "LC_CTYPE",
     85     "LC_MONETARY",
     86     "LC_NUMERIC",
     87     "LC_TIME",
     88     "LC_MESSAGES"
     89 };
     90 
     91 /*
     92  * Current locales for each category
     93  */
     94 static char current_categories[_LC_LAST][32] = {
     95     "C",
     96     "C",
     97     "C",
     98     "C",
     99     "C",
    100     "C",
    101     "C"
    102 };
    103 
    104 /*
    105  * The locales we are going to try and load
    106  */
    107 static char new_categories[_LC_LAST][32];
    108 
    109 static char current_locale_string[_LC_LAST * 33];
    110 #ifndef WITH_RUNE
    111 static char *PathLocale;
    112 static char *PathLocaleUnshared;
    113 /*
    114  * Data paths for each category.
    115  */
    116 static char **paths[_LC_LAST] = {
    117     &PathLocale,			/* LC_ALL */
    118     &PathLocaleUnshared,		/* LC_COLLATE */
    119     &PathLocaleUnshared,		/* LC_CTYPE */
    120     &PathLocale,			/* LC_MONETARY */
    121     &PathLocale,			/* LC_NUMERIC */
    122     &PathLocale,			/* LC_TIME */
    123     &PathLocale,			/* LC_MESSAGES */
    124 };
    125 #else
    126 #include "setlocale.h"
    127 #endif
    128 
    129 static char	*currentlocale __P((void));
    130 static char	*loadlocale __P((int));
    131 #ifdef WITH_RUNE
    132 static int	stub_load_locale __P((const char *, int));
    133 #endif
    134 
    135 char *
    136 __setlocale_mb(category, locale)
    137 	int category;
    138 	const char *locale;
    139 {
    140 	int i;
    141 	size_t len;
    142 	char *env, *r;
    143 	int found;
    144 
    145 #ifndef WITH_RUNE
    146 	/*
    147 	 * XXX potential security problem here with set-id programs
    148 	 * being able to read files the user can not normally read.
    149 	 */
    150 	if (!PathLocale) {
    151 		if(getenv("PATH_LOCALE")) {
    152 			PathLocale = strdup(getenv("PATH_LOCALE"));
    153 			PathLocaleUnshared = PathLocale;
    154 		} else {
    155 			PathLocale = _PATH_LOCALE;
    156 			PathLocaleUnshared = _PATH_LOCALE_UNSHARED;
    157 		}
    158 	}
    159 #else
    160 	if (_PathLocale == NULL) {
    161 #ifndef _NO_NATIVE_ISSETUGID
    162 		char *p = getenv("PATH_LOCALE");
    163 
    164 		if (p != NULL && !issetugid()) {
    165 			if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
    166 			    1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
    167 				return(EFAULT);
    168 			_PathLocale = strdup(p);
    169 			if (_PathLocale == NULL)
    170 				return (errno);
    171 			_PathLocaleUnshared = _PathLocale;
    172 		} else {
    173 			_PathLocale = _PATH_LOCALE;
    174 			_PathLocaleUnshared = _PATH_LOCALE_UNSHARED;
    175 		}
    176 #else
    177 		_PathLocale = _PATH_LOCALE;
    178 		_PathLocaleUnshared = _PATH_LOCALE_UNSHARED;
    179 #endif
    180 	}
    181 #endif
    182 
    183 	if (category < 0 || category >= _LC_LAST)
    184 		return (NULL);
    185 
    186 	if (!locale)
    187 		return (category ?
    188 		    current_categories[category] : currentlocale());
    189 
    190 	/*
    191 	 * Default to the current locale for everything.
    192 	 */
    193 	for (i = 1; i < _LC_LAST; ++i)
    194 		(void)strncpy(new_categories[i], current_categories[i],
    195 		    sizeof(new_categories[i]) - 1);
    196 
    197 	/*
    198 	 * Now go fill up new_categories from the locale argument
    199 	 */
    200 	if (!*locale) {
    201 		env = getenv(categories[category]);
    202 
    203 		if (!env || !*env)
    204 			env = getenv(categories[0]);
    205 
    206 		if (!env || !*env)
    207 			env = getenv("LANG");
    208 
    209 		if (!env || !*env)
    210 			env = "C";
    211 
    212 		(void)strncpy(new_categories[category], env, 31);
    213 		new_categories[category][31] = 0;
    214 		if (!category) {
    215 			for (i = 1; i < _LC_LAST; ++i) {
    216 				if (!(env = getenv(categories[i])) || !*env)
    217 					env = new_categories[0];
    218 				(void)strncpy(new_categories[i], env, 31);
    219 				new_categories[i][31] = 0;
    220 			}
    221 		}
    222 	} else if (category) {
    223 		(void)strncpy(new_categories[category], locale, 31);
    224 		new_categories[category][31] = 0;
    225 	} else {
    226 		if ((r = strchr(locale, '/')) == 0) {
    227 			for (i = 1; i < _LC_LAST; ++i) {
    228 				(void)strncpy(new_categories[i], locale, 31);
    229 				new_categories[i][31] = 0;
    230 			}
    231 		} else {
    232 			for (i = 1; r[1] == '/'; ++r);
    233 			if (!r[1])
    234 				return (NULL);	/* Hmm, just slashes... */
    235 			do {
    236 				len = r - locale > 31 ? 31 : r - locale;
    237 				(void)strncpy(new_categories[i++], locale, len);
    238 				new_categories[i++][len] = 0;
    239 				locale = r;
    240 				while (*locale == '/')
    241 				    ++locale;
    242 				while (*++r && *r != '/');
    243 			} while (*locale);
    244 			while (i < _LC_LAST)
    245 				(void)strncpy(new_categories[i],
    246 				    new_categories[i-1],
    247 				    sizeof(new_categories[i]) - 1);
    248 		}
    249 	}
    250 
    251 	if (category)
    252 		return (loadlocale(category));
    253 
    254 	found = 0;
    255 	for (i = 1; i < _LC_LAST; ++i) {
    256 		if ( loadlocale(i) != NULL )
    257 			found = 1;
    258 		else if ( !category ) {
    259 			found = 0;
    260 			break;
    261 		}
    262 	}
    263 
    264 	return (found ? currentlocale():NULL);
    265 }
    266 
    267 static char *
    268 currentlocale()
    269 {
    270 	int i;
    271 
    272 	(void)strncpy(current_locale_string, current_categories[1],
    273 	    sizeof(current_locale_string) - 1);
    274 
    275 	for (i = 2; i < _LC_LAST; ++i)
    276 		if (strcmp(current_categories[1], current_categories[i])) {
    277 			(void)snprintf(current_locale_string,
    278 			    sizeof(current_locale_string), "%s/%s/%s/%s/%s",
    279 			    current_categories[1], current_categories[2],
    280 			    current_categories[3], current_categories[4],
    281 			    current_categories[5]);
    282 			break;
    283 		}
    284 	return (current_locale_string);
    285 }
    286 
    287 static char *
    288 loadlocale(category)
    289 	int category;
    290 {
    291 #ifndef WITH_RUNE
    292 	char name[PATH_MAX];
    293 #endif
    294 
    295 	if (strcmp(new_categories[category], current_categories[category]) == 0)
    296 		return (current_categories[category]);
    297 
    298 	if (!strcmp(new_categories[category], "C") ||
    299 	    !strcmp(new_categories[category], "POSIX")) {
    300 
    301 		switch (category) {
    302 		case LC_CTYPE:
    303 			if (_ctype_ != _C_ctype_) {
    304 				/* LINTED const castaway */
    305 				free((void *)_ctype_);
    306 				_ctype_ = _C_ctype_;
    307 			}
    308 			if (_toupper_tab_ != _C_toupper_) {
    309 				/* LINTED const castaway */
    310 				free((void *)_toupper_tab_);
    311 				_toupper_tab_ = _C_toupper_;
    312 			}
    313 			if (_tolower_tab_ != _C_tolower_) {
    314 				/* LINTED const castaway */
    315 				free((void *)_tolower_tab_);
    316 				_tolower_tab_ = _C_tolower_;
    317 			}
    318 #ifdef WITH_RUNE
    319                         (void)_xpg4_setrunelocale("C");
    320 #endif
    321 		}
    322 
    323 		(void)strncpy(current_categories[category],
    324 		    new_categories[category],
    325 		    sizeof(current_categories[category]) - 1);
    326 		return current_categories[category];
    327 	}
    328 
    329 #ifndef WITH_RUNE
    330 	/*
    331 	 * Some day we will actually look at this file.
    332 	 */
    333 	(void)snprintf(name, sizeof(name), "%s/%s/%s",
    334 	    *paths[category],
    335 	    new_categories[category], categories[category]);
    336 #endif
    337 
    338 	switch (category) {
    339 	case LC_CTYPE:
    340 #ifndef WITH_RUNE
    341 		if (__loadctype(name)) {
    342 #else
    343                 if (!_xpg4_setrunelocale(new_categories[category])) {
    344                         __runetable_to_netbsd_ctype();
    345 #endif
    346 			(void)strncpy(current_categories[category],
    347 			    new_categories[category],
    348 			    sizeof(current_categories[category]) - 1);
    349 			return current_categories[category];
    350                 }
    351 		return NULL;
    352 
    353 	case LC_COLLATE:
    354 #ifdef WITH_RUNE
    355 		if (__collate_load_tables(new_categories[category]) == 0)
    356 			return new_categories[category];
    357 		(void)__collate_load_tables(current_categories[category]);
    358 		return NULL;
    359 #else
    360 		/* FALLTHROUGH */
    361 #endif
    362 	case LC_TIME:
    363 #ifdef WITH_RUNE
    364 		if (__time_load_locale(new_categories[category]) == 0)
    365 			return new_categories[category];
    366 		(void)__time_load_locale(current_categories[category]);
    367 		return NULL;
    368 
    369 #else
    370 		/* FALLTHROUGH */
    371 #endif
    372 	case LC_MESSAGES:
    373 	case LC_MONETARY:
    374 	case LC_NUMERIC:
    375 #ifdef WITH_RUNE
    376 		if (stub_load_locale(new_categories[category], category) == 0)
    377 			return new_categories[category];
    378 		stub_load_locale(current_categories[category], category);
    379 		return NULL;
    380 #else
    381 		return NULL;
    382 #endif
    383 	}
    384 
    385 	return NULL;
    386 }
    387 
    388 /*
    389  * Temporary code until we implement actual LC_* code.
    390  */
    391 static int
    392 stub_load_locale(encoding, category)
    393  const char *encoding;
    394  int category;
    395 {
    396 	char name[PATH_MAX];
    397 	struct stat st;
    398 
    399 	if (!encoding)
    400 		return(1);
    401 	/*
    402 	 * The "C" and "POSIX" locale are always here.
    403 	 */
    404 	if (!strcmp(encoding, "C") || !strcmp(encoding, "POSIX"))
    405 		return(0);
    406 	if (!_PathLocale)	/* XXX: shouldn't we initialize this? */
    407 		return(1);
    408 	/* Range checking not needed, encoding has fixed size */
    409 	strcpy(name, *_LocalePaths[category]);
    410 	strcat(name, "/");
    411 	strcat(name, encoding);
    412 #if 0
    413 	/*
    414 	 * Some day we will actually look at this file.
    415 	 */
    416 #endif
    417 	return (stat(name, &st) != 0 || !S_ISDIR(st.st_mode));
    418 }
    419