147845Sbostic /*-
247845Sbostic * Copyright (c) 1991 The Regents of the University of California.
347845Sbostic * All rights reserved.
447845Sbostic *
547845Sbostic * %sccs.include.proprietary.c%
622502Sdist */
722502Sdist
812154Ssam #ifndef lint
922502Sdist char copyright[] =
1047845Sbostic "@(#) Copyright (c) 1991 The Regents of the University of California.\n\
1122502Sdist All rights reserved.\n";
1233082Sbostic #endif /* not lint */
136250Sroot
1422502Sdist #ifndef lint
15*54198Sbostic static char sccsid[] = "@(#)tar.c 5.19 (Berkeley) 06/21/92";
1633082Sbostic #endif /* not lint */
1722502Sdist
186250Sroot /*
196250Sroot * Tape Archival Program
206250Sroot */
216413Smckusic #include <sys/param.h>
221119Sbill #include <sys/stat.h>
238150Smckusick #include <sys/ioctl.h>
243457Swnj #include <sys/mtio.h>
2512983Ssam #include <sys/time.h>
2646656Sbostic #include <dirent.h>
2746656Sbostic #include <fcntl.h>
281119Sbill #include <signal.h>
2912154Ssam #include <errno.h>
3027058Smckusick #include <fcntl.h>
3146656Sbostic #include <unistd.h>
3246656Sbostic #include <stdio.h>
3342013Sbostic #include <string.h>
3446656Sbostic #include <stdlib.h>
3537681Sbostic #include "pathnames.h"
361119Sbill
371119Sbill #define TBLOCK 512
383355Swnj #define NBLOCK 20
391119Sbill #define NAMSIZ 100
406250Sroot
4153771Selan #define ARGV 0
4253771Selan #define PUTFILE 1
4353771Selan
4421457Skjd #define writetape(b) writetbuf(b, 1)
4521457Skjd #define min(a,b) ((a) < (b) ? (a) : (b))
4621457Skjd #define max(a,b) ((a) > (b) ? (a) : (b))
4721457Skjd
481119Sbill union hblock {
491119Sbill char dummy[TBLOCK];
501119Sbill struct header {
511119Sbill char name[NAMSIZ];
521119Sbill char mode[8];
531119Sbill char uid[8];
541119Sbill char gid[8];
551119Sbill char size[12];
561119Sbill char mtime[12];
571119Sbill char chksum[8];
581119Sbill char linkflag;
591119Sbill char linkname[NAMSIZ];
601119Sbill } dbuf;
616250Sroot };
621119Sbill
631119Sbill struct linkbuf {
641119Sbill ino_t inum;
651119Sbill dev_t devnum;
661119Sbill int count;
671119Sbill char pathname[NAMSIZ];
681119Sbill struct linkbuf *nextp;
696250Sroot };
701119Sbill
716250Sroot union hblock dblock;
7213492Ssam union hblock *tbuf;
736250Sroot struct linkbuf *ihead;
746250Sroot struct stat stbuf;
751119Sbill
766250Sroot int rflag;
7734429Sbostic int sflag;
786250Sroot int xflag;
796250Sroot int vflag;
806250Sroot int tflag;
816250Sroot int cflag;
826250Sroot int mflag;
836250Sroot int fflag;
8412154Ssam int iflag;
856250Sroot int oflag;
866250Sroot int pflag;
876250Sroot int wflag;
886250Sroot int hflag;
8953771Selan int Hflag;
908737Smckusick int Bflag;
9112154Ssam int Fflag;
926250Sroot
936250Sroot int mt;
946250Sroot int term;
956250Sroot int chksum;
966250Sroot int recno;
9722688Slepreau int first;
9822688Slepreau int prtlinkerr;
991119Sbill int freemem = 1;
10022353Skjd int nblock = 0;
10146656Sbostic void onintr(), onquit(), onhup();
10222688Slepreau #ifdef notdef
10346656Sbostic void onterm();
10422688Slepreau #endif
1051119Sbill
1061119Sbill daddr_t low;
1071119Sbill daddr_t high;
1086250Sroot daddr_t bsrch();
1091119Sbill
11021910Skjd FILE *vfile = stdout;
1111119Sbill FILE *tfile;
11237681Sbostic char tname[] = _PATH_TMP;
1131119Sbill char *usefile;
11437681Sbostic char magtape[] = _PATH_MAGTAPE;
11546656Sbostic char *cwd();
11622688Slepreau char *getmem();
1171119Sbill
11836227Sbostic extern int errno;
11936227Sbostic
main(argc,argv)1201119Sbill main(argc, argv)
12136227Sbostic int argc;
12236227Sbostic char **argv;
1231119Sbill {
1241119Sbill char *cp;
1251119Sbill
1261119Sbill if (argc < 2)
1271119Sbill usage();
1281119Sbill
1291119Sbill tfile = NULL;
1301119Sbill usefile = magtape;
1311119Sbill argv[argc] = 0;
1321119Sbill argv++;
1331119Sbill for (cp = *argv++; *cp; cp++)
1341119Sbill switch(*cp) {
1356250Sroot
13653771Selan case 'H':
13753771Selan Hflag++;
13853771Selan break;
1391119Sbill case 'f':
14012154Ssam if (*argv == 0) {
14112154Ssam fprintf(stderr,
14212154Ssam "tar: tapefile must be specified with 'f' option\n");
14312154Ssam usage();
14412154Ssam }
1451119Sbill usefile = *argv++;
1461119Sbill fflag++;
1471119Sbill break;
1486250Sroot
1491119Sbill case 'c':
1501119Sbill cflag++;
1511119Sbill rflag++;
1521119Sbill break;
1536250Sroot
1541119Sbill case 'o':
1551119Sbill oflag++;
1561119Sbill break;
1576250Sroot
1581119Sbill case 'p':
1591119Sbill pflag++;
1601119Sbill break;
1616250Sroot
1621119Sbill case 'u':
16336227Sbostic (void)mktemp(tname);
1641119Sbill if ((tfile = fopen(tname, "w")) == NULL) {
1656250Sroot fprintf(stderr,
16622688Slepreau "tar: cannot create temporary file (%s)\n",
1676250Sroot tname);
1681119Sbill done(1);
1691119Sbill }
1701119Sbill fprintf(tfile, "!!!!!/!/!/!/!/!/!/! 000\n");
1716250Sroot /*FALL THRU*/
1726250Sroot
1731119Sbill case 'r':
1741119Sbill rflag++;
1751119Sbill break;
1766250Sroot
17734429Sbostic case 's':
17834429Sbostic sflag++;
17934429Sbostic break;
18034429Sbostic
1811119Sbill case 'v':
1821119Sbill vflag++;
1831119Sbill break;
1846250Sroot
1851119Sbill case 'w':
1861119Sbill wflag++;
1871119Sbill break;
1886250Sroot
1891119Sbill case 'x':
1901119Sbill xflag++;
1911119Sbill break;
1926250Sroot
1931119Sbill case 't':
1941119Sbill tflag++;
1951119Sbill break;
1966250Sroot
1971119Sbill case 'm':
1981119Sbill mflag++;
1991119Sbill break;
2006250Sroot
2011119Sbill case '-':
2021119Sbill break;
2036250Sroot
2041119Sbill case '0':
2051119Sbill case '1':
2061119Sbill case '4':
2071119Sbill case '5':
20821457Skjd case '7':
2091119Sbill case '8':
2101119Sbill magtape[8] = *cp;
2111119Sbill usefile = magtape;
2121119Sbill break;
2136250Sroot
2141119Sbill case 'b':
21513492Ssam if (*argv == 0) {
21613492Ssam fprintf(stderr,
21713492Ssam "tar: blocksize must be specified with 'b' option\n");
21813492Ssam usage();
21913492Ssam }
22013492Ssam nblock = atoi(*argv);
22113492Ssam if (nblock <= 0) {
22213492Ssam fprintf(stderr,
22313492Ssam "tar: invalid blocksize \"%s\"\n", *argv);
2241119Sbill done(1);
2251119Sbill }
22613492Ssam argv++;
2271119Sbill break;
2286250Sroot
2291119Sbill case 'l':
23022688Slepreau prtlinkerr++;
2311119Sbill break;
2326250Sroot
2336250Sroot case 'h':
2346250Sroot hflag++;
2356250Sroot break;
2366250Sroot
23712154Ssam case 'i':
23812154Ssam iflag++;
23912154Ssam break;
24012154Ssam
2418737Smckusick case 'B':
2428737Smckusick Bflag++;
2438737Smckusick break;
2448737Smckusick
24512154Ssam case 'F':
24612154Ssam Fflag++;
24712154Ssam break;
24812154Ssam
2491119Sbill default:
2501119Sbill fprintf(stderr, "tar: %c: unknown option\n", *cp);
2511119Sbill usage();
2521119Sbill }
2531119Sbill
2546250Sroot if (!rflag && !xflag && !tflag)
2556250Sroot usage();
2561119Sbill if (rflag) {
2576250Sroot if (cflag && tfile != NULL)
2581119Sbill usage();
2591119Sbill if (signal(SIGINT, SIG_IGN) != SIG_IGN)
26027445Slepreau (void) signal(SIGINT, onintr);
2611119Sbill if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
26227445Slepreau (void) signal(SIGHUP, onhup);
2631119Sbill if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
26427445Slepreau (void) signal(SIGQUIT, onquit);
2656250Sroot #ifdef notdef
2661119Sbill if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
26727445Slepreau (void) signal(SIGTERM, onterm);
2686250Sroot #endif
26927058Smckusick mt = openmt(usefile, 1);
2701119Sbill dorep(argv);
2716250Sroot done(0);
2721119Sbill }
27327058Smckusick mt = openmt(usefile, 0);
2746250Sroot if (xflag)
2751119Sbill doxtract(argv);
2766250Sroot else
27727445Slepreau dotable(argv);
2781119Sbill done(0);
2791119Sbill }
2801119Sbill
usage()2811119Sbill usage()
2821119Sbill {
2836250Sroot fprintf(stderr,
28421457Skjd "tar: usage: tar -{txru}[cvfblmhopwBi] [tapefile] [blocksize] file1 file2...\n");
2851119Sbill done(1);
2861119Sbill }
2871119Sbill
28827058Smckusick int
openmt(tape,writing)28927058Smckusick openmt(tape, writing)
29027058Smckusick char *tape;
29127058Smckusick int writing;
29227058Smckusick {
29327058Smckusick if (strcmp(tape, "-") == 0) {
29427058Smckusick /*
29527058Smckusick * Read from standard input or write to standard output.
29627058Smckusick */
29727058Smckusick if (writing) {
29827058Smckusick if (cflag == 0) {
29927058Smckusick fprintf(stderr,
30027058Smckusick "tar: can only create standard output archives\n");
30127058Smckusick done(1);
30227058Smckusick }
30327058Smckusick vfile = stderr;
30427058Smckusick setlinebuf(vfile);
30527058Smckusick mt = dup(1);
30627058Smckusick } else {
30727058Smckusick mt = dup(0);
30827058Smckusick Bflag++;
30927058Smckusick }
31027058Smckusick } else {
31127058Smckusick /*
31227058Smckusick * Use file or tape on local machine.
31327058Smckusick */
31427058Smckusick if (writing) {
31527058Smckusick if (cflag)
31627445Slepreau mt = open(tape, O_RDWR|O_CREAT|O_TRUNC, 0666);
31727058Smckusick else
31827058Smckusick mt = open(tape, O_RDWR);
31927058Smckusick } else
32027058Smckusick mt = open(tape, O_RDONLY);
32127058Smckusick if (mt < 0) {
32236227Sbostic fprintf(stderr, "tar: %s: %s\n", tape, strerror(errno));
32327058Smckusick done(1);
32427058Smckusick }
32527058Smckusick }
32627058Smckusick return(mt);
32727058Smckusick }
32827058Smckusick
dorep(argv)3291119Sbill dorep(argv)
3306250Sroot char *argv[];
3311119Sbill {
3321119Sbill register char *cp, *cp2;
33346656Sbostic char *parent, *wdir;
3341119Sbill
3351119Sbill if (!cflag) {
3361119Sbill getdir();
3371119Sbill do {
3381119Sbill passtape();
3391119Sbill if (term)
3401119Sbill done(0);
3411119Sbill getdir();
3421119Sbill } while (!endtape());
34313492Ssam backtape();
3441119Sbill if (tfile != NULL) {
3451119Sbill char buf[200];
3461119Sbill
34732420Sbostic (void)sprintf(buf,
3486250Sroot "sort +0 -1 +1nr %s -o %s; awk '$1 != prev {print; prev=$1}' %s >%sX; mv %sX %s",
3491119Sbill tname, tname, tname, tname, tname, tname);
3501119Sbill fflush(tfile);
3511119Sbill system(buf);
3521119Sbill freopen(tname, "r", tfile);
3531119Sbill fstat(fileno(tfile), &stbuf);
3541119Sbill high = stbuf.st_size;
3551119Sbill }
3561119Sbill }
3571119Sbill
35846656Sbostic wdir = cwd();
3591119Sbill while (*argv && ! term) {
3601119Sbill cp2 = *argv;
3611119Sbill if (!strcmp(cp2, "-C") && argv[1]) {
3621119Sbill argv++;
36327058Smckusick if (chdir(*argv) < 0) {
36436227Sbostic fprintf(stderr,
36536227Sbostic "tar: can't change directories to %s: %s\n",
36636227Sbostic *argv, strerror(errno));
36746656Sbostic } else {
36846656Sbostic free(wdir);
36946656Sbostic wdir = cwd();
37046656Sbostic }
3711119Sbill argv++;
3721119Sbill continue;
3731119Sbill }
3749601Ssam parent = wdir;
3751119Sbill for (cp = *argv; *cp; cp++)
3761119Sbill if (*cp == '/')
3771119Sbill cp2 = cp;
3781119Sbill if (cp2 != *argv) {
3791119Sbill *cp2 = '\0';
3809601Ssam if (chdir(*argv) < 0) {
38136227Sbostic fprintf(stderr,
38236227Sbostic "tar: can't change directories to %s: %s\n",
38336227Sbostic *argv, strerror(errno));
3849601Ssam continue;
3859601Ssam }
38646656Sbostic parent = cwd();
3871119Sbill *cp2 = '/';
3881119Sbill cp2++;
3891119Sbill }
39053771Selan putfile(*argv++, cp2, parent, ARGV);
39136227Sbostic if (chdir(wdir) < 0)
39236227Sbostic fprintf(stderr, "tar: cannot change back?: %s: %s\n",
39336227Sbostic wdir, strerror(errno));
3941119Sbill }
3951119Sbill putempty();
3961119Sbill putempty();
3971119Sbill flushtape();
39822688Slepreau if (prtlinkerr == 0)
3996250Sroot return;
4006250Sroot for (; ihead != NULL; ihead = ihead->nextp) {
4016250Sroot if (ihead->count == 0)
4026250Sroot continue;
40313492Ssam fprintf(stderr, "tar: missing links to %s\n", ihead->pathname);
4046250Sroot }
4051119Sbill }
4061119Sbill
endtape()4071119Sbill endtape()
4081119Sbill {
40921457Skjd return (dblock.dbuf.name[0] == '\0');
4101119Sbill }
4111119Sbill
getdir()4121119Sbill getdir()
4131119Sbill {
4141119Sbill register struct stat *sp;
415*54198Sbostic long tempquad;
4161119Sbill int i;
4171119Sbill
41812154Ssam top:
4196250Sroot readtape((char *)&dblock);
4201119Sbill if (dblock.dbuf.name[0] == '\0')
4211119Sbill return;
4221119Sbill sp = &stbuf;
4231119Sbill sscanf(dblock.dbuf.mode, "%o", &i);
4241119Sbill sp->st_mode = i;
4251119Sbill sscanf(dblock.dbuf.uid, "%o", &i);
4261119Sbill sp->st_uid = i;
4271119Sbill sscanf(dblock.dbuf.gid, "%o", &i);
4281119Sbill sp->st_gid = i;
429*54198Sbostic sscanf(dblock.dbuf.size, "%lo", &tempquad);
430*54198Sbostic sp->st_size = tempquad;
4311119Sbill sscanf(dblock.dbuf.mtime, "%lo", &sp->st_mtime);
4321119Sbill sscanf(dblock.dbuf.chksum, "%o", &chksum);
43312154Ssam if (chksum != (i = checksum())) {
43413492Ssam fprintf(stderr, "tar: directory checksum error (%d != %d)\n",
43512154Ssam chksum, i);
43612154Ssam if (iflag)
43712154Ssam goto top;
4381119Sbill done(2);
4391119Sbill }
44034429Sbostic /* strip off leading "/" if present */
44134429Sbostic if (sflag && dblock.dbuf.name[0] == '/') {
44234429Sbostic register char *cp1, *cp2;
44334429Sbostic for (cp1 = cp2 = dblock.dbuf.name; *cp2 && *cp2 == '/'; ++cp2);
44434429Sbostic if (!*cp2)
44534429Sbostic goto top;
44634429Sbostic while (*cp1++ = *cp2++);
44734429Sbostic }
4481119Sbill if (tfile != NULL)
4491119Sbill fprintf(tfile, "%s %s\n", dblock.dbuf.name, dblock.dbuf.mtime);
4501119Sbill }
4511119Sbill
passtape()4521119Sbill passtape()
4531119Sbill {
4541119Sbill long blocks;
45521457Skjd char *bufp;
4561119Sbill
4571119Sbill if (dblock.dbuf.linkflag == '1')
4581119Sbill return;
4591119Sbill blocks = stbuf.st_size;
4601119Sbill blocks += TBLOCK-1;
4611119Sbill blocks /= TBLOCK;
4621119Sbill
46321457Skjd while (blocks-- > 0)
46427445Slepreau (void) readtbuf(&bufp, TBLOCK);
4651119Sbill }
4661119Sbill
putfile(longname,shortname,parent,source)46753771Selan putfile(longname, shortname, parent, source)
4686250Sroot char *longname;
4696250Sroot char *shortname;
4709601Ssam char *parent;
4711119Sbill {
47221457Skjd int infile = 0;
47321457Skjd long blocks;
4741119Sbill char buf[TBLOCK];
47521457Skjd char *bigbuf;
47622688Slepreau register char *cp;
47746656Sbostic struct dirent *dp;
4785931Smckusic DIR *dirp;
47927445Slepreau register int i;
48027445Slepreau long l;
4819601Ssam char newparent[NAMSIZ+64];
48221457Skjd int maxread;
48321457Skjd int hint; /* amount to write to get "in sync" */
4841119Sbill
48553771Selan if (hflag || (Hflag && source == ARGV))
48653771Selan i = stat(shortname, &stbuf);
48753771Selan else
48812154Ssam i = lstat(shortname, &stbuf);
48953771Selan
49012154Ssam if (i < 0) {
49136227Sbostic fprintf(stderr, "tar: %s: %s\n", longname, strerror(errno));
4929601Ssam return;
4939601Ssam }
49412154Ssam if (tfile != NULL && checkupdate(longname) == 0)
4951119Sbill return;
49612154Ssam if (checkw('r', longname) == 0)
4971119Sbill return;
49812154Ssam if (Fflag && checkf(shortname, stbuf.st_mode, Fflag) == 0)
49912154Ssam return;
5001119Sbill
50112154Ssam switch (stbuf.st_mode & S_IFMT) {
50212154Ssam case S_IFDIR:
5036250Sroot for (i = 0, cp = buf; *cp++ = longname[i++];)
5046250Sroot ;
5051119Sbill *--cp = '/';
5061119Sbill *++cp = 0 ;
5071119Sbill if (!oflag) {
5086250Sroot if ((cp - buf) >= NAMSIZ) {
50913492Ssam fprintf(stderr, "tar: %s: file name too long\n",
51013492Ssam longname);
5116250Sroot return;
5126250Sroot }
5136250Sroot stbuf.st_size = 0;
5146250Sroot tomodes(&stbuf);
5156250Sroot strcpy(dblock.dbuf.name,buf);
51632420Sbostic (void)sprintf(dblock.dbuf.chksum, "%6o", checksum());
51727445Slepreau (void) writetape((char *)&dblock);
5181119Sbill }
51932420Sbostic (void)sprintf(newparent, "%s/%s", parent, shortname);
52015045Smckusick if (chdir(shortname) < 0) {
52136227Sbostic fprintf(stderr, "tar: chdir %s: %s\n",
52236227Sbostic shortname, strerror(errno));
52315045Smckusick return;
52415045Smckusick }
5255931Smckusic if ((dirp = opendir(".")) == NULL) {
52613492Ssam fprintf(stderr, "tar: %s: directory read error\n",
52713492Ssam longname);
52815045Smckusick if (chdir(parent) < 0) {
52936227Sbostic fprintf(stderr,
53036227Sbostic "tar: cannot change back?: %s: %s\n",
53136227Sbostic parent, strerror(errno));
53215045Smckusick }
5335931Smckusic return;
5345931Smckusic }
5355931Smckusic while ((dp = readdir(dirp)) != NULL && !term) {
5366250Sroot if (!strcmp(".", dp->d_name) ||
5376250Sroot !strcmp("..", dp->d_name))
5381119Sbill continue;
5395931Smckusic strcpy(cp, dp->d_name);
54027445Slepreau l = telldir(dirp);
5415931Smckusic closedir(dirp);
54253771Selan putfile(buf, cp, newparent, PUTFILE);
5435931Smckusic dirp = opendir(".");
54427445Slepreau seekdir(dirp, l);
5451119Sbill }
5465931Smckusic closedir(dirp);
54715045Smckusick if (chdir(parent) < 0) {
54836227Sbostic fprintf(stderr,
54936227Sbostic "tar: cannot change back?: %s: %s\n",
55036227Sbostic parent, strerror(errno));
55115045Smckusick }
55212154Ssam break;
55312154Ssam
55412154Ssam case S_IFLNK:
55512154Ssam tomodes(&stbuf);
55612154Ssam if (strlen(longname) >= NAMSIZ) {
55713492Ssam fprintf(stderr, "tar: %s: file name too long\n",
55813492Ssam longname);
55912154Ssam return;
56012154Ssam }
56112154Ssam strcpy(dblock.dbuf.name, longname);
5626250Sroot if (stbuf.st_size + 1 >= NAMSIZ) {
56313492Ssam fprintf(stderr, "tar: %s: symbolic link too long\n",
56413492Ssam longname);
5656250Sroot return;
5666250Sroot }
5679601Ssam i = readlink(shortname, dblock.dbuf.linkname, NAMSIZ - 1);
5686250Sroot if (i < 0) {
56936227Sbostic fprintf(stderr,
57036227Sbostic "tar: can't read symbolic link %s: %s\n",
57136227Sbostic longname, strerror(errno));
5726250Sroot return;
5736250Sroot }
5746250Sroot dblock.dbuf.linkname[i] = '\0';
5756250Sroot dblock.dbuf.linkflag = '2';
57622688Slepreau if (vflag)
57722688Slepreau fprintf(vfile, "a %s symbolic link to %s\n",
57822688Slepreau longname, dblock.dbuf.linkname);
57932420Sbostic (void)sprintf(dblock.dbuf.size, "%11lo", 0L);
58032420Sbostic (void)sprintf(dblock.dbuf.chksum, "%6o", checksum());
58127445Slepreau (void) writetape((char *)&dblock);
58212154Ssam break;
5831119Sbill
58412154Ssam case S_IFREG:
58512154Ssam if ((infile = open(shortname, 0)) < 0) {
58636227Sbostic fprintf(stderr, "tar: %s: %s\n",
58736227Sbostic longname, strerror(errno));
5881119Sbill return;
5891119Sbill }
59012154Ssam tomodes(&stbuf);
59112154Ssam if (strlen(longname) >= NAMSIZ) {
59213492Ssam fprintf(stderr, "tar: %s: file name too long\n",
59313492Ssam longname);
59421457Skjd close(infile);
59512154Ssam return;
59612154Ssam }
59712154Ssam strcpy(dblock.dbuf.name, longname);
59812154Ssam if (stbuf.st_nlink > 1) {
59912154Ssam struct linkbuf *lp;
60012154Ssam int found = 0;
60112154Ssam
60212154Ssam for (lp = ihead; lp != NULL; lp = lp->nextp)
60312154Ssam if (lp->inum == stbuf.st_ino &&
60412154Ssam lp->devnum == stbuf.st_dev) {
60512154Ssam found++;
60612154Ssam break;
60712154Ssam }
60812154Ssam if (found) {
60912154Ssam strcpy(dblock.dbuf.linkname, lp->pathname);
61012154Ssam dblock.dbuf.linkflag = '1';
61132420Sbostic (void)sprintf(dblock.dbuf.chksum, "%6o", checksum());
61227445Slepreau (void) writetape( (char *) &dblock);
61322688Slepreau if (vflag)
61422688Slepreau fprintf(vfile, "a %s link to %s\n",
61522688Slepreau longname, lp->pathname);
61612154Ssam lp->count--;
61712154Ssam close(infile);
61812154Ssam return;
6191119Sbill }
62022688Slepreau lp = (struct linkbuf *) getmem(sizeof(*lp));
62122688Slepreau if (lp != NULL) {
62212154Ssam lp->nextp = ihead;
62312154Ssam ihead = lp;
62412154Ssam lp->inum = stbuf.st_ino;
62512154Ssam lp->devnum = stbuf.st_dev;
62612154Ssam lp->count = stbuf.st_nlink - 1;
62712154Ssam strcpy(lp->pathname, longname);
62812154Ssam }
6291119Sbill }
63021457Skjd blocks = (stbuf.st_size + (TBLOCK-1)) / TBLOCK;
63122688Slepreau if (vflag)
63222688Slepreau fprintf(vfile, "a %s %ld blocks\n", longname, blocks);
63332420Sbostic (void)sprintf(dblock.dbuf.chksum, "%6o", checksum());
63421457Skjd hint = writetape((char *)&dblock);
63521457Skjd maxread = max(stbuf.st_blksize, (nblock * TBLOCK));
63627445Slepreau if ((bigbuf = malloc((unsigned)maxread)) == 0) {
63721457Skjd maxread = TBLOCK;
63821457Skjd bigbuf = buf;
63921457Skjd }
6401119Sbill
64121457Skjd while ((i = read(infile, bigbuf, min((hint*TBLOCK), maxread))) > 0
64221457Skjd && blocks > 0) {
64321457Skjd register int nblks;
64421457Skjd
64521457Skjd nblks = ((i-1)/TBLOCK)+1;
64621457Skjd if (nblks > blocks)
64721457Skjd nblks = blocks;
64821457Skjd hint = writetbuf(bigbuf, nblks);
64921457Skjd blocks -= nblks;
65021457Skjd }
65121457Skjd close(infile);
65221457Skjd if (bigbuf != buf)
65321457Skjd free(bigbuf);
65427058Smckusick if (i < 0) {
65536227Sbostic fprintf(stderr, "tar: Read error on %s: %s\n",
65636227Sbostic longname, strerror(errno));
65727058Smckusick } else if (blocks != 0 || i != 0)
65813492Ssam fprintf(stderr, "tar: %s: file changed size\n",
65913492Ssam longname);
66012154Ssam while (--blocks >= 0)
66112154Ssam putempty();
66212154Ssam break;
66312154Ssam
66412154Ssam default:
66512154Ssam fprintf(stderr, "tar: %s is not a file. Not dumped\n",
66613492Ssam longname);
66712154Ssam break;
6681119Sbill }
6691119Sbill }
6701119Sbill
doxtract(argv)6711119Sbill doxtract(argv)
6726250Sroot char *argv[];
6731119Sbill {
6741119Sbill long blocks, bytes;
67527445Slepreau int ofile, i;
6761119Sbill
6771119Sbill for (;;) {
67827445Slepreau if ((i = wantit(argv)) == 0)
67927445Slepreau continue;
68027445Slepreau if (i == -1)
68127445Slepreau break; /* end of tape */
6821119Sbill if (checkw('x', dblock.dbuf.name) == 0) {
6831119Sbill passtape();
6841119Sbill continue;
6851119Sbill }
68612154Ssam if (Fflag) {
68712154Ssam char *s;
68812154Ssam
68912154Ssam if ((s = rindex(dblock.dbuf.name, '/')) == 0)
69012154Ssam s = dblock.dbuf.name;
69112154Ssam else
69212154Ssam s++;
69312154Ssam if (checkf(s, stbuf.st_mode, Fflag) == 0) {
69412154Ssam passtape();
69512154Ssam continue;
69612154Ssam }
69712154Ssam }
69822688Slepreau if (checkdir(dblock.dbuf.name)) { /* have a directory */
69922688Slepreau if (mflag == 0)
70022688Slepreau dodirtimes(&dblock);
7016250Sroot continue;
70222688Slepreau }
70322688Slepreau if (dblock.dbuf.linkflag == '2') { /* symlink */
70425250Sbloom /*
70525250Sbloom * only unlink non directories or empty
70625250Sbloom * directories
70725250Sbloom */
70825250Sbloom if (rmdir(dblock.dbuf.name) < 0) {
70925250Sbloom if (errno == ENOTDIR)
71025250Sbloom unlink(dblock.dbuf.name);
71125250Sbloom }
7126250Sroot if (symlink(dblock.dbuf.linkname, dblock.dbuf.name)<0) {
71336227Sbostic fprintf(stderr,
71436227Sbostic "tar: %s: symbolic link failed: %s\n",
71536227Sbostic dblock.dbuf.name, strerror(errno));
7166250Sroot continue;
7176250Sroot }
7186250Sroot if (vflag)
71921910Skjd fprintf(vfile, "x %s symbolic link to %s\n",
72013492Ssam dblock.dbuf.name, dblock.dbuf.linkname);
72122353Skjd #ifdef notdef
72222353Skjd /* ignore alien orders */
72322353Skjd chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid);
72422688Slepreau if (mflag == 0)
72522688Slepreau setimes(dblock.dbuf.name, stbuf.st_mtime);
72622353Skjd if (pflag)
72722353Skjd chmod(dblock.dbuf.name, stbuf.st_mode & 07777);
72822353Skjd #endif
7291119Sbill continue;
7306250Sroot }
73122688Slepreau if (dblock.dbuf.linkflag == '1') { /* regular link */
73225250Sbloom /*
73325250Sbloom * only unlink non directories or empty
73425250Sbloom * directories
73525250Sbloom */
73625250Sbloom if (rmdir(dblock.dbuf.name) < 0) {
73725250Sbloom if (errno == ENOTDIR)
73825250Sbloom unlink(dblock.dbuf.name);
73925250Sbloom }
7401119Sbill if (link(dblock.dbuf.linkname, dblock.dbuf.name) < 0) {
74136227Sbostic fprintf(stderr,
74236227Sbostic "tar: can't link %s to %s: %s\n",
74336227Sbostic dblock.dbuf.name, dblock.dbuf.linkname,
74436227Sbostic strerror(errno));
7451119Sbill continue;
7461119Sbill }
7471119Sbill if (vflag)
74822688Slepreau fprintf(vfile, "%s linked to %s\n",
74913492Ssam dblock.dbuf.name, dblock.dbuf.linkname);
7501119Sbill continue;
7511119Sbill }
7526250Sroot if ((ofile = creat(dblock.dbuf.name,stbuf.st_mode&0xfff)) < 0) {
75336227Sbostic fprintf(stderr, "tar: can't create %s: %s\n",
75436227Sbostic dblock.dbuf.name, strerror(errno));
7551119Sbill passtape();
7561119Sbill continue;
7571119Sbill }
75821457Skjd chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid);
7591119Sbill blocks = ((bytes = stbuf.st_size) + TBLOCK-1)/TBLOCK;
7601119Sbill if (vflag)
76122688Slepreau fprintf(vfile, "x %s, %ld bytes, %ld tape blocks\n",
76213492Ssam dblock.dbuf.name, bytes, blocks);
76321457Skjd for (; blocks > 0;) {
76421457Skjd register int nread;
76521457Skjd char *bufp;
76621457Skjd register int nwant;
76721457Skjd
76821457Skjd nwant = NBLOCK*TBLOCK;
76921457Skjd if (nwant > (blocks*TBLOCK))
77021457Skjd nwant = (blocks*TBLOCK);
77121457Skjd nread = readtbuf(&bufp, nwant);
77222688Slepreau if (write(ofile, bufp, (int)min(nread, bytes)) < 0) {
7736250Sroot fprintf(stderr,
77436227Sbostic "tar: %s: HELP - extract write error: %s\n",
77536227Sbostic dblock.dbuf.name, strerror(errno));
7766250Sroot done(2);
7776250Sroot }
77821457Skjd bytes -= nread;
77921457Skjd blocks -= (((nread-1)/TBLOCK)+1);
7801119Sbill }
7811119Sbill close(ofile);
78222688Slepreau if (mflag == 0)
78322688Slepreau setimes(dblock.dbuf.name, stbuf.st_mtime);
7841926Swnj if (pflag)
7856250Sroot chmod(dblock.dbuf.name, stbuf.st_mode & 07777);
7861119Sbill }
78722688Slepreau if (mflag == 0) {
78822688Slepreau dblock.dbuf.name[0] = '\0'; /* process the whole stack */
78922688Slepreau dodirtimes(&dblock);
79022688Slepreau }
7911119Sbill }
7921119Sbill
dotable(argv)79327445Slepreau dotable(argv)
79427445Slepreau char *argv[];
7951119Sbill {
79627445Slepreau register int i;
79727445Slepreau
7981119Sbill for (;;) {
79927445Slepreau if ((i = wantit(argv)) == 0)
80027445Slepreau continue;
80127445Slepreau if (i == -1)
80227445Slepreau break; /* end of tape */
8031119Sbill if (vflag)
8041119Sbill longt(&stbuf);
8051119Sbill printf("%s", dblock.dbuf.name);
8061119Sbill if (dblock.dbuf.linkflag == '1')
8071119Sbill printf(" linked to %s", dblock.dbuf.linkname);
8086250Sroot if (dblock.dbuf.linkflag == '2')
8096250Sroot printf(" symbolic link to %s", dblock.dbuf.linkname);
8101119Sbill printf("\n");
8111119Sbill passtape();
8121119Sbill }
8131119Sbill }
8141119Sbill
putempty()8151119Sbill putempty()
8161119Sbill {
8171119Sbill char buf[TBLOCK];
8181119Sbill
81913492Ssam bzero(buf, sizeof (buf));
82027445Slepreau (void) writetape(buf);
8211119Sbill }
8221119Sbill
longt(st)8231119Sbill longt(st)
8246250Sroot register struct stat *st;
8251119Sbill {
8261119Sbill register char *cp;
8271119Sbill char *ctime();
8281119Sbill
8291119Sbill pmode(st);
83036227Sbostic printf("%3u/%1u", st->st_uid, st->st_gid);
831*54198Sbostic printf("%7qd", st->st_size);
8321119Sbill cp = ctime(&st->st_mtime);
8331119Sbill printf(" %-12.12s %-4.4s ", cp+4, cp+20);
8341119Sbill }
8351119Sbill
8361119Sbill #define SUID 04000
8371119Sbill #define SGID 02000
8381119Sbill #define ROWN 0400
8391119Sbill #define WOWN 0200
8401119Sbill #define XOWN 0100
8411119Sbill #define RGRP 040
8421119Sbill #define WGRP 020
8431119Sbill #define XGRP 010
8441119Sbill #define ROTH 04
8451119Sbill #define WOTH 02
8461119Sbill #define XOTH 01
8471119Sbill #define STXT 01000
8481119Sbill int m1[] = { 1, ROWN, 'r', '-' };
8491119Sbill int m2[] = { 1, WOWN, 'w', '-' };
8501119Sbill int m3[] = { 2, SUID, 's', XOWN, 'x', '-' };
8511119Sbill int m4[] = { 1, RGRP, 'r', '-' };
8521119Sbill int m5[] = { 1, WGRP, 'w', '-' };
8531119Sbill int m6[] = { 2, SGID, 's', XGRP, 'x', '-' };
8541119Sbill int m7[] = { 1, ROTH, 'r', '-' };
8551119Sbill int m8[] = { 1, WOTH, 'w', '-' };
8561119Sbill int m9[] = { 2, STXT, 't', XOTH, 'x', '-' };
8571119Sbill
8581119Sbill int *m[] = { m1, m2, m3, m4, m5, m6, m7, m8, m9};
8591119Sbill
pmode(st)8601119Sbill pmode(st)
8616250Sroot register struct stat *st;
8621119Sbill {
8631119Sbill register int **mp;
8641119Sbill
8651119Sbill for (mp = &m[0]; mp < &m[9];)
86627058Smckusick selectbits(*mp++, st);
8671119Sbill }
8681119Sbill
selectbits(pairp,st)86927058Smckusick selectbits(pairp, st)
8706250Sroot int *pairp;
8716250Sroot struct stat *st;
8721119Sbill {
8731119Sbill register int n, *ap;
8741119Sbill
8751119Sbill ap = pairp;
8761119Sbill n = *ap++;
8771119Sbill while (--n>=0 && (st->st_mode&*ap++)==0)
8781119Sbill ap++;
87927445Slepreau putchar(*ap);
8801119Sbill }
8811119Sbill
88222688Slepreau /*
88327442Slepreau * Make all directories needed by `name'. If `name' is itself
88427442Slepreau * a directory on the tar tape (indicated by a trailing '/'),
88522688Slepreau * return 1; else 0.
88622688Slepreau */
checkdir(name)8871119Sbill checkdir(name)
8886250Sroot register char *name;
8891119Sbill {
8901119Sbill register char *cp;
8916250Sroot
89212154Ssam /*
89322688Slepreau * Quick check for existence of directory.
89412154Ssam */
89512154Ssam if ((cp = rindex(name, '/')) == 0)
89612154Ssam return (0);
89712154Ssam *cp = '\0';
89836227Sbostic if (access(name, F_OK) == 0) { /* already exists */
89912154Ssam *cp = '/';
90022688Slepreau return (cp[1] == '\0'); /* return (lastchar == '/') */
90112154Ssam }
90212154Ssam *cp = '/';
90312154Ssam
90412154Ssam /*
90512154Ssam * No luck, try to make all directories in path.
90612154Ssam */
9071119Sbill for (cp = name; *cp; cp++) {
9086250Sroot if (*cp != '/')
9096250Sroot continue;
9106250Sroot *cp = '\0';
91136227Sbostic if (access(name, F_OK) < 0) {
9129844Ssam if (mkdir(name, 0777) < 0) {
91336227Sbostic fprintf(stderr, "tar: mkdir: %s: %s\n",
91436227Sbostic name, strerror(errno));
91512154Ssam *cp = '/';
91612154Ssam return (0);
9171119Sbill }
91821457Skjd chown(name, stbuf.st_uid, stbuf.st_gid);
91927442Slepreau if (pflag && cp[1] == '\0') /* dir on the tape */
92027442Slepreau chmod(name, stbuf.st_mode & 07777);
9211119Sbill }
9226250Sroot *cp = '/';
9231119Sbill }
9246250Sroot return (cp[-1]=='/');
9251119Sbill }
9261119Sbill
92746656Sbostic void
onintr()9281119Sbill onintr()
9291119Sbill {
93027445Slepreau (void) signal(SIGINT, SIG_IGN);
9311119Sbill term++;
9321119Sbill }
9331119Sbill
93446656Sbostic void
onquit()9351119Sbill onquit()
9361119Sbill {
93727445Slepreau (void) signal(SIGQUIT, SIG_IGN);
9381119Sbill term++;
9391119Sbill }
9401119Sbill
94146656Sbostic void
onhup()9421119Sbill onhup()
9431119Sbill {
94427445Slepreau (void) signal(SIGHUP, SIG_IGN);
9451119Sbill term++;
9461119Sbill }
9471119Sbill
94822688Slepreau #ifdef notdef
94946656Sbostic void
onterm()9501119Sbill onterm()
9511119Sbill {
95227445Slepreau (void) signal(SIGTERM, SIG_IGN);
9531119Sbill term++;
9541119Sbill }
95522688Slepreau #endif
9561119Sbill
tomodes(sp)9571119Sbill tomodes(sp)
9581119Sbill register struct stat *sp;
9591119Sbill {
9601119Sbill register char *cp;
9611119Sbill
9621119Sbill for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++)
9631119Sbill *cp = '\0';
96432420Sbostic (void)sprintf(dblock.dbuf.mode, "%6o ", sp->st_mode & 07777);
96532420Sbostic (void)sprintf(dblock.dbuf.uid, "%6o ", sp->st_uid);
96632420Sbostic (void)sprintf(dblock.dbuf.gid, "%6o ", sp->st_gid);
967*54198Sbostic (void)sprintf(dblock.dbuf.size, "%11qo ", sp->st_size);
96832420Sbostic (void)sprintf(dblock.dbuf.mtime, "%11lo ", sp->st_mtime);
9691119Sbill }
9701119Sbill
checksum()9711119Sbill checksum()
9721119Sbill {
9731119Sbill register i;
9741119Sbill register char *cp;
9751119Sbill
9766250Sroot for (cp = dblock.dbuf.chksum;
9776250Sroot cp < &dblock.dbuf.chksum[sizeof(dblock.dbuf.chksum)]; cp++)
9781119Sbill *cp = ' ';
9791119Sbill i = 0;
9801119Sbill for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++)
9811119Sbill i += *cp;
9826250Sroot return (i);
9831119Sbill }
9841119Sbill
checkw(c,name)9851119Sbill checkw(c, name)
9866250Sroot char *name;
9871119Sbill {
9886250Sroot if (!wflag)
9896250Sroot return (1);
9906250Sroot printf("%c ", c);
9916250Sroot if (vflag)
9926250Sroot longt(&stbuf);
9936250Sroot printf("%s: ", name);
9946250Sroot return (response() == 'y');
9951119Sbill }
9961119Sbill
response()9971119Sbill response()
9981119Sbill {
9991119Sbill char c;
10001119Sbill
10011119Sbill c = getchar();
10021119Sbill if (c != '\n')
10036250Sroot while (getchar() != '\n')
10046250Sroot ;
10056250Sroot else
10066250Sroot c = 'n';
10076250Sroot return (c);
10081119Sbill }
10091119Sbill
checkf(name,mode,howmuch)101012154Ssam checkf(name, mode, howmuch)
101112154Ssam char *name;
101212154Ssam int mode, howmuch;
101312154Ssam {
101412154Ssam int l;
101512154Ssam
101621910Skjd if ((mode & S_IFMT) == S_IFDIR){
101721910Skjd if ((strcmp(name, "SCCS")==0) || (strcmp(name, "RCS")==0))
101821910Skjd return(0);
101921910Skjd return(1);
102021910Skjd }
102112154Ssam if ((l = strlen(name)) < 3)
102212154Ssam return (1);
102312154Ssam if (howmuch > 1 && name[l-2] == '.' && name[l-1] == 'o')
102412154Ssam return (0);
102512154Ssam if (strcmp(name, "core") == 0 ||
102612154Ssam strcmp(name, "errs") == 0 ||
102712154Ssam (howmuch > 1 && strcmp(name, "a.out") == 0))
102812154Ssam return (0);
102912154Ssam /* SHOULD CHECK IF IT IS EXECUTABLE */
103012154Ssam return (1);
103112154Ssam }
103212154Ssam
103322688Slepreau /* Is the current file a new file, or the newest one of the same name? */
checkupdate(arg)10341119Sbill checkupdate(arg)
10356250Sroot char *arg;
10361119Sbill {
10371119Sbill char name[100];
10386250Sroot long mtime;
10391119Sbill daddr_t seekp;
10401119Sbill daddr_t lookup();
10411119Sbill
10421119Sbill rewind(tfile);
10431119Sbill for (;;) {
10441119Sbill if ((seekp = lookup(arg)) < 0)
10456250Sroot return (1);
10461119Sbill fseek(tfile, seekp, 0);
10471119Sbill fscanf(tfile, "%s %lo", name, &mtime);
10486250Sroot return (stbuf.st_mtime > mtime);
10491119Sbill }
10501119Sbill }
10511119Sbill
done(n)10521119Sbill done(n)
10531119Sbill {
105421457Skjd unlink(tname);
10551119Sbill exit(n);
10561119Sbill }
10571119Sbill
105827445Slepreau /*
105927445Slepreau * Do we want the next entry on the tape, i.e. is it selected? If
106027445Slepreau * not, skip over the entire entry. Return -1 if reached end of tape.
106127445Slepreau */
wantit(argv)106227445Slepreau wantit(argv)
106327445Slepreau char *argv[];
106427445Slepreau {
106527445Slepreau register char **cp;
106627445Slepreau
106727445Slepreau getdir();
106827445Slepreau if (endtape())
106927445Slepreau return (-1);
107027445Slepreau if (*argv == 0)
107127445Slepreau return (1);
107227445Slepreau for (cp = argv; *cp; cp++)
107327445Slepreau if (prefix(*cp, dblock.dbuf.name))
107427445Slepreau return (1);
107527445Slepreau passtape();
107627445Slepreau return (0);
107727445Slepreau }
107827445Slepreau
107922688Slepreau /*
108022688Slepreau * Does s2 begin with the string s1, on a directory boundary?
108122688Slepreau */
prefix(s1,s2)10821119Sbill prefix(s1, s2)
10836250Sroot register char *s1, *s2;
10841119Sbill {
10851119Sbill while (*s1)
10861119Sbill if (*s1++ != *s2++)
10876250Sroot return (0);
10881119Sbill if (*s2)
10896250Sroot return (*s2 == '/');
10906250Sroot return (1);
10911119Sbill }
10921119Sbill
10931119Sbill #define N 200
10941119Sbill int njab;
10956250Sroot
10961119Sbill daddr_t
lookup(s)10971119Sbill lookup(s)
10986250Sroot char *s;
10991119Sbill {
11001119Sbill register i;
11011119Sbill daddr_t a;
11021119Sbill
11031119Sbill for(i=0; s[i]; i++)
11046250Sroot if (s[i] == ' ')
11051119Sbill break;
11061119Sbill a = bsrch(s, i, low, high);
11076250Sroot return (a);
11081119Sbill }
11091119Sbill
11101119Sbill daddr_t
bsrch(s,n,l,h)11111119Sbill bsrch(s, n, l, h)
11126250Sroot daddr_t l, h;
11136250Sroot char *s;
11141119Sbill {
11151119Sbill register i, j;
11161119Sbill char b[N];
11171119Sbill daddr_t m, m1;
11181119Sbill
11191119Sbill njab = 0;
11201119Sbill
11211119Sbill loop:
11226250Sroot if (l >= h)
112322688Slepreau return ((daddr_t) -1);
11241119Sbill m = l + (h-l)/2 - N/2;
11256250Sroot if (m < l)
11261119Sbill m = l;
11271119Sbill fseek(tfile, m, 0);
11281119Sbill fread(b, 1, N, tfile);
11291119Sbill njab++;
11301119Sbill for(i=0; i<N; i++) {
11316250Sroot if (b[i] == '\n')
11321119Sbill break;
11331119Sbill m++;
11341119Sbill }
11356250Sroot if (m >= h)
113622688Slepreau return ((daddr_t) -1);
11371119Sbill m1 = m;
11381119Sbill j = i;
11391119Sbill for(i++; i<N; i++) {
11401119Sbill m1++;
11416250Sroot if (b[i] == '\n')
11421119Sbill break;
11431119Sbill }
11441119Sbill i = cmp(b+j, s, n);
11456250Sroot if (i < 0) {
11461119Sbill h = m;
11471119Sbill goto loop;
11481119Sbill }
11496250Sroot if (i > 0) {
11501119Sbill l = m1;
11511119Sbill goto loop;
11521119Sbill }
11536250Sroot return (m);
11541119Sbill }
11551119Sbill
cmp(b,s,n)11561119Sbill cmp(b, s, n)
11576250Sroot char *b, *s;
11581119Sbill {
11591119Sbill register i;
11601119Sbill
11616250Sroot if (b[0] != '\n')
116221457Skjd exit(2);
11631119Sbill for(i=0; i<n; i++) {
11646250Sroot if (b[i+1] > s[i])
11656250Sroot return (-1);
11666250Sroot if (b[i+1] < s[i])
11676250Sroot return (1);
11681119Sbill }
11696250Sroot return (b[i+1] == ' '? 0 : -1);
11701119Sbill }
11711119Sbill
readtape(buffer)117222688Slepreau readtape(buffer)
11736250Sroot char *buffer;
11741119Sbill {
117521457Skjd char *bufp;
117621457Skjd
117722688Slepreau if (first == 0)
117822688Slepreau getbuf();
117927445Slepreau (void) readtbuf(&bufp, TBLOCK);
118021457Skjd bcopy(bufp, buffer, TBLOCK);
118121457Skjd return(TBLOCK);
118221457Skjd }
118321457Skjd
readtbuf(bufpp,size)118421457Skjd readtbuf(bufpp, size)
118521457Skjd char **bufpp;
118621457Skjd int size;
118721457Skjd {
11883457Swnj register int i;
11891119Sbill
11901119Sbill if (recno >= nblock || first == 0) {
119127445Slepreau if ((i = bread(mt, (char *)tbuf, TBLOCK*nblock)) < 0)
119227058Smckusick mterr("read", i, 3);
11931119Sbill if (first == 0) {
11941119Sbill if ((i % TBLOCK) != 0) {
119513492Ssam fprintf(stderr, "tar: tape blocksize error\n");
11961119Sbill done(3);
11971119Sbill }
11981119Sbill i /= TBLOCK;
11993457Swnj if (i != nblock) {
120013492Ssam fprintf(stderr, "tar: blocksize = %d\n", i);
12011119Sbill nblock = i;
12021119Sbill }
120322353Skjd first = 1;
12041119Sbill }
12051119Sbill recno = 0;
12061119Sbill }
120721457Skjd if (size > ((nblock-recno)*TBLOCK))
120821457Skjd size = (nblock-recno)*TBLOCK;
120921457Skjd *bufpp = (char *)&tbuf[recno];
121021457Skjd recno += (size/TBLOCK);
121121457Skjd return (size);
12121119Sbill }
12131119Sbill
writetbuf(buffer,n)121421457Skjd writetbuf(buffer, n)
121521457Skjd register char *buffer;
121621457Skjd register int n;
12171119Sbill {
121827058Smckusick int i;
121922688Slepreau
122022688Slepreau if (first == 0) {
122122353Skjd getbuf();
122222353Skjd first = 1;
122322353Skjd }
12241119Sbill if (recno >= nblock) {
122527445Slepreau i = write(mt, (char *)tbuf, TBLOCK*nblock);
122627058Smckusick if (i != TBLOCK*nblock)
122727058Smckusick mterr("write", i, 2);
12281119Sbill recno = 0;
12291119Sbill }
123021457Skjd
123121457Skjd /*
123221457Skjd * Special case: We have an empty tape buffer, and the
123321457Skjd * users data size is >= the tape block size: Avoid
123421457Skjd * the bcopy and dma direct to tape. BIG WIN. Add the
123521457Skjd * residual to the tape buffer.
123621457Skjd */
123721457Skjd while (recno == 0 && n >= nblock) {
123827058Smckusick i = write(mt, buffer, TBLOCK*nblock);
123927058Smckusick if (i != TBLOCK*nblock)
124027058Smckusick mterr("write", i, 2);
124121457Skjd n -= nblock;
124221457Skjd buffer += (nblock * TBLOCK);
12431119Sbill }
124421457Skjd
124521457Skjd while (n-- > 0) {
124621457Skjd bcopy(buffer, (char *)&tbuf[recno++], TBLOCK);
124721457Skjd buffer += TBLOCK;
124821457Skjd if (recno >= nblock) {
124927445Slepreau i = write(mt, (char *)tbuf, TBLOCK*nblock);
125027058Smckusick if (i != TBLOCK*nblock)
125127058Smckusick mterr("write", i, 2);
125221457Skjd recno = 0;
125321457Skjd }
125421457Skjd }
125521457Skjd
125621457Skjd /* Tell the user how much to write to get in sync */
125721457Skjd return (nblock - recno);
12581119Sbill }
12591119Sbill
backtape()12601119Sbill backtape()
12611119Sbill {
126227058Smckusick static int mtdev = 1;
12633457Swnj static struct mtop mtop = {MTBSR, 1};
126427058Smckusick struct mtget mtget;
126527058Smckusick
126627058Smckusick if (mtdev == 1)
126727445Slepreau mtdev = ioctl(mt, MTIOCGET, (char *)&mtget);
12683457Swnj if (mtdev == 0) {
126927445Slepreau if (ioctl(mt, MTIOCTOP, (char *)&mtop) < 0) {
127036227Sbostic fprintf(stderr, "tar: tape backspace error: %s\n",
127136227Sbostic strerror(errno));
12721119Sbill done(4);
12731119Sbill }
12743457Swnj } else
1275*54198Sbostic (void)lseek(mt, (off_t) -TBLOCK*nblock, 1);
12763457Swnj recno--;
12771119Sbill }
12781119Sbill
flushtape()12791119Sbill flushtape()
12801119Sbill {
128127058Smckusick int i;
128227058Smckusick
128327445Slepreau i = write(mt, (char *)tbuf, TBLOCK*nblock);
128427058Smckusick if (i != TBLOCK*nblock)
128527058Smckusick mterr("write", i, 2);
12861119Sbill }
12871119Sbill
mterr(operation,i,exitcode)128827058Smckusick mterr(operation, i, exitcode)
128927058Smckusick char *operation;
129027058Smckusick int i;
129127058Smckusick {
129236227Sbostic fprintf(stderr, "tar: tape %s error: %s\n",
129336227Sbostic operation, i < 0 ? strerror(errno) : "unexpected EOF");
129427058Smckusick done(exitcode);
129527058Smckusick }
129627058Smckusick
bread(fd,buf,size)12978737Smckusick bread(fd, buf, size)
12988737Smckusick int fd;
12998737Smckusick char *buf;
13008737Smckusick int size;
13018737Smckusick {
13028737Smckusick int count;
13038737Smckusick static int lastread = 0;
13048737Smckusick
130522688Slepreau if (!Bflag)
130622688Slepreau return (read(fd, buf, size));
130722353Skjd
13088737Smckusick for (count = 0; count < size; count += lastread) {
130924281Smckusick lastread = read(fd, buf, size - count);
131024281Smckusick if (lastread <= 0) {
13118737Smckusick if (count > 0)
13128737Smckusick return (count);
13138737Smckusick return (lastread);
13148737Smckusick }
13158737Smckusick buf += lastread;
13168737Smckusick }
13178737Smckusick return (count);
13188737Smckusick }
131910165Ssam
132010165Ssam char *
cwd()132146656Sbostic cwd()
132210165Ssam {
132346656Sbostic char *p;
132446656Sbostic
132546656Sbostic p = getcwd((char *)NULL, 0);
132646656Sbostic if (p == NULL) {
132746656Sbostic (void)fprintf(stderr, "tar: %s\n", strerror(errno));
132821457Skjd exit(1);
132910165Ssam }
133046656Sbostic return (p);
133110165Ssam }
133221910Skjd
getbuf()133321910Skjd getbuf()
133421910Skjd {
133522353Skjd
133627058Smckusick if (nblock == 0) {
133721910Skjd fstat(mt, &stbuf);
133821910Skjd if ((stbuf.st_mode & S_IFMT) == S_IFCHR)
133927058Smckusick nblock = NBLOCK;
134021910Skjd else {
134127058Smckusick nblock = stbuf.st_blksize / TBLOCK;
134227058Smckusick if (nblock == 0)
134327058Smckusick nblock = NBLOCK;
134421910Skjd }
134521910Skjd }
134627445Slepreau tbuf = (union hblock *)malloc((unsigned)nblock*TBLOCK);
134721910Skjd if (tbuf == NULL) {
134821910Skjd fprintf(stderr, "tar: blocksize %d too big, can't get memory\n",
134921910Skjd nblock);
135021910Skjd done(1);
135121910Skjd }
135221910Skjd }
135322353Skjd
135422688Slepreau /*
135527442Slepreau * Save this directory and its mtime on the stack, popping and setting
135627442Slepreau * the mtimes of any stacked dirs which aren't parents of this one.
135727442Slepreau * A null directory causes the entire stack to be unwound and set.
135822688Slepreau *
135927442Slepreau * Since all the elements of the directory "stack" share a common
136027442Slepreau * prefix, we can make do with one string. We keep only the current
136127442Slepreau * directory path, with an associated array of mtime's, one for each
136227442Slepreau * '/' in the path. A negative mtime means no mtime. The mtime's are
136327442Slepreau * offset by one (first index 1, not 0) because calling this with a null
136427442Slepreau * directory causes mtime[0] to be set.
136527442Slepreau *
136622688Slepreau * This stack algorithm is not guaranteed to work for tapes created
136722688Slepreau * with the 'r' option, but the vast majority of tapes with
136822688Slepreau * directories are not. This avoids saving every directory record on
136922688Slepreau * the tape and setting all the times at the end.
137022688Slepreau */
137122688Slepreau char dirstack[NAMSIZ];
137222688Slepreau #define NTIM (NAMSIZ/2+1) /* a/b/c/d/... */
137322688Slepreau time_t mtime[NTIM];
137422353Skjd
137522688Slepreau dodirtimes(hp)
137622688Slepreau union hblock *hp;
137722353Skjd {
137822688Slepreau register char *p = dirstack;
137922688Slepreau register char *q = hp->dbuf.name;
138022688Slepreau register int ndir = 0;
138122688Slepreau char *savp;
138222688Slepreau int savndir;
138322353Skjd
138422688Slepreau /* Find common prefix */
138530090Sbostic while (*p == *q && *p) {
138622688Slepreau if (*p++ == '/')
138722688Slepreau ++ndir;
138822688Slepreau q++;
138922688Slepreau }
139022353Skjd
139122688Slepreau savp = p;
139222688Slepreau savndir = ndir;
139322688Slepreau while (*p) {
139422688Slepreau /*
139522688Slepreau * Not a child: unwind the stack, setting the times.
139622688Slepreau * The order we do this doesn't matter, so we go "forward."
139722688Slepreau */
139822688Slepreau if (*p++ == '/')
139922688Slepreau if (mtime[++ndir] >= 0) {
140022688Slepreau *--p = '\0'; /* zap the slash */
140122688Slepreau setimes(dirstack, mtime[ndir]);
140222688Slepreau *p++ = '/';
140322688Slepreau }
140422688Slepreau }
140522688Slepreau p = savp;
140622688Slepreau ndir = savndir;
140722353Skjd
140822688Slepreau /* Push this one on the "stack" */
140922688Slepreau while (*p = *q++) /* append the rest of the new dir */
141022688Slepreau if (*p++ == '/')
141122688Slepreau mtime[++ndir] = -1;
141222688Slepreau mtime[ndir] = stbuf.st_mtime; /* overwrite the last one */
141322688Slepreau }
141422353Skjd
setimes(path,mt)141522688Slepreau setimes(path, mt)
141622688Slepreau char *path;
141722688Slepreau time_t mt;
141822688Slepreau {
141922688Slepreau struct timeval tv[2];
142022353Skjd
142122688Slepreau tv[0].tv_sec = time((time_t *) 0);
142222688Slepreau tv[1].tv_sec = mt;
142322688Slepreau tv[0].tv_usec = tv[1].tv_usec = 0;
142436227Sbostic if (utimes(path, tv) < 0)
142536227Sbostic fprintf(stderr, "tar: can't set time on %s: %s\n",
142636227Sbostic path, strerror(errno));
142722688Slepreau }
142822688Slepreau
142922688Slepreau char *
getmem(size)143022688Slepreau getmem(size)
143122688Slepreau {
143222688Slepreau char *p = malloc((unsigned) size);
143322688Slepreau
143422688Slepreau if (p == NULL && freemem) {
143522688Slepreau fprintf(stderr,
143622688Slepreau "tar: out of memory, link and directory modtime info lost\n");
143722688Slepreau freemem = 0;
143822353Skjd }
143922688Slepreau return (p);
144022353Skjd }
1441