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