xref: /netbsd-src/lib/libc/nls/catopen.c (revision 44751e4dbf973d10d9aaba59f5e655ff00d4c9a8)
1*44751e4dSchristos /*	$NetBSD: catopen.c,v 1.33 2014/09/16 01:30:28 christos Exp $	*/
27ce7ea94Scgd 
3adfd5ba7Sjtc /*-
4adfd5ba7Sjtc  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5adfd5ba7Sjtc  * All rights reserved.
6adfd5ba7Sjtc  *
7adfd5ba7Sjtc  * This code is derived from software contributed to The NetBSD Foundation
8adfd5ba7Sjtc  * by J.T. Conklin.
9adfd5ba7Sjtc  *
10adfd5ba7Sjtc  * Redistribution and use in source and binary forms, with or without
11adfd5ba7Sjtc  * modification, are permitted provided that the following conditions
12adfd5ba7Sjtc  * are met:
13adfd5ba7Sjtc  * 1. Redistributions of source code must retain the above copyright
14adfd5ba7Sjtc  *    notice, this list of conditions and the following disclaimer.
15adfd5ba7Sjtc  * 2. Redistributions in binary form must reproduce the above copyright
16adfd5ba7Sjtc  *    notice, this list of conditions and the following disclaimer in the
17adfd5ba7Sjtc  *    documentation and/or other materials provided with the distribution.
18adfd5ba7Sjtc  *
19adfd5ba7Sjtc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20adfd5ba7Sjtc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21adfd5ba7Sjtc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2287f4ccd4Sjtc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2387f4ccd4Sjtc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24adfd5ba7Sjtc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25adfd5ba7Sjtc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26adfd5ba7Sjtc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27adfd5ba7Sjtc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28adfd5ba7Sjtc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29adfd5ba7Sjtc  * POSSIBILITY OF SUCH DAMAGE.
3056fa6d53Sjtc  */
3156fa6d53Sjtc 
3288c3eadbSlukem #include <sys/cdefs.h>
33*44751e4dSchristos __RCSID("$NetBSD: catopen.c,v 1.33 2014/09/16 01:30:28 christos Exp $");
3488c3eadbSlukem 
35adfd5ba7Sjtc #define _NLS_PRIVATE
369c11b124Sjoerg #define __SETLOCALE_SOURCE__
3756fa6d53Sjtc 
384515f6e8Schristos #include "namespace.h"
39adfd5ba7Sjtc #include <sys/param.h>
40adfd5ba7Sjtc #include <sys/stat.h>
41adfd5ba7Sjtc #include <sys/mman.h>
42b48252f3Slukem 
43b48252f3Slukem #include <assert.h>
44adfd5ba7Sjtc #include <fcntl.h>
45b48252f3Slukem #include <limits.h>
462dda3534Syamt #include <locale.h>
4756fa6d53Sjtc #include <nl_types.h>
48b48252f3Slukem #include <stdlib.h>
49b48252f3Slukem #include <string.h>
50b48252f3Slukem #include <unistd.h>
5156fa6d53Sjtc 
5252ed7b03Stnozaki #include "citrus_namespace.h"
5352ed7b03Stnozaki #include "citrus_bcs.h"
5452ed7b03Stnozaki #include "citrus_region.h"
5552ed7b03Stnozaki #include "citrus_lookup.h"
5652ed7b03Stnozaki #include "citrus_aliasname_local.h"
579c11b124Sjoerg #include "setlocale_local.h"
588ad80618Stshiozak 
592dda3534Syamt #define NLS_ALIAS_DB "/usr/share/nls/nls.alias"
602dda3534Syamt 
61adfd5ba7Sjtc #define NLS_DEFAULT_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L"
62adfd5ba7Sjtc #define NLS_DEFAULT_LANG "C"
6356fa6d53Sjtc 
649f5f4ac6Smycroft __weak_alias(catopen, _catopen)
659c11b124Sjoerg __weak_alias(catopen_l, _catopen_l)
669f5f4ac6Smycroft 
67504f8671Smatt static nl_catd load_msgcat(const char *);
68c40394d1Sjtc 
6956fa6d53Sjtc nl_catd
catopen(const char * name,int oflag)709c11b124Sjoerg catopen(const char *name, int oflag)
719c11b124Sjoerg {
729c11b124Sjoerg 
739c11b124Sjoerg 	return catopen_l(name, oflag, _current_locale());
749c11b124Sjoerg }
759c11b124Sjoerg 
769c11b124Sjoerg nl_catd
catopen_l(const char * name,int oflag,locale_t loc)779c11b124Sjoerg catopen_l(const char *name, int oflag, locale_t loc)
7856fa6d53Sjtc {
79a23abe20Ssommerfeld 	char tmppath[PATH_MAX+1];
8003256c6eSchristos 	const char *nlspath;
8152ed7b03Stnozaki 	const char *lang, *reallang;
8203256c6eSchristos 	char *t;
8303256c6eSchristos 	const char *s, *u;
84adfd5ba7Sjtc 	nl_catd catd;
852dda3534Syamt 	char langbuf[PATH_MAX];
86adfd5ba7Sjtc 
87adfd5ba7Sjtc 	if (name == NULL || *name == '\0')
88adfd5ba7Sjtc 		return (nl_catd)-1;
89adfd5ba7Sjtc 
90adfd5ba7Sjtc 	/* absolute or relative path? */
91c40394d1Sjtc 	if (strchr(name, '/'))
92c40394d1Sjtc 		return load_msgcat(name);
93adfd5ba7Sjtc 
94fcc188c1Stshiozak 	if (issetugid() || (nlspath = getenv("NLSPATH")) == NULL)
95adfd5ba7Sjtc 		nlspath = NLS_DEFAULT_PATH;
96a143089bSyamt 	/*
979c11b124Sjoerg 	 * Historical note:
98a143089bSyamt 	 * http://www.hauN.org/ml/b-l-j/a/800/828.html (in japanese)
99a143089bSyamt 	 */
1002dda3534Syamt 	if (oflag == NL_CAT_LOCALE) {
1019c11b124Sjoerg 		lang = loc->part_name[LC_MESSAGES];
102a143089bSyamt 	} else {
1032dda3534Syamt 		lang = getenv("LANG");
1042dda3534Syamt 	}
1052dda3534Syamt 	if (lang == NULL || strchr(lang, '/'))
106adfd5ba7Sjtc 		lang = NLS_DEFAULT_LANG;
10756fa6d53Sjtc 
10852ed7b03Stnozaki 	reallang = __unaliasname(NLS_ALIAS_DB, lang, langbuf, sizeof(langbuf));
10952ed7b03Stnozaki 	if (reallang == NULL)
11052ed7b03Stnozaki 		reallang = lang;
1112dda3534Syamt 
112c40394d1Sjtc 	s = nlspath;
113c40394d1Sjtc 	t = tmppath;
114c40394d1Sjtc 	do {
115c40394d1Sjtc 		while (*s && *s != ':') {
116adfd5ba7Sjtc 			if (*s == '%') {
117749fcbc5Sjtc 				switch (*(++s)) {
118c40394d1Sjtc 				case 'L':	/* locale */
11952ed7b03Stnozaki 					u = reallang;
120749fcbc5Sjtc 					while (*u && t < tmppath + PATH_MAX)
121749fcbc5Sjtc 						*t++ = *u++;
122c40394d1Sjtc 					break;
123c40394d1Sjtc 				case 'N':	/* name */
124749fcbc5Sjtc 					u = name;
125749fcbc5Sjtc 					while (*u && t < tmppath + PATH_MAX)
126749fcbc5Sjtc 						*t++ = *u++;
127c40394d1Sjtc 					break;
128c40394d1Sjtc 				case 'l':	/* lang */
129c40394d1Sjtc 				case 't':	/* territory */
130c40394d1Sjtc 				case 'c':	/* codeset */
131c40394d1Sjtc 					break;
132c40394d1Sjtc 				default:
133749fcbc5Sjtc 					if (t < tmppath + PATH_MAX)
1343a375fb2Sjtc 						*t++ = *s;
135c40394d1Sjtc 				}
136adfd5ba7Sjtc 			} else {
137749fcbc5Sjtc 				if (t < tmppath + PATH_MAX)
1383a375fb2Sjtc 					*t++ = *s;
139adfd5ba7Sjtc 			}
1403a375fb2Sjtc 			s++;
141c40394d1Sjtc 		}
142c40394d1Sjtc 
143adfd5ba7Sjtc 		*t = '\0';
144c40394d1Sjtc 		catd = load_msgcat(tmppath);
1453a375fb2Sjtc 		if (catd != (nl_catd)-1)
146c40394d1Sjtc 			return catd;
147adfd5ba7Sjtc 
1483a375fb2Sjtc 		if (*s)
149c40394d1Sjtc 			s++;
150adfd5ba7Sjtc 		t = tmppath;
151c40394d1Sjtc 	} while (*s);
152adfd5ba7Sjtc 
1533a375fb2Sjtc 	return (nl_catd)-1;
154adfd5ba7Sjtc }
155adfd5ba7Sjtc 
156c40394d1Sjtc static nl_catd
load_msgcat(const char * path)157504f8671Smatt load_msgcat(const char *path)
158c40394d1Sjtc {
159c40394d1Sjtc 	struct stat st;
160c40394d1Sjtc 	nl_catd catd;
1613a375fb2Sjtc 	void *data;
162c40394d1Sjtc 	int fd;
163c40394d1Sjtc 
164b48252f3Slukem 	_DIAGASSERT(path != NULL);
165b48252f3Slukem 
166*44751e4dSchristos 	if ((fd = open(path, O_RDONLY|O_CLOEXEC)) == -1)
1673a375fb2Sjtc 		return (nl_catd)-1;
168adfd5ba7Sjtc 
169adfd5ba7Sjtc 	if (fstat(fd, &st) != 0) {
170adfd5ba7Sjtc 		close (fd);
1713a375fb2Sjtc 		return (nl_catd)-1;
1723a375fb2Sjtc 	}
1733a375fb2Sjtc 
1744462053aSmycroft 	data = mmap(0, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd,
17538676913Schristos 	    (off_t)0);
1763a375fb2Sjtc 	close (fd);
1773a375fb2Sjtc 
1789d4bdfc6Sjoerg 	if (data == MAP_FAILED) {
1793a375fb2Sjtc 		return (nl_catd)-1;
1803a375fb2Sjtc 	}
1813a375fb2Sjtc 
18238676913Schristos 	if (ntohl((u_int32_t)((struct _nls_cat_hdr *)data)->__magic) !=
18338676913Schristos 	    _NLS_MAGIC) {
1843a375fb2Sjtc 		munmap(data, (size_t)st.st_size);
1853a375fb2Sjtc 		return (nl_catd)-1;
186adfd5ba7Sjtc 	}
187adfd5ba7Sjtc 
1889d4bdfc6Sjoerg 	if ((catd = malloc(sizeof (*catd))) == NULL) {
1893a375fb2Sjtc 		munmap(data, (size_t)st.st_size);
1903a375fb2Sjtc 		return (nl_catd)-1;
191adfd5ba7Sjtc 	}
192adfd5ba7Sjtc 
1933a375fb2Sjtc 	catd->__data = data;
19438676913Schristos 	catd->__size = (int)st.st_size;
195adfd5ba7Sjtc 	return catd;
196adfd5ba7Sjtc }
197