Home | History | Annotate | Line # | Download | only in locale
setlocale.c revision 1.17.6.5
      1  1.17.6.5    itojun /*	$NetBSD: setlocale.c,v 1.17.6.5 2002/09/04 01:05:40 itojun 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.17.6.5    itojun __RCSID("$NetBSD: setlocale.c,v 1.17.6.5 2002/09/04 01:05:40 itojun 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.11    kleink #include <ctype.h>
     53      1.11    kleink #include <limits.h>
     54  1.17.6.1  tshiozak #define __SETLOCALE_SOURCE__
     55       1.1       cgd #include <locale.h>
     56      1.11    kleink #include <paths.h>
     57       1.6       jtc #include <stdio.h>
     58       1.5       jtc #include <stdlib.h>
     59       1.1       cgd #include <string.h>
     60  1.17.6.3  sommerfe #include <unistd.h>
     61      1.11    kleink #include "ctypeio.h"
     62       1.1       cgd 
     63       1.5       jtc /*
     64       1.5       jtc  * Category names for getenv()
     65       1.5       jtc  */
     66      1.13   mycroft static const char *const categories[_LC_LAST] = {
     67       1.5       jtc     "LC_ALL",
     68       1.5       jtc     "LC_COLLATE",
     69       1.5       jtc     "LC_CTYPE",
     70       1.5       jtc     "LC_MONETARY",
     71       1.5       jtc     "LC_NUMERIC",
     72       1.5       jtc     "LC_TIME",
     73       1.5       jtc     "LC_MESSAGES"
     74       1.5       jtc };
     75       1.1       cgd 
     76       1.1       cgd /*
     77       1.5       jtc  * Current locales for each category
     78       1.5       jtc  */
     79       1.5       jtc static char current_categories[_LC_LAST][32] = {
     80       1.5       jtc     "C",
     81       1.5       jtc     "C",
     82       1.5       jtc     "C",
     83       1.5       jtc     "C",
     84       1.5       jtc     "C",
     85       1.5       jtc     "C",
     86       1.5       jtc     "C"
     87       1.5       jtc };
     88  1.17.6.2    kleink 
     89  1.17.6.2    kleink size_t __mb_cur_max = 1;
     90       1.5       jtc 
     91       1.5       jtc /*
     92       1.5       jtc  * The locales we are going to try and load
     93       1.1       cgd  */
     94       1.5       jtc static char new_categories[_LC_LAST][32];
     95       1.5       jtc 
     96  1.17.6.4     jhawk /*
     97  1.17.6.4     jhawk  * Backup area to back out changes on failure
     98  1.17.6.4     jhawk  */
     99  1.17.6.4     jhawk static char saved_categories[_LC_LAST][32];
    100  1.17.6.4     jhawk 
    101       1.5       jtc static char current_locale_string[_LC_LAST * 33];
    102       1.5       jtc static char *PathLocale;
    103       1.5       jtc 
    104       1.5       jtc static char	*currentlocale __P((void));
    105       1.5       jtc static char	*loadlocale __P((int));
    106       1.5       jtc 
    107       1.1       cgd char *
    108  1.17.6.1  tshiozak __setlocale_mb_len_max_32(category, locale)
    109       1.1       cgd 	int category;
    110       1.1       cgd 	const char *locale;
    111       1.1       cgd {
    112  1.17.6.4     jhawk 	int i, loadlocale_success;
    113      1.16  christos 	size_t len;
    114       1.5       jtc 	char *env, *r;
    115       1.5       jtc 
    116  1.17.6.3  sommerfe 	if (issetugid() ||
    117  1.17.6.3  sommerfe 	    (!PathLocale && !(PathLocale = getenv("PATH_LOCALE"))))
    118       1.5       jtc 		PathLocale = _PATH_LOCALE;
    119       1.5       jtc 
    120       1.5       jtc 	if (category < 0 || category >= _LC_LAST)
    121       1.1       cgd 		return (NULL);
    122       1.5       jtc 
    123       1.5       jtc 	if (!locale)
    124       1.5       jtc 		return (category ?
    125       1.5       jtc 		    current_categories[category] : currentlocale());
    126       1.5       jtc 
    127       1.5       jtc 	/*
    128       1.5       jtc 	 * Default to the current locale for everything.
    129       1.5       jtc 	 */
    130       1.5       jtc 	for (i = 1; i < _LC_LAST; ++i)
    131  1.17.6.4     jhawk 		(void)strlcpy(new_categories[i], current_categories[i],
    132  1.17.6.4     jhawk 		    sizeof(new_categories[i]));
    133       1.5       jtc 
    134       1.5       jtc 	/*
    135       1.5       jtc 	 * Now go fill up new_categories from the locale argument
    136       1.5       jtc 	 */
    137       1.5       jtc 	if (!*locale) {
    138       1.5       jtc 		env = getenv(categories[category]);
    139       1.5       jtc 
    140       1.9    kleink 		if (!env || !*env)
    141       1.5       jtc 			env = getenv(categories[0]);
    142       1.5       jtc 
    143       1.9    kleink 		if (!env || !*env)
    144       1.5       jtc 			env = getenv("LANG");
    145       1.5       jtc 
    146  1.17.6.3  sommerfe 		if (!env || !*env || strchr(env, '/'))
    147       1.5       jtc 			env = "C";
    148       1.5       jtc 
    149  1.17.6.4     jhawk 		(void)strlcpy(new_categories[category], env,
    150  1.17.6.4     jhawk 		    sizeof(new_categories[category]));
    151       1.5       jtc 		if (!category) {
    152       1.5       jtc 			for (i = 1; i < _LC_LAST; ++i) {
    153  1.17.6.4     jhawk 				env = getenv(categories[i]);
    154  1.17.6.4     jhawk 				if (!env || !*env || strchr(env, '/'))
    155       1.5       jtc 					env = new_categories[0];
    156  1.17.6.4     jhawk 				(void)strlcpy(new_categories[i], env,
    157  1.17.6.4     jhawk 				    sizeof(new_categories[i]));
    158       1.5       jtc 			}
    159       1.5       jtc 		}
    160       1.8       mrg 	} else if (category) {
    161  1.17.6.4     jhawk 		(void)strlcpy(new_categories[category], locale,
    162  1.17.6.4     jhawk 		    sizeof(new_categories[category]));
    163       1.5       jtc 	} else {
    164       1.5       jtc 		if ((r = strchr(locale, '/')) == 0) {
    165       1.5       jtc 			for (i = 1; i < _LC_LAST; ++i) {
    166  1.17.6.4     jhawk 				(void)strlcpy(new_categories[i], locale,
    167  1.17.6.4     jhawk 				    sizeof(new_categories[i]));
    168       1.5       jtc 			}
    169       1.5       jtc 		} else {
    170  1.17.6.4     jhawk 			for (i = 1; r[1] == '/'; ++r)
    171  1.17.6.4     jhawk 				;
    172       1.5       jtc 			if (!r[1])
    173       1.5       jtc 				return (NULL);	/* Hmm, just slashes... */
    174       1.5       jtc 			do {
    175  1.17.6.5    itojun 				if (i == _LC_LAST)
    176  1.17.6.5    itojun 					return (NULL);	/* too many slashes. */
    177  1.17.6.5    itojun 				len = r - locale;
    178  1.17.6.5    itojun 				if (len + 1 > sizeof(new_categories[i]))
    179  1.17.6.5    itojun 					return (NULL);	/* too long */
    180  1.17.6.5    itojun 				(void)memcpy(new_categories[i], locale, len);
    181  1.17.6.5    itojun 				new_categories[i][len] = '\0';
    182  1.17.6.5    itojun 				i++;
    183       1.5       jtc 				locale = r;
    184       1.5       jtc 				while (*locale == '/')
    185  1.17.6.5    itojun 					++locale;
    186       1.5       jtc 				while (*++r && *r != '/');
    187       1.5       jtc 			} while (*locale);
    188  1.17.6.5    itojun 			while (i < _LC_LAST) {
    189  1.17.6.4     jhawk 				(void)strlcpy(new_categories[i],
    190  1.17.6.4     jhawk 				    new_categories[i - 1],
    191  1.17.6.4     jhawk 				    sizeof(new_categories[i]));
    192  1.17.6.5    itojun 				i++;
    193  1.17.6.5    itojun 			}
    194       1.5       jtc 		}
    195       1.5       jtc 	}
    196       1.5       jtc 
    197       1.5       jtc 	if (category)
    198       1.5       jtc 		return (loadlocale(category));
    199       1.5       jtc 
    200  1.17.6.4     jhawk 	loadlocale_success = 0;
    201  1.17.6.4     jhawk 	for (i = 1; i < _LC_LAST; ++i) {
    202  1.17.6.4     jhawk 		(void)strlcpy(saved_categories[i], current_categories[i],
    203  1.17.6.4     jhawk 		    sizeof(saved_categories[i]));
    204  1.17.6.4     jhawk 		if (loadlocale(i) != NULL)
    205  1.17.6.4     jhawk 			loadlocale_success = 1;
    206  1.17.6.4     jhawk 	}
    207  1.17.6.4     jhawk 
    208  1.17.6.4     jhawk 	/*
    209  1.17.6.4     jhawk 	 * If all categories failed, return NULL; we don't need to back
    210  1.17.6.4     jhawk 	 * changes off, since none happened.
    211  1.17.6.4     jhawk 	 */
    212  1.17.6.4     jhawk 	if (!loadlocale_success)
    213  1.17.6.4     jhawk 		return NULL;
    214       1.9    kleink 
    215       1.9    kleink 	return (currentlocale());
    216       1.5       jtc }
    217       1.5       jtc 
    218       1.5       jtc static char *
    219       1.5       jtc currentlocale()
    220       1.5       jtc {
    221       1.5       jtc 	int i;
    222       1.5       jtc 
    223  1.17.6.4     jhawk 	(void)strlcpy(current_locale_string, current_categories[1],
    224  1.17.6.4     jhawk 	    sizeof(current_locale_string));
    225       1.5       jtc 
    226       1.5       jtc 	for (i = 2; i < _LC_LAST; ++i)
    227       1.5       jtc 		if (strcmp(current_categories[1], current_categories[i])) {
    228       1.5       jtc 			(void)snprintf(current_locale_string,
    229       1.5       jtc 			    sizeof(current_locale_string), "%s/%s/%s/%s/%s",
    230       1.5       jtc 			    current_categories[1], current_categories[2],
    231       1.5       jtc 			    current_categories[3], current_categories[4],
    232       1.5       jtc 			    current_categories[5]);
    233       1.5       jtc 			break;
    234       1.5       jtc 		}
    235       1.5       jtc 	return (current_locale_string);
    236       1.5       jtc }
    237       1.5       jtc 
    238      1.15    kleink static char *
    239       1.5       jtc loadlocale(category)
    240       1.5       jtc 	int category;
    241       1.5       jtc {
    242       1.5       jtc 	char name[PATH_MAX];
    243       1.5       jtc 
    244      1.11    kleink 	if (strcmp(new_categories[category], current_categories[category]) == 0)
    245       1.5       jtc 		return (current_categories[category]);
    246       1.5       jtc 
    247       1.5       jtc 	if (!strcmp(new_categories[category], "C") ||
    248      1.11    kleink 	    !strcmp(new_categories[category], "POSIX")) {
    249      1.11    kleink 
    250      1.11    kleink 		switch (category) {
    251      1.11    kleink 		case LC_CTYPE:
    252      1.11    kleink 			if (_ctype_ != _C_ctype_) {
    253      1.16  christos 				/* LINTED const castaway */
    254      1.11    kleink 				free((void *)_ctype_);
    255      1.11    kleink 				_ctype_ = _C_ctype_;
    256      1.11    kleink 			}
    257      1.11    kleink 			if (_toupper_tab_ != _C_toupper_) {
    258      1.16  christos 				/* LINTED const castaway */
    259      1.11    kleink 				free((void *)_toupper_tab_);
    260      1.11    kleink 				_toupper_tab_ = _C_toupper_;
    261      1.11    kleink 			}
    262      1.11    kleink 			if (_tolower_tab_ != _C_tolower_) {
    263      1.16  christos 				/* LINTED const castaway */
    264      1.11    kleink 				free((void *)_tolower_tab_);
    265      1.11    kleink 				_tolower_tab_ = _C_tolower_;
    266      1.11    kleink 			}
    267      1.11    kleink 		}
    268       1.5       jtc 
    269  1.17.6.4     jhawk 		(void)strlcpy(current_categories[category],
    270       1.7       mrg 		    new_categories[category],
    271  1.17.6.4     jhawk 		    sizeof(current_categories[category]));
    272      1.11    kleink 		return current_categories[category];
    273       1.5       jtc 	}
    274       1.5       jtc 
    275       1.5       jtc 	/*
    276       1.5       jtc 	 * Some day we will actually look at this file.
    277       1.5       jtc 	 */
    278       1.5       jtc 	(void)snprintf(name, sizeof(name), "%s/%s/%s",
    279       1.5       jtc 	    PathLocale, new_categories[category], categories[category]);
    280       1.5       jtc 
    281       1.5       jtc 	switch (category) {
    282      1.11    kleink 	case LC_CTYPE:
    283      1.11    kleink 		if (__loadctype(name)) {
    284  1.17.6.4     jhawk 			(void)strlcpy(current_categories[category],
    285      1.11    kleink 			    new_categories[category],
    286  1.17.6.4     jhawk 			    sizeof(current_categories[category]));
    287      1.11    kleink 			return current_categories[category];
    288      1.11    kleink 		}
    289      1.11    kleink 		return NULL;
    290      1.11    kleink 
    291      1.11    kleink 	case LC_COLLATE:
    292      1.11    kleink 	case LC_MESSAGES:
    293      1.11    kleink 	case LC_MONETARY:
    294      1.11    kleink 	case LC_NUMERIC:
    295      1.11    kleink 	case LC_TIME:
    296      1.11    kleink 		return NULL;
    297       1.5       jtc 	}
    298      1.11    kleink 
    299      1.11    kleink 	return NULL;
    300       1.1       cgd }
    301