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 #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 */
71 __private_extern__
int
72 __setrunelocale(const char *encoding
, locale_t loc
)
76 struct __xlocale_st_runelocale
*xrl
;
79 static struct __xlocale_st_runelocale
*CachedRuneLocale
;
80 extern int __mb_cur_max
;
81 static pthread_lock_t cache_lock
= LOCK_INITIALIZER
;
84 * The "C" and "POSIX" locale are always here.
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
;
98 * If the locale name is the same as our cache, use the cache.
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
;
116 * Slurp the locale file into the cache.
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");
125 if ((fp
= fopen(name
, "r")) == NULL
)
126 return (errno
== 0 ? ENOENT
: errno
);
128 if ((xrl
= _Read_RuneMagi(fp
)) == NULL
) {
129 saverr
= (errno
== 0 ? EFTYPE
: errno
);
135 xrl
->__mbrtowc
= NULL
;
136 xrl
->__mbsinit
= NULL
;
137 xrl
->__mbsnrtowcs
= __mbsnrtowcs_std
;
138 xrl
->__wcrtomb
= NULL
;
139 xrl
->__wcsnrtombs
= __wcsnrtombs_std
;
141 rl
= &xrl
->_CurrentRuneLocale
;
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 */
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
);
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
;
181 XL_RELEASE(CachedRuneLocale
);
182 CachedRuneLocale
= xrl
;
183 XL_RETAIN(CachedRuneLocale
);
191 #ifdef LEGACY_RUNE_APIS
193 setrunelocale(const char *encoding
)
197 XL_LOCK(&__global_locale
);
198 ret
= __setrunelocale(encoding
, &__global_locale
);
199 XL_UNLOCK(&__global_locale
);
202 #endif /* LEGACY_RUNE_APIS */
204 __private_extern__
int
205 __wrap_setrunelocale(const char *locale
, locale_t loc
)
207 int ret
= __setrunelocale(locale
, loc
);
213 return (_LDP_LOADED
);