Home | History | Annotate | Line # | Download | only in locale
setlocale.c revision 1.6
      1  1.1  cgd /*
      2  1.5  jtc  * Copyright (c) 1991, 1993
      3  1.5  jtc  *	The Regents of the University of California.  All rights reserved.
      4  1.5  jtc  *
      5  1.5  jtc  * This code is derived from software contributed to Berkeley by
      6  1.5  jtc  * Paul Borman at Krystal Technologies.
      7  1.1  cgd  *
      8  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      9  1.1  cgd  * modification, are permitted provided that the following conditions
     10  1.1  cgd  * are met:
     11  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     12  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     13  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     16  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     17  1.1  cgd  *    must display the following acknowledgement:
     18  1.1  cgd  *	This product includes software developed by the University of
     19  1.1  cgd  *	California, Berkeley and its contributors.
     20  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     21  1.1  cgd  *    may be used to endorse or promote products derived from this software
     22  1.1  cgd  *    without specific prior written permission.
     23  1.1  cgd  *
     24  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1  cgd  * SUCH DAMAGE.
     35  1.1  cgd  */
     36  1.1  cgd 
     37  1.1  cgd #if defined(LIBC_SCCS) && !defined(lint)
     38  1.5  jtc static char sccsid[] = "@(#)setlocale.c	8.1 (Berkeley) 7/4/93";
     39  1.1  cgd #endif /* LIBC_SCCS and not lint */
     40  1.1  cgd 
     41  1.5  jtc #include <sys/localedef.h>
     42  1.1  cgd #include <locale.h>
     43  1.5  jtc #include <limits.h>
     44  1.6  jtc #include <stdio.h>
     45  1.5  jtc #include <stdlib.h>
     46  1.1  cgd #include <string.h>
     47  1.5  jtc #include <paths.h>
     48  1.1  cgd 
     49  1.5  jtc /*
     50  1.5  jtc  * Category names for getenv()
     51  1.5  jtc  */
     52  1.5  jtc static char *categories[_LC_LAST] = {
     53  1.5  jtc     "LC_ALL",
     54  1.5  jtc     "LC_COLLATE",
     55  1.5  jtc     "LC_CTYPE",
     56  1.5  jtc     "LC_MONETARY",
     57  1.5  jtc     "LC_NUMERIC",
     58  1.5  jtc     "LC_TIME",
     59  1.5  jtc     "LC_MESSAGES"
     60  1.5  jtc };
     61  1.1  cgd 
     62  1.1  cgd /*
     63  1.5  jtc  * Current locales for each category
     64  1.5  jtc  */
     65  1.5  jtc static char current_categories[_LC_LAST][32] = {
     66  1.5  jtc     "C",
     67  1.5  jtc     "C",
     68  1.5  jtc     "C",
     69  1.5  jtc     "C",
     70  1.5  jtc     "C",
     71  1.5  jtc     "C",
     72  1.5  jtc     "C"
     73  1.5  jtc };
     74  1.5  jtc 
     75  1.5  jtc /*
     76  1.5  jtc  * The locales we are going to try and load
     77  1.1  cgd  */
     78  1.5  jtc static char new_categories[_LC_LAST][32];
     79  1.5  jtc 
     80  1.5  jtc static char current_locale_string[_LC_LAST * 33];
     81  1.5  jtc static char *PathLocale;
     82  1.5  jtc 
     83  1.5  jtc static char	*currentlocale __P((void));
     84  1.5  jtc static char	*loadlocale __P((int));
     85  1.5  jtc 
     86  1.1  cgd char *
     87  1.1  cgd setlocale(category, locale)
     88  1.1  cgd 	int category;
     89  1.1  cgd 	const char *locale;
     90  1.1  cgd {
     91  1.5  jtc 	int found, i, len;
     92  1.5  jtc 	char *env, *r;
     93  1.5  jtc 
     94  1.5  jtc 	if (!PathLocale && !(PathLocale = getenv("PATH_LOCALE")))
     95  1.5  jtc 		PathLocale = _PATH_LOCALE;
     96  1.5  jtc 
     97  1.5  jtc 	if (category < 0 || category >= _LC_LAST)
     98  1.1  cgd 		return (NULL);
     99  1.5  jtc 
    100  1.5  jtc 	if (!locale)
    101  1.5  jtc 		return (category ?
    102  1.5  jtc 		    current_categories[category] : currentlocale());
    103  1.5  jtc 
    104  1.5  jtc 	/*
    105  1.5  jtc 	 * Default to the current locale for everything.
    106  1.5  jtc 	 */
    107  1.5  jtc 	for (i = 1; i < _LC_LAST; ++i)
    108  1.5  jtc 		(void)strcpy(new_categories[i], current_categories[i]);
    109  1.5  jtc 
    110  1.5  jtc 	/*
    111  1.5  jtc 	 * Now go fill up new_categories from the locale argument
    112  1.5  jtc 	 */
    113  1.5  jtc 	if (!*locale) {
    114  1.5  jtc 		env = getenv(categories[category]);
    115  1.5  jtc 
    116  1.5  jtc 		if (!env)
    117  1.5  jtc 			env = getenv(categories[0]);
    118  1.5  jtc 
    119  1.5  jtc 		if (!env)
    120  1.5  jtc 			env = getenv("LANG");
    121  1.5  jtc 
    122  1.5  jtc 		if (!env)
    123  1.5  jtc 			env = "C";
    124  1.5  jtc 
    125  1.5  jtc 		(void) strncpy(new_categories[category], env, 31);
    126  1.5  jtc 		new_categories[category][31] = 0;
    127  1.5  jtc 		if (!category) {
    128  1.5  jtc 			for (i = 1; i < _LC_LAST; ++i) {
    129  1.5  jtc 				if (!(env = getenv(categories[i])))
    130  1.5  jtc 					env = new_categories[0];
    131  1.5  jtc 				(void)strncpy(new_categories[i], env, 31);
    132  1.5  jtc 				new_categories[i][31] = 0;
    133  1.5  jtc 			}
    134  1.5  jtc 		}
    135  1.5  jtc 	} else if (category)  {
    136  1.5  jtc 		(void)strncpy(new_categories[category], locale, 31);
    137  1.5  jtc 		new_categories[category][31] = 0;
    138  1.5  jtc 	} else {
    139  1.5  jtc 		if ((r = strchr(locale, '/')) == 0) {
    140  1.5  jtc 			for (i = 1; i < _LC_LAST; ++i) {
    141  1.5  jtc 				(void)strncpy(new_categories[i], locale, 31);
    142  1.5  jtc 				new_categories[i][31] = 0;
    143  1.5  jtc 			}
    144  1.5  jtc 		} else {
    145  1.5  jtc 			for (i = 1; r[1] == '/'; ++r);
    146  1.5  jtc 			if (!r[1])
    147  1.5  jtc 				return (NULL);	/* Hmm, just slashes... */
    148  1.5  jtc 			do {
    149  1.5  jtc 				len = r - locale > 31 ? 31 : r - locale;
    150  1.5  jtc 				(void)strncpy(new_categories[i++], locale, len);
    151  1.5  jtc 				new_categories[i++][len] = 0;
    152  1.5  jtc 				locale = r;
    153  1.5  jtc 				while (*locale == '/')
    154  1.5  jtc 				    ++locale;
    155  1.5  jtc 				while (*++r && *r != '/');
    156  1.5  jtc 			} while (*locale);
    157  1.5  jtc 			while (i < _LC_LAST)
    158  1.5  jtc 				(void)strcpy(new_categories[i],
    159  1.5  jtc 				    new_categories[i-1]);
    160  1.5  jtc 		}
    161  1.5  jtc 	}
    162  1.5  jtc 
    163  1.5  jtc 	if (category)
    164  1.5  jtc 		return (loadlocale(category));
    165  1.5  jtc 
    166  1.5  jtc 	found = 0;
    167  1.5  jtc 	for (i = 1; i < _LC_LAST; ++i)
    168  1.5  jtc 		if (loadlocale(i) != NULL)
    169  1.5  jtc 			found = 1;
    170  1.5  jtc 	if (found)
    171  1.5  jtc 	    return (currentlocale());
    172  1.5  jtc 	return (NULL);
    173  1.5  jtc }
    174  1.5  jtc 
    175  1.5  jtc static char *
    176  1.5  jtc currentlocale()
    177  1.5  jtc {
    178  1.5  jtc 	int i;
    179  1.5  jtc 
    180  1.5  jtc 	(void)strcpy(current_locale_string, current_categories[1]);
    181  1.5  jtc 
    182  1.5  jtc 	for (i = 2; i < _LC_LAST; ++i)
    183  1.5  jtc 		if (strcmp(current_categories[1], current_categories[i])) {
    184  1.5  jtc 			(void)snprintf(current_locale_string,
    185  1.5  jtc 			    sizeof(current_locale_string), "%s/%s/%s/%s/%s",
    186  1.5  jtc 			    current_categories[1], current_categories[2],
    187  1.5  jtc 			    current_categories[3], current_categories[4],
    188  1.5  jtc 			    current_categories[5]);
    189  1.5  jtc 			break;
    190  1.5  jtc 		}
    191  1.5  jtc 	return (current_locale_string);
    192  1.5  jtc }
    193  1.5  jtc 
    194  1.5  jtc static char *
    195  1.5  jtc loadlocale(category)
    196  1.5  jtc 	int category;
    197  1.5  jtc {
    198  1.5  jtc 	char name[PATH_MAX];
    199  1.5  jtc 
    200  1.5  jtc 	if (strcmp(new_categories[category],
    201  1.5  jtc 	    current_categories[category]) == 0)
    202  1.5  jtc 		return (current_categories[category]);
    203  1.5  jtc 
    204  1.5  jtc 	if (!strcmp(new_categories[category], "C") ||
    205  1.5  jtc 		!strcmp(new_categories[category], "POSIX")) {
    206  1.5  jtc 
    207  1.5  jtc 		/*
    208  1.5  jtc 		 * Some day this will need to reset the locale to the default
    209  1.5  jtc 		 * C locale.  Since we have no way to change them as of yet,
    210  1.5  jtc 		 * there is no need to reset them.
    211  1.5  jtc 		 */
    212  1.5  jtc 		(void)strcpy(current_categories[category],
    213  1.5  jtc 		    new_categories[category]);
    214  1.5  jtc 		return (current_categories[category]);
    215  1.5  jtc 	}
    216  1.5  jtc 
    217  1.5  jtc 	/*
    218  1.5  jtc 	 * Some day we will actually look at this file.
    219  1.5  jtc 	 */
    220  1.5  jtc 	(void)snprintf(name, sizeof(name), "%s/%s/%s",
    221  1.5  jtc 	    PathLocale, new_categories[category], categories[category]);
    222  1.5  jtc 
    223  1.5  jtc 	switch (category) {
    224  1.5  jtc 		case LC_CTYPE:
    225  1.5  jtc 		case LC_COLLATE:
    226  1.5  jtc 		case LC_MESSAGES:
    227  1.5  jtc 		case LC_MONETARY:
    228  1.5  jtc 		case LC_NUMERIC:
    229  1.5  jtc 		case LC_TIME:
    230  1.5  jtc 			return (NULL);
    231  1.5  jtc 	}
    232  1.1  cgd }
    233