setlocale.c revision 114256
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 114256 2003-04-29 21:13:50Z nectar $"); 43 44#include "namespace.h" 45#include <sys/types.h> 46#include <sys/stat.h> 47#include <errno.h> 48#include <limits.h> 49#include <locale.h> 50#include <rune.h> 51#include <stdlib.h> 52#include <string.h> 53#include <unistd.h> 54#include "un-namespace.h" 55#include "collate.h" 56#include "lmonetary.h" /* for __monetary_load_locale() */ 57#include "lnumeric.h" /* for __numeric_load_locale() */ 58#include "lmessages.h" /* for __messages_load_locale() */ 59#include "setlocale.h" 60#include "ldpart.h" 61#include "../stdtime/timelocal.h" /* for __time_load_locale() */ 62 63/* 64 * Category names for getenv() 65 */ 66static char *categories[_LC_LAST] = { 67 "LC_ALL", 68 "LC_COLLATE", 69 "LC_CTYPE", 70 "LC_MONETARY", 71 "LC_NUMERIC", 72 "LC_TIME", 73 "LC_MESSAGES", 74}; 75 76/* 77 * Current locales for each category 78 */ 79static char current_categories[_LC_LAST][ENCODING_LEN + 1] = { 80 "C", 81 "C", 82 "C", 83 "C", 84 "C", 85 "C", 86 "C", 87}; 88 89/* 90 * The locales we are going to try and load 91 */ 92static char new_categories[_LC_LAST][ENCODING_LEN + 1]; 93static char saved_categories[_LC_LAST][ENCODING_LEN + 1]; 94 95static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)]; 96 97static char *currentlocale(void); 98static int wrap_setrunelocale(const char *); 99static char *loadlocale(int); 100 101char * 102setlocale(category, locale) 103 int category; 104 const char *locale; 105{ 106 int i, j, len, saverr; 107 char *env, *r; 108 109 if (category < LC_ALL || category >= _LC_LAST) { 110 errno = EINVAL; 111 return (NULL); 112 } 113 114 if (locale == NULL) 115 return (category != LC_ALL ? 116 current_categories[category] : currentlocale()); 117 118 /* 119 * Default to the current locale for everything. 120 */ 121 for (i = 1; i < _LC_LAST; ++i) 122 (void)strcpy(new_categories[i], current_categories[i]); 123 124 /* 125 * Now go fill up new_categories from the locale argument 126 */ 127 if (!*locale) { 128 env = getenv("LC_ALL"); 129 130 if (category != LC_ALL && (env == NULL || !*env)) 131 env = getenv(categories[category]); 132 133 if (env == NULL || !*env) 134 env = getenv("LANG"); 135 136 if (env == NULL || !*env) 137 env = "C"; 138 139 if (strlen(env) > ENCODING_LEN) { 140 errno = EINVAL; 141 return (NULL); 142 } 143 (void)strcpy(new_categories[category], env); 144 145 if (category == LC_ALL) { 146 for (i = 1; i < _LC_LAST; ++i) { 147 if ((env = getenv(categories[i])) == NULL || 148 !*env) 149 env = new_categories[LC_ALL]; 150 else if (strlen(env) > ENCODING_LEN) { 151 errno = EINVAL; 152 return (NULL); 153 } 154 (void)strcpy(new_categories[i], env); 155 } 156 } 157 } else if (category != LC_ALL) { 158 if (strlen(locale) > ENCODING_LEN) { 159 errno = EINVAL; 160 return (NULL); 161 } 162 (void)strcpy(new_categories[category], locale); 163 } else { 164 if ((r = strchr(locale, '/')) == NULL) { 165 if (strlen(locale) > ENCODING_LEN) { 166 errno = EINVAL; 167 return (NULL); 168 } 169 for (i = 1; i < _LC_LAST; ++i) 170 (void)strcpy(new_categories[i], locale); 171 } else { 172 for (i = 1; r[1] == '/'; ++r) 173 ; 174 if (!r[1]) { 175 errno = EINVAL; 176 return (NULL); /* Hmm, just slashes... */ 177 } 178 do { 179 if (i == _LC_LAST) 180 break; /* Too many slashes... */ 181 if ((len = r - locale) > ENCODING_LEN) { 182 errno = EINVAL; 183 return (NULL); 184 } 185 (void)_strlcpy(new_categories[i], locale, 186 len + 1); 187 i++; 188 locale = r; 189 while (*locale == '/') 190 ++locale; 191 while (*++r && *r != '/') 192 ; 193 } while (*locale); 194 while (i < _LC_LAST) { 195 (void)strcpy(new_categories[i], 196 new_categories[i-1]); 197 i++; 198 } 199 } 200 } 201 202 if (category != LC_ALL) 203 return (loadlocale(category)); 204 205 for (i = 1; i < _LC_LAST; ++i) { 206 (void)strcpy(saved_categories[i], current_categories[i]); 207 if (loadlocale(i) == NULL) { 208 saverr = errno; 209 for (j = 1; j < i; j++) { 210 (void)strcpy(new_categories[j], 211 saved_categories[j]); 212 if (loadlocale(j) == NULL) { 213 (void)strcpy(new_categories[j], "C"); 214 (void)loadlocale(j); 215 } 216 } 217 errno = saverr; 218 return (NULL); 219 } 220 } 221 return (currentlocale()); 222} 223 224static char * 225currentlocale() 226{ 227 int i; 228 229 (void)strcpy(current_locale_string, current_categories[1]); 230 231 for (i = 2; i < _LC_LAST; ++i) 232 if (strcmp(current_categories[1], current_categories[i])) { 233 for (i = 2; i < _LC_LAST; ++i) { 234 (void)strcat(current_locale_string, "/"); 235 (void)strcat(current_locale_string, 236 current_categories[i]); 237 } 238 break; 239 } 240 return (current_locale_string); 241} 242 243static int 244wrap_setrunelocale(const char *locale) 245{ 246 int ret = setrunelocale((char *)locale); 247 248 if (ret != 0) { 249 errno = ret; 250 return (_LDP_ERROR); 251 } 252 return (_LDP_LOADED); 253} 254 255static char * 256loadlocale(category) 257 int category; 258{ 259 char *new = new_categories[category]; 260 char *old = current_categories[category]; 261 int (*func)(const char *); 262 263 if ((new[0] == '.' && 264 (new[1] == '\0' || (new[1] == '.' && new[2] == '\0'))) || 265 strchr(new, '/') != NULL) { 266 errno = EINVAL; 267 return (NULL); 268 } 269 270 if (_PathLocale == NULL) { 271 char *p = getenv("PATH_LOCALE"); 272 273 if (p != NULL 274#ifndef __NETBSD_SYSCALLS 275 && !issetugid() 276#endif 277 ) { 278 if (strlen(p) + 1/*"/"*/ + ENCODING_LEN + 279 1/*"/"*/ + CATEGORY_LEN >= PATH_MAX) { 280 errno = ENAMETOOLONG; 281 return (NULL); 282 } 283 _PathLocale = strdup(p); 284 if (_PathLocale == NULL) { 285 errno = ENOMEM; 286 return (NULL); 287 } 288 } else 289 _PathLocale = _PATH_LOCALE; 290 } 291 292 switch (category) { 293 case LC_CTYPE: 294 func = wrap_setrunelocale; 295 break; 296 case LC_COLLATE: 297 func = __collate_load_tables; 298 break; 299 case LC_TIME: 300 func = __time_load_locale; 301 break; 302 case LC_NUMERIC: 303 func = __numeric_load_locale; 304 break; 305 case LC_MONETARY: 306 func = __monetary_load_locale; 307 break; 308 case LC_MESSAGES: 309 func = __messages_load_locale; 310 break; 311 default: 312 errno = EINVAL; 313 return (NULL); 314 } 315 316 if (strcmp(new, old) == 0) 317 return (old); 318 319 if (func(new) != _LDP_ERROR) { 320 (void)strcpy(old, new); 321 return (old); 322 } 323 324 return (NULL); 325} 326 327