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