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