catopen.c revision 1.6 1 /* $NetBSD: catopen.c,v 1.6 1996/05/13 23:29:39 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 nl_catd
56 _catopen(name, oflag)
57 const char *name;
58 int oflag;
59 {
60 const char *path;
61 struct stat st;
62 nl_catd catd;
63 int fd;
64
65 struct _nls_cat_hdr *cat_hdr;
66
67 if (name == NULL || *name == '\0')
68 return (nl_catd) -1;
69
70 /* absolute or relative path? */
71 if (strchr (name, '/')) {
72 if (stat (name, &st)) {
73 return (nl_catd) 0;
74 }
75 path = name;
76 } else {
77 char tmppath[PATH_MAX];
78 char *nlspath;
79 char *lang;
80 char *s, *t;
81
82 if ((nlspath = getenv ("NLSPATH")) == NULL) {
83 nlspath = NLS_DEFAULT_PATH;
84 }
85 if ((lang = getenv ("LANG")) == NULL) {
86 lang = NLS_DEFAULT_LANG;
87 }
88
89 for (s = nlspath, t = tmppath; *s; ) {
90 if (*s == '%') {
91 if (*(s + 1) == 'L') {
92 strcpy(t, lang);
93 t += strlen(lang);
94 s += 2;
95 } else if (*(s + 1) == 'N') {
96 strcpy(t, name);
97 t += strlen(name);
98 s += 2;
99 } else {
100 *t++ = *s++;
101 }
102 } else if (*s == ':') {
103 *t = '\0';
104
105 if (stat (tmppath, &st) == 0) {
106 path = tmppath;
107 goto load_msgcat;
108 }
109
110 t = tmppath;
111 } else {
112 *t++ = *s++;
113 }
114 }
115
116 return (nl_catd) 0;
117 }
118
119 load_msgcat:
120 if ((fd = open (path, O_RDONLY)) == -1)
121 return (nl_catd) 0;
122
123 if (fstat(fd, &st) != 0) {
124 close (fd);
125 return (nl_catd) 0;
126 }
127
128 if ((catd = malloc (sizeof (*catd))) == 0) {
129 close (fd);
130 return (nl_catd) 0;
131 }
132
133 catd->__data = mmap(0, (size_t) st.st_size, PROT_READ, MAP_SHARED, fd, 0);
134 close (fd);
135
136 if (catd->__data == (void *) -1) {
137 free (catd);
138 return (nl_catd) 0;
139 }
140 catd->__size = st.st_size;
141
142 cat_hdr = (struct _nls_cat_hdr *) catd->__data;
143 if (ntohl(cat_hdr->__magic) != _NLS_MAGIC) {
144 free (catd);
145 close (fd);
146 return (nl_catd) 0;
147 }
148
149 return catd;
150 }
151