xref: /netbsd-src/usr.bin/gzip/gzip.c (revision 4b71a66d0f279143147d63ebfcfd8a59499a3684)
1 /*	$NetBSD: gzip.c,v 1.91 2008/05/29 14:51:27 mrg 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 Matthew R. Green\n\
32      All rights reserved.\n");
33 __RCSID("$NetBSD: gzip.c,v 1.91 2008/05/29 14:51:27 mrg 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 rv, fd, ofd, zfd = -1;
1265 #ifndef SMALL
1266 	time_t timestamp = 0;
1267 	unsigned char name[PATH_MAX + 1];
1268 #endif
1269 
1270 	/* gather the old name info */
1271 
1272 	fd = open(file, O_RDONLY);
1273 	if (fd < 0) {
1274 		maybe_warn("can't open %s", file);
1275 		goto lose;
1276 	}
1277 
1278 	strlcpy(outfile, file, outsize);
1279 	if (check_suffix(outfile, 1) == NULL && !(cflag || lflag)) {
1280 		maybe_warnx("%s: unknown suffix -- ignored", file);
1281 		goto lose;
1282 	}
1283 
1284 	rbytes = read(fd, header1, sizeof header1);
1285 	if (rbytes != sizeof header1) {
1286 		/* we don't want to fail here. */
1287 #ifndef SMALL
1288 		if (fflag)
1289 			goto lose;
1290 #endif
1291 		if (rbytes == -1)
1292 			maybe_warn("can't read %s", file);
1293 		else
1294 			goto unexpected_EOF;
1295 		goto lose;
1296 	}
1297 
1298 	method = file_gettype(header1);
1299 
1300 #ifndef SMALL
1301 	if (fflag == 0 && method == FT_UNKNOWN) {
1302 		maybe_warnx("%s: not in gzip format", file);
1303 		goto lose;
1304 	}
1305 
1306 #endif
1307 
1308 #ifndef SMALL
1309 	if (method == FT_GZIP && Nflag) {
1310 		unsigned char ts[4];	/* timestamp */
1311 
1312 		rv = pread(fd, ts, sizeof ts, GZIP_TIMESTAMP);
1313 		if (rv >= 0 && rv < sizeof ts)
1314 			goto unexpected_EOF;
1315 		if (rv == -1) {
1316 			if (!fflag)
1317 				maybe_warn("can't read %s", file);
1318 			goto lose;
1319 		}
1320 		timestamp = ts[3] << 24 | ts[2] << 16 | ts[1] << 8 | ts[0];
1321 
1322 		if (header1[3] & ORIG_NAME) {
1323 			rbytes = pread(fd, name, sizeof name, GZIP_ORIGNAME);
1324 			if (rbytes < 0) {
1325 				maybe_warn("can't read %s", file);
1326 				goto lose;
1327 			}
1328 			if (name[0] != 0) {
1329 				/* preserve original directory name */
1330 				char *dp = strrchr(file, '/');
1331 				if (dp == NULL)
1332 					dp = file;
1333 				else
1334 					dp++;
1335 				snprintf(outfile, outsize, "%.*s%.*s",
1336 						(int) (dp - file),
1337 						file, (int) rbytes, name);
1338 			}
1339 		}
1340 	}
1341 #endif
1342 	lseek(fd, 0, SEEK_SET);
1343 
1344 	if (cflag == 0 || lflag) {
1345 		if (fstat(fd, &isb) != 0)
1346 			goto lose;
1347 #ifndef SMALL
1348 		if (isb.st_nlink > 1 && lflag == 0 && fflag == 0) {
1349 			maybe_warnx("%s has %d other links -- skipping",
1350 			    file, isb.st_nlink - 1);
1351 			goto lose;
1352 		}
1353 		if (nflag == 0 && timestamp)
1354 			isb.st_mtime = timestamp;
1355 		if (check_outfile(outfile) == 0)
1356 			goto lose;
1357 #endif
1358 	}
1359 
1360 	if (cflag == 0 && lflag == 0) {
1361 		zfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0600);
1362 		if (zfd == STDOUT_FILENO) {
1363 			/* We won't close STDOUT_FILENO later... */
1364 			zfd = dup(zfd);
1365 			close(STDOUT_FILENO);
1366 		}
1367 		if (zfd == -1) {
1368 			maybe_warn("can't open %s", outfile);
1369 			goto lose;
1370 		}
1371 	} else
1372 		zfd = STDOUT_FILENO;
1373 
1374 #ifndef NO_BZIP2_SUPPORT
1375 	if (method == FT_BZIP2) {
1376 
1377 		/* XXX */
1378 		if (lflag) {
1379 			maybe_warnx("no -l with bzip2 files");
1380 			goto lose;
1381 		}
1382 
1383 		size = unbzip2(fd, zfd, NULL, 0, NULL);
1384 	} else
1385 #endif
1386 
1387 #ifndef NO_COMPRESS_SUPPORT
1388 	if (method == FT_Z) {
1389 		FILE *in, *out;
1390 
1391 		/* XXX */
1392 		if (lflag) {
1393 			maybe_warnx("no -l with Lempel-Ziv files");
1394 			goto lose;
1395 		}
1396 
1397 		if ((in = zdopen(fd)) == NULL) {
1398 			maybe_warn("zdopen for read: %s", file);
1399 			goto lose;
1400 		}
1401 
1402 		out = fdopen(dup(zfd), "w");
1403 		if (out == NULL) {
1404 			maybe_warn("fdopen for write: %s", outfile);
1405 			fclose(in);
1406 			goto lose;
1407 		}
1408 
1409 		size = zuncompress(in, out, NULL, 0, NULL);
1410 		/* need to fclose() if ferror() is true... */
1411 		if (ferror(in) | fclose(in)) {
1412 			maybe_warn("failed infile fclose");
1413 			unlink(outfile);
1414 			(void)fclose(out);
1415 		}
1416 		if (fclose(out) != 0) {
1417 			maybe_warn("failed outfile fclose");
1418 			unlink(outfile);
1419 			goto lose;
1420 		}
1421 	} else
1422 #endif
1423 
1424 #ifndef SMALL
1425 	if (method == FT_UNKNOWN) {
1426 		if (lflag) {
1427 			maybe_warnx("no -l for unknown filetypes");
1428 			goto lose;
1429 		}
1430 		size = cat_fd(NULL, 0, NULL, fd);
1431 	} else
1432 #endif
1433 	{
1434 		if (lflag) {
1435 			print_list(fd, isb.st_size, outfile, isb.st_mtime);
1436 			close(fd);
1437 			return -1;	/* XXX */
1438 		}
1439 
1440 		size = gz_uncompress(fd, zfd, NULL, 0, NULL, file);
1441 	}
1442 
1443 	if (close(fd) != 0)
1444 		maybe_warn("couldn't close input");
1445 	if (zfd != STDOUT_FILENO && close(zfd) != 0)
1446 		maybe_warn("couldn't close output");
1447 
1448 	if (size == -1) {
1449 		if (cflag == 0)
1450 			unlink(outfile);
1451 		maybe_warnx("%s: uncompress failed", file);
1452 		return -1;
1453 	}
1454 
1455 	/* if testing, or we uncompressed to stdout, this is all we need */
1456 #ifndef SMALL
1457 	if (tflag)
1458 		return size;
1459 #endif
1460 	/* if we are uncompressing to stdin, don't remove the file. */
1461 	if (cflag)
1462 		return size;
1463 
1464 	/*
1465 	 * if we create a file...
1466 	 */
1467 	/*
1468 	 * if we can't stat the file don't remove the file.
1469 	 */
1470 
1471 	ofd = open(outfile, O_RDWR, 0);
1472 	if (ofd == -1) {
1473 		maybe_warn("couldn't open (leaving original): %s",
1474 			   outfile);
1475 		return -1;
1476 	}
1477 	if (fstat(ofd, &osb) != 0) {
1478 		maybe_warn("couldn't stat (leaving original): %s",
1479 			   outfile);
1480 		close(ofd);
1481 		return -1;
1482 	}
1483 	if (osb.st_size != size) {
1484 		maybe_warnx("stat gave different size: %" PRIdOFF
1485 				" != %" PRIdOFF " (leaving original)",
1486 				size, osb.st_size);
1487 		close(ofd);
1488 		unlink(outfile);
1489 		return -1;
1490 	}
1491 	unlink_input(file, &isb);
1492 #ifndef SMALL
1493 	copymodes(ofd, &isb, outfile);
1494 #endif
1495 	close(ofd);
1496 	return size;
1497 
1498     unexpected_EOF:
1499 	maybe_warnx("%s: unexpected end of file", file);
1500     lose:
1501 	if (fd != -1)
1502 		close(fd);
1503 	if (zfd != -1 && zfd != STDOUT_FILENO)
1504 		close(fd);
1505 	return -1;
1506 }
1507 
1508 #ifndef SMALL
1509 static off_t
1510 cat_fd(unsigned char * prepend, size_t count, off_t *gsizep, int fd)
1511 {
1512 	char buf[BUFLEN];
1513 	off_t in_tot;
1514 	ssize_t w;
1515 
1516 	in_tot = count;
1517 	w = write(STDOUT_FILENO, prepend, count);
1518 	if (w == -1 || (size_t)w != count) {
1519 		maybe_warn("write to stdout");
1520 		return -1;
1521 	}
1522 	for (;;) {
1523 		ssize_t rv;
1524 
1525 		rv = read(fd, buf, sizeof buf);
1526 		if (rv == 0)
1527 			break;
1528 		if (rv < 0) {
1529 			maybe_warn("read from fd %d", fd);
1530 			break;
1531 		}
1532 
1533 		if (write(STDOUT_FILENO, buf, rv) != rv) {
1534 			maybe_warn("write to stdout");
1535 			break;
1536 		}
1537 		in_tot += rv;
1538 	}
1539 
1540 	if (gsizep)
1541 		*gsizep = in_tot;
1542 	return (in_tot);
1543 }
1544 #endif
1545 
1546 static void
1547 handle_stdin(void)
1548 {
1549 	unsigned char header1[4];
1550 	off_t usize, gsize;
1551 	enum filetype method;
1552 	ssize_t bytes_read;
1553 #ifndef NO_COMPRESS_SUPPORT
1554 	FILE *in;
1555 #endif
1556 
1557 #ifndef SMALL
1558 	if (fflag == 0 && lflag == 0 && isatty(STDIN_FILENO)) {
1559 		maybe_warnx("standard input is a terminal -- ignoring");
1560 		return;
1561 	}
1562 #endif
1563 
1564 	if (lflag) {
1565 		struct stat isb;
1566 
1567 		/* XXX could read the whole file, etc. */
1568 		if (fstat(STDIN_FILENO, &isb) < 0) {
1569 			maybe_warn("fstat");
1570 			return;
1571 		}
1572 		print_list(STDIN_FILENO, isb.st_size, "stdout", isb.st_mtime);
1573 		return;
1574 	}
1575 
1576 	bytes_read = read_retry(STDIN_FILENO, header1, sizeof header1);
1577 	if (bytes_read == -1) {
1578 		maybe_warn("can't read stdin");
1579 		return;
1580 	} else if (bytes_read != sizeof(header1)) {
1581 		maybe_warnx("(stdin): unexpected end of file");
1582 		return;
1583 	}
1584 
1585 	method = file_gettype(header1);
1586 	switch (method) {
1587 	default:
1588 #ifndef SMALL
1589 		if (fflag == 0) {
1590 			maybe_warnx("unknown compression format");
1591 			return;
1592 		}
1593 		usize = cat_fd(header1, sizeof header1, &gsize, STDIN_FILENO);
1594 		break;
1595 #endif
1596 	case FT_GZIP:
1597 		usize = gz_uncompress(STDIN_FILENO, STDOUT_FILENO,
1598 			      header1, sizeof header1, &gsize, "(stdin)");
1599 		break;
1600 #ifndef NO_BZIP2_SUPPORT
1601 	case FT_BZIP2:
1602 		usize = unbzip2(STDIN_FILENO, STDOUT_FILENO,
1603 				header1, sizeof header1, &gsize);
1604 		break;
1605 #endif
1606 #ifndef NO_COMPRESS_SUPPORT
1607 	case FT_Z:
1608 		if ((in = zdopen(STDIN_FILENO)) == NULL) {
1609 			maybe_warnx("zopen of stdin");
1610 			return;
1611 		}
1612 
1613 		usize = zuncompress(in, stdout, header1, sizeof header1, &gsize);
1614 		fclose(in);
1615 		break;
1616 #endif
1617 	}
1618 
1619 #ifndef SMALL
1620         if (vflag && !tflag && usize != -1 && gsize != -1)
1621 		print_verbage(NULL, NULL, usize, gsize);
1622 	if (vflag && tflag)
1623 		print_test("(stdin)", usize != -1);
1624 #endif
1625 
1626 }
1627 
1628 static void
1629 handle_stdout(void)
1630 {
1631 	off_t gsize, usize;
1632 	struct stat sb;
1633 	time_t systime;
1634 	uint32_t mtime;
1635 	int ret;
1636 
1637 #ifndef SMALL
1638 	if (fflag == 0 && isatty(STDOUT_FILENO)) {
1639 		maybe_warnx("standard output is a terminal -- ignoring");
1640 		return;
1641 	}
1642 #endif
1643 	/* If stdin is a file use it's mtime, otherwise use current time */
1644 	ret = fstat(STDIN_FILENO, &sb);
1645 
1646 #ifndef SMALL
1647 	if (ret < 0) {
1648 		maybe_warn("Can't stat stdin");
1649 		return;
1650 	}
1651 #endif
1652 
1653 	if (S_ISREG(sb.st_mode))
1654 		mtime = (uint32_t)sb.st_mtime;
1655 	else {
1656 		systime = time(NULL);
1657 #ifndef SMALL
1658 		if (systime == -1) {
1659 			maybe_warn("time");
1660 			return;
1661 		}
1662 #endif
1663 		mtime = (uint32_t)systime;
1664 	}
1665 
1666 	usize = gz_compress(STDIN_FILENO, STDOUT_FILENO, &gsize, "", mtime);
1667 #ifndef SMALL
1668         if (vflag && !tflag && usize != -1 && gsize != -1)
1669 		print_verbage(NULL, NULL, usize, gsize);
1670 #endif
1671 }
1672 
1673 /* do what is asked for, for the path name */
1674 static void
1675 handle_pathname(char *path)
1676 {
1677 	char *opath = path, *s = NULL;
1678 	ssize_t len;
1679 	int slen;
1680 	struct stat sb;
1681 
1682 	/* check for stdout/stdin */
1683 	if (path[0] == '-' && path[1] == '\0') {
1684 		if (dflag)
1685 			handle_stdin();
1686 		else
1687 			handle_stdout();
1688 		return;
1689 	}
1690 
1691 retry:
1692 	if (stat(path, &sb) != 0) {
1693 		/* lets try <path>.gz if we're decompressing */
1694 		if (dflag && s == NULL && errno == ENOENT) {
1695 			len = strlen(path);
1696 			slen = suffixes[0].ziplen;
1697 			s = malloc(len + slen + 1);
1698 			if (s == NULL)
1699 				maybe_err("malloc");
1700 			memcpy(s, path, len);
1701 			memcpy(s + len, suffixes[0].zipped, slen + 1);
1702 			path = s;
1703 			goto retry;
1704 		}
1705 		maybe_warn("can't stat: %s", opath);
1706 		goto out;
1707 	}
1708 
1709 	if (S_ISDIR(sb.st_mode)) {
1710 #ifndef SMALL
1711 		if (rflag)
1712 			handle_dir(path);
1713 		else
1714 #endif
1715 			maybe_warnx("%s is a directory", path);
1716 		goto out;
1717 	}
1718 
1719 	if (S_ISREG(sb.st_mode))
1720 		handle_file(path, &sb);
1721 	else
1722 		maybe_warnx("%s is not a regular file", path);
1723 
1724 out:
1725 	if (s)
1726 		free(s);
1727 }
1728 
1729 /* compress/decompress a file */
1730 static void
1731 handle_file(char *file, struct stat *sbp)
1732 {
1733 	off_t usize, gsize;
1734 	char	outfile[PATH_MAX];
1735 
1736 	infile = file;
1737 	if (dflag) {
1738 		usize = file_uncompress(file, outfile, sizeof(outfile));
1739 #ifndef SMALL
1740 		if (vflag && tflag)
1741 			print_test(file, usize != -1);
1742 #endif
1743 		if (usize == -1)
1744 			return;
1745 		gsize = sbp->st_size;
1746 	} else {
1747 		gsize = file_compress(file, outfile, sizeof(outfile));
1748 		if (gsize == -1)
1749 			return;
1750 		usize = sbp->st_size;
1751 	}
1752 
1753 
1754 #ifndef SMALL
1755 	if (vflag && !tflag)
1756 		print_verbage(file, (cflag) ? NULL : outfile, usize, gsize);
1757 #endif
1758 }
1759 
1760 #ifndef SMALL
1761 /* this is used with -r to recursively descend directories */
1762 static void
1763 handle_dir(char *dir)
1764 {
1765 	char *path_argv[2];
1766 	FTS *fts;
1767 	FTSENT *entry;
1768 
1769 	path_argv[0] = dir;
1770 	path_argv[1] = 0;
1771 	fts = fts_open(path_argv, FTS_PHYSICAL, NULL);
1772 	if (fts == NULL) {
1773 		warn("couldn't fts_open %s", dir);
1774 		return;
1775 	}
1776 
1777 	while ((entry = fts_read(fts))) {
1778 		switch(entry->fts_info) {
1779 		case FTS_D:
1780 		case FTS_DP:
1781 			continue;
1782 
1783 		case FTS_DNR:
1784 		case FTS_ERR:
1785 		case FTS_NS:
1786 			maybe_warn("%s", entry->fts_path);
1787 			continue;
1788 		case FTS_F:
1789 			handle_file(entry->fts_name, entry->fts_statp);
1790 		}
1791 	}
1792 	(void)fts_close(fts);
1793 }
1794 #endif
1795 
1796 /* print a ratio - size reduction as a fraction of uncompressed size */
1797 static void
1798 print_ratio(off_t in, off_t out, FILE *where)
1799 {
1800 	int percent10;	/* 10 * percent */
1801 	off_t diff;
1802 	char buff[8];
1803 	int len;
1804 
1805 	diff = in - out/2;
1806 	if (diff <= 0)
1807 		/*
1808 		 * Output is more than double size of input! print -99.9%
1809 		 * Quite possibly we've failed to get the original size.
1810 		 */
1811 		percent10 = -999;
1812 	else {
1813 		/*
1814 		 * We only need 12 bits of result from the final division,
1815 		 * so reduce the values until a 32bit division will suffice.
1816 		 */
1817 		while (in > 0x100000) {
1818 			diff >>= 1;
1819 			in >>= 1;
1820 		}
1821 		if (in != 0)
1822 			percent10 = ((u_int)diff * 2000) / (u_int)in - 1000;
1823 		else
1824 			percent10 = 0;
1825 	}
1826 
1827 	len = snprintf(buff, sizeof buff, "%2.2d.", percent10);
1828 	/* Move the '.' to before the last digit */
1829 	buff[len - 1] = buff[len - 2];
1830 	buff[len - 2] = '.';
1831 	fprintf(where, "%5s%%", buff);
1832 }
1833 
1834 #ifndef SMALL
1835 /* print compression statistics, and the new name (if there is one!) */
1836 static void
1837 print_verbage(const char *file, const char *nfile, off_t usize, off_t gsize)
1838 {
1839 	if (file)
1840 		fprintf(stderr, "%s:%s  ", file,
1841 		    strlen(file) < 7 ? "\t\t" : "\t");
1842 	print_ratio(usize, gsize, stderr);
1843 	if (nfile)
1844 		fprintf(stderr, " -- replaced with %s", nfile);
1845 	fprintf(stderr, "\n");
1846 	fflush(stderr);
1847 }
1848 
1849 /* print test results */
1850 static void
1851 print_test(const char *file, int ok)
1852 {
1853 
1854 	if (exit_value == 0 && ok == 0)
1855 		exit_value = 1;
1856 	fprintf(stderr, "%s:%s  %s\n", file,
1857 	    strlen(file) < 7 ? "\t\t" : "\t", ok ? "OK" : "NOT OK");
1858 	fflush(stderr);
1859 }
1860 #endif
1861 
1862 /* print a file's info ala --list */
1863 /* eg:
1864   compressed uncompressed  ratio uncompressed_name
1865       354841      1679360  78.8% /usr/pkgsrc/distfiles/libglade-2.0.1.tar
1866 */
1867 static void
1868 print_list(int fd, off_t out, const char *outfile, time_t ts)
1869 {
1870 	static int first = 1;
1871 #ifndef SMALL
1872 	static off_t in_tot, out_tot;
1873 	uint32_t crc = 0;
1874 #endif
1875 	off_t in = 0, rv;
1876 
1877 	if (first) {
1878 #ifndef SMALL
1879 		if (vflag)
1880 			printf("method  crc     date  time  ");
1881 #endif
1882 		if (qflag == 0)
1883 			printf("  compressed uncompressed  "
1884 			       "ratio uncompressed_name\n");
1885 	}
1886 	first = 0;
1887 
1888 	/* print totals? */
1889 #ifndef SMALL
1890 	if (fd == -1) {
1891 		in = in_tot;
1892 		out = out_tot;
1893 	} else
1894 #endif
1895 	{
1896 		/* read the last 4 bytes - this is the uncompressed size */
1897 		rv = lseek(fd, (off_t)(-8), SEEK_END);
1898 		if (rv != -1) {
1899 			unsigned char buf[8];
1900 			uint32_t usize;
1901 
1902 			rv = read(fd, (char *)buf, sizeof(buf));
1903 			if (rv == -1)
1904 				maybe_warn("read of uncompressed size");
1905 			else if (rv != sizeof(buf))
1906 				maybe_warnx("read of uncompressed size");
1907 
1908 			else {
1909 				usize = buf[4] | buf[5] << 8 |
1910 					buf[6] << 16 | buf[7] << 24;
1911 				in = (off_t)usize;
1912 #ifndef SMALL
1913 				crc = buf[0] | buf[1] << 8 |
1914 				      buf[2] << 16 | buf[3] << 24;
1915 #endif
1916 			}
1917 		}
1918 	}
1919 
1920 #ifndef SMALL
1921 	if (vflag && fd == -1)
1922 		printf("                            ");
1923 	else if (vflag) {
1924 		char *date = ctime(&ts);
1925 
1926 		/* skip the day, 1/100th second, and year */
1927 		date += 4;
1928 		date[12] = 0;
1929 		printf("%5s %08x %11s ", "defla"/*XXX*/, crc, date);
1930 	}
1931 	in_tot += in;
1932 	out_tot += out;
1933 #endif
1934 	printf("%12llu %12llu ", (unsigned long long)out, (unsigned long long)in);
1935 	print_ratio(in, out, stdout);
1936 	printf(" %s\n", outfile);
1937 }
1938 
1939 /* display the usage of NetBSD gzip */
1940 static void
1941 usage(void)
1942 {
1943 
1944 	fprintf(stderr, "%s\n", gzip_version);
1945 	fprintf(stderr,
1946     "usage: %s [-" OPT_LIST "] [<file> [<file> ...]]\n"
1947 #ifndef SMALL
1948     " -1 --fast            fastest (worst) compression\n"
1949     " -2 .. -8             set compression level\n"
1950     " -9 --best            best (slowest) compression\n"
1951     " -c --stdout          write to stdout, keep original files\n"
1952     "    --to-stdout\n"
1953     " -d --decompress      uncompress files\n"
1954     "    --uncompress\n"
1955     " -f --force           force overwriting & compress links\n"
1956     " -h --help            display this help\n"
1957     " -l --list            list compressed file contents\n"
1958     " -N --name            save or restore original file name and time stamp\n"
1959     " -n --no-name         don't save original file name or time stamp\n"
1960     " -q --quiet           output no warnings\n"
1961     " -r --recursive       recursively compress files in directories\n"
1962     " -S .suf              use suffix .suf instead of .gz\n"
1963     "    --suffix .suf\n"
1964     " -t --test            test compressed file\n"
1965     " -V --version         display program version\n"
1966     " -v --verbose         print extra statistics\n",
1967 #else
1968     ,
1969 #endif
1970 	    getprogname());
1971 	exit(0);
1972 }
1973 
1974 /* display the version of NetBSD gzip */
1975 static void
1976 display_version(void)
1977 {
1978 
1979 	fprintf(stderr, "%s\n", gzip_version);
1980 	exit(0);
1981 }
1982 
1983 #ifndef NO_BZIP2_SUPPORT
1984 #include "unbzip2.c"
1985 #endif
1986 #ifndef NO_COMPRESS_SUPPORT
1987 #include "zuncompress.c"
1988 #endif
1989 
1990 static ssize_t
1991 read_retry(int fd, void *buf, size_t sz)
1992 {
1993 	char *cp = buf;
1994 	size_t left = MIN(sz, (size_t) SSIZE_MAX);
1995 
1996 	while (left > 0) {
1997 		ssize_t ret;
1998 
1999 		ret = read(fd, cp, left);
2000 		if (ret == -1) {
2001 			return ret;
2002 		} else if (ret == 0) {
2003 			break; /* EOF */
2004 		}
2005 		cp += ret;
2006 		left -= ret;
2007 	}
2008 
2009 	return sz - left;
2010 }
2011