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