2 * tar - `tape archiver', actually usable on any medium.
3 * POSIX "ustar" compliant when extracting, and by default when creating.
4 * this tar attempts to read and write multiple Tblock-byte blocks
5 * at once to and from the filesystem, and does not copy blocks
12 #include <fcall.h> /* for %M */
16 * modified versions of those in libc.h; scans only the first arg for
17 * keyletters and options.
20 (argv0 || (argv0 = *argv)), argv++, argc--;\
26 while(*_args && (_args += chartorune(&_argc, _args)))\
28 #define TARGEND SET(_argt); USED(_argt);USED(_argc);USED(_args); \
30 USED(argv); USED(argc); }
31 #define TARGC() (_argc)
33 #define ROUNDUP(a, b) (((a) + (b) - 1)/(b))
34 #define BYTES2TBLKS(bytes) ROUNDUP(bytes, Tblock)
36 /* read big-endian binary integers; args must be (uchar *) */
37 #define G2BEBYTE(x) (((x)[0]<<8) | (x)[1])
38 #define G3BEBYTE(x) (((x)[0]<<16) | ((x)[1]<<8) | (x)[2])
39 #define G4BEBYTE(x) (((x)[0]<<24) | ((x)[1]<<16) | ((x)[2]<<8) | (x)[3])
40 #define G8BEBYTE(x) (((vlong)G4BEBYTE(x)<<32) | (u32int)G4BEBYTE((x)+4))
43 typedef char *(*Refill)(int ar, char *bufs, int justhdr);
45 enum { Stdin, Stdout, Stderr };
46 enum { Rd, Wr }; /* pipe fd-array indices */
47 enum { Output, Input };
48 enum { None, Toc, Xtract, Replace };
49 enum { Alldata, Justnxthdr };
53 Maxpfx = 155, /* from POSIX */
54 Maxname = Namsiz + 1 + Maxpfx,
56 Binsize = 0x80, /* flag in size[0], from gnu: positive binary size */
57 Binnegsz = 0xff, /* flag in size[0]: negative binary size */
59 Nblock = 40, /* maximum blocksize */
60 Dblock = 20, /* default blocksize */
64 /* POSIX link flags */
70 LF_SYMLINK2 = 's', /* 4BSD used this */
77 /* 'A' - 'Z' are reserved for custom implementations */
79 LF_LONGNAME = 'L', /* GNU extenstion */
83 #define islink(lf) (isreallink(lf) || issymlink(lf))
84 #define isreallink(lf) ((lf) == LF_LINK)
85 #define issymlink(lf) ((lf) == LF_SYMLINK1 || (lf) == LF_SYMLINK2)
98 char linkname[Namsiz];
100 /* rest are defined by POSIX's ustar format; see p1003.2b */
101 char magic[6]; /* "ustar" */
107 char prefix[Maxpfx]; /* if non-null, path= prefix "/" name */
117 static Compress comps[] = {
118 "gzip", "gunzip", { ".tar.gz", ".tgz" }, /* default */
119 "compress", "uncompress", { ".tar.Z", ".tz" },
120 "bzip2", "bunzip2", { ".tar.bz", ".tbz",
121 ".tar.bz2",".tbz2" },
126 int fd; /* original fd */
127 int rfd; /* replacement fd */
132 #define OTHER(rdwr) ((rdwr) == Rd? Wr: Rd)
134 static int fixednblock;
136 static int posix = 1;
140 static int relative = 1;
143 static int docompress;
144 static int keepexisting;
145 static int ignerrs; /* flag: ignore i/o errors if possible */
146 static Off blkoff; /* offset of the current archive block (not Tblock) */
149 static int nblock = Dblock;
151 static char *usefile, *arname = "archive";
152 static char origdir[Maxlongname+1];
153 static Hdr *tpblk, *endblk;
159 fprint(2, "usage: %s {crtx}[PRTfgikmpsuvz] [archive] [file1 file2...]\n",
164 /* I/O, with error retry or exit */
167 cope(char *name, int fd, void *buf, long len, Off off)
169 fprint(2, "%s: %serror reading %s: %r\n", argv0,
170 (ignerrs? "ignoring ": ""), name);
174 /* pretend we read len bytes of zeroes */
176 if (off >= 0) /* seekable? */
177 seek(fd, off + len, 0);
182 eread(char *name, int fd, void *buf, long len)
187 off = seek(fd, 0, 1); /* for coping with errors */
188 rd = read(fd, buf, len);
190 rd = cope(name, fd, buf, len, off);
195 ereadn(char *name, int fd, void *buf, long len)
200 off = seek(fd, 0, 1);
201 rd = readn(fd, buf, len);
203 rd = cope(name, fd, buf, len, off);
208 ewrite(char *name, int fd, void *buf, long len)
213 rd = write(fd, buf, len);
215 sysfatal("error writing %s: %r", name);
222 compmethod(char *name)
225 int i, nmlen, sfxlen;
228 nmlen = strlen(name);
229 for (cp = comps; cp < comps + nelem(comps); cp++) {
230 for (i = 0; i < nelem(cp->sfx) && cp->sfx[i]; i++) {
231 sfxlen = strlen(cp->sfx[i]);
232 if (nmlen > sfxlen &&
233 strcmp(cp->sfx[i], name + nmlen - sfxlen) == 0)
238 return docompress? comps: nil;
242 * push a filter, cmd, onto fd. if input, it's an input descriptor.
243 * returns a descriptor to replace fd, or -1 on error.
246 push(int fd, char *cmd, int input, Pushstate *ps)
254 if (fd < 0 || pipe(pifds) < 0)
262 dup(pifds[Wr], Stdout);
264 dup(pifds[Rd], Stdin);
265 close(pifds[input? Rd: Wr]);
266 dup(fd, (input? Stdin: Stdout));
269 s_append(s, "/bin/");
271 execl(s_to_c(s), cmd, nil);
272 sysfatal("can't exec %s: %r", cmd);
274 nfd = pifds[input? Rd: Wr];
275 close(pifds[input? Wr: Rd]);
284 pushclose(Pushstate *ps)
288 if (ps->fd < 0 || ps->rfd < 0 || !ps->open)
293 while ((wm = wait()) != nil && wm->pid != ps->kid)
295 return wm? wm->msg: nil;
299 * block-buffer management
306 tpblk = malloc(Tblock * nblock);
307 assert(tpblk != nil);
308 endblk = tpblk + nblock;
312 * (re)fill block buffers from archive. `justhdr' means we don't care
313 * about the data before the next header block.
316 refill(int ar, char *bufs, int justhdr)
319 unsigned bytes = Tblock * nblock;
320 static int done, first = 1, seekable;
325 blkoff = seek(ar, 0, 1); /* note position for `tar r' */
327 seekable = blkoff >= 0;
328 /* try to size non-pipe input at first read */
329 if (first && usefile && !fixednblock) {
330 n = eread(arname, ar, bufs, bytes);
332 sysfatal("EOF reading archive %s: %r", arname);
335 sysfatal("%s: archive block size (%d) error", arname, i);
339 fprint(2, "%s: blocking = %d\n", argv0, nblock);
340 endblk = (Hdr *)bufs + nblock;
343 } else if (justhdr && seekable && nexthdr - blkoff >= bytes) {
344 /* optimisation for huge archive members on seekable media */
345 if (seek(ar, bytes, 1) < 0)
346 sysfatal("can't seek on archive %s: %r", arname);
349 n = ereadn(arname, ar, bufs, bytes);
353 sysfatal("unexpected EOF reading archive %s", arname);
355 sysfatal("partial block read from archive %s", arname);
358 memset(bufs + n, 0, bytes - n);
364 getblk(int ar, Refill rfp, int justhdr)
366 if (curblk == nil || curblk >= endblk) { /* input block exhausted? */
367 if (rfp != nil && (*rfp)(ar, (char *)tpblk, justhdr) == nil)
375 getblkrd(int ar, int justhdr)
377 return getblk(ar, refill, justhdr);
383 return getblk(ar, nil, Alldata);
389 Hdr *hp = getblke(ar);
392 memset(hp->data, 0, Tblock);
397 * how many block buffers are available, starting at the address
398 * just returned by getblk*?
403 int n = endblk - (curblk - 1);
405 return n > max? max: n;
409 * indicate that one is done with the last block obtained from getblke
410 * and it is now available to be written into the archive.
415 unsigned bytes = Tblock * nblock;
417 /* if writing end-of-archive, aid compression (good hygiene too) */
419 memset(curblk, 0, (char *)endblk - (char *)curblk);
420 ewrite(arname, ar, tpblk, bytes);
426 if (curblk >= endblk)
438 putreadblks(int ar, int blks)
445 putblkmany(int ar, int blks)
457 * modifies hp->chksum but restores it; important for the last block of the
458 * old archive when updating with `tar rf archive'
465 uchar *cp = hp->data;
466 char oldsum[sizeof hp->chksum];
468 memmove(oldsum, hp->chksum, sizeof oldsum);
469 memset(hp->chksum, ' ', sizeof hp->chksum);
472 memmove(hp->chksum, oldsum, sizeof oldsum);
479 return strcmp(hp->magic, "ustar") == 0;
483 * s is at most n bytes long, but need not be NUL-terminated.
484 * if shorter than n bytes, all bytes after the first NUL must also
488 strnlen(char *s, int n)
490 return s[n - 1] != '\0'? n: strlen(s);
493 /* set fullname from header */
499 static char fullnamebuf[2+Maxname+1]; /* 2+ for ./ on relative names */
501 fullname = fullnamebuf+2;
502 namlen = strnlen(hp->name, sizeof hp->name);
503 if (hp->prefix[0] == '\0' || !isustar(hp)) { /* old-style name? */
504 memmove(fullname, hp->name, namlen);
505 fullname[namlen] = '\0';
509 /* name is in two pieces */
510 pfxlen = strnlen(hp->prefix, sizeof hp->prefix);
511 memmove(fullname, hp->prefix, pfxlen);
512 fullname[pfxlen] = '/';
513 memmove(fullname + pfxlen + 1, hp->name, namlen);
514 fullname[pfxlen + 1 + namlen] = '\0';
521 /* the mode test is ugly but sometimes necessary */
522 return hp->linkflag == LF_DIR ||
523 strrchr(name(hp), '\0')[-1] == '/' ||
524 (strtoul(hp->mode, nil, 8)&0170000) == 040000;
530 return name(hp)[0] == '\0';
535 getbe(uchar *src, int size)
548 putbe(uchar *dest, uvlong vl, int size)
550 for (dest += size; size-- > 0; vl >>= 8)
555 * cautious parsing of octal numbers as ascii strings in
556 * a tar header block. this is particularly important for
557 * trusting the checksum when trying to resync.
560 hdrotoull(char *st, char *end, uvlong errval, char *name, char *field)
564 for (numb = st; (*numb == ' ' || *numb == '\0') && numb < end; numb++)
566 if (numb < end && isascii(*numb) && isdigit(*numb))
567 return strtoull(numb, nil, 8);
568 else if (numb >= end)
569 fprint(2, "%s: %s: empty %s in header\n", argv0, name, field);
571 fprint(2, "%s: %s: %s: non-numeric %s in header\n",
572 argv0, name, numb, field);
577 * return the nominal size from the header block, which is not always the
578 * size in the archive (the archive size may be zero for some file types
579 * regardless of the nominal size).
581 * gnu and freebsd tars are now recording vlongs as big-endian binary
582 * with a flag in byte 0 to indicate this, which permits file sizes up to
583 * 2^64-1 (actually 2^80-1 but our file sizes are vlongs) rather than 2^33-1.
590 if((uchar)hp->size[0] == Binnegsz) {
591 fprint(2, "%s: %s: negative length, which is insane\n",
594 } else if((uchar)hp->size[0] == Binsize) {
595 p = (uchar *)hp->size + sizeof hp->size - 1 -
596 sizeof(vlong); /* -1 for terminating space */
600 return hdrotoull(hp->size, hp->size + sizeof hp->size, 0,
605 * return the number of bytes recorded in the archive.
610 if(isdir(hp) || islink(hp->linkflag))
616 parsecksum(char *cksum, char *name)
620 return hdrotoull(cksum, cksum + sizeof hp->chksum, (uvlong)-1LL,
630 hp = getblkrd(ar, Alldata);
632 sysfatal("unexpected EOF instead of archive header in %s",
634 if (eotar(hp)) /* end-of-archive block? */
637 hdrcksum = parsecksum(hp->chksum, name(hp));
638 if (hdrcksum == -1 || chksum(hp) != hdrcksum) {
640 sysfatal("bad archive header checksum in %s: "
641 "name %.100s...; expected %#luo got %#luo",
642 arname, hp->name, hdrcksum, chksum(hp));
643 fprint(2, "%s: skipping past archive header with bad checksum in %s...",
646 hp = getblkrd(ar, Alldata);
648 sysfatal("unexpected EOF looking for archive header in %s",
650 hdrcksum = parsecksum(hp->chksum, name(hp));
651 } while (hdrcksum == -1 || chksum(hp) != hdrcksum);
652 fprint(2, "found %s\n", name(hp));
654 nexthdr += Tblock*(1 + BYTES2TBLKS(arsize(hp)));
663 * if name is longer than Namsiz bytes, try to split it at a slash and fit the
664 * pieces into hp->prefix and hp->name.
667 putfullname(Hdr *hp, char *name)
671 String *slname = nil;
675 s_append(slname, name);
676 s_append(slname, "/"); /* posix requires this */
677 name = s_to_c(slname);
680 namlen = strlen(name);
681 if (namlen <= Namsiz) {
682 strncpy(hp->name, name, Namsiz);
683 hp->prefix[0] = '\0'; /* ustar paranoia */
687 if (!posix || namlen > Maxname) {
688 fprint(2, "%s: name too long for tar header: %s\n",
693 * try various splits until one results in pieces that fit into the
694 * appropriate fields of the header. look for slashes from right
695 * to left, in the hopes of putting the largest part of the name into
696 * hp->prefix, which is larger than hp->name.
698 sl = strrchr(name, '/');
701 if (pfxlen <= sizeof hp->prefix && namlen-1 - pfxlen <= Namsiz)
705 sl = strrchr(name, '/');
709 fprint(2, "%s: name can't be split to fit tar header: %s\n",
714 strncpy(hp->prefix, name, sizeof hp->prefix);
716 strncpy(hp->name, sl, sizeof hp->name);
723 mkhdr(Hdr *hp, Dir *dir, char *file)
728 * some of these fields run together, so we format them left-to-right
729 * and don't use snprint.
731 sprint(hp->mode, "%6lo ", dir->mode & 0777);
732 sprint(hp->uid, "%6o ", aruid);
733 sprint(hp->gid, "%6o ", argid);
734 if (dir->length >= (Off)1<<32) {
739 fprint(2, "%s: storing large sizes in \"base 256\"\n", argv0);
741 hp->size[0] = Binsize;
742 /* emit so-called `base 256' representation of size */
743 putbe((uchar *)hp->size+1, dir->length, sizeof hp->size - 2);
744 hp->size[sizeof hp->size - 1] = ' ';
746 sprint(hp->size, "%11lluo ", dir->length);
747 sprint(hp->mtime, "%11luo ", dir->mtime);
748 hp->linkflag = (dir->mode&DMDIR? LF_DIR: LF_PLAIN1);
749 r = putfullname(hp, file);
751 strncpy(hp->magic, "ustar", sizeof hp->magic);
752 strncpy(hp->version, "00", sizeof hp->version);
753 strncpy(hp->uname, dir->uid, sizeof hp->uname);
754 strncpy(hp->gname, dir->gid, sizeof hp->gname);
756 sprint(hp->chksum, "%6luo", chksum(hp));
760 static void addtoar(int ar, char *file, char *shortf);
763 addtreetoar(int ar, char *file, char *shortf, int fd)
767 String *name = s_new();
769 n = dirreadall(fd, &dirents);
771 fprint(2, "%s: dirreadall %s: %r\n", argv0, file);
776 if (chdir(shortf) < 0)
777 sysfatal("chdir %s: %r", file);
779 fprint(2, "chdir %s\t# %s\n", shortf, file);
781 for (dent = dirents; dent < dirents + n; dent++) {
783 s_append(name, file);
785 s_append(name, dent->name);
786 addtoar(ar, s_to_c(name), dent->name);
792 * this assumes that shortf is just one component, which is true
793 * during directory descent, but not necessarily true of command-line
794 * arguments. Our caller (or addtoar's) must reset the working
795 * directory if necessary.
798 sysfatal("chdir %s/..: %r", file);
800 fprint(2, "chdir ..\n");
804 addtoar(int ar, char *file, char *shortf)
807 long bytes, blksread;
813 if (shortf[0] == '#') {
815 s_append(name, "./");
816 s_append(name, shortf);
817 shortf = s_to_c(name);
821 fprint(2, "opening %s # %s\n", shortf, file);
822 fd = open(shortf, OREAD);
824 fprint(2, "%s: can't open %s: %r\n", argv0, file);
831 sysfatal("can't fstat %s: %r", file);
834 isdir = (dir->qid.type & QTDIR) != 0;
835 if (mkhdr(hbp, dir, file) < 0) {
845 blksleft = BYTES2TBLKS(dir->length);
849 addtreetoar(ar, file, shortf, fd);
851 for (; blksleft > 0; blksleft -= blksread) {
853 blksread = gothowmany(blksleft);
854 assert(blksread >= 0);
855 bytes = blksread * Tblock;
856 n = ereadn(file, fd, hbp->data, bytes);
859 * ignore EOF. zero any partial block to aid
860 * compression and emergency recovery of data.
863 memset(hbp->data + n, 0, bytes - n);
864 putblkmany(ar, blksread);
868 fprint(2, "%s\n", file);
878 ulong blksleft, blksread;
881 Compress *comp = nil;
884 if (usefile && docreate)
885 ar = create(usefile, OWRITE, 0666);
887 ar = open(usefile, ORDWR);
890 if (docreate && docompress) {
891 comp = compmethod(usefile);
893 ar = push(ar, comp->comp, Output, &ps);
896 sysfatal("can't open archive %s: %r", usefile);
898 if (usefile && !docreate) {
899 /* skip quickly to the end */
900 while ((hp = readhdr(ar)) != nil) {
902 for (blksleft = BYTES2TBLKS(bytes);
903 blksleft > 0 && getblkrd(ar, Justnxthdr) != nil;
904 blksleft -= blksread) {
905 blksread = gothowmany(blksleft);
906 putreadblks(ar, blksread);
910 * we have just read the end-of-archive Tblock.
911 * now seek back over the (big) archive block containing it,
912 * and back up curblk ptr over end-of-archive Tblock in memory.
914 if (seek(ar, blkoff, 0) < 0)
915 sysfatal("can't seek back over end-of-archive in %s: %r",
920 for (i = 0; argv[i] != nil; i++) {
921 addtoar(ar, argv[i], argv[i]);
922 chdir(origdir); /* for correctness & profiling */
925 /* write end-of-archive marker */
932 return pushclose(&ps);
942 /* is pfx a file-name prefix of name? */
944 prefix(char *name, char *pfx)
946 char clpfx[Maxlongname+1];
947 int pfxlen = strlen(pfx);
949 clpfx[Maxlongname] = '\0';
950 strncpy(clpfx, pfx, Maxlongname);
952 return strncmp(clpfx, name, pfxlen) == 0 &&
953 (name[pfxlen] == '\0' || name[pfxlen] == '/');
957 match(char *name, char **argv)
959 char clname[Maxlongname+1];
964 clname[Maxlongname] = '\0';
965 strncpy(clname, name, Maxlongname);
967 for (i = 0; argv[i] != nil; i++)
968 if (prefix(clname, argv[i]))
974 cantcreate(char *s, int mode)
980 * Always print about files. Only print about directories
981 * we haven't printed about. (Assumes archive is ordered
986 /* already printed this directory */
987 if(strcmp(s, last) == 0)
989 /* printed a higher directory, so printed this one */
991 if(memcmp(s, last, len) == 0 && last[len] == '/')
998 fprint(2, "%s: can't create %s: %r\n", argv0, s);
1006 if (access(s, AEXIST) == 0)
1008 f = create(s, OREAD, DMDIR | 0777);
1012 cantcreate(s, DMDIR);
1024 while (!err && (p = strchr(p+1, '/')) != nil) {
1026 err = (access(s, AEXIST) < 0 && makedir(s) < 0);
1032 /* Call access but preserve the error string. */
1034 xaccess(char *name, int mode)
1040 errstr(err, sizeof err);
1041 rv = access(name, mode);
1042 errstr(err, sizeof err);
1047 openfname(Hdr *hp, char *fname, int dir, int mode)
1053 switch (hp->linkflag) {
1058 fprint(2, "%s: can't make (sym)link %s\n",
1062 fprint(2, "%s: can't make fifo %s\n", argv0, fname);
1065 if (!keepexisting || access(fname, AEXIST) < 0) {
1066 int rw = (dir? OREAD: OWRITE);
1068 fd = create(fname, rw, mode);
1071 fd = create(fname, rw, mode);
1073 if (fd < 0 && (!dir || xaccess(fname, AEXIST) < 0))
1074 cantcreate(fname, mode);
1076 if (fd >= 0 && verbose)
1077 fprint(2, "%s\n", fname);
1083 /* copy from archive to file system (or nowhere for table-of-contents) */
1085 copyfromar(int ar, int fd, char *fname, ulong blksleft, Off bytes)
1091 if (blksleft == 0 || bytes < 0)
1093 for (; blksleft > 0; blksleft -= blksread) {
1094 hbp = getblkrd(ar, (fd >= 0? Alldata: Justnxthdr));
1096 sysfatal("unexpected EOF on archive extracting %s from %s",
1098 blksread = gothowmany(blksleft);
1099 if (blksread <= 0) {
1100 fprint(2, "%s: got %ld blocks reading %s!\n",
1101 argv0, blksread, fname);
1104 wrbytes = Tblock*blksread;
1108 assert(wrbytes >= 0);
1110 ewrite(fname, fd, hbp->data, wrbytes);
1111 putreadblks(ar, blksread);
1116 fprint(2, "%s: %lld bytes uncopied at EOF on archive %s; "
1117 "%s not fully extracted\n", argv0, bytes, arname, fname);
1121 wrmeta(int fd, Hdr *hp, long mtime, int mode) /* update metadata */
1140 * copy a file from the archive into the filesystem.
1141 * fname is result of name(), so has two extra bytes at beginning.
1144 extract1(int ar, Hdr *hp, char *fname)
1146 int fd = -1, dir = 0;
1147 long mtime = strtol(hp->mtime, nil, 8);
1148 ulong mode = strtoul(hp->mode, nil, 8) & 0777;
1149 Off bytes = hdrsize(hp); /* for printing */
1150 ulong blksleft = BYTES2TBLKS(arsize(hp));
1152 /* fiddle name, figure out mode and blocks */
1157 switch (hp->linkflag) {
1168 else if(fname[0] == '#'){
1174 fd = openfname(hp, fname, dir, mode);
1176 char *cp = ctime(mtime);
1178 print("%M %8lld %-12.12s %-4.4s %s\n",
1179 mode, bytes, cp+4, cp+24, fname);
1181 print("%s\n", fname);
1183 copyfromar(ar, fd, fname, blksleft, bytes);
1185 /* touch up meta data and close */
1188 * directories should be wstated *after* we're done
1189 * creating files in them, but we don't do that.
1192 wrmeta(fd, hp, mtime, mode);
1198 skip(int ar, Hdr *hp, char *fname)
1200 ulong blksleft, blksread;
1203 for (blksleft = BYTES2TBLKS(arsize(hp)); blksleft > 0;
1204 blksleft -= blksread) {
1205 hbp = getblkrd(ar, Justnxthdr);
1207 sysfatal("unexpected EOF on archive extracting %s from %s",
1209 blksread = gothowmany(blksleft);
1210 putreadblks(ar, blksread);
1215 getname(int ar, Hdr *hp)
1217 static char namebuf[Maxlongname+1], *nextname = nil;
1218 ulong blksleft, blksread;
1222 if(nextname != nil && nextname[0] != '\0'){
1223 fname = nextname, nextname = nil;
1227 if(hp->linkflag == LF_LONGNAME){
1229 for (blksleft = BYTES2TBLKS(arsize(hp)); blksleft > 0;
1230 blksleft -= blksread) {
1231 hp = getblkrd(ar, Alldata);
1233 sysfatal("unexpected EOF on archive reading %s from %s",
1235 blksread = gothowmany(blksleft);
1236 n = &namebuf[Maxlongname] - p;
1237 if(Tblock*blksread < n)
1238 n = Tblock*blksread;
1239 memmove(p, hp->data, n);
1241 putreadblks(ar, blksread);
1247 namebuf[Maxlongname] = '\0';
1248 strncpy(namebuf, fname, Maxlongname);
1255 extract(char **argv)
1264 ar = open(usefile, OREAD);
1267 comp = compmethod(usefile);
1269 ar = push(ar, comp->decomp, Input, &ps);
1271 sysfatal("can't open archive %s: %r", usefile);
1273 while ((hp = readhdr(ar)) != nil) {
1274 longname = getname(ar, hp);
1277 if (match(longname, argv))
1278 extract1(ar, hp, longname);
1280 skip(ar, hp, longname);
1284 return pushclose(&ps);
1291 main(int argc, char *argv[])
1296 fmtinstall('M', dirmodefmt);
1304 usefile = arname = EARGF(usage());
1307 argid = strtoul(EARGF(usage()), 0, 0);
1315 case 'm': /* compatibility */
1340 aruid = strtoul(EARGF(usage()), 0, 0);
1354 fprint(2, "tar: unknown letter %C\n", TARGC());
1359 if (argc < 0 || errflg)
1366 ret = extract(argv);
1369 if (getwd(origdir, sizeof origdir) == nil)
1370 strcpy(origdir, "/tmp");
1371 ret = replace(argv);