Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/gzip - fix "gzip -t" to not output anything by defau...



details:   https://anonhg.NetBSD.org/src/rev/0401a2ac399b
branches:  trunk
changeset: 566939:0401a2ac399b
user:      mrg <mrg%NetBSD.org@localhost>
date:      Tue May 25 04:34:40 2004 +0000

description:
- fix "gzip -t" to not output anything by default.  PR#25507
- fix any decompression on corrupted gzip files.  PR#25508
- ask to overwrite files if we have a tty, rather than failing the
  operation.  PR#25509.
- clean up maybe_err()/maybe_warn(): use maybe_err() only for fatal
  errors.  maybe_warn() is for processing errors.  this allows
  "gzip -d file1.gz file2.gz" to decompress file2.gz even if file1.gz
  is corrupted, etc.
- change the internal compressor/decompressor API to return "-1" on
  failure, not 0.  this allows for 0-sized files to be decompressed
  correctly.

diffstat:

 usr.bin/gzip/gzip.c        |  376 ++++++++++++++++++++++++++++----------------
 usr.bin/gzip/unbzip2.c     |   30 ++-
 usr.bin/gzip/zuncompress.c |    4 +-
 3 files changed, 255 insertions(+), 155 deletions(-)

diffs (truncated from 880 to 300 lines):

diff -r 1020b57209a7 -r 0401a2ac399b usr.bin/gzip/gzip.c
--- a/usr.bin/gzip/gzip.c       Tue May 25 04:33:59 2004 +0000
+++ b/usr.bin/gzip/gzip.c       Tue May 25 04:34:40 2004 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gzip.c,v 1.45 2004/05/21 12:16:10 agc Exp $    */
+/*     $NetBSD: gzip.c,v 1.46 2004/05/25 04:34:40 mrg Exp $    */
 
 /*
  * Copyright (c) 1997, 1998, 2003, 2004 Matthew R. Green
@@ -32,7 +32,7 @@
 #ifndef lint
 __COPYRIGHT("@(#) Copyright (c) 1997, 1998, 2003, 2004 Matthew R. Green\n\
      All rights reserved.\n");
-__RCSID("$NetBSD: gzip.c,v 1.45 2004/05/21 12:16:10 agc Exp $");
+__RCSID("$NetBSD: gzip.c,v 1.46 2004/05/25 04:34:40 mrg Exp $");
 #endif /* not lint */
 
 /*
@@ -42,7 +42,6 @@
  *     - handle .taz/.tgz files?
  *     - use mmap where possible
  *     - handle some signals better (remove outfile?)
- *     - audit maybe_err()/maybe_warn() usage
  *     - make bzip2/compress -v/-t/-l support work as well as possible
  */
 
@@ -103,7 +102,7 @@
 
 #define OS_CODE                3       /* Unix */
 
-static const char      gzip_version[] = "NetBSD gzip 20040427";
+static const char      gzip_version[] = "NetBSD gzip 20040524";
 
 static int     cflag;                  /* stdout mode */
 static int     dflag;                  /* decompress mode */
@@ -123,17 +122,20 @@
 #define                qflag   0
 #endif
 
+static int     exit_value = 0;         /* exit value */
+
 static const char      *suffix;
 #define suffix_len     (strlen(suffix) + 1)    /* len + nul */
 static char    *infile;                /* name of file coming in */
 
-static void    maybe_err(int rv, const char *fmt, ...);
-static void    maybe_errx(int rv, const char *fmt, ...);
+static void    maybe_err(const char *fmt, ...);
+static void    maybe_errx(const char *fmt, ...);
 static void    maybe_warn(const char *fmt, ...);
 static void    maybe_warnx(const char *fmt, ...);
 static enum filetype file_gettype(u_char *);
+static int     check_outfile(const char *outfile, struct stat *sb);
 static off_t   gz_compress(FILE *, int, off_t *, const char *, time_t);
-static off_t   gz_uncompress(int, int, char *, size_t, off_t *);
+static off_t   gz_uncompress(int, int, char *, size_t, off_t *, const char *);
 static off_t   file_compress(char *, char *, size_t);
 static off_t   file_uncompress(char *, char *, size_t);
 static void    handle_pathname(char *);
@@ -302,7 +304,7 @@
        if (qflag == 0 && lflag && argc > 1)
                print_list(-1, 0, "(totals)", 0);
 #endif
-       exit(0);
+       exit(exit_value);
 }
 
 /* maybe print a warning */
@@ -316,8 +318,11 @@
                vwarn(fmt, ap);
                va_end(ap);
        }
+       if (exit_value == 0)
+               exit_value = 1;
 }
 
+/* ... without an errno. */
 void
 maybe_warnx(const char *fmt, ...)
 {
@@ -328,11 +333,13 @@
                vwarnx(fmt, ap);
                va_end(ap);
        }
+       if (exit_value == 0)
+               exit_value = 1;
 }
 
-/* maybe print a warning */
+/* maybe print an error */
 void
-maybe_err(int rv, const char *fmt, ...)
+maybe_err(const char *fmt, ...)
 {
        va_list ap;
 
@@ -341,12 +348,12 @@
                vwarn(fmt, ap);
                va_end(ap);
        }
-       exit(rv);
+       exit(2);
 }
 
-/* maybe print a warning */
+/* ... without an errno. */
 void
-maybe_errx(int rv, const char *fmt, ...)
+maybe_errx(const char *fmt, ...)
 {
        va_list ap;
 
@@ -355,7 +362,7 @@
                vwarnx(fmt, ap);
                va_end(ap);
        }
-       exit(rv);
+       exit(2);
 }
 
 #ifndef SMALL
