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