1 /* $NetBSD: catopen.c,v 1.33 2014/09/16 01:30:28 christos 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 __RCSID("$NetBSD: catopen.c,v 1.33 2014/09/16 01:30:28 christos Exp $");
36 #define __SETLOCALE_SOURCE__
38 #include "namespace.h"
39 #include <sys/param.h>
52 #include "citrus_namespace.h"
53 #include "citrus_bcs.h"
54 #include "citrus_region.h"
55 #include "citrus_lookup.h"
56 #include "citrus_aliasname_local.h"
57 #include "setlocale_local.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 __weak_alias(catopen
, _catopen
)
65 __weak_alias(catopen_l
, _catopen_l
)
67 static nl_catd
load_msgcat(const char *);
70 catopen(const char *name
, int oflag
)
73 return catopen_l(name
, oflag
, _current_locale());
77 catopen_l(const char *name
, int oflag
, locale_t loc
)
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
;
98 * http://www.hauN.org/ml/b-l-j/a/800/828.html (in japanese)
100 if (oflag
== NL_CAT_LOCALE
) {
101 lang
= loc
->part_name
[LC_MESSAGES
];
103 lang
= getenv("LANG");
105 if (lang
== NULL
|| strchr(lang
, '/'))
106 lang
= NLS_DEFAULT_LANG
;
108 reallang
= __unaliasname(NLS_ALIAS_DB
, lang
, langbuf
, sizeof(langbuf
));
109 if (reallang
== NULL
)
115 while (*s
&& *s
!= ':') {
118 case 'L': /* locale */
120 while (*u
&& t
< tmppath
+ PATH_MAX
)
125 while (*u
&& t
< tmppath
+ PATH_MAX
)
129 case 't': /* territory */
130 case 'c': /* codeset */
133 if (t
< tmppath
+ PATH_MAX
)
137 if (t
< tmppath
+ PATH_MAX
)
144 catd
= load_msgcat(tmppath
);
145 if (catd
!= (nl_catd
)-1)
157 load_msgcat(const char *path
)
164 _DIAGASSERT(path
!= NULL
);
166 if ((fd
= open(path
, O_RDONLY
|O_CLOEXEC
)) == -1)
169 if (fstat(fd
, &st
) != 0) {
174 data
= mmap(0, (size_t)st
.st_size
, PROT_READ
, MAP_FILE
|MAP_SHARED
, fd
,
178 if (data
== MAP_FAILED
) {
182 if (ntohl((u_int32_t
)((struct _nls_cat_hdr
*)data
)->__magic
) !=
184 munmap(data
, (size_t)st
.st_size
);
188 if ((catd
= malloc(sizeof (*catd
))) == NULL
) {
189 munmap(data
, (size_t)st
.st_size
);
194 catd
->__size
= (int)st
.st_size
;