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 * 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.
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
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 $");
36 #include "xlocale_private.h"
48 #include "setlocale.h"
50 extern struct __xlocale_st_runelocale
*_Read_RuneMagi(FILE *);
52 #ifdef UNIFDEF_LEGACY_RUNE_APIS
53 /* depreciated interfaces */
54 rune_t
sgetrune(const char *, size_t, char const **);
55 int sputrune(rune_t
, char *, size_t, char **);
56 #endif /* UNIFDEF_LEGACY_RUNE_APIS */
58 __private_extern__
int
59 __setrunelocale(const char *encoding
, locale_t loc
)
63 struct __xlocale_st_runelocale
*xrl
;
66 static struct __xlocale_st_runelocale
*CachedRuneLocale
;
67 extern int __mb_cur_max
;
68 extern int __mb_sb_limit
;
69 static pthread_lock_t cache_lock
= LOCK_INITIALIZER
;
72 * The "C" and "POSIX" locale are always here.
74 if (strcmp(encoding
, "C") == 0 || strcmp(encoding
, "POSIX") == 0) {
75 XL_RELEASE(loc
->__lc_ctype
);
76 loc
->__lc_ctype
= &_DefaultRuneXLocale
;
77 /* no need to retain _DefaultRuneXLocale */
78 if (loc
== &__global_locale
) {
79 _CurrentRuneLocale
= &loc
->__lc_ctype
->_CurrentRuneLocale
;
80 __mb_cur_max
= loc
->__lc_ctype
->__mb_cur_max
;
81 __mb_sb_limit
= loc
->__lc_ctype
->__mb_sb_limit
;
87 * If the locale name is the same as our cache, use the cache.
90 if (CachedRuneLocale
!= NULL
&&
91 strcmp(encoding
, CachedRuneLocale
->__ctype_encoding
) == 0) {
92 XL_RELEASE(loc
->__lc_ctype
);
93 loc
->__lc_ctype
= CachedRuneLocale
;
94 XL_RETAIN(loc
->__lc_ctype
);
95 if (loc
== &__global_locale
) {
96 _CurrentRuneLocale
= &loc
->__lc_ctype
->_CurrentRuneLocale
;
97 __mb_cur_max
= loc
->__lc_ctype
->__mb_cur_max
;
98 __mb_sb_limit
= loc
->__lc_ctype
->__mb_sb_limit
;
106 * Slurp the locale file into the cache.
109 /* Range checking not needed, encoding length already checked before */
110 (void) strcpy(name
, encoding
);
111 (void) strcat(name
, "/LC_CTYPE");
113 if ((fp
= fdopen(__open_path_locale(name
), "r")) == NULL
)
114 return (errno
== 0 ? ENOENT
: errno
);
116 if ((xrl
= _Read_RuneMagi(fp
)) == NULL
) {
117 saverr
= (errno
== 0 ? EFTYPE
: errno
);
123 xrl
->__mbrtowc
= NULL
;
124 xrl
->__mbsinit
= NULL
;
125 xrl
->__mbsnrtowcs
= __mbsnrtowcs_std
;
126 xrl
->__wcrtomb
= NULL
;
127 xrl
->__wcsnrtombs
= __wcsnrtombs_std
;
129 rl
= &xrl
->_CurrentRuneLocale
;
131 #ifdef UNIFDEF_LEGACY_RUNE_APIS
132 /* provide backwards compatibility (depreciated interface) */
133 rl
->__sputrune
= sputrune
;
134 rl
->__sgetrune
= sgetrune
;
135 #else /* UNIFDEF_LEGACY_RUNE_APIS */
136 rl
->__sputrune
= NULL
;
137 rl
->__sgetrune
= NULL
;
138 #endif /* UNIFDEF_LEGACY_RUNE_APIS */
140 if (strcmp(rl
->__encoding
, "NONE") == 0)
141 ret
= _none_init(xrl
);
142 else if (strcmp(rl
->__encoding
, "ASCII") == 0)
143 ret
= _ascii_init(xrl
);
144 else if (strcmp(rl
->__encoding
, "UTF-8") == 0)
145 ret
= _UTF8_init(xrl
);
146 else if (strcmp(rl
->__encoding
, "EUC") == 0)
147 ret
= _EUC_init(xrl
);
148 else if (strcmp(rl
->__encoding
, "GB18030") == 0)
149 ret
= _GB18030_init(xrl
);
150 else if (strcmp(rl
->__encoding
, "GB2312") == 0)
151 ret
= _GB2312_init(xrl
);
152 else if (strcmp(rl
->__encoding
, "GBK") == 0)
153 ret
= _GBK_init(xrl
);
154 else if (strcmp(rl
->__encoding
, "BIG5") == 0)
155 ret
= _BIG5_init(xrl
);
156 else if (strcmp(rl
->__encoding
, "MSKanji") == 0)
157 ret
= _MSKanji_init(xrl
);
158 else if (strcmp(rl
->__encoding
, "UTF2") == 0)
159 ret
= _UTF2_init(xrl
);
164 (void)strcpy(xrl
->__ctype_encoding
, encoding
);
165 XL_RELEASE(loc
->__lc_ctype
);
166 loc
->__lc_ctype
= xrl
;
167 if (loc
== &__global_locale
) {
168 _CurrentRuneLocale
= &loc
->__lc_ctype
->_CurrentRuneLocale
;
169 __mb_cur_max
= loc
->__lc_ctype
->__mb_cur_max
;
170 __mb_sb_limit
= loc
->__lc_ctype
->__mb_sb_limit
;
173 XL_RELEASE(CachedRuneLocale
);
174 CachedRuneLocale
= xrl
;
175 XL_RETAIN(CachedRuneLocale
);
183 #ifdef UNIFDEF_LEGACY_RUNE_APIS
185 setrunelocale(const char *encoding
)
189 XL_LOCK(&__global_locale
);
190 ret
= __setrunelocale(encoding
, &__global_locale
);
191 XL_UNLOCK(&__global_locale
);
194 #endif /* UNIFDEF_LEGACY_RUNE_APIS */
196 __private_extern__
int
197 __wrap_setrunelocale(const char *locale
, locale_t loc
)
199 int ret
= __setrunelocale(locale
, loc
);
205 return (_LDP_LOADED
);