xref: /csrg-svn/old/tar/tar.c (revision 24281)
122502Sdist /*
222502Sdist  * Copyright (c) 1980 Regents of the University of California.
322502Sdist  * All rights reserved.  The Berkeley software License Agreement
422502Sdist  * specifies the terms and conditions for redistribution.
522502Sdist  */
622502Sdist 
712154Ssam #ifndef lint
822502Sdist char copyright[] =
922502Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
1022502Sdist  All rights reserved.\n";
1122502Sdist #endif not lint
126250Sroot 
1322502Sdist #ifndef lint
14*24281Smckusick static char sccsid[] = "@(#)tar.c	5.3 (Berkeley) 08/13/85";
1522502Sdist #endif not lint
1622502Sdist 
176250Sroot /*
186250Sroot  * Tape Archival Program
196250Sroot  */
201119Sbill #include <stdio.h>
216413Smckusic #include <sys/param.h>
221119Sbill #include <sys/stat.h>
2312154Ssam #include <sys/dir.h>
248150Smckusick #include <sys/ioctl.h>
253457Swnj #include <sys/mtio.h>
2612983Ssam #include <sys/time.h>
271119Sbill #include <signal.h>
2812154Ssam #include <errno.h>
291119Sbill 
301119Sbill #define TBLOCK	512
313355Swnj #define NBLOCK	20
321119Sbill #define NAMSIZ	100
3322353Skjd #define FILEBLOCK 20
346250Sroot 
3521457Skjd #define	writetape(b)	writetbuf(b, 1)
3621457Skjd #define	min(a,b)  ((a) < (b) ? (a) : (b))
3721457Skjd #define	max(a,b)  ((a) > (b) ? (a) : (b))
3821457Skjd 
391119Sbill union hblock {
401119Sbill 	char dummy[TBLOCK];
411119Sbill 	struct header {
421119Sbill 		char name[NAMSIZ];
431119Sbill 		char mode[8];
441119Sbill 		char uid[8];
451119Sbill 		char gid[8];
461119Sbill 		char size[12];
471119Sbill 		char mtime[12];
481119Sbill 		char chksum[8];
491119Sbill 		char linkflag;
501119Sbill 		char linkname[NAMSIZ];
511119Sbill 	} dbuf;
526250Sroot };
531119Sbill 
541119Sbill struct linkbuf {
551119Sbill 	ino_t	inum;
561119Sbill 	dev_t	devnum;
571119Sbill 	int	count;
581119Sbill 	char	pathname[NAMSIZ];
591119Sbill 	struct	linkbuf *nextp;
606250Sroot };
611119Sbill 
626250Sroot union	hblock dblock;
6313492Ssam union	hblock *tbuf;
646250Sroot struct	linkbuf *ihead;
656250Sroot struct	stat stbuf;
661119Sbill 
676250Sroot int	rflag;
686250Sroot int	xflag;
696250Sroot int	vflag;
706250Sroot int	tflag;
716250Sroot int	cflag;
726250Sroot int	mflag;
736250Sroot int	fflag;
7412154Ssam int	iflag;
756250Sroot int	oflag;
766250Sroot int	pflag;
776250Sroot int	wflag;
786250Sroot int	hflag;
798737Smckusick int	Bflag;
8012154Ssam int	Fflag;
816250Sroot 
826250Sroot int	mt;
8322353Skjd int	mtdev = 1;
846250Sroot int	term;
856250Sroot int	chksum;
866250Sroot int	recno;
8722688Slepreau int	first;
8822688Slepreau int	prtlinkerr;
891119Sbill int	freemem = 1;
9022353Skjd int	nblock = 0;
916250Sroot int	onintr();
926250Sroot int	onquit();
936250Sroot int	onhup();
9422688Slepreau #ifdef notdef
956250Sroot int	onterm();
9622688Slepreau #endif
971119Sbill 
981119Sbill daddr_t	low;
991119Sbill daddr_t	high;
1006250Sroot daddr_t	bsrch();
1011119Sbill 
10221910Skjd FILE	*vfile = stdout;
1031119Sbill FILE	*tfile;
1041119Sbill char	tname[] = "/tmp/tarXXXXXX";
1051119Sbill char	*usefile;
1066250Sroot char	magtape[] = "/dev/rmt8";
1071119Sbill char	*malloc();
1086250Sroot char	*sprintf();
1096250Sroot char	*strcat();
11012154Ssam char	*rindex();
11110165Ssam char	*getcwd();
1129844Ssam char	*getwd();
11322688Slepreau char	*getmem();
1141119Sbill 
1151119Sbill main(argc, argv)
1161119Sbill int	argc;
1171119Sbill char	*argv[];
1181119Sbill {
1191119Sbill 	char *cp;
1201119Sbill 
1211119Sbill 	if (argc < 2)
1221119Sbill 		usage();
1231119Sbill 
1241119Sbill 	tfile = NULL;
1251119Sbill 	usefile =  magtape;
1261119Sbill 	argv[argc] = 0;
1271119Sbill 	argv++;
1281119Sbill 	for (cp = *argv++; *cp; cp++)
1291119Sbill 		switch(*cp) {
1306250Sroot 
1311119Sbill 		case 'f':
13212154Ssam 			if (*argv == 0) {
13312154Ssam 				fprintf(stderr,
13412154Ssam 			"tar: tapefile must be specified with 'f' option\n");
13512154Ssam 				usage();
13612154Ssam 			}
1371119Sbill 			usefile = *argv++;
1381119Sbill 			fflag++;
1391119Sbill 			break;
1406250Sroot 
1411119Sbill 		case 'c':
1421119Sbill 			cflag++;
1431119Sbill 			rflag++;
1441119Sbill 			break;
1456250Sroot 
1461119Sbill 		case 'o':
1471119Sbill 			oflag++;
1481119Sbill 			break;
1496250Sroot 
1501119Sbill 		case 'p':
1511119Sbill 			pflag++;
1521119Sbill 			break;
1536250Sroot 
1541119Sbill 		case 'u':
1551119Sbill 			mktemp(tname);
1561119Sbill 			if ((tfile = fopen(tname, "w")) == NULL) {
1576250Sroot 				fprintf(stderr,
15822688Slepreau 				 "tar: cannot create temporary file (%s)\n",
1596250Sroot 				 tname);
1601119Sbill 				done(1);
1611119Sbill 			}
1621119Sbill 			fprintf(tfile, "!!!!!/!/!/!/!/!/!/! 000\n");
1636250Sroot 			/*FALL THRU*/
1646250Sroot 
1651119Sbill 		case 'r':
1661119Sbill 			rflag++;
1671119Sbill 			break;
1686250Sroot 
1691119Sbill 		case 'v':
1701119Sbill 			vflag++;
1711119Sbill 			break;
1726250Sroot 
1731119Sbill 		case 'w':
1741119Sbill 			wflag++;
1751119Sbill 			break;
1766250Sroot 
1771119Sbill 		case 'x':
1781119Sbill 			xflag++;
1791119Sbill 			break;
1806250Sroot 
1811119Sbill 		case 't':
1821119Sbill 			tflag++;
1831119Sbill 			break;
1846250Sroot 
1851119Sbill 		case 'm':
1861119Sbill 			mflag++;
1871119Sbill 			break;
1886250Sroot 
1891119Sbill 		case '-':
1901119Sbill 			break;
1916250Sroot 
1921119Sbill 		case '0':
1931119Sbill 		case '1':
1941119Sbill 		case '4':
1951119Sbill 		case '5':
19621457Skjd 		case '7':
1971119Sbill 		case '8':
1981119Sbill 			magtape[8] = *cp;
1991119Sbill 			usefile = magtape;
2001119Sbill 			break;
2016250Sroot 
2021119Sbill 		case 'b':
20313492Ssam 			if (*argv == 0) {
20413492Ssam 				fprintf(stderr,
20513492Ssam 			"tar: blocksize must be specified with 'b' option\n");
20613492Ssam 				usage();
20713492Ssam 			}
20813492Ssam 			nblock = atoi(*argv);
20913492Ssam 			if (nblock <= 0) {
21013492Ssam 				fprintf(stderr,
21113492Ssam 				    "tar: invalid blocksize \"%s\"\n", *argv);
2121119Sbill 				done(1);
2131119Sbill 			}
21413492Ssam 			argv++;
2151119Sbill 			break;
2166250Sroot 
2171119Sbill 		case 'l':
21822688Slepreau 			prtlinkerr++;
2191119Sbill 			break;
2206250Sroot 
2216250Sroot 		case 'h':
2226250Sroot 			hflag++;
2236250Sroot 			break;
2246250Sroot 
22512154Ssam 		case 'i':
22612154Ssam 			iflag++;
22712154Ssam 			break;
22812154Ssam 
2298737Smckusick 		case 'B':
2308737Smckusick 			Bflag++;
2318737Smckusick 			break;
2328737Smckusick 
23312154Ssam 		case 'F':
23412154Ssam 			Fflag++;
23512154Ssam 			break;
23612154Ssam 
2371119Sbill 		default:
2381119Sbill 			fprintf(stderr, "tar: %c: unknown option\n", *cp);
2391119Sbill 			usage();
2401119Sbill 		}
2411119Sbill 
2426250Sroot 	if (!rflag && !xflag && !tflag)
2436250Sroot 		usage();
2441119Sbill 	if (rflag) {
2456250Sroot 		if (cflag && tfile != NULL)
2461119Sbill 			usage();
2471119Sbill 		if (signal(SIGINT, SIG_IGN) != SIG_IGN)
2481119Sbill 			signal(SIGINT, onintr);
2491119Sbill 		if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
2501119Sbill 			signal(SIGHUP, onhup);
2511119Sbill 		if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
2521119Sbill 			signal(SIGQUIT, onquit);
2536250Sroot #ifdef notdef
2541119Sbill 		if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
2551119Sbill 			signal(SIGTERM, onterm);
2566250Sroot #endif
2571119Sbill 		if (strcmp(usefile, "-") == 0) {
2581119Sbill 			if (cflag == 0) {
2596250Sroot 				fprintf(stderr,
26013492Ssam 			 "tar: can only create standard output archives\n");
2611119Sbill 				done(1);
2621119Sbill 			}
26321910Skjd 			vfile = stderr;
26421910Skjd 			setlinebuf(vfile);
2651119Sbill 			mt = dup(1);
2666250Sroot 		} else if ((mt = open(usefile, 2)) < 0) {
2671119Sbill 			if (cflag == 0 || (mt =  creat(usefile, 0666)) < 0) {
2686250Sroot 				fprintf(stderr,
2696250Sroot 					"tar: cannot open %s\n", usefile);
2701119Sbill 				done(1);
2711119Sbill 			}
2721119Sbill 		}
2731119Sbill 		dorep(argv);
2746250Sroot 		done(0);
2751119Sbill 	}
2766250Sroot 	if (strcmp(usefile, "-") == 0) {
2776250Sroot 		mt = dup(0);
278*24281Smckusick 		Bflag++;
2796250Sroot 	} else if ((mt = open(usefile, 0)) < 0) {
2806250Sroot 		fprintf(stderr, "tar: cannot open %s\n", usefile);
2816250Sroot 		done(1);
2826250Sroot 	}
2836250Sroot 	if (xflag)
2841119Sbill 		doxtract(argv);
2856250Sroot 	else
2861119Sbill 		dotable();
2871119Sbill 	done(0);
2881119Sbill }
2891119Sbill 
2901119Sbill usage()
2911119Sbill {
2926250Sroot 	fprintf(stderr,
29321457Skjd "tar: usage: tar -{txru}[cvfblmhopwBi] [tapefile] [blocksize] file1 file2...\n");
2941119Sbill 	done(1);
2951119Sbill }
2961119Sbill 
2971119Sbill dorep(argv)
2986250Sroot 	char *argv[];
2991119Sbill {
3001119Sbill 	register char *cp, *cp2;
3019601Ssam 	char wdir[MAXPATHLEN], tempdir[MAXPATHLEN], *parent;
3021119Sbill 
3031119Sbill 	if (!cflag) {
3041119Sbill 		getdir();
3051119Sbill 		do {
3061119Sbill 			passtape();
3071119Sbill 			if (term)
3081119Sbill 				done(0);
3091119Sbill 			getdir();
3101119Sbill 		} while (!endtape());
31113492Ssam 		backtape();
3121119Sbill 		if (tfile != NULL) {
3131119Sbill 			char buf[200];
3141119Sbill 
3156250Sroot 			sprintf(buf,
3166250Sroot "sort +0 -1 +1nr %s -o %s; awk '$1 != prev {print; prev=$1}' %s >%sX; mv %sX %s",
3171119Sbill 				tname, tname, tname, tname, tname, tname);
3181119Sbill 			fflush(tfile);
3191119Sbill 			system(buf);
3201119Sbill 			freopen(tname, "r", tfile);
3211119Sbill 			fstat(fileno(tfile), &stbuf);
3221119Sbill 			high = stbuf.st_size;
3231119Sbill 		}
3241119Sbill 	}
3251119Sbill 
32610165Ssam 	(void) getcwd(wdir);
3271119Sbill 	while (*argv && ! term) {
3281119Sbill 		cp2 = *argv;
3291119Sbill 		if (!strcmp(cp2, "-C") && argv[1]) {
3301119Sbill 			argv++;
3311119Sbill 			if (chdir(*argv) < 0)
3321119Sbill 				perror(*argv);
3331119Sbill 			else
33410165Ssam 				(void) getcwd(wdir);
3351119Sbill 			argv++;
3361119Sbill 			continue;
3371119Sbill 		}
3389601Ssam 		parent = wdir;
3391119Sbill 		for (cp = *argv; *cp; cp++)
3401119Sbill 			if (*cp == '/')
3411119Sbill 				cp2 = cp;
3421119Sbill 		if (cp2 != *argv) {
3431119Sbill 			*cp2 = '\0';
3449601Ssam 			if (chdir(*argv) < 0) {
3459601Ssam 				perror(*argv);
3469601Ssam 				continue;
3479601Ssam 			}
34810165Ssam 			parent = getcwd(tempdir);
3491119Sbill 			*cp2 = '/';
3501119Sbill 			cp2++;
3511119Sbill 		}
3529601Ssam 		putfile(*argv++, cp2, parent);
35315045Smckusick 		if (chdir(wdir) < 0) {
35422688Slepreau 			fprintf(stderr, "tar: cannot change back?: ");
35515045Smckusick 			perror(wdir);
35615045Smckusick 		}
3571119Sbill 	}
3581119Sbill 	putempty();
3591119Sbill 	putempty();
3601119Sbill 	flushtape();
36122688Slepreau 	if (prtlinkerr == 0)
3626250Sroot 		return;
3636250Sroot 	for (; ihead != NULL; ihead = ihead->nextp) {
3646250Sroot 		if (ihead->count == 0)
3656250Sroot 			continue;
36613492Ssam 		fprintf(stderr, "tar: missing links to %s\n", ihead->pathname);
3676250Sroot 	}
3681119Sbill }
3691119Sbill 
3701119Sbill endtape()
3711119Sbill {
37221457Skjd 	return (dblock.dbuf.name[0] == '\0');
3731119Sbill }
3741119Sbill 
3751119Sbill getdir()
3761119Sbill {
3771119Sbill 	register struct stat *sp;
3781119Sbill 	int i;
3791119Sbill 
38012154Ssam top:
3816250Sroot 	readtape((char *)&dblock);
3821119Sbill 	if (dblock.dbuf.name[0] == '\0')
3831119Sbill 		return;
3841119Sbill 	sp = &stbuf;
3851119Sbill 	sscanf(dblock.dbuf.mode, "%o", &i);
3861119Sbill 	sp->st_mode = i;
3871119Sbill 	sscanf(dblock.dbuf.uid, "%o", &i);
3881119Sbill 	sp->st_uid = i;
3891119Sbill 	sscanf(dblock.dbuf.gid, "%o", &i);
3901119Sbill 	sp->st_gid = i;
3911119Sbill 	sscanf(dblock.dbuf.size, "%lo", &sp->st_size);
3921119Sbill 	sscanf(dblock.dbuf.mtime, "%lo", &sp->st_mtime);
3931119Sbill 	sscanf(dblock.dbuf.chksum, "%o", &chksum);
39412154Ssam 	if (chksum != (i = checksum())) {
39513492Ssam 		fprintf(stderr, "tar: directory checksum error (%d != %d)\n",
39612154Ssam 		    chksum, i);
39712154Ssam 		if (iflag)
39812154Ssam 			goto top;
3991119Sbill 		done(2);
4001119Sbill 	}
4011119Sbill 	if (tfile != NULL)
4021119Sbill 		fprintf(tfile, "%s %s\n", dblock.dbuf.name, dblock.dbuf.mtime);
4031119Sbill }
4041119Sbill 
4051119Sbill passtape()
4061119Sbill {
4071119Sbill 	long blocks;
40821457Skjd 	char *bufp;
4091119Sbill 
4101119Sbill 	if (dblock.dbuf.linkflag == '1')
4111119Sbill 		return;
4121119Sbill 	blocks = stbuf.st_size;
4131119Sbill 	blocks += TBLOCK-1;
4141119Sbill 	blocks /= TBLOCK;
4151119Sbill 
41621457Skjd 	while (blocks-- > 0)
41721457Skjd 		readtbuf(&bufp, TBLOCK);
4181119Sbill }
4191119Sbill 
4209601Ssam putfile(longname, shortname, parent)
4216250Sroot 	char *longname;
4226250Sroot 	char *shortname;
4239601Ssam 	char *parent;
4241119Sbill {
42521457Skjd 	int infile = 0;
42621457Skjd 	long blocks;
4271119Sbill 	char buf[TBLOCK];
42821457Skjd 	char *bigbuf;
42922688Slepreau 	register char *cp;
4305931Smckusic 	struct direct *dp;
4315931Smckusic 	DIR *dirp;
43222688Slepreau 	int i;
4339601Ssam 	char newparent[NAMSIZ+64];
43412154Ssam 	extern int errno;
43521457Skjd 	int	maxread;
43621457Skjd 	int	hint;		/* amount to write to get "in sync" */
4371119Sbill 
4389601Ssam 	if (!hflag)
43912154Ssam 		i = lstat(shortname, &stbuf);
44012154Ssam 	else
44112154Ssam 		i = stat(shortname, &stbuf);
44212154Ssam 	if (i < 0) {
44312154Ssam 		switch (errno) {
44412154Ssam 		case EACCES:
44512154Ssam 			fprintf(stderr, "tar: %s: cannot open file\n", longname);
44612154Ssam 			break;
44712154Ssam 		case ENOENT:
44812154Ssam 			fprintf(stderr, "tar: %s: no such file or directory\n",
44912154Ssam 			    longname);
45012154Ssam 			break;
45112154Ssam 		default:
45212154Ssam 			fprintf(stderr, "tar: %s: cannot stat file\n", longname);
45312154Ssam 			break;
45412154Ssam 		}
4559601Ssam 		return;
4569601Ssam 	}
45712154Ssam 	if (tfile != NULL && checkupdate(longname) == 0)
4581119Sbill 		return;
45912154Ssam 	if (checkw('r', longname) == 0)
4601119Sbill 		return;
46112154Ssam 	if (Fflag && checkf(shortname, stbuf.st_mode, Fflag) == 0)
46212154Ssam 		return;
4631119Sbill 
46412154Ssam 	switch (stbuf.st_mode & S_IFMT) {
46512154Ssam 	case S_IFDIR:
4666250Sroot 		for (i = 0, cp = buf; *cp++ = longname[i++];)
4676250Sroot 			;
4681119Sbill 		*--cp = '/';
4691119Sbill 		*++cp = 0  ;
4701119Sbill 		if (!oflag) {
4716250Sroot 			if ((cp - buf) >= NAMSIZ) {
47213492Ssam 				fprintf(stderr, "tar: %s: file name too long\n",
47313492Ssam 				    longname);
4746250Sroot 				return;
4756250Sroot 			}
4766250Sroot 			stbuf.st_size = 0;
4776250Sroot 			tomodes(&stbuf);
4786250Sroot 			strcpy(dblock.dbuf.name,buf);
4796250Sroot 			sprintf(dblock.dbuf.chksum, "%6o", checksum());
4806250Sroot 			writetape((char *)&dblock);
4811119Sbill 		}
4829601Ssam 		sprintf(newparent, "%s/%s", parent, shortname);
48315045Smckusick 		if (chdir(shortname) < 0) {
48415045Smckusick 			perror(shortname);
48515045Smckusick 			return;
48615045Smckusick 		}
4875931Smckusic 		if ((dirp = opendir(".")) == NULL) {
48813492Ssam 			fprintf(stderr, "tar: %s: directory read error\n",
48913492Ssam 			    longname);
49015045Smckusick 			if (chdir(parent) < 0) {
49122688Slepreau 				fprintf(stderr, "tar: cannot change back?: ");
49215045Smckusick 				perror(parent);
49315045Smckusick 			}
4945931Smckusic 			return;
4955931Smckusic 		}
4965931Smckusic 		while ((dp = readdir(dirp)) != NULL && !term) {
4975931Smckusic 			if (dp->d_ino == 0)
4981119Sbill 				continue;
4996250Sroot 			if (!strcmp(".", dp->d_name) ||
5006250Sroot 			    !strcmp("..", dp->d_name))
5011119Sbill 				continue;
5025931Smckusic 			strcpy(cp, dp->d_name);
5035931Smckusic 			i = telldir(dirp);
5045931Smckusic 			closedir(dirp);
5059601Ssam 			putfile(buf, cp, newparent);
5065931Smckusic 			dirp = opendir(".");
5075931Smckusic 			seekdir(dirp, i);
5081119Sbill 		}
5095931Smckusic 		closedir(dirp);
51015045Smckusick 		if (chdir(parent) < 0) {
51122688Slepreau 			fprintf(stderr, "tar: cannot change back?: ");
51215045Smckusick 			perror(parent);
51315045Smckusick 		}
51412154Ssam 		break;
51512154Ssam 
51612154Ssam 	case S_IFLNK:
51712154Ssam 		tomodes(&stbuf);
51812154Ssam 		if (strlen(longname) >= NAMSIZ) {
51913492Ssam 			fprintf(stderr, "tar: %s: file name too long\n",
52013492Ssam 			    longname);
52112154Ssam 			return;
52212154Ssam 		}
52312154Ssam 		strcpy(dblock.dbuf.name, longname);
5246250Sroot 		if (stbuf.st_size + 1 >= NAMSIZ) {
52513492Ssam 			fprintf(stderr, "tar: %s: symbolic link too long\n",
52613492Ssam 			    longname);
5276250Sroot 			return;
5286250Sroot 		}
5299601Ssam 		i = readlink(shortname, dblock.dbuf.linkname, NAMSIZ - 1);
5306250Sroot 		if (i < 0) {
5319601Ssam 			perror(longname);
5326250Sroot 			return;
5336250Sroot 		}
5346250Sroot 		dblock.dbuf.linkname[i] = '\0';
5356250Sroot 		dblock.dbuf.linkflag = '2';
53622688Slepreau 		if (vflag)
53722688Slepreau 			fprintf(vfile, "a %s symbolic link to %s\n",
53822688Slepreau 			    longname, dblock.dbuf.linkname);
5396250Sroot 		sprintf(dblock.dbuf.size, "%11lo", 0);
5406250Sroot 		sprintf(dblock.dbuf.chksum, "%6o", checksum());
5416250Sroot 		writetape((char *)&dblock);
54212154Ssam 		break;
5431119Sbill 
54412154Ssam 	case S_IFREG:
54512154Ssam 		if ((infile = open(shortname, 0)) < 0) {
54612154Ssam 			fprintf(stderr, "tar: %s: cannot open file\n", longname);
5471119Sbill 			return;
5481119Sbill 		}
54912154Ssam 		tomodes(&stbuf);
55012154Ssam 		if (strlen(longname) >= NAMSIZ) {
55113492Ssam 			fprintf(stderr, "tar: %s: file name too long\n",
55213492Ssam 			    longname);
55321457Skjd 			close(infile);
55412154Ssam 			return;
55512154Ssam 		}
55612154Ssam 		strcpy(dblock.dbuf.name, longname);
55712154Ssam 		if (stbuf.st_nlink > 1) {
55812154Ssam 			struct linkbuf *lp;
55912154Ssam 			int found = 0;
56012154Ssam 
56112154Ssam 			for (lp = ihead; lp != NULL; lp = lp->nextp)
56212154Ssam 				if (lp->inum == stbuf.st_ino &&
56312154Ssam 				    lp->devnum == stbuf.st_dev) {
56412154Ssam 					found++;
56512154Ssam 					break;
56612154Ssam 				}
56712154Ssam 			if (found) {
56812154Ssam 				strcpy(dblock.dbuf.linkname, lp->pathname);
56912154Ssam 				dblock.dbuf.linkflag = '1';
57012154Ssam 				sprintf(dblock.dbuf.chksum, "%6o", checksum());
57121457Skjd 				writetape( (char *) &dblock);
57222688Slepreau 				if (vflag)
57322688Slepreau 					fprintf(vfile, "a %s link to %s\n",
57422688Slepreau 					    longname, lp->pathname);
57512154Ssam 				lp->count--;
57612154Ssam 				close(infile);
57712154Ssam 				return;
5781119Sbill 			}
57922688Slepreau 			lp = (struct linkbuf *) getmem(sizeof(*lp));
58022688Slepreau 			if (lp != NULL) {
58112154Ssam 				lp->nextp = ihead;
58212154Ssam 				ihead = lp;
58312154Ssam 				lp->inum = stbuf.st_ino;
58412154Ssam 				lp->devnum = stbuf.st_dev;
58512154Ssam 				lp->count = stbuf.st_nlink - 1;
58612154Ssam 				strcpy(lp->pathname, longname);
58712154Ssam 			}
5881119Sbill 		}
58921457Skjd 		blocks = (stbuf.st_size + (TBLOCK-1)) / TBLOCK;
59022688Slepreau 		if (vflag)
59122688Slepreau 			fprintf(vfile, "a %s %ld blocks\n", longname, blocks);
59212154Ssam 		sprintf(dblock.dbuf.chksum, "%6o", checksum());
59321457Skjd 		hint = writetape((char *)&dblock);
59421457Skjd 		maxread = max(stbuf.st_blksize, (nblock * TBLOCK));
59521457Skjd 		if ((bigbuf = malloc(maxread)) == 0) {
59621457Skjd 			maxread = TBLOCK;
59721457Skjd 			bigbuf = buf;
59821457Skjd 		}
5991119Sbill 
60021457Skjd 		while ((i = read(infile, bigbuf, min((hint*TBLOCK), maxread))) > 0
60121457Skjd 		  && blocks > 0) {
60221457Skjd 		  	register int nblks;
60321457Skjd 
60421457Skjd 			nblks = ((i-1)/TBLOCK)+1;
60521457Skjd 		  	if (nblks > blocks)
60621457Skjd 		  		nblks = blocks;
60721457Skjd 			hint = writetbuf(bigbuf, nblks);
60821457Skjd 			blocks -= nblks;
60921457Skjd 		}
61021457Skjd 		close(infile);
61121457Skjd 		if (bigbuf != buf)
61221457Skjd 			free(bigbuf);
61321457Skjd 		if (blocks != 0 || i != 0)
61413492Ssam 			fprintf(stderr, "tar: %s: file changed size\n",
61513492Ssam 			    longname);
61612154Ssam 		while (--blocks >=  0)
61712154Ssam 			putempty();
61812154Ssam 		break;
61912154Ssam 
62012154Ssam 	default:
62112154Ssam 		fprintf(stderr, "tar: %s is not a file. Not dumped\n",
62213492Ssam 		    longname);
62312154Ssam 		break;
6241119Sbill 	}
6251119Sbill }
6261119Sbill 
6271119Sbill doxtract(argv)
6286250Sroot 	char *argv[];
6291119Sbill {
6301119Sbill 	long blocks, bytes;
6311119Sbill 	char **cp;
6321119Sbill 	int ofile;
6331119Sbill 
6341119Sbill 	for (;;) {
6351119Sbill 		getdir();
6361119Sbill 		if (endtape())
6371119Sbill 			break;
6381119Sbill 		if (*argv == 0)
6391119Sbill 			goto gotit;
6401119Sbill 		for (cp = argv; *cp; cp++)
6411119Sbill 			if (prefix(*cp, dblock.dbuf.name))
6421119Sbill 				goto gotit;
6431119Sbill 		passtape();
6441119Sbill 		continue;
6451119Sbill 
6461119Sbill gotit:
6471119Sbill 		if (checkw('x', dblock.dbuf.name) == 0) {
6481119Sbill 			passtape();
6491119Sbill 			continue;
6501119Sbill 		}
65112154Ssam 		if (Fflag) {
65212154Ssam 			char *s;
65312154Ssam 
65412154Ssam 			if ((s = rindex(dblock.dbuf.name, '/')) == 0)
65512154Ssam 				s = dblock.dbuf.name;
65612154Ssam 			else
65712154Ssam 				s++;
65812154Ssam 			if (checkf(s, stbuf.st_mode, Fflag) == 0) {
65912154Ssam 				passtape();
66012154Ssam 				continue;
66112154Ssam 			}
66212154Ssam 		}
66322688Slepreau 		if (checkdir(dblock.dbuf.name)) {	/* have a directory */
66422688Slepreau 			if (mflag == 0)
66522688Slepreau 				dodirtimes(&dblock);
6666250Sroot 			continue;
66722688Slepreau 		}
66822688Slepreau 		if (dblock.dbuf.linkflag == '2') {	/* symlink */
6696250Sroot 			unlink(dblock.dbuf.name);
6706250Sroot 			if (symlink(dblock.dbuf.linkname, dblock.dbuf.name)<0) {
67113492Ssam 				fprintf(stderr, "tar: %s: symbolic link failed\n",
67213492Ssam 				    dblock.dbuf.name);
6736250Sroot 				continue;
6746250Sroot 			}
6756250Sroot 			if (vflag)
67621910Skjd 				fprintf(vfile, "x %s symbolic link to %s\n",
67713492Ssam 				    dblock.dbuf.name, dblock.dbuf.linkname);
67822353Skjd #ifdef notdef
67922353Skjd 			/* ignore alien orders */
68022353Skjd 			chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid);
68122688Slepreau 			if (mflag == 0)
68222688Slepreau 				setimes(dblock.dbuf.name, stbuf.st_mtime);
68322353Skjd 			if (pflag)
68422353Skjd 				chmod(dblock.dbuf.name, stbuf.st_mode & 07777);
68522353Skjd #endif
6861119Sbill 			continue;
6876250Sroot 		}
68822688Slepreau 		if (dblock.dbuf.linkflag == '1') {	/* regular link */
6891119Sbill 			unlink(dblock.dbuf.name);
6901119Sbill 			if (link(dblock.dbuf.linkname, dblock.dbuf.name) < 0) {
69113492Ssam 				fprintf(stderr, "tar: %s: cannot link\n",
69213492Ssam 				    dblock.dbuf.name);
6931119Sbill 				continue;
6941119Sbill 			}
6951119Sbill 			if (vflag)
69622688Slepreau 				fprintf(vfile, "%s linked to %s\n",
69713492Ssam 				    dblock.dbuf.name, dblock.dbuf.linkname);
6981119Sbill 			continue;
6991119Sbill 		}
7006250Sroot 		if ((ofile = creat(dblock.dbuf.name,stbuf.st_mode&0xfff)) < 0) {
7016250Sroot 			fprintf(stderr, "tar: %s - cannot create\n",
70213492Ssam 			    dblock.dbuf.name);
7031119Sbill 			passtape();
7041119Sbill 			continue;
7051119Sbill 		}
70621457Skjd 		chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid);
7071119Sbill 		blocks = ((bytes = stbuf.st_size) + TBLOCK-1)/TBLOCK;
7081119Sbill 		if (vflag)
70922688Slepreau 			fprintf(vfile, "x %s, %ld bytes, %ld tape blocks\n",
71013492Ssam 			    dblock.dbuf.name, bytes, blocks);
71121457Skjd 		for (; blocks > 0;) {
71221457Skjd 			register int nread;
71321457Skjd 			char	*bufp;
71421457Skjd 			register int nwant;
71521457Skjd 
71621457Skjd 			nwant = NBLOCK*TBLOCK;
71721457Skjd 			if (nwant > (blocks*TBLOCK))
71821457Skjd 				nwant = (blocks*TBLOCK);
71921457Skjd 			nread = readtbuf(&bufp, nwant);
72022688Slepreau 			if (write(ofile, bufp, (int)min(nread, bytes)) < 0) {
7216250Sroot 				fprintf(stderr,
72213492Ssam 				    "tar: %s: HELP - extract write error\n",
72313492Ssam 				    dblock.dbuf.name);
7246250Sroot 				done(2);
7256250Sroot 			}
72621457Skjd 			bytes -= nread;
72721457Skjd 			blocks -= (((nread-1)/TBLOCK)+1);
7281119Sbill 		}
7291119Sbill 		close(ofile);
73022688Slepreau 		if (mflag == 0)
73122688Slepreau 			setimes(dblock.dbuf.name, stbuf.st_mtime);
7321926Swnj 		if (pflag)
7336250Sroot 			chmod(dblock.dbuf.name, stbuf.st_mode & 07777);
7341119Sbill 	}
73522688Slepreau 	if (mflag == 0) {
73622688Slepreau 		dblock.dbuf.name[0] = '\0';	/* process the whole stack */
73722688Slepreau 		dodirtimes(&dblock);
73822688Slepreau 	}
7391119Sbill }
7401119Sbill 
7411119Sbill dotable()
7421119Sbill {
7431119Sbill 	for (;;) {
7441119Sbill 		getdir();
7451119Sbill 		if (endtape())
7461119Sbill 			break;
7471119Sbill 		if (vflag)
7481119Sbill 			longt(&stbuf);
7491119Sbill 		printf("%s", dblock.dbuf.name);
7501119Sbill 		if (dblock.dbuf.linkflag == '1')
7511119Sbill 			printf(" linked to %s", dblock.dbuf.linkname);
7526250Sroot 		if (dblock.dbuf.linkflag == '2')
7536250Sroot 			printf(" symbolic link to %s", dblock.dbuf.linkname);
7541119Sbill 		printf("\n");
7551119Sbill 		passtape();
7561119Sbill 	}
7571119Sbill }
7581119Sbill 
7591119Sbill putempty()
7601119Sbill {
7611119Sbill 	char buf[TBLOCK];
7621119Sbill 
76313492Ssam 	bzero(buf, sizeof (buf));
7641119Sbill 	writetape(buf);
7651119Sbill }
7661119Sbill 
7671119Sbill longt(st)
7686250Sroot 	register struct stat *st;
7691119Sbill {
7701119Sbill 	register char *cp;
7711119Sbill 	char *ctime();
7721119Sbill 
7731119Sbill 	pmode(st);
7741119Sbill 	printf("%3d/%1d", st->st_uid, st->st_gid);
7751119Sbill 	printf("%7D", st->st_size);
7761119Sbill 	cp = ctime(&st->st_mtime);
7771119Sbill 	printf(" %-12.12s %-4.4s ", cp+4, cp+20);
7781119Sbill }
7791119Sbill 
7801119Sbill #define	SUID	04000
7811119Sbill #define	SGID	02000
7821119Sbill #define	ROWN	0400
7831119Sbill #define	WOWN	0200
7841119Sbill #define	XOWN	0100
7851119Sbill #define	RGRP	040
7861119Sbill #define	WGRP	020
7871119Sbill #define	XGRP	010
7881119Sbill #define	ROTH	04
7891119Sbill #define	WOTH	02
7901119Sbill #define	XOTH	01
7911119Sbill #define	STXT	01000
7921119Sbill int	m1[] = { 1, ROWN, 'r', '-' };
7931119Sbill int	m2[] = { 1, WOWN, 'w', '-' };
7941119Sbill int	m3[] = { 2, SUID, 's', XOWN, 'x', '-' };
7951119Sbill int	m4[] = { 1, RGRP, 'r', '-' };
7961119Sbill int	m5[] = { 1, WGRP, 'w', '-' };
7971119Sbill int	m6[] = { 2, SGID, 's', XGRP, 'x', '-' };
7981119Sbill int	m7[] = { 1, ROTH, 'r', '-' };
7991119Sbill int	m8[] = { 1, WOTH, 'w', '-' };
8001119Sbill int	m9[] = { 2, STXT, 't', XOTH, 'x', '-' };
8011119Sbill 
8021119Sbill int	*m[] = { m1, m2, m3, m4, m5, m6, m7, m8, m9};
8031119Sbill 
8041119Sbill pmode(st)
8056250Sroot 	register struct stat *st;
8061119Sbill {
8071119Sbill 	register int **mp;
8081119Sbill 
8091119Sbill 	for (mp = &m[0]; mp < &m[9];)
8101119Sbill 		select(*mp++, st);
8111119Sbill }
8121119Sbill 
8131119Sbill select(pairp, st)
8146250Sroot 	int *pairp;
8156250Sroot 	struct stat *st;
8161119Sbill {
8171119Sbill 	register int n, *ap;
8181119Sbill 
8191119Sbill 	ap = pairp;
8201119Sbill 	n = *ap++;
8211119Sbill 	while (--n>=0 && (st->st_mode&*ap++)==0)
8221119Sbill 		ap++;
8231119Sbill 	printf("%c", *ap);
8241119Sbill }
8251119Sbill 
82622688Slepreau /*
82722688Slepreau  * Make all directories needed by `name'.  If `name' is a
82822688Slepreau  * directory itself on the tar tape (indicated by a trailing '/'),
82922688Slepreau  * return 1; else 0.
83022688Slepreau  */
8311119Sbill checkdir(name)
8326250Sroot 	register char *name;
8331119Sbill {
8341119Sbill 	register char *cp;
8356250Sroot 
83612154Ssam 	/*
83722688Slepreau 	 * Quick check for existence of directory.
83812154Ssam 	 */
83912154Ssam 	if ((cp = rindex(name, '/')) == 0)
84012154Ssam 		return (0);
84112154Ssam 	*cp = '\0';
84222688Slepreau 	if (access(name, 0) == 0) {	/* already exists */
84312154Ssam 		*cp = '/';
84422688Slepreau 		return (cp[1] == '\0');	/* return (lastchar == '/') */
84512154Ssam 	}
84612154Ssam 	*cp = '/';
84712154Ssam 
84812154Ssam 	/*
84912154Ssam 	 * No luck, try to make all directories in path.
85012154Ssam 	 */
8511119Sbill 	for (cp = name; *cp; cp++) {
8526250Sroot 		if (*cp != '/')
8536250Sroot 			continue;
8546250Sroot 		*cp = '\0';
85512154Ssam 		if (access(name, 0) < 0) {
8569844Ssam 			if (mkdir(name, 0777) < 0) {
8579844Ssam 				perror(name);
85812154Ssam 				*cp = '/';
85912154Ssam 				return (0);
8601119Sbill 			}
86121457Skjd 			chown(name, stbuf.st_uid, stbuf.st_gid);
86214956Skarels 			if (pflag && cp[1] == '\0')
8639844Ssam 				chmod(name, stbuf.st_mode & 0777);
8641119Sbill 		}
8656250Sroot 		*cp = '/';
8661119Sbill 	}
8676250Sroot 	return (cp[-1]=='/');
8681119Sbill }
8691119Sbill 
8701119Sbill onintr()
8711119Sbill {
8721119Sbill 	signal(SIGINT, SIG_IGN);
8731119Sbill 	term++;
8741119Sbill }
8751119Sbill 
8761119Sbill onquit()
8771119Sbill {
8781119Sbill 	signal(SIGQUIT, SIG_IGN);
8791119Sbill 	term++;
8801119Sbill }
8811119Sbill 
8821119Sbill onhup()
8831119Sbill {
8841119Sbill 	signal(SIGHUP, SIG_IGN);
8851119Sbill 	term++;
8861119Sbill }
8871119Sbill 
88822688Slepreau #ifdef notdef
8891119Sbill onterm()
8901119Sbill {
8911119Sbill 	signal(SIGTERM, SIG_IGN);
8921119Sbill 	term++;
8931119Sbill }
89422688Slepreau #endif
8951119Sbill 
8961119Sbill tomodes(sp)
8971119Sbill register struct stat *sp;
8981119Sbill {
8991119Sbill 	register char *cp;
9001119Sbill 
9011119Sbill 	for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++)
9021119Sbill 		*cp = '\0';
9031119Sbill 	sprintf(dblock.dbuf.mode, "%6o ", sp->st_mode & 07777);
9041119Sbill 	sprintf(dblock.dbuf.uid, "%6o ", sp->st_uid);
9051119Sbill 	sprintf(dblock.dbuf.gid, "%6o ", sp->st_gid);
9061119Sbill 	sprintf(dblock.dbuf.size, "%11lo ", sp->st_size);
9071119Sbill 	sprintf(dblock.dbuf.mtime, "%11lo ", sp->st_mtime);
9081119Sbill }
9091119Sbill 
9101119Sbill checksum()
9111119Sbill {
9121119Sbill 	register i;
9131119Sbill 	register char *cp;
9141119Sbill 
9156250Sroot 	for (cp = dblock.dbuf.chksum;
9166250Sroot 	     cp < &dblock.dbuf.chksum[sizeof(dblock.dbuf.chksum)]; cp++)
9171119Sbill 		*cp = ' ';
9181119Sbill 	i = 0;
9191119Sbill 	for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++)
9201119Sbill 		i += *cp;
9216250Sroot 	return (i);
9221119Sbill }
9231119Sbill 
9241119Sbill checkw(c, name)
9256250Sroot 	char *name;
9261119Sbill {
9276250Sroot 	if (!wflag)
9286250Sroot 		return (1);
9296250Sroot 	printf("%c ", c);
9306250Sroot 	if (vflag)
9316250Sroot 		longt(&stbuf);
9326250Sroot 	printf("%s: ", name);
9336250Sroot 	return (response() == 'y');
9341119Sbill }
9351119Sbill 
9361119Sbill response()
9371119Sbill {
9381119Sbill 	char c;
9391119Sbill 
9401119Sbill 	c = getchar();
9411119Sbill 	if (c != '\n')
9426250Sroot 		while (getchar() != '\n')
9436250Sroot 			;
9446250Sroot 	else
9456250Sroot 		c = 'n';
9466250Sroot 	return (c);
9471119Sbill }
9481119Sbill 
94912154Ssam checkf(name, mode, howmuch)
95012154Ssam 	char *name;
95112154Ssam 	int mode, howmuch;
95212154Ssam {
95312154Ssam 	int l;
95412154Ssam 
95521910Skjd 	if ((mode & S_IFMT) == S_IFDIR){
95621910Skjd 		if ((strcmp(name, "SCCS")==0) || (strcmp(name, "RCS")==0))
95721910Skjd 			return(0);
95821910Skjd 		return(1);
95921910Skjd 	}
96012154Ssam 	if ((l = strlen(name)) < 3)
96112154Ssam 		return (1);
96212154Ssam 	if (howmuch > 1 && name[l-2] == '.' && name[l-1] == 'o')
96312154Ssam 		return (0);
96412154Ssam 	if (strcmp(name, "core") == 0 ||
96512154Ssam 	    strcmp(name, "errs") == 0 ||
96612154Ssam 	    (howmuch > 1 && strcmp(name, "a.out") == 0))
96712154Ssam 		return (0);
96812154Ssam 	/* SHOULD CHECK IF IT IS EXECUTABLE */
96912154Ssam 	return (1);
97012154Ssam }
97112154Ssam 
97222688Slepreau /* Is the current file a new file, or the newest one of the same name? */
9731119Sbill checkupdate(arg)
9746250Sroot 	char *arg;
9751119Sbill {
9761119Sbill 	char name[100];
9776250Sroot 	long mtime;
9781119Sbill 	daddr_t seekp;
9791119Sbill 	daddr_t	lookup();
9801119Sbill 
9811119Sbill 	rewind(tfile);
9821119Sbill 	for (;;) {
9831119Sbill 		if ((seekp = lookup(arg)) < 0)
9846250Sroot 			return (1);
9851119Sbill 		fseek(tfile, seekp, 0);
9861119Sbill 		fscanf(tfile, "%s %lo", name, &mtime);
9876250Sroot 		return (stbuf.st_mtime > mtime);
9881119Sbill 	}
9891119Sbill }
9901119Sbill 
9911119Sbill done(n)
9921119Sbill {
99321457Skjd 	unlink(tname);
9941119Sbill 	exit(n);
9951119Sbill }
9961119Sbill 
99722688Slepreau /*
99822688Slepreau  * Does s2 begin with the string s1, on a directory boundary?
99922688Slepreau  */
10001119Sbill prefix(s1, s2)
10016250Sroot 	register char *s1, *s2;
10021119Sbill {
10031119Sbill 	while (*s1)
10041119Sbill 		if (*s1++ != *s2++)
10056250Sroot 			return (0);
10061119Sbill 	if (*s2)
10076250Sroot 		return (*s2 == '/');
10086250Sroot 	return (1);
10091119Sbill }
10101119Sbill 
10111119Sbill #define	N	200
10121119Sbill int	njab;
10136250Sroot 
10141119Sbill daddr_t
10151119Sbill lookup(s)
10166250Sroot 	char *s;
10171119Sbill {
10181119Sbill 	register i;
10191119Sbill 	daddr_t a;
10201119Sbill 
10211119Sbill 	for(i=0; s[i]; i++)
10226250Sroot 		if (s[i] == ' ')
10231119Sbill 			break;
10241119Sbill 	a = bsrch(s, i, low, high);
10256250Sroot 	return (a);
10261119Sbill }
10271119Sbill 
10281119Sbill daddr_t
10291119Sbill bsrch(s, n, l, h)
10306250Sroot 	daddr_t l, h;
10316250Sroot 	char *s;
10321119Sbill {
10331119Sbill 	register i, j;
10341119Sbill 	char b[N];
10351119Sbill 	daddr_t m, m1;
10361119Sbill 
10371119Sbill 	njab = 0;
10381119Sbill 
10391119Sbill loop:
10406250Sroot 	if (l >= h)
104122688Slepreau 		return ((daddr_t) -1);
10421119Sbill 	m = l + (h-l)/2 - N/2;
10436250Sroot 	if (m < l)
10441119Sbill 		m = l;
10451119Sbill 	fseek(tfile, m, 0);
10461119Sbill 	fread(b, 1, N, tfile);
10471119Sbill 	njab++;
10481119Sbill 	for(i=0; i<N; i++) {
10496250Sroot 		if (b[i] == '\n')
10501119Sbill 			break;
10511119Sbill 		m++;
10521119Sbill 	}
10536250Sroot 	if (m >= h)
105422688Slepreau 		return ((daddr_t) -1);
10551119Sbill 	m1 = m;
10561119Sbill 	j = i;
10571119Sbill 	for(i++; i<N; i++) {
10581119Sbill 		m1++;
10596250Sroot 		if (b[i] == '\n')
10601119Sbill 			break;
10611119Sbill 	}
10621119Sbill 	i = cmp(b+j, s, n);
10636250Sroot 	if (i < 0) {
10641119Sbill 		h = m;
10651119Sbill 		goto loop;
10661119Sbill 	}
10676250Sroot 	if (i > 0) {
10681119Sbill 		l = m1;
10691119Sbill 		goto loop;
10701119Sbill 	}
10716250Sroot 	return (m);
10721119Sbill }
10731119Sbill 
10741119Sbill cmp(b, s, n)
10756250Sroot 	char *b, *s;
10761119Sbill {
10771119Sbill 	register i;
10781119Sbill 
10796250Sroot 	if (b[0] != '\n')
108021457Skjd 		exit(2);
10811119Sbill 	for(i=0; i<n; i++) {
10826250Sroot 		if (b[i+1] > s[i])
10836250Sroot 			return (-1);
10846250Sroot 		if (b[i+1] < s[i])
10856250Sroot 			return (1);
10861119Sbill 	}
10876250Sroot 	return (b[i+1] == ' '? 0 : -1);
10881119Sbill }
10891119Sbill 
109022688Slepreau readtape(buffer)
10916250Sroot 	char *buffer;
10921119Sbill {
109321457Skjd 	char *bufp;
109421457Skjd 
109522688Slepreau 	if (first == 0)
109622688Slepreau 		getbuf();
109722688Slepreau 	readtbuf(&bufp, TBLOCK);
109821457Skjd 	bcopy(bufp, buffer, TBLOCK);
109921457Skjd 	return(TBLOCK);
110021457Skjd }
110121457Skjd 
110221457Skjd readtbuf(bufpp, size)
110321457Skjd 	char **bufpp;
110421457Skjd 	int size;
110521457Skjd {
11063457Swnj 	register int i;
11071119Sbill 
11081119Sbill 	if (recno >= nblock || first == 0) {
11098737Smckusick 		if ((i = bread(mt, tbuf, TBLOCK*nblock)) < 0) {
111013492Ssam 			fprintf(stderr, "tar: tape read error\n");
11111119Sbill 			done(3);
11121119Sbill 		}
11131119Sbill 		if (first == 0) {
11141119Sbill 			if ((i % TBLOCK) != 0) {
111513492Ssam 				fprintf(stderr, "tar: tape blocksize error\n");
11161119Sbill 				done(3);
11171119Sbill 			}
11181119Sbill 			i /= TBLOCK;
11193457Swnj 			if (i != nblock) {
112013492Ssam 				fprintf(stderr, "tar: blocksize = %d\n", i);
11211119Sbill 				nblock = i;
11221119Sbill 			}
112322353Skjd 			first = 1;
11241119Sbill 		}
11251119Sbill 		recno = 0;
11261119Sbill 	}
112721457Skjd 	if (size > ((nblock-recno)*TBLOCK))
112821457Skjd 		size = (nblock-recno)*TBLOCK;
112921457Skjd 	*bufpp = (char *)&tbuf[recno];
113021457Skjd 	recno += (size/TBLOCK);
113121457Skjd 	return (size);
11321119Sbill }
11331119Sbill 
113421457Skjd writetbuf(buffer, n)
113521457Skjd 	register char *buffer;
113621457Skjd 	register int n;
11371119Sbill {
113822688Slepreau 
113922688Slepreau 	if (first == 0) {
114022353Skjd 		getbuf();
114122353Skjd 		first = 1;
114222353Skjd 	}
11431119Sbill 	if (recno >= nblock) {
114422688Slepreau 		if (write(mt, tbuf, TBLOCK*nblock) < 0) {
114522688Slepreau 			fprintf(stderr,"tar: tape write error\n");
11461119Sbill 			done(2);
11471119Sbill 		}
11481119Sbill 		recno = 0;
11491119Sbill 	}
115021457Skjd 
115121457Skjd 	/*
115221457Skjd 	 *  Special case:  We have an empty tape buffer, and the
115321457Skjd 	 *  users data size is >= the tape block size:  Avoid
115421457Skjd 	 *  the bcopy and dma direct to tape.  BIG WIN.  Add the
115521457Skjd 	 *  residual to the tape buffer.
115621457Skjd 	 */
115721457Skjd 	while (recno == 0 && n >= nblock) {
115821457Skjd 		if (write(mt, buffer, TBLOCK*nblock) < 0) {
115922688Slepreau 			fprintf(stderr,"tar: tape write error\n");
11601119Sbill 			done(2);
11611119Sbill 		}
116221457Skjd 		n -= nblock;
116321457Skjd 		buffer += (nblock * TBLOCK);
11641119Sbill 	}
116521457Skjd 
116621457Skjd 	while (n-- > 0) {
116721457Skjd 		bcopy(buffer, (char *)&tbuf[recno++], TBLOCK);
116821457Skjd 		buffer += TBLOCK;
116921457Skjd 		if (recno >= nblock) {
117021457Skjd 			if (write(mt, tbuf, TBLOCK*nblock) < 0) {
117122353Skjd 					fprintf(stderr,"tar: tape write error\n");
117222353Skjd 					done(2);
117321457Skjd 			}
117421457Skjd 			recno = 0;
117521457Skjd 		}
117621457Skjd 	}
117721457Skjd 
117821457Skjd 	/* Tell the user how much to write to get in sync */
117921457Skjd 	return (nblock - recno);
11801119Sbill }
11811119Sbill 
11821119Sbill backtape()
11831119Sbill {
11843457Swnj 	static struct mtop mtop = {MTBSR, 1};
11853457Swnj 
11863457Swnj 	if (mtdev == 0) {
11873457Swnj 		if (ioctl(mt, MTIOCTOP, &mtop) < 0) {
118813492Ssam 			fprintf(stderr, "tar: tape backspace error\n");
11891119Sbill 			done(4);
11901119Sbill 		}
11913457Swnj 	} else
119222688Slepreau 		lseek(mt, (daddr_t) -TBLOCK*nblock, 1);
11933457Swnj 	recno--;
11941119Sbill }
11951119Sbill 
11961119Sbill flushtape()
11971119Sbill {
119821457Skjd 	write(mt, tbuf, TBLOCK*nblock);
11991119Sbill }
12001119Sbill 
12018737Smckusick bread(fd, buf, size)
12028737Smckusick 	int fd;
12038737Smckusick 	char *buf;
12048737Smckusick 	int size;
12058737Smckusick {
12068737Smckusick 	int count;
12078737Smckusick 	static int lastread = 0;
12088737Smckusick 
120922688Slepreau 	if (!Bflag)
121022688Slepreau 		return (read(fd, buf, size));
121122353Skjd 
12128737Smckusick 	for (count = 0; count < size; count += lastread) {
1213*24281Smckusick 		lastread = read(fd, buf, size - count);
1214*24281Smckusick 		if (lastread <= 0) {
12158737Smckusick 			if (count > 0)
12168737Smckusick 				return (count);
12178737Smckusick 			return (lastread);
12188737Smckusick 		}
12198737Smckusick 		buf += lastread;
12208737Smckusick 	}
12218737Smckusick 	return (count);
12228737Smckusick }
122310165Ssam 
122410165Ssam char *
122510165Ssam getcwd(buf)
122610165Ssam 	char *buf;
122710165Ssam {
122810165Ssam 	if (getwd(buf) == NULL) {
122910165Ssam 		fprintf(stderr, "tar: %s\n", buf);
123021457Skjd 		exit(1);
123110165Ssam 	}
123210165Ssam 	return (buf);
123310165Ssam }
123421910Skjd 
123521910Skjd getbuf()
123621910Skjd {
123722353Skjd 
123822688Slepreau 	if (mtdev == 1) {
123921910Skjd 		fstat(mt, &stbuf);
124021910Skjd 		if ((stbuf.st_mode & S_IFMT) == S_IFCHR)
124121910Skjd 			mtdev = 0;
124221910Skjd 		else
124321910Skjd 			mtdev = -1;
124421910Skjd 	}
124522688Slepreau 	if (nblock == 0) {
124621910Skjd 		if (mtdev == 0)
124721910Skjd 			nblock = FILEBLOCK;
124821910Skjd 		else {
124921910Skjd 			fstat(mt, &stbuf);
125021910Skjd 			nblock = stbuf.st_blocks / TBLOCK;
125121910Skjd 		}
125221910Skjd 	}
125322688Slepreau 	if (nblock == 0)
125422688Slepreau 		nblock = FILEBLOCK;
125521910Skjd 	tbuf = (union hblock *)malloc(nblock*TBLOCK);
125621910Skjd 	if (tbuf == NULL) {
125721910Skjd 		fprintf(stderr, "tar: blocksize %d too big, can't get memory\n",
125821910Skjd 		    nblock);
125921910Skjd 		done(1);
126021910Skjd 	}
126121910Skjd }
126222353Skjd 
126322688Slepreau /*
126422688Slepreau  * We are really keeping a directory stack, but since all the
126522688Slepreau  * elements of it share a common prefix, we can make do with one
126622688Slepreau  * string.  We keep only the current directory path, with an associated
126722688Slepreau  * array of mtime's, one for each '/' in the path.  A negative mtime
126822688Slepreau  * means no mtime.  The mtime's are offset by one (first index 1, not
126922688Slepreau  * 0) because calling this with the null string causes mtime[0] to be set.
127022688Slepreau  *
127122688Slepreau  * This stack algorithm is not guaranteed to work for tapes created
127222688Slepreau  * with the 'r' option, but the vast majority of tapes with
127322688Slepreau  * directories are not.  This avoids saving every directory record on
127422688Slepreau  * the tape and setting all the times at the end.
127522688Slepreau  */
127622688Slepreau char dirstack[NAMSIZ];
127722688Slepreau #define NTIM (NAMSIZ/2+1)		/* a/b/c/d/... */
127822688Slepreau time_t mtime[NTIM];
127922353Skjd 
128022688Slepreau dodirtimes(hp)
128122688Slepreau 	union hblock *hp;
128222353Skjd {
128322688Slepreau 	register char *p = dirstack;
128422688Slepreau 	register char *q = hp->dbuf.name;
128522688Slepreau 	register int ndir = 0;
128622688Slepreau 	char *savp;
128722688Slepreau 	int savndir;
128822353Skjd 
128922688Slepreau 	/* Find common prefix */
129022688Slepreau 	while (*p == *q) {
129122688Slepreau 		if (*p++ == '/')
129222688Slepreau 			++ndir;
129322688Slepreau 		q++;
129422688Slepreau 	}
129522353Skjd 
129622688Slepreau 	savp = p;
129722688Slepreau 	savndir = ndir;
129822688Slepreau 	while (*p) {
129922688Slepreau 		/*
130022688Slepreau 		 * Not a child: unwind the stack, setting the times.
130122688Slepreau 		 * The order we do this doesn't matter, so we go "forward."
130222688Slepreau 		 */
130322688Slepreau 		if (*p++ == '/')
130422688Slepreau 			if (mtime[++ndir] >= 0) {
130522688Slepreau 				*--p = '\0';	/* zap the slash */
130622688Slepreau 				setimes(dirstack, mtime[ndir]);
130722688Slepreau 				*p++ = '/';
130822688Slepreau 			}
130922688Slepreau 	}
131022688Slepreau 	p = savp;
131122688Slepreau 	ndir = savndir;
131222353Skjd 
131322688Slepreau 	/* Push this one on the "stack" */
131422688Slepreau 	while (*p = *q++)	/* append the rest of the new dir */
131522688Slepreau 		if (*p++ == '/')
131622688Slepreau 			mtime[++ndir] = -1;
131722688Slepreau 	mtime[ndir] = stbuf.st_mtime;	/* overwrite the last one */
131822688Slepreau }
131922353Skjd 
132022688Slepreau setimes(path, mt)
132122688Slepreau 	char *path;
132222688Slepreau 	time_t mt;
132322688Slepreau {
132422688Slepreau 	struct timeval tv[2];
132522353Skjd 
132622688Slepreau 	tv[0].tv_sec = time((time_t *) 0);
132722688Slepreau 	tv[1].tv_sec = mt;
132822688Slepreau 	tv[0].tv_usec = tv[1].tv_usec = 0;
132922688Slepreau 	if (utimes(path, tv) < 0)
133022688Slepreau 		fprintf(stderr, "tar: can't set time on %s\n", path);
133122688Slepreau }
133222688Slepreau 
133322688Slepreau char *
133422688Slepreau getmem(size)
133522688Slepreau {
133622688Slepreau 	char *p = malloc((unsigned) size);
133722688Slepreau 
133822688Slepreau 	if (p == NULL && freemem) {
133922688Slepreau 		fprintf(stderr,
134022688Slepreau 		    "tar: out of memory, link and directory modtime info lost\n");
134122688Slepreau 		freemem = 0;
134222353Skjd 	}
134322688Slepreau 	return (p);
134422353Skjd }
1345