use getopt; do not check for root filesystem when given a list of devices
[unix-history] / usr / src / sbin / fsck / setup.c
index 20fa24f..0ebcfcf 100644 (file)
@@ -1,37 +1,68 @@
 /*
 /*
- * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1980, 1986 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)setup.c    5.11 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)setup.c    5.27 (Berkeley) %G%";
+#endif /* not lint */
 
 #define DKTYPENAMES
 #include <sys/param.h>
 
 #define DKTYPENAMES
 #include <sys/param.h>
-#include <sys/inode.h>
-#include <sys/fs.h>
+#include <ufs/dinode.h>
+#include <ufs/fs.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <sys/disklabel.h>
 #include <sys/file.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <sys/disklabel.h>
 #include <sys/file.h>
+#include <machine/endian.h>
 #include <ctype.h>
 #include "fsck.h"
 
 #include <ctype.h>
 #include "fsck.h"
 
+struct bufarea asblk;
+#define altsblock (*asblk.b_un.b_fs)
 #define POWEROF2(num)  (((num) & ((num) - 1)) == 0)
 
 #define POWEROF2(num)  (((num) & ((num) - 1)) == 0)
 
-char   *calloc();
+/*
+ * The size of a cylinder group is calculated by CGSIZE. The maximum size
+ * is limited by the fact that cylinder groups are at most one block.
+ * Its size is derived from the size of the maps maintained in the 
+ * cylinder group and the (struct cg) size.
+ */
+#define CGSIZE(fs) \
+    /* base cg */      (sizeof(struct cg) + \
+    /* blktot size */  (fs)->fs_cpg * sizeof(long) + \
+    /* blks size */    (fs)->fs_cpg * (fs)->fs_nrpos * sizeof(short) + \
+    /* inode map */    howmany((fs)->fs_ipg, NBBY) + \
+    /* block map */    howmany((fs)->fs_cpg * (fs)->fs_spc / NSPF(fs), NBBY))
+
+char   *malloc(), *calloc();
 char   *index();
 char   *index();
+struct disklabel *getdisklabel();
 
 setup(dev)
        char *dev;
 {
        dev_t rootdev;
 
 setup(dev)
        char *dev;
 {
        dev_t rootdev;
-       long cg, ncg, size, i, j;
+       long cg, size, asked, i, j;
+       long bmapsize;
+       struct disklabel *lp;
        struct stat statb;
        struct fs proto;
 
        struct stat statb;
        struct fs proto;
 
+       havesb = 0;
        if (stat("/", &statb) < 0)
                errexit("Can't stat root\n");
        rootdev = statb.st_dev;
        if (stat("/", &statb) < 0)
                errexit("Can't stat root\n");
        rootdev = statb.st_dev;
@@ -40,43 +71,44 @@ setup(dev)
                printf("Can't stat %s\n", dev);
                return (0);
        }
                printf("Can't stat %s\n", dev);
                return (0);
        }
-       rawflg = 0;
-       if ((statb.st_mode & S_IFMT) == S_IFBLK)
-               ;
-       else if ((statb.st_mode & S_IFMT) == S_IFCHR)
-               rawflg++;
-       else {
-               if (reply("file is not a block or character device; OK") == 0)
-                       return (0);
-       }
+       if ((statb.st_mode & S_IFMT) != S_IFBLK &&
+           (statb.st_mode & S_IFMT) != S_IFCHR &&
+           reply("file is not a block or character device; OK") == 0)
+               return (0);
        if (rootdev == statb.st_rdev)
                hotroot++;
        if (rootdev == statb.st_rdev)
                hotroot++;
-       if ((dfile.rfdes = open(dev, O_RDONLY)) < 0) {
+       if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
                perror(dev);
                printf("Can't open %s\n", dev);
                return (0);
        }
        if (preen == 0)
                printf("** %s", dev);
                perror(dev);
                printf("Can't open %s\n", dev);
                return (0);
        }
        if (preen == 0)
                printf("** %s", dev);
-       if (nflag || (dfile.wfdes = open(dev, O_WRONLY)) < 0) {
-               dfile.wfdes = -1;
+       if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
+               fswritefd = -1;
                if (preen)
                        pfatal("NO WRITE ACCESS");
                printf(" (NO WRITE)");
        }
        if (preen == 0)
                printf("\n");
                if (preen)
                        pfatal("NO WRITE ACCESS");
                printf(" (NO WRITE)");
        }
        if (preen == 0)
                printf("\n");
