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