@@ -386,7 +393,7 @@
 
        nargv = (char **)malloc((*argc + 1) * sizeof(char *));
        if (nargv == NULL)
-               maybe_err(1, "malloc");
+               maybe_err("malloc");
 
        /* stash this away */
        *argv = nargv;
@@ -398,7 +405,7 @@
        /* take a copy of $GZIP and add it to the array */
        s = strdup(gzip);
        if (s == NULL)
-               maybe_err(1, "strdup");
+               maybe_err("strdup");
        for (; *s; s++) {
                if (*s == ' ' || *s == '\t')
                        continue;
@@ -438,11 +445,14 @@
                     (int)(mtime >> 24) & 0xff,
                     0, OS_CODE, origname ? origname : "");
        if (i == -1)     
-               maybe_err(1, "asprintf");
+               maybe_err("asprintf");
        if (origname)
                i++;
-       if (write(out, str, i) != i)
-               maybe_err(1, "write");
+       if (write(out, str, i) != i) {
+               maybe_warn("write");
+               in_tot = -1;
+               goto out;
+       }
        free(str);
 
        memset(&z, 0, sizeof z);
@@ -454,14 +464,20 @@
 
        error = deflateInit2(&z, numflag, Z_DEFLATED,
                             -MAX_WBITS, 8, Z_DEFAULT_STRATEGY);
-       if (error != Z_OK)
-               maybe_errx(1, "deflateInit2 failed");
+       if (error != Z_OK) {
+               maybe_warnx("deflateInit2 failed");
+               in_tot = -1;
+               goto out;
+       }
 
        crc = crc32(0L, Z_NULL, 0);
        for (;;) {
                if (z.avail_out == 0) {
-                       if (write(out, outbuf, sizeof outbuf) != sizeof outbuf)
-                               maybe_err(1, "write");
+                       if (write(out, outbuf, sizeof outbuf) != sizeof outbuf) {
+                               maybe_warn("write");
+                               in_tot = -1;
+                               goto out;
+                       }
 
                        out_tot += sizeof outbuf;
                        z.next_out = outbuf;
@@ -470,8 +486,11 @@
 
                if (z.avail_in == 0) {
                        in_size = fread(inbuf, 1, sizeof inbuf, in);
-                       if (ferror(in))
-                               maybe_err(1, "fread");
+                       if (ferror(in)) {
+                               maybe_warn("fread");
+                               in_tot = -1;
+                               goto out;
+                       }
                        if (in_size == 0)
                                break;
 
@@ -482,8 +501,11 @@
                }
 
                error = deflate(&z, Z_NO_FLUSH);
-               if (error != Z_OK && error != Z_STREAM_END)
-                       maybe_errx(1, "deflate failed");
+               if (error != Z_OK && error != Z_STREAM_END) {
+                       maybe_warnx("deflate failed");
+                       in_tot = -1;
+                       goto out;
+               }
        }
 
        /* clean up */
@@ -491,13 +513,19 @@
                size_t len;
 
                error = deflate(&z, Z_FINISH);
-               if (error != Z_OK && error != Z_STREAM_END)
-                       maybe_errx(1, "deflate failed");
+               if (error != Z_OK && error != Z_STREAM_END) {
+                       maybe_warnx("deflate failed");
+                       in_tot = -1;
+                       goto out;
+               }
 
                len = sizeof outbuf - z.avail_out;
 
-               if (write(out, outbuf, len) != len)
-                       maybe_err(1, "write");
+               if (write(out, outbuf, len) != len) {
+                       maybe_warn("write");
+                       out_tot = -1;
+                       goto out;
+               }
                out_tot += len;
                z.next_out = outbuf;
                z.avail_out = sizeof outbuf;
@@ -506,8 +534,11 @@
                        break;
        }
 
-       if (deflateEnd(&z) != Z_OK)
-               maybe_errx(1, "deflateEnd failed");
+       if (deflateEnd(&z) != Z_OK) {
+               maybe_warnx("deflateEnd failed");
+               in_tot = -1;
+               goto out;
+       }
 
        i = asprintf(&str, "%c%c%c%c%c%c%c%c", 
                 (int)crc & 0xff,
@@ -519,14 +550,14 @@
                 (int)(in_tot >> 16) & 0xff,
                 (int)(in_tot >> 24) & 0xff);
        if (i != 8)
-               maybe_err(1, "asprintf");
-       if (write(out, str, i) != i)
-               maybe_err(1, "write");
+               maybe_err("asprintf");
+       if (write(out, str, i) != i) {
+               maybe_warn("write");
+               in_tot = -1;
+       }
        free(str);
 
-       if (fclose(in) != 0)
-               maybe_err(1, "failed fclose");
-
+out:
        if (gsizep)
                *gsizep = out_tot;
        return in_tot;
@@ -538,7 +569,8 @@
  * into `*gsizep'.
  */
 static off_t
-gz_uncompress(int in, int out, char *pre, size_t prelen, off_t *gsizep)
+gz_uncompress(int in, int out, char *pre, size_t prelen, off_t *gsizep,
+             const char *filename)
 {
        z_stream z;
        char outbuf[BUFLEN], inbuf[BUFLEN];
@@ -582,13 +614,12 @@
 
                        if (in_size == -1) {
 #ifndef SMALL
-                               if (tflag) {
-                                       print_test("(stdin)", 0);
-                                       return 0;
-                               }
+                               if (tflag && vflag)
+                                       print_test(filename, 0);
 #endif
                                maybe_warn("failed to read stdin\n");
-                               return -1;
+                               out_tot = -1;
+                               goto stop;
                        } else if (in_size == 0)
                                done_reading = 1;
 
@@ -599,23 +630,32 @@
                }
                switch (state) {



Home | Main Index | Thread Index | Old Index