1 /* $NetBSD: setlocale.c,v 1.57 2009/03/09 02:22:25 tnozaki 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.57 2009/03/09 02:22:25 tnozaki 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 extern const _locale_category_t _generic_LC_ALL_desc; 51 extern const _locale_category_t _dummy_LC_COLLATE_desc; 52 #ifdef WITH_RUNE 53 extern const _locale_category_t _citrus_LC_CTYPE_desc; 54 extern const _locale_category_t _citrus_LC_MONETARY_desc; 55 extern const _locale_category_t _citrus_LC_NUMERIC_desc; 56 extern const _locale_category_t _citrus_LC_TIME_desc; 57 extern const _locale_category_t _citrus_LC_MESSAGES_desc; 58 #else 59 extern const _locale_category_t _localeio_LC_CTYPE_desc; 60 extern const _locale_category_t _localeio_LC_MONETARY_desc; 61 extern const _locale_category_t _localeio_LC_NUMERIC_desc; 62 extern const _locale_category_t _localeio_LC_TIME_desc; 63 extern const _locale_category_t _localeio_LC_MESSAGES_desc; 64 #endif 65 66 __link_set_add_data(all_categories, _generic_LC_ALL_desc); 67 __link_set_add_data(all_categories, _dummy_LC_COLLATE_desc); 68 #ifdef WITH_RUNE 69 __link_set_add_data(all_categories, _citrus_LC_CTYPE_desc); 70 __link_set_add_data(all_categories, _citrus_LC_MONETARY_desc); 71 __link_set_add_data(all_categories, _citrus_LC_NUMERIC_desc); 72 __link_set_add_data(all_categories, _citrus_LC_TIME_desc); 73 __link_set_add_data(all_categories, _citrus_LC_MESSAGES_desc); 74 #else 75 __link_set_add_data(all_categories, _localeio_LC_CTYPE_desc); 76 __link_set_add_data(all_categories, _localeio_LC_MONETARY_desc); 77 __link_set_add_data(all_categories, _localeio_LC_NUMERIC_desc); 78 __link_set_add_data(all_categories, _localeio_LC_TIME_desc); 79 __link_set_add_data(all_categories, _localeio_LC_MESSAGES_desc); 80 #endif 81 82 _locale_category_t * 83 _find_category(int category) 84 { 85 _locale_category_t * const *p; 86 87 __link_set_foreach(p, all_categories) { 88 if ((*p)->category == category) 89 return *p; 90 } 91 return NULL; 92 } 93 94 const char * 95 _get_locale_env(const char *category) 96 { 97 const char *name; 98 99 /* 1. check LC_ALL */ 100 name = (const char *)getenv("LC_ALL"); 101 if (name == NULL || *name == '\0') { 102 /* 2. check LC_* */ 103 name = (const char *)getenv(category); 104 if (name == NULL || *name == '\0') { 105 /* 3. check LANG */ 106 name = getenv("LANG"); 107 } 108 } 109 if (name == NULL || *name == '\0' || strchr(name, '/')) 110 /* 4. if none is set, fall to "C" */ 111 name = _C_LOCALE; 112 return name; 113 } 114 115 char * 116 __setlocale(int category, const char *name) 117 { 118 _locale_category_t *l; 119 struct _locale_impl_t *impl; 120 121 if (category >= LC_ALL && category < _LC_LAST) { 122 l = _find_category(category); 123 if (l != NULL) { 124 if (issetugid() || ((_PathLocale == NULL && 125 (_PathLocale = getenv("PATH_LOCALE")) == NULL) || 126 *_PathLocale == '\0')) 127 _PathLocale = _PATH_LOCALE; 128 impl = *_current_locale(); 129 return __UNCONST((*l->setlocale)(name, impl)); 130 } 131 } 132 return NULL; 133 } 134