1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "locale-setup.h"
31 /* We don't list LC_ALL here on purpose. People should be
32 * using LANG instead. */
44 VARIABLE_LC_TELEPHONE,
45 VARIABLE_LC_MEASUREMENT,
46 VARIABLE_LC_IDENTIFICATION,
50 static const char * const variable_names[_VARIABLE_MAX] = {
51 [VARIABLE_LANG] = "LANG",
52 [VARIABLE_LC_CTYPE] = "LC_CTYPE",
53 [VARIABLE_LC_NUMERIC] = "LC_NUMERIC",
54 [VARIABLE_LC_TIME] = "TIME",
55 [VARIABLE_LC_COLLATE] = "COLLATE",
56 [VARIABLE_LC_MONETARY] = "MONETARY",
57 [VARIABLE_LC_MESSAGES] = "MESSAGE",
58 [VARIABLE_LC_PAPER] = "PAPER",
59 [VARIABLE_LC_NAME] = "NAME",
60 [VARIABLE_LC_ADDRESS] = "ADDRESS",
61 [VARIABLE_LC_TELEPHONE] = "TELEPHONE",
62 [VARIABLE_LC_MEASUREMENT] = "MEASUREMENT",
63 [VARIABLE_LC_IDENTIFICATION] = "IDENTIFICATION"
66 int locale_setup(void) {
67 char *variables[_VARIABLE_MAX];
73 if ((r = parse_env_file("/etc/sysconfig/i18n", NEWLINE,
74 "LANG", &variables[VARIABLE_LANG],
78 log_warning("Failed to read /etc/sysconfig/i18n: %s", strerror(-r));
80 #elif defined(TARGET_ARCH)
81 if ((r = parse_env_file("/etc/rc.conf", NEWLINE,
82 "LOCALE", &variables[VARIABLE_LANG],
86 log_warning("Failed to read /etc/rc.conf: %s", strerror(-r));
88 #elif defined(TARGET_GENTOO)
89 /* Gentoo's openrc expects locale variables in /etc/env.d/
90 * These files are later compiled by env-update into shell
91 * export commands at /etc/profile.env, with variables being
92 * exported by openrc's runscript (so /etc/init.d/)
94 if ((r = parse_env_file("/etc/profile.env", NEWLINE,
95 "export LANG", &variables[VARIABLE_LANG],
96 "export LC_CTYPE", &variables[VARIABLE_LC_CTYPE],
97 "export LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC],
98 "export LC_TIME", &variables[VARIABLE_LC_TIME],
99 "export LC_COLLATE", &variables[VARIABLE_LC_COLLATE],
100 "export LC_MONETARY", &variables[VARIABLE_LC_MONETARY],
101 "export LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES],
102 "export LC_PAPER", &variables[VARIABLE_LC_PAPER],
103 "export LC_NAME", &variables[VARIABLE_LC_NAME],
104 "export LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS],
105 "export LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE],
106 "export LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT],
107 "export LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
111 log_warning("Failed to read /etc/profile.env: %s", strerror(-r));
115 /* Override distribution-specific options with the
116 * distribution-independent configuration */
117 if ((r = parse_env_file("/etc/locale", NEWLINE,
118 "LANG", &variables[VARIABLE_LANG],
119 "LC_CTYPE", &variables[VARIABLE_LC_CTYPE],
120 "LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC],
121 "LC_TIME", &variables[VARIABLE_LC_TIME],
122 "LC_COLLATE", &variables[VARIABLE_LC_COLLATE],
123 "LC_MONETARY", &variables[VARIABLE_LC_MONETARY],
124 "LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES],
125 "LC_PAPER", &variables[VARIABLE_LC_PAPER],
126 "LC_NAME", &variables[VARIABLE_LC_NAME],
127 "LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS],
128 "LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE],
129 "LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT],
130 "LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
134 log_warning("Failed to read /etc/locale: %s", strerror(-r));
137 if ((r = parse_env_file("/proc/cmdline", WHITESPACE,
139 "LANG", &variables[VARIABLE_LANG],
141 "locale.LANG", &variables[VARIABLE_LANG],
142 "locale.LC_CTYPE", &variables[VARIABLE_LC_CTYPE],
143 "locale.LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC],
144 "locale.LC_TIME", &variables[VARIABLE_LC_TIME],
145 "locale.LC_COLLATE", &variables[VARIABLE_LC_COLLATE],
146 "locale.LC_MONETARY", &variables[VARIABLE_LC_MONETARY],
147 "locale.LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES],
148 "locale.LC_PAPER", &variables[VARIABLE_LC_PAPER],
149 "locale.LC_NAME", &variables[VARIABLE_LC_NAME],
150 "locale.LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS],
151 "locale.LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE],
152 "locale.LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT],
153 "locale.LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
157 log_warning("Failed to read /proc/cmdline: %s", strerror(-r));
160 if (!variables[VARIABLE_LANG]) {
161 if (!(variables[VARIABLE_LANG] = strdup("C"))) {
167 for (i = 0; i < _VARIABLE_MAX; i++) {
170 if (setenv(variable_names[i], variables[i], 1) < 0) {
175 unsetenv(variable_names[i]);
181 for (i = 0; i < _VARIABLE_MAX; i++)