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
;
46 r
= environment_dirs(&dirs
);
50 r
= conf_files_list_strv(&files
, ".conf", NULL
, 0, (const char **) dirs
);
54 /* This will mutate the existing environment, based on the presumption
55 * that in case of failure, a partial update is better than none. */
57 STRV_FOREACH(i
, files
) {
58 log_debug("Reading %s…", *i
);
60 r
= merge_env_file(&env
, NULL
, *i
);
65 STRV_FOREACH(i
, env
) {
67 _cleanup_free_
char *q
= NULL
;
72 q
= shell_maybe_quote(t
+ 1, 0);
76 printf("%.*s=%s\n", (int) (t
- *i
), *i
, q
);
82 int main(int argc
, char *argv
[]) {
85 log_parse_environment();
89 log_error("This program takes no arguments.");
95 log_error_errno(r
, "Failed to load environment.d: %m");
97 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;