]> git.saurik.com Git - apple/libc.git/blob - locale/FreeBSD/setrunelocale.c.patch
Libc-594.1.4.tar.gz
[apple/libc.git] / locale / FreeBSD / setrunelocale.c.patch
1 --- setrunelocale.c.orig 2008-05-12 17:37:36.000000000 -0700
2 +++ setrunelocale.c 2008-05-13 00:32:37.000000000 -0700
3 @@ -37,6 +37,8 @@
4 #include <sys/cdefs.h>
5 __FBSDID("$FreeBSD: src/lib/libc/locale/setrunelocale.c,v 1.44 2004/10/18 02:06:18 ache Exp $");
6
7 +#include "xlocale_private.h"
8 +
9 #include <runetype.h>
10 #include <errno.h>
11 #include <limits.h>
12 @@ -49,67 +51,66 @@ __FBSDID("$FreeBSD: src/lib/libc/locale/
13 #include "mblocal.h"
14 #include "setlocale.h"
15
16 -extern int _none_init(_RuneLocale *);
17 -extern int _UTF8_init(_RuneLocale *);
18 -extern int _EUC_init(_RuneLocale *);
19 -extern int _GB18030_init(_RuneLocale *);
20 -extern int _GB2312_init(_RuneLocale *);
21 -extern int _GBK_init(_RuneLocale *);
22 -extern int _BIG5_init(_RuneLocale *);
23 -extern int _MSKanji_init(_RuneLocale *);
24 -extern _RuneLocale *_Read_RuneMagi(FILE *);
25 -
26 -static int __setrunelocale(const char *);
27 +extern int _none_init(struct __xlocale_st_runelocale *);
28 +extern int _UTF8_init(struct __xlocale_st_runelocale *);
29 +extern int _EUC_init(struct __xlocale_st_runelocale *);
30 +extern int _GB18030_init(struct __xlocale_st_runelocale *);
31 +extern int _GB2312_init(struct __xlocale_st_runelocale *);
32 +extern int _GBK_init(struct __xlocale_st_runelocale *);
33 +extern int _BIG5_init(struct __xlocale_st_runelocale *);
34 +extern int _MSKanji_init(struct __xlocale_st_runelocale *);
35 +extern int _UTF2_init(struct __xlocale_st_runelocale *); /* deprecated */
36 +extern struct __xlocale_st_runelocale *_Read_RuneMagi(FILE *);
37 +
38 +#ifdef LEGACY_RUNE_APIS
39 +/* depreciated interfaces */
40 +rune_t sgetrune(const char *, size_t, char const **);
41 +int sputrune(rune_t, char *, size_t, char **);
42 +#endif /* LEGACY_RUNE_APIS */
43
44 -static int
45 -__setrunelocale(const char *encoding)
46 +__private_extern__ int
47 +__setrunelocale(const char *encoding, locale_t loc)
48 {
49 FILE *fp;
50 char name[PATH_MAX];
51 + struct __xlocale_st_runelocale *xrl;
52 _RuneLocale *rl;
53 int saverr, ret;
54 - static char ctype_encoding[ENCODING_LEN + 1];
55 - static _RuneLocale *CachedRuneLocale;
56 - static int Cached__mb_cur_max;
57 - static size_t (*Cached__mbrtowc)(wchar_t * __restrict,
58 - const char * __restrict, size_t, mbstate_t * __restrict);
59 - static size_t (*Cached__wcrtomb)(char * __restrict, wchar_t,
60 - mbstate_t * __restrict);
61 - static int (*Cached__mbsinit)(const mbstate_t *);
62 - static size_t (*Cached__mbsnrtowcs)(wchar_t * __restrict,
63 - const char ** __restrict, size_t, size_t, mbstate_t * __restrict);
64 - static size_t (*Cached__wcsnrtombs)(char * __restrict,
65 - const wchar_t ** __restrict, size_t, size_t,
66 - mbstate_t * __restrict);
67 + static struct __xlocale_st_runelocale *CachedRuneLocale;
68 + extern int __mb_cur_max;
69 + static pthread_lock_t cache_lock = LOCK_INITIALIZER;
70
71 /*
72 * The "C" and "POSIX" locale are always here.
73 */
74 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
75 - _CurrentRuneLocale = &_DefaultRuneLocale;
76 - __mb_cur_max = 1;
77 - __mbrtowc = _none_mbrtowc;
78 - __mbsinit = _none_mbsinit;
79 - __mbsnrtowcs = _none_mbsnrtowcs;
80 - __wcrtomb = _none_wcrtomb;
81 - __wcsnrtombs = _none_wcsnrtombs;
82 + XL_RELEASE(loc->__lc_ctype);
83 + loc->__lc_ctype = &_DefaultRuneXLocale;
84 + /* no need to retain _DefaultRuneXLocale */
85 + if (loc == &__global_locale) {
86 + _CurrentRuneLocale = &loc->__lc_ctype->_CurrentRuneLocale;
87 + __mb_cur_max = loc->__lc_ctype->__mb_cur_max;
88 + }
89 return (0);
90 }
91
92 /*
93 * If the locale name is the same as our cache, use the cache.
94 */
95 + LOCK(cache_lock);
96 if (CachedRuneLocale != NULL &&
97 - strcmp(encoding, ctype_encoding) == 0) {
98 - _CurrentRuneLocale = CachedRuneLocale;
99 - __mb_cur_max = Cached__mb_cur_max;
100 - __mbrtowc = Cached__mbrtowc;
101 - __mbsinit = Cached__mbsinit;
102 - __mbsnrtowcs = Cached__mbsnrtowcs;
103 - __wcrtomb = Cached__wcrtomb;
104 - __wcsnrtombs = Cached__wcsnrtombs;
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;
112 + }
113 + UNLOCK(cache_lock);
114 return (0);
115 }
116 + UNLOCK(cache_lock);
117
118 /*
119 * Slurp the locale file into the cache.
120 @@ -124,63 +125,86 @@ __setrunelocale(const char *encoding)
121 if ((fp = fopen(name, "r")) == NULL)
122 return (errno == 0 ? ENOENT : errno);
123
124 - if ((rl = _Read_RuneMagi(fp)) == NULL) {
125 + if ((xrl = _Read_RuneMagi(fp)) == NULL) {
126 saverr = (errno == 0 ? EFTYPE : errno);
127 (void)fclose(fp);
128 return (saverr);
129 }
130 (void)fclose(fp);
131
132 - __mbrtowc = NULL;
133 - __mbsinit = NULL;
134 - __mbsnrtowcs = __mbsnrtowcs_std;
135 - __wcrtomb = NULL;
136 - __wcsnrtombs = __wcsnrtombs_std;
137 + xrl->__mbrtowc = NULL;
138 + xrl->__mbsinit = NULL;
139 + xrl->__mbsnrtowcs = __mbsnrtowcs_std;
140 + xrl->__wcrtomb = NULL;
141 + xrl->__wcsnrtombs = __wcsnrtombs_std;
142 +
143 + rl = &xrl->_CurrentRuneLocale;
144 +
145 +#ifdef LEGACY_RUNE_APIS
146 + /* provide backwards compatibility (depreciated interface) */
147 + rl->__sputrune = sputrune;
148 + rl->__sgetrune = sgetrune;
149 +#else /* LEGACY_RUNE_APIS */
150 rl->__sputrune = NULL;
151 rl->__sgetrune = NULL;
152 +#endif /* LEGACY_RUNE_APIS */
153 +
154 if (strcmp(rl->__encoding, "NONE") == 0)
155 - ret = _none_init(rl);
156 + ret = _none_init(xrl);
157 else if (strcmp(rl->__encoding, "UTF-8") == 0)
158 - ret = _UTF8_init(rl);
159 + ret = _UTF8_init(xrl);
160 else if (strcmp(rl->__encoding, "EUC") == 0)
161 - ret = _EUC_init(rl);
162 + ret = _EUC_init(xrl);
163 else if (strcmp(rl->__encoding, "GB18030") == 0)
164 - ret = _GB18030_init(rl);
165 + ret = _GB18030_init(xrl);
166 else if (strcmp(rl->__encoding, "GB2312") == 0)
167 - ret = _GB2312_init(rl);
168 + ret = _GB2312_init(xrl);
169 else if (strcmp(rl->__encoding, "GBK") == 0)
170 - ret = _GBK_init(rl);
171 + ret = _GBK_init(xrl);
172 else if (strcmp(rl->__encoding, "BIG5") == 0)
173 - ret = _BIG5_init(rl);
174 + ret = _BIG5_init(xrl);
175 else if (strcmp(rl->__encoding, "MSKanji") == 0)
176 - ret = _MSKanji_init(rl);
177 + ret = _MSKanji_init(xrl);
178 + else if (strcmp(rl->__encoding, "UTF2") == 0)
179 + ret = _UTF2_init(xrl);
180 else
181 ret = EFTYPE;
182 if (ret == 0) {
183 - if (CachedRuneLocale != NULL) {
184 - /* See euc.c */
185 - if (strcmp(CachedRuneLocale->__encoding, "EUC") == 0)
186 - free(CachedRuneLocale->__variable);
187 - free(CachedRuneLocale);
188 + (void)strcpy(xrl->__ctype_encoding, encoding);
189 + XL_RELEASE(loc->__lc_ctype);
190 + loc->__lc_ctype = xrl;
191 + if (loc == &__global_locale) {
192 + _CurrentRuneLocale = &loc->__lc_ctype->_CurrentRuneLocale;
193 + __mb_cur_max = loc->__lc_ctype->__mb_cur_max;
194 }
195 - CachedRuneLocale = _CurrentRuneLocale;
196 - Cached__mb_cur_max = __mb_cur_max;
197 - Cached__mbrtowc = __mbrtowc;
198 - Cached__mbsinit = __mbsinit;
199 - Cached__mbsnrtowcs = __mbsnrtowcs;
200 - Cached__wcrtomb = __wcrtomb;
201 - Cached__wcsnrtombs = __wcsnrtombs;
202 - (void)strcpy(ctype_encoding, encoding);
203 + LOCK(cache_lock);
204 + XL_RELEASE(CachedRuneLocale);
205 + CachedRuneLocale = xrl;
206 + XL_RETAIN(CachedRuneLocale);
207 + UNLOCK(cache_lock);
208 } else
209 - free(rl);
210 + XL_RELEASE(xrl);
211
212 return (ret);
213 }
214
215 +#ifdef LEGACY_RUNE_APIS
216 int
217 -__wrap_setrunelocale(const char *locale)
218 +setrunelocale(const char *encoding)
219 +{
220 + int ret;
221 +
222 + XL_LOCK(&__global_locale);
223 + ret = __setrunelocale(encoding, &__global_locale);
224 + XL_UNLOCK(&__global_locale);
225 + return ret;
226 +}
227 +#endif /* LEGACY_RUNE_APIS */
228 +
229 +__private_extern__ int
230 +__wrap_setrunelocale(const char *locale, locale_t loc)
231 {
232 - int ret = __setrunelocale(locale);
233 + int ret = __setrunelocale(locale, loc);
234
235 if (ret != 0) {
236 errno = ret;