1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "conf-files.h"
10 #include "path-lookup.h"
13 static int environment_dirs(char ***ret
) {
14 _cleanup_strv_free_
char **dirs
= NULL
;
15 _cleanup_free_
char *c
= NULL
;
18 dirs
= strv_new(CONF_PATHS_USR("environment.d"), NULL
);
22 /* ~/.config/systemd/environment.d */
23 r
= sd_path_lookup(SD_PATH_USER_CONFIGURATION
, "environment.d", &c
);
27 r
= strv_extend_front(&dirs
, c
);
32 _cleanup_free_
char *t
= NULL
;
34 t
= strv_join(dirs
, "\n\t");
35 log_debug("Looking for environment.d files in (higher priority first):\n\t%s", strna(t
));
38 *ret
= TAKE_PTR(dirs
);
42 static int load_and_print(void) {
43 _cleanup_strv_free_
char **dirs
= NULL
, **files
= NULL
, **env
= NULL
;
47 r
= environment_dirs(&dirs
);
51 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char **) dirs
);
55 /* This will mutate the existing environment, based on the presumption
56 * that in case of failure, a partial update is better than none. */
58 STRV_FOREACH(i
, files
) {
59 log_debug("Reading %s…", *i
);
61 r
= merge_env_file(&env
, NULL
, *i
);
66 STRV_FOREACH(i
, env
) {
68 _cleanup_free_
char *q
= NULL
;
73 q
= shell_maybe_quote(t
+ 1, 0);
77 printf("%.*s=%s\n", (int) (t
- *i
), *i
, q
);
83 int main(int argc
, char *argv
[]) {
86 log_parse_environment();
90 log_error("This program takes no arguments.");
96 log_error_errno(r
, "Failed to load environment.d: %m");
98 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;