xref: /csrg-svn/old/tar/tar.c (revision 21457)
1 #ifndef lint
2 static	char *sccsid = "@(#)tar.c	4.21 (Berkeley) 05/30/85";
3 #endif
4 
5 /*
6  *			T A R . C
7  *
8  * $Revision: 1.4 $
9  *
10  * $Log:	tar.c,v $
11  * Revision 1.4  84/11/14  00:08:15  root
12  * New more efficient version.  Minimizes the number of bcopys
13  * and maximizes block buffering.  Page aligns block buffers.
14  *
15  * Revision 1.3  84/02/23  20:24:42  dpk
16  * Added missing close(infile) to prevent running out of fd's
17  *
18  * Revision 1.2  84/02/23  20:17:02  dpk
19  * Added distinctive RCS header
20  *
21  */
22 #ifndef lint
23 static char RCSid[] = "@(#)$Header: tar.c,v 1.4 84/11/14 00:08:15 root Exp $";
24 #endif
25 
26 
27 /*
28  * Tape Archival Program
29  */
30 #include <stdio.h>
31 #include <sys/param.h>
32 #include <sys/stat.h>
33 #include <sys/dir.h>
34 #include <sys/ioctl.h>
35 #include <sys/mtio.h>
36 #include <sys/time.h>
37 #include <signal.h>
38 #include <errno.h>
39 
40 #define TBLOCK	512
41 #define NBLOCK	20
42 #define NAMSIZ	100
43 
44 #define	writetape(b)	writetbuf(b, 1)
45 #define	min(a,b)  ((a) < (b) ? (a) : (b))
46 #define	max(a,b)  ((a) > (b) ? (a) : (b))
47 
48 union hblock {
49 	char dummy[TBLOCK];
50 	struct header {
51 		char name[NAMSIZ];
52 		char mode[8];
53 		char uid[8];
54 		char gid[8];
55 		char size[12];
56 		char mtime[12];
57 		char chksum[8];
58 		char linkflag;
59 		char linkname[NAMSIZ];
60 	} dbuf;
61 };
62 
63 struct linkbuf {
64 	ino_t	inum;
65 	dev_t	devnum;
66 	int	count;
67 	char	pathname[NAMSIZ];
68 	struct	linkbuf *nextp;
69 };
70 
71 union	hblock dblock;
72 union	hblock *tbuf;
73 struct	linkbuf *ihead;
74 struct	stat stbuf;
75 
76 int	rflag;
77 int	xflag;
78 int	vflag;
79 int	tflag;
80 int	cflag;
81 int	mflag;
82 int	fflag;
83 int	iflag;
84 int	oflag;
85 int	pflag;
86 int	wflag;
87 int	hflag;
88 int	Bflag;
89 int	Fflag;
90 
91 int	mt;
92 int	term;
93 int	chksum;
94 int	recno;
95 int	first;
96 int	linkerrok;
97 int	freemem = 1;
98 int	nblock = NBLOCK;
99 int	onintr();
100 int	onquit();
101 int	onhup();
102 int	onterm();
103 
104 daddr_t	low;
105 daddr_t	high;
106 daddr_t	bsrch();
107 
108 FILE	*tfile;
109 char	tname[] = "/tmp/tarXXXXXX";
110 char	*usefile;
111 char	magtape[] = "/dev/rmt8";
112 char	*malloc();
113 char	*sprintf();
114 char	*strcat();
115 char	*rindex();
116 char	*getcwd();
117 char	*getwd();
118 
119 main(argc, argv)
120 int	argc;
121 char	*argv[];
122 {
123 	char *cp;
124 
125 	if (argc < 2)
126 		usage();
127 
128 	tfile = NULL;
129 	usefile =  magtape;
130 	argv[argc] = 0;
131 	argv++;
132 	for (cp = *argv++; *cp; cp++)
133 		switch(*cp) {
134 
135 		case 'f':
136 			if (*argv == 0) {
137 				fprintf(stderr,
138 			"tar: tapefile must be specified with 'f' option\n");
139 				usage();
140 			}
141 			usefile = *argv++;
142 			fflag++;
143 			break;
144 
145 		case 'c':
146 			cflag++;
147 			rflag++;
148 			break;
149 
150 		case 'o':
151 			oflag++;
152 			break;
153 
154 		case 'p':
155 			pflag++;
156 			break;
157 
158 		case 'u':
159 			mktemp(tname);
160 			if ((tfile = fopen(tname, "w")) == NULL) {
161 				fprintf(stderr,
162 				 "Tar: cannot create temporary file (%s)\n",
163 				 tname);
164 				done(1);
165 			}
166 			fprintf(tfile, "!!!!!/!/!/!/!/!/!/! 000\n");
167 			/*FALL THRU*/
168 
169 		case 'r':
170 			rflag++;
171 			break;
172 
173 		case 'v':
174 			vflag++;
175 			break;
176 
177 		case 'w':
178 			wflag++;
179 			break;
180 
181 		case 'x':
182 			xflag++;
183 			break;
184 
185 		case 't':
186 			tflag++;
187 			break;
188 
189 		case 'm':
190 			mflag++;
191 			break;
192 
193 		case '-':
194 			break;
195 
196 		case '0':
197 		case '1':
198 		case '4':
199 		case '5':
200 		case '7':
201 		case '8':
202 			magtape[8] = *cp;
203 			usefile = magtape;
204 			break;
205 
206 		case 'b':
207 			if (*argv == 0) {
208 				fprintf(stderr,
209 			"tar: blocksize must be specified with 'b' option\n");
210 				usage();
211 			}
212 			nblock = atoi(*argv);
213 			if (nblock <= 0) {
214 				fprintf(stderr,
215 				    "tar: invalid blocksize \"%s\"\n", *argv);
216 				done(1);
217 			}
218 			argv++;
219 			break;
220 
221 		case 'l':
222 			linkerrok++;
223 			break;
224 
225 		case 'h':
226 			hflag++;
227 			break;
228 
229 		case 'i':
230 			iflag++;
231 			break;
232 
233 		case 'B':
234 			Bflag++;
235 			break;
236 
237 		case 'F':
238 			Fflag++;
239 			break;
240 
241 		default:
242 			fprintf(stderr, "tar: %c: unknown option\n", *cp);
243 			usage();
244 		}
245 
246 	if (!rflag && !xflag && !tflag)
247 		usage();
248 #ifndef vax
249 	tbuf = (union hblock *)malloc(nblock*TBLOCK);
250 #else
251 	/*
252 	 *  The following is for 4.2BSD and related systems to force
253 	 *  the buffer to be page aligned.  The kernel will avoid
254 	 *  bcopy()'s on disk IO this way by manipulating the page tables.
255 	 */
256 	{
257 		int pagesize = getpagesize();
258 
259 		tbuf = (union hblock *)malloc((nblock*TBLOCK)+pagesize);
260 		tbuf = (union hblock *)(((int)tbuf+pagesize)&~(pagesize-1));
261 	}
262 #endif vax
263 	if (tbuf == NULL) {
264 		fprintf(stderr, "tar: blocksize %d too big, can't get memory\n",
265 		    nblock);
266 		done(1);
267 	}
268 	if (rflag) {
269 		if (cflag && tfile != NULL)
270 			usage();
271 		if (signal(SIGINT, SIG_IGN) != SIG_IGN)
272 			signal(SIGINT, onintr);
273 		if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
274 			signal(SIGHUP, onhup);
275 		if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
276 			signal(SIGQUIT, onquit);
277 #ifdef notdef
278 		if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
279 			signal(SIGTERM, onterm);
280 #endif
281 		if (strcmp(usefile, "-") == 0) {
282 			if (cflag == 0) {
283 				fprintf(stderr,
284 			 "tar: can only create standard output archives\n");
285 				done(1);
286 			}
287 			mt = dup(1);
288 			nblock = 1;
289 		} else if ((mt = open(usefile, 2)) < 0) {
290 			if (cflag == 0 || (mt =  creat(usefile, 0666)) < 0) {
291 				fprintf(stderr,
292 					"tar: cannot open %s\n", usefile);
293 				done(1);
294 			}
295 		}
296 		dorep(argv);
297 		done(0);
298 	}
299 	if (strcmp(usefile, "-") == 0) {
300 		mt = dup(0);
301 		nblock = 1;
302 	} else if ((mt = open(usefile, 0)) < 0) {
303 		fprintf(stderr, "tar: cannot open %s\n", usefile);
304 		done(1);
305 	}
306 	if (xflag)
307 		doxtract(argv);
308 	else
309 		dotable();
310 	done(0);
311 }
312 
313 usage()
314 {
315 	fprintf(stderr,
316 "tar: usage: tar -{txru}[cvfblmhopwBi] [tapefile] [blocksize] file1 file2...\n");
317 	done(1);
318 }
319 
320 dorep(argv)
321 	char *argv[];
322 {
323 	register char *cp, *cp2;
324 	char wdir[MAXPATHLEN], tempdir[MAXPATHLEN], *parent;
325 
326 	if (!cflag) {
327 		getdir();
328 		do {
329 			passtape();
330 			if (term)
331 				done(0);
332 			getdir();
333 		} while (!endtape());
334 		backtape();
335 		if (tfile != NULL) {
336 			char buf[200];
337 
338 			sprintf(buf,
339 "sort +0 -1 +1nr %s -o %s; awk '$1 != prev {print; prev=$1}' %s >%sX; mv %sX %s",
340 				tname, tname, tname, tname, tname, tname);
341 			fflush(tfile);
342 			system(buf);
343 			freopen(tname, "r", tfile);
344 			fstat(fileno(tfile), &stbuf);
345 			high = stbuf.st_size;
346 		}
347 	}
348 
349 	(void) getcwd(wdir);
350 	while (*argv && ! term) {
351 		cp2 = *argv;
352 		if (!strcmp(cp2, "-C") && argv[1]) {
353 			argv++;
354 			if (chdir(*argv) < 0)
355 				perror(*argv);
356 			else
357 				(void) getcwd(wdir);
358 			argv++;
359 			continue;
360 		}
361 		parent = wdir;
362 		for (cp = *argv; *cp; cp++)
363 			if (*cp == '/')
364 				cp2 = cp;
365 		if (cp2 != *argv) {
366 			*cp2 = '\0';
367 			if (chdir(*argv) < 0) {
368 				perror(*argv);
369 				continue;
370 			}
371 			parent = getcwd(tempdir);
372 			*cp2 = '/';
373 			cp2++;
374 		}
375 		putfile(*argv++, cp2, parent);
376 		if (chdir(wdir) < 0) {
377 			fprintf(stderr, "cannot change back?: ");
378 			perror(wdir);
379 		}
380 	}
381 	putempty();
382 	putempty();
383 	flushtape();
384 	if (linkerrok == 0)
385 		return;
386 	for (; ihead != NULL; ihead = ihead->nextp) {
387 		if (ihead->count == 0)
388 			continue;
389 		fprintf(stderr, "tar: missing links to %s\n", ihead->pathname);
390 	}
391 }
392 
393 endtape()
394 {
395 	return (dblock.dbuf.name[0] == '\0');
396 }
397 
398 getdir()
399 {
400 	register struct stat *sp;
401 	int i;
402 
403 top:
404 	readtape((char *)&dblock);
405 	if (dblock.dbuf.name[0] == '\0')
406 		return;
407 	sp = &stbuf;
408 	sscanf(dblock.dbuf.mode, "%o", &i);
409 	sp->st_mode = i;
410 	sscanf(dblock.dbuf.uid, "%o", &i);
411 	sp->st_uid = i;
412 	sscanf(dblock.dbuf.gid, "%o", &i);
413 	sp->st_gid = i;
414 	sscanf(dblock.dbuf.size, "%lo", &sp->st_size);
415 	sscanf(dblock.dbuf.mtime, "%lo", &sp->st_mtime);
416 	sscanf(dblock.dbuf.chksum, "%o", &chksum);
417 	if (chksum != (i = checksum())) {
418 		fprintf(stderr, "tar: directory checksum error (%d != %d)\n",
419 		    chksum, i);
420 		if (iflag)
421 			goto top;
422 		done(2);
423 	}
424 	if (tfile != NULL)
425 		fprintf(tfile, "%s %s\n", dblock.dbuf.name, dblock.dbuf.mtime);
426 }
427 
428 passtape()
429 {
430 	long blocks;
431 	char *bufp;
432 
433 	if (dblock.dbuf.linkflag == '1')
434 		return;
435 	blocks = stbuf.st_size;
436 	blocks += TBLOCK-1;
437 	blocks /= TBLOCK;
438 
439 	while (blocks-- > 0)
440 		readtbuf(&bufp, TBLOCK);
441 }
442 
443 putfile(longname, shortname, parent)
444 	char *longname;
445 	char *shortname;
446 	char *parent;
447 {
448 	int infile = 0;
449 	long blocks;
450 	char buf[TBLOCK];
451 #ifdef vax
452 	char *origbuf;
453 #endif
454 	char *bigbuf;
455 	register char *cp, *cp2;
456 	struct direct *dp;
457 	DIR *dirp;
458 	int i, j;
459 	char newparent[NAMSIZ+64];
460 	extern int errno;
461 	int	maxread;
462 	int	hint;		/* amount to write to get "in sync" */
463 
464 	if (!hflag)
465 		i = lstat(shortname, &stbuf);
466 	else
467 		i = stat(shortname, &stbuf);
468 	if (i < 0) {
469 		switch (errno) {
470 		case EACCES:
471 			fprintf(stderr, "tar: %s: cannot open file\n", longname);
472 			break;
473 		case ENOENT:
474 			fprintf(stderr, "tar: %s: no such file or directory\n",
475 			    longname);
476 			break;
477 		default:
478 			fprintf(stderr, "tar: %s: cannot stat file\n", longname);
479 			break;
480 		}
481 		return;
482 	}
483 	if (tfile != NULL && checkupdate(longname) == 0)
484 		return;
485 	if (checkw('r', longname) == 0)
486 		return;
487 	if (Fflag && checkf(shortname, stbuf.st_mode, Fflag) == 0)
488 		return;
489 
490 	switch (stbuf.st_mode & S_IFMT) {
491 	case S_IFDIR:
492 		for (i = 0, cp = buf; *cp++ = longname[i++];)
493 			;
494 		*--cp = '/';
495 		*++cp = 0  ;
496 		if (!oflag) {
497 			if ((cp - buf) >= NAMSIZ) {
498 				fprintf(stderr, "tar: %s: file name too long\n",
499 				    longname);
500 				return;
501 			}
502 			stbuf.st_size = 0;
503 			tomodes(&stbuf);
504 			strcpy(dblock.dbuf.name,buf);
505 			sprintf(dblock.dbuf.chksum, "%6o", checksum());
506 			writetape((char *)&dblock);
507 		}
508 		sprintf(newparent, "%s/%s", parent, shortname);
509 		if (chdir(shortname) < 0) {
510 			perror(shortname);
511 			return;
512 		}
513 		if ((dirp = opendir(".")) == NULL) {
514 			fprintf(stderr, "tar: %s: directory read error\n",
515 			    longname);
516 			if (chdir(parent) < 0) {
517 				fprintf(stderr, "cannot change back?: ");
518 				perror(parent);
519 			}
520 			return;
521 		}
522 		while ((dp = readdir(dirp)) != NULL && !term) {
523 			if (dp->d_ino == 0)
524 				continue;
525 			if (!strcmp(".", dp->d_name) ||
526 			    !strcmp("..", dp->d_name))
527 				continue;
528 			strcpy(cp, dp->d_name);
529 			i = telldir(dirp);
530 			closedir(dirp);
531 			putfile(buf, cp, newparent);
532 			dirp = opendir(".");
533 			seekdir(dirp, i);
534 		}
535 		closedir(dirp);
536 		if (chdir(parent) < 0) {
537 			fprintf(stderr, "cannot change back?: ");
538 			perror(parent);
539 		}
540 		break;
541 
542 	case S_IFLNK:
543 		tomodes(&stbuf);
544 		if (strlen(longname) >= NAMSIZ) {
545 			fprintf(stderr, "tar: %s: file name too long\n",
546 			    longname);
547 			return;
548 		}
549 		strcpy(dblock.dbuf.name, longname);
550 		if (stbuf.st_size + 1 >= NAMSIZ) {
551 			fprintf(stderr, "tar: %s: symbolic link too long\n",
552 			    longname);
553 			return;
554 		}
555 		i = readlink(shortname, dblock.dbuf.linkname, NAMSIZ - 1);
556 		if (i < 0) {
557 			perror(longname);
558 			return;
559 		}
560 		dblock.dbuf.linkname[i] = '\0';
561 		dblock.dbuf.linkflag = '2';
562 		if (vflag) {
563 			fprintf(stderr, "a %s ", longname);
564 			fprintf(stderr, "symbolic link to %s\n",
565 			    dblock.dbuf.linkname);
566 		}
567 		sprintf(dblock.dbuf.size, "%11lo", 0);
568 		sprintf(dblock.dbuf.chksum, "%6o", checksum());
569 		writetape((char *)&dblock);
570 		break;
571 
572 	case S_IFREG:
573 		if ((infile = open(shortname, 0)) < 0) {
574 			fprintf(stderr, "tar: %s: cannot open file\n", longname);
575 			return;
576 		}
577 		tomodes(&stbuf);
578 		if (strlen(longname) >= NAMSIZ) {
579 			fprintf(stderr, "tar: %s: file name too long\n",
580 			    longname);
581 			close(infile);
582 			return;
583 		}
584 		strcpy(dblock.dbuf.name, longname);
585 		if (stbuf.st_nlink > 1) {
586 			struct linkbuf *lp;
587 			int found = 0;
588 
589 			for (lp = ihead; lp != NULL; lp = lp->nextp)
590 				if (lp->inum == stbuf.st_ino &&
591 				    lp->devnum == stbuf.st_dev) {
592 					found++;
593 					break;
594 				}
595 			if (found) {
596 				strcpy(dblock.dbuf.linkname, lp->pathname);
597 				dblock.dbuf.linkflag = '1';
598 				sprintf(dblock.dbuf.chksum, "%6o", checksum());
599 				writetape( (char *) &dblock);
600 				if (vflag) {
601 					fprintf(stderr, "a %s ", longname);
602 					fprintf(stderr, "link to %s\n",
603 					    lp->pathname);
604 				}
605 				lp->count--;
606 				close(infile);
607 				return;
608 			}
609 			lp = (struct linkbuf *) malloc(sizeof(*lp));
610 			if (lp == NULL) {
611 				if (freemem) {
612 					fprintf(stderr,
613 				"tar: out of memory, link information lost\n");
614 					freemem = 0;
615 				}
616 			} else {
617 				lp->nextp = ihead;
618 				ihead = lp;
619 				lp->inum = stbuf.st_ino;
620 				lp->devnum = stbuf.st_dev;
621 				lp->count = stbuf.st_nlink - 1;
622 				strcpy(lp->pathname, longname);
623 			}
624 		}
625 		blocks = (stbuf.st_size + (TBLOCK-1)) / TBLOCK;
626 		if (vflag) {
627 			fprintf(stderr, "a %s ", longname);
628 			fprintf(stderr, "%ld blocks\n", blocks);
629 		}
630 		sprintf(dblock.dbuf.chksum, "%6o", checksum());
631 		hint = writetape((char *)&dblock);
632 		maxread = max(stbuf.st_blksize, (nblock * TBLOCK));
633 #ifndef vax
634 		if ((bigbuf = malloc(maxread)) == 0) {
635 			maxread = TBLOCK;
636 			bigbuf = buf;
637 		}
638 #else
639 		/*
640 		 *  The following is for 4.2BSD and related systems to force
641 		 *  the buffer to be page aligned.  The kernel will avoid
642 		 *  bcopy()'s on disk IO this way by manipulating the page tables.
643 		 */
644 		{
645 			int pagesize = getpagesize();
646 
647 			if ((origbuf = malloc(maxread+pagesize)) == 0) {
648 				maxread = TBLOCK;
649 				bigbuf = buf;
650 			} else {
651 				bigbuf = (char *)(((int)origbuf+pagesize)&~(pagesize-1));
652 			}
653 		}
654 #endif vax
655 
656 		while ((i = read(infile, bigbuf, min((hint*TBLOCK), maxread))) > 0
657 		  && blocks > 0) {
658 		  	register int nblks;
659 
660 			nblks = ((i-1)/TBLOCK)+1;
661 		  	if (nblks > blocks)
662 		  		nblks = blocks;
663 			hint = writetbuf(bigbuf, nblks);
664 			blocks -= nblks;
665 		}
666 		close(infile);
667 		if (bigbuf != buf)
668 #ifndef vax
669 			free(bigbuf);
670 #else
671 			free(origbuf);
672 #endif
673 		if (blocks != 0 || i != 0)
674 			fprintf(stderr, "tar: %s: file changed size\n",
675 			    longname);
676 		while (--blocks >=  0)
677 			putempty();
678 		break;
679 
680 	default:
681 		fprintf(stderr, "tar: %s is not a file. Not dumped\n",
682 		    longname);
683 		break;
684 	}
685 }
686 
687 doxtract(argv)
688 	char *argv[];
689 {
690 	long blocks, bytes;
691 	char **cp;
692 	int ofile;
693 
694 	for (;;) {
695 		getdir();
696 		if (endtape())
697 			break;
698 		if (*argv == 0)
699 			goto gotit;
700 		for (cp = argv; *cp; cp++)
701 			if (prefix(*cp, dblock.dbuf.name))
702 				goto gotit;
703 		passtape();
704 		continue;
705 
706 gotit:
707 		if (checkw('x', dblock.dbuf.name) == 0) {
708 			passtape();
709 			continue;
710 		}
711 		if (Fflag) {
712 			char *s;
713 
714 			if ((s = rindex(dblock.dbuf.name, '/')) == 0)
715 				s = dblock.dbuf.name;
716 			else
717 				s++;
718 			if (checkf(s, stbuf.st_mode, Fflag) == 0) {
719 				passtape();
720 				continue;
721 			}
722 		}
723 		if (checkdir(dblock.dbuf.name))
724 			continue;
725 		if (dblock.dbuf.linkflag == '2') {
726 			unlink(dblock.dbuf.name);
727 			if (symlink(dblock.dbuf.linkname, dblock.dbuf.name)<0) {
728 				fprintf(stderr, "tar: %s: symbolic link failed\n",
729 				    dblock.dbuf.name);
730 				continue;
731 			}
732 			if (vflag)
733 				fprintf(stderr, "x %s symbolic link to %s\n",
734 				    dblock.dbuf.name, dblock.dbuf.linkname);
735 #ifdef notdef
736 			/* ignore alien orders */
737 			chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid);
738 			if (mflag == 0) {
739 				struct timeval tv[2];
740 
741 				tv[0].tv_sec = time(0);
742 				tv[0].tv_usec = 0;
743 				tv[1].tv_sec = stbuf.st_mtime;
744 				tv[1].tv_usec = 0;
745 				utimes(dblock.dbuf.name, tv);
746 			}
747 			if (pflag)
748 				chmod(dblock.dbuf.name, stbuf.st_mode & 07777);
749 #endif
750 			continue;
751 		}
752 		if (dblock.dbuf.linkflag == '1') {
753 			unlink(dblock.dbuf.name);
754 			if (link(dblock.dbuf.linkname, dblock.dbuf.name) < 0) {
755 				fprintf(stderr, "tar: %s: cannot link\n",
756 				    dblock.dbuf.name);
757 				continue;
758 			}
759 			if (vflag)
760 				fprintf(stderr, "%s linked to %s\n",
761 				    dblock.dbuf.name, dblock.dbuf.linkname);
762 			continue;
763 		}
764 		if ((ofile = creat(dblock.dbuf.name,stbuf.st_mode&0xfff)) < 0) {
765 			fprintf(stderr, "tar: %s - cannot create\n",
766 			    dblock.dbuf.name);
767 			passtape();
768 			continue;
769 		}
770 		chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid);
771 		blocks = ((bytes = stbuf.st_size) + TBLOCK-1)/TBLOCK;
772 		if (vflag)
773 			fprintf(stderr, "x %s, %ld bytes, %ld tape blocks\n",
774 			    dblock.dbuf.name, bytes, blocks);
775 		for (; blocks > 0;) {
776 			register int nread;
777 			char	*bufp;
778 			register int nwant;
779 
780 			nwant = NBLOCK*TBLOCK;
781 			if (nwant > (blocks*TBLOCK))
782 				nwant = (blocks*TBLOCK);
783 			nread = readtbuf(&bufp, nwant);
784 			if (bytes > nread) {
785 				if (write(ofile, bufp, nread) < 0) {
786 					fprintf(stderr,
787 					"tar: %s: HELP - extract write error\n",
788 					    dblock.dbuf.name);
789 					done(2);
790 				}
791 			} else if (write(ofile, bufp, (int) bytes) < 0) {
792 				fprintf(stderr,
793 				    "tar: %s: HELP - extract write error\n",
794 				    dblock.dbuf.name);
795 				done(2);
796 			}
797 			bytes -= nread;
798 			blocks -= (((nread-1)/TBLOCK)+1);
799 		}
800 		close(ofile);
801 		if (mflag == 0) {
802 			struct timeval tv[2];
803 
804 			tv[0].tv_sec = time(0);
805 			tv[0].tv_usec = 0;
806 			tv[1].tv_sec = stbuf.st_mtime;
807 			tv[1].tv_usec = 0;
808 			utimes(dblock.dbuf.name, tv);
809 		}
810 		if (pflag)
811 			chmod(dblock.dbuf.name, stbuf.st_mode & 07777);
812 	}
813 }
814 
815 dotable()
816 {
817 	for (;;) {
818 		getdir();
819 		if (endtape())
820 			break;
821 		if (vflag)
822 			longt(&stbuf);
823 		printf("%s", dblock.dbuf.name);
824 		if (dblock.dbuf.linkflag == '1')
825 			printf(" linked to %s", dblock.dbuf.linkname);
826 		if (dblock.dbuf.linkflag == '2')
827 			printf(" symbolic link to %s", dblock.dbuf.linkname);
828 		printf("\n");
829 		passtape();
830 	}
831 }
832 
833 putempty()
834 {
835 	char buf[TBLOCK];
836 
837 	bzero(buf, sizeof (buf));
838 	writetape(buf);
839 }
840 
841 longt(st)
842 	register struct stat *st;
843 {
844 	register char *cp;
845 	char *ctime();
846 
847 	pmode(st);
848 	printf("%3d/%1d", st->st_uid, st->st_gid);
849 	printf("%7D", st->st_size);
850 	cp = ctime(&st->st_mtime);
851 	printf(" %-12.12s %-4.4s ", cp+4, cp+20);
852 }
853 
854 #define	SUID	04000
855 #define	SGID	02000
856 #define	ROWN	0400
857 #define	WOWN	0200
858 #define	XOWN	0100
859 #define	RGRP	040
860 #define	WGRP	020
861 #define	XGRP	010
862 #define	ROTH	04
863 #define	WOTH	02
864 #define	XOTH	01
865 #define	STXT	01000
866 int	m1[] = { 1, ROWN, 'r', '-' };
867 int	m2[] = { 1, WOWN, 'w', '-' };
868 int	m3[] = { 2, SUID, 's', XOWN, 'x', '-' };
869 int	m4[] = { 1, RGRP, 'r', '-' };
870 int	m5[] = { 1, WGRP, 'w', '-' };
871 int	m6[] = { 2, SGID, 's', XGRP, 'x', '-' };
872 int	m7[] = { 1, ROTH, 'r', '-' };
873 int	m8[] = { 1, WOTH, 'w', '-' };
874 int	m9[] = { 2, STXT, 't', XOTH, 'x', '-' };
875 
876 int	*m[] = { m1, m2, m3, m4, m5, m6, m7, m8, m9};
877 
878 pmode(st)
879 	register struct stat *st;
880 {
881 	register int **mp;
882 
883 	for (mp = &m[0]; mp < &m[9];)
884 		select(*mp++, st);
885 }
886 
887 select(pairp, st)
888 	int *pairp;
889 	struct stat *st;
890 {
891 	register int n, *ap;
892 
893 	ap = pairp;
894 	n = *ap++;
895 	while (--n>=0 && (st->st_mode&*ap++)==0)
896 		ap++;
897 	printf("%c", *ap);
898 }
899 
900 checkdir(name)
901 	register char *name;
902 {
903 	register char *cp;
904 
905 	/*
906 	 * Quick check for existance of directory.
907 	 */
908 	if ((cp = rindex(name, '/')) == 0)
909 		return (0);
910 	*cp = '\0';
911 	if (access(name, 0) >= 0) {
912 		*cp = '/';
913 		return (cp[1] == '\0');
914 	}
915 	*cp = '/';
916 
917 	/*
918 	 * No luck, try to make all directories in path.
919 	 */
920 	for (cp = name; *cp; cp++) {
921 		if (*cp != '/')
922 			continue;
923 		*cp = '\0';
924 		if (access(name, 0) < 0) {
925 			if (mkdir(name, 0777) < 0) {
926 				perror(name);
927 				*cp = '/';
928 				return (0);
929 			}
930 			chown(name, stbuf.st_uid, stbuf.st_gid);
931 			if (pflag && cp[1] == '\0')
932 				chmod(name, stbuf.st_mode & 0777);
933 		}
934 		*cp = '/';
935 	}
936 	return (cp[-1]=='/');
937 }
938 
939 onintr()
940 {
941 	signal(SIGINT, SIG_IGN);
942 	term++;
943 }
944 
945 onquit()
946 {
947 	signal(SIGQUIT, SIG_IGN);
948 	term++;
949 }
950 
951 onhup()
952 {
953 	signal(SIGHUP, SIG_IGN);
954 	term++;
955 }
956 
957 onterm()
958 {
959 	signal(SIGTERM, SIG_IGN);
960 	term++;
961 }
962 
963 tomodes(sp)
964 register struct stat *sp;
965 {
966 	register char *cp;
967 
968 	for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++)
969 		*cp = '\0';
970 	sprintf(dblock.dbuf.mode, "%6o ", sp->st_mode & 07777);
971 	sprintf(dblock.dbuf.uid, "%6o ", sp->st_uid);
972 	sprintf(dblock.dbuf.gid, "%6o ", sp->st_gid);
973 	sprintf(dblock.dbuf.size, "%11lo ", sp->st_size);
974 	sprintf(dblock.dbuf.mtime, "%11lo ", sp->st_mtime);
975 }
976 
977 checksum()
978 {
979 	register i;
980 	register char *cp;
981 
982 	for (cp = dblock.dbuf.chksum;
983 	     cp < &dblock.dbuf.chksum[sizeof(dblock.dbuf.chksum)]; cp++)
984 		*cp = ' ';
985 	i = 0;
986 	for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++)
987 		i += *cp;
988 	return (i);
989 }
990 
991 checkw(c, name)
992 	char *name;
993 {
994 	if (!wflag)
995 		return (1);
996 	printf("%c ", c);
997 	if (vflag)
998 		longt(&stbuf);
999 	printf("%s: ", name);
1000 	return (response() == 'y');
1001 }
1002 
1003 response()
1004 {
1005 	char c;
1006 
1007 	c = getchar();
1008 	if (c != '\n')
1009 		while (getchar() != '\n')
1010 			;
1011 	else
1012 		c = 'n';
1013 	return (c);
1014 }
1015 
1016 checkf(name, mode, howmuch)
1017 	char *name;
1018 	int mode, howmuch;
1019 {
1020 	int l;
1021 
1022 	if ((mode & S_IFMT) == S_IFDIR)
1023 		return (strcmp(name, "SCCS") != 0);
1024 	if ((l = strlen(name)) < 3)
1025 		return (1);
1026 	if (howmuch > 1 && name[l-2] == '.' && name[l-1] == 'o')
1027 		return (0);
1028 	if (strcmp(name, "core") == 0 ||
1029 	    strcmp(name, "errs") == 0 ||
1030 	    (howmuch > 1 && strcmp(name, "a.out") == 0))
1031 		return (0);
1032 	/* SHOULD CHECK IF IT IS EXECUTABLE */
1033 	return (1);
1034 }
1035 
1036 checkupdate(arg)
1037 	char *arg;
1038 {
1039 	char name[100];
1040 	long mtime;
1041 	daddr_t seekp;
1042 	daddr_t	lookup();
1043 
1044 	rewind(tfile);
1045 	for (;;) {
1046 		if ((seekp = lookup(arg)) < 0)
1047 			return (1);
1048 		fseek(tfile, seekp, 0);
1049 		fscanf(tfile, "%s %lo", name, &mtime);
1050 		return (stbuf.st_mtime > mtime);
1051 	}
1052 }
1053 
1054 done(n)
1055 {
1056 	unlink(tname);
1057 	exit(n);
1058 }
1059 
1060 prefix(s1, s2)
1061 	register char *s1, *s2;
1062 {
1063 	while (*s1)
1064 		if (*s1++ != *s2++)
1065 			return (0);
1066 	if (*s2)
1067 		return (*s2 == '/');
1068 	return (1);
1069 }
1070 
1071 #define	N	200
1072 int	njab;
1073 
1074 daddr_t
1075 lookup(s)
1076 	char *s;
1077 {
1078 	register i;
1079 	daddr_t a;
1080 
1081 	for(i=0; s[i]; i++)
1082 		if (s[i] == ' ')
1083 			break;
1084 	a = bsrch(s, i, low, high);
1085 	return (a);
1086 }
1087 
1088 daddr_t
1089 bsrch(s, n, l, h)
1090 	daddr_t l, h;
1091 	char *s;
1092 {
1093 	register i, j;
1094 	char b[N];
1095 	daddr_t m, m1;
1096 
1097 	njab = 0;
1098 
1099 loop:
1100 	if (l >= h)
1101 		return (-1L);
1102 	m = l + (h-l)/2 - N/2;
1103 	if (m < l)
1104 		m = l;
1105 	fseek(tfile, m, 0);
1106 	fread(b, 1, N, tfile);
1107 	njab++;
1108 	for(i=0; i<N; i++) {
1109 		if (b[i] == '\n')
1110 			break;
1111 		m++;
1112 	}
1113 	if (m >= h)
1114 		return (-1L);
1115 	m1 = m;
1116 	j = i;
1117 	for(i++; i<N; i++) {
1118 		m1++;
1119 		if (b[i] == '\n')
1120 			break;
1121 	}
1122 	i = cmp(b+j, s, n);
1123 	if (i < 0) {
1124 		h = m;
1125 		goto loop;
1126 	}
1127 	if (i > 0) {
1128 		l = m1;
1129 		goto loop;
1130 	}
1131 	return (m);
1132 }
1133 
1134 cmp(b, s, n)
1135 	char *b, *s;
1136 {
1137 	register i;
1138 
1139 	if (b[0] != '\n')
1140 		exit(2);
1141 	for(i=0; i<n; i++) {
1142 		if (b[i+1] > s[i])
1143 			return (-1);
1144 		if (b[i+1] < s[i])
1145 			return (1);
1146 	}
1147 	return (b[i+1] == ' '? 0 : -1);
1148 }
1149 
1150 readtape (buffer)
1151 	char *buffer;
1152 {
1153 	char *bufp;
1154 	int nread;
1155 
1156 	readtbuf (&bufp, TBLOCK);
1157 	bcopy(bufp, buffer, TBLOCK);
1158 	return(TBLOCK);
1159 }
1160 
1161 readtbuf(bufpp, size)
1162 	char **bufpp;
1163 	int size;
1164 {
1165 	register int i;
1166 
1167 	if (recno >= nblock || first == 0) {
1168 		if ((i = bread(mt, tbuf, TBLOCK*nblock)) < 0) {
1169 			fprintf(stderr, "tar: tape read error\n");
1170 			done(3);
1171 		}
1172 		if (first == 0) {
1173 			if ((i % TBLOCK) != 0) {
1174 				fprintf(stderr, "tar: tape blocksize error\n");
1175 				done(3);
1176 			}
1177 			i /= TBLOCK;
1178 			if (i != nblock) {
1179 				fprintf(stderr, "tar: blocksize = %d\n", i);
1180 				nblock = i;
1181 			}
1182 		}
1183 		recno = 0;
1184 	}
1185 	first = 1;
1186 	if (size > ((nblock-recno)*TBLOCK))
1187 		size = (nblock-recno)*TBLOCK;
1188 	*bufpp = (char *)&tbuf[recno];
1189 	recno += (size/TBLOCK);
1190 	return (size);
1191 }
1192 
1193 writetbuf(buffer, n)
1194 	register char *buffer;
1195 	register int n;
1196 {
1197 	first = 1;
1198 	if (recno >= nblock) {
1199 		if (write(mt, tbuf, TBLOCK*nblock) < 0) {
1200 			fprintf(stderr, "tar: tape write error\n");
1201 			done(2);
1202 		}
1203 		recno = 0;
1204 	}
1205 
1206 	/*
1207 	 *  Special case:  We have an empty tape buffer, and the
1208 	 *  users data size is >= the tape block size:  Avoid
1209 	 *  the bcopy and dma direct to tape.  BIG WIN.  Add the
1210 	 *  residual to the tape buffer.
1211 	 */
1212 	while (recno == 0 && n >= nblock) {
1213 		if (write(mt, buffer, TBLOCK*nblock) < 0) {
1214 			fprintf(stderr, "tar: tape write error\n");
1215 			done(2);
1216 		}
1217 		n -= nblock;
1218 		buffer += (nblock * TBLOCK);
1219 	}
1220 
1221 	while (n-- > 0) {
1222 		bcopy(buffer, (char *)&tbuf[recno++], TBLOCK);
1223 		buffer += TBLOCK;
1224 		if (recno >= nblock) {
1225 			if (write(mt, tbuf, TBLOCK*nblock) < 0) {
1226 				fprintf(stderr, "tar: tape write error\n");
1227 				done(2);
1228 			}
1229 			recno = 0;
1230 		}
1231 	}
1232 
1233 	/* Tell the user how much to write to get in sync */
1234 	return (nblock - recno);
1235 }
1236 
1237 backtape()
1238 {
1239 	static int mtdev = 1;
1240 	static struct mtop mtop = {MTBSR, 1};
1241 	struct mtget mtget;
1242 
1243 	if (mtdev == 1)
1244 		mtdev = ioctl(mt, MTIOCGET, &mtget);
1245 	if (mtdev == 0) {
1246 		if (ioctl(mt, MTIOCTOP, &mtop) < 0) {
1247 			fprintf(stderr, "tar: tape backspace error\n");
1248 			done(4);
1249 		}
1250 	} else
1251 		lseek(mt, (long) -TBLOCK*nblock, 1);
1252 	recno--;
1253 }
1254 
1255 flushtape()
1256 {
1257 	write(mt, tbuf, TBLOCK*nblock);
1258 }
1259 
1260 bread(fd, buf, size)
1261 	int fd;
1262 	char *buf;
1263 	int size;
1264 {
1265 	int count;
1266 	static int lastread = 0;
1267 
1268 	if (!Bflag)
1269 		return (read(fd, buf, size));
1270 	for (count = 0; count < size; count += lastread) {
1271 		if (lastread < 0) {
1272 			if (count > 0)
1273 				return (count);
1274 			return (lastread);
1275 		}
1276 		lastread = read(fd, buf, size - count);
1277 		buf += lastread;
1278 	}
1279 	return (count);
1280 }
1281 
1282 char *
1283 getcwd(buf)
1284 	char *buf;
1285 {
1286 
1287 	if (getwd(buf) == NULL) {
1288 		fprintf(stderr, "tar: %s\n", buf);
1289 		exit(1);
1290 	}
1291 	return (buf);
1292 }
1293