-static char *sccsid = "@(#)tar.c 4.9 (Berkeley) 82/05/07";
+#ifndef lint
+static char *sccsid = "@(#)tar.c 4.18 (Berkeley) %G%";
+#endif
/*
* Tape Archival Program
#include <stdio.h>
#include <sys/param.h>
#include <sys/stat.h>
-#include <dir.h>
+#include <sys/dir.h>
+#include <sys/ioctl.h>
#include <sys/mtio.h>
+#include <sys/time.h>
#include <signal.h>
+#include <errno.h>
#define TBLOCK 512
#define NBLOCK 20
};
union hblock dblock;
-union hblock tbuf[NBLOCK];
+union hblock *tbuf;
struct linkbuf *ihead;
struct stat stbuf;
int cflag;
int mflag;
int fflag;
+int iflag;
int oflag;
int pflag;
int wflag;
int hflag;
+int Bflag;
+int Fflag;
int mt;
int term;
char *malloc();
char *sprintf();
char *strcat();
+char *rindex();
+char *getcwd();
+char *getwd();
main(argc, argv)
int argc;
switch(*cp) {
case 'f':
+ if (*argv == 0) {
+ fprintf(stderr,
+ "tar: tapefile must be specified with 'f' option\n");
+ usage();
+ }
usefile = *argv++;
fflag++;
break;
break;
case 'b':
- nblock = atoi(*argv++);
- if (nblock > NBLOCK || nblock <= 0) {
- fprintf(stderr, "Invalid blocksize. (Max %d)\n",
- NBLOCK);
+ if (*argv == 0) {
+ fprintf(stderr,
+ "tar: blocksize must be specified with 'b' option\n");
+ usage();
+ }
+ nblock = atoi(*argv);
+ if (nblock <= 0) {
+ fprintf(stderr,
+ "tar: invalid blocksize \"%s\"\n", *argv);
done(1);
}
+ argv++;
break;
case 'l':
hflag++;
break;
+ case 'i':
+ iflag++;
+ break;
+
+ case 'B':
+ Bflag++;
+ break;
+
+ case 'F':
+ Fflag++;
+ break;
+
default:
fprintf(stderr, "tar: %c: unknown option\n", *cp);
usage();
if (!rflag && !xflag && !tflag)
usage();
+ tbuf = (union hblock *)malloc(nblock*TBLOCK);
+ if (tbuf == NULL) {
+ fprintf(stderr, "tar: blocksize %d too big, can't get memory\n",
+ nblock);
+ done(1);
+ }
if (rflag) {
if (cflag && tfile != NULL)
usage();
if (strcmp(usefile, "-") == 0) {
if (cflag == 0) {
fprintf(stderr,
- "Can only create standard output archives\n");
+ "tar: can only create standard output archives\n");
done(1);
}
mt = dup(1);
usage()
{
fprintf(stderr,
-"tar: usage tar -{txru}[cvfblmh] [tapefile] [blocksize] file1 file2...\n");
+"tar: usage: tar -{txru}[cvfblmhopwBi] [tapefile] [blocksize] file1 file2...\n");
done(1);
}
char *argv[];
{
register char *cp, *cp2;
- char wdir[60];
+ char wdir[MAXPATHLEN], tempdir[MAXPATHLEN], *parent;
if (!cflag) {
getdir();
done(0);
getdir();
} while (!endtape());
+ backtape();
if (tfile != NULL) {
char buf[200];
}
}
- getwdir(wdir);
+ (void) getcwd(wdir);
while (*argv && ! term) {
cp2 = *argv;
if (!strcmp(cp2, "-C") && argv[1]) {
if (chdir(*argv) < 0)
perror(*argv);
else
- getwdir(wdir);
+ (void) getcwd(wdir);
argv++;
continue;
}
+ parent = wdir;
for (cp = *argv; *cp; cp++)
if (*cp == '/')
cp2 = cp;
if (cp2 != *argv) {
*cp2 = '\0';
- chdir(*argv);
+ if (chdir(*argv) < 0) {
+ perror(*argv);
+ continue;
+ }
+ parent = getcwd(tempdir);
*cp2 = '/';
cp2++;
}
- putfile(*argv++, cp2);
+ putfile(*argv++, cp2, parent);
chdir(wdir);
}
putempty();
for (; ihead != NULL; ihead = ihead->nextp) {
if (ihead->count == 0)
continue;
- fprintf(stderr, "Missing links to %s\n", ihead->pathname);
+ fprintf(stderr, "tar: missing links to %s\n", ihead->pathname);
}
}
endtape()
{
- if (dblock.dbuf.name[0] != '\0')
- return (0);
- backtape();
- return (1);
+ return (dblock.dbuf.name[0] == '\0');
}
getdir()
register struct stat *sp;
int i;
+top:
readtape((char *)&dblock);
if (dblock.dbuf.name[0] == '\0')
return;
sscanf(dblock.dbuf.size, "%lo", &sp->st_size);
sscanf(dblock.dbuf.mtime, "%lo", &sp->st_mtime);
sscanf(dblock.dbuf.chksum, "%o", &chksum);
- if (chksum != checksum()) {
- fprintf(stderr, "directory checksum error\n");
+ if (chksum != (i = checksum())) {
+ fprintf(stderr, "tar: directory checksum error (%d != %d)\n",
+ chksum, i);
+ if (iflag)
+ goto top;
done(2);
}
if (tfile != NULL)
readtape(buf);
}
-putfile(longname, shortname)
+putfile(longname, shortname, parent)
char *longname;
char *shortname;
+ char *parent;
{
- int infile;
+ int infile = 0;
long blocks;
char buf[TBLOCK];
register char *cp, *cp2;
struct direct *dp;
DIR *dirp;
int i, j;
+ char newparent[NAMSIZ+64];
+ extern int errno;
- infile = open(shortname, 0);
- if (infile < 0) {
- fprintf(stderr, "tar: %s: cannot open file\n", longname);
+ if (!hflag)
+ i = lstat(shortname, &stbuf);
+ else
+ i = stat(shortname, &stbuf);
+ if (i < 0) {
+ switch (errno) {
+ case EACCES:
+ fprintf(stderr, "tar: %s: cannot open file\n", longname);
+ break;
+ case ENOENT:
+ fprintf(stderr, "tar: %s: no such file or directory\n",
+ longname);
+ break;
+ default:
+ fprintf(stderr, "tar: %s: cannot stat file\n", longname);
+ break;
+ }
return;
}
- stat(shortname, &stbuf);
- if (tfile != NULL && checkupdate(longname) == 0) {
- close(infile);
+ if (tfile != NULL && checkupdate(longname) == 0)
return;
- }
- if (checkw('r', longname) == 0) {
- close(infile);
+ if (checkw('r', longname) == 0)
+ return;
+ if (Fflag && checkf(shortname, stbuf.st_mode, Fflag) == 0)
return;
- }
- if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
+ switch (stbuf.st_mode & S_IFMT) {
+ case S_IFDIR:
for (i = 0, cp = buf; *cp++ = longname[i++];)
;
*--cp = '/';
*++cp = 0 ;
if (!oflag) {
if ((cp - buf) >= NAMSIZ) {
- fprintf(stderr, "%s: file name too long\n",
- longname);
- close(infile);
+ fprintf(stderr, "tar: %s: file name too long\n",
+ longname);
return;
}
stbuf.st_size = 0;
sprintf(dblock.dbuf.chksum, "%6o", checksum());
writetape((char *)&dblock);
}
+ sprintf(newparent, "%s/%s", parent, shortname);
chdir(shortname);
- close(infile);
if ((dirp = opendir(".")) == NULL) {
- fprintf(stderr, "%s: directory read error\n", longname);
+ fprintf(stderr, "tar: %s: directory read error\n",
+ longname);
+ chdir(parent);
return;
}
while ((dp = readdir(dirp)) != NULL && !term) {
strcpy(cp, dp->d_name);
i = telldir(dirp);
closedir(dirp);
- putfile(buf, cp);
+ putfile(buf, cp, newparent);
dirp = opendir(".");
seekdir(dirp, i);
}
closedir(dirp);
- chdir("..");
- return;
- }
- i = stbuf.st_mode & S_IFMT;
- if (i != S_IFREG && i != S_IFLNK) {
- fprintf(stderr, "tar: %s is not a file. Not dumped\n",
- longname);
- return;
- }
- tomodes(&stbuf);
- cp2 = longname; cp = dblock.dbuf.name; i = 0;
- while ((*cp++ = *cp2++) && i < NAMSIZ)
- i++;
- if (i >= NAMSIZ) {
- fprintf(stderr, "%s: file name too long\n", longname);
- close(infile);
- return;
- }
- if ((stbuf.st_mode & S_IFMT) == S_IFLNK) {
+ chdir(parent);
+ break;
+
+ case S_IFLNK:
+ tomodes(&stbuf);
+ if (strlen(longname) >= NAMSIZ) {
+ fprintf(stderr, "tar: %s: file name too long\n",
+ longname);
+ return;
+ }
+ strcpy(dblock.dbuf.name, longname);
if (stbuf.st_size + 1 >= NAMSIZ) {
- fprintf(stderr, "%s: symbolic link too long\n",
- longname);
- close(infile);
+ fprintf(stderr, "tar: %s: symbolic link too long\n",
+ longname);
return;
}
- i = readlink(longname, dblock.dbuf.linkname, NAMSIZ - 1);
+ i = readlink(shortname, dblock.dbuf.linkname, NAMSIZ - 1);
if (i < 0) {
- perror("readlink");
- close(infile);
+ perror(longname);
return;
}
dblock.dbuf.linkname[i] = '\0';
if (vflag) {
fprintf(stderr, "a %s ", longname);
fprintf(stderr, "symbolic link to %s\n",
- dblock.dbuf.linkname);
+ dblock.dbuf.linkname);
}
sprintf(dblock.dbuf.size, "%11lo", 0);
sprintf(dblock.dbuf.chksum, "%6o", checksum());
writetape((char *)&dblock);
- close(infile);
- return;
- }
- if (stbuf.st_nlink > 1) {
- struct linkbuf *lp;
- int found = 0;
-
- for (lp = ihead; lp != NULL; lp = lp->nextp)
- if (lp->inum == stbuf.st_ino &&
- lp->devnum == stbuf.st_dev) {
- found++;
- break;
- }
- if (found) {
- strcpy(dblock.dbuf.linkname, lp->pathname);
- dblock.dbuf.linkflag = '1';
- sprintf(dblock.dbuf.chksum, "%6o", checksum());
- writetape( (char *) &dblock);
- if (vflag) {
- fprintf(stderr, "a %s ", longname);
- fprintf(stderr, "link to %s\n", lp->pathname);
- }
- lp->count--;
- close(infile);
+ break;
+
+ case S_IFREG:
+ if ((infile = open(shortname, 0)) < 0) {
+ fprintf(stderr, "tar: %s: cannot open file\n", longname);
return;
}
- lp = (struct linkbuf *) malloc(sizeof(*lp));
- if (lp == NULL) {
- if (freemem) {
- fprintf(stderr,
- "Out of memory. Link information lost\n");
- freemem = 0;
+ tomodes(&stbuf);
+ if (strlen(longname) >= NAMSIZ) {
+ fprintf(stderr, "tar: %s: file name too long\n",
+ longname);
+ return;
+ }
+ strcpy(dblock.dbuf.name, longname);
+ if (stbuf.st_nlink > 1) {
+ struct linkbuf *lp;
+ int found = 0;
+
+ for (lp = ihead; lp != NULL; lp = lp->nextp)
+ if (lp->inum == stbuf.st_ino &&
+ lp->devnum == stbuf.st_dev) {
+ found++;
+ break;
+ }
+ if (found) {
+ strcpy(dblock.dbuf.linkname, lp->pathname);
+ dblock.dbuf.linkflag = '1';
+ sprintf(dblock.dbuf.chksum, "%6o", checksum());
+ writetape( (char *) &dblock);
+ if (vflag) {
+ fprintf(stderr, "a %s ", longname);
+ fprintf(stderr, "link to %s\n",
+ lp->pathname);
+ }
+ lp->count--;
+ close(infile);
+ return;
+ }
+ lp = (struct linkbuf *) malloc(sizeof(*lp));
+ if (lp == NULL) {
+ if (freemem) {
+ fprintf(stderr,
+ "tar: out of memory, link information lost\n");
+ freemem = 0;
+ }
+ } else {
+ lp->nextp = ihead;
+ ihead = lp;
+ lp->inum = stbuf.st_ino;
+ lp->devnum = stbuf.st_dev;
+ lp->count = stbuf.st_nlink - 1;
+ strcpy(lp->pathname, longname);
}
- } else {
- lp->nextp = ihead;
- ihead = lp;
- lp->inum = stbuf.st_ino;
- lp->devnum = stbuf.st_dev;
- lp->count = stbuf.st_nlink - 1;
- strcpy(lp->pathname, longname);
}
- }
- blocks = (stbuf.st_size + (TBLOCK-1)) / TBLOCK;
- if (vflag) {
- fprintf(stderr, "a %s ", longname);
- fprintf(stderr, "%ld blocks\n", blocks);
- }
- sprintf(dblock.dbuf.chksum, "%6o", checksum());
- writetape((char *)&dblock);
+ blocks = (stbuf.st_size + (TBLOCK-1)) / TBLOCK;
+ if (vflag) {
+ fprintf(stderr, "a %s ", longname);
+ fprintf(stderr, "%ld blocks\n", blocks);
+ }
+ sprintf(dblock.dbuf.chksum, "%6o", checksum());
+ writetape((char *)&dblock);
- while ((i = read(infile, buf, TBLOCK)) > 0 && blocks > 0) {
- writetape(buf);
- blocks--;
+ while ((i = read(infile, buf, TBLOCK)) > 0 && blocks > 0) {
+ writetape(buf);
+ blocks--;
+ }
+ close(infile);
+ if (blocks != 0 || i != 0)
+ fprintf(stderr, "tar: %s: file changed size\n",
+ longname);
+ while (--blocks >= 0)
+ putempty();
+ break;
+
+ default:
+ fprintf(stderr, "tar: %s is not a file. Not dumped\n",
+ longname);
+ break;
}
- close(infile);
- if (blocks != 0 || i != 0)
- fprintf(stderr, "%s: file changed size\n", longname);
- while (--blocks >= 0)
- putempty();
}
doxtract(argv)
passtape();
continue;
}
+ if (Fflag) {
+ char *s;
+
+ if ((s = rindex(dblock.dbuf.name, '/')) == 0)
+ s = dblock.dbuf.name;
+ else
+ s++;
+ if (checkf(s, stbuf.st_mode, Fflag) == 0) {
+ passtape();
+ continue;
+ }
+ }
if (checkdir(dblock.dbuf.name))
continue;
if (dblock.dbuf.linkflag == '2') {
unlink(dblock.dbuf.name);
if (symlink(dblock.dbuf.linkname, dblock.dbuf.name)<0) {
- fprintf(stderr, "%s: symbolic link failed\n",
- dblock.dbuf.name);
+ fprintf(stderr, "tar: %s: symbolic link failed\n",
+ dblock.dbuf.name);
continue;
}
if (vflag)
fprintf(stderr, "x %s symbolic link to %s\n",
- dblock.dbuf.name, dblock.dbuf.linkname);
+ dblock.dbuf.name, dblock.dbuf.linkname);
+#ifdef notdef
+ /* ignore alien orders */
chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid);
if (mflag == 0) {
- time_t timep[2];
+ struct timeval tv[2];
- timep[0] = time(0);
- timep[1] = stbuf.st_mtime;
- utime(dblock.dbuf.name, timep);
+ tv[0].tv_sec = time(0);
+ tv[0].tv_usec = 0;
+ tv[1].tv_sec = stbuf.st_mtime;
+ tv[1].tv_usec = 0;
+ utimes(dblock.dbuf.name, tv);
}
if (pflag)
chmod(dblock.dbuf.name, stbuf.st_mode & 07777);
+#endif
continue;
}
if (dblock.dbuf.linkflag == '1') {
unlink(dblock.dbuf.name);
if (link(dblock.dbuf.linkname, dblock.dbuf.name) < 0) {
- fprintf(stderr, "%s: cannot link\n",
- dblock.dbuf.name);
+ fprintf(stderr, "tar: %s: cannot link\n",
+ dblock.dbuf.name);
continue;
}
if (vflag)
fprintf(stderr, "%s linked to %s\n",
- dblock.dbuf.name, dblock.dbuf.linkname);
+ dblock.dbuf.name, dblock.dbuf.linkname);
continue;
}
if ((ofile = creat(dblock.dbuf.name,stbuf.st_mode&0xfff)) < 0) {
fprintf(stderr, "tar: %s - cannot create\n",
- dblock.dbuf.name);
+ dblock.dbuf.name);
passtape();
continue;
}
blocks = ((bytes = stbuf.st_size) + TBLOCK-1)/TBLOCK;
if (vflag)
fprintf(stderr, "x %s, %ld bytes, %ld tape blocks\n",
- dblock.dbuf.name, bytes, blocks);
+ dblock.dbuf.name, bytes, blocks);
for (; blocks-- > 0; bytes -= TBLOCK) {
readtape(buf);
if (bytes > TBLOCK) {
if (write(ofile, buf, TBLOCK) < 0) {
fprintf(stderr,
"tar: %s: HELP - extract write error\n",
- dblock.dbuf.name);
+ dblock.dbuf.name);
done(2);
}
continue;
}
if (write(ofile, buf, (int) bytes) < 0) {
fprintf(stderr,
- "tar: %s: HELP - extract write error\n",
- dblock.dbuf.name);
+ "tar: %s: HELP - extract write error\n",
+ dblock.dbuf.name);
done(2);
}
}
close(ofile);
if (mflag == 0) {
- time_t timep[2];
+ struct timeval tv[2];
- timep[0] = time(NULL);
- timep[1] = stbuf.st_mtime;
- utime(dblock.dbuf.name, timep);
+ tv[0].tv_sec = time(0);
+ tv[0].tv_usec = 0;
+ tv[1].tv_sec = stbuf.st_mtime;
+ tv[1].tv_usec = 0;
+ utimes(dblock.dbuf.name, tv);
}
if (pflag)
chmod(dblock.dbuf.name, stbuf.st_mode & 07777);
putempty()
{
char buf[TBLOCK];
- char *cp;
- for (cp = buf; cp < &buf[TBLOCK]; )
- *cp++ = '\0';
+ bzero(buf, sizeof (buf));
writetape(buf);
}
{
register char *cp;
+ /*
+ * Quick check for existance of directory.
+ */
+ if ((cp = rindex(name, '/')) == 0)
+ return (0);
+ *cp = '\0';
+ if (access(name, 0) >= 0) {
+ *cp = '/';
+ return (cp[1] == '\0');
+ }
+ *cp = '/';
+
+ /*
+ * No luck, try to make all directories in path.
+ */
for (cp = name; *cp; cp++) {
if (*cp != '/')
continue;
*cp = '\0';
- if (access(name, 1) < 0) {
- register int pid, rp;
- int i;
-
- if ((pid = fork()) == 0) {
- execl("/bin/mkdir", "mkdir", name, 0);
- execl("/usr/bin/mkdir", "mkdir", name, 0);
- fprintf(stderr, "tar: cannot find mkdir!\n");
- done(0);
+ if (access(name, 0) < 0) {
+ if (mkdir(name, 0777) < 0) {
+ perror(name);
+ *cp = '/';
+ return (0);
}
- while ((rp = wait(&i)) >= 0 && rp != pid)
- ;
chown(name, stbuf.st_uid, stbuf.st_gid);
- if (pflag)
- chmod(dblock.dbuf.name, stbuf.st_mode & 0777);
+ if (pflag && cp[1] == '\0')
+ chmod(name, stbuf.st_mode & 0777);
}
*cp = '/';
}
return (c);
}
+checkf(name, mode, howmuch)
+ char *name;
+ int mode, howmuch;
+{
+ int l;
+
+ if ((mode & S_IFMT) == S_IFDIR)
+ return (strcmp(name, "SCCS") != 0);
+ if ((l = strlen(name)) < 3)
+ return (1);
+ if (howmuch > 1 && name[l-2] == '.' && name[l-1] == 'o')
+ return (0);
+ if (strcmp(name, "core") == 0 ||
+ strcmp(name, "errs") == 0 ||
+ (howmuch > 1 && strcmp(name, "a.out") == 0))
+ return (0);
+ /* SHOULD CHECK IF IT IS EXECUTABLE */
+ return (1);
+}
+
checkupdate(arg)
char *arg;
{
return (1);
}
-getwdir(s)
- char *s;
-{
- int i, pipdes[2];
-
- pipe(pipdes);
- if ((i = fork()) == 0) {
- close(1);
- dup(pipdes[1]);
- execl("/bin/pwd", "pwd", 0);
- execl("/usr/bin/pwd", "pwd", 0);
- fprintf(stderr, "pwd failed!\n");
- printf("/\n");
- exit(1);
- }
- while (wait((int *)NULL) != -1)
- ;
- read(pipdes[0], s, 50);
- while (*s != '\n')
- s++;
- *s = '\0';
- close(pipdes[0]);
- close(pipdes[1]);
-}
-
#define N 200
int njab;
register int i;
if (recno >= nblock || first == 0) {
- if ((i = read(mt, tbuf, TBLOCK*nblock)) < 0) {
- fprintf(stderr, "Tar: tape read error\n");
+ if ((i = bread(mt, tbuf, TBLOCK*nblock)) < 0) {
+ fprintf(stderr, "tar: tape read error\n");
done(3);
}
if (first == 0) {
if ((i % TBLOCK) != 0) {
- fprintf(stderr, "Tar: tape blocksize error\n");
+ fprintf(stderr, "tar: tape blocksize error\n");
done(3);
}
i /= TBLOCK;
if (i != nblock) {
- fprintf(stderr, "Tar: blocksize = %d\n", i);
+ fprintf(stderr, "tar: blocksize = %d\n", i);
nblock = i;
}
}
recno = 0;
}
first = 1;
- copy(buffer, &tbuf[recno++]);
+ bcopy((char *)&tbuf[recno++], buffer, TBLOCK);
return (TBLOCK);
}
first = 1;
if (recno >= nblock) {
if (write(mt, tbuf, TBLOCK*nblock) < 0) {
- fprintf(stderr, "Tar: tape write error\n");
+ fprintf(stderr, "tar: tape write error\n");
done(2);
}
recno = 0;
}
- copy(&tbuf[recno++], buffer);
+ bcopy(buffer, (char *)&tbuf[recno++], TBLOCK);
if (recno >= nblock) {
if (write(mt, tbuf, TBLOCK*nblock) < 0) {
- fprintf(stderr, "Tar: tape write error\n");
+ fprintf(stderr, "tar: tape write error\n");
done(2);
}
recno = 0;
mtdev = ioctl(mt, MTIOCGET, &mtget);
if (mtdev == 0) {
if (ioctl(mt, MTIOCTOP, &mtop) < 0) {
- fprintf(stderr, "Tar: tape backspace error\n");
+ fprintf(stderr, "tar: tape backspace error\n");
done(4);
}
} else
write(mt, tbuf, TBLOCK*nblock);
}
-copy(to, from)
- register char *to, *from;
+bread(fd, buf, size)
+ int fd;
+ char *buf;
+ int size;
+{
+ int count;
+ static int lastread = 0;
+
+ if (!Bflag)
+ return (read(fd, buf, size));
+ for (count = 0; count < size; count += lastread) {
+ if (lastread < 0) {
+ if (count > 0)
+ return (count);
+ return (lastread);
+ }
+ lastread = read(fd, buf, size - count);
+ buf += lastread;
+ }
+ return (count);
+}
+
+char *
+getcwd(buf)
+ char *buf;
{
- register i;
- i = TBLOCK;
- do {
- *to++ = *from++;
- } while (--i);
+ if (getwd(buf) == NULL) {
+ fprintf(stderr, "tar: %s\n", buf);
+ exit(1);
+ }
+ return (buf);
}