added missing "skipchar" in reading ranges which was accidentally
[unix-history] / usr / src / old / tar / tar.c
index f630744..385bb99 100644 (file)
@@ -1,17 +1,24 @@
-static char *sccsid = "@(#)tar.c       4.6 (Berkeley) 82/02/21";
+#ifndef lint
+static char *sccsid = "@(#)tar.c       4.17 (Berkeley) %G%";
+#endif
+
+/*
+ * Tape Archival Program
+ */
 #include <stdio.h>
 #include <stdio.h>
-#include <sys/types.h>
+#include <sys/param.h>
 #include <sys/stat.h>
 #include <sys/stat.h>
-#include <ndir.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>
 
 
-char   *sprintf();
-char   *strcat();
-daddr_t        bsrch();
 #define TBLOCK 512
 #define NBLOCK 20
 #define NAMSIZ 100
 #define TBLOCK 512
 #define NBLOCK 20
 #define NAMSIZ 100
+
 union hblock {
        char dummy[TBLOCK];
        struct header {
 union hblock {
        char dummy[TBLOCK];
        struct header {
@@ -25,7 +32,7 @@ union hblock {
                char linkflag;
                char linkname[NAMSIZ];
        } dbuf;
                char linkflag;
                char linkname[NAMSIZ];
        } dbuf;
-} dblock, tbuf[NBLOCK];
+};
 
 struct linkbuf {
        ino_t   inum;
 
 struct linkbuf {
        ino_t   inum;
@@ -33,33 +40,61 @@ struct linkbuf {
        int     count;
        char    pathname[NAMSIZ];
        struct  linkbuf *nextp;
        int     count;
        char    pathname[NAMSIZ];
        struct  linkbuf *nextp;
-} *ihead;
-
-struct stat stbuf;
-
-int    rflag, xflag, vflag, tflag, mt, cflag, mflag, fflag, oflag, pflag;
-int    term, chksum, wflag, recno, first, linkerrok;
+};
+
+union  hblock dblock;
+union  hblock *tbuf;
+struct linkbuf *ihead;
+struct stat stbuf;
+
+int    rflag;
+int    xflag;
+int    vflag;
+int    tflag;
+int    cflag;
+int    mflag;
+int    fflag;
+int    iflag;
+int    oflag;
+int    pflag;
+int    wflag;
+int    hflag;
+int    Bflag;
+int    Fflag;
+
+int    mt;
+int    term;
+int    chksum;
+int    recno;
+int    first;
+int    linkerrok;
 int    freemem = 1;
 int    nblock = NBLOCK;
 int    freemem = 1;
 int    nblock = NBLOCK;
+int    onintr();
+int    onquit();
+int    onhup();
+int    onterm();
 
 daddr_t        low;
 daddr_t        high;
 
 daddr_t        low;
 daddr_t        high;
+daddr_t        bsrch();
 
 FILE   *tfile;
 char   tname[] = "/tmp/tarXXXXXX";
 
 FILE   *tfile;
 char   tname[] = "/tmp/tarXXXXXX";
-
-
 char   *usefile;
 char   *usefile;
-char   magtape[]       = "/dev/rmt8";
-
+char   magtape[] = "/dev/rmt8";
 char   *malloc();
 char   *malloc();
+char   *sprintf();
+char   *strcat();
+char   *rindex();
+char   *getcwd();
+char   *getwd();
 
 main(argc, argv)
 int    argc;
 char   *argv[];
 {
        char *cp;
 
 main(argc, argv)
 int    argc;
 char   *argv[];
 {
        char *cp;
-       int onintr(), onquit(), onhup(), onterm();
 
        if (argc < 2)
                usage();
 
        if (argc < 2)
                usage();
@@ -70,48 +105,68 @@ char       *argv[];
        argv++;
        for (cp = *argv++; *cp; cp++) 
                switch(*cp) {
        argv++;
        for (cp = *argv++; *cp; cp++) 
                switch(*cp) {
+
                case 'f':
                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;
+
                case 'c':
                        cflag++;
                        rflag++;
                        break;
                case 'c':
                        cflag++;
                        rflag++;
                        break;
+
                case 'o':
                        oflag++;
                        break;
                case 'o':
                        oflag++;
                        break;
+
                case 'p':
                        pflag++;
                        break;
                case 'p':
                        pflag++;
                        break;
+               
                case 'u':
                        mktemp(tname);
                        if ((tfile = fopen(tname, "w")) == NULL) {
                case 'u':
                        mktemp(tname);
                        if ((tfile = fopen(tname, "w")) == NULL) {
-                               fprintf(stderr, "Tar: cannot create temporary file (%s)\n", tname);
+                               fprintf(stderr,
+                                "Tar: cannot create temporary file (%s)\n",
+                                tname);
                                done(1);
                        }
                        fprintf(tfile, "!!!!!/!/!/!/!/!/!/! 000\n");
                                done(1);
                        }
                        fprintf(tfile, "!!!!!/!/!/!/!/!/!/! 000\n");
-                       /* FALL THROUGH */
+                       /*FALL THRU*/
+
                case 'r':
                        rflag++;
                        break;
                case 'r':
                        rflag++;
                        break;
+
                case 'v':
                        vflag++;
                        break;
                case 'v':
                        vflag++;
                        break;
+
                case 'w':
                        wflag++;
                        break;
                case 'w':
                        wflag++;
                        break;
+
                case 'x':
                        xflag++;
                        break;
                case 'x':
                        xflag++;
                        break;
+
                case 't':
                        tflag++;
                        break;
                case 't':
                        tflag++;
                        break;
+
                case 'm':
                        mflag++;
                        break;
                case 'm':
                        mflag++;
                        break;
+
                case '-':
                        break;
                case '-':
                        break;
+
                case '0':
                case '1':
                case '4':
                case '0':
                case '1':
                case '4':
@@ -121,90 +176,112 @@ char     *argv[];
                        magtape[8] = *cp;
                        usefile = magtape;
                        break;
                        magtape[8] = *cp;
                        usefile = magtape;
                        break;
+
                case 'b':
                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;
                        break;
+
                case 'l':
                        linkerrok++;
                        break;
                case 'l':
                        linkerrok++;
                        break;
+
+               case 'h':
+                       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();
                }
 
+       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 (rflag) {
-               if (cflag && tfile != NULL) {
+               if (cflag && tfile != NULL)
                        usage();
                        usage();
-                       done(1);
-               }
                if (signal(SIGINT, SIG_IGN) != SIG_IGN)
                        signal(SIGINT, onintr);
                if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
                        signal(SIGHUP, onhup);
                if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
                        signal(SIGQUIT, onquit);
                if (signal(SIGINT, SIG_IGN) != SIG_IGN)
                        signal(SIGINT, onintr);
                if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
                        signal(SIGHUP, onhup);
                if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
                        signal(SIGQUIT, onquit);
-/*
+#ifdef notdef
                if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
                        signal(SIGTERM, onterm);
                if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
                        signal(SIGTERM, onterm);
-*/
+#endif
                if (strcmp(usefile, "-") == 0) {
                        if (cflag == 0) {
                if (strcmp(usefile, "-") == 0) {
                        if (cflag == 0) {
-                               fprintf(stderr, "Can only create standard output archives\n");
+                               fprintf(stderr,
+                        "tar: can only create standard output archives\n");
                                done(1);
                        }
                        mt = dup(1);
                        nblock = 1;
                                done(1);
                        }
                        mt = dup(1);
                        nblock = 1;
-               }
-               else if ((mt = open(usefile, 2)) < 0) {
+               } else if ((mt = open(usefile, 2)) < 0) {
                        if (cflag == 0 || (mt =  creat(usefile, 0666)) < 0) {
                        if (cflag == 0 || (mt =  creat(usefile, 0666)) < 0) {
-                               fprintf(stderr, "tar: cannot open %s\n", usefile);
+                               fprintf(stderr,
+                                       "tar: cannot open %s\n", usefile);
                                done(1);
                        }
                }
                dorep(argv);
                                done(1);
                        }
                }
                dorep(argv);
+               done(0);
        }
        }
-       else if (xflag)  {
-               if (strcmp(usefile, "-") == 0) {
-                       mt = dup(0);
-                       nblock = 1;
-               }
-               else if ((mt = open(usefile, 0)) < 0) {
-                       fprintf(stderr, "tar: cannot open %s\n", usefile);
-                       done(1);
-               }
-               doxtract(argv);
-       }
-       else if (tflag) {
-               if (strcmp(usefile, "-") == 0) {
-                       mt = dup(0);
-                       nblock = 1;
-               }
-               else if ((mt = open(usefile, 0)) < 0) {
-                       fprintf(stderr, "tar: cannot open %s\n", usefile);
-                       done(1);
-               }
-               dotable();
+       if (strcmp(usefile, "-") == 0) {
+               mt = dup(0);
+               nblock = 1;
+       } else if ((mt = open(usefile, 0)) < 0) {
+               fprintf(stderr, "tar: cannot open %s\n", usefile);
+               done(1);
        }
        }
+       if (xflag)
+               doxtract(argv);
        else
        else
-               usage();
+               dotable();
        done(0);
 }
 
 usage()
 {
        done(0);
 }
 
 usage()
 {
-       fprintf(stderr, "tar: usage  tar -{txru}[cvfblm] [tapefile] [blocksize] file1 file2...\n");
+       fprintf(stderr,
+"tar: usage: tar -{txru}[cvfblmhopwBi] [tapefile] [blocksize] file1 file2...\n");
        done(1);
 }
 
 dorep(argv)
        done(1);
 }
 
 dorep(argv)
-char   *argv[];
+       char *argv[];
 {
        register char *cp, *cp2;
 {
        register char *cp, *cp2;
-       char wdir[60];
+       char wdir[MAXPATHLEN], tempdir[MAXPATHLEN], *parent;
 
        if (!cflag) {
                getdir();
 
        if (!cflag) {
                getdir();
@@ -214,10 +291,12 @@ char      *argv[];
                                done(0);
                        getdir();
                } while (!endtape());
                                done(0);
                        getdir();
                } while (!endtape());
+               backtape();
                if (tfile != NULL) {
                        char buf[200];
 
                if (tfile != NULL) {
                        char buf[200];
 
-                       sprintf(buf, "sort +0 -1 +1nr %s -o %s; awk '$1 != prev {print; prev=$1}' %s >%sX; mv %sX %s",
+                       sprintf(buf,
+"sort +0 -1 +1nr %s -o %s; awk '$1 != prev {print; prev=$1}' %s >%sX; mv %sX %s",
                                tname, tname, tname, tname, tname, tname);
                        fflush(tfile);
                        system(buf);
                                tname, tname, tname, tname, tname, tname);
                        fflush(tfile);
                        system(buf);
@@ -227,7 +306,7 @@ char        *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]) {
@@ -235,39 +314,42 @@ char      *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();
        putempty();
        flushtape();
                chdir(wdir);
        }
        putempty();
        putempty();
        flushtape();
-       if (linkerrok == 1)
-               for (; ihead != NULL; ihead = ihead->nextp)
-                       if (ihead->count != 0)
-                               fprintf(stderr, "Missing links to %s\n", ihead->pathname);
+       if (linkerrok == 0)
+               return;
+       for (; ihead != NULL; ihead = ihead->nextp) {
+               if (ihead->count == 0)
+                       continue;
+               fprintf(stderr, "tar: missing links to %s\n", ihead->pathname);
+       }
 }
 
 endtape()
 {
 }
 
 endtape()
 {
-       if (dblock.dbuf.name[0] == '\0') {
-               backtape();
-               return(1);
-       }
-       else
-               return(0);
+       return (dblock.dbuf.name[0] == '\0');
 }
 
 getdir()
 }
 
 getdir()
@@ -275,7 +357,8 @@ getdir()
        register struct stat *sp;
        int i;
 
        register struct stat *sp;
        int i;
 
-       readtape( (char *) &dblock);
+top:
+       readtape((char *)&dblock);
        if (dblock.dbuf.name[0] == '\0')
                return;
        sp = &stbuf;
        if (dblock.dbuf.name[0] == '\0')
                return;
        sp = &stbuf;
@@ -288,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)
@@ -311,120 +397,164 @@ passtape()
                readtape(buf);
 }
 
                readtape(buf);
 }
 