-       dfile.mod = 0;
+       fsmodified = 0;
        lfdir = 0;
        initbarea(&sblk);
        lfdir = 0;
        initbarea(&sblk);
-       initbarea(&fileblk);
-       initbarea(&inoblk);
-       initbarea(&cgblk);
+       initbarea(&asblk);
+       sblk.b_un.b_buf = malloc(SBSIZE);
+       asblk.b_un.b_buf = malloc(SBSIZE);
+       if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
+               errexit("cannot allocate space for superblock\n");
+       if (lp = getdisklabel((char *)NULL, fsreadfd))
+               dev_bsize = secsize = lp->d_secsize;
+       else
+               dev_bsize = secsize = DEV_BSIZE;
        /*
         * Read in the superblock, looking for alternates if necessary
         */
        if (readsb(1) == 0) {
        /*
         * Read in the superblock, looking for alternates if necessary
         */
        if (readsb(1) == 0) {
-               if (bflag || calcsb(dev, dfile.rfdes, &proto) == 0 || preen)
+               if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
                        return(0);
                if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
                        return (0);
                        return(0);
                if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
                        return (0);
@@ -97,8 +129,8 @@ setup(dev)
                }
                pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
        }
                }
                pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
        }
-       fmax = sblock.fs_size;
-       imax = sblock.fs_ncg * sblock.fs_ipg;
+       maxfsblock = sblock.fs_size;
+       maxino = sblock.fs_ncg * sblock.fs_ipg;
        /*
         * Check and potentially fix certain fields in the super block.
         */
        /*
         * Check and potentially fix certain fields in the super block.
         */
@@ -118,56 +150,133 @@ setup(dev)
                }
        }
        if (sblock.fs_interleave < 1) {
                }
        }
        if (sblock.fs_interleave < 1) {
-               pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
                        sblock.fs_interleave);
                sblock.fs_interleave = 1;
                        sblock.fs_interleave);
                sblock.fs_interleave = 1;
-               if (preen)
-                       printf(" (FIXED)\n");
-               if (preen || reply("SET TO DEFAULT") == 1)
                        sbdirty();
                        sbdirty();
+                       dirty(&asblk);
+               }
        }
        if (sblock.fs_npsect < sblock.fs_nsect) {
        }
        if (sblock.fs_npsect < sblock.fs_nsect) {
-               pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
                        sblock.fs_npsect);
                sblock.fs_npsect = sblock.fs_nsect;
                        sblock.fs_npsect);
                sblock.fs_npsect = sblock.fs_nsect;
-               if (preen)
-                       printf(" (FIXED)\n");
-               if (preen || reply("SET TO DEFAULT") == 1)
                        sbdirty();
                        sbdirty();
