Home | History | Annotate | Line # | Download | only in gzip
gzip.c revision 1.38
      1 /*	$NetBSD: gzip.c,v 1.38 2004/04/26 03:01:55 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1998, 2003, 2004 Matthew R. Green
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 #ifndef lint
     33 __COPYRIGHT("@(#) Copyright (c) 1997, 1998, 2003, 2004 Matthew R. Green\n\
     34      All rights reserved.\n");
     35 __RCSID("$NetBSD: gzip.c,v 1.38 2004/04/26 03:01:55 mrg Exp $");
     36 #endif /* not lint */
     37 
     38 /*
     39  * gzip.c -- GPL free gzip using zlib.
     40  *
     41  * TODO:
     42  *	- handle .taz/.tgz files?
     43  *	- use mmap where possible
     44  *	- handle some signals better (remove outfile?)
     45  *	- audit maybe_err()/maybe_warn() usage
     46  */
     47 
     48 #include <sys/param.h>
     49 #include <sys/stat.h>
     50 #include <sys/time.h>
     51 
     52 #include <unistd.h>
     53 #include <stdio.h>
     54 #include <string.h>
     55 #include <stdlib.h>
     56 #include <err.h>
     57 #include <errno.h>
     58 #include <fcntl.h>
     59 #include <zlib.h>
     60 #include <fts.h>
     61 #include <libgen.h>
     62 #include <stdarg.h>
     63 #include <getopt.h>
     64 
     65 /* what type of file are we dealing with */
     66 enum filetype {
     67 	FT_GZIP,
     68 #ifndef NO_BZIP2_SUPPORT
     69 	FT_BZIP2,
     70 #endif
     71 #ifndef NO_COMPRESS_SUPPORT
     72 	FT_Z,
     73 #endif
     74 	FT_LAST,
     75 	FT_UNKNOWN
     76 };
     77 
     78 #ifndef NO_BZIP2_SUPPORT
     79 #include <bzlib.h>
     80 
     81 #define BZ2_SUFFIX	".bz2"
     82 #define BZIP2_MAGIC	"\102\132\150"
     83 #endif
     84 
     85 #ifndef NO_COMPRESS_SUPPORT
     86 #define Z_SUFFIX	".Z"
     87 #define Z_MAGIC		"\037\235"
     88 #endif
     89 
     90 #define GZ_SUFFIX	".gz"
     91 
     92 #define BUFLEN		(64 * 1024)
     93 
     94 #define GZIP_MAGIC0	0x1F
     95 #define GZIP_MAGIC1	0x8B
     96 #define GZIP_OMAGIC1	0x9E
     97 
     98 #define HEAD_CRC	0x02
     99 #define EXTRA_FIELD	0x04
    100 #define ORIG_NAME	0x08
    101 #define COMMENT		0x10
    102 
    103 #define OS_CODE		3	/* Unix */
    104 
    105 static	const char	gzip_version[] = "NetBSD gzip 20040425";
    106 
    107 static	int	cflag;			/* stdout mode */
    108 static	int	dflag;			/* decompress mode */
    109 static	int	lflag;			/* list mode */
    110 static	int	numflag = 5;		/* gzip -1..-9 value */
    111 
    112 #ifndef SMALL
    113 static	int	fflag;			/* force mode */
    114 static	int	nflag;			/* don't save name/timestamp */
    115 static	int	Nflag;			/* don't restore name/timestamp */
    116 static	int	qflag;			/* quiet mode */
    117 static	int	rflag;			/* recursive mode */
    118 static	int	tflag;			/* test */
    119 static	char	*Sflag;
    120 static	int	vflag;			/* verbose mode */
    121 #else
    122 #define		qflag	0
    123 #endif
    124 
    125 static	char	*suffix;
    126 #define suffix_len	(strlen(suffix) + 1)	/* len + nul */
    127 static	char	*newfile;		/* name of newly created file */
    128 static	char	*infile;		/* name of file coming in */
    129 
    130 static	void	maybe_err(int rv, const char *fmt, ...);
    131 static	void	maybe_errx(int rv, const char *fmt, ...);
    132 static	void	maybe_warn(const char *fmt, ...);
    133 static	void	maybe_warnx(const char *fmt, ...);
    134 static	enum filetype file_gettype(u_char *);
    135 static	off_t	gz_compress(FILE *, int, off_t *, const char *, time_t);
    136 static	off_t	gz_uncompress(int, int, char *, size_t, off_t *);
    137 static	off_t	file_compress(char *);
    138 static	off_t	file_uncompress(char *);
    139 static	void	handle_pathname(char *);
    140 static	void	handle_file(char *, struct stat *);
    141 static	void	handle_stdin(void);
    142 static	void	handle_stdout(void);
    143 static	void	print_ratio(off_t, off_t, FILE *);
    144 static	void	print_list(int fd, off_t, const char *, time_t);
    145 static	void	usage(void);
    146 static	void	display_version(void);
    147 
    148 #ifndef SMALL
    149 static	void	prepend_gzip(char *, int *, char ***);
    150 static	void	handle_dir(char *, struct stat *);
    151 static	void	print_verbage(char *, char *, off_t, off_t);
    152 static	void	print_test(char *, int);
    153 static	void	copymodes(const char *, struct stat *);
    154 #endif
    155 
    156 #ifndef NO_BZIP2_SUPPORT
    157 static	off_t	unbzip2(int, int, char *, size_t, off_t *);
    158 #endif
    159 
    160 #ifndef NO_COMPRESS_SUPPORT
    161 static	FILE 	*zopen(const char *, FILE *);
    162 static	off_t	zuncompress(FILE *, FILE *, char *, size_t, off_t *);
    163 #endif
    164 
    165 int main(int, char *p[]);
    166 
    167 #ifdef SMALL
    168 #define getopt_long(a,b,c,d,e) getopt(a,b,c)
    169 #else
    170 static const struct option longopts[] = {
    171 	{ "stdout",		no_argument,		0,	'c' },
    172 	{ "to-stdout",		no_argument,		0,	'c' },
    173 	{ "decompress",		no_argument,		0,	'd' },
    174 	{ "uncompress",		no_argument,		0,	'd' },
    175 	{ "force",		no_argument,		0,	'f' },
    176 	{ "help",		no_argument,		0,	'h' },
    177 	{ "list",		no_argument,		0,	'l' },
    178 	{ "no-name",		no_argument,		0,	'n' },
    179 	{ "name",		no_argument,		0,	'N' },
    180 	{ "quiet",		no_argument,		0,	'q' },
    181 	{ "recursive",		no_argument,		0,	'r' },
    182 	{ "suffix",		required_argument,	0,	'S' },
    183 	{ "test",		no_argument,		0,	't' },
    184 	{ "verbose",		no_argument,		0,	'v' },
    185 	{ "version",		no_argument,		0,	'V' },
    186 	{ "fast",		no_argument,		0,	'1' },
    187 	{ "best",		no_argument,		0,	'9' },
    188 #if 0
    189 	/*
    190 	 * This is what else GNU gzip implements.  --ascii isn't useful
    191 	 * on NetBSD, and I don't care to have a --license.
    192 	 */
    193 	{ "ascii",		no_argument,		0,	'a' },
    194 	{ "license",		no_argument,		0,	'L' },
    195 #endif
    196 	{ NULL,			no_argument,		0,	0 },
    197 };
    198 #endif
    199 
    200 int
    201 main(int argc, char **argv)
    202 {
    203 	const char *progname = getprogname();
    204 #ifndef SMALL
    205 	char *gzip;
    206 #endif
    207 	int ch;
    208 
    209 	/* XXX set up signals */
    210 
    211 	suffix = GZ_SUFFIX;;
    212 
    213 #ifndef SMALL
    214 	if ((gzip = getenv("GZIP")) != NULL)
    215 		prepend_gzip(gzip, &argc, &argv);
    216 #endif
    217 
    218 	/*
    219 	 * XXX
    220 	 * handle being called `gunzip', `zcat' and `gzcat'
    221 	 */
    222 	if (strcmp(progname, "gunzip") == 0)
    223 		dflag = 1;
    224 	else if (strcmp(progname, "zcat") == 0 ||
    225 		 strcmp(progname, "gzcat") == 0)
    226 		dflag = cflag = 1;
    227 
    228 #ifdef SMALL
    229 #define OPT_LIST "cdhHltV123456789"
    230 #else
    231 #define OPT_LIST "cdfhHlnNqrS:tvV123456789"
    232 #endif
    233 
    234 	while ((ch = getopt_long(argc, argv, OPT_LIST, longopts, NULL)) != -1)
    235 		switch (ch) {
    236 		case 'c':
    237 			cflag = 1;
    238 			break;
    239 		case 'd':
    240 			dflag = 1;
    241 			break;
    242 		case 'l':
    243 			lflag = 1;
    244 			dflag = 1;
    245 			break;
    246 		case 'V':
    247 			display_version();
    248 			/* NOTREACHED */
    249 		case '1': case '2': case '3':
    250 		case '4': case '5': case '6':
    251 		case '7': case '8': case '9':
    252 			numflag = ch - '0';
    253 			break;
    254 #ifndef SMALL
    255 		case 'f':
    256 			fflag = 1;
    257 			break;
    258 		case 'n':
    259 			nflag = 1;
    260 			Nflag = 0;
    261 			break;
    262 		case 'N':
    263 			nflag = 0;
    264 			Nflag = 1;
    265 			break;
    266 		case 'q':
    267 			qflag = 1;
    268 			break;
    269 		case 'r':
    270 			rflag = 1;
    271 			break;
    272 		case 'S':
    273 			Sflag = optarg;
    274 			break;
    275 		case 't':
    276 			cflag = 1;
    277 			tflag = 1;
    278 			dflag = 1;
    279 			break;
    280 		case 'v':
    281 			vflag = 1;
    282 			break;
    283 #endif
    284 		default:
    285 			usage();
    286 			/* NOTREACHED */
    287 		}
    288 	argv += optind;
    289 	argc -= optind;
    290 
    291 	if (argc == 0) {
    292 		if (dflag)	/* stdin mode */
    293 			handle_stdin();
    294 		else		/* stdout mode */
    295 			handle_stdout();
    296 	} else {
    297 		do {
    298 			handle_pathname(argv[0]);
    299 		} while (*++argv);
    300 	}
    301 #ifndef SMALL
    302 	if (qflag == 0 && lflag && argc > 1)
    303 		print_list(-1, 0, "(totals)", 0);
    304 #endif
    305 	exit(0);
    306 }
    307 
    308 /* maybe print a warning */
    309 void
    310 maybe_warn(const char *fmt, ...)
    311 {
    312 	va_list ap;
    313 
    314 	if (qflag == 0) {
    315 		va_start(ap, fmt);
    316 		vwarn(fmt, ap);
    317 		va_end(ap);
    318 	}
    319 }
    320 
    321 void
    322 maybe_warnx(const char *fmt, ...)
    323 {
    324 	va_list ap;
    325 
    326 	if (qflag == 0) {
    327 		va_start(ap, fmt);
    328 		vwarnx(fmt, ap);
    329 		va_end(ap);
    330 	}
    331 }
    332 
    333 /* maybe print a warning */
    334 void
    335 maybe_err(int rv, const char *fmt, ...)
    336 {
    337 	va_list ap;
    338 
    339 	if (qflag == 0) {
    340 		va_start(ap, fmt);
    341 		vwarn(fmt, ap);
    342 		va_end(ap);
    343 	}
    344 	exit(rv);
    345 }
    346 
    347 /* maybe print a warning */
    348 void
    349 maybe_errx(int rv, const char *fmt, ...)
    350 {
    351 	va_list ap;
    352 
    353 	if (qflag == 0) {
    354 		va_start(ap, fmt);
    355 		vwarnx(fmt, ap);
    356 		va_end(ap);
    357 	}
    358 	exit(rv);
    359 }
    360 
    361 #ifndef SMALL
    362 /* split up $GZIP and prepend it to the argument list */
    363 static void
    364 prepend_gzip(char *gzip, int *argc, char ***argv)
    365 {
    366 	char *s, **nargv, **ac;
    367 	int nenvarg = 0, i;
    368 
    369 	/* scan how many arguments there are */
    370 	for (s = gzip; *s; s++) {
    371 		if (*s == ' ' || *s == '\t')
    372 			continue;
    373 		nenvarg++;
    374 		for (; *s; s++)
    375 			if (*s == ' ' || *s == '\t')
    376 				break;
    377 		if (*s == 0)
    378 			break;
    379 	}
    380 	/* punt early */
    381 	if (nenvarg == 0)
    382 		return;
    383 
    384 	*argc += nenvarg;
    385 	ac = *argv;
    386 
    387 	nargv = (char **)malloc((*argc + 1) * sizeof(char *));
    388 	if (nargv == NULL)
    389 		maybe_err(1, "malloc");
    390 
    391 	/* stash this away */
    392 	*argv = nargv;
    393 
    394 	/* copy the program name first */
    395 	i = 0;
    396 	nargv[i++] = *(ac++);
    397 
    398 	/* take a copy of $GZIP and add it to the array */
    399 	s = strdup(gzip);
    400 	if (s == NULL)
    401 		maybe_err(1, "strdup");
    402 	for (; *s; s++) {
    403 		if (*s == ' ' || *s == '\t')
    404 			continue;
    405 		nargv[i++] = s;
    406 		for (; *s; s++)
    407 			if (*s == ' ' || *s == '\t') {
    408 				*s = 0;
    409 				break;
    410 			}
    411 	}
    412 
    413 	/* copy the original arguments and a NULL */
    414 	while (*ac)
    415 		nargv[i++] = *(ac++);
    416 	nargv[i] = NULL;
    417 }
    418 #endif
    419 
    420 /* compress input to output then close both files */
    421 static off_t
    422 gz_compress(FILE *in, int out, off_t *gsizep, const char *origname, time_t mtime)
    423 {
    424 	z_stream z;
    425 	char inbuf[BUFLEN], outbuf[BUFLEN];
    426 	off_t in_tot = 0, out_tot = 0;
    427 	ssize_t in_size;
    428 	char *str;
    429 	int i, error;
    430 	uLong crc;
    431 
    432 	i = asprintf(&str, "%c%c%c%c%c%c%c%c%c%c%s",
    433 		     GZIP_MAGIC0, GZIP_MAGIC1,
    434 		     Z_DEFLATED, origname ? ORIG_NAME : 0,
    435 		     (int)mtime & 0xff,
    436 		     (int)(mtime >> 8) & 0xff,
    437 		     (int)(mtime >> 16) & 0xff,
    438 		     (int)(mtime >> 24) & 0xff,
    439 		     0, OS_CODE, origname ? origname : "");
    440 	if (i == -1)
    441 		maybe_err(1, "asprintf");
    442 	if (origname)
    443 		i++;
    444 	if (write(out, str, i) != i)
    445 		maybe_err(1, "write");
    446 	free(str);
    447 
    448 	memset(&z, 0, sizeof z);
    449 	z.next_out = outbuf;
    450 	z.avail_out = sizeof outbuf;
    451 	z.zalloc = Z_NULL;
    452 	z.zfree = Z_NULL;
    453 	z.opaque = 0;
    454 
    455 	error = deflateInit2(&z, numflag, Z_DEFLATED,
    456 			     -MAX_WBITS, 8, Z_DEFAULT_STRATEGY);
    457 	if (error != Z_OK)
    458 		maybe_errx(1, "deflateInit2 failed");
    459 
    460 	crc = crc32(0L, Z_NULL, 0);
    461 	for (;;) {
    462 		if (z.avail_out == 0) {
    463 			if (write(out, outbuf, sizeof outbuf) != sizeof outbuf)
    464 				maybe_err(1, "write");
    465 
    466 			out_tot += sizeof outbuf;
    467 			z.next_out = outbuf;
    468 			z.avail_out = sizeof outbuf;
    469 		}
    470 
    471 		if (z.avail_in == 0) {
    472 			in_size = fread(inbuf, 1, sizeof inbuf, in);
    473 			if (ferror(in))
    474 				maybe_err(1, "fread");
    475 			if (in_size == 0)
    476 				break;
    477 
    478 			crc = crc32(crc, (const Bytef *)inbuf, (unsigned)in_size);
    479 			in_tot += in_size;
    480 			z.next_in = inbuf;
    481 			z.avail_in = in_size;
    482 		}
    483 
    484 		error = deflate(&z, Z_NO_FLUSH);
    485 		if (error != Z_OK && error != Z_STREAM_END)
    486 			maybe_errx(1, "deflate failed");
    487 	}
    488 
    489 	/* clean up */
    490 	for (;;) {
    491 		size_t len;
    492 
    493 		error = deflate(&z, Z_FINISH);
    494 		if (error != Z_OK && error != Z_STREAM_END)
    495 			maybe_errx(1, "deflate failed");
    496 
    497 		len = sizeof outbuf - z.avail_out;
    498 
    499 		if (write(out, outbuf, len) != len)
    500 			maybe_err(1, "write");
    501 		out_tot += len;
    502 		z.next_out = outbuf;
    503 		z.avail_out = sizeof outbuf;
    504 
    505 		if (error == Z_STREAM_END)
    506 			break;
    507 	}
    508 
    509 	if (deflateEnd(&z) != Z_OK)
    510 		maybe_errx(1, "deflateEnd failed");
    511 
    512 	i = asprintf(&str, "%c%c%c%c%c%c%c%c",
    513 		 (int)crc & 0xff,
    514 		 (int)(crc >> 8) & 0xff,
    515 		 (int)(crc >> 16) & 0xff,
    516 		 (int)(crc >> 24) & 0xff,
    517 		 (int)in_tot & 0xff,
    518 		 (int)(in_tot >> 8) & 0xff,
    519 		 (int)(in_tot >> 16) & 0xff,
    520 		 (int)(in_tot >> 24) & 0xff);
    521 	if (i != 8)
    522 		maybe_err(1, "asprintf");
    523 	if (write(out, str, i) != i)
    524 		maybe_err(1, "write");
    525 	free(str);
    526 
    527 	if (fclose(in) < 0)
    528 		maybe_err(1, "failed fclose");
    529 
    530 	if (gsizep)
    531 		*gsizep = out_tot;
    532 	return in_tot;
    533 }
    534 
    535 /*
    536  * uncompress input to output then close the input.  return the
    537  * uncompressed size written, and put the compressed sized read
    538  * into `*gsizep'.
    539  */
    540 static off_t
    541 gz_uncompress(int in, int out, char *pre, size_t prelen, off_t *gsizep)
    542 {
    543 	z_stream z;
    544 	char outbuf[BUFLEN], inbuf[BUFLEN];
    545 	off_t out_tot, in_tot;
    546 	enum {
    547 		GZSTATE_MAGIC0,
    548 		GZSTATE_MAGIC1,
    549 		GZSTATE_METHOD,
    550 		GZSTATE_FLAGS,
    551 		GZSTATE_SKIPPING,
    552 		GZSTATE_EXTRA,
    553 		GZSTATE_EXTRA2,
    554 		GZSTATE_EXTRA3,
    555 		GZSTATE_ORIGNAME,
    556 		GZSTATE_COMMENT,
    557 		GZSTATE_HEAD_CRC1,
    558 		GZSTATE_HEAD_CRC2,
    559 		GZSTATE_INIT,
    560 		GZSTATE_READ
    561 	} state = GZSTATE_MAGIC0;
    562 	int flags = 0, skip_count = 0;
    563 	int error, done_reading = 0;
    564 
    565 #define ADVANCE()       { z.next_in++; z.avail_in--; }
    566 
    567 	memset(&z, 0, sizeof z);
    568 	z.avail_in = prelen;
    569 	z.next_in = pre;
    570 	z.avail_out = sizeof outbuf;
    571 	z.next_out = outbuf;
    572 	z.zalloc = NULL;
    573 	z.zfree = NULL;
    574 	z.opaque = 0;
    575 
    576 	in_tot = prelen;
    577 	out_tot = 0;
    578 
    579 	for (;;) {
    580 		if (z.avail_in == 0 && done_reading == 0) {
    581 			size_t in_size = read(in, inbuf, BUFLEN);
    582 
    583 			if (in_size == -1) {
    584 #ifndef SMALL
    585 				if (tflag) {
    586 					print_test("(stdin)", 0);
    587 					return 0;
    588 				}
    589 #endif
    590 				maybe_warn("failed to read stdin\n");
    591 				return -1;
    592 			} else if (in_size == 0)
    593 				done_reading = 1;
    594 
    595 			z.avail_in = in_size;
    596 			z.next_in = inbuf;
    597 
    598 			in_tot += in_size;
    599 		}
    600 		switch (state) {
    601 		case GZSTATE_MAGIC0:
    602 			if (*z.next_in != GZIP_MAGIC0)
    603 				maybe_err(1, "input not gziped\n");
    604 			ADVANCE();
    605 			state++;
    606 			break;
    607 
    608 		case GZSTATE_MAGIC1:
    609 			if (*z.next_in != GZIP_MAGIC1 &&
    610 			    *z.next_in != GZIP_OMAGIC1)
    611 				maybe_err(1, "input not gziped\n");
    612 			ADVANCE();
    613 			state++;
    614 			break;
    615 
    616 		case GZSTATE_METHOD:
    617 			if (*z.next_in != Z_DEFLATED)
    618 				maybe_err(1, "unknown compression method\n");
    619 			ADVANCE();
    620 			state++;
    621 			break;
    622 
    623 		case GZSTATE_FLAGS:
    624 			flags = *z.next_in;
    625 			ADVANCE();
    626 			skip_count = 6;
    627 			state++;
    628 			break;
    629 
    630 		case GZSTATE_SKIPPING:
    631 			if (skip_count > 0) {
    632 				skip_count--;
    633 				ADVANCE();
    634 			} else
    635 				state++;
    636 			break;
    637 
    638 		case GZSTATE_EXTRA:
    639 			if ((flags & EXTRA_FIELD) == 0) {
    640 				state = GZSTATE_ORIGNAME;
    641 				break;
    642 			}
    643 			skip_count = *z.next_in;
    644 			ADVANCE();
    645 			state++;
    646 			break;
    647 
    648 		case GZSTATE_EXTRA2:
    649 			skip_count |= ((*z.next_in) << 8);
    650 			ADVANCE();
    651 			state++;
    652 			break;
    653 
    654 		case GZSTATE_EXTRA3:
    655 			if (skip_count > 0) {
    656 				skip_count--;
    657 				ADVANCE();
    658 			} else
    659 				state++;
    660 			break;
    661 
    662 		case GZSTATE_ORIGNAME:
    663 			if ((flags & ORIG_NAME) == 0) {
    664 				state++;
    665 				break;
    666 			}
    667 			if (*z.next_in == 0)
    668 				state++;
    669 			ADVANCE();
    670 			break;
    671 
    672 		case GZSTATE_COMMENT:
    673 			if ((flags & COMMENT) == 0) {
    674 				state++;
    675 				break;
    676 			}
    677 			if (*z.next_in == 0)
    678 				state++;
    679 			ADVANCE();
    680 			break;
    681 
    682 		case GZSTATE_HEAD_CRC1:
    683 			if (flags & HEAD_CRC)
    684 				skip_count = 2;
    685 			else
    686 				skip_count = 0;
    687 			state++;
    688 			break;
    689 
    690 		case GZSTATE_HEAD_CRC2:
    691 			if (skip_count > 0) {
    692 				skip_count--;
    693 				ADVANCE();
    694 			} else
    695 				state++;
    696 			break;
    697 
    698 		case GZSTATE_INIT:
    699 			if (inflateInit2(&z, -MAX_WBITS) != Z_OK) {
    700 				maybe_err(1, "failed to inflateInit\n");
    701 				goto stop;
    702 			}
    703 			state++;
    704 			break;
    705 
    706 		case GZSTATE_READ:
    707 			error = inflate(&z, Z_FINISH);
    708 			if (error == Z_STREAM_END || error == Z_BUF_ERROR) {
    709 				size_t wr = BUFLEN - z.avail_out;
    710 
    711 				if (
    712 #ifndef SMALL
    713 				    /* don't write anything with -t */
    714 				    tflag == 0 &&
    715 #endif
    716 				    write(STDOUT_FILENO, outbuf, wr) != wr)
    717 					maybe_err(1, "error writing "
    718 						     "to stdout\n");
    719 
    720 				if (error == Z_STREAM_END)
    721 					goto stop;
    722 				z.next_out = outbuf;
    723 				z.avail_out = BUFLEN;
    724 
    725 				out_tot += wr;
    726 				break;
    727 			}
    728 			if (error < 0) {
    729 				maybe_warnx("decompression error\n");
    730 				out_tot = -1;
    731 				goto stop;
    732 			}
    733 			break;
    734 		}
    735 		continue;
    736 stop:
    737 		break;
    738 	}
    739 	if (state > GZSTATE_INIT)
    740 		inflateEnd(&z);
    741 
    742 #ifndef SMALL
    743 	if (tflag) {
    744 		print_test("(stdin)", 1);
    745 		return 0;
    746 	}
    747 #endif
    748 
    749 	if (gsizep)
    750 		*gsizep = in_tot;
    751 	return (out_tot);
    752 }
    753 
    754 #ifndef SMALL
    755 /*
    756  * set the owner, mode, flags & utimes for a file
    757  */
    758 static void
    759 copymodes(const char *file, struct stat *sbp)
    760 {
    761 	struct timeval times[2];
    762 
    763 	/*
    764 	 * If we have no info on the input, give this file some
    765 	 * default values and return..
    766 	 */
    767 	if (sbp == NULL) {
    768 		mode_t mask = umask(022);
    769 
    770 		(void)chmod(file, DEFFILEMODE & ~mask);
    771 		(void)umask(mask);
    772 		return;
    773 	}
    774 
    775 	/* if the chown fails, remove set-id bits as-per compress(1) */
    776 	if (chown(file, sbp->st_uid, sbp->st_gid) < 0) {
    777 		if (errno != EPERM)
    778 			maybe_warn("couldn't chown: %s", file);
    779 		sbp->st_mode &= ~(S_ISUID|S_ISGID);
    780 	}
    781 
    782 	/* we only allow set-id and the 9 normal permission bits */
    783 	sbp->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
    784 	if (chmod(file, sbp->st_mode) < 0)
    785 		maybe_warn("couldn't chmod: %s", file);
    786 
    787 	/* only try flags if they exist already */
    788         if (sbp->st_flags != 0 && chflags(file, sbp->st_flags) < 0)
    789 		maybe_warn("couldn't chflags: %s", file);
    790 
    791 	TIMESPEC_TO_TIMEVAL(&times[0], &sbp->st_atimespec);
    792 	TIMESPEC_TO_TIMEVAL(&times[1], &sbp->st_mtimespec);
    793 	if (utimes(file, times) < 0)
    794 		maybe_warn("couldn't utimes: %s", file);
    795 }
    796 #endif
    797 
    798 /* what sort of file is this? */
    799 static enum filetype
    800 file_gettype(u_char *buf)
    801 {
    802 
    803 	if (buf[0] == GZIP_MAGIC0 &&
    804 	    (buf[1] == GZIP_MAGIC1 || buf[1] == GZIP_OMAGIC1))
    805 		return FT_GZIP;
    806 	else
    807 #ifndef NO_BZIP2_SUPPORT
    808 	if (memcmp(buf, BZIP2_MAGIC, 3) == 0 &&
    809 	    buf[3] >= '0' && buf[3] <= '9')
    810 		return FT_BZIP2;
    811 	else
    812 #endif
    813 #ifndef NO_COMPRESS_SUPPORT
    814 	if (memcmp(buf, Z_MAGIC, 2) == 0)
    815 		return FT_Z;
    816 	else
    817 #endif
    818 		return FT_UNKNOWN;
    819 }
    820 
    821 /*
    822  * compress the given file: create a corresponding .gz file and remove the
    823  * original.
    824  */
    825 static off_t
    826 file_compress(char *file)
    827 {
    828 	FILE *in;
    829 	int out;
    830 	struct stat isb, osb;
    831 	char outfile[MAXPATHLEN];
    832 	off_t size;
    833 #ifndef SMALL
    834 	u_int32_t mtime = 0;
    835 	char *savename;
    836 #endif
    837 
    838 	if (cflag == 0) {
    839 		(void)strncpy(outfile, file, MAXPATHLEN - suffix_len);
    840 		outfile[MAXPATHLEN - suffix_len] = '\0';
    841 		(void)strlcat(outfile, suffix, sizeof(outfile));
    842 
    843 #ifndef SMALL
    844 		if (fflag == 0) {
    845 			if (stat(outfile, &osb) == 0) {
    846 				maybe_warnx("%s already exists -- skipping",
    847 					      outfile);
    848 				goto lose;
    849 			}
    850 		}
    851 		if (stat(file, &isb) == 0) {
    852 			if (isb.st_nlink > 1 && fflag == 0) {
    853 				maybe_warnx("%s has %d other link%s -- "
    854 					    "skipping", file, isb.st_nlink - 1,
    855 					    isb.st_nlink == 1 ? "" : "s");
    856 				goto lose;
    857 			}
    858 			if (nflag == 0)
    859 				mtime = (u_int32_t)isb.st_mtime;
    860 		}
    861 #endif
    862 	}
    863 	in = fopen(file, "r");
    864 	if (in == 0)
    865 		maybe_err(1, "can't fopen %s", file);
    866 
    867 	if (cflag == 0) {
    868 #ifndef SMALL
    869 		if (nflag == 0)
    870 			savename = basename(file);
    871 		else
    872 			savename = NULL;
    873 #endif
    874 		out = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0600);
    875 	} else
    876 		out = STDOUT_FILENO;
    877 
    878 #ifdef SMALL
    879 	gz_compress(in, out, NULL, NULL, 0);
    880 #else
    881 	gz_compress(in, out, NULL, savename, mtime);
    882 #endif
    883 
    884 	/*
    885 	 * if we compressed to stdout, we don't know the size and
    886 	 * we don't know the new file name, punt.  if we can't stat
    887 	 * the file, whine, otherwise set the size from the stat
    888 	 * buffer.  we only blow away the file if we can stat the
    889 	 * output, just in case.
    890 	 */
    891 	if (cflag == 0) {
    892 		if (stat(outfile, &osb) < 0) {
    893 			maybe_warn("couldn't stat: %s", outfile);
    894 			maybe_warnx("leaving original %s", file);
    895 			size = 0;
    896 		} else {
    897 			unlink(file);
    898 			size = osb.st_size;
    899 		}
    900 		newfile = outfile;
    901 #ifndef SMALL
    902 		copymodes(outfile, &isb);
    903 #endif
    904 	} else {
    905 lose:
    906 		size = 0;
    907 		newfile = 0;
    908 	}
    909 
    910 	return (size);
    911 }
    912 
    913 /* uncompress the given file and remove the original */
    914 static off_t
    915 file_uncompress(char *file)
    916 {
    917 	struct stat isb, osb;
    918 	char buf[PATH_MAX];
    919 	char *outfile = buf, *s;
    920 	off_t size;
    921 	ssize_t len = strlen(file);
    922 	int fd;
    923 	unsigned char header1[4], name[PATH_MAX + 1];
    924 	enum filetype method;
    925 
    926 	/* gather the old name info */
    927 
    928 	fd = open(file, O_RDONLY);
    929 	if (fd < 0)
    930 		maybe_err(1, "can't open %s", file);
    931 	if (read(fd, header1, sizeof header1) != sizeof header1) {
    932 		/* we don't want to fail here. */
    933 #ifndef SMALL
    934 		if (fflag)
    935 			goto close_it;
    936 #endif
    937 		maybe_err(1, "can't read %s", file);
    938 	}
    939 
    940 	method = file_gettype(header1);
    941 
    942 #ifndef SMALL
    943 	if (Sflag == NULL) {
    944 # ifndef NO_BZIP2_SUPPORT
    945 		if (method == FT_BZIP2)
    946 			suffix = BZ2_SUFFIX;
    947 		else
    948 # endif
    949 # ifndef NO_COMPRESS_SUPPORT
    950 		if (method == FT_Z)
    951 			suffix = Z_SUFFIX;
    952 # endif
    953 	}
    954 
    955 	if (fflag == 0 && method == FT_UNKNOWN)
    956 		maybe_errx(1, "%s: not in gzip format", file);
    957 #endif
    958 
    959 	if (cflag == 0 || lflag) {
    960 		s = &file[len - suffix_len + 1];
    961 		if (strncmp(s, suffix, suffix_len) == 0) {
    962 			(void)strncpy(outfile, file, len - suffix_len + 1);
    963 			outfile[len - suffix_len + 1] = '\0';
    964 		} else if (lflag == 0)
    965 			maybe_errx(1, "unknown suffix %s", s);
    966 	}
    967 
    968 #ifdef SMALL
    969 	if (method == FT_GZIP && lflag)
    970 #else
    971 	if (method == FT_GZIP && (Nflag || lflag))
    972 #endif
    973 	{
    974 		if (header1[3] & ORIG_NAME) {
    975 			size_t rbytes;
    976 			int i;
    977 
    978 			rbytes = read(fd, name, PATH_MAX + 1);
    979 			if (rbytes < 0)
    980 				maybe_err(1, "can't read %s", file);
    981 			for (i = 0; i < rbytes && name[i]; i++)
    982 				;
    983 			if (i < rbytes) {
    984 				name[i] = 0;
    985 				/* now maybe merge old dirname */
    986 				if (strchr(outfile, '/') == 0)
    987 					outfile = name;
    988 				else {
    989 					char *dir = dirname(outfile);
    990 					if (asprintf(&outfile, "%s/%s", dir,
    991 					    name) == -1)
    992 						maybe_err(1, "malloc");
    993 				}
    994 			}
    995 		}
    996 	}
    997 #ifndef SMALL
    998 close_it:
    999 #endif
   1000 	close(fd);
   1001 
   1002 	if (cflag == 0 || lflag) {
   1003 #ifndef SMALL
   1004 		if (fflag == 0 && lflag == 0 && stat(outfile, &osb) == 0) {
   1005 			maybe_warnx("%s already exists -- skipping", outfile);
   1006 			goto lose;
   1007 		}
   1008 #endif
   1009 		if (stat(file, &isb) == 0) {
   1010 #ifndef SMALL
   1011 			if (isb.st_nlink > 1 && lflag == 0 && fflag == 0) {
   1012 				maybe_warnx("%s has %d other links -- skipping",
   1013 				    file, isb.st_nlink - 1);
   1014 				goto lose;
   1015 			}
   1016 #endif
   1017 		} else
   1018 			goto lose;
   1019 	}
   1020 
   1021 #ifndef NO_BZIP2_SUPPORT
   1022 	if (method == FT_BZIP2) {
   1023 		int in, out;
   1024 
   1025 		/* XXX */
   1026 		if (lflag)
   1027 			maybe_errx(1, "no -l with bzip2 files");
   1028 
   1029 		if ((in = open(file, O_RDONLY)) == -1)
   1030 			maybe_err(1, "open for read: %s", file);
   1031 		if (cflag == 1)
   1032 			out = STDOUT_FILENO;
   1033 		else
   1034 			out = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0600);
   1035 		if (out == -1)
   1036 			maybe_err(1, "open for write: %s", outfile);
   1037 
   1038 		if ((size = unbzip2(in, out, NULL, 0, NULL)) == 0) {
   1039 			if (cflag == 0)
   1040 				unlink(outfile);
   1041 			goto lose;
   1042 		}
   1043 	} else
   1044 #endif
   1045 
   1046 #ifndef NO_COMPRESS_SUPPORT
   1047 	if (method == FT_Z) {
   1048 		FILE *in, *out;
   1049 		int fd;
   1050 
   1051 		/* XXX */
   1052 		if (lflag)
   1053 			maybe_errx(1, "no -l with Lempel-Ziv files");
   1054 
   1055 		if ((in = zopen(file, NULL)) == NULL)
   1056 			maybe_err(1, "open for read: %s", file);
   1057 
   1058 		if (cflag == 1)
   1059 			fd = STDOUT_FILENO;
   1060 		else {
   1061 			fd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0600);
   1062 			if (fd == -1)
   1063 				maybe_err(1, "open for write: %s", outfile);
   1064 		}
   1065 		out = fdopen(fd, "w");
   1066 		if (out == NULL)
   1067 			maybe_err(1, "open for write: %s", outfile);
   1068 
   1069 		size = zuncompress(in, out, NULL, 0, NULL);
   1070 		if (cflag == 0) {
   1071 			if (size == 0) {
   1072 				unlink(outfile);
   1073 				goto lose;
   1074 			}
   1075 			if (ferror(in) || fclose(in)) {
   1076 				unlink(outfile);
   1077 				maybe_err(1, "failed infile fclose");
   1078 			}
   1079 			if (fclose(out)) {
   1080 				unlink(outfile);
   1081 				maybe_err(1, "failed outfile close");
   1082 			}
   1083 		}
   1084 	} else
   1085 #endif
   1086 	{
   1087 		int fd, in;
   1088 
   1089 		if (lflag) {
   1090 			if ((fd = open(file, O_RDONLY)) == -1)
   1091 				maybe_err(1, "open");
   1092 			print_list(fd, isb.st_size, outfile, isb.st_mtime);
   1093 			return 0;	/* XXX */
   1094 		}
   1095 
   1096 		in = open(file, O_RDONLY);
   1097 		if (in == -1)
   1098 			maybe_err(1, "can't open %s", file);
   1099 
   1100 		if (cflag == 0) {
   1101 			/* Use open(2) directly to get a safe file.  */
   1102 			fd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0600);
   1103 			if (fd < 0)
   1104 				maybe_err(1, "can't open %s", outfile);
   1105 		} else
   1106 			fd = STDOUT_FILENO;
   1107 
   1108 		size = gz_uncompress(in, fd, NULL, 0, NULL);
   1109 		if (cflag == 0) {
   1110 			if (size == -1) {
   1111 				unlink(outfile);
   1112 				goto lose;
   1113 			}
   1114 			if (close(fd))
   1115 				maybe_err(1, "failed close");
   1116 		}
   1117 	}
   1118 
   1119 	/* if testing, or we uncompressed to stdout, this is all we need */
   1120 #ifndef SMALL
   1121 	if (tflag)
   1122 		return (size);
   1123 #endif
   1124 	if (cflag)
   1125 		return (size);
   1126 
   1127 	/*
   1128 	 * if we create a file...
   1129 	 */
   1130 	if (cflag == 0) {
   1131 		/*
   1132 		 * if we can't stat the file, or we are uncompressing to
   1133 		 * stdin, don't remove the file.
   1134 		 */
   1135 		if (stat(outfile, &osb) < 0) {
   1136 			maybe_warn("couldn't stat (leaving original): %s",
   1137 				   outfile);
   1138 			goto lose;
   1139 		}
   1140 		if (osb.st_size != size) {
   1141 			maybe_warn("stat gave different size: %llu != %llu "
   1142 			    "(leaving original)",
   1143 			    (unsigned long long)size,
   1144 			    (unsigned long long)osb.st_size);
   1145 			goto lose;
   1146 		}
   1147 		newfile = outfile;
   1148 		if (cflag == 0)
   1149 			unlink(file);
   1150 		size = osb.st_size;
   1151 #ifndef SMALL
   1152 		copymodes(outfile, &isb);
   1153 #endif
   1154 	}
   1155 	return (size);
   1156 
   1157 lose:
   1158 	newfile = 0;
   1159 	return 0;
   1160 }
   1161 
   1162 #ifndef SMALL
   1163 static off_t
   1164 cat_stdin(unsigned char * prepend, size_t count, off_t *gsizep)
   1165 {
   1166 	char buf[BUFLEN];
   1167 	size_t rv;
   1168 	off_t in_tot;
   1169 
   1170 	in_tot = count;
   1171 	if (write(STDOUT_FILENO, prepend, count) != count)
   1172 		maybe_err(1, "write to stdout");
   1173 	for (;;) {
   1174 		rv = read(STDIN_FILENO, buf, sizeof buf);
   1175 
   1176 		if (write(STDOUT_FILENO, buf, rv) != rv)
   1177 			maybe_err(1, "write to stdout");
   1178 		in_tot += rv;
   1179 	}
   1180 
   1181 	if (gsizep)
   1182 		*gsizep = in_tot;
   1183 	return (in_tot);
   1184 }
   1185 #endif
   1186 
   1187 static void
   1188 handle_stdin(void)
   1189 {
   1190 	unsigned char header1[4];
   1191 	off_t usize, gsize;
   1192 	enum filetype method;
   1193 #ifndef NO_COMPRESS_SUPPORT
   1194 	FILE *in;
   1195 #endif
   1196 
   1197 #ifndef SMALL
   1198 	if (fflag == 0 && lflag == 0 && isatty(STDIN_FILENO)) {
   1199 		maybe_warnx("standard input is a terminal -- ignoring");
   1200 		return;
   1201 	}
   1202 #endif
   1203 
   1204 	if (lflag) {
   1205 		struct stat isb;
   1206 
   1207 		/* XXX could read the whole file, etc. */
   1208 		if (fstat(STDIN_FILENO, &isb) < 0)
   1209 			maybe_err(1, "fstat");
   1210 		print_list(STDIN_FILENO, isb.st_size, "stdout", isb.st_mtime);
   1211 		return;
   1212 	}
   1213 
   1214 	if (read(STDIN_FILENO, header1, sizeof header1) != sizeof header1)
   1215 		maybe_err(1, "can't read stdin");
   1216 
   1217 	method = file_gettype(header1);
   1218 	switch (method) {
   1219 	default:
   1220 #ifndef SMALL
   1221 		if (fflag == 0)
   1222 			maybe_errx(1, "unknown compression format");
   1223 		usize = cat_stdin(header1, sizeof header1, &gsize);
   1224 		break;
   1225 #endif
   1226 	case FT_GZIP:
   1227 		usize = gz_uncompress(STDIN_FILENO, STDOUT_FILENO,
   1228 			      header1, sizeof header1, &gsize);
   1229 		break;
   1230 #ifndef NO_BZIP2_SUPPORT
   1231 	case FT_BZIP2:
   1232 		usize = unbzip2(STDIN_FILENO, STDOUT_FILENO,
   1233 				header1, sizeof header1, &gsize);
   1234 		break;
   1235 #endif
   1236 #ifndef NO_COMPRESS_SUPPORT
   1237 	case FT_Z:
   1238 		if ((in = zopen(NULL, stdin)) == NULL)
   1239 			maybe_err(1, "zopen of stdin");
   1240 
   1241 		usize = zuncompress(in, stdout, header1, sizeof header1, &gsize);
   1242 		break;
   1243 #endif
   1244 	}
   1245 
   1246 #ifndef SMALL
   1247         if (vflag && !tflag && usize != -1 && gsize != -1)
   1248 		print_verbage(NULL, 0, usize, gsize);
   1249 #endif
   1250 
   1251 }
   1252 
   1253 static void
   1254 handle_stdout(void)
   1255 {
   1256 	off_t gsize, usize;
   1257 
   1258 #ifndef SMALL
   1259 	if (fflag == 0 && isatty(STDOUT_FILENO)) {
   1260 		maybe_warnx("standard output is a terminal -- ignoring");
   1261 		return;
   1262 	}
   1263 #endif
   1264 	usize = gz_compress(stdin, STDOUT_FILENO, &gsize, NULL, 0);
   1265 
   1266 #ifndef SMALL
   1267         if (vflag && !tflag && usize != -1 && gsize != -1)
   1268 		print_verbage(NULL, 0, usize, gsize);
   1269 #endif
   1270 }
   1271 
   1272 /* do what is asked for, for the path name */
   1273 static void
   1274 handle_pathname(char *path)
   1275 {
   1276 	char *opath = path, *s = 0;
   1277 	ssize_t len;
   1278 	struct stat sb;
   1279 
   1280 	/* check for stdout/stdin */
   1281 	if (path[0] == '-' && path[1] == '\0') {
   1282 		if (dflag)
   1283 			handle_stdin();
   1284 		else
   1285 			handle_stdout();
   1286 	}
   1287 
   1288 retry:
   1289 	if (stat(path, &sb) < 0) {
   1290 		/* lets try <path>.gz if we're decompressing */
   1291 		if (dflag && s == 0 && errno == ENOENT) {
   1292 			len = strlen(path);
   1293 			s = malloc(len + suffix_len);
   1294 			if (s == 0)
   1295 				maybe_err(1, "malloc");
   1296 			memmove(s, path, len);
   1297 			memmove(&s[len], suffix, suffix_len);
   1298 			path = s;
   1299 			goto retry;
   1300 		}
   1301 		maybe_warn("can't stat: %s", opath);
   1302 		goto out;
   1303 	}
   1304 
   1305 	if (S_ISDIR(sb.st_mode)) {
   1306 #ifndef SMALL
   1307 		if (rflag)
   1308 			handle_dir(path, &sb);
   1309 		else
   1310 #endif
   1311 			maybe_warn("%s is a directory", path);
   1312 		goto out;
   1313 	}
   1314 
   1315 	if (S_ISREG(sb.st_mode))
   1316 		handle_file(path, &sb);
   1317 
   1318 out:
   1319 	if (s)
   1320 		free(s);
   1321 }
   1322 
   1323 /* compress/decompress a file */
   1324 static void
   1325 handle_file(char *file, struct stat *sbp)
   1326 {
   1327 	off_t usize, gsize;
   1328 
   1329 	infile = file;
   1330 	if (dflag) {
   1331 		usize = file_uncompress(file);
   1332 		if (usize == 0)
   1333 			return;
   1334 		gsize = sbp->st_size;
   1335 	} else {
   1336 		gsize = file_compress(file);
   1337 		if (gsize == 0)
   1338 			return;
   1339 		usize = sbp->st_size;
   1340 	}
   1341 
   1342 
   1343 #ifndef SMALL
   1344 	if (vflag && !tflag)
   1345 		print_verbage(file, cflag == 0 ? newfile : 0, usize, gsize);
   1346 #endif
   1347 }
   1348 
   1349 #ifndef SMALL
   1350 /* this is used with -r to recursively decend directories */
   1351 static void
   1352 handle_dir(char *dir, struct stat *sbp)
   1353 {
   1354 	char *path_argv[2];
   1355 	FTS *fts;
   1356 	FTSENT *entry;
   1357 
   1358 	path_argv[0] = dir;
   1359 	path_argv[1] = 0;
   1360 	fts = fts_open(path_argv, FTS_PHYSICAL, NULL);
   1361 	if (fts == NULL) {
   1362 		warn("couldn't fts_open %s", dir);
   1363 		return;
   1364 	}
   1365 
   1366 	while ((entry = fts_read(fts))) {
   1367 		switch(entry->fts_info) {
   1368 		case FTS_D:
   1369 		case FTS_DP:
   1370 			continue;
   1371 
   1372 		case FTS_DNR:
   1373 		case FTS_ERR:
   1374 		case FTS_NS:
   1375 			maybe_warn("%s", entry->fts_path);
   1376 			continue;
   1377 		case FTS_F:
   1378 			handle_file(entry->fts_name, entry->fts_statp);
   1379 		}
   1380 	}
   1381 	(void)fts_close(fts);
   1382 }
   1383 #endif
   1384 
   1385 /* print a ratio */
   1386 static void
   1387 print_ratio(off_t in, off_t out, FILE *where)
   1388 {
   1389 	int64_t percent10;	/* 10 * percent */
   1390 	off_t diff = in - out;
   1391 	char ch;
   1392 
   1393 	if (in == 0)
   1394 		percent10 = 0;
   1395 	else if (diff > 0x400000) 	/* anything with 22 or more bits */
   1396 		percent10 = diff / (in / 1000);
   1397 	else
   1398 		percent10 = (1000 * diff) / in;
   1399 
   1400 	if (percent10 < 0) {
   1401 		percent10 = -percent10;
   1402 		ch = '-';
   1403 	} else
   1404 		ch = ' ';
   1405 
   1406 	/*
   1407 	 * ugh.  for negative percentages < 10, we need to avoid printing a
   1408 	 * a space between the "-" and the single number.
   1409 	 */
   1410 	if (ch == '-' && percent10 / 10LL < 10)
   1411 		fprintf(where, " -%1d.%1u%%", (unsigned)(percent10 / 10LL),
   1412 					      (unsigned)(percent10 % 10LL));
   1413 	else
   1414 		fprintf(where, "%c%2d.%1u%%", ch, (unsigned)(percent10 / 10LL),
   1415 					          (unsigned)(percent10 % 10LL));
   1416 }
   1417 
   1418 #ifndef SMALL
   1419 /* print compression statistics, and the new name (if there is one!) */
   1420 static void
   1421 print_verbage(char *file, char *nfile, off_t usize, off_t gsize)
   1422 {
   1423 	if (file)
   1424 		fprintf(stderr, "%s:%s  ", file,
   1425 		    strlen(file) < 7 ? "\t\t" : "\t");
   1426 	print_ratio((off_t)usize, (off_t)gsize, stderr);
   1427 	if (nfile)
   1428 		fprintf(stderr, " -- replaced with %s", nfile);
   1429 	fprintf(stderr, "\n");
   1430 	fflush(stderr);
   1431 }
   1432 
   1433 /* print test results */
   1434 static void
   1435 print_test(char *file, int ok)
   1436 {
   1437 
   1438 	fprintf(stderr, "%s:%s  %s\n", file,
   1439 	    strlen(file) < 7 ? "\t\t" : "\t", ok ? "OK" : "NOT OK");
   1440 	fflush(stderr);
   1441 }
   1442 #endif
   1443 
   1444 /* print a file's info ala --list */
   1445 /* eg:
   1446   compressed uncompressed  ratio uncompressed_name
   1447       354841      1679360  78.8% /usr/pkgsrc/distfiles/libglade-2.0.1.tar
   1448 */
   1449 static void
   1450 print_list(int fd, off_t out, const char *outfile, time_t ts)
   1451 {
   1452 	static int first = 1;
   1453 #ifndef SMALL
   1454 	static off_t in_tot, out_tot;
   1455 	u_int32_t crc;
   1456 #endif
   1457 	off_t in;
   1458 	int rv;
   1459 
   1460 	if (first) {
   1461 #ifndef SMALL
   1462 		if (vflag)
   1463 			printf("method  crc     date  time  ");
   1464 #endif
   1465 		if (qflag == 0)
   1466 			printf("  compressed uncompressed  "
   1467 			       "ratio uncompressed_name\n");
   1468 	}
   1469 	first = 0;
   1470 
   1471 	/* print totals? */
   1472 #ifndef SMALL
   1473 	if (fd == -1) {
   1474 		in = in_tot;
   1475 		out = out_tot;
   1476 	} else
   1477 #endif
   1478 	{
   1479 		/* read the last 4 bytes - this is the uncompressed size */
   1480 		rv = lseek(fd, (off_t)(-8), SEEK_END);
   1481 		if (rv != -1) {
   1482 			unsigned char buf[8];
   1483 			u_int32_t usize;
   1484 
   1485 			if (read(fd, (char *)buf, sizeof(buf)) != sizeof(buf))
   1486 				maybe_warn("read of uncompressed size");
   1487 			usize = buf[4] | buf[5] << 8 | buf[6] << 16 | buf[7] << 24;
   1488 			in = (off_t)usize;
   1489 #ifndef SMALL
   1490 			crc = buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;
   1491 #endif
   1492 		}
   1493 	}
   1494 
   1495 #ifndef SMALL
   1496 	if (vflag && fd == -1)
   1497 		printf("                            ");
   1498 	else if (vflag) {
   1499 		char *date = ctime(&ts);
   1500 
   1501 		/* skip the day, 1/100th second, and year */
   1502 		date += 4;
   1503 		date[12] = 0;
   1504 		printf("%5s %08x %11s ", "defla"/*XXX*/, crc, date);
   1505 	}
   1506 	in_tot += in;
   1507 	out_tot += out;
   1508 #endif
   1509 	printf("%12llu %12llu ", (unsigned long long)out, (unsigned long long)in);
   1510 	print_ratio(in, out, stdout);
   1511 	printf(" %s\n", outfile);
   1512 }
   1513 
   1514 /* display the usage of NetBSD gzip */
   1515 static void
   1516 usage(void)
   1517 {
   1518 
   1519 	fprintf(stderr, "%s\n", gzip_version);
   1520 	fprintf(stderr,
   1521     "usage: %s [-" OPT_LIST "] [<file> [<file> ...]]\n"
   1522 #ifndef SMALL
   1523     " -c --stdout          write to stdout, keep original files\n"
   1524     "    --to-stdout\n"
   1525     " -d --decompress      uncompress files\n"
   1526     "    --uncompress\n"
   1527     " -f --force           force overwriting & compress links\n"
   1528     " -h --help            display this help\n"
   1529     " -n --no-name         don't save original file name or time stamp\n"
   1530     " -N --name            save or restore original file name and time stamp\n"
   1531     " -q --quiet           output no warnings\n"
   1532     " -r --recursive       recursively compress files in directories\n"
   1533     " -S .suf              use suffix .suf instead of .gz\n"
   1534     "    --suffix .suf\n"
   1535     " -t --test            test compressed file\n"
   1536     " -v --verbose         print extra statistics\n"
   1537     " -V --version         display program version\n"
   1538     " -1 --fast            fastest (worst) compression\n"
   1539     " -2 .. -8             set compression level\n"
   1540     " -9 --best            best (slowest) compression\n",
   1541 #else
   1542     ,
   1543 #endif
   1544 	    getprogname());
   1545 	exit(0);
   1546 }
   1547 
   1548 /* display the version of NetBSD gzip */
   1549 static void
   1550 display_version(void)
   1551 {
   1552 
   1553 	fprintf(stderr, "%s\n", gzip_version);
   1554 	exit(0);
   1555 }
   1556 
   1557 #ifndef NO_BZIP2_SUPPORT
   1558 #include "unbzip2.c"
   1559 #endif
   1560 #ifndef NO_COMPRESS_SUPPORT
   1561 #include "zuncompress.c"
   1562 #endif
   1563