3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Paul Borman at Krystal Technologies.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
50 #include "setlocale.h"
52 extern _RuneLocale *_Read_RuneMagi(FILE *);
54 static int __setrunelocale(const char *);
57 __setrunelocale(const char *encoding)
63 static char ctype_encoding[ENCODING_LEN + 1];
64 static _RuneLocale *CachedRuneLocale;
65 static int Cached__mb_cur_max;
66 static size_t (*Cached__mbrtowc)(wchar_t * __restrict,
67 const char * __restrict, size_t, mbstate_t * __restrict);
68 static size_t (*Cached__wcrtomb)(char * __restrict, wchar_t,
69 mbstate_t * __restrict);
70 static int (*Cached__mbsinit)(const mbstate_t *);
71 static size_t (*Cached__mbsnrtowcs)(wchar_t * __restrict,
72 const char ** __restrict, size_t, size_t, mbstate_t * __restrict);
73 static size_t (*Cached__wcsnrtombs)(char * __restrict,
74 const wchar_t ** __restrict, size_t, size_t,
75 mbstate_t * __restrict);
78 * The "C" and "POSIX" locale are always here.
80 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
81 _none_init(&_DefaultRuneLocale);
86 * If the locale name is the same as our cache, use the cache.
88 if (CachedRuneLocale != NULL &&
89 strcmp(encoding, ctype_encoding) == 0) {
90 _CurrentRuneLocale = CachedRuneLocale;
91 __mb_cur_max = Cached__mb_cur_max;
92 __mbrtowc = Cached__mbrtowc;
93 __mbsinit = Cached__mbsinit;
94 __mbsnrtowcs = Cached__mbsnrtowcs;
95 __wcrtomb = Cached__wcrtomb;
96 __wcsnrtombs = Cached__wcsnrtombs;
101 * Slurp the locale file into the cache.
104 /* Range checking not needed, encoding length already checked before */
105 (void) strcpy(name, _PathLocale);
106 (void) strcat(name, "/");
107 (void) strcat(name, encoding);
108 (void) strcat(name, "/LC_CTYPE");
110 if ((fp = fopen(name, "r")) == NULL)
111 return (errno == 0 ? ENOENT : errno);
113 if ((rl = _Read_RuneMagi(fp)) == NULL) {
114 saverr = (errno == 0 ? EFTYPE : errno);
122 __mbsnrtowcs = __mbsnrtowcs_std;
124 __wcsnrtombs = __wcsnrtombs_std;
125 rl->__sputrune = NULL;
126 rl->__sgetrune = NULL;
127 if (strcmp(rl->__encoding, "NONE") == 0)
128 ret = _none_init(rl);
129 else if (strcmp(rl->__encoding, "UTF-8") == 0)
130 ret = _UTF8_init(rl);
131 else if (strcmp(rl->__encoding, "EUC") == 0)
133 else if (strcmp(rl->__encoding, "GB18030") == 0)
134 ret = _GB18030_init(rl);
135 else if (strcmp(rl->__encoding, "GB2312") == 0)
136 ret = _GB2312_init(rl);
137 else if (strcmp(rl->__encoding, "GBK") == 0)
139 else if (strcmp(rl->__encoding, "BIG5") == 0)
140 ret = _BIG5_init(rl);
141 else if (strcmp(rl->__encoding, "MSKanji") == 0)
142 ret = _MSKanji_init(rl);
146 if (CachedRuneLocale != NULL) {
148 if (strcmp(CachedRuneLocale->__encoding, "EUC") == 0)
149 free(CachedRuneLocale->__variable);
150 free(CachedRuneLocale);
152 CachedRuneLocale = _CurrentRuneLocale;
153 Cached__mb_cur_max = __mb_cur_max;
154 Cached__mbrtowc = __mbrtowc;
155 Cached__mbsinit = __mbsinit;
156 Cached__mbsnrtowcs = __mbsnrtowcs;
157 Cached__wcrtomb = __wcrtomb;
158 Cached__wcsnrtombs = __wcsnrtombs;
159 (void)strcpy(ctype_encoding, encoding);
167 __wrap_setrunelocale(const char *locale)
169 int ret = __setrunelocale(locale);
175 return (_LDP_LOADED);