]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/locale-setup.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / core / locale-setup.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <stdlib.h>
23
24 #include "env-util.h"
25 #include "fileio.h"
26 #include "locale-setup.h"
27 #include "locale-util.h"
28 #include "string-util.h"
29 #include "strv.h"
30 #include "util.h"
31 #include "virt.h"
32
33 int locale_setup(char ***environment) {
34 char **add;
35 char *variables[_VARIABLE_LC_MAX] = {};
36 int r = 0, i;
37
38 if (detect_container() <= 0) {
39 r = parse_env_file("/proc/cmdline", WHITESPACE,
40 "locale.LANG", &variables[VARIABLE_LANG],
41 "locale.LANGUAGE", &variables[VARIABLE_LANGUAGE],
42 "locale.LC_CTYPE", &variables[VARIABLE_LC_CTYPE],
43 "locale.LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC],
44 "locale.LC_TIME", &variables[VARIABLE_LC_TIME],
45 "locale.LC_COLLATE", &variables[VARIABLE_LC_COLLATE],
46 "locale.LC_MONETARY", &variables[VARIABLE_LC_MONETARY],
47 "locale.LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES],
48 "locale.LC_PAPER", &variables[VARIABLE_LC_PAPER],
49 "locale.LC_NAME", &variables[VARIABLE_LC_NAME],
50 "locale.LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS],
51 "locale.LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE],
52 "locale.LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT],
53 "locale.LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
54 NULL);
55
56 if (r < 0 && r != -ENOENT)
57 log_warning_errno(r, "Failed to read /proc/cmdline: %m");
58 }
59
60 /* Hmm, nothing set on the kernel cmd line? Then let's
61 * try /etc/locale.conf */
62 if (r <= 0) {
63 r = parse_env_file("/etc/locale.conf", NEWLINE,
64 "LANG", &variables[VARIABLE_LANG],
65 "LANGUAGE", &variables[VARIABLE_LANGUAGE],
66 "LC_CTYPE", &variables[VARIABLE_LC_CTYPE],
67 "LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC],
68 "LC_TIME", &variables[VARIABLE_LC_TIME],
69 "LC_COLLATE", &variables[VARIABLE_LC_COLLATE],
70 "LC_MONETARY", &variables[VARIABLE_LC_MONETARY],
71 "LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES],
72 "LC_PAPER", &variables[VARIABLE_LC_PAPER],
73 "LC_NAME", &variables[VARIABLE_LC_NAME],
74 "LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS],
75 "LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE],
76 "LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT],
77 "LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION],
78 NULL);
79
80 if (r < 0 && r != -ENOENT)
81 log_warning_errno(r, "Failed to read /etc/locale.conf: %m");
82 }
83
84 add = NULL;
85 for (i = 0; i < _VARIABLE_LC_MAX; i++) {
86 char *s;
87
88 if (!variables[i])
89 continue;
90
91 s = strjoin(locale_variable_to_string(i), "=", variables[i]);
92 if (!s) {
93 r = -ENOMEM;
94 goto finish;
95 }
96
97 if (strv_consume(&add, s) < 0) {
98 r = -ENOMEM;
99 goto finish;
100 }
101 }
102
103 if (!strv_isempty(add)) {
104 char **e;
105
106 e = strv_env_merge(2, *environment, add);
107 if (!e) {
108 r = -ENOMEM;
109 goto finish;
110 }
111
112 strv_free(*environment);
113 *environment = e;
114 }
115
116 r = 0;
117
118 finish:
119 strv_free(add);
120
121 for (i = 0; i < _VARIABLE_LC_MAX; i++)
122 free(variables[i]);
123
124 return r;
125 }