Home | History | Annotate | Line # | Download | only in nls
catopen.c revision 1.15
      1  1.15   mycroft /*	$NetBSD: catopen.c,v 1.15 1999/08/17 04:00:51 mycroft Exp $	*/
      2   1.4       cgd 
      3   1.6       jtc /*-
      4   1.6       jtc  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5   1.6       jtc  * All rights reserved.
      6   1.6       jtc  *
      7   1.6       jtc  * This code is derived from software contributed to The NetBSD Foundation
      8   1.6       jtc  * by J.T. Conklin.
      9   1.6       jtc  *
     10   1.6       jtc  * Redistribution and use in source and binary forms, with or without
     11   1.6       jtc  * modification, are permitted provided that the following conditions
     12   1.6       jtc  * are met:
     13   1.6       jtc  * 1. Redistributions of source code must retain the above copyright
     14   1.6       jtc  *    notice, this list of conditions and the following disclaimer.
     15   1.6       jtc  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.6       jtc  *    notice, this list of conditions and the following disclaimer in the
     17   1.6       jtc  *    documentation and/or other materials provided with the distribution.
     18   1.6       jtc  * 3. All advertising materials mentioning features or use of this software
     19   1.6       jtc  *    must display the following acknowledgement:
     20   1.6       jtc  *        This product includes software developed by the NetBSD
     21   1.6       jtc  *        Foundation, Inc. and its contributors.
     22   1.6       jtc  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.6       jtc  *    contributors may be used to endorse or promote products derived
     24   1.6       jtc  *    from this software without specific prior written permission.
     25   1.6       jtc  *
     26   1.6       jtc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.6       jtc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.6       jtc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.12       jtc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.12       jtc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.6       jtc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.6       jtc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.6       jtc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.6       jtc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.6       jtc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.6       jtc  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1       jtc  */
     38   1.1       jtc 
     39   1.6       jtc #define _NLS_PRIVATE
     40   1.1       jtc 
     41  1.11  christos #include "namespace.h"
     42   1.6       jtc #include <limits.h>
     43   1.6       jtc #include <stdlib.h>
     44   1.6       jtc #include <string.h>
     45   1.6       jtc #include <sys/types.h>
     46   1.6       jtc #include <sys/param.h>
     47   1.6       jtc #include <sys/stat.h>
     48   1.6       jtc #include <sys/mman.h>
     49   1.6       jtc #include <unistd.h>
     50   1.6       jtc #include <fcntl.h>
     51   1.1       jtc #include <nl_types.h>
     52   1.1       jtc 
     53   1.6       jtc #define NLS_DEFAULT_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L"
     54   1.6       jtc #define NLS_DEFAULT_LANG "C"
     55  1.15   mycroft 
     56  1.15   mycroft #ifdef __weak_alias
     57  1.15   mycroft __weak_alias(catopen, _catopen)
     58  1.15   mycroft #endif
     59   1.1       jtc 
     60  1.11  christos static nl_catd load_msgcat __P((const char *));
     61   1.7       jtc 
     62  1.14  christos /* ARGSUSED */
     63   1.1       jtc nl_catd
     64   1.6       jtc _catopen(name, oflag)
     65   1.6       jtc 	const char *name;
     66   1.1       jtc 	int oflag;
     67   1.1       jtc {
     68   1.7       jtc 	char tmppath[PATH_MAX];
     69   1.7       jtc 	char *nlspath;
     70   1.7       jtc 	char *lang;
     71   1.7       jtc 	char *s, *t;
     72   1.8       jtc 	const char *u;
     73   1.6       jtc 	nl_catd catd;
     74   1.7       jtc 
     75   1.6       jtc 	if (name == NULL || *name == '\0')
     76  1.10       mrg 		return (nl_catd)-1;
     77   1.6       jtc 
     78   1.6       jtc 	/* absolute or relative path? */
     79  1.10       mrg 	if (strchr(name, '/'))
     80   1.7       jtc 		return load_msgcat(name);
     81   1.7       jtc 
     82  1.10       mrg 	/*
     83  1.10       mrg 	 * XXX potential security problem here if this is used in a
     84  1.10       mrg 	 * set-id program, and NLSPATH or LANG are set to read files
     85  1.10       mrg 	 * the user normally does not have access to.
     86  1.10       mrg 	 */
     87  1.10       mrg 	if ((nlspath = getenv("NLSPATH")) == NULL)
     88   1.7       jtc 		nlspath = NLS_DEFAULT_PATH;
     89  1.10       mrg 	if ((lang = getenv("LANG")) == NULL)
     90   1.7       jtc 		lang = NLS_DEFAULT_LANG;
     91   1.6       jtc 
     92   1.7       jtc 	s = nlspath;
     93   1.7       jtc 	t = tmppath;
     94   1.7       jtc 	do {
     95   1.7       jtc 		while (*s && *s != ':') {
     96   1.6       jtc 			if (*s == '%') {
     97   1.8       jtc 				switch (*(++s)) {
     98   1.7       jtc 				case 'L':	/* locale */
     99   1.8       jtc 					u = lang;
    100   1.8       jtc 					while (*u && t < tmppath + PATH_MAX)
    101   1.8       jtc 						*t++ = *u++;
    102   1.7       jtc 					break;
    103   1.7       jtc 				case 'N':	/* name */
    104   1.8       jtc 					u = name;
    105   1.8       jtc 					while (*u && t < tmppath + PATH_MAX)
    106   1.8       jtc 						*t++ = *u++;
    107   1.7       jtc 					break;
    108   1.7       jtc 				case 'l':	/* lang */
    109   1.7       jtc 				case 't':	/* territory */
    110   1.7       jtc 				case 'c':	/* codeset */
    111   1.7       jtc 					break;
    112   1.7       jtc 				default:
    113   1.8       jtc 					if (t < tmppath + PATH_MAX)
    114   1.9       jtc 						*t++ = *s;
    115   1.6       jtc 				}
    116   1.6       jtc 			} else {
    117   1.8       jtc 				if (t < tmppath + PATH_MAX)
    118   1.9       jtc 					*t++ = *s;
    119   1.6       jtc 			}
    120   1.9       jtc 			s++;
    121   1.6       jtc 		}
    122   1.6       jtc 
    123   1.7       jtc 		*t = '\0';
    124   1.7       jtc 		catd = load_msgcat(tmppath);
    125  1.10       mrg 		if (catd != (nl_catd)-1)
    126   1.7       jtc 			return catd;
    127   1.7       jtc 
    128   1.9       jtc 		if (*s)
    129   1.9       jtc 			s++;
    130   1.7       jtc 		t = tmppath;
    131   1.7       jtc 	} while (*s);
    132   1.7       jtc 
    133  1.10       mrg 	return (nl_catd)-1;
    134   1.7       jtc }
    135   1.7       jtc 
    136   1.7       jtc static nl_catd
    137   1.7       jtc load_msgcat(path)
    138   1.7       jtc 	const char *path;
    139   1.7       jtc {
    140   1.7       jtc 	struct stat st;
    141   1.7       jtc 	nl_catd catd;
    142   1.9       jtc 	void *data;
    143   1.7       jtc 	int fd;
    144   1.6       jtc 
    145  1.10       mrg 	if ((fd = open(path, O_RDONLY)) == -1)
    146  1.10       mrg 		return (nl_catd)-1;
    147   1.6       jtc 
    148   1.6       jtc 	if (fstat(fd, &st) != 0) {
    149   1.6       jtc 		close (fd);
    150  1.10       mrg 		return (nl_catd)-1;
    151   1.6       jtc 	}
    152   1.6       jtc 
    153  1.13   mycroft 	data = mmap(0, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd,
    154  1.14  christos 	    (off_t)0);
    155   1.9       jtc 	close (fd);
    156   1.9       jtc 
    157  1.10       mrg 	if (data == (void *)-1) {
    158  1.10       mrg 		munmap(data, (size_t)st.st_size);
    159  1.10       mrg 		return (nl_catd)-1;
    160   1.6       jtc 	}
    161   1.6       jtc 
    162  1.14  christos 	if (ntohl((u_int32_t)((struct _nls_cat_hdr *)data)->__magic) !=
    163  1.14  christos 	    _NLS_MAGIC) {
    164  1.10       mrg 		munmap(data, (size_t)st.st_size);
    165  1.10       mrg 		return (nl_catd)-1;
    166   1.6       jtc 	}
    167   1.6       jtc 
    168  1.10       mrg 	if ((catd = malloc(sizeof (*catd))) == 0) {
    169  1.10       mrg 		munmap(data, (size_t)st.st_size);
    170  1.10       mrg 		return (nl_catd)-1;
    171   1.6       jtc 	}
    172   1.6       jtc 
    173   1.9       jtc 	catd->__data = data;
    174  1.14  christos 	catd->__size = (int)st.st_size;
    175   1.6       jtc 	return catd;
    176   1.1       jtc }
    177