main.c revision 1.13 1 1.13 jnemeth /* $NetBSD: main.c,v 1.13 2019/06/25 04:53:40 jnemeth Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2002 Marcel Moolenaar
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos *
11 1.1 christos * 1. Redistributions of source code must retain the above copyright
12 1.1 christos * notice, this list of conditions and the following disclaimer.
13 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer in the
15 1.1 christos * documentation and/or other materials provided with the distribution.
16 1.1 christos *
17 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1 christos *
28 1.1 christos * CRC32 code derived from work by Gary S. Brown.
29 1.1 christos */
30 1.1 christos
31 1.1 christos #if HAVE_NBTOOL_CONFIG_H
32 1.1 christos #include "nbtool_config.h"
33 1.1 christos #endif
34 1.1 christos
35 1.1 christos #include <sys/cdefs.h>
36 1.1 christos #ifdef __RCSID
37 1.13 jnemeth __RCSID("$NetBSD: main.c,v 1.13 2019/06/25 04:53:40 jnemeth Exp $");
38 1.1 christos #endif
39 1.1 christos
40 1.1 christos #include <stdio.h>
41 1.1 christos #include <stdlib.h>
42 1.1 christos #include <unistd.h>
43 1.1 christos #include <err.h>
44 1.9 christos #include <errno.h>
45 1.9 christos #include <sys/stat.h>
46 1.9 christos #ifndef NBTOOL_CONFIG_H
47 1.9 christos #include <util.h>
48 1.9 christos #endif
49 1.1 christos
50 1.1 christos #include "map.h"
51 1.1 christos #include "gpt.h"
52 1.1 christos
53 1.3 christos static const struct gpt_cmd c_null;
54 1.3 christos
55 1.3 christos extern const struct gpt_cmd
56 1.3 christos c_add,
57 1.1 christos #ifndef HAVE_NBTOOL_CONFIG_H
58 1.3 christos c_backup,
59 1.1 christos #endif
60 1.3 christos c_biosboot,
61 1.3 christos c_create,
62 1.3 christos c_destroy,
63 1.3 christos c_header,
64 1.3 christos c_label,
65 1.3 christos c_migrate,
66 1.3 christos c_recover,
67 1.3 christos c_remove,
68 1.3 christos c_resize,
69 1.3 christos c_resizedisk,
70 1.3 christos #ifndef HAVE_NBTOOL_CONFIG_H
71 1.3 christos c_restore,
72 1.3 christos #endif
73 1.3 christos c_set,
74 1.3 christos c_show,
75 1.3 christos c_type,
76 1.13 jnemeth c_unset,
77 1.13 jnemeth c_uuid;
78 1.3 christos
79 1.3 christos static const struct gpt_cmd *cmdsw[] = {
80 1.3 christos &c_add,
81 1.3 christos #ifndef HAVE_NBTOOL_CONFIG_H
82 1.3 christos &c_backup,
83 1.3 christos #endif
84 1.3 christos &c_biosboot,
85 1.3 christos &c_create,
86 1.3 christos &c_destroy,
87 1.3 christos &c_header,
88 1.3 christos &c_label,
89 1.3 christos &c_migrate,
90 1.3 christos &c_recover,
91 1.3 christos &c_remove,
92 1.3 christos &c_resize,
93 1.3 christos &c_resizedisk,
94 1.3 christos #ifndef HAVE_NBTOOL_CONFIG_H
95 1.3 christos &c_restore,
96 1.3 christos #endif
97 1.3 christos &c_set,
98 1.3 christos &c_show,
99 1.3 christos &c_type,
100 1.3 christos &c_unset,
101 1.13 jnemeth &c_uuid,
102 1.3 christos &c_null,
103 1.1 christos };
104 1.1 christos
105 1.1 christos __dead static void
106 1.1 christos usage(void)
107 1.1 christos {
108 1.1 christos const char *p = getprogname();
109 1.1 christos const char *f =
110 1.12 sevan "[-nrqv] [-m mediasize] [-s sectorsize] [-T timestamp]";
111 1.3 christos size_t i;
112 1.1 christos
113 1.2 christos if (strcmp(p, "gpt") == 0)
114 1.2 christos fprintf(stderr,
115 1.5 christos "Usage: %s %s command device\n", p, f);
116 1.2 christos else
117 1.2 christos fprintf(stderr,
118 1.5 christos "Usage: %s %s device command\n", p, f);
119 1.3 christos fprintf(stderr, "Commands:\n");
120 1.3 christos for (i = 0; i < __arraycount(cmdsw); i++)
121 1.3 christos gpt_usage("\t", cmdsw[i]);
122 1.3 christos exit(EXIT_FAILURE);
123 1.1 christos }
124 1.1 christos
125 1.1 christos static void
126 1.1 christos prefix(const char *cmd)
127 1.1 christos {
128 1.1 christos char *pfx;
129 1.1 christos
130 1.1 christos if (asprintf(&pfx, "%s %s", getprogname(), cmd) < 0)
131 1.1 christos pfx = NULL;
132 1.1 christos else
133 1.1 christos setprogname(pfx);
134 1.1 christos }
135 1.1 christos
136 1.9 christos static time_t
137 1.9 christos get_tstamp(const char *b)
138 1.9 christos {
139 1.9 christos struct stat st;
140 1.9 christos char *eb;
141 1.9 christos long long l;
142 1.9 christos #ifndef HAVE_NBTOOL_CONFIG_H
143 1.9 christos time_t when;
144 1.9 christos #endif
145 1.9 christos
146 1.9 christos if (stat(b, &st) != -1)
147 1.9 christos return (time_t)st.st_mtime;
148 1.9 christos
149 1.9 christos #ifndef HAVE_NBTOOL_CONFIG_H
150 1.9 christos errno = 0;
151 1.9 christos if ((when = parsedate(b, NULL, NULL)) != -1 || errno == 0)
152 1.9 christos return when;
153 1.9 christos #endif
154 1.9 christos errno = 0;
155 1.9 christos l = strtoll(b, &eb, 0);
156 1.9 christos if (b == eb || *eb || errno)
157 1.9 christos errx(EXIT_FAILURE, "Can't parse timestamp `%s'", b);
158 1.9 christos return (time_t)l;
159 1.9 christos }
160 1.9 christos
161 1.1 christos int
162 1.1 christos main(int argc, char *argv[])
163 1.1 christos {
164 1.2 christos char *cmd, *p, *dev = NULL;
165 1.1 christos int ch, i;
166 1.2 christos u_int secsz = 0;
167 1.2 christos off_t mediasz = 0;
168 1.2 christos int flags = 0;
169 1.2 christos int verbose = 0;
170 1.9 christos time_t timestamp = 0;
171 1.2 christos gpt_t gpt;
172 1.2 christos
173 1.2 christos setprogname(argv[0]);
174 1.2 christos
175 1.2 christos if (strcmp(getprogname(), "gpt") == 0) {
176 1.2 christos if (argc < 3)
177 1.2 christos usage();
178 1.2 christos dev = argv[--argc];
179 1.2 christos }
180 1.1 christos
181 1.8 aymeric #ifdef __GLIBC__
182 1.8 aymeric #define GETOPT_BE_POSIX "+"
183 1.8 aymeric #else
184 1.8 aymeric #define GETOPT_BE_POSIX ""
185 1.8 aymeric #endif
186 1.8 aymeric
187 1.1 christos /* Get the generic options */
188 1.9 christos while ((ch = getopt(argc, argv, GETOPT_BE_POSIX "m:nqrs:T:v")) != -1) {
189 1.1 christos switch(ch) {
190 1.1 christos case 'm':
191 1.1 christos if (mediasz > 0)
192 1.1 christos usage();
193 1.6 christos mediasz = strtol(optarg, &p, 10);
194 1.1 christos if (*p != 0 || mediasz < 1)
195 1.1 christos usage();
196 1.1 christos break;
197 1.1 christos case 'n':
198 1.2 christos flags |= GPT_NOSYNC;
199 1.1 christos break;
200 1.1 christos case 'r':
201 1.2 christos flags |= GPT_READONLY;
202 1.1 christos break;
203 1.1 christos case 'q':
204 1.2 christos flags |= GPT_QUIET;
205 1.1 christos break;
206 1.1 christos case 's':
207 1.7 christos if (gpt_uint_get(NULL, &secsz) == -1)
208 1.1 christos usage();
209 1.1 christos break;
210 1.9 christos case 'T':
211 1.10 christos flags |= GPT_TIMESTAMP;
212 1.9 christos timestamp = get_tstamp(optarg);
213 1.9 christos break;
214 1.1 christos case 'v':
215 1.1 christos verbose++;
216 1.1 christos break;
217 1.1 christos default:
218 1.1 christos usage();
219 1.1 christos }
220 1.1 christos }
221 1.2 christos
222 1.2 christos if (argc == optind)
223 1.2 christos usage();
224 1.2 christos
225 1.2 christos if (dev == NULL)
226 1.2 christos dev = argv[optind++];
227 1.1 christos
228 1.1 christos if (argc == optind)
229 1.1 christos usage();
230 1.1 christos
231 1.1 christos cmd = argv[optind++];
232 1.3 christos for (i = 0; cmdsw[i]->name != NULL && strcmp(cmd, cmdsw[i]->name); i++)
233 1.2 christos continue;
234 1.1 christos
235 1.3 christos if (cmdsw[i]->fptr == NULL)
236 1.2 christos errx(EXIT_FAILURE, "Unknown command: %s", cmd);
237 1.1 christos
238 1.1 christos prefix(cmd);
239 1.2 christos
240 1.7 christos if (*dev != '-') {
241 1.7 christos gpt = gpt_open(dev, flags | cmdsw[i]->flags,
242 1.9 christos verbose, mediasz, secsz, timestamp);
243 1.7 christos if (gpt == NULL)
244 1.7 christos return EXIT_FAILURE;
245 1.7 christos } else {
246 1.11 mlelstv if ((cmdsw[i]->flags & GPT_OPTDEV) == 0)
247 1.11 mlelstv errx(EXIT_FAILURE,
248 1.11 mlelstv "Command %s needs a device parameter", cmd);
249 1.7 christos argc++;
250 1.7 christos gpt = NULL;
251 1.7 christos }
252 1.2 christos
253 1.3 christos if ((*cmdsw[i]->fptr)(gpt, argc, argv) == -1)
254 1.2 christos return EXIT_FAILURE;
255 1.2 christos
256 1.7 christos if (gpt)
257 1.7 christos gpt_close(gpt);
258 1.2 christos return EXIT_SUCCESS;
259 1.1 christos }
260