ref: c7eae3fb729cf1831dc3f711444f2edf782a3e7d
parent: 4d4b825dea3c41bdad7d10ae32e7d04c1a3712d0
author: cinap_lenrek <[email protected]>
date: Tue Sep 8 14:27:48 EDT 2015
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.
--- a/sys/src/cmd/tar.c
+++ b/sys/src/cmd/tar.c
@@ -222,16 +222,20 @@
static Compress *
compmethod(char *name)
{
- int i, nmlen = strlen(name), sfxlen;
- Compress *cp;
+ if (name) {
+ int i, nmlen, 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;
+ 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 @@
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 @@
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)