1 /* $OpenBSD: main.c,v 1.96 2019/06/28 13:35:00 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * Copyright (c) 1997-2002 Michael Shalayeff 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 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT, 24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30 * THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/time.h> 34 #include <sys/stat.h> 35 36 #include <getopt.h> 37 #include <err.h> 38 #include <errno.h> 39 #include <fts.h> 40 #include <libgen.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <stdbool.h> 44 #include <string.h> 45 #include <unistd.h> 46 #include <limits.h> 47 #include <fcntl.h> 48 #include <paths.h> 49 #include "compress.h" 50 51 #define min(a,b) ((a) < (b)? (a) : (b)) 52 53 int cat, decomp, pipin, force, verbose, testmode, list, recurse, storename; 54 extern char *__progname; 55 56 const struct compressor { 57 const char *name; 58 const char *suffix; 59 const u_char *magic; 60 const char *comp_opts; 61 const char *decomp_opts; 62 const char *cat_opts; 63 void *(*ropen)(int, char *, int); 64 int (*read)(void *, char *, int); 65 #ifndef SMALL 66 void *(*wopen)(int, char *, int, u_int32_t); 67 int (*write)(void *, const char *, int); 68 #endif 69 int (*close)(void *, struct z_info *, const char *, struct stat *); 70 } c_table[] = { 71 #define M_DEFLATE (&c_table[0]) 72 { 73 "deflate", 74 ".gz", 75 "\037\213", 76 "123456789ab:cdfhLlNnOo:qrS:tVv", 77 "cfhLlNno:qrtVv", 78 "fhqr", 79 gz_ropen, 80 gz_read, 81 #ifndef SMALL 82 gz_wopen, 83 gz_write, 84 #endif 85 gz_close 86 }, 87 #define M_COMPRESS (&c_table[1]) 88 #ifndef SMALL 89 { 90 "compress", 91 ".Z", 92 "\037\235", 93 "123456789ab:cdfghlNnOo:qrS:tv", 94 "cfhlNno:qrtv", 95 "fghqr", 96 z_ropen, 97 zread, 98 z_wopen, 99 zwrite, 100 z_close 101 }, 102 #endif /* SMALL */ 103 { NULL } 104 }; 105 106 #ifndef SMALL 107 const struct compressor null_method = { 108 "null", 109 ".nul", 110 "XX", 111 "123456789ab:cdfghlNnOo:qrS:tv", 112 "cfhlNno:qrtv", 113 "fghqr", 114 null_ropen, 115 null_read, 116 null_wopen, 117 null_write, 118 null_close 119 }; 120 #endif /* SMALL */ 121 122 int permission(const char *); 123 __dead void usage(int); 124 int docompress(const char *, char *, const struct compressor *, 125 int, struct stat *); 126 int dodecompress(const char *, char *, struct stat *); 127 const struct compressor *check_method(int); 128 const char *check_suffix(const char *); 129 char *set_outfile(const char *, char *, size_t); 130 void list_stats(const char *, const struct compressor *, struct z_info *); 131 void verbose_info(const char *, off_t, off_t, u_int32_t); 132 133 const struct option longopts[] = { 134 #ifndef SMALL 135 { "ascii", no_argument, 0, 'a' }, 136 { "stdout", no_argument, 0, 'c' }, 137 { "to-stdout", no_argument, 0, 'c' }, 138 { "decompress", no_argument, 0, 'd' }, 139 { "uncompress", no_argument, 0, 'd' }, 140 { "force", no_argument, 0, 'f' }, 141 { "help", no_argument, 0, 'h' }, 142 { "list", no_argument, 0, 'l' }, 143 { "license", no_argument, 0, 'L' }, 144 { "no-name", no_argument, 0, 'n' }, 145 { "name", no_argument, 0, 'N' }, 146 { "quiet", no_argument, 0, 'q' }, 147 { "recursive", no_argument, 0, 'r' }, 148 { "suffix", required_argument, 0, 'S' }, 149 { "test", no_argument, 0, 't' }, 150 { "verbose", no_argument, 0, 'v' }, 151 { "version", no_argument, 0, 'V' }, 152 { "fast", no_argument, 0, '1' }, 153 { "best", no_argument, 0, '9' }, 154 #endif /* SMALL */ 155 { NULL } 156 }; 157 158 int 159 main(int argc, char *argv[]) 160 { 161 FTS *ftsp; 162 FTSENT *entry; 163 const struct compressor *method; 164 const char *optstr, *s; 165 char *p, *infile; 166 char outfile[PATH_MAX], _infile[PATH_MAX], suffix[16]; 167 int bits, ch, error, rc, cflag, oflag; 168 169 if (pledge("stdio rpath wpath cpath fattr chown", NULL) == -1) 170 err(1, "pledge"); 171 172 bits = cflag = oflag = 0; 173 storename = -1; 174 p = __progname; 175 if (p[0] == 'g') { 176 method = M_DEFLATE; 177 bits = 6; 178 p++; 179 } else { 180 #ifdef SMALL 181 method = M_DEFLATE; 182 #else 183 method = M_COMPRESS; 184 #endif /* SMALL */ 185 } 186 optstr = method->comp_opts; 187 188 decomp = 0; 189 pmode = MODE_COMP; 190 if (!strcmp(p, "zcat")) { 191 decomp++; 192 cflag = 1; 193 pmode = MODE_CAT; 194 } else { 195 if (p[0] == 'u' && p[1] == 'n') { 196 p += 2; 197 decomp++; 198 pmode = MODE_DECOMP; 199 } 200 201 if (strcmp(p, "zip") && 202 strcmp(p, "compress")) 203 errx(1, "unknown program name"); 204 } 205 206 strlcpy(suffix, method->suffix, sizeof(suffix)); 207 208 if (method == M_DEFLATE && (p = getenv("GZIP")) != NULL) { 209 char *evbuf, *last, **nargv = NULL; 210 int argc_extra = 0, nargc = 0; 211 212 if ((evbuf = strdup(p)) == NULL) 213 err(1, NULL); 214 for ((p = strtok_r(evbuf, " ", &last)); p != NULL; 215 (p = strtok_r(NULL, " ", &last))) { 216 if (nargc + 1 >= argc_extra) { 217 argc_extra += 1024; 218 nargv = reallocarray(nargv, 219 argc + argc_extra + 1, sizeof(char *)); 220 if (nargv == NULL) 221 err(1, NULL); 222 } 223 nargv[++nargc] = p; 224 } 225 if (nargv != NULL) { 226 nargv[0] = *argv++; 227 while ((nargv[++nargc] = *argv++)) 228 ; 229 argv = nargv; 230 argc = nargc; 231 } 232 } 233 234 optstr += pmode; 235 while ((ch = getopt_long(argc, argv, optstr, longopts, NULL)) != -1) 236 switch (ch) { 237 case '1': 238 case '2': 239 case '3': 240 case '4': 241 case '5': 242 case '6': 243 case '7': 244 case '8': 245 case '9': 246 method = M_DEFLATE; 247 strlcpy(suffix, method->suffix, sizeof(suffix)); 248 bits = ch - '0'; 249 break; 250 case 'a': 251 warnx("option -a is ignored on this system"); 252 break; 253 case 'b': 254 bits = strtol(optarg, &p, 10); 255 /* 256 * POSIX 1002.3 says 9 <= bits <= 14 for portable 257 * apps, but says the implementation may allow 258 * greater. 259 */ 260 if (*p) 261 errx(1, "illegal bit count -- %s", optarg); 262 break; 263 case 'c': 264 cflag = 1; 265 break; 266 case 'd': /* Backward compatible. */ 267 decomp++; 268 break; 269 case 'f': 270 force++; 271 break; 272 case 'g': 273 method = M_DEFLATE; 274 strlcpy(suffix, method->suffix, sizeof(suffix)); 275 bits = 6; 276 break; 277 case 'l': 278 list++; 279 testmode = 1; 280 decomp++; 281 break; 282 case 'n': 283 storename = 0; 284 break; 285 case 'N': 286 storename = 1; 287 break; 288 #ifndef SMALL 289 case 'O': 290 method = M_COMPRESS; 291 strlcpy(suffix, method->suffix, sizeof(suffix)); 292 break; 293 #endif /* SMALL */ 294 case 'o': 295 if (strlcpy(outfile, optarg, 296 sizeof(outfile)) >= sizeof(outfile)) 297 errx(1, "-o argument is too long"); 298 oflag = 1; 299 break; 300 case 'q': 301 verbose = -1; 302 break; 303 case 'S': 304 p = suffix; 305 if (optarg[0] != '.') 306 *p++ = '.'; 307 strlcpy(p, optarg, sizeof(suffix) - (p - suffix)); 308 p = optarg; 309 break; 310 case 't': 311 testmode = 1; 312 decomp++; 313 break; 314 case 'V': 315 exit (0); 316 case 'v': 317 verbose++; 318 break; 319 case 'L': 320 exit (0); 321 case 'r': 322 recurse++; 323 break; 324 325 case 'h': 326 usage(0); 327 break; 328 default: 329 usage(1); 330 } 331 argc -= optind; 332 argv += optind; 333 334 if (cflag || testmode || (!oflag && argc == 0)) 335 if (pledge("stdio rpath", NULL) == -1) 336 err(1, "pledge"); 337 338 if (argc == 0) { 339 argv = calloc(2, sizeof(char *)); 340 if (argv == NULL) 341 err(1, NULL); 342 argv[0] = "-"; 343 argc = 1; 344 } 345 if (oflag && (recurse || argc > 1)) 346 errx(1, "-o option may only be used with a single input file"); 347 348 if ((cat && argc) + testmode + oflag > 1) 349 errx(1, "may not mix -o, -c, or -t options"); 350 /* 351 * By default, when compressing store the original name and timestamp 352 * in the header. Do not restore these when decompressing unless 353 * the -N option is given. 354 */ 355 if (storename == -1) 356 storename = !decomp; 357 358 if ((ftsp = fts_open(argv, FTS_PHYSICAL|FTS_NOCHDIR, 0)) == NULL) 359 err(1, NULL); 360 for (rc = SUCCESS; (entry = fts_read(ftsp)) != NULL;) { 361 cat = cflag; 362 pipin = 0; 363 infile = entry->fts_path; 364 if (infile[0] == '-' && infile[1] == '\0') { 365 infile = "stdin"; 366 pipin++; 367 if (!oflag) 368 cat = 1; 369 } 370 else 371 switch (entry->fts_info) { 372 case FTS_D: 373 if (!recurse) { 374 warnx("%s is a directory: ignored", 375 infile); 376 fts_set(ftsp, entry, FTS_SKIP); 377 } 378 continue; 379 case FTS_DP: 380 continue; 381 case FTS_NS: 382 /* 383 * If file does not exist and has no suffix, 384 * tack on the default suffix and try that. 385 */ 386 if (entry->fts_errno == ENOENT) { 387 p = strrchr(entry->fts_accpath, '.'); 388 if ((p == NULL || 389 strcmp(p, suffix) != 0) && 390 snprintf(_infile, sizeof(_infile), 391 "%s%s", infile, suffix) < 392 sizeof(_infile) && 393 stat(_infile, entry->fts_statp) == 394 0 && 395 S_ISREG(entry->fts_statp->st_mode)) { 396 infile = _infile; 397 break; 398 } 399 } 400 case FTS_ERR: 401 case FTS_DNR: 402 warnx("%s: %s", infile, 403 strerror(entry->fts_errno)); 404 rc = rc ? rc : WARNING; 405 continue; 406 default: 407 if (!S_ISREG(entry->fts_statp->st_mode) && 408 !(S_ISLNK(entry->fts_statp->st_mode) && 409 cat)) { 410 warnx("%s not a regular file%s", 411 infile, cat ? "" : ": unchanged"); 412 rc = rc ? rc : WARNING; 413 continue; 414 } 415 break; 416 } 417 418 if (!decomp && !pipin && (s = check_suffix(infile)) != NULL) { 419 warnx("%s already has %s suffix -- unchanged", 420 infile, s); 421 rc = rc ? rc : WARNING; 422 continue; 423 } 424 425 if (!oflag) { 426 if (cat) 427 strlcpy(outfile, "stdout", sizeof(outfile)); 428 else if (decomp) { 429 if (set_outfile(infile, outfile, 430 sizeof outfile) == NULL) { 431 if (!recurse) { 432 warnx("%s: unknown suffix: " 433 "ignored", infile); 434 rc = rc ? rc : WARNING; 435 } 436 continue; 437 } 438 } else { 439 if (snprintf(outfile, sizeof(outfile), 440 "%s%s", infile, suffix) >= sizeof(outfile)) { 441 warnx("%s%s: name too long", 442 infile, suffix); 443 rc = rc ? rc : WARNING; 444 continue; 445 } 446 } 447 } 448 449 if (verbose > 0 && !pipin && !list) 450 fprintf(stderr, "%s:\t", infile); 451 452 if (decomp) 453 error = dodecompress(infile, outfile, entry->fts_statp); 454 else 455 error = docompress(infile, outfile, method, bits, entry->fts_statp); 456 457 switch (error) { 458 case SUCCESS: 459 if (!cat && !testmode) { 460 if (!pipin && unlink(infile) && verbose >= 0) 461 warn("input: %s", infile); 462 } 463 break; 464 case WARNING: 465 rc = rc ? rc : WARNING; 466 break; 467 default: 468 rc = FAILURE; 469 break; 470 } 471 } 472 if (list) 473 list_stats(NULL, NULL, NULL); 474 fts_close(ftsp); 475 exit(rc); 476 } 477 478 int 479 docompress(const char *in, char *out, const struct compressor *method, 480 int bits, struct stat *sb) 481 { 482 #ifndef SMALL 483 u_char buf[Z_BUFSIZE]; 484 char *name; 485 int error, ifd, ofd, oreg; 486 void *cookie; 487 ssize_t nr; 488 u_int32_t mtime; 489 struct z_info info; 490 struct stat osb; 491 492 mtime = 0; 493 oreg = 0; 494 error = SUCCESS; 495 name = NULL; 496 cookie = NULL; 497 498 if (pipin) 499 ifd = dup(STDIN_FILENO); 500 else 501 ifd = open(in, O_RDONLY); 502 if (ifd == -1) { 503 if (verbose >= 0) 504 warn("%s", in); 505 return (FAILURE); 506 } 507 508 if (cat) 509 ofd = dup(STDOUT_FILENO); 510 else { 511 if (stat(out, &osb) == 0) { 512 oreg = S_ISREG(osb.st_mode); 513 if (!force && oreg && !permission(out)) { 514 (void) close(ifd); 515 return (WARNING); 516 } 517 } 518 ofd = open(out, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR); 519 } 520 if (ofd == -1) { 521 if (verbose >= 0) 522 warn("%s", out); 523 (void) close(ifd); 524 return (FAILURE); 525 } 526 527 if (method != M_COMPRESS && !force && isatty(ofd)) { 528 if (verbose >= 0) 529 warnx("%s: won't write compressed data to terminal", 530 out); 531 (void) close(ofd); 532 (void) close(ifd); 533 return (FAILURE); 534 } 535 536 if (!pipin && storename) { 537 name = basename(in); 538 mtime = (u_int32_t)sb->st_mtime; 539 } 540 if ((cookie = method->wopen(ofd, name, bits, mtime)) == NULL) { 541 if (verbose >= 0) 542 warn("%s", out); 543 if (oreg) 544 (void) unlink(out); 545 (void) close(ofd); 546 (void) close(ifd); 547 return (FAILURE); 548 } 549 550 while ((nr = read(ifd, buf, sizeof(buf))) > 0) 551 if (method->write(cookie, buf, nr) != nr) { 552 if (verbose >= 0) 553 warn("%s", out); 554 error = FAILURE; 555 break; 556 } 557 558 if (!error && nr < 0) { 559 if (verbose >= 0) 560 warn("%s", in); 561 error = FAILURE; 562 } 563 564 if (method->close(cookie, &info, out, sb)) { 565 if (!error && verbose >= 0) 566 warn("%s", out); 567 error = FAILURE; 568 } 569 570 if (close(ifd)) { 571 if (!error && verbose >= 0) 572 warn("%s", in); 573 error = FAILURE; 574 } 575 576 if (!force && !cat && info.total_out >= info.total_in) { 577 if (verbose > 0) 578 fprintf(stderr, "file would grow; left unmodified\n"); 579 (void) unlink(out); 580 error = WARNING; 581 } 582 583 if (error) { 584 if (oreg) 585 (void) unlink(out); 586 } else if (verbose > 0) 587 verbose_info(out, info.total_out, info.total_in, info.hlen); 588 589 return (error); 590 #else 591 warnx("compression not supported"); 592 return (FAILURE); 593 #endif 594 } 595 596 const struct compressor * 597 check_method(int fd) 598 { 599 const struct compressor *method; 600 u_char magic[2]; 601 602 if (read(fd, magic, sizeof(magic)) != 2) 603 return (NULL); 604 for (method = &c_table[0]; method->name != NULL; method++) { 605 if (magic[0] == method->magic[0] && 606 magic[1] == method->magic[1]) 607 return (method); 608 } 609 #ifndef SMALL 610 if (force && cat) { 611 null_magic[0] = magic[0]; 612 null_magic[1] = magic[1]; 613 return (&null_method); 614 } 615 #endif /* SMALL */ 616 return (NULL); 617 } 618 619 int 620 dodecompress(const char *in, char *out, struct stat *sb) 621 { 622 const struct compressor *method; 623 u_char buf[Z_BUFSIZE]; 624 char oldname[PATH_MAX]; 625 int error, oreg, ifd, ofd; 626 void *cookie; 627 ssize_t nr; 628 struct z_info info; 629 struct stat osb; 630 631 oreg = 0; 632 error = SUCCESS; 633 cookie = NULL; 634 635 if (pipin) 636 ifd = dup(STDIN_FILENO); 637 else 638 ifd = open(in, O_RDONLY); 639 if (ifd == -1) { 640 if (verbose >= 0) 641 warn("%s", in); 642 return -1; 643 } 644 645 if (!force && isatty(ifd)) { 646 if (verbose >= 0) 647 warnx("%s: won't read compressed data from terminal", 648 in); 649 close (ifd); 650 return -1; 651 } 652 653 if ((method = check_method(ifd)) == NULL) { 654 if (verbose >= 0) 655 warnx("%s: unrecognized file format", in); 656 close (ifd); 657 return -1; 658 } 659 660 /* XXX - open constrains outfile to PATH_MAX so this is safe */ 661 oldname[0] = '\0'; 662 if ((cookie = method->ropen(ifd, oldname, 1)) == NULL) { 663 if (verbose >= 0) 664 warn("%s", in); 665 close (ifd); 666 return (FAILURE); 667 } 668 if (storename && oldname[0] != '\0') { 669 char *oldbase = basename(oldname); 670 char *cp = strrchr(out, '/'); 671 if (cp != NULL) { 672 *(cp + 1) = '\0'; 673 strlcat(out, oldbase, PATH_MAX); 674 } else 675 strlcpy(out, oldbase, PATH_MAX); 676 cat = 0; /* XXX should -c override? */ 677 } 678 679 if (testmode) 680 ofd = -1; 681 else { 682 if (cat) 683 ofd = dup(STDOUT_FILENO); 684 else { 685 if (stat(out, &osb) == 0) { 686 oreg = S_ISREG(osb.st_mode); 687 if (!force && oreg && !permission(out)) { 688 (void) close(ifd); 689 return (WARNING); 690 } 691 } 692 ofd = open(out, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR); 693 } 694 if (ofd == -1) { 695 if (verbose >= 0) 696 warn("%s", in); 697 method->close(cookie, NULL, NULL, NULL); 698 return (FAILURE); 699 } 700 } 701 702 while ((nr = method->read(cookie, buf, sizeof(buf))) > 0) { 703 if (ofd != -1 && write(ofd, buf, nr) != nr) { 704 if (verbose >= 0) 705 warn("%s", out); 706 error = FAILURE; 707 break; 708 } 709 } 710 711 if (!error && nr < 0) { 712 if (verbose >= 0) 713 warnx("%s: %s", in, 714 errno == EINVAL ? "crc error" : strerror(errno)); 715 error = errno == EINVAL ? WARNING : FAILURE; 716 } 717 718 if (method->close(cookie, &info, NULL, NULL)) { 719 if (!error && verbose >= 0) 720 warnx("%s", in); 721 error = FAILURE; 722 } 723 if (storename && !cat) { 724 if (info.mtime != 0) { 725 sb->st_mtimespec.tv_sec = 726 sb->st_atimespec.tv_sec = info.mtime; 727 sb->st_mtimespec.tv_nsec = 728 sb->st_atimespec.tv_nsec = 0; 729 } else 730 storename = 0; /* no timestamp to restore */ 731 } 732 if (error == SUCCESS) 733 setfile(out, ofd, sb); 734 735 if (ofd != -1 && close(ofd)) { 736 if (!error && verbose >= 0) 737 warn("%s", out); 738 error = FAILURE; 739 } 740 741 if (!error) { 742 if (list) { 743 if (info.mtime == 0) 744 info.mtime = (u_int32_t)sb->st_mtime; 745 list_stats(out, method, &info); 746 } else if (verbose > 0) { 747 verbose_info(out, info.total_in, info.total_out, 748 info.hlen); 749 } 750 } 751 752 /* On error, clean up the file we created but preserve errno. */ 753 if (error && oreg) 754 unlink(out); 755 756 return (error); 757 } 758 759 void 760 setfile(const char *name, int fd, struct stat *fs) 761 { 762 struct timespec ts[2]; 763 764 if (name == NULL || cat || testmode) 765 return; 766 767 /* 768 * If input was a pipe we don't have any info to restore but we 769 * must set the mode since the current mode on the file is 0200. 770 */ 771 if (pipin) { 772 mode_t mask = umask(022); 773 fchmod(fd, DEFFILEMODE & ~mask); 774 umask(mask); 775 return; 776 } 777 778 /* 779 * Changing the ownership probably won't succeed, unless we're root 780 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid bits are not 781 * allowed. 782 */ 783 fs->st_mode &= ACCESSPERMS; 784 if (fchown(fd, fs->st_uid, fs->st_gid)) { 785 if (errno != EPERM) 786 warn("fchown: %s", name); 787 fs->st_mode &= ~(S_ISUID|S_ISGID); 788 } 789 if (fchmod(fd, fs->st_mode)) 790 warn("fchmod: %s", name); 791 792 if (fs->st_flags && fchflags(fd, fs->st_flags)) 793 warn("fchflags: %s", name); 794 795 ts[0] = fs->st_atim; 796 ts[1] = fs->st_mtim; 797 if (futimens(fd, ts)) 798 warn("futimens: %s", name); 799 } 800 801 int 802 permission(const char *fname) 803 { 804 int ch, first; 805 806 if (!isatty(fileno(stderr))) 807 return (0); 808 (void)fprintf(stderr, "overwrite %s? ", fname); 809 first = ch = getchar(); 810 while (ch != '\n' && ch != EOF) 811 ch = getchar(); 812 return (first == 'y'); 813 } 814 815 /* 816 * Check infile for a known suffix and return the suffix portion or NULL. 817 */ 818 const char * 819 check_suffix(const char *infile) 820 { 821 int i; 822 char *suf, *sep, *separators = ".-_"; 823 static char *suffixes[] = { "Z", "gz", "z", "tgz", "taz", NULL }; 824 825 for (sep = separators; *sep != '\0'; sep++) { 826 if ((suf = strrchr(infile, *sep)) == NULL) 827 continue; 828 suf++; 829 830 for (i = 0; suffixes[i] != NULL; i++) { 831 if (strcmp(suf, suffixes[i]) == 0) 832 return (suf - 1); 833 } 834 } 835 return (NULL); 836 } 837 838 /* 839 * Set outfile based on the suffix. In most cases we just strip 840 * off the suffix but things like .tgz and .taz are special. 841 */ 842 char * 843 set_outfile(const char *infile, char *outfile, size_t osize) 844 { 845 const char *s; 846 char *cp; 847 848 if ((s = check_suffix(infile)) == NULL) 849 return (NULL); 850 851 (void)strlcpy(outfile, infile, osize); 852 cp = outfile + (s - infile) + 1; 853 /* 854 * Convert tgz and taz -> tar, else drop the suffix. 855 */ 856 if (strcmp(cp, "tgz") == 0) { 857 cp[1] = 'a'; 858 cp[2] = 'r'; 859 } else if (strcmp(cp, "taz") == 0) 860 cp[2] = 'r'; 861 else 862 cp[-1] = '\0'; 863 return (outfile); 864 } 865 866 /* 867 * Print output for the -l option. 868 */ 869 void 870 list_stats(const char *name, const struct compressor *method, 871 struct z_info *info) 872 { 873 static off_t compressed_total, uncompressed_total, header_total; 874 static u_int nruns; 875 char *timestr; 876 877 if (nruns == 0) { 878 if (verbose >= 0) { 879 if (verbose > 0) 880 fputs("method crc date time ", stdout); 881 puts("compressed uncompressed ratio uncompressed_name"); 882 } 883 } 884 nruns++; 885 886 if (name != NULL) { 887 if (verbose > 0) { 888 time_t t = info->mtime; /* XXX 32 bit mtime */ 889 890 timestr = ctime(&t) + 4; 891 timestr[12] = '\0'; 892 if (timestr[4] == ' ') 893 timestr[4] = '0'; 894 printf("%-7.7s %08x %s ", method->name, info->crc, 895 timestr); 896 } 897 printf("%10lld %10lld %4.1f%% %s\n", 898 (long long)(info->total_in + info->hlen), 899 (long long)info->total_out, 900 ((long long)info->total_out - (long long)info->total_in) * 901 100.0 / info->total_out, name); 902 compressed_total += info->total_in; 903 uncompressed_total += info->total_out; 904 header_total += info->hlen; 905 } else if (verbose >= 0) { 906 if (nruns < 3) /* only do totals for > 1 files */ 907 return; 908 if (verbose > 0) 909 fputs(" ", stdout); 910 printf("%10lld %10lld %4.1f%% (totals)\n", 911 (long long)(compressed_total + header_total), 912 (long long)uncompressed_total, 913 (uncompressed_total - compressed_total) * 914 100.0 / uncompressed_total); 915 } 916 } 917 918 void 919 verbose_info(const char *file, off_t compressed, off_t uncompressed, 920 u_int32_t hlen) 921 { 922 if (testmode) { 923 fputs("OK\n", stderr); 924 return; 925 } 926 if (!pipin) { 927 fprintf(stderr, "\t%4.1f%% -- replaced with %s\n", 928 (uncompressed - compressed) * 100.0 / uncompressed, file); 929 } 930 compressed += hlen; 931 fprintf(stderr, "%lld bytes in, %lld bytes out\n", 932 (long long)(decomp ? compressed : uncompressed), 933 (long long)(decomp ? uncompressed : compressed)); 934 } 935 936 __dead void 937 usage(int status) 938 { 939 const bool gzip = (__progname[0] == 'g'); 940 941 switch (pmode) { 942 case MODE_COMP: 943 fprintf(stderr, "usage: %s [-123456789cdf%sh%slNnOqrt%sv] " 944 "[-b bits] [-o filename] [-S suffix]\n" 945 " %*s [file ...]\n", __progname, 946 !gzip ? "g" : "", gzip ? "L" : "", gzip ? "V" : "", 947 (int)strlen(__progname), ""); 948 break; 949 case MODE_DECOMP: 950 fprintf(stderr, "usage: %s [-cfh%slNnqrt%sv] [-o filename] " 951 "[file ...]\n", __progname, 952 gzip ? "L" : "", gzip ? "V" : ""); 953 break; 954 case MODE_CAT: 955 fprintf(stderr, "usage: %s [-f%shqr] [file ...]\n", 956 __progname, gzip ? "" : "g"); 957 break; 958 } 959 exit(status); 960 } 961