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