Home | History | Annotate | Line # | Download | only in nls
catopen.c revision 1.23
      1 /*	$NetBSD: catopen.c,v 1.23 2005/11/29 03:12:00 christos 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 #include <sys/cdefs.h>
     40 #if defined(LIBC_SCCS) && !defined(lint)
     41 __RCSID("$NetBSD: catopen.c,v 1.23 2005/11/29 03:12:00 christos Exp $");
     42 #endif /* LIBC_SCCS and not lint */
     43 
     44 #define _NLS_PRIVATE
     45 
     46 #include "namespace.h"
     47 #include <sys/param.h>
     48 #include <sys/stat.h>
     49 #include <sys/mman.h>
     50 
     51 #include <assert.h>
     52 #include <fcntl.h>
     53 #include <limits.h>
     54 #include <locale.h>
     55 #include <nl_types.h>
     56 #include <stdlib.h>
     57 #include <string.h>
     58 #include <unistd.h>
     59 
     60 #ifdef CITRUS
     61 #include <citrus/citrus_namespace.h>
     62 #include <citrus/citrus_region.h>
     63 #include <citrus/citrus_lookup.h>
     64 #else
     65 #include <locale/aliasname_local.h>
     66 #define _lookup_alias(p, a, b, s, c)	__unaliasname((p), (a), (b), (s))
     67 #endif
     68 
     69 #define NLS_ALIAS_DB "/usr/share/nls/nls.alias"
     70 
     71 #define NLS_DEFAULT_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L"
     72 #define NLS_DEFAULT_LANG "C"
     73 
     74 #ifdef __weak_alias
     75 __weak_alias(catopen, _catopen)
     76 #endif
     77 
     78 static nl_catd load_msgcat __P((const char *));
     79 
     80 nl_catd
     81 _catopen(name, oflag)
     82 	const char *name;
     83 	int oflag;
     84 {
     85 	char tmppath[PATH_MAX+1];
     86 	const char *nlspath;
     87 	const char *lang;
     88 	char *t;
     89 	const char *s, *u;
     90 	nl_catd catd;
     91 	char langbuf[PATH_MAX];
     92 
     93 	if (name == NULL || *name == '\0')
     94 		return (nl_catd)-1;
     95 
     96 	/* absolute or relative path? */
     97 	if (strchr(name, '/'))
     98 		return load_msgcat(name);
     99 
    100 	if (issetugid() || (nlspath = getenv("NLSPATH")) == NULL)
    101 		nlspath = NLS_DEFAULT_PATH;
    102 	if (oflag == NL_CAT_LOCALE) {
    103 		lang = setlocale(LC_MESSAGES, NULL);
    104 	}
    105 	else {
    106 		lang = getenv("LANG");
    107 	}
    108 	if (lang == NULL || strchr(lang, '/'))
    109 		lang = NLS_DEFAULT_LANG;
    110 
    111 	lang = _lookup_alias(NLS_ALIAS_DB, lang, langbuf, sizeof(langbuf),
    112 			     _LOOKUP_CASE_SENSITIVE);
    113 
    114 	s = nlspath;
    115 	t = tmppath;
    116 	do {
    117 		while (*s && *s != ':') {
    118 			if (*s == '%') {
    119 				switch (*(++s)) {
    120 				case 'L':	/* locale */
    121 					u = lang;
    122 					while (*u && t < tmppath + PATH_MAX)
    123 						*t++ = *u++;
    124 					break;
    125 				case 'N':	/* name */
    126 					u = name;
    127 					while (*u && t < tmppath + PATH_MAX)
    128 						*t++ = *u++;
    129 					break;
    130 				case 'l':	/* lang */
    131 				case 't':	/* territory */
    132 				case 'c':	/* codeset */
    133 					break;
    134 				default:
    135 					if (t < tmppath + PATH_MAX)
    136 						*t++ = *s;
    137 				}
    138 			} else {
    139 				if (t < tmppath + PATH_MAX)
    140 					*t++ = *s;
    141 			}
    142 			s++;
    143 		}
    144 
    145 		*t = '\0';
    146 		catd = load_msgcat(tmppath);
    147 		if (catd != (nl_catd)-1)
    148 			return catd;
    149 
    150 		if (*s)
    151 			s++;
    152 		t = tmppath;
    153 	} while (*s);
    154 
    155 	return (nl_catd)-1;
    156 }
    157 
    158 static nl_catd
    159 load_msgcat(path)
    160 	const char *path;
    161 {
    162 	struct stat st;
    163 	nl_catd catd;
    164 	void *data;
    165 	int fd;
    166 
    167 	_DIAGASSERT(path != NULL);
    168 
    169 	if ((fd = open(path, O_RDONLY)) == -1)
    170 		return (nl_catd)-1;
    171 
    172 	if (fstat(fd, &st) != 0) {
    173 		close (fd);
    174 		return (nl_catd)-1;
    175 	}
    176 
    177 	data = mmap(0, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd,
    178 	    (off_t)0);
    179 	close (fd);
    180 
    181 	if (data == (void *)-1) {
    182 		munmap(data, (size_t)st.st_size);
    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))) == 0) {
    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