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