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