Home | History | Annotate | Line # | Download | only in nls
catopen.c revision 1.7
      1 /*	$NetBSD: catopen.c,v 1.7 1996/06/20 14:54:38 jtc 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 REGENTS OR CONTRIBUTORS BE
     30  * 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 <limits.h>
     42 #include <stdlib.h>
     43 #include <string.h>
     44 #include <sys/types.h>
     45 #include <sys/param.h>
     46 #include <sys/stat.h>
     47 #include <sys/mman.h>
     48 #include <unistd.h>
     49 #include <fcntl.h>
     50 #include <nl_types.h>
     51 
     52 #define NLS_DEFAULT_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L"
     53 #define NLS_DEFAULT_LANG "C"
     54 
     55 static nl_catd load_msgcat();
     56 
     57 nl_catd
     58 _catopen(name, oflag)
     59 	const char *name;
     60 	int oflag;
     61 {
     62 	char tmppath[PATH_MAX];
     63 	char *nlspath;
     64 	char *lang;
     65 	char *s, *t;
     66 	nl_catd catd;
     67 
     68 	if (name == NULL || *name == '\0')
     69 		return (nl_catd) -1;
     70 
     71 	/* absolute or relative path? */
     72 	if (strchr (name, '/'))
     73 		return load_msgcat(name);
     74 
     75 	if ((nlspath = getenv ("NLSPATH")) == NULL) {
     76 		nlspath = NLS_DEFAULT_PATH;
     77 	}
     78 	if ((lang = getenv ("LANG")) == NULL) {
     79 		lang = NLS_DEFAULT_LANG;
     80 	}
     81 
     82 	s = nlspath;
     83 	t = tmppath;
     84 	do {
     85 		while (*s && *s != ':') {
     86 			if (*s == '%') {
     87 				switch (*(s+1)) {
     88 				case 'L':	/* locale */
     89 					strcpy(t, lang);
     90 					t += strlen(lang);
     91 					s += 2;
     92 					break;
     93 				case 'N':	/* name */
     94 					strcpy(t, name);
     95 					t += strlen(name);
     96 					s += 2;
     97 					break;
     98 				case 'l':	/* lang */
     99 				case 't':	/* territory */
    100 				case 'c':	/* codeset */
    101 					break;
    102 				default:
    103 					*t++ = *s++;
    104 				}
    105 			} else {
    106 				*t++ = *s++;
    107 			}
    108 		}
    109 
    110 		*t = '\0';
    111 		catd = load_msgcat(tmppath);
    112 		if (catd != (nl_catd) 0)
    113 			return catd;
    114 
    115 		s++;
    116 		t = tmppath;
    117 	} while (*s);
    118 
    119 	return (nl_catd) 0;
    120 }
    121 
    122 static nl_catd
    123 load_msgcat(path)
    124 	const char *path;
    125 {
    126 	struct _nls_cat_hdr *cat_hdr;
    127 	struct stat st;
    128 	nl_catd catd;
    129 	int fd;
    130 
    131 	if ((fd = open (path, O_RDONLY)) == -1)
    132 		return (nl_catd) 0;
    133 
    134 	if (fstat(fd, &st) != 0) {
    135 		close (fd);
    136 		return (nl_catd) 0;
    137 	}
    138 
    139 	if ((catd = malloc (sizeof (*catd))) == 0) {
    140 		close (fd);
    141 		return (nl_catd) 0;
    142 	}
    143 
    144 	catd->__data = mmap(0, (size_t) st.st_size, PROT_READ, MAP_SHARED, fd, 0);
    145 	close (fd);
    146 
    147 	if (catd->__data == (void *) -1) {
    148 		free (catd);
    149 		return (nl_catd) 0;
    150 	}
    151 	catd->__size = st.st_size;
    152 
    153 	cat_hdr = (struct _nls_cat_hdr *) catd->__data;
    154 	if (ntohl(cat_hdr->__magic) != _NLS_MAGIC) {
    155 		free (catd);
    156 		close (fd);
    157 		return (nl_catd) 0;
    158 	}
    159 
    160 	return catd;
    161 }
    162