Home | History | Annotate | Line # | Download | only in disklabel
interact.c revision 1.13.4.1
      1  1.13.4.1        tv /*	$NetBSD: interact.c,v 1.13.4.1 2000/10/18 02:53:09 tv 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.13.4.1        tv __RCSID("$NetBSD: interact.c,v 1.13.4.1 2000/10/18 02:53:09 tv 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.13.4.1        tv static int alphacmp __P((const void *, const void *));
     62       1.1  christos static void defnum __P((char *, struct disklabel *, int));
     63  1.13.4.1        tv static void dumpnames __P((const char *, const char * const *, size_t));
     64      1.12       abs static int getnum __P((char *, int, struct disklabel *));
     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 	if (i <= 0)
    115       1.9  christos 		return;
    116       1.9  christos 
    117       1.9  christos 	switch (line[0]) {
    118       1.9  christos 	case 'y':
    119       1.9  christos 		chaining = 1;
    120       1.9  christos 		return;
    121       1.9  christos 	case 'n':
    122       1.9  christos 		chaining = 0;
    123       1.9  christos 		return;
    124       1.9  christos 	default:
    125       1.9  christos 		printf("Invalid answer\n");
    126       1.9  christos 		return;
    127       1.9  christos 	}
    128       1.9  christos }
    129       1.9  christos 
    130       1.9  christos static void
    131      1.11       mrg cmd_printall(lp, s, fd)
    132      1.11       mrg 	struct disklabel *lp;
    133      1.11       mrg 	char *s;
    134      1.11       mrg 	int fd;
    135      1.11       mrg {
    136      1.11       mrg 
    137      1.11       mrg 	showinfo(stdout, lp);
    138      1.11       mrg 	showpartitions(stdout, lp);
    139      1.11       mrg }
    140      1.11       mrg 
    141      1.11       mrg static void
    142       1.1  christos cmd_print(lp, s, fd)
    143       1.1  christos 	struct disklabel *lp;
    144       1.1  christos 	char *s;
    145       1.1  christos 	int fd;
    146       1.1  christos {
    147       1.9  christos 	showpartitions(stdout, lp);
    148       1.1  christos }
    149       1.1  christos 
    150      1.11       mrg static void
    151      1.11       mrg cmd_info(lp, s, fd)
    152      1.11       mrg 	struct disklabel *lp;
    153      1.11       mrg 	char *s;
    154      1.11       mrg 	int fd;
    155      1.11       mrg {
    156      1.11       mrg 	char line[BUFSIZ];
    157      1.11       mrg 	char def[BUFSIZ];
    158      1.11       mrg 	int v, i;
    159      1.11       mrg 	u_int32_t u;
    160      1.11       mrg 
    161      1.11       mrg 	printf("# Current values:\n");
    162      1.11       mrg 	showinfo(stdout, lp);
    163      1.11       mrg 
    164  1.13.4.1        tv 	/* d_type */
    165      1.11       mrg 	for (;;) {
    166  1.13.4.1        tv 		i = lp->d_type;
    167  1.13.4.1        tv 		if (i < 0 || i >= DKMAXTYPES)
    168  1.13.4.1        tv 			i = 0;
    169  1.13.4.1        tv 		snprintf(def, sizeof(def), "%s", dktypenames[i]);
    170  1.13.4.1        tv 		i = getinput(":", "Disk type [?]", def, line);
    171  1.13.4.1        tv 		if (i == -1)
    172  1.13.4.1        tv 			return;
    173  1.13.4.1        tv 		else if (i == 0)
    174  1.13.4.1        tv 			break;
    175  1.13.4.1        tv 		if (!strcmp(line, "?")) {
    176  1.13.4.1        tv 			dumpnames("Supported disk types", dktypenames,
    177  1.13.4.1        tv 			    DKMAXTYPES);
    178  1.13.4.1        tv 			continue;
    179  1.13.4.1        tv 		}
    180  1.13.4.1        tv 		for (i = 0; i < DKMAXTYPES; i++) {
    181  1.13.4.1        tv 			if (!strcasecmp(dktypenames[i], line)) {
    182  1.13.4.1        tv 				lp->d_type = i;
    183      1.11       mrg 				goto done_typename;
    184      1.11       mrg 			}
    185  1.13.4.1        tv 		}
    186      1.11       mrg 		v = atoi(line);
    187      1.11       mrg 		if ((unsigned)v >= DKMAXTYPES) {
    188  1.13.4.1        tv 			warnx("Unknown disk type: %s", line);
    189      1.11       mrg 			continue;
    190      1.11       mrg 		}
    191      1.11       mrg 		lp->d_type = v;
    192      1.11       mrg done_typename:
    193      1.11       mrg 		break;
    194      1.11       mrg 	}
    195      1.11       mrg 
    196  1.13.4.1        tv 	/* d_typename */
    197  1.13.4.1        tv 	snprintf(def, sizeof(def), "%.*s",
    198  1.13.4.1        tv 	    (int) sizeof(lp->d_typename), lp->d_typename);
    199  1.13.4.1        tv 	i = getinput(":", "Disk name", def, line);
    200  1.13.4.1        tv 	if (i == -1)
    201  1.13.4.1        tv 		return;
    202  1.13.4.1        tv 	else if (i == 1)
    203  1.13.4.1        tv 		(void) strncpy(lp->d_typename, line, sizeof(lp->d_typename));
    204  1.13.4.1        tv 
    205      1.11       mrg 	/* d_packname */
    206      1.11       mrg 	cmd_name(lp, s, fd);
    207      1.11       mrg 
    208      1.11       mrg 	/* d_npartitions */
    209      1.11       mrg 	for (;;) {
    210  1.13.4.1        tv 		snprintf(def, sizeof(def), "%u", lp->d_npartitions);
    211      1.11       mrg 		i = getinput(":", "Number of partitions", def, line);
    212  1.13.4.1        tv 		if (i == -1)
    213  1.13.4.1        tv 			return;
    214  1.13.4.1        tv 		else if (i == 0)
    215      1.11       mrg 			break;
    216      1.11       mrg 		if (sscanf(line, "%u", &u) != 1) {
    217  1.13.4.1        tv 			printf("Invalid number of partitions `%s'\n", line);
    218      1.11       mrg 			continue;
    219      1.11       mrg 		}
    220      1.11       mrg 		lp->d_npartitions = u;
    221      1.11       mrg 		break;
    222      1.11       mrg 	}
    223      1.11       mrg 
    224      1.11       mrg 	/* d_secsize */
    225      1.11       mrg 	for (;;) {
    226  1.13.4.1        tv 		snprintf(def, sizeof(def), "%u", lp->d_secsize);
    227      1.11       mrg 		i = getinput(":", "Sector size (bytes)", def, line);
    228  1.13.4.1        tv 		if (i == -1)
    229  1.13.4.1        tv 			return;
    230  1.13.4.1        tv 		else if (i == 0)
    231      1.11       mrg 			break;
    232      1.11       mrg 		if (sscanf(line, "%u", &u) != 1) {
    233      1.11       mrg 			printf("Invalid sector size `%s'\n", line);
    234      1.11       mrg 			continue;
    235      1.11       mrg 		}
    236      1.11       mrg 		lp->d_secsize = u;
    237      1.11       mrg 		break;
    238      1.11       mrg 	}
    239      1.11       mrg 
    240      1.11       mrg 	/* d_nsectors */
    241      1.11       mrg 	for (;;) {
    242  1.13.4.1        tv 		snprintf(def, sizeof(def), "%u", lp->d_nsectors);
    243      1.11       mrg 		i = getinput(":", "Number of sectors per track", def, line);
    244  1.13.4.1        tv 		if (i == -1)
    245  1.13.4.1        tv 			return;
    246  1.13.4.1        tv 		else if (i == 0)
    247      1.11       mrg 			break;
    248      1.11       mrg 		if (sscanf(line, "%u", &u) != 1) {
    249  1.13.4.1        tv 			printf("Invalid number of sectors `%s'\n", line);
    250      1.11       mrg 			continue;
    251      1.11       mrg 		}
    252      1.11       mrg 		lp->d_nsectors = u;
    253      1.11       mrg 		break;
    254      1.11       mrg 	}
    255      1.11       mrg 
    256      1.11       mrg 	/* d_ntracks */
    257      1.11       mrg 	for (;;) {
    258  1.13.4.1        tv 		snprintf(def, sizeof(def), "%u", lp->d_ntracks);
    259      1.11       mrg 		i = getinput(":", "Number of tracks per cylinder", def, line);
    260  1.13.4.1        tv 		if (i == -1)
    261  1.13.4.1        tv 			return;
    262  1.13.4.1        tv 		else if (i == 0)
    263      1.11       mrg 			break;
    264      1.11       mrg 		if (sscanf(line, "%u", &u) != 1) {
    265      1.11       mrg 			printf("Invalid number of tracks `%s'\n", line);
    266      1.11       mrg 			continue;
    267      1.11       mrg 		}
    268      1.11       mrg 		lp->d_ntracks = u;
    269      1.11       mrg 		break;
    270      1.11       mrg 	}
    271      1.11       mrg 
    272      1.11       mrg 	/* d_secpercyl */
    273      1.11       mrg 	for (;;) {
    274  1.13.4.1        tv 		snprintf(def, sizeof(def), "%u", lp->d_secpercyl);
    275      1.11       mrg 		i = getinput(":", "Number of sectors/cylinder", def, line);
    276  1.13.4.1        tv 		if (i == -1)
    277  1.13.4.1        tv 			return;
    278  1.13.4.1        tv 		else if (i == 0)
    279      1.11       mrg 			break;
    280      1.11       mrg 		if (sscanf(line, "%u", &u) != 1) {
    281  1.13.4.1        tv 			printf("Invalid number of sector/cylinder `%s'\n",
    282  1.13.4.1        tv 			    line);
    283      1.11       mrg 			continue;
    284      1.11       mrg 		}
    285      1.11       mrg 		lp->d_secpercyl = u;
    286      1.11       mrg 		break;
    287      1.11       mrg 	}
    288      1.11       mrg 
    289      1.11       mrg 	/* d_ncylinders */
    290      1.11       mrg 	for (;;) {
    291  1.13.4.1        tv 		snprintf(def, sizeof(def), "%u", lp->d_ncylinders);
    292      1.11       mrg 		i = getinput(":", "Total number of cylinders", def, line);
    293  1.13.4.1        tv 		if (i == -1)
    294  1.13.4.1        tv 			return;
    295  1.13.4.1        tv 		else if (i == 0)
    296      1.11       mrg 			break;
    297      1.11       mrg 		if (sscanf(line, "%u", &u) != 1) {
    298      1.11       mrg 			printf("Invalid sector size `%s'\n", line);
    299      1.11       mrg 			continue;
    300      1.11       mrg 		}
    301      1.11       mrg 		lp->d_ncylinders = u;
    302      1.11       mrg 		break;
    303      1.11       mrg 	}
    304      1.11       mrg 
    305      1.11       mrg 	/* d_secperunit */
    306      1.11       mrg 	for (;;) {
    307  1.13.4.1        tv 		snprintf(def, sizeof(def), "%u", lp->d_secperunit);
    308      1.11       mrg 		i = getinput(":", "Total number of sectors", def, line);
    309  1.13.4.1        tv 		if (i == -1)
    310  1.13.4.1        tv 			return;
    311  1.13.4.1        tv 		else if (i == 0)
    312      1.11       mrg 			break;
    313      1.11       mrg 		if (sscanf(line, "%u", &u) != 1) {
    314  1.13.4.1        tv 			printf("Invalid number of sectors `%s'\n", line);
    315      1.11       mrg 			continue;
    316      1.11       mrg 		}
    317      1.11       mrg 		lp->d_secperunit = u;
    318      1.11       mrg 		break;
    319      1.11       mrg 	}
    320      1.11       mrg 
    321      1.11       mrg 	/* d_rpm */
    322      1.11       mrg 
    323      1.11       mrg 	/* d_interleave */
    324      1.11       mrg 	for (;;) {
    325  1.13.4.1        tv 		snprintf(def, sizeof(def), "%u", lp->d_interleave);
    326      1.11       mrg 		i = getinput(":", "Hardware sectors interleave", def, line);
    327  1.13.4.1        tv 		if (i == -1)
    328  1.13.4.1        tv 			return;
    329  1.13.4.1        tv 		else if (i == 0)
    330      1.11       mrg 			break;
    331      1.11       mrg 		if (sscanf(line, "%u", &u) != 1) {
    332  1.13.4.1        tv 			printf("Invalid sector interleave `%s'\n", line);
    333      1.11       mrg 			continue;
    334      1.11       mrg 		}
    335      1.11       mrg 		lp->d_interleave = u;
    336      1.11       mrg 		break;
    337      1.11       mrg 	}
    338      1.11       mrg 
    339      1.11       mrg 	/* d_trackskew */
    340      1.11       mrg 	for (;;) {
    341  1.13.4.1        tv 		snprintf(def, sizeof(def), "%u", lp->d_trackskew);
    342      1.11       mrg 		i = getinput(":", "Sector 0 skew, per track", def, line);
    343  1.13.4.1        tv 		if (i == -1)
    344  1.13.4.1        tv 			return;
    345  1.13.4.1        tv 		else if (i == 0)
    346      1.11       mrg 			break;
    347      1.11       mrg 		if (sscanf(line, "%u", &u) != 1) {
    348  1.13.4.1        tv 			printf("Invalid track sector skew `%s'\n", line);
    349      1.11       mrg 			continue;
    350      1.11       mrg 		}
    351      1.11       mrg 		lp->d_trackskew = u;
    352      1.11       mrg 		break;
    353      1.11       mrg 	}
    354      1.11       mrg 
    355      1.11       mrg 	/* d_cylskew */
    356      1.11       mrg 	for (;;) {
    357  1.13.4.1        tv 		snprintf(def, sizeof(def), "%u", lp->d_cylskew);
    358      1.11       mrg 		i = getinput(":", "Sector 0 skew, per cylinder", def, line);
    359  1.13.4.1        tv 		if (i == -1)
    360  1.13.4.1        tv 			return;
    361  1.13.4.1        tv 		else if (i == 0)
    362      1.11       mrg 			break;
    363      1.11       mrg 		if (sscanf(line, "%u", &u) != 1) {
    364  1.13.4.1        tv 			printf("Invalid cylinder sector `%s'\n", line);
    365      1.11       mrg 			continue;
    366      1.11       mrg 		}
    367      1.11       mrg 		lp->d_cylskew = u;
    368      1.11       mrg 		break;
    369      1.11       mrg 	}
    370      1.11       mrg 
    371      1.11       mrg 	/* d_headswitch */
    372      1.11       mrg 	for (;;) {
    373  1.13.4.1        tv 		snprintf(def, sizeof(def), "%u", lp->d_headswitch);
    374      1.11       mrg 		i = getinput(":", "Head switch time (usec)", def, line);
    375  1.13.4.1        tv 		if (i == -1)
    376  1.13.4.1        tv 			return;
    377  1.13.4.1        tv 		else if (i == 0)
    378      1.11       mrg 			break;
    379      1.11       mrg 		if (sscanf(line, "%u", &u) != 1) {
    380  1.13.4.1        tv 			printf("Invalid head switch time `%s'\n", line);
    381      1.11       mrg 			continue;
    382      1.11       mrg 		}
    383      1.11       mrg 		lp->d_headswitch = u;
    384      1.11       mrg 		break;
    385      1.11       mrg 	}
    386      1.11       mrg 
    387      1.11       mrg 	/* d_trkseek */
    388      1.11       mrg 	for (;;) {
    389  1.13.4.1        tv 		snprintf(def, sizeof(def), "%u", lp->d_trkseek);
    390      1.11       mrg 		i = getinput(":", "Track seek time (usec)", def, line);
    391  1.13.4.1        tv 		if (i == -1)
    392  1.13.4.1        tv 			return;
    393  1.13.4.1        tv 		else if (i == 0)
    394      1.11       mrg 			break;
    395      1.11       mrg 		if (sscanf(line, "%u", &u) != 1) {
    396  1.13.4.1        tv 			printf("Invalid track seek time `%s'\n", line);
    397      1.11       mrg 			continue;
    398      1.11       mrg 		}
    399      1.11       mrg 		lp->d_trkseek = u;
    400      1.11       mrg 		break;
    401      1.11       mrg 	}
    402      1.11       mrg }
    403       1.1  christos 
    404       1.1  christos static void
    405       1.1  christos cmd_name(lp, s, fd)
    406       1.1  christos 	struct disklabel *lp;
    407       1.1  christos 	char *s;
    408       1.1  christos 	int fd;
    409       1.1  christos {
    410       1.1  christos 	char line[BUFSIZ];
    411  1.13.4.1        tv 	char def[BUFSIZ];
    412  1.13.4.1        tv 	int i;
    413       1.1  christos 
    414  1.13.4.1        tv 	snprintf(def, sizeof(def), "%.*s",
    415  1.13.4.1        tv 	    (int) sizeof(lp->d_packname), lp->d_packname);
    416  1.13.4.1        tv 	i = getinput(":", "Label name", def, line);
    417       1.1  christos 	if (i <= 0)
    418       1.1  christos 		return;
    419       1.1  christos 	(void) strncpy(lp->d_packname, line, sizeof(lp->d_packname));
    420       1.1  christos }
    421       1.1  christos 
    422       1.1  christos static void
    423       1.1  christos cmd_round(lp, s, fd)
    424       1.1  christos 	struct disklabel *lp;
    425       1.1  christos 	char *s;
    426       1.1  christos 	int fd;
    427       1.1  christos {
    428       1.1  christos 	int i;
    429       1.1  christos 	char line[BUFSIZ];
    430       1.1  christos 
    431       1.1  christos 	i = getinput(":", "Rounding", rounding ? "cylinders" : "sectors", line);
    432       1.1  christos 	if (i <= 0)
    433       1.1  christos 		return;
    434       1.1  christos 
    435       1.1  christos 	switch (line[0]) {
    436       1.1  christos 	case 'c':
    437       1.1  christos 		rounding = 1;
    438       1.1  christos 		return;
    439       1.1  christos 	case 's':
    440       1.1  christos 		rounding = 0;
    441       1.1  christos 		return;
    442       1.1  christos 	default:
    443       1.1  christos 		printf("Rounding can be (c)ylinders or (s)ectors\n");
    444       1.1  christos 		return;
    445       1.1  christos 	}
    446       1.1  christos }
    447       1.1  christos 
    448       1.1  christos static void
    449       1.1  christos cmd_part(lp, s, fd)
    450       1.1  christos 	struct disklabel *lp;
    451       1.1  christos 	char *s;
    452       1.1  christos 	int fd;
    453       1.1  christos {
    454       1.1  christos 	int i;
    455       1.1  christos 	char line[BUFSIZ];
    456       1.1  christos 	char def[BUFSIZ];
    457       1.1  christos 	int part = *s - 'a';
    458       1.1  christos 	struct partition *p = &lp->d_partitions[part];
    459       1.1  christos 
    460       1.4  christos 	if (part >= lp->d_npartitions)
    461       1.1  christos 		lp->d_npartitions = part + 1;
    462       1.1  christos 
    463       1.1  christos 	for (;;) {
    464  1.13.4.1        tv 		i = p->p_fstype;
    465  1.13.4.1        tv 		if (i < 0 || i >= FSMAXTYPES)
    466  1.13.4.1        tv 			i = 0;
    467  1.13.4.1        tv 		snprintf(def, sizeof(def), "%s", fstypenames[i]);
    468  1.13.4.1        tv 		i = getinput(":", "Filesystem type [?]", def, line);
    469  1.13.4.1        tv 		if (i == -1)
    470  1.13.4.1        tv 			return;
    471  1.13.4.1        tv 		else if (i == 0)
    472  1.13.4.1        tv 			break;
    473  1.13.4.1        tv 		if (!strcmp(line, "?")) {
    474  1.13.4.1        tv 			dumpnames("Supported file system types",
    475  1.13.4.1        tv 			    fstypenames, FSMAXTYPES);
    476  1.13.4.1        tv 			continue;
    477  1.13.4.1        tv 		}
    478  1.13.4.1        tv 		for (i = 0; i < FSMAXTYPES; i++)
    479  1.13.4.1        tv 			if (!strcasecmp(line, fstypenames[i])) {
    480  1.13.4.1        tv 				p->p_fstype = i;
    481  1.13.4.1        tv 				goto done_typename;
    482  1.13.4.1        tv 			}
    483  1.13.4.1        tv 		printf("Invalid file system typename `%s'\n", line);
    484  1.13.4.1        tv 		continue;
    485  1.13.4.1        tv done_typename:
    486       1.1  christos 		break;
    487       1.1  christos 	}
    488       1.1  christos 	for (;;) {
    489       1.1  christos 		defnum(def, lp, p->p_offset);
    490       1.1  christos 		i = getinput(":", "Start offset", def, line);
    491  1.13.4.1        tv 		if (i == -1)
    492  1.13.4.1        tv 			return;
    493  1.13.4.1        tv 		else if (i == 0)
    494       1.1  christos 			break;
    495      1.12       abs 		if ((i = getnum(line, 0, lp)) == -1) {
    496       1.1  christos 			printf("Bad offset `%s'\n", line);
    497       1.1  christos 			continue;
    498       1.1  christos 		}
    499       1.1  christos 		p->p_offset = i;
    500       1.1  christos 		break;
    501       1.1  christos 	}
    502       1.1  christos 	for (;;) {
    503       1.1  christos 		defnum(def, lp, p->p_size);
    504      1.13       abs 		i = getinput(":", "Partition size ('$' for all remaining)",
    505      1.12       abs 		    def, line);
    506  1.13.4.1        tv 		if (i == -1)
    507  1.13.4.1        tv 			return;
    508  1.13.4.1        tv 		else if (i == 0)
    509       1.1  christos 			break;
    510      1.12       abs 		if ((i = getnum(line, lp->d_secperunit - p->p_offset, lp))
    511      1.12       abs 		    == -1) {
    512       1.1  christos 			printf("Bad size `%s'\n", line);
    513       1.1  christos 			continue;
    514       1.1  christos 		}
    515       1.1  christos 		p->p_size = i;
    516       1.1  christos 		break;
    517       1.9  christos 	}
    518       1.9  christos 
    519       1.9  christos 	if (chaining) {
    520       1.9  christos 		int offs = p[0].p_offset + p[0].p_size;
    521       1.9  christos 		p = lp->d_partitions;
    522       1.9  christos 		part = getrawpartition();
    523       1.9  christos 		for (i = 1; i < lp->d_npartitions; i++) {
    524       1.9  christos 			if (i != part && p[i].p_fstype) {
    525       1.9  christos 				p[i].p_offset = offs;
    526       1.9  christos 				offs = p[i].p_offset + p[i].p_size;
    527       1.9  christos 			}
    528       1.9  christos 		}
    529       1.1  christos 	}
    530       1.1  christos }
    531       1.1  christos 
    532       1.1  christos 
    533       1.1  christos static void
    534       1.1  christos cmd_label(lp, s, fd)
    535       1.1  christos 	struct disklabel *lp;
    536       1.1  christos 	char *s;
    537       1.1  christos 	int fd;
    538       1.1  christos {
    539       1.1  christos 	char line[BUFSIZ];
    540       1.1  christos 	int i;
    541       1.1  christos 
    542       1.1  christos 	i = getinput("?", "Label disk", "n", line);
    543      1.10       abs 	if (i <= 0 || (*line != 'y' && *line != 'Y') )
    544       1.1  christos 		return;
    545       1.1  christos 
    546       1.1  christos 	if (checklabel(lp) != 0) {
    547       1.1  christos 		printf("Label not written\n");
    548       1.1  christos 		return;
    549       1.1  christos 	}
    550       1.1  christos 
    551       1.6     enami 	if (writelabel(fd, bootarea, lp) != 0) {
    552       1.6     enami 		printf("Label not written\n");
    553       1.1  christos 		return;
    554       1.1  christos 	}
    555       1.1  christos 	printf("Label written\n");
    556       1.1  christos }
    557       1.1  christos 
    558       1.1  christos 
    559       1.1  christos static int
    560       1.1  christos runcmd(line, lp, fd)
    561       1.1  christos 	char *line;
    562       1.1  christos 	struct disklabel *lp;
    563       1.1  christos 	int fd;
    564       1.1  christos {
    565       1.1  christos 	struct cmds *cmd;
    566       1.1  christos 
    567       1.1  christos 	for (cmd = cmds; cmd->name != NULL; cmd++)
    568       1.1  christos 		if (strncmp(line, cmd->name, strlen(cmd->name)) == 0) {
    569       1.1  christos 			if (cmd->func == NULL)
    570       1.1  christos 				return -1;
    571       1.1  christos 			(*cmd->func)(lp, line, fd);
    572       1.5  christos 			return 0;
    573       1.1  christos 		}
    574       1.2  christos 
    575       1.2  christos 	if (line[1] == '\0' &&
    576       1.2  christos 	    line[0] >= 'a' && line[0] < 'a' + getmaxpartitions()) {
    577       1.2  christos 		cmd_part(lp, line, fd);
    578       1.5  christos 		return 0;
    579       1.2  christos 	}
    580       1.2  christos 
    581       1.1  christos 	printf("Unknown command %s\n", line);
    582       1.5  christos 	return 1;
    583       1.1  christos }
    584       1.1  christos 
    585       1.1  christos 
    586       1.1  christos static int
    587       1.1  christos getinput(sep, prompt, def, line)
    588       1.1  christos 	const char *sep;
    589       1.1  christos 	const char *prompt;
    590       1.1  christos 	const char *def;
    591       1.1  christos 	char *line;
    592       1.1  christos {
    593       1.1  christos 	for (;;) {
    594       1.1  christos 		printf("%s", prompt);
    595       1.1  christos 		if (def)
    596       1.1  christos 			printf(" [%s]", def);
    597       1.1  christos 		printf("%s ", sep);
    598       1.1  christos 
    599       1.1  christos 		if (fgets(line, BUFSIZ, stdin) == NULL)
    600       1.1  christos 			return -1;
    601       1.1  christos 		if (line[0] == '\n' || line[0] == '\0') {
    602       1.1  christos 			if (def)
    603       1.1  christos 				return 0;
    604       1.1  christos 		}
    605       1.1  christos 		else {
    606       1.1  christos 			char *p;
    607       1.1  christos 
    608       1.1  christos 			if ((p = strrchr(line, '\n')) != NULL)
    609       1.1  christos 				*p = '\0';
    610       1.1  christos 			return 1;
    611       1.1  christos 		}
    612       1.1  christos 	}
    613       1.1  christos }
    614       1.1  christos 
    615  1.13.4.1        tv static int
    616  1.13.4.1        tv alphacmp(a, b)
    617  1.13.4.1        tv 	const void *a, *b;
    618  1.13.4.1        tv {
    619  1.13.4.1        tv 
    620  1.13.4.1        tv 	return (strcasecmp(*(const char **)a, *(const char **)b));
    621  1.13.4.1        tv }
    622  1.13.4.1        tv 
    623  1.13.4.1        tv 
    624  1.13.4.1        tv static void
    625  1.13.4.1        tv dumpnames(prompt, olist, numentries)
    626  1.13.4.1        tv 	const char *prompt;
    627  1.13.4.1        tv 	const char * const *olist;
    628  1.13.4.1        tv 	size_t numentries;
    629  1.13.4.1        tv {
    630  1.13.4.1        tv 	int i, j, w;
    631  1.13.4.1        tv 	int columns, width, lines;
    632  1.13.4.1        tv 	const char *p;
    633  1.13.4.1        tv 	const char **list;
    634  1.13.4.1        tv 
    635  1.13.4.1        tv 	list = (const char **)malloc(sizeof(char *) * numentries);
    636  1.13.4.1        tv 	width = 0;
    637  1.13.4.1        tv 	printf("%s:\n", prompt);
    638  1.13.4.1        tv 	for (i = 0; i < numentries; i++) {
    639  1.13.4.1        tv 		list[i] = olist[i];
    640  1.13.4.1        tv 		w = strlen(list[i]);
    641  1.13.4.1        tv 		if (w > width)
    642  1.13.4.1        tv 			width = w;
    643  1.13.4.1        tv 	}
    644  1.13.4.1        tv #if 0
    645  1.13.4.1        tv 	for (i = 0; i < numentries; i++)
    646  1.13.4.1        tv 		printf("%s%s", i == 0 ? "" : ", ", list[i]);
    647  1.13.4.1        tv 	puts("");
    648  1.13.4.1        tv #endif
    649  1.13.4.1        tv 	(void)qsort((void *)list, numentries, sizeof(char *), alphacmp);
    650  1.13.4.1        tv 	width++;		/* want two spaces between items */
    651  1.13.4.1        tv 	width = (width + 8) &~ 7;
    652  1.13.4.1        tv 
    653  1.13.4.1        tv #define ttywidth 72
    654  1.13.4.1        tv 	columns = ttywidth / width;
    655  1.13.4.1        tv #undef ttywidth
    656  1.13.4.1        tv 	if (columns == 0)
    657  1.13.4.1        tv 		columns = 1;
    658  1.13.4.1        tv 	lines = (numentries + columns - 1) / columns;
    659  1.13.4.1        tv 	for (i = 0; i < lines; i++) {
    660  1.13.4.1        tv 		for (j = 0; j < columns; j++) {
    661  1.13.4.1        tv 			p = list[j * lines + i];
    662  1.13.4.1        tv 			if (j == 0)
    663  1.13.4.1        tv 				putc('\t', stdout);
    664  1.13.4.1        tv 			if (p) {
    665  1.13.4.1        tv 				fputs(p, stdout);
    666  1.13.4.1        tv 			}
    667  1.13.4.1        tv 			if (j * lines + i + lines >= numentries) {
    668  1.13.4.1        tv 				putc('\n', stdout);
    669  1.13.4.1        tv 				break;
    670  1.13.4.1        tv 			}
    671  1.13.4.1        tv 			w = strlen(p);
    672  1.13.4.1        tv 			while (w < width) {
    673  1.13.4.1        tv 				w = (w + 8) &~ 7;
    674  1.13.4.1        tv 				putc('\t', stdout);
    675  1.13.4.1        tv 			}
    676  1.13.4.1        tv 		}
    677  1.13.4.1        tv 	}
    678  1.13.4.1        tv 	free(list);
    679  1.13.4.1        tv }
    680  1.13.4.1        tv 
    681       1.1  christos 
    682       1.1  christos static void
    683       1.1  christos defnum(buf, lp, size)
    684       1.1  christos 	char *buf;
    685       1.1  christos 	struct disklabel *lp;
    686       1.1  christos 	int size;
    687       1.1  christos {
    688       1.1  christos 	(void) snprintf(buf, BUFSIZ, "%gc, %ds, %gM",
    689       1.1  christos 	    size / (float) lp->d_secpercyl,
    690       1.1  christos 	    size, size  * (lp->d_secsize / (float) (1024 * 1024)));
    691       1.1  christos }
    692       1.1  christos 
    693       1.1  christos 
    694       1.1  christos static int
    695      1.12       abs getnum(buf, max, lp)
    696       1.1  christos 	char *buf;
    697      1.12       abs 	int max;
    698       1.1  christos 	struct disklabel *lp;
    699       1.1  christos {
    700       1.1  christos 	char *ep;
    701      1.12       abs 	double d;
    702       1.1  christos 	int rv;
    703       1.1  christos 
    704      1.13       abs 	if (max && buf[0] == '$' && buf[1] == 0)
    705      1.12       abs 		return max;
    706      1.12       abs 
    707      1.12       abs 	d = strtod(buf, &ep);
    708       1.1  christos 	if (buf == ep)
    709       1.1  christos 		return -1;
    710       1.1  christos 
    711       1.1  christos #define ROUND(a)	((a / lp->d_secpercyl) + \
    712      1.12       abs 		 ((a % lp->d_secpercyl) ? 1 : 0)) * lp->d_secpercyl
    713       1.1  christos 
    714       1.1  christos 	switch (*ep) {
    715       1.1  christos 	case '\0':
    716       1.1  christos 	case 's':
    717       1.1  christos 		rv = (int) d;
    718       1.1  christos 		break;
    719       1.1  christos 
    720       1.1  christos 	case 'c':
    721       1.1  christos 		rv = (int) (d * lp->d_secpercyl);
    722       1.1  christos 		break;
    723       1.1  christos 
    724  1.13.4.1        tv 	case 'm':
    725       1.1  christos 	case 'M':
    726       1.1  christos 		rv =  (int) (d * 1024 * 1024 / lp->d_secsize);
    727       1.1  christos 		break;
    728       1.1  christos 
    729       1.1  christos 	default:
    730       1.1  christos 		printf("Unit error %c\n", *ep);
    731       1.1  christos 		return -1;
    732       1.1  christos 	}
    733       1.1  christos 
    734       1.1  christos 	if (rounding)
    735       1.1  christos 		return ROUND(rv);
    736       1.1  christos 	else
    737       1.1  christos 		return rv;
    738       1.1  christos }
    739       1.1  christos 
    740       1.1  christos 
    741       1.1  christos void
    742       1.1  christos interact(lp, fd)
    743       1.1  christos 	struct disklabel *lp;
    744       1.1  christos 	int fd;
    745       1.1  christos {
    746       1.1  christos 	char line[BUFSIZ];
    747       1.1  christos 
    748       1.1  christos 	for (;;) {
    749       1.1  christos 		if (getinput(">", "partition", NULL, line) == -1)
    750       1.1  christos 			return;
    751       1.1  christos 		if (runcmd(line, lp, fd) == -1)
    752       1.1  christos 			return;
    753       1.1  christos 	}
    754       1.1  christos }
    755