chiark / gitweb /
udev: set default rules permissions only at "add" events
[elogind.git] / src / core / 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 Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 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   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser 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 #include "virt.h"
30
31 enum {
32         /* We don't list LC_ALL here on purpose. People should be
33          * using LANG instead. */
34
35         VARIABLE_LANG,
36         VARIABLE_LANGUAGE,
37         VARIABLE_LC_CTYPE,
38         VARIABLE_LC_NUMERIC,
39         VARIABLE_LC_TIME,
40         VARIABLE_LC_COLLATE,
41         VARIABLE_LC_MONETARY,
42         VARIABLE_LC_MESSAGES,
43         VARIABLE_LC_PAPER,
44         VARIABLE_LC_NAME,
45         VARIABLE_LC_ADDRESS,
46         VARIABLE_LC_TELEPHONE,
47         VARIABLE_LC_MEASUREMENT,
48         VARIABLE_LC_IDENTIFICATION,
49         _VARIABLE_MAX
50 };
51
52 static const char * const variable_names[_VARIABLE_MAX] = {
53         [VARIABLE_LANG] = "LANG",
54         [VARIABLE_LANGUAGE] = "LANGUAGE",
55         [VARIABLE_LC_CTYPE] = "LC_CTYPE",
56         [VARIABLE_LC_NUMERIC] = "LC_NUMERIC",
57         [VARIABLE_LC_TIME] = "LC_TIME",
58         [VARIABLE_LC_COLLATE] = "LC_COLLATE",
59         [VARIABLE_LC_MONETARY] = "LC_MONETARY",
60         [VARIABLE_LC_MESSAGES] = "LC_MESSAGES",
61         [VARIABLE_LC_PAPER] = "LC_PAPER",
62         [VARIABLE_LC_NAME] = "LC_NAME",
63         [VARIABLE_LC_ADDRESS] = "LC_ADDRESS",
64         [VARIABLE_LC_TELEPHONE] = "LC_TELEPHONE",
65         [VARIABLE_LC_MEASUREMENT] = "LC_MEASUREMENT",
66         [VARIABLE_LC_IDENTIFICATION] = "LC_IDENTIFICATION"
67 };
68
69 int locale_setup(void) {
70         char *variables[_VARIABLE_MAX];
71         int r = 0, i;
72
73         zero(variables);
74
75         if (detect_container(NULL) <= 0) {
76                 r = parse_env_file("/proc/cmdline", WHITESPACE,
77                                    "locale.LANG",              &variables[VARIABLE_LANG],
78                                    "locale.LANGUAGE",          &variables[VARIABLE_LANGUAGE],
79                                    "locale.LC_CTYPE",          &variables[VARIABLE_LC_CTYPE],
80                                    "locale.LC_NUMERIC",        &variables[VARIABLE_LC_NUMERIC],
81                                    "locale.LC_TIME",           &variables[VARIABLE_LC_TIME],
82                                    "locale.LC_COLLATE",        &variables[VARIABLE_LC_COLLATE],
83                                    "locale.LC_MONETARY",       &variables[VARIABLE_LC_MONETARY],
84                                    "locale.LC_MESSAGES",       &variables[VARIABLE_LC_MESSAGES],
85                                    "locale.LC_PAPER",          &variables[VARIABLE_LC_PAPER],
86                                    "locale.LC_NAME",           &variables[VARIABLE_LC_NAME],
87                                    "locale.LC_ADDRESS",        &variables[VARIABLE_LC_ADDRESS],
88                                    "locale.LC_TELEPHONE",      &variables[VARIABLE_LC_TELEPHONE],
89                                    "locale.LC_MEASUREMENT",    &variables[VARIABLE_LC_MEASUREMENT],
90                                    "locale.LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
91                                    NULL);
92
93                 if (r < 0 && r != -ENOENT)
94                         log_warning("Failed to read /proc/cmdline: %s", strerror(-r));
95         }
96
97         /* Hmm, nothing set on the kernel cmd line? Then let's
98          * try /etc/locale.conf */
99         if (r <= 0) {
100                 r = parse_env_file("/etc/locale.conf", NEWLINE,
101                                    "LANG",              &variables[VARIABLE_LANG],
102                                    "LANGUAGE",          &variables[VARIABLE_LANGUAGE],
103                                    "LC_CTYPE",          &variables[VARIABLE_LC_CTYPE],
104                                    "LC_NUMERIC",        &variables[VARIABLE_LC_NUMERIC],
105                                    "LC_TIME",           &variables[VARIABLE_LC_TIME],
106                                    "LC_COLLATE",        &variables[VARIABLE_LC_COLLATE],
107                                    "LC_MONETARY",       &variables[VARIABLE_LC_MONETARY],
108                                    "LC_MESSAGES",       &variables[VARIABLE_LC_MESSAGES],
109                                    "LC_PAPER",          &variables[VARIABLE_LC_PAPER],
110                                    "LC_NAME",           &variables[VARIABLE_LC_NAME],
111                                    "LC_ADDRESS",        &variables[VARIABLE_LC_ADDRESS],
112                                    "LC_TELEPHONE",      &variables[VARIABLE_LC_TELEPHONE],
113                                    "LC_MEASUREMENT",    &variables[VARIABLE_LC_MEASUREMENT],
114                                    "LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
115                                    NULL);
116
117                 if (r < 0 && r != -ENOENT)
118                         log_warning("Failed to read /etc/locale.conf: %s", strerror(-r));
119         }
120
121         if (!variables[VARIABLE_LANG]) {
122                 variables[VARIABLE_LANG] = strdup("C");
123                 if (!variables[VARIABLE_LANG]) {
124                         r = -ENOMEM;
125                         goto finish;
126                 }
127         }
128
129         for (i = 0; i < _VARIABLE_MAX; i++) {
130                 if (variables[i]) {
131                         if (setenv(variable_names[i], variables[i], 1) < 0) {
132                                 r = -errno;
133                                 goto finish;
134                         }
135                 } else
136                         unsetenv(variable_names[i]);
137         }
138
139         r = 0;
140
141 finish:
142         for (i = 0; i < _VARIABLE_MAX; i++)
143                 free(variables[i]);
144
145         return r;
146 }