chiark / gitweb /
locale: unset locale vars that are not set any longer
[elogind.git] / src / locale-setup.c
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
30 enum {
31         /* We don't list LC_ALL here on purpose. People should be
32          * using LANG instead. */
33
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,
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
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"
64 };
65
66 int locale_setup(void) {
67         char *variables[_VARIABLE_MAX];
68         int r, i;
69
70         zero(variables);
71
72 #ifdef TARGET_FEDORA
73         if ((r = parse_env_file("/etc/sysconfig/i18n", NEWLINE,
74                                 "LANG", &variables[VARIABLE_LANG],
75                                 NULL)) < 0) {
76
77                 if (r != -ENOENT)
78                         log_warning("Failed to read /etc/sysconfig/i18n: %s", strerror(-r));
79         }
80 #elif defined(TARGET_ARCH)
81         if ((r = parse_env_file("/etc/rc.conf", NEWLINE,
82                                 "LOCALE", &variables[VARIABLE_LANG],
83                                 NULL)) < 0) {
84
85                 if (r != -ENOENT)
86                         log_warning("Failed to read /etc/rc.conf: %s", strerror(-r));
87         }
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/)
93          */
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],
108                                 NULL)) < 0) {
109
110                 if (r != -ENOENT)
111                         log_warning("Failed to read /etc/profile.env: %s", strerror(-r));
112         }
113 #endif
114
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],
131                                 NULL)) < 0) {
132
133                 if (r != -ENOENT)
134                         log_warning("Failed to read /etc/locale: %s", strerror(-r));
135         }
136
137         if ((r = parse_env_file("/proc/cmdline", WHITESPACE,
138 #ifdef TARGET_FEDORA
139                                 "LANG",                     &variables[VARIABLE_LANG],
140 #endif
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],
154                                 NULL)) < 0) {
155
156                 if (r != -ENOENT)
157                         log_warning("Failed to read /proc/cmdline: %s", strerror(-r));
158         }
159
160         if (!variables[VARIABLE_LANG]) {
161                 if (!(variables[VARIABLE_LANG] = strdup("C"))) {
162                         r = -ENOMEM;
163                         goto finish;
164                 }
165         }
166
167         for (i = 0; i < _VARIABLE_MAX; i++) {
168
169                 if (variables[i]) {
170                         if (setenv(variable_names[i], variables[i], 1) < 0) {
171                                 r = -errno;
172                                 goto finish;
173                         }
174                 } else
175                         unsetenv(variable_names[i]);
176         }
177
178         r = 0;
179
180 finish:
181         for (i = 0; i < _VARIABLE_MAX; i++)
182                 free(variables[i]);
183
184         return r;
185 }