-putfile(longname, shortname)
-char *longname;
-char *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;
        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;
        }
-
-       fstat(infile, &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) {
-               for (i = 0, cp = buf; *cp++ = longname[i++];);
+       switch (stbuf.st_mode & S_IFMT) {
+       case S_IFDIR:
+               for (i = 0, cp = buf; *cp++ = longname[i++];)
+                       ;
                *--cp = '/';
                *++cp = 0  ;
                if (!oflag) {
                *--cp = '/';
                *++cp = 0  ;
                if (!oflag) {
-                   if( (cp - buf) >= NAMSIZ) {
-                       fprintf(stderr, "%s: file name too long\n", longname);
-                       close(infile);
-                       return;
-                   }
-                   stbuf.st_size = 0;
-                   tomodes(&stbuf);
-                   strcpy(dblock.dbuf.name,buf);
-                   sprintf(dblock.dbuf.chksum, "%6o", checksum());
-                   writetape( (char *) &dblock);
+                       if ((cp - buf) >= NAMSIZ) {
+                               fprintf(stderr, "tar: %s: file name too long\n",
+                                   longname);
+                               return;
+                       }
+                       stbuf.st_size = 0;
+                       tomodes(&stbuf);
+                       strcpy(dblock.dbuf.name,buf);
+                       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) {
                        if (dp->d_ino == 0)
                                continue;
                        return;
                }
                while ((dp = readdir(dirp)) != NULL && !term) {
                        if (dp->d_ino == 0)
                                continue;
-                       if (!strcmp(".", dp->d_name) || !strcmp("..", dp->d_name))
+                       if (!strcmp(".", dp->d_name) ||
+                           !strcmp("..", dp->d_name))
                                continue;
                        strcpy(cp, dp->d_name);
                        i = telldir(dirp);
                        closedir(dirp);
                                continue;
                        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;
-       }
-       if ((stbuf.st_mode & S_IFMT) != S_IFREG) {
-               fprintf(stderr, "tar: %s is not a file. Not dumped\n", longname);
-               return;
-       }
-
-       tomodes(&stbuf);
-
-       cp2 = longname;
-       for (cp = dblock.dbuf.name, i=0; (*cp++ = *cp2++) && i < NAMSIZ; i++);
-       if (i >= NAMSIZ) {
-               fprintf(stderr, "%s: file name too long\n", longname);
-               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;
-                       }
+               chdir(parent);
+               break;
+
+       case S_IFLNK:
+               tomodes(&stbuf);
+               if (strlen(longname) >= NAMSIZ) {
+                       fprintf(stderr, "tar: %s: file name too long\n",
+                           longname);
+                       return;
                }
                }
-               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);
+               strcpy(dblock.dbuf.name, longname);
+               if (stbuf.st_size + 1 >= NAMSIZ) {
+                       fprintf(stderr, "tar: %s: symbolic link too long\n",
+                           longname);
+                       return;
+               }
+               i = readlink(shortname, dblock.dbuf.linkname, NAMSIZ - 1);
+               if (i < 0) {
+                       perror(longname);
+                       return;
+               }
+               dblock.dbuf.linkname[i] = '\0';
+               dblock.dbuf.linkflag = '2';
+               if (vflag) {
+                       fprintf(stderr, "a %s ", longname);
+                       fprintf(stderr, "symbolic link to %s\n",
+                           dblock.dbuf.linkname);
+               }
+               sprintf(dblock.dbuf.size, "%11lo", 0);
+               sprintf(dblock.dbuf.chksum, "%6o", checksum());
+               writetape((char *)&dblock);
+               break;
+
+       case S_IFREG:
+               if ((infile = open(shortname, 0)) < 0) {
+                       fprintf(stderr, "tar: %s: cannot open file\n", longname);
+                       return;
+               }
+               tomodes(&stbuf);
+               if (strlen(longname) >= NAMSIZ) {
+                       fprintf(stderr, "tar: %s: file name too long\n",
+                           longname);
                        return;
                }
                        return;
                }
