include: reduce default stack size
[minix.git] / lib / libc / nls / catopen.c
blob48519debdf87b9310392770ca3b4bfbb741aa778
1 /* $NetBSD: catopen.c,v 1.28 2009/03/10 13:15:40 joerg Exp $ */
3 /*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by J.T. Conklin.
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.
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.
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 __RCSID("$NetBSD: catopen.c,v 1.28 2009/03/10 13:15:40 joerg Exp $");
35 #endif /* LIBC_SCCS and not lint */
37 #define _NLS_PRIVATE
39 #include "namespace.h"
40 #include <sys/param.h>
41 #include <sys/stat.h>
42 #include <sys/mman.h>
44 #include <assert.h>
45 #include <fcntl.h>
46 #include <limits.h>
47 #include <locale.h>
48 #include <nl_types.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
53 #ifdef HAVE_CITRUS
54 #include "citrus_namespace.h"
55 #include "citrus_bcs.h"
56 #include "citrus_region.h"
57 #include "citrus_lookup.h"
58 #include "citrus_aliasname_local.h"
59 #else
60 #include "aliasname_local.h"
61 #endif
63 #define NLS_ALIAS_DB "/usr/share/nls/nls.alias"
65 #define NLS_DEFAULT_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L"
66 #define NLS_DEFAULT_LANG "C"
68 #ifdef __weak_alias
69 __weak_alias(catopen, _catopen)
70 #endif
72 static nl_catd load_msgcat __P((const char *));
74 nl_catd
75 _catopen(name, oflag)
76 const char *name;
77 int oflag;
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];
87 if (name == NULL || *name == '\0')
88 return (nl_catd)-1;
90 /* absolute or relative path? */
91 if (strchr(name, '/'))
92 return load_msgcat(name);
94 if (issetugid() || (nlspath = getenv("NLSPATH")) == NULL)
95 nlspath = NLS_DEFAULT_PATH;
96 if (oflag == NL_CAT_LOCALE) {
97 lang = setlocale(LC_MESSAGES, NULL);
99 else {
100 lang = getenv("LANG");
102 if (lang == NULL || strchr(lang, '/'))
103 lang = NLS_DEFAULT_LANG;
105 reallang = __unaliasname(NLS_ALIAS_DB, lang, langbuf, sizeof(langbuf));
106 if (reallang == NULL)
107 reallang = lang;
109 s = nlspath;
110 t = tmppath;
111 do {
112 while (*s && *s != ':') {
113 if (*s == '%') {
114 switch (*(++s)) {
115 case 'L': /* locale */
116 u = reallang;
117 while (*u && t < tmppath + PATH_MAX)
118 *t++ = *u++;
119 break;
120 case 'N': /* name */
121 u = name;
122 while (*u && t < tmppath + PATH_MAX)
123 *t++ = *u++;
124 break;
125 case 'l': /* lang */
126 case 't': /* territory */
127 case 'c': /* codeset */
128 break;
129 default:
130 if (t < tmppath + PATH_MAX)
131 *t++ = *s;
133 } else {
134 if (t < tmppath + PATH_MAX)
135 *t++ = *s;
137 s++;
140 *t = '\0';
141 catd = load_msgcat(tmppath);
142 if (catd != (nl_catd)-1)
143 return catd;
145 if (*s)
146 s++;
147 t = tmppath;
148 } while (*s);
150 return (nl_catd)-1;
153 static nl_catd
154 load_msgcat(path)
155 const char *path;
157 struct stat st;
158 nl_catd catd;
159 void *data;
160 int fd;
162 _DIAGASSERT(path != NULL);
164 if ((fd = open(path, O_RDONLY)) == -1)
165 return (nl_catd)-1;
167 if (fstat(fd, &st) != 0) {
168 close (fd);
169 return (nl_catd)-1;
172 #ifdef __minix
173 if(!(data = malloc((size_t)st.st_size))) {
174 return (nl_catd)-1;
177 if (read(fd, data, st.st_size) != st.st_size)
179 free(data);
180 return (nl_catd)-1;
182 close (fd);
183 #else /* !__minix */
184 data = mmap(0, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd,
185 (off_t)0);
186 close (fd);
188 if (data == MAP_FAILED) {
189 return (nl_catd)-1;
191 #endif /* __minix */
193 if (ntohl((u_int32_t)((struct _nls_cat_hdr *)data)->__magic) !=
194 _NLS_MAGIC) {
195 #ifdef __minix
196 free(data);
197 #else
198 munmap(data, (size_t)st.st_size);
199 #endif
200 return (nl_catd)-1;
203 if ((catd = malloc(sizeof (*catd))) == NULL) {
204 #ifdef __minix
205 free(data);
206 #else
207 munmap(data, (size_t)st.st_size);
208 #endif
209 return (nl_catd)-1;
212 catd->__data = data;
213 catd->__size = (int)st.st_size;
214 return catd;