Home | History | Annotate | Line # | Download | only in locale
setlocale.c revision 1.58.6.1
      1 /* $NetBSD: setlocale.c,v 1.58.6.1 2012/04/17 00:05:20 yamt Exp $ */
      2 
      3 /*-
      4  * Copyright (c)2008 Citrus Project,
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 #if defined(LIBC_SCCS) && !defined(lint)
     31 __RCSID("$NetBSD: setlocale.c,v 1.58.6.1 2012/04/17 00:05:20 yamt Exp $");
     32 #endif /* LIBC_SCCS and not lint */
     33 
     34 #include <sys/types.h>
     35 #include <locale.h>
     36 #include <limits.h>
     37 #include <paths.h>
     38 #include <stdlib.h>
     39 #include <string.h>
     40 #include <unistd.h>
     41 
     42 #include "setlocale_local.h"
     43 
     44 const char *_PathLocale = NULL;
     45 
     46 static _locale_set_t all_categories[_LC_LAST] = {
     47 	[LC_ALL     ] = &_generic_LC_ALL_setlocale,
     48 	[LC_COLLATE ] = &_dummy_LC_COLLATE_setlocale,
     49 	[LC_CTYPE   ] = &_citrus_LC_CTYPE_setlocale,
     50 	[LC_MONETARY] = &_citrus_LC_MONETARY_setlocale,
     51 	[LC_NUMERIC ] = &_citrus_LC_NUMERIC_setlocale,
     52 	[LC_TIME    ] = &_citrus_LC_TIME_setlocale,
     53 	[LC_MESSAGES] = &_citrus_LC_MESSAGES_setlocale,
     54 };
     55 
     56 _locale_set_t
     57 _find_category(int category)
     58 {
     59 	if (category >= LC_ALL && category < _LC_LAST)
     60 		return all_categories[category];
     61 	return NULL;
     62 }
     63 
     64 const char *
     65 _get_locale_env(const char *category)
     66 {
     67 	const char *name;
     68 
     69 	/* 1. check LC_ALL */
     70 	name = (const char *)getenv("LC_ALL");
     71 	if (name == NULL || *name == '\0') {
     72 		/* 2. check LC_* */
     73 		name = (const char *)getenv(category);
     74 		if (name == NULL || *name == '\0') {
     75 			/* 3. check LANG */
     76 			name = getenv("LANG");
     77 		}
     78 	}
     79 	if (name == NULL || *name == '\0' || strchr(name, '/'))
     80 		/* 4. if none is set, fall to "C" */
     81 		name = _C_LOCALE;
     82 	return name;
     83 }
     84 
     85 char *
     86 __setlocale(int category, const char *name)
     87 {
     88 	_locale_set_t sl;
     89 	struct _locale_impl_t *impl;
     90 
     91 	sl = _find_category(category);
     92 	if (sl == NULL)
     93 		return NULL;
     94 	if (issetugid() || ((_PathLocale == NULL &&
     95 	    (_PathLocale = getenv("PATH_LOCALE")) == NULL) ||
     96 	    *_PathLocale == '\0'))
     97 		_PathLocale = _PATH_LOCALE;
     98 	impl = *_current_locale();
     99 	return __UNCONST((*sl)(name, impl));
    100 }
    101 
    102 char *
    103 setlocale(int category, const char *locale)
    104 {
    105 
    106 	/* locale may be NULL */
    107 
    108 	__mb_len_max_runtime = MB_LEN_MAX;
    109 	return __setlocale(category, locale);
    110 }
    111