diff options
author | cinap_lenrek <cinap_lenrek@felloff.net> | 2015-09-08 18:27:48 +0200 |
---|---|---|
committer | cinap_lenrek <cinap_lenrek@felloff.net> | 2015-09-08 18:27:48 +0200 |
commit | c7eae3fb729cf1831dc3f711444f2edf782a3e7d (patch) | |
tree | d0716b51a9cb81d947cb60db5af60e4f6ee5e6eb /sys/src/cmd/tar.c | |
parent | 4d4b825dea3c41bdad7d10ae32e7d04c1a3712d0 (diff) |
tar: make z flag work, even when no file name was provided (thanks aiju)
tar used to infer compression type from the filenames extension, but when
no file name is given (stdin/stdout), the -z flag was ignored and no
compression filter applied. this changes tar to assume the default
gzip compression method when z is given and no file name is specified.
Diffstat (limited to 'sys/src/cmd/tar.c')
-rw-r--r-- | sys/src/cmd/tar.c | 39 |
1 files changed, 22 insertions, 17 deletions
diff --git a/sys/src/cmd/tar.c b/sys/src/cmd/tar.c index 242476024..49defda64 100644 --- a/sys/src/cmd/tar.c +++ b/sys/src/cmd/tar.c @@ -222,16 +222,20 @@ ewrite(char *name, int fd, void *buf, long len) static Compress * compmethod(char *name) { - int i, nmlen = strlen(name), sfxlen; - Compress *cp; - - for (cp = comps; cp < comps + nelem(comps); cp++) - for (i = 0; i < nelem(cp->sfx) && cp->sfx[i]; i++) { - sfxlen = strlen(cp->sfx[i]); - if (nmlen > sfxlen && - strcmp(cp->sfx[i], name + nmlen - sfxlen) == 0) - return cp; + if (name) { + int i, nmlen, sfxlen; + Compress *cp; + + nmlen = strlen(name); + for (cp = comps; cp < comps + nelem(comps); cp++) { + for (i = 0; i < nelem(cp->sfx) && cp->sfx[i]; i++) { + sfxlen = strlen(cp->sfx[i]); + if (nmlen > sfxlen && + strcmp(cp->sfx[i], name + nmlen - sfxlen) == 0) + return cp; + } } + } return docompress? comps: nil; } @@ -880,14 +884,15 @@ replace(char **argv) if (usefile && docreate) { ar = create(usefile, OWRITE, 0666); - if (docompress) - comp = compmethod(usefile); } else if (usefile) ar = open(usefile, ORDWR); else ar = Stdout; - if (comp) - ar = push(ar, comp->comp, Output, &ps); + if (docreate && docompress) { + comp = compmethod(usefile); + if (comp) + ar = push(ar, comp->comp, Output, &ps); + } if (ar < 0) sysfatal("can't open archive %s: %r", usefile); @@ -1253,14 +1258,14 @@ extract(char **argv) int ar; char *longname; Hdr *hp; - Compress *comp = nil; + Compress *comp; Pushstate ps; - if (usefile) { + if (usefile) ar = open(usefile, OREAD); - comp = compmethod(usefile); - } else + else ar = Stdin; + comp = compmethod(usefile); if (comp) ar = push(ar, comp->decomp, Input, &ps); if (ar < 0) |