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