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