Home | History | Annotate | Line # | Download | only in gzip
gzip.c revision 1.114
      1 /*	$NetBSD: gzip.c,v 1.114 2018/10/06 16:36:45 martin Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1998, 2003, 2004, 2006, 2008, 2009, 2010, 2011, 2015, 2017
      5  *    Matthew R. Green
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 #ifndef lint
     32 __COPYRIGHT("@(#) Copyright (c) 1997, 1998, 2003, 2004, 2006, 2008,\
     33  2009, 2010, 2011, 2015, 2017 Matthew R. Green.  All rights reserved.");
     34 __RCSID("$NetBSD: gzip.c,v 1.114 2018/10/06 16:36:45 martin Exp $");
     35 #endif /* not lint */
     36 
     37 /*
     38  * gzip.c -- GPL free gzip using zlib.
     39  *
     40  * RFC 1950 covers the zlib format
     41  * RFC 1951 covers the deflate format
     42  * RFC 1952 covers the gzip format
     43  *
     44  * TODO:
     45  *	- use mmap where possible
     46  *	- handle some signals better (remove outfile?)
     47  *	- make bzip2/compress -v/-t/-l support work as well as possible
     48  */
     49 
     50 #include <sys/param.h>
     51 #include <sys/stat.h>
     52 #include <sys/time.h>
     53 
     54 #include <inttypes.h>
     55 #include <unistd.h>
     56 #include <stdio.h>
     57 #include <string.h>
     58 #include <stdlib.h>
     59 #include <err.h>
     60 #include <errno.h>
     61 #include <fcntl.h>
     62 #include <zlib.h>
     63 #include <fts.h>
     64 #include <libgen.h>
     65 #include <stdarg.h>
     66 #include <getopt.h>
     67 #include <time.h>
     68 
     69 #ifndef PRIdOFF
     70 #define PRIdOFF PRId64
     71 #endif
     72 
     73 /* what type of file are we dealing with */
     74 enum filetype {
     75 	FT_GZIP,
     76 #ifndef NO_BZIP2_SUPPORT
     77 	FT_BZIP2,
     78 #endif
     79 #ifndef NO_COMPRESS_SUPPORT
     80 	FT_Z,
     81 #endif
     82 #ifndef NO_PACK_SUPPORT
     83 	FT_PACK,
     84 #endif
     85 #ifndef NO_XZ_SUPPORT
     86 	FT_XZ,
     87 #endif
     88 	FT_LAST,
     89 	FT_UNKNOWN
     90 };
     91 
     92 #ifndef NO_BZIP2_SUPPORT
     93 #include <bzlib.h>
     94 
     95 #define BZ2_SUFFIX	".bz2"
     96 #define BZIP2_MAGIC	"\102\132\150"
     97 #endif
     98 
     99 #ifndef NO_COMPRESS_SUPPORT
    100 #define Z_SUFFIX	".Z"
    101 #define Z_MAGIC		"\037\235"
    102 #endif
    103 
    104 #ifndef NO_PACK_SUPPORT
    105 #define PACK_MAGIC	"\037\036"
    106 #endif
    107 
    108 #ifndef NO_XZ_SUPPORT
    109 #include <lzma.h>
    110 #define XZ_SUFFIX	".xz"
    111 #define XZ_MAGIC	"\3757zXZ"
    112 #endif
    113 
    114 #define GZ_SUFFIX	".gz"
    115 
    116 #define BUFLEN		(64 * 1024)
    117 
    118 #define GZIP_MAGIC0	0x1F
    119 #define GZIP_MAGIC1	0x8B
    120 #define GZIP_OMAGIC1	0x9E
    121 
    122 #define GZIP_TIMESTAMP	(off_t)4
    123 #define GZIP_ORIGNAME	(off_t)10
    124 
    125 #define HEAD_CRC	0x02
    126 #define EXTRA_FIELD	0x04
    127 #define ORIG_NAME	0x08
    128 #define COMMENT		0x10
    129 
    130 #define OS_CODE		3	/* Unix */
    131 
    132 typedef struct {
    133     const char	*zipped;
    134     int		ziplen;
    135     const char	*normal;	/* for unzip - must not be longer than zipped */
    136 } suffixes_t;
    137 static suffixes_t suffixes[] = {
    138 #define	SUFFIX(Z, N) {Z, sizeof Z - 1, N}
    139 	SUFFIX(GZ_SUFFIX,	""),	/* Overwritten by -S .xxx */
    140 #ifndef SMALL
    141 	SUFFIX(GZ_SUFFIX,	""),
    142 	SUFFIX(".z",		""),
    143 	SUFFIX("-gz",		""),
    144 	SUFFIX("-z",		""),
    145 	SUFFIX("_z",		""),
    146 	SUFFIX(".taz",		".tar"),
    147 	SUFFIX(".tgz",		".tar"),
    148 #ifndef NO_BZIP2_SUPPORT
    149 	SUFFIX(BZ2_SUFFIX,	""),
    150 #endif
    151 #ifndef NO_COMPRESS_SUPPORT
    152 	SUFFIX(Z_SUFFIX,	""),
    153 #endif
    154 #ifndef NO_XZ_SUPPORT
    155 	SUFFIX(XZ_SUFFIX,	""),
    156 #endif
    157 	SUFFIX(GZ_SUFFIX,	""),	/* Overwritten by -S "" */
    158 #endif /* SMALL */
    159 #undef SUFFIX
    160 };
    161 #define NUM_SUFFIXES (sizeof suffixes / sizeof suffixes[0])
    162 #define SUFFIX_MAXLEN	30
    163 
    164 static	const char	gzip_version[] = "NetBSD gzip 20170803";
    165 
    166 static	int	cflag;			/* stdout mode */
    167 static	int	dflag;			/* decompress mode */
    168 static	int	lflag;			/* list mode */
    169 static	int	numflag = 6;		/* gzip -1..-9 value */
    170 
    171 #ifndef SMALL
    172 static	int	fflag;			/* force mode */
    173 static	int	kflag;			/* don't delete input files */
    174 static	int	nflag;			/* don't save name/timestamp */
    175 static	int	Nflag;			/* don't restore name/timestamp */
    176 static	int	qflag;			/* quiet mode */
    177 static	int	rflag;			/* recursive mode */
    178 static	int	tflag;			/* test */
    179 static	int	vflag;			/* verbose mode */
    180 static	sig_atomic_t print_info = 0;
    181 #else
    182 #define		qflag	0
    183 #define		tflag	0
    184 #endif
    185 
    186 static	int	exit_value = 0;		/* exit value */
    187 
    188 static	const char *infile;		/* name of file coming in */
    189 
    190 static	void	maybe_err(const char *fmt, ...) __printflike(1, 2) __dead;
    191 #if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) ||	\
    192     !defined(NO_XZ_SUPPORT)
    193 static	void	maybe_errx(const char *fmt, ...) __printflike(1, 2) __dead;
    194 #endif
    195 static	void	maybe_warn(const char *fmt, ...) __printflike(1, 2);
    196 static	void	maybe_warnx(const char *fmt, ...) __printflike(1, 2);
    197 static	enum filetype file_gettype(u_char *);
    198 #ifdef SMALL
    199 #define gz_compress(if, of, sz, fn, tm) gz_compress(if, of, sz)
    200 #endif
    201 static	off_t	gz_compress(int, int, off_t *, const char *, uint32_t);
    202 static	off_t	gz_uncompress(int, int, char *, size_t, off_t *, const char *);
    203 static	off_t	file_compress(char *, char *, size_t);
    204 static	off_t	file_uncompress(char *, char *, size_t);
    205 static	void	handle_pathname(char *);
    206 static	void	handle_file(char *, struct stat *);
    207 static	void	handle_stdin(void);
    208 static	void	handle_stdout(void);
    209 static	void	print_ratio(off_t, off_t, FILE *);
    210 static	void	print_list(int fd, off_t, const char *, time_t);
    211 __dead static	void	usage(void);
    212 __dead static	void	display_version(void);
    213 static	const suffixes_t *check_suffix(char *, int);
    214 static	ssize_t	read_retry(int, void *, size_t);
    215 static	ssize_t	write_retry(int, const void *, size_t);
    216 static void	print_list_out(off_t, off_t, const char*);
    217 
    218 #ifdef SMALL
    219 #define infile_set(f,t) infile_set(f)
    220 #endif
    221 static	void	infile_set(const char *newinfile, off_t total);
    222 
    223 #ifdef SMALL
    224 #define unlink_input(f, sb) unlink(f)
    225 #define check_siginfo() /* nothing */
    226 #define setup_signals() /* nothing */
    227 #define infile_newdata(t) /* nothing */
    228 #else
    229 static	off_t	infile_total;		/* total expected to read/write */
    230 static	off_t	infile_current;		/* current read/write */
    231 
    232 static	void	check_siginfo(void);
    233 static	off_t	cat_fd(unsigned char *, size_t, off_t *, int fd);
    234 static	void	prepend_gzip(char *, int *, char ***);
    235 static	void	handle_dir(char *);
    236 static	void	print_verbage(const char *, const char *, off_t, off_t);
    237 static	void	print_test(const char *, int);
    238 static	void	copymodes(int fd, const struct stat *, const char *file);
    239 static	int	check_outfile(const char *outfile);
    240 static	void	setup_signals(void);
    241 static	void	infile_newdata(size_t newdata);
    242 static	void	infile_clear(void);
    243 #endif
    244 
    245 #ifndef NO_BZIP2_SUPPORT
    246 static	off_t	unbzip2(int, int, char *, size_t, off_t *);
    247 #endif
    248 
    249 #ifndef NO_COMPRESS_SUPPORT
    250 static	FILE 	*zdopen(int);
    251 static	off_t	zuncompress(FILE *, FILE *, char *, size_t, off_t *);
    252 #endif
    253 
    254 #ifndef NO_PACK_SUPPORT
    255 static	off_t	unpack(int, int, char *, size_t, off_t *);
    256 #endif
    257 
    258 #ifndef NO_XZ_SUPPORT
    259 static	off_t	unxz(int, int, char *, size_t, off_t *);
    260 static	off_t	unxz_len(int);
    261 #endif
    262 
    263 #ifdef SMALL
    264 #define getopt_long(a,b,c,d,e) getopt(a,b,c)
    265 #else
    266 static const struct option longopts[] = {
    267 	{ "stdout",		no_argument,		0,	'c' },
    268 	{ "to-stdout",		no_argument,		0,	'c' },
    269 	{ "decompress",		no_argument,		0,	'd' },
    270 	{ "uncompress",		no_argument,		0,	'd' },
    271 	{ "force",		no_argument,		0,	'f' },
    272 	{ "help",		no_argument,		0,	'h' },
    273 	{ "keep",		no_argument,		0,	'k' },
    274 	{ "list",		no_argument,		0,	'l' },
    275 	{ "no-name",		no_argument,		0,	'n' },
    276 	{ "name",		no_argument,		0,	'N' },
    277 	{ "quiet",		no_argument,		0,	'q' },
    278 	{ "recursive",		no_argument,		0,	'r' },
    279 	{ "suffix",		required_argument,	0,	'S' },
    280 	{ "test",		no_argument,		0,	't' },
    281 	{ "verbose",		no_argument,		0,	'v' },
    282 	{ "version",		no_argument,		0,	'V' },
    283 	{ "fast",		no_argument,		0,	'1' },
    284 	{ "best",		no_argument,		0,	'9' },
    285 #if 0
    286 	/*
    287 	 * This is what else GNU gzip implements.  --ascii isn't useful
    288 	 * on NetBSD, and I don't care to have a --license.
    289 	 */
    290 	{ "ascii",		no_argument,		0,	'a' },
    291 	{ "license",		no_argument,		0,	'L' },
    292 #endif
    293 	{ NULL,			no_argument,		0,	0 },
    294 };
    295 #endif
    296 
    297 int
    298 main(int argc, char **argv)
    299 {
    300 	const char *progname = getprogname();
    301 #ifndef SMALL
    302 	char *gzip;
    303 	int len;
    304 #endif
    305 	int ch;
    306 
    307 	setup_signals();
    308 
    309 #ifndef SMALL
    310 	if ((gzip = getenv("GZIP")) != NULL)
    311 		prepend_gzip(gzip, &argc, &argv);
    312 #endif
    313 
    314 	/*
    315 	 * XXX
    316 	 * handle being called `gunzip', `zcat' and `gzcat'
    317 	 */
    318 	if (strcmp(progname, "gunzip") == 0)
    319 		dflag = 1;
    320 	else if (strcmp(progname, "zcat") == 0 ||
    321 		 strcmp(progname, "gzcat") == 0)
    322 		dflag = cflag = 1;
    323 
    324 #ifdef SMALL
    325 #define OPT_LIST "123456789cdhlV"
    326 #else
    327 #define OPT_LIST "123456789cdfhklNnqrS:tVv"
    328 #endif
    329 
    330 	while ((ch = getopt_long(argc, argv, OPT_LIST, longopts, NULL)) != -1) {
    331 		switch (ch) {
    332 		case '1': case '2': case '3':
    333 		case '4': case '5': case '6':
    334 		case '7': case '8': case '9':
    335 			numflag = ch - '0';
    336 			break;
    337 		case 'c':
    338 			cflag = 1;
    339 			break;
    340 		case 'd':
    341 			dflag = 1;
    342 			break;
    343 		case 'l':
    344 			lflag = 1;
    345 			dflag = 1;
    346 			break;
    347 		case 'V':
    348 			display_version();
    349 			/* NOTREACHED */
    350 #ifndef SMALL
    351 		case 'f':
    352 			fflag = 1;
    353 			break;
    354 		case 'k':
    355 			kflag = 1;
    356 			break;
    357 		case 'N':
    358 			nflag = 0;
    359 			Nflag = 1;
    360 			break;
    361 		case 'n':
    362 			nflag = 1;
    363 			Nflag = 0;
    364 			break;
    365 		case 'q':
    366 			qflag = 1;
    367 			break;
    368 		case 'r':
    369 			rflag = 1;
    370 			break;
    371 		case 'S':
    372 			len = strlen(optarg);
    373 			if (len != 0) {
    374 				if (len > SUFFIX_MAXLEN)
    375 					errx(1, "incorrect suffix: '%s'", optarg);
    376 				suffixes[0].zipped = optarg;
    377 				suffixes[0].ziplen = len;
    378 			} else {
    379 				suffixes[NUM_SUFFIXES - 1].zipped = "";
    380 				suffixes[NUM_SUFFIXES - 1].ziplen = 0;
    381 			}
    382 			break;
    383 		case 't':
    384 			cflag = 1;
    385 			tflag = 1;
    386 			dflag = 1;
    387 			break;
    388 		case 'v':
    389 			vflag = 1;
    390 			break;
    391 #endif
    392 		default:
    393 			usage();
    394 			/* NOTREACHED */
    395 		}
    396 	}
    397 	argv += optind;
    398 	argc -= optind;
    399 
    400 	if (argc == 0) {
    401 		if (dflag)	/* stdin mode */
    402 			handle_stdin();
    403 		else		/* stdout mode */
    404 			handle_stdout();
    405 	} else {
    406 		do {
    407 			handle_pathname(argv[0]);
    408 		} while (*++argv);
    409 	}
    410 #ifndef SMALL
    411 	if (qflag == 0 && lflag && argc > 1)
    412 		print_list(-1, 0, "(totals)", 0);
    413 #endif
    414 	exit(exit_value);
    415 }
    416 
    417 /* maybe print a warning */
    418 void
    419 maybe_warn(const char *fmt, ...)
    420 {
    421 	va_list ap;
    422 
    423 	if (qflag == 0) {
    424 		va_start(ap, fmt);
    425 		vwarn(fmt, ap);
    426 		va_end(ap);
    427 	}
    428 	if (exit_value == 0)
    429 		exit_value = 1;
    430 }
    431 
    432 /* ... without an errno. */
    433 void
    434 maybe_warnx(const char *fmt, ...)
    435 {
    436 	va_list ap;
    437 
    438 	if (qflag == 0) {
    439 		va_start(ap, fmt);
    440 		vwarnx(fmt, ap);
    441 		va_end(ap);
    442 	}
    443 	if (exit_value == 0)
    444 		exit_value = 1;
    445 }
    446 
    447 /* maybe print an error */
    448 void
    449 maybe_err(const char *fmt, ...)
    450 {
    451 	va_list ap;
    452 
    453 	if (qflag == 0) {
    454 		va_start(ap, fmt);
    455 		vwarn(fmt, ap);
    456 		va_end(ap);
    457 	}
    458 	exit(2);
    459 }
    460 
    461 #if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) ||	\
    462     !defined(NO_XZ_SUPPORT)
    463 /* ... without an errno. */
    464 void
    465 maybe_errx(const char *fmt, ...)
    466 {
    467 	va_list ap;
    468 
    469 	if (qflag == 0) {
    470 		va_start(ap, fmt);
    471 		vwarnx(fmt, ap);
    472 		va_end(ap);
    473 	}
    474 	exit(2);
    475 }
    476 #endif
    477 
    478 #ifndef SMALL
    479 /* split up $GZIP and prepend it to the argument list */
    480 static void
    481 prepend_gzip(char *gzip, int *argc, char ***argv)
    482 {
    483 	char *s, **nargv, **ac;
    484 	int nenvarg = 0, i;
    485 
    486 	/* scan how many arguments there are */
    487 	for (s = gzip;;) {
    488 		while (*s == ' ' || *s == '\t')
    489 			s++;
    490 		if (*s == 0)
    491 			goto count_done;
    492 		nenvarg++;
    493 		while (*s != ' ' && *s != '\t')
    494 			if (*s++ == 0)
    495 				goto count_done;
    496 	}
    497 count_done:
    498 	/* punt early */
    499 	if (nenvarg == 0)
    500 		return;
    501 
    502 	*argc += nenvarg;
    503 	ac = *argv;
    504 
    505 	nargv = (char **)malloc((*argc + 1) * sizeof(char *));
    506 	if (nargv == NULL)
    507 		maybe_err("malloc");
    508 
    509 	/* stash this away */
    510 	*argv = nargv;
    511 
    512 	/* copy the program name first */
    513 	i = 0;
    514 	nargv[i++] = *(ac++);
    515 
    516 	/* take a copy of $GZIP and add it to the array */
    517 	s = strdup(gzip);
    518 	if (s == NULL)
    519 		maybe_err("strdup");
    520 	for (;;) {
    521 		/* Skip whitespaces. */
    522 		while (*s == ' ' || *s == '\t')
    523 			s++;
    524 		if (*s == 0)
    525 			goto copy_done;
    526 		nargv[i++] = s;
    527 		/* Find the end of this argument. */
    528 		while (*s != ' ' && *s != '\t')
    529 			if (*s++ == 0)
    530 				/* Argument followed by NUL. */
    531 				goto copy_done;
    532 		/* Terminate by overwriting ' ' or '\t' with NUL. */
    533 		*s++ = 0;
    534 	}
    535 copy_done:
    536 
    537 	/* copy the original arguments and a NULL */
    538 	while (*ac)
    539 		nargv[i++] = *(ac++);
    540 	nargv[i] = NULL;
    541 }
    542 #endif
    543 
    544 /* compress input to output. Return bytes read, -1 on error */
    545 static off_t
    546 gz_compress(int in, int out, off_t *gsizep, const char *origname, uint32_t mtime)
    547 {
    548 	z_stream z;
    549 	char *outbufp, *inbufp;
    550 	off_t in_tot = 0, out_tot = 0;
    551 	ssize_t in_size;
    552 	int i, error;
    553 	uLong crc;
    554 #ifdef SMALL
    555 	static char header[] = { GZIP_MAGIC0, GZIP_MAGIC1, Z_DEFLATED, 0,
    556 				 0, 0, 0, 0,
    557 				 0, OS_CODE };
    558 #endif
    559 
    560 	outbufp = malloc(BUFLEN);
    561 	inbufp = malloc(BUFLEN);
    562 	if (outbufp == NULL || inbufp == NULL) {
    563 		maybe_err("malloc failed");
    564 		goto out;
    565 	}
    566 
    567 	memset(&z, 0, sizeof z);
    568 	z.zalloc = Z_NULL;
    569 	z.zfree = Z_NULL;
    570 	z.opaque = 0;
    571 
    572 #ifdef SMALL
    573 	memcpy(outbufp, header, sizeof header);
    574 	i = sizeof header;
    575 #else
    576 	if (nflag != 0) {
    577 		mtime = 0;
    578 		origname = "";
    579 	}
    580 
    581 	i = snprintf(outbufp, BUFLEN, "%c%c%c%c%c%c%c%c%c%c%s",
    582 		     GZIP_MAGIC0, GZIP_MAGIC1, Z_DEFLATED,
    583 		     *origname ? ORIG_NAME : 0,
    584 		     mtime & 0xff,
    585 		     (mtime >> 8) & 0xff,
    586 		     (mtime >> 16) & 0xff,
    587 		     (mtime >> 24) & 0xff,
    588 		     numflag == 1 ? 4 : numflag == 9 ? 2 : 0,
    589 		     OS_CODE, origname);
    590 	if (i >= BUFLEN)
    591 		/* this need PATH_MAX > BUFLEN ... */
    592 		maybe_err("snprintf");
    593 	if (*origname)
    594 		i++;
    595 #endif
    596 
    597 	z.next_out = (unsigned char *)outbufp + i;
    598 	z.avail_out = BUFLEN - i;
    599 
    600 	error = deflateInit2(&z, numflag, Z_DEFLATED,
    601 			     (-MAX_WBITS), 8, Z_DEFAULT_STRATEGY);
    602 	if (error != Z_OK) {
    603 		maybe_warnx("deflateInit2 failed");
    604 		in_tot = -1;
    605 		goto out;
    606 	}
    607 
    608 	crc = crc32(0L, Z_NULL, 0);
    609 	for (;;) {
    610 		if (z.avail_out == 0) {
    611 			if (write_retry(out, outbufp, BUFLEN) != BUFLEN) {
    612 				maybe_warn("write");
    613 				out_tot = -1;
    614 				goto out;
    615 			}
    616 
    617 			out_tot += BUFLEN;
    618 			z.next_out = (unsigned char *)outbufp;
    619 			z.avail_out = BUFLEN;
    620 		}
    621 
    622 		if (z.avail_in == 0) {
    623 			in_size = read(in, inbufp, BUFLEN);
    624 			if (in_size < 0) {
    625 				maybe_warn("read");
    626 				in_tot = -1;
    627 				goto out;
    628 			}
    629 			if (in_size == 0)
    630 				break;
    631 			infile_newdata(in_size);
    632 
    633 			crc = crc32(crc, (const Bytef *)inbufp, (unsigned)in_size);
    634 			in_tot += in_size;
    635 			z.next_in = (unsigned char *)inbufp;
    636 			z.avail_in = in_size;
    637 		}
    638 
    639 		error = deflate(&z, Z_NO_FLUSH);
    640 		if (error != Z_OK && error != Z_STREAM_END) {
    641 			maybe_warnx("deflate failed");
    642 			in_tot = -1;
    643 			goto out;
    644 		}
    645 	}
    646 
    647 	/* clean up */
    648 	for (;;) {
    649 		size_t len;
    650 		ssize_t w;
    651 
    652 		error = deflate(&z, Z_FINISH);
    653 		if (error != Z_OK && error != Z_STREAM_END) {
    654 			maybe_warnx("deflate failed");
    655 			in_tot = -1;
    656 			goto out;
    657 		}
    658 
    659 		len = (char *)z.next_out - outbufp;
    660 
    661 		w = write_retry(out, outbufp, len);
    662 		if (w == -1 || (size_t)w != len) {
    663 			maybe_warn("write");
    664 			out_tot = -1;
    665 			goto out;
    666 		}
    667 		out_tot += len;
    668 		z.next_out = (unsigned char *)outbufp;
    669 		z.avail_out = BUFLEN;
    670 
    671 		if (error == Z_STREAM_END)
    672 			break;
    673 	}
    674 
    675 	if (deflateEnd(&z) != Z_OK) {
    676 		maybe_warnx("deflateEnd failed");
    677 		in_tot = -1;
    678 		goto out;
    679 	}
    680 
    681 	i = snprintf(outbufp, BUFLEN, "%c%c%c%c%c%c%c%c",
    682 		 (int)crc & 0xff,
    683 		 (int)(crc >> 8) & 0xff,
    684 		 (int)(crc >> 16) & 0xff,
    685 		 (int)(crc >> 24) & 0xff,
    686 		 (int)in_tot & 0xff,
    687 		 (int)(in_tot >> 8) & 0xff,
    688 		 (int)(in_tot >> 16) & 0xff,
    689 		 (int)(in_tot >> 24) & 0xff);
    690 	if (i != 8)
    691 		maybe_err("snprintf");
    692 #if 0
    693 	if (in_tot > 0xffffffff)
    694 		maybe_warn("input file size >= 4GB cannot be saved");
    695 #endif
    696 	if (write_retry(out, outbufp, i) != i) {
    697 		maybe_warn("write");
    698 		in_tot = -1;
    699 	} else
    700 		out_tot += i;
    701 
    702 out:
    703 	if (inbufp != NULL)
    704 		free(inbufp);
    705 	if (outbufp != NULL)
    706 		free(outbufp);
    707 	if (gsizep)
    708 		*gsizep = out_tot;
    709 	return in_tot;
    710 }
    711 
    712 /*
    713  * uncompress input to output then close the input.  return the
    714  * uncompressed size written, and put the compressed sized read
    715  * into `*gsizep'.
    716  */
    717 static off_t
    718 gz_uncompress(int in, int out, char *pre, size_t prelen, off_t *gsizep,
    719 	      const char *filename)
    720 {
    721 	z_stream z;
    722 	char *outbufp, *inbufp;
    723 	off_t out_tot = -1, in_tot = 0;
    724 	uint32_t out_sub_tot = 0;
    725 	enum {
    726 		GZSTATE_MAGIC0,
    727 		GZSTATE_MAGIC1,
    728 		GZSTATE_METHOD,
    729 		GZSTATE_FLAGS,
    730 		GZSTATE_SKIPPING,
    731 		GZSTATE_EXTRA,
    732 		GZSTATE_EXTRA2,
    733 		GZSTATE_EXTRA3,
    734 		GZSTATE_ORIGNAME,
    735 		GZSTATE_COMMENT,
    736 		GZSTATE_HEAD_CRC1,
    737 		GZSTATE_HEAD_CRC2,
    738 		GZSTATE_INIT,
    739 		GZSTATE_READ,
    740 		GZSTATE_CRC,
    741 		GZSTATE_LEN,
    742 	} state = GZSTATE_MAGIC0;
    743 	int flags = 0, skip_count = 0;
    744 	int error = Z_STREAM_ERROR, done_reading = 0;
    745 	uLong crc = 0;
    746 	ssize_t wr;
    747 	int needmore = 0;
    748 
    749 #define ADVANCE()       { z.next_in++; z.avail_in--; }
    750 
    751 	if ((outbufp = malloc(BUFLEN)) == NULL) {
    752 		maybe_err("malloc failed");
    753 		goto out2;
    754 	}
    755 	if ((inbufp = malloc(BUFLEN)) == NULL) {
    756 		maybe_err("malloc failed");
    757 		goto out1;
    758 	}
    759 
    760 	memset(&z, 0, sizeof z);
    761 	z.avail_in = prelen;
    762 	z.next_in = (unsigned char *)pre;
    763 	z.avail_out = BUFLEN;
    764 	z.next_out = (unsigned char *)outbufp;
    765 	z.zalloc = NULL;
    766 	z.zfree = NULL;
    767 	z.opaque = 0;
    768 
    769 	in_tot = prelen;
    770 	out_tot = 0;
    771 
    772 	for (;;) {
    773 		check_siginfo();
    774 		if ((z.avail_in == 0 || needmore) && done_reading == 0) {
    775 			ssize_t in_size;
    776 
    777 			if (z.avail_in > 0) {
    778 				memmove(inbufp, z.next_in, z.avail_in);
    779 			}
    780 			z.next_in = (unsigned char *)inbufp;
    781 			in_size = read(in, z.next_in + z.avail_in,
    782 			    BUFLEN - z.avail_in);
    783 
    784 			if (in_size == -1) {
    785 				maybe_warn("failed to read stdin");
    786 				goto stop_and_fail;
    787 			} else if (in_size == 0) {
    788 				done_reading = 1;
    789 			}
    790 			infile_newdata(in_size);
    791 
    792 			z.avail_in += in_size;
    793 			needmore = 0;
    794 
    795 			in_tot += in_size;
    796 		}
    797 		if (z.avail_in == 0) {
    798 			if (done_reading && state != GZSTATE_MAGIC0) {
    799 				maybe_warnx("%s: unexpected end of file",
    800 					    filename);
    801 				goto stop_and_fail;
    802 			}
    803 			goto stop;
    804 		}
    805 		switch (state) {
    806 		case GZSTATE_MAGIC0:
    807 			if (*z.next_in != GZIP_MAGIC0) {
    808 				if (in_tot > 0) {
    809 					maybe_warnx("%s: trailing garbage "
    810 						    "ignored", filename);
    811 					goto stop;
    812 				}
    813 				maybe_warnx("input not gziped (MAGIC0)");
    814 				exit_value = 2;
    815 				goto stop_and_fail;
    816 			}
    817 			ADVANCE();
    818 			state++;
    819 			out_sub_tot = 0;
    820 			crc = crc32(0L, Z_NULL, 0);
    821 			break;
    822 
    823 		case GZSTATE_MAGIC1:
    824 			if (*z.next_in != GZIP_MAGIC1 &&
    825 			    *z.next_in != GZIP_OMAGIC1) {
    826 				maybe_warnx("input not gziped (MAGIC1)");
    827 				goto stop_and_fail;
    828 			}
    829 			ADVANCE();
    830 			state++;
    831 			break;
    832 
    833 		case GZSTATE_METHOD:
    834 			if (*z.next_in != Z_DEFLATED) {
    835 				maybe_warnx("unknown compression method");
    836 				goto stop_and_fail;
    837 			}
    838 			ADVANCE();
    839 			state++;
    840 			break;
    841 
    842 		case GZSTATE_FLAGS:
    843 			flags = *z.next_in;
    844 			ADVANCE();
    845 			skip_count = 6;
    846 			state++;
    847 			break;
    848 
    849 		case GZSTATE_SKIPPING:
    850 			if (skip_count > 0) {
    851 				skip_count--;
    852 				ADVANCE();
    853 			} else
    854 				state++;
    855 			break;
    856 
    857 		case GZSTATE_EXTRA:
    858 			if ((flags & EXTRA_FIELD) == 0) {
    859 				state = GZSTATE_ORIGNAME;
    860 				break;
    861 			}
    862 			skip_count = *z.next_in;
    863 			ADVANCE();
    864 			state++;
    865 			break;
    866 
    867 		case GZSTATE_EXTRA2:
    868 			skip_count |= ((*z.next_in) << 8);
    869 			ADVANCE();
    870 			state++;
    871 			break;
    872 
    873 		case GZSTATE_EXTRA3:
    874 			if (skip_count > 0) {
    875 				skip_count--;
    876 				ADVANCE();
    877 			} else
    878 				state++;
    879 			break;
    880 
    881 		case GZSTATE_ORIGNAME:
    882 			if ((flags & ORIG_NAME) == 0) {
    883 				state++;
    884 				break;
    885 			}
    886 			if (*z.next_in == 0)
    887 				state++;
    888 			ADVANCE();
    889 			break;
    890 
    891 		case GZSTATE_COMMENT:
    892 			if ((flags & COMMENT) == 0) {
    893 				state++;
    894 				break;
    895 			}
    896 			if (*z.next_in == 0)
    897 				state++;
    898 			ADVANCE();
    899 			break;
    900 
    901 		case GZSTATE_HEAD_CRC1:
    902 			if (flags & HEAD_CRC)
    903 				skip_count = 2;
    904 			else
    905 				skip_count = 0;
    906 			state++;
    907 			break;
    908 
    909 		case GZSTATE_HEAD_CRC2:
    910 			if (skip_count > 0) {
    911 				skip_count--;
    912 				ADVANCE();
    913 			} else
    914 				state++;
    915 			break;
    916 
    917 		case GZSTATE_INIT:
    918 			if (inflateInit2(&z, -MAX_WBITS) != Z_OK) {
    919 				maybe_warnx("failed to inflateInit");
    920 				goto stop_and_fail;
    921 			}
    922 			state++;
    923 			break;
    924 
    925 		case GZSTATE_READ:
    926 			error = inflate(&z, Z_FINISH);
    927 			switch (error) {
    928 			/* Z_BUF_ERROR goes with Z_FINISH... */
    929 			case Z_BUF_ERROR:
    930 				if (z.avail_out > 0 && !done_reading)
    931 					continue;
    932 
    933 			case Z_STREAM_END:
    934 			case Z_OK:
    935 				break;
    936 
    937 			case Z_NEED_DICT:
    938 				maybe_warnx("Z_NEED_DICT error");
    939 				goto stop_and_fail;
    940 			case Z_DATA_ERROR:
    941 				maybe_warnx("data stream error");
    942 				goto stop_and_fail;
    943 			case Z_STREAM_ERROR:
    944 				maybe_warnx("internal stream error");
    945 				goto stop_and_fail;
    946 			case Z_MEM_ERROR:
    947 				maybe_warnx("memory allocation error");
    948 				goto stop_and_fail;
    949 
    950 			default:
    951 				maybe_warn("unknown error from inflate(): %d",
    952 				    error);
    953 			}
    954 			wr = BUFLEN - z.avail_out;
    955 
    956 			if (wr != 0) {
    957 				crc = crc32(crc, (const Bytef *)outbufp, (unsigned)wr);
    958 				if (
    959 #ifndef SMALL
    960 				    /* don't write anything with -t */
    961 				    tflag == 0 &&
    962 #endif
    963 				    write_retry(out, outbufp, wr) != wr) {
    964 					maybe_warn("error writing to output");
    965 					goto stop_and_fail;
    966 				}
    967 
    968 				out_tot += wr;
    969 				out_sub_tot += wr;
    970 			}
    971 
    972 			if (error == Z_STREAM_END) {
    973 				inflateEnd(&z);
    974 				state++;
    975 			}
    976 
    977 			z.next_out = (unsigned char *)outbufp;
    978 			z.avail_out = BUFLEN;
    979 
    980 			break;
    981 		case GZSTATE_CRC:
    982 			{
    983 				uLong origcrc;
    984 
    985 				if (z.avail_in < 4) {
    986 					if (!done_reading) {
    987 						needmore = 1;
    988 						continue;
    989 					}
    990 					maybe_warnx("truncated input");
    991 					goto stop_and_fail;
    992 				}
    993 				origcrc = ((unsigned)z.next_in[0] & 0xff) |
    994 					((unsigned)z.next_in[1] & 0xff) << 8 |
    995 					((unsigned)z.next_in[2] & 0xff) << 16 |
    996 					((unsigned)z.next_in[3] & 0xff) << 24;
    997 				if (origcrc != crc) {
    998 					maybe_warnx("invalid compressed"
    999 					     " data--crc error");
   1000 					goto stop_and_fail;
   1001 				}
   1002 			}
   1003 
   1004 			z.avail_in -= 4;
   1005 			z.next_in += 4;
   1006 
   1007 			if (!z.avail_in && done_reading) {
   1008 				goto stop;
   1009 			}
   1010 			state++;
   1011 			break;
   1012 		case GZSTATE_LEN:
   1013 			{
   1014 				uLong origlen;
   1015 
   1016 				if (z.avail_in < 4) {
   1017 					if (!done_reading) {
   1018 						needmore = 1;
   1019 						continue;
   1020 					}
   1021 					maybe_warnx("truncated input");
   1022 					goto stop_and_fail;
   1023 				}
   1024 				origlen = ((unsigned)z.next_in[0] & 0xff) |
   1025 					((unsigned)z.next_in[1] & 0xff) << 8 |
   1026 					((unsigned)z.next_in[2] & 0xff) << 16 |
   1027 					((unsigned)z.next_in[3] & 0xff) << 24;
   1028 
   1029 				if (origlen != out_sub_tot) {
   1030 					maybe_warnx("invalid compressed"
   1031 					     " data--length error");
   1032 					goto stop_and_fail;
   1033 				}
   1034 			}
   1035 
   1036 			z.avail_in -= 4;
   1037 			z.next_in += 4;
   1038 
   1039 			if (error < 0) {
   1040 				maybe_warnx("decompression error");
   1041 				goto stop_and_fail;
   1042 			}
   1043 			state = GZSTATE_MAGIC0;
   1044 			break;
   1045 		}
   1046 		continue;
   1047 stop_and_fail:
   1048 		out_tot = -1;
   1049 stop:
   1050 		break;
   1051 	}
   1052 	if (state > GZSTATE_INIT)
   1053 		inflateEnd(&z);
   1054 
   1055 	free(inbufp);
   1056 out1:
   1057 	free(outbufp);
   1058 out2:
   1059 	if (gsizep)
   1060 		*gsizep = in_tot;
   1061 	return (out_tot);
   1062 }
   1063 
   1064 #ifndef SMALL
   1065 /*
   1066  * set the owner, mode, flags & utimes using the given file descriptor.
   1067  * file is only used in possible warning messages.
   1068  */
   1069 static void
   1070 copymodes(int fd, const struct stat *sbp, const char *file)
   1071 {
   1072 	struct timeval times[2];
   1073 	struct stat sb;
   1074 
   1075 	/*
   1076 	 * If we have no info on the input, give this file some
   1077 	 * default values and return..
   1078 	 */
   1079 	if (sbp == NULL) {
   1080 		mode_t mask = umask(022);
   1081 
   1082 		(void)fchmod(fd, DEFFILEMODE & ~mask);
   1083 		(void)umask(mask);
   1084 		return;
   1085 	}
   1086 	sb = *sbp;
   1087 
   1088 	/* if the chown fails, remove set-id bits as-per compress(1) */
   1089 	if (fchown(fd, sb.st_uid, sb.st_gid) < 0) {
   1090 		if (errno != EPERM)
   1091 			maybe_warn("couldn't fchown: %s", file);
   1092 		sb.st_mode &= ~(S_ISUID|S_ISGID);
   1093 	}
   1094 
   1095 	/* we only allow set-id and the 9 normal permission bits */
   1096 	sb.st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
   1097 	if (fchmod(fd, sb.st_mode) < 0)
   1098 		maybe_warn("couldn't fchmod: %s", file);
   1099 
   1100 	/* only try flags if they exist already */
   1101         if (sb.st_flags != 0 && fchflags(fd, sb.st_flags) < 0)
   1102 		maybe_warn("couldn't fchflags: %s", file);
   1103 
   1104 	TIMESPEC_TO_TIMEVAL(&times[0], &sb.st_atimespec);
   1105 	TIMESPEC_TO_TIMEVAL(&times[1], &sb.st_mtimespec);
   1106 	if (futimes(fd, times) < 0)
   1107 		maybe_warn("couldn't utimes: %s", file);
   1108 }
   1109 #endif
   1110 
   1111 /* what sort of file is this? */
   1112 static enum filetype
   1113 file_gettype(u_char *buf)
   1114 {
   1115 
   1116 	if (buf[0] == GZIP_MAGIC0 &&
   1117 	    (buf[1] == GZIP_MAGIC1 || buf[1] == GZIP_OMAGIC1))
   1118 		return FT_GZIP;
   1119 	else
   1120 #ifndef NO_BZIP2_SUPPORT
   1121 	if (memcmp(buf, BZIP2_MAGIC, 3) == 0 &&
   1122 	    buf[3] >= '0' && buf[3] <= '9')
   1123 		return FT_BZIP2;
   1124 	else
   1125 #endif
   1126 #ifndef NO_COMPRESS_SUPPORT
   1127 	if (memcmp(buf, Z_MAGIC, 2) == 0)
   1128 		return FT_Z;
   1129 	else
   1130 #endif
   1131 #ifndef NO_PACK_SUPPORT
   1132 	if (memcmp(buf, PACK_MAGIC, 2) == 0)
   1133 		return FT_PACK;
   1134 	else
   1135 #endif
   1136 #ifndef NO_XZ_SUPPORT
   1137 	if (memcmp(buf, XZ_MAGIC, 4) == 0)	/* XXX: We only have 4 bytes */
   1138 		return FT_XZ;
   1139 	else
   1140 #endif
   1141 		return FT_UNKNOWN;
   1142 }
   1143 
   1144 #ifndef SMALL
   1145 /* check the outfile is OK. */
   1146 static int
   1147 check_outfile(const char *outfile)
   1148 {
   1149 	struct stat sb;
   1150 	int ok = 1;
   1151 
   1152 	if (lflag == 0 && stat(outfile, &sb) == 0) {
   1153 		if (fflag)
   1154 			unlink(outfile);
   1155 		else if (isatty(STDIN_FILENO)) {
   1156 			char ans[10] = { 'n', '\0' };	/* default */
   1157 
   1158 			fprintf(stderr, "%s already exists -- do you wish to "
   1159 					"overwrite (y or n)? " , outfile);
   1160 			(void)fgets(ans, sizeof(ans) - 1, stdin);
   1161 			if (ans[0] != 'y' && ans[0] != 'Y') {
   1162 				fprintf(stderr, "\tnot overwriting\n");
   1163 				ok = 0;
   1164 			} else
   1165 				unlink(outfile);
   1166 		} else {
   1167 			maybe_warnx("%s already exists -- skipping", outfile);
   1168 			ok = 0;
   1169 		}
   1170 	}
   1171 	return ok;
   1172 }
   1173 
   1174 static void
   1175 unlink_input(const char *file, const struct stat *sb)
   1176 {
   1177 	struct stat nsb;
   1178 
   1179 	if (kflag)
   1180 		return;
   1181 	if (stat(file, &nsb) != 0)
   1182 		/* Must be gone already */
   1183 		return;
   1184 	if (nsb.st_dev != sb->st_dev || nsb.st_ino != sb->st_ino)
   1185 		/* Definitely a different file */
   1186 		return;
   1187 	unlink(file);
   1188 }
   1189 
   1190 static void
   1191 got_siginfo(int signo)
   1192 {
   1193 
   1194 	print_info = 1;
   1195 }
   1196 
   1197 static void
   1198 setup_signals(void)
   1199 {
   1200 
   1201 	signal(SIGINFO, got_siginfo);
   1202 }
   1203 
   1204 static	void
   1205 infile_newdata(size_t newdata)
   1206 {
   1207 
   1208 	infile_current += newdata;
   1209 }
   1210 #endif
   1211 
   1212 static	void
   1213 infile_set(const char *newinfile, off_t total)
   1214 {
   1215 
   1216 	if (newinfile)
   1217 		infile = newinfile;
   1218 #ifndef SMALL
   1219 	infile_total = total;
   1220 #endif
   1221 }
   1222 
   1223 static	void
   1224 infile_clear(void)
   1225 {
   1226 
   1227 	infile = NULL;
   1228 #ifndef SMALL
   1229 	infile_total = infile_current = 0;
   1230 #endif
   1231 }
   1232 
   1233 static const suffixes_t *
   1234 check_suffix(char *file, int xlate)
   1235 {
   1236 	const suffixes_t *s;
   1237 	int len = strlen(file);
   1238 	char *sp;
   1239 
   1240 	for (s = suffixes; s != suffixes + NUM_SUFFIXES; s++) {
   1241 		/* if it doesn't fit in "a.suf", don't bother */
   1242 		if (s->ziplen >= len)
   1243 			continue;
   1244 		sp = file + len - s->ziplen;
   1245 		if (strcmp(s->zipped, sp) != 0)
   1246 			continue;
   1247 		if (xlate)
   1248 			strcpy(sp, s->normal);
   1249 		return s;
   1250 	}
   1251 	return NULL;
   1252 }
   1253 
   1254 /*
   1255  * compress the given file: create a corresponding .gz file and remove the
   1256  * original.
   1257  */
   1258 static off_t
   1259 file_compress(char *file, char *outfile, size_t outsize)
   1260 {
   1261 	int in;
   1262 	int out;
   1263 	off_t size, in_size;
   1264 #ifndef SMALL
   1265 	struct stat isb, osb;
   1266 	const suffixes_t *suff;
   1267 #endif
   1268 
   1269 	in = open(file, O_RDONLY);
   1270 	if (in == -1) {
   1271 		maybe_warn("can't open %s", file);
   1272 		return -1;
   1273 	}
   1274 
   1275 #ifndef SMALL
   1276 	if (fstat(in, &isb) != 0) {
   1277 		close(in);
   1278 		maybe_warn("can't stat %s", file);
   1279 		return -1;
   1280 	}
   1281 	infile_set(file, isb.st_size);
   1282 #endif
   1283 
   1284 	if (cflag == 0) {
   1285 #ifndef SMALL
   1286 		if (isb.st_nlink > 1 && fflag == 0) {
   1287 			maybe_warnx("%s has %d other link%s -- "
   1288 				    "skipping", file, isb.st_nlink - 1,
   1289 				    isb.st_nlink == 1 ? "" : "s");
   1290 			close(in);
   1291 			return -1;
   1292 		}
   1293 
   1294 		if (fflag == 0 && (suff = check_suffix(file, 0))
   1295 		    && suff->zipped[0] != 0) {
   1296 			maybe_warnx("%s already has %s suffix -- unchanged",
   1297 				    file, suff->zipped);
   1298 			close(in);
   1299 			return -1;
   1300 		}
   1301 #endif
   1302 
   1303 		/* Add (usually) .gz to filename */
   1304 		if ((size_t)snprintf(outfile, outsize, "%s%s",
   1305 					file, suffixes[0].zipped) >= outsize)
   1306 			memcpy(outfile + outsize - suffixes[0].ziplen - 1,
   1307 				suffixes[0].zipped, suffixes[0].ziplen + 1);
   1308 
   1309 #ifndef SMALL
   1310 		if (check_outfile(outfile) == 0) {
   1311 			close(in);
   1312 			return -1;
   1313 		}
   1314 #endif
   1315 	}
   1316 
   1317 	if (cflag == 0) {
   1318 		out = open(outfile, O_WRONLY | O_CREAT | O_EXCL, 0600);
   1319 		if (out == -1) {
   1320 			maybe_warn("could not create output: %s", outfile);
   1321 			fclose(stdin);
   1322 			return -1;
   1323 		}
   1324 	} else
   1325 		out = STDOUT_FILENO;
   1326 
   1327 	in_size = gz_compress(in, out, &size, basename(file), (uint32_t)isb.st_mtime);
   1328 
   1329 	(void)close(in);
   1330 
   1331 	/*
   1332 	 * If there was an error, in_size will be -1.
   1333 	 * If we compressed to stdout, just return the size.
   1334 	 * Otherwise stat the file and check it is the correct size.
   1335 	 * We only blow away the file if we can stat the output and it
   1336 	 * has the expected size.
   1337 	 */
   1338 	if (cflag != 0)
   1339 		return in_size == -1 ? -1 : size;
   1340 
   1341 #ifndef SMALL
   1342 	if (fstat(out, &osb) != 0) {
   1343 		maybe_warn("couldn't stat: %s", outfile);
   1344 		goto bad_outfile;
   1345 	}
   1346 
   1347 	if (osb.st_size != size) {
   1348 		maybe_warnx("output file: %s wrong size (%" PRIdOFF
   1349 				" != %" PRIdOFF "), deleting",
   1350 				outfile, osb.st_size, size);
   1351 		goto bad_outfile;
   1352 	}
   1353 
   1354 	copymodes(out, &isb, outfile);
   1355 #endif
   1356 	if (close(out) == -1)
   1357 		maybe_warn("couldn't close output");
   1358 
   1359 	/* output is good, ok to delete input */
   1360 	unlink_input(file, &isb);
   1361 	return size;
   1362 
   1363 #ifndef SMALL
   1364     bad_outfile:
   1365 	if (close(out) == -1)
   1366 		maybe_warn("couldn't close output");
   1367 
   1368 	maybe_warnx("leaving original %s", file);
   1369 	unlink(outfile);
   1370 	return size;
   1371 #endif
   1372 }
   1373 
   1374 /* uncompress the given file and remove the original */
   1375 static off_t
   1376 file_uncompress(char *file, char *outfile, size_t outsize)
   1377 {
   1378 	struct stat isb, osb;
   1379 	off_t size;
   1380 	ssize_t rbytes;
   1381 	unsigned char header1[4];
   1382 	enum filetype method;
   1383 	int fd, ofd, zfd = -1;
   1384 	size_t in_size;
   1385 #ifndef SMALL
   1386 	ssize_t rv;
   1387 	time_t timestamp = 0;
   1388 	char name[PATH_MAX + 1];
   1389 #endif
   1390 
   1391 	/* gather the old name info */
   1392 
   1393 	fd = open(file, O_RDONLY);
   1394 	if (fd < 0) {
   1395 		maybe_warn("can't open %s", file);
   1396 		goto lose;
   1397 	}
   1398 	if (fstat(fd, &isb) != 0) {
   1399 		close(fd);
   1400 		maybe_warn("can't stat %s", file);
   1401 		goto lose;
   1402 	}
   1403 	if (S_ISREG(isb.st_mode))
   1404 		in_size = isb.st_size;
   1405 	else
   1406 		in_size = 0;
   1407 	infile_set(file, in_size);
   1408 
   1409 	strlcpy(outfile, file, outsize);
   1410 	if (check_suffix(outfile, 1) == NULL && !(cflag || lflag)) {
   1411 		maybe_warnx("%s: unknown suffix -- ignored", file);
   1412 		goto lose;
   1413 	}
   1414 
   1415 	rbytes = read(fd, header1, sizeof header1);
   1416 	if (rbytes != sizeof header1) {
   1417 		/* we don't want to fail here. */
   1418 #ifndef SMALL
   1419 		if (fflag)
   1420 			goto lose;
   1421 #endif
   1422 		if (rbytes == -1)
   1423 			maybe_warn("can't read %s", file);
   1424 		else
   1425 			goto unexpected_EOF;
   1426 		goto lose;
   1427 	}
   1428 	infile_newdata(rbytes);
   1429 
   1430 	method = file_gettype(header1);
   1431 #ifndef SMALL
   1432 	if (fflag == 0 && method == FT_UNKNOWN) {
   1433 		maybe_warnx("%s: not in gzip format", file);
   1434 		goto lose;
   1435 	}
   1436 
   1437 #endif
   1438 
   1439 #ifndef SMALL
   1440 	if (method == FT_GZIP && Nflag) {
   1441 		unsigned char ts[4];	/* timestamp */
   1442 
   1443 		rv = pread(fd, ts, sizeof ts, GZIP_TIMESTAMP);
   1444 		if (rv >= 0 && rv < (ssize_t)(sizeof ts))
   1445 			goto unexpected_EOF;
   1446 		if (rv == -1) {
   1447 			if (!fflag)
   1448 				maybe_warn("can't read %s", file);
   1449 			goto lose;
   1450 		}
   1451 		infile_newdata(rv);
   1452 		timestamp = ts[3] << 24 | ts[2] << 16 | ts[1] << 8 | ts[0];
   1453 
   1454 		if (header1[3] & ORIG_NAME) {
   1455 			rbytes = pread(fd, name, sizeof(name) - 1, GZIP_ORIGNAME);
   1456 			if (rbytes < 0) {
   1457 				maybe_warn("can't read %s", file);
   1458 				goto lose;
   1459 			}
   1460 			if (name[0] != '\0') {
   1461 				char *dp, *nf;
   1462 
   1463 				/* Make sure that name is NUL-terminated */
   1464 				name[rbytes] = '\0';
   1465 
   1466 				/* strip saved directory name */
   1467 				nf = strrchr(name, '/');
   1468 				if (nf == NULL)
   1469 					nf = name;
   1470 				else
   1471 					nf++;
   1472 
   1473 				/* preserve original directory name */
   1474 				dp = strrchr(file, '/');
   1475 				if (dp == NULL)
   1476 					dp = file;
   1477 				else
   1478 					dp++;
   1479 				snprintf(outfile, outsize, "%.*s%.*s",
   1480 						(int) (dp - file),
   1481 						file, (int) rbytes, nf);
   1482 			}
   1483 		}
   1484 	}
   1485 #endif
   1486 	lseek(fd, 0, SEEK_SET);
   1487 
   1488 	if (cflag == 0 || lflag) {
   1489 #ifndef SMALL
   1490 		if (isb.st_nlink > 1 && lflag == 0 && fflag == 0) {
   1491 			maybe_warnx("%s has %d other links -- skipping",
   1492 			    file, isb.st_nlink - 1);
   1493 			goto lose;
   1494 		}
   1495 		if (nflag == 0 && timestamp)
   1496 			isb.st_mtime = timestamp;
   1497 		if (check_outfile(outfile) == 0)
   1498 			goto lose;
   1499 #endif
   1500 	}
   1501 
   1502 	if (cflag)
   1503 		zfd = STDOUT_FILENO;
   1504 	else if (lflag)
   1505 		zfd = -1;
   1506 	else {
   1507 		zfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0600);
   1508 		if (zfd == STDOUT_FILENO) {
   1509 			/* We won't close STDOUT_FILENO later... */
   1510 			zfd = dup(zfd);
   1511 			close(STDOUT_FILENO);
   1512 		}
   1513 		if (zfd == -1) {
   1514 			maybe_warn("can't open %s", outfile);
   1515 			goto lose;
   1516 		}
   1517 	}
   1518 
   1519 	switch (method) {
   1520 #ifndef NO_BZIP2_SUPPORT
   1521 	case FT_BZIP2:
   1522 		/* XXX */
   1523 		if (lflag) {
   1524 			maybe_warnx("no -l with bzip2 files");
   1525 			goto lose;
   1526 		}
   1527 
   1528 		size = unbzip2(fd, zfd, NULL, 0, NULL);
   1529 		break;
   1530 #endif
   1531 
   1532 #ifndef NO_COMPRESS_SUPPORT
   1533 	case FT_Z: {
   1534 		FILE *in, *out;
   1535 
   1536 		/* XXX */
   1537 		if (lflag) {
   1538 			maybe_warnx("no -l with Lempel-Ziv files");
   1539 			goto lose;
   1540 		}
   1541 
   1542 		if ((in = zdopen(fd)) == NULL) {
   1543 			maybe_warn("zdopen for read: %s", file);
   1544 			goto lose;
   1545 		}
   1546 
   1547 		out = fdopen(dup(zfd), "w");
   1548 		if (out == NULL) {
   1549 			maybe_warn("fdopen for write: %s", outfile);
   1550 			fclose(in);
   1551 			goto lose;
   1552 		}
   1553 
   1554 		size = zuncompress(in, out, NULL, 0, NULL);
   1555 		/* need to fclose() if ferror() is true... */
   1556 		if (ferror(in) | fclose(in)) {
   1557 			maybe_warn("failed infile fclose");
   1558 			unlink(outfile);
   1559 			(void)fclose(out);
   1560 		}
   1561 		if (fclose(out) != 0) {
   1562 			maybe_warn("failed outfile fclose");
   1563 			unlink(outfile);
   1564 			goto lose;
   1565 		}
   1566 		break;
   1567 	}
   1568 #endif
   1569 
   1570 #ifndef NO_PACK_SUPPORT
   1571 	case FT_PACK:
   1572 		if (lflag) {
   1573 			maybe_warnx("no -l with packed files");
   1574 			goto lose;
   1575 		}
   1576 
   1577 		size = unpack(fd, zfd, NULL, 0, NULL);
   1578 		break;
   1579 #endif
   1580 
   1581 #ifndef NO_XZ_SUPPORT
   1582 	case FT_XZ:
   1583 		if (lflag) {
   1584 			size = unxz_len(fd);
   1585 			print_list_out(in_size, size, file);
   1586 			return -1;
   1587 		}
   1588 		size = unxz(fd, zfd, NULL, 0, NULL);
   1589 		break;
   1590 #endif
   1591 
   1592 #ifndef SMALL
   1593 	case FT_UNKNOWN:
   1594 		if (lflag) {
   1595 			maybe_warnx("no -l for unknown filetypes");
   1596 			goto lose;
   1597 		}
   1598 		size = cat_fd(NULL, 0, NULL, fd);
   1599 		break;
   1600 #endif
   1601 	default:
   1602 		if (lflag) {
   1603 			print_list(fd, in_size, outfile, isb.st_mtime);
   1604 			close(fd);
   1605 			return -1;	/* XXX */
   1606 		}
   1607 
   1608 		size = gz_uncompress(fd, zfd, NULL, 0, NULL, file);
   1609 		break;
   1610 	}
   1611 
   1612 	if (close(fd) != 0)
   1613 		maybe_warn("couldn't close input");
   1614 	if (zfd != STDOUT_FILENO && close(zfd) != 0)
   1615 		maybe_warn("couldn't close output");
   1616 
   1617 	if (size == -1) {
   1618 		if (cflag == 0)
   1619 			unlink(outfile);
   1620 		maybe_warnx("%s: uncompress failed", file);
   1621 		return -1;
   1622 	}
   1623 
   1624 	/* if testing, or we uncompressed to stdout, this is all we need */
   1625 #ifndef SMALL
   1626 	if (tflag)
   1627 		return size;
   1628 #endif
   1629 	/* if we are uncompressing to stdin, don't remove the file. */
   1630 	if (cflag)
   1631 		return size;
   1632 
   1633 	/*
   1634 	 * if we create a file...
   1635 	 */
   1636 	/*
   1637 	 * if we can't stat the file don't remove the file.
   1638 	 */
   1639 
   1640 	ofd = open(outfile, O_RDWR, 0);
   1641 	if (ofd == -1) {
   1642 		maybe_warn("couldn't open (leaving original): %s",
   1643 			   outfile);
   1644 		return -1;
   1645 	}
   1646 	if (fstat(ofd, &osb) != 0) {
   1647 		maybe_warn("couldn't stat (leaving original): %s",
   1648 			   outfile);
   1649 		close(ofd);
   1650 		return -1;
   1651 	}
   1652 	if (osb.st_size != size) {
   1653 		maybe_warnx("stat gave different size: %" PRIdOFF
   1654 				" != %" PRIdOFF " (leaving original)",
   1655 				size, osb.st_size);
   1656 		close(ofd);
   1657 		unlink(outfile);
   1658 		return -1;
   1659 	}
   1660 	unlink_input(file, &isb);
   1661 #ifndef SMALL
   1662 	copymodes(ofd, &isb, outfile);
   1663 #endif
   1664 	close(ofd);
   1665 	return size;
   1666 
   1667     unexpected_EOF:
   1668 	maybe_warnx("%s: unexpected end of file", file);
   1669     lose:
   1670 	if (fd != -1)
   1671 		close(fd);
   1672 	if (zfd != -1 && zfd != STDOUT_FILENO)
   1673 		close(fd);
   1674 	return -1;
   1675 }
   1676 
   1677 #ifndef SMALL
   1678 static void
   1679 check_siginfo(void)
   1680 {
   1681 	if (print_info == 0)
   1682 		return;
   1683 	if (infile) {
   1684 		if (infile_total) {
   1685 			int pcent = (int)((100.0 * infile_current) / infile_total);
   1686 
   1687 			fprintf(stderr, "%s: done %llu/%llu bytes %d%%\n",
   1688 				infile, (unsigned long long)infile_current,
   1689 				(unsigned long long)infile_total, pcent);
   1690 		} else
   1691 			fprintf(stderr, "%s: done %llu bytes\n",
   1692 				infile, (unsigned long long)infile_current);
   1693 	}
   1694 	print_info = 0;
   1695 }
   1696 
   1697 static off_t
   1698 cat_fd(unsigned char * prepend, size_t count, off_t *gsizep, int fd)
   1699 {
   1700 	char buf[BUFLEN];
   1701 	off_t in_tot;
   1702 	ssize_t w;
   1703 
   1704 	in_tot = count;
   1705 	w = write_retry(STDOUT_FILENO, prepend, count);
   1706 	if (w == -1 || (size_t)w != count) {
   1707 		maybe_warn("write to stdout");
   1708 		return -1;
   1709 	}
   1710 	for (;;) {
   1711 		ssize_t rv;
   1712 
   1713 		rv = read(fd, buf, sizeof buf);
   1714 		if (rv == 0)
   1715 			break;
   1716 		if (rv < 0) {
   1717 			maybe_warn("read from fd %d", fd);
   1718 			break;
   1719 		}
   1720 		infile_newdata(rv);
   1721 
   1722 		if (write_retry(STDOUT_FILENO, buf, rv) != rv) {
   1723 			maybe_warn("write to stdout");
   1724 			break;
   1725 		}
   1726 		in_tot += rv;
   1727 	}
   1728 
   1729 	if (gsizep)
   1730 		*gsizep = in_tot;
   1731 	return (in_tot);
   1732 }
   1733 #endif
   1734 
   1735 static void
   1736 handle_stdin(void)
   1737 {
   1738 	struct stat isb;
   1739 	unsigned char header1[4];
   1740 	size_t in_size;
   1741 	off_t usize, gsize;
   1742 	enum filetype method;
   1743 	ssize_t bytes_read;
   1744 #ifndef NO_COMPRESS_SUPPORT
   1745 	FILE *in;
   1746 #endif
   1747 
   1748 #ifndef SMALL
   1749 	if (fflag == 0 && lflag == 0 && isatty(STDIN_FILENO)) {
   1750 		maybe_warnx("standard input is a terminal -- ignoring");
   1751 		goto out;
   1752 	}
   1753 #endif
   1754 
   1755 	if (fstat(STDIN_FILENO, &isb) < 0) {
   1756 		maybe_warn("fstat");
   1757 		goto out;
   1758 	}
   1759 	if (S_ISREG(isb.st_mode))
   1760 		in_size = isb.st_size;
   1761 	else
   1762 		in_size = 0;
   1763 	infile_set("(stdin)", in_size);
   1764 
   1765 	if (lflag) {
   1766 		print_list(STDIN_FILENO, in_size, infile, isb.st_mtime);
   1767 		goto out;
   1768 	}
   1769 
   1770 	bytes_read = read_retry(STDIN_FILENO, header1, sizeof header1);
   1771 	if (bytes_read == -1) {
   1772 		maybe_warn("can't read stdin");
   1773 		goto out;
   1774 	} else if (bytes_read != sizeof(header1)) {
   1775 		maybe_warnx("(stdin): unexpected end of file");
   1776 		goto out;
   1777 	}
   1778 
   1779 	method = file_gettype(header1);
   1780 	switch (method) {
   1781 	default:
   1782 #ifndef SMALL
   1783 		if (fflag == 0) {
   1784 			maybe_warnx("unknown compression format");
   1785 			goto out;
   1786 		}
   1787 		usize = cat_fd(header1, sizeof header1, &gsize, STDIN_FILENO);
   1788 		break;
   1789 #endif
   1790 	case FT_GZIP:
   1791 		usize = gz_uncompress(STDIN_FILENO, STDOUT_FILENO,
   1792 			      (char *)header1, sizeof header1, &gsize, "(stdin)");
   1793 		break;
   1794 #ifndef NO_BZIP2_SUPPORT
   1795 	case FT_BZIP2:
   1796 		usize = unbzip2(STDIN_FILENO, STDOUT_FILENO,
   1797 				(char *)header1, sizeof header1, &gsize);
   1798 		break;
   1799 #endif
   1800 #ifndef NO_COMPRESS_SUPPORT
   1801 	case FT_Z:
   1802 		if ((in = zdopen(STDIN_FILENO)) == NULL) {
   1803 			maybe_warnx("zopen of stdin");
   1804 			goto out;
   1805 		}
   1806 
   1807 		usize = zuncompress(in, stdout, (char *)header1,
   1808 		    sizeof header1, &gsize);
   1809 		fclose(in);
   1810 		break;
   1811 #endif
   1812 #ifndef NO_PACK_SUPPORT
   1813 	case FT_PACK:
   1814 		usize = unpack(STDIN_FILENO, STDOUT_FILENO,
   1815 			       (char *)header1, sizeof header1, &gsize);
   1816 		break;
   1817 #endif
   1818 #ifndef NO_XZ_SUPPORT
   1819 	case FT_XZ:
   1820 		usize = unxz(STDIN_FILENO, STDOUT_FILENO,
   1821 			     (char *)header1, sizeof header1, &gsize);
   1822 		break;
   1823 #endif
   1824 	}
   1825 
   1826 #ifndef SMALL
   1827         if (vflag && !tflag && usize != -1 && gsize != -1)
   1828 		print_verbage(NULL, NULL, usize, gsize);
   1829 	if (vflag && tflag)
   1830 		print_test("(stdin)", usize != -1);
   1831 #else
   1832 	(void)&usize;
   1833 #endif
   1834 
   1835 out:
   1836 	infile_clear();
   1837 }
   1838 
   1839 static void
   1840 handle_stdout(void)
   1841 {
   1842 	off_t gsize;
   1843 #ifndef SMALL
   1844 	off_t usize;
   1845 	struct stat sb;
   1846 	time_t systime;
   1847 	uint32_t mtime;
   1848 	int ret;
   1849 
   1850 	infile_set("(stdout)", 0);
   1851 
   1852 	if (fflag == 0 && isatty(STDOUT_FILENO)) {
   1853 		maybe_warnx("standard output is a terminal -- ignoring");
   1854 		return;
   1855 	}
   1856 
   1857 	/* If stdin is a file use its mtime, otherwise use current time */
   1858 	ret = fstat(STDIN_FILENO, &sb);
   1859 	if (ret < 0) {
   1860 		maybe_warn("Can't stat stdin");
   1861 		return;
   1862 	}
   1863 
   1864 	if (S_ISREG(sb.st_mode)) {
   1865 		infile_set("(stdout)", sb.st_size);
   1866 		mtime = (uint32_t)sb.st_mtime;
   1867 	} else {
   1868 		systime = time(NULL);
   1869 		if (systime == -1) {
   1870 			maybe_warn("time");
   1871 			return;
   1872 		}
   1873 		mtime = (uint32_t)systime;
   1874 	}
   1875 
   1876 	usize =
   1877 #endif
   1878 		gz_compress(STDIN_FILENO, STDOUT_FILENO, &gsize, "", mtime);
   1879 #ifndef SMALL
   1880         if (vflag && !tflag && usize != -1 && gsize != -1)
   1881 		print_verbage(NULL, NULL, usize, gsize);
   1882 #endif
   1883 }
   1884 
   1885 /* do what is asked for, for the path name */
   1886 static void
   1887 handle_pathname(char *path)
   1888 {
   1889 	char *opath = path, *s = NULL;
   1890 	ssize_t len;
   1891 	int slen;
   1892 	struct stat sb;
   1893 
   1894 	/* check for stdout/stdin */
   1895 	if (path[0] == '-' && path[1] == '\0') {
   1896 		if (dflag)
   1897 			handle_stdin();
   1898 		else
   1899 			handle_stdout();
   1900 		return;
   1901 	}
   1902 
   1903 retry:
   1904 	if (stat(path, &sb) != 0) {
   1905 		/* lets try <path>.gz if we're decompressing */
   1906 		if (dflag && s == NULL && errno == ENOENT) {
   1907 			len = strlen(path);
   1908 			slen = suffixes[0].ziplen;
   1909 			s = malloc(len + slen + 1);
   1910 			if (s == NULL)
   1911 				maybe_err("malloc");
   1912 			memcpy(s, path, len);
   1913 			memcpy(s + len, suffixes[0].zipped, slen + 1);
   1914 			path = s;
   1915 			goto retry;
   1916 		}
   1917 		maybe_warn("can't stat: %s", opath);
   1918 		goto out;
   1919 	}
   1920 
   1921 	if (S_ISDIR(sb.st_mode)) {
   1922 #ifndef SMALL
   1923 		if (rflag)
   1924 			handle_dir(path);
   1925 		else
   1926 #endif
   1927 			maybe_warnx("%s is a directory", path);
   1928 		goto out;
   1929 	}
   1930 
   1931 	if (S_ISREG(sb.st_mode))
   1932 		handle_file(path, &sb);
   1933 	else
   1934 		maybe_warnx("%s is not a regular file", path);
   1935 
   1936 out:
   1937 	if (s)
   1938 		free(s);
   1939 }
   1940 
   1941 /* compress/decompress a file */
   1942 static void
   1943 handle_file(char *file, struct stat *sbp)
   1944 {
   1945 	off_t usize, gsize;
   1946 	char	outfile[PATH_MAX];
   1947 
   1948 	infile_set(file, sbp->st_size);
   1949 	if (dflag) {
   1950 		usize = file_uncompress(file, outfile, sizeof(outfile));
   1951 #ifndef SMALL
   1952 		if (vflag && tflag)
   1953 			print_test(file, usize != -1);
   1954 #endif
   1955 		if (usize == -1)
   1956 			return;
   1957 		gsize = sbp->st_size;
   1958 	} else {
   1959 		gsize = file_compress(file, outfile, sizeof(outfile));
   1960 		if (gsize == -1)
   1961 			return;
   1962 		usize = sbp->st_size;
   1963 	}
   1964 	infile_clear();
   1965 
   1966 #ifndef SMALL
   1967 	if (vflag && !tflag)
   1968 		print_verbage(file, (cflag) ? NULL : outfile, usize, gsize);
   1969 #endif
   1970 }
   1971 
   1972 #ifndef SMALL
   1973 /* this is used with -r to recursively descend directories */
   1974 static void
   1975 handle_dir(char *dir)
   1976 {
   1977 	char *path_argv[2];
   1978 	FTS *fts;
   1979 	FTSENT *entry;
   1980 
   1981 	path_argv[0] = dir;
   1982 	path_argv[1] = 0;
   1983 	fts = fts_open(path_argv, FTS_PHYSICAL, NULL);
   1984 	if (fts == NULL) {
   1985 		warn("couldn't fts_open %s", dir);
   1986 		return;
   1987 	}
   1988 
   1989 	while ((entry = fts_read(fts))) {
   1990 		switch(entry->fts_info) {
   1991 		case FTS_D:
   1992 		case FTS_DP:
   1993 			continue;
   1994 
   1995 		case FTS_DNR:
   1996 		case FTS_ERR:
   1997 		case FTS_NS:
   1998 			maybe_warn("%s", entry->fts_path);
   1999 			continue;
   2000 		case FTS_F:
   2001 			handle_file(entry->fts_name, entry->fts_statp);
   2002 		}
   2003 	}
   2004 	(void)fts_close(fts);
   2005 }
   2006 #endif
   2007 
   2008 /* print a ratio - size reduction as a fraction of uncompressed size */
   2009 static void
   2010 print_ratio(off_t in, off_t out, FILE *where)
   2011 {
   2012 	int percent10;	/* 10 * percent */
   2013 	off_t diff;
   2014 	char buff[8];
   2015 	int len;
   2016 
   2017 	diff = in - out/2;
   2018 	if (in == 0 && out == 0)
   2019 		percent10 = 0;
   2020 	else if (diff < 0)
   2021 		/*
   2022 		 * Output is more than double size of input! print -99.9%
   2023 		 * Quite possibly we've failed to get the original size.
   2024 		 */
   2025 		percent10 = -999;
   2026 	else {
   2027 		/*
   2028 		 * We only need 12 bits of result from the final division,
   2029 		 * so reduce the values until a 32bit division will suffice.
   2030 		 */
   2031 		while (in > 0x100000) {
   2032 			diff >>= 1;
   2033 			in >>= 1;
   2034 		}
   2035 		if (in != 0)
   2036 			percent10 = ((u_int)diff * 2000) / (u_int)in - 1000;
   2037 		else
   2038 			percent10 = 0;
   2039 	}
   2040 
   2041 	len = snprintf(buff, sizeof buff, "%2.2d.", percent10);
   2042 	/* Move the '.' to before the last digit */
   2043 	buff[len - 1] = buff[len - 2];
   2044 	buff[len - 2] = '.';
   2045 	fprintf(where, "%5s%%", buff);
   2046 }
   2047 
   2048 #ifndef SMALL
   2049 /* print compression statistics, and the new name (if there is one!) */
   2050 static void
   2051 print_verbage(const char *file, const char *nfile, off_t usize, off_t gsize)
   2052 {
   2053 	if (file)
   2054 		fprintf(stderr, "%s:%s  ", file,
   2055 		    strlen(file) < 7 ? "\t\t" : "\t");
   2056 	print_ratio(usize, gsize, stderr);
   2057 	if (nfile)
   2058 		fprintf(stderr, " -- replaced with %s", nfile);
   2059 	fprintf(stderr, "\n");
   2060 	fflush(stderr);
   2061 }
   2062 
   2063 /* print test results */
   2064 static void
   2065 print_test(const char *file, int ok)
   2066 {
   2067 
   2068 	if (exit_value == 0 && ok == 0)
   2069 		exit_value = 1;
   2070 	fprintf(stderr, "%s:%s  %s\n", file,
   2071 	    strlen(file) < 7 ? "\t\t" : "\t", ok ? "OK" : "NOT OK");
   2072 	fflush(stderr);
   2073 }
   2074 #endif
   2075 
   2076 /* print a file's info ala --list */
   2077 /* eg:
   2078   compressed uncompressed  ratio uncompressed_name
   2079       354841      1679360  78.8% /usr/pkgsrc/distfiles/libglade-2.0.1.tar
   2080 */
   2081 static void
   2082 print_list(int fd, off_t out, const char *outfile, time_t ts)
   2083 {
   2084 	static int first = 1;
   2085 #ifndef SMALL
   2086 	static off_t in_tot, out_tot;
   2087 	uint32_t crc = 0;
   2088 #endif
   2089 	off_t in = 0, rv;
   2090 
   2091 	if (first) {
   2092 #ifndef SMALL
   2093 		if (vflag)
   2094 			printf("method  crc     date  time  ");
   2095 #endif
   2096 		if (qflag == 0)
   2097 			printf("  compressed uncompressed  "
   2098 			       "ratio uncompressed_name\n");
   2099 	}
   2100 	first = 0;
   2101 
   2102 	/* print totals? */
   2103 #ifndef SMALL
   2104 	if (fd == -1) {
   2105 		in = in_tot;
   2106 		out = out_tot;
   2107 	} else
   2108 #endif
   2109 	{
   2110 		/* read the last 4 bytes - this is the uncompressed size */
   2111 		rv = lseek(fd, (off_t)(-8), SEEK_END);
   2112 		if (rv != -1) {
   2113 			unsigned char buf[8];
   2114 			uint32_t usize;
   2115 
   2116 			rv = read(fd, (char *)buf, sizeof(buf));
   2117 			if (rv == -1)
   2118 				maybe_warn("read of uncompressed size");
   2119 			else if (rv != sizeof(buf))
   2120 				maybe_warnx("read of uncompressed size");
   2121 
   2122 			else {
   2123 				usize = buf[4];
   2124 				usize |= (unsigned int)buf[5] << 8;
   2125 				usize |= (unsigned int)buf[6] << 16;
   2126 				usize |= (unsigned int)buf[7] << 24;
   2127 				in = (off_t)usize;
   2128 #ifndef SMALL
   2129 				crc = buf[0];
   2130 				crc |= (unsigned int)buf[1] << 8;
   2131 				crc |= (unsigned int)buf[2] << 16;
   2132 				crc |= (unsigned int)buf[3] << 24;
   2133 #endif
   2134 			}
   2135 		}
   2136 	}
   2137 
   2138 #ifndef SMALL
   2139 	if (vflag && fd == -1)
   2140 		printf("                            ");
   2141 	else if (vflag) {
   2142 		char *date = ctime(&ts);
   2143 
   2144 		/* skip the day, 1/100th second, and year */
   2145 		date += 4;
   2146 		date[12] = 0;
   2147 		printf("%5s %08x %11s ", "defla"/*XXX*/, crc, date);
   2148 	}
   2149 	in_tot += in;
   2150 	out_tot += out;
   2151 #endif
   2152 	print_list_out(out, in, outfile);
   2153 }
   2154 
   2155 static void
   2156 print_list_out(off_t out, off_t in, const char *outfile)
   2157 {
   2158 	printf("%12llu %12llu ", (unsigned long long)out, (unsigned long long)in);
   2159 	print_ratio(in, out, stdout);
   2160 	printf(" %s\n", outfile);
   2161 }
   2162 
   2163 /* display the usage of NetBSD gzip */
   2164 static void
   2165 usage(void)
   2166 {
   2167 
   2168 	fprintf(stderr, "%s\n", gzip_version);
   2169 	fprintf(stderr,
   2170     "usage: %s [-" OPT_LIST "] [<file> [<file> ...]]\n"
   2171 #ifndef SMALL
   2172     " -1 --fast            fastest (worst) compression\n"
   2173     " -2 .. -8             set compression level\n"
   2174     " -9 --best            best (slowest) compression\n"
   2175     " -c --stdout          write to stdout, keep original files\n"
   2176     "    --to-stdout\n"
   2177     " -d --decompress      uncompress files\n"
   2178     "    --uncompress\n"
   2179     " -f --force           force overwriting & compress links\n"
   2180     " -h --help            display this help\n"
   2181     " -k --keep            don't delete input files during operation\n"
   2182     " -l --list            list compressed file contents\n"
   2183     " -N --name            save or restore original file name and time stamp\n"
   2184     " -n --no-name         don't save original file name or time stamp\n"
   2185     " -q --quiet           output no warnings\n"
   2186     " -r --recursive       recursively compress files in directories\n"
   2187     " -S .suf              use suffix .suf instead of .gz\n"
   2188     "    --suffix .suf\n"
   2189     " -t --test            test compressed file\n"
   2190     " -V --version         display program version\n"
   2191     " -v --verbose         print extra statistics\n",
   2192 #else
   2193     ,
   2194 #endif
   2195 	    getprogname());
   2196 	exit(0);
   2197 }
   2198 
   2199 /* display the version of NetBSD gzip */
   2200 static void
   2201 display_version(void)
   2202 {
   2203 
   2204 	fprintf(stderr, "%s\n", gzip_version);
   2205 	exit(0);
   2206 }
   2207 
   2208 #ifndef NO_BZIP2_SUPPORT
   2209 #include "unbzip2.c"
   2210 #endif
   2211 #ifndef NO_COMPRESS_SUPPORT
   2212 #include "zuncompress.c"
   2213 #endif
   2214 #ifndef NO_PACK_SUPPORT
   2215 #include "unpack.c"
   2216 #endif
   2217 #ifndef NO_XZ_SUPPORT
   2218 #include "unxz.c"
   2219 #endif
   2220 
   2221 static ssize_t
   2222 read_retry(int fd, void *buf, size_t sz)
   2223 {
   2224 	char *cp = buf;
   2225 	size_t left = MIN(sz, (size_t) SSIZE_MAX);
   2226 
   2227 	while (left > 0) {
   2228 		ssize_t ret;
   2229 
   2230 		ret = read(fd, cp, left);
   2231 		if (ret == -1) {
   2232 			return ret;
   2233 		} else if (ret == 0) {
   2234 			break; /* EOF */
   2235 		}
   2236 		cp += ret;
   2237 		left -= ret;
   2238 	}
   2239 
   2240 	return sz - left;
   2241 }
   2242 
   2243 static ssize_t
   2244 write_retry(int fd, const void *buf, size_t sz)
   2245 {
   2246 	const char *cp = buf;
   2247 	size_t left = MIN(sz, (size_t) SSIZE_MAX);
   2248 
   2249 	while (left > 0) {
   2250 		ssize_t ret;
   2251 
   2252 		ret = write(fd, cp, left);
   2253 		if (ret == -1) {
   2254 			return ret;
   2255 		} else if (ret == 0) {
   2256 			abort();	/* Can't happen */
   2257 		}
   2258 		cp += ret;
   2259 		left -= ret;
   2260 	}
   2261 
   2262 	return sz - left;
   2263 }
   2264