fix for close bug in tablet driver (from dagobah!bill)
[unix-history] / usr / src / old / tar / tar.c
index 3f1b60a..5481408 100644 (file)
@@ -1,4 +1,6 @@
-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
 
 /*
  * Tape Archival Program
@@ -6,9 +8,12 @@ static char *sccsid = "@(#)tar.c       4.9 (Berkeley) 82/05/07";
 #include <stdio.h>
 #include <sys/param.h>
 #include <sys/stat.h>
 #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/mtio.h>
+#include <sys/time.h>
 #include <signal.h>
 #include <signal.h>
+#include <errno.h>
 
 #define TBLOCK 512
 #define NBLOCK 20
 
 #define TBLOCK 512
 #define NBLOCK 20
@@ -38,7 +43,7 @@ struct linkbuf {
 };
 
 union  hblock dblock;
 };
 
 union  hblock dblock;
-union  hblock tbuf[NBLOCK];
+union  hblock *tbuf;
 struct linkbuf *ihead;
 struct stat stbuf;
 
 struct linkbuf *ihead;
 struct stat stbuf;
 
@@ -49,10 +54,13 @@ int tflag;
 int    cflag;
 int    mflag;
 int    fflag;
 int    cflag;
 int    mflag;
 int    fflag;
+int    iflag;
 int    oflag;
 int    pflag;
 int    wflag;
 int    hflag;
 int    oflag;
 int    pflag;
 int    wflag;
 int    hflag;
+int    Bflag;
+int    Fflag;
 
 int    mt;
 int    term;
 
 int    mt;
 int    term;
@@ -78,6 +86,9 @@ char  magtape[] = "/dev/rmt8";
 char   *malloc();
 char   *sprintf();
 char   *strcat();
 char   *malloc();
 char   *sprintf();
 char   *strcat();
+char   *rindex();
+char   *getcwd();
+char   *getwd();
 
 main(argc, argv)
 int    argc;
 
 main(argc, argv)
 int    argc;
@@ -96,6 +107,11 @@ char        *argv[];
                switch(*cp) {
 
                case 'f':
                switch(*cp) {
 
                case 'f':
+                       if (*argv == 0) {
+                               fprintf(stderr,
+                       "tar: tapefile must be specified with 'f' option\n");
+                               usage();
+                       }
                        usefile = *argv++;
                        fflag++;
                        break;
                        usefile = *argv++;
                        fflag++;
                        break;
@@ -162,12 +178,18 @@ char      *argv[];
                        break;
 
                case 'b':
                        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);
                        }
                                done(1);
                        }
+                       argv++;
                        break;
 
                case 'l':
                        break;
 
                case 'l':
@@ -178,6 +200,18 @@ char       *argv[];
                        hflag++;
                        break;
 
                        hflag++;
                        break;
 
+               case 'i':
+                       iflag++;
+                       break;
+
+               case 'B':
+                       Bflag++;
+                       break;
+
+               case 'F':
+                       Fflag++;
+                       break;
+
                default:
                        fprintf(stderr, "tar: %c: unknown option\n", *cp);
                        usage();
                default:
                        fprintf(stderr, "tar: %c: unknown option\n", *cp);
                        usage();
@@ -185,6 +219,12 @@ char       *argv[];
 
        if (!rflag && !xflag && !tflag)
                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 (rflag) {
                if (cflag && tfile != NULL)
                        usage();
@@ -201,7 +241,7 @@ char        *argv[];
                if (strcmp(usefile, "-") == 0) {
                        if (cflag == 0) {
                                fprintf(stderr,
                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);
                                done(1);
                        }
                        mt = dup(1);
@@ -233,7 +273,7 @@ char        *argv[];
 usage()
 {
        fprintf(stderr,
 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);
 }
 
        done(1);
 }
 
@@ -241,7 +281,7 @@ dorep(argv)
        char *argv[];
 {
        register char *cp, *cp2;
        char *argv[];
 {
        register char *cp, *cp2;
-       char wdir[60];
+       char wdir[MAXPATHLEN], tempdir[MAXPATHLEN], *parent;
 
        if (!cflag) {
                getdir();
 
        if (!cflag) {
                getdir();
@@ -251,6 +291,7 @@ dorep(argv)
                                done(0);
                        getdir();
                } while (!endtape());
                                done(0);
                        getdir();
                } while (!endtape());
+               backtape();
                if (tfile != NULL) {
                        char buf[200];
 
                if (tfile != NULL) {
                        char buf[200];
 
@@ -265,7 +306,7 @@ dorep(argv)
                }
        }
 
                }
        }
 
-       getwdir(wdir);
+       (void) getcwd(wdir);
        while (*argv && ! term) {
                cp2 = *argv;
                if (!strcmp(cp2, "-C") && argv[1]) {
        while (*argv && ! term) {
                cp2 = *argv;
                if (!strcmp(cp2, "-C") && argv[1]) {
@@ -273,20 +314,25 @@ dorep(argv)
                        if (chdir(*argv) < 0)
                                perror(*argv);
                        else
                        if (chdir(*argv) < 0)
                                perror(*argv);
                        else
-                               getwdir(wdir);
+                               (void) getcwd(wdir);
                        argv++;
                        continue;
                }
                        argv++;
                        continue;
                }
+               parent = wdir;
                for (cp = *argv; *cp; cp++)
                        if (*cp == '/')
                                cp2 = cp;
                if (cp2 != *argv) {
                        *cp2 = '\0';
                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++;
                }
                        *cp2 = '/';
                        cp2++;
                }
-               putfile(*argv++, cp2);
+               putfile(*argv++, cp2, parent);
                chdir(wdir);
        }
        putempty();
                chdir(wdir);
        }
        putempty();
@@ -297,16 +343,13 @@ dorep(argv)
        for (; ihead != NULL; ihead = ihead->nextp) {
                if (ihead->count == 0)
                        continue;
        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()
 {
        }
 }
 
 endtape()
 {
-       if (dblock.dbuf.name[0] != '\0')
-               return (0);
-       backtape();
-       return (1);
+       return (dblock.dbuf.name[0] == '\0');
 }
 
 getdir()
 }
 
 getdir()
@@ -314,6 +357,7 @@ getdir()
        register struct stat *sp;
        int i;
 
        register struct stat *sp;
        int i;
 
+top:
        readtape((char *)&dblock);
        if (dblock.dbuf.name[0] == '\0')
                return;
        readtape((char *)&dblock);
        if (dblock.dbuf.name[0] == '\0')
                return;
@@ -327,8 +371,11 @@ getdir()
        sscanf(dblock.dbuf.size, "%lo", &sp->st_size);
        sscanf(dblock.dbuf.mtime, "%lo", &sp->st_mtime);
        sscanf(dblock.dbuf.chksum, "%o", &chksum);
        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)
                done(2);
        }
        if (tfile != NULL)
@@ -350,43 +397,57 @@ passtape()
                readtape(buf);
 }
 
                readtape(buf);
 }
 
-putfile(longname, shortname)
+putfile(longname, shortname, parent)
        char *longname;
        char *shortname;
        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;
        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;
        }
                return;
        }
-       stat(shortname, &stbuf);
-       if (tfile != NULL && checkupdate(longname) == 0) {
-               close(infile);
+       if (tfile != NULL && checkupdate(longname) == 0)
                return;
                return;
-       }
-       if (checkw('r', longname) == 0) {
-               close(infile);
+       if (checkw('r', longname) == 0)
+               return;
+       if (Fflag && checkf(shortname, stbuf.st_mode, Fflag) == 0)
                return;
                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) {
                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;
                                return;
                        }
                        stbuf.st_size = 0;
@@ -395,10 +456,12 @@ putfile(longname, shortname)
                        sprintf(dblock.dbuf.chksum, "%6o", checksum());
                        writetape((char *)&dblock);
                }
                        sprintf(dblock.dbuf.chksum, "%6o", checksum());
                        writetape((char *)&dblock);
                }
+               sprintf(newparent, "%s/%s", parent, shortname);
                chdir(shortname);
                chdir(shortname);
-               close(infile);
                if ((dirp = opendir(".")) == NULL) {
                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) {
                        return;
                }
                while ((dp = readdir(dirp)) != NULL && !term) {
@@ -410,40 +473,30 @@ putfile(longname, shortname)
                        strcpy(cp, dp->d_name);
                        i = telldir(dirp);
                        closedir(dirp);
                        strcpy(cp, dp->d_name);
                        i = telldir(dirp);
                        closedir(dirp);
-                       putfile(buf, cp);
+                       putfile(buf, cp, newparent);
                        dirp = opendir(".");
                        seekdir(dirp, i);
                }
                closedir(dirp);
                        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) {
                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;
                }
                        return;
                }
-               i = readlink(longname, dblock.dbuf.linkname, NAMSIZ - 1);
+               i = readlink(shortname, dblock.dbuf.linkname, NAMSIZ - 1);
                if (i < 0) {
                if (i < 0) {
-                       perror("readlink");
-                       close(infile);
+                       perror(longname);
                        return;
                }
                dblock.dbuf.linkname[i] = '\0';
                        return;
                }
                dblock.dbuf.linkname[i] = '\0';
@@ -451,70 +504,90 @@ putfile(longname, shortname)
                if (vflag) {
                        fprintf(stderr, "a %s ", longname);
                        fprintf(stderr, "symbolic link to %s\n",
                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);
                }
                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;
                }
                        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)
 }
 
 doxtract(argv)
@@ -542,45 +615,62 @@ gotit:
                        passtape();
                        continue;
                }
                        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) {
                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",
                                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) {
                        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);
                        }
                        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) {
                        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",
                                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",
                        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;
                }
                        passtape();
                        continue;
                }
@@ -588,32 +678,34 @@ gotit:
                blocks = ((bytes = stbuf.st_size) + TBLOCK-1)/TBLOCK;
                if (vflag)
                        fprintf(stderr, "x %s, %ld bytes, %ld tape blocks\n",
                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",
                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,
                                        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) {
                                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);
                }
                if (pflag)
                        chmod(dblock.dbuf.name, stbuf.st_mode & 07777);
@@ -641,10 +733,8 @@ dotable()
 putempty()
 {
        char buf[TBLOCK];
 putempty()
 {
        char buf[TBLOCK];
-       char *cp;
 
 
-       for (cp = buf; cp < &buf[TBLOCK]; )
-               *cp++ = '\0';
+       bzero(buf, sizeof (buf));
        writetape(buf);
 }
 
        writetape(buf);
 }
 
@@ -712,25 +802,34 @@ checkdir(name)
 {
        register char *cp;
 
 {
        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';
        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);
                        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 = '/';
        }
                }
                *cp = '/';
        }
@@ -814,6 +913,26 @@ response()
        return (c);
 }
 
        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;
 {
 checkupdate(arg)
        char *arg;
 {
@@ -849,31 +968,6 @@ prefix(s1, s2)
        return (1);
 }
 
        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;
 
 #define        N       200
 int    njab;
 
@@ -959,25 +1053,25 @@ readtape(buffer)
        register int i;
 
        if (recno >= nblock || first == 0) {
        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) {
                        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) {
                                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;
                                nblock = i;
                        }
                }
                recno = 0;
        }
        first = 1;
-       copy(buffer, &tbuf[recno++]);
+       bcopy((char *)&tbuf[recno++], buffer, TBLOCK);
        return (TBLOCK);
 }
 
        return (TBLOCK);
 }
 
@@ -987,15 +1081,15 @@ writetape(buffer)
        first = 1;
        if (recno >= nblock) {
                if (write(mt, tbuf, TBLOCK*nblock) < 0) {
        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;
        }
                        done(2);
                }
                recno = 0;
        }
-       copy(&tbuf[recno++], buffer);
+       bcopy(buffer, (char *)&tbuf[recno++], TBLOCK);
        if (recno >= nblock) {
                if (write(mt, tbuf, TBLOCK*nblock) < 0) {
        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;
                        done(2);
                }
                recno = 0;
@@ -1013,7 +1107,7 @@ backtape()
                mtdev = ioctl(mt, MTIOCGET, &mtget);
        if (mtdev == 0) {
                if (ioctl(mt, MTIOCTOP, &mtop) < 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
                        done(4);
                }
        } else
@@ -1026,13 +1120,36 @@ flushtape()
        write(mt, tbuf, TBLOCK*nblock);
 }
 
        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);
 }
 }