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