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