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