Home | History | Annotate | Line # | Download | only in gzip
gzip.c revision 1.39
      1 /*	$NetBSD: gzip.c,v 1.39 2004/04/27 01:23:35 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.39 2004/04/27 01:23:35 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(out, outbuf, wr) != wr)
    717 					maybe_err(1, "error writing "
    718 						     "to output\n");
    719 
    720 				out_tot += wr;
    721 
    722 				if (error == Z_STREAM_END)
    723 					goto stop;
    724 
    725 				z.next_out = outbuf;
    726 				z.avail_out = BUFLEN;
    727 
    728 				break;
    729 			}
    730 			if (error < 0) {
    731 				maybe_warnx("decompression error\n");
    732 				out_tot = -1;
    733 				goto stop;
    734 			}
    735 			break;
    736 		}
    737 		continue;
    738 stop:
    739 		break;
    740 	}
    741 	if (state > GZSTATE_INIT)
    742 		inflateEnd(&z);
    743 
    744 #ifndef SMALL
    745 	if (tflag) {
    746 		print_test("(stdin)", 1);
    747 		return 0;
    748 	}
    749 #endif
    750 
    751 	if (gsizep)
    752 		*gsizep = in_tot;
    753 	return (out_tot);
    754 }
    755 
    756 #ifndef SMALL
    757 /*
    758  * set the owner, mode, flags & utimes for a file
    759  */
    760 static void
    761 copymodes(const char *file, struct stat *sbp)
    762 {
    763 	struct timeval times[2];
    764 
    765 	/*
    766 	 * If we have no info on the input, give this file some
    767 	 * default values and return..
    768 	 */
    769 	if (sbp == NULL) {
    770 		mode_t mask = umask(022);
    771 
    772 		(void)chmod(file, DEFFILEMODE & ~mask);
    773 		(void)umask(mask);
    774 		return;
    775 	}
    776 
    777 	/* if the chown fails, remove set-id bits as-per compress(1) */
    778 	if (chown(file, sbp->st_uid, sbp->st_gid) < 0) {
    779 		if (errno != EPERM)
    780 			maybe_warn("couldn't chown: %s", file);
    781 		sbp->st_mode &= ~(S_ISUID|S_ISGID);
    782 	}
    783 
    784 	/* we only allow set-id and the 9 normal permission bits */
    785 	sbp->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
    786 	if (chmod(file, sbp->st_mode) < 0)
    787 		maybe_warn("couldn't chmod: %s", file);
    788 
    789 	/* only try flags if they exist already */
    790         if (sbp->st_flags != 0 && chflags(file, sbp->st_flags) < 0)
    791 		maybe_warn("couldn't chflags: %s", file);
    792 
    793 	TIMESPEC_TO_TIMEVAL(&times[0], &sbp->st_atimespec);
    794 	TIMESPEC_TO_TIMEVAL(&times[1], &sbp->st_mtimespec);
    795 	if (utimes(file, times) < 0)
    796 		maybe_warn("couldn't utimes: %s", file);
    797 }
    798 #endif
    799 
    800 /* what sort of file is this? */
    801 static enum filetype
    802 file_gettype(u_char *buf)
    803 {
    804 
    805 	if (buf[0] == GZIP_MAGIC0 &&
    806 	    (buf[1] == GZIP_MAGIC1 || buf[1] == GZIP_OMAGIC1))
    807 		return FT_GZIP;
    808 	else
    809 #ifndef NO_BZIP2_SUPPORT
    810 	if (memcmp(buf, BZIP2_MAGIC, 3) == 0 &&
    811 	    buf[3] >= '0' && buf[3] <= '9')
    812 		return FT_BZIP2;
    813 	else
    814 #endif
    815 #ifndef NO_COMPRESS_SUPPORT
    816 	if (memcmp(buf, Z_MAGIC, 2) == 0)
    817 		return FT_Z;
    818 	else
    819 #endif
    820 		return FT_UNKNOWN;
    821 }
    822 
    823 /*
    824  * compress the given file: create a corresponding .gz file and remove the
    825  * original.
    826  */
    827 static off_t
    828 file_compress(char *file)
    829 {
    830 	FILE *in;
    831 	int out;
    832 	struct stat isb, osb;
    833 	char outfile[MAXPATHLEN];
    834 	off_t size;
    835 #ifndef SMALL
    836 	u_int32_t mtime = 0;
    837 	char *savename;
    838 #endif
    839 
    840 	if (cflag == 0) {
    841 		(void)strncpy(outfile, file, MAXPATHLEN - suffix_len);
    842 		outfile[MAXPATHLEN - suffix_len] = '\0';
    843 		(void)strlcat(outfile, suffix, sizeof(outfile));
    844 
    845 #ifndef SMALL
    846 		if (fflag == 0) {
    847 			if (stat(outfile, &osb) == 0) {
    848 				maybe_warnx("%s already exists -- skipping",
    849 					      outfile);
    850 				goto lose;
    851 			}
    852 		}
    853 		if (stat(file, &isb) == 0) {
    854 			if (isb.st_nlink > 1 && fflag == 0) {
    855 				maybe_warnx("%s has %d other link%s -- "
    856 					    "skipping", file, isb.st_nlink - 1,
    857 					    isb.st_nlink == 1 ? "" : "s");
    858 				goto lose;
    859 			}
    860 			if (nflag == 0)
    861 				mtime = (u_int32_t)isb.st_mtime;
    862 		}
    863 #endif
    864 	}
    865 	in = fopen(file, "r");
    866 	if (in == 0)
    867 		maybe_err(1, "can't fopen %s", file);
    868 
    869 	if (cflag == 0) {
    870 #ifndef SMALL
    871 		if (nflag == 0)
    872 			savename = basename(file);
    873 		else
    874 			savename = NULL;
    875 #endif
    876 		out = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0600);
    877 		if (out == -1) {
    878 			maybe_warn("could not create output: %s", outfile);
    879 			goto lose;
    880 		}
    881 	} else
    882 		out = STDOUT_FILENO;
    883 
    884 #ifdef SMALL
    885 	gz_compress(in, out, NULL, NULL, 0);
    886 #else
    887 	gz_compress(in, out, NULL, savename, mtime);
    888 #endif
    889 
    890 	/*
    891 	 * if we compressed to stdout, we don't know the size and
    892 	 * we don't know the new file name, punt.  if we can't stat
    893 	 * the file, whine, otherwise set the size from the stat
    894 	 * buffer.  we only blow away the file if we can stat the
    895 	 * output, just in case.
    896 	 */
    897 	if (cflag == 0) {
    898 		if (close(out) == -1)
    899 			maybe_warn("couldn't close ouput");
    900 
    901 		if (stat(outfile, &osb) < 0) {
    902 			maybe_warn("couldn't stat: %s", outfile);
    903 			maybe_warnx("leaving original %s", file);
    904 			size = 0;
    905 		} else {
    906 			unlink(file);
    907 			size = osb.st_size;
    908 		}
    909 		newfile = outfile;
    910 #ifndef SMALL
    911 		copymodes(outfile, &isb);
    912 #endif
    913 	} else {
    914 lose:
    915 		size = 0;
    916 		newfile = 0;
    917 	}
    918 
    919 	return (size);
    920 }
    921 
    922 /* uncompress the given file and remove the original */
    923 static off_t
    924 file_uncompress(char *file)
    925 {
    926 	struct stat isb, osb;
    927 	char buf[PATH_MAX];
    928 	char *outfile = buf, *s;
    929 	off_t size;
    930 	ssize_t len = strlen(file);
    931 	int fd;
    932 	unsigned char header1[4], name[PATH_MAX + 1];
    933 	enum filetype method;
    934 
    935 	/* gather the old name info */
    936 
    937 	fd = open(file, O_RDONLY);
    938 	if (fd < 0)
    939 		maybe_err(1, "can't open %s", file);
    940 	if (read(fd, header1, sizeof header1) != sizeof header1) {
    941 		/* we don't want to fail here. */
    942 #ifndef SMALL
    943 		if (fflag)
    944 			goto close_it;
    945 #endif
    946 		maybe_err(1, "can't read %s", file);
    947 	}
    948 
    949 	method = file_gettype(header1);
    950 
    951 #ifndef SMALL
    952 	if (Sflag == NULL) {
    953 # ifndef NO_BZIP2_SUPPORT
    954 		if (method == FT_BZIP2)
    955 			suffix = BZ2_SUFFIX;
    956 		else
    957 # endif
    958 # ifndef NO_COMPRESS_SUPPORT
    959 		if (method == FT_Z)
    960 			suffix = Z_SUFFIX;
    961 # endif
    962 	}
    963 
    964 	if (fflag == 0 && method == FT_UNKNOWN)
    965 		maybe_errx(1, "%s: not in gzip format", file);
    966 #endif
    967 
    968 	if (cflag == 0 || lflag) {
    969 		s = &file[len - suffix_len + 1];
    970 		if (strncmp(s, suffix, suffix_len) == 0) {
    971 			(void)strncpy(outfile, file, len - suffix_len + 1);
    972 			outfile[len - suffix_len + 1] = '\0';
    973 		} else if (lflag == 0)
    974 			maybe_errx(1, "unknown suffix %s", s);
    975 	}
    976 
    977 #ifdef SMALL
    978 	if (method == FT_GZIP && lflag)
    979 #else
    980 	if (method == FT_GZIP && (Nflag || lflag))
    981 #endif
    982 	{
    983 		if (header1[3] & ORIG_NAME) {
    984 			size_t rbytes;
    985 			int i;
    986 
    987 			rbytes = read(fd, name, PATH_MAX + 1);
    988 			if (rbytes < 0)
    989 				maybe_err(1, "can't read %s", file);
    990 			for (i = 0; i < rbytes && name[i]; i++)
    991 				;
    992 			if (i < rbytes) {
    993 				name[i] = 0;
    994 				/* now maybe merge old dirname */
    995 				if (strchr(outfile, '/') == 0)
    996 					outfile = name;
    997 				else {
    998 					char *dir = dirname(outfile);
    999 					if (asprintf(&outfile, "%s/%s", dir,
   1000 					    name) == -1)
   1001 						maybe_err(1, "malloc");
   1002 				}
   1003 			}
   1004 		}
   1005 	}
   1006 #ifndef SMALL
   1007 close_it:
   1008 #endif
   1009 	close(fd);
   1010 
   1011 	if (cflag == 0 || lflag) {
   1012 #ifndef SMALL
   1013 		if (fflag == 0 && lflag == 0 && stat(outfile, &osb) == 0) {
   1014 			maybe_warnx("%s already exists -- skipping", outfile);
   1015 			goto lose;
   1016 		}
   1017 #endif
   1018 		if (stat(file, &isb) == 0) {
   1019 #ifndef SMALL
   1020 			if (isb.st_nlink > 1 && lflag == 0 && fflag == 0) {
   1021 				maybe_warnx("%s has %d other links -- skipping",
   1022 				    file, isb.st_nlink - 1);
   1023 				goto lose;
   1024 			}
   1025 #endif
   1026 		} else
   1027 			goto lose;
   1028 	}
   1029 
   1030 #ifndef NO_BZIP2_SUPPORT
   1031 	if (method == FT_BZIP2) {
   1032 		int in, out;
   1033 
   1034 		/* XXX */
   1035 		if (lflag)
   1036 			maybe_errx(1, "no -l with bzip2 files");
   1037 
   1038 		if ((in = open(file, O_RDONLY)) == -1)
   1039 			maybe_err(1, "open for read: %s", file);
   1040 		if (cflag == 1)
   1041 			out = STDOUT_FILENO;
   1042 		else
   1043 			out = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0600);
   1044 		if (out == -1)
   1045 			maybe_err(1, "open for write: %s", outfile);
   1046 
   1047 		if ((size = unbzip2(in, out, NULL, 0, NULL)) == 0) {
   1048 			if (cflag == 0)
   1049 				unlink(outfile);
   1050 			goto lose;
   1051 		}
   1052 	} else
   1053 #endif
   1054 
   1055 #ifndef NO_COMPRESS_SUPPORT
   1056 	if (method == FT_Z) {
   1057 		FILE *in, *out;
   1058 		int fd;
   1059 
   1060 		/* XXX */
   1061 		if (lflag)
   1062 			maybe_errx(1, "no -l with Lempel-Ziv files");
   1063 
   1064 		if ((in = zopen(file, NULL)) == NULL)
   1065 			maybe_err(1, "open for read: %s", file);
   1066 
   1067 		if (cflag == 1)
   1068 			fd = STDOUT_FILENO;
   1069 		else {
   1070 			fd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0600);
   1071 			if (fd == -1)
   1072 				maybe_err(1, "open for write: %s", outfile);
   1073 		}
   1074 		out = fdopen(fd, "w");
   1075 		if (out == NULL)
   1076 			maybe_err(1, "open for write: %s", outfile);
   1077 
   1078 		size = zuncompress(in, out, NULL, 0, NULL);
   1079 		if (cflag == 0) {
   1080 			if (size == 0) {
   1081 				unlink(outfile);
   1082 				goto lose;
   1083 			}
   1084 			if (ferror(in) || fclose(in)) {
   1085 				unlink(outfile);
   1086 				maybe_err(1, "failed infile fclose");
   1087 			}
   1088 			if (fclose(out)) {
   1089 				unlink(outfile);
   1090 				maybe_err(1, "failed outfile close");
   1091 			}
   1092 		}
   1093 	} else
   1094 #endif
   1095 	{
   1096 		int fd, in;
   1097 
   1098 		if (lflag) {
   1099 			if ((fd = open(file, O_RDONLY)) == -1)
   1100 				maybe_err(1, "open");
   1101 			print_list(fd, isb.st_size, outfile, isb.st_mtime);
   1102 			return 0;	/* XXX */
   1103 		}
   1104 
   1105 		in = open(file, O_RDONLY);
   1106 		if (in == -1)
   1107 			maybe_err(1, "can't open %s", file);
   1108 
   1109 		if (cflag == 0) {
   1110 			/* Use open(2) directly to get a safe file.  */
   1111 			fd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0600);
   1112 			if (fd < 0)
   1113 				maybe_err(1, "can't open %s", outfile);
   1114 		} else
   1115 			fd = STDOUT_FILENO;
   1116 
   1117 		size = gz_uncompress(in, fd, NULL, 0, NULL);
   1118 		if (cflag == 0) {
   1119 			if (size == -1) {
   1120 				unlink(outfile);
   1121 				goto lose;
   1122 			}
   1123 			if (close(fd))
   1124 				maybe_err(1, "failed close");
   1125 		}
   1126 	}
   1127 
   1128 	/* if testing, or we uncompressed to stdout, this is all we need */
   1129 #ifndef SMALL
   1130 	if (tflag)
   1131 		return (size);
   1132 #endif
   1133 	if (cflag)
   1134 		return (size);
   1135 
   1136 	/*
   1137 	 * if we create a file...
   1138 	 */
   1139 	if (cflag == 0) {
   1140 		/*
   1141 		 * if we can't stat the file, or we are uncompressing to
   1142 		 * stdin, don't remove the file.
   1143 		 */
   1144 		if (stat(outfile, &osb) < 0) {
   1145 			maybe_warn("couldn't stat (leaving original): %s",
   1146 				   outfile);
   1147 			goto lose;
   1148 		}
   1149 		if (osb.st_size != size) {
   1150 			maybe_warn("stat gave different size: %llu != %llu "
   1151 			    "(leaving original)",
   1152 			    (unsigned long long)size,
   1153 			    (unsigned long long)osb.st_size);
   1154 			goto lose;
   1155 		}
   1156 		newfile = outfile;
   1157 		if (cflag == 0)
   1158 			unlink(file);
   1159 		size = osb.st_size;
   1160 #ifndef SMALL
   1161 		copymodes(outfile, &isb);
   1162 #endif
   1163 	}
   1164 	return (size);
   1165 
   1166 lose:
   1167 	newfile = 0;
   1168 	return 0;
   1169 }
   1170 
   1171 #ifndef SMALL
   1172 static off_t
   1173 cat_stdin(unsigned char * prepend, size_t count, off_t *gsizep)
   1174 {
   1175 	char buf[BUFLEN];
   1176 	size_t rv;
   1177 	off_t in_tot;
   1178 
   1179 	in_tot = count;
   1180 	if (write(STDOUT_FILENO, prepend, count) != count)
   1181 		maybe_err(1, "write to stdout");
   1182 	for (;;) {
   1183 		rv = read(STDIN_FILENO, buf, sizeof buf);
   1184 
   1185 		if (write(STDOUT_FILENO, buf, rv) != rv)
   1186 			maybe_err(1, "write to stdout");
   1187 		in_tot += rv;
   1188 	}
   1189 
   1190 	if (gsizep)
   1191 		*gsizep = in_tot;
   1192 	return (in_tot);
   1193 }
   1194 #endif
   1195 
   1196 static void
   1197 handle_stdin(void)
   1198 {
   1199 	unsigned char header1[4];
   1200 	off_t usize, gsize;
   1201 	enum filetype method;
   1202 #ifndef NO_COMPRESS_SUPPORT
   1203 	FILE *in;
   1204 #endif
   1205 
   1206 #ifndef SMALL
   1207 	if (fflag == 0 && lflag == 0 && isatty(STDIN_FILENO)) {
   1208 		maybe_warnx("standard input is a terminal -- ignoring");
   1209 		return;
   1210 	}
   1211 #endif
   1212 
   1213 	if (lflag) {
   1214 		struct stat isb;
   1215 
   1216 		/* XXX could read the whole file, etc. */
   1217 		if (fstat(STDIN_FILENO, &isb) < 0)
   1218 			maybe_err(1, "fstat");
   1219 		print_list(STDIN_FILENO, isb.st_size, "stdout", isb.st_mtime);
   1220 		return;
   1221 	}
   1222 
   1223 	if (read(STDIN_FILENO, header1, sizeof header1) != sizeof header1)
   1224 		maybe_err(1, "can't read stdin");
   1225 
   1226 	method = file_gettype(header1);
   1227 	switch (method) {
   1228 	default:
   1229 #ifndef SMALL
   1230 		if (fflag == 0)
   1231 			maybe_errx(1, "unknown compression format");
   1232 		usize = cat_stdin(header1, sizeof header1, &gsize);
   1233 		break;
   1234 #endif
   1235 	case FT_GZIP:
   1236 		usize = gz_uncompress(STDIN_FILENO, STDOUT_FILENO,
   1237 			      header1, sizeof header1, &gsize);
   1238 		break;
   1239 #ifndef NO_BZIP2_SUPPORT
   1240 	case FT_BZIP2:
   1241 		usize = unbzip2(STDIN_FILENO, STDOUT_FILENO,
   1242 				header1, sizeof header1, &gsize);
   1243 		break;
   1244 #endif
   1245 #ifndef NO_COMPRESS_SUPPORT
   1246 	case FT_Z:
   1247 		if ((in = zopen(NULL, stdin)) == NULL)
   1248 			maybe_err(1, "zopen of stdin");
   1249 
   1250 		usize = zuncompress(in, stdout, header1, sizeof header1, &gsize);
   1251 		break;
   1252 #endif
   1253 	}
   1254 
   1255 #ifndef SMALL
   1256         if (vflag && !tflag && usize != -1 && gsize != -1)
   1257 		print_verbage(NULL, 0, usize, gsize);
   1258 #endif
   1259 
   1260 }
   1261 
   1262 static void
   1263 handle_stdout(void)
   1264 {
   1265 	off_t gsize, usize;
   1266 
   1267 #ifndef SMALL
   1268 	if (fflag == 0 && isatty(STDOUT_FILENO)) {
   1269 		maybe_warnx("standard output is a terminal -- ignoring");
   1270 		return;
   1271 	}
   1272 #endif
   1273 	usize = gz_compress(stdin, STDOUT_FILENO, &gsize, NULL, 0);
   1274 
   1275 #ifndef SMALL
   1276         if (vflag && !tflag && usize != -1 && gsize != -1)
   1277 		print_verbage(NULL, 0, usize, gsize);
   1278 #endif
   1279 }
   1280 
   1281 /* do what is asked for, for the path name */
   1282 static void
   1283 handle_pathname(char *path)
   1284 {
   1285 	char *opath = path, *s = 0;
   1286 	ssize_t len;
   1287 	struct stat sb;
   1288 
   1289 	/* check for stdout/stdin */
   1290 	if (path[0] == '-' && path[1] == '\0') {
   1291 		if (dflag)
   1292 			handle_stdin();
   1293 		else
   1294 			handle_stdout();
   1295 	}
   1296 
   1297 retry:
   1298 	if (stat(path, &sb) < 0) {
   1299 		/* lets try <path>.gz if we're decompressing */
   1300 		if (dflag && s == 0 && errno == ENOENT) {
   1301 			len = strlen(path);
   1302 			s = malloc(len + suffix_len);
   1303 			if (s == 0)
   1304 				maybe_err(1, "malloc");
   1305 			memmove(s, path, len);
   1306 			memmove(&s[len], suffix, suffix_len);
   1307 			path = s;
   1308 			goto retry;
   1309 		}
   1310 		maybe_warn("can't stat: %s", opath);
   1311 		goto out;
   1312 	}
   1313 
   1314 	if (S_ISDIR(sb.st_mode)) {
   1315 #ifndef SMALL
   1316 		if (rflag)
   1317 			handle_dir(path, &sb);
   1318 		else
   1319 #endif
   1320 			maybe_warn("%s is a directory", path);
   1321 		goto out;
   1322 	}
   1323 
   1324 	if (S_ISREG(sb.st_mode))
   1325 		handle_file(path, &sb);
   1326 
   1327 out:
   1328 	if (s)
   1329 		free(s);
   1330 }
   1331 
   1332 /* compress/decompress a file */
   1333 static void
   1334 handle_file(char *file, struct stat *sbp)
   1335 {
   1336 	off_t usize, gsize;
   1337 
   1338 	infile = file;
   1339 	if (dflag) {
   1340 		usize = file_uncompress(file);
   1341 		if (usize == 0)
   1342 			return;
   1343 		gsize = sbp->st_size;
   1344 	} else {
   1345 		gsize = file_compress(file);
   1346 		if (gsize == 0)
   1347 			return;
   1348 		usize = sbp->st_size;
   1349 	}
   1350 
   1351 
   1352 #ifndef SMALL
   1353 	if (vflag && !tflag)
   1354 		print_verbage(file, cflag == 0 ? newfile : 0, usize, gsize);
   1355 #endif
   1356 }
   1357 
   1358 #ifndef SMALL
   1359 /* this is used with -r to recursively decend directories */
   1360 static void
   1361 handle_dir(char *dir, struct stat *sbp)
   1362 {
   1363 	char *path_argv[2];
   1364 	FTS *fts;
   1365 	FTSENT *entry;
   1366 
   1367 	path_argv[0] = dir;
   1368 	path_argv[1] = 0;
   1369 	fts = fts_open(path_argv, FTS_PHYSICAL, NULL);
   1370 	if (fts == NULL) {
   1371 		warn("couldn't fts_open %s", dir);
   1372 		return;
   1373 	}
   1374 
   1375 	while ((entry = fts_read(fts))) {
   1376 		switch(entry->fts_info) {
   1377 		case FTS_D:
   1378 		case FTS_DP:
   1379 			continue;
   1380 
   1381 		case FTS_DNR:
   1382 		case FTS_ERR:
   1383 		case FTS_NS:
   1384 			maybe_warn("%s", entry->fts_path);
   1385 			continue;
   1386 		case FTS_F:
   1387 			handle_file(entry->fts_name, entry->fts_statp);
   1388 		}
   1389 	}
   1390 	(void)fts_close(fts);
   1391 }
   1392 #endif
   1393 
   1394 /* print a ratio */
   1395 static void
   1396 print_ratio(off_t in, off_t out, FILE *where)
   1397 {
   1398 	int64_t percent10;	/* 10 * percent */
   1399 	off_t diff = in - out;
   1400 	char ch;
   1401 
   1402 	if (in == 0)
   1403 		percent10 = 0;
   1404 	else if (diff > 0x400000) 	/* anything with 22 or more bits */
   1405 		percent10 = diff / (in / 1000);
   1406 	else
   1407 		percent10 = (1000 * diff) / in;
   1408 
   1409 	if (percent10 < 0) {
   1410 		percent10 = -percent10;
   1411 		ch = '-';
   1412 	} else
   1413 		ch = ' ';
   1414 
   1415 	/*
   1416 	 * ugh.  for negative percentages < 10, we need to avoid printing a
   1417 	 * a space between the "-" and the single number.
   1418 	 */
   1419 	if (ch == '-' && percent10 / 10LL < 10)
   1420 		fprintf(where, " -%1d.%1u%%", (unsigned)(percent10 / 10LL),
   1421 					      (unsigned)(percent10 % 10LL));
   1422 	else
   1423 		fprintf(where, "%c%2d.%1u%%", ch, (unsigned)(percent10 / 10LL),
   1424 					          (unsigned)(percent10 % 10LL));
   1425 }
   1426 
   1427 #ifndef SMALL
   1428 /* print compression statistics, and the new name (if there is one!) */
   1429 static void
   1430 print_verbage(char *file, char *nfile, off_t usize, off_t gsize)
   1431 {
   1432 	if (file)
   1433 		fprintf(stderr, "%s:%s  ", file,
   1434 		    strlen(file) < 7 ? "\t\t" : "\t");
   1435 	print_ratio((off_t)usize, (off_t)gsize, stderr);
   1436 	if (nfile)
   1437 		fprintf(stderr, " -- replaced with %s", nfile);
   1438 	fprintf(stderr, "\n");
   1439 	fflush(stderr);
   1440 }
   1441 
   1442 /* print test results */
   1443 static void
   1444 print_test(char *file, int ok)
   1445 {
   1446 
   1447 	fprintf(stderr, "%s:%s  %s\n", file,
   1448 	    strlen(file) < 7 ? "\t\t" : "\t", ok ? "OK" : "NOT OK");
   1449 	fflush(stderr);
   1450 }
   1451 #endif
   1452 
   1453 /* print a file's info ala --list */
   1454 /* eg:
   1455   compressed uncompressed  ratio uncompressed_name
   1456       354841      1679360  78.8% /usr/pkgsrc/distfiles/libglade-2.0.1.tar
   1457 */
   1458 static void
   1459 print_list(int fd, off_t out, const char *outfile, time_t ts)
   1460 {
   1461 	static int first = 1;
   1462 #ifndef SMALL
   1463 	static off_t in_tot, out_tot;
   1464 	u_int32_t crc;
   1465 #endif
   1466 	off_t in;
   1467 	int rv;
   1468 
   1469 	if (first) {
   1470 #ifndef SMALL
   1471 		if (vflag)
   1472 			printf("method  crc     date  time  ");
   1473 #endif
   1474 		if (qflag == 0)
   1475 			printf("  compressed uncompressed  "
   1476 			       "ratio uncompressed_name\n");
   1477 	}
   1478 	first = 0;
   1479 
   1480 	/* print totals? */
   1481 #ifndef SMALL
   1482 	if (fd == -1) {
   1483 		in = in_tot;
   1484 		out = out_tot;
   1485 	} else
   1486 #endif
   1487 	{
   1488 		/* read the last 4 bytes - this is the uncompressed size */
   1489 		rv = lseek(fd, (off_t)(-8), SEEK_END);
   1490 		if (rv != -1) {
   1491 			unsigned char buf[8];
   1492 			u_int32_t usize;
   1493 
   1494 			if (read(fd, (char *)buf, sizeof(buf)) != sizeof(buf))
   1495 				maybe_warn("read of uncompressed size");
   1496 			usize = buf[4] | buf[5] << 8 | buf[6] << 16 | buf[7] << 24;
   1497 			in = (off_t)usize;
   1498 #ifndef SMALL
   1499 			crc = buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;
   1500 #endif
   1501 		}
   1502 	}
   1503 
   1504 #ifndef SMALL
   1505 	if (vflag && fd == -1)
   1506 		printf("                            ");
   1507 	else if (vflag) {
   1508 		char *date = ctime(&ts);
   1509 
   1510 		/* skip the day, 1/100th second, and year */
   1511 		date += 4;
   1512 		date[12] = 0;
   1513 		printf("%5s %08x %11s ", "defla"/*XXX*/, crc, date);
   1514 	}
   1515 	in_tot += in;
   1516 	out_tot += out;
   1517 #endif
   1518 	printf("%12llu %12llu ", (unsigned long long)out, (unsigned long long)in);
   1519 	print_ratio(in, out, stdout);
   1520 	printf(" %s\n", outfile);
   1521 }
   1522 
   1523 /* display the usage of NetBSD gzip */
   1524 static void
   1525 usage(void)
   1526 {
   1527 
   1528 	fprintf(stderr, "%s\n", gzip_version);
   1529 	fprintf(stderr,
   1530     "usage: %s [-" OPT_LIST "] [<file> [<file> ...]]\n"
   1531 #ifndef SMALL
   1532     " -c --stdout          write to stdout, keep original files\n"
   1533     "    --to-stdout\n"
   1534     " -d --decompress      uncompress files\n"
   1535     "    --uncompress\n"
   1536     " -f --force           force overwriting & compress links\n"
   1537     " -h --help            display this help\n"
   1538     " -n --no-name         don't save original file name or time stamp\n"
   1539     " -N --name            save or restore original file name and time stamp\n"
   1540     " -q --quiet           output no warnings\n"
   1541     " -r --recursive       recursively compress files in directories\n"
   1542     " -S .suf              use suffix .suf instead of .gz\n"
   1543     "    --suffix .suf\n"
   1544     " -t --test            test compressed file\n"
   1545     " -v --verbose         print extra statistics\n"
   1546     " -V --version         display program version\n"
   1547     " -1 --fast            fastest (worst) compression\n"
   1548     " -2 .. -8             set compression level\n"
   1549     " -9 --best            best (slowest) compression\n",
   1550 #else
   1551     ,
   1552 #endif
   1553 	    getprogname());
   1554 	exit(0);
   1555 }
   1556 
   1557 /* display the version of NetBSD gzip */
   1558 static void
   1559 display_version(void)
   1560 {
   1561 
   1562 	fprintf(stderr, "%s\n", gzip_version);
   1563 	exit(0);
   1564 }
   1565 
   1566 #ifndef NO_BZIP2_SUPPORT
   1567 #include "unbzip2.c"
   1568 #endif
   1569 #ifndef NO_COMPRESS_SUPPORT
   1570 #include "zuncompress.c"
   1571 #endif
   1572