Home | History | Annotate | Line # | Download | only in nls
catopen.c revision 1.28.6.1
      1  1.28.6.1      yamt /*	$NetBSD: catopen.c,v 1.28.6.1 2012/04/17 00:05:22 yamt 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  *
     19       1.6       jtc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.6       jtc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.6       jtc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.12       jtc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.12       jtc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.6       jtc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.6       jtc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.6       jtc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.6       jtc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.6       jtc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.6       jtc  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1       jtc  */
     31       1.1       jtc 
     32      1.22     lukem #include <sys/cdefs.h>
     33      1.22     lukem #if defined(LIBC_SCCS) && !defined(lint)
     34  1.28.6.1      yamt __RCSID("$NetBSD: catopen.c,v 1.28.6.1 2012/04/17 00:05:22 yamt Exp $");
     35      1.22     lukem #endif /* LIBC_SCCS and not lint */
     36      1.22     lukem 
     37       1.6       jtc #define _NLS_PRIVATE
     38       1.1       jtc 
     39      1.11  christos #include "namespace.h"
     40       1.6       jtc #include <sys/param.h>
     41       1.6       jtc #include <sys/stat.h>
     42       1.6       jtc #include <sys/mman.h>
     43      1.16     lukem 
     44      1.16     lukem #include <assert.h>
     45       1.6       jtc #include <fcntl.h>
     46      1.16     lukem #include <limits.h>
     47      1.19      yamt #include <locale.h>
     48       1.1       jtc #include <nl_types.h>
     49      1.16     lukem #include <stdlib.h>
     50      1.16     lukem #include <string.h>
     51      1.16     lukem #include <unistd.h>
     52       1.1       jtc 
     53      1.26   tnozaki #include "citrus_namespace.h"
     54      1.26   tnozaki #include "citrus_bcs.h"
     55      1.26   tnozaki #include "citrus_region.h"
     56      1.26   tnozaki #include "citrus_lookup.h"
     57      1.26   tnozaki #include "citrus_aliasname_local.h"
     58      1.20  tshiozak 
     59      1.19      yamt #define NLS_ALIAS_DB "/usr/share/nls/nls.alias"
     60      1.19      yamt 
     61       1.6       jtc #define NLS_DEFAULT_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L"
     62       1.6       jtc #define NLS_DEFAULT_LANG "C"
     63      1.15   mycroft 
     64      1.15   mycroft #ifdef __weak_alias
     65      1.15   mycroft __weak_alias(catopen, _catopen)
     66      1.15   mycroft #endif
     67       1.1       jtc 
     68  1.28.6.1      yamt static nl_catd load_msgcat(const char *);
     69       1.7       jtc 
     70       1.1       jtc nl_catd
     71  1.28.6.1      yamt _catopen(const char *name, int oflag)
     72       1.1       jtc {
     73      1.18  sommerfe 	char tmppath[PATH_MAX+1];
     74      1.23  christos 	const char *nlspath;
     75      1.26   tnozaki 	const char *lang, *reallang;
     76      1.23  christos 	char *t;
     77      1.23  christos 	const char *s, *u;
     78       1.6       jtc 	nl_catd catd;
     79      1.19      yamt 	char langbuf[PATH_MAX];
     80      1.20  tshiozak 
     81       1.6       jtc 	if (name == NULL || *name == '\0')
     82      1.10       mrg 		return (nl_catd)-1;
     83       1.6       jtc 
     84       1.6       jtc 	/* absolute or relative path? */
     85      1.10       mrg 	if (strchr(name, '/'))
     86       1.7       jtc 		return load_msgcat(name);
     87       1.7       jtc 
     88      1.17  tshiozak 	if (issetugid() || (nlspath = getenv("NLSPATH")) == NULL)
     89       1.7       jtc 		nlspath = NLS_DEFAULT_PATH;
     90      1.19      yamt 	if (oflag == NL_CAT_LOCALE) {
     91      1.19      yamt 		lang = setlocale(LC_MESSAGES, NULL);
     92      1.19      yamt 	}
     93      1.19      yamt 	else {
     94      1.19      yamt 		lang = getenv("LANG");
     95      1.19      yamt 	}
     96      1.19      yamt 	if (lang == NULL || strchr(lang, '/'))
     97       1.7       jtc 		lang = NLS_DEFAULT_LANG;
     98      1.19      yamt 
     99      1.26   tnozaki 	reallang = __unaliasname(NLS_ALIAS_DB, lang, langbuf, sizeof(langbuf));
    100      1.26   tnozaki 	if (reallang == NULL)
    101      1.26   tnozaki 		reallang = lang;
    102       1.6       jtc 
    103       1.7       jtc 	s = nlspath;
    104      1.20  tshiozak 	t = tmppath;
    105       1.7       jtc 	do {
    106       1.7       jtc 		while (*s && *s != ':') {
    107       1.6       jtc 			if (*s == '%') {
    108       1.8       jtc 				switch (*(++s)) {
    109       1.7       jtc 				case 'L':	/* locale */
    110      1.26   tnozaki 					u = reallang;
    111       1.8       jtc 					while (*u && t < tmppath + PATH_MAX)
    112       1.8       jtc 						*t++ = *u++;
    113       1.7       jtc 					break;
    114       1.7       jtc 				case 'N':	/* name */
    115       1.8       jtc 					u = name;
    116       1.8       jtc 					while (*u && t < tmppath + PATH_MAX)
    117       1.8       jtc 						*t++ = *u++;
    118       1.7       jtc 					break;
    119       1.7       jtc 				case 'l':	/* lang */
    120       1.7       jtc 				case 't':	/* territory */
    121       1.7       jtc 				case 'c':	/* codeset */
    122       1.7       jtc 					break;
    123       1.7       jtc 				default:
    124       1.8       jtc 					if (t < tmppath + PATH_MAX)
    125       1.9       jtc 						*t++ = *s;
    126       1.6       jtc 				}
    127       1.6       jtc 			} else {
    128       1.8       jtc 				if (t < tmppath + PATH_MAX)
    129       1.9       jtc 					*t++ = *s;
    130       1.6       jtc 			}
    131       1.9       jtc 			s++;
    132       1.6       jtc 		}
    133       1.6       jtc 
    134       1.7       jtc 		*t = '\0';
    135       1.7       jtc 		catd = load_msgcat(tmppath);
    136      1.10       mrg 		if (catd != (nl_catd)-1)
    137       1.7       jtc 			return catd;
    138       1.7       jtc 
    139       1.9       jtc 		if (*s)
    140       1.9       jtc 			s++;
    141       1.7       jtc 		t = tmppath;
    142       1.7       jtc 	} while (*s);
    143       1.7       jtc 
    144      1.10       mrg 	return (nl_catd)-1;
    145       1.7       jtc }
    146       1.7       jtc 
    147       1.7       jtc static nl_catd
    148  1.28.6.1      yamt load_msgcat(const char *path)
    149       1.7       jtc {
    150       1.7       jtc 	struct stat st;
    151       1.7       jtc 	nl_catd catd;
    152       1.9       jtc 	void *data;
    153       1.7       jtc 	int fd;
    154      1.16     lukem 
    155      1.16     lukem 	_DIAGASSERT(path != NULL);
    156       1.6       jtc 
    157      1.10       mrg 	if ((fd = open(path, O_RDONLY)) == -1)
    158      1.10       mrg 		return (nl_catd)-1;
    159       1.6       jtc 
    160       1.6       jtc 	if (fstat(fd, &st) != 0) {
    161       1.6       jtc 		close (fd);
    162      1.10       mrg 		return (nl_catd)-1;
    163       1.6       jtc 	}
    164       1.6       jtc 
    165      1.13   mycroft 	data = mmap(0, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd,
    166      1.14  christos 	    (off_t)0);
    167       1.9       jtc 	close (fd);
    168       1.9       jtc 
    169      1.28     joerg 	if (data == MAP_FAILED) {
    170      1.10       mrg 		return (nl_catd)-1;
    171       1.6       jtc 	}
    172       1.6       jtc 
    173      1.14  christos 	if (ntohl((u_int32_t)((struct _nls_cat_hdr *)data)->__magic) !=
    174      1.14  christos 	    _NLS_MAGIC) {
    175      1.10       mrg 		munmap(data, (size_t)st.st_size);
    176      1.10       mrg 		return (nl_catd)-1;
    177       1.6       jtc 	}
    178       1.6       jtc 
    179      1.28     joerg 	if ((catd = malloc(sizeof (*catd))) == NULL) {
    180      1.10       mrg 		munmap(data, (size_t)st.st_size);
    181      1.10       mrg 		return (nl_catd)-1;
    182       1.6       jtc 	}
    183       1.6       jtc 
    184       1.9       jtc 	catd->__data = data;
    185      1.14  christos 	catd->__size = (int)st.st_size;
    186       1.6       jtc 	return catd;
    187       1.1       jtc }
    188