]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/locale-setup.c
vconsole-setup: ignore empty strings like FOO="", imported from config files
[thirdparty/systemd.git] / src / locale-setup.c
CommitLineData
72bca11b
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20***/
21
22#include <string.h>
23#include <stdlib.h>
24#include <errno.h>
25
26#include "locale-setup.h"
27#include "util.h"
28#include "macro.h"
29
30enum {
97e3d13f
LP
31 /* We don't list LC_ALL here on purpose. People should be
32 * using LANG instead. */
33
72bca11b
LP
34 VARIABLE_LANG,
35 VARIABLE_LC_CTYPE,
36 VARIABLE_LC_NUMERIC,
37 VARIABLE_LC_TIME,
38 VARIABLE_LC_COLLATE,
39 VARIABLE_LC_MONETARY,
40 VARIABLE_LC_MESSAGES,
72bca11b
LP
41 VARIABLE_LC_PAPER,
42 VARIABLE_LC_NAME,
43 VARIABLE_LC_ADDRESS,
44 VARIABLE_LC_TELEPHONE,
45 VARIABLE_LC_MEASUREMENT,
46 VARIABLE_LC_IDENTIFICATION,
47 _VARIABLE_MAX
48};
49
50static 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",
72bca11b
LP
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"
64};
65
66int locale_setup(void) {
67 char *variables[_VARIABLE_MAX];
68 int r, i;
69
70 zero(variables);
71
ce8a6aa1
LP
72 if ((r = parse_env_file("/proc/cmdline", WHITESPACE,
73#ifdef TARGET_FEDORA
74 "LANG", &variables[VARIABLE_LANG],
75#endif
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],
89 NULL)) < 0) {
90
91 if (r != -ENOENT)
92 log_warning("Failed to read /proc/cmdline: %s", strerror(-r));
93 }
94
95 /* Hmm, nothing set on the kernel cmd line? Then let's
96 * try /etc/locale */
ce8a6aa1
LP
97 if (r <= 0 &&
98 (r = parse_env_file("/etc/locale", NEWLINE,
99 "LANG", &variables[VARIABLE_LANG],
100 "LC_CTYPE", &variables[VARIABLE_LC_CTYPE],
101 "LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC],
102 "LC_TIME", &variables[VARIABLE_LC_TIME],
103 "LC_COLLATE", &variables[VARIABLE_LC_COLLATE],
104 "LC_MONETARY", &variables[VARIABLE_LC_MONETARY],
105 "LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES],
106 "LC_PAPER", &variables[VARIABLE_LC_PAPER],
107 "LC_NAME", &variables[VARIABLE_LC_NAME],
108 "LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS],
109 "LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE],
110 "LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT],
111 "LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
112 NULL)) < 0) {
113
114 if (r != -ENOENT)
115 log_warning("Failed to read /etc/locale: %s", strerror(-r));
116 }
117
72bca11b 118#ifdef TARGET_FEDORA
ce8a6aa1
LP
119 if (r <= 0 &&
120 (r = parse_env_file("/etc/sysconfig/i18n", NEWLINE,
72bca11b
LP
121 "LANG", &variables[VARIABLE_LANG],
122 NULL)) < 0) {
123
124 if (r != -ENOENT)
125 log_warning("Failed to read /etc/sysconfig/i18n: %s", strerror(-r));
126 }
653ab83b 127
03aeb5be
KS
128#elif defined(TARGET_SUSE)
129 if (r <= 0 &&
130 (r = parse_env_file("/etc/sysconfig/language", NEWLINE,
131 "RC_LANG", &variables[VARIABLE_LANG],
132 NULL)) < 0) {
133
134 if (r != -ENOENT)
135 log_warning("Failed to read /etc/sysconfig/language: %s", strerror(-r));
136 }
137
f6943caa 138#elif defined(TARGET_DEBIAN)
7472744b
MB
139 if (r <= 0 &&
140 (r = parse_env_file("/etc/default/locale", NEWLINE,
141 "LANG", &variables[VARIABLE_LANG],
142 NULL)) < 0) {
143
144 if (r != -ENOENT)
145 log_warning("Failed to read /etc/default/locale: %s", strerror(-r));
146 }
147
f2fcd59f 148#elif defined(TARGET_ARCH)
ce8a6aa1
LP
149 if (r <= 0 &&
150 (r = parse_env_file("/etc/rc.conf", NEWLINE,
97e3d13f
LP
151 "LOCALE", &variables[VARIABLE_LANG],
152 NULL)) < 0) {
f2fcd59f 153
97e3d13f
LP
154 if (r != -ENOENT)
155 log_warning("Failed to read /etc/rc.conf: %s", strerror(-r));
156 }
653ab83b 157
918f4c69
GSB
158#elif defined(TARGET_GENTOO)
159 /* Gentoo's openrc expects locale variables in /etc/env.d/
160 * These files are later compiled by env-update into shell
161 * export commands at /etc/profile.env, with variables being
162 * exported by openrc's runscript (so /etc/init.d/)
163 */
ce8a6aa1
LP
164 if (r <= 0 &&
165 (r = parse_env_file("/etc/profile.env", NEWLINE,
918f4c69
GSB
166 "export LANG", &variables[VARIABLE_LANG],
167 "export LC_CTYPE", &variables[VARIABLE_LC_CTYPE],
168 "export LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC],
169 "export LC_TIME", &variables[VARIABLE_LC_TIME],
170 "export LC_COLLATE", &variables[VARIABLE_LC_COLLATE],
171 "export LC_MONETARY", &variables[VARIABLE_LC_MONETARY],
172 "export LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES],
918f4c69
GSB
173 "export LC_PAPER", &variables[VARIABLE_LC_PAPER],
174 "export LC_NAME", &variables[VARIABLE_LC_NAME],
175 "export LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS],
176 "export LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE],
177 "export LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT],
178 "export LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
179 NULL)) < 0) {
180
181 if (r != -ENOENT)
182 log_warning("Failed to read /etc/profile.env: %s", strerror(-r));
183 }
72bca11b
LP
184#endif
185
72bca11b
LP
186 if (!variables[VARIABLE_LANG]) {
187 if (!(variables[VARIABLE_LANG] = strdup("C"))) {
188 r = -ENOMEM;
189 goto finish;
190 }
191 }
192
193 for (i = 0; i < _VARIABLE_MAX; i++) {
194
8780d48d
LP
195 if (variables[i]) {
196 if (setenv(variable_names[i], variables[i], 1) < 0) {
197 r = -errno;
198 goto finish;
199 }
200 } else
201 unsetenv(variable_names[i]);
72bca11b 202 }
8780d48d 203
72bca11b
LP
204 r = 0;
205
206finish:
207 for (i = 0; i < _VARIABLE_MAX; i++)
208 free(variables[i]);
209
210 return r;
211}