catopen.c revision 1.18 1 /* $NetBSD: catopen.c,v 1.18 2000/09/30 16:47:26 sommerfeld 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 <sys/param.h>
43 #include <sys/stat.h>
44 #include <sys/mman.h>
45
46 #include <assert.h>
47 #include <fcntl.h>
48 #include <limits.h>
49 #include <nl_types.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53
54 #define NLS_DEFAULT_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L"
55 #define NLS_DEFAULT_LANG "C"
56
57 #ifdef __weak_alias
58 __weak_alias(catopen, _catopen)
59 #endif
60
61 static nl_catd load_msgcat __P((const char *));
62
63 /* ARGSUSED */
64 nl_catd
65 _catopen(name, oflag)
66 const char *name;
67 int oflag;
68 {
69 char tmppath[PATH_MAX+1];
70 char *nlspath;
71 char *lang;
72 char *s, *t;
73 const char *u;
74 nl_catd catd;
75
76 if (name == NULL || *name == '\0')
77 return (nl_catd)-1;
78
79 /* absolute or relative path? */
80 if (strchr(name, '/'))
81 return load_msgcat(name);
82
83 if (issetugid() || (nlspath = getenv("NLSPATH")) == NULL)
84 nlspath = NLS_DEFAULT_PATH;
85 if ((lang = getenv("LANG")) == NULL || strchr(lang, '/'))
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 _DIAGASSERT(path != NULL);
142
143 if ((fd = open(path, O_RDONLY)) == -1)
144 return (nl_catd)-1;
145
146 if (fstat(fd, &st) != 0) {
147 close (fd);
148 return (nl_catd)-1;
149 }
150
151 data = mmap(0, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd,
152 (off_t)0);
153 close (fd);
154
155 if (data == (void *)-1) {
156 munmap(data, (size_t)st.st_size);
157 return (nl_catd)-1;
158 }
159
160 if (ntohl((u_int32_t)((struct _nls_cat_hdr *)data)->__magic) !=
161 _NLS_MAGIC) {
162 munmap(data, (size_t)st.st_size);
163 return (nl_catd)-1;
164 }
165
166 if ((catd = malloc(sizeof (*catd))) == 0) {
167 munmap(data, (size_t)st.st_size);
168 return (nl_catd)-1;
169 }
170
171 catd->__data = data;
172 catd->__size = (int)st.st_size;
173 return catd;
174 }
175