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 $ */
5 * Copyright (c) 1996 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
42 #include <sys/param.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 *);
71 _catopen(const char *name, int oflag)
73 char tmppath[PATH_MAX+1];
79 char langbuf[PATH_MAX];
81 if (name == NULL || *name == '\0')
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);
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);
104 while (*s && *s != ':') {
107 case 'L': /* locale */
109 while (*u && t < tmppath + PATH_MAX)
114 while (*u && t < tmppath + PATH_MAX)
118 case 't': /* territory */
119 case 'c': /* codeset */
122 if (t < tmppath + PATH_MAX)
126 if (t < tmppath + PATH_MAX)
133 catd = load_msgcat(tmppath);
134 if (catd != INVALID_CAT)
146 load_msgcat(const char *path)
153 _DIAGASSERT(path != NULL);
155 if ((fd = open(path, O_RDONLY)) == -1)
158 if (fstat(fd, &st) != 0) {
163 data = mmap(0, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_SHARED, fd,
167 if (data == (void *)-1) {
168 munmap(data, (size_t)st.st_size);
172 if (ntohl((uint32_t)((struct _nls_cat_hdr *)data)->__magic) !=
174 munmap(data, (size_t)st.st_size);
178 if ((catd = malloc(sizeof (*catd))) == 0) {
179 munmap(data, (size_t)st.st_size);
184 catd->__size = st.st_size;