setlocale.c revision 101292
1/* 2 * Copyright (c) 1996 - 2002 FreeBSD Project 3 * Copyright (c) 1991, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * Paul Borman at Krystal Technologies. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38#if defined(LIBC_SCCS) && !defined(lint) 39static char sccsid[] = "@(#)setlocale.c 8.1 (Berkeley) 7/4/93"; 40#endif /* LIBC_SCCS and not lint */ 41#include <sys/cdefs.h> 42__FBSDID("$FreeBSD: head/lib/libc/locale/setlocale.c 101292 2002-08-04 04:29:54Z ache $"); 43 44#include <sys/types.h> 45#include <sys/stat.h> 46#include <errno.h> 47#include <limits.h> 48#include <locale.h> 49#include <rune.h> 50#include <stdlib.h> 51#include <string.h> 52#include <unistd.h> 53#include "collate.h" 54#include "lmonetary.h" /* for __monetary_load_locale() */ 55#include "lnumeric.h" /* for __numeric_load_locale() */ 56#include "lmessages.h" /* for __messages_load_locale() */ 57#include "setlocale.h" 58#include "../stdtime/timelocal.h" /* for __time_load_locale() */ 59 60/* 61 * Category names for getenv() 62 */ 63static char *categories[_LC_LAST] = { 64 "LC_ALL", 65 "LC_COLLATE", 66 "LC_CTYPE", 67 "LC_MONETARY", 68 "LC_NUMERIC", 69 "LC_TIME", 70 "LC_MESSAGES", 71}; 72 73/* 74 * Current locales for each category 75 */ 76static char current_categories[_LC_LAST][ENCODING_LEN + 1] = { 77 "C", 78 "C", 79 "C", 80 "C", 81 "C", 82 "C", 83 "C", 84}; 85 86/* 87 * The locales we are going to try and load 88 */ 89static char new_categories[_LC_LAST][ENCODING_LEN + 1]; 90static char saved_categories[_LC_LAST][ENCODING_LEN + 1]; 91 92static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)]; 93 94static char *currentlocale(void); 95static int wrap_setrunelocale(char *); 96static char *loadlocale(int); 97 98char * 99setlocale(category, locale) 100 int category; 101 const char *locale; 102{ 103 int i, j, len; 104 char *env, *r; 105 106 if (category < LC_ALL || category >= _LC_LAST) { 107 errno = EINVAL; 108 return (NULL); 109 } 110 111 if (!locale) 112 return (category != LC_ALL ? 113 current_categories[category] : currentlocale()); 114 115 /* 116 * Default to the current locale for everything. 117 */ 118 for (i = 1; i < _LC_LAST; ++i) 119 (void)strcpy(new_categories[i], current_categories[i]); 120 121 /* 122 * Now go fill up new_categories from the locale argument 123 */ 124 if (!*locale) { 125 env = getenv("LC_ALL"); 126 127 if (category != LC_ALL && (!env || !*env)) 128 env = getenv(categories[category]); 129 130 if (!env || !*env) 131 env = getenv("LANG"); 132 133 if (!env || !*env) 134 env = "C"; 135 136 (void)strlcpy(new_categories[category], env, ENCODING_LEN + 1); 137 if (category == LC_ALL) { 138 for (i = 1; i < _LC_LAST; ++i) { 139 if (!(env = getenv(categories[i])) || !*env) 140 env = new_categories[LC_ALL]; 141 (void)strlcpy(new_categories[i], env, 142 ENCODING_LEN + 1); 143 } 144 } 145 } else if (category != LC_ALL) 146 (void)strlcpy(new_categories[category], locale, 147 ENCODING_LEN + 1); 148 else { 149 if ((r = strchr(locale, '/')) == NULL) { 150 for (i = 1; i < _LC_LAST; ++i) 151 (void)strlcpy(new_categories[i], locale, 152 ENCODING_LEN + 1); 153 } else { 154 for (i = 1; r[1] == '/'; ++r) 155 ; 156 if (!r[1]) { 157 errno = EINVAL; 158 return (NULL); /* Hmm, just slashes... */ 159 } 160 do { 161 if (i == _LC_LAST) 162 break; /* Too many slashes... */ 163 len = r - locale > ENCODING_LEN ? 164 ENCODING_LEN : r - locale; 165 (void)strlcpy(new_categories[i], locale, 166 len + 1); 167 i++; 168 locale = r; 169 while (*locale == '/') 170 ++locale; 171 while (*++r && *r != '/') 172 ; 173 } while (*locale); 174 while (i < _LC_LAST) { 175 (void)strcpy(new_categories[i], 176 new_categories[i-1]); 177 i++; 178 } 179 } 180 } 181 182 if (category != LC_ALL) 183 return (loadlocale(category)); 184 185 for (i = 1; i < _LC_LAST; ++i) { 186 (void)strcpy(saved_categories[i], current_categories[i]); 187 if (loadlocale(i) == NULL) { 188 int saverr = errno; 189 190 for (j = 1; j < i; j++) { 191 (void)strcpy(new_categories[j], 192 saved_categories[j]); 193 (void)loadlocale(j); 194 } 195 errno = saverr; 196 return (NULL); 197 } 198 } 199 return (currentlocale()); 200} 201 202static char * 203currentlocale() 204{ 205 int i; 206 207 (void)strcpy(current_locale_string, current_categories[1]); 208 209 for (i = 2; i < _LC_LAST; ++i) 210 if (strcmp(current_categories[1], current_categories[i])) { 211 for (i = 2; i < _LC_LAST; ++i) { 212 (void)strcat(current_locale_string, "/"); 213 (void)strcat(current_locale_string, 214 current_categories[i]); 215 } 216 break; 217 } 218 return (current_locale_string); 219} 220 221static int 222wrap_setrunelocale(locale) 223 char *locale; 224{ 225 int ret = setrunelocale(locale); 226 227 if (ret != 0) { 228 errno = ret; 229 return (-1); 230 } 231 return (0); 232} 233 234static char * 235loadlocale(category) 236 int category; 237{ 238 char *ret; 239 char *new = new_categories[category]; 240 char *old = current_categories[category]; 241 int (*func)(); 242 int saverr; 243 244 if ((new[0] == '.' && 245 (new[1] == '\0' || (new[1] == '.' && new[2] == '\0'))) || 246 strchr(new, '/') != NULL) { 247 errno = EINVAL; 248 return (NULL); 249 } 250 251 if (_PathLocale == NULL) { 252 char *p = getenv("PATH_LOCALE"); 253 254 if (p != NULL 255#ifndef __NETBSD_SYSCALLS 256 && !issetugid() 257#endif 258 ) { 259 if (strlen(p) + 1/*"/"*/ + ENCODING_LEN + 260 1/*"/"*/ + CATEGORY_LEN >= PATH_MAX) { 261 errno = ENAMETOOLONG; 262 return (NULL); 263 } 264 _PathLocale = strdup(p); 265 if (_PathLocale == NULL) 266 return (NULL); 267 } else 268 _PathLocale = _PATH_LOCALE; 269 } 270 271 switch (category) { 272 case LC_CTYPE: 273 func = wrap_setrunelocale; 274 break; 275 case LC_COLLATE: 276 func = __collate_load_tables; 277 break; 278 case LC_TIME: 279 func = __time_load_locale; 280 break; 281 case LC_NUMERIC: 282 func = __numeric_load_locale; 283 break; 284 case LC_MONETARY: 285 func = __monetary_load_locale; 286 break; 287 case LC_MESSAGES: 288 func = __messages_load_locale; 289 break; 290 default: 291 errno = EINVAL; 292 return (NULL); 293 } 294 295 if (strcmp(new, old) == 0) 296 return (old); 297 298 ret = func(new) != 0 ? NULL : new; 299 if (ret == NULL) { 300 saverr = errno; 301 if (func(old) != 0 && func("C") == 0) 302 (void)strcpy(old, "C"); 303 errno = saverr; 304 } else 305 (void)strcpy(old, new); 306 307 return (ret); 308} 309 310