catopen.c revision 1.11 1 /* $NetBSD: catopen.c,v 1.11 1997/07/13 19:58:37 christos 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 REGENTS OR CONTRIBUTORS BE
30 * 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 static nl_catd load_msgcat __P((const char *));
57
58 nl_catd
59 _catopen(name, oflag)
60 const char *name;
61 int oflag;
62 {
63 char tmppath[PATH_MAX];
64 char *nlspath;
65 char *lang;
66 char *s, *t;
67 const char *u;
68 nl_catd catd;
69
70 if (name == NULL || *name == '\0')
71 return (nl_catd)-1;
72
73 /* absolute or relative path? */
74 if (strchr(name, '/'))
75 return load_msgcat(name);
76
77 /*
78 * XXX potential security problem here if this is used in a
79 * set-id program, and NLSPATH or LANG are set to read files
80 * the user normally does not have access to.
81 */
82 if ((nlspath = getenv("NLSPATH")) == NULL)
83 nlspath = NLS_DEFAULT_PATH;
84 if ((lang = getenv("LANG")) == NULL)
85 lang = NLS_DEFAULT_LANG;
86
87 s = nlspath;
88 t = tmppath;
89 do {
90 while (*s && *s != ':') {
91 if (*s == '%') {
92 switch (*(++s)) {
93 case 'L': /* locale */
94 u = lang;
95 while (*u && t < tmppath + PATH_MAX)
96 *t++ = *u++;
97 break;
98 case 'N': /* name */
99 u = name;
100 while (*u && t < tmppath + PATH_MAX)
101 *t++ = *u++;
102 break;
103 case 'l': /* lang */
104 case 't': /* territory */
105 case 'c': /* codeset */
106 break;
107 default:
108 if (t < tmppath + PATH_MAX)
109 *t++ = *s;
110 }
111 } else {
112 if (t < tmppath + PATH_MAX)
113 *t++ = *s;
114 }
115 s++;
116 }
117
118 *t = '\0';
119 catd = load_msgcat(tmppath);
120 if (catd != (nl_catd)-1)
121 return catd;
122
123 if (*s)
124 s++;
125 t = tmppath;
126 } while (*s);
127
128 return (nl_catd)-1;
129 }
130
131 static nl_catd
132 load_msgcat(path)
133 const char *path;
134 {
135 struct stat st;
136 nl_catd catd;
137 void *data;
138 int fd;
139
140 if ((fd = open(path, O_RDONLY)) == -1)
141 return (nl_catd)-1;
142
143 if (fstat(fd, &st) != 0) {
144 close (fd);
145 return (nl_catd)-1;
146 }
147
148 data = mmap(0, (size_t)st.st_size, PROT_READ, MAP_SHARED, fd, 0);
149 close (fd);
150
151 if (data == (void *)-1) {
152 munmap(data, (size_t)st.st_size);
153 return (nl_catd)-1;
154 }
155
156 if (ntohl(((struct _nls_cat_hdr *)data)->__magic) != _NLS_MAGIC) {
157 munmap(data, (size_t)st.st_size);
158 return (nl_catd)-1;
159 }
160
161 if ((catd = malloc(sizeof (*catd))) == 0) {
162 munmap(data, (size_t)st.st_size);
163 return (nl_catd)-1;
164 }
165
166 catd->__data = data;
167 catd->__size = st.st_size;
168 return catd;
169 }
170