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