-               else {
+               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) {
                        lp = (struct linkbuf *) malloc(sizeof(*lp));
                        if (lp == NULL) {
                                if (freemem) {
-                                       fprintf(stderr, "Out of memory. Link information lost\n");
+                                       fprintf(stderr,
+                               "tar: out of memory, link information lost\n");
                                        freemem = 0;
                                }
                                        freemem = 0;
                                }
-                       }
-                       else {
+                       } else {
                                lp->nextp = ihead;
                                ihead = lp;
                                lp->inum = stbuf.st_ino;
                                lp->nextp = ihead;
                                ihead = lp;
                                lp->inum = stbuf.st_ino;
@@ -433,31 +563,35 @@ char *shortname;
                                strcpy(lp->pathname, longname);
                        }
                }
                                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)
-char   *argv[];
+       char *argv[];
 {
        long blocks, bytes;
        char buf[TBLOCK];
 {
        long blocks, bytes;
        char buf[TBLOCK];
@@ -468,10 +602,8 @@ char       *argv[];
                getdir();
                if (endtape())
                        break;
                getdir();
                if (endtape())
                        break;
-
                if (*argv == 0)
                        goto gotit;
                if (*argv == 0)
                        goto gotit;
-
                for (cp = argv; *cp; cp++)
                        if (prefix(*cp, dblock.dbuf.name))
                                goto gotit;
                for (cp = argv; *cp; cp++)
                        if (prefix(*cp, dblock.dbuf.name))
                                goto gotit;
@@ -483,56 +615,100 @@ gotit:
                        passtape();
                        continue;
                }
                        passtape();
                        continue;
                }
