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