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