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