+               if (Fflag) {
+                       char *s;
 
 
-               if(checkdir(dblock.dbuf.name))
+                       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;
                        continue;
-
+               if (dblock.dbuf.linkflag == '2') {
+                       unlink(dblock.dbuf.name);
+                       if (symlink(dblock.dbuf.linkname, dblock.dbuf.name)<0) {
+                               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);
+#ifdef notdef
+                       /* ignore alien orders */
+                       chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid);
+                       if (mflag == 0) {
+                               struct timeval tv[2];
+
+                               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) {
                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;
                }
                        continue;
                }
-               if ((ofile = creat(dblock.dbuf.name, stbuf.st_mode & 07777)) < 0) {
-                       fprintf(stderr, "tar: %s - cannot create\n", dblock.dbuf.name);
+               if ((ofile = creat(dblock.dbuf.name,stbuf.st_mode&0xfff)) < 0) {
+                       fprintf(stderr, "tar: %s - cannot create\n",
+                           dblock.dbuf.name);
                        passtape();
                        continue;
                }
                chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid);
                        passtape();
                        continue;
                }
                chown(dblock.dbuf.name, stbuf.st_uid, stbuf.st_gid);
-
                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);
-               while (blocks-- > 0) {
+                           dblock.dbuf.name, bytes, blocks);
+               for (; blocks-- > 0; bytes -= TBLOCK) {
                        readtape(buf);
                        if (bytes > TBLOCK) {
                                if (write(ofile, buf, TBLOCK) < 0) {
                        readtape(buf);
                        if (bytes > TBLOCK) {
                                if (write(ofile, buf, TBLOCK) < 0) {
-                                       fprintf(stderr, "tar: %s: HELP - extract write error\n", dblock.dbuf.name);
+                                       fprintf(stderr,
+                                       "tar: %s: HELP - extract write error\n",
+                                           dblock.dbuf.name);
                                        done(2);
                                }
                                        done(2);
                                }
-                       } else
-                               if (write(ofile, buf, (int) bytes) < 0) {
-                                       fprintf(stderr, "tar: %s: HELP - extract write error\n", dblock.dbuf.name);
-                                       done(2);
-                               }
-                       bytes -= TBLOCK;
+                               continue;
+                       }
+                       if (write(ofile, buf, (int) bytes) < 0) {
+                               fprintf(stderr,
+                                   "tar: %s: HELP - extract write error\n",
+                                   dblock.dbuf.name);
+                               done(2);
+                       }
                }
                close(ofile);
                if (mflag == 0) {
                }
                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)
                }
                if (pflag)
