catopen.c revision 1.15 1 /* $NetBSD: catopen.c,v 1.15 1999/08/17 04:00:51 mycroft 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 <limits.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #include <sys/stat.h>
48 #include <sys/mman.h>
49 #include <unistd.h>
50 #include <fcntl.h>
51 #include <nl_types.h>
52
53 #define NLS_DEFAULT_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L"
54 #define NLS_DEFAULT_LANG "C"
55
56 #ifdef __weak_alias
57 __weak_alias(catopen, _catopen)
58 #endif
59
60 static nl_catd load_msgcat __P((const char *));
61
62 /* ARGSUSED */
63 nl_catd
64 _catopen(name, oflag)
65 const char *name;
66 int oflag;
67 {
68 char tmppath[PATH_MAX];
69 char *nlspath;
70 char *lang;
71 char *s, *t;
72 const char *u;
73 nl_catd catd;
74
75 if (name == NULL || *name == '\0')
76 return (nl_catd)-1;
77
78 /* absolute or relative path? */
79 if (strchr(name, '/'))
80 return load_msgcat(name);
81
82 /*
83 * XXX potential security problem here if this is used in a
84 * set-id program, and NLSPATH or LANG are set to read files
85 * the user normally does not have access to.
86 */
87 if ((nlspath = getenv("NLSPATH")) == NULL)
88 nlspath = NLS_DEFAULT_PATH;
89 if ((lang = getenv("LANG")) == NULL)
90 lang = NLS_DEFAULT_LANG;
91
92 s = nlspath;
93 t = tmppath;
94 do {
95 while (*s && *s != ':') {
96 if (*s == '%') {
97 switch (*(++s)) {
98 case 'L': /* locale */
99 u = lang;
100 while (*u && t < tmppath + PATH_MAX)
101 *t++ = *u++;
102 break;
103 case 'N': /* name */
104 u = name;
105 while (*u && t < tmppath + PATH_MAX)
106 *t++ = *u++;
107 break;
108 case 'l': /* lang */
109 case 't': /* territory */
110 case 'c': /* codeset */
111 break;
112 default:
113 if (t < tmppath + PATH_MAX)
114 *t++ = *s;
115 }
116 } else {
117 if (t < tmppath + PATH_MAX)
118 *t++ = *s;
119 }
120 s++;
121 }
122
123 *t = '\0';
124 catd = load_msgcat(tmppath);
125 if (catd != (nl_catd)-1)
126 return catd;
127
128 if (*s)
129 s++;
130 t = tmppath;
131 } while (*s);
132
133 return (nl_catd)-1;
134 }
135
136 static nl_catd
137 load_msgcat(path)
138 const char *path;
139 {
140 struct stat st;
141 nl_catd catd;
142 void *data;
143 int fd;
144
145 if ((fd = open(path, O_RDONLY)) == -1)
146 return (nl_catd)-1;
147
148 if (fstat(fd, &st) != 0) {
149 close (fd);
150 return (nl_catd)-1;
151 }
152
153 data = mmap(0, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd,
154 (off_t)0);
155 close (fd);
156
157 if (data == (void *)-1) {
158 munmap(data, (size_t)st.st_size);
159 return (nl_catd)-1;
160 }
161
162 if (ntohl((u_int32_t)((struct _nls_cat_hdr *)data)->__magic) !=
163 _NLS_MAGIC) {
164 munmap(data, (size_t)st.st_size);
165 return (nl_catd)-1;
166 }
167
168 if ((catd = malloc(sizeof (*catd))) == 0) {
169 munmap(data, (size_t)st.st_size);
170 return (nl_catd)-1;
171 }
172
173 catd->__data = data;
174 catd->__size = (int)st.st_size;
175 return catd;
176 }
177