]> git.ipfire.org Git - thirdparty/systemd.git/blame - 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
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 22#include <errno.h>
07630cea 23#include <stdlib.h>
72bca11b 24
bcd8e6d1 25#include "env-util.h"
07630cea 26#include "fileio.h"
a3428668 27#include "locale-util.h"
07630cea
LP
28#include "string-util.h"
29#include "strv.h"
30#include "util.h"
31#include "virt.h"
32#include "locale-setup.h"
72bca11b 33
e21fea24 34int locale_setup(char ***environment) {
bcd8e6d1 35 char **add;
a3428668 36 char *variables[_VARIABLE_LC_MAX] = {};
d885ac66 37 int r = 0, i;
72bca11b 38
75f86906 39 if (detect_container() <= 0) {
39d99a07 40 r = parse_env_file("/proc/cmdline", WHITESPACE,
39d99a07
LP
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)
da927ba9 58 log_warning_errno(r, "Failed to read /proc/cmdline: %m");
39d99a07 59 }
ce8a6aa1
LP
60
61 /* Hmm, nothing set on the kernel cmd line? Then let's
fd5bf055 62 * try /etc/locale.conf */
39d99a07
LP
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)
da927ba9 82 log_warning_errno(r, "Failed to read /etc/locale.conf: %m");
ce8a6aa1
LP
83 }
84
bcd8e6d1 85 add = NULL;
a3428668 86 for (i = 0; i < _VARIABLE_LC_MAX; i++) {
bcd8e6d1
LP
87 char *s;
88
e21fea24
KS
89 if (!variables[i])
90 continue;
91
a3428668 92 s = strjoin(locale_variable_to_string(i), "=", variables[i], NULL);
bcd8e6d1
LP
93 if (!s) {
94 r = -ENOMEM;
95 goto finish;
96 }
97
6e18964d 98 if (strv_consume(&add, s) < 0) {
bcd8e6d1
LP
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) {
e21fea24
KS
109 r = -ENOMEM;
110 goto finish;
111 }
112
bcd8e6d1
LP
113 strv_free(*environment);
114 *environment = e;
72bca11b 115 }
8780d48d 116
72bca11b
LP
117 r = 0;
118
119finish:
bcd8e6d1
LP
120 strv_free(add);
121
a3428668 122 for (i = 0; i < _VARIABLE_LC_MAX; i++)
72bca11b
LP
123 free(variables[i]);
124
125 return r;
126}