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