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