1 /* $NetBSD: setlocale.c,v 1.56 2009/01/11 02:46:29 christos Exp $ */
4 * Copyright (c)2008 Citrus Project,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: setlocale.c,v 1.56 2009/01/11 02:46:29 christos Exp $");
32 #endif /* LIBC_SCCS and not lint */
34 #include <sys/cdefs.h>
35 #include <sys/types.h>
37 #define __SETLOCALE_SOURCE__
44 #include "setlocale_local.h"
46 const char *_PathLocale
= NULL
;
48 __link_set_decl(all_categories
, _locale_category_t
);
50 extern const _locale_category_t _generic_LC_ALL_desc
;
51 extern const _locale_category_t _dummy_LC_COLLATE_desc
;
53 extern const _locale_category_t _citrus_LC_CTYPE_desc
;
54 extern const _locale_category_t _citrus_LC_MONETARY_desc
;
55 extern const _locale_category_t _citrus_LC_NUMERIC_desc
;
56 extern const _locale_category_t _citrus_LC_TIME_desc
;
57 extern const _locale_category_t _citrus_LC_MESSAGES_desc
;
59 extern const _locale_category_t _localeio_LC_CTYPE_desc
;
60 extern const _locale_category_t _localeio_LC_MONETARY_desc
;
61 extern const _locale_category_t _localeio_LC_NUMERIC_desc
;
62 extern const _locale_category_t _localeio_LC_TIME_desc
;
63 extern const _locale_category_t _localeio_LC_MESSAGES_desc
;
66 __link_set_add_data(all_categories
, _generic_LC_ALL_desc
);
67 __link_set_add_data(all_categories
, _dummy_LC_COLLATE_desc
);
69 __link_set_add_data(all_categories
, _citrus_LC_CTYPE_desc
);
70 __link_set_add_data(all_categories
, _citrus_LC_MONETARY_desc
);
71 __link_set_add_data(all_categories
, _citrus_LC_NUMERIC_desc
);
72 __link_set_add_data(all_categories
, _citrus_LC_TIME_desc
);
73 __link_set_add_data(all_categories
, _citrus_LC_MESSAGES_desc
);
75 __link_set_add_data(all_categories
, _localeio_LC_CTYPE_desc
);
76 __link_set_add_data(all_categories
, _localeio_LC_MONETARY_desc
);
77 __link_set_add_data(all_categories
, _localeio_LC_NUMERIC_desc
);
78 __link_set_add_data(all_categories
, _localeio_LC_TIME_desc
);
79 __link_set_add_data(all_categories
, _localeio_LC_MESSAGES_desc
);
83 _find_category(int category
)
85 _locale_category_t
* const *p
;
87 __link_set_foreach(p
, all_categories
) {
88 if ((*p
)->category
== category
)
95 _get_locale_env(const char *category
)
100 name
= (const char *)getenv("LC_ALL");
101 if (name
== NULL
|| *name
== '\0') {
103 name
= (const char *)getenv(category
);
104 if (name
== NULL
|| *name
== '\0') {
106 name
= getenv("LANG");
109 if (name
== NULL
|| *name
== '\0' || strchr(name
, '/'))
110 /* 4. if none is set, fall to "C" */
116 __setlocale(int category
, const char *name
)
118 _locale_category_t
*l
;
119 struct _locale_impl_t
*impl
;
121 if (category
>= LC_ALL
&& category
< _LC_LAST
) {
122 l
= _find_category(category
);
124 if (issetugid() || ((_PathLocale
== NULL
&&
125 (_PathLocale
= getenv("PATH_LOCALE")) == NULL
) ||
126 *_PathLocale
== '\0'))
127 _PathLocale
= _PATH_LOCALE
;
128 impl
= *_current_locale();
129 return __UNCONST((*l
->setlocale
)(name
, impl
));