]> git.saurik.com Git - apple/libc.git/blob - locale/setrunelocale-fbsd.c
Libc-594.1.4.tar.gz
[apple/libc.git] / locale / setrunelocale-fbsd.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 "xlocale_private.h"
41
42 #include <runetype.h>
43 #include <errno.h>
44 #include <limits.h>
45 #include <string.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <wchar.h>
50 #include "ldpart.h"
51 #include "mblocal.h"
52 #include "setlocale.h"
53
54 extern int _none_init(struct __xlocale_st_runelocale *);
55 extern int _UTF8_init(struct __xlocale_st_runelocale *);
56 extern int _EUC_init(struct __xlocale_st_runelocale *);
57 extern int _GB18030_init(struct __xlocale_st_runelocale *);
58 extern int _GB2312_init(struct __xlocale_st_runelocale *);
59 extern int _GBK_init(struct __xlocale_st_runelocale *);
60 extern int _BIG5_init(struct __xlocale_st_runelocale *);
61 extern int _MSKanji_init(struct __xlocale_st_runelocale *);
62 extern int _UTF2_init(struct __xlocale_st_runelocale *); /* deprecated */
63 extern struct __xlocale_st_runelocale *_Read_RuneMagi(FILE *);
64
65 #ifdef LEGACY_RUNE_APIS
66 /* depreciated interfaces */
67 rune_t sgetrune(const char *, size_t, char const **);
68 int sputrune(rune_t, char *, size_t, char **);
69 #endif /* LEGACY_RUNE_APIS */
70
71 __private_extern__ int
72 __setrunelocale(const char *encoding, locale_t loc)
73 {
74 FILE *fp;
75 char name[PATH_MAX];
76 struct __xlocale_st_runelocale *xrl;
77 _RuneLocale *rl;
78 int saverr, ret;
79 static struct __xlocale_st_runelocale *CachedRuneLocale;
80 extern int __mb_cur_max;
81 static pthread_lock_t cache_lock = LOCK_INITIALIZER;
82
83 /*
84 * The "C" and "POSIX" locale are always here.
85 */
86 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
87 XL_RELEASE(loc->__lc_ctype);
88 loc->__lc_ctype = &_DefaultRuneXLocale;
89 /* no need to retain _DefaultRuneXLocale */
90 if (loc == &__global_locale) {
91 _CurrentRuneLocale = &loc->__lc_ctype->_CurrentRuneLocale;
92 __mb_cur_max = loc->__lc_ctype->__mb_cur_max;
93 }
94 return (0);
95 }
96
97 /*
98 * If the locale name is the same as our cache, use the cache.
99 */
100 LOCK(cache_lock);
101 if (CachedRuneLocale != NULL &&
102 strcmp(encoding, CachedRuneLocale->__ctype_encoding) == 0) {
103 XL_RELEASE(loc->__lc_ctype);
104 loc->__lc_ctype = CachedRuneLocale;
105 XL_RETAIN(loc->__lc_ctype);
106 if (loc == &__global_locale) {
107 _CurrentRuneLocale = &loc->__lc_ctype->_CurrentRuneLocale;
108 __mb_cur_max = loc->__lc_ctype->__mb_cur_max;
109 }
110 UNLOCK(cache_lock);
111 return (0);
112 }
113 UNLOCK(cache_lock);
114
115 /*
116 * Slurp the locale file into the cache.
117 */
118
119 /* Range checking not needed, encoding length already checked before */
120 (void) strcpy(name, _PathLocale);
121 (void) strcat(name, "/");
122 (void) strcat(name, encoding);
123 (void) strcat(name, "/LC_CTYPE");
124
125 if ((fp = fopen(name, "r")) == NULL)
126 return (errno == 0 ? ENOENT : errno);
127
128 if ((xrl = _Read_RuneMagi(fp)) == NULL) {
129 saverr = (errno == 0 ? EFTYPE : errno);
130 (void)fclose(fp);
131 return (saverr);
132 }
133 (void)fclose(fp);
134
135 xrl->__mbrtowc = NULL;
136 xrl->__mbsinit = NULL;
137 xrl->__mbsnrtowcs = __mbsnrtowcs_std;
138 xrl->__wcrtomb = NULL;
139 xrl->__wcsnrtombs = __wcsnrtombs_std;
140
141 rl = &xrl->_CurrentRuneLocale;
142
143 #ifdef LEGACY_RUNE_APIS
144 /* provide backwards compatibility (depreciated interface) */
145 rl->__sputrune = sputrune;
146 rl->__sgetrune = sgetrune;
147 #else /* LEGACY_RUNE_APIS */
148 rl->__sputrune = NULL;
149 rl->__sgetrune = NULL;
150 #endif /* LEGACY_RUNE_APIS */
151
152 if (strcmp(rl->__encoding, "NONE") == 0)
153 ret = _none_init(xrl);
154 else if (strcmp(rl->__encoding, "UTF-8") == 0)
155 ret = _UTF8_init(xrl);
156 else if (strcmp(rl->__encoding, "EUC") == 0)
157 ret = _EUC_init(xrl);
158 else if (strcmp(rl->__encoding, "GB18030") == 0)
159 ret = _GB18030_init(xrl);
160 else if (strcmp(rl->__encoding, "GB2312") == 0)
161 ret = _GB2312_init(xrl);
162 else if (strcmp(rl->__encoding, "GBK") == 0)
163 ret = _GBK_init(xrl);
164 else if (strcmp(rl->__encoding, "BIG5") == 0)
165 ret = _BIG5_init(xrl);
166 else if (strcmp(rl->__encoding, "MSKanji") == 0)
167 ret = _MSKanji_init(xrl);
168 else if (strcmp(rl->__encoding, "UTF2") == 0)
169 ret = _UTF2_init(xrl);
170 else
171 ret = EFTYPE;
172 if (ret == 0) {
173 (void)strcpy(xrl->__ctype_encoding, encoding);
174 XL_RELEASE(loc->__lc_ctype);
175 loc->__lc_ctype = xrl;
176 if (loc == &__global_locale) {
177 _CurrentRuneLocale = &loc->__lc_ctype->_CurrentRuneLocale;
178 __mb_cur_max = loc->__lc_ctype->__mb_cur_max;
179 }
180 LOCK(cache_lock);
181 XL_RELEASE(CachedRuneLocale);
182 CachedRuneLocale = xrl;
183 XL_RETAIN(CachedRuneLocale);
184 UNLOCK(cache_lock);
185 } else
186 XL_RELEASE(xrl);
187
188 return (ret);
189 }
190
191 #ifdef LEGACY_RUNE_APIS
192 int
193 setrunelocale(const char *encoding)
194 {
195 int ret;
196
197 XL_LOCK(&__global_locale);
198 ret = __setrunelocale(encoding, &__global_locale);
199 XL_UNLOCK(&__global_locale);
200 return ret;
201 }
202 #endif /* LEGACY_RUNE_APIS */
203
204 __private_extern__ int
205 __wrap_setrunelocale(const char *locale, locale_t loc)
206 {
207 int ret = __setrunelocale(locale, loc);
208
209 if (ret != 0) {
210 errno = ret;
211 return (_LDP_ERROR);
212 }
213 return (_LDP_LOADED);
214 }
215