]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
9a00f57a LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2014 Lennart Poettering | |
9a00f57a LP |
6 | ***/ |
7 | ||
9a00f57a | 8 | #include <errno.h> |
3f6fd1ba LP |
9 | #include <getopt.h> |
10 | #include <stdio.h> | |
9a00f57a | 11 | #include <stdlib.h> |
9a00f57a LP |
12 | |
13 | #include "sd-path.h" | |
3f6fd1ba | 14 | |
b5efdb8a | 15 | #include "alloc-util.h" |
3f6fd1ba | 16 | #include "log.h" |
9a00f57a | 17 | #include "macro.h" |
07630cea | 18 | #include "string-util.h" |
9a00f57a | 19 | #include "util.h" |
9a00f57a LP |
20 | |
21 | static const char *arg_suffix = NULL; | |
22 | ||
23 | static const char* const path_table[_SD_PATH_MAX] = { | |
24 | [SD_PATH_TEMPORARY] = "temporary", | |
25 | [SD_PATH_TEMPORARY_LARGE] = "temporary-large", | |
26 | [SD_PATH_SYSTEM_BINARIES] = "system-binaries", | |
27 | [SD_PATH_SYSTEM_INCLUDE] = "system-include", | |
28 | [SD_PATH_SYSTEM_LIBRARY_PRIVATE] = "system-library-private", | |
29 | [SD_PATH_SYSTEM_LIBRARY_ARCH] = "system-library-arch", | |
30 | [SD_PATH_SYSTEM_SHARED] = "system-shared", | |
31 | [SD_PATH_SYSTEM_CONFIGURATION_FACTORY] = "system-configuration-factory", | |
32 | [SD_PATH_SYSTEM_STATE_FACTORY] = "system-state-factory", | |
33 | [SD_PATH_SYSTEM_CONFIGURATION] = "system-configuration", | |
34 | [SD_PATH_SYSTEM_RUNTIME] = "system-runtime", | |
35 | [SD_PATH_SYSTEM_RUNTIME_LOGS] = "system-runtime-logs", | |
36 | [SD_PATH_SYSTEM_STATE_PRIVATE] = "system-state-private", | |
37 | [SD_PATH_SYSTEM_STATE_LOGS] = "system-state-logs", | |
38 | [SD_PATH_SYSTEM_STATE_CACHE] = "system-state-cache", | |
39 | [SD_PATH_SYSTEM_STATE_SPOOL] = "system-state-spool", | |
40 | [SD_PATH_USER_BINARIES] = "user-binaries", | |
41 | [SD_PATH_USER_LIBRARY_PRIVATE] = "user-library-private", | |
42 | [SD_PATH_USER_LIBRARY_ARCH] = "user-library-arch", | |
43 | [SD_PATH_USER_SHARED] = "user-shared", | |
44 | [SD_PATH_USER_CONFIGURATION] = "user-configuration", | |
45 | [SD_PATH_USER_RUNTIME] = "user-runtime", | |
46 | [SD_PATH_USER_STATE_CACHE] = "user-state-cache", | |
47 | [SD_PATH_USER] = "user", | |
48 | [SD_PATH_USER_DOCUMENTS] = "user-documents", | |
49 | [SD_PATH_USER_MUSIC] = "user-music", | |
50 | [SD_PATH_USER_PICTURES] = "user-pictures", | |
51 | [SD_PATH_USER_VIDEOS] = "user-videos", | |
52 | [SD_PATH_USER_DOWNLOAD] = "user-download", | |
53 | [SD_PATH_USER_PUBLIC] = "user-public", | |
54 | [SD_PATH_USER_TEMPLATES] = "user-templates", | |
55 | [SD_PATH_USER_DESKTOP] = "user-desktop", | |
56 | [SD_PATH_SEARCH_BINARIES] = "search-binaries", | |
e12d446b | 57 | [SD_PATH_SEARCH_BINARIES_DEFAULT] = "search-binaries-default", |
9a00f57a LP |
58 | [SD_PATH_SEARCH_LIBRARY_PRIVATE] = "search-library-private", |
59 | [SD_PATH_SEARCH_LIBRARY_ARCH] = "search-library-arch", | |
60 | [SD_PATH_SEARCH_SHARED] = "search-shared", | |
61 | [SD_PATH_SEARCH_CONFIGURATION_FACTORY] = "search-configuration-factory", | |
62 | [SD_PATH_SEARCH_STATE_FACTORY] = "search-state-factory", | |
63 | [SD_PATH_SEARCH_CONFIGURATION] = "search-configuration", | |
64 | }; | |
65 | ||
9a00f57a LP |
66 | static int list_homes(void) { |
67 | uint64_t i = 0; | |
68 | int r = 0; | |
69 | ||
70 | for (i = 0; i < ELEMENTSOF(path_table); i++) { | |
71 | _cleanup_free_ char *p = NULL; | |
72 | int q; | |
73 | ||
74 | q = sd_path_home(i, arg_suffix, &p); | |
75 | if (q == -ENXIO) | |
76 | continue; | |
77 | if (q < 0) { | |
da927ba9 | 78 | log_error_errno(r, "Failed to query %s: %m", path_table[i]); |
9a00f57a LP |
79 | r = q; |
80 | continue; | |
81 | } | |
82 | ||
83 | printf("%s: %s\n", path_table[i], p); | |
84 | } | |
85 | ||
86 | return r; | |
87 | } | |
88 | ||
89 | static int print_home(const char *n) { | |
90 | uint64_t i = 0; | |
91 | int r; | |
92 | ||
93 | for (i = 0; i < ELEMENTSOF(path_table); i++) { | |
94 | if (streq(path_table[i], n)) { | |
95 | _cleanup_free_ char *p = NULL; | |
96 | ||
97 | r = sd_path_home(i, arg_suffix, &p); | |
f647962d MS |
98 | if (r < 0) |
99 | return log_error_errno(r, "Failed to query %s: %m", n); | |
9a00f57a LP |
100 | |
101 | printf("%s\n", p); | |
102 | return 0; | |
103 | } | |
104 | } | |
105 | ||
106 | log_error("Path %s not known.", n); | |
15411c0c | 107 | return -EOPNOTSUPP; |
9a00f57a LP |
108 | } |
109 | ||
601185b4 ZJS |
110 | static void help(void) { |
111 | printf("%s [OPTIONS...] [NAME...]\n\n" | |
112 | "Show system and user paths.\n\n" | |
113 | " -h --help Show this help\n" | |
114 | " --version Show package version\n" | |
115 | " --suffix=SUFFIX Suffix to append to paths\n", | |
116 | program_invocation_short_name); | |
117 | } | |
118 | ||
9a00f57a LP |
119 | static int parse_argv(int argc, char *argv[]) { |
120 | ||
121 | enum { | |
122 | ARG_VERSION = 0x100, | |
123 | ARG_SUFFIX, | |
124 | }; | |
125 | ||
126 | static const struct option options[] = { | |
127 | { "help", no_argument, NULL, 'h' }, | |
128 | { "version", no_argument, NULL, ARG_VERSION }, | |
129 | { "suffix", required_argument, NULL, ARG_SUFFIX }, | |
130 | {} | |
131 | }; | |
132 | ||
133 | int c; | |
134 | ||
135 | assert(argc >= 0); | |
136 | assert(argv); | |
137 | ||
601185b4 | 138 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) |
9a00f57a LP |
139 | |
140 | switch (c) { | |
141 | ||
142 | case 'h': | |
601185b4 ZJS |
143 | help(); |
144 | return 0; | |
9a00f57a LP |
145 | |
146 | case ARG_VERSION: | |
3f6fd1ba | 147 | return version(); |
9a00f57a LP |
148 | |
149 | case ARG_SUFFIX: | |
150 | arg_suffix = optarg; | |
151 | break; | |
152 | ||
153 | case '?': | |
154 | return -EINVAL; | |
155 | ||
156 | default: | |
157 | assert_not_reached("Unhandled option"); | |
158 | } | |
9a00f57a LP |
159 | |
160 | return 1; | |
161 | } | |
162 | ||
163 | int main(int argc, char* argv[]) { | |
164 | int r; | |
165 | ||
166 | log_parse_environment(); | |
167 | log_open(); | |
168 | ||
169 | r = parse_argv(argc, argv); | |
170 | if (r <= 0) | |
171 | goto finish; | |
172 | ||
173 | if (argc > optind) { | |
174 | int i, q; | |
175 | ||
176 | for (i = optind; i < argc; i++) { | |
177 | q = print_home(argv[i]); | |
178 | if (q < 0) | |
179 | r = q; | |
180 | } | |
181 | } else | |
182 | r = list_homes(); | |
183 | ||
184 | ||
185 | finish: | |
186 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
187 | } |