-                   chmod(dblock.dbuf.name, stbuf.st_mode & 07777);
+                       chmod(dblock.dbuf.name, stbuf.st_mode & 07777);
        }
 }
 
        }
 }
 
@@ -547,6 +723,8 @@ dotable()
                printf("%s", dblock.dbuf.name);
                if (dblock.dbuf.linkflag == '1')
                        printf(" linked to %s", dblock.dbuf.linkname);
                printf("%s", dblock.dbuf.name);
                if (dblock.dbuf.linkflag == '1')
                        printf(" linked to %s", dblock.dbuf.linkname);
+               if (dblock.dbuf.linkflag == '2')
+                       printf(" symbolic link to %s", dblock.dbuf.linkname);
                printf("\n");
                passtape();
        }
                printf("\n");
                passtape();
        }
@@ -555,15 +733,13 @@ 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);
 }
 
 longt(st)
        writetape(buf);
 }
 
 longt(st)
-register struct stat *st;
+       register struct stat *st;
 {
        register char *cp;
        char *ctime();
 {
        register char *cp;
        char *ctime();
@@ -600,7 +776,7 @@ int m9[] = { 2, STXT, 't', XOTH, 'x', '-' };
 int    *m[] = { m1, m2, m3, m4, m5, m6, m7, m8, m9};
 
 pmode(st)
 int    *m[] = { m1, m2, m3, m4, m5, m6, m7, m8, m9};
 
 pmode(st)
-register struct stat *st;
+       register struct stat *st;
 {
        register int **mp;
 
 {
        register int **mp;
 
@@ -609,8 +785,8 @@ register struct stat *st;
 }
 
 select(pairp, st)
 }
 
 select(pairp, st)
-int *pairp;
-struct stat *st;
+       int *pairp;
+       struct stat *st;
 {
        register int n, *ap;
 
 {
        register int n, *ap;
 
@@ -622,33 +798,42 @@ struct stat *st;
 }
 
 checkdir(name)
 }
 
 checkdir(name)
-register char *name;
+       register char *name;
 {
        register char *cp;
 {
        register char *cp;
-       int i;
+
+       /*
+        * 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++) {
        for (cp = name; *cp; cp++) {
-               if (*cp == '/') {
-                       *cp = '\0';
-                       if (access(name, 01) < 0) {
-                               register int pid, rp;
-
-                               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);
-                               }
-                               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 (*cp != '/')
+                       continue;
+               *cp = '\0';
+               if (access(name, 0) < 0) {
+                       if (mkdir(name, 0777) < 0) {
+                               perror(name);
+                               *cp = '/';
+                               return (0);
                        }
                        }
-                       *cp = '/';
+                       chown(name, stbuf.st_uid, stbuf.st_gid);
+                       if (pflag)
+                               chmod(name, stbuf.st_mode & 0777);
                }
                }
+               *cp = '/';
        }
        }
-       return(cp[-1]=='/');
+       return (cp[-1]=='/');
 }
 
 onintr()
 }
 
 onintr()
@@ -694,28 +879,25 @@ checksum()
        register i;
        register char *cp;
 
        register i;
        register char *cp;
 
-       for (cp = dblock.dbuf.chksum; cp < &dblock.dbuf.chksum[sizeof(dblock.dbuf.chksum)]; cp++)
+       for (cp = dblock.dbuf.chksum;
+            cp < &dblock.dbuf.chksum[sizeof(dblock.dbuf.chksum)]; cp++)
                *cp = ' ';
        i = 0;
        for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++)
                i += *cp;
                *cp = ' ';
        i = 0;
        for (cp = dblock.dummy; cp < &dblock.dummy[TBLOCK]; cp++)
                i += *cp;
-       return(i);
+       return (i);
 }
 
 checkw(c, name)
 }
 
 checkw(c, name)
-char *name;
+       char *name;
 {
 {
-       if (wflag) {
-               printf("%c ", c);
-               if (vflag)
-                       longt(&stbuf);
-               printf("%s: ", name);
-               if (response() == 'y'){
-                       return(1);
-               }
-               return(0);
-       }
-       return(1);
+       if (!wflag)
+               return (1);
+       printf("%c ", c);
+       if (vflag)
+               longt(&stbuf);
+       printf("%s: ", name);
+       return (response() == 'y');
 }
 
 response()
 }
 
 response()
@@ -724,29 +906,48 @@ response()
 
        c = getchar();
        if (c != '\n')
 
        c = getchar();
        if (c != '\n')
-               while (getchar() != '\n');
-       else c = 'n';
-       return(c);
+               while (getchar() != '\n')
+                       ;
+       else
+               c = 'n';
+       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)
 }
 
 checkupdate(arg)
-char   *arg;
+       char *arg;
 {
        char name[100];
 {
        char name[100];
-       long    mtime;
+       long mtime;
        daddr_t seekp;
        daddr_t lookup();
 
        rewind(tfile);
        for (;;) {
                if ((seekp = lookup(arg)) < 0)
        daddr_t seekp;
        daddr_t lookup();
 
        rewind(tfile);
        for (;;) {
                if ((seekp = lookup(arg)) < 0)
-                       return(1);
+                       return (1);
                fseek(tfile, seekp, 0);
                fscanf(tfile, "%s %lo", name, &mtime);
                fseek(tfile, seekp, 0);
                fscanf(tfile, "%s %lo", name, &mtime);
-               if (stbuf.st_mtime > mtime)
-                       return(1);
-               else
-                       return(0);
+               return (stbuf.st_mtime > mtime);
        }
 }
 
        }
 }
 
@@ -757,62 +958,37 @@ done(n)
 }
 
 prefix(s1, s2)
 }
 
 prefix(s1, s2)
-register char *s1, *s2;
+       register char *s1, *s2;
 {
        while (*s1)
                if (*s1++ != *s2++)
 {
        while (*s1)
                if (*s1++ != *s2++)
-                       return(0);
+                       return (0);
        if (*s2)
        if (*s2)
-               return(*s2 == '/');
-       return(1);
-}
-
-getwdir(s)
-char *s;
-{
-       int i;
-       int     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]);
+               return (*s2 == '/');
+       return (1);
 }
 
 #define        N       200
 int    njab;
 }
 
 #define        N       200
 int    njab;
+
 daddr_t
 lookup(s)
 daddr_t
 lookup(s)
-char *s;
+       char *s;
 {
        register i;
        daddr_t a;
 
        for(i=0; s[i]; i++)
 {
        register i;
        daddr_t a;
 
        for(i=0; s[i]; i++)
-               if(s[i] == ' ')
+               if (s[i] == ' ')
                        break;
        a = bsrch(s, i, low, high);
                        break;
        a = bsrch(s, i, low, high);
-       return(a);
+       return (a);
 }
 
 daddr_t
 bsrch(s, n, l, h)
 }
 
 daddr_t
 bsrch(s, n, l, h)
-daddr_t l, h;
-char *s;
+       daddr_t l, h;
+       char *s;
 {
        register i, j;
        char b[N];
 {
        register i, j;
        char b[N];
@@ -821,104 +997,104 @@ char *s;
        njab = 0;
 
 loop:
        njab = 0;
 
 loop:
-       if(l >= h)
-               return(-1L);
+       if (l >= h)
+               return (-1L);
        m = l + (h-l)/2 - N/2;
        m = l + (h-l)/2 - N/2;
-       if(m < l)
+       if (m < l)
                m = l;
        fseek(tfile, m, 0);
        fread(b, 1, N, tfile);
        njab++;
        for(i=0; i<N; i++) {
                m = l;
        fseek(tfile, m, 0);
        fread(b, 1, N, tfile);
        njab++;
        for(i=0; i<N; i++) {
-               if(b[i] == '\n')
+               if (b[i] == '\n')
                        break;
                m++;
        }
                        break;
                m++;
        }
-       if(m >= h)
-               return(-1L);
+       if (m >= h)
+               return (-1L);
        m1 = m;
        j = i;
        for(i++; i<N; i++) {
                m1++;
        m1 = m;
        j = i;
        for(i++; i<N; i++) {
                m1++;
-               if(b[i] == '\n')
+               if (b[i] == '\n')
                        break;
        }
        i = cmp(b+j, s, n);
                        break;
        }
        i = cmp(b+j, s, n);
-       if(i < 0) {
+       if (i < 0) {
                h = m;
                goto loop;
        }
                h = m;
                goto loop;
        }
-       if(i > 0) {
+       if (i > 0) {
                l = m1;
                goto loop;
        }
                l = m1;
                goto loop;
        }
-       return(m);
+       return (m);
 }
 
 cmp(b, s, n)
 }
 
 cmp(b, s, n)
-char *b, *s;
+       char *b, *s;
 {
        register i;
 
 {
        register i;
 
-       if(b[0] != '\n')
+       if (b[0] != '\n')
                exit(2);
        for(i=0; i<n; i++) {
                exit(2);
        for(i=0; i<n; i++) {
-               if(b[i+1] > s[i])
-                       return(-1);
-               if(b[i+1] < s[i])
-                       return(1);
+               if (b[i+1] > s[i])
+                       return (-1);
+               if (b[i+1] < s[i])
+                       return (1);
        }
        }
-       return(b[i+1] == ' '? 0 : -1);
+       return (b[i+1] == ' '? 0 : -1);
 }
 
 readtape(buffer)
 }
 
 readtape(buffer)
-char *buffer;
+       char *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++]);
-       return(TBLOCK);
+       bcopy((char *)&tbuf[recno++], buffer, TBLOCK);
+       return (TBLOCK);
 }
 
 writetape(buffer)
 }
 
 writetape(buffer)
-char *buffer;
+       char *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;
        }
-       return(TBLOCK);
+       return (TBLOCK);
 }
 
 backtape()
 }
 
 backtape()
@@ -931,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
@@ -944,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);
 }
 }