]> git.saurik.com Git - apple/libc.git/blame - locale/setrunelocale-fbsd.c
Libc-498.1.5.tar.gz
[apple/libc.git] / locale / setrunelocale-fbsd.c
CommitLineData
224c7076
A
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
54extern int _none_init(struct __xlocale_st_runelocale *);
55extern int _UTF8_init(struct __xlocale_st_runelocale *);
56extern int _EUC_init(struct __xlocale_st_runelocale *);
57extern int _GB18030_init(struct __xlocale_st_runelocale *);
58extern int _GB2312_init(struct __xlocale_st_runelocale *);
59extern int _GBK_init(struct __xlocale_st_runelocale *);
60extern int _BIG5_init(struct __xlocale_st_runelocale *);
61extern int _MSKanji_init(struct __xlocale_st_runelocale *);
62extern int _UTF2_init(struct __xlocale_st_runelocale *); /* deprecated */
63extern struct __xlocale_st_runelocale *_Read_RuneMagi(FILE *);
64
65extern void spin_lock(int *);
66extern void spin_unlock(int *);
67
b5d655f7 68#ifdef LEGACY_RUNE_APIS
224c7076
A
69/* depreciated interfaces */
70rune_t sgetrune(const char *, size_t, char const **);
71int sputrune(rune_t, char *, size_t, char **);
b5d655f7 72#endif /* LEGACY_RUNE_APIS */
224c7076
A
73
74__private_extern__ int
75__setrunelocale(const char *encoding, locale_t loc)
76{
77 FILE *fp;
78 char name[PATH_MAX];
79 struct __xlocale_st_runelocale *xrl;
80 _RuneLocale *rl;
81 int saverr, ret;
82 static struct __xlocale_st_runelocale *CachedRuneLocale;
83 extern int __mb_cur_max;
84 static int cache_lock = 0;
85
86 /*
87 * The "C" and "POSIX" locale are always here.
88 */
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;
96 }
97 return (0);
98 }
99
100 /*
101 * If the locale name is the same as our cache, use the cache.
102 */
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;
112 }
113 spin_unlock(&cache_lock);
114 return (0);
115 }
116 spin_unlock(&cache_lock);
117
118 /*
119 * Slurp the locale file into the cache.
120 */
121
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");
127
128 if ((fp = fopen(name, "r")) == NULL)
129 return (errno == 0 ? ENOENT : errno);
130
131 if ((xrl = _Read_RuneMagi(fp)) == NULL) {
132 saverr = (errno == 0 ? EFTYPE : errno);
133 (void)fclose(fp);
134 return (saverr);
135 }
136 (void)fclose(fp);
137
138 xrl->__mbrtowc = NULL;
139 xrl->__mbsinit = NULL;
140 xrl->__mbsnrtowcs = __mbsnrtowcs_std;
141 xrl->__wcrtomb = NULL;
142 xrl->__wcsnrtombs = __wcsnrtombs_std;
143
144 rl = &xrl->_CurrentRuneLocale;
145
b5d655f7 146#ifdef LEGACY_RUNE_APIS
224c7076
A
147 /* provide backwards compatibility (depreciated interface) */
148 rl->__sputrune = sputrune;
149 rl->__sgetrune = sgetrune;
b5d655f7
A
150#else /* LEGACY_RUNE_APIS */
151 rl->__sputrune = NULL;
152 rl->__sgetrune = NULL;
153#endif /* LEGACY_RUNE_APIS */
224c7076
A
154
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);
173 else
174 ret = EFTYPE;
175 if (ret == 0) {
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;
182 }
183 spin_lock(&cache_lock);
184 XL_RELEASE(CachedRuneLocale);
185 CachedRuneLocale = xrl;
186 XL_RETAIN(CachedRuneLocale);
187 spin_unlock(&cache_lock);
188 } else
189 XL_RELEASE(xrl);
190
191 return (ret);
192}
193
b5d655f7 194#ifdef LEGACY_RUNE_APIS
224c7076
A
195int
196setrunelocale(const char *encoding)
197{
198 return __setrunelocale(encoding, &__global_locale);
199}
b5d655f7 200#endif /* LEGACY_RUNE_APIS */
224c7076
A
201
202__private_extern__ int
203__wrap_setrunelocale(const char *locale, locale_t loc)
204{
205 int ret = __setrunelocale(locale, loc);
206
207 if (ret != 0) {
208 errno = ret;
209 return (_LDP_ERROR);
210 }
211 return (_LDP_LOADED);
212}
213