xref: /csrg-svn/old/tar/tar.c (revision 9844)
1*9844Ssam static	char *sccsid = "@(#)tar.c	4.13 (Berkeley) 82/12/19";
26250Sroot 
36250Sroot /*
46250Sroot  * Tape Archival Program
56250Sroot  */
61119Sbill #include <stdio.h>
76413Smckusic #include <sys/param.h>
81119Sbill #include <sys/stat.h>
96654Smckusick #include <dir.h>
108150Smckusick #include <sys/ioctl.h>
113457Swnj #include <sys/mtio.h>
121119Sbill #include <signal.h>
131119Sbill 
141119Sbill #define TBLOCK	512
153355Swnj #define NBLOCK	20
161119Sbill #define NAMSIZ	100
176250Sroot 
181119Sbill union hblock {
191119Sbill 	char dummy[TBLOCK];
201119Sbill 	struct header {
211119Sbill 		char name[NAMSIZ];
221119Sbill 		char mode[8];
231119Sbill 		char uid[8];
241119Sbill 		char gid[8];
251119Sbill 		char size[12];
261119Sbill 		char mtime[12];
271119Sbill 		char chksum[8];
281119Sbill 		char linkflag;
291119Sbill 		char linkname[NAMSIZ];
301119Sbill 	} dbuf;
316250Sroot };
321119Sbill 
331119Sbill struct linkbuf {
341119Sbill 	ino_t	inum;
351119Sbill 	dev_t	devnum;
361119Sbill 	int	count;
371119Sbill 	char	pathname[NAMSIZ];
381119Sbill 	struct	linkbuf *nextp;
396250Sroot };
401119Sbill 
416250Sroot union	hblock dblock;
426250Sroot union	hblock tbuf[NBLOCK];
436250Sroot struct	linkbuf *ihead;
446250Sroot struct	stat stbuf;
451119Sbill 
466250Sroot int	rflag;
476250Sroot int	xflag;
486250Sroot int	vflag;
496250Sroot int	tflag;
506250Sroot int	cflag;
516250Sroot int	mflag;
526250Sroot int	fflag;
536250Sroot int	oflag;
546250Sroot int	pflag;
556250Sroot int	wflag;
566250Sroot int	hflag;
578737Smckusick int	Bflag;
586250Sroot 
596250Sroot int	mt;
606250Sroot int	term;
616250Sroot int	chksum;
626250Sroot int	recno;
636250Sroot int	first;
646250Sroot int	linkerrok;
651119Sbill int	freemem = 1;
663457Swnj int	nblock = NBLOCK;
676250Sroot int	onintr();
686250Sroot int	onquit();
696250Sroot int	onhup();
706250Sroot int	onterm();
711119Sbill 
721119Sbill daddr_t	low;
731119Sbill daddr_t	high;
746250Sroot daddr_t	bsrch();
751119Sbill 
761119Sbill FILE	*tfile;
771119Sbill char	tname[] = "/tmp/tarXXXXXX";
781119Sbill char	*usefile;
796250Sroot char	magtape[] = "/dev/rmt8";
801119Sbill char	*malloc();
816250Sroot char	*sprintf();
826250Sroot char	*strcat();
83*9844Ssam char	*getwd();
841119Sbill 
851119Sbill main(argc, argv)
861119Sbill int	argc;
871119Sbill char	*argv[];
881119Sbill {
891119Sbill 	char *cp;
901119Sbill 
911119Sbill 	if (argc < 2)
921119Sbill 		usage();
931119Sbill 
941119Sbill 	tfile = NULL;
951119Sbill 	usefile =  magtape;
961119Sbill 	argv[argc] = 0;
971119Sbill 	argv++;
981119Sbill 	for (cp = *argv++; *cp; cp++)
991119Sbill 		switch(*cp) {
1006250Sroot 
1011119Sbill 		case 'f':
1021119Sbill 			usefile = *argv++;
1031119Sbill 			fflag++;
1041119Sbill 			break;
1056250Sroot 
1061119Sbill 		case 'c':
1071119Sbill 			cflag++;
1081119Sbill 			rflag++;
1091119Sbill 			break;
1106250Sroot 
1111119Sbill 		case 'o':
1121119Sbill 			oflag++;
1131119Sbill 			break;
1146250Sroot 
1151119Sbill 		case 'p':
1161119Sbill 			pflag++;
1171119Sbill 			break;
1186250Sroot 
1191119Sbill 		case 'u':
1201119Sbill 			mktemp(tname);
1211119Sbill 			if ((tfile = fopen(tname, "w")) == NULL) {
1226250Sroot 				fprintf(stderr,
1236250Sroot 				 "Tar: cannot create temporary file (%s)\n",
1246250Sroot 				 tname);
1251119Sbill 				done(1);
1261119Sbill 			}
1271119Sbill 			fprintf(tfile, "!!!!!/!/!/!/!/!/!/! 000\n");
1286250Sroot 			/*FALL THRU*/
1296250Sroot 
1301119Sbill 		case 'r':
1311119Sbill 			rflag++;
1321119Sbill 			break;
1336250Sroot 
1341119Sbill 		case 'v':
1351119Sbill 			vflag++;
1361119Sbill 			break;
1376250Sroot 
1381119Sbill 		case 'w':
1391119Sbill 			wflag++;
1401119Sbill 			break;
1416250Sroot 
1421119Sbill 		case 'x':
1431119Sbill 			xflag++;
1441119Sbill 			break;
1456250Sroot 
1461119Sbill 		case 't':
1471119Sbill 			tflag++;
1481119Sbill 			break;
1496250Sroot 
1501119Sbill 		case 'm':
1511119Sbill 			mflag++;
1521119Sbill 			break;
1536250Sroot 
1541119Sbill 		case '-':
1551119Sbill 			break;
1566250Sroot 
1571119Sbill 		case '0':
1581119Sbill 		case '1':
1591119Sbill 		case '4':
1601119Sbill 		case '5':
1611119Sbill 		case '7':
1621119Sbill 		case '8':
1631119Sbill 			magtape[8] = *cp;
1641119Sbill 			usefile = magtape;
1651119Sbill 			break;
1666250Sroot 
1671119Sbill 		case 'b':
1681119Sbill 			nblock = atoi(*argv++);
1691119Sbill 			if (nblock > NBLOCK || nblock <= 0) {
1706250Sroot 				fprintf(stderr, "Invalid blocksize. (Max %d)\n",
1716250Sroot 					NBLOCK);
1721119Sbill 				done(1);
1731119Sbill 			}
1741119Sbill 			break;
1756250Sroot 
1761119Sbill 		case 'l':
1771119Sbill 			linkerrok++;
1781119Sbill 			break;
1796250Sroot 
1806250Sroot 		case 'h':
1816250Sroot 			hflag++;
1826250Sroot 			break;
1836250Sroot 
1848737Smckusick 		case 'B':
1858737Smckusick 			Bflag++;
1868737Smckusick 			break;
1878737Smckusick 
1881119Sbill 		default:
1891119Sbill 			fprintf(stderr, "tar: %c: unknown option\n", *cp);
1901119Sbill 			usage();
1911119Sbill 		}
1921119Sbill 
1936250Sroot 	if (!rflag && !xflag && !tflag)
1946250Sroot 		usage();
1951119Sbill 	if (rflag) {
1966250Sroot 		if (cflag && tfile != NULL)
1971119Sbill 			usage();
1981119Sbill 		if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1991119Sbill 			signal(SIGINT, onintr);
2001119Sbill 		if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
2011119Sbill 			signal(SIGHUP, onhup);
2021119Sbill 		if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
2031119Sbill 			signal(SIGQUIT, onquit);
2046250Sroot #ifdef notdef
2051119Sbill 		if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
2061119Sbill 			signal(SIGTERM, onterm);
2076250Sroot #endif
2081119Sbill 		if (strcmp(usefile, "-") == 0) {
2091119Sbill 			if (cflag == 0) {
2106250Sroot 				fprintf(stderr,
2116250Sroot 				 "Can only create standard output archives\n");
2121119Sbill 				done(1);
2131119Sbill 			}
2141119Sbill 			mt = dup(1);
2151119Sbill 			nblock = 1;
2166250Sroot 		} else if ((mt = open(usefile, 2)) < 0) {
2171119Sbill 			if (cflag == 0 || (mt =  creat(usefile, 0666)) < 0) {
2186250Sroot 				fprintf(stderr,
2196250Sroot 					"tar: cannot open %s\n", usefile);
2201119Sbill 				done(1);
2211119Sbill 			}
2221119Sbill 		}
2231119Sbill 		dorep(argv);
2246250Sroot 		done(0);
2251119Sbill 	}
2266250Sroot 	if (strcmp(usefile, "-") == 0) {
2276250Sroot 		mt = dup(0);
2286250Sroot 		nblock = 1;
2296250Sroot 	} else if ((mt = open(usefile, 0)) < 0) {
2306250Sroot 		fprintf(stderr, "tar: cannot open %s\n", usefile);
2316250Sroot 		done(1);
2326250Sroot 	}
2336250Sroot 	if (xflag)
2341119Sbill 		doxtract(argv);
2356250Sroot 	else
2361119Sbill 		dotable();
2371119Sbill 	done(0);
2381119Sbill }
2391119Sbill 
2401119Sbill usage()
2411119Sbill {
2426250Sroot 	fprintf(stderr,
2438737Smckusick "tar: usage  tar -{txruB}[cvfblmh] [tapefile] [blocksize] file1 file2...\n");
2441119Sbill 	done(1);
2451119Sbill }
2461119Sbill 
2471119Sbill dorep(argv)
2486250Sroot 	char *argv[];
2491119Sbill {
2501119Sbill 	register char *cp, *cp2;
2519601Ssam 	char wdir[MAXPATHLEN], tempdir[MAXPATHLEN], *parent;
2521119Sbill 
2531119Sbill 	if (!cflag) {
2541119Sbill 		getdir();
2551119Sbill 		do {
2561119Sbill 			passtape();
2571119Sbill 			if (term)
2581119Sbill 				done(0);
2591119Sbill 			getdir();
2601119Sbill 		} while (!endtape());
2611119Sbill 		if (tfile != NULL) {
2621119Sbill 			char buf[200];
2631119Sbill 
2646250Sroot 			sprintf(buf,
2656250Sroot "sort +0 -1 +1nr %s -o %s; awk '$1 != prev {print; prev=$1}' %s >%sX; mv %sX %s",
2661119Sbill 				tname, tname, tname, tname, tname, tname);
2671119Sbill 			fflush(tfile);
2681119Sbill 			system(buf);
2691119Sbill 			freopen(tname, "r", tfile);
2701119Sbill 			fstat(fileno(tfile), &stbuf);
2711119Sbill 			high = stbuf.st_size;
2721119Sbill 		}
2731119Sbill 	}
2741119Sbill 
275*9844Ssam 	(void) getwd(wdir);
2761119Sbill 	while (*argv && ! term) {
2771119Sbill 		cp2 = *argv;
2781119Sbill 		if (!strcmp(cp2, "-C") && argv[1]) {
2791119Sbill 			argv++;
2801119Sbill 			if (chdir(*argv) < 0)
2811119Sbill 				perror(*argv);
2821119Sbill 			else
283*9844Ssam 				(void) getwd(wdir);
2841119Sbill 			argv++;
2851119Sbill 			continue;
2861119Sbill 		}
2879601Ssam 		parent = wdir;
2881119Sbill 		for (cp = *argv; *cp; cp++)
2891119Sbill 			if (*cp == '/')
2901119Sbill 				cp2 = cp;
2911119Sbill 		if (cp2 != *argv) {
2921119Sbill 			*cp2 = '\0';
2939601Ssam 			if (chdir(*argv) < 0) {
2949601Ssam 				perror(*argv);
2959601Ssam 				continue;
2969601Ssam 			}
297*9844Ssam 			parent = getwd(tempdir);
2981119Sbill 			*cp2 = '/';
2991119Sbill 			cp2++;
3001119Sbill 		}
3019601Ssam 		putfile(*argv++, cp2, parent);
3021119Sbill 		chdir(wdir);
3031119Sbill 	}
3041119Sbill 	putempty();
3051119Sbill 	putempty();
3061119Sbill 	flushtape();
3076250Sroot 	if (linkerrok == 0)
3086250Sroot 		return;
3096250Sroot 	for (; ihead != NULL; ihead = ihead->nextp) {
3106250Sroot 		if (ihead->count == 0)
3116250Sroot 			continue;
3126250Sroot 		fprintf(stderr, "Missing links to %s\n", ihead->pathname);
3136250Sroot 	}
3141119Sbill }
3151119Sbill 
3161119Sbill endtape()
3171119Sbill {
3186250Sroot 	if (dblock.dbuf.name[0] != '\0')
3196250Sroot 		return (0);
3206250Sroot 	backtape();
3216250Sroot 	return (1);
3221119Sbill }
3231119Sbill 
3241119Sbill getdir()
3251119Sbill {
3261119Sbill 	register struct stat *sp;
3271119Sbill 	int i;
3281119Sbill 
3296250Sroot 	readtape((char *)&dblock);
3301119Sbill 	if (dblock.dbuf.name[0] == '\0')
3311119Sbill 		return;
3321119Sbill 	sp = &stbuf;
3331119Sbill 	sscanf(dblock.dbuf.mode, "%o", &i);
3341119Sbill 	sp->st_mode = i;
3351119Sbill 	sscanf(dblock.dbuf.uid, "%o", &i);
3361119Sbill 	sp->st_uid = i;
3371119Sbill 	sscanf(dblock.dbuf.gid, "%o", &i);
3381119Sbill 	sp->st_gid = i;
3391119Sbill 	sscanf(dblock.dbuf.size, "%lo", &sp->st_size);
3401119Sbill 	sscanf(dblock.dbuf.mtime, "%lo", &sp->st_mtime);
3411119Sbill 	sscanf(dblock.dbuf.chksum, "%o", &chksum);
3421119Sbill 	if (chksum != checksum()) {
3431119Sbill 		fprintf(stderr, "directory checksum error\n");
3441119Sbill 		done(2);
3451119Sbill 	}
3461119Sbill 	if (tfile != NULL)
3471119Sbill 		fprintf(tfile, "%s %s\n", dblock.dbuf.name, dblock.dbuf.mtime);
3481119Sbill }
3491119Sbill 
3501119Sbill passtape()
3511119Sbill {
3521119Sbill 	long blocks;
3531119Sbill 	char buf[TBLOCK];
3541119Sbill 
3551119Sbill 	if (dblock.dbuf.linkflag == '1')
3561119Sbill 		return;
3571119Sbill 	blocks = stbuf.st_size;
3581119Sbill 	blocks += TBLOCK-1;
3591119Sbill 	blocks /= TBLOCK;
3601119Sbill 
3611119Sbill 	while (blocks-- > 0)
3621119Sbill 		readtape(buf);
3631119Sbill }
3641119Sbill 
3659601Ssam putfile(longname, shortname, parent)
3666250Sroot 	char *longname;
3676250Sroot 	char *shortname;
3689601Ssam 	char *parent;
3691119Sbill {
3701119Sbill 	int infile;
3711119Sbill 	long blocks;
3721119Sbill 	char buf[TBLOCK];
3731119Sbill 	register char *cp, *cp2;
3745931Smckusic 	struct direct *dp;
3755931Smckusic 	DIR *dirp;
3761119Sbill 	int i, j;
3779601Ssam 	char newparent[NAMSIZ+64];
3781119Sbill 
3791119Sbill 	infile = open(shortname, 0);
3801119Sbill 	if (infile < 0) {
3811119Sbill 		fprintf(stderr, "tar: %s: cannot open file\n", longname);
3821119Sbill 		return;
3831119Sbill 	}
3849601Ssam 	if (!hflag)
3859601Ssam 		lstat(shortname, &stbuf);
3869601Ssam 	else if (stat(shortname, &stbuf) < 0) {
3879601Ssam 		perror(longname);
3889601Ssam 		close(infile);
3899601Ssam 		return;
3909601Ssam 	}
3911119Sbill 	if (tfile != NULL && checkupdate(longname) == 0) {
3921119Sbill 		close(infile);
3931119Sbill 		return;
3941119Sbill 	}
3951119Sbill 	if (checkw('r', longname) == 0) {
3961119Sbill 		close(infile);
3971119Sbill 		return;
3981119Sbill 	}
3991119Sbill 
4001119Sbill 	if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
4016250Sroot 		for (i = 0, cp = buf; *cp++ = longname[i++];)
4026250Sroot 			;
4031119Sbill 		*--cp = '/';
4041119Sbill 		*++cp = 0  ;
4051119Sbill 		if (!oflag) {
4066250Sroot 			if ((cp - buf) >= NAMSIZ) {
4076250Sroot 				fprintf(stderr, "%s: file name too long\n",
4086250Sroot 					longname);
4096250Sroot 				close(infile);
4106250Sroot 				return;
4116250Sroot 			}
4126250Sroot 			stbuf.st_size = 0;
4136250Sroot 			tomodes(&stbuf);
4146250Sroot 			strcpy(dblock.dbuf.name,buf);
4156250Sroot 			sprintf(dblock.dbuf.chksum, "%6o", checksum());
4166250Sroot 			writetape((char *)&dblock);
4171119Sbill 		}
4189601Ssam 		sprintf(newparent, "%s/%s", parent, shortname);
4191119Sbill 		chdir(shortname);
4205931Smckusic 		close(infile);
4215931Smckusic 		if ((dirp = opendir(".")) == NULL) {
4225931Smckusic 			fprintf(stderr, "%s: directory read error\n", longname);
4239601Ssam 			chdir(parent);
4245931Smckusic 			return;
4255931Smckusic 		}
4265931Smckusic 		while ((dp = readdir(dirp)) != NULL && !term) {
4275931Smckusic 			if (dp->d_ino == 0)
4281119Sbill 				continue;
4296250Sroot 			if (!strcmp(".", dp->d_name) ||
4306250Sroot 			    !strcmp("..", dp->d_name))
4311119Sbill 				continue;
4325931Smckusic 			strcpy(cp, dp->d_name);
4335931Smckusic 			i = telldir(dirp);
4345931Smckusic 			closedir(dirp);
4359601Ssam 			putfile(buf, cp, newparent);
4365931Smckusic 			dirp = opendir(".");
4375931Smckusic 			seekdir(dirp, i);
4381119Sbill 		}
4395931Smckusic 		closedir(dirp);
4409601Ssam 		chdir(parent);
4411119Sbill 		return;
4421119Sbill 	}
4436250Sroot 	i = stbuf.st_mode & S_IFMT;
4446250Sroot 	if (i != S_IFREG && i != S_IFLNK) {
4456250Sroot 		fprintf(stderr, "tar: %s is not a file. Not dumped\n",
4466250Sroot 			longname);
4471119Sbill 		return;
4481119Sbill 	}
4491119Sbill 	tomodes(&stbuf);
4506250Sroot 	cp2 = longname; cp = dblock.dbuf.name; i = 0;
4516250Sroot 	while ((*cp++ = *cp2++) && i < NAMSIZ)
4526250Sroot 		i++;
4531119Sbill 	if (i >= NAMSIZ) {
4541119Sbill 		fprintf(stderr, "%s: file name too long\n", longname);
4551119Sbill 		close(infile);
4561119Sbill 		return;
4571119Sbill 	}
4586250Sroot 	if ((stbuf.st_mode & S_IFMT) == S_IFLNK) {
4596250Sroot 		if (stbuf.st_size + 1 >= NAMSIZ) {
4606250Sroot 			fprintf(stderr, "%s: symbolic link too long\n",
4616250Sroot 				longname);
4626250Sroot 			close(infile);
4636250Sroot 			return;
4646250Sroot 		}
4659601Ssam 		i = readlink(shortname, dblock.dbuf.linkname, NAMSIZ - 1);
4666250Sroot 		if (i < 0) {
4679601Ssam 			perror(longname);
4686250Sroot 			close(infile);
4696250Sroot 			return;
4706250Sroot 		}
4716250Sroot 		dblock.dbuf.linkname[i] = '\0';
4726250Sroot 		dblock.dbuf.linkflag = '2';
4736250Sroot 		if (vflag) {
4746250Sroot 			fprintf(stderr, "a %s ", longname);
4756250Sroot 			fprintf(stderr, "symbolic link to %s\n",
4766250Sroot 				dblock.dbuf.linkname);
4776250Sroot 		}
4786250Sroot 		sprintf(dblock.dbuf.size, "%11lo", 0);
4796250Sroot 		sprintf(dblock.dbuf.chksum, "%6o", checksum());
4806250Sroot 		writetape((char *)&dblock);
4816250Sroot 		close(infile);
4826250Sroot 		return;
4836250Sroot 	}
4841119Sbill 	if (stbuf.st_nlink > 1) {
4851119Sbill 		struct linkbuf *lp;
4861119Sbill 		int found = 0;
4871119Sbill 
4886250Sroot 		for (lp = ihead; lp != NULL; lp = lp->nextp)
4896250Sroot 			if (lp->inum == stbuf.st_ino &&
4906250Sroot 			    lp->devnum == stbuf.st_dev) {
4911119Sbill 				found++;
4921119Sbill 				break;
4931119Sbill 			}
4941119Sbill 		if (found) {
4951119Sbill 			strcpy(dblock.dbuf.linkname, lp->pathname);
4961119Sbill 			dblock.dbuf.linkflag = '1';
4971119Sbill 			sprintf(dblock.dbuf.chksum, "%6o", checksum());
4981119Sbill 			writetape( (char *) &dblock);
4991119Sbill 			if (vflag) {
5001119Sbill 				fprintf(stderr, "a %s ", longname);
5011119Sbill 				fprintf(stderr, "link to %s\n", lp->pathname);
5021119Sbill 			}
5031119Sbill 			lp->count--;
5041119Sbill 			close(infile);
5051119Sbill 			return;
5061119Sbill 		}
5076250Sroot 		lp = (struct linkbuf *) malloc(sizeof(*lp));
5086250Sroot 		if (lp == NULL) {
5096250Sroot 			if (freemem) {
5106250Sroot 				fprintf(stderr,
5116250Sroot 				  "Out of memory. Link information lost\n");
5126250Sroot 				freemem = 0;
5131119Sbill 			}
5146250Sroot 		} else {
5156250Sroot 			lp->nextp = ihead;
5166250Sroot 			ihead = lp;
5176250Sroot 			lp->inum = stbuf.st_ino;
5186250Sroot 			lp->devnum = stbuf.st_dev;
5196250Sroot 			lp->count = stbuf.st_nlink - 1;
5206250Sroot 			strcpy(lp->pathname, longname);
5211119Sbill 		}
5221119Sbill 	}
5231119Sbill 	blocks = (stbuf.st_size + (TBLOCK-1)) / TBLOCK;
5241119Sbill 	if (vflag) {
5251119Sbill 		fprintf(stderr, "a %s ", longname);
5261119Sbill 		fprintf(stderr, "%ld blocks\n", blocks);
5271119Sbill 	}
5281119Sbill 	sprintf(dblock.dbuf.chksum, "%6o", checksum());
5296250Sroot 	writetape((char *)&dblock);
5301119Sbill 
5311119Sbill 	while ((i = read(infile, buf, TBLOCK)) > 0 && blocks > 0) {
5321119Sbill 		writetape(buf);
5331119Sbill 		blocks--;
5341119Sbill 	}
5351119Sbill 	close(infile);
5361119Sbill 	if (blocks != 0 || i != 0)
5371119Sbill 		fprintf(stderr, "%s: file changed size\n", longname);
5386250Sroot 	while (--blocks >=  0)
5391119Sbill 		putempty();
5401119Sbill }
5411119Sbill 
5421119Sbill doxtract(argv)
5436250Sroot 	char *argv[];
5441119Sbill {
5451119Sbill 	long blocks, bytes;
5461119Sbill 	char buf[TBLOCK];
5471119Sbill 	char **cp;
5481119Sbill 	int ofile;
5491119Sbill 
5501119Sbill 	for (;;) {
5511119Sbill 		getdir();
5521119Sbill 		if (endtape())
5531119Sbill 			break;
5541119Sbill 		if (*argv == 0)
5551119Sbill 			goto gotit;
5561119Sbill 		for (cp = argv; *cp; cp++)
5571119Sbill 			if (prefix(*cp, dblock.dbuf.name))
5581119Sbill 				goto gotit;
5591119Sbill 		passtape();
5601119Sbill 		continue;
5611119Sbill 
5621119Sbill gotit:
5631119Sbill 		if (checkw('x', dblock.dbuf.name) == 0) {
5641119Sbill 			passtape();
5651119Sbill 			continue;
5661119Sbill 		}
5676250Sroot 		if (checkdir(dblock.dbuf.name))
5686250Sroot 			continue;
5696250Sroot 		if (dblock.dbuf.linkflag == '2') {
5706250Sroot 			unlink(dblock.dbuf.name);
5716250Sroot 			if (symlink(dblock.dbuf.linkname, dblock.dbuf.name)<0) {
5726250Sroot 				fprintf(stderr, "%s: symbolic link failed\n",
5736250Sroot 					dblock.dbuf.name);
5746250Sroot 				continue;
5756250Sroot 			}
5766250Sroot 			if (vflag)
5776250Sroot 				fprintf(stderr, "x %s symbolic link to %s\n",
5786250Sroot 				  dblock.dbuf.name, dblock.dbuf.linkname);
5798150Smckusick #ifdef notdef
5808150Smckusick 			/* ignore alien orders */
5816250Sroot 			chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid);
5826250Sroot 			if (mflag == 0) {
5836250Sroot 				time_t timep[2];
5841119Sbill 
5856250Sroot 				timep[0] = time(0);
5866250Sroot 				timep[1] = stbuf.st_mtime;
5876250Sroot 				utime(dblock.dbuf.name, timep);
5886250Sroot 			}
5896250Sroot 			if (pflag)
5906250Sroot 				chmod(dblock.dbuf.name, stbuf.st_mode & 07777);
5918150Smckusick #endif
5921119Sbill 			continue;
5936250Sroot 		}
5941119Sbill 		if (dblock.dbuf.linkflag == '1') {
5951119Sbill 			unlink(dblock.dbuf.name);
5961119Sbill 			if (link(dblock.dbuf.linkname, dblock.dbuf.name) < 0) {
5976250Sroot 				fprintf(stderr, "%s: cannot link\n",
5986250Sroot 					dblock.dbuf.name);
5991119Sbill 				continue;
6001119Sbill 			}
6011119Sbill 			if (vflag)
6023457Swnj 				fprintf(stderr, "%s linked to %s\n",
6033457Swnj 					dblock.dbuf.name, dblock.dbuf.linkname);
6041119Sbill 			continue;
6051119Sbill 		}
6066250Sroot 		if ((ofile = creat(dblock.dbuf.name,stbuf.st_mode&0xfff)) < 0) {
6076250Sroot 			fprintf(stderr, "tar: %s - cannot create\n",
6086250Sroot 				dblock.dbuf.name);
6091119Sbill 			passtape();
6101119Sbill 			continue;
6111119Sbill 		}
6121926Swnj 		chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid);
6131119Sbill 		blocks = ((bytes = stbuf.st_size) + TBLOCK-1)/TBLOCK;
6141119Sbill 		if (vflag)
6153457Swnj 			fprintf(stderr, "x %s, %ld bytes, %ld tape blocks\n",
6163457Swnj 				dblock.dbuf.name, bytes, blocks);
6176250Sroot 		for (; blocks-- > 0; bytes -= TBLOCK) {
6181119Sbill 			readtape(buf);
6191119Sbill 			if (bytes > TBLOCK) {
6201119Sbill 				if (write(ofile, buf, TBLOCK) < 0) {
6216250Sroot 					fprintf(stderr,
6226250Sroot 					"tar: %s: HELP - extract write error\n",
6236250Sroot 					 dblock.dbuf.name);
6241119Sbill 					done(2);
6251119Sbill 				}
6266250Sroot 				continue;
6276250Sroot 			}
6286250Sroot 			if (write(ofile, buf, (int) bytes) < 0) {
6296250Sroot 				fprintf(stderr,
6306250Sroot 					"tar: %s: HELP - extract write error\n",
6316250Sroot 					dblock.dbuf.name);
6326250Sroot 				done(2);
6336250Sroot 			}
6341119Sbill 		}
6351119Sbill 		close(ofile);
6361119Sbill 		if (mflag == 0) {
6371119Sbill 			time_t timep[2];
6381119Sbill 
6391119Sbill 			timep[0] = time(NULL);
6401119Sbill 			timep[1] = stbuf.st_mtime;
6411119Sbill 			utime(dblock.dbuf.name, timep);
6421119Sbill 		}
6431926Swnj 		if (pflag)
6446250Sroot 			chmod(dblock.dbuf.name, stbuf.st_mode & 07777);
6451119Sbill 	}
6461119Sbill }
6471119Sbill 
6481119Sbill dotable()
6491119Sbill {
6501119Sbill 	for (;;) {
6511119Sbill 		getdir();
6521119Sbill 		if (endtape())
6531119Sbill 			break;
6541119Sbill 		if (vflag)
6551119Sbill 			longt(&stbuf);
6561119Sbill 		printf("%s", dblock.dbuf.name);
6571119Sbill 		if (dblock.dbuf.linkflag == '1')
6581119Sbill 			printf(" linked to %s", dblock.dbuf.linkname);
6596250Sroot 		if (dblock.dbuf.linkflag == '2')
6606250Sroot 			printf(" symbolic link to %s", dblock.dbuf.linkname);
6611119Sbill 		printf("\n");
6621119Sbill 		passtape();
6631119Sbill 	}
6641119Sbill }
6651119Sbill 
6661119Sbill putempty()
6671119Sbill {
6681119Sbill 	char buf[TBLOCK];
6691119Sbill 	char *cp;
6701119Sbill 
6711119Sbill 	for (cp = buf; cp < &buf[TBLOCK]; )
6721119Sbill 		*cp++ = '\0';
6731119Sbill 	writetape(buf);
6741119Sbill }
6751119Sbill 
6761119Sbill longt(st)
6776250Sroot 	register struct stat *st;
6781119Sbill {
6791119Sbill 	register char *cp;
6801119Sbill 	char *ctime();
6811119Sbill 
6821119Sbill 	pmode(st);
6831119Sbill 	printf("%3d/%1d", st->st_uid, st->st_gid);
6841119Sbill 	printf("%7D", st->st_size);
6851119Sbill 	cp = ctime(&st->st_mtime);
6861119Sbill 	printf(" %-12.12s %-4.4s ", cp+4, cp+20);
6871119Sbill }
6881119Sbill 
6891119Sbill #define	SUID	04000
6901119Sbill #define	SGID	02000
6911119Sbill #define	ROWN	0400
6921119Sbill #define	WOWN	0200
6931119Sbill #define	XOWN	0100
6941119Sbill #define	RGRP	040
6951119Sbill #define	WGRP	020
6961119Sbill #define	XGRP	010
6971119Sbill #define	ROTH	04
6981119Sbill #define	WOTH	02
6991119Sbill #define	XOTH	01
7001119Sbill #define	STXT	01000
7011119Sbill int	m1[] = { 1, ROWN, 'r', '-' };
7021119Sbill int	m2[] = { 1, WOWN, 'w', '-' };
7031119Sbill int	m3[] = { 2, SUID, 's', XOWN, 'x', '-' };
7041119Sbill int	m4[] = { 1, RGRP, 'r', '-' };
7051119Sbill int	m5[] = { 1, WGRP, 'w', '-' };
7061119Sbill int	m6[] = { 2, SGID, 's', XGRP, 'x', '-' };
7071119Sbill int	m7[] = { 1, ROTH, 'r', '-' };
7081119Sbill int	m8[] = { 1, WOTH, 'w', '-' };
7091119Sbill int	m9[] = { 2, STXT, 't', XOTH, 'x', '-' };
7101119Sbill 
7111119Sbill int	*m[] = { m1, m2, m3, m4, m5, m6, m7, m8, m9};
7121119Sbill 
7131119Sbill pmode(st)
7146250Sroot 	register struct stat *st;
7151119Sbill {
7161119Sbill 	register int **mp;
7171119Sbill 
7181119Sbill 	for (mp = &m[0]; mp < &m[9];)
7191119Sbill 		select(*mp++, st);
7201119Sbill }
7211119Sbill 
7221119Sbill select(pairp, st)
7236250Sroot 	int *pairp;
7246250Sroot 	struct stat *st;
7251119Sbill {
7261119Sbill 	register int n, *ap;
7271119Sbill 
7281119Sbill 	ap = pairp;
7291119Sbill 	n = *ap++;
7301119Sbill 	while (--n>=0 && (st->st_mode&*ap++)==0)
7311119Sbill 		ap++;
7321119Sbill 	printf("%c", *ap);
7331119Sbill }
7341119Sbill 
7351119Sbill checkdir(name)
7366250Sroot 	register char *name;
7371119Sbill {
7381119Sbill 	register char *cp;
7396250Sroot 
7401119Sbill 	for (cp = name; *cp; cp++) {
7416250Sroot 		if (*cp != '/')
7426250Sroot 			continue;
7436250Sroot 		*cp = '\0';
7446250Sroot 		if (access(name, 1) < 0) {
745*9844Ssam 			if (mkdir(name, 0777) < 0) {
746*9844Ssam 				perror(name);
7476250Sroot 				done(0);
7481119Sbill 			}
7496250Sroot 			chown(name, stbuf.st_uid, stbuf.st_gid);
7506250Sroot 			if (pflag)
751*9844Ssam 				chmod(name, stbuf.st_mode & 0777);
7521119Sbill 		}
7536250Sroot 		*cp = '/';
7541119Sbill 	}
7556250Sroot 	return (cp[-1]=='/');
7561119Sbill }
7571119Sbill 
7581119Sbill onintr()
7591119Sbill {
7601119Sbill 	signal(SIGINT, SIG_IGN);
7611119Sbill 	term++;
7621119Sbill }
7631119Sbill 
7641119Sbill onquit()
7651119Sbill {
7661119Sbill 	signal(SIGQUIT, SIG_IGN);
7671119Sbill 	term++;
7681119Sbill }
7691119Sbill 
7701119Sbill onhup()
7711119Sbill {
7721119Sbill 	signal(SIGHUP, SIG_IGN);
7731119Sbill 	term++;
7741119Sbill }
7751119Sbill 
7761119Sbill onterm()
7771119Sbill {
7781119Sbill 	signal(SIGTERM, SIG_IGN);
7791119Sbill 	term++;
7801119Sbill }
7811119Sbill 
7821119Sbill tomodes(sp)
7831119Sbill register struct stat *sp;
7841119Sbill {
7851119Sbill 	register char *cp;
7861119Sbill 
7871119Sbill 	for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++)
7881119Sbill 		*cp = '\0';
7891119Sbill 	sprintf(dblock.dbuf.mode, "%6o ", sp->st_mode & 07777);
7901119Sbill 	sprintf(dblock.dbuf.uid, "%6o ", sp->st_uid);
7911119Sbill 	sprintf(dblock.dbuf.gid, "%6o ", sp->st_gid);
7921119Sbill 	sprintf(dblock.dbuf.size, "%11lo ", sp->st_size);
7931119Sbill 	sprintf(dblock.dbuf.mtime, "%11lo ", sp->st_mtime);
7941119Sbill }
7951119Sbill 
7961119Sbill checksum()
7971119Sbill {
7981119Sbill 	register i;
7991119Sbill 	register char *cp;
8001119Sbill 
8016250Sroot 	for (cp = dblock.dbuf.chksum;
8026250Sroot 	     cp < &dblock.dbuf.chksum[sizeof(dblock.dbuf.chksum)]; cp++)
8031119Sbill 		*cp = ' ';
8041119Sbill 	i = 0;
8051119Sbill 	for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++)
8061119Sbill 		i += *cp;
8076250Sroot 	return (i);
8081119Sbill }
8091119Sbill 
8101119Sbill checkw(c, name)
8116250Sroot 	char *name;
8121119Sbill {
8136250Sroot 	if (!wflag)
8146250Sroot 		return (1);
8156250Sroot 	printf("%c ", c);
8166250Sroot 	if (vflag)
8176250Sroot 		longt(&stbuf);
8186250Sroot 	printf("%s: ", name);
8196250Sroot 	return (response() == 'y');
8201119Sbill }
8211119Sbill 
8221119Sbill response()
8231119Sbill {
8241119Sbill 	char c;
8251119Sbill 
8261119Sbill 	c = getchar();
8271119Sbill 	if (c != '\n')
8286250Sroot 		while (getchar() != '\n')
8296250Sroot 			;
8306250Sroot 	else
8316250Sroot 		c = 'n';
8326250Sroot 	return (c);
8331119Sbill }
8341119Sbill 
8351119Sbill checkupdate(arg)
8366250Sroot 	char *arg;
8371119Sbill {
8381119Sbill 	char name[100];
8396250Sroot 	long mtime;
8401119Sbill 	daddr_t seekp;
8411119Sbill 	daddr_t	lookup();
8421119Sbill 
8431119Sbill 	rewind(tfile);
8441119Sbill 	for (;;) {
8451119Sbill 		if ((seekp = lookup(arg)) < 0)
8466250Sroot 			return (1);
8471119Sbill 		fseek(tfile, seekp, 0);
8481119Sbill 		fscanf(tfile, "%s %lo", name, &mtime);
8496250Sroot 		return (stbuf.st_mtime > mtime);
8501119Sbill 	}
8511119Sbill }
8521119Sbill 
8531119Sbill done(n)
8541119Sbill {
8551119Sbill 	unlink(tname);
8561119Sbill 	exit(n);
8571119Sbill }
8581119Sbill 
8591119Sbill prefix(s1, s2)
8606250Sroot 	register char *s1, *s2;
8611119Sbill {
8621119Sbill 	while (*s1)
8631119Sbill 		if (*s1++ != *s2++)
8646250Sroot 			return (0);
8651119Sbill 	if (*s2)
8666250Sroot 		return (*s2 == '/');
8676250Sroot 	return (1);
8681119Sbill }
8691119Sbill 
8701119Sbill #define	N	200
8711119Sbill int	njab;
8726250Sroot 
8731119Sbill daddr_t
8741119Sbill lookup(s)
8756250Sroot 	char *s;
8761119Sbill {
8771119Sbill 	register i;
8781119Sbill 	daddr_t a;
8791119Sbill 
8801119Sbill 	for(i=0; s[i]; i++)
8816250Sroot 		if (s[i] == ' ')
8821119Sbill 			break;
8831119Sbill 	a = bsrch(s, i, low, high);
8846250Sroot 	return (a);
8851119Sbill }
8861119Sbill 
8871119Sbill daddr_t
8881119Sbill bsrch(s, n, l, h)
8896250Sroot 	daddr_t l, h;
8906250Sroot 	char *s;
8911119Sbill {
8921119Sbill 	register i, j;
8931119Sbill 	char b[N];
8941119Sbill 	daddr_t m, m1;
8951119Sbill 
8961119Sbill 	njab = 0;
8971119Sbill 
8981119Sbill loop:
8996250Sroot 	if (l >= h)
9006250Sroot 		return (-1L);
9011119Sbill 	m = l + (h-l)/2 - N/2;
9026250Sroot 	if (m < l)
9031119Sbill 		m = l;
9041119Sbill 	fseek(tfile, m, 0);
9051119Sbill 	fread(b, 1, N, tfile);
9061119Sbill 	njab++;
9071119Sbill 	for(i=0; i<N; i++) {
9086250Sroot 		if (b[i] == '\n')
9091119Sbill 			break;
9101119Sbill 		m++;
9111119Sbill 	}
9126250Sroot 	if (m >= h)
9136250Sroot 		return (-1L);
9141119Sbill 	m1 = m;
9151119Sbill 	j = i;
9161119Sbill 	for(i++; i<N; i++) {
9171119Sbill 		m1++;
9186250Sroot 		if (b[i] == '\n')
9191119Sbill 			break;
9201119Sbill 	}
9211119Sbill 	i = cmp(b+j, s, n);
9226250Sroot 	if (i < 0) {
9231119Sbill 		h = m;
9241119Sbill 		goto loop;
9251119Sbill 	}
9266250Sroot 	if (i > 0) {
9271119Sbill 		l = m1;
9281119Sbill 		goto loop;
9291119Sbill 	}
9306250Sroot 	return (m);
9311119Sbill }
9321119Sbill 
9331119Sbill cmp(b, s, n)
9346250Sroot 	char *b, *s;
9351119Sbill {
9361119Sbill 	register i;
9371119Sbill 
9386250Sroot 	if (b[0] != '\n')
9391119Sbill 		exit(2);
9401119Sbill 	for(i=0; i<n; i++) {
9416250Sroot 		if (b[i+1] > s[i])
9426250Sroot 			return (-1);
9436250Sroot 		if (b[i+1] < s[i])
9446250Sroot 			return (1);
9451119Sbill 	}
9466250Sroot 	return (b[i+1] == ' '? 0 : -1);
9471119Sbill }
9481119Sbill 
9491119Sbill readtape(buffer)
9506250Sroot 	char *buffer;
9511119Sbill {
9523457Swnj 	register int i;
9531119Sbill 
9541119Sbill 	if (recno >= nblock || first == 0) {
9558737Smckusick 		if ((i = bread(mt, tbuf, TBLOCK*nblock)) < 0) {
9561119Sbill 			fprintf(stderr, "Tar: tape read error\n");
9571119Sbill 			done(3);
9581119Sbill 		}
9591119Sbill 		if (first == 0) {
9601119Sbill 			if ((i % TBLOCK) != 0) {
9611119Sbill 				fprintf(stderr, "Tar: tape blocksize error\n");
9621119Sbill 				done(3);
9631119Sbill 			}
9641119Sbill 			i /= TBLOCK;
9653457Swnj 			if (i != nblock) {
9661119Sbill 				fprintf(stderr, "Tar: blocksize = %d\n", i);
9671119Sbill 				nblock = i;
9681119Sbill 			}
9691119Sbill 		}
9701119Sbill 		recno = 0;
9711119Sbill 	}
9721119Sbill 	first = 1;
9731119Sbill 	copy(buffer, &tbuf[recno++]);
9746250Sroot 	return (TBLOCK);
9751119Sbill }
9761119Sbill 
9771119Sbill writetape(buffer)
9786250Sroot 	char *buffer;
9791119Sbill {
9801119Sbill 	first = 1;
9811119Sbill 	if (recno >= nblock) {
9821119Sbill 		if (write(mt, tbuf, TBLOCK*nblock) < 0) {
9831119Sbill 			fprintf(stderr, "Tar: tape write error\n");
9841119Sbill 			done(2);
9851119Sbill 		}
9861119Sbill 		recno = 0;
9871119Sbill 	}
9881119Sbill 	copy(&tbuf[recno++], buffer);
9891119Sbill 	if (recno >= nblock) {
9901119Sbill 		if (write(mt, tbuf, TBLOCK*nblock) < 0) {
9911119Sbill 			fprintf(stderr, "Tar: tape write error\n");
9921119Sbill 			done(2);
9931119Sbill 		}
9941119Sbill 		recno = 0;
9951119Sbill 	}
9966250Sroot 	return (TBLOCK);
9971119Sbill }
9981119Sbill 
9991119Sbill backtape()
10001119Sbill {
10013457Swnj 	static int mtdev = 1;
10023457Swnj 	static struct mtop mtop = {MTBSR, 1};
10033457Swnj 	struct mtget mtget;
10043457Swnj 
10053457Swnj 	if (mtdev == 1)
10063457Swnj 		mtdev = ioctl(mt, MTIOCGET, &mtget);
10073457Swnj 	if (mtdev == 0) {
10083457Swnj 		if (ioctl(mt, MTIOCTOP, &mtop) < 0) {
10093457Swnj 			fprintf(stderr, "Tar: tape backspace error\n");
10101119Sbill 			done(4);
10111119Sbill 		}
10123457Swnj 	} else
10133457Swnj 		lseek(mt, (long) -TBLOCK*nblock, 1);
10143457Swnj 	recno--;
10151119Sbill }
10161119Sbill 
10171119Sbill flushtape()
10181119Sbill {
10191119Sbill 	write(mt, tbuf, TBLOCK*nblock);
10201119Sbill }
10211119Sbill 
10221119Sbill copy(to, from)
10236250Sroot 	register char *to, *from;
10241119Sbill {
10251119Sbill 	register i;
10261119Sbill 
10271119Sbill 	i = TBLOCK;
10281119Sbill 	do {
10291119Sbill 		*to++ = *from++;
10301119Sbill 	} while (--i);
10311119Sbill }
10328737Smckusick 
10338737Smckusick bread(fd, buf, size)
10348737Smckusick 	int fd;
10358737Smckusick 	char *buf;
10368737Smckusick 	int size;
10378737Smckusick {
10388737Smckusick 	int count;
10398737Smckusick 	static int lastread = 0;
10408737Smckusick 
10418737Smckusick 	if (!Bflag)
10428737Smckusick 		return (read(fd, buf, size));
10438737Smckusick 	for (count = 0; count < size; count += lastread) {
10448737Smckusick 		if (lastread < 0) {
10458737Smckusick 			if (count > 0)
10468737Smckusick 				return (count);
10478737Smckusick 			return (lastread);
10488737Smckusick 		}
10498737Smckusick 		lastread = read(fd, buf, size - count);
10508737Smckusick 		buf += lastread;
10518737Smckusick 	}
10528737Smckusick 	return (count);
10538737Smckusick }
1054