Home | History | Annotate | Line # | Download | only in nls
catopen.c revision 1.16
      1 /*	$NetBSD: catopen.c,v 1.16 1999/09/16 11:45:19 lukem 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 <nl_types.h>
     50 #include <stdlib.h>
     51 #include <string.h>
     52 #include <unistd.h>
     53 
     54 #define NLS_DEFAULT_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L"
     55 #define NLS_DEFAULT_LANG "C"
     56 
     57 #ifdef __weak_alias
     58 __weak_alias(catopen, _catopen)
     59 #endif
     60 
     61 static nl_catd load_msgcat __P((const char *));
     62 
     63 /* ARGSUSED */
     64 nl_catd
     65 _catopen(name, oflag)
     66 	const char *name;
     67 	int oflag;
     68 {
     69 	char tmppath[PATH_MAX];
     70 	char *nlspath;
     71 	char *lang;
     72 	char *s, *t;
     73 	const char *u;
     74 	nl_catd catd;
     75 
     76 	if (name == NULL || *name == '\0')
     77 		return (nl_catd)-1;
     78 
     79 	/* absolute or relative path? */
     80 	if (strchr(name, '/'))
     81 		return load_msgcat(name);
     82 
     83 	/*
     84 	 * XXX potential security problem here if this is used in a
     85 	 * set-id program, and NLSPATH or LANG are set to read files
     86 	 * the user normally does not have access to.
     87 	 */
     88 	if ((nlspath = getenv("NLSPATH")) == NULL)
     89 		nlspath = NLS_DEFAULT_PATH;
     90 	if ((lang = getenv("LANG")) == NULL)
     91 		lang = NLS_DEFAULT_LANG;
     92 
     93 	s = nlspath;
     94 	t = tmppath;
     95 	do {
     96 		while (*s && *s != ':') {
     97 			if (*s == '%') {
     98 				switch (*(++s)) {
     99 				case 'L':	/* locale */
    100 					u = lang;
    101 					while (*u && t < tmppath + PATH_MAX)
    102 						*t++ = *u++;
    103 					break;
    104 				case 'N':	/* name */
    105 					u = name;
    106 					while (*u && t < tmppath + PATH_MAX)
    107 						*t++ = *u++;
    108 					break;
    109 				case 'l':	/* lang */
    110 				case 't':	/* territory */
    111 				case 'c':	/* codeset */
    112 					break;
    113 				default:
    114 					if (t < tmppath + PATH_MAX)
    115 						*t++ = *s;
    116 				}
    117 			} else {
    118 				if (t < tmppath + PATH_MAX)
    119 					*t++ = *s;
    120 			}
    121 			s++;
    122 		}
    123 
    124 		*t = '\0';
    125 		catd = load_msgcat(tmppath);
    126 		if (catd != (nl_catd)-1)
    127 			return catd;
    128 
    129 		if (*s)
    130 			s++;
    131 		t = tmppath;
    132 	} while (*s);
    133 
    134 	return (nl_catd)-1;
    135 }
    136 
    137 static nl_catd
    138 load_msgcat(path)
    139 	const char *path;
    140 {
    141 	struct stat st;
    142 	nl_catd catd;
    143 	void *data;
    144 	int fd;
    145 
    146 	_DIAGASSERT(path != NULL);
    147 
    148 	if ((fd = open(path, O_RDONLY)) == -1)
    149 		return (nl_catd)-1;
    150 
    151 	if (fstat(fd, &st) != 0) {
    152 		close (fd);
    153 		return (nl_catd)-1;
    154 	}
    155 
    156 	data = mmap(0, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd,
    157 	    (off_t)0);
    158 	close (fd);
    159 
    160 	if (data == (void *)-1) {
    161 		munmap(data, (size_t)st.st_size);
    162 		return (nl_catd)-1;
    163 	}
    164 
    165 	if (ntohl((u_int32_t)((struct _nls_cat_hdr *)data)->__magic) !=
    166 	    _NLS_MAGIC) {
    167 		munmap(data, (size_t)st.st_size);
    168 		return (nl_catd)-1;
    169 	}
    170 
    171 	if ((catd = malloc(sizeof (*catd))) == 0) {
    172 		munmap(data, (size_t)st.st_size);
    173 		return (nl_catd)-1;
    174 	}
    175 
    176 	catd->__data = data;
    177 	catd->__size = (int)st.st_size;
    178 	return catd;
    179 }
    180