chiark / gitweb /
e993c573219c17888bb522e8f37359c397066b2c
[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 #include "fileio.h"
31 #include "strv.h"
32 #include "env-util.h"
33 #include "locale-util.h"
34
35 int locale_setup(char ***environment) {
36         char **add;
37         char *variables[_VARIABLE_LC_MAX] = {};
38         int r = 0, i;
39
40         if (detect_container(NULL) <= 0) {
41                 r = parse_env_file("/proc/cmdline", WHITESPACE,
42                                    "locale.LANG",              &variables[VARIABLE_LANG],
43                                    "locale.LANGUAGE",          &variables[VARIABLE_LANGUAGE],
44                                    "locale.LC_CTYPE",          &variables[VARIABLE_LC_CTYPE],
45                                    "locale.LC_NUMERIC",        &variables[VARIABLE_LC_NUMERIC],
46                                    "locale.LC_TIME",           &variables[VARIABLE_LC_TIME],
47                                    "locale.LC_COLLATE",        &variables[VARIABLE_LC_COLLATE],
48                                    "locale.LC_MONETARY",       &variables[VARIABLE_LC_MONETARY],
49                                    "locale.LC_MESSAGES",       &variables[VARIABLE_LC_MESSAGES],
50                                    "locale.LC_PAPER",          &variables[VARIABLE_LC_PAPER],
51                                    "locale.LC_NAME",           &variables[VARIABLE_LC_NAME],
52                                    "locale.LC_ADDRESS",        &variables[VARIABLE_LC_ADDRESS],
53                                    "locale.LC_TELEPHONE",      &variables[VARIABLE_LC_TELEPHONE],
54                                    "locale.LC_MEASUREMENT",    &variables[VARIABLE_LC_MEASUREMENT],
55                                    "locale.LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
56                                    NULL);
57
58                 if (r < 0 && r != -ENOENT)
59                         log_warning_errno(r, "Failed to read /proc/cmdline: %m");
60         }
61
62         /* Hmm, nothing set on the kernel cmd line? Then let's
63          * try /etc/locale.conf */
64         if (r <= 0) {
65                 r = parse_env_file("/etc/locale.conf", NEWLINE,
66                                    "LANG",              &variables[VARIABLE_LANG],
67                                    "LANGUAGE",          &variables[VARIABLE_LANGUAGE],
68                                    "LC_CTYPE",          &variables[VARIABLE_LC_CTYPE],
69                                    "LC_NUMERIC",        &variables[VARIABLE_LC_NUMERIC],
70                                    "LC_TIME",           &variables[VARIABLE_LC_TIME],
71                                    "LC_COLLATE",        &variables[VARIABLE_LC_COLLATE],
72                                    "LC_MONETARY",       &variables[VARIABLE_LC_MONETARY],
73                                    "LC_MESSAGES",       &variables[VARIABLE_LC_MESSAGES],
74                                    "LC_PAPER",          &variables[VARIABLE_LC_PAPER],
75                                    "LC_NAME",           &variables[VARIABLE_LC_NAME],
76                                    "LC_ADDRESS",        &variables[VARIABLE_LC_ADDRESS],
77                                    "LC_TELEPHONE",      &variables[VARIABLE_LC_TELEPHONE],
78                                    "LC_MEASUREMENT",    &variables[VARIABLE_LC_MEASUREMENT],
79                                    "LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
80                                    NULL);
81
82                 if (r < 0 && r != -ENOENT)
83                         log_warning_errno(r, "Failed to read /etc/locale.conf: %m");
84         }
85
86         add = NULL;
87         for (i = 0; i < _VARIABLE_LC_MAX; i++) {
88                 char *s;
89
90                 if (!variables[i])
91                         continue;
92
93                 s = strjoin(locale_variable_to_string(i), "=", variables[i], NULL);
94                 if (!s) {
95                         r = -ENOMEM;
96                         goto finish;
97                 }
98
99                 if (strv_consume(&add, s) < 0) {
100                         r = -ENOMEM;
101                         goto finish;
102                 }
103         }
104
105         if (!strv_isempty(add)) {
106                 char **e;
107
108                 e = strv_env_merge(2, *environment, add);
109                 if (!e) {
110                         r = -ENOMEM;
111                         goto finish;
112                 }
113
114                 strv_free(*environment);
115                 *environment = e;
116         }
117
118         r = 0;
119
120 finish:
121         strv_free(add);
122
123         for (i = 0; i < _VARIABLE_LC_MAX; i++)
124                 free(variables[i]);
125
126         return r;
127 }