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