]>
git.saurik.com Git - apple/libc.git/blob - locale/setrunelocale-fbsd.c
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: src/lib/libc/locale/setrunelocale.c,v 1.44 2004/10/18 02:06:18 ache Exp $");
40 #include "xlocale_private.h"
52 #include "setlocale.h"
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 *);
65 extern void spin_lock(int *);
66 extern void spin_unlock(int *);
68 #ifdef LEGACY_RUNE_APIS
69 /* depreciated interfaces */
70 rune_t
sgetrune(const char *, size_t, char const **);
71 int sputrune(rune_t
, char *, size_t, char **);
72 #endif /* LEGACY_RUNE_APIS */
74 __private_extern__
int
75 __setrunelocale(const char *encoding
, locale_t loc
)
79 struct __xlocale_st_runelocale
*xrl
;
82 static struct __xlocale_st_runelocale
*CachedRuneLocale
;
83 extern int __mb_cur_max
;
84 static int cache_lock
= 0;
87 * The "C" and "POSIX" locale are always here.
89 if (strcmp(encoding
, "C") == 0 || strcmp(encoding
, "POSIX") == 0) {
90 XL_RELEASE(loc
->__lc_ctype
);
91 loc
->__lc_ctype
= &_DefaultRuneXLocale
;
92 /* no need to retain _DefaultRuneXLocale */
93 if (loc
== &__global_locale
) {
94 _CurrentRuneLocale
= &loc
->__lc_ctype
->_CurrentRuneLocale
;
95 __mb_cur_max
= loc
->__lc_ctype
->__mb_cur_max
;
101 * If the locale name is the same as our cache, use the cache.
103 spin_lock(&cache_lock
);
104 if (CachedRuneLocale
!= NULL
&&
105 strcmp(encoding
, CachedRuneLocale
->__ctype_encoding
) == 0) {
106 XL_RELEASE(loc
->__lc_ctype
);
107 loc
->__lc_ctype
= CachedRuneLocale
;
108 XL_RETAIN(loc
->__lc_ctype
);
109 if (loc
== &__global_locale
) {
110 _CurrentRuneLocale
= &loc
->__lc_ctype
->_CurrentRuneLocale
;
111 __mb_cur_max
= loc
->__lc_ctype
->__mb_cur_max
;
113 spin_unlock(&cache_lock
);
116 spin_unlock(&cache_lock
);
119 * Slurp the locale file into the cache.
122 /* Range checking not needed, encoding length already checked before */
123 (void) strcpy(name
, _PathLocale
);
124 (void) strcat(name
, "/");
125 (void) strcat(name
, encoding
);
126 (void) strcat(name
, "/LC_CTYPE");
128 if ((fp
= fopen(name
, "r")) == NULL
)
129 return (errno
== 0 ? ENOENT
: errno
);
131 if ((xrl
= _Read_RuneMagi(fp
)) == NULL
) {
132 saverr
= (errno
== 0 ? EFTYPE
: errno
);
138 xrl
->__mbrtowc
= NULL
;
139 xrl
->__mbsinit
= NULL
;
140 xrl
->__mbsnrtowcs
= __mbsnrtowcs_std
;
141 xrl
->__wcrtomb
= NULL
;
142 xrl
->__wcsnrtombs
= __wcsnrtombs_std
;
144 rl
= &xrl
->_CurrentRuneLocale
;
146 #ifdef LEGACY_RUNE_APIS
147 /* provide backwards compatibility (depreciated interface) */
148 rl
->__sputrune
= sputrune
;
149 rl
->__sgetrune
= sgetrune
;
150 #else /* LEGACY_RUNE_APIS */
151 rl
->__sputrune
= NULL
;
152 rl
->__sgetrune
= NULL
;
153 #endif /* LEGACY_RUNE_APIS */
155 if (strcmp(rl
->__encoding
, "NONE") == 0)
156 ret
= _none_init(xrl
);
157 else if (strcmp(rl
->__encoding
, "UTF-8") == 0)
158 ret
= _UTF8_init(xrl
);
159 else if (strcmp(rl
->__encoding
, "EUC") == 0)
160 ret
= _EUC_init(xrl
);
161 else if (strcmp(rl
->__encoding
, "GB18030") == 0)
162 ret
= _GB18030_init(xrl
);
163 else if (strcmp(rl
->__encoding
, "GB2312") == 0)
164 ret
= _GB2312_init(xrl
);
165 else if (strcmp(rl
->__encoding
, "GBK") == 0)
166 ret
= _GBK_init(xrl
);
167 else if (strcmp(rl
->__encoding
, "BIG5") == 0)
168 ret
= _BIG5_init(xrl
);
169 else if (strcmp(rl
->__encoding
, "MSKanji") == 0)
170 ret
= _MSKanji_init(xrl
);
171 else if (strcmp(rl
->__encoding
, "UTF2") == 0)
172 ret
= _UTF2_init(xrl
);
176 (void)strcpy(xrl
->__ctype_encoding
, encoding
);
177 XL_RELEASE(loc
->__lc_ctype
);
178 loc
->__lc_ctype
= xrl
;
179 if (loc
== &__global_locale
) {
180 _CurrentRuneLocale
= &loc
->__lc_ctype
->_CurrentRuneLocale
;
181 __mb_cur_max
= loc
->__lc_ctype
->__mb_cur_max
;
183 spin_lock(&cache_lock
);
184 XL_RELEASE(CachedRuneLocale
);
185 CachedRuneLocale
= xrl
;
186 XL_RETAIN(CachedRuneLocale
);
187 spin_unlock(&cache_lock
);
194 #ifdef LEGACY_RUNE_APIS
196 setrunelocale(const char *encoding
)
198 return __setrunelocale(encoding
, &__global_locale
);
200 #endif /* LEGACY_RUNE_APIS */
202 __private_extern__
int
203 __wrap_setrunelocale(const char *locale
, locale_t loc
)
205 int ret
= __setrunelocale(locale
, loc
);
211 return (_LDP_LOADED
);