1 /* $NetBSD: src/lib/libc/locale/setrunelocale.c,v 1.14 2003/08/07 16:43:07 agc Exp $ */
2 /* $DragonFly: src/lib/libc/locale/setrunelocale.c,v 1.6 2005/05/08 15:55:15 joerg Exp $ */
5 * Copyright (c)1999 Citrus Project,
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.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Copyright (c) 1998 The NetBSD Foundation, Inc.
32 * All rights reserved.
34 * This code is derived from software contributed to The NetBSD Foundation
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the NetBSD
48 * Foundation, Inc. and its contributors.
49 * 4. Neither the name of The NetBSD Foundation nor the names of its
50 * contributors may be used to endorse or promote products derived
51 * from this software without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
54 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
57 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63 * POSSIBILITY OF SUCH DAMAGE.
68 * The Regents of the University of California. All rights reserved.
70 * This code is derived from software contributed to Berkeley by
71 * Paul Borman at Krystal Technologies.
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution.
81 * 3. Neither the name of the University nor the names of its contributors
82 * may be used to endorse or promote products derived from this software
83 * without specific prior written permission.
85 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
86 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
89 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
90 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
91 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
92 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
93 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
94 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
108 #include "../citrus/citrus_module.h"
109 #include "../citrus/citrus_ctype.h"
111 #include "rune_local.h"
112 #include "multibyte.h"
116 _RuneLocale *runelocale;
117 struct localetable *next;
119 static struct localetable *localetable_head;
122 _findrunelocale(char *path)
124 struct localetable *lt;
126 _DIAGASSERT(path != NULL);
128 /* ones which we have seen already */
129 for (lt = localetable_head; lt; lt = lt->next) {
130 if (strcmp(path, lt->path) == 0)
131 return(lt->runelocale);
138 _newrunelocale(char *path)
140 struct localetable *lt;
145 /* path may be NULL (actually, it's checked below) */
147 if (path == NULL || strlen(path) + 1 > sizeof(lt->path))
150 rl = _findrunelocale(path);
154 if ((fp = fopen(path, "r")) == NULL)
157 if ((rl = _Read_RuneMagi(fp)) != NULL)
166 rl->rl_citrus_ctype = NULL;
167 ret = _citrus_ctype_open(&rl->rl_citrus_ctype, rl->rl_encoding,
168 rl->rl_variable, rl->rl_variable_len,
174 if (__MB_LEN_MAX_RUNTIME <
175 _citrus_ctype_get_mb_cur_max(rl->rl_citrus_ctype)) {
181 lt = malloc(sizeof(struct localetable));
186 strlcpy(lt->path, path, sizeof(lt->path));
188 lt->next = localetable_head;
189 localetable_head = lt;
195 _xpg4_setrunelocale(const char *encoding)
201 _DIAGASSERT(encoding != NULL);
203 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
204 rl = &_DefaultRuneLocale;
208 snprintf(path, sizeof(path), "%s/%s/LC_CTYPE", _PathLocale, encoding);
210 error = _newrunelocale(path);
213 rl = _findrunelocale(path);
218 _CurrentRuneLocale = rl;
219 __mb_cur_max = _citrus_ctype_get_mb_cur_max(rl->rl_citrus_ctype);