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
36 * $FreeBSD: src/lib/libc/locale/setrunelocale.c,v 1.14.6.4 2002/10/24 11:00:52 tjr Exp $
37 * $DragonFly: src/lib/libcr/locale/Attic/setrunelocale.c,v 1.3 2003/12/08 13:56:35 eirikn Exp $
47 #include "setlocale.h"
49 extern int _none_init(_RuneLocale *);
50 extern int _UTF2_init(_RuneLocale *);
51 extern int _UTF8_init(_RuneLocale *);
52 extern int _EUC_init(_RuneLocale *);
53 extern int _GB18030_init(_RuneLocale *);
54 extern int _GBK_init(_RuneLocale *);
55 extern int _BIG5_init(_RuneLocale *);
56 extern int _MSKanji_init(_RuneLocale *);
57 extern _RuneLocale *_Read_RuneMagi(FILE *);
60 setrunelocale(char *encoding)
66 static char ctype_encoding[ENCODING_LEN + 1];
67 static _RuneLocale *CachedRuneLocale;
68 static int Cached__mb_cur_max;
70 if (!encoding || !*encoding || strlen(encoding) > ENCODING_LEN ||
71 (encoding[0] == '.' &&
72 (encoding[1] == '\0' ||
73 (encoding[1] == '.' && encoding[2] == '\0'))) ||
74 strchr(encoding, '/') != NULL)
78 * The "C" and "POSIX" locale are always here.
80 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
81 _CurrentRuneLocale = &_DefaultRuneLocale;
87 * If the locale name is the same as our cache, use the cache.
89 if (CachedRuneLocale != NULL &&
90 strcmp(encoding, ctype_encoding) == 0) {
91 _CurrentRuneLocale = CachedRuneLocale;
92 __mb_cur_max = Cached__mb_cur_max;
97 * Slurp the locale file into the cache.
99 if (_PathLocale == NULL) {
100 char *p = getenv("PATH_LOCALE");
103 #ifndef __NETBSD_SYSCALLS
107 if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
108 1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
109 return (ENAMETOOLONG);
110 _PathLocale = strdup(p);
111 if (_PathLocale == NULL)
112 return (errno == 0 ? ENOMEM : errno);
114 _PathLocale = _PATH_LOCALE;
116 /* Range checking not needed, encoding length already checked above */
117 (void) strcpy(name, _PathLocale);
118 (void) strcat(name, "/");
119 (void) strcat(name, encoding);
120 (void) strcat(name, "/LC_CTYPE");
122 if ((fp = fopen(name, "r")) == NULL)
123 return (errno == 0 ? ENOENT : errno);
125 if ((rl = _Read_RuneMagi(fp)) == NULL) {
126 saverr = (errno == 0 ? EFTYPE : errno);
132 if (strcmp(rl->encoding, "NONE") == 0)
133 ret = _none_init(rl);
134 else if (strcmp(rl->encoding, "UTF2") == 0)
135 ret = _UTF2_init(rl);
136 else if (strcmp(rl->encoding, "UTF-8") == 0)
137 ret = _UTF8_init(rl);
138 else if (strcmp(rl->encoding, "EUC") == 0)
140 else if (strcmp(rl->encoding, "GB18030") == 0)
141 ret = _GB18030_init(rl);
142 else if (strcmp(rl->encoding, "GBK") == 0)
144 else if (strcmp(rl->encoding, "BIG5") == 0)
145 ret = _BIG5_init(rl);
146 else if (strcmp(rl->encoding, "MSKanji") == 0)
147 ret = _MSKanji_init(rl);
151 if (CachedRuneLocale != NULL) {
153 if (strcmp(CachedRuneLocale->encoding, "EUC") == 0)
154 free(CachedRuneLocale->variable);
155 free(CachedRuneLocale);
157 CachedRuneLocale = _CurrentRuneLocale;
158 Cached__mb_cur_max = __mb_cur_max;
159 (void)strcpy(ctype_encoding, encoding);