+                       dirty(&asblk);
+               }
+       }
+       if (cvtflag) {
+               if (sblock.fs_postblformat == FS_42POSTBLFMT) {
+                       /*
+                        * Requested to convert from old format to new format
+                        */
+                       if (preen)
+                               pwarn("CONVERTING TO NEW FILE SYSTEM FORMAT\n");
+                       else if (!reply("CONVERT TO NEW FILE SYSTEM FORMAT"))
+                               return(0);
+                       sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
+                       sblock.fs_nrpos = 8;
+                       sblock.fs_postbloff =
+                           (char *)(&sblock.fs_opostbl[0][0]) -
+                           (char *)(&sblock.fs_link);
+                       sblock.fs_rotbloff = &sblock.fs_space[0] -
+                           (u_char *)(&sblock.fs_link);
+                       sblock.fs_cgsize =
+                               fragroundup(&sblock, CGSIZE(&sblock));
+                       /*
+                        * Planning now for future expansion.
+                        */
+#                      if (BYTE_ORDER == BIG_ENDIAN)
+                               sblock.fs_qbmask.val[0] = 0;
+                               sblock.fs_qbmask.val[1] = ~sblock.fs_bmask;
+                               sblock.fs_qfmask.val[0] = 0;
+                               sblock.fs_qfmask.val[1] = ~sblock.fs_fmask;
+#                      endif /* BIG_ENDIAN */
+#                      if (BYTE_ORDER == LITTLE_ENDIAN)
+                               sblock.fs_qbmask.val[0] = ~sblock.fs_bmask;
+                               sblock.fs_qbmask.val[1] = 0;
+                               sblock.fs_qfmask.val[0] = ~sblock.fs_fmask;
+                               sblock.fs_qfmask.val[1] = 0;
+#                      endif /* LITTLE_ENDIAN */
+                       sbdirty();
+                       dirty(&asblk);
+               } else if (sblock.fs_postblformat == FS_DYNAMICPOSTBLFMT) {
+                       /*
+                        * Requested to convert from new format to old format
+                        */
+                       if (sblock.fs_nrpos != 8 || sblock.fs_ipg > 2048 ||
+                           sblock.fs_cpg > 32 || sblock.fs_cpc > 16) {
+                               printf(
+                               "PARAMETERS OF CURRENT FILE SYSTEM DO NOT\n\t");
+                               errexit(
+                               "ALLOW CONVERSION TO OLD FILE SYSTEM FORMAT\n");
+                       }
+                       if (preen)
+                               pwarn("CONVERTING TO OLD FILE SYSTEM FORMAT\n");
+                       else if (!reply("CONVERT TO OLD FILE SYSTEM FORMAT"))
+                               return(0);
+                       sblock.fs_postblformat = FS_42POSTBLFMT;
+                       sblock.fs_cgsize = fragroundup(&sblock,
+                           sizeof(struct ocg) + howmany(sblock.fs_fpg, NBBY));
+                       sbdirty();
+                       dirty(&asblk);
+               } else {
+                       errexit("UNKNOWN FILE SYSTEM FORMAT\n");
+               }
+       }
+       if (asblk.b_dirty) {
+               bcopy((char *)&sblock, (char *)&altsblock,
+                       (int)sblock.fs_sbsize);
+               flush(fswritefd, &asblk);
        }
        /*
         * read in the summary info.
         */
        }
        /*
         * read in the summary info.
         */
+       asked = 0;
        for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
                size = sblock.fs_cssize - i < sblock.fs_bsize ?
                    sblock.fs_cssize - i : sblock.fs_bsize;
                sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
        for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
                size = sblock.fs_cssize - i < sblock.fs_bsize ?
                    sblock.fs_cssize - i : sblock.fs_bsize;
                sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
-               if (bread(&dfile, (char *)sblock.fs_csp[j],
+               if (bread(fsreadfd, (char *)sblock.fs_csp[j],
                    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
                    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
-                   size) != 0)
-                       return (0);
+                   size) != 0 && !asked) {
+                       pfatal("BAD SUMMARY INFORMATION");
+                       if (reply("CONTINUE") == 0)
+                               errexit("");
+                       asked++;
+               }
        }
        /*
         * allocate and initialize the necessary maps
         */
        }
        /*
         * allocate and initialize the necessary maps
         */
-       bmapsz = roundup(howmany(fmax, NBBY), sizeof(short));
-       blockmap = calloc((unsigned)bmapsz, sizeof (char));
+       bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short));
+       blockmap = calloc((unsigned)bmapsize, sizeof (char));
        if (blockmap == NULL) {
        if (blockmap == NULL) {
-               printf("cannot alloc %d bytes for blockmap\n", bmapsz);
+               printf("cannot alloc %d bytes for blockmap\n", bmapsize);
                goto badsb;
        }
                goto badsb;
        }
-       statemap = calloc((unsigned)(imax + 1), sizeof(char));
+       statemap = calloc((unsigned)(maxino + 1), sizeof(char));
        if (statemap == NULL) {
        if (statemap == NULL) {
-               printf("cannot alloc %d bytes for statemap\n", imax + 1);
+               printf("cannot alloc %d bytes for statemap\n", maxino + 1);
                goto badsb;
        }
                goto badsb;
        }
-       lncntp = (short *)calloc((unsigned)(imax + 1), sizeof(short));
+       lncntp = (short *)calloc((unsigned)(maxino + 1), sizeof(short));
        if (lncntp == NULL) {
                printf("cannot alloc %d bytes for lncntp\n", 
        if (lncntp == NULL) {
                printf("cannot alloc %d bytes for lncntp\n", 
-                   (imax + 1) * sizeof(short));
+                   (maxino + 1) * sizeof(short));
                goto badsb;
        }
                goto badsb;
        }
