catopen.c revision 1.28.6.1 1 /* $NetBSD: catopen.c,v 1.28.6.1 2012/04/17 00:05:22 yamt 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 __RCSID("$NetBSD: catopen.c,v 1.28.6.1 2012/04/17 00:05:22 yamt Exp $");
35 #endif /* LIBC_SCCS and not lint */
36
37 #define _NLS_PRIVATE
38
39 #include "namespace.h"
40 #include <sys/param.h>
41 #include <sys/stat.h>
42 #include <sys/mman.h>
43
44 #include <assert.h>
45 #include <fcntl.h>
46 #include <limits.h>
47 #include <locale.h>
48 #include <nl_types.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52
53 #include "citrus_namespace.h"
54 #include "citrus_bcs.h"
55 #include "citrus_region.h"
56 #include "citrus_lookup.h"
57 #include "citrus_aliasname_local.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(const char *);
69
70 nl_catd
71 _catopen(const char *name, int oflag)
72 {
73 char tmppath[PATH_MAX+1];
74 const char *nlspath;
75 const char *lang, *reallang;
76 char *t;
77 const char *s, *u;
78 nl_catd catd;
79 char langbuf[PATH_MAX];
80
81 if (name == NULL || *name == '\0')
82 return (nl_catd)-1;
83
84 /* absolute or relative path? */
85 if (strchr(name, '/'))
86 return load_msgcat(name);
87
88 if (issetugid() || (nlspath = getenv("NLSPATH")) == NULL)
89 nlspath = NLS_DEFAULT_PATH;
90 if (oflag == NL_CAT_LOCALE) {
91 lang = setlocale(LC_MESSAGES, NULL);
92 }
93 else {
94 lang = getenv("LANG");
95 }
96 if (lang == NULL || strchr(lang, '/'))
97 lang = NLS_DEFAULT_LANG;
98
99 reallang = __unaliasname(NLS_ALIAS_DB, lang, langbuf, sizeof(langbuf));
100 if (reallang == NULL)
101 reallang = lang;
102
103 s = nlspath;
104 t = tmppath;
105 do {
106 while (*s && *s != ':') {
107 if (*s == '%') {
108 switch (*(++s)) {
109 case 'L': /* locale */
110 u = reallang;
111 while (*u && t < tmppath + PATH_MAX)
112 *t++ = *u++;
113 break;
114 case 'N': /* name */
115 u = name;
116 while (*u && t < tmppath + PATH_MAX)
117 *t++ = *u++;
118 break;
119 case 'l': /* lang */
120 case 't': /* territory */
121 case 'c': /* codeset */
122 break;
123 default:
124 if (t < tmppath + PATH_MAX)
125 *t++ = *s;
126 }
127 } else {
128 if (t < tmppath + PATH_MAX)
129 *t++ = *s;
130 }
131 s++;
132 }
133
134 *t = '\0';
135 catd = load_msgcat(tmppath);
136 if (catd != (nl_catd)-1)
137 return catd;
138
139 if (*s)
140 s++;
141 t = tmppath;
142 } while (*s);
143
144 return (nl_catd)-1;
145 }
146
147 static nl_catd
148 load_msgcat(const char *path)
149 {
150 struct stat st;
151 nl_catd catd;
152 void *data;
153 int fd;
154
155 _DIAGASSERT(path != NULL);
156
157 if ((fd = open(path, O_RDONLY)) == -1)
158 return (nl_catd)-1;
159
160 if (fstat(fd, &st) != 0) {
161 close (fd);
162 return (nl_catd)-1;
163 }
164
165 data = mmap(0, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd,
166 (off_t)0);
167 close (fd);
168
169 if (data == MAP_FAILED) {
170 return (nl_catd)-1;
171 }
172
173 if (ntohl((u_int32_t)((struct _nls_cat_hdr *)data)->__magic) !=
174 _NLS_MAGIC) {
175 munmap(data, (size_t)st.st_size);
176 return (nl_catd)-1;
177 }
178
179 if ((catd = malloc(sizeof (*catd))) == NULL) {
180 munmap(data, (size_t)st.st_size);
181 return (nl_catd)-1;
182 }
183
184 catd->__data = data;
185 catd->__size = (int)st.st_size;
186 return catd;
187 }
188