1 /* $OpenBSD: catopen.c,v 1.13 2008/06/26 05:42:05 ray Exp $ */ 2 /*- 3 * Copyright (c) 1996 The NetBSD Foundation, Inc. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to The NetBSD Foundation 7 * by J.T. Conklin. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #define _NLS_PRIVATE 32 33 #include <limits.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <sys/types.h> 37 #include <sys/param.h> 38 #include <sys/stat.h> 39 #include <sys/mman.h> 40 #include <unistd.h> 41 #include <fcntl.h> 42 #include <nl_types.h> 43 44 #define NLS_DEFAULT_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L" 45 #define NLS_DEFAULT_LANG "C" 46 47 static nl_catd load_msgcat(const char *); 48 49 /* ARGSUSED */ 50 nl_catd 51 _catopen(const char *name, int oflag) 52 { 53 char tmppath[PATH_MAX]; 54 char *nlspath; 55 char *lang; 56 char *s, *t; 57 const char *u; 58 nl_catd catd; 59 60 if (name == NULL || *name == '\0') 61 return (nl_catd) -1; 62 63 /* absolute or relative path? */ 64 if (strchr(name, '/')) 65 return load_msgcat(name); 66 67 if (issetugid() != 0 || (nlspath = getenv("NLSPATH")) == NULL) 68 nlspath = NLS_DEFAULT_PATH; 69 if ((lang = getenv("LANG")) == NULL) 70 lang = NLS_DEFAULT_LANG; 71 72 s = nlspath; 73 t = tmppath; 74 do { 75 while (*s && *s != ':') { 76 if (*s == '%') { 77 switch (*(++s)) { 78 case 'L': /* locale */ 79 u = lang; 80 while (*u && t < tmppath + PATH_MAX-1) 81 *t++ = *u++; 82 break; 83 case 'N': /* name */ 84 u = name; 85 while (*u && t < tmppath + PATH_MAX-1) 86 *t++ = *u++; 87 break; 88 case 'l': /* lang */ 89 case 't': /* territory */ 90 case 'c': /* codeset */ 91 break; 92 default: 93 if (t < tmppath + PATH_MAX-1) 94 *t++ = *s; 95 } 96 } else { 97 if (t < tmppath + PATH_MAX-1) 98 *t++ = *s; 99 } 100 s++; 101 } 102 103 *t = '\0'; 104 catd = load_msgcat(tmppath); 105 if (catd != (nl_catd) -1) 106 return catd; 107 108 if (*s) 109 s++; 110 t = tmppath; 111 } while (*s); 112 113 return (nl_catd) -1; 114 } 115 116 static nl_catd 117 load_msgcat(const char *path) 118 { 119 struct stat st; 120 nl_catd catd; 121 void *data; 122 int fd; 123 124 if ((fd = open(path, O_RDONLY)) == -1) 125 return (nl_catd) -1; 126 127 if (fstat(fd, &st) != 0) { 128 close (fd); 129 return (nl_catd) -1; 130 } 131 132 data = mmap(0, (size_t) st.st_size, PROT_READ, MAP_SHARED, fd, (off_t)0); 133 close (fd); 134 135 if (data == MAP_FAILED) { 136 munmap(data, (size_t) st.st_size); 137 return (nl_catd) -1; 138 } 139 140 if (ntohl(((struct _nls_cat_hdr *) data)->__magic) != _NLS_MAGIC) { 141 munmap(data, (size_t) st.st_size); 142 return (nl_catd) -1; 143 } 144 145 if ((catd = malloc(sizeof (*catd))) == 0) { 146 munmap(data, (size_t) st.st_size); 147 return (nl_catd) -1; 148 } 149 150 catd->__data = data; 151 catd->__size = st.st_size; 152 return catd; 153 } 154