interact.c revision 1.10.4.1 1 1.10.4.1 wrstuden /* $NetBSD: interact.c,v 1.10.4.1 1999/12/27 18:30:21 wrstuden Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 1997 Christos Zoulas. All rights reserved.
5 1.1 christos *
6 1.1 christos * Redistribution and use in source and binary forms, with or without
7 1.1 christos * modification, are permitted provided that the following conditions
8 1.1 christos * are met:
9 1.1 christos * 1. Redistributions of source code must retain the above copyright
10 1.1 christos * notice, this list of conditions and the following disclaimer.
11 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 christos * notice, this list of conditions and the following disclaimer in the
13 1.1 christos * documentation and/or other materials provided with the distribution.
14 1.1 christos * 3. All advertising materials mentioning features or use of this software
15 1.1 christos * must display the following acknowledgement:
16 1.1 christos * This product includes software developed by Christos Zoulas.
17 1.1 christos * 4. The name of the author may not be used to endorse or promote products
18 1.1 christos * derived from this software without specific prior written permission.
19 1.1 christos *
20 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos
32 1.5 christos #include <sys/cdefs.h>
33 1.1 christos #ifndef lint
34 1.10.4.1 wrstuden __RCSID("$NetBSD: interact.c,v 1.10.4.1 1999/12/27 18:30:21 wrstuden Exp $");
35 1.1 christos #endif /* lint */
36 1.1 christos
37 1.10.4.1 wrstuden #include <sys/param.h>
38 1.10.4.1 wrstuden #define FSTYPENAMES
39 1.10.4.1 wrstuden #define DKTYPENAMES
40 1.10.4.1 wrstuden #include <sys/disklabel.h>
41 1.10.4.1 wrstuden
42 1.10.4.1 wrstuden #include <err.h>
43 1.1 christos #include <stdio.h>
44 1.1 christos #include <string.h>
45 1.1 christos #include <stdlib.h>
46 1.2 christos #include <util.h>
47 1.1 christos
48 1.1 christos #include "extern.h"
49 1.1 christos
50 1.1 christos static void cmd_help __P((struct disklabel *, char *, int));
51 1.9 christos static void cmd_chain __P((struct disklabel *, char *, int));
52 1.1 christos static void cmd_print __P((struct disklabel *, char *, int));
53 1.10.4.1 wrstuden static void cmd_printall __P((struct disklabel *, char *, int));
54 1.10.4.1 wrstuden static void cmd_info __P((struct disklabel *, char *, int));
55 1.1 christos static void cmd_part __P((struct disklabel *, char *, int));
56 1.1 christos static void cmd_label __P((struct disklabel *, char *, int));
57 1.1 christos static void cmd_round __P((struct disklabel *, char *, int));
58 1.1 christos static void cmd_name __P((struct disklabel *, char *, int));
59 1.1 christos static int runcmd __P((char *, struct disklabel *, int));
60 1.1 christos static int getinput __P((const char *, const char *, const char *, char *));
61 1.1 christos static void defnum __P((char *, struct disklabel *, int));
62 1.10.4.1 wrstuden static int getnum __P((char *, int, struct disklabel *));
63 1.1 christos static void deffstypename __P((char *, int));
64 1.1 christos static int getfstypename __P((const char *));
65 1.1 christos
66 1.1 christos static int rounding = 0; /* sector rounding */
67 1.9 christos static int chaining = 0; /* make partitions contiguous */
68 1.1 christos
69 1.1 christos static struct cmds {
70 1.1 christos const char *name;
71 1.1 christos void (*func) __P((struct disklabel *, char *, int));
72 1.1 christos const char *help;
73 1.1 christos } cmds[] = {
74 1.1 christos { "?", cmd_help, "print this menu" },
75 1.9 christos { "C", cmd_chain, "make partitions contiguous" },
76 1.10.4.1 wrstuden { "E", cmd_printall, "print disk label and current partition table"},
77 1.10.4.1 wrstuden { "I", cmd_info, "change label information" },
78 1.2 christos { "N", cmd_name, "name the label" },
79 1.2 christos { "P", cmd_print, "print current partition table" },
80 1.2 christos { "Q", NULL, "quit" },
81 1.2 christos { "R", cmd_round, "rounding (c)ylinders (s)ectors" },
82 1.2 christos { "W", cmd_label, "write the current partition table" },
83 1.1 christos { NULL, NULL, NULL }
84 1.1 christos };
85 1.1 christos
86 1.1 christos
87 1.1 christos
88 1.1 christos static void
89 1.1 christos cmd_help(lp, s, fd)
90 1.1 christos struct disklabel *lp;
91 1.1 christos char *s;
92 1.1 christos int fd;
93 1.1 christos {
94 1.1 christos struct cmds *cmd;
95 1.2 christos
96 1.1 christos for (cmd = cmds; cmd->name != NULL; cmd++)
97 1.1 christos printf("%s\t%s\n", cmd->name, cmd->help);
98 1.2 christos printf("[a-%c]\tdefine named partition\n",
99 1.2 christos 'a' + getmaxpartitions() - 1);
100 1.1 christos }
101 1.1 christos
102 1.1 christos
103 1.1 christos static void
104 1.9 christos cmd_chain(lp, s, fd)
105 1.9 christos struct disklabel *lp;
106 1.9 christos char *s;
107 1.9 christos int fd;
108 1.9 christos {
109 1.9 christos int i;
110 1.9 christos char line[BUFSIZ];
111 1.9 christos
112 1.9 christos i = getinput(":", "Automatically adjust partitions",
113 1.9 christos chaining ? "yes" : "no", line);
114 1.9 christos
115 1.9 christos if (i <= 0)
116 1.9 christos return;
117 1.9 christos
118 1.9 christos switch (line[0]) {
119 1.9 christos case 'y':
120 1.9 christos chaining = 1;
121 1.9 christos return;
122 1.9 christos case 'n':
123 1.9 christos chaining = 0;
124 1.9 christos return;
125 1.9 christos default:
126 1.9 christos printf("Invalid answer\n");
127 1.9 christos return;
128 1.9 christos }
129 1.9 christos }
130 1.9 christos
131 1.9 christos static void
132 1.10.4.1 wrstuden cmd_printall(lp, s, fd)
133 1.10.4.1 wrstuden struct disklabel *lp;
134 1.10.4.1 wrstuden char *s;
135 1.10.4.1 wrstuden int fd;
136 1.10.4.1 wrstuden {
137 1.10.4.1 wrstuden
138 1.10.4.1 wrstuden showinfo(stdout, lp);
139 1.10.4.1 wrstuden showpartitions(stdout, lp);
140 1.10.4.1 wrstuden }
141 1.10.4.1 wrstuden
142 1.10.4.1 wrstuden static void
143 1.1 christos cmd_print(lp, s, fd)
144 1.1 christos struct disklabel *lp;
145 1.1 christos char *s;
146 1.1 christos int fd;
147 1.1 christos {
148 1.9 christos showpartitions(stdout, lp);
149 1.1 christos }
150 1.1 christos
151 1.10.4.1 wrstuden static void
152 1.10.4.1 wrstuden cmd_info(lp, s, fd)
153 1.10.4.1 wrstuden struct disklabel *lp;
154 1.10.4.1 wrstuden char *s;
155 1.10.4.1 wrstuden int fd;
156 1.10.4.1 wrstuden {
157 1.10.4.1 wrstuden char line[BUFSIZ];
158 1.10.4.1 wrstuden char def[BUFSIZ];
159 1.10.4.1 wrstuden const char * const *cpp;
160 1.10.4.1 wrstuden const char *t;
161 1.10.4.1 wrstuden int v, i;
162 1.10.4.1 wrstuden u_int32_t u;
163 1.10.4.1 wrstuden
164 1.10.4.1 wrstuden printf("# Current values:\n");
165 1.10.4.1 wrstuden showinfo(stdout, lp);
166 1.10.4.1 wrstuden
167 1.10.4.1 wrstuden /* d_typename */
168 1.10.4.1 wrstuden for (;;) {
169 1.10.4.1 wrstuden strncpy(def, lp->d_typename, sizeof(def));
170 1.10.4.1 wrstuden def[sizeof(def) - 1] = '\0';
171 1.10.4.1 wrstuden i = getinput(":", "Disk type", def, line);
172 1.10.4.1 wrstuden if (i <= 0)
173 1.10.4.1 wrstuden break;
174 1.10.4.1 wrstuden cpp = dktypenames;
175 1.10.4.1 wrstuden for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
176 1.10.4.1 wrstuden if ((t = *cpp) && !strcmp(t, line)) {
177 1.10.4.1 wrstuden lp->d_type = cpp - dktypenames;
178 1.10.4.1 wrstuden goto done_typename;
179 1.10.4.1 wrstuden }
180 1.10.4.1 wrstuden v = atoi(line);
181 1.10.4.1 wrstuden if ((unsigned)v >= DKMAXTYPES) {
182 1.10.4.1 wrstuden warnx("unknown disk type: %s", line);
183 1.10.4.1 wrstuden continue;
184 1.10.4.1 wrstuden }
185 1.10.4.1 wrstuden lp->d_type = v;
186 1.10.4.1 wrstuden done_typename:
187 1.10.4.1 wrstuden break;
188 1.10.4.1 wrstuden }
189 1.10.4.1 wrstuden
190 1.10.4.1 wrstuden /* d_packname */
191 1.10.4.1 wrstuden cmd_name(lp, s, fd);
192 1.10.4.1 wrstuden
193 1.10.4.1 wrstuden /* d_npartitions */
194 1.10.4.1 wrstuden for (;;) {
195 1.10.4.1 wrstuden snprintf(def, sizeof def, "%u", lp->d_npartitions);
196 1.10.4.1 wrstuden i = getinput(":", "Number of partitions", def, line);
197 1.10.4.1 wrstuden if (i <= 0)
198 1.10.4.1 wrstuden break;
199 1.10.4.1 wrstuden if (sscanf(line, "%u", &u) != 1) {
200 1.10.4.1 wrstuden printf("Invalid sector size `%s'\n", line);
201 1.10.4.1 wrstuden continue;
202 1.10.4.1 wrstuden }
203 1.10.4.1 wrstuden lp->d_npartitions = u;
204 1.10.4.1 wrstuden break;
205 1.10.4.1 wrstuden }
206 1.10.4.1 wrstuden
207 1.10.4.1 wrstuden /* d_secsize */
208 1.10.4.1 wrstuden for (;;) {
209 1.10.4.1 wrstuden snprintf(def, sizeof def, "%u", lp->d_secsize);
210 1.10.4.1 wrstuden i = getinput(":", "Sector size (bytes)", def, line);
211 1.10.4.1 wrstuden if (i <= 0)
212 1.10.4.1 wrstuden break;
213 1.10.4.1 wrstuden if (sscanf(line, "%u", &u) != 1) {
214 1.10.4.1 wrstuden printf("Invalid sector size `%s'\n", line);
215 1.10.4.1 wrstuden continue;
216 1.10.4.1 wrstuden }
217 1.10.4.1 wrstuden lp->d_secsize = u;
218 1.10.4.1 wrstuden break;
219 1.10.4.1 wrstuden }
220 1.10.4.1 wrstuden
221 1.10.4.1 wrstuden /* d_nsectors */
222 1.10.4.1 wrstuden for (;;) {
223 1.10.4.1 wrstuden snprintf(def, sizeof def, "%u", lp->d_nsectors);
224 1.10.4.1 wrstuden i = getinput(":", "Number of sectors per track", def, line);
225 1.10.4.1 wrstuden if (i <= 0)
226 1.10.4.1 wrstuden break;
227 1.10.4.1 wrstuden if (sscanf(line, "%u", &u) != 1) {
228 1.10.4.1 wrstuden printf("Invalid number of sector `%s'\n", line);
229 1.10.4.1 wrstuden continue;
230 1.10.4.1 wrstuden }
231 1.10.4.1 wrstuden lp->d_nsectors = u;
232 1.10.4.1 wrstuden break;
233 1.10.4.1 wrstuden }
234 1.10.4.1 wrstuden
235 1.10.4.1 wrstuden /* d_ntracks */
236 1.10.4.1 wrstuden for (;;) {
237 1.10.4.1 wrstuden snprintf(def, sizeof def, "%u", lp->d_ntracks);
238 1.10.4.1 wrstuden i = getinput(":", "Number of tracks per cylinder", def, line);
239 1.10.4.1 wrstuden if (i <= 0)
240 1.10.4.1 wrstuden break;
241 1.10.4.1 wrstuden if (sscanf(line, "%u", &u) != 1) {
242 1.10.4.1 wrstuden printf("Invalid number of tracks `%s'\n", line);
243 1.10.4.1 wrstuden continue;
244 1.10.4.1 wrstuden }
245 1.10.4.1 wrstuden lp->d_ntracks = u;
246 1.10.4.1 wrstuden break;
247 1.10.4.1 wrstuden }
248 1.10.4.1 wrstuden
249 1.10.4.1 wrstuden /* d_secpercyl */
250 1.10.4.1 wrstuden for (;;) {
251 1.10.4.1 wrstuden snprintf(def, sizeof def, "%u", lp->d_secpercyl);
252 1.10.4.1 wrstuden i = getinput(":", "Number of sectors/cylinder", def, line);
253 1.10.4.1 wrstuden if (i <= 0)
254 1.10.4.1 wrstuden break;
255 1.10.4.1 wrstuden if (sscanf(line, "%u", &u) != 1) {
256 1.10.4.1 wrstuden printf("Invalid number of sector/cylinder `%s'\n", line);
257 1.10.4.1 wrstuden continue;
258 1.10.4.1 wrstuden }
259 1.10.4.1 wrstuden lp->d_secpercyl = u;
260 1.10.4.1 wrstuden break;
261 1.10.4.1 wrstuden }
262 1.10.4.1 wrstuden
263 1.10.4.1 wrstuden /* d_ncylinders */
264 1.10.4.1 wrstuden for (;;) {
265 1.10.4.1 wrstuden snprintf(def, sizeof def, "%u", lp->d_ncylinders);
266 1.10.4.1 wrstuden i = getinput(":", "Total number of cylinders", def, line);
267 1.10.4.1 wrstuden if (i <= 0)
268 1.10.4.1 wrstuden break;
269 1.10.4.1 wrstuden if (sscanf(line, "%u", &u) != 1) {
270 1.10.4.1 wrstuden printf("Invalid sector size `%s'\n", line);
271 1.10.4.1 wrstuden continue;
272 1.10.4.1 wrstuden }
273 1.10.4.1 wrstuden lp->d_ncylinders = u;
274 1.10.4.1 wrstuden break;
275 1.10.4.1 wrstuden }
276 1.10.4.1 wrstuden
277 1.10.4.1 wrstuden /* d_secperunit */
278 1.10.4.1 wrstuden for (;;) {
279 1.10.4.1 wrstuden snprintf(def, sizeof def, "%u", lp->d_secperunit);
280 1.10.4.1 wrstuden i = getinput(":", "Total number of sectors", def, line);
281 1.10.4.1 wrstuden if (i <= 0)
282 1.10.4.1 wrstuden break;
283 1.10.4.1 wrstuden if (sscanf(line, "%u", &u) != 1) {
284 1.10.4.1 wrstuden printf("Invalid number of sector `%s'\n", line);
285 1.10.4.1 wrstuden continue;
286 1.10.4.1 wrstuden }
287 1.10.4.1 wrstuden lp->d_secperunit = u;
288 1.10.4.1 wrstuden break;
289 1.10.4.1 wrstuden }
290 1.10.4.1 wrstuden
291 1.10.4.1 wrstuden /* d_rpm */
292 1.10.4.1 wrstuden
293 1.10.4.1 wrstuden /* d_interleave */
294 1.10.4.1 wrstuden for (;;) {
295 1.10.4.1 wrstuden snprintf(def, sizeof def, "%u", lp->d_interleave);
296 1.10.4.1 wrstuden i = getinput(":", "Hardware sectors interleave", def, line);
297 1.10.4.1 wrstuden
298 1.10.4.1 wrstuden if (i <= 0)
299 1.10.4.1 wrstuden break;
300 1.10.4.1 wrstuden if (sscanf(line, "%u", &u) != 1) {
301 1.10.4.1 wrstuden printf("Invalid sector size `%s'\n", line);
302 1.10.4.1 wrstuden continue;
303 1.10.4.1 wrstuden }
304 1.10.4.1 wrstuden lp->d_interleave = u;
305 1.10.4.1 wrstuden break;
306 1.10.4.1 wrstuden }
307 1.10.4.1 wrstuden
308 1.10.4.1 wrstuden /* d_trackskew */
309 1.10.4.1 wrstuden for (;;) {
310 1.10.4.1 wrstuden snprintf(def, sizeof def, "%u", lp->d_trackskew);
311 1.10.4.1 wrstuden i = getinput(":", "Sector 0 skew, per track", def, line);
312 1.10.4.1 wrstuden if (i <= 0)
313 1.10.4.1 wrstuden break;
314 1.10.4.1 wrstuden if (sscanf(line, "%u", &u) != 1) {
315 1.10.4.1 wrstuden printf("Invalid sector size `%s'\n", line);
316 1.10.4.1 wrstuden continue;
317 1.10.4.1 wrstuden }
318 1.10.4.1 wrstuden lp->d_trackskew = u;
319 1.10.4.1 wrstuden break;
320 1.10.4.1 wrstuden }
321 1.10.4.1 wrstuden
322 1.10.4.1 wrstuden /* d_cylskew */
323 1.10.4.1 wrstuden for (;;) {
324 1.10.4.1 wrstuden snprintf(def, sizeof def, "%u", lp->d_cylskew);
325 1.10.4.1 wrstuden i = getinput(":", "Sector 0 skew, per cylinder", def, line);
326 1.10.4.1 wrstuden if (i <= 0)
327 1.10.4.1 wrstuden break;
328 1.10.4.1 wrstuden if (sscanf(line, "%u", &u) != 1) {
329 1.10.4.1 wrstuden printf("Invalid sector size `%s'\n", line);
330 1.10.4.1 wrstuden continue;
331 1.10.4.1 wrstuden }
332 1.10.4.1 wrstuden lp->d_cylskew = u;
333 1.10.4.1 wrstuden break;
334 1.10.4.1 wrstuden }
335 1.10.4.1 wrstuden
336 1.10.4.1 wrstuden /* d_headswitch */
337 1.10.4.1 wrstuden for (;;) {
338 1.10.4.1 wrstuden snprintf(def, sizeof def, "%u", lp->d_headswitch);
339 1.10.4.1 wrstuden i = getinput(":", "Head switch time (usec)", def, line);
340 1.10.4.1 wrstuden if (i <= 0)
341 1.10.4.1 wrstuden break;
342 1.10.4.1 wrstuden if (sscanf(line, "%u", &u) != 1) {
343 1.10.4.1 wrstuden printf("Invalid sector size `%s'\n", line);
344 1.10.4.1 wrstuden continue;
345 1.10.4.1 wrstuden }
346 1.10.4.1 wrstuden lp->d_headswitch = u;
347 1.10.4.1 wrstuden break;
348 1.10.4.1 wrstuden }
349 1.10.4.1 wrstuden
350 1.10.4.1 wrstuden /* d_trkseek */
351 1.10.4.1 wrstuden for (;;) {
352 1.10.4.1 wrstuden snprintf(def, sizeof def, "%u", lp->d_trkseek);
353 1.10.4.1 wrstuden i = getinput(":", "Track seek time (usec)", def, line);
354 1.10.4.1 wrstuden if (i <= 0)
355 1.10.4.1 wrstuden break;
356 1.10.4.1 wrstuden if (sscanf(line, "%u", &u) != 1) {
357 1.10.4.1 wrstuden printf("Invalid sector size `%s'\n", line);
358 1.10.4.1 wrstuden continue;
359 1.10.4.1 wrstuden }
360 1.10.4.1 wrstuden lp->d_trkseek = u;
361 1.10.4.1 wrstuden break;
362 1.10.4.1 wrstuden }
363 1.10.4.1 wrstuden
364 1.10.4.1 wrstuden }
365 1.1 christos
366 1.1 christos static void
367 1.1 christos cmd_name(lp, s, fd)
368 1.1 christos struct disklabel *lp;
369 1.1 christos char *s;
370 1.1 christos int fd;
371 1.1 christos {
372 1.1 christos char line[BUFSIZ];
373 1.1 christos int i = getinput(":", "Label name", lp->d_packname, line);
374 1.1 christos
375 1.1 christos if (i <= 0)
376 1.1 christos return;
377 1.1 christos (void) strncpy(lp->d_packname, line, sizeof(lp->d_packname));
378 1.1 christos }
379 1.1 christos
380 1.1 christos static void
381 1.1 christos cmd_round(lp, s, fd)
382 1.1 christos struct disklabel *lp;
383 1.1 christos char *s;
384 1.1 christos int fd;
385 1.1 christos {
386 1.1 christos int i;
387 1.1 christos char line[BUFSIZ];
388 1.1 christos
389 1.1 christos i = getinput(":", "Rounding", rounding ? "cylinders" : "sectors", line);
390 1.1 christos
391 1.1 christos if (i <= 0)
392 1.1 christos return;
393 1.1 christos
394 1.1 christos switch (line[0]) {
395 1.1 christos case 'c':
396 1.1 christos rounding = 1;
397 1.1 christos return;
398 1.1 christos case 's':
399 1.1 christos rounding = 0;
400 1.1 christos return;
401 1.1 christos default:
402 1.1 christos printf("Rounding can be (c)ylinders or (s)ectors\n");
403 1.1 christos return;
404 1.1 christos }
405 1.1 christos }
406 1.1 christos
407 1.1 christos static void
408 1.1 christos cmd_part(lp, s, fd)
409 1.1 christos struct disklabel *lp;
410 1.1 christos char *s;
411 1.1 christos int fd;
412 1.1 christos {
413 1.1 christos int i;
414 1.1 christos char line[BUFSIZ];
415 1.1 christos char def[BUFSIZ];
416 1.1 christos int part = *s - 'a';
417 1.1 christos struct partition *p = &lp->d_partitions[part];
418 1.1 christos
419 1.4 christos if (part >= lp->d_npartitions)
420 1.1 christos lp->d_npartitions = part + 1;
421 1.1 christos
422 1.1 christos for (;;) {
423 1.1 christos deffstypename(def, p->p_fstype);
424 1.1 christos i = getinput(":", "Filesystem type", def, line);
425 1.1 christos if (i <= 0)
426 1.1 christos break;
427 1.1 christos if ((i = getfstypename(line)) == -1) {
428 1.1 christos printf("Invalid file system typename `%s'\n", line);
429 1.1 christos continue;
430 1.1 christos }
431 1.1 christos p->p_fstype = i;
432 1.1 christos break;
433 1.1 christos }
434 1.1 christos for (;;) {
435 1.1 christos defnum(def, lp, p->p_offset);
436 1.1 christos i = getinput(":", "Start offset", def, line);
437 1.1 christos if (i <= 0)
438 1.1 christos break;
439 1.10.4.1 wrstuden if ((i = getnum(line, 0, lp)) == -1) {
440 1.1 christos printf("Bad offset `%s'\n", line);
441 1.1 christos continue;
442 1.1 christos }
443 1.1 christos p->p_offset = i;
444 1.1 christos break;
445 1.1 christos }
446 1.1 christos for (;;) {
447 1.1 christos defnum(def, lp, p->p_size);
448 1.10.4.1 wrstuden i = getinput(":", "Partition size ('$' for all remaining)",
449 1.10.4.1 wrstuden def, line);
450 1.1 christos if (i <= 0)
451 1.1 christos break;
452 1.10.4.1 wrstuden if ((i = getnum(line, lp->d_secperunit - p->p_offset, lp))
453 1.10.4.1 wrstuden == -1) {
454 1.1 christos printf("Bad size `%s'\n", line);
455 1.1 christos continue;
456 1.1 christos }
457 1.1 christos p->p_size = i;
458 1.1 christos break;
459 1.9 christos }
460 1.9 christos
461 1.9 christos if (chaining) {
462 1.9 christos int offs = p[0].p_offset + p[0].p_size;
463 1.9 christos p = lp->d_partitions;
464 1.9 christos part = getrawpartition();
465 1.9 christos for (i = 1; i < lp->d_npartitions; i++) {
466 1.9 christos if (i != part && p[i].p_fstype) {
467 1.9 christos p[i].p_offset = offs;
468 1.9 christos offs = p[i].p_offset + p[i].p_size;
469 1.9 christos }
470 1.9 christos }
471 1.1 christos }
472 1.1 christos }
473 1.1 christos
474 1.1 christos
475 1.1 christos static void
476 1.1 christos cmd_label(lp, s, fd)
477 1.1 christos struct disklabel *lp;
478 1.1 christos char *s;
479 1.1 christos int fd;
480 1.1 christos {
481 1.1 christos char line[BUFSIZ];
482 1.1 christos int i;
483 1.1 christos
484 1.1 christos i = getinput("?", "Label disk", "n", line);
485 1.1 christos
486 1.10 abs if (i <= 0 || (*line != 'y' && *line != 'Y') )
487 1.1 christos return;
488 1.1 christos
489 1.1 christos if (checklabel(lp) != 0) {
490 1.1 christos printf("Label not written\n");
491 1.1 christos return;
492 1.1 christos }
493 1.1 christos
494 1.6 enami if (writelabel(fd, bootarea, lp) != 0) {
495 1.6 enami printf("Label not written\n");
496 1.1 christos return;
497 1.1 christos }
498 1.1 christos printf("Label written\n");
499 1.1 christos }
500 1.1 christos
501 1.1 christos
502 1.1 christos static int
503 1.1 christos runcmd(line, lp, fd)
504 1.1 christos char *line;
505 1.1 christos struct disklabel *lp;
506 1.1 christos int fd;
507 1.1 christos {
508 1.1 christos struct cmds *cmd;
509 1.1 christos
510 1.1 christos for (cmd = cmds; cmd->name != NULL; cmd++)
511 1.1 christos if (strncmp(line, cmd->name, strlen(cmd->name)) == 0) {
512 1.1 christos if (cmd->func == NULL)
513 1.1 christos return -1;
514 1.1 christos (*cmd->func)(lp, line, fd);
515 1.5 christos return 0;
516 1.1 christos }
517 1.2 christos
518 1.2 christos if (line[1] == '\0' &&
519 1.2 christos line[0] >= 'a' && line[0] < 'a' + getmaxpartitions()) {
520 1.2 christos cmd_part(lp, line, fd);
521 1.5 christos return 0;
522 1.2 christos }
523 1.2 christos
524 1.1 christos printf("Unknown command %s\n", line);
525 1.5 christos return 1;
526 1.1 christos }
527 1.1 christos
528 1.1 christos
529 1.1 christos static int
530 1.1 christos getinput(sep, prompt, def, line)
531 1.1 christos const char *sep;
532 1.1 christos const char *prompt;
533 1.1 christos const char *def;
534 1.1 christos char *line;
535 1.1 christos {
536 1.1 christos for (;;) {
537 1.1 christos printf("%s", prompt);
538 1.1 christos if (def)
539 1.1 christos printf(" [%s]", def);
540 1.1 christos printf("%s ", sep);
541 1.1 christos
542 1.1 christos if (fgets(line, BUFSIZ, stdin) == NULL)
543 1.1 christos return -1;
544 1.1 christos if (line[0] == '\n' || line[0] == '\0') {
545 1.1 christos if (def)
546 1.1 christos return 0;
547 1.1 christos }
548 1.1 christos else {
549 1.1 christos char *p;
550 1.1 christos
551 1.1 christos if ((p = strrchr(line, '\n')) != NULL)
552 1.1 christos *p = '\0';
553 1.1 christos return 1;
554 1.1 christos }
555 1.1 christos }
556 1.1 christos }
557 1.1 christos
558 1.1 christos
559 1.1 christos static void
560 1.1 christos defnum(buf, lp, size)
561 1.1 christos char *buf;
562 1.1 christos struct disklabel *lp;
563 1.1 christos int size;
564 1.1 christos {
565 1.1 christos (void) snprintf(buf, BUFSIZ, "%gc, %ds, %gM",
566 1.1 christos size / (float) lp->d_secpercyl,
567 1.1 christos size, size * (lp->d_secsize / (float) (1024 * 1024)));
568 1.1 christos }
569 1.1 christos
570 1.1 christos
571 1.1 christos static int
572 1.10.4.1 wrstuden getnum(buf, max, lp)
573 1.1 christos char *buf;
574 1.10.4.1 wrstuden int max;
575 1.1 christos struct disklabel *lp;
576 1.1 christos {
577 1.1 christos char *ep;
578 1.10.4.1 wrstuden double d;
579 1.1 christos int rv;
580 1.1 christos
581 1.10.4.1 wrstuden if (max && buf[0] == '$' && buf[1] == 0)
582 1.10.4.1 wrstuden return max;
583 1.10.4.1 wrstuden
584 1.10.4.1 wrstuden d = strtod(buf, &ep);
585 1.1 christos if (buf == ep)
586 1.1 christos return -1;
587 1.1 christos
588 1.1 christos #define ROUND(a) ((a / lp->d_secpercyl) + \
589 1.10.4.1 wrstuden ((a % lp->d_secpercyl) ? 1 : 0)) * lp->d_secpercyl
590 1.1 christos
591 1.1 christos switch (*ep) {
592 1.1 christos case '\0':
593 1.1 christos case 's':
594 1.1 christos rv = (int) d;
595 1.1 christos break;
596 1.1 christos
597 1.1 christos case 'c':
598 1.1 christos rv = (int) (d * lp->d_secpercyl);
599 1.1 christos break;
600 1.1 christos
601 1.1 christos case 'M':
602 1.1 christos rv = (int) (d * 1024 * 1024 / lp->d_secsize);
603 1.1 christos break;
604 1.1 christos
605 1.1 christos default:
606 1.1 christos printf("Unit error %c\n", *ep);
607 1.1 christos return -1;
608 1.1 christos }
609 1.1 christos
610 1.1 christos if (rounding)
611 1.1 christos return ROUND(rv);
612 1.1 christos else
613 1.1 christos return rv;
614 1.1 christos }
615 1.1 christos
616 1.1 christos
617 1.1 christos static void
618 1.1 christos deffstypename(buf, i)
619 1.1 christos char *buf;
620 1.1 christos int i;
621 1.1 christos {
622 1.7 bouyer if (i < 0 || i >= FSMAXTYPES)
623 1.1 christos i = 0;
624 1.1 christos (void) strcpy(buf, fstypenames[i]);
625 1.1 christos }
626 1.1 christos
627 1.1 christos
628 1.1 christos static int
629 1.1 christos getfstypename(buf)
630 1.1 christos const char *buf;
631 1.1 christos {
632 1.1 christos int i;
633 1.1 christos
634 1.7 bouyer for (i = 0; i < FSMAXTYPES; i++)
635 1.1 christos if (strcmp(buf, fstypenames[i]) == 0)
636 1.1 christos return i;
637 1.1 christos return -1;
638 1.1 christos }
639 1.1 christos
640 1.1 christos
641 1.1 christos void
642 1.1 christos interact(lp, fd)
643 1.1 christos struct disklabel *lp;
644 1.1 christos int fd;
645 1.1 christos {
646 1.1 christos char line[BUFSIZ];
647 1.1 christos
648 1.1 christos for (;;) {
649 1.1 christos if (getinput(">", "partition", NULL, line) == -1)
650 1.1 christos return;
651 1.1 christos if (runcmd(line, lp, fd) == -1)
652 1.1 christos return;
653 1.1 christos }
654 1.1 christos }
655