Resurrect z*grep script to support gzip'ped files.
[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                                 if (z.avail_out > 0 && !done_reading)
894                                         continue;
895                         case Z_STREAM_END:
896                         case Z_OK:
897                                 break;
898
899                         case Z_NEED_DICT:
900                                 maybe_warnx("Z_NEED_DICT error");
901                                 goto stop_and_fail;
902                         case Z_DATA_ERROR:
903                                 maybe_warnx("data stream error");
904                                 goto stop_and_fail;
905                         case Z_STREAM_ERROR:
906                                 maybe_warnx("internal stream error");
907                                 goto stop_and_fail;
908                         case Z_MEM_ERROR:
909                                 maybe_warnx("memory allocation error");
910                                 goto stop_and_fail;
911
912                         default:
913                                 maybe_warn("unknown error from inflate(): %d",
914                                     error);
915                         }
916                         wr = BUFLEN - z.avail_out;
917
918                         if (wr != 0) {
919                                 crc = crc32(crc, (const Bytef *)outbufp, (unsigned)wr);
920                                 if (
921 #ifndef SMALL
922                                     /* don't write anything with -t */
923                                     tflag == 0 &&
924 #endif
925                                     write(out, outbufp, wr) != wr) {
926                                         maybe_warn("error writing to output");
927                                         goto stop_and_fail;
928                                 }
929
930                                 out_tot += wr;
931                                 out_sub_tot += wr;
932                         }
933
934                         if (error == Z_STREAM_END) {
935                                 inflateEnd(&z);
936                                 state++;
937                         }
938
939                         z.next_out = outbufp;
940                         z.avail_out = BUFLEN;
941
942                         break;
943                 case GZSTATE_CRC:
944                         {
945                                 uLong origcrc;
946
947                                 if (z.avail_in < 4) {
948                                         if (!done_reading) {
949                                                 needmore = 1;
950                                                 continue;
951                                         }
952                                         maybe_warnx("truncated input");
953                                         goto stop_and_fail;
954                                 }
955                                 origcrc = ((unsigned)z.next_in[0] & 0xff) |
956                                         ((unsigned)z.next_in[1] & 0xff) << 8 |
957                                         ((unsigned)z.next_in[2] & 0xff) << 16 |
958                                         ((unsigned)z.next_in[3] & 0xff) << 24;
959                                 if (origcrc != crc) {
960                                         maybe_warnx("invalid compressed"
961                                              " data--crc error");
962                                         goto stop_and_fail;
963                                 }
964                         }
965
966                         z.avail_in -= 4;
967                         z.next_in += 4;
968
969                         if (!z.avail_in && done_reading) {
970                                 goto stop;
971                         }
972                         state++;
973                         break;
974                 case GZSTATE_LEN:
975                         {
976                                 uLong origlen;
977
978                                 if (z.avail_in < 4) {
979                                         if (!done_reading) {
980                                                 needmore = 1;
981                                                 continue;
982                                         }
983                                         maybe_warnx("truncated input");
984                                         goto stop_and_fail;
985                                 }
986                                 origlen = ((unsigned)z.next_in[0] & 0xff) |
987                                         ((unsigned)z.next_in[1] & 0xff) << 8 |
988                                         ((unsigned)z.next_in[2] & 0xff) << 16 |
989                                         ((unsigned)z.next_in[3] & 0xff) << 24;
990
991                                 if (origlen != out_sub_tot) {
992                                         maybe_warnx("invalid compressed"
993                                              " data--length error");
994                                         goto stop_and_fail;
995                                 }
996                         }
997                                 
998                         z.avail_in -= 4;
999                         z.next_in += 4;
1000
1001                         if (error < 0) {
1002                                 maybe_warnx("decompression error");
1003                                 goto stop_and_fail;
1004                         }
1005                         state = GZSTATE_MAGIC0;
1006                         break;
1007                 }
1008                 continue;
1009 stop_and_fail:
1010                 out_tot = -1;
1011 stop:
1012                 break;
1013         }
1014         if (state > GZSTATE_INIT)
1015                 inflateEnd(&z);
1016
1017         free(inbufp);
1018 out1:
1019         free(outbufp);
1020 out2:
1021         if (gsizep)
1022                 *gsizep = in_tot;
1023         return (out_tot);
1024 }
1025
1026 #ifndef SMALL
1027 /*
1028  * set the owner, mode, flags & utimes using the given file descriptor.
1029  * file is only used in possible warning messages.
1030  */
1031 static void
1032 copymodes(int fd, const struct stat *sbp, const char *file)
1033 {
1034         struct timeval times[2];
1035         struct stat sb;
1036
1037         /*
1038          * If we have no info on the input, give this file some
1039          * default values and return..
1040          */
1041         if (sbp == NULL) {
1042                 mode_t mask = umask(022);
1043
1044                 (void)fchmod(fd, DEFFILEMODE & ~mask);
1045                 (void)umask(mask);
1046                 return; 
1047         }
1048         sb = *sbp;
1049
1050         /* if the chown fails, remove set-id bits as-per compress(1) */
1051         if (fchown(fd, sb.st_uid, sb.st_gid) < 0) {
1052                 if (errno != EPERM)
1053                         maybe_warn("couldn't fchown: %s", file);
1054                 sb.st_mode &= ~(S_ISUID|S_ISGID);
1055         }
1056
1057         /* we only allow set-id and the 9 normal permission bits */
1058         sb.st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
1059         if (fchmod(fd, sb.st_mode) < 0)
1060                 maybe_warn("couldn't fchmod: %s", file);
1061
1062         /* only try flags if they exist already */
1063         if (sb.st_flags != 0 && fchflags(fd, sb.st_flags) < 0)
1064                 maybe_warn("couldn't fchflags: %s", file);
1065
1066         TIMESPEC_TO_TIMEVAL(&times[0], &sb.st_atimespec);
1067         TIMESPEC_TO_TIMEVAL(&times[1], &sb.st_mtimespec);
1068         if (futimes(fd, times) < 0)
1069                 maybe_warn("couldn't utimes: %s", file);
1070 }
1071 #endif
1072
1073 /* what sort of file is this? */
1074 static enum filetype
1075 file_gettype(u_char *buf)
1076 {
1077
1078         if (buf[0] == GZIP_MAGIC0 &&
1079             (buf[1] == GZIP_MAGIC1 || buf[1] == GZIP_OMAGIC1))
1080                 return FT_GZIP;
1081         else
1082 #ifndef NO_BZIP2_SUPPORT
1083         if (memcmp(buf, BZIP2_MAGIC, 3) == 0 &&
1084             buf[3] >= '0' && buf[3] <= '9')
1085                 return FT_BZIP2;
1086         else
1087 #endif
1088 #ifndef NO_COMPRESS_SUPPORT
1089         if (memcmp(buf, Z_MAGIC, 2) == 0)
1090                 return FT_Z;
1091         else
1092 #endif
1093 #ifndef NO_PACK_SUPPORT
1094         if (memcmp(buf, PACK_MAGIC, 2) == 0)
1095                 return FT_PACK;
1096         else
1097 #endif
1098                 return FT_UNKNOWN;
1099 }
1100
1101 #ifndef SMALL
1102 /* check the outfile is OK. */
1103 static int
1104 check_outfile(const char *outfile)
1105 {
1106         struct stat sb;
1107         int ok = 1;
1108
1109         if (lflag == 0 && stat(outfile, &sb) == 0) {
1110                 if (fflag)
1111                         unlink(outfile);
1112                 else if (isatty(STDIN_FILENO)) {
1113                         char ans[10] = { 'n', '\0' };   /* default */
1114
1115                         fprintf(stderr, "%s already exists -- do you wish to "
1116                                         "overwrite (y or n)? " , outfile);
1117                         (void)fgets(ans, sizeof(ans) - 1, stdin);
1118                         if (ans[0] != 'y' && ans[0] != 'Y') {
1119                                 fprintf(stderr, "\tnot overwriting\n");
1120                                 ok = 0;
1121                         } else
1122                                 unlink(outfile);
1123                 } else {
1124                         maybe_warnx("%s already exists -- skipping", outfile);
1125                         ok = 0;
1126                 }
1127         }
1128         return ok;
1129 }
1130
1131 static void
1132 unlink_input(const char *file, const struct stat *sb)
1133 {
1134         struct stat nsb;
1135
1136         if (kflag)
1137                 return;
1138         if (stat(file, &nsb) != 0)
1139                 /* Must be gone alrady */
1140                 return;
1141         if (nsb.st_dev != sb->st_dev || nsb.st_ino != sb->st_ino)
1142                 /* Definitely a different file */
1143                 return;
1144         unlink(file);
1145 }
1146 #endif
1147
1148 static const suffixes_t *
1149 check_suffix(char *file, int xlate)
1150 {
1151         const suffixes_t *s;
1152         int len = strlen(file);
1153         char *sp;
1154
1155         for (s = suffixes; s != suffixes + NUM_SUFFIXES; s++) {
1156                 /* if it doesn't fit in "a.suf", don't bother */
1157                 if (s->ziplen >= len)
1158                         continue;
1159                 sp = file + len - s->ziplen;
1160                 if (strcmp(s->zipped, sp) != 0)
1161                         continue;
1162                 if (xlate)
1163                         strcpy(sp, s->normal);
1164                 return s;
1165         }
1166         return NULL;
1167 }
1168
1169 /*
1170  * compress the given file: create a corresponding .gz file and remove the
1171  * original.
1172  */
1173 static off_t
1174 file_compress(char *file, char *outfile, size_t outsize)
1175 {
1176         int in;
1177         int out;
1178         off_t size, insize;
1179 #ifndef SMALL
1180         struct stat isb, osb;
1181         const suffixes_t *suff;
1182 #endif
1183
1184         in = open(file, O_RDONLY);
1185         if (in == -1) {
1186                 maybe_warn("can't open %s", file);
1187                 return -1;
1188         }
1189
1190         if (cflag == 0) {
1191 #ifndef SMALL
1192                 if (fstat(in, &isb) == 0) {
1193                         if (isb.st_nlink > 1 && fflag == 0) {
1194                                 maybe_warnx("%s has %d other link%s -- "
1195                                             "skipping", file, isb.st_nlink - 1,
1196                                             isb.st_nlink == 1 ? "" : "s");
1197                                 close(in);
1198                                 return -1;
1199                         }
1200                 }
1201
1202                 if (fflag == 0 && (suff = check_suffix(file, 0))
1203                     && suff->zipped[0] != 0) {
1204                         maybe_warnx("%s already has %s suffix -- unchanged",
1205                                     file, suff->zipped);
1206                         close(in);
1207                         return -1;
1208                 }
1209 #endif
1210
1211                 /* Add (usually) .gz to filename */
1212                 if ((size_t)snprintf(outfile, outsize, "%s%s",
1213                                      file, suffixes[0].zipped) >= outsize) {
1214                         errx(1, "file path too long: %s", file);
1215                 }
1216 #ifndef SMALL
1217                 if (check_outfile(outfile) == 0) {
1218                         close(in);
1219                         return -1;
1220                 }
1221 #endif
1222         }
1223
1224         if (cflag == 0) {
1225                 out = open(outfile, O_WRONLY | O_CREAT | O_EXCL, 0600);
1226                 if (out == -1) {
1227                         maybe_warn("could not create output: %s", outfile);
1228                         fclose(stdin);
1229                         return -1;
1230                 }
1231         } else
1232                 out = STDOUT_FILENO;
1233
1234         insize = gz_compress(in, out, &size, basename(file), (uint32_t)isb.st_mtime);
1235
1236         (void)close(in);
1237
1238         /*
1239          * If there was an error, insize will be -1.
1240          * If we compressed to stdout, just return the size.
1241          * Otherwise stat the file and check it is the correct size.
1242          * We only blow away the file if we can stat the output and it
1243          * has the expected size.
1244          */
1245         if (cflag != 0)
1246                 return insize == -1 ? -1 : size;
1247
1248 #ifndef SMALL
1249         if (fstat(out, &osb) != 0) {
1250                 maybe_warn("couldn't stat: %s", outfile);
1251                 goto bad_outfile;
1252         }
1253
1254         if (osb.st_size != size) {
1255                 maybe_warnx("output file: %s wrong size (%" PRIdOFF
1256                                 " != %" PRIdOFF "), deleting",
1257                                 outfile, osb.st_size, size);
1258                 goto bad_outfile;
1259         }
1260
1261         copymodes(out, &isb, outfile);
1262 #endif
1263         if (close(out) == -1)
1264                 maybe_warn("couldn't close output");
1265
1266         /* output is good, ok to delete input */
1267         unlink_input(file, &isb);
1268         return size;
1269
1270 #ifndef SMALL
1271     bad_outfile:
1272         if (close(out) == -1)
1273                 maybe_warn("couldn't close output");
1274
1275         maybe_warnx("leaving original %s", file);
1276         unlink(outfile);
1277         return size;
1278 #endif
1279 }
1280
1281 /* uncompress the given file and remove the original */
1282 static off_t
1283 file_uncompress(char *file, char *outfile, size_t outsize)
1284 {
1285         struct stat isb, osb;
1286         off_t size;
1287         ssize_t rbytes;
1288         unsigned char header1[4];
1289         enum filetype method;
1290         int fd, ofd, zfd = -1;
1291 #ifndef SMALL
1292         ssize_t rv;
1293         time_t timestamp = 0;
1294         unsigned char name[PATH_MAX + 1];
1295 #endif
1296
1297         /* gather the old name info */
1298
1299         fd = open(file, O_RDONLY);
1300         if (fd < 0) {
1301                 maybe_warn("can't open %s", file);
1302                 goto lose;
1303         }
1304
1305         if ((size_t)snprintf(outfile, outsize, "%s", file) >= outsize)
1306                 errx(1, "file path too long: %s", file);
1307         if (check_suffix(outfile, 1) == NULL && !(cflag || lflag)) {
1308                 maybe_warnx("%s: unknown suffix -- ignored", file);
1309                 goto lose;
1310         }
1311
1312         rbytes = read(fd, header1, sizeof header1);
1313         if (rbytes != sizeof header1) {
1314                 /* we don't want to fail here. */
1315 #ifndef SMALL
1316                 if (fflag)
1317                         goto lose;
1318 #endif
1319                 if (rbytes == -1)
1320                         maybe_warn("can't read %s", file);
1321                 else
1322                         goto unexpected_EOF;
1323                 goto lose;
1324         }
1325
1326         method = file_gettype(header1);
1327
1328 #ifndef SMALL
1329         if (fflag == 0 && method == FT_UNKNOWN) {
1330                 maybe_warnx("%s: not in gzip format", file);
1331                 goto lose;
1332         }
1333
1334 #endif
1335
1336 #ifndef SMALL
1337         if (method == FT_GZIP && Nflag) {
1338                 unsigned char ts[4];    /* timestamp */
1339
1340                 rv = pread(fd, ts, sizeof ts, GZIP_TIMESTAMP);
1341                 if (rv >= 0 && rv < (ssize_t)(sizeof ts))
1342                         goto unexpected_EOF;
1343                 if (rv == -1) {
1344                         if (!fflag)
1345                                 maybe_warn("can't read %s", file);
1346                         goto lose;
1347                 }
1348                 timestamp = ts[3] << 24 | ts[2] << 16 | ts[1] << 8 | ts[0];
1349
1350                 if (header1[3] & ORIG_NAME) {
1351                         rbytes = pread(fd, name, sizeof name, GZIP_ORIGNAME);
1352                         if (rbytes < 0) {
1353                                 maybe_warn("can't read %s", file);
1354                                 goto lose;
1355                         }
1356                         if (name[0] != 0) {
1357                                 /* preserve original directory name */
1358                                 char *dp = strrchr(file, '/');
1359                                 if (dp == NULL)
1360                                         dp = file;
1361                                 else
1362                                         dp++;
1363                                 snprintf(outfile, outsize, "%.*s%.*s",
1364                                                 (int) (dp - file), 
1365                                                 file, (int) rbytes, name);
1366                         }
1367                 }
1368         }
1369 #endif
1370         lseek(fd, 0, SEEK_SET);
1371
1372         if (cflag == 0 || lflag) {
1373                 if (fstat(fd, &isb) != 0)
1374                         goto lose;
1375 #ifndef SMALL
1376                 if (isb.st_nlink > 1 && lflag == 0 && fflag == 0) {
1377                         maybe_warnx("%s has %d other links -- skipping",
1378                             file, isb.st_nlink - 1);
1379                         goto lose;
1380                 }
1381                 if (nflag == 0 && timestamp)
1382                         isb.st_mtime = timestamp;
1383                 if (check_outfile(outfile) == 0)
1384                         goto lose;
1385 #endif
1386         }
1387
1388         if (cflag == 0 && lflag == 0) {
1389                 zfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0600);
1390                 if (zfd == STDOUT_FILENO) {
1391                         /* We won't close STDOUT_FILENO later... */
1392                         zfd = dup(zfd);
1393                         close(STDOUT_FILENO);
1394                 }
1395                 if (zfd == -1) {
1396                         maybe_warn("can't open %s", outfile);
1397                         goto lose;
1398                 }
1399         } else
1400                 zfd = STDOUT_FILENO;
1401
1402 #ifndef NO_BZIP2_SUPPORT
1403         if (method == FT_BZIP2) {
1404
1405                 /* XXX */
1406                 if (lflag) {
1407                         maybe_warnx("no -l with bzip2 files");
1408                         goto lose;
1409                 }
1410
1411                 size = unbzip2(fd, zfd, NULL, 0, NULL);
1412         } else
1413 #endif
1414
1415 #ifndef NO_COMPRESS_SUPPORT
1416         if (method == FT_Z) {
1417                 FILE *in, *out;
1418
1419                 /* XXX */
1420                 if (lflag) {
1421                         maybe_warnx("no -l with Lempel-Ziv files");
1422                         goto lose;
1423                 }
1424
1425                 if ((in = zdopen(fd)) == NULL) {
1426                         maybe_warn("zdopen for read: %s", file);
1427                         goto lose;
1428                 }
1429
1430                 out = fdopen(dup(zfd), "w");
1431                 if (out == NULL) {
1432                         maybe_warn("fdopen for write: %s", outfile);
1433                         fclose(in);
1434                         goto lose;
1435                 }
1436
1437                 size = zuncompress(in, out, NULL, 0, NULL);
1438                 /* need to fclose() if ferror() is true... */
1439                 if (ferror(in) | fclose(in)) {
1440                         maybe_warn("failed infile fclose");
1441                         unlink(outfile);
1442                         (void)fclose(out);
1443                 }
1444                 if (fclose(out) != 0) {
1445                         maybe_warn("failed outfile fclose");
1446                         unlink(outfile);
1447                         goto lose;
1448                 }
1449         } else
1450 #endif
1451
1452 #ifndef NO_PACK_SUPPORT
1453         if (method == FT_PACK) {
1454                 if (lflag) {
1455                         maybe_warnx("no -l with packed files");
1456                         goto lose;
1457                 }
1458
1459                 size = unpack(fd, zfd, NULL, 0, NULL);
1460         } else
1461 #endif
1462
1463 #ifndef SMALL
1464         if (method == FT_UNKNOWN) {
1465                 if (lflag) {
1466                         maybe_warnx("no -l for unknown filetypes");
1467                         goto lose;
1468                 }
1469                 size = cat_fd(NULL, 0, NULL, fd);
1470         } else
1471 #endif
1472         {
1473                 if (lflag) {
1474                         print_list(fd, isb.st_size, outfile, isb.st_mtime);
1475                         close(fd);
1476                         return -1;      /* XXX */
1477                 }
1478
1479                 size = gz_uncompress(fd, zfd, NULL, 0, NULL, file);
1480         }
1481
1482         if (close(fd) != 0)
1483                 maybe_warn("couldn't close input");
1484         if (zfd != STDOUT_FILENO && close(zfd) != 0)
1485                 maybe_warn("couldn't close output");
1486
1487         if (size == -1) {
1488                 if (cflag == 0)
1489                         unlink(outfile);
1490                 maybe_warnx("%s: uncompress failed", file);
1491                 return -1;
1492         }
1493
1494         /* if testing, or we uncompressed to stdout, this is all we need */
1495 #ifndef SMALL
1496         if (tflag)
1497                 return size;
1498 #endif
1499         /* if we are uncompressing to stdin, don't remove the file. */
1500         if (cflag)
1501                 return size;
1502
1503         /*
1504          * if we create a file...
1505          */
1506         /*
1507          * if we can't stat the file don't remove the file.
1508          */
1509
1510         ofd = open(outfile, O_RDWR, 0);
1511         if (ofd == -1) {
1512                 maybe_warn("couldn't open (leaving original): %s",
1513                            outfile);
1514                 return -1;
1515         }
1516         if (fstat(ofd, &osb) != 0) {
1517                 maybe_warn("couldn't stat (leaving original): %s",
1518                            outfile);
1519                 close(ofd);
1520                 return -1;
1521         }
1522         if (osb.st_size != size) {
1523                 maybe_warnx("stat gave different size: %" PRIdOFF
1524                                 " != %" PRIdOFF " (leaving original)",
1525                                 size, osb.st_size);
1526                 close(ofd);
1527                 unlink(outfile);
1528                 return -1;
1529         }
1530         unlink_input(file, &isb);
1531 #ifndef SMALL
1532         copymodes(ofd, &isb, outfile);
1533 #endif
1534         close(ofd);
1535         return size;
1536
1537     unexpected_EOF:
1538         maybe_warnx("%s: unexpected end of file", file);
1539     lose:
1540         if (fd != -1)
1541                 close(fd);
1542         if (zfd != -1 && zfd != STDOUT_FILENO)
1543                 close(fd);
1544         return -1;
1545 }
1546
1547 #ifndef SMALL
1548 static off_t
1549 cat_fd(unsigned char * prepend, size_t count, off_t *gsizep, int fd)
1550 {
1551         char buf[BUFLEN];
1552         off_t in_tot;
1553         ssize_t w;
1554
1555         in_tot = count;
1556         w = write(STDOUT_FILENO, prepend, count);
1557         if (w == -1 || (size_t)w != count) {
1558                 maybe_warn("write to stdout");
1559                 return -1;
1560         }
1561         for (;;) {
1562                 ssize_t rv;
1563
1564                 rv = read(fd, buf, sizeof buf);
1565                 if (rv == 0)
1566                         break;
1567                 if (rv < 0) {
1568                         maybe_warn("read from fd %d", fd);
1569                         break;
1570                 }
1571
1572                 if (write(STDOUT_FILENO, buf, rv) != rv) {
1573                         maybe_warn("write to stdout");
1574                         break;
1575                 }
1576                 in_tot += rv;
1577         }
1578
1579         if (gsizep)
1580                 *gsizep = in_tot;
1581         return (in_tot);
1582 }
1583 #endif
1584
1585 static void
1586 handle_stdin(void)
1587 {
1588         unsigned char header1[4];
1589         off_t usize, gsize;
1590         enum filetype method;
1591         ssize_t bytes_read;
1592 #ifndef NO_COMPRESS_SUPPORT
1593         FILE *in;
1594 #endif
1595
1596 #ifndef SMALL
1597         if (fflag == 0 && lflag == 0 && isatty(STDIN_FILENO)) {
1598                 maybe_warnx("standard input is a terminal -- ignoring");
1599                 return;
1600         }
1601 #endif
1602
1603         if (lflag) {
1604                 struct stat isb;
1605
1606                 /* XXX could read the whole file, etc. */
1607                 if (fstat(STDIN_FILENO, &isb) < 0) {
1608                         maybe_warn("fstat");
1609                         return;
1610                 }
1611                 print_list(STDIN_FILENO, isb.st_size, "stdout", isb.st_mtime);
1612                 return;
1613         }
1614
1615         bytes_read = read_retry(STDIN_FILENO, header1, sizeof header1);
1616         if (bytes_read == -1) {
1617                 maybe_warn("can't read stdin");
1618                 return;
1619         } else if (bytes_read != sizeof(header1)) {
1620                 maybe_warnx("(stdin): unexpected end of file");
1621                 return;
1622         }
1623
1624         method = file_gettype(header1);
1625         switch (method) {
1626         default:
1627 #ifndef SMALL
1628                 if (fflag == 0) {
1629                         maybe_warnx("unknown compression format");
1630                         return;
1631                 }
1632                 usize = cat_fd(header1, sizeof header1, &gsize, STDIN_FILENO);
1633                 break;
1634 #endif
1635         case FT_GZIP:
1636                 usize = gz_uncompress(STDIN_FILENO, STDOUT_FILENO, 
1637                               header1, sizeof header1, &gsize, "(stdin)");
1638                 break;
1639 #ifndef NO_BZIP2_SUPPORT
1640         case FT_BZIP2:
1641                 usize = unbzip2(STDIN_FILENO, STDOUT_FILENO,
1642                                 header1, sizeof header1, &gsize);
1643                 break;
1644 #endif
1645 #ifndef NO_COMPRESS_SUPPORT
1646         case FT_Z:
1647                 if ((in = zdopen(STDIN_FILENO)) == NULL) {
1648                         maybe_warnx("zopen of stdin");
1649                         return;
1650                 }
1651
1652                 usize = zuncompress(in, stdout, header1, sizeof header1, &gsize);
1653                 fclose(in);
1654                 break;
1655 #endif
1656 #ifndef NO_PACK_SUPPORT
1657         case FT_PACK:
1658                 usize = unpack(STDIN_FILENO, STDOUT_FILENO,
1659                                (char *)header1, sizeof header1, &gsize);
1660                 break;
1661 #endif
1662         }
1663
1664 #ifndef SMALL
1665         if (vflag && !tflag && usize != -1 && gsize != -1)
1666                 print_verbage(NULL, NULL, usize, gsize);
1667         if (vflag && tflag)
1668                 print_test("(stdin)", usize != -1);
1669 #endif 
1670
1671 }
1672
1673 static void
1674 handle_stdout(void)
1675 {
1676         off_t gsize, usize;
1677         struct stat sb;
1678         time_t systime;
1679         uint32_t mtime;
1680         int ret;
1681
1682 #ifndef SMALL
1683         if (fflag == 0 && isatty(STDOUT_FILENO)) {
1684                 maybe_warnx("standard output is a terminal -- ignoring");
1685                 return;
1686         }
1687 #endif
1688         /* If stdin is a file use it's mtime, otherwise use current time */
1689         ret = fstat(STDIN_FILENO, &sb);
1690
1691 #ifndef SMALL
1692         if (ret < 0) {
1693                 maybe_warn("Can't stat stdin");
1694                 return;
1695         }
1696 #endif
1697
1698         if (S_ISREG(sb.st_mode))
1699                 mtime = (uint32_t)sb.st_mtime;
1700         else {
1701                 systime = time(NULL);
1702 #ifndef SMALL
1703                 if (systime == -1) {
1704                         maybe_warn("time");
1705                         return;
1706                 }
1707 #endif
1708                 mtime = (uint32_t)systime;
1709         }
1710
1711         usize = gz_compress(STDIN_FILENO, STDOUT_FILENO, &gsize, "", mtime);
1712 #ifndef SMALL
1713         if (vflag && !tflag && usize != -1 && gsize != -1)
1714                 print_verbage(NULL, NULL, usize, gsize);
1715 #endif 
1716 }
1717
1718 /* do what is asked for, for the path name */
1719 static void
1720 handle_pathname(char *path)
1721 {
1722         char *opath = path, *s = NULL;
1723         ssize_t len;
1724         int slen;
1725         struct stat sb;
1726
1727         /* check for stdout/stdin */
1728         if (path[0] == '-' && path[1] == '\0') {
1729                 if (dflag)
1730                         handle_stdin();
1731                 else
1732                         handle_stdout();
1733                 return;
1734         }
1735
1736 retry:
1737         if (stat(path, &sb) != 0) {
1738                 /* lets try <path>.gz if we're decompressing */
1739                 if (dflag && s == NULL && errno == ENOENT) {
1740                         len = strlen(path);
1741                         slen = suffixes[0].ziplen;
1742                         s = malloc(len + slen + 1);
1743                         if (s == NULL)
1744                                 maybe_err("malloc");
1745                         memcpy(s, path, len);
1746                         memcpy(s + len, suffixes[0].zipped, slen + 1);
1747                         path = s;
1748                         goto retry;
1749                 }
1750                 maybe_warn("can't stat: %s", opath);
1751                 goto out;
1752         }
1753
1754         if (S_ISDIR(sb.st_mode)) {
1755 #ifndef SMALL
1756                 if (rflag)
1757                         handle_dir(path);
1758                 else
1759 #endif
1760                         maybe_warnx("%s is a directory", path);
1761                 goto out;
1762         }
1763
1764         if (S_ISREG(sb.st_mode))
1765                 handle_file(path, &sb);
1766         else
1767                 maybe_warnx("%s is not a regular file", path);
1768
1769 out:
1770         if (s)
1771                 free(s);
1772 }
1773
1774 /* compress/decompress a file */
1775 static void
1776 handle_file(char *file, struct stat *sbp)
1777 {
1778         off_t usize, gsize;
1779         char    outfile[PATH_MAX];
1780
1781         infile = file;
1782         if (dflag) {
1783                 usize = file_uncompress(file, outfile, sizeof(outfile));
1784 #ifndef SMALL
1785                 if (vflag && tflag)
1786                         print_test(file, usize != -1);
1787 #endif
1788                 if (usize == -1)
1789                         return;
1790                 gsize = sbp->st_size;
1791         } else {
1792                 gsize = file_compress(file, outfile, sizeof(outfile));
1793                 if (gsize == -1)
1794                         return;
1795                 usize = sbp->st_size;
1796         }
1797
1798
1799 #ifndef SMALL
1800         if (vflag && !tflag)
1801                 print_verbage(file, (cflag) ? NULL : outfile, usize, gsize);
1802 #endif
1803 }
1804
1805 #ifndef SMALL
1806 /* this is used with -r to recursively descend directories */
1807 static void
1808 handle_dir(char *dir)
1809 {
1810         char *path_argv[2];
1811         FTS *fts;
1812         FTSENT *entry;
1813
1814         path_argv[0] = dir;
1815         path_argv[1] = 0;
1816         fts = fts_open(path_argv, FTS_PHYSICAL | FTS_NOCHDIR, NULL);
1817         if (fts == NULL) {
1818                 warn("couldn't fts_open %s", dir);
1819                 return;
1820         }
1821
1822         while ((entry = fts_read(fts))) {
1823                 switch(entry->fts_info) {
1824                 case FTS_D:
1825                 case FTS_DP:
1826                         continue;
1827
1828                 case FTS_DNR:
1829                 case FTS_ERR:
1830                 case FTS_NS:
1831                         maybe_warn("%s", entry->fts_path);
1832                         continue;
1833                 case FTS_F:
1834                         handle_file(entry->fts_path, entry->fts_statp);
1835                 }
1836         }
1837         (void)fts_close(fts);
1838 }
1839 #endif
1840
1841 /* print a ratio - size reduction as a fraction of uncompressed size */
1842 static void
1843 print_ratio(off_t in, off_t out, FILE *where)
1844 {
1845         int percent10;  /* 10 * percent */
1846         off_t diff;
1847         char buff[8];
1848         int len;
1849
1850         diff = in - out/2;
1851         if (diff <= 0)
1852                 /*
1853                  * Output is more than double size of input! print -99.9%
1854                  * Quite possibly we've failed to get the original size.
1855                  */
1856                 percent10 = -999;
1857         else {
1858                 /*
1859                  * We only need 12 bits of result from the final division,
1860                  * so reduce the values until a 32bit division will suffice.
1861                  */
1862                 while (in > 0x100000) {
1863                         diff >>= 1;
1864                         in >>= 1;
1865                 }
1866                 if (in != 0)
1867                         percent10 = ((u_int)diff * 2000) / (u_int)in - 1000;
1868                 else
1869                         percent10 = 0;
1870         }
1871
1872         len = snprintf(buff, sizeof buff, "%2.2d.", percent10);
1873         /* Move the '.' to before the last digit */
1874         buff[len - 1] = buff[len - 2];
1875         buff[len - 2] = '.';
1876         fprintf(where, "%5s%%", buff);
1877 }
1878
1879 #ifndef SMALL
1880 /* print compression statistics, and the new name (if there is one!) */
1881 static void
1882 print_verbage(const char *file, const char *nfile, off_t usize, off_t gsize)
1883 {
1884         if (file)
1885                 fprintf(stderr, "%s:%s  ", file,
1886                     strlen(file) < 7 ? "\t\t" : "\t");
1887         print_ratio(usize, gsize, stderr);
1888         if (nfile)
1889                 fprintf(stderr, " -- replaced with %s", nfile);
1890         fprintf(stderr, "\n");
1891         fflush(stderr);
1892 }
1893
1894 /* print test results */
1895 static void
1896 print_test(const char *file, int ok)
1897 {
1898
1899         if (exit_value == 0 && ok == 0)
1900                 exit_value = 1;
1901         fprintf(stderr, "%s:%s  %s\n", file,
1902             strlen(file) < 7 ? "\t\t" : "\t", ok ? "OK" : "NOT OK");
1903         fflush(stderr);
1904 }
1905 #endif
1906
1907 /* print a file's info ala --list */
1908 /* eg:
1909   compressed uncompressed  ratio uncompressed_name
1910       354841      1679360  78.8% /usr/pkgsrc/distfiles/libglade-2.0.1.tar
1911 */
1912 static void
1913 print_list(int fd, off_t out, const char *outfile, time_t ts)
1914 {
1915         static int first = 1;
1916 #ifndef SMALL
1917         static off_t in_tot, out_tot;
1918         uint32_t crc = 0;
1919 #endif
1920         off_t in = 0, rv;
1921
1922         if (first) {
1923 #ifndef SMALL
1924                 if (vflag)
1925                         printf("method  crc     date  time  ");
1926 #endif
1927                 if (qflag == 0)
1928                         printf("  compressed uncompressed  "
1929                                "ratio uncompressed_name\n");
1930         }
1931         first = 0;
1932
1933         /* print totals? */
1934 #ifndef SMALL
1935         if (fd == -1) {
1936                 in = in_tot;
1937                 out = out_tot;
1938         } else
1939 #endif
1940         {
1941                 /* read the last 4 bytes - this is the uncompressed size */
1942                 rv = lseek(fd, (off_t)(-8), SEEK_END);
1943                 if (rv != -1) {
1944                         unsigned char buf[8];
1945                         uint32_t usize;
1946
1947                         rv = read(fd, (char *)buf, sizeof(buf));
1948                         if (rv == -1)
1949                                 maybe_warn("read of uncompressed size");
1950                         else if (rv != sizeof(buf))
1951                                 maybe_warnx("read of uncompressed size");
1952
1953                         else {
1954                                 usize = buf[4] | buf[5] << 8 |
1955                                         buf[6] << 16 | buf[7] << 24;
1956                                 in = (off_t)usize;
1957 #ifndef SMALL
1958                                 crc = buf[0] | buf[1] << 8 |
1959                                       buf[2] << 16 | buf[3] << 24;
1960 #endif
1961                         }
1962                 }
1963         }
1964
1965 #ifndef SMALL
1966         if (vflag && fd == -1)
1967                 printf("                            ");
1968         else if (vflag) {
1969                 char *date = ctime(&ts);
1970
1971                 /* skip the day, 1/100th second, and year */
1972                 date += 4;
1973                 date[12] = 0;
1974                 printf("%5s %08x %11s ", "defla"/*XXX*/, crc, date);
1975         }
1976         in_tot += in;
1977         out_tot += out;
1978 #endif
1979         printf("%12llu %12llu ", (unsigned long long)out, (unsigned long long)in);
1980         print_ratio(in, out, stdout);
1981         printf(" %s\n", outfile);
1982 }
1983
1984 /* display the usage of NetBSD gzip */
1985 static void
1986 usage(void)
1987 {
1988
1989         fprintf(stderr, "%s\n", gzip_version);
1990         fprintf(stderr,
1991 #ifdef SMALL
1992         "usage: %s [-" OPT_LIST "] [<file> [<file> ...]]\n",
1993 #else
1994         "usage: %s [-123456789acdfhklLNnqrtVv] [-S .suffix] [<file> [<file> ...]]\n"
1995         " -1 --fast            fastest (worst) compression\n"
1996         " -2 .. -8             set compression level\n"
1997         " -9 --best            best (slowest) compression\n"
1998         " -c --stdout          write to stdout, keep original files\n"
1999         "    --to-stdout\n"
2000         " -d --decompress      uncompress files\n"
2001         "    --uncompress\n"
2002         " -f --force           force overwriting & compress links\n"
2003         " -h --help            display this help\n"
2004         " -k --keep            don't delete input files during operation\n"
2005         " -l --list            list compressed file contents\n"
2006         " -N --name            save or restore original file name and time stamp\n"
2007         " -n --no-name         don't save original file name or time stamp\n"
2008         " -q --quiet           output no warnings\n"
2009         " -r --recursive       recursively compress files in directories\n"
2010         " -S .suf              use suffix .suf instead of .gz\n"
2011         "    --suffix .suf\n"
2012         " -t --test            test compressed file\n"
2013         " -V --version         display program version\n"
2014         " -v --verbose         print extra statistics\n",
2015 #endif
2016         getprogname());
2017         exit(0);
2018 }
2019
2020 /* display the version of NetBSD gzip */
2021 static void
2022 display_version(void)
2023 {
2024
2025         fprintf(stderr, "%s\n", gzip_version);
2026         exit(0);
2027 }
2028
2029 #ifndef NO_BZIP2_SUPPORT
2030 #include "unbzip2.c"
2031 #endif
2032 #ifndef NO_COMPRESS_SUPPORT
2033 #include "zuncompress.c"
2034 #endif
2035 #ifndef NO_PACK_SUPPORT
2036 #include "unpack.c"
2037 #endif
2038
2039 static ssize_t
2040 read_retry(int fd, void *buf, size_t sz)
2041 {
2042         char *cp = buf;
2043         size_t left = MIN(sz, (size_t) SSIZE_MAX);
2044
2045         while (left > 0) {
2046                 ssize_t ret;
2047
2048                 ret = read(fd, cp, left);
2049                 if (ret == -1) {
2050                         return ret;
2051                 } else if (ret == 0) {
2052                         break; /* EOF */
2053                 }
2054                 cp += ret;
2055                 left -= ret;
2056         }
2057
2058         return sz - left;
2059 }