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