]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
9a00f57a | 2 | |
9a00f57a | 3 | #include <errno.h> |
3f6fd1ba LP |
4 | #include <getopt.h> |
5 | #include <stdio.h> | |
9a00f57a | 6 | #include <stdlib.h> |
9a00f57a LP |
7 | |
8 | #include "sd-path.h" | |
3f6fd1ba | 9 | |
b5efdb8a | 10 | #include "alloc-util.h" |
3f6fd1ba | 11 | #include "log.h" |
9a00f57a | 12 | #include "macro.h" |
07630cea | 13 | #include "string-util.h" |
37ec0fdd | 14 | #include "terminal-util.h" |
9a00f57a | 15 | #include "util.h" |
9a00f57a LP |
16 | |
17 | static const char *arg_suffix = NULL; | |
18 | ||
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", | |
e12d446b | 53 | [SD_PATH_SEARCH_BINARIES_DEFAULT] = "search-binaries-default", |
9a00f57a LP |
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", | |
60 | }; | |
61 | ||
9a00f57a LP |
62 | static int list_homes(void) { |
63 | uint64_t i = 0; | |
64 | int r = 0; | |
65 | ||
66 | for (i = 0; i < ELEMENTSOF(path_table); i++) { | |
67 | _cleanup_free_ char *p = NULL; | |
68 | int q; | |
69 | ||
70 | q = sd_path_home(i, arg_suffix, &p); | |
71 | if (q == -ENXIO) | |
72 | continue; | |
73 | if (q < 0) { | |
da927ba9 | 74 | log_error_errno(r, "Failed to query %s: %m", path_table[i]); |
9a00f57a LP |
75 | r = q; |
76 | continue; | |
77 | } | |
78 | ||
79 | printf("%s: %s\n", path_table[i], p); | |
80 | } | |
81 | ||
82 | return r; | |
83 | } | |
84 | ||
85 | static int print_home(const char *n) { | |
86 | uint64_t i = 0; | |
87 | int r; | |
88 | ||
89 | for (i = 0; i < ELEMENTSOF(path_table); i++) { | |
90 | if (streq(path_table[i], n)) { | |
91 | _cleanup_free_ char *p = NULL; | |
92 | ||
93 | r = sd_path_home(i, arg_suffix, &p); | |
f647962d MS |
94 | if (r < 0) |
95 | return log_error_errno(r, "Failed to query %s: %m", n); | |
9a00f57a LP |
96 | |
97 | printf("%s\n", p); | |
98 | return 0; | |
99 | } | |
100 | } | |
101 | ||
102 | log_error("Path %s not known.", n); | |
15411c0c | 103 | return -EOPNOTSUPP; |
9a00f57a LP |
104 | } |
105 | ||
37ec0fdd LP |
106 | static int help(void) { |
107 | _cleanup_free_ char *link = NULL; | |
108 | int r; | |
109 | ||
110 | r = terminal_urlify_man("systemd-path", "1", &link); | |
111 | if (r < 0) | |
112 | return log_oom(); | |
113 | ||
601185b4 ZJS |
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" | |
37ec0fdd LP |
118 | " --suffix=SUFFIX Suffix to append to paths\n" |
119 | "\nSee the %s for details.\n" | |
120 | , program_invocation_short_name | |
121 | , link | |
122 | ); | |
123 | ||
124 | return 0; | |
601185b4 ZJS |
125 | } |
126 | ||
9a00f57a LP |
127 | static int parse_argv(int argc, char *argv[]) { |
128 | ||
129 | enum { | |
130 | ARG_VERSION = 0x100, | |
131 | ARG_SUFFIX, | |
132 | }; | |
133 | ||
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 }, | |
138 | {} | |
139 | }; | |
140 | ||
141 | int c; | |
142 | ||
143 | assert(argc >= 0); | |
144 | assert(argv); | |
145 | ||
601185b4 | 146 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) |
9a00f57a LP |
147 | |
148 | switch (c) { | |
149 | ||
150 | case 'h': | |
37ec0fdd | 151 | return help(); |
9a00f57a LP |
152 | |
153 | case ARG_VERSION: | |
3f6fd1ba | 154 | return version(); |
9a00f57a LP |
155 | |
156 | case ARG_SUFFIX: | |
157 | arg_suffix = optarg; | |
158 | break; | |
159 | ||
160 | case '?': | |
161 | return -EINVAL; | |
162 | ||
163 | default: | |
164 | assert_not_reached("Unhandled option"); | |
165 | } | |
9a00f57a LP |
166 | |
167 | return 1; | |
168 | } | |
169 | ||
170 | int main(int argc, char* argv[]) { | |
171 | int r; | |
172 | ||
173 | log_parse_environment(); | |
174 | log_open(); | |
175 | ||
176 | r = parse_argv(argc, argv); | |
177 | if (r <= 0) | |
178 | goto finish; | |
179 | ||
180 | if (argc > optind) { | |
181 | int i, q; | |
182 | ||
183 | for (i = optind; i < argc; i++) { | |
184 | q = print_home(argv[i]); | |
185 | if (q < 0) | |
186 | r = q; | |
187 | } | |
188 | } else | |
189 | r = list_homes(); | |
190 | ||
9a00f57a LP |
191 | finish: |
192 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
193 | } |