gzip.c revision 1.7 1 /* $NetBSD: gzip.c,v 1.7 2003/12/26 15:06:16 mrg Exp $ */
2
3 /*
4 * Copyright (c) 1997, 1998, 2003 Matthew R. Green
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 #ifndef lint
33 __COPYRIGHT("@(#) Copyright (c) 1997, 1998, 2003 Matthew R. Green\n\
34 All rights reserved.\n");
35 __RCSID("$NetBSD: gzip.c,v 1.7 2003/12/26 15:06:16 mrg Exp $");
36 #endif /* not lint */
37
38 /*
39 * gzip.c -- GPL free gzip using zlib.
40 *
41 * very minor portions of this code are (very loosely) derived from
42 * the minigzip.c in the zlib distribution.
43 *
44 * TODO:
45 * - handle .taz/.tgz files?
46 */
47
48 #include <sys/param.h>
49 #include <sys/stat.h>
50 #include <sys/time.h>
51
52 #include <unistd.h>
53 #include <stdio.h>
54 #include <string.h>
55 #include <stdlib.h>
56 #include <err.h>
57 #include <errno.h>
58 #include <fcntl.h>
59 #include <zlib.h>
60 #include <fts.h>
61 #include <libgen.h>
62 #include <stdarg.h>
63 #include <getopt.h>
64
65 #ifndef GZ_SUFFIX
66 # define GZ_SUFFIX ".gz"
67 #endif
68
69 #define BUFLEN 4096
70
71 #define ORIG_NAME 0x08
72
73 /* Define this if you have the NetBSD gzopenfull(3) extension to zlib(3) */
74 #define HAVE_ZLIB_GZOPENFULL 0
75
76 static const char gzip_version[] = "NetBSD gzip 2.0";
77
78 static char gzipflags[3]; /* `w' or `r', possible with [1-9] */
79 static int cflag; /* stdout mode */
80 static int dflag; /* decompress mode */
81 static int fflag; /* force mode */
82 static int lflag; /* list mode */
83 static int nflag; /* don't save name/timestamp */
84 static int Nflag; /* don't restore name/timestamp */
85 static int qflag; /* quiet mode */
86 static int rflag; /* recursive mode */
87 static int tflag; /* test */
88 static int vflag; /* verbose mode */
89 static const char *Sflag = GZ_SUFFIX; /* suffix (.gz) */
90
91 static int suffix_len; /* length of suffix; includes nul */
92 static char *newfile; /* name of newly created file */
93 static char *infile; /* name of file coming in */
94
95 static void maybe_err(int rv, const char *fmt, ...);
96 static void maybe_warn(const char *fmt, ...);
97 static void maybe_warnx(const char *fmt, ...);
98 static void usage(void);
99 static void display_version(void);
100 static void gz_compress(FILE *, gzFile);
101 static off_t gz_uncompress(gzFile, FILE *);
102 static void copymodes(const char *, struct stat *);
103 static ssize_t file_compress(char *);
104 static ssize_t file_uncompress(char *);
105 static void handle_pathname(char *);
106 static void handle_file(char *, struct stat *);
107 static void handle_dir(char *, struct stat *);
108 static void handle_stdin(void);
109 static void handle_stdout(void);
110 static void print_ratio(off_t, off_t, FILE *);
111 static void print_verbage(char *, char *, ssize_t, ssize_t);
112 static void print_test(char *, int);
113 static void print_list(int fd, off_t, const char *, time_t);
114
115 int main(int, char *p[]);
116
117 static const struct option longopts[] = {
118 { "stdout", no_argument, 0, 'c' },
119 { "to-stdout", no_argument, 0, 'c' },
120 { "decompress", no_argument, 0, 'd' },
121 { "uncompress", no_argument, 0, 'd' },
122 { "force", no_argument, 0, 'f' },
123 { "help", no_argument, 0, 'h' },
124 { "list", no_argument, 0, 'l' },
125 { "no-name", no_argument, 0, 'n' },
126 { "name", no_argument, 0, 'N' },
127 { "quiet", no_argument, 0, 'q' },
128 { "recursive", no_argument, 0, 'r' },
129 { "suffix", required_argument, 0, 'S' },
130 { "test", no_argument, 0, 't' },
131 { "verbose", no_argument, 0, 'v' },
132 { "version", no_argument, 0, 'V' },
133 { "fast", no_argument, 0, '1' },
134 { "best", no_argument, 0, '9' },
135 #if 0
136 /*
137 * This is what else GNU gzip implements. Maybe --list is
138 * useful, but --ascii isn't useful on NetBSD, and I don't
139 * care to have a --license.
140 */
141 { "ascii", no_argument, 0, 'a' },
142 { "license", no_argument, 0, 'L' },
143 #endif
144 };
145
146 int
147 main(int argc, char **argv)
148 {
149 const char *progname = getprogname();
150 int ch;
151
152 gzipflags[0] = 'w';
153 gzipflags[1] = '\0';
154
155 /*
156 * XXX
157 * handle being called `gunzip', `zcat' and `gzcat'
158 */
159 if (strcmp(progname, "gunzip") == 0)
160 dflag = 1;
161 else if (strcmp(progname, "zcat") == 0 ||
162 strcmp(progname, "gzcat") == 0)
163 dflag = cflag = 1;
164
165 while ((ch = getopt_long(argc, argv, "cdfhHlnNqrS:tvV123456789",
166 longopts, NULL)) != -1)
167 switch (ch) {
168 case 'c':
169 cflag = 1;
170 break;
171 case 'd':
172 dflag = 1;
173 break;
174 case 'f':
175 fflag = 1;
176 break;
177 case 'h':
178 case 'H':
179 usage();
180 /* NOTREACHED */
181 case 'l':
182 lflag = 1;
183 tflag = 1;
184 dflag = 1;
185 break;
186 case 'n':
187 nflag = 1;
188 Nflag = 0;
189 break;
190 case 'N':
191 nflag = 0;
192 Nflag = 1;
193 break;
194 case 'q':
195 qflag = 1;
196 break;
197 case 'r':
198 rflag = 1;
199 break;
200 case 'S':
201 Sflag = optarg;
202 break;
203 case 't':
204 cflag = 1;
205 tflag = 1;
206 dflag = 1;
207 break;
208 case 'v':
209 vflag = 1;
210 break;
211 case 'V':
212 display_version();
213 /* NOTREACHED */
214 case '1': case '2': case '3':
215 case '4': case '5': case '6':
216 case '7': case '8': case '9':
217 gzipflags[1] = (char)ch;
218 gzipflags[2] = '\0';
219 break;
220 }
221 argv += optind;
222 argc -= optind;
223 if (dflag)
224 gzipflags[0] = 'r';
225
226 suffix_len = strlen(Sflag) + 1;
227
228 if (argc == 0) {
229 if (dflag) /* stdin mode */
230 handle_stdin();
231 else /* stdout mode */
232 handle_stdout();
233 } else {
234 do {
235 handle_pathname(argv[0]);
236 } while (*++argv);
237 }
238 if (qflag == 0 && lflag && argc > 1)
239 print_list(-1, 0, "(totals)", 0);
240 exit(0);
241 }
242
243 /* maybe print a warning */
244 void
245 maybe_warn(const char *fmt, ...)
246 {
247 va_list ap;
248
249 if (qflag == 0) {
250 va_start(ap, fmt);
251 vwarn(fmt, ap);
252 va_end(ap);
253 }
254 }
255
256 void
257 maybe_warnx(const char *fmt, ...)
258 {
259 va_list ap;
260
261 if (qflag == 0) {
262 va_start(ap, fmt);
263 vwarnx(fmt, ap);
264 va_end(ap);
265 }
266 }
267
268 /* maybe print a warning */
269 void
270 maybe_err(int rv, const char *fmt, ...)
271 {
272 va_list ap;
273
274 if (qflag == 0) {
275 va_start(ap, fmt);
276 vwarn(fmt, ap);
277 va_end(ap);
278 }
279 exit(rv);
280 }
281
282 /* compress input to output then close both files */
283 static void
284 gz_compress(FILE *in, gzFile out)
285 {
286 char buf[BUFLEN];
287 ssize_t len;
288 int i;
289
290 for (;;) {
291 len = fread(buf, 1, sizeof(buf), in);
292 if (ferror(in))
293 maybe_err(1, "fread");
294 if (len == 0)
295 break;
296
297 if ((ssize_t)gzwrite(out, buf, len) != len)
298 maybe_err(1, gzerror(out, &i));
299 }
300 if (fclose(in) < 0)
301 maybe_err(1, "failed fclose");
302 if (gzclose(out) != Z_OK)
303 maybe_err(1, "failed gzclose");
304 }
305
306 /* uncompress input to output then close the input */
307 static off_t
308 gz_uncompress(gzFile in, FILE *out)
309 {
310 char buf[BUFLEN];
311 off_t size;
312 ssize_t len;
313 int i;
314
315 for (size = 0;;) {
316 len = gzread(in, buf, sizeof(buf));
317
318 if (len < 0) {
319 if (tflag) {
320 print_test(infile, 0);
321 return (0);
322 } else
323 maybe_err(1, gzerror(in, &i));
324 } else if (len == 0) {
325 if (tflag)
326 print_test(infile, 1);
327 break;
328 }
329
330 size += len;
331
332 /* don't write anything with -t */
333 if (tflag)
334 continue;
335
336 if (fwrite(buf, 1, (unsigned)len, out) != (ssize_t)len)
337 maybe_err(1, "failed fwrite");
338 }
339 if (gzclose(in) != Z_OK)
340 maybe_err(1, "failed gzclose");
341
342 return (size);
343 }
344
345 /*
346 * set the owner, mode, flags & utimes for a file
347 */
348 static void
349 copymodes(const char *file, struct stat *sbp)
350 {
351 struct timeval times[2];
352
353 /*
354 * If we have no info on the input, give this file some
355 * default values and return..
356 */
357 if (sbp == NULL) {
358 mode_t mask = umask(022);
359
360 (void)chmod(file, DEFFILEMODE & ~mask);
361 (void)umask(mask);
362 return;
363 }
364
365 /* if the chown fails, remove set-id bits as-per compress(1) */
366 if (chown(file, sbp->st_uid, sbp->st_gid) < 0) {
367 if (errno != EPERM)
368 maybe_warn("couldn't chown: %s", file);
369 sbp->st_mode &= ~(S_ISUID|S_ISGID);
370 }
371
372 /* we only allow set-id and the 9 normal permission bits */
373 sbp->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
374 if (chmod(file, sbp->st_mode) < 0)
375 maybe_warn("couldn't chmod: %s", file);
376
377 /* only try flags if they exist already */
378 if (sbp->st_flags != 0 && chflags(file, sbp->st_flags) < 0)
379 maybe_warn("couldn't chflags: %s", file);
380
381 TIMESPEC_TO_TIMEVAL(×[0], &sbp->st_atimespec);
382 TIMESPEC_TO_TIMEVAL(×[1], &sbp->st_mtimespec);
383 if (utimes(file, times) < 0)
384 maybe_warn("couldn't utimes: %s", file);
385 }
386
387 /*
388 * compress the given file: create a corresponding .gz file and remove the
389 * original.
390 */
391 static ssize_t
392 file_compress(char *file)
393 {
394 FILE *in;
395 gzFile out;
396 struct stat isb, osb;
397 char outfile[MAXPATHLEN];
398 ssize_t size;
399 u_int32_t mtime = 0;
400
401 if (cflag == 0) {
402 (void)strncpy(outfile, file, MAXPATHLEN - suffix_len);
403 outfile[MAXPATHLEN - suffix_len] = '\0';
404 (void)strlcat(outfile, Sflag, sizeof(outfile));
405
406 if (fflag == 0) {
407 if (stat(outfile, &osb) == 0) {
408 maybe_warnx("%s already exists -- skipping",
409 outfile);
410 goto lose;
411 }
412 }
413 if (stat(file, &isb) == 0) {
414 if (isb.st_nlink > 1) {
415 maybe_warnx("%s has %d other link%s -- "
416 "skipping", file, isb.st_nlink-1,
417 isb.st_nlink == 1 ? "" : "s");
418 goto lose;
419 }
420 if (nflag == 0)
421 mtime = (u_int32_t)isb.st_mtime;
422 }
423 }
424 in = fopen(file, "r");
425 if (in == 0)
426 maybe_err(1, "can't fopen %s", file);
427
428 if (cflag == 0) {
429 #if HAVE_ZLIB_GZOPENFULL
430 char *savename;
431
432 if (nflag == 0)
433 savename = basename(file);
434 else
435 savename = NULL;
436 out = gzopenfull(outfile, gzipflags, savename, mtime);
437 #else
438 out = gzopen(outfile, gzipflags);
439 #endif
440 } else
441 out = gzdopen(STDOUT_FILENO, gzipflags);
442
443 if (out == 0)
444 maybe_err(1, "can't gz%sopen %s",
445 cflag ? "d" : "",
446 cflag ? "stdout" : outfile);
447
448 gz_compress(in, out);
449
450 /*
451 * if we compressed to stdout, we don't know the size and
452 * we don't know the new file name, punt. if we can't stat
453 * the file, whine, otherwise set the size from the stat
454 * buffer. we only blow away the file if we can stat the
455 * output, just in case.
456 */
457 if (cflag == 0) {
458 if (stat(outfile, &osb) < 0) {
459 maybe_warn("couldn't stat: %s", outfile);
460 maybe_warnx("leaving original %s", file);
461 size = 0;
462 } else {
463 unlink(file);
464 size = osb.st_size;
465 }
466 newfile = outfile;
467 copymodes(outfile, &isb);
468 } else {
469 lose:
470 size = 0;
471 newfile = 0;
472 }
473
474 return (size);
475 }
476
477 /* uncompress the given file and remove the original */
478 static ssize_t
479 file_uncompress(char *file)
480 {
481 struct stat isb, osb;
482 char buf[PATH_MAX];
483 char *outfile = buf, *s;
484 FILE *out;
485 gzFile in;
486 off_t size;
487 ssize_t len = strlen(file);
488
489 if (cflag == 0 || lflag) {
490 s = &file[len - suffix_len + 1];
491 if (strncmp(s, Sflag, suffix_len) == 0) {
492 (void)strncpy(outfile, file, len - suffix_len + 1);
493 outfile[len - suffix_len + 1] = '\0';
494 } else
495 maybe_err(1, "unknown suffix %s", s);
496
497 /* gather the old name info */
498 if (Nflag || lflag) {
499 int fd;
500 char header1[10], name[PATH_MAX + 1];
501
502 fd = open(file, O_RDONLY);
503 if (fd < 0)
504 maybe_err(1, "can't open %s", file);
505 if (read(fd, header1, 10) != 10)
506 maybe_err(1, "can't read %s", file);
507
508 if (header1[3] & ORIG_NAME) {
509 size_t rbytes;
510 int i;
511
512 rbytes = read(fd, name, PATH_MAX + 1);
513 if (rbytes < 0)
514 maybe_err(1, "can't read %s", file);
515 for (i = 0; i < rbytes && name[i]; i++)
516 ;
517 if (i < rbytes) {
518 name[i] = 0;
519 /* now maybe merge old dirname */
520 if (strchr(outfile, '/') == 0)
521 outfile = name;
522 else {
523 char *dir = dirname(outfile);
524 if (asprintf(&outfile, "%s/%s",
525 dir, name) == -1)
526 maybe_err(1, "malloc");
527 }
528 }
529 }
530 close(fd);
531 }
532
533 if (fflag == 0) {
534 if (lflag == 0 && stat(outfile, &osb) == 0) {
535 maybe_warnx("%s already exists -- skipping",
536 outfile);
537 goto lose;
538 }
539 if (stat(file, &isb) == 0) {
540 if (isb.st_nlink > 1 && lflag == 0) {
541 maybe_warnx("%s has %d other link%s -- "
542 "skipping", file, isb.st_nlink-1,
543 isb.st_nlink == 1 ? "" : "s");
544 goto lose;
545 }
546 } else
547 goto lose;
548 }
549 }
550
551 if (lflag) {
552 int fd;
553
554 if ((fd = open(file, O_RDONLY)) == -1)
555 maybe_err(1, "open");
556 print_list(fd, isb.st_size, outfile, isb.st_mtime);
557 return 0; /* XXX */
558 }
559
560 in = gzopen(file, gzipflags);
561 if (in == NULL)
562 maybe_err(1, "can't gzopen %s", file);
563
564 if (cflag == 0) {
565 int fd;
566
567 /* Use open(2) directly to get a safe file. */
568 fd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0600);
569 if (fd < 0)
570 maybe_err(1, "can't open %s", outfile);
571 out = fdopen(fd, "w");
572 if (out == NULL)
573 maybe_err(1, "can't fdopen %s", outfile);
574 } else
575 out = stdout;
576
577 if ((size = gz_uncompress(in, out)) == 0)
578 goto lose;
579
580 /* if testing, or we uncompressed to stdout, this is all we need */
581 if (tflag || cflag)
582 return (size);
583
584 /*
585 * if we create a file...
586 */
587 if (cflag == 0) {
588 /* close the file */
589 if (fclose(out))
590 maybe_err(1, "failed fclose");
591
592 /*
593 * if we can't stat the file, or we are uncompressing to
594 * stdin, don't remove the file.
595 */
596 if (stat(outfile, &osb) < 0) {
597 maybe_warn("couldn't stat (leaving original): %s",
598 outfile);
599 goto lose;
600 }
601 if (osb.st_size != size) {
602 maybe_warn("stat gave different size: %llu != %llu "
603 "(leaving original)",
604 (unsigned long long)size,
605 (unsigned long long)osb.st_size);
606 goto lose;
607 }
608 newfile = outfile;
609 unlink(file);
610 size = osb.st_size;
611 copymodes(outfile, &isb);
612 }
613 return (size);
614
615 lose:
616 newfile = 0;
617 return (0);
618 }
619
620 static void
621 handle_stdin(void)
622 {
623 gzFile *file;
624
625 if (fflag == 0 && lflag == 0 && isatty(STDIN_FILENO)) {
626 maybe_warnx("standard input is a terminal -- ignoring");
627 return;
628 }
629
630 if (lflag) {
631 struct stat isb;
632
633 if (fstat(STDIN_FILENO, &isb) < 0)
634 maybe_err(1, "fstat");
635 print_list(STDIN_FILENO, isb.st_size, "stdout", isb.st_mtime);
636 return;
637 }
638
639 file = gzdopen(STDIN_FILENO, gzipflags);
640 if (file == NULL)
641 maybe_err(1, "can't gzdopen stdin");
642 gz_uncompress(file, stdout);
643 }
644
645 static void
646 handle_stdout(void)
647 {
648 gzFile *file;
649
650 if (fflag == 0 && isatty(STDOUT_FILENO)) {
651 maybe_warnx("standard output is a terminal -- ignoring");
652 return;
653 }
654 file = gzdopen(STDOUT_FILENO, gzipflags);
655 if (file == NULL)
656 maybe_err(1, "can't gzdopen stdout");
657 gz_compress(stdin, file);
658 }
659
660 /* do what is asked for, for the path name */
661 static void
662 handle_pathname(char *path)
663 {
664 char *opath = path, *s = 0;
665 ssize_t len;
666 struct stat sb;
667
668 /* check for stdout/stdin */
669 if (path[0] == '-' && path[1] == '\0') {
670 if (dflag)
671 handle_stdin();
672 else
673 handle_stdout();
674 }
675
676 retry:
677 if (stat(path, &sb) < 0) {
678 /* lets try <path>.gz if we're decompressing */
679 if (dflag && s == 0 && errno == ENOENT) {
680 len = strlen(path);
681 s = malloc(len + suffix_len);
682 if (s == 0)
683 maybe_err(1, "malloc");
684 memmove(s, path, len);
685 memmove(&s[len], Sflag, suffix_len);
686 path = s;
687 goto retry;
688 }
689 maybe_warn("can't stat: %s", opath);
690 goto out;
691 }
692
693 if (S_ISDIR(sb.st_mode)) {
694 if (rflag)
695 handle_dir(path, &sb);
696 else
697 maybe_warn("%s is a directory", path);
698 goto out;
699 }
700
701 if (S_ISREG(sb.st_mode))
702 handle_file(path, &sb);
703
704 out:
705 if (s)
706 free(s);
707 return;
708 }
709
710 /* compress/decompress a file */
711 static void
712 handle_file(char *file, struct stat *sbp)
713 {
714 ssize_t usize, gsize;
715
716 infile = file;
717 if (dflag) {
718 usize = file_uncompress(file);
719 if (usize == 0)
720 return;
721 gsize = sbp->st_size;
722 } else {
723 gsize = file_compress(file);
724 if (gsize == 0)
725 return;
726 usize = sbp->st_size;
727 }
728
729 if (vflag && !tflag)
730 print_verbage(file, cflag == 0 ? newfile : 0, usize, gsize);
731 }
732
733 /* this is used with -r to recursively decend directories */
734 static void
735 handle_dir(char *dir, struct stat *sbp)
736 {
737 char *path_argv[2];
738 FTS *fts;
739 FTSENT *entry;
740
741 path_argv[0] = dir;
742 path_argv[1] = 0;
743 fts = fts_open(path_argv, FTS_PHYSICAL, NULL);
744 if (fts == NULL) {
745 warn("couldn't fts_open %s", dir);
746 return;
747 }
748
749 while ((entry = fts_read(fts))) {
750 switch(entry->fts_info) {
751 case FTS_D:
752 case FTS_DP:
753 continue;
754
755 case FTS_DNR:
756 case FTS_ERR:
757 case FTS_NS:
758 maybe_warn("%s", entry->fts_path);
759 continue;
760 case FTS_F:
761 handle_file(entry->fts_name, entry->fts_statp);
762 }
763 }
764 (void)fts_close(fts);
765 }
766
767 /* print a ratio */
768 static void
769 print_ratio(off_t in, off_t out, FILE *where)
770 {
771 u_int64_t percent;
772
773 if (out == 0)
774 percent = 0;
775 else if (out < 1000 * 1000)
776 percent = 999 - (in * 1000) / out;
777 else
778 percent = 999 - in / (out / 1000);
779 fprintf(where, "%3lu.%1lu%%", (unsigned long)percent / 10UL,
780 (unsigned long)percent % 10);
781 }
782
783 /* print compression statistics, and the new name (if there is one!) */
784 static void
785 print_verbage(char *file, char *nfile, ssize_t usize, ssize_t gsize)
786 {
787 fprintf(stderr, "%s:%s ", file,
788 strlen(file) < 7 ? "\t\t" : "\t");
789 print_ratio(usize, gsize, stderr);
790 if (nfile)
791 fprintf(stderr, " -- replaced with %s", nfile);
792 fprintf(stderr, "\n");
793 fflush(stderr);
794 }
795
796 /* print test results */
797 static void
798 print_test(char *file, int ok)
799 {
800
801 fprintf(stderr, "%s:%s %s\n", file,
802 strlen(file) < 7 ? "\t\t" : "\t", ok ? "OK" : "NOT OK");
803 fflush(stderr);
804 }
805
806 /* print a file's info ala --list */
807 /* eg:
808 compressed uncompressed ratio uncompressed_name
809 354841 1679360 78.8% /usr/pkgsrc/distfiles/libglade-2.0.1.tar
810 */
811 static void
812 print_list(int fd, off_t in, const char *outfile, time_t ts)
813 {
814 static int first = 1;
815 static off_t in_tot, out_tot;
816 off_t out;
817 u_int32_t crc;
818 int rv;
819
820 if (first) {
821 if (vflag)
822 printf("method crc date time ");
823 if (qflag == 0)
824 printf(" compressed uncompressed "
825 "ratio uncompressed_name\n");
826 }
827 first = 0;
828
829 /* print totals? */
830 if (fd == -1) {
831 in = in_tot;
832 out = out_tot;
833 } else {
834 /* read the last 4 bytes - this is the uncompressed size */
835 rv = lseek(fd, (off_t)(-8), SEEK_END);
836 if (rv != -1) {
837 unsigned char buf[8];
838 u_int32_t usize;
839
840 if (read(fd, (char *)buf, sizeof(buf)) != sizeof(buf))
841 maybe_err(1, "read of uncompressed size");
842 crc = buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;
843 usize = buf[4] | buf[5] << 8 | buf[6] << 16 | buf[7] << 24;
844 out = (off_t)usize;
845 }
846 }
847
848 if (vflag && fd == -1)
849 printf(" ");
850 else if (vflag) {
851 char *date = ctime(&ts);
852
853 /* skip the day, 1/100th second, and year */
854 date += 4;
855 date[12] = 0;
856 printf("%5s %08x %11s ", "defla"/*XXX*/, crc, date);
857 }
858 printf("%12llu %12llu ", (unsigned long long)in, (unsigned long long)out);
859 print_ratio(in, out, stdout);
860 printf(" %s\n", outfile);
861 in_tot += in;
862 out_tot += out;
863 }
864
865 /* display the usage of NetBSD gzip */
866 static void
867 usage(void)
868 {
869
870 fprintf(stderr, "%s\n", gzip_version);
871 fprintf(stderr,
872 "Usage: %s [-cdfhnNqrStvV123456789] [<file> [<file> ...]]\n"
873 " -c --stdout write to stdout, keep original files\n"
874 " --to-stdout\n"
875 " -d --decompress uncompress files\n"
876 " --uncompress\n"
877 " -f --force force overwriting & compress links\n"
878 " -h --help display this help\n"
879 " -n --no-name don't save original file name or time stamp\n"
880 " -N --name save or restore original file name and time stamp\n"
881 " -q --quiet output no warnings\n"
882 " -r --recursive recursively compress files in directories\n"
883 " -S .suf use suffix .suf instead of .gz\n"
884 " --suffix .suf\n"
885 " -t --test test compressed file\n"
886 " -v --verbose print extra statistics\n"
887 " -V --version display program version\n"
888 " -1 --fast fastest (worst) compression\n"
889 " -2 .. -8 set compression level\n"
890 " -9 --best best (slowest) compression\n",
891 getprogname());
892 fflush(stderr);
893 exit(0);
894 }
895
896 /* display the version of NetBSD gzip */
897 static void
898 display_version(void)
899 {
900
901 fprintf(stderr, "%s\n", gzip_version);
902 fflush(stderr);
903 exit(0);
904 }
905