]> git.saurik.com Git - apple/libc.git/blob - locale/FreeBSD/setrunelocale.c
Libc-391.4.1.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 * 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 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD: src/lib/libc/locale/setrunelocale.c,v 1.44 2004/10/18 02:06:18 ache Exp $");
39
40 #include <runetype.h>
41 #include <errno.h>
42 #include <limits.h>
43 #include <string.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <wchar.h>
48 #include "ldpart.h"
49 #include "mblocal.h"
50 #include "setlocale.h"
51
52 extern int _none_init(_RuneLocale *);
53 extern int _UTF8_init(_RuneLocale *);
54 extern int _EUC_init(_RuneLocale *);
55 extern int _GB18030_init(_RuneLocale *);
56 extern int _GB2312_init(_RuneLocale *);
57 extern int _GBK_init(_RuneLocale *);
58 extern int _BIG5_init(_RuneLocale *);
59 extern int _MSKanji_init(_RuneLocale *);
60 extern _RuneLocale *_Read_RuneMagi(FILE *);
61
62 static int __setrunelocale(const char *);
63
64 static int
65 __setrunelocale(const char *encoding)
66 {
67 FILE *fp;
68 char name[PATH_MAX];
69 _RuneLocale *rl;
70 int saverr, ret;
71 static char ctype_encoding[ENCODING_LEN + 1];
72 static _RuneLocale *CachedRuneLocale;
73 static int Cached__mb_cur_max;
74 static size_t (*Cached__mbrtowc)(wchar_t * __restrict,
75 const char * __restrict, size_t, mbstate_t * __restrict);
76 static size_t (*Cached__wcrtomb)(char * __restrict, wchar_t,
77 mbstate_t * __restrict);
78 static int (*Cached__mbsinit)(const mbstate_t *);
79 static size_t (*Cached__mbsnrtowcs)(wchar_t * __restrict,
80 const char ** __restrict, size_t, size_t, mbstate_t * __restrict);
81 static size_t (*Cached__wcsnrtombs)(char * __restrict,
82 const wchar_t ** __restrict, size_t, size_t,
83 mbstate_t * __restrict);
84
85 /*
86 * The "C" and "POSIX" locale are always here.
87 */
88 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
89 _CurrentRuneLocale = &_DefaultRuneLocale;
90 __mb_cur_max = 1;
91 __mbrtowc = _none_mbrtowc;
92 __mbsinit = _none_mbsinit;
93 __mbsnrtowcs = _none_mbsnrtowcs;
94 __wcrtomb = _none_wcrtomb;
95 __wcsnrtombs = _none_wcsnrtombs;
96 return (0);
97 }
98
99 /*
100 * If the locale name is the same as our cache, use the cache.
101 */
102 if (CachedRuneLocale != NULL &&
103 strcmp(encoding, ctype_encoding) == 0) {
104 _CurrentRuneLocale = CachedRuneLocale;
105 __mb_cur_max = Cached__mb_cur_max;
106 __mbrtowc = Cached__mbrtowc;
107 __mbsinit = Cached__mbsinit;
108 __mbsnrtowcs = Cached__mbsnrtowcs;
109 __wcrtomb = Cached__wcrtomb;
110 __wcsnrtombs = Cached__wcsnrtombs;
111 return (0);
112 }
113
114 /*
115 * Slurp the locale file into the cache.
116 */
117
118 /* Range checking not needed, encoding length already checked before */
119 (void) strcpy(name, _PathLocale);
120 (void) strcat(name, "/");
121 (void) strcat(name, encoding);
122 (void) strcat(name, "/LC_CTYPE");
123
124 if ((fp = fopen(name, "r")) == NULL)
125 return (errno == 0 ? ENOENT : errno);
126
127 if ((rl = _Read_RuneMagi(fp)) == NULL) {
128 saverr = (errno == 0 ? EFTYPE : errno);
129 (void)fclose(fp);
130 return (saverr);
131 }
132 (void)fclose(fp);
133
134 __mbrtowc = NULL;
135 __mbsinit = NULL;
136 __mbsnrtowcs = __mbsnrtowcs_std;
137 __wcrtomb = NULL;
138 __wcsnrtombs = __wcsnrtombs_std;
139 rl->__sputrune = NULL;
140 rl->__sgetrune = NULL;
141 if (strcmp(rl->__encoding, "NONE") == 0)
142 ret = _none_init(rl);
143 else if (strcmp(rl->__encoding, "UTF-8") == 0)
144 ret = _UTF8_init(rl);
145 else if (strcmp(rl->__encoding, "EUC") == 0)
146 ret = _EUC_init(rl);
147 else if (strcmp(rl->__encoding, "GB18030") == 0)
148 ret = _GB18030_init(rl);
149 else if (strcmp(rl->__encoding, "GB2312") == 0)
150 ret = _GB2312_init(rl);
151 else if (strcmp(rl->__encoding, "GBK") == 0)
152 ret = _GBK_init(rl);
153 else if (strcmp(rl->__encoding, "BIG5") == 0)
154 ret = _BIG5_init(rl);
155 else if (strcmp(rl->__encoding, "MSKanji") == 0)
156 ret = _MSKanji_init(rl);
157 else
158 ret = EFTYPE;
159 if (ret == 0) {
160 if (CachedRuneLocale != NULL) {
161 /* See euc.c */
162 if (strcmp(CachedRuneLocale->__encoding, "EUC") == 0)
163 free(CachedRuneLocale->__variable);
164 free(CachedRuneLocale);
165 }
166 CachedRuneLocale = _CurrentRuneLocale;
167 Cached__mb_cur_max = __mb_cur_max;
168 Cached__mbrtowc = __mbrtowc;
169 Cached__mbsinit = __mbsinit;
170 Cached__mbsnrtowcs = __mbsnrtowcs;
171 Cached__wcrtomb = __wcrtomb;
172 Cached__wcsnrtombs = __wcsnrtombs;
173 (void)strcpy(ctype_encoding, encoding);
174 } else
175 free(rl);
176
177 return (ret);
178 }
179
180 int
181 __wrap_setrunelocale(const char *locale)
182 {
183 int ret = __setrunelocale(locale);
184
185 if (ret != 0) {
186 errno = ret;
187 return (_LDP_ERROR);
188 }
189 return (_LDP_LOADED);
190 }
191