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