xref: /csrg-svn/old/tar/tar.c (revision 9601)
1*9601Ssam static	char *sccsid = "@(#)tar.c	4.12 (Berkeley) 82/12/09";
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();
831119Sbill 
841119Sbill main(argc, argv)
851119Sbill int	argc;
861119Sbill char	*argv[];
871119Sbill {
881119Sbill 	char *cp;
891119Sbill 
901119Sbill 	if (argc < 2)
911119Sbill 		usage();
921119Sbill 
931119Sbill 	tfile = NULL;
941119Sbill 	usefile =  magtape;
951119Sbill 	argv[argc] = 0;
961119Sbill 	argv++;
971119Sbill 	for (cp = *argv++; *cp; cp++)
981119Sbill 		switch(*cp) {
996250Sroot 
1001119Sbill 		case 'f':
1011119Sbill 			usefile = *argv++;
1021119Sbill 			fflag++;
1031119Sbill 			break;
1046250Sroot 
1051119Sbill 		case 'c':
1061119Sbill 			cflag++;
1071119Sbill 			rflag++;
1081119Sbill 			break;
1096250Sroot 
1101119Sbill 		case 'o':
1111119Sbill 			oflag++;
1121119Sbill 			break;
1136250Sroot 
1141119Sbill 		case 'p':
1151119Sbill 			pflag++;
1161119Sbill 			break;
1176250Sroot 
1181119Sbill 		case 'u':
1191119Sbill 			mktemp(tname);
1201119Sbill 			if ((tfile = fopen(tname, "w")) == NULL) {
1216250Sroot 				fprintf(stderr,
1226250Sroot 				 "Tar: cannot create temporary file (%s)\n",
1236250Sroot 				 tname);
1241119Sbill 				done(1);
1251119Sbill 			}
1261119Sbill 			fprintf(tfile, "!!!!!/!/!/!/!/!/!/! 000\n");
1276250Sroot 			/*FALL THRU*/
1286250Sroot 
1291119Sbill 		case 'r':
1301119Sbill 			rflag++;
1311119Sbill 			break;
1326250Sroot 
1331119Sbill 		case 'v':
1341119Sbill 			vflag++;
1351119Sbill 			break;
1366250Sroot 
1371119Sbill 		case 'w':
1381119Sbill 			wflag++;
1391119Sbill 			break;
1406250Sroot 
1411119Sbill 		case 'x':
1421119Sbill 			xflag++;
1431119Sbill 			break;
1446250Sroot 
1451119Sbill 		case 't':
1461119Sbill 			tflag++;
1471119Sbill 			break;
1486250Sroot 
1491119Sbill 		case 'm':
1501119Sbill 			mflag++;
1511119Sbill 			break;
1526250Sroot 
1531119Sbill 		case '-':
1541119Sbill 			break;
1556250Sroot 
1561119Sbill 		case '0':
1571119Sbill 		case '1':
1581119Sbill 		case '4':
1591119Sbill 		case '5':
1601119Sbill 		case '7':
1611119Sbill 		case '8':
1621119Sbill 			magtape[8] = *cp;
1631119Sbill 			usefile = magtape;
1641119Sbill 			break;
1656250Sroot 
1661119Sbill 		case 'b':
1671119Sbill 			nblock = atoi(*argv++);
1681119Sbill 			if (nblock > NBLOCK || nblock <= 0) {
1696250Sroot 				fprintf(stderr, "Invalid blocksize. (Max %d)\n",
1706250Sroot 					NBLOCK);
1711119Sbill 				done(1);
1721119Sbill 			}
1731119Sbill 			break;
1746250Sroot 
1751119Sbill 		case 'l':
1761119Sbill 			linkerrok++;
1771119Sbill 			break;
1786250Sroot 
1796250Sroot 		case 'h':
1806250Sroot 			hflag++;
1816250Sroot 			break;
1826250Sroot 
1838737Smckusick 		case 'B':
1848737Smckusick 			Bflag++;
1858737Smckusick 			break;
1868737Smckusick 
1871119Sbill 		default:
1881119Sbill 			fprintf(stderr, "tar: %c: unknown option\n", *cp);
1891119Sbill 			usage();
1901119Sbill 		}
1911119Sbill 
1926250Sroot 	if (!rflag && !xflag && !tflag)
1936250Sroot 		usage();
1941119Sbill 	if (rflag) {
1956250Sroot 		if (cflag && tfile != NULL)
1961119Sbill 			usage();
1971119Sbill 		if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1981119Sbill 			signal(SIGINT, onintr);
1991119Sbill 		if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
2001119Sbill 			signal(SIGHUP, onhup);
2011119Sbill 		if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
2021119Sbill 			signal(SIGQUIT, onquit);
2036250Sroot #ifdef notdef
2041119Sbill 		if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
2051119Sbill 			signal(SIGTERM, onterm);
2066250Sroot #endif
2071119Sbill 		if (strcmp(usefile, "-") == 0) {
2081119Sbill 			if (cflag == 0) {
2096250Sroot 				fprintf(stderr,
2106250Sroot 				 "Can only create standard output archives\n");
2111119Sbill 				done(1);
2121119Sbill 			}
2131119Sbill 			mt = dup(1);
2141119Sbill 			nblock = 1;
2156250Sroot 		} else if ((mt = open(usefile, 2)) < 0) {
2161119Sbill 			if (cflag == 0 || (mt =  creat(usefile, 0666)) < 0) {
2176250Sroot 				fprintf(stderr,
2186250Sroot 					"tar: cannot open %s\n", usefile);
2191119Sbill 				done(1);
2201119Sbill 			}
2211119Sbill 		}
2221119Sbill 		dorep(argv);
2236250Sroot 		done(0);
2241119Sbill 	}
2256250Sroot 	if (strcmp(usefile, "-") == 0) {
2266250Sroot 		mt = dup(0);
2276250Sroot 		nblock = 1;
2286250Sroot 	} else if ((mt = open(usefile, 0)) < 0) {
2296250Sroot 		fprintf(stderr, "tar: cannot open %s\n", usefile);
2306250Sroot 		done(1);
2316250Sroot 	}
2326250Sroot 	if (xflag)
2331119Sbill 		doxtract(argv);
2346250Sroot 	else
2351119Sbill 		dotable();
2361119Sbill 	done(0);
2371119Sbill }
2381119Sbill 
2391119Sbill usage()
2401119Sbill {
2416250Sroot 	fprintf(stderr,
2428737Smckusick "tar: usage  tar -{txruB}[cvfblmh] [tapefile] [blocksize] file1 file2...\n");
2431119Sbill 	done(1);
2441119Sbill }
2451119Sbill 
2461119Sbill dorep(argv)
2476250Sroot 	char *argv[];
2481119Sbill {
2491119Sbill 	register char *cp, *cp2;
250*9601Ssam 	char wdir[MAXPATHLEN], tempdir[MAXPATHLEN], *parent;
2511119Sbill 
2521119Sbill 	if (!cflag) {
2531119Sbill 		getdir();
2541119Sbill 		do {
2551119Sbill 			passtape();
2561119Sbill 			if (term)
2571119Sbill 				done(0);
2581119Sbill 			getdir();
2591119Sbill 		} while (!endtape());
2601119Sbill 		if (tfile != NULL) {
2611119Sbill 			char buf[200];
2621119Sbill 
2636250Sroot 			sprintf(buf,
2646250Sroot "sort +0 -1 +1nr %s -o %s; awk '$1 != prev {print; prev=$1}' %s >%sX; mv %sX %s",
2651119Sbill 				tname, tname, tname, tname, tname, tname);
2661119Sbill 			fflush(tfile);
2671119Sbill 			system(buf);
2681119Sbill 			freopen(tname, "r", tfile);
2691119Sbill 			fstat(fileno(tfile), &stbuf);
2701119Sbill 			high = stbuf.st_size;
2711119Sbill 		}
2721119Sbill 	}
2731119Sbill 
2741119Sbill 	getwdir(wdir);
2751119Sbill 	while (*argv && ! term) {
2761119Sbill 		cp2 = *argv;
2771119Sbill 		if (!strcmp(cp2, "-C") && argv[1]) {
2781119Sbill 			argv++;
2791119Sbill 			if (chdir(*argv) < 0)
2801119Sbill 				perror(*argv);
2811119Sbill 			else
2821119Sbill 				getwdir(wdir);
2831119Sbill 			argv++;
2841119Sbill 			continue;
2851119Sbill 		}
286*9601Ssam 		parent = wdir;
2871119Sbill 		for (cp = *argv; *cp; cp++)
2881119Sbill 			if (*cp == '/')
2891119Sbill 				cp2 = cp;
2901119Sbill 		if (cp2 != *argv) {
2911119Sbill 			*cp2 = '\0';
292*9601Ssam 			if (chdir(*argv) < 0) {
293*9601Ssam 				perror(*argv);
294*9601Ssam 				continue;
295*9601Ssam 			}
296*9601Ssam 			getwdir(tempdir);
297*9601Ssam 			parent = tempdir;
2981119Sbill 			*cp2 = '/';
2991119Sbill 			cp2++;
3001119Sbill 		}
301*9601Ssam 		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 
365*9601Ssam putfile(longname, shortname, parent)
3666250Sroot 	char *longname;
3676250Sroot 	char *shortname;
368*9601Ssam 	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;
377*9601Ssam 	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 	}
384*9601Ssam 	if (!hflag)
385*9601Ssam 		lstat(shortname, &stbuf);
386*9601Ssam 	else if (stat(shortname, &stbuf) < 0) {
387*9601Ssam 		perror(longname);
388*9601Ssam 		close(infile);
389*9601Ssam 		return;
390*9601Ssam 	}
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 		}
418*9601Ssam 		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);
423*9601Ssam 			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);
435*9601Ssam 			putfile(buf, cp, newparent);
4365931Smckusic 			dirp = opendir(".");
4375931Smckusic 			seekdir(dirp, i);
4381119Sbill 		}
4395931Smckusic 		closedir(dirp);
440*9601Ssam 		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 		}
465*9601Ssam 		i = readlink(shortname, dblock.dbuf.linkname, NAMSIZ - 1);
4666250Sroot 		if (i < 0) {
467*9601Ssam 			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) {
7456250Sroot 			register int pid, rp;
7466250Sroot 			int i;
7471119Sbill 
7486250Sroot 			if ((pid = fork()) == 0) {
7496250Sroot 				execl("/bin/mkdir", "mkdir", name, 0);
7506250Sroot 				execl("/usr/bin/mkdir", "mkdir", name, 0);
7516250Sroot 				fprintf(stderr, "tar: cannot find mkdir!\n");
7526250Sroot 				done(0);
7531119Sbill 			}
7546250Sroot 			while ((rp = wait(&i)) >= 0 && rp != pid)
7556250Sroot 				;
7566250Sroot 			chown(name, stbuf.st_uid, stbuf.st_gid);
7576250Sroot 			if (pflag)
7586250Sroot 				chmod(dblock.dbuf.name, stbuf.st_mode & 0777);
7591119Sbill 		}
7606250Sroot 		*cp = '/';
7611119Sbill 	}
7626250Sroot 	return (cp[-1]=='/');
7631119Sbill }
7641119Sbill 
7651119Sbill onintr()
7661119Sbill {
7671119Sbill 	signal(SIGINT, SIG_IGN);
7681119Sbill 	term++;
7691119Sbill }
7701119Sbill 
7711119Sbill onquit()
7721119Sbill {
7731119Sbill 	signal(SIGQUIT, SIG_IGN);
7741119Sbill 	term++;
7751119Sbill }
7761119Sbill 
7771119Sbill onhup()
7781119Sbill {
7791119Sbill 	signal(SIGHUP, SIG_IGN);
7801119Sbill 	term++;
7811119Sbill }
7821119Sbill 
7831119Sbill onterm()
7841119Sbill {
7851119Sbill 	signal(SIGTERM, SIG_IGN);
7861119Sbill 	term++;
7871119Sbill }
7881119Sbill 
7891119Sbill tomodes(sp)
7901119Sbill register struct stat *sp;
7911119Sbill {
7921119Sbill 	register char *cp;
7931119Sbill 
7941119Sbill 	for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++)
7951119Sbill 		*cp = '\0';
7961119Sbill 	sprintf(dblock.dbuf.mode, "%6o ", sp->st_mode & 07777);
7971119Sbill 	sprintf(dblock.dbuf.uid, "%6o ", sp->st_uid);
7981119Sbill 	sprintf(dblock.dbuf.gid, "%6o ", sp->st_gid);
7991119Sbill 	sprintf(dblock.dbuf.size, "%11lo ", sp->st_size);
8001119Sbill 	sprintf(dblock.dbuf.mtime, "%11lo ", sp->st_mtime);
8011119Sbill }
8021119Sbill 
8031119Sbill checksum()
8041119Sbill {
8051119Sbill 	register i;
8061119Sbill 	register char *cp;
8071119Sbill 
8086250Sroot 	for (cp = dblock.dbuf.chksum;
8096250Sroot 	     cp < &dblock.dbuf.chksum[sizeof(dblock.dbuf.chksum)]; cp++)
8101119Sbill 		*cp = ' ';
8111119Sbill 	i = 0;
8121119Sbill 	for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++)
8131119Sbill 		i += *cp;
8146250Sroot 	return (i);
8151119Sbill }
8161119Sbill 
8171119Sbill checkw(c, name)
8186250Sroot 	char *name;
8191119Sbill {
8206250Sroot 	if (!wflag)
8216250Sroot 		return (1);
8226250Sroot 	printf("%c ", c);
8236250Sroot 	if (vflag)
8246250Sroot 		longt(&stbuf);
8256250Sroot 	printf("%s: ", name);
8266250Sroot 	return (response() == 'y');
8271119Sbill }
8281119Sbill 
8291119Sbill response()
8301119Sbill {
8311119Sbill 	char c;
8321119Sbill 
8331119Sbill 	c = getchar();
8341119Sbill 	if (c != '\n')
8356250Sroot 		while (getchar() != '\n')
8366250Sroot 			;
8376250Sroot 	else
8386250Sroot 		c = 'n';
8396250Sroot 	return (c);
8401119Sbill }
8411119Sbill 
8421119Sbill checkupdate(arg)
8436250Sroot 	char *arg;
8441119Sbill {
8451119Sbill 	char name[100];
8466250Sroot 	long mtime;
8471119Sbill 	daddr_t seekp;
8481119Sbill 	daddr_t	lookup();
8491119Sbill 
8501119Sbill 	rewind(tfile);
8511119Sbill 	for (;;) {
8521119Sbill 		if ((seekp = lookup(arg)) < 0)
8536250Sroot 			return (1);
8541119Sbill 		fseek(tfile, seekp, 0);
8551119Sbill 		fscanf(tfile, "%s %lo", name, &mtime);
8566250Sroot 		return (stbuf.st_mtime > mtime);
8571119Sbill 	}
8581119Sbill }
8591119Sbill 
8601119Sbill done(n)
8611119Sbill {
8621119Sbill 	unlink(tname);
8631119Sbill 	exit(n);
8641119Sbill }
8651119Sbill 
8661119Sbill prefix(s1, s2)
8676250Sroot 	register char *s1, *s2;
8681119Sbill {
8691119Sbill 	while (*s1)
8701119Sbill 		if (*s1++ != *s2++)
8716250Sroot 			return (0);
8721119Sbill 	if (*s2)
8736250Sroot 		return (*s2 == '/');
8746250Sroot 	return (1);
8751119Sbill }
8761119Sbill 
8771119Sbill getwdir(s)
8786250Sroot 	char *s;
8791119Sbill {
8806250Sroot 	int i, pipdes[2];
8811119Sbill 
8821119Sbill 	pipe(pipdes);
8831119Sbill 	if ((i = fork()) == 0) {
8841119Sbill 		close(1);
8851119Sbill 		dup(pipdes[1]);
8861119Sbill 		execl("/bin/pwd", "pwd", 0);
8871119Sbill 		execl("/usr/bin/pwd", "pwd", 0);
8881119Sbill 		fprintf(stderr, "pwd failed!\n");
8891119Sbill 		printf("/\n");
8901119Sbill 		exit(1);
8911119Sbill 	}
8921119Sbill 	while (wait((int *)NULL) != -1)
8931119Sbill 			;
8941119Sbill 	read(pipdes[0], s, 50);
895*9601Ssam 	read(pipdes[0], s, 50);
8966250Sroot 	while (*s != '\n')
8971119Sbill 		s++;
8981119Sbill 	*s = '\0';
8991119Sbill 	close(pipdes[0]);
9001119Sbill 	close(pipdes[1]);
9011119Sbill }
9021119Sbill 
9031119Sbill #define	N	200
9041119Sbill int	njab;
9056250Sroot 
9061119Sbill daddr_t
9071119Sbill lookup(s)
9086250Sroot 	char *s;
9091119Sbill {
9101119Sbill 	register i;
9111119Sbill 	daddr_t a;
9121119Sbill 
9131119Sbill 	for(i=0; s[i]; i++)
9146250Sroot 		if (s[i] == ' ')
9151119Sbill 			break;
9161119Sbill 	a = bsrch(s, i, low, high);
9176250Sroot 	return (a);
9181119Sbill }
9191119Sbill 
9201119Sbill daddr_t
9211119Sbill bsrch(s, n, l, h)
9226250Sroot 	daddr_t l, h;
9236250Sroot 	char *s;
9241119Sbill {
9251119Sbill 	register i, j;
9261119Sbill 	char b[N];
9271119Sbill 	daddr_t m, m1;
9281119Sbill 
9291119Sbill 	njab = 0;
9301119Sbill 
9311119Sbill loop:
9326250Sroot 	if (l >= h)
9336250Sroot 		return (-1L);
9341119Sbill 	m = l + (h-l)/2 - N/2;
9356250Sroot 	if (m < l)
9361119Sbill 		m = l;
9371119Sbill 	fseek(tfile, m, 0);
9381119Sbill 	fread(b, 1, N, tfile);
9391119Sbill 	njab++;
9401119Sbill 	for(i=0; i<N; i++) {
9416250Sroot 		if (b[i] == '\n')
9421119Sbill 			break;
9431119Sbill 		m++;
9441119Sbill 	}
9456250Sroot 	if (m >= h)
9466250Sroot 		return (-1L);
9471119Sbill 	m1 = m;
9481119Sbill 	j = i;
9491119Sbill 	for(i++; i<N; i++) {
9501119Sbill 		m1++;
9516250Sroot 		if (b[i] == '\n')
9521119Sbill 			break;
9531119Sbill 	}
9541119Sbill 	i = cmp(b+j, s, n);
9556250Sroot 	if (i < 0) {
9561119Sbill 		h = m;
9571119Sbill 		goto loop;
9581119Sbill 	}
9596250Sroot 	if (i > 0) {
9601119Sbill 		l = m1;
9611119Sbill 		goto loop;
9621119Sbill 	}
9636250Sroot 	return (m);
9641119Sbill }
9651119Sbill 
9661119Sbill cmp(b, s, n)
9676250Sroot 	char *b, *s;
9681119Sbill {
9691119Sbill 	register i;
9701119Sbill 
9716250Sroot 	if (b[0] != '\n')
9721119Sbill 		exit(2);
9731119Sbill 	for(i=0; i<n; i++) {
9746250Sroot 		if (b[i+1] > s[i])
9756250Sroot 			return (-1);
9766250Sroot 		if (b[i+1] < s[i])
9776250Sroot 			return (1);
9781119Sbill 	}
9796250Sroot 	return (b[i+1] == ' '? 0 : -1);
9801119Sbill }
9811119Sbill 
9821119Sbill readtape(buffer)
9836250Sroot 	char *buffer;
9841119Sbill {
9853457Swnj 	register int i;
9861119Sbill 
9871119Sbill 	if (recno >= nblock || first == 0) {
9888737Smckusick 		if ((i = bread(mt, tbuf, TBLOCK*nblock)) < 0) {
9891119Sbill 			fprintf(stderr, "Tar: tape read error\n");
9901119Sbill 			done(3);
9911119Sbill 		}
9921119Sbill 		if (first == 0) {
9931119Sbill 			if ((i % TBLOCK) != 0) {
9941119Sbill 				fprintf(stderr, "Tar: tape blocksize error\n");
9951119Sbill 				done(3);
9961119Sbill 			}
9971119Sbill 			i /= TBLOCK;
9983457Swnj 			if (i != nblock) {
9991119Sbill 				fprintf(stderr, "Tar: blocksize = %d\n", i);
10001119Sbill 				nblock = i;
10011119Sbill 			}
10021119Sbill 		}
10031119Sbill 		recno = 0;
10041119Sbill 	}
10051119Sbill 	first = 1;
10061119Sbill 	copy(buffer, &tbuf[recno++]);
10076250Sroot 	return (TBLOCK);
10081119Sbill }
10091119Sbill 
10101119Sbill writetape(buffer)
10116250Sroot 	char *buffer;
10121119Sbill {
10131119Sbill 	first = 1;
10141119Sbill 	if (recno >= nblock) {
10151119Sbill 		if (write(mt, tbuf, TBLOCK*nblock) < 0) {
10161119Sbill 			fprintf(stderr, "Tar: tape write error\n");
10171119Sbill 			done(2);
10181119Sbill 		}
10191119Sbill 		recno = 0;
10201119Sbill 	}
10211119Sbill 	copy(&tbuf[recno++], buffer);
10221119Sbill 	if (recno >= nblock) {
10231119Sbill 		if (write(mt, tbuf, TBLOCK*nblock) < 0) {
10241119Sbill 			fprintf(stderr, "Tar: tape write error\n");
10251119Sbill 			done(2);
10261119Sbill 		}
10271119Sbill 		recno = 0;
10281119Sbill 	}
10296250Sroot 	return (TBLOCK);
10301119Sbill }
10311119Sbill 
10321119Sbill backtape()
10331119Sbill {
10343457Swnj 	static int mtdev = 1;
10353457Swnj 	static struct mtop mtop = {MTBSR, 1};
10363457Swnj 	struct mtget mtget;
10373457Swnj 
10383457Swnj 	if (mtdev == 1)
10393457Swnj 		mtdev = ioctl(mt, MTIOCGET, &mtget);
10403457Swnj 	if (mtdev == 0) {
10413457Swnj 		if (ioctl(mt, MTIOCTOP, &mtop) < 0) {
10423457Swnj 			fprintf(stderr, "Tar: tape backspace error\n");
10431119Sbill 			done(4);
10441119Sbill 		}
10453457Swnj 	} else
10463457Swnj 		lseek(mt, (long) -TBLOCK*nblock, 1);
10473457Swnj 	recno--;
10481119Sbill }
10491119Sbill 
10501119Sbill flushtape()
10511119Sbill {
10521119Sbill 	write(mt, tbuf, TBLOCK*nblock);
10531119Sbill }
10541119Sbill 
10551119Sbill copy(to, from)
10566250Sroot 	register char *to, *from;
10571119Sbill {
10581119Sbill 	register i;
10591119Sbill 
10601119Sbill 	i = TBLOCK;
10611119Sbill 	do {
10621119Sbill 		*to++ = *from++;
10631119Sbill 	} while (--i);
10641119Sbill }
10658737Smckusick 
10668737Smckusick bread(fd, buf, size)
10678737Smckusick 	int fd;
10688737Smckusick 	char *buf;
10698737Smckusick 	int size;
10708737Smckusick {
10718737Smckusick 	int count;
10728737Smckusick 	static int lastread = 0;
10738737Smckusick 
10748737Smckusick 	if (!Bflag)
10758737Smckusick 		return (read(fd, buf, size));
10768737Smckusick 	for (count = 0; count < size; count += lastread) {
10778737Smckusick 		if (lastread < 0) {
10788737Smckusick 			if (count > 0)
10798737Smckusick 				return (count);
10808737Smckusick 			return (lastread);
10818737Smckusick 		}
10828737Smckusick 		lastread = read(fd, buf, size - count);
10838737Smckusick 		buf += lastread;
10848737Smckusick 	}
10858737Smckusick 	return (count);
10868737Smckusick }
1087