catopen.c revision 1.20 1 /* $NetBSD: catopen.c,v 1.20 2004/07/21 14:17:22 tshiozak 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 <locale.h>
50 #include <nl_types.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 #include "../citrus/citrus_namespace.h"
56 #include "../citrus/citrus_region.h"
57 #include "../citrus/citrus_lookup.h"
58
59 #define NLS_ALIAS_DB "/usr/share/nls/nls.alias"
60
61 #define NLS_DEFAULT_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L"
62 #define NLS_DEFAULT_LANG "C"
63
64 #ifdef __weak_alias
65 __weak_alias(catopen, _catopen)
66 #endif
67
68 static nl_catd load_msgcat __P((const char *));
69
70 nl_catd
71 _catopen(name, oflag)
72 const char *name;
73 int oflag;
74 {
75 char tmppath[PATH_MAX+1];
76 char *nlspath;
77 const char *lang;
78 char *s, *t;
79 const char *u;
80 nl_catd catd;
81 char langbuf[PATH_MAX];
82
83 if (name == NULL || *name == '\0')
84 return (nl_catd)-1;
85
86 /* absolute or relative path? */
87 if (strchr(name, '/'))
88 return load_msgcat(name);
89
90 if (issetugid() || (nlspath = getenv("NLSPATH")) == NULL)
91 nlspath = NLS_DEFAULT_PATH;
92 if (oflag == NL_CAT_LOCALE) {
93 lang = setlocale(LC_MESSAGES, NULL);
94 }
95 else {
96 lang = getenv("LANG");
97 }
98 if (lang == NULL || strchr(lang, '/'))
99 lang = NLS_DEFAULT_LANG;
100
101 lang = _lookup_alias(NLS_ALIAS_DB, lang, langbuf, sizeof(langbuf),
102 _LOOKUP_CASE_SENSITIVE);
103
104 s = nlspath;
105 t = tmppath;
106 do {
107 while (*s && *s != ':') {
108 if (*s == '%') {
109 switch (*(++s)) {
110 case 'L': /* locale */
111 u = lang;
112 while (*u && t < tmppath + PATH_MAX)
113 *t++ = *u++;
114 break;
115 case 'N': /* name */
116 u = name;
117 while (*u && t < tmppath + PATH_MAX)
118 *t++ = *u++;
119 break;
120 case 'l': /* lang */
121 case 't': /* territory */
122 case 'c': /* codeset */
123 break;
124 default:
125 if (t < tmppath + PATH_MAX)
126 *t++ = *s;
127 }
128 } else {
129 if (t < tmppath + PATH_MAX)
130 *t++ = *s;
131 }
132 s++;
133 }
134
135 *t = '\0';
136 catd = load_msgcat(tmppath);
137 if (catd != (nl_catd)-1)
138 return catd;
139
140 if (*s)
141 s++;
142 t = tmppath;
143 } while (*s);
144
145 return (nl_catd)-1;
146 }
147
148 static nl_catd
149 load_msgcat(path)
150 const char *path;
151 {
152 struct stat st;
153 nl_catd catd;
154 void *data;
155 int fd;
156
157 _DIAGASSERT(path != NULL);
158
159 if ((fd = open(path, O_RDONLY)) == -1)
160 return (nl_catd)-1;
161
162 if (fstat(fd, &st) != 0) {
163 close (fd);
164 return (nl_catd)-1;
165 }
166
167 data = mmap(0, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd,
168 (off_t)0);
169 close (fd);
170
171 if (data == (void *)-1) {
172 munmap(data, (size_t)st.st_size);
173 return (nl_catd)-1;
174 }
175
176 if (ntohl((u_int32_t)((struct _nls_cat_hdr *)data)->__magic) !=
177 _NLS_MAGIC) {
178 munmap(data, (size_t)st.st_size);
179 return (nl_catd)-1;
180 }
181
182 if ((catd = malloc(sizeof (*catd))) == 0) {
183 munmap(data, (size_t)st.st_size);
184 return (nl_catd)-1;
185 }
186
187 catd->__data = data;
188 catd->__size = (int)st.st_size;
189 return catd;
190 }
191