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];
72 if ((r = parse_env_file("/proc/cmdline", WHITESPACE,
74 "LANG", &variables[VARIABLE_LANG],
76 "locale.LANG", &variables[VARIABLE_LANG],
77 "locale.LC_CTYPE", &variables[VARIABLE_LC_CTYPE],
78 "locale.LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC],
79 "locale.LC_TIME", &variables[VARIABLE_LC_TIME],
80 "locale.LC_COLLATE", &variables[VARIABLE_LC_COLLATE],
81 "locale.LC_MONETARY", &variables[VARIABLE_LC_MONETARY],
82 "locale.LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES],
83 "locale.LC_PAPER", &variables[VARIABLE_LC_PAPER],
84 "locale.LC_NAME", &variables[VARIABLE_LC_NAME],
85 "locale.LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS],
86 "locale.LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE],
87 "locale.LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT],
88 "locale.LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
92 log_warning("Failed to read /proc/cmdline: %s", strerror(-r));
95 /* Hmm, nothing set on the kernel cmd line? Then let's
99 (r = parse_env_file("/etc/locale", NEWLINE,
100 "LANG", &variables[VARIABLE_LANG],
101 "LC_CTYPE", &variables[VARIABLE_LC_CTYPE],
102 "LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC],
103 "LC_TIME", &variables[VARIABLE_LC_TIME],
104 "LC_COLLATE", &variables[VARIABLE_LC_COLLATE],
105 "LC_MONETARY", &variables[VARIABLE_LC_MONETARY],
106 "LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES],
107 "LC_PAPER", &variables[VARIABLE_LC_PAPER],
108 "LC_NAME", &variables[VARIABLE_LC_NAME],
109 "LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS],
110 "LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE],
111 "LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT],
112 "LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
116 log_warning("Failed to read /etc/locale: %s", strerror(-r));
121 (r = parse_env_file("/etc/sysconfig/i18n", NEWLINE,
122 "LANG", &variables[VARIABLE_LANG],
126 log_warning("Failed to read /etc/sysconfig/i18n: %s", strerror(-r));
128 #elif defined(TARGET_ARCH)
130 (r = parse_env_file("/etc/rc.conf", NEWLINE,
131 "LOCALE", &variables[VARIABLE_LANG],
135 log_warning("Failed to read /etc/rc.conf: %s", strerror(-r));
137 #elif defined(TARGET_GENTOO)
138 /* Gentoo's openrc expects locale variables in /etc/env.d/
139 * These files are later compiled by env-update into shell
140 * export commands at /etc/profile.env, with variables being
141 * exported by openrc's runscript (so /etc/init.d/)
144 (r = parse_env_file("/etc/profile.env", NEWLINE,
145 "export LANG", &variables[VARIABLE_LANG],
146 "export LC_CTYPE", &variables[VARIABLE_LC_CTYPE],
147 "export LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC],
148 "export LC_TIME", &variables[VARIABLE_LC_TIME],
149 "export LC_COLLATE", &variables[VARIABLE_LC_COLLATE],
150 "export LC_MONETARY", &variables[VARIABLE_LC_MONETARY],
151 "export LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES],
152 "export LC_PAPER", &variables[VARIABLE_LC_PAPER],
153 "export LC_NAME", &variables[VARIABLE_LC_NAME],
154 "export LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS],
155 "export LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE],
156 "export LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT],
157 "export LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
161 log_warning("Failed to read /etc/profile.env: %s", strerror(-r));
165 if (!variables[VARIABLE_LANG]) {
166 if (!(variables[VARIABLE_LANG] = strdup("C"))) {
172 for (i = 0; i < _VARIABLE_MAX; i++) {
175 if (setenv(variable_names[i], variables[i], 1) < 0) {
180 unsetenv(variable_names[i]);
186 for (i = 0; i < _VARIABLE_MAX; i++)