1 /* $NetBSD: catopen.c,v 1.28 2009/03/10 13:15:40 joerg Exp $ */
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 */
39 #include "namespace.h"
40 #include <sys/param.h>
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"
60 #include "aliasname_local.h"
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"
69 __weak_alias(catopen
, _catopen
)
72 static nl_catd load_msgcat
__P((const char *));
79 char tmppath
[PATH_MAX
+1];
81 const char *lang
, *reallang
;
85 char langbuf
[PATH_MAX
];
87 if (name
== NULL
|| *name
== '\0')
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
);
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
)
112 while (*s
&& *s
!= ':') {
115 case 'L': /* locale */
117 while (*u
&& t
< tmppath
+ PATH_MAX
)
122 while (*u
&& t
< tmppath
+ PATH_MAX
)
126 case 't': /* territory */
127 case 'c': /* codeset */
130 if (t
< tmppath
+ PATH_MAX
)
134 if (t
< tmppath
+ PATH_MAX
)
141 catd
= load_msgcat(tmppath
);
142 if (catd
!= (nl_catd
)-1)
162 _DIAGASSERT(path
!= NULL
);
164 if ((fd
= open(path
, O_RDONLY
)) == -1)
167 if (fstat(fd
, &st
) != 0) {
173 if(!(data
= malloc((size_t)st
.st_size
))) {
177 if (read(fd
, data
, st
.st_size
) != st
.st_size
)
184 data
= mmap(0, (size_t)st
.st_size
, PROT_READ
, MAP_FILE
|MAP_SHARED
, fd
,
188 if (data
== MAP_FAILED
) {
193 if (ntohl((u_int32_t
)((struct _nls_cat_hdr
*)data
)->__magic
) !=
198 munmap(data
, (size_t)st
.st_size
);
203 if ((catd
= malloc(sizeof (*catd
))) == NULL
) {
207 munmap(data
, (size_t)st
.st_size
);
213 catd
->__size
= (int)st
.st_size
;