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