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