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"
42 VARIABLE_LC_TELEPHONE,
43 VARIABLE_LC_MEASUREMENT,
44 VARIABLE_LC_IDENTIFICATION,
48 static const char * const variable_names[_VARIABLE_MAX] = {
49 [VARIABLE_LANG] = "LANG",
50 [VARIABLE_LC_CTYPE] = "LC_CTYPE",
51 [VARIABLE_LC_NUMERIC] = "LC_NUMERIC",
52 [VARIABLE_LC_TIME] = "TIME",
53 [VARIABLE_LC_COLLATE] = "COLLATE",
54 [VARIABLE_LC_MONETARY] = "MONETARY",
55 [VARIABLE_LC_MESSAGES] = "MESSAGE",
56 [VARIABLE_LC_ALL] = "ALL",
57 [VARIABLE_LC_PAPER] = "PAPER",
58 [VARIABLE_LC_NAME] = "NAME",
59 [VARIABLE_LC_ADDRESS] = "ADDRESS",
60 [VARIABLE_LC_TELEPHONE] = "TELEPHONE",
61 [VARIABLE_LC_MEASUREMENT] = "MEASUREMENT",
62 [VARIABLE_LC_IDENTIFICATION] = "IDENTIFICATION"
65 int locale_setup(void) {
66 char *variables[_VARIABLE_MAX];
72 if ((r = parse_env_file("/etc/sysconfig/i18n", NEWLINE,
73 "LANG", &variables[VARIABLE_LANG],
77 log_warning("Failed to read /etc/sysconfig/i18n: %s", strerror(-r));
79 #elif defined(TARGET_ARCH)
80 if ((r = parse_env_file("/etc/rc.conf", NEWLINE,
81 "LOCALE", &variables[VARIABLE_LANG],
85 log_warning("Failed to read /etc/rc.conf: %s", strerror(-r));
87 #elif defined(TARGET_GENTOO)
88 /* Gentoo's openrc expects locale variables in /etc/env.d/
89 * These files are later compiled by env-update into shell
90 * export commands at /etc/profile.env, with variables being
91 * exported by openrc's runscript (so /etc/init.d/)
93 if ((r = parse_env_file("/etc/profile.env", NEWLINE,
94 "export LANG", &variables[VARIABLE_LANG],
95 "export LC_CTYPE", &variables[VARIABLE_LC_CTYPE],
96 "export LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC],
97 "export LC_TIME", &variables[VARIABLE_LC_TIME],
98 "export LC_COLLATE", &variables[VARIABLE_LC_COLLATE],
99 "export LC_MONETARY", &variables[VARIABLE_LC_MONETARY],
100 "export LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES],
101 "export LC_ALL", &variables[VARIABLE_LC_ALL],
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_ALL", &variables[VARIABLE_LC_ALL],
126 "LC_PAPER", &variables[VARIABLE_LC_PAPER],
127 "LC_NAME", &variables[VARIABLE_LC_NAME],
128 "LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS],
129 "LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE],
130 "LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT],
131 "LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
135 log_warning("Failed to read /etc/locale: %s", strerror(-r));
138 if ((r = parse_env_file("/proc/cmdline", WHITESPACE,
140 "LANG", &variables[VARIABLE_LANG],
142 "locale.LANG", &variables[VARIABLE_LANG],
143 "locale.LC_CTYPE", &variables[VARIABLE_LC_CTYPE],
144 "locale.LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC],
145 "locale.LC_TIME", &variables[VARIABLE_LC_TIME],
146 "locale.LC_COLLATE", &variables[VARIABLE_LC_COLLATE],
147 "locale.LC_MONETARY", &variables[VARIABLE_LC_MONETARY],
148 "locale.LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES],
149 "locale.LC_ALL", &variables[VARIABLE_LC_ALL],
150 "locale.LC_PAPER", &variables[VARIABLE_LC_PAPER],
151 "locale.LC_NAME", &variables[VARIABLE_LC_NAME],
152 "locale.LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS],
153 "locale.LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE],
154 "locale.LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT],
155 "locale.LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
159 log_warning("Failed to read /proc/cmdline: %s", strerror(-r));
162 if (!variables[VARIABLE_LANG]) {
163 if (!(variables[VARIABLE_LANG] = strdup("C"))) {
169 for (i = 0; i < _VARIABLE_MAX; i++) {
174 if (setenv(variable_names[i], variables[i], 1) < 0) {
183 for (i = 0; i < _VARIABLE_MAX; i++)