setlocale.c revision 101498
1285SN/A/* 2461SN/A * Copyright (c) 1996 - 2002 FreeBSD Project 3285SN/A * Copyright (c) 1991, 1993 4285SN/A * The Regents of the University of California. All rights reserved. 5285SN/A * 6285SN/A * This code is derived from software contributed to Berkeley by 7285SN/A * Paul Borman at Krystal Technologies. 8285SN/A * 9285SN/A * Redistribution and use in source and binary forms, with or without 10285SN/A * modification, are permitted provided that the following conditions 11285SN/A * are met: 12285SN/A * 1. Redistributions of source code must retain the above copyright 13285SN/A * notice, this list of conditions and the following disclaimer. 14285SN/A * 2. Redistributions in binary form must reproduce the above copyright 15285SN/A * notice, this list of conditions and the following disclaimer in the 16285SN/A * documentation and/or other materials provided with the distribution. 17285SN/A * 3. All advertising materials mentioning features or use of this software 18285SN/A * must display the following acknowledgement: 19285SN/A * This product includes software developed by the University of 20285SN/A * California, Berkeley and its contributors. 21285SN/A * 4. Neither the name of the University nor the names of its contributors 22285SN/A * may be used to endorse or promote products derived from this software 23285SN/A * without specific prior written permission. 24285SN/A * 25285SN/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26285SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27285SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28285SN/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29285SN/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30285SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31285SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32285SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33285SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34285SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35285SN/A * SUCH DAMAGE. 36285SN/A */ 37285SN/A 38285SN/A#if defined(LIBC_SCCS) && !defined(lint) 39285SN/Astatic char sccsid[] = "@(#)setlocale.c 8.1 (Berkeley) 7/4/93"; 40285SN/A#endif /* LIBC_SCCS and not lint */ 41285SN/A#include <sys/cdefs.h> 42285SN/A__FBSDID("$FreeBSD: head/lib/libc/locale/setlocale.c 101498 2002-08-08 05:51:54Z ache $"); 43285SN/A 44285SN/A#include <sys/types.h> 45285SN/A#include <sys/stat.h> 46285SN/A#include <errno.h> 47285SN/A#include <limits.h> 48285SN/A#include <locale.h> 49285SN/A#include <rune.h> 50285SN/A#include <stdlib.h> 51285SN/A#include <string.h> 52285SN/A#include <unistd.h> 53285SN/A#include "collate.h" 54285SN/A#include "lmonetary.h" /* for __monetary_load_locale() */ 55285SN/A#include "lnumeric.h" /* for __numeric_load_locale() */ 56285SN/A#include "lmessages.h" /* for __messages_load_locale() */ 57285SN/A#include "setlocale.h" 58285SN/A#include "ldpart.h" 59285SN/A#include "../stdtime/timelocal.h" /* for __time_load_locale() */ 60285SN/A 61285SN/A/* 62285SN/A * Category names for getenv() 63285SN/A */ 64285SN/Astatic char *categories[_LC_LAST] = { 65285SN/A "LC_ALL", 66285SN/A "LC_COLLATE", 67285SN/A "LC_CTYPE", 68285SN/A "LC_MONETARY", 69285SN/A "LC_NUMERIC", 70285SN/A "LC_TIME", 71285SN/A "LC_MESSAGES", 72285SN/A}; 73285SN/A 74285SN/A/* 75285SN/A * Current locales for each category 76285SN/A */ 77285SN/Astatic char current_categories[_LC_LAST][ENCODING_LEN + 1] = { 78285SN/A "C", 79285SN/A "C", 80285SN/A "C", 81285SN/A "C", 82285SN/A "C", 83285SN/A "C", 84285SN/A "C", 85285SN/A}; 86285SN/A 87285SN/A/* 88285SN/A * The locales we are going to try and load 89285SN/A */ 90285SN/Astatic char new_categories[_LC_LAST][ENCODING_LEN + 1]; 91285SN/Astatic char saved_categories[_LC_LAST][ENCODING_LEN + 1]; 92285SN/A 93285SN/Astatic char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)]; 94285SN/A 95285SN/Astatic char *currentlocale(void); 96285SN/Astatic int wrap_setrunelocale(const char *); 97285SN/Astatic char *loadlocale(int); 98285SN/A 99285SN/Achar * 100285SN/Asetlocale(category, locale) 101285SN/A int category; 102285SN/A const char *locale; 103285SN/A{ 104285SN/A int i, j, len, saverr; 105285SN/A char *env, *r; 106285SN/A 107285SN/A if (category < LC_ALL || category >= _LC_LAST) { 108285SN/A errno = EINVAL; 109285SN/A return (NULL); 110285SN/A } 111285SN/A 112285SN/A if (locale == NULL) 113285SN/A return (category != LC_ALL ? 114285SN/A current_categories[category] : currentlocale()); 115285SN/A 116285SN/A /* 117285SN/A * Default to the current locale for everything. 118285SN/A */ 119285SN/A for (i = 1; i < _LC_LAST; ++i) 120285SN/A (void)strcpy(new_categories[i], current_categories[i]); 121285SN/A 122285SN/A /* 123285SN/A * Now go fill up new_categories from the locale argument 124285SN/A */ 125285SN/A if (!*locale) { 126285SN/A env = getenv("LC_ALL"); 127285SN/A 128285SN/A if (category != LC_ALL && (env == NULL || !*env)) 129285SN/A env = getenv(categories[category]); 130285SN/A 131285SN/A if (env == NULL || !*env) 132285SN/A env = getenv("LANG"); 133285SN/A 134285SN/A if (env == NULL || !*env) 135285SN/A env = "C"; 136285SN/A 137285SN/A if (strlen(env) > ENCODING_LEN) { 138285SN/A errno = EINVAL; 139285SN/A return (NULL); 140285SN/A } 141285SN/A (void)strcpy(new_categories[category], env); 142285SN/A 143285SN/A if (category == LC_ALL) { 144285SN/A for (i = 1; i < _LC_LAST; ++i) { 145285SN/A if ((env = getenv(categories[i])) == NULL || 146285SN/A !*env) 147285SN/A env = new_categories[LC_ALL]; 148285SN/A else if (strlen(env) > ENCODING_LEN) { 149285SN/A errno = EINVAL; 150285SN/A return (NULL); 151285SN/A } 152285SN/A (void)strcpy(new_categories[i], env); 153285SN/A } 154285SN/A } 155285SN/A } else if (category != LC_ALL) { 156285SN/A if (strlen(locale) > ENCODING_LEN) { 157285SN/A errno = EINVAL; 158285SN/A return (NULL); 159285SN/A } 160285SN/A (void)strcpy(new_categories[category], locale); 161285SN/A } else { 162285SN/A if ((r = strchr(locale, '/')) == NULL) { 163285SN/A if (strlen(locale) > ENCODING_LEN) { 164285SN/A errno = EINVAL; 165285SN/A return (NULL); 166285SN/A } 167285SN/A for (i = 1; i < _LC_LAST; ++i) 168285SN/A (void)strcpy(new_categories[i], locale); 169285SN/A } else { 170285SN/A for (i = 1; r[1] == '/'; ++r) 171285SN/A ; 172285SN/A if (!r[1]) { 173285SN/A errno = EINVAL; 174285SN/A return (NULL); /* Hmm, just slashes... */ 175285SN/A } 176285SN/A do { 177285SN/A if (i == _LC_LAST) 178285SN/A break; /* Too many slashes... */ 179285SN/A if ((len = r - locale) > ENCODING_LEN) { 180285SN/A errno = EINVAL; 181285SN/A return (NULL); 182285SN/A } 183285SN/A (void)strlcpy(new_categories[i], locale, 184285SN/A len + 1); 185285SN/A i++; 186285SN/A locale = r; 187285SN/A while (*locale == '/') 188285SN/A ++locale; 189285SN/A while (*++r && *r != '/') 190285SN/A ; 191285SN/A } while (*locale); 192285SN/A while (i < _LC_LAST) { 193285SN/A (void)strcpy(new_categories[i], 194285SN/A new_categories[i-1]); 195285SN/A i++; 196285SN/A } 197285SN/A } 198285SN/A } 199285SN/A 200285SN/A if (category != LC_ALL) 201285SN/A return (loadlocale(category)); 202285SN/A 203285SN/A for (i = 1; i < _LC_LAST; ++i) { 204285SN/A (void)strcpy(saved_categories[i], current_categories[i]); 205285SN/A if (loadlocale(i) == NULL) { 206285SN/A saverr = errno; 207285SN/A for (j = 1; j < i; j++) { 208285SN/A (void)strcpy(new_categories[j], 209285SN/A saved_categories[j]); 210285SN/A if (loadlocale(j) == NULL) { 211285SN/A (void)strcpy(new_categories[j], "C"); 212285SN/A (void)loadlocale(j); 213285SN/A } 214285SN/A } 215285SN/A errno = saverr; 216285SN/A return (NULL); 217285SN/A } 218285SN/A } 219285SN/A return (currentlocale()); 220285SN/A} 221285SN/A 222285SN/Astatic char * 223285SN/Acurrentlocale() 224285SN/A{ 225285SN/A int i; 226285SN/A 227285SN/A (void)strcpy(current_locale_string, current_categories[1]); 228285SN/A 229285SN/A for (i = 2; i < _LC_LAST; ++i) 230285SN/A if (strcmp(current_categories[1], current_categories[i])) { 231285SN/A for (i = 2; i < _LC_LAST; ++i) { 232285SN/A (void)strcat(current_locale_string, "/"); 233285SN/A (void)strcat(current_locale_string, 234285SN/A current_categories[i]); 235285SN/A } 236285SN/A break; 237285SN/A } 238285SN/A return (current_locale_string); 239285SN/A} 240285SN/A 241285SN/Astatic int 242285SN/Awrap_setrunelocale(const char *locale) 243285SN/A{ 244285SN/A int ret = setrunelocale((char *)locale); 245285SN/A 246285SN/A if (ret != 0) { 247285SN/A errno = ret; 248285SN/A return (_LDP_ERROR); 249285SN/A } 250285SN/A return (_LDP_LOADED); 251285SN/A} 252285SN/A 253285SN/Astatic char * 254285SN/Aloadlocale(category) 255285SN/A int category; 256285SN/A{ 257285SN/A char *new = new_categories[category]; 258285SN/A char *old = current_categories[category]; 259285SN/A int (*func)(const char *); 260285SN/A 261285SN/A if ((new[0] == '.' && 262285SN/A (new[1] == '\0' || (new[1] == '.' && new[2] == '\0'))) || 263285SN/A strchr(new, '/') != NULL) { 264285SN/A errno = EINVAL; 265285SN/A return (NULL); 266285SN/A } 267285SN/A 268285SN/A if (_PathLocale == NULL) { 269285SN/A char *p = getenv("PATH_LOCALE"); 270285SN/A 271285SN/A if (p != NULL 272285SN/A#ifndef __NETBSD_SYSCALLS 273285SN/A && !issetugid() 274285SN/A#endif 275285SN/A ) { 276285SN/A if (strlen(p) + 1/*"/"*/ + ENCODING_LEN + 277285SN/A 1/*"/"*/ + CATEGORY_LEN >= PATH_MAX) { 278285SN/A errno = ENAMETOOLONG; 279285SN/A return (NULL); 280285SN/A } 281285SN/A _PathLocale = strdup(p); 282285SN/A if (_PathLocale == NULL) { 283285SN/A errno = ENOMEM; 284285SN/A return (NULL); 285285SN/A } 286285SN/A } else 287285SN/A _PathLocale = _PATH_LOCALE; 288285SN/A } 289285SN/A 290285SN/A switch (category) { 291285SN/A case LC_CTYPE: 292285SN/A func = wrap_setrunelocale; 293285SN/A break; 294285SN/A case LC_COLLATE: 295285SN/A func = __collate_load_tables; 296285SN/A break; 297285SN/A case LC_TIME: 298285SN/A func = __time_load_locale; 299285SN/A break; 300285SN/A case LC_NUMERIC: 301285SN/A func = __numeric_load_locale; 302285SN/A break; 303285SN/A case LC_MONETARY: 304285SN/A func = __monetary_load_locale; 305285SN/A break; 306285SN/A case LC_MESSAGES: 307285SN/A func = __messages_load_locale; 308285SN/A break; 309285SN/A default: 310285SN/A errno = EINVAL; 311285SN/A return (NULL); 312285SN/A } 313285SN/A 314285SN/A if (strcmp(new, old) == 0) 315285SN/A return (old); 316285SN/A 317285SN/A if (func(new) != _LDP_ERROR) { 318285SN/A (void)strcpy(old, new); 319285SN/A return (old); 320285SN/A } 321285SN/A 322285SN/A return (NULL); 323285SN/A} 324285SN/A 325285SN/A