Merge branch 'vendor/GDTOA'
[dragonfly.git] / lib / libc / nls / catopen.c
blob679d7e7cfa027d49892aa8f6a8cf1917ad1b5e75
1 /* $NetBSD: src/lib/libc/nls/catopen.c,v 1.21 2004/07/21 20:27:46 tshiozak Exp $ */
2 /* $DragonFly: src/lib/libc/nls/catopen.c,v 1.1 2005/03/16 06:54:41 joerg Exp $ */
4 /*-
5 * Copyright (c) 1996 The NetBSD Foundation, Inc.
6 * All rights reserved.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by J.T. Conklin.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
40 #define _NLS_PRIVATE
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #include <sys/mman.h>
46 #include <assert.h>
47 #include <fcntl.h>
48 #include <limits.h>
49 #include <locale.h>
50 #include <nl_types.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
55 #include "../citrus/citrus_namespace.h"
56 #include "../citrus/citrus_region.h"
57 #include "../citrus/citrus_lookup.h"
59 #define NLS_ALIAS_DB "/usr/share/nls/nls.alias"
61 #define NLS_DEFAULT_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L"
62 #define NLS_DEFAULT_LANG "C"
64 #define INVALID_CAT (nl_catd)(-1)
66 __weak_reference(_catopen, catopen);
68 static nl_catd load_msgcat(const char *);
70 nl_catd
71 _catopen(const char *name, int oflag)
73 char tmppath[PATH_MAX+1];
74 char *nlspath;
75 const char *lang;
76 char *s, *t;
77 const char *u;
78 nl_catd catd;
79 char langbuf[PATH_MAX];
81 if (name == NULL || *name == '\0')
82 return(INVALID_CAT);
84 /* absolute or relative path? */
85 if (strchr(name, '/'))
86 return(load_msgcat(name));
88 if (issetugid() || (nlspath = getenv("NLSPATH")) == NULL)
89 nlspath = NLS_DEFAULT_PATH;
90 if (oflag == NL_CAT_LOCALE)
91 lang = setlocale(LC_MESSAGES, NULL);
92 else
93 lang = getenv("LANG");
95 if (lang == NULL || strchr(lang, '/'))
96 lang = NLS_DEFAULT_LANG;
98 lang = _lookup_alias(NLS_ALIAS_DB, lang, langbuf, sizeof(langbuf),
99 _LOOKUP_CASE_SENSITIVE);
101 s = nlspath;
102 t = tmppath;
103 do {
104 while (*s && *s != ':') {
105 if (*s == '%') {
106 switch (*(++s)) {
107 case 'L': /* locale */
108 u = lang;
109 while (*u && t < tmppath + PATH_MAX)
110 *t++ = *u++;
111 break;
112 case 'N': /* name */
113 u = name;
114 while (*u && t < tmppath + PATH_MAX)
115 *t++ = *u++;
116 break;
117 case 'l': /* lang */
118 case 't': /* territory */
119 case 'c': /* codeset */
120 break;
121 default:
122 if (t < tmppath + PATH_MAX)
123 *t++ = *s;
125 } else {
126 if (t < tmppath + PATH_MAX)
127 *t++ = *s;
129 s++;
132 *t = '\0';
133 catd = load_msgcat(tmppath);
134 if (catd != INVALID_CAT)
135 return(catd);
137 if (*s)
138 s++;
139 t = tmppath;
140 } while (*s);
142 return(INVALID_CAT);
145 static nl_catd
146 load_msgcat(const char *path)
148 struct stat st;
149 nl_catd catd;
150 void *data;
151 int fd;
153 _DIAGASSERT(path != NULL);
155 if ((fd = open(path, O_RDONLY)) == -1)
156 return(INVALID_CAT);
158 if (fstat(fd, &st) != 0) {
159 close (fd);
160 return(INVALID_CAT);
163 data = mmap(0, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd,
164 (off_t)0);
165 close(fd);
167 if (data == (void *)-1) {
168 munmap(data, (size_t)st.st_size);
169 return(INVALID_CAT);
172 if (ntohl((uint32_t)((struct _nls_cat_hdr *)data)->__magic) !=
173 _NLS_MAGIC) {
174 munmap(data, (size_t)st.st_size);
175 return(INVALID_CAT);
178 if ((catd = malloc(sizeof (*catd))) == 0) {
179 munmap(data, (size_t)st.st_size);
180 return(INVALID_CAT);
183 catd->__data = data;
184 catd->__size = st.st_size;
185 return(catd);