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