1 /* $NetBSD: setlocale.c,v 1.63 2013/05/17 12:55:57 joerg 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.63 2013/05/17 12:55:57 joerg 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 static int initialised; 60 61 if (!initialised) { 62 if (issetugid() || ((_PathLocale == NULL && 63 (_PathLocale = getenv("PATH_LOCALE")) == NULL) || 64 *_PathLocale == '\0')) 65 _PathLocale = _PATH_LOCALE; 66 initialised = 1; 67 } 68 69 if (category >= LC_ALL && category < _LC_LAST) 70 return all_categories[category]; 71 return NULL; 72 } 73 74 const char * 75 _get_locale_env(const char *category) 76 { 77 const char *name; 78 79 /* 1. check LC_ALL */ 80 name = (const char *)getenv("LC_ALL"); 81 if (name == NULL || *name == '\0') { 82 /* 2. check LC_* */ 83 name = (const char *)getenv(category); 84 if (name == NULL || *name == '\0') { 85 /* 3. check LANG */ 86 name = getenv("LANG"); 87 } 88 } 89 if (name == NULL || *name == '\0' || strchr(name, '/')) 90 /* 4. if none is set, fall to "C" */ 91 name = _C_LOCALE; 92 return name; 93 } 94 95 char * 96 __setlocale(int category, const char *name) 97 { 98 _locale_set_t sl; 99 struct _locale *impl; 100 101 sl = _find_category(category); 102 if (sl == NULL) 103 return NULL; 104 impl = _current_locale(); 105 return __UNCONST((*sl)(name, impl)); 106 } 107 108 char * 109 setlocale(int category, const char *locale) 110 { 111 112 /* locale may be NULL */ 113 114 __mb_len_max_runtime = MB_LEN_MAX; 115 return __setlocale(category, locale); 116 } 117