1 /* $NetBSD: catopen.c,v 1.34 2025/12/16 12:39:02 nia 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 __RCSID("$NetBSD: catopen.c,v 1.34 2025/12/16 12:39:02 nia Exp $"); 34 35 #define _NLS_PRIVATE 36 #define __SETLOCALE_SOURCE__ 37 38 #include "namespace.h" 39 #include <sys/param.h> 40 #include <sys/endian.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 #include "setlocale_local.h" 59 60 #define NLS_ALIAS_DB "/usr/share/nls/nls.alias" 61 62 #define NLS_DEFAULT_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L" 63 #define NLS_DEFAULT_LANG "C" 64 65 __weak_alias(catopen, _catopen) 66 __weak_alias(catopen_l, _catopen_l) 67 68 static nl_catd load_msgcat(const char *); 69 70 nl_catd 71 catopen(const char *name, int oflag) 72 { 73 74 return catopen_l(name, oflag, _current_locale()); 75 } 76 77 nl_catd 78 catopen_l(const char *name, int oflag, locale_t loc) 79 { 80 char tmppath[PATH_MAX+1]; 81 const char *nlspath; 82 const char *lang, *reallang; 83 char *t; 84 const char *s, *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 /* 98 * Historical note: 99 * http://www.hauN.org/ml/b-l-j/a/800/828.html (in japanese) 100 */ 101 if (oflag == NL_CAT_LOCALE) { 102 lang = loc->part_name[LC_MESSAGES]; 103 } else { 104 lang = getenv("LANG"); 105 } 106 if (lang == NULL || strchr(lang, '/')) 107 lang = NLS_DEFAULT_LANG; 108 109 reallang = __unaliasname(NLS_ALIAS_DB, lang, langbuf, sizeof(langbuf)); 110 if (reallang == NULL) 111 reallang = lang; 112 113 s = nlspath; 114 t = tmppath; 115 do { 116 while (*s && *s != ':') { 117 if (*s == '%') { 118 switch (*(++s)) { 119 case 'L': /* locale */ 120 u = reallang; 121 while (*u && t < tmppath + PATH_MAX) 122 *t++ = *u++; 123 break; 124 case 'N': /* name */ 125 u = name; 126 while (*u && t < tmppath + PATH_MAX) 127 *t++ = *u++; 128 break; 129 case 'l': /* lang */ 130 case 't': /* territory */ 131 case 'c': /* codeset */ 132 break; 133 default: 134 if (t < tmppath + PATH_MAX) 135 *t++ = *s; 136 } 137 } else { 138 if (t < tmppath + PATH_MAX) 139 *t++ = *s; 140 } 141 s++; 142 } 143 144 *t = '\0'; 145 catd = load_msgcat(tmppath); 146 if (catd != (nl_catd)-1) 147 return catd; 148 149 if (*s) 150 s++; 151 t = tmppath; 152 } while (*s); 153 154 return (nl_catd)-1; 155 } 156 157 static nl_catd 158 load_msgcat(const char *path) 159 { 160 struct stat st; 161 nl_catd catd; 162 void *data; 163 int fd; 164 165 _DIAGASSERT(path != NULL); 166 167 if ((fd = open(path, O_RDONLY|O_CLOEXEC)) == -1) 168 return (nl_catd)-1; 169 170 if (fstat(fd, &st) != 0) { 171 close (fd); 172 return (nl_catd)-1; 173 } 174 175 data = mmap(0, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd, 176 (off_t)0); 177 close (fd); 178 179 if (data == MAP_FAILED) { 180 return (nl_catd)-1; 181 } 182 183 if (ntohl((u_int32_t)((struct _nls_cat_hdr *)data)->__magic) != 184 _NLS_MAGIC) { 185 munmap(data, (size_t)st.st_size); 186 return (nl_catd)-1; 187 } 188 189 if ((catd = malloc(sizeof (*catd))) == NULL) { 190 munmap(data, (size_t)st.st_size); 191 return (nl_catd)-1; 192 } 193 194 catd->__data = data; 195 catd->__size = (int)st.st_size; 196 return catd; 197 } 198