1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "alloc-util.h"
13 #include "string-util.h"
14 #include "terminal-util.h"
17 static const char *arg_suffix
= NULL
;
19 static const char* const path_table
[_SD_PATH_MAX
] = {
20 [SD_PATH_TEMPORARY
] = "temporary",
21 [SD_PATH_TEMPORARY_LARGE
] = "temporary-large",
22 [SD_PATH_SYSTEM_BINARIES
] = "system-binaries",
23 [SD_PATH_SYSTEM_INCLUDE
] = "system-include",
24 [SD_PATH_SYSTEM_LIBRARY_PRIVATE
] = "system-library-private",
25 [SD_PATH_SYSTEM_LIBRARY_ARCH
] = "system-library-arch",
26 [SD_PATH_SYSTEM_SHARED
] = "system-shared",
27 [SD_PATH_SYSTEM_CONFIGURATION_FACTORY
] = "system-configuration-factory",
28 [SD_PATH_SYSTEM_STATE_FACTORY
] = "system-state-factory",
29 [SD_PATH_SYSTEM_CONFIGURATION
] = "system-configuration",
30 [SD_PATH_SYSTEM_RUNTIME
] = "system-runtime",
31 [SD_PATH_SYSTEM_RUNTIME_LOGS
] = "system-runtime-logs",
32 [SD_PATH_SYSTEM_STATE_PRIVATE
] = "system-state-private",
33 [SD_PATH_SYSTEM_STATE_LOGS
] = "system-state-logs",
34 [SD_PATH_SYSTEM_STATE_CACHE
] = "system-state-cache",
35 [SD_PATH_SYSTEM_STATE_SPOOL
] = "system-state-spool",
36 [SD_PATH_USER_BINARIES
] = "user-binaries",
37 [SD_PATH_USER_LIBRARY_PRIVATE
] = "user-library-private",
38 [SD_PATH_USER_LIBRARY_ARCH
] = "user-library-arch",
39 [SD_PATH_USER_SHARED
] = "user-shared",
40 [SD_PATH_USER_CONFIGURATION
] = "user-configuration",
41 [SD_PATH_USER_RUNTIME
] = "user-runtime",
42 [SD_PATH_USER_STATE_CACHE
] = "user-state-cache",
43 [SD_PATH_USER
] = "user",
44 [SD_PATH_USER_DOCUMENTS
] = "user-documents",
45 [SD_PATH_USER_MUSIC
] = "user-music",
46 [SD_PATH_USER_PICTURES
] = "user-pictures",
47 [SD_PATH_USER_VIDEOS
] = "user-videos",
48 [SD_PATH_USER_DOWNLOAD
] = "user-download",
49 [SD_PATH_USER_PUBLIC
] = "user-public",
50 [SD_PATH_USER_TEMPLATES
] = "user-templates",
51 [SD_PATH_USER_DESKTOP
] = "user-desktop",
52 [SD_PATH_SEARCH_BINARIES
] = "search-binaries",
53 [SD_PATH_SEARCH_BINARIES_DEFAULT
] = "search-binaries-default",
54 [SD_PATH_SEARCH_LIBRARY_PRIVATE
] = "search-library-private",
55 [SD_PATH_SEARCH_LIBRARY_ARCH
] = "search-library-arch",
56 [SD_PATH_SEARCH_SHARED
] = "search-shared",
57 [SD_PATH_SEARCH_CONFIGURATION_FACTORY
] = "search-configuration-factory",
58 [SD_PATH_SEARCH_STATE_FACTORY
] = "search-state-factory",
59 [SD_PATH_SEARCH_CONFIGURATION
] = "search-configuration",
62 static int list_homes(void) {
66 for (i
= 0; i
< ELEMENTSOF(path_table
); i
++) {
67 _cleanup_free_
char *p
= NULL
;
70 q
= sd_path_home(i
, arg_suffix
, &p
);
74 log_error_errno(r
, "Failed to query %s: %m", path_table
[i
]);
79 printf("%s: %s\n", path_table
[i
], p
);
85 static int print_home(const char *n
) {
89 for (i
= 0; i
< ELEMENTSOF(path_table
); i
++) {
90 if (streq(path_table
[i
], n
)) {
91 _cleanup_free_
char *p
= NULL
;
93 r
= sd_path_home(i
, arg_suffix
, &p
);
95 return log_error_errno(r
, "Failed to query %s: %m", n
);
102 log_error("Path %s not known.", n
);
106 static int help(void) {
107 _cleanup_free_
char *link
= NULL
;
110 r
= terminal_urlify_man("systemd-path", "1", &link
);
114 printf("%s [OPTIONS...] [NAME...]\n\n"
115 "Show system and user paths.\n\n"
116 " -h --help Show this help\n"
117 " --version Show package version\n"
118 " --suffix=SUFFIX Suffix to append to paths\n"
119 "\nSee the %s for details.\n"
120 , program_invocation_short_name
127 static int parse_argv(int argc
, char *argv
[]) {
134 static const struct option options
[] = {
135 { "help", no_argument
, NULL
, 'h' },
136 { "version", no_argument
, NULL
, ARG_VERSION
},
137 { "suffix", required_argument
, NULL
, ARG_SUFFIX
},
146 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
164 assert_not_reached("Unhandled option");
170 int main(int argc
, char* argv
[]) {
173 log_parse_environment();
176 r
= parse_argv(argc
, argv
);
183 for (i
= optind
; i
< argc
; i
++) {
184 q
= print_home(argv
[i
]);
192 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;