setrunelocale.c revision 121845
1/*- 2 * Copyright (c) 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Paul Borman at Krystal Technologies. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37/* setrunelocale() is obsolete in FreeBSD 6 -- use ANSI functions instead. */ 38#define OBSOLETE_IN_6 39 40#include <sys/cdefs.h> 41__FBSDID("$FreeBSD: head/lib/libc/locale/setrunelocale.c 121845 2003-11-01 05:13:13Z tjr $"); 42 43#include <rune.h> 44#include <errno.h> 45#include <limits.h> 46#include <string.h> 47#include <stdio.h> 48#include <stdlib.h> 49#include <unistd.h> 50#include <wchar.h> 51#include "ldpart.h" 52#include "setlocale.h" 53 54extern int _none_init(_RuneLocale *); 55extern int _UTF2_init(_RuneLocale *); 56extern int _UTF8_init(_RuneLocale *); 57extern int _EUC_init(_RuneLocale *); 58extern int _GB18030_init(_RuneLocale *); 59extern int _GBK_init(_RuneLocale *); 60extern int _BIG5_init(_RuneLocale *); 61extern int _MSKanji_init(_RuneLocale *); 62extern _RuneLocale *_Read_RuneMagi(FILE *); 63 64extern size_t (*__mbrtowc)(wchar_t * __restrict, const char * __restrict, 65 size_t, mbstate_t * __restrict); 66extern size_t (*__wcrtomb)(char * __restrict, wchar_t, mbstate_t * __restrict); 67extern size_t __emulated_mbrtowc(wchar_t * __restrict, const char * __restrict, 68 size_t, mbstate_t * __restrict ps); 69extern size_t __emulated_wcrtomb(char * __restrict, wchar_t, 70 mbstate_t * __restrict ps); 71extern rune_t __emulated_sgetrune(const char *, size_t, const char **); 72extern int __emulated_sputrune(rune_t, char *, size_t, char **); 73 74static int __setrunelocale(const char *); 75 76__warn_references(setrunelocale, "warning: setrunelocale() is deprecated. See setrunelocale(3)."); 77int 78setrunelocale(char *encoding) 79{ 80 int ret; 81 82 if (!encoding || !*encoding || strlen(encoding) > ENCODING_LEN || 83 (encoding[0] == '.' && 84 (encoding[1] == '\0' || 85 (encoding[1] == '.' && encoding[2] == '\0'))) || 86 strchr(encoding, '/') != NULL) 87 return (EINVAL); 88 89 ret = __detect_path_locale(); 90 if (ret != 0) 91 return (ret); 92 93 return (__setrunelocale((const char *)encoding)); 94} 95 96static int 97__setrunelocale(const char *encoding) 98{ 99 FILE *fp; 100 char name[PATH_MAX]; 101 _RuneLocale *rl; 102 int saverr, ret; 103 static char ctype_encoding[ENCODING_LEN + 1]; 104 static _RuneLocale *CachedRuneLocale; 105 static int Cached__mb_cur_max; 106 107 /* 108 * The "C" and "POSIX" locale are always here. 109 */ 110 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) { 111 _CurrentRuneLocale = &_DefaultRuneLocale; 112 __mb_cur_max = 1; 113 return (0); 114 } 115 116 /* 117 * If the locale name is the same as our cache, use the cache. 118 */ 119 if (CachedRuneLocale != NULL && 120 strcmp(encoding, ctype_encoding) == 0) { 121 _CurrentRuneLocale = CachedRuneLocale; 122 __mb_cur_max = Cached__mb_cur_max; 123 return (0); 124 } 125 126 /* 127 * Slurp the locale file into the cache. 128 */ 129 130 /* Range checking not needed, encoding length already checked before */ 131 (void) strcpy(name, _PathLocale); 132 (void) strcat(name, "/"); 133 (void) strcat(name, encoding); 134 (void) strcat(name, "/LC_CTYPE"); 135 136 if ((fp = fopen(name, "r")) == NULL) 137 return (errno == 0 ? ENOENT : errno); 138 139 if ((rl = _Read_RuneMagi(fp)) == NULL) { 140 saverr = (errno == 0 ? EFTYPE : errno); 141 (void)fclose(fp); 142 return (saverr); 143 } 144 (void)fclose(fp); 145 146 __mbrtowc = __emulated_mbrtowc; 147 __wcrtomb = __emulated_wcrtomb; 148 rl->sputrune = __emulated_sputrune; 149 rl->sgetrune = __emulated_sgetrune; 150 if (strcmp(rl->encoding, "NONE") == 0) 151 ret = _none_init(rl); 152 else if (strcmp(rl->encoding, "UTF2") == 0) 153 ret = _UTF2_init(rl); 154 else if (strcmp(rl->encoding, "UTF-8") == 0) 155 ret = _UTF8_init(rl); 156 else if (strcmp(rl->encoding, "EUC") == 0) 157 ret = _EUC_init(rl); 158 else if (strcmp(rl->encoding, "GB18030") == 0) 159 ret = _GB18030_init(rl); 160 else if (strcmp(rl->encoding, "GBK") == 0) 161 ret = _GBK_init(rl); 162 else if (strcmp(rl->encoding, "BIG5") == 0) 163 ret = _BIG5_init(rl); 164 else if (strcmp(rl->encoding, "MSKanji") == 0) 165 ret = _MSKanji_init(rl); 166 else 167 ret = EFTYPE; 168 if (ret == 0) { 169 if (CachedRuneLocale != NULL) { 170 /* See euc.c */ 171 if (strcmp(CachedRuneLocale->encoding, "EUC") == 0) 172 free(CachedRuneLocale->variable); 173 free(CachedRuneLocale); 174 } 175 CachedRuneLocale = _CurrentRuneLocale; 176 Cached__mb_cur_max = __mb_cur_max; 177 (void)strcpy(ctype_encoding, encoding); 178 } else 179 free(rl); 180 181 return (ret); 182} 183 184int 185__wrap_setrunelocale(const char *locale) 186{ 187 int ret = __setrunelocale(locale); 188 189 if (ret != 0) { 190 errno = ret; 191 return (_LDP_ERROR); 192 } 193 return (_LDP_LOADED); 194} 195 196