]>
Commit | Line | Data |
---|---|---|
e842803a MB |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2014 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <stdio.h> | |
23 | #include <getopt.h> | |
e842803a MB |
24 | #include <errno.h> |
25 | #include <unistd.h> | |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
28 | ||
29 | #include "sd-path.h" | |
30 | #include "build.h" | |
31 | #include "macro.h" | |
32 | #include "util.h" | |
33 | #include "log.h" | |
34 | ||
35 | static const char *arg_suffix = NULL; | |
36 | ||
37 | static const char* const path_table[_SD_PATH_MAX] = { | |
38 | [SD_PATH_TEMPORARY] = "temporary", | |
39 | [SD_PATH_TEMPORARY_LARGE] = "temporary-large", | |
40 | [SD_PATH_SYSTEM_BINARIES] = "system-binaries", | |
41 | [SD_PATH_SYSTEM_INCLUDE] = "system-include", | |
42 | [SD_PATH_SYSTEM_LIBRARY_PRIVATE] = "system-library-private", | |
43 | [SD_PATH_SYSTEM_LIBRARY_ARCH] = "system-library-arch", | |
44 | [SD_PATH_SYSTEM_SHARED] = "system-shared", | |
45 | [SD_PATH_SYSTEM_CONFIGURATION_FACTORY] = "system-configuration-factory", | |
46 | [SD_PATH_SYSTEM_STATE_FACTORY] = "system-state-factory", | |
47 | [SD_PATH_SYSTEM_CONFIGURATION] = "system-configuration", | |
48 | [SD_PATH_SYSTEM_RUNTIME] = "system-runtime", | |
49 | [SD_PATH_SYSTEM_RUNTIME_LOGS] = "system-runtime-logs", | |
50 | [SD_PATH_SYSTEM_STATE_PRIVATE] = "system-state-private", | |
51 | [SD_PATH_SYSTEM_STATE_LOGS] = "system-state-logs", | |
52 | [SD_PATH_SYSTEM_STATE_CACHE] = "system-state-cache", | |
53 | [SD_PATH_SYSTEM_STATE_SPOOL] = "system-state-spool", | |
54 | [SD_PATH_USER_BINARIES] = "user-binaries", | |
55 | [SD_PATH_USER_LIBRARY_PRIVATE] = "user-library-private", | |
56 | [SD_PATH_USER_LIBRARY_ARCH] = "user-library-arch", | |
57 | [SD_PATH_USER_SHARED] = "user-shared", | |
58 | [SD_PATH_USER_CONFIGURATION] = "user-configuration", | |
59 | [SD_PATH_USER_RUNTIME] = "user-runtime", | |
60 | [SD_PATH_USER_STATE_CACHE] = "user-state-cache", | |
61 | [SD_PATH_USER] = "user", | |
62 | [SD_PATH_USER_DOCUMENTS] = "user-documents", | |
63 | [SD_PATH_USER_MUSIC] = "user-music", | |
64 | [SD_PATH_USER_PICTURES] = "user-pictures", | |
65 | [SD_PATH_USER_VIDEOS] = "user-videos", | |
66 | [SD_PATH_USER_DOWNLOAD] = "user-download", | |
67 | [SD_PATH_USER_PUBLIC] = "user-public", | |
68 | [SD_PATH_USER_TEMPLATES] = "user-templates", | |
69 | [SD_PATH_USER_DESKTOP] = "user-desktop", | |
70 | [SD_PATH_SEARCH_BINARIES] = "search-binaries", | |
71 | [SD_PATH_SEARCH_LIBRARY_PRIVATE] = "search-library-private", | |
72 | [SD_PATH_SEARCH_LIBRARY_ARCH] = "search-library-arch", | |
73 | [SD_PATH_SEARCH_SHARED] = "search-shared", | |
74 | [SD_PATH_SEARCH_CONFIGURATION_FACTORY] = "search-configuration-factory", | |
75 | [SD_PATH_SEARCH_STATE_FACTORY] = "search-state-factory", | |
76 | [SD_PATH_SEARCH_CONFIGURATION] = "search-configuration", | |
77 | }; | |
78 | ||
e842803a MB |
79 | static int list_homes(void) { |
80 | uint64_t i = 0; | |
81 | int r = 0; | |
82 | ||
83 | for (i = 0; i < ELEMENTSOF(path_table); i++) { | |
84 | _cleanup_free_ char *p = NULL; | |
85 | int q; | |
86 | ||
87 | q = sd_path_home(i, arg_suffix, &p); | |
88 | if (q == -ENXIO) | |
89 | continue; | |
90 | if (q < 0) { | |
f47781d8 | 91 | log_error_errno(r, "Failed to query %s: %m", path_table[i]); |
e842803a MB |
92 | r = q; |
93 | continue; | |
94 | } | |
95 | ||
96 | printf("%s: %s\n", path_table[i], p); | |
97 | } | |
98 | ||
99 | return r; | |
100 | } | |
101 | ||
102 | static int print_home(const char *n) { | |
103 | uint64_t i = 0; | |
104 | int r; | |
105 | ||
106 | for (i = 0; i < ELEMENTSOF(path_table); i++) { | |
107 | if (streq(path_table[i], n)) { | |
108 | _cleanup_free_ char *p = NULL; | |
109 | ||
110 | r = sd_path_home(i, arg_suffix, &p); | |
f47781d8 MP |
111 | if (r < 0) |
112 | return log_error_errno(r, "Failed to query %s: %m", n); | |
e842803a MB |
113 | |
114 | printf("%s\n", p); | |
115 | return 0; | |
116 | } | |
117 | } | |
118 | ||
119 | log_error("Path %s not known.", n); | |
120 | return -ENOTSUP; | |
121 | } | |
122 | ||
5eef597e MP |
123 | static void help(void) { |
124 | printf("%s [OPTIONS...] [NAME...]\n\n" | |
125 | "Show system and user paths.\n\n" | |
126 | " -h --help Show this help\n" | |
127 | " --version Show package version\n" | |
128 | " --suffix=SUFFIX Suffix to append to paths\n", | |
129 | program_invocation_short_name); | |
130 | } | |
131 | ||
e842803a MB |
132 | static int parse_argv(int argc, char *argv[]) { |
133 | ||
134 | enum { | |
135 | ARG_VERSION = 0x100, | |
136 | ARG_SUFFIX, | |
137 | }; | |
138 | ||
139 | static const struct option options[] = { | |
140 | { "help", no_argument, NULL, 'h' }, | |
141 | { "version", no_argument, NULL, ARG_VERSION }, | |
142 | { "suffix", required_argument, NULL, ARG_SUFFIX }, | |
143 | {} | |
144 | }; | |
145 | ||
146 | int c; | |
147 | ||
148 | assert(argc >= 0); | |
149 | assert(argv); | |
150 | ||
5eef597e | 151 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) |
e842803a MB |
152 | |
153 | switch (c) { | |
154 | ||
155 | case 'h': | |
5eef597e MP |
156 | help(); |
157 | return 0; | |
e842803a MB |
158 | |
159 | case ARG_VERSION: | |
160 | puts(PACKAGE_STRING); | |
161 | puts(SYSTEMD_FEATURES); | |
162 | return 0; | |
163 | ||
164 | case ARG_SUFFIX: | |
165 | arg_suffix = optarg; | |
166 | break; | |
167 | ||
168 | case '?': | |
169 | return -EINVAL; | |
170 | ||
171 | default: | |
172 | assert_not_reached("Unhandled option"); | |
173 | } | |
e842803a MB |
174 | |
175 | return 1; | |
176 | } | |
177 | ||
178 | int main(int argc, char* argv[]) { | |
179 | int r; | |
180 | ||
181 | log_parse_environment(); | |
182 | log_open(); | |
183 | ||
184 | r = parse_argv(argc, argv); | |
185 | if (r <= 0) | |
186 | goto finish; | |
187 | ||
188 | if (argc > optind) { | |
189 | int i, q; | |
190 | ||
191 | for (i = optind; i < argc; i++) { | |
192 | q = print_home(argv[i]); | |
193 | if (q < 0) | |
194 | r = q; | |
195 | } | |
196 | } else | |
197 | r = list_homes(); | |
198 | ||
199 | ||
200 | finish: | |
201 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
202 | } |