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 $ */ 3 4 /*- 5 * Copyright (c)1999 Citrus Project, 6 * All rights reserved. 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 * 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 27 * SUCH DAMAGE. 28 */ 29 30 /*- 31 * Copyright (c) 1998 The NetBSD Foundation, Inc. 32 * All rights reserved. 33 * 34 * This code is derived from software contributed to The NetBSD Foundation 35 * by Paul Kranenburg. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 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. 52 * 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. 64 */ 65 66 /*- 67 * Copyright (c) 1993 68 * The Regents of the University of California. All rights reserved. 69 * 70 * This code is derived from software contributed to Berkeley by 71 * Paul Borman at Krystal Technologies. 72 * 73 * Redistribution and use in source and binary forms, with or without 74 * modification, are permitted provided that the following conditions 75 * are met: 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. 84 * 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 95 * SUCH DAMAGE. 96 */ 97 98 #include <assert.h> 99 #include <errno.h> 100 #include <limits.h> 101 #include <locale.h> 102 #include <stddef.h> 103 #include <stdio.h> 104 #include <stdlib.h> 105 #include <string.h> 106 #include <unistd.h> 107 #include <wchar.h> 108 #include "../citrus/citrus_module.h" 109 #include "../citrus/citrus_ctype.h" 110 #include "rune.h" 111 #include "rune_local.h" 112 #include "multibyte.h" 113 114 struct localetable { 115 char path[PATH_MAX]; 116 _RuneLocale *runelocale; 117 struct localetable *next; 118 }; 119 static struct localetable *localetable_head; 120 121 _RuneLocale * 122 _findrunelocale(char *path) 123 { 124 struct localetable *lt; 125 126 _DIAGASSERT(path != NULL); 127 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); 132 } 133 134 return(NULL); 135 } 136 137 int 138 _newrunelocale(char *path) 139 { 140 struct localetable *lt; 141 FILE *fp; 142 _RuneLocale *rl; 143 int ret; 144 145 /* path may be NULL (actually, it's checked below) */ 146 147 if (path == NULL || strlen(path) + 1 > sizeof(lt->path)) 148 return(EFAULT); 149 150 rl = _findrunelocale(path); 151 if (rl) 152 return(0); 153 154 if ((fp = fopen(path, "r")) == NULL) 155 return(ENOENT); 156 157 if ((rl = _Read_RuneMagi(fp)) != NULL) 158 goto found; 159 160 fclose(fp); 161 return(EFTYPE); 162 163 found: 164 fclose(fp); 165 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, 169 _PRIVSIZE); 170 if (ret) { 171 _NukeRune(rl); 172 return(ret); 173 } 174 if (__MB_LEN_MAX_RUNTIME < 175 _citrus_ctype_get_mb_cur_max(rl->rl_citrus_ctype)) { 176 _NukeRune(rl); 177 return(EINVAL); 178 } 179 180 /* register it */ 181 lt = malloc(sizeof(struct localetable)); 182 if (lt == NULL) { 183 _NukeRune(rl); 184 return(ENOMEM); 185 } 186 strlcpy(lt->path, path, sizeof(lt->path)); 187 lt->runelocale = rl; 188 lt->next = localetable_head; 189 localetable_head = lt; 190 191 return(0); 192 } 193 194 int 195 _xpg4_setrunelocale(const char *encoding) 196 { 197 char path[PATH_MAX]; 198 _RuneLocale *rl; 199 int error; 200 201 _DIAGASSERT(encoding != NULL); 202 203 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) { 204 rl = &_DefaultRuneLocale; 205 goto found; 206 } 207 208 snprintf(path, sizeof(path), "%s/%s/LC_CTYPE", _PathLocale, encoding); 209 210 error = _newrunelocale(path); 211 if (error) 212 return(error); 213 rl = _findrunelocale(path); 214 if (!rl) 215 return(ENOENT); 216 217 found: 218 _CurrentRuneLocale = rl; 219 __mb_cur_max = _citrus_ctype_get_mb_cur_max(rl->rl_citrus_ctype); 220 221 return(0); 222 } 223