Home | History | Annotate | Line # | Download | only in locale
setlocale.c revision 1.17.4.2
      1 /*	$NetBSD: setlocale.c,v 1.17.4.2 2000/06/23 16:58:58 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.2 2000/06/23 16:58:58 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 #include <unistd.h>
     65 #ifndef WITH_RUNE
     66 #include "ctypeio.h"
     67 #else
     68 #include <sys/stat.h>
     69 #include "collate.h"
     70 #include "timelocal.h"
     71 extern void __runetable_to_netbsd_ctype __P((void));
     72 extern int _xpg4_setrunelocale __P((char *));
     73 int __fl_rune_singlebyte = 0;
     74 #ifdef __NetBSD__
     75 #define _NO_NATIVE_ISSETUGID
     76 #endif
     77 #endif
     78 
     79 /*
     80  * Category names for getenv()
     81  */
     82 static const char *const categories[_LC_LAST] = {
     83     "LC_ALL",
     84     "LC_COLLATE",
     85     "LC_CTYPE",
     86     "LC_MONETARY",
     87     "LC_NUMERIC",
     88     "LC_TIME",
     89     "LC_MESSAGES"
     90 };
     91 
     92 /*
     93  * Current locales for each category
     94  */
     95 static char current_categories[_LC_LAST][32] = {
     96     "C",
     97     "C",
     98     "C",
     99     "C",
    100     "C",
    101     "C",
    102     "C"
    103 };
    104 
    105 /*
    106  * The locales we are going to try and load
    107  */
    108 static char new_categories[_LC_LAST][32];
    109 
    110 static char current_locale_string[_LC_LAST * 33];
    111 #ifndef WITH_RUNE
    112 static char *PathLocale;
    113 static char *PathLocaleUnshared;
    114 /*
    115  * Data paths for each category.
    116  */
    117 static char **paths[_LC_LAST] = {
    118     &PathLocale,			/* LC_ALL */
    119     &PathLocaleUnshared,		/* LC_COLLATE */
    120     &PathLocaleUnshared,		/* LC_CTYPE */
    121     &PathLocale,			/* LC_MONETARY */
    122     &PathLocale,			/* LC_NUMERIC */
    123     &PathLocale,			/* LC_TIME */
    124     &PathLocale,			/* LC_MESSAGES */
    125 };
    126 #else
    127 #include "setlocale.h"
    128 #endif
    129 
    130 static char	*currentlocale __P((void));
    131 static char	*loadlocale __P((int));
    132 #ifdef WITH_RUNE
    133 static int	stub_load_locale __P((const char *, int));
    134 #endif
    135 
    136 char *
    137 __setlocale_mb(category, locale)
    138 	int category;
    139 	const char *locale;
    140 {
    141 	int i;
    142 	size_t len;
    143 	char *env, *r;
    144 	int found;
    145 
    146 #ifndef WITH_RUNE
    147 	/*
    148 	 * XXX potential security problem here with set-id programs
    149 	 * being able to read files the user can not normally read.
    150 	 */
    151 	if (!PathLocale) {
    152 		if(getenv("PATH_LOCALE")) {
    153 			PathLocale = strdup(getenv("PATH_LOCALE"));
    154 			PathLocaleUnshared = PathLocale;
    155 		} else {
    156 			PathLocale = _PATH_LOCALE;
    157 			PathLocaleUnshared = _PATH_LOCALE_UNSHARED;
    158 		}
    159 	}
    160 #else
    161 	if (_PathLocale == NULL) {
    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 				errno = EFAULT;
    168 				return(NULL);
    169 			}
    170 			_PathLocale = strdup(p);
    171 			if (_PathLocale == NULL)
    172 				return (NULL);
    173 			_PathLocaleUnshared = _PathLocale;
    174 		} else {
    175 			_PathLocale = _PATH_LOCALE;
    176 			_PathLocaleUnshared = _PATH_LOCALE_UNSHARED;
    177 		}
    178 	}
    179 #endif
    180 
    181 	if (category < 0 || category >= _LC_LAST)
    182 		return (NULL);
    183 
    184 	if (!locale)
    185 		return (category ?
    186 		    current_categories[category] : currentlocale());
    187 
    188 	/*
    189 	 * Default to the current locale for everything.
    190 	 */
    191 	for (i = 1; i < _LC_LAST; ++i)
    192 		(void)strncpy(new_categories[i], current_categories[i],
    193 		    sizeof(new_categories[i]) - 1);
    194 
    195 	/*
    196 	 * Now go fill up new_categories from the locale argument
    197 	 */
    198 	if (!*locale) {
    199 		env = getenv(categories[category]);
    200 
    201 		if (!env || !*env)
    202 			env = getenv(categories[0]);
    203 
    204 		if (!env || !*env)
    205 			env = getenv("LANG");
    206 
    207 		if (!env || !*env)
    208 			env = "C";
    209 
    210 		(void)strncpy(new_categories[category], env, 31);
    211 		new_categories[category][31] = 0;
    212 		if (!category) {
    213 			for (i = 1; i < _LC_LAST; ++i) {
    214 				if (!(env = getenv(categories[i])) || !*env)
    215 					env = new_categories[0];
    216 				(void)strncpy(new_categories[i], env, 31);
    217 				new_categories[i][31] = 0;
    218 			}
    219 		}
    220 	} else if (category) {
    221 		(void)strncpy(new_categories[category], locale, 31);
    222 		new_categories[category][31] = 0;
    223 	} else {
    224 		if ((r = strchr(locale, '/')) == 0) {
    225 			for (i = 1; i < _LC_LAST; ++i) {
    226 				(void)strncpy(new_categories[i], locale, 31);
    227 				new_categories[i][31] = 0;
    228 			}
    229 		} else {
    230 			for (i = 1; r[1] == '/'; ++r);
    231 			if (!r[1])
    232 				return (NULL);	/* Hmm, just slashes... */
    233 			do {
    234 				len = r - locale > 31 ? 31 : r - locale;
    235 				(void)strncpy(new_categories[i++], locale, len);
    236 				new_categories[i++][len] = 0;
    237 				locale = r;
    238 				while (*locale == '/')
    239 				    ++locale;
    240 				while (*++r && *r != '/');
    241 			} while (*locale);
    242 			while (i < _LC_LAST)
    243 				(void)strncpy(new_categories[i],
    244 				    new_categories[i-1],
    245 				    sizeof(new_categories[i]) - 1);
    246 		}
    247 	}
    248 
    249 	if (category)
    250 		return (loadlocale(category));
    251 
    252 	found = 0;
    253 	for (i = 1; i < _LC_LAST; ++i) {
    254 		if ( loadlocale(i) != NULL )
    255 			found = 1;
    256 		else if ( !category ) {
    257 			found = 0;
    258 			break;
    259 		}
    260 	}
    261 
    262 	return (found ? currentlocale():NULL);
    263 }
    264 
    265 static char *
    266 currentlocale()
    267 {
    268 	int i;
    269 
    270 	(void)strncpy(current_locale_string, current_categories[1],
    271 	    sizeof(current_locale_string) - 1);
    272 
    273 	for (i = 2; i < _LC_LAST; ++i)
    274 		if (strcmp(current_categories[1], current_categories[i])) {
    275 			(void)snprintf(current_locale_string,
    276 			    sizeof(current_locale_string), "%s/%s/%s/%s/%s",
    277 			    current_categories[1], current_categories[2],
    278 			    current_categories[3], current_categories[4],
    279 			    current_categories[5]);
    280 			break;
    281 		}
    282 	return (current_locale_string);
    283 }
    284 
    285 static char *
    286 loadlocale(category)
    287 	int category;
    288 {
    289 #ifndef WITH_RUNE
    290 	char name[PATH_MAX];
    291 #endif
    292 
    293 	if (strcmp(new_categories[category], current_categories[category]) == 0)
    294 		return (current_categories[category]);
    295 
    296 	if (!strcmp(new_categories[category], "C") ||
    297 	    !strcmp(new_categories[category], "POSIX")) {
    298 
    299 		switch (category) {
    300 		case LC_CTYPE:
    301 			if (_ctype_ != _C_ctype_) {
    302 				/* LINTED const castaway */
    303 				free((void *)_ctype_);
    304 				_ctype_ = _C_ctype_;
    305 			}
    306 			if (_toupper_tab_ != _C_toupper_) {
    307 				/* LINTED const castaway */
    308 				free((void *)_toupper_tab_);
    309 				_toupper_tab_ = _C_toupper_;
    310 			}
    311 			if (_tolower_tab_ != _C_tolower_) {
    312 				/* LINTED const castaway */
    313 				free((void *)_tolower_tab_);
    314 				_tolower_tab_ = _C_tolower_;
    315 			}
    316 #ifdef WITH_RUNE
    317 			(void)_xpg4_setrunelocale("C");
    318 #endif
    319 		}
    320 
    321 		(void)strncpy(current_categories[category],
    322 		    new_categories[category],
    323 		    sizeof(current_categories[category]) - 1);
    324 		return current_categories[category];
    325 	}
    326 
    327 #ifndef WITH_RUNE
    328 	/*
    329 	 * Some day we will actually look at this file.
    330 	 */
    331 	(void)snprintf(name, sizeof(name), "%s/%s/%s",
    332 	    *paths[category],
    333 	    new_categories[category], categories[category]);
    334 #endif
    335 
    336 	switch (category) {
    337 	case LC_CTYPE:
    338 #ifndef WITH_RUNE
    339 		if (__loadctype(name)) {
    340 #else
    341 		if (!_xpg4_setrunelocale(new_categories[category])) {
    342 			__runetable_to_netbsd_ctype();
    343 #endif
    344 			(void)strncpy(current_categories[category],
    345 			    new_categories[category],
    346 			    sizeof(current_categories[category]) - 1);
    347 			return current_categories[category];
    348 		}
    349 		return NULL;
    350 
    351 	case LC_COLLATE:
    352 #ifdef WITH_RUNE
    353 		if (__collate_load_tables(new_categories[category]) == 0)
    354 			return new_categories[category];
    355 		(void)__collate_load_tables(current_categories[category]);
    356 		return NULL;
    357 #else
    358 		/* FALLTHROUGH */
    359 #endif
    360 	case LC_TIME:
    361 #ifdef WITH_RUNE
    362 		if (__time_load_locale(new_categories[category]) == 0)
    363 			return new_categories[category];
    364 		(void)__time_load_locale(current_categories[category]);
    365 		return NULL;
    366 
    367 #else
    368 		/* FALLTHROUGH */
    369 #endif
    370 	case LC_MESSAGES:
    371 	case LC_MONETARY:
    372 	case LC_NUMERIC:
    373 #ifdef WITH_RUNE
    374 		if (stub_load_locale(new_categories[category], category) == 0)
    375 			return new_categories[category];
    376 		stub_load_locale(current_categories[category], category);
    377 		return NULL;
    378 #else
    379 		return NULL;
    380 #endif
    381 	}
    382 
    383 	return NULL;
    384 }
    385 
    386 /*
    387  * Temporary code until we implement actual LC_* code.
    388  */
    389 static int
    390 stub_load_locale(encoding, category)
    391  const char *encoding;
    392  int category;
    393 {
    394 	char name[PATH_MAX];
    395 	struct stat st;
    396 
    397 	if (!encoding)
    398 		return(1);
    399 	/*
    400 	 * The "C" and "POSIX" locale are always here.
    401 	 */
    402 	if (!strcmp(encoding, "C") || !strcmp(encoding, "POSIX"))
    403 		return(0);
    404 	if (!_PathLocale)	/* XXX: shouldn't we initialize this? */
    405 		return(1);
    406 	/* Range checking not needed, encoding has fixed size */
    407 	strcpy(name, *_LocalePaths[category]);
    408 	strcat(name, "/");
    409 	strcat(name, encoding);
    410 #if 0
    411 	/*
    412 	 * Some day we will actually look at this file.
    413 	 */
    414 #endif
    415 	return (stat(name, &st) != 0 || !S_ISDIR(st.st_mode));
    416 }
    417