chiark / gitweb /
systemctl: make condrestart equivalent to try-restart
[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         VARIABLE_LANG,
32         VARIABLE_LC_CTYPE,
33         VARIABLE_LC_NUMERIC,
34         VARIABLE_LC_TIME,
35         VARIABLE_LC_COLLATE,
36         VARIABLE_LC_MONETARY,
37         VARIABLE_LC_MESSAGES,
38         VARIABLE_LC_ALL,
39         VARIABLE_LC_PAPER,
40         VARIABLE_LC_NAME,
41         VARIABLE_LC_ADDRESS,
42         VARIABLE_LC_TELEPHONE,
43         VARIABLE_LC_MEASUREMENT,
44         VARIABLE_LC_IDENTIFICATION,
45         _VARIABLE_MAX
46 };
47
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"
63 };
64
65 int locale_setup(void) {
66         char *variables[_VARIABLE_MAX];
67         int r, i;
68
69         zero(variables);
70
71 #ifdef TARGET_FEDORA
72         if ((r = parse_env_file("/etc/sysconfig/i18n", NEWLINE,
73                                 "LANG", &variables[VARIABLE_LANG],
74                                 NULL)) < 0) {
75
76                 if (r != -ENOENT)
77                         log_warning("Failed to read /etc/sysconfig/i18n: %s", strerror(-r));
78         }
79 #elif defined(TARGET_ARCH)
80         if ((r = parse_env_file("/etc/rc.conf", NEWLINE,
81                                 "LOCALE", &variables[VARIABLE_LANG],
82                                 NULL)) < 0) {
83
84                 if (r != -ENOENT)
85                         log_warning("Failed to read /etc/rc.conf: %s", strerror(-r));
86         }
87 #endif
88
89         /* Override distribution-specific options with the
90          * distribution-independent configuration */
91         if ((r = parse_env_file("/etc/locale", NEWLINE,
92                                 "LANG",              &variables[VARIABLE_LANG],
93                                 "LC_CTYPE",          &variables[VARIABLE_LC_CTYPE],
94                                 "LC_NUMERIC",        &variables[VARIABLE_LC_NUMERIC],
95                                 "LC_TIME",           &variables[VARIABLE_LC_TIME],
96                                 "LC_COLLATE",        &variables[VARIABLE_LC_COLLATE],
97                                 "LC_MONETARY",       &variables[VARIABLE_LC_MONETARY],
98                                 "LC_MESSAGES",       &variables[VARIABLE_LC_MESSAGES],
99                                 "LC_ALL",            &variables[VARIABLE_LC_ALL],
100                                 "LC_PAPER",          &variables[VARIABLE_LC_PAPER],
101                                 "LC_NAME",           &variables[VARIABLE_LC_NAME],
102                                 "LC_ADDRESS",        &variables[VARIABLE_LC_ADDRESS],
103                                 "LC_TELEPHONE",      &variables[VARIABLE_LC_TELEPHONE],
104                                 "LC_MEASUREMENT",    &variables[VARIABLE_LC_MEASUREMENT],
105                                 "LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
106                                 NULL)) < 0) {
107
108                 if (r != -ENOENT)
109                         log_warning("Failed to read /etc/locale: %s", strerror(-r));
110         }
111
112         if ((r = parse_env_file("/proc/cmdline", WHITESPACE,
113 #ifdef TARGET_FEDORA
114                                 "LANG",                     &variables[VARIABLE_LANG],
115 #endif
116                                 "locale.LANG",              &variables[VARIABLE_LANG],
117                                 "locale.LC_CTYPE",          &variables[VARIABLE_LC_CTYPE],
118                                 "locale.LC_NUMERIC",        &variables[VARIABLE_LC_NUMERIC],
119                                 "locale.LC_TIME",           &variables[VARIABLE_LC_TIME],
120                                 "locale.LC_COLLATE",        &variables[VARIABLE_LC_COLLATE],
121                                 "locale.LC_MONETARY",       &variables[VARIABLE_LC_MONETARY],
122                                 "locale.LC_MESSAGES",       &variables[VARIABLE_LC_MESSAGES],
123                                 "locale.LC_ALL",            &variables[VARIABLE_LC_ALL],
124                                 "locale.LC_PAPER",          &variables[VARIABLE_LC_PAPER],
125                                 "locale.LC_NAME",           &variables[VARIABLE_LC_NAME],
126                                 "locale.LC_ADDRESS",        &variables[VARIABLE_LC_ADDRESS],
127                                 "locale.LC_TELEPHONE",      &variables[VARIABLE_LC_TELEPHONE],
128                                 "locale.LC_MEASUREMENT",    &variables[VARIABLE_LC_MEASUREMENT],
129                                 "locale.LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
130                                 NULL)) < 0) {
131
132                 if (r != -ENOENT)
133                         log_warning("Failed to read /proc/cmdline: %s", strerror(-r));
134         }
135
136         if (!variables[VARIABLE_LANG]) {
137                 if (!(variables[VARIABLE_LANG] = strdup("C"))) {
138                         r = -ENOMEM;
139                         goto finish;
140                 }
141         }
142
143         for (i = 0; i < _VARIABLE_MAX; i++) {
144
145                 if (!variables[i])
146                         continue;
147
148                 if (setenv(variable_names[i], variables[i], 1) < 0) {
149                         r = -errno;
150                         goto finish;
151                 }
152
153         }
154         r = 0;
155
156 finish:
157         for (i = 0; i < _VARIABLE_MAX; i++)
158                 free(variables[i]);
159
160         return r;
161 }