]> git.saurik.com Git - apple/libc.git/blob - locale/FreeBSD/setrunelocale.c
Libc-763.13.tar.gz
[apple/libc.git] / locale / FreeBSD / setrunelocale.c
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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD: src/lib/libc/locale/setrunelocale.c,v 1.51 2008/01/23 03:05:35 ache Exp $");
35
36 #include <runetype.h>
37 #include <errno.h>
38 #include <limits.h>
39 #include <string.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <wchar.h>
44 #include "ldpart.h"
45 #include "mblocal.h"
46 #include "setlocale.h"
47
48 extern int __mb_sb_limit;
49
50 extern _RuneLocale *_Read_RuneMagi(FILE *);
51
52 static int __setrunelocale(const char *);
53
54 static int
55 __setrunelocale(const char *encoding)
56 {
57 FILE *fp;
58 char name[PATH_MAX];
59 _RuneLocale *rl;
60 int saverr, ret;
61 size_t (*old__mbrtowc)(wchar_t * __restrict,
62 const char * __restrict, size_t, mbstate_t * __restrict);
63 size_t (*old__wcrtomb)(char * __restrict, wchar_t,
64 mbstate_t * __restrict);
65 int (*old__mbsinit)(const mbstate_t *);
66 size_t (*old__mbsnrtowcs)(wchar_t * __restrict,
67 const char ** __restrict, size_t, size_t, mbstate_t * __restrict);
68 size_t (*old__wcsnrtombs)(char * __restrict,
69 const wchar_t ** __restrict, size_t, size_t,
70 mbstate_t * __restrict);
71 static char ctype_encoding[ENCODING_LEN + 1];
72 static _RuneLocale *CachedRuneLocale;
73 static int Cached__mb_cur_max;
74 static int Cached__mb_sb_limit;
75 static size_t (*Cached__mbrtowc)(wchar_t * __restrict,
76 const char * __restrict, size_t, mbstate_t * __restrict);
77 static size_t (*Cached__wcrtomb)(char * __restrict, wchar_t,
78 mbstate_t * __restrict);
79 static int (*Cached__mbsinit)(const mbstate_t *);
80 static size_t (*Cached__mbsnrtowcs)(wchar_t * __restrict,
81 const char ** __restrict, size_t, size_t, mbstate_t * __restrict);
82 static size_t (*Cached__wcsnrtombs)(char * __restrict,
83 const wchar_t ** __restrict, size_t, size_t,
84 mbstate_t * __restrict);
85
86 /*
87 * The "C" and "POSIX" locale are always here.
88 */
89 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
90 (void) _none_init(&_DefaultRuneLocale);
91 return (0);
92 }
93
94 /*
95 * If the locale name is the same as our cache, use the cache.
96 */
97 if (CachedRuneLocale != NULL &&
98 strcmp(encoding, ctype_encoding) == 0) {
99 _CurrentRuneLocale = CachedRuneLocale;
100 __mb_cur_max = Cached__mb_cur_max;
101 __mb_sb_limit = Cached__mb_sb_limit;
102 __mbrtowc = Cached__mbrtowc;
103 __mbsinit = Cached__mbsinit;
104 __mbsnrtowcs = Cached__mbsnrtowcs;
105 __wcrtomb = Cached__wcrtomb;
106 __wcsnrtombs = Cached__wcsnrtombs;
107 return (0);
108 }
109
110 /*
111 * Slurp the locale file into the cache.
112 */
113
114 /* Range checking not needed, encoding length already checked before */
115 (void) strcpy(name, _PathLocale);
116 (void) strcat(name, "/");
117 (void) strcat(name, encoding);
118 (void) strcat(name, "/LC_CTYPE");
119
120 if ((fp = fopen(name, "r")) == NULL)
121 return (errno == 0 ? ENOENT : errno);
122
123 if ((rl = _Read_RuneMagi(fp)) == NULL) {
124 saverr = (errno == 0 ? EFTYPE : errno);
125 (void)fclose(fp);
126 return (saverr);
127 }
128 (void)fclose(fp);
129
130 old__mbrtowc = __mbrtowc;
131 old__mbsinit = __mbsinit;
132 old__mbsnrtowcs = __mbsnrtowcs;
133 old__wcrtomb = __wcrtomb;
134 old__wcsnrtombs = __wcsnrtombs;
135
136 __mbrtowc = NULL;
137 __mbsinit = NULL;
138 __mbsnrtowcs = __mbsnrtowcs_std;
139 __wcrtomb = NULL;
140 __wcsnrtombs = __wcsnrtombs_std;
141
142 rl->__sputrune = NULL;
143 rl->__sgetrune = NULL;
144 if (strcmp(rl->__encoding, "NONE") == 0)
145 ret = _none_init(rl);
146 else if (strcmp(rl->__encoding, "ASCII") == 0)
147 ret = _ascii_init(rl);
148 else if (strcmp(rl->__encoding, "UTF-8") == 0)
149 ret = _UTF8_init(rl);
150 else if (strcmp(rl->__encoding, "EUC") == 0)
151 ret = _EUC_init(rl);
152 else if (strcmp(rl->__encoding, "GB18030") == 0)
153 ret = _GB18030_init(rl);
154 else if (strcmp(rl->__encoding, "GB2312") == 0)
155 ret = _GB2312_init(rl);
156 else if (strcmp(rl->__encoding, "GBK") == 0)
157 ret = _GBK_init(rl);
158 else if (strcmp(rl->__encoding, "BIG5") == 0)
159 ret = _BIG5_init(rl);
160 else if (strcmp(rl->__encoding, "MSKanji") == 0)
161 ret = _MSKanji_init(rl);
162 else
163 ret = EFTYPE;
164
165 if (ret == 0) {
166 if (CachedRuneLocale != NULL) {
167 /* See euc.c */
168 if (strcmp(CachedRuneLocale->__encoding, "EUC") == 0)
169 free(CachedRuneLocale->__variable);
170 free(CachedRuneLocale);
171 }
172 CachedRuneLocale = _CurrentRuneLocale;
173 Cached__mb_cur_max = __mb_cur_max;
174 Cached__mb_sb_limit = __mb_sb_limit;
175 Cached__mbrtowc = __mbrtowc;
176 Cached__mbsinit = __mbsinit;
177 Cached__mbsnrtowcs = __mbsnrtowcs;
178 Cached__wcrtomb = __wcrtomb;
179 Cached__wcsnrtombs = __wcsnrtombs;
180 (void)strcpy(ctype_encoding, encoding);
181 } else {
182 __mbrtowc = old__mbrtowc;
183 __mbsinit = old__mbsinit;
184 __mbsnrtowcs = old__mbsnrtowcs;
185 __wcrtomb = old__wcrtomb;
186 __wcsnrtombs = old__wcsnrtombs;
187 free(rl);
188 }
189
190 return (ret);
191 }
192
193 int
194 __wrap_setrunelocale(const char *locale)
195 {
196 int ret = __setrunelocale(locale);
197
198 if (ret != 0) {
199 errno = ret;
200 return (_LDP_ERROR);
201 }
202 return (_LDP_LOADED);
203 }
204