main.c revision 1.3 1 1.3 christos /* $NetBSD: main.c,v 1.3 2015/12/01 16:32:19 christos 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.3 christos __RCSID("$NetBSD: main.c,v 1.3 2015/12/01 16:32:19 christos 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.1 christos
45 1.1 christos #include "map.h"
46 1.1 christos #include "gpt.h"
47 1.1 christos
48 1.3 christos static const struct gpt_cmd c_null;
49 1.3 christos
50 1.3 christos extern const struct gpt_cmd
51 1.3 christos c_add,
52 1.1 christos #ifndef HAVE_NBTOOL_CONFIG_H
53 1.3 christos c_backup,
54 1.1 christos #endif
55 1.3 christos c_biosboot,
56 1.3 christos c_create,
57 1.3 christos c_destroy,
58 1.3 christos c_header,
59 1.3 christos c_label,
60 1.3 christos c_migrate,
61 1.3 christos c_recover,
62 1.3 christos c_remove,
63 1.3 christos c_resize,
64 1.3 christos c_resizedisk,
65 1.3 christos #ifndef HAVE_NBTOOL_CONFIG_H
66 1.3 christos c_restore,
67 1.3 christos #endif
68 1.3 christos c_set,
69 1.3 christos c_show,
70 1.3 christos c_type,
71 1.3 christos c_unset;
72 1.3 christos
73 1.3 christos static const struct gpt_cmd *cmdsw[] = {
74 1.3 christos &c_add,
75 1.3 christos #ifndef HAVE_NBTOOL_CONFIG_H
76 1.3 christos &c_backup,
77 1.3 christos #endif
78 1.3 christos &c_biosboot,
79 1.3 christos &c_create,
80 1.3 christos &c_destroy,
81 1.3 christos &c_header,
82 1.3 christos &c_label,
83 1.3 christos &c_migrate,
84 1.3 christos &c_recover,
85 1.3 christos &c_remove,
86 1.3 christos &c_resize,
87 1.3 christos &c_resizedisk,
88 1.3 christos #ifndef HAVE_NBTOOL_CONFIG_H
89 1.3 christos &c_restore,
90 1.3 christos #endif
91 1.3 christos &c_set,
92 1.3 christos &c_show,
93 1.3 christos &c_type,
94 1.3 christos &c_unset,
95 1.3 christos &c_null,
96 1.1 christos };
97 1.1 christos
98 1.1 christos __dead static void
99 1.1 christos usage(void)
100 1.1 christos {
101 1.1 christos const char *p = getprogname();
102 1.1 christos const char *f =
103 1.2 christos "[-nrqv] [-m <mediasize>] [-s <sectorsize>]";
104 1.3 christos size_t i;
105 1.1 christos
106 1.2 christos if (strcmp(p, "gpt") == 0)
107 1.2 christos fprintf(stderr,
108 1.2 christos "Usage: %s %s <command> [<args>] <device>\n", p, f);
109 1.2 christos else
110 1.2 christos fprintf(stderr,
111 1.2 christos "Usage: %s %s <device> <command> [<args>]\n", p, f);
112 1.3 christos fprintf(stderr, "Commands:\n");
113 1.3 christos for (i = 0; i < __arraycount(cmdsw); i++)
114 1.3 christos gpt_usage("\t", cmdsw[i]);
115 1.3 christos exit(EXIT_FAILURE);
116 1.1 christos }
117 1.1 christos
118 1.1 christos static void
119 1.1 christos prefix(const char *cmd)
120 1.1 christos {
121 1.1 christos char *pfx;
122 1.1 christos
123 1.1 christos if (asprintf(&pfx, "%s %s", getprogname(), cmd) < 0)
124 1.1 christos pfx = NULL;
125 1.1 christos else
126 1.1 christos setprogname(pfx);
127 1.1 christos }
128 1.1 christos
129 1.1 christos int
130 1.1 christos main(int argc, char *argv[])
131 1.1 christos {
132 1.2 christos char *cmd, *p, *dev = NULL;
133 1.1 christos int ch, i;
134 1.2 christos u_int secsz = 0;
135 1.2 christos off_t mediasz = 0;
136 1.2 christos int flags = 0;
137 1.2 christos int verbose = 0;
138 1.2 christos gpt_t gpt;
139 1.2 christos
140 1.2 christos setprogname(argv[0]);
141 1.2 christos
142 1.2 christos if (strcmp(getprogname(), "gpt") == 0) {
143 1.2 christos if (argc < 3)
144 1.2 christos usage();
145 1.2 christos dev = argv[--argc];
146 1.2 christos }
147 1.1 christos
148 1.1 christos /* Get the generic options */
149 1.2 christos while ((ch = getopt(argc, argv, "m:nqrs:v")) != -1) {
150 1.1 christos switch(ch) {
151 1.1 christos case 'm':
152 1.1 christos if (mediasz > 0)
153 1.1 christos usage();
154 1.1 christos mediasz = strtoul(optarg, &p, 10);
155 1.1 christos if (*p != 0 || mediasz < 1)
156 1.1 christos usage();
157 1.1 christos break;
158 1.1 christos case 'n':
159 1.2 christos flags |= GPT_NOSYNC;
160 1.1 christos break;
161 1.1 christos case 'r':
162 1.2 christos flags |= GPT_READONLY;
163 1.1 christos break;
164 1.1 christos case 'q':
165 1.2 christos flags |= GPT_QUIET;
166 1.1 christos break;
167 1.1 christos case 's':
168 1.1 christos if (secsz > 0)
169 1.1 christos usage();
170 1.1 christos secsz = strtoul(optarg, &p, 10);
171 1.1 christos if (*p != 0 || secsz < 1)
172 1.1 christos usage();
173 1.1 christos break;
174 1.1 christos case 'v':
175 1.1 christos verbose++;
176 1.1 christos break;
177 1.1 christos default:
178 1.1 christos usage();
179 1.1 christos }
180 1.1 christos }
181 1.2 christos
182 1.2 christos if (argc == optind)
183 1.2 christos usage();
184 1.2 christos
185 1.2 christos if (dev == NULL)
186 1.2 christos dev = argv[optind++];
187 1.1 christos
188 1.1 christos if (argc == optind)
189 1.1 christos usage();
190 1.1 christos
191 1.1 christos cmd = argv[optind++];
192 1.3 christos for (i = 0; cmdsw[i]->name != NULL && strcmp(cmd, cmdsw[i]->name); i++)
193 1.2 christos continue;
194 1.1 christos
195 1.3 christos if (cmdsw[i]->fptr == NULL)
196 1.2 christos errx(EXIT_FAILURE, "Unknown command: %s", cmd);
197 1.1 christos
198 1.1 christos prefix(cmd);
199 1.2 christos
200 1.2 christos gpt = gpt_open(dev, flags, verbose, mediasz, secsz);
201 1.2 christos if (gpt == NULL)
202 1.2 christos return EXIT_FAILURE;
203 1.2 christos
204 1.3 christos if ((*cmdsw[i]->fptr)(gpt, argc, argv) == -1)
205 1.2 christos return EXIT_FAILURE;
206 1.2 christos
207 1.2 christos gpt_close(gpt);
208 1.2 christos return EXIT_SUCCESS;
209 1.1 christos }
210