-
+       numdirs = sblock.fs_cstotal.cs_ndir;
+       inplast = 0;
+       listmax = numdirs + 10;
+       inpsort = (struct inoinfo **)calloc((unsigned)listmax,
+           sizeof(struct inoinfo *));
+       inphead = (struct inoinfo **)calloc((unsigned)numdirs,
+           sizeof(struct inoinfo *));
+       if (inpsort == NULL || inphead == NULL) {
+               printf("cannot alloc %d bytes for inphead\n", 
+                   numdirs * sizeof(struct inoinfo *));
+               goto badsb;
+       }
+       bufinit();
        return (1);
 
 badsb:
        return (1);
 
 badsb:
@@ -181,13 +290,9 @@ badsb:
 readsb(listerr)
        int listerr;
 {
 readsb(listerr)
        int listerr;
 {
-       BUFAREA asblk;
-#      define altsblock asblk.b_un.b_fs
-       daddr_t super = bflag ? bflag * DEV_BSIZE : SBOFF;
+       daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
 
 
-       initbarea(&asblk);
-       dev_bsize = 1;
-       if (bread(&dfile, (char *)&sblock, super, (long)SBSIZE) != 0)
+       if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0)
                return (0);
        sblk.b_bno = super;
        sblk.b_size = SBSIZE;
                return (0);
        sblk.b_bno = super;
        sblk.b_size = SBSIZE;
@@ -198,26 +303,33 @@ readsb(listerr)
                { badsb(listerr, "MAGIC NUMBER WRONG"); return (0); }
        if (sblock.fs_ncg < 1)
                { badsb(listerr, "NCG OUT OF RANGE"); return (0); }
                { badsb(listerr, "MAGIC NUMBER WRONG"); return (0); }
        if (sblock.fs_ncg < 1)
                { badsb(listerr, "NCG OUT OF RANGE"); return (0); }
-       if (sblock.fs_cpg < 1 || sblock.fs_cpg > MAXCPG)
+       if (sblock.fs_cpg < 1)
                { badsb(listerr, "CPG OUT OF RANGE"); return (0); }
        if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
            (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
                { badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
        if (sblock.fs_sbsize > SBSIZE)
                { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
                { badsb(listerr, "CPG OUT OF RANGE"); return (0); }
        if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
            (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
                { badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
        if (sblock.fs_sbsize > SBSIZE)
                { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
+       /*
+        * Compute block size that the filesystem is based on,
+        * according to fsbtodb, and adjust superblock block number
+        * so we can tell if this is an alternate later.
+        */
+       super *= dev_bsize;
+       dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
+       sblk.b_bno = super / dev_bsize;
        /*
         * Set all possible fields that could differ, then do check
         * of whole super block against an alternate super block.
         * When an alternate super-block is specified this check is skipped.
         */
        /*
         * Set all possible fields that could differ, then do check
         * of whole super block against an alternate super block.
         * When an alternate super-block is specified this check is skipped.
         */
-       dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
-       sblk.b_bno = sblk.b_bno / dev_bsize;
-       if (bflag)
-               return (1);
        getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
        getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
-       if (asblk.b_errs != NULL)
+       if (asblk.b_errs)
                return (0);
                return (0);
-       altsblock.fs_fsbtodb = sblock.fs_fsbtodb;
+       if (bflag) {
+               havesb = 1;
+               return (1);
+       }
        altsblock.fs_link = sblock.fs_link;
        altsblock.fs_rlink = sblock.fs_rlink;
        altsblock.fs_time = sblock.fs_time;
        altsblock.fs_link = sblock.fs_link;
        altsblock.fs_rlink = sblock.fs_rlink;
        altsblock.fs_time = sblock.fs_time;
@@ -232,21 +344,26 @@ readsb(listerr)
        altsblock.fs_optim = sblock.fs_optim;
        altsblock.fs_rotdelay = sblock.fs_rotdelay;
        altsblock.fs_maxbpg = sblock.fs_maxbpg;
        altsblock.fs_optim = sblock.fs_optim;
        altsblock.fs_rotdelay = sblock.fs_rotdelay;
        altsblock.fs_maxbpg = sblock.fs_maxbpg;
-       altsblock.fs_npsect = sblock.fs_npsect;
-       altsblock.fs_interleave = sblock.fs_interleave;
        bcopy((char *)sblock.fs_csp, (char *)altsblock.fs_csp,
                sizeof sblock.fs_csp);
        bcopy((char *)sblock.fs_fsmnt, (char *)altsblock.fs_fsmnt,
                sizeof sblock.fs_fsmnt);
        bcopy((char *)sblock.fs_sparecon, (char *)altsblock.fs_sparecon,
                sizeof sblock.fs_sparecon);
        bcopy((char *)sblock.fs_csp, (char *)altsblock.fs_csp,
                sizeof sblock.fs_csp);
        bcopy((char *)sblock.fs_fsmnt, (char *)altsblock.fs_fsmnt,
                sizeof sblock.fs_fsmnt);
        bcopy((char *)sblock.fs_sparecon, (char *)altsblock.fs_sparecon,
                sizeof sblock.fs_sparecon);
+       /*
+        * The following should not have to be copied.
+        */
+       altsblock.fs_fsbtodb = sblock.fs_fsbtodb;
+       altsblock.fs_interleave = sblock.fs_interleave;
+       altsblock.fs_npsect = sblock.fs_npsect;
+       altsblock.fs_nrpos = sblock.fs_nrpos;
        if (bcmp((char *)&sblock, (char *)&altsblock, (int)sblock.fs_sbsize)) {
                badsb(listerr,
                "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
                return (0);
        }
        if (bcmp((char *)&sblock, (char *)&altsblock, (int)sblock.fs_sbsize)) {
                badsb(listerr,
                "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
                return (0);
        }
+       havesb = 1;
        return (1);
        return (1);
-#      undef altsblock
 }
 
 badsb(listerr, s)
 }
 
 badsb(listerr, s)
@@ -275,7 +392,6 @@ calcsb(dev, devfd, fs)
        register struct disklabel *lp;
        register struct partition *pp;
        register char *cp;
        register struct disklabel *lp;
        register struct partition *pp;
        register char *cp;
-       struct disklabel *getdisklabel();
        int i;
 
        cp = index(dev, '\0') - 1;
        int i;
 
        cp = index(dev, '\0') - 1;
@@ -289,12 +405,12 @@ calcsb(dev, devfd, fs)
        else
                pp = &lp->d_partitions[*cp - 'a'];
        if (pp->p_fstype != FS_BSDFFS) {
        else
                pp = &lp->d_partitions[*cp - 'a'];
        if (pp->p_fstype != FS_BSDFFS) {
-               pfatal("%s: NOT FORMATTED AS A BSD FILE SYSTEM (%s)\n",
+               pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
                        dev, pp->p_fstype < FSMAXTYPES ?
                        fstypenames[pp->p_fstype] : "unknown");
                return (0);
        }
                        dev, pp->p_fstype < FSMAXTYPES ?
                        fstypenames[pp->p_fstype] : "unknown");
                return (0);
        }
-       bzero(fs, sizeof(struct fs));
+       bzero((char *)fs, sizeof(struct fs));
        fs->fs_fsize = pp->p_fsize;
        fs->fs_frag = pp->p_frag;
        fs->fs_cpg = pp->p_cpg;
        fs->fs_fsize = pp->p_fsize;
        fs->fs_frag = pp->p_frag;
        fs->fs_cpg = pp->p_cpg;
@@ -317,6 +433,7 @@ calcsb(dev, devfd, fs)
        fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
        for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
                fs->fs_fsbtodb++;
        fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
        for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
                fs->fs_fsbtodb++;
+       dev_bsize = lp->d_secsize;
        return (1);
 }
 
        return (1);
 }
 
@@ -328,6 +445,8 @@ getdisklabel(s, fd)
        static struct disklabel lab;
 
        if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
        static struct disklabel lab;
 
        if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
+               if (s == NULL)
+                       return ((struct disklabel *)NULL);
                pwarn("");
                perror("ioctl (GDINFO)");
                errexit("%s: can't read disk label", s);
                pwarn("");
                perror("ioctl (GDINFO)");
                errexit("%s: can't read disk label", s);