nano-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Nano-devel] Fwd: .gz support, DONE


From: Mike Frysinger
Subject: Re: [Nano-devel] Fwd: .gz support, DONE
Date: Fri, 8 Jan 2016 15:39:23 -0500

On 08 Jan 2016 21:22, Benno Schulenberg wrote:
> Tilman Weiers wrote:
> +/* Does the given filename end in ".gz"? */
> +int isgz(char *fname)
> +{
> +    char *strend;
> +    int len;
> +
> +    if (fname == NULL)
> +     return -1;
> +
> +    len = strlen(fname);
> +    if (len < 3)
> +     return -1;
> +
> +    strend = malloc(4 * sizeof(char));
> +    sprintf(strend, "%c%c%c", fname[len - 3], fname[len - 2], fname[len - 
> 1]);
> +
> +    if (strcmp(strend, ".gz") == 0) {
> +     free(strend);
> +
> +     /* The filename ends in ".gz", but do we have gzip? */
> +     if (system("gzip --version > /dev/null 2>&1") != 0)
> +         return -2;
> +
> +     /* Use gzip to handle the file. */
> +     return 0;
> +    }
> +
> +    free(strend);
> +    return -1;
> +}

this malloc logic is unnecessary and wasteful.  just do:
  size_t len = strlen(fname);
  if (strcmp(fname + len - 3, ".gz") == 0)

> +     /* The file is A-OK.  Open it, via gzip or directly. */
> +     if (isgz(full_filename) == 0) {
> +         char *open_cmd = (char *) nmalloc(sizeof(char) * (20 + 
> strlen(full_filename)));
> +         sprintf(open_cmd, "gzip -cdfq \'%s\'", full_filename);
> +         *f = popen(open_cmd, "r");
> +         free(open_cmd);
> +     } else
> +         *f = fdopen(fd, "rb");

imo we really should be using zlib directly instead of shelling (ugh) out
to random tools
-mike

Attachment: signature.asc
Description: Digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]