setlocale.c revision 1.54.4.2 1 1.54.4.2 snj /* $NetBSD: setlocale.c,v 1.54.4.2 2009/03/12 23:52:06 snj Exp $ */
2 1.10 kleink
3 1.54.4.1 snj /*-
4 1.54.4.1 snj * Copyright (c)2008 Citrus Project,
5 1.54.4.1 snj * All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd *
16 1.54.4.1 snj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.54.4.1 snj * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 cgd * SUCH DAMAGE.
27 1.1 cgd */
28 1.1 cgd
29 1.12 christos #include <sys/cdefs.h>
30 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
31 1.54.4.2 snj __RCSID("$NetBSD: setlocale.c,v 1.54.4.2 2009/03/12 23:52:06 snj Exp $");
32 1.1 cgd #endif /* LIBC_SCCS and not lint */
33 1.1 cgd
34 1.54.4.1 snj #include <sys/cdefs.h>
35 1.37 yamt #include <sys/types.h>
36 1.54.4.1 snj #include <langinfo.h>
37 1.18 tshiozak #define __SETLOCALE_SOURCE__
38 1.1 cgd #include <locale.h>
39 1.11 kleink #include <paths.h>
40 1.5 jtc #include <stdlib.h>
41 1.1 cgd #include <string.h>
42 1.21 veego #include <unistd.h>
43 1.44 tshiozak
44 1.54.4.1 snj #include "setlocale_local.h"
45 1.1 cgd
46 1.54.4.1 snj const char *_PathLocale = NULL;
47 1.19 kleink
48 1.54.4.1 snj __link_set_decl(all_categories, _locale_category_t);
49 1.5 jtc
50 1.54.4.2 snj extern const _locale_category_t _generic_LC_ALL_desc;
51 1.54.4.2 snj extern const _locale_category_t _dummy_LC_COLLATE_desc;
52 1.54.4.2 snj #ifdef WITH_RUNE
53 1.54.4.2 snj extern const _locale_category_t _citrus_LC_CTYPE_desc;
54 1.54.4.2 snj extern const _locale_category_t _citrus_LC_MONETARY_desc;
55 1.54.4.2 snj extern const _locale_category_t _citrus_LC_NUMERIC_desc;
56 1.54.4.2 snj extern const _locale_category_t _citrus_LC_TIME_desc;
57 1.54.4.2 snj extern const _locale_category_t _citrus_LC_MESSAGES_desc;
58 1.54.4.2 snj #else
59 1.54.4.2 snj extern const _locale_category_t _localeio_LC_CTYPE_desc;
60 1.54.4.2 snj extern const _locale_category_t _localeio_LC_MONETARY_desc;
61 1.54.4.2 snj extern const _locale_category_t _localeio_LC_NUMERIC_desc;
62 1.54.4.2 snj extern const _locale_category_t _localeio_LC_TIME_desc;
63 1.54.4.2 snj extern const _locale_category_t _localeio_LC_MESSAGES_desc;
64 1.54.4.1 snj #endif
65 1.5 jtc
66 1.54.4.2 snj __link_set_add_data(all_categories, _generic_LC_ALL_desc);
67 1.54.4.2 snj __link_set_add_data(all_categories, _dummy_LC_COLLATE_desc);
68 1.54.4.2 snj #ifdef WITH_RUNE
69 1.54.4.2 snj __link_set_add_data(all_categories, _citrus_LC_CTYPE_desc);
70 1.54.4.2 snj __link_set_add_data(all_categories, _citrus_LC_MONETARY_desc);
71 1.54.4.2 snj __link_set_add_data(all_categories, _citrus_LC_NUMERIC_desc);
72 1.54.4.2 snj __link_set_add_data(all_categories, _citrus_LC_TIME_desc);
73 1.54.4.2 snj __link_set_add_data(all_categories, _citrus_LC_MESSAGES_desc);
74 1.54.4.2 snj #else
75 1.54.4.2 snj __link_set_add_data(all_categories, _localeio_LC_CTYPE_desc);
76 1.54.4.2 snj __link_set_add_data(all_categories, _localeio_LC_MONETARY_desc);
77 1.54.4.2 snj __link_set_add_data(all_categories, _localeio_LC_NUMERIC_desc);
78 1.54.4.2 snj __link_set_add_data(all_categories, _localeio_LC_TIME_desc);
79 1.54.4.2 snj __link_set_add_data(all_categories, _localeio_LC_MESSAGES_desc);
80 1.54.4.2 snj #endif
81 1.24 itojun
82 1.54.4.1 snj _locale_category_t *
83 1.54.4.1 snj _find_category(int category)
84 1.24 itojun {
85 1.54.4.1 snj _locale_category_t * const *p;
86 1.5 jtc
87 1.54.4.1 snj __link_set_foreach(p, all_categories) {
88 1.54.4.1 snj if ((*p)->category == category)
89 1.54.4.1 snj return *p;
90 1.22 itojun }
91 1.54.4.1 snj return NULL;
92 1.5 jtc }
93 1.5 jtc
94 1.54.4.1 snj const char *
95 1.54.4.1 snj _get_locale_env(const char *category)
96 1.5 jtc {
97 1.54.4.1 snj const char *name;
98 1.5 jtc
99 1.54.4.1 snj /* 1. check LC_ALL */
100 1.54.4.1 snj name = (const char *)getenv("LC_ALL");
101 1.54.4.1 snj if (name == NULL || *name == '\0') {
102 1.54.4.1 snj /* 2. check LC_* */
103 1.54.4.1 snj name = (const char *)getenv(category);
104 1.54.4.1 snj if (name == NULL || *name == '\0') {
105 1.54.4.1 snj /* 3. check LANG */
106 1.54.4.1 snj name = getenv("LANG");
107 1.51 manu }
108 1.46 tshiozak }
109 1.54.4.1 snj if (name == NULL || *name == '\0' || strchr(name, '/'))
110 1.54.4.1 snj /* 4. if none is set, fall to "C" */
111 1.54.4.1 snj name = _C_LOCALE;
112 1.54.4.1 snj return name;
113 1.46 tshiozak }
114 1.46 tshiozak
115 1.54.4.1 snj char *
116 1.54.4.1 snj __setlocale(int category, const char *name)
117 1.44 tshiozak {
118 1.54.4.1 snj _locale_category_t *l;
119 1.54.4.1 snj struct _locale_impl_t *impl;
120 1.44 tshiozak
121 1.54.4.1 snj if (category >= LC_ALL && category < _LC_LAST) {
122 1.54.4.1 snj l = _find_category(category);
123 1.54.4.1 snj if (l != NULL) {
124 1.54.4.1 snj if (issetugid() || ((_PathLocale == NULL &&
125 1.54.4.1 snj (_PathLocale = getenv("PATH_LOCALE")) == NULL) ||
126 1.54.4.1 snj *_PathLocale == '\0'))
127 1.54.4.1 snj _PathLocale = _PATH_LOCALE;
128 1.54.4.1 snj impl = *_current_locale();
129 1.54.4.1 snj return __UNCONST((*l->setlocale)(name, impl));
130 1.44 tshiozak }
131 1.44 tshiozak }
132 1.44 tshiozak return NULL;
133 1.1 cgd }
134