setlocale.c revision 1.62 1 1.62 joerg /* $NetBSD: setlocale.c,v 1.62 2013/04/30 00:45:05 joerg Exp $ */
2 1.10 kleink
3 1.55 tnozaki /*-
4 1.55 tnozaki * Copyright (c)2008 Citrus Project,
5 1.55 tnozaki * 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.55 tnozaki * 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.55 tnozaki * 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.62 joerg __RCSID("$NetBSD: setlocale.c,v 1.62 2013/04/30 00:45:05 joerg Exp $");
32 1.1 cgd #endif /* LIBC_SCCS and not lint */
33 1.1 cgd
34 1.37 yamt #include <sys/types.h>
35 1.1 cgd #include <locale.h>
36 1.58 tnozaki #include <limits.h>
37 1.11 kleink #include <paths.h>
38 1.5 jtc #include <stdlib.h>
39 1.1 cgd #include <string.h>
40 1.21 veego #include <unistd.h>
41 1.44 tshiozak
42 1.55 tnozaki #include "setlocale_local.h"
43 1.55 tnozaki
44 1.55 tnozaki const char *_PathLocale = NULL;
45 1.44 tshiozak
46 1.60 tnozaki static _locale_set_t all_categories[_LC_LAST] = {
47 1.60 tnozaki [LC_ALL ] = &_generic_LC_ALL_setlocale,
48 1.60 tnozaki [LC_COLLATE ] = &_dummy_LC_COLLATE_setlocale,
49 1.60 tnozaki [LC_CTYPE ] = &_citrus_LC_CTYPE_setlocale,
50 1.60 tnozaki [LC_MONETARY] = &_citrus_LC_MONETARY_setlocale,
51 1.60 tnozaki [LC_NUMERIC ] = &_citrus_LC_NUMERIC_setlocale,
52 1.60 tnozaki [LC_TIME ] = &_citrus_LC_TIME_setlocale,
53 1.60 tnozaki [LC_MESSAGES] = &_citrus_LC_MESSAGES_setlocale,
54 1.60 tnozaki };
55 1.1 cgd
56 1.60 tnozaki _locale_set_t
57 1.55 tnozaki _find_category(int category)
58 1.24 itojun {
59 1.62 joerg static int initialised;
60 1.62 joerg
61 1.62 joerg if (!initialised) {
62 1.62 joerg if (issetugid() || ((_PathLocale == NULL &&
63 1.62 joerg (_PathLocale = getenv("PATH_LOCALE")) == NULL) ||
64 1.62 joerg *_PathLocale == '\0'))
65 1.62 joerg _PathLocale = _PATH_LOCALE;
66 1.62 joerg initialised = 1;
67 1.62 joerg }
68 1.62 joerg
69 1.60 tnozaki if (category >= LC_ALL && category < _LC_LAST)
70 1.60 tnozaki return all_categories[category];
71 1.55 tnozaki return NULL;
72 1.5 jtc }
73 1.5 jtc
74 1.55 tnozaki const char *
75 1.55 tnozaki _get_locale_env(const char *category)
76 1.5 jtc {
77 1.55 tnozaki const char *name;
78 1.5 jtc
79 1.55 tnozaki /* 1. check LC_ALL */
80 1.55 tnozaki name = (const char *)getenv("LC_ALL");
81 1.55 tnozaki if (name == NULL || *name == '\0') {
82 1.55 tnozaki /* 2. check LC_* */
83 1.55 tnozaki name = (const char *)getenv(category);
84 1.55 tnozaki if (name == NULL || *name == '\0') {
85 1.55 tnozaki /* 3. check LANG */
86 1.55 tnozaki name = getenv("LANG");
87 1.51 manu }
88 1.46 tshiozak }
89 1.55 tnozaki if (name == NULL || *name == '\0' || strchr(name, '/'))
90 1.55 tnozaki /* 4. if none is set, fall to "C" */
91 1.55 tnozaki name = _C_LOCALE;
92 1.55 tnozaki return name;
93 1.46 tshiozak }
94 1.46 tshiozak
95 1.55 tnozaki char *
96 1.55 tnozaki __setlocale(int category, const char *name)
97 1.44 tshiozak {
98 1.60 tnozaki _locale_set_t sl;
99 1.61 joerg struct _locale *impl;
100 1.44 tshiozak
101 1.60 tnozaki sl = _find_category(category);
102 1.60 tnozaki if (sl == NULL)
103 1.60 tnozaki return NULL;
104 1.60 tnozaki impl = *_current_locale();
105 1.60 tnozaki return __UNCONST((*sl)(name, impl));
106 1.1 cgd }
107 1.58 tnozaki
108 1.58 tnozaki char *
109 1.58 tnozaki setlocale(int category, const char *locale)
110 1.58 tnozaki {
111 1.58 tnozaki
112 1.58 tnozaki /* locale may be NULL */
113 1.58 tnozaki
114 1.58 tnozaki __mb_len_max_runtime = MB_LEN_MAX;
115 1.58 tnozaki return __setlocale(category, locale);
116 1.58 tnozaki }
117