main.c revision 1.2
1/*	$NetBSD: main.c,v 1.2 2015/12/01 09:05:33 christos Exp $	*/
2
3/*-
4 * Copyright (c) 2002 Marcel Moolenaar
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * CRC32 code derived from work by Gary S. Brown.
29 */
30
31#if HAVE_NBTOOL_CONFIG_H
32#include "nbtool_config.h"
33#endif
34
35#include <sys/cdefs.h>
36#ifdef __RCSID
37__RCSID("$NetBSD: main.c,v 1.2 2015/12/01 09:05:33 christos Exp $");
38#endif
39
40#include <stdio.h>
41#include <stdlib.h>
42#include <unistd.h>
43#include <err.h>
44
45#include "map.h"
46#include "gpt.h"
47
48static struct {
49	int (*fptr)(gpt_t, int, char *[]);
50	const char *name;
51} cmdsw[] = {
52	{ cmd_add, "add" },
53#ifndef HAVE_NBTOOL_CONFIG_H
54	{ cmd_backup, "backup" },
55#endif
56	{ cmd_biosboot, "biosboot" },
57	{ cmd_create, "create" },
58	{ cmd_destroy, "destroy" },
59	{ cmd_header, "header" },
60	{ NULL, "help" },
61	{ cmd_label, "label" },
62	{ cmd_migrate, "migrate" },
63	{ cmd_recover, "recover" },
64	{ cmd_remove, "remove" },
65	{ NULL, "rename" },
66	{ cmd_resize, "resize" },
67	{ cmd_resizedisk, "resizedisk" },
68#ifndef HAVE_NBTOOL_CONFIG_H
69	{ cmd_restore, "restore" },
70#endif
71	{ cmd_set, "set" },
72	{ cmd_show, "show" },
73	{ cmd_type, "type" },
74	{ cmd_unset, "unset" },
75	{ NULL, "verify" },
76	{ NULL, NULL }
77};
78
79__dead static void
80usage(void)
81{
82	extern const char addmsg1[], addmsg2[], biosbootmsg[];
83	extern const char createmsg[], destroymsg[], headermsg[], labelmsg1[];
84	extern const char labelmsg2[], labelmsg3[], migratemsg[], recovermsg[];
85	extern const char removemsg1[], removemsg2[], resizemsg[];
86	extern const char resizediskmsg[], setmsg[], showmsg[], typemsg1[];
87	extern const char typemsg2[], typemsg3[], unsetmsg[];
88#ifndef HAVE_NBTOOL_CONFIG_H
89	extern const char backupmsg[], restoremsg[];
90#endif
91	const char *p = getprogname();
92	const char *f =
93	    "[-nrqv] [-m <mediasize>] [-s <sectorsize>]";
94
95	if (strcmp(p, "gpt") == 0)
96		fprintf(stderr,
97		    "Usage: %s %s <command> [<args>] <device>\n", p, f);
98	else
99		fprintf(stderr,
100		    "Usage: %s %s <device> <command> [<args>]\n", p, f);
101	fprintf(stderr,
102	    "Commands:\n"
103#ifndef HAVE_NBTOOL_CONFIG_H
104	    "       %s\n"
105	    "       %s\n"
106#endif
107	    "       %s\n"
108	    "       %s\n"
109	    "       %s\n"
110	    "       %s\n"
111	    "       %s\n"
112	    "       %s\n"
113	    "       %s\n"
114	    "       %s\n"
115	    "       %s\n"
116	    "       %s\n"
117	    "       %s\n"
118	    "       %s\n"
119	    "       %s\n"
120	    "       %s\n"
121	    "       %s\n"
122	    "       %s\n"
123	    "       %s\n"
124	    "       %s\n"
125	    "       %s\n"
126	    "       %s\n"
127	    "       %s\n",
128	    addmsg1, addmsg2,
129#ifndef HAVE_NBTOOL_CONFIG_H
130	    backupmsg,
131#endif
132	    biosbootmsg, createmsg, destroymsg,
133	    headermsg, labelmsg1, labelmsg2, labelmsg3,
134	    migratemsg, recovermsg,
135	    removemsg1, removemsg2,
136	    resizemsg, resizediskmsg,
137#ifndef HAVE_NBTOOL_CONFIG_H
138	    restoremsg,
139#endif
140	    setmsg, showmsg,
141	    typemsg1, typemsg2, typemsg3,
142	    unsetmsg);
143	exit(1);
144}
145
146static void
147prefix(const char *cmd)
148{
149	char *pfx;
150
151	if (asprintf(&pfx, "%s %s", getprogname(), cmd) < 0)
152		pfx = NULL;
153	else
154		setprogname(pfx);
155}
156
157int
158main(int argc, char *argv[])
159{
160	char *cmd, *p, *dev = NULL;
161	int ch, i;
162	u_int secsz = 0;
163	off_t mediasz = 0;
164	int flags = 0;
165	int verbose = 0;
166	gpt_t gpt;
167
168	setprogname(argv[0]);
169
170	if (strcmp(getprogname(), "gpt") == 0) {
171		if (argc < 3)
172			usage();
173		dev = argv[--argc];
174	}
175
176	/* Get the generic options */
177	while ((ch = getopt(argc, argv, "m:nqrs:v")) != -1) {
178		switch(ch) {
179		case 'm':
180			if (mediasz > 0)
181				usage();
182			mediasz = strtoul(optarg, &p, 10);
183			if (*p != 0 || mediasz < 1)
184				usage();
185			break;
186		case 'n':
187			flags |= GPT_NOSYNC;
188			break;
189		case 'r':
190			flags |= GPT_READONLY;
191			break;
192		case 'q':
193			flags |= GPT_QUIET;
194			break;
195		case 's':
196			if (secsz > 0)
197				usage();
198			secsz = strtoul(optarg, &p, 10);
199			if (*p != 0 || secsz < 1)
200				usage();
201			break;
202		case 'v':
203			verbose++;
204			break;
205		default:
206			usage();
207		}
208	}
209
210	if (argc == optind)
211		usage();
212
213	if (dev == NULL)
214		dev = argv[optind++];
215
216	if (argc == optind)
217		usage();
218
219	cmd = argv[optind++];
220	for (i = 0; cmdsw[i].name != NULL && strcmp(cmd, cmdsw[i].name); i++)
221		continue;
222
223	if (cmdsw[i].fptr == NULL)
224		errx(EXIT_FAILURE, "Unknown command: %s", cmd);
225
226	prefix(cmd);
227
228	gpt = gpt_open(dev, flags, verbose, mediasz, secsz);
229	if (gpt == NULL)
230		return EXIT_FAILURE;
231
232	if ((*cmdsw[i].fptr)(gpt, argc, argv) == -1)
233		return EXIT_FAILURE;
234
235	gpt_close(gpt);
236	return EXIT_SUCCESS;
237}
238