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