]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/locale-setup.c
util: split out escaping code into escape.[ch]
[thirdparty/systemd.git] / src / core / locale-setup.c
CommitLineData
72bca11b
LP
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
5430f7f2
LP
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
72bca11b
LP
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
5430f7f2 16 Lesser General Public License for more details.
72bca11b 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
72bca11b
LP
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
72bca11b
LP
22#include <stdlib.h>
23#include <errno.h>
24
25#include "locale-setup.h"
26#include "util.h"
5dc4c17f 27#include "virt.h"
a5c32cff 28#include "fileio.h"
e21fea24 29#include "strv.h"
bcd8e6d1 30#include "env-util.h"
a3428668 31#include "locale-util.h"
72bca11b 32
e21fea24 33int locale_setup(char ***environment) {
bcd8e6d1 34 char **add;
a3428668 35 char *variables[_VARIABLE_LC_MAX] = {};
d885ac66 36 int r = 0, i;
72bca11b 37
75f86906 38 if (detect_container() <= 0) {
39d99a07 39 r = parse_env_file("/proc/cmdline", WHITESPACE,
39d99a07
LP
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)
da927ba9 57 log_warning_errno(r, "Failed to read /proc/cmdline: %m");
39d99a07 58 }
ce8a6aa1
LP
59
60 /* Hmm, nothing set on the kernel cmd line? Then let's
fd5bf055 61 * try /etc/locale.conf */
39d99a07
LP
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)
da927ba9 81 log_warning_errno(r, "Failed to read /etc/locale.conf: %m");
ce8a6aa1
LP
82 }
83
bcd8e6d1 84 add = NULL;
a3428668 85 for (i = 0; i < _VARIABLE_LC_MAX; i++) {
bcd8e6d1
LP
86 char *s;
87
e21fea24
KS
88 if (!variables[i])
89 continue;
90
a3428668 91 s = strjoin(locale_variable_to_string(i), "=", variables[i], NULL);
bcd8e6d1
LP
92 if (!s) {
93 r = -ENOMEM;
94 goto finish;
95 }
96
6e18964d 97 if (strv_consume(&add, s) < 0) {
bcd8e6d1
LP
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) {
e21fea24
KS
108 r = -ENOMEM;
109 goto finish;
110 }
111
bcd8e6d1
LP
112 strv_free(*environment);
113 *environment = e;
72bca11b 114 }
8780d48d 115
72bca11b
LP
116 r = 0;
117
118finish:
bcd8e6d1
LP
119 strv_free(add);
120
a3428668 121 for (i = 0; i < _VARIABLE_LC_MAX; i++)
72bca11b
LP
122 free(variables[i]);
123
124 return r;
125}