Home | History | Annotate | Line # | Download | only in locale
setlocale.c revision 1.56
      1 /* $NetBSD: setlocale.c,v 1.56 2009/01/11 02:46:29 christos 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.56 2009/01/11 02:46:29 christos Exp $");
     32 #endif /* LIBC_SCCS and not lint */
     33 
     34 #include <sys/cdefs.h>
     35 #include <sys/types.h>
     36 #include <langinfo.h>
     37 #define __SETLOCALE_SOURCE__
     38 #include <locale.h>
     39 #include <paths.h>
     40 #include <stdlib.h>
     41 #include <string.h>
     42 #include <unistd.h>
     43 
     44 #include "setlocale_local.h"
     45 
     46 const char *_PathLocale = NULL;
     47 
     48 __link_set_decl(all_categories, _locale_category_t);
     49 
     50 #ifndef __lint__
     51 static
     52 #endif
     53 const _locale_category_t dummy = {
     54     .category = _LC_LAST, /* XXX */
     55     .setlocale = NULL,
     56 };
     57 
     58 __link_set_add_data(all_categories, dummy);
     59 
     60 _locale_category_t *
     61 _find_category(int category)
     62 {
     63 	_locale_category_t * const *p;
     64 
     65 	__link_set_foreach(p, all_categories) {
     66 		if ((*p)->category == category)
     67 			return *p;
     68 	}
     69 	return NULL;
     70 }
     71 
     72 const char *
     73 _get_locale_env(const char *category)
     74 {
     75 	const char *name;
     76 
     77 	/* 1. check LC_ALL */
     78 	name = (const char *)getenv("LC_ALL");
     79 	if (name == NULL || *name == '\0') {
     80 		/* 2. check LC_* */
     81 		name = (const char *)getenv(category);
     82 		if (name == NULL || *name == '\0') {
     83 			/* 3. check LANG */
     84 			name = getenv("LANG");
     85 		}
     86 	}
     87 	if (name == NULL || *name == '\0' || strchr(name, '/'))
     88 		/* 4. if none is set, fall to "C" */
     89 		name = _C_LOCALE;
     90 	return name;
     91 }
     92 
     93 char *
     94 __setlocale(int category, const char *name)
     95 {
     96 	_locale_category_t *l;
     97 	struct _locale_impl_t *impl;
     98 
     99 	if (category >= LC_ALL && category < _LC_LAST) {
    100 		l = _find_category(category);
    101 		if (l != NULL) {
    102 			if (issetugid() || ((_PathLocale == NULL &&
    103 			    (_PathLocale = getenv("PATH_LOCALE")) == NULL) ||
    104 			    *_PathLocale == '\0'))
    105 				_PathLocale = _PATH_LOCALE;
    106 			impl = *_current_locale();
    107 			return __UNCONST((*l->setlocale)(name, impl));
    108 		}
    109 	}
    110 	return NULL;
    111 }
    112