Home | History | Annotate | Line # | Download | only in edlabel
      1  1.1  abs /*	$NetBSD: edlabel.c,v 1.1 2010/03/10 23:16:16 abs Exp $	*/
      2  1.1  abs 
      3  1.1  abs /*
      4  1.1  abs  * Copyright (c) 1995 Gordon W. Ross
      5  1.1  abs  * All rights reserved.
      6  1.1  abs  *
      7  1.1  abs  * Redistribution and use in source and binary forms, with or without
      8  1.1  abs  * modification, are permitted provided that the following conditions
      9  1.1  abs  * are met:
     10  1.1  abs  * 1. Redistributions of source code must retain the above copyright
     11  1.1  abs  *    notice, this list of conditions and the following disclaimer.
     12  1.1  abs  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  abs  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  abs  *    documentation and/or other materials provided with the distribution.
     15  1.1  abs  *
     16  1.1  abs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  abs  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  abs  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  abs  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  abs  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  1.1  abs  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  1.1  abs  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  1.1  abs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  1.1  abs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  1.1  abs  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  1.1  abs  */
     27  1.1  abs 
     28  1.1  abs #include <sys/cdefs.h>
     29  1.1  abs #include <sys/types.h>
     30  1.1  abs #include <sys/param.h>
     31  1.1  abs #include <sys/ioctl.h>
     32  1.1  abs #define FSTYPENAMES
     33  1.1  abs #include <sys/disklabel.h>
     34  1.1  abs 
     35  1.1  abs #include <fcntl.h>
     36  1.1  abs #include <stdio.h>
     37  1.1  abs #include <ctype.h>
     38  1.1  abs #include <string.h>
     39  1.1  abs #include <errno.h>
     40  1.1  abs #include <unistd.h>
     41  1.1  abs #include <util.h>
     42  1.1  abs #include <stdlib.h>
     43  1.1  abs 
     44  1.1  abs /*
     45  1.1  abs  * Machine dependent constants you want to retrieve only once...
     46  1.1  abs  */
     47  1.1  abs int rawpartition, maxpartitions;
     48  1.1  abs 
     49  1.1  abs /*
     50  1.1  abs  * This is a data-driven program
     51  1.1  abs  */
     52  1.1  abs struct field {
     53  1.1  abs 	const char *f_name;
     54  1.1  abs 	int f_offset;
     55  1.1  abs 	int f_type;	/* 1:char, 2:short, 4:int, >4:string */
     56  1.1  abs };
     57  1.1  abs 
     58  1.1  abs /* Table describing fields in the head of a disklabel. */
     59  1.1  abs #define	dloff(f) (int)(&((struct disklabel *)0)->f)
     60  1.1  abs struct field label_head[] = {
     61  1.1  abs   { "        type_num", dloff(d_type), 2 },
     62  1.1  abs   { "        sub_type", dloff(d_subtype), 2 },
     63  1.1  abs   { "       type_name", dloff(d_typename), 16 },
     64  1.1  abs   { "       pack_name", dloff(d_packname),  16 },
     65  1.1  abs   { "    bytes/sector", dloff(d_secsize), 4 },
     66  1.1  abs   { "   sectors/track", dloff(d_nsectors), 4 },
     67  1.1  abs   { " tracks/cylinder", dloff(d_ntracks),  4 },
     68  1.1  abs   { "       cylinders", dloff(d_ncylinders), 4 },
     69  1.1  abs   { "sectors/cylinder", dloff(d_secpercyl), 4 },
     70  1.1  abs   /* Don't care about the others until later... */
     71  1.1  abs   { .f_name = NULL },
     72  1.1  abs };
     73  1.1  abs #undef dloff
     74  1.1  abs 
     75  1.1  abs void	check_divisors(struct disklabel *);
     76  1.1  abs u_short	dkcksum(struct disklabel *);
     77  1.1  abs void	edit_geo(struct disklabel *);
     78  1.1  abs void	edit_head_all(struct disklabel *, int);
     79  1.1  abs void	edit_head_field(void *, struct field *, int);
     80  1.1  abs void	edit_partition(struct disklabel *, int, int);
     81  1.1  abs void	get_fstype(char *, u_int8_t *);
     82  1.1  abs void	get_val_cts(struct disklabel *, char *, u_int32_t *);
     83  1.1  abs void	label_modify(struct disklabel *, char *);
     84  1.1  abs void	label_print(struct disklabel *, char *);
     85  1.1  abs void	label_quit(struct disklabel *, char *);
     86  1.1  abs void	label_read(struct disklabel *, char *);
     87  1.1  abs void	label_write(struct disklabel *, char *);
     88  1.1  abs void	menu(void);
     89  1.1  abs void	print_val_cts(struct disklabel *, u_long val);
     90  1.1  abs 
     91  1.1  abs char	tmpbuf[64];
     92  1.1  abs 
     93  1.1  abs void
     94  1.1  abs edit_head_field(void *v, struct field *f, int modify /* also modify */)
     95  1.1  abs {
     96  1.1  abs 	u_int8_t  *cp;
     97  1.1  abs 	u_int tmp;
     98  1.1  abs 
     99  1.1  abs 	cp = v;
    100  1.1  abs 	cp += f->f_offset;
    101  1.1  abs 
    102  1.1  abs 	printf("%s: ", f->f_name);
    103  1.1  abs 
    104  1.1  abs 	/* Print current value... */
    105  1.1  abs 	switch (f->f_type) {
    106  1.1  abs 	case 1:
    107  1.1  abs 		tmp = *cp;
    108  1.1  abs 		printf("%d", tmp);
    109  1.1  abs 		break;
    110  1.1  abs 	case 2:
    111  1.1  abs 		tmp = *((u_int16_t *)cp);
    112  1.1  abs 		printf("%d", tmp);
    113  1.1  abs 		break;
    114  1.1  abs 	case 4:
    115  1.1  abs 		tmp = *((u_int32_t *)cp);
    116  1.1  abs 		printf("%d", tmp);
    117  1.1  abs 		break;
    118  1.1  abs 
    119  1.1  abs 	default:
    120  1.1  abs 		/* must be a string. */
    121  1.1  abs 		strlcpy(tmpbuf, (char*)cp, sizeof(tmpbuf));
    122  1.1  abs 		printf("%s", tmpbuf);
    123  1.1  abs 		break;
    124  1.1  abs 	}
    125  1.1  abs 
    126  1.1  abs 	if (modify == 0) {
    127  1.1  abs 		printf("\n");
    128  1.1  abs 		return;
    129  1.1  abs 	}
    130  1.1  abs 	printf(" ? ");
    131  1.1  abs 	fflush(stdout);
    132  1.1  abs 
    133  1.1  abs 	tmpbuf[0] = '\0';
    134  1.1  abs 	if (fgets(tmpbuf, sizeof(tmpbuf), stdin) == NULL)
    135  1.1  abs 		return;
    136  1.1  abs 	if ((tmpbuf[0] == '\0') || (tmpbuf[0] == '\n')) {
    137  1.1  abs 		/* no new value supplied. */
    138  1.1  abs 		return;
    139  1.1  abs 	}
    140  1.1  abs 
    141  1.1  abs 	/* store new value */
    142  1.1  abs 	if (f->f_type <= 4)
    143  1.1  abs 		if (sscanf(tmpbuf, "%d", &tmp) != 1)
    144  1.1  abs 			return;
    145  1.1  abs 
    146  1.1  abs 	switch (f->f_type) {
    147  1.1  abs 	case 1:
    148  1.1  abs 		*cp = tmp;
    149  1.1  abs 		break;
    150  1.1  abs 	case 2:
    151  1.1  abs 		*((u_int16_t *)cp) = tmp;
    152  1.1  abs 		break;
    153  1.1  abs 	case 4:
    154  1.1  abs 		*((u_int32_t *)cp) = tmp;
    155  1.1  abs 		break;
    156  1.1  abs 	default:
    157  1.1  abs 		/* Get rid of the trailing newline. */
    158  1.1  abs 		tmp = strlen(tmpbuf);
    159  1.1  abs 		if (tmp < 1)
    160  1.1  abs 			break;
    161  1.1  abs 		if (tmpbuf[tmp-1] == '\n')
    162  1.1  abs 			tmpbuf[tmp-1] = '\0';
    163  1.1  abs 		strncpy((char*)cp, tmpbuf, f->f_type);
    164  1.1  abs 		break;
    165  1.1  abs 	}
    166  1.1  abs }
    167  1.1  abs 
    168  1.1  abs void
    169  1.1  abs edit_head_all(struct disklabel *d, int modify)
    170  1.1  abs {
    171  1.1  abs 	struct field *f;
    172  1.1  abs 
    173  1.1  abs 	/* Edit head stuff. */
    174  1.1  abs 	for (f = label_head; f->f_name; f++)
    175  1.1  abs 		edit_head_field(d, f, modify);
    176  1.1  abs }
    177  1.1  abs 
    178  1.1  abs void
    179  1.1  abs edit_geo(struct disklabel *d)
    180  1.1  abs {
    181  1.1  abs 	int nsect, ntrack, ncyl, spc;
    182  1.1  abs 
    183  1.1  abs 	nsect = ntrack = ncyl = spc = 0;
    184  1.1  abs 
    185  1.1  abs 	printf("Sectors/track: ");
    186  1.1  abs 	fflush(stdout);
    187  1.1  abs 	if (fgets(tmpbuf, sizeof(tmpbuf), stdin) == NULL)
    188  1.1  abs 		return;
    189  1.1  abs 	if (sscanf(tmpbuf, "%d", &nsect) != 1)
    190  1.1  abs 		nsect = d->d_nsectors;
    191  1.1  abs 	printf("Track/cyl: ");
    192  1.1  abs 	fflush(stdout);
    193  1.1  abs 	if (fgets(tmpbuf, sizeof(tmpbuf), stdin) == NULL)
    194  1.1  abs 		return;
    195  1.1  abs 	if (sscanf(tmpbuf, "%d", &ntrack) != 1)
    196  1.1  abs 		ntrack = d->d_ntracks;
    197  1.1  abs 	if (!nsect || !ntrack)
    198  1.1  abs 		return;
    199  1.1  abs 	spc = nsect * ntrack;
    200  1.1  abs 	if (!(ncyl = d->d_secperunit / spc))
    201  1.1  abs 		return;
    202  1.1  abs 	d->d_nsectors   = nsect;
    203  1.1  abs 	d->d_ntracks    = ntrack;
    204  1.1  abs 	d->d_ncylinders = ncyl;
    205  1.1  abs 	d->d_secpercyl  = spc;
    206  1.1  abs }
    207  1.1  abs 
    208  1.1  abs void
    209  1.1  abs print_val_cts(struct disklabel *d, u_long val)
    210  1.1  abs {
    211  1.1  abs 	int	sects, trks, cyls;
    212  1.1  abs 	char	marker;
    213  1.1  abs 	char	buf[80];
    214  1.1  abs 
    215  1.1  abs 	marker = (val % d->d_secpercyl) ? '*' : ' ';
    216  1.1  abs 	sects  = val % d->d_nsectors;
    217  1.1  abs 	cyls   = val / d->d_nsectors;
    218  1.1  abs 	trks   = cyls % d->d_ntracks;
    219  1.1  abs 	cyls  /= d->d_ntracks;
    220  1.1  abs 	snprintf(buf, sizeof(buf), "(%d/%02d/%02d)%c", cyls, trks, sects,
    221  1.1  abs 	    marker);
    222  1.1  abs 	printf(" %9ld %16s", val, buf);
    223  1.1  abs }
    224  1.1  abs 
    225  1.1  abs void
    226  1.1  abs get_val_cts(struct disklabel *d, char *buf, u_int32_t *result)
    227  1.1  abs {
    228  1.1  abs 	u_long tmp;
    229  1.1  abs 	int	cyls, trks, sects;
    230  1.1  abs 
    231  1.1  abs 	tmp = sscanf(buf, "%d/%d/%d", &cyls, &trks, &sects);
    232  1.1  abs 	if (tmp == 1)
    233  1.1  abs 		*result = cyls;	/* really nblks! */
    234  1.1  abs 	if (tmp == 3) {
    235  1.1  abs 		tmp = cyls;
    236  1.1  abs 		tmp *= d->d_ntracks;
    237  1.1  abs 		tmp += trks;
    238  1.1  abs 		tmp *= d->d_nsectors;
    239  1.1  abs 		tmp += sects;
    240  1.1  abs 		*result = tmp;
    241  1.1  abs 	}
    242  1.1  abs }
    243  1.1  abs 
    244  1.1  abs void
    245  1.1  abs get_fstype(char *buf, u_int8_t *fstype)
    246  1.1  abs {
    247  1.1  abs 	int	i, len;
    248  1.1  abs 
    249  1.1  abs 	/* An empty response retains previous value */
    250  1.1  abs 	if (buf[0] == '\n')
    251  1.1  abs 		return;
    252  1.1  abs 	for (i = 0, len = strlen(buf) - 1; i < FSMAXTYPES; i++) {
    253  1.1  abs 		if (!strncasecmp(buf, fstypenames[i], len)) {
    254  1.1  abs 			*fstype = i;
    255  1.1  abs 			return;
    256  1.1  abs 		}
    257  1.1  abs 	}
    258  1.1  abs }
    259  1.1  abs 
    260  1.1  abs void
    261  1.1  abs edit_partition(struct disklabel *d, int idx, int modify)
    262  1.1  abs {
    263  1.1  abs 	struct partition *p;
    264  1.1  abs 	char letter;
    265  1.1  abs 	const char *comment;
    266  1.1  abs 
    267  1.1  abs 	if ((idx < 0) || (idx >= maxpartitions)) {
    268  1.1  abs 		printf("bad partition index\n");
    269  1.1  abs 		return;
    270  1.1  abs 	}
    271  1.1  abs 
    272  1.1  abs 	p = &d->d_partitions[idx];
    273  1.1  abs 	letter = 'a' + idx;
    274  1.1  abs 
    275  1.1  abs 	/* Set hint about partition type */
    276  1.1  abs 	if (idx == rawpartition)
    277  1.1  abs 		comment = "disk";
    278  1.1  abs 	else {
    279  1.1  abs 		comment = "user";
    280  1.1  abs 		switch(idx) {
    281  1.1  abs 			case 0:
    282  1.1  abs 				comment = "root";
    283  1.1  abs 			break;
    284  1.1  abs 			case 1:
    285  1.1  abs 				comment = "swap";
    286  1.1  abs 				break;
    287  1.1  abs 		}
    288  1.1  abs 	}
    289  1.1  abs 
    290  1.1  abs 	/* Print current value... */
    291  1.1  abs 	printf(" %c (%s) ", letter, comment);
    292  1.1  abs 	print_val_cts(d, p->p_offset);
    293  1.1  abs 	print_val_cts(d, p->p_size);
    294  1.1  abs 	printf(" %s\n", fstypenames[p->p_fstype]);
    295  1.1  abs 
    296  1.1  abs 	if (modify == 0)
    297  1.1  abs 		return;
    298  1.1  abs 
    299  1.1  abs 	/* starting block, or cyls/trks/sects */
    300  1.1  abs 	printf("start as <blkno> or <cyls/trks/sects> : ");
    301  1.1  abs 	fflush(stdout);
    302  1.1  abs 	if (fgets(tmpbuf, sizeof(tmpbuf), stdin) == NULL)
    303  1.1  abs 		return;
    304  1.1  abs 	get_val_cts(d, tmpbuf, &p->p_offset);
    305  1.1  abs 
    306  1.1  abs 	/* number of blocks, or cyls/trks/sects */
    307  1.1  abs 	printf("length as <nblks> or <cyls/trks/sects> : ");
    308  1.1  abs 	fflush(stdout);
    309  1.1  abs 	if (fgets(tmpbuf, sizeof(tmpbuf), stdin) == NULL)
    310  1.1  abs 		return;
    311  1.1  abs 	get_val_cts(d, tmpbuf, &p->p_size);
    312  1.1  abs 	/* partition type */
    313  1.1  abs 	printf("type: ");
    314  1.1  abs 	fflush(stdout);
    315  1.1  abs 	if (fgets(tmpbuf, sizeof(tmpbuf), stdin) == NULL)
    316  1.1  abs 		return;
    317  1.1  abs 	get_fstype(tmpbuf, &p->p_fstype);
    318  1.1  abs }
    319  1.1  abs 
    320  1.1  abs /*****************************************************************/
    321  1.1  abs 
    322  1.1  abs void
    323  1.1  abs check_divisors(struct disklabel *d)
    324  1.1  abs {
    325  1.1  abs 	if (d->d_nsectors == 0) {
    326  1.1  abs 		d->d_nsectors = 1;
    327  1.1  abs 		printf("bad sect/trk, set to 1\n");
    328  1.1  abs 	}
    329  1.1  abs 	if (d->d_ntracks == 0) {
    330  1.1  abs 		d->d_ntracks = 1;
    331  1.1  abs 		printf("bad trks/cyl, set to 1\n");
    332  1.1  abs 	}
    333  1.1  abs 	if (d->d_ncylinders == 0) {
    334  1.1  abs 		d->d_ncylinders = 1;
    335  1.1  abs 		printf("bad cyls, set to 1\n");
    336  1.1  abs 	}
    337  1.1  abs 	if (d->d_secpercyl == 0) {
    338  1.1  abs 		d->d_secpercyl = (d->d_nsectors * d->d_ntracks);
    339  1.1  abs 		printf("bad sect/cyl, set to %d\n", d->d_secpercyl);
    340  1.1  abs 	}
    341  1.1  abs 
    342  1.1  abs }
    343  1.1  abs 
    344  1.1  abs u_short
    345  1.1  abs dkcksum(struct disklabel *d)
    346  1.1  abs {
    347  1.1  abs 	u_short *start, *end;
    348  1.1  abs 	u_short sum = 0;
    349  1.1  abs 
    350  1.1  abs 	start = (u_short *)d;
    351  1.1  abs 	end = (u_short *)&d->d_partitions[d->d_npartitions];
    352  1.1  abs 	while (start < end)
    353  1.1  abs 		sum ^= *start++;
    354  1.1  abs 	return (sum);
    355  1.1  abs }
    356  1.1  abs 
    357  1.1  abs void
    358  1.1  abs label_write(struct disklabel *d, char *dn)
    359  1.1  abs {
    360  1.1  abs 	int fd;
    361  1.1  abs 
    362  1.1  abs 	d->d_magic = DISKMAGIC;
    363  1.1  abs 	d->d_magic2 = DISKMAGIC;
    364  1.1  abs 	d->d_checksum = 0;
    365  1.1  abs 	d->d_checksum = dkcksum(d);
    366  1.1  abs 
    367  1.1  abs 	fd = open(dn, O_RDWR, 0);
    368  1.1  abs 	if (fd < 0) {
    369  1.1  abs 		perror(dn);
    370  1.1  abs 		return;
    371  1.1  abs 	}
    372  1.1  abs 	if (ioctl(fd, DIOCWDINFO, d) < 0) {
    373  1.1  abs 		perror("ioctl DIOCWDINFO");
    374  1.1  abs 	}
    375  1.1  abs 	close(fd);
    376  1.1  abs }
    377  1.1  abs 
    378  1.1  abs void
    379  1.1  abs label_read(struct disklabel *dl, char *dn)
    380  1.1  abs {
    381  1.1  abs 	int fd;
    382  1.1  abs 
    383  1.1  abs 	fd = open(dn, O_RDONLY, 0);
    384  1.1  abs 	if (fd < 0) {
    385  1.1  abs 		perror(dn);
    386  1.1  abs 		exit(1);
    387  1.1  abs 	}
    388  1.1  abs 	if (ioctl(fd, DIOCGDINFO, dl) < 0) {
    389  1.1  abs 		if (errno == ESRCH)
    390  1.1  abs 			fprintf(stderr, "edlabel: No disk label on disk\n");
    391  1.1  abs 		else
    392  1.1  abs 		    	perror("ioctl DIOCGDINFO");
    393  1.1  abs 		exit(1);
    394  1.1  abs 	}
    395  1.1  abs 
    396  1.1  abs 	/* Make sure divisors are non-zero. */
    397  1.1  abs 	check_divisors(dl);
    398  1.1  abs 
    399  1.1  abs 	close(fd);
    400  1.1  abs }
    401  1.1  abs 
    402  1.1  abs /*****************************************************************/
    403  1.1  abs 
    404  1.1  abs void
    405  1.1  abs label_print(struct disklabel *dl, char *dn)
    406  1.1  abs {
    407  1.1  abs 	int i;
    408  1.1  abs 
    409  1.1  abs 	/* Print out head stuff. */
    410  1.1  abs 	edit_head_all(dl, 0);
    411  1.1  abs 
    412  1.1  abs 	/* And the partition header. */
    413  1.1  abs 	printf("partition%6sstart%9s(c/t/s)%6snblks%9s(c/t/s)  type\n\n"
    414  1.1  abs 							"", "", "", "", "");
    415  1.1  abs 	for (i = 0; i < dl->d_npartitions; i++)
    416  1.1  abs 		edit_partition(dl, i, 0);
    417  1.1  abs }
    418  1.1  abs 
    419  1.1  abs char modify_cmds[] = "modify subcommands:\n\
    420  1.1  abs  @   : modify disk parameters\n\
    421  1.1  abs  a-%c : modify partition\n%s\
    422  1.1  abs  q   : quit this subcommand\n";
    423  1.1  abs 
    424  1.1  abs void
    425  1.1  abs label_modify(struct disklabel *dl, char *dn)
    426  1.1  abs {
    427  1.1  abs 	int c, i;
    428  1.1  abs 	int scsi_fict = 0;
    429  1.1  abs 
    430  1.1  abs 	if (!strcmp(dl->d_typename, "SCSI disk")
    431  1.1  abs 	     && !strcmp(dl->d_packname, "fictitious"))
    432  1.1  abs 		scsi_fict = 1;
    433  1.1  abs 
    434  1.1  abs 	printf(modify_cmds, 'a' + maxpartitions - 1,
    435  1.1  abs 		scsi_fict ? " s   : standardize geometry\n" : "");
    436  1.1  abs 	for (;;) {
    437  1.1  abs 		printf("edlabel/modify> ");
    438  1.1  abs 		fflush(stdout);
    439  1.1  abs 		if (fgets(tmpbuf, sizeof(tmpbuf), stdin) == NULL)
    440  1.1  abs 			break;
    441  1.1  abs 		c = tmpbuf[0];
    442  1.1  abs 		if ((c == '\0') || (c == '\n'))
    443  1.1  abs 			continue;	/* blank line */
    444  1.1  abs 		if (c == 'q')
    445  1.1  abs 			break;
    446  1.1  abs 		if (c == '@') {
    447  1.1  abs 			edit_head_all(dl, 1);
    448  1.1  abs 			check_divisors(dl);
    449  1.1  abs 			continue;
    450  1.1  abs 		}
    451  1.1  abs 		if ((c == 's') && scsi_fict) {
    452  1.1  abs 			edit_geo(dl);
    453  1.1  abs 			continue;
    454  1.1  abs 		}
    455  1.1  abs 		if ((c < 'a') || (c > 'q')) {
    456  1.1  abs 			printf("bad input.  ");
    457  1.1  abs 			printf(modify_cmds, 'a' + maxpartitions - 1,
    458  1.1  abs 			    scsi_fict ? " s   : standardize geometry\n" : "");
    459  1.1  abs 			continue;
    460  1.1  abs 		}
    461  1.1  abs 		edit_partition(dl, c - 'a', 1);
    462  1.1  abs 	}
    463  1.1  abs 	/* Set the d_npartitions field correctly */
    464  1.1  abs 	for (i = 0; i < maxpartitions; i++) {
    465  1.1  abs 		if (dl->d_partitions[i].p_size)
    466  1.1  abs 			dl->d_npartitions = i + 1;
    467  1.1  abs 	}
    468  1.1  abs 
    469  1.1  abs }
    470  1.1  abs 
    471  1.1  abs void
    472  1.1  abs label_quit(struct disklabel *dl, char *dn)
    473  1.1  abs {
    474  1.1  abs 	exit(0);
    475  1.1  abs }
    476  1.1  abs 
    477  1.1  abs struct cmd {
    478  1.1  abs 	void (*cmd_func)(struct disklabel *, char *);
    479  1.1  abs 	const char *cmd_name;
    480  1.1  abs 	const char *cmd_descr;
    481  1.1  abs } cmds[] = {
    482  1.1  abs 	{ label_print,  "print",  "display the current disk label" },
    483  1.1  abs 	{ label_modify, "modify", "prompt for changes to the label" },
    484  1.1  abs 	{ label_write,  "write",  "write the new label to disk" },
    485  1.1  abs 	{ label_quit,   "quit",   "terminate program" },
    486  1.1  abs 	{ .cmd_func = 0 },
    487  1.1  abs };
    488  1.1  abs 
    489  1.1  abs void
    490  1.1  abs menu(void)
    491  1.1  abs {
    492  1.1  abs 	struct cmd *cmd;
    493  1.1  abs 
    494  1.1  abs 	printf("edlabel menu:\n");
    495  1.1  abs 	for (cmd = cmds; cmd->cmd_func; cmd++)
    496  1.1  abs 		printf("%s\t- %s\n", cmd->cmd_name, cmd->cmd_descr);
    497  1.1  abs }
    498  1.1  abs 
    499  1.1  abs int
    500  1.1  abs main(int argc, char **argv)
    501  1.1  abs {
    502  1.1  abs 	struct disklabel dl;
    503  1.1  abs 	struct cmd *cmd;
    504  1.1  abs 	char *dev_name;
    505  1.1  abs 
    506  1.1  abs 	if (argc != 2) {
    507  1.1  abs 		fprintf(stderr, "usage: edlabel RAWDISK\n");
    508  1.1  abs 		exit(1);
    509  1.1  abs 	}
    510  1.1  abs 	dev_name = argv[1];
    511  1.1  abs 
    512  1.1  abs 	rawpartition = getrawpartition();
    513  1.1  abs 	maxpartitions = getmaxpartitions();
    514  1.1  abs 
    515  1.1  abs 	label_read(&dl, dev_name);
    516  1.1  abs 
    517  1.1  abs 	menu();
    518  1.1  abs 
    519  1.1  abs 	for (;;) {
    520  1.1  abs 		printf("edlabel> ");
    521  1.1  abs 		fflush(stdout);
    522  1.1  abs 		if (fgets(tmpbuf, sizeof(tmpbuf), stdin) == NULL)
    523  1.1  abs 			break;
    524  1.1  abs 		for (cmd = cmds; cmd->cmd_func; cmd++)
    525  1.1  abs 			if (cmd->cmd_name[0] == tmpbuf[0])
    526  1.1  abs 				goto found;
    527  1.1  abs 		printf("Invalid command.  ");
    528  1.1  abs 		menu();
    529  1.1  abs 		continue;
    530  1.1  abs 
    531  1.1  abs 	found:
    532  1.1  abs 		cmd->cmd_func(&dl, dev_name);
    533  1.1  abs 	}
    534  1.1  abs 	exit(0);
    535  1.1  abs }
    536