Home | History | Annotate | Line # | Download | only in sunlabel
sunlabel.c revision 1.3
      1 /* $NetBSD: sunlabel.c,v 1.3 2002/01/10 21:43:10 christos Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Der Mouse.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __RCSID("$NetBSD: sunlabel.c,v 1.3 2002/01/10 21:43:10 christos Exp $");
     41 
     42 #include <stdio.h>
     43 #include <errno.h>
     44 #include <ctype.h>
     45 #include <stdlib.h>
     46 #include <unistd.h>
     47 #include <termcap.h>
     48 #include <strings.h>
     49 #include <inttypes.h>
     50 #include <err.h>
     51 #include <util.h>
     52 #include <sys/file.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/disklabel.h>
     55 
     56 /*
     57  * NPART is the total number of partitions.  This must be <= 43, given the
     58  * amount of space available to store extended partitions. It also must be
     59  * <=26, given the use of single letters to name partitions.  The 8 is the
     60  * number of `standard' partitions; this arguably should be a #define, since
     61  * it occurs not only here but scattered throughout the code.
     62  */
     63 #define NPART 16
     64 #define NXPART (NPART - 8)
     65 #define PARTLETTER(i) ((i) + 'a')
     66 #define LETTERPART(i) ((i) - 'a')
     67 
     68 /*
     69  * A partition.  We keep redundant information around, making sure
     70  * that whenever we change one, we keep another constant and update
     71  * the third.  Which one is which depends.  Arguably a partition
     72  * should also know its partition number; here, if we need that we
     73  * cheat, using (effectively) ptr-&label.partitions[0].
     74  */
     75 struct part {
     76 	uint32_t    startcyl;
     77 	uint32_t    nblk;
     78 	uint32_t    endcyl;
     79 };
     80 
     81 /*
     82  * A label.  As the embedded comments indicate, much of this structure
     83  * corresponds directly to Sun's struct dk_label.  Some of the values
     84  * here are historical holdovers.  Apparently really old Suns did
     85  * their own sparing in software, so a sector or two per cylinder,
     86  * plus a whole cylinder or two at the end, got set aside as spares.
     87  * acyl and apc count those spares, and this is also why ncyl and pcyl
     88  * both exist.  These days the spares generally are hidden from the
     89  * host by the disk, and there's no reason not to set
     90  * ncyl=pcyl=ceil(device size/spc) and acyl=apc=0.
     91  *
     92  * Note also that the geometry assumptions behind having nhead and
     93  * nsect assume that the sect/trk and trk/cyl values are constant
     94  * across the whole drive.  The latter is still usually true; the
     95  * former isn't.  In my experience, you can just put fixed values
     96  * here; the basis for software knowing the drive geometry is also
     97  * mostly invalid these days anyway.  (I just use nhead=32 nsect=64,
     98  * which gives me 1M "cylinders", a convenient size.)
     99  */
    100 struct label {
    101 	/* BEGIN fields taken directly from struct dk_label */
    102 	char asciilabel[128];
    103 	uint32_t rpm;	/* Spindle rotation speed - useless now */
    104 	uint32_t pcyl;	/* Physical cylinders */
    105 	uint32_t apc;	/* Alternative sectors per cylinder */
    106 	uint32_t obs1;	/* Obsolete? */
    107 	uint32_t obs2;	/* Obsolete? */
    108 	uint32_t intrlv;	/* Interleave - never anything but 1 IME */
    109 	uint32_t ncyl;	/* Number of usable cylinders */
    110 	uint32_t acyl;	/* Alternative cylinders - pcyl minus ncyl */
    111 	uint32_t nhead;	/* Tracks-per-cylinder (usually # of heads) */
    112 	uint32_t nsect;	/* Sectors-per-track */
    113 	uint32_t obs3;	/* Obsolete? */
    114 	uint32_t obs4;	/* Obsolete? */
    115 	/* END fields taken directly from struct dk_label */
    116 	uint32_t spc;	/* Sectors per cylinder - nhead*nsect */
    117 	uint32_t dirty:1;/* Modified since last read */
    118 	struct part partitions[NPART];/* The partitions themselves */
    119 };
    120 
    121 /*
    122  * Describes a field in the label.
    123  *
    124  * tag is a short name for the field, like "apc" or "nsect".  loc is a
    125  * pointer to the place in the label where it's stored.  print is a
    126  * function to print the value; the second argument is the current
    127  * column number, and the return value is the new current column
    128  * number.  (This allows print functions to do proper line wrapping.)
    129  * chval is called to change a field; the first argument is the
    130  * command line portion that contains the new value (in text form).
    131  * The chval function is responsible for parsing and error-checking as
    132  * well as doing the modification.  changed is a function which does
    133  * field-specific actions necessary when the field has been changed.
    134  * This could be rolled into the chval function, but I believe this
    135  * way provides better code sharing.
    136  *
    137  * Note that while the fields in the label vary in size (8, 16, or 32
    138  * bits), we store everything as ints in the label struct, above, and
    139  * convert when packing and unpacking.  This allows us to have only
    140  * one numeric chval function.
    141  */
    142 struct field {
    143 	const char *tag;
    144 	void *loc;
    145 	int (*print)(struct field *, int);
    146 	void (*chval)(const char *, struct field *);
    147 	void (*changed)(void);
    148 	int taglen;
    149 };
    150 
    151 /* LABEL_MAGIC was chosen by Sun and cannot be trivially changed. */
    152 #define LABEL_MAGIC 0xdabe
    153 /*
    154  * LABEL_XMAGIC needs to agree between here and any other code that uses
    155  * extended partitions (mainly the kernel).
    156  */
    157 #define LABEL_XMAGIC (0x199d1fe2+8)
    158 
    159 static int diskfd;			/* fd on the disk */
    160 static const char *diskname;		/* name of the disk, for messages */
    161 static int readonly;			/* true iff it's open RO */
    162 static unsigned char labelbuf[512];	/* Buffer holding the label sector */
    163 static struct label label;		/* The label itself. */
    164 static int fixmagic;			/* -m, ignore bad magic #s */
    165 static int fixcksum;			/* -s, ignore bad cksums */
    166 static int newlabel;			/* -n, ignore all on-disk values */
    167 static int quiet;			/* -q, don't print chatter */
    168 
    169 /*
    170  * The various functions that go in the field function pointers.  The
    171  * _ascii functions are for 128-byte string fields (the ASCII label);
    172  * the _int functions are for int-valued fields (everything else).
    173  * update_spc is a `changed' function for updating the spc value when
    174  * changing one of the two values that make it up.
    175  */
    176 static int print_ascii(struct field *, int);
    177 static void chval_ascii(const char *, struct field *);
    178 static int print_int(struct field *, int);
    179 static void chval_int(const char *, struct field *);
    180 static void update_spc(void);
    181 
    182 int  main(int, char **);
    183 
    184 /* The fields themselves. */
    185 static struct field fields[] =
    186 {
    187 	{"ascii", &label.asciilabel[0], print_ascii, chval_ascii, 0},
    188 	{"rpm", &label.rpm, print_int, chval_int, 0},
    189 	{"pcyl", &label.pcyl, print_int, chval_int, 0},
    190 	{"apc", &label.apc, print_int, chval_int, 0},
    191 	{"obs1", &label.obs1, print_int, chval_int, 0},
    192 	{"obs2", &label.obs2, print_int, chval_int, 0},
    193 	{"intrlv", &label.intrlv, print_int, chval_int, 0},
    194 	{"ncyl", &label.ncyl, print_int, chval_int, 0},
    195 	{"acyl", &label.acyl, print_int, chval_int, 0},
    196 	{"nhead", &label.nhead, print_int, chval_int, update_spc},
    197 	{"nsect", &label.nsect, print_int, chval_int, update_spc},
    198 	{"obs3", &label.obs3, print_int, chval_int, 0},
    199 	{"obs4", &label.obs4, print_int, chval_int, 0},
    200 	{NULL, NULL, NULL, NULL, 0}
    201 };
    202 /*
    203  * We'd _like_ to use howmany() from the include files, but can't count
    204  *  on its being present or working.
    205  */
    206 static __inline__ uint32_t how_many(uint32_t amt, uint32_t unit)
    207     __attribute__((__const__));
    208 static __inline__ uint32_t how_many(uint32_t amt, uint32_t unit)
    209 {
    210 	return ((amt + unit - 1) / unit);
    211 }
    212 
    213 /*
    214  * Try opening the disk, given a name.  If mustsucceed is true, we
    215  *  "cannot fail"; failures produce gripe-and-exit, and if we return,
    216  *  our return value is 1.  Otherwise, we return 1 on success and 0 on
    217  *  failure.
    218  */
    219 static int
    220 trydisk(const char *s, int mustsucceed)
    221 {
    222 	int ro = 0;
    223 
    224 	diskname = s;
    225 	if ((diskfd = open(s, O_RDWR)) == -1 ||
    226 	    (diskfd = open(s, O_RDWR | O_NDELAY)) == -1) {
    227 		if ((diskfd = open(s, O_RDONLY)) == -1) {
    228 			if (mustsucceed)
    229 				err(1, "Cannot open `%s'", s);
    230 			else
    231 				return 0;
    232 		}
    233 		ro = 1;
    234 	}
    235 	if (ro && !quiet)
    236 		warnx("No write access, label is readonly");
    237 	readonly = ro;
    238 	return 1;
    239 }
    240 
    241 /*
    242  * Set the disk device, given the user-supplied string.  Note that even
    243  * if we malloc, we never free, because either trydisk eventually
    244  * succeeds, in which case the string is saved in diskname, or it
    245  * fails, in which case we exit and freeing is irrelevant.
    246  */
    247 static void
    248 setdisk(const char *s)
    249 {
    250 	char *tmp;
    251 
    252 	if (strchr(s, '/')) {
    253 		trydisk(s, 1);
    254 		return;
    255 	}
    256 	if (trydisk(s, 0))
    257 		return;
    258 	tmp = malloc(strlen(s) + 7);
    259 	sprintf(tmp, "/dev/%s", s);
    260 	if (trydisk(tmp, 0))
    261 		return;
    262 	sprintf(tmp, "/dev/%s%c", s, getrawpartition() + 'a');
    263 	if (trydisk(tmp, 0))
    264 		return;
    265 	errx(1, "Can't find device for disk `%s'", s);
    266 }
    267 
    268 static void usage(void) __attribute__((__noreturn__));
    269 static void
    270 usage(void)
    271 {
    272 	(void)fprintf(stderr, "Usage: %s [-mnqs] [-d disk]\n", getprogname());
    273 	exit(1);
    274 }
    275 
    276 /*
    277  * Command-line arguments.  We can have at most one non-flag
    278  *  argument, which is the disk name; we can also have flags
    279  *
    280  *	-d diskdev
    281  *		Specifies disk device unambiguously (if it begins with
    282  *		a dash, it will be mistaken for a flag if simply placed
    283  *		on the command line).
    284  *
    285  *	-m
    286  *		Turns on fixmagic, which causes bad magic numbers to be
    287  *		ignored (though a complaint is still printed), rather
    288  *		than being fatal errors.
    289  *
    290  *	-s
    291  *		Turns on fixcksum, which causes bad checksums to be
    292  *		ignored (though a complaint is still printed), rather
    293  *		than being fatal errors.
    294  *
    295  *	-n
    296  *		Turns on newlabel, which means we're creating a new
    297  *		label and anything in the label sector should be
    298  *		ignored.  This is a bit like -fixmagic -fixsum, except
    299  *		that it doesn't print complaints and it ignores
    300  *		possible garbage on-disk.
    301  *
    302  *	-q
    303  *		Turns on quiet, which suppresses printing of prompts
    304  *		and other irrelevant chatter.  If you're trying to use
    305  *		sunlabel in an automated way, you probably want this.
    306  */
    307 static void handleargs(int ac, char **av)
    308 {
    309 	int c;
    310 
    311 	while ((c = getopt(ac, av, "d:mnqs")) != -1) {
    312 		switch (c) {
    313 		case 'd':
    314 			setdisk(optarg);
    315 			break;
    316 		case 'm':
    317 			fixmagic++;
    318 			break;
    319 		case 'n':
    320 			newlabel++;
    321 			break;
    322 		case 'q':
    323 			quiet++;
    324 			break;
    325 		case 's':
    326 			fixcksum++;
    327 			break;
    328 		case '?':
    329 			warnx("Illegal option `%c'", c);
    330 			usage();
    331 		}
    332 	}
    333 }
    334 /*
    335  * Sets the ending cylinder for a partition.  This exists mainly to
    336  * centralize the check.  (If spc is zero, cylinder numbers make
    337  * little sense, and the code would otherwise die on divide-by-0 if we
    338  * barged blindly ahead.)  We need to call this on a partition
    339  * whenever we change it; we need to call it on all partitions
    340  * whenever we change spc.
    341  */
    342 static void
    343 set_endcyl(struct part *p)
    344 {
    345 	if (label.spc == 0) {
    346 		p->endcyl = p->startcyl;
    347 	} else {
    348 		p->endcyl = p->startcyl + how_many(p->nblk, label.spc);
    349 	}
    350 }
    351 
    352 /*
    353  * Unpack a label from disk into the in-core label structure.  If
    354  * newlabel is set, we don't actually do so; we just synthesize a
    355  * blank label instead.  This is where knowledge of the Sun label
    356  * format is kept for read; pack_label is the corresponding routine
    357  * for write.  We are careful to use labelbuf, l_s, or l_l as
    358  * appropriate to avoid byte-sex issues, so we can work on
    359  * little-endian machines.
    360  *
    361  * Note that a bad magic number for the extended partition information
    362  * is not considered an error; it simply indicates there is no
    363  * extended partition information.  Arguably this is the Wrong Thing,
    364  * and we should take zero as meaning no info, and anything other than
    365  * zero or LABEL_XMAGIC as reason to gripe.
    366  */
    367 static const char *
    368 unpack_label(void)
    369 {
    370 	unsigned short int l_s[256];
    371 	unsigned long int l_l[128];
    372 	int i;
    373 	unsigned long int sum;
    374 	int have_x;
    375 
    376 	if (newlabel) {
    377 		bzero(&label.asciilabel[0], 128);
    378 		label.rpm = 0;
    379 		label.pcyl = 0;
    380 		label.apc = 0;
    381 		label.obs1 = 0;
    382 		label.obs2 = 0;
    383 		label.intrlv = 0;
    384 		label.ncyl = 0;
    385 		label.acyl = 0;
    386 		label.nhead = 0;
    387 		label.nsect = 0;
    388 		label.obs3 = 0;
    389 		label.obs4 = 0;
    390 		for (i = 0; i < NPART; i++) {
    391 			label.partitions[i].startcyl = 0;
    392 			label.partitions[i].nblk = 0;
    393 			set_endcyl(&label.partitions[i]);
    394 		}
    395 		label.spc = 0;
    396 		label.dirty = 1;
    397 		return (0);
    398 	}
    399 	for (i = 0; i < 256; i++)
    400 		l_s[i] = (labelbuf[i + i] << 8) | labelbuf[i + i + 1];
    401 	for (i = 0; i < 128; i++)
    402 		l_l[i] = (l_s[i + i] << 16) | l_s[i + i + 1];
    403 	if (l_s[254] != LABEL_MAGIC) {
    404 		if (fixmagic) {
    405 			label.dirty = 1;
    406 			warnx("ignoring incorrect magic number.");
    407 		} else {
    408 			return "bad magic number";
    409 		}
    410 	}
    411 	sum = 0;
    412 	for (i = 0; i < 256; i++)
    413 		sum ^= l_s[i];
    414 	label.dirty = 0;
    415 	if (sum != 0) {
    416 		if (fixcksum) {
    417 			label.dirty = 1;
    418 			warnx("ignoring incorrect checksum.");
    419 		} else {
    420 			return "checksum wrong";
    421 		}
    422 	}
    423 	(void)memcpy(&label.asciilabel[0], &labelbuf[0], 128);
    424 	label.rpm = l_s[210];
    425 	label.pcyl = l_s[211];
    426 	label.apc = l_s[212];
    427 	label.obs1 = l_s[213];
    428 	label.obs2 = l_s[214];
    429 	label.intrlv = l_s[215];
    430 	label.ncyl = l_s[216];
    431 	label.acyl = l_s[217];
    432 	label.nhead = l_s[218];
    433 	label.nsect = l_s[219];
    434 	label.obs3 = l_s[220];
    435 	label.obs4 = l_s[221];
    436 	label.spc = label.nhead * label.nsect;
    437 	for (i = 0; i < 8; i++) {
    438 		label.partitions[i].startcyl = (uint32_t)l_l[i + i + 111];
    439 		label.partitions[i].nblk = (uint32_t)l_l[i + i + 112];
    440 		set_endcyl(&label.partitions[i]);
    441 	}
    442 	have_x = 0;
    443 	if (l_l[33] == LABEL_XMAGIC) {
    444 		sum = 0;
    445 		for (i = 0; i < ((NXPART * 2) + 1); i++)
    446 			sum += l_l[33 + i];
    447 		if (sum != l_l[32]) {
    448 			if (fixcksum) {
    449 				label.dirty = 1;
    450 				warnx("Ignoring incorrect extended-partition checksum.");
    451 				have_x = 1;
    452 			} else {
    453 				warnx("Extended-partition magic right but checksum wrong.");
    454 			}
    455 		} else {
    456 			have_x = 1;
    457 		}
    458 	}
    459 	if (have_x) {
    460 		for (i = 0; i < NXPART; i++) {
    461 			int j = i + i + 34;
    462 			label.partitions[i + 8].startcyl = (uint32_t)l_l[j++];
    463 			label.partitions[i + 8].nblk = (uint32_t)l_l[j++];
    464 			set_endcyl(&label.partitions[i + 8]);
    465 		}
    466 	} else {
    467 		for (i = 0; i < NXPART; i++) {
    468 			label.partitions[i + 8].startcyl = 0;
    469 			label.partitions[i + 8].nblk = 0;
    470 			set_endcyl(&label.partitions[i + 8]);
    471 		}
    472 	}
    473 	return 0;
    474 }
    475 
    476 /*
    477  * Pack a label from the in-core label structure into on-disk format.
    478  * This is where knowledge of the Sun label format is kept for write;
    479  * unpack_label is the corresponding routine for read.  If all
    480  * partitions past the first 8 are size=0 cyl=0, we store all-0s in
    481  * the extended partition space, to be fully compatible with Sun
    482  * labels.  Since AFIAK nothing works in that case that would break if
    483  * we put extended partition info there in the same format we'd use if
    484  * there were real info there, this is arguably unnecessary, but it's
    485  * easy to do.
    486  *
    487  * We are careful to avoid endianness issues by constructing everything
    488  * in an array of shorts.  We do this rather than using chars or longs
    489  * because the checksum is defined in terms of shorts; using chars or
    490  * longs would simplify small amounts of code at the price of
    491  * complicating more.
    492  */
    493 static void
    494 pack_label(void)
    495 {
    496 	unsigned short int l_s[256];
    497 	int i;
    498 	unsigned short int sum;
    499 
    500 	memset(&l_s[0], 0, 512);
    501 	memcpy(&labelbuf[0], &label.asciilabel[0], 128);
    502 	for (i = 0; i < 64; i++)
    503 		l_s[i] = (labelbuf[i + i] << 8) | labelbuf[i + i + 1];
    504 	l_s[210] = label.rpm;
    505 	l_s[211] = label.pcyl;
    506 	l_s[212] = label.apc;
    507 	l_s[213] = label.obs1;
    508 	l_s[214] = label.obs2;
    509 	l_s[215] = label.intrlv;
    510 	l_s[216] = label.ncyl;
    511 	l_s[217] = label.acyl;
    512 	l_s[218] = label.nhead;
    513 	l_s[219] = label.nsect;
    514 	l_s[220] = label.obs3;
    515 	l_s[221] = label.obs4;
    516 	for (i = 0; i < 8; i++) {
    517 		l_s[(i * 4) + 222] = label.partitions[i].startcyl >> 16;
    518 		l_s[(i * 4) + 223] = label.partitions[i].startcyl & 0xffff;
    519 		l_s[(i * 4) + 224] = label.partitions[i].nblk >> 16;
    520 		l_s[(i * 4) + 225] = label.partitions[i].nblk & 0xffff;
    521 	}
    522 	for (i = 0; i < NXPART; i++) {
    523 		if (label.partitions[i + 8].startcyl ||
    524 		    label.partitions[i + 8].nblk)
    525 			break;
    526 	}
    527 	if (i < NXPART) {
    528 		unsigned long int xsum;
    529 		l_s[66] = LABEL_XMAGIC >> 16;
    530 		l_s[67] = LABEL_XMAGIC & 0xffff;
    531 		for (i = 0; i < NXPART; i++) {
    532 			int j = (i * 4) + 68;
    533 			l_s[j++] = label.partitions[i + 8].startcyl >> 16;
    534 			l_s[j++] = label.partitions[i + 8].startcyl & 0xffff;
    535 			l_s[j++] = label.partitions[i + 8].nblk >> 16;
    536 			l_s[j++] = label.partitions[i + 8].nblk & 0xffff;
    537 		}
    538 		xsum = 0;
    539 		for (i = 0; i < ((NXPART * 2) + 1); i++)
    540 			xsum += (l_s[i + i + 66] << 16) | l_s[i + i + 67];
    541 		l_s[64] = (int32_t)(xsum >> 16);
    542 		l_s[65] = (int32_t)(xsum & 0xffff);
    543 	}
    544 	l_s[254] = LABEL_MAGIC;
    545 	sum = 0;
    546 	for (i = 0; i < 255; i++)
    547 		sum ^= l_s[i];
    548 	l_s[255] = sum;
    549 	for (i = 0; i < 256; i++) {
    550 		labelbuf[i + i] = ((uint32_t)l_s[i]) >> 8;
    551 		labelbuf[i + i + 1] = l_s[i] & 0xff;
    552 	}
    553 }
    554 
    555 /*
    556  * Get the label.  Read it off the disk and unpack it.  This function
    557  *  is nothing but lseek, read, unpack_label, and error checking.
    558  */
    559 static void
    560 getlabel(void)
    561 {
    562 	int rv;
    563 	const char *lerr;
    564 
    565 	if (lseek(diskfd, (off_t)0, L_SET) == (off_t)-1)
    566 		err(1, "lseek to 0 on `%s' failed", diskname);
    567 
    568 	if ((rv = read(diskfd, &labelbuf[0], 512)) == -1)
    569 		err(1, "read label from `%s' failed", diskname);
    570 
    571 	if (rv != 512)
    572 		errx(1, "short read from `%s' wanted %d, got %d.", diskname,
    573 		    512, rv);
    574 
    575 	lerr = unpack_label();
    576 	if (lerr)
    577 		errx(1, "bogus label on `%s' (%s)\n", diskname, lerr);
    578 }
    579 
    580 /*
    581  * Put the label.  Pack it and write it to the disk.  This function is
    582  *  little more than pack_label, lseek, write, and error checking.
    583  */
    584 static void
    585 putlabel(void)
    586 {
    587 	int rv;
    588 
    589 	if (readonly) {
    590 		warnx("No write access to `%s'", diskname);
    591 		return;
    592 	}
    593 
    594 	if (lseek(diskfd, (off_t)0, L_SET) < (off_t)-1)
    595 		err(1, "lseek to 0 on `%s' failed", diskname);
    596 
    597 	pack_label();
    598 
    599 	if ((rv = write(diskfd, &labelbuf[0], 512)) == -1) {
    600 		err(1, "write label to `%s' failed", diskname);
    601 		exit(1);
    602 	}
    603 
    604 	if (rv != 512)
    605 		errx(1, "short write to `%s': wanted %d, got %d",
    606 		    diskname, 512, rv);
    607 
    608 	label.dirty = 0;
    609 }
    610 
    611 /*
    612  * Skip whitespace.  Used several places in the command-line parsing
    613  * code.
    614  */
    615 static void
    616 skipspaces(const char **cpp)
    617 {
    618 	const char *cp = *cpp;
    619 	while (*cp && isspace((unsigned char)*cp))
    620 		cp++;
    621 	*cpp = cp;
    622 }
    623 
    624 /*
    625  * Scan a number.  The first arg points to the char * that's moving
    626  *  along the string.  The second arg points to where we should store
    627  *  the result.  The third arg says what we're scanning, for errors.
    628  *  The return value is 0 on error, or nonzero if all goes well.
    629  */
    630 static int
    631 scannum(const char **cpp, uint32_t *np, const char *tag)
    632 {
    633 	uint32_t v;
    634 	int nd;
    635 	const char *cp;
    636 
    637 	skipspaces(cpp);
    638 	v = 0;
    639 	nd = 0;
    640 
    641 	cp = *cpp;
    642 	while (*cp && isdigit(*cp)) {
    643 		v = (10 * v) + (*cp++ - '0');
    644 		nd++;
    645 	}
    646 	*cpp = cp;
    647 
    648 	if (nd == 0) {
    649 		printf("Missing/invalid %s: %s\n", tag, cp);
    650 		return (0);
    651 	}
    652 	*np = v;
    653 	return (1);
    654 }
    655 
    656 /*
    657  * Change a partition.  pno is the number of the partition to change;
    658  *  numbers is a pointer to the string containing the specification for
    659  *  the new start and size.  This always takes the form "start size",
    660  *  where start can be
    661  *
    662  *	a number
    663  *		The partition starts at the beginning of that cylinder.
    664  *
    665  *	start-X
    666  *		The partition starts at the same place partition X does.
    667  *
    668  *	end-X
    669  *		The partition starts at the place partition X ends.  If
    670  *		partition X does not exactly on a cylinder boundary, it
    671  *		is effectively rounded up.
    672  *
    673  *  and size can be
    674  *
    675  *	a number
    676  *		The partition is that many sectors long.
    677  *
    678  *	num/num/num
    679  *		The three numbers are cyl/trk/sect counts.  n1/n2/n3 is
    680  *		equivalent to specifying a single number
    681  *		((n1*label.nhead)+n2)*label.nsect)+n3.  In particular,
    682  *		if label.nhead or label.nsect is zero, this has limited
    683  *		usefulness.
    684  *
    685  *	end-X
    686  *		The partition ends where partition X ends.  It is an
    687  *		error for partition X to end before the specified start
    688  *		point.  This always goes to exactly where partition X
    689  *		ends, even if that's partway through a cylinder.
    690  *
    691  *	start-X
    692  *		The partition extends to end exactly where partition X
    693  *		begins.  It is an error for partition X to begin before
    694  *		the specified start point.
    695  *
    696  *	size-X
    697  *		The partition has the same size as partition X.
    698  *
    699  * If label.spc is nonzero but the partition size is not a multiple of
    700  *  it, a warning is printed, since you usually don't want this.  Most
    701  *  often, in my experience, this comes from specifying a cylinder
    702  *  count as a single number N instead of N/0/0.
    703  */
    704 static void
    705 chpart(int pno, const char *numbers)
    706 {
    707 	uint32_t cyl0;
    708 	uint32_t size;
    709 	uint32_t sizec;
    710 	uint32_t sizet;
    711 	uint32_t sizes;
    712 
    713 	skipspaces(&numbers);
    714 	if (!memcmp(numbers, "end-", 4) && numbers[4]) {
    715 		int epno = LETTERPART(numbers[4]);
    716 		if ((epno >= 0) && (epno < NPART)) {
    717 			cyl0 = label.partitions[epno].endcyl;
    718 			numbers += 5;
    719 		} else {
    720 			if (!scannum(&numbers, &cyl0, "starting cylinder"))
    721 				return;
    722 		}
    723 	} else if (!memcmp(numbers, "start-", 6) && numbers[6]) {
    724 		int spno = LETTERPART(numbers[6]);
    725 		if ((spno >= 0) && (spno < NPART)) {
    726 			cyl0 = label.partitions[spno].startcyl;
    727 			numbers += 7;
    728 		} else {
    729 			if (!scannum(&numbers, &cyl0, "starting cylinder"))
    730 				return;
    731 		}
    732 	} else {
    733 		if (!scannum(&numbers, &cyl0, "starting cylinder"))
    734 			return;
    735 	}
    736 	skipspaces(&numbers);
    737 	if (!memcmp(numbers, "end-", 4) && numbers[4]) {
    738 		int epno = LETTERPART(numbers[4]);
    739 		if ((epno >= 0) && (epno < NPART)) {
    740 			if (label.partitions[epno].endcyl <= cyl0) {
    741 				warnx("Partition %c ends before cylinder %u",
    742 				    PARTLETTER(epno), cyl0);
    743 				return;
    744 			}
    745 			size = label.partitions[epno].nblk;
    746 			/* Be careful of unsigned arithmetic */
    747 			if (cyl0 > label.partitions[epno].startcyl) {
    748 				size -= (cyl0 - label.partitions[epno].startcyl)
    749 				    * label.spc;
    750 			} else if (cyl0 < label.partitions[epno].startcyl) {
    751 				size += (label.partitions[epno].startcyl - cyl0)
    752 				    * label.spc;
    753 			}
    754 			numbers += 5;
    755 		} else {
    756 			if (!scannum(&numbers, &size, "partition size"))
    757 				return;
    758 		}
    759 	} else if (!memcmp(numbers, "start-", 6) && numbers[6]) {
    760 		int  spno = LETTERPART(numbers[6]);
    761 		if ((spno >= 0) && (spno < NPART)) {
    762 			if (label.partitions[spno].startcyl <= cyl0) {
    763 				warnx("Partition %c starts before cylinder %u",
    764 				    PARTLETTER(spno), cyl0);
    765 				return;
    766 			}
    767 			size = (label.partitions[spno].startcyl - cyl0)
    768 			    * label.spc;
    769 			numbers += 7;
    770 		} else {
    771 			if (!scannum(&numbers, &size, "partition size"))
    772 				return;
    773 		}
    774 	} else if (!memcmp(numbers, "size-", 5) && numbers[5]) {
    775 		int spno = LETTERPART(numbers[5]);
    776 		if ((spno >= 0) && (spno < NPART)) {
    777 			size = label.partitions[spno].nblk;
    778 			numbers += 6;
    779 		} else {
    780 			if (!scannum(&numbers, &size, "partition size"))
    781 				return;
    782 		}
    783 	} else {
    784 		if (!scannum(&numbers, &size, "partition size"))
    785 			return;
    786 		skipspaces(&numbers);
    787 		if (*numbers == '/') {
    788 			sizec = size;
    789 			numbers++;
    790 			if (!scannum(&numbers, &sizet,
    791 			    "partition size track value"))
    792 				return;
    793 			skipspaces(&numbers);
    794 			if (*numbers != '/') {
    795 				warnx("Invalid c/t/s syntax - no second slash");
    796 				return;
    797 			}
    798 			numbers++;
    799 			if (!scannum(&numbers, &sizes,
    800 			    "partition size sector value"))
    801 				return;
    802 			size = sizes + (label.nsect * (sizet
    803 			    + (label.nhead * sizec)));
    804 		}
    805 	}
    806 	if (label.spc && (size % label.spc)) {
    807 		warnx("Size is not a multiple of cylinder size (is %u/%u/%u)\n",
    808 		    size / label.spc,
    809 		    (size % label.spc) / label.nsect, size % label.nsect);
    810 	}
    811 	label.partitions[pno].startcyl = cyl0;
    812 	label.partitions[pno].nblk = size;
    813 	set_endcyl(&label.partitions[pno]);
    814 	if ((label.partitions[pno].startcyl * label.spc)
    815 	    + label.partitions[pno].nblk > label.spc * label.ncyl) {
    816 		warnx("Partition extends beyond end of disk");
    817 	}
    818 	label.dirty = 1;
    819 }
    820 
    821 /*
    822  * Change a 128-byte-string field.  There's currently only one such,
    823  *  the ASCII label field.
    824  */
    825 static void
    826 chval_ascii(const char *cp, struct field *f)
    827 {
    828 	const char *nl;
    829 
    830 	skipspaces(&cp);
    831 	if ((nl = strchr(cp, '\n')) == NULL)
    832 		nl = cp + strlen(cp);
    833 	if (nl - cp > 128) {
    834 		warnx("Ascii label string too long - max 128 characters");
    835 	} else {
    836 		memset(f->loc, 0, 128);
    837 		memcpy(f->loc, cp, (size_t)(nl - cp));
    838 		label.dirty = 1;
    839 	}
    840 }
    841 /*
    842  * Change an int-valued field.  As noted above, there's only one
    843  *  function, regardless of the field size in the on-disk label.
    844  */
    845 static void
    846 chval_int(const char *cp, struct field *f)
    847 {
    848 	uint32_t v;
    849 
    850 	if (!scannum(&cp, &v, "value"))
    851 		return;
    852 	*(uint32_t *)f->loc = v;
    853 	label.dirty = 1;
    854 }
    855 /*
    856  * Change a field's value.  The string argument contains the field name
    857  *  and the new value in text form.  Look up the field and call its
    858  *  chval and changed functions.
    859  */
    860 static void
    861 chvalue(const char *str)
    862 {
    863 	const char *cp;
    864 	int i;
    865 	size_t n;
    866 
    867 	if (fields[0].taglen < 1) {
    868 		for (i = 0; fields[i].tag; i++)
    869 			fields[i].taglen = strlen(fields[i].tag);
    870 	}
    871 	skipspaces(&str);
    872 	cp = str;
    873 	while (*cp && !isspace(*cp))
    874 		cp++;
    875 	n = cp - str;
    876 	for (i = 0; fields[i].tag; i++) {
    877 		if ((n == fields[i].taglen) && !memcmp(str, fields[i].tag, n)) {
    878 			(*fields[i].chval) (cp, &fields[i]);
    879 			if (fields[i].changed)
    880 				(*fields[i].changed)();
    881 			break;
    882 		}
    883 	}
    884 	if (!fields[i].tag)
    885 		warnx("Bad name %.*s - see l output for names", (int)n, str);
    886 }
    887 
    888 /*
    889  * `changed' function for the ntrack and nsect fields; update label.spc
    890  *  and call set_endcyl on all partitions.
    891  */
    892 static void
    893 update_spc(void)
    894 {
    895 	int i;
    896 
    897 	label.spc = label.nhead * label.nsect;
    898 	for (i = 0; i < NPART; i++)
    899 		set_endcyl(&label.partitions[i]);
    900 }
    901 
    902 /*
    903  * Print function for 128-byte-string fields.  Currently only the ASCII
    904  *  label, but we don't depend on that.
    905  */
    906 static int
    907 /*ARGSUSED*/
    908 print_ascii(struct field *f, int sofar __attribute__((__unused__)))
    909 {
    910 	printf("%s: %.128s\n", f->tag, (char *)f->loc);
    911 	return 0;
    912 }
    913 
    914 /*
    915  * Print an int-valued field.  We are careful to do proper line wrap,
    916  *  making each value occupy 16 columns.
    917  */
    918 static int
    919 print_int(struct field *f, int sofar)
    920 {
    921 	if (sofar >= 60) {
    922 		printf("\n");
    923 		sofar = 0;
    924 	}
    925 	printf("%s: %-*u", f->tag, 14 - (int)strlen(f->tag),
    926 	    *(uint32_t *)f->loc);
    927 	return sofar + 16;
    928 }
    929 
    930 /*
    931  * Print the whole label.  Just call the print function for each field,
    932  *  then append a newline if necessary.
    933  */
    934 static void
    935 print_label(void)
    936 {
    937 	int i;
    938 	int c;
    939 
    940 	c = 0;
    941 	for (i = 0; fields[i].tag; i++)
    942 		c = (*fields[i].print) (&fields[i], c);
    943 	if (c > 0)
    944 		printf("\n");
    945 }
    946 
    947 /*
    948  * Figure out how many columns wide the screen is.  We impose a minimum
    949  *  width of 20 columns; I suspect the output code has some issues if
    950  *  we have fewer columns than partitions.
    951  */
    952 static int
    953 screen_columns(void)
    954 {
    955 	int ncols;
    956 #ifndef NO_TERMCAP_WIDTH
    957 	char *term;
    958 	char tbuf[1024];
    959 #endif
    960 #if defined(TIOCGWINSZ)
    961 	struct winsize wsz;
    962 #elif defined(TIOCGSIZE)
    963 	struct ttysize tsz;
    964 #endif
    965 
    966 	ncols = 80;
    967 #ifndef NO_TERMCAP_WIDTH
    968 	term = getenv("TERM");
    969 	if (term && (tgetent(&tbuf[0], term) == 1)) {
    970 		int n = tgetnum("co");
    971 		if (n > 1)
    972 			ncols = n;
    973 	}
    974 #endif
    975 #if defined(TIOCGWINSZ)
    976 	if ((ioctl(1, TIOCGWINSZ, &wsz) == 0) && (wsz.ws_col > 0)) {
    977 		ncols = wsz.ws_col;
    978 	}
    979 #elif defined(TIOCGSIZE)
    980 	if ((ioctl(1, TIOCGSIZE, &tsz) == 0) && (tsz.ts_cols > 0)) {
    981 		ncols = tsz.ts_cols;
    982 	}
    983 #endif
    984 	if (ncols < 20)
    985 		ncols = 20;
    986 	return ncols;
    987 }
    988 
    989 /*
    990  * Print the partitions.  The argument is true iff we should print all
    991  * partitions, even those set start=0 size=0.  We generate one line
    992  * per partition (or, if all==0, per `interesting' partition), plus a
    993  * visually graphic map of partition letters.  Most of the hair in the
    994  * visual display lies in ensuring that nothing takes up less than one
    995  * character column, that if two boundaries appear visually identical,
    996  * they _are_ identical.  Within that constraint, we try to make the
    997  * number of character columns proportional to the size....
    998  */
    999 static void
   1000 print_part(int all)
   1001 {
   1002 	int i, j, k, n, r, c;
   1003 	size_t ncols;
   1004 	uint32_t edges[2 * NPART];
   1005 	int ce[2 * NPART];
   1006 	int row[NPART];
   1007 	unsigned char table[2 * NPART][NPART];
   1008 	char *line;
   1009 	struct part *p = label.partitions;
   1010 
   1011 	for (i = 0; i < NPART; i++) {
   1012 		if (all || p[i].startcyl || p[i].nblk) {
   1013 			printf("%c: start cyl = %6u, size = %8u (",
   1014 			    PARTLETTER(i), p[i].startcyl, p[i].nblk);
   1015 			if (label.spc) {
   1016 				printf("%u/%u/%u - ", p[i].nblk / label.spc,
   1017 				    (p[i].nblk % label.spc) / label.nsect,
   1018 				    p[i].nblk % label.nsect);
   1019 			}
   1020 			printf("%gMb)\n", p[i].nblk / 2048.0);
   1021 		}
   1022 	}
   1023 
   1024 	j = 0;
   1025 	for (i = 0; i < NPART; i++) {
   1026 		if (p[i].nblk > 0) {
   1027 			edges[j++] = p[i].startcyl;
   1028 			edges[j++] = p[i].endcyl;
   1029 		}
   1030 	}
   1031 
   1032 	do {
   1033 		n = 0;
   1034 		for (i = 1; i < j; i++) {
   1035 			if (edges[i] < edges[i - 1]) {
   1036 				uint32_t    t;
   1037 				t = edges[i];
   1038 				edges[i] = edges[i - 1];
   1039 				edges[i - 1] = t;
   1040 				n++;
   1041 			}
   1042 		}
   1043 	} while (n > 0);
   1044 
   1045 	for (i = 1; i < j; i++) {
   1046 		if (edges[i] != edges[n]) {
   1047 			n++;
   1048 			if (n != i)
   1049 				edges[n] = edges[i];
   1050 		}
   1051 	}
   1052 
   1053 	n++;
   1054 	for (i = 0; i < NPART; i++) {
   1055 		if (p[i].nblk > 0) {
   1056 			for (j = 0; j < n; j++) {
   1057 				if ((p[i].startcyl <= edges[j]) &&
   1058 				    (p[i].endcyl > edges[j])) {
   1059 					table[j][i] = 1;
   1060 				} else {
   1061 					table[j][i] = 0;
   1062 				}
   1063 			}
   1064 		}
   1065 	}
   1066 
   1067 	ncols = screen_columns() - 2;
   1068 	for (i = 0; i < n; i++)
   1069 		ce[i] = (edges[i] * ncols) / (double) edges[n - 1];
   1070 
   1071 	for (i = 1; i < n; i++)
   1072 		if (ce[i] <= ce[i - 1])
   1073 			ce[i] = ce[i - 1] + 1;
   1074 
   1075 	if (ce[n - 1] > ncols) {
   1076 		ce[n - 1] = ncols;
   1077 		for (i = n - 1; (i > 0) && (ce[i] <= ce[i - 1]); i--)
   1078 			ce[i - 1] = ce[i] - 1;
   1079 		if (ce[0] < 0)
   1080 			for (i = 0; i < n; i++)
   1081 				ce[i] = i;
   1082 	}
   1083 
   1084 	printf("\n");
   1085 	for (i = 0; i < NPART; i++) {
   1086 		if (p[i].nblk > 0) {
   1087 			r = -1;
   1088 			do {
   1089 				r++;
   1090 				for (j = i - 1; j >= 0; j--) {
   1091 					if (row[j] != r)
   1092 						continue;
   1093 					for (k = 0; k < n; k++)
   1094 						if (table[k][i] && table[k][j])
   1095 							break;
   1096 					if (k < n)
   1097 						break;
   1098 				}
   1099 			} while (j >= 0);
   1100 			row[i] = r;
   1101 		} else {
   1102 			row[i] = -1;
   1103 		}
   1104 	}
   1105 	r = row[0];
   1106 	for (i = 1; i < NPART; i++)
   1107 		if (row[i] > r)
   1108 			r = row[i];
   1109 
   1110 	if ((line = malloc(ncols + 1)) == NULL)
   1111 		err(1, "Can't allocate memory");
   1112 
   1113 	for (i = 0; i <= r; i++) {
   1114 		for (j = 0; j < ncols; j++)
   1115 			line[j] = ' ';
   1116 		for (j = 0; j < NPART; j++) {
   1117 			if (row[j] != i)
   1118 				continue;
   1119 			k = 0;
   1120 			for (k = 0; k < n; k++) {
   1121 				if (table[k][j]) {
   1122 					for (c = ce[k]; c < ce[k + 1]; c++)
   1123 						line[c] = 'a' + j;
   1124 				}
   1125 			}
   1126 		}
   1127 		for (j = ncols - 1; (j >= 0) && (line[j] == ' '); j--);
   1128 		printf("%.*s\n", j + 1, line);
   1129 	}
   1130 	free(line);
   1131 }
   1132 
   1133 #ifdef S_COMMAND
   1134 /*
   1135  * This computes an appropriate checksum for an in-core label.  It's
   1136  * not really related to the S command, except that it's needed only
   1137  * by setlabel(), which is #ifdef S_COMMAND.
   1138  */
   1139 static unsigned short int
   1140 dkcksum(const struct disklabel *lp)
   1141 {
   1142 	const unsigned short int *start;
   1143 	const unsigned short int *end;
   1144 	unsigned short int sum;
   1145 	const unsigned short int *p;
   1146 
   1147 	start = (const void *)lp;
   1148 	end = (const void *)&lp->d_partitions[lp->d_npartitions];
   1149 	sum = 0;
   1150 	for (p = start; p < end; p++)
   1151 		sum ^= *p;
   1152 	return (sum);
   1153 }
   1154 
   1155 /*
   1156  * Set the in-core label.  This is basically putlabel, except it builds
   1157  * a struct disklabel instead of a Sun label buffer, and uses
   1158  * DIOCSDINFO instead of lseek-and-write.
   1159  */
   1160 static void
   1161 setlabel(void)
   1162 {
   1163 	union {
   1164 		struct disklabel l;
   1165 		char pad[sizeof(struct disklabel) -
   1166 		     (MAXPARTITIONS * sizeof(struct partition)) +
   1167 		      (16 * sizeof(struct partition))];
   1168 	} u;
   1169 	int i;
   1170 	struct part *p = label.partitions;
   1171 
   1172 	if (ioctl(diskfd, DIOCGDINFO, &u.l) == -1) {
   1173 		warn("ioctl DIOCGDINFO failed");
   1174 		return;
   1175 	}
   1176 	if (u.l.d_secsize != 512) {
   1177 		warnx("Disk claims %d-byte sectors\n", (int)u.l.d_secsize);
   1178 	}
   1179 	u.l.d_nsectors = label.nsect;
   1180 	u.l.d_ntracks = label.nhead;
   1181 	u.l.d_ncylinders = label.ncyl;
   1182 	u.l.d_secpercyl = label.nsect * label.nhead;
   1183 	u.l.d_rpm = label.rpm;
   1184 	u.l.d_interleave = label.intrlv;
   1185 	u.l.d_npartitions = getmaxpartitions();
   1186 	memset(&u.l.d_partitions[0], 0,
   1187 	    u.l.d_npartitions * sizeof(struct partition));
   1188 	for (i = 0; i < u.l.d_npartitions; i++) {
   1189 		u.l.d_partitions[i].p_size = p[i].nblk;
   1190 		u.l.d_partitions[i].p_offset = p[i].startcyl
   1191 		    * label.nsect * label.nhead;
   1192 		u.l.d_partitions[i].p_fsize = 0;
   1193 		u.l.d_partitions[i].p_fstype = (i == 1) ? FS_SWAP :
   1194 		    (i == 2) ? FS_UNUSED : FS_BSDFFS;
   1195 		u.l.d_partitions[i].p_frag = 0;
   1196 		u.l.d_partitions[i].p_cpg = 0;
   1197 	}
   1198 	u.l.d_checksum = 0;
   1199 	u.l.d_checksum = dkcksum(&u.l);
   1200 	if (ioctl(diskfd, DIOCSDINFO, &u.l) == -1) {
   1201 		warn("ioctl DIOCSDINFO failed");
   1202 		return;
   1203 	}
   1204 }
   1205 #endif
   1206 
   1207 static const char *help[] = {
   1208 	"? - print this help",
   1209 	"L - print label, except for partition table",
   1210 	"P - print partition table",
   1211 	"PP - print partition table including size=0 offset=0 entries",
   1212 	"[abcdefghijklmnop] <cylno> <size> - change partition",
   1213 	"V <name> <value> - change a non-partition label value",
   1214 	"W - write (possibly modified) label out",
   1215 #ifdef S_COMMAND
   1216 	"S - set label in the kernel (orthogonal to W)",
   1217 #endif
   1218 	"Q - quit program (error if no write since last change)",
   1219 	"Q! - quit program (unconditionally) [EOF also quits]",
   1220 	NULL
   1221 };
   1222 
   1223 /*
   1224  * Read and execute one command line from the user.
   1225  */
   1226 static void
   1227 docmd(void)
   1228 {
   1229 	char cmdline[512];
   1230 	int i;
   1231 
   1232 	if (!quiet)
   1233 		printf("sunlabel> ");
   1234 	if (fgets(&cmdline[0], sizeof(cmdline), stdin) != &cmdline[0])
   1235 		exit(0);
   1236 	switch (cmdline[0]) {
   1237 	case '?':
   1238 		for (i = 0; help[i]; i++)
   1239 			printf("%s\n", help[i]);
   1240 		break;
   1241 	case 'L':
   1242 		print_label();
   1243 		break;
   1244 	case 'P':
   1245 		print_part(cmdline[1] == 'P');
   1246 		break;
   1247 	case 'W':
   1248 		putlabel();
   1249 		break;
   1250 	case 'S':
   1251 #ifdef S_COMMAND
   1252 		setlabel();
   1253 #else
   1254 		printf("This compilation doesn't support S.\n");
   1255 #endif
   1256 		break;
   1257 	case 'Q':
   1258 		if ((cmdline[1] == '!') || !label.dirty)
   1259 			exit(0);
   1260 		printf("Label is dirty - use w to write it\n");
   1261 		printf("Use Q! to quit anyway.\n");
   1262 		break;
   1263 	case 'a':
   1264 	case 'b':
   1265 	case 'c':
   1266 	case 'd':
   1267 	case 'e':
   1268 	case 'f':
   1269 	case 'g':
   1270 	case 'h':
   1271 	case 'i':
   1272 	case 'j':
   1273 	case 'k':
   1274 	case 'l':
   1275 	case 'm':
   1276 	case 'n':
   1277 	case 'o':
   1278 	case 'p':
   1279 		chpart(LETTERPART(cmdline[0]), &cmdline[1]);
   1280 		break;
   1281 	case 'V':
   1282 		chvalue(&cmdline[1]);
   1283 		break;
   1284 	case '\n':
   1285 		break;
   1286 	default:
   1287 		printf("(Unrecognized command character %c ignored.)\n",
   1288 		    cmdline[0]);
   1289 		break;
   1290 	}
   1291 }
   1292 /*
   1293  * main() (duh!).  Pretty boring.
   1294  */
   1295 int
   1296 main(int ac, char **av)
   1297 {
   1298 	handleargs(ac, av);
   1299 	getlabel();
   1300 	for (;;)
   1301 		docmd();
   1302 }
   1303