catopen.c revision 1.14.2.1 1 /* $NetBSD: catopen.c,v 1.14.2.1 2000/10/04 14:07:22 he 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+1];
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 if (issetugid() || (nlspath = getenv("NLSPATH")) == NULL)
79 nlspath = NLS_DEFAULT_PATH;
80 if ((lang = getenv("LANG")) == NULL || strchr(lang, '/'))
81 lang = NLS_DEFAULT_LANG;
82
83 s = nlspath;
84 t = tmppath;
85 do {
86 while (*s && *s != ':') {
87 if (*s == '%') {
88 switch (*(++s)) {
89 case 'L': /* locale */
90 u = lang;
91 while (*u && t < tmppath + PATH_MAX)
92 *t++ = *u++;
93 break;
94 case 'N': /* name */
95 u = name;
96 while (*u && t < tmppath + PATH_MAX)
97 *t++ = *u++;
98 break;
99 case 'l': /* lang */
100 case 't': /* territory */
101 case 'c': /* codeset */
102 break;
103 default:
104 if (t < tmppath + PATH_MAX)
105 *t++ = *s;
106 }
107 } else {
108 if (t < tmppath + PATH_MAX)
109 *t++ = *s;
110 }
111 s++;
112 }
113
114 *t = '\0';
115 catd = load_msgcat(tmppath);
116 if (catd != (nl_catd)-1)
117 return catd;
118
119 if (*s)
120 s++;
121 t = tmppath;
122 } while (*s);
123
124 return (nl_catd)-1;
125 }
126
127 static nl_catd
128 load_msgcat(path)
129 const char *path;
130 {
131 struct stat st;
132 nl_catd catd;
133 void *data;
134 int fd;
135
136 if ((fd = open(path, O_RDONLY)) == -1)
137 return (nl_catd)-1;
138
139 if (fstat(fd, &st) != 0) {
140 close (fd);
141 return (nl_catd)-1;
142 }
143
144 data = mmap(0, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd,
145 (off_t)0);
146 close (fd);
147
148 if (data == (void *)-1) {
149 munmap(data, (size_t)st.st_size);
150 return (nl_catd)-1;
151 }
152
153 if (ntohl((u_int32_t)((struct _nls_cat_hdr *)data)->__magic) !=
154 _NLS_MAGIC) {
155 munmap(data, (size_t)st.st_size);
156 return (nl_catd)-1;
157 }
158
159 if ((catd = malloc(sizeof (*catd))) == 0) {
160 munmap(data, (size_t)st.st_size);
161 return (nl_catd)-1;
162 }
163
164 catd->__data = data;
165 catd->__size = (int)st.st_size;
166 return catd;
167 }
168