interact.c revision 1.11 1 1.11 mrg /* $NetBSD: interact.c,v 1.11 1999/11/26 06:03:10 mrg 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.11 mrg __RCSID("$NetBSD: interact.c,v 1.11 1999/11/26 06:03:10 mrg Exp $");
35 1.1 christos #endif /* lint */
36 1.1 christos
37 1.11 mrg #include <sys/param.h>
38 1.11 mrg #define FSTYPENAMES
39 1.11 mrg #define DKTYPENAMES
40 1.11 mrg #include <sys/disklabel.h>
41 1.11 mrg
42 1.11 mrg #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.11 mrg static void cmd_printall __P((struct disklabel *, char *, int));
54 1.11 mrg 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.1 christos static int getnum __P((char *, 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.11 mrg { "E", cmd_printall, "print disk label and current partition table"},
77 1.11 mrg { "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.11 mrg cmd_printall(lp, s, fd)
133 1.11 mrg struct disklabel *lp;
134 1.11 mrg char *s;
135 1.11 mrg int fd;
136 1.11 mrg {
137 1.11 mrg
138 1.11 mrg showinfo(stdout, lp);
139 1.11 mrg showpartitions(stdout, lp);
140 1.11 mrg }
141 1.11 mrg
142 1.11 mrg 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.11 mrg static void
152 1.11 mrg cmd_info(lp, s, fd)
153 1.11 mrg struct disklabel *lp;
154 1.11 mrg char *s;
155 1.11 mrg int fd;
156 1.11 mrg {
157 1.11 mrg char line[BUFSIZ];
158 1.11 mrg char def[BUFSIZ];
159 1.11 mrg const char * const *cpp;
160 1.11 mrg const char *t;
161 1.11 mrg int v, i;
162 1.11 mrg u_int32_t u;
163 1.11 mrg
164 1.11 mrg printf("# Current values:\n");
165 1.11 mrg showinfo(stdout, lp);
166 1.11 mrg
167 1.11 mrg /* d_typename */
168 1.11 mrg for (;;) {
169 1.11 mrg strncpy(def, lp->d_typename, sizeof(def));
170 1.11 mrg def[sizeof(def) - 1] = '\0';
171 1.11 mrg i = getinput(":", "Disk type", def, line);
172 1.11 mrg if (i <= 0)
173 1.11 mrg break;
174 1.11 mrg cpp = dktypenames;
175 1.11 mrg for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
176 1.11 mrg if ((t = *cpp) && !strcmp(t, line)) {
177 1.11 mrg lp->d_type = cpp - dktypenames;
178 1.11 mrg goto done_typename;
179 1.11 mrg }
180 1.11 mrg v = atoi(line);
181 1.11 mrg if ((unsigned)v >= DKMAXTYPES) {
182 1.11 mrg warnx("unknown disk type: %s", line);
183 1.11 mrg continue;
184 1.11 mrg }
185 1.11 mrg lp->d_type = v;
186 1.11 mrg done_typename:
187 1.11 mrg break;
188 1.11 mrg }
189 1.11 mrg
190 1.11 mrg /* d_packname */
191 1.11 mrg cmd_name(lp, s, fd);
192 1.11 mrg
193 1.11 mrg /* d_npartitions */
194 1.11 mrg for (;;) {
195 1.11 mrg snprintf(def, sizeof def, "%u", lp->d_npartitions);
196 1.11 mrg i = getinput(":", "Number of partitions", def, line);
197 1.11 mrg if (i <= 0)
198 1.11 mrg break;
199 1.11 mrg if (sscanf(line, "%u", &u) != 1) {
200 1.11 mrg printf("Invalid sector size `%s'\n", line);
201 1.11 mrg continue;
202 1.11 mrg }
203 1.11 mrg lp->d_npartitions = u;
204 1.11 mrg break;
205 1.11 mrg }
206 1.11 mrg
207 1.11 mrg /* d_secsize */
208 1.11 mrg for (;;) {
209 1.11 mrg snprintf(def, sizeof def, "%u", lp->d_secsize);
210 1.11 mrg i = getinput(":", "Sector size (bytes)", def, line);
211 1.11 mrg if (i <= 0)
212 1.11 mrg break;
213 1.11 mrg if (sscanf(line, "%u", &u) != 1) {
214 1.11 mrg printf("Invalid sector size `%s'\n", line);
215 1.11 mrg continue;
216 1.11 mrg }
217 1.11 mrg lp->d_secsize = u;
218 1.11 mrg break;
219 1.11 mrg }
220 1.11 mrg
221 1.11 mrg /* d_nsectors */
222 1.11 mrg for (;;) {
223 1.11 mrg snprintf(def, sizeof def, "%u", lp->d_nsectors);
224 1.11 mrg i = getinput(":", "Number of sectors per track", def, line);
225 1.11 mrg if (i <= 0)
226 1.11 mrg break;
227 1.11 mrg if (sscanf(line, "%u", &u) != 1) {
228 1.11 mrg printf("Invalid number of sector `%s'\n", line);
229 1.11 mrg continue;
230 1.11 mrg }
231 1.11 mrg lp->d_nsectors = u;
232 1.11 mrg break;
233 1.11 mrg }
234 1.11 mrg
235 1.11 mrg /* d_ntracks */
236 1.11 mrg for (;;) {
237 1.11 mrg snprintf(def, sizeof def, "%u", lp->d_ntracks);
238 1.11 mrg i = getinput(":", "Number of tracks per cylinder", def, line);
239 1.11 mrg if (i <= 0)
240 1.11 mrg break;
241 1.11 mrg if (sscanf(line, "%u", &u) != 1) {
242 1.11 mrg printf("Invalid number of tracks `%s'\n", line);
243 1.11 mrg continue;
244 1.11 mrg }
245 1.11 mrg lp->d_ntracks = u;
246 1.11 mrg break;
247 1.11 mrg }
248 1.11 mrg
249 1.11 mrg /* d_secpercyl */
250 1.11 mrg for (;;) {
251 1.11 mrg snprintf(def, sizeof def, "%u", lp->d_secpercyl);
252 1.11 mrg i = getinput(":", "Number of sectors/cylinder", def, line);
253 1.11 mrg if (i <= 0)
254 1.11 mrg break;
255 1.11 mrg if (sscanf(line, "%u", &u) != 1) {
256 1.11 mrg printf("Invalid number of sector/cylinder `%s'\n", line);
257 1.11 mrg continue;
258 1.11 mrg }
259 1.11 mrg lp->d_secpercyl = u;
260 1.11 mrg break;
261 1.11 mrg }
262 1.11 mrg
263 1.11 mrg /* d_ncylinders */
264 1.11 mrg for (;;) {
265 1.11 mrg snprintf(def, sizeof def, "%u", lp->d_ncylinders);
266 1.11 mrg i = getinput(":", "Total number of cylinders", def, line);
267 1.11 mrg if (i <= 0)
268 1.11 mrg break;
269 1.11 mrg if (sscanf(line, "%u", &u) != 1) {
270 1.11 mrg printf("Invalid sector size `%s'\n", line);
271 1.11 mrg continue;
272 1.11 mrg }
273 1.11 mrg lp->d_ncylinders = u;
274 1.11 mrg break;
275 1.11 mrg }
276 1.11 mrg
277 1.11 mrg /* d_secperunit */
278 1.11 mrg for (;;) {
279 1.11 mrg snprintf(def, sizeof def, "%u", lp->d_secperunit);
280 1.11 mrg i = getinput(":", "Total number of sectors", def, line);
281 1.11 mrg if (i <= 0)
282 1.11 mrg break;
283 1.11 mrg if (sscanf(line, "%u", &u) != 1) {
284 1.11 mrg printf("Invalid number of sector `%s'\n", line);
285 1.11 mrg continue;
286 1.11 mrg }
287 1.11 mrg lp->d_secperunit = u;
288 1.11 mrg break;
289 1.11 mrg }
290 1.11 mrg
291 1.11 mrg /* d_rpm */
292 1.11 mrg
293 1.11 mrg /* d_interleave */
294 1.11 mrg for (;;) {
295 1.11 mrg snprintf(def, sizeof def, "%u", lp->d_interleave);
296 1.11 mrg i = getinput(":", "Hardware sectors interleave", def, line);
297 1.11 mrg
298 1.11 mrg if (i <= 0)
299 1.11 mrg break;
300 1.11 mrg if (sscanf(line, "%u", &u) != 1) {
301 1.11 mrg printf("Invalid sector size `%s'\n", line);
302 1.11 mrg continue;
303 1.11 mrg }
304 1.11 mrg lp->d_interleave = u;
305 1.11 mrg break;
306 1.11 mrg }
307 1.11 mrg
308 1.11 mrg /* d_trackskew */
309 1.11 mrg for (;;) {
310 1.11 mrg snprintf(def, sizeof def, "%u", lp->d_trackskew);
311 1.11 mrg i = getinput(":", "Sector 0 skew, per track", def, line);
312 1.11 mrg if (i <= 0)
313 1.11 mrg break;
314 1.11 mrg if (sscanf(line, "%u", &u) != 1) {
315 1.11 mrg printf("Invalid sector size `%s'\n", line);
316 1.11 mrg continue;
317 1.11 mrg }
318 1.11 mrg lp->d_trackskew = u;
319 1.11 mrg break;
320 1.11 mrg }
321 1.11 mrg
322 1.11 mrg /* d_cylskew */
323 1.11 mrg for (;;) {
324 1.11 mrg snprintf(def, sizeof def, "%u", lp->d_cylskew);
325 1.11 mrg i = getinput(":", "Sector 0 skew, per cylinder", def, line);
326 1.11 mrg if (i <= 0)
327 1.11 mrg break;
328 1.11 mrg if (sscanf(line, "%u", &u) != 1) {
329 1.11 mrg printf("Invalid sector size `%s'\n", line);
330 1.11 mrg continue;
331 1.11 mrg }
332 1.11 mrg lp->d_cylskew = u;
333 1.11 mrg break;
334 1.11 mrg }
335 1.11 mrg
336 1.11 mrg /* d_headswitch */
337 1.11 mrg for (;;) {
338 1.11 mrg snprintf(def, sizeof def, "%u", lp->d_headswitch);
339 1.11 mrg i = getinput(":", "Head switch time (usec)", def, line);
340 1.11 mrg if (i <= 0)
341 1.11 mrg break;
342 1.11 mrg if (sscanf(line, "%u", &u) != 1) {
343 1.11 mrg printf("Invalid sector size `%s'\n", line);
344 1.11 mrg continue;
345 1.11 mrg }
346 1.11 mrg lp->d_headswitch = u;
347 1.11 mrg break;
348 1.11 mrg }
349 1.11 mrg
350 1.11 mrg /* d_trkseek */
351 1.11 mrg for (;;) {
352 1.11 mrg snprintf(def, sizeof def, "%u", lp->d_trkseek);
353 1.11 mrg i = getinput(":", "Track seek time (usec)", def, line);
354 1.11 mrg if (i <= 0)
355 1.11 mrg break;
356 1.11 mrg if (sscanf(line, "%u", &u) != 1) {
357 1.11 mrg printf("Invalid sector size `%s'\n", line);
358 1.11 mrg continue;
359 1.11 mrg }
360 1.11 mrg lp->d_trkseek = u;
361 1.11 mrg break;
362 1.11 mrg }
363 1.11 mrg
364 1.11 mrg }
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.1 christos if ((i = getnum(line, 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.1 christos i = getinput(":", "Partition size", def, line);
449 1.1 christos if (i <= 0)
450 1.1 christos break;
451 1.1 christos if ((i = getnum(line, lp)) == -1) {
452 1.1 christos printf("Bad size `%s'\n", line);
453 1.1 christos continue;
454 1.1 christos }
455 1.1 christos p->p_size = i;
456 1.1 christos break;
457 1.9 christos }
458 1.9 christos
459 1.9 christos if (chaining) {
460 1.9 christos int offs = p[0].p_offset + p[0].p_size;
461 1.9 christos p = lp->d_partitions;
462 1.9 christos part = getrawpartition();
463 1.9 christos for (i = 1; i < lp->d_npartitions; i++) {
464 1.9 christos if (i != part && p[i].p_fstype) {
465 1.9 christos p[i].p_offset = offs;
466 1.9 christos offs = p[i].p_offset + p[i].p_size;
467 1.9 christos }
468 1.9 christos }
469 1.1 christos }
470 1.1 christos }
471 1.1 christos
472 1.1 christos
473 1.1 christos static void
474 1.1 christos cmd_label(lp, s, fd)
475 1.1 christos struct disklabel *lp;
476 1.1 christos char *s;
477 1.1 christos int fd;
478 1.1 christos {
479 1.1 christos char line[BUFSIZ];
480 1.1 christos int i;
481 1.1 christos
482 1.1 christos i = getinput("?", "Label disk", "n", line);
483 1.1 christos
484 1.10 abs if (i <= 0 || (*line != 'y' && *line != 'Y') )
485 1.1 christos return;
486 1.1 christos
487 1.1 christos if (checklabel(lp) != 0) {
488 1.1 christos printf("Label not written\n");
489 1.1 christos return;
490 1.1 christos }
491 1.1 christos
492 1.6 enami if (writelabel(fd, bootarea, lp) != 0) {
493 1.6 enami printf("Label not written\n");
494 1.1 christos return;
495 1.1 christos }
496 1.1 christos printf("Label written\n");
497 1.1 christos }
498 1.1 christos
499 1.1 christos
500 1.1 christos static int
501 1.1 christos runcmd(line, lp, fd)
502 1.1 christos char *line;
503 1.1 christos struct disklabel *lp;
504 1.1 christos int fd;
505 1.1 christos {
506 1.1 christos struct cmds *cmd;
507 1.1 christos
508 1.1 christos for (cmd = cmds; cmd->name != NULL; cmd++)
509 1.1 christos if (strncmp(line, cmd->name, strlen(cmd->name)) == 0) {
510 1.1 christos if (cmd->func == NULL)
511 1.1 christos return -1;
512 1.1 christos (*cmd->func)(lp, line, fd);
513 1.5 christos return 0;
514 1.1 christos }
515 1.2 christos
516 1.2 christos if (line[1] == '\0' &&
517 1.2 christos line[0] >= 'a' && line[0] < 'a' + getmaxpartitions()) {
518 1.2 christos cmd_part(lp, line, fd);
519 1.5 christos return 0;
520 1.2 christos }
521 1.2 christos
522 1.1 christos printf("Unknown command %s\n", line);
523 1.5 christos return 1;
524 1.1 christos }
525 1.1 christos
526 1.1 christos
527 1.1 christos static int
528 1.1 christos getinput(sep, prompt, def, line)
529 1.1 christos const char *sep;
530 1.1 christos const char *prompt;
531 1.1 christos const char *def;
532 1.1 christos char *line;
533 1.1 christos {
534 1.1 christos for (;;) {
535 1.1 christos printf("%s", prompt);
536 1.1 christos if (def)
537 1.1 christos printf(" [%s]", def);
538 1.1 christos printf("%s ", sep);
539 1.1 christos
540 1.1 christos if (fgets(line, BUFSIZ, stdin) == NULL)
541 1.1 christos return -1;
542 1.1 christos if (line[0] == '\n' || line[0] == '\0') {
543 1.1 christos if (def)
544 1.1 christos return 0;
545 1.1 christos }
546 1.1 christos else {
547 1.1 christos char *p;
548 1.1 christos
549 1.1 christos if ((p = strrchr(line, '\n')) != NULL)
550 1.1 christos *p = '\0';
551 1.1 christos return 1;
552 1.1 christos }
553 1.1 christos }
554 1.1 christos }
555 1.1 christos
556 1.1 christos
557 1.1 christos static void
558 1.1 christos defnum(buf, lp, size)
559 1.1 christos char *buf;
560 1.1 christos struct disklabel *lp;
561 1.1 christos int size;
562 1.1 christos {
563 1.1 christos (void) snprintf(buf, BUFSIZ, "%gc, %ds, %gM",
564 1.1 christos size / (float) lp->d_secpercyl,
565 1.1 christos size, size * (lp->d_secsize / (float) (1024 * 1024)));
566 1.1 christos }
567 1.1 christos
568 1.1 christos
569 1.1 christos static int
570 1.1 christos getnum(buf, lp)
571 1.1 christos char *buf;
572 1.1 christos struct disklabel *lp;
573 1.1 christos {
574 1.1 christos char *ep;
575 1.1 christos double d = strtod(buf, &ep);
576 1.1 christos int rv;
577 1.1 christos
578 1.1 christos if (buf == ep)
579 1.1 christos return -1;
580 1.1 christos
581 1.1 christos #define ROUND(a) ((a / lp->d_secpercyl) + \
582 1.1 christos ((a % lp->d_secpercyl) ? 1 : 0)) * lp->d_secpercyl
583 1.1 christos
584 1.1 christos switch (*ep) {
585 1.1 christos case '\0':
586 1.1 christos case 's':
587 1.1 christos rv = (int) d;
588 1.1 christos break;
589 1.1 christos
590 1.1 christos case 'c':
591 1.1 christos rv = (int) (d * lp->d_secpercyl);
592 1.1 christos break;
593 1.1 christos
594 1.1 christos case 'M':
595 1.1 christos rv = (int) (d * 1024 * 1024 / lp->d_secsize);
596 1.1 christos break;
597 1.1 christos
598 1.1 christos default:
599 1.1 christos printf("Unit error %c\n", *ep);
600 1.1 christos return -1;
601 1.1 christos }
602 1.1 christos
603 1.1 christos if (rounding)
604 1.1 christos return ROUND(rv);
605 1.1 christos else
606 1.1 christos return rv;
607 1.1 christos }
608 1.1 christos
609 1.1 christos
610 1.1 christos static void
611 1.1 christos deffstypename(buf, i)
612 1.1 christos char *buf;
613 1.1 christos int i;
614 1.1 christos {
615 1.7 bouyer if (i < 0 || i >= FSMAXTYPES)
616 1.1 christos i = 0;
617 1.1 christos (void) strcpy(buf, fstypenames[i]);
618 1.1 christos }
619 1.1 christos
620 1.1 christos
621 1.1 christos static int
622 1.1 christos getfstypename(buf)
623 1.1 christos const char *buf;
624 1.1 christos {
625 1.1 christos int i;
626 1.1 christos
627 1.7 bouyer for (i = 0; i < FSMAXTYPES; i++)
628 1.1 christos if (strcmp(buf, fstypenames[i]) == 0)
629 1.1 christos return i;
630 1.1 christos return -1;
631 1.1 christos }
632 1.1 christos
633 1.1 christos
634 1.1 christos void
635 1.1 christos interact(lp, fd)
636 1.1 christos struct disklabel *lp;
637 1.1 christos int fd;
638 1.1 christos {
639 1.1 christos char line[BUFSIZ];
640 1.1 christos
641 1.1 christos for (;;) {
642 1.1 christos if (getinput(">", "partition", NULL, line) == -1)
643 1.1 christos return;
644 1.1 christos if (runcmd(line, lp, fd) == -1)
645 1.1 christos return;
646 1.1 christos }
647 1.1 christos }
648