xref: /minix3/lib/libc/nls/catopen.c (revision c9ea9e7af84fcba485b32ccd2c85edb945b1f323)
1 /*	$NetBSD: catopen.c,v 1.32 2013/08/19 08:03:34 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 __RCSID("$NetBSD: catopen.c,v 1.32 2013/08/19 08:03:34 joerg Exp $");
34 
35 #define _NLS_PRIVATE
36 #define __SETLOCALE_SOURCE__
37 
38 #include "namespace.h"
39 #include <sys/param.h>
40 #include <sys/stat.h>
41 #include <sys/mman.h>
42 
43 #include <assert.h>
44 #include <fcntl.h>
45 #include <limits.h>
46 #include <locale.h>
47 #include <nl_types.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 
52 #include "citrus_namespace.h"
53 #include "citrus_bcs.h"
54 #include "citrus_region.h"
55 #include "citrus_lookup.h"
56 #include "citrus_aliasname_local.h"
57 #include "setlocale_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 #if defined(__minix) && !defined(MAP_SHARED)
65 #define MAP_SHARED MAP_PRIVATE /* LSC: We lose some memory, but it's OK as this is RO. */
66 #endif /* defined(__minix) && !defined(MAP_SHARED) */
67 
68 __weak_alias(catopen, _catopen)
69 __weak_alias(catopen_l, _catopen_l)
70 
71 static nl_catd load_msgcat(const char *);
72 
73 nl_catd
74 catopen(const char *name, int oflag)
75 {
76 
77 	return catopen_l(name, oflag, _current_locale());
78 }
79 
80 nl_catd
81 catopen_l(const char *name, int oflag, locale_t loc)
82 {
83 	char tmppath[PATH_MAX+1];
84 	const char *nlspath;
85 	const char *lang, *reallang;
86 	char *t;
87 	const char *s, *u;
88 	nl_catd catd;
89 	char langbuf[PATH_MAX];
90 
91 	if (name == NULL || *name == '\0')
92 		return (nl_catd)-1;
93 
94 	/* absolute or relative path? */
95 	if (strchr(name, '/'))
96 		return load_msgcat(name);
97 
98 	if (issetugid() || (nlspath = getenv("NLSPATH")) == NULL)
99 		nlspath = NLS_DEFAULT_PATH;
100 	/*
101 	 * Historical note:
102 	 * http://www.hauN.org/ml/b-l-j/a/800/828.html (in japanese)
103 	 */
104 	if (oflag == NL_CAT_LOCALE) {
105 		lang = loc->part_name[LC_MESSAGES];
106 	} else {
107 		lang = getenv("LANG");
108 	}
109 	if (lang == NULL || strchr(lang, '/'))
110 		lang = NLS_DEFAULT_LANG;
111 
112 	reallang = __unaliasname(NLS_ALIAS_DB, lang, langbuf, sizeof(langbuf));
113 	if (reallang == NULL)
114 		reallang = lang;
115 
116 	s = nlspath;
117 	t = tmppath;
118 	do {
119 		while (*s && *s != ':') {
120 			if (*s == '%') {
121 				switch (*(++s)) {
122 				case 'L':	/* locale */
123 					u = reallang;
124 					while (*u && t < tmppath + PATH_MAX)
125 						*t++ = *u++;
126 					break;
127 				case 'N':	/* name */
128 					u = name;
129 					while (*u && t < tmppath + PATH_MAX)
130 						*t++ = *u++;
131 					break;
132 				case 'l':	/* lang */
133 				case 't':	/* territory */
134 				case 'c':	/* codeset */
135 					break;
136 				default:
137 					if (t < tmppath + PATH_MAX)
138 						*t++ = *s;
139 				}
140 			} else {
141 				if (t < tmppath + PATH_MAX)
142 					*t++ = *s;
143 			}
144 			s++;
145 		}
146 
147 		*t = '\0';
148 		catd = load_msgcat(tmppath);
149 		if (catd != (nl_catd)-1)
150 			return catd;
151 
152 		if (*s)
153 			s++;
154 		t = tmppath;
155 	} while (*s);
156 
157 	return (nl_catd)-1;
158 }
159 
160 static nl_catd
161 load_msgcat(const char *path)
162 {
163 	struct stat st;
164 	nl_catd catd;
165 	void *data;
166 	int fd;
167 
168 	_DIAGASSERT(path != NULL);
169 
170 	if ((fd = open(path, O_RDONLY)) == -1)
171 		return (nl_catd)-1;
172 
173 	if (fstat(fd, &st) != 0) {
174 		close (fd);
175 		return (nl_catd)-1;
176 	}
177 
178 	data = mmap(0, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd,
179 	    (off_t)0);
180 	close (fd);
181 
182 	if (data == MAP_FAILED) {
183 		return (nl_catd)-1;
184 	}
185 
186 	if (ntohl((u_int32_t)((struct _nls_cat_hdr *)data)->__magic) !=
187 	    _NLS_MAGIC) {
188 		munmap(data, (size_t)st.st_size);
189 		return (nl_catd)-1;
190 	}
191 
192 	if ((catd = malloc(sizeof (*catd))) == NULL) {
193 		munmap(data, (size_t)st.st_size);
194 		return (nl_catd)-1;
195 	}
196 
197 	catd->__data = data;
198 	catd->__size = (int)st.st_size;
199 	return catd;
200 }
201