1/* $NetBSD: catopen.c,v 1.28 2009/03/10 13:15:40 joerg 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.28 2009/03/10 13:15:40 joerg 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 68static nl_catd load_msgcat __P((const char *)); 69 70nl_catd 71_catopen(name, oflag) 72 const char *name; 73 int oflag; 74{ 75 char tmppath[PATH_MAX+1]; 76 const char *nlspath; 77 const char *lang, *reallang; 78 char *t; 79 const char *s, *u; 80 nl_catd catd; 81 char langbuf[PATH_MAX]; 82 83 if (name == NULL || *name == '\0') 84 return (nl_catd)-1; 85 86 /* absolute or relative path? */ 87 if (strchr(name, '/')) 88 return load_msgcat(name); 89 90 if (issetugid() || (nlspath = getenv("NLSPATH")) == NULL) 91 nlspath = NLS_DEFAULT_PATH; 92 if (oflag == NL_CAT_LOCALE) { 93 lang = setlocale(LC_MESSAGES, NULL); 94 } 95 else { 96 lang = getenv("LANG"); 97 } 98 if (lang == NULL || strchr(lang, '/')) 99 lang = NLS_DEFAULT_LANG; 100 101 reallang = __unaliasname(NLS_ALIAS_DB, lang, langbuf, sizeof(langbuf)); 102 if (reallang == NULL) 103 reallang = lang; 104 105 s = nlspath; 106 t = tmppath; 107 do { 108 while (*s && *s != ':') { 109 if (*s == '%') { 110 switch (*(++s)) { 111 case 'L': /* locale */ 112 u = reallang; 113 while (*u && t < tmppath + PATH_MAX) 114 *t++ = *u++; 115 break; 116 case 'N': /* name */ 117 u = name; 118 while (*u && t < tmppath + PATH_MAX) 119 *t++ = *u++; 120 break; 121 case 'l': /* lang */ 122 case 't': /* territory */ 123 case 'c': /* codeset */ 124 break; 125 default: 126 if (t < tmppath + PATH_MAX) 127 *t++ = *s; 128 } 129 } else { 130 if (t < tmppath + PATH_MAX) 131 *t++ = *s; 132 } 133 s++; 134 } 135 136 *t = '\0'; 137 catd = load_msgcat(tmppath); 138 if (catd != (nl_catd)-1) 139 return catd; 140 141 if (*s) 142 s++; 143 t = tmppath; 144 } while (*s); 145 146 return (nl_catd)-1; 147} 148 149static nl_catd 150load_msgcat(path) 151 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