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