Home | History | Annotate | Line # | Download | only in restore
tape.c revision 1.37
      1 /*	$NetBSD: tape.c,v 1.37 1998/06/24 19:56:11 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)tape.c	8.9 (Berkeley) 5/1/95";
     45 #else
     46 __RCSID("$NetBSD: tape.c,v 1.37 1998/06/24 19:56:11 christos Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #include <sys/file.h>
     52 #include <sys/ioctl.h>
     53 #include <sys/mtio.h>
     54 #include <sys/stat.h>
     55 
     56 #include <ufs/ufs/dinode.h>
     57 #include <protocols/dumprestore.h>
     58 
     59 #include <errno.h>
     60 #include <paths.h>
     61 #include <setjmp.h>
     62 #include <stdio.h>
     63 #include <stdlib.h>
     64 #include <string.h>
     65 #include <time.h>
     66 #include <unistd.h>
     67 
     68 #include "restore.h"
     69 #include "extern.h"
     70 
     71 static u_int32_t fssize = MAXBSIZE;
     72 static int	mt = -1;
     73 static int	pipein = 0;
     74 static char	magtape[BUFSIZ];
     75 static int	blkcnt;
     76 static int	numtrec;
     77 static char	*tapebuf;
     78 static union	u_spcl endoftapemark;
     79 static int	blksread;		/* blocks read since last header */
     80 static int	tpblksread = 0;		/* TP_BSIZE blocks read */
     81 static int	tapesread;
     82 static jmp_buf	restart;
     83 static int	gettingfile = 0;	/* restart has a valid frame */
     84 static char	*host = NULL;
     85 
     86 static int	ofile;
     87 static char	*map;
     88 static char	lnkbuf[MAXPATHLEN + 1];
     89 static int	pathlen;
     90 
     91 int		oldinofmt;	/* old inode format conversion required */
     92 int		Bcvt;		/* Swap Bytes (for CCI or sun) */
     93 static int	Qcvt;		/* Swap quads (for sun) */
     94 
     95 #define	FLUSHTAPEBUF()	blkcnt = ntrec + 1
     96 
     97 static void	 accthdr __P((struct s_spcl *));
     98 static int	 checksum __P((int *));
     99 static void	 findinode __P((struct s_spcl *));
    100 static void	 findtapeblksize __P((void));
    101 static int	 gethead __P((struct s_spcl *));
    102 static void	 readtape __P((char *));
    103 static void	 setdumpnum __P((void));
    104 static u_long	 swabl __P((u_long));
    105 static u_char	*swablong __P((u_char *, int));
    106 static u_char	*swabshort __P((u_char *, int));
    107 static void	 terminateinput __P((void));
    108 static void	 xtrfile __P((char *, long));
    109 static void	 xtrlnkfile __P((char *, long));
    110 static void	 xtrlnkskip __P((char *, long));
    111 static void	 xtrmap __P((char *, long));
    112 static void	 xtrmapskip __P((char *, long));
    113 static void	 xtrskip __P((char *, long));
    114 
    115 /*
    116  * Set up an input source
    117  */
    118 void
    119 setinput(source)
    120 	char *source;
    121 {
    122 	FLUSHTAPEBUF();
    123 	if (bflag)
    124 		newtapebuf(ntrec);
    125 	else
    126 		newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
    127 	terminal = stdin;
    128 
    129 #ifdef RRESTORE
    130 	if (strchr(source, ':')) {
    131 		host = source;
    132 		source = strchr(host, ':');
    133 		*source++ = '\0';
    134 		if (rmthost(host) == 0)
    135 			exit(1);
    136 	} else
    137 #endif
    138 	if (strcmp(source, "-") == 0) {
    139 		/*
    140 		 * Since input is coming from a pipe we must establish
    141 		 * our own connection to the terminal.
    142 		 */
    143 		terminal = fopen(_PATH_TTY, "r");
    144 		if (terminal == NULL) {
    145 			(void)fprintf(stderr, "cannot open %s: %s\n",
    146 			    _PATH_TTY, strerror(errno));
    147 			terminal = fopen(_PATH_DEVNULL, "r");
    148 			if (terminal == NULL) {
    149 				(void)fprintf(stderr, "cannot open %s: %s\n",
    150 				    _PATH_DEVNULL, strerror(errno));
    151 				exit(1);
    152 			}
    153 		}
    154 		pipein++;
    155 	}
    156 	(void) strcpy(magtape, source);
    157 }
    158 
    159 void
    160 newtapebuf(size)
    161 	long size;
    162 {
    163 	static int tapebufsize = -1;
    164 
    165 	ntrec = size;
    166 	if (size <= tapebufsize)
    167 		return;
    168 	if (tapebuf != NULL)
    169 		free(tapebuf);
    170 	tapebuf = malloc(size * TP_BSIZE);
    171 	if (tapebuf == NULL) {
    172 		fprintf(stderr, "Cannot allocate space for tape buffer\n");
    173 		exit(1);
    174 	}
    175 	tapebufsize = size;
    176 }
    177 
    178 /*
    179  * Verify that the tape drive can be accessed and
    180  * that it actually is a dump tape.
    181  */
    182 void
    183 setup()
    184 {
    185 	int i, j, *ip;
    186 	struct stat stbuf;
    187 
    188 	vprintf(stdout, "Verify tape and initialize maps\n");
    189 #ifdef RRESTORE
    190 	if (host)
    191 		mt = rmtopen(magtape, 0);
    192 	else
    193 #endif
    194 	if (pipein)
    195 		mt = 0;
    196 	else
    197 		mt = open(magtape, O_RDONLY, 0);
    198 	if (mt < 0) {
    199 		fprintf(stderr, "%s: %s\n", magtape, strerror(errno));
    200 		exit(1);
    201 	}
    202 	volno = 1;
    203 	setdumpnum();
    204 	FLUSHTAPEBUF();
    205 	if (!pipein && !bflag)
    206 		findtapeblksize();
    207 	if (gethead(&spcl) == FAIL) {
    208 		blkcnt--; /* push back this block */
    209 		blksread--;
    210 		tpblksread--;
    211 		cvtflag++;
    212 		if (gethead(&spcl) == FAIL) {
    213 			fprintf(stderr, "Tape is not a dump tape\n");
    214 			exit(1);
    215 		}
    216 		fprintf(stderr, "Converting to new file system format.\n");
    217 	}
    218 	if (pipein) {
    219 		endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
    220 		endoftapemark.s_spcl.c_type = TS_END;
    221 		ip = (int *)&endoftapemark;
    222 		j = sizeof(union u_spcl) / sizeof(int);
    223 		i = 0;
    224 		do
    225 			i += *ip++;
    226 		while (--j);
    227 		endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
    228 	}
    229 	if (vflag || command == 't')
    230 		printdumpinfo();
    231 	dumptime = spcl.c_ddate;
    232 	dumpdate = spcl.c_date;
    233 	if (stat(".", &stbuf) < 0) {
    234 		fprintf(stderr, "cannot stat .: %s\n", strerror(errno));
    235 		exit(1);
    236 	}
    237 	if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
    238 		fssize = stbuf.st_blksize;
    239 	if (((fssize - 1) & fssize) != 0) {
    240 		fprintf(stderr, "bad block size %d\n", fssize);
    241 		exit(1);
    242 	}
    243 	if (spcl.c_volume != 1) {
    244 		fprintf(stderr, "Tape is not volume 1 of the dump\n");
    245 		exit(1);
    246 	}
    247 	if (gethead(&spcl) == FAIL) {
    248 		dprintf(stdout, "header read failed at %d blocks\n", blksread);
    249 		panic("no header after volume mark!\n");
    250 	}
    251 	findinode(&spcl);
    252 	if (spcl.c_type != TS_CLRI) {
    253 		fprintf(stderr, "Cannot find file removal list\n");
    254 		exit(1);
    255 	}
    256 	maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
    257 	dprintf(stdout, "maxino = %d\n", maxino);
    258 	map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
    259 	if (map == NULL)
    260 		panic("no memory for active inode map\n");
    261 	usedinomap = map;
    262 	curfile.action = USING;
    263 	getfile(xtrmap, xtrmapskip);
    264 	if (spcl.c_type != TS_BITS) {
    265 		fprintf(stderr, "Cannot find file dump list\n");
    266 		exit(1);
    267 	}
    268 	map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
    269 	if (map == (char *)NULL)
    270 		panic("no memory for file dump list\n");
    271 	dumpmap = map;
    272 	curfile.action = USING;
    273 	getfile(xtrmap, xtrmapskip);
    274 	/*
    275 	 * If there may be whiteout entries on the tape, pretend that the
    276 	 * whiteout inode exists, so that the whiteout entries can be
    277 	 * extracted.
    278 	 */
    279 	if (oldinofmt == 0)
    280 		SETINO(WINO, dumpmap);
    281 }
    282 
    283 /*
    284  * Prompt user to load a new dump volume.
    285  * "Nextvol" is the next suggested volume to use.
    286  * This suggested volume is enforced when doing full
    287  * or incremental restores, but can be overrridden by
    288  * the user when only extracting a subset of the files.
    289  */
    290 void
    291 getvol(nextvol)
    292 	int nextvol;
    293 {
    294 	int newvol, savecnt, wantnext, i;
    295 	union u_spcl tmpspcl;
    296 #	define tmpbuf tmpspcl.s_spcl
    297 	char buf[TP_BSIZE];
    298 
    299 	newvol = savecnt = wantnext = 0;
    300 	if (nextvol == 1) {
    301 		tapesread = 0;
    302 		gettingfile = 0;
    303 	}
    304 	if (pipein) {
    305 		if (nextvol != 1)
    306 			panic("Changing volumes on pipe input?\n");
    307 		if (volno == 1)
    308 			return;
    309 		goto gethdr;
    310 	}
    311 	savecnt = blksread;
    312 again:
    313 	if (pipein)
    314 		exit(1); /* pipes do not get a second chance */
    315 	if (command == 'R' || command == 'r' || curfile.action != SKIP) {
    316 		newvol = nextvol;
    317 		wantnext = 1;
    318 	} else {
    319 		newvol = 0;
    320 		wantnext = 0;
    321 	}
    322 	while (newvol <= 0) {
    323 		if (tapesread == 0) {
    324 			fprintf(stderr, "%s%s%s%s%s",
    325 			    "You have not read any tapes yet.\n",
    326 			    "Unless you know which volume your",
    327 			    " file(s) are on you should start\n",
    328 			    "with the last volume and work",
    329 			    " towards the first.\n");
    330 		} else {
    331 			fprintf(stderr, "You have read volumes");
    332 			strcpy(buf, ": ");
    333 			for (i = 1; i < 32; i++)
    334 				if (tapesread & (1 << i)) {
    335 					fprintf(stderr, "%s%d", buf, i);
    336 					strcpy(buf, ", ");
    337 				}
    338 			fprintf(stderr, "\n");
    339 		}
    340 		do	{
    341 			fprintf(stderr, "Specify next volume #: ");
    342 			(void) fflush(stderr);
    343 			(void) fgets(buf, BUFSIZ, terminal);
    344 		} while (!feof(terminal) && buf[0] == '\n');
    345 		if (feof(terminal))
    346 			exit(1);
    347 		newvol = atoi(buf);
    348 		if (newvol <= 0) {
    349 			fprintf(stderr,
    350 			    "Volume numbers are positive numerics\n");
    351 		}
    352 	}
    353 	if (newvol == volno) {
    354 		tapesread |= 1 << volno;
    355 		return;
    356 	}
    357 	closemt();
    358 	fprintf(stderr, "Mount tape volume %d\n", newvol);
    359 	fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
    360 	fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
    361 	(void) fflush(stderr);
    362 	(void) fgets(buf, BUFSIZ, terminal);
    363 	if (feof(terminal))
    364 		exit(1);
    365 	if (!strcmp(buf, "none\n")) {
    366 		terminateinput();
    367 		return;
    368 	}
    369 	if (buf[0] != '\n') {
    370 		(void) strcpy(magtape, buf);
    371 		magtape[strlen(magtape) - 1] = '\0';
    372 	}
    373 #ifdef RRESTORE
    374 	if (host)
    375 		mt = rmtopen(magtape, 0);
    376 	else
    377 #endif
    378 		mt = open(magtape, O_RDONLY, 0);
    379 
    380 	if (mt == -1) {
    381 		fprintf(stderr, "Cannot open %s\n", magtape);
    382 		volno = -1;
    383 		goto again;
    384 	}
    385 gethdr:
    386 	volno = newvol;
    387 	setdumpnum();
    388 	FLUSHTAPEBUF();
    389 	if (gethead(&tmpbuf) == FAIL) {
    390 		dprintf(stdout, "header read failed at %d blocks\n", blksread);
    391 		fprintf(stderr, "tape is not dump tape\n");
    392 		volno = 0;
    393 		goto again;
    394 	}
    395 	if (tmpbuf.c_volume != volno) {
    396 		fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume);
    397 		volno = 0;
    398 		goto again;
    399 	}
    400 	if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
    401 		fprintf(stderr, "Wrong dump date\n\tgot: %s",
    402 			ctime(&tmpbuf.c_date));
    403 		fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
    404 		volno = 0;
    405 		goto again;
    406 	}
    407 	tapesread |= 1 << volno;
    408 	blksread = savecnt;
    409  	/*
    410  	 * If continuing from the previous volume, skip over any
    411  	 * blocks read already at the end of the previous volume.
    412  	 *
    413  	 * If coming to this volume at random, skip to the beginning
    414  	 * of the next record.
    415  	 */
    416 	dprintf(stdout, "read %ld recs, tape starts with %ld\n",
    417 		(long)tpblksread, (long)tmpbuf.c_firstrec);
    418  	if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
    419  		if (!wantnext) {
    420  			tpblksread = tmpbuf.c_firstrec;
    421  			for (i = tmpbuf.c_count; i > 0; i--)
    422  				readtape(buf);
    423  		} else if (tmpbuf.c_firstrec > 0 &&
    424 			   tmpbuf.c_firstrec < tpblksread - 1) {
    425 			/*
    426 			 * -1 since we've read the volume header
    427 			 */
    428  			i = tpblksread - tmpbuf.c_firstrec - 1;
    429 			dprintf(stderr, "Skipping %d duplicate record%s.\n",
    430 				i, i > 1 ? "s" : "");
    431  			while (--i >= 0)
    432  				readtape(buf);
    433  		}
    434  	}
    435 	if (curfile.action == USING) {
    436 		if (volno == 1)
    437 			panic("active file into volume 1\n");
    438 		return;
    439 	}
    440 	/*
    441 	 * Skip up to the beginning of the next record
    442 	 */
    443 	if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER))
    444 		for (i = tmpbuf.c_count; i > 0; i--)
    445 			readtape(buf);
    446 	(void) gethead(&spcl);
    447 	findinode(&spcl);
    448 	if (gettingfile) {
    449 		gettingfile = 0;
    450 		longjmp(restart, 1);
    451 	}
    452 }
    453 
    454 /*
    455  * Handle unexpected EOF.
    456  */
    457 static void
    458 terminateinput()
    459 {
    460 
    461 	if (gettingfile && curfile.action == USING) {
    462 		printf("Warning: %s %s\n",
    463 		    "End-of-input encountered while extracting", curfile.name);
    464 	}
    465 	curfile.name = "<name unknown>";
    466 	curfile.action = UNKNOWN;
    467 	curfile.dip = NULL;
    468 	curfile.ino = maxino;
    469 	if (gettingfile) {
    470 		gettingfile = 0;
    471 		longjmp(restart, 1);
    472 	}
    473 }
    474 
    475 /*
    476  * handle multiple dumps per tape by skipping forward to the
    477  * appropriate one.
    478  */
    479 static void
    480 setdumpnum()
    481 {
    482 	struct mtop tcom;
    483 
    484 	if (dumpnum == 1 || volno != 1)
    485 		return;
    486 	if (pipein) {
    487 		fprintf(stderr, "Cannot have multiple dumps on pipe input\n");
    488 		exit(1);
    489 	}
    490 	tcom.mt_op = MTFSF;
    491 	tcom.mt_count = dumpnum - 1;
    492 #ifdef RRESTORE
    493 	if (host)
    494 		rmtioctl(MTFSF, dumpnum - 1);
    495 	else
    496 #endif
    497 		if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0)
    498 			fprintf(stderr, "ioctl MTFSF: %s\n", strerror(errno));
    499 }
    500 
    501 void
    502 printdumpinfo()
    503 {
    504 	fprintf(stdout, "Dump   date: %s", ctime(&spcl.c_date));
    505 	fprintf(stdout, "Dumped from: %s",
    506 	    (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&spcl.c_ddate));
    507 	if (spcl.c_host[0] == '\0')
    508 		return;
    509 	fprintf(stderr, "Level %d dump of %s on %s:%s\n",
    510 		spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
    511 	fprintf(stderr, "Label: %s\n", spcl.c_label);
    512 }
    513 
    514 int
    515 extractfile(name)
    516 	char *name;
    517 {
    518 	int flags;
    519 	uid_t uid;
    520 	gid_t gid;
    521 	mode_t mode;
    522 	struct timeval timep[2];
    523 	struct entry *ep;
    524 
    525 	curfile.name = name;
    526 	curfile.action = USING;
    527 	timep[0].tv_sec = curfile.dip->di_atime;
    528 	timep[0].tv_usec = curfile.dip->di_atimensec / 1000;
    529 	timep[1].tv_sec = curfile.dip->di_mtime;
    530 	timep[1].tv_usec = curfile.dip->di_mtimensec / 1000;
    531 	uid = curfile.dip->di_uid;
    532 	gid = curfile.dip->di_gid;
    533 	mode = curfile.dip->di_mode;
    534 	flags = curfile.dip->di_flags;
    535 	switch (mode & IFMT) {
    536 
    537 	default:
    538 		fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
    539 		skipfile();
    540 		return (FAIL);
    541 
    542 	case IFSOCK:
    543 		vprintf(stdout, "skipped socket %s\n", name);
    544 		skipfile();
    545 		return (GOOD);
    546 
    547 	case IFDIR:
    548 		if (mflag) {
    549 			ep = lookupname(name);
    550 			if (ep == NULL || ep->e_flags & EXTRACT)
    551 				panic("unextracted directory %s\n", name);
    552 			skipfile();
    553 			return (GOOD);
    554 		}
    555 		vprintf(stdout, "extract file %s\n", name);
    556 		return (genliteraldir(name, curfile.ino));
    557 
    558 	case IFLNK:
    559 		lnkbuf[0] = '\0';
    560 		pathlen = 0;
    561 		getfile(xtrlnkfile, xtrlnkskip);
    562 		if (pathlen == 0) {
    563 			vprintf(stdout,
    564 			    "%s: zero length symbolic link (ignored)\n", name);
    565 			return (GOOD);
    566 		}
    567 		if (uflag)
    568 			(void) unlink(name);
    569 		if (linkit(lnkbuf, name, SYMLINK) == GOOD) {
    570 			(void) lutimes(name, timep);
    571 			(void) lchown(name, uid, gid);
    572 			(void) lchmod(name, mode);
    573 			return (GOOD);
    574 		}
    575 		return (FAIL);
    576 
    577 	case IFCHR:
    578 	case IFBLK:
    579 		vprintf(stdout, "extract special file %s\n", name);
    580 		if (Nflag) {
    581 			skipfile();
    582 			return (GOOD);
    583 		}
    584 		if (uflag)
    585 			(void) unlink(name);
    586 		if (mknod(name, (mode & (IFCHR | IFBLK)) | 0600,
    587 		    (int)curfile.dip->di_rdev) < 0) {
    588 			fprintf(stderr, "%s: cannot create special file: %s\n",
    589 			    name, strerror(errno));
    590 			skipfile();
    591 			return (FAIL);
    592 		}
    593 		skipfile();
    594 		(void) utimes(name, timep);
    595 		(void) chown(name, uid, gid);
    596 		(void) chmod(name, mode);
    597 		(void) chflags(name, flags);
    598 		return (GOOD);
    599 
    600 	case IFIFO:
    601 		vprintf(stdout, "extract fifo %s\n", name);
    602 		if (Nflag) {
    603 			skipfile();
    604 			return (GOOD);
    605 		}
    606 		if (uflag)
    607 			(void) unlink(name);
    608 		if (mkfifo(name, 0600) < 0) {
    609 			fprintf(stderr, "%s: cannot create fifo: %s\n",
    610 			    name, strerror(errno));
    611 			skipfile();
    612 			return (FAIL);
    613 		}
    614 		skipfile();
    615 		(void) utimes(name, timep);
    616 		(void) chown(name, uid, gid);
    617 		(void) chmod(name, mode);
    618 		(void) chflags(name, flags);
    619 		return (GOOD);
    620 
    621 	case IFREG:
    622 		vprintf(stdout, "extract file %s\n", name);
    623 		if (Nflag) {
    624 			skipfile();
    625 			return (GOOD);
    626 		}
    627 		if (uflag)
    628 			(void) unlink(name);
    629 		if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
    630 		    0600)) < 0) {
    631 			fprintf(stderr, "%s: cannot create file: %s\n",
    632 			    name, strerror(errno));
    633 			skipfile();
    634 			return (FAIL);
    635 		}
    636 		getfile(xtrfile, xtrskip);
    637 		(void) futimes(ofile, timep);
    638 		(void) fchown(ofile, uid, gid);
    639 		(void) fchmod(ofile, mode);
    640 		(void) fchflags(ofile, flags);
    641 		(void) close(ofile);
    642 		return (GOOD);
    643 	}
    644 	/* NOTREACHED */
    645 }
    646 
    647 /*
    648  * skip over bit maps on the tape
    649  */
    650 void
    651 skipmaps()
    652 {
    653 
    654 	while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
    655 		skipfile();
    656 }
    657 
    658 /*
    659  * skip over a file on the tape
    660  */
    661 void
    662 skipfile()
    663 {
    664 
    665 	curfile.action = SKIP;
    666 	getfile(xtrnull, xtrnull);
    667 }
    668 
    669 /*
    670  * Extract a file from the tape.
    671  * When an allocated block is found it is passed to the fill function;
    672  * when an unallocated block (hole) is found, a zeroed buffer is passed
    673  * to the skip function.
    674  */
    675 void
    676 getfile(fill, skip)
    677 	void	(*fill) __P((char *, long));
    678 	void	(*skip) __P((char *, long));
    679 {
    680 	int i;
    681 	int curblk = 0;
    682 	quad_t size = spcl.c_dinode.di_size;
    683 	static char clearedbuf[MAXBSIZE];
    684 	char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
    685 	char junk[TP_BSIZE];
    686 
    687 #ifdef __GNUC__			/* XXX: to shut up gcc warnings */
    688 	(void)&curblk;
    689 	(void)&size;
    690 #endif
    691 
    692 	if (spcl.c_type == TS_END)
    693 		panic("ran off end of tape\n");
    694 	if (spcl.c_magic != NFS_MAGIC)
    695 		panic("not at beginning of a file\n");
    696 	if (!gettingfile && setjmp(restart) != 0)
    697 		return;
    698 	gettingfile++;
    699 loop:
    700 	for (i = 0; i < spcl.c_count; i++) {
    701 		if (spcl.c_addr[i]) {
    702 			readtape(&buf[curblk++][0]);
    703 			if (curblk == fssize / TP_BSIZE) {
    704 				(*fill)((char *)buf, (long)(size > TP_BSIZE ?
    705 				     fssize : (curblk - 1) * TP_BSIZE + size));
    706 				curblk = 0;
    707 			}
    708 		} else {
    709 			if (curblk > 0) {
    710 				(*fill)((char *)buf, (long)(size > TP_BSIZE ?
    711 				     curblk * TP_BSIZE :
    712 				     (curblk - 1) * TP_BSIZE + size));
    713 				curblk = 0;
    714 			}
    715 			(*skip)(clearedbuf, (long)(size > TP_BSIZE ?
    716 				TP_BSIZE : size));
    717 		}
    718 		if ((size -= TP_BSIZE) <= 0) {
    719 			for (i++; i < spcl.c_count; i++)
    720 				if (spcl.c_addr[i])
    721 					readtape(junk);
    722 			break;
    723 		}
    724 	}
    725 	if (gethead(&spcl) == GOOD && size > 0) {
    726 		if (spcl.c_type == TS_ADDR)
    727 			goto loop;
    728 		dprintf(stdout,
    729 			"Missing address (header) block for %s at %d blocks\n",
    730 			curfile.name, blksread);
    731 	}
    732 	if (curblk > 0)
    733 		(*fill)((char *)buf, (long)((curblk * TP_BSIZE) + size));
    734 	findinode(&spcl);
    735 	gettingfile = 0;
    736 }
    737 
    738 /*
    739  * Write out the next block of a file.
    740  */
    741 static void
    742 xtrfile(buf, size)
    743 	char	*buf;
    744 	long	size;
    745 {
    746 
    747 	if (Nflag)
    748 		return;
    749 	if (write(ofile, buf, (int) size) == -1) {
    750 		fprintf(stderr,
    751 		    "write error extracting inode %d, name %s\nwrite: %s\n",
    752 			curfile.ino, curfile.name, strerror(errno));
    753 		exit(1);
    754 	}
    755 }
    756 
    757 /*
    758  * Skip over a hole in a file.
    759  */
    760 /* ARGSUSED */
    761 static void
    762 xtrskip(buf, size)
    763 	char *buf;
    764 	long size;
    765 {
    766 
    767 	if (lseek(ofile, size, SEEK_CUR) == -1) {
    768 		fprintf(stderr,
    769 		    "seek error extracting inode %d, name %s\nlseek: %s\n",
    770 			curfile.ino, curfile.name, strerror(errno));
    771 		exit(1);
    772 	}
    773 }
    774 
    775 /*
    776  * Collect the next block of a symbolic link.
    777  */
    778 static void
    779 xtrlnkfile(buf, size)
    780 	char	*buf;
    781 	long	size;
    782 {
    783 
    784 	pathlen += size;
    785 	if (pathlen > MAXPATHLEN) {
    786 		fprintf(stderr, "symbolic link name: %s->%s%s; too long %d\n",
    787 		    curfile.name, lnkbuf, buf, pathlen);
    788 		exit(1);
    789 	}
    790 	(void) strcat(lnkbuf, buf);
    791 }
    792 
    793 /*
    794  * Skip over a hole in a symbolic link (should never happen).
    795  */
    796 /* ARGSUSED */
    797 static void
    798 xtrlnkskip(buf, size)
    799 	char *buf;
    800 	long size;
    801 {
    802 
    803 	fprintf(stderr, "unallocated block in symbolic link %s\n",
    804 		curfile.name);
    805 	exit(1);
    806 }
    807 
    808 /*
    809  * Collect the next block of a bit map.
    810  */
    811 static void
    812 xtrmap(buf, size)
    813 	char	*buf;
    814 	long	size;
    815 {
    816 
    817 	memmove(map, buf, size);
    818 	map += size;
    819 }
    820 
    821 /*
    822  * Skip over a hole in a bit map (should never happen).
    823  */
    824 /* ARGSUSED */
    825 static void
    826 xtrmapskip(buf, size)
    827 	char *buf;
    828 	long size;
    829 {
    830 
    831 	panic("hole in map\n");
    832 	map += size;
    833 }
    834 
    835 /*
    836  * Noop, when an extraction function is not needed.
    837  */
    838 /* ARGSUSED */
    839 void
    840 xtrnull(buf, size)
    841 	char *buf;
    842 	long size;
    843 {
    844 
    845 	return;
    846 }
    847 
    848 /*
    849  * Read TP_BSIZE blocks from the input.
    850  * Handle read errors, and end of media.
    851  */
    852 static void
    853 readtape(buf)
    854 	char *buf;
    855 {
    856 	int rd, newvol, i;
    857 	int cnt, seek_failed;
    858 
    859 	if (blkcnt < numtrec) {
    860 		memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], (long)TP_BSIZE);
    861 		blksread++;
    862 		tpblksread++;
    863 		return;
    864 	}
    865 	for (i = 0; i < ntrec; i++)
    866 		((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
    867 	if (numtrec == 0)
    868 		numtrec = ntrec;
    869 	cnt = ntrec * TP_BSIZE;
    870 	rd = 0;
    871 getmore:
    872 #ifdef RRESTORE
    873 	if (host)
    874 		i = rmtread(&tapebuf[rd], cnt);
    875 	else
    876 #endif
    877 		i = read(mt, &tapebuf[rd], cnt);
    878 	/*
    879 	 * Check for mid-tape short read error.
    880 	 * If found, skip rest of buffer and start with the next.
    881 	 */
    882 	if (!pipein && numtrec < ntrec && i > 0) {
    883 		dprintf(stdout, "mid-media short read error.\n");
    884 		numtrec = ntrec;
    885 	}
    886 	/*
    887 	 * Handle partial block read.
    888 	 */
    889 	if (pipein && i == 0 && rd > 0)
    890 		i = rd;
    891 	else if (i > 0 && i != ntrec * TP_BSIZE) {
    892 		if (pipein) {
    893 			rd += i;
    894 			cnt -= i;
    895 			if (cnt > 0)
    896 				goto getmore;
    897 			i = rd;
    898 		} else {
    899 			/*
    900 			 * Short read. Process the blocks read.
    901 			 */
    902 			if (i % TP_BSIZE != 0)
    903 				vprintf(stdout,
    904 				    "partial block read: %d should be %d\n",
    905 				    i, ntrec * TP_BSIZE);
    906 			numtrec = i / TP_BSIZE;
    907 		}
    908 	}
    909 	/*
    910 	 * Handle read error.
    911 	 */
    912 	if (i < 0) {
    913 		fprintf(stderr, "Tape read error while ");
    914 		switch (curfile.action) {
    915 		default:
    916 			fprintf(stderr, "trying to set up tape\n");
    917 			break;
    918 		case UNKNOWN:
    919 			fprintf(stderr, "trying to resynchronize\n");
    920 			break;
    921 		case USING:
    922 			fprintf(stderr, "restoring %s\n", curfile.name);
    923 			break;
    924 		case SKIP:
    925 			fprintf(stderr, "skipping over inode %d\n",
    926 				curfile.ino);
    927 			break;
    928 		}
    929 		if (!yflag && !reply("continue"))
    930 			exit(1);
    931 		i = ntrec * TP_BSIZE;
    932 		memset(tapebuf, 0, i);
    933 #ifdef RRESTORE
    934 		if (host)
    935 			seek_failed = (rmtseek(i, 1) < 0);
    936 		else
    937 #endif
    938 			seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1);
    939 
    940 		if (seek_failed) {
    941 			fprintf(stderr,
    942 			    "continuation failed: %s\n", strerror(errno));
    943 			exit(1);
    944 		}
    945 	}
    946 	/*
    947 	 * Handle end of tape.
    948 	 */
    949 	if (i == 0) {
    950 		vprintf(stdout, "End-of-tape encountered\n");
    951 		if (!pipein) {
    952 			newvol = volno + 1;
    953 			volno = 0;
    954 			numtrec = 0;
    955 			getvol(newvol);
    956 			readtape(buf);
    957 			return;
    958 		}
    959 		if (rd % TP_BSIZE != 0)
    960 			panic("partial block read: %d should be %d\n",
    961 				rd, ntrec * TP_BSIZE);
    962 		terminateinput();
    963 		memmove(&tapebuf[rd], &endoftapemark, (long)TP_BSIZE);
    964 	}
    965 	blkcnt = 0;
    966 	memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], (long)TP_BSIZE);
    967 	blksread++;
    968 	tpblksread++;
    969 }
    970 
    971 static void
    972 findtapeblksize()
    973 {
    974 	long i;
    975 
    976 	for (i = 0; i < ntrec; i++)
    977 		((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
    978 	blkcnt = 0;
    979 #ifdef RRESTORE
    980 	if (host)
    981 		i = rmtread(tapebuf, ntrec * TP_BSIZE);
    982 	else
    983 #endif
    984 		i = read(mt, tapebuf, ntrec * TP_BSIZE);
    985 
    986 	if (i <= 0) {
    987 		fprintf(stderr, "tape read error: %s\n", strerror(errno));
    988 		exit(1);
    989 	}
    990 	if (i % TP_BSIZE != 0) {
    991 		fprintf(stderr, "Tape block size (%ld) %s (%ld)\n",
    992 			(long)i, "is not a multiple of dump block size",
    993 			(long)TP_BSIZE);
    994 		exit(1);
    995 	}
    996 	ntrec = i / TP_BSIZE;
    997 	numtrec = ntrec;
    998 	vprintf(stdout, "Tape block size is %d\n", ntrec);
    999 }
   1000 
   1001 void
   1002 closemt()
   1003 {
   1004 
   1005 	if (mt < 0)
   1006 		return;
   1007 #ifdef RRESTORE
   1008 	if (host)
   1009 		rmtclose();
   1010 	else
   1011 #endif
   1012 		(void) close(mt);
   1013 }
   1014 
   1015 /*
   1016  * Read the next block from the tape.
   1017  * Check to see if it is one of several vintage headers.
   1018  * If it is an old style header, convert it to a new style header.
   1019  * If it is not any valid header, return an error.
   1020  */
   1021 static int
   1022 gethead(buf)
   1023 	struct s_spcl *buf;
   1024 {
   1025 	long i;
   1026 	union {
   1027 		quad_t	qval;
   1028 		int32_t	val[2];
   1029 	} qcvt;
   1030 	union u_ospcl {
   1031 		char dummy[TP_BSIZE];
   1032 		struct	s_ospcl {
   1033 			int32_t   c_type;
   1034 			int32_t   c_date;
   1035 			int32_t   c_ddate;
   1036 			int32_t   c_volume;
   1037 			int32_t   c_tapea;
   1038 			u_int16_t c_inumber;
   1039 			int32_t   c_magic;
   1040 			int32_t   c_checksum;
   1041 			struct odinode {
   1042 				unsigned short odi_mode;
   1043 				u_int16_t odi_nlink;
   1044 				u_int16_t odi_uid;
   1045 				u_int16_t odi_gid;
   1046 				int32_t   odi_size;
   1047 				int32_t   odi_rdev;
   1048 				char      odi_addr[36];
   1049 				int32_t   odi_atime;
   1050 				int32_t   odi_mtime;
   1051 				int32_t   odi_ctime;
   1052 			} c_dinode;
   1053 			int32_t c_count;
   1054 			char    c_addr[256];
   1055 		} s_ospcl;
   1056 	} u_ospcl;
   1057 
   1058 	if (!cvtflag) {
   1059 		readtape((char *)buf);
   1060 		if (buf->c_magic != NFS_MAGIC) {
   1061 			if (swabl(buf->c_magic) != NFS_MAGIC)
   1062 				return (FAIL);
   1063 			if (!Bcvt) {
   1064 				vprintf(stdout, "Note: Doing Byte swapping\n");
   1065 				Bcvt = 1;
   1066 			}
   1067 		}
   1068 		if (checksum((int *)buf) == FAIL)
   1069 			return (FAIL);
   1070 		if (Bcvt)
   1071 			swabst((u_char *)"8l4s31l528b1l192b2l", (u_char *)buf);
   1072 		goto good;
   1073 	}
   1074 	readtape((char *)(&u_ospcl.s_ospcl));
   1075 	memset(buf, 0, (long)TP_BSIZE);
   1076 	buf->c_type = u_ospcl.s_ospcl.c_type;
   1077 	buf->c_date = u_ospcl.s_ospcl.c_date;
   1078 	buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
   1079 	buf->c_volume = u_ospcl.s_ospcl.c_volume;
   1080 	buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
   1081 	buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
   1082 	buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
   1083 	buf->c_magic = u_ospcl.s_ospcl.c_magic;
   1084 	buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
   1085 	buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
   1086 	buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
   1087 	buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
   1088 	buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
   1089 	buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
   1090 	buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
   1091 	buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
   1092 	buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
   1093 	buf->c_count = u_ospcl.s_ospcl.c_count;
   1094 	memmove(buf->c_addr, u_ospcl.s_ospcl.c_addr, (long)256);
   1095 	if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
   1096 	    checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
   1097 		return(FAIL);
   1098 	buf->c_magic = NFS_MAGIC;
   1099 
   1100 good:
   1101 	if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
   1102 	    (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
   1103 		qcvt.qval = buf->c_dinode.di_size;
   1104 		if (qcvt.val[0] || qcvt.val[1]) {
   1105 			printf("Note: Doing Quad swapping\n");
   1106 			Qcvt = 1;
   1107 		}
   1108 	}
   1109 	if (Qcvt) {
   1110 		qcvt.qval = buf->c_dinode.di_size;
   1111 		i = qcvt.val[1];
   1112 		qcvt.val[1] = qcvt.val[0];
   1113 		qcvt.val[0] = i;
   1114 		buf->c_dinode.di_size = qcvt.qval;
   1115 	}
   1116 
   1117 	switch (buf->c_type) {
   1118 
   1119 	case TS_CLRI:
   1120 	case TS_BITS:
   1121 		/*
   1122 		 * Have to patch up missing information in bit map headers
   1123 		 */
   1124 		buf->c_inumber = 0;
   1125 		buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
   1126 		for (i = 0; i < buf->c_count; i++)
   1127 			buf->c_addr[i]++;
   1128 		break;
   1129 
   1130 	case TS_TAPE:
   1131 		if ((buf->c_flags & DR_NEWINODEFMT) == 0)
   1132 			oldinofmt = 1;
   1133 		/* fall through */
   1134 	case TS_END:
   1135 		buf->c_inumber = 0;
   1136 		break;
   1137 
   1138 	case TS_INODE:
   1139 	case TS_ADDR:
   1140 		break;
   1141 
   1142 	default:
   1143 		panic("gethead: unknown inode type %d\n", buf->c_type);
   1144 		break;
   1145 	}
   1146 	/*
   1147 	 * If we are restoring a filesystem with old format inodes,
   1148 	 * copy the uid/gid to the new location.
   1149 	 */
   1150 	if (oldinofmt) {
   1151 		buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
   1152 		buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
   1153 	}
   1154 	if (dflag)
   1155 		accthdr(buf);
   1156 	return(GOOD);
   1157 }
   1158 
   1159 /*
   1160  * Check that a header is where it belongs and predict the next header
   1161  */
   1162 static void
   1163 accthdr(header)
   1164 	struct s_spcl *header;
   1165 {
   1166 	static ino_t previno = 0x7fffffff;
   1167 	static int prevtype;
   1168 	static long predict;
   1169 	long blks, i;
   1170 
   1171 	if (header->c_type == TS_TAPE) {
   1172 		fprintf(stderr, "Volume header (%s inode format) ",
   1173 		    oldinofmt ? "old" : "new");
   1174  		if (header->c_firstrec)
   1175  			fprintf(stderr, "begins with record %d",
   1176  				header->c_firstrec);
   1177  		fprintf(stderr, "\n");
   1178 		previno = 0x7fffffff;
   1179 		return;
   1180 	}
   1181 	if (previno == 0x7fffffff)
   1182 		goto newcalc;
   1183 	switch (prevtype) {
   1184 	case TS_BITS:
   1185 		fprintf(stderr, "Dumped inodes map header");
   1186 		break;
   1187 	case TS_CLRI:
   1188 		fprintf(stderr, "Used inodes map header");
   1189 		break;
   1190 	case TS_INODE:
   1191 		fprintf(stderr, "File header, ino %d", previno);
   1192 		break;
   1193 	case TS_ADDR:
   1194 		fprintf(stderr, "File continuation header, ino %d", previno);
   1195 		break;
   1196 	case TS_END:
   1197 		fprintf(stderr, "End of tape header");
   1198 		break;
   1199 	}
   1200 	if (predict != blksread - 1)
   1201 		fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
   1202 			(long)predict, (long)(blksread - 1));
   1203 	fprintf(stderr, "\n");
   1204 newcalc:
   1205 	blks = 0;
   1206 	if (header->c_type != TS_END)
   1207 		for (i = 0; i < header->c_count; i++)
   1208 			if (header->c_addr[i] != 0)
   1209 				blks++;
   1210 	predict = blks;
   1211 	blksread = 0;
   1212 	prevtype = header->c_type;
   1213 	previno = header->c_inumber;
   1214 }
   1215 
   1216 /*
   1217  * Find an inode header.
   1218  * Complain if had to skip, and complain is set.
   1219  */
   1220 static void
   1221 findinode(header)
   1222 	struct s_spcl *header;
   1223 {
   1224 	static long skipcnt = 0;
   1225 	long i;
   1226 	char buf[TP_BSIZE];
   1227 
   1228 	curfile.name = "<name unknown>";
   1229 	curfile.action = UNKNOWN;
   1230 	curfile.dip = NULL;
   1231 	curfile.ino = 0;
   1232 	do {
   1233 		if (header->c_magic != NFS_MAGIC) {
   1234 			skipcnt++;
   1235 			while (gethead(header) == FAIL ||
   1236 			    header->c_date != dumpdate)
   1237 				skipcnt++;
   1238 		}
   1239 		switch (header->c_type) {
   1240 
   1241 		case TS_ADDR:
   1242 			/*
   1243 			 * Skip up to the beginning of the next record
   1244 			 */
   1245 			for (i = 0; i < header->c_count; i++)
   1246 				if (header->c_addr[i])
   1247 					readtape(buf);
   1248 			while (gethead(header) == FAIL ||
   1249 			    header->c_date != dumpdate)
   1250 				skipcnt++;
   1251 			break;
   1252 
   1253 		case TS_INODE:
   1254 			curfile.dip = &header->c_dinode;
   1255 			curfile.ino = header->c_inumber;
   1256 			break;
   1257 
   1258 		case TS_END:
   1259 			curfile.ino = maxino;
   1260 			break;
   1261 
   1262 		case TS_CLRI:
   1263 			curfile.name = "<file removal list>";
   1264 			break;
   1265 
   1266 		case TS_BITS:
   1267 			curfile.name = "<file dump list>";
   1268 			break;
   1269 
   1270 		case TS_TAPE:
   1271 			panic("unexpected tape header\n");
   1272 			/* NOTREACHED */
   1273 
   1274 		default:
   1275 			panic("unknown tape header type %d\n", spcl.c_type);
   1276 			/* NOTREACHED */
   1277 
   1278 		}
   1279 	} while (header->c_type == TS_ADDR);
   1280 	if (skipcnt > 0)
   1281 		fprintf(stderr, "resync restore, skipped %ld blocks\n",
   1282 		    (long)skipcnt);
   1283 	skipcnt = 0;
   1284 }
   1285 
   1286 static int
   1287 checksum(buf)
   1288 	int *buf;
   1289 {
   1290 	int i, j;
   1291 
   1292 	j = sizeof(union u_spcl) / sizeof(int);
   1293 	i = 0;
   1294 	if(!Bcvt) {
   1295 		do
   1296 			i += *buf++;
   1297 		while (--j);
   1298 	} else {
   1299 		/* What happens if we want to read restore tapes
   1300 			for a 16bit int machine??? */
   1301 		do
   1302 			i += swabl(*buf++);
   1303 		while (--j);
   1304 	}
   1305 
   1306 	if (i != CHECKSUM) {
   1307 		fprintf(stderr, "Checksum error %o, inode %d file %s\n", i,
   1308 			curfile.ino, curfile.name);
   1309 		return(FAIL);
   1310 	}
   1311 	return(GOOD);
   1312 }
   1313 
   1314 #ifdef RRESTORE
   1315 #if __STDC__
   1316 #include <stdarg.h>
   1317 #else
   1318 #include <varargs.h>
   1319 #endif
   1320 
   1321 void
   1322 #if __STDC__
   1323 msg(const char *fmt, ...)
   1324 #else
   1325 msg(fmt, va_alist)
   1326 	char *fmt;
   1327 	va_dcl
   1328 #endif
   1329 {
   1330 	va_list ap;
   1331 #if __STDC__
   1332 	va_start(ap, fmt);
   1333 #else
   1334 	va_start(ap);
   1335 #endif
   1336 	(void)vfprintf(stderr, fmt, ap);
   1337 	va_end(ap);
   1338 }
   1339 #endif /* RRESTORE */
   1340 
   1341 static u_char *
   1342 swabshort(sp, n)
   1343 	u_char *sp;
   1344 	int n;
   1345 {
   1346 	char c;
   1347 
   1348 	while (--n >= 0) {
   1349 		c = sp[0]; sp[0] = sp[1]; sp[1] = c;
   1350 		sp += 2;
   1351 	}
   1352 	return (sp);
   1353 }
   1354 
   1355 static u_char *
   1356 swablong(sp, n)
   1357 	u_char *sp;
   1358 	int n;
   1359 {
   1360 	char c;
   1361 
   1362 	while (--n >= 0) {
   1363 		c = sp[0]; sp[0] = sp[3]; sp[3] = c;
   1364 		c = sp[2]; sp[2] = sp[1]; sp[1] = c;
   1365 		sp += 4;
   1366 	}
   1367 	return (sp);
   1368 }
   1369 
   1370 void
   1371 swabst(cp, sp)
   1372 	u_char *cp, *sp;
   1373 {
   1374 	int n = 0;
   1375 
   1376 	while (*cp) {
   1377 		switch (*cp) {
   1378 		case '0': case '1': case '2': case '3': case '4':
   1379 		case '5': case '6': case '7': case '8': case '9':
   1380 			n = (n * 10) + (*cp++ - '0');
   1381 			continue;
   1382 
   1383 		case 's': case 'w': case 'h':
   1384 			if (n == 0)
   1385 				n = 1;
   1386 			sp = swabshort(sp, n);
   1387 			break;
   1388 
   1389 		case 'l':
   1390 			if (n == 0)
   1391 				n = 1;
   1392 			sp = swablong(sp, n);
   1393 			break;
   1394 
   1395 		default: /* Any other character, like 'b' counts as byte. */
   1396 			if (n == 0)
   1397 				n = 1;
   1398 			sp += n;
   1399 			break;
   1400 		}
   1401 		cp++;
   1402 		n = 0;
   1403 	}
   1404 }
   1405 
   1406 static u_long
   1407 swabl(x)
   1408 	u_long x;
   1409 {
   1410 	swabst((u_char *)"l", (u_char *)&x);
   1411 	return (x);
   1412 }
   1413