Module Name:    src
Committed By:   snj
Date:           Sun Mar 15 21:15:11 UTC 2015

Modified Files:
        src/usr.bin/gzip [netbsd-5-1]: Makefile gzip.1 gzip.c zuncompress.c

Log Message:
Pull up following revision(s) (requested by mrg in ticket #1951):
        usr.bin/gzip/Makefile: patch
        usr.bin/gzip/gzip.1: revisions 1.20-1.24
        usr.bin/gzip/gzip.c: revisions 1.98-1.107
        usr.bin/gzip/zuncompress.c: revisions 1.7-1.8
Sync gzip core with HEAD.


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.12.12.1 src/usr.bin/gzip/Makefile
cvs rdiff -u -r1.19 -r1.19.12.1 src/usr.bin/gzip/gzip.1
cvs rdiff -u -r1.93.4.3 -r1.93.4.3.2.1 src/usr.bin/gzip/gzip.c
cvs rdiff -u -r1.6.36.1 -r1.6.36.2 src/usr.bin/gzip/zuncompress.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/gzip/Makefile
diff -u src/usr.bin/gzip/Makefile:1.12 src/usr.bin/gzip/Makefile:1.12.12.1
--- src/usr.bin/gzip/Makefile:1.12	Fri Aug 29 00:02:24 2008
+++ src/usr.bin/gzip/Makefile	Sun Mar 15 21:15:11 2015
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.12 2008/08/29 00:02:24 gmcgarry Exp $
+#	$NetBSD: Makefile,v 1.12.12.1 2015/03/15 21:15:11 snj Exp $
 
 USE_FORT?= yes	# data-driven bugs?
 
@@ -30,3 +30,6 @@ LINKS+=		${BINDIR}/gzip ${BINDIR}/gunzip
 .if (defined(HAVE_GCC) && ${HAVE_GCC} == 4) || defined(HAVE_PCC)
 COPTS.gzip.c+=	-Wno-pointer-sign
 .endif
+
+# NetBSD-5 has no -lzma
+CPPFLAGS+=	-DNO_XZ_SUPPORT

Index: src/usr.bin/gzip/gzip.1
diff -u src/usr.bin/gzip/gzip.1:1.19 src/usr.bin/gzip/gzip.1:1.19.12.1
--- src/usr.bin/gzip/gzip.1:1.19	Thu May 29 14:51:27 2008
+++ src/usr.bin/gzip/gzip.1	Sun Mar 15 21:15:11 2015
@@ -1,4 +1,4 @@
-.\"	$NetBSD: gzip.1,v 1.19 2008/05/29 14:51:27 mrg Exp $
+.\"	$NetBSD: gzip.1,v 1.19.12.1 2015/03/15 21:15:11 snj Exp $
 .\"
 .\" Copyright (c) 1997, 2003, 2004 Matthew R. Green
 .\" All rights reserved.
@@ -24,7 +24,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd April 27, 2004
+.Dd January 13, 2015
 .Dt GZIP 1
 .Os
 .Sh NAME
@@ -138,8 +138,8 @@ embedded in the file.
 This option causes the stored filename in the input file to be used
 as the output file.
 .It Fl n , -no-name
-This option stops the filename from being stored in the output
-file.
+This option stops the filename and timestamp from being stored in
+the output file.
 .It Fl q , -quiet
 With this option, no warnings or errors are printed.
 .It Fl r , -recursive
@@ -171,6 +171,7 @@ Options on the command line will overrid
 .Sh SEE ALSO
 .Xr bzip2 1 ,
 .Xr compress 1 ,
+.Xr xz 1 ,
 .Xr fts 3 ,
 .Xr zlib 3
 .Sh HISTORY
@@ -190,9 +191,9 @@ program for
 This manual documents
 .Nx
 .Nm
-version 20040427.
+version 20150113.
 .Sh AUTHORS
 This implementation of
 .Nm
 was written by
-.An Matthew R. Green Aq m...@eterna.com.au .
+.An Matthew R. Green Aq Mt m...@eterna.com.au .

Index: src/usr.bin/gzip/gzip.c
diff -u src/usr.bin/gzip/gzip.c:1.93.4.3 src/usr.bin/gzip/gzip.c:1.93.4.3.2.1
--- src/usr.bin/gzip/gzip.c:1.93.4.3	Sun Nov  8 22:55:24 2009
+++ src/usr.bin/gzip/gzip.c	Sun Mar 15 21:15:11 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: gzip.c,v 1.93.4.3 2009/11/08 22:55:24 snj Exp $	*/
+/*	$NetBSD: gzip.c,v 1.93.4.3.2.1 2015/03/15 21:15:11 snj Exp $	*/
 
 /*
  * Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green
@@ -30,7 +30,7 @@
 #ifndef lint
 __COPYRIGHT("@(#) Copyright (c) 1997, 1998, 2003, 2004, 2006\
  Matthew R. Green.  All rights reserved.");
-__RCSID("$NetBSD: gzip.c,v 1.93.4.3 2009/11/08 22:55:24 snj Exp $");
+__RCSID("$NetBSD: gzip.c,v 1.93.4.3.2.1 2015/03/15 21:15:11 snj Exp $");
 #endif /* not lint */
 
 /*
@@ -81,6 +81,9 @@ enum filetype {
 #ifndef NO_PACK_SUPPORT
 	FT_PACK,
 #endif
+#ifndef NO_XZ_SUPPORT
+	FT_XZ,
+#endif
 	FT_LAST,
 	FT_UNKNOWN
 };
@@ -101,6 +104,12 @@ enum filetype {
 #define PACK_MAGIC	"\037\036"
 #endif
 
+#ifndef NO_XZ_SUPPORT
+#include <lzma.h>
+#define XZ_SUFFIX	".xz"
+#define XZ_MAGIC	"\3757zXZ"
+#endif
+
 #define GZ_SUFFIX	".gz"
 
 #define BUFLEN		(64 * 1024)
@@ -141,6 +150,9 @@ static suffixes_t suffixes[] = {
 #ifndef NO_COMPRESS_SUPPORT
 	SUFFIX(Z_SUFFIX,	""),
 #endif
+#ifndef NO_XZ_SUPPORT
+	SUFFIX(XZ_SUFFIX,	""),
+#endif
 	SUFFIX(GZ_SUFFIX,	""),	/* Overwritten by -S "" */
 #endif /* SMALL */
 #undef SUFFIX
@@ -148,7 +160,7 @@ static suffixes_t suffixes[] = {
 #define NUM_SUFFIXES (sizeof suffixes / sizeof suffixes[0])
 #define SUFFIX_MAXLEN	30
 
-static	const char	gzip_version[] = "NetBSD gzip 20091011";
+static	const char	gzip_version[] = "NetBSD gzip 20150113";
 
 static	int	cflag;			/* stdout mode */
 static	int	dflag;			/* decompress mode */
@@ -157,6 +169,7 @@ static	int	numflag = 6;		/* gzip -1..-9 
 
 #ifndef SMALL
 static	int	fflag;			/* force mode */
+static	int	kflag;			/* don't delete input files */
 static	int	nflag;			/* don't save name/timestamp */
 static	int	Nflag;			/* don't restore name/timestamp */
 static	int	qflag;			/* quiet mode */
@@ -172,16 +185,17 @@ static	int	exit_value = 0;		/* exit valu
 
 static	char	*infile;		/* name of file coming in */
 
-static	void	maybe_err(const char *fmt, ...)
-    __attribute__((__format__(__printf__, 1, 2)));
-#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT)
-static	void	maybe_errx(const char *fmt, ...)
-    __attribute__((__format__(__printf__, 1, 2)));
-#endif
-static	void	maybe_warn(const char *fmt, ...)
-    __attribute__((__format__(__printf__, 1, 2)));
-static	void	maybe_warnx(const char *fmt, ...)
-    __attribute__((__format__(__printf__, 1, 2)));
+/* from -current sys/cdefs.h */
+#define __printflike(fmtarg, firstvararg)	\
+	    __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
+
+static	void	maybe_err(const char *fmt, ...) __printflike(1, 2) __dead;
+#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) ||	\
+    !defined(NO_XZ_SUPPORT)
+static	void	maybe_errx(const char *fmt, ...) __printflike(1, 2) __dead;
+#endif
+static	void	maybe_warn(const char *fmt, ...) __printflike(1, 2);
+static	void	maybe_warnx(const char *fmt, ...) __printflike(1, 2);
 static	enum filetype file_gettype(u_char *);
 #ifdef SMALL
 #define gz_compress(if, of, sz, fn, tm) gz_compress(if, of, sz)
@@ -196,8 +210,8 @@ static	void	handle_stdin(void);
 static	void	handle_stdout(void);
 static	void	print_ratio(off_t, off_t, FILE *);
 static	void	print_list(int fd, off_t, const char *, time_t);
-static	void	usage(void);
-static	void	display_version(void);
+__dead static	void	usage(void);
+__dead static	void	display_version(void);
 static	const suffixes_t *check_suffix(char *, int);
 static	ssize_t	read_retry(int, void *, size_t);
 
@@ -226,7 +240,9 @@ static	off_t	zuncompress(FILE *, FILE *,
 static	off_t	unpack(int, int, char *, size_t, off_t *);
 #endif
 
-int main(int, char *p[]);
+#ifndef NO_XZ_SUPPORT
+static	off_t	unxz(int, int, char *, size_t, off_t *);
+#endif
 
 #ifdef SMALL
 #define getopt_long(a,b,c,d,e) getopt(a,b,c)
@@ -238,6 +254,7 @@ static const struct option longopts[] = 
 	{ "uncompress",		no_argument,		0,	'd' },
 	{ "force",		no_argument,		0,	'f' },
 	{ "help",		no_argument,		0,	'h' },
+	{ "keep",		no_argument,		0,	'k' },
 	{ "list",		no_argument,		0,	'l' },
 	{ "no-name",		no_argument,		0,	'n' },
 	{ "name",		no_argument,		0,	'N' },
@@ -289,9 +306,9 @@ main(int argc, char **argv)
 		dflag = cflag = 1;
 
 #ifdef SMALL
-#define OPT_LIST "123456789cdhltV"
+#define OPT_LIST "123456789cdhlV"
 #else
-#define OPT_LIST "123456789cdfhlNnqrS:tVv"
+#define OPT_LIST "123456789cdfhklNnqrS:tVv"
 #endif
 
 	while ((ch = getopt_long(argc, argv, OPT_LIST, longopts, NULL)) != -1) {
@@ -318,6 +335,9 @@ main(int argc, char **argv)
 		case 'f':
 			fflag = 1;
 			break;
+		case 'k':
+			kflag = 1;
+			break;
 		case 'N':
 			nflag = 0;
 			Nflag = 1;
@@ -422,7 +442,8 @@ maybe_err(const char *fmt, ...)
 	exit(2);
 }
 
-#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT)
+#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) ||	\
+    !defined(NO_XZ_SUPPORT)
 /* ... without an errno. */
 void
 maybe_errx(const char *fmt, ...)
@@ -557,7 +578,7 @@ gz_compress(int in, int out, off_t *gsiz
 		i++;
 #endif
 
-	z.next_out = outbufp + i;
+	z.next_out = (unsigned char *)outbufp + i;
 	z.avail_out = BUFLEN - i;
 
 	error = deflateInit2(&z, numflag, Z_DEFLATED,
@@ -578,7 +599,7 @@ gz_compress(int in, int out, off_t *gsiz
 			}
 
 			out_tot += BUFLEN;
-			z.next_out = outbufp;
+			z.next_out = (unsigned char *)outbufp;
 			z.avail_out = BUFLEN;
 		}
 
@@ -594,7 +615,7 @@ gz_compress(int in, int out, off_t *gsiz
 
 			crc = crc32(crc, (const Bytef *)inbufp, (unsigned)in_size);
 			in_tot += in_size;
-			z.next_in = inbufp;
+			z.next_in = (unsigned char *)inbufp;
 			z.avail_in = in_size;
 		}
 
@@ -627,7 +648,7 @@ gz_compress(int in, int out, off_t *gsiz
 			goto out;
 		}
 		out_tot += len;
-		z.next_out = outbufp;
+		z.next_out = (unsigned char *)outbufp;
 		z.avail_out = BUFLEN;
 
 		if (error == Z_STREAM_END)
@@ -721,9 +742,9 @@ gz_uncompress(int in, int out, char *pre
 
 	memset(&z, 0, sizeof z);
 	z.avail_in = prelen;
-	z.next_in = pre;
+	z.next_in = (unsigned char *)pre;
 	z.avail_out = BUFLEN;
-	z.next_out = outbufp;
+	z.next_out = (unsigned char *)outbufp;
 	z.zalloc = NULL;
 	z.zfree = NULL;
 	z.opaque = 0;
@@ -738,7 +759,7 @@ gz_uncompress(int in, int out, char *pre
 			if (z.avail_in > 0) {
 				memmove(inbufp, z.next_in, z.avail_in);
 			}
-			z.next_in = inbufp;
+			z.next_in = (unsigned char *)inbufp;
 			in_size = read(in, z.next_in + z.avail_in,
 			    BUFLEN - z.avail_in);
 
@@ -886,6 +907,9 @@ gz_uncompress(int in, int out, char *pre
 			switch (error) {
 			/* Z_BUF_ERROR goes with Z_FINISH... */
 			case Z_BUF_ERROR:
+				if (z.avail_out > 0 && !done_reading)
+					continue;
+
 			case Z_STREAM_END:
 			case Z_OK:
 				break;
@@ -930,7 +954,7 @@ gz_uncompress(int in, int out, char *pre
 				state++;
 			}
 
-			z.next_out = outbufp;
+			z.next_out = (unsigned char *)outbufp;
 			z.avail_out = BUFLEN;
 
 			break;
@@ -1089,6 +1113,11 @@ file_gettype(u_char *buf)
 		return FT_PACK;
 	else
 #endif
+#ifndef NO_XZ_SUPPORT
+	if (memcmp(buf, XZ_MAGIC, 4) == 0)	/* XXX: We only have 4 bytes */
+		return FT_XZ;
+	else
+#endif
 		return FT_UNKNOWN;
 }
 
@@ -1127,8 +1156,10 @@ unlink_input(const char *file, const str
 {
 	struct stat nsb;
 
+	if (kflag)
+		return;
 	if (stat(file, &nsb) != 0)
-		/* Must be gone alrady */
+		/* Must be gone already */
 		return;
 	if (nsb.st_dev != sb->st_dev || nsb.st_ino != sb->st_ino)
 		/* Definitely a different file */
@@ -1284,7 +1315,7 @@ file_uncompress(char *file, char *outfil
 #ifndef SMALL
 	int rv;
 	time_t timestamp = 0;
-	unsigned char name[PATH_MAX + 1];
+	char name[PATH_MAX + 1];
 #endif
 
 	/* gather the old name info */
@@ -1316,7 +1347,6 @@ file_uncompress(char *file, char *outfil
 	}
 
 	method = file_gettype(header1);
-
 #ifndef SMALL
 	if (fflag == 0 && method == FT_UNKNOWN) {
 		maybe_warnx("%s: not in gzip format", file);
@@ -1346,15 +1376,24 @@ file_uncompress(char *file, char *outfil
 				goto lose;
 			}
 			if (name[0] != 0) {
+				char *dp, *nf;
+
+				/* strip saved directory name */
+				nf = strrchr(name, '/');
+				if (nf == NULL)
+					nf = name;
+				else
+					nf++;
+
 				/* preserve original directory name */
-				char *dp = strrchr(file, '/');
+				dp = strrchr(file, '/');
 				if (dp == NULL)
 					dp = file;
 				else
 					dp++;
 				snprintf(outfile, outsize, "%.*s%.*s",
 						(int) (dp - file), 
-						file, (int) rbytes, name);
+						file, (int) rbytes, nf);
 			}
 		}
 	}
@@ -1391,9 +1430,9 @@ file_uncompress(char *file, char *outfil
 	} else
 		zfd = STDOUT_FILENO;
 
+	switch (method) {
 #ifndef NO_BZIP2_SUPPORT
-	if (method == FT_BZIP2) {
-
+	case FT_BZIP2:
 		/* XXX */
 		if (lflag) {
 			maybe_warnx("no -l with bzip2 files");
@@ -1401,11 +1440,11 @@ file_uncompress(char *file, char *outfil
 		}
 
 		size = unbzip2(fd, zfd, NULL, 0, NULL);
-	} else
+		break;
 #endif
 
 #ifndef NO_COMPRESS_SUPPORT
-	if (method == FT_Z) {
+	case FT_Z: {
 		FILE *in, *out;
 
 		/* XXX */
@@ -1438,30 +1477,42 @@ file_uncompress(char *file, char *outfil
 			unlink(outfile);
 			goto lose;
 		}
-	} else
+		break;
+	}
 #endif
 
 #ifndef NO_PACK_SUPPORT
-	if (method == FT_PACK) {
+	case FT_PACK:
 		if (lflag) {
 			maybe_warnx("no -l with packed files");
 			goto lose;
 		}
 
 		size = unpack(fd, zfd, NULL, 0, NULL);
-	} else
+		break;
+#endif
+
+#ifndef NO_XZ_SUPPORT
+	case FT_XZ:
+		if (lflag) {
+			maybe_warnx("no -l with xz files");
+			goto lose;
+		}
+
+		size = unxz(fd, zfd, NULL, 0, NULL);
+		break;
 #endif
 
 #ifndef SMALL
-	if (method == FT_UNKNOWN) {
+	case FT_UNKNOWN:
 		if (lflag) {
 			maybe_warnx("no -l for unknown filetypes");
 			goto lose;
 		}
 		size = cat_fd(NULL, 0, NULL, fd);
-	} else
+		break;
 #endif
-	{
+	default:
 		if (lflag) {
 			print_list(fd, isb.st_size, outfile, isb.st_mtime);
 			close(fd);
@@ -1469,6 +1520,7 @@ file_uncompress(char *file, char *outfil
 		}
 
 		size = gz_uncompress(fd, zfd, NULL, 0, NULL, file);
+		break;
 	}
 
 	if (close(fd) != 0)
@@ -1626,12 +1678,12 @@ handle_stdin(void)
 #endif
 	case FT_GZIP:
 		usize = gz_uncompress(STDIN_FILENO, STDOUT_FILENO, 
-			      header1, sizeof header1, &gsize, "(stdin)");
+			      (char *)header1, sizeof header1, &gsize, "(stdin)");
 		break;
 #ifndef NO_BZIP2_SUPPORT
 	case FT_BZIP2:
 		usize = unbzip2(STDIN_FILENO, STDOUT_FILENO,
-				header1, sizeof header1, &gsize);
+				(char *)header1, sizeof header1, &gsize);
 		break;
 #endif
 #ifndef NO_COMPRESS_SUPPORT
@@ -1641,7 +1693,8 @@ handle_stdin(void)
 			return;
 		}
 
-		usize = zuncompress(in, stdout, header1, sizeof header1, &gsize);
+		usize = zuncompress(in, stdout, (char *)header1,
+		    sizeof header1, &gsize);
 		fclose(in);
 		break;
 #endif
@@ -1651,6 +1704,12 @@ handle_stdin(void)
 			       (char *)header1, sizeof header1, &gsize);
 		break;
 #endif
+#ifndef NO_XZ_SUPPORT
+	case FT_XZ:
+		usize = unxz(STDIN_FILENO, STDOUT_FILENO,
+			     (char *)header1, sizeof header1, &gsize);
+		break;
+#endif
 	}
 
 #ifndef SMALL
@@ -1677,7 +1736,7 @@ handle_stdout(void)
 		return;
 	}
 #endif
-	/* If stdin is a file use it's mtime, otherwise use current time */
+	/* If stdin is a file use its mtime, otherwise use current time */
 	ret = fstat(STDIN_FILENO, &sb);
 
 #ifndef SMALL
@@ -1991,6 +2050,7 @@ usage(void)
     "    --uncompress\n"
     " -f --force           force overwriting & compress links\n"
     " -h --help            display this help\n"
+    " -k --keep            don't delete input files during operation\n"
     " -l --list            list compressed file contents\n"
     " -N --name            save or restore original file name and time stamp\n"
     " -n --no-name         don't save original file name or time stamp\n"
@@ -2026,6 +2086,9 @@ display_version(void)
 #ifndef NO_PACK_SUPPORT
 #include "unpack.c"
 #endif
+#ifndef NO_XZ_SUPPORT
+#include "unxz.c"
+#endif
 
 static ssize_t
 read_retry(int fd, void *buf, size_t sz)

Index: src/usr.bin/gzip/zuncompress.c
diff -u src/usr.bin/gzip/zuncompress.c:1.6.36.1 src/usr.bin/gzip/zuncompress.c:1.6.36.2
--- src/usr.bin/gzip/zuncompress.c:1.6.36.1	Fri Aug 19 20:58:33 2011
+++ src/usr.bin/gzip/zuncompress.c	Sun Mar 15 21:15:11 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: zuncompress.c,v 1.6.36.1 2011/08/19 20:58:33 riz Exp $ */
+/*	$NetBSD: zuncompress.c,v 1.6.36.2 2015/03/15 21:15:11 snj Exp $ */
 
 /*-
  * Copyright (c) 1985, 1986, 1992, 1993
@@ -114,7 +114,7 @@ struct s_zstate {
 			code_int zs_ent;
 			code_int zs_hsize_reg;
 			int zs_hshift;
-		} w;			/* Write paramenters */
+		} w;			/* Write parameters */
 		struct {
 			char_type *zs_stackp;
 			int zs_finchar;
@@ -146,7 +146,7 @@ zuncompress(FILE *in, FILE *out, char *p
 		compressed_pre = NULL;
 
 	while ((bin = fread(buf, 1, sizeof(buf), in)) != 0) {
-		if (tflag == 0 && fwrite(buf, 1, bin, out) != bin) {
+		if (tflag == 0 && (off_t)fwrite(buf, 1, bin, out) != bin) {
 			free(buf);
 			return -1;
 		}

Reply via email to