1 /* $NetBSD: catopen.c,v 1.21 2004/07/21 20:27:46 tshiozak Exp $ */ 2 3 /*- 4 * Copyright (c) 1996 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by J.T. Conklin. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #define _NLS_PRIVATE 40 41 #include "namespace.h" 42 #include <sys/param.h> 43 #include <sys/stat.h> 44 #include <sys/mman.h> 45 46 #include <assert.h> 47 #include <fcntl.h> 48 #include <limits.h> 49 #include <locale.h> 50 #include <nl_types.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <unistd.h> 54 55 #ifdef CITRUS 56 #include <citrus/citrus_namespace.h> 57 #include <citrus/citrus_region.h> 58 #include <citrus/citrus_lookup.h> 59 #else 60 #include <locale/aliasname_local.h> 61 #define _lookup_alias(p, a, b, s, c) __unaliasname((p), (a), (b), (s)) 62 #endif 63 64 #define NLS_ALIAS_DB "/usr/share/nls/nls.alias" 65 66 #define NLS_DEFAULT_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L" 67 #define NLS_DEFAULT_LANG "C" 68 69 #ifdef __weak_alias 70 __weak_alias(catopen, _catopen) 71 #endif 72 73 static nl_catd load_msgcat __P((const char *)); 74 75 nl_catd 76 _catopen(name, oflag) 77 const char *name; 78 int oflag; 79 { 80 char tmppath[PATH_MAX+1]; 81 char *nlspath; 82 const char *lang; 83 char *s, *t; 84 const char *u; 85 nl_catd catd; 86 char langbuf[PATH_MAX]; 87 88 if (name == NULL || *name == '\0') 89 return (nl_catd)-1; 90 91 /* absolute or relative path? */ 92 if (strchr(name, '/')) 93 return load_msgcat(name); 94 95 if (issetugid() || (nlspath = getenv("NLSPATH")) == NULL) 96 nlspath = NLS_DEFAULT_PATH; 97 if (oflag == NL_CAT_LOCALE) { 98 lang = setlocale(LC_MESSAGES, NULL); 99 } 100 else { 101 lang = getenv("LANG"); 102 } 103 if (lang == NULL || strchr(lang, '/')) 104 lang = NLS_DEFAULT_LANG; 105 106 lang = _lookup_alias(NLS_ALIAS_DB, lang, langbuf, sizeof(langbuf), 107 _LOOKUP_CASE_SENSITIVE); 108 109 s = nlspath; 110 t = tmppath; 111 do { 112 while (*s && *s != ':') { 113 if (*s == '%') { 114 switch (*(++s)) { 115 case 'L': /* locale */ 116 u = lang; 117 while (*u && t < tmppath + PATH_MAX) 118 *t++ = *u++; 119 break; 120 case 'N': /* name */ 121 u = name; 122 while (*u && t < tmppath + PATH_MAX) 123 *t++ = *u++; 124 break; 125 case 'l': /* lang */ 126 case 't': /* territory */ 127 case 'c': /* codeset */ 128 break; 129 default: 130 if (t < tmppath + PATH_MAX) 131 *t++ = *s; 132 } 133 } else { 134 if (t < tmppath + PATH_MAX) 135 *t++ = *s; 136 } 137 s++; 138 } 139 140 *t = '\0'; 141 catd = load_msgcat(tmppath); 142 if (catd != (nl_catd)-1) 143 return catd; 144 145 if (*s) 146 s++; 147 t = tmppath; 148 } while (*s); 149 150 return (nl_catd)-1; 151 } 152 153 static nl_catd 154 load_msgcat(path) 155 const char *path; 156 { 157 struct stat st; 158 nl_catd catd; 159 void *data; 160 int fd; 161 162 _DIAGASSERT(path != NULL); 163 164 if ((fd = open(path, O_RDONLY)) == -1) 165 return (nl_catd)-1; 166 167 if (fstat(fd, &st) != 0) { 168 close (fd); 169 return (nl_catd)-1; 170 } 171 172 data = mmap(0, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd, 173 (off_t)0); 174 close (fd); 175 176 if (data == (void *)-1) { 177 munmap(data, (size_t)st.st_size); 178 return (nl_catd)-1; 179 } 180 181 if (ntohl((u_int32_t)((struct _nls_cat_hdr *)data)->__magic) != 182 _NLS_MAGIC) { 183 munmap(data, (size_t)st.st_size); 184 return (nl_catd)-1; 185 } 186 187 if ((catd = malloc(sizeof (*catd))) == 0) { 188 munmap(data, (size_t)st.st_size); 189 return (nl_catd)-1; 190 } 191 192 catd->__data = data; 193 catd->__size = (int)st.st_size; 194 return catd; 195 } 196