Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/netbsd-2-0]: src/usr.bin/gzip Pull up revision 1.46 (requested by mrg in...
details: https://anonhg.NetBSD.org/src/rev/bf465fb7d758
branches: netbsd-2-0
changeset: 561228:bf465fb7d758
user: tron <tron%NetBSD.org@localhost>
date: Sun May 30 14:48:21 2004 +0000
description:
Pull up revision 1.46 (requested by mrg in ticket #420):
- 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 | 435 +++++++++++++++++++++++++++++++--------------------
1 files changed, 263 insertions(+), 172 deletions(-)
diffs (truncated from 898 to 300 lines):
diff -r 69e9ad4063f4 -r bf465fb7d758 usr.bin/gzip/gzip.c
--- a/usr.bin/gzip/gzip.c Sun May 30 14:45:06 2004 +0000
+++ b/usr.bin/gzip/gzip.c Sun May 30 14:48:21 2004 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: gzip.c,v 1.29.2.9 2004/05/30 14:45:06 tron Exp $ */
+/* $NetBSD: gzip.c,v 1.29.2.10 2004/05/30 14:48:21 tron 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.29.2.9 2004/05/30 14:45:06 tron Exp $");
+__RCSID("$NetBSD: gzip.c,v 1.29.2.10 2004/05/30 14:48:21 tron 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,20 +122,22 @@
#define qflag 0
#endif
-static char *suffix;
+static int exit_value = 0; /* exit value */
+
+static const char *suffix;
#define suffix_len (strlen(suffix) + 1) /* len + nul */
-static char *newfile; /* name of newly created file */
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 file_compress(char *);
-static off_t file_uncompress(char *);
+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 *);
static void handle_file(char *, struct stat *);
static void handle_stdin(void);
@@ -150,7 +151,7 @@
static void prepend_gzip(char *, int *, char ***);
static void handle_dir(char *, struct stat *);
static void print_verbage(char *, char *, off_t, off_t);
-static void print_test(char *, int);
+static void print_test(const char *, int);
static void copymodes(const char *, struct stat *);
#endif
@@ -303,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 */
@@ -317,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, ...)
{
@@ -329,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;
@@ -342,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;
@@ -356,7 +362,7 @@
vwarnx(fmt, ap);
va_end(ap);
}
- exit(rv);
+ exit(2);
}
#ifndef SMALL
@@ -375,7 +381,7 @@
for (; *s; s++)
if (*s == ' ' || *s == '\t')
break;
- if (*s == 0)
+ if (*s == 0x0)
break;
}
/* punt early */
@@ -387,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;
@@ -399,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;
@@ -439,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);
@@ -455,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;
@@ -471,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;
@@ -483,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 */
@@ -492,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;
@@ -507,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,
@@ -520,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;
@@ -539,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)
Home |
Main Index |
Thread Index |
Old Index