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