1 /* $NetBSD: catopen.c,v 1.31 2012/07/30 23:02:41 yamt 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #if defined(LIBC_SCCS) && !defined(lint) 34 __RCSID("$NetBSD: catopen.c,v 1.31 2012/07/30 23:02:41 yamt Exp $"); 35 #endif /* LIBC_SCCS and not lint */ 36 37 #define _NLS_PRIVATE 38 39 #include "namespace.h" 40 #include <sys/param.h> 41 #include <sys/stat.h> 42 #include <sys/mman.h> 43 44 #include <assert.h> 45 #include <fcntl.h> 46 #include <limits.h> 47 #include <locale.h> 48 #include <nl_types.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <unistd.h> 52 53 #include "citrus_namespace.h" 54 #include "citrus_bcs.h" 55 #include "citrus_region.h" 56 #include "citrus_lookup.h" 57 #include "citrus_aliasname_local.h" 58 59 #define NLS_ALIAS_DB "/usr/share/nls/nls.alias" 60 61 #define NLS_DEFAULT_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L" 62 #define NLS_DEFAULT_LANG "C" 63 64 #ifdef __weak_alias 65 __weak_alias(catopen, _catopen) 66 #endif 67 68 static nl_catd load_msgcat(const char *); 69 70 nl_catd 71 _catopen(const char *name, int oflag) 72 { 73 char tmppath[PATH_MAX+1]; 74 const char *nlspath; 75 const char *lang, *reallang; 76 char *t; 77 const char *s, *u; 78 nl_catd catd; 79 char langbuf[PATH_MAX]; 80 81 if (name == NULL || *name == '\0') 82 return (nl_catd)-1; 83 84 /* absolute or relative path? */ 85 if (strchr(name, '/')) 86 return load_msgcat(name); 87 88 if (issetugid() || (nlspath = getenv("NLSPATH")) == NULL) 89 nlspath = NLS_DEFAULT_PATH; 90 /* 91 * histrical note: 92 * http://www.hauN.org/ml/b-l-j/a/800/828.html (in japanese) 93 */ 94 if (oflag == NL_CAT_LOCALE) { 95 lang = setlocale(LC_MESSAGES, NULL); 96 } else { 97 lang = getenv("LANG"); 98 } 99 if (lang == NULL || strchr(lang, '/')) 100 lang = NLS_DEFAULT_LANG; 101 102 reallang = __unaliasname(NLS_ALIAS_DB, lang, langbuf, sizeof(langbuf)); 103 if (reallang == NULL) 104 reallang = lang; 105 106 s = nlspath; 107 t = tmppath; 108 do { 109 while (*s && *s != ':') { 110 if (*s == '%') { 111 switch (*(++s)) { 112 case 'L': /* locale */ 113 u = reallang; 114 while (*u && t < tmppath + PATH_MAX) 115 *t++ = *u++; 116 break; 117 case 'N': /* name */ 118 u = name; 119 while (*u && t < tmppath + PATH_MAX) 120 *t++ = *u++; 121 break; 122 case 'l': /* lang */ 123 case 't': /* territory */ 124 case 'c': /* codeset */ 125 break; 126 default: 127 if (t < tmppath + PATH_MAX) 128 *t++ = *s; 129 } 130 } else { 131 if (t < tmppath + PATH_MAX) 132 *t++ = *s; 133 } 134 s++; 135 } 136 137 *t = '\0'; 138 catd = load_msgcat(tmppath); 139 if (catd != (nl_catd)-1) 140 return catd; 141 142 if (*s) 143 s++; 144 t = tmppath; 145 } while (*s); 146 147 return (nl_catd)-1; 148 } 149 150 static nl_catd 151 load_msgcat(const char *path) 152 { 153 struct stat st; 154 nl_catd catd; 155 void *data; 156 int fd; 157 158 _DIAGASSERT(path != NULL); 159 160 if ((fd = open(path, O_RDONLY)) == -1) 161 return (nl_catd)-1; 162 163 if (fstat(fd, &st) != 0) { 164 close (fd); 165 return (nl_catd)-1; 166 } 167 168 data = mmap(0, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd, 169 (off_t)0); 170 close (fd); 171 172 if (data == MAP_FAILED) { 173 return (nl_catd)-1; 174 } 175 176 if (ntohl((u_int32_t)((struct _nls_cat_hdr *)data)->__magic) != 177 _NLS_MAGIC) { 178 munmap(data, (size_t)st.st_size); 179 return (nl_catd)-1; 180 } 181 182 if ((catd = malloc(sizeof (*catd))) == NULL) { 183 munmap(data, (size_t)st.st_size); 184 return (nl_catd)-1; 185 } 186 187 catd->__data = data; 188 catd->__size = (int)st.st_size; 189 return catd; 190 } 191