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