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