Annotation of src/usr.bin/compress/main.c, Revision 1.37
1.37 ! millert 1: /* $OpenBSD: main.c,v 1.36 2003/07/15 19:01:46 millert Exp $ */
1.1 mickey 2:
1.18 mickey 3: static const char copyright[] =
4: "@(#) Copyright (c) 1992, 1993\n\
5: The Regents of the University of California. All rights reserved.\n"
6: "Copyright (c) 1997-2002 Michael Shalayeff\n";
1.1 mickey 7:
1.18 mickey 8: static const char license[] =
9: "\n"
10: " Redistribution and use in source and binary forms, with or without\n"
11: " modification, are permitted provided that the following conditions\n"
12: " are met:\n"
13: " 1. Redistributions of source code must retain the above copyright\n"
14: " notice, this list of conditions and the following disclaimer.\n"
15: " 2. Redistributions in binary form must reproduce the above copyright\n"
16: " notice, this list of conditions and the following disclaimer in the\n"
17: " documentation and/or other materials provided with the distribution.\n"
1.23 deraadt 18: " 3. Neither the name of the University nor the names of its contributors\n"
19: " may be used to endorse or promote products derived from this software\n"
20: " without specific prior written permission.\n"
1.18 mickey 21: "\n"
22: " THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n"
23: " IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n"
24: " OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n"
25: " IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,\n"
26: " INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n"
27: " (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n"
28: " SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n"
29: " HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n"
30: " STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\n"
31: " IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF\n"
32: " THE POSSIBILITY OF SUCH DAMAGE.\n";
1.1 mickey 33:
34: #ifndef lint
35: #if 0
36: static char sccsid[] = "@(#)compress.c 8.2 (Berkeley) 1/7/94";
37: #else
1.37 ! millert 38: static const char main_rcsid[] = "$OpenBSD: main.c,v 1.36 2003/07/15 19:01:46 millert Exp $";
1.1 mickey 39: #endif
40: #endif /* not lint */
41:
42: #include <sys/param.h>
43: #include <sys/time.h>
44: #include <sys/stat.h>
45:
1.18 mickey 46: #include <getopt.h>
1.1 mickey 47: #include <err.h>
48: #include <errno.h>
1.19 millert 49: #include <fts.h>
1.37 ! millert 50: #include <libgen.h>
1.1 mickey 51: #include <stdio.h>
52: #include <stdlib.h>
53: #include <string.h>
54: #include <unistd.h>
55: #include <fcntl.h>
56: #include <paths.h>
57: #include "compress.h"
58:
59: #define min(a,b) ((a) < (b)? (a) : (b))
60:
1.18 mickey 61: int pipin, force, verbose, testmode, list, nosave;
62: int savename, recurse;
1.37 ! millert 63: int cat, decomp;
1.1 mickey 64: extern char *__progname;
65:
1.18 mickey 66: const struct compressor {
1.1 mickey 67: char *name;
68: char *suffix;
1.33 millert 69: u_char *magic;
1.37 ! millert 70: void *(*open)(int, const char *, char *, int, u_int32_t, int);
1.17 millert 71: int (*read)(void *, char *, int);
72: int (*write)(void *, const char *, int);
1.37 ! millert 73: int (*close)(void *, struct z_info *);
1.1 mickey 74: } c_table[] = {
75: #define M_COMPRESS (&c_table[0])
1.37 ! millert 76: { "compress", ".Z", "\037\235", z_open, zread, zwrite, z_close },
1.1 mickey 77: #define M_DEFLATE (&c_table[1])
1.33 millert 78: { "deflate", ".gz", "\037\213", gz_open, gz_read, gz_write, gz_close },
1.18 mickey 79: #if 0
80: #define M_LZH (&c_table[2])
1.33 millert 81: { "lzh", ".lzh", "\037\240", lzh_open, lzh_read, lzh_write, lzh_close },
1.18 mickey 82: #define M_ZIP (&c_table[3])
1.33 millert 83: { "zip", ".zip", "PK", zip_open, zip_read, zip_write, zip_close },
1.18 mickey 84: #define M_PACK (&c_table[4])
1.33 millert 85: { "pack", ".pak", "\037\036", pak_open, pak_read, pak_write, pak_close },
1.18 mickey 86: #endif
1.1 mickey 87: { NULL }
88: };
89:
1.18 mickey 90: int permission(const char *);
91: void setfile(const char *, struct stat *);
1.31 millert 92: __dead void usage(int);
1.37 ! millert 93: int compress(const char *, char *, const struct compressor *,
1.25 deraadt 94: int, struct stat *);
1.37 ! millert 95: int decompress(const char *, char *, const struct compressor *,
1.25 deraadt 96: int, struct stat *);
1.37 ! millert 97: const struct compressor *check_method(int);
1.36 millert 98: const char *check_suffix(const char *);
99: char *set_outfile(const char *, char *, size_t);
1.37 ! millert 100: void list_stats(const char *, const struct compressor *, struct z_info *);
! 101: void verbose_info(const char *, off_t, off_t, u_int32_t);
1.18 mickey 102:
1.19 millert 103: #define OPTSTRING "123456789ab:cdfghlLnNOo:qrS:tvV"
1.18 mickey 104: const struct option longopts[] = {
105: { "ascii", no_argument, 0, 'a' },
106: { "stdout", no_argument, 0, 'c' },
107: { "to-stdout", no_argument, 0, 'c' },
108: { "decompress", no_argument, 0, 'd' },
109: { "uncompress", no_argument, 0, 'd' },
110: { "force", no_argument, 0, 'f' },
111: { "help", no_argument, 0, 'h' },
112: { "list", no_argument, 0, 'l' },
113: { "license", no_argument, 0, 'L' },
114: { "no-name", no_argument, 0, 'n' },
115: { "name", no_argument, 0, 'N' },
116: { "quiet", no_argument, 0, 'q' },
117: { "recursive", no_argument, 0, 'r' },
118: { "suffix", required_argument, 0, 'S' },
119: { "test", no_argument, 0, 't' },
120: { "verbose", no_argument, 0, 'v' },
121: { "version", no_argument, 0, 'V' },
122: { "fast", no_argument, 0, '1' },
123: { "best", no_argument, 0, '9' },
124: { NULL }
125: };
1.1 mickey 126:
127: int
1.24 deraadt 128: main(int argc, char *argv[])
1.1 mickey 129: {
1.19 millert 130: FTS *ftsp;
131: FTSENT *entry;
132: struct stat osb;
1.18 mickey 133: const struct compressor *method;
1.36 millert 134: const char *s;
135: char *p, *infile;
1.19 millert 136: char outfile[MAXPATHLEN], _infile[MAXPATHLEN], suffix[16];
1.18 mickey 137: char *nargv[512]; /* some estimate based on ARG_MAX */
1.37 ! millert 138: int bits, exists, oreg, ch, error, i, rc, oflag;
1.1 mickey 139:
1.19 millert 140: bits = cat = oflag = decomp = 0;
1.1 mickey 141: p = __progname;
142: if (p[0] == 'g') {
143: method = M_DEFLATE;
1.18 mickey 144: bits = 6;
1.1 mickey 145: p++;
146: } else
147: method = M_COMPRESS;
148:
149: decomp = 0;
150: if (!strcmp(p, "zcat")) {
151: decomp++;
1.20 mickey 152: cat = 1;
1.1 mickey 153: } else {
154: if (p[0] == 'u' && p[1] == 'n') {
155: p += 2;
156: decomp++;
157: }
158:
1.2 mickey 159: if (strcmp(p, "zip") &&
160: strcmp(p, "compress"))
1.1 mickey 161: errx(1, "unknown program name");
162: }
163:
1.18 mickey 164: strlcpy(suffix, method->suffix, sizeof(suffix));
165:
1.19 millert 166: nargv[0] = NULL;
1.36 millert 167: if ((p = getenv("GZIP")) != NULL) {
1.18 mickey 168: char *last;
169:
170: nargv[0] = *argv++;
1.36 millert 171: for (i = 1, (p = strtok_r(p, " ", &last)); p != NULL;
1.18 mickey 172: (p = strtok_r(NULL, " ", &last)), i++)
173: if (i < sizeof(nargv)/sizeof(nargv[1]) - argc - 1)
174: nargv[i] = p;
175: else {
176: errx(1, "GZIP is too long");
177: }
178: argc += i - 1;
179: while ((nargv[i++] = *argv++))
180: ;
181: argv = nargv;
182: }
183:
184: while ((ch = getopt_long(argc, argv, OPTSTRING, longopts, NULL)) != -1)
1.1 mickey 185: switch(ch) {
186: case '1':
187: case '2':
188: case '3':
189: case '4':
190: case '5':
191: case '6':
192: case '7':
193: case '8':
194: case '9':
195: method = M_DEFLATE;
1.18 mickey 196: strlcpy(suffix, method->suffix, sizeof(suffix));
1.1 mickey 197: bits = ch - '0';
198: break;
1.18 mickey 199: case 'a':
200: warnx("option -a is ignored on this system");
201: break;
1.1 mickey 202: case 'b':
203: bits = strtol(optarg, &p, 10);
1.7 denny 204: /*
205: * POSIX 1002.3 says 9 <= bits <= 14 for portable
206: * apps, but says the implementation may allow
207: * greater.
208: */
1.1 mickey 209: if (*p)
210: errx(1, "illegal bit count -- %s", optarg);
211: break;
212: case 'c':
1.20 mickey 213: cat = 1;
1.1 mickey 214: break;
215: case 'd': /* Backward compatible. */
216: decomp++;
217: break;
218: case 'f':
219: force++;
220: break;
221: case 'g':
222: method = M_DEFLATE;
1.18 mickey 223: strlcpy(suffix, method->suffix, sizeof(suffix));
224: bits = 6;
1.1 mickey 225: break;
226: case 'l':
227: list++;
1.37 ! millert 228: testmode++;
! 229: decomp++;
1.1 mickey 230: break;
231: case 'n':
1.37 ! millert 232: nosave = 1;
1.1 mickey 233: break;
234: case 'N':
1.37 ! millert 235: nosave = -1;
1.1 mickey 236: break;
237: case 'O':
238: method = M_COMPRESS;
1.18 mickey 239: strlcpy(suffix, method->suffix, sizeof(suffix));
1.1 mickey 240: break;
241: case 'o':
1.18 mickey 242: if (strlcpy(outfile, optarg,
243: sizeof(outfile)) >= sizeof(outfile))
244: errx(1, "-o argument is too long");
1.20 mickey 245: oflag = 1;
1.1 mickey 246: break;
247: case 'q':
248: verbose = -1;
249: break;
250: case 'S':
251: p = suffix;
252: if (optarg[0] != '.')
253: *p++ = '.';
1.18 mickey 254: strlcpy(p, optarg, sizeof(suffix) - (p - suffix));
255: p = optarg;
1.1 mickey 256: break;
257: case 't':
1.20 mickey 258: testmode = 1;
1.29 millert 259: decomp++;
1.1 mickey 260: break;
1.18 mickey 261: case 'V':
262: printf("%s\n%s\n%s\n", main_rcsid,
263: z_rcsid, gz_rcsid);
1.19 millert 264: exit (0);
1.1 mickey 265: case 'v':
266: verbose++;
267: break;
1.18 mickey 268: case 'L':
269: fputs(copyright, stderr);
270: fputs(license, stderr);
1.19 millert 271: exit (0);
1.18 mickey 272: case 'r':
1.19 millert 273: recurse++;
1.18 mickey 274: break;
275:
1.1 mickey 276: case 'h':
1.31 millert 277: usage(0);
278: break;
1.1 mickey 279: default:
1.31 millert 280: usage(1);
1.1 mickey 281: }
282: argc -= optind;
283: argv += optind;
284:
1.19 millert 285: if (argc == 0) {
286: if (nargv[0] == NULL)
287: argv = nargv;
288: /* XXX - make sure we don't oflow nargv in $GZIP case (millert) */
289: argv[0] = "/dev/stdin";
290: argv[1] = NULL;
291: pipin++;
1.20 mickey 292: cat = 1;
1.28 millert 293: } else {
294: for (i = 0; i < argc; i++) {
295: if (argv[i][0] == '-' && argv[i][1] == '\0') {
296: argv[i] = "/dev/stdin";
297: pipin++;
298: cat = 1;
299: }
300: }
1.19 millert 301: }
302: if (oflag && (recurse || argc > 1))
303: errx(1, "-o option may only be used with a single input file");
1.20 mickey 304:
1.34 mickey 305: if ((cat && argc) + testmode + oflag > 1)
1.19 millert 306: errx(1, "may not mix -o, -c, or -t options");
307:
308: if ((ftsp = fts_open(argv, FTS_PHYSICAL|FTS_NOCHDIR, 0)) == NULL)
309: err(1, NULL);
1.37 ! millert 310: for (rc = SUCCESS; (entry = fts_read(ftsp)) != NULL;) {
1.19 millert 311: infile = entry->fts_path;
312: switch (entry->fts_info) {
313: case FTS_D:
314: if (!recurse) {
315: warnx("%s is a directory: ignored",
316: infile);
317: fts_set(ftsp, entry, FTS_SKIP);
318: }
319: continue;
320: case FTS_DP:
321: continue;
322: case FTS_NS:
323: /*
324: * If file does not exist and has no suffix,
325: * tack on the default suffix and try that.
326: */
327: /* XXX - is overwriting fts_statp legal? (millert) */
328: if (entry->fts_errno == ENOENT &&
329: strchr(entry->fts_accpath, '.') == NULL &&
330: snprintf(_infile, sizeof(_infile), "%s%s", infile,
331: suffix) < sizeof(_infile) &&
332: stat(_infile, entry->fts_statp) == 0 &&
333: S_ISREG(entry->fts_statp->st_mode)) {
334: infile = _infile;
335: break;
336: }
337: case FTS_ERR:
338: case FTS_DNR:
339: warnx("%s: %s", infile, strerror(entry->fts_errno));
1.37 ! millert 340: rc = rc ? rc : WARNING;
1.19 millert 341: continue;
342: default:
1.20 mickey 343: if (!S_ISREG(entry->fts_statp->st_mode) && !pipin) {
1.27 millert 344: warnx("%s not a regular file%s",
1.28 millert 345: infile, cat ? "" : ": unchanged");
1.37 ! millert 346: rc = rc ? rc : WARNING;
1.19 millert 347: continue;
1.18 mickey 348: }
1.19 millert 349: break;
1.9 mickey 350: }
1.1 mickey 351:
1.36 millert 352: if (!decomp && !pipin && (s = check_suffix(infile)) != NULL) {
353: warnx("%s already has %s suffix -- unchanged",
354: infile, s);
1.37 ! millert 355: rc = rc ? rc : WARNING;
1.36 millert 356: continue;
357: }
358:
1.37 ! millert 359: if (cat)
1.22 deraadt 360: strlcpy(outfile, "/dev/stdout", sizeof outfile);
1.19 millert 361: else if (!oflag) {
362: if (decomp) {
1.35 millert 363: if (set_outfile(infile, outfile,
364: sizeof outfile) == NULL) {
1.19 millert 365: if (!recurse)
366: warnx("%s: unknown suffix: "
367: "ignored", infile);
368: continue;
369: }
370: } else {
371: if (snprintf(outfile, sizeof(outfile),
372: "%s%s", infile, suffix) >= sizeof(outfile)) {
373: warnx("%s%s: name too long",
374: infile, suffix);
375: continue;
376: }
377: }
1.1 mickey 378: }
379:
1.19 millert 380: exists = !stat(outfile, &osb);
381: if (!force && exists && S_ISREG(osb.st_mode) &&
1.37 ! millert 382: !permission(outfile)) {
! 383: rc = rc ? rc : WARNING;
1.1 mickey 384: continue;
1.37 ! millert 385: }
1.1 mickey 386:
1.19 millert 387: oreg = !exists || S_ISREG(osb.st_mode);
1.1 mickey 388:
1.37 ! millert 389: if (verbose > 0 && !pipin && !list)
1.1 mickey 390: fprintf(stderr, "%s:\t", infile);
391:
1.19 millert 392: error = (decomp ? decompress : compress)
393: (infile, outfile, method, bits, entry->fts_statp);
1.1 mickey 394:
1.37 ! millert 395: switch (error) {
! 396: case SUCCESS:
! 397: if (!cat && !testmode) {
1.19 millert 398: setfile(outfile, entry->fts_statp);
1.37 ! millert 399: if (!pipin && unlink(infile) && verbose >= 0)
1.18 mickey 400: warn("input: %s", infile);
1.1 mickey 401: }
1.37 ! millert 402: break;
! 403: case WARNING:
! 404: rc = rc ? rc : WARNING;
! 405: break;
! 406: default:
! 407: rc = FAILURE;
! 408: if (oreg && unlink(outfile) && errno != ENOENT &&
! 409: verbose >= 0) {
! 410: if (force)
! 411: warn("output: %s", outfile);
! 412: else
! 413: err(1, "output: %s", outfile);
! 414: }
! 415: break;
1.1 mickey 416: }
1.19 millert 417: }
1.37 ! millert 418: if (list)
! 419: list_stats(NULL, NULL, NULL);
1.1 mickey 420:
1.18 mickey 421: exit(rc);
1.1 mickey 422: }
423:
424: int
1.37 ! millert 425: compress(const char *in, char *out, const struct compressor *method,
1.24 deraadt 426: int bits, struct stat *sb)
1.1 mickey 427: {
1.18 mickey 428: u_char buf[Z_BUFSIZE];
1.37 ! millert 429: char *name;
! 430: int error, ifd, ofd, flags;
1.16 mpech 431: void *cookie;
432: ssize_t nr;
1.37 ! millert 433: u_int32_t mtime;
! 434: struct z_info info;
1.1 mickey 435:
1.37 ! millert 436: mtime = 0;
! 437: flags = 0;
! 438: error = SUCCESS;
! 439: name = NULL;
1.1 mickey 440: cookie = NULL;
1.3 mickey 441:
1.21 millert 442: if ((ifd = open(in, O_RDONLY)) < 0) {
443: if (verbose >= 0)
444: warn("%s", out);
1.37 ! millert 445: return (FAILURE);
1.21 millert 446: }
447:
1.3 mickey 448: if ((ofd = open(out, O_WRONLY|O_CREAT, S_IWUSR)) < 0) {
449: if (verbose >= 0)
450: warn("%s", out);
1.32 mickey 451: (void) close(ifd);
1.37 ! millert 452: return (FAILURE);
1.3 mickey 453: }
454:
1.4 mickey 455: if (method != M_COMPRESS && !force && isatty(ofd)) {
1.3 mickey 456: if (verbose >= 0)
457: warnx("%s: won't write compressed data to terminal",
1.25 deraadt 458: out);
1.32 mickey 459: (void) close(ofd);
460: (void) close(ifd);
1.37 ! millert 461: return (FAILURE);
1.3 mickey 462: }
1.1 mickey 463:
1.37 ! millert 464: if (!pipin && nosave <= 0) {
! 465: name = basename(in);
! 466: mtime = (u_int32_t)sb->st_mtime;
! 467: }
! 468: if ((cookie = (*method->open)(ofd, "w", name, bits, mtime, flags)) == NULL) {
1.32 mickey 469: if (verbose >= 0)
470: warn("%s", in);
471: (void) close(ofd);
472: (void) close(ifd);
1.37 ! millert 473: return (FAILURE);
1.32 mickey 474: }
1.1 mickey 475:
1.32 mickey 476: while ((nr = read(ifd, buf, sizeof(buf))) > 0)
477: if ((method->write)(cookie, buf, nr) != nr) {
478: if (verbose >= 0)
479: warn("%s", out);
1.37 ! millert 480: error = FAILURE;
1.32 mickey 481: break;
482: }
1.1 mickey 483:
1.32 mickey 484: if (!error && nr < 0) {
485: if (verbose >= 0)
1.1 mickey 486: warn("%s", in);
1.37 ! millert 487: error = FAILURE;
1.1 mickey 488: }
489:
1.37 ! millert 490: if ((method->close)(cookie, &info)) {
1.1 mickey 491: if (!error && verbose >= 0)
492: warn("%s", out);
1.37 ! millert 493: error = FAILURE;
1.1 mickey 494: }
495:
1.18 mickey 496: if (close(ifd)) {
497: if (!error && verbose >= 0)
1.32 mickey 498: warn("%s", in);
1.37 ! millert 499: error = FAILURE;
1.18 mickey 500: }
1.37 ! millert 501:
! 502: if (!force && info.total_out >= info.total_in) {
! 503: if (verbose > 0)
! 504: fprintf(stderr, "file would grow; left unmodified\n");
! 505: error = WARNING;
! 506: }
! 507:
! 508: if (!error && verbose > 0)
! 509: verbose_info(out, info.total_out, info.total_in, info.hlen);
1.32 mickey 510:
1.21 millert 511: return (error);
1.1 mickey 512: }
513:
1.18 mickey 514: const struct compressor *
1.37 ! millert 515: check_method(int fd)
1.1 mickey 516: {
1.18 mickey 517: const struct compressor *method;
1.33 millert 518: u_char magic[2];
1.1 mickey 519:
1.33 millert 520: if (read(fd, magic, sizeof(magic)) != 2)
521: return (NULL);
522: for (method = &c_table[0]; method->name != NULL; method++) {
523: if (magic[0] == method->magic[0] &&
524: magic[1] == method->magic[1])
525: return (method);
526: }
527: return (NULL);
1.1 mickey 528: }
529:
530: int
1.37 ! millert 531: decompress(const char *in, char *out, const struct compressor *method,
1.24 deraadt 532: int bits, struct stat *sb)
1.1 mickey 533: {
1.18 mickey 534: u_char buf[Z_BUFSIZE];
535: int error, ifd, ofd;
1.16 mpech 536: void *cookie;
537: ssize_t nr;
1.37 ! millert 538: struct z_info info;
1.1 mickey 539:
1.37 ! millert 540: error = SUCCESS;
1.1 mickey 541: cookie = NULL;
542:
1.3 mickey 543: if ((ifd = open(in, O_RDONLY)) < 0) {
544: if (verbose >= 0)
545: warn("%s", in);
546: return -1;
547: }
548:
549: if (!force && isatty(ifd)) {
550: if (verbose >= 0)
551: warnx("%s: won't read compressed data from terminal",
1.25 deraadt 552: in);
1.3 mickey 553: close (ifd);
554: return -1;
555: }
556:
1.37 ! millert 557: if ((method = check_method(ifd)) == NULL) {
1.3 mickey 558: if (verbose >= 0)
559: warnx("%s: unrecognized file format", in);
1.13 d 560: close (ifd);
1.3 mickey 561: return -1;
562: }
563:
1.37 ! millert 564: /* XXX - open constrains outfile to MAXPATHLEN so this is safe */
! 565: if ((cookie = (*method->open)(ifd, "r", nosave < 0 ? out : NULL,
! 566: bits, 0, 1)) == NULL) {
1.32 mickey 567: if (verbose >= 0)
568: warn("%s", in);
569: close (ifd);
1.37 ! millert 570: return (FAILURE);
1.32 mickey 571: }
572:
1.37 ! millert 573: if (testmode)
! 574: ofd = -1;
! 575: else if ((ofd = open(out, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR)) < 0) {
1.32 mickey 576: if (verbose >= 0)
577: warn("%s", in);
1.37 ! millert 578: (method->close)(cookie, NULL);
! 579: return (FAILURE);
1.32 mickey 580: }
581:
1.37 ! millert 582: while ((nr = (method->read)(cookie, buf, sizeof(buf))) > 0) {
! 583: if (ofd != -1 && write(ofd, buf, nr) != nr) {
1.21 millert 584: if (verbose >= 0)
1.32 mickey 585: warn("%s", out);
1.37 ! millert 586: error = FAILURE;
1.32 mickey 587: break;
1.21 millert 588: }
1.37 ! millert 589: }
1.1 mickey 590:
1.32 mickey 591: if (!error && nr < 0) {
592: if (verbose >= 0)
593: warnx("%s: %s", in,
594: errno == EINVAL ? "crc error" : strerror(errno));
1.37 ! millert 595: error = errno == EINVAL ? WARNING : FAILURE;
1.1 mickey 596: }
597:
1.37 ! millert 598: if ((method->close)(cookie, &info)) {
1.1 mickey 599: if (!error && verbose >= 0)
1.32 mickey 600: warnx("%s", in);
1.37 ! millert 601: error = FAILURE;
! 602: }
! 603:
! 604: if (nosave < 0) {
! 605: sb->st_mtimespec.tv_sec = info.mtime;
! 606: sb->st_mtimespec.tv_nsec = 0;
1.1 mickey 607: }
608:
1.37 ! millert 609: if (ofd != -1 && close(ofd)) {
1.1 mickey 610: if (!error && verbose >= 0)
1.18 mickey 611: warn("%s", out);
1.37 ! millert 612: error = FAILURE;
! 613: }
! 614:
! 615: if (!error) {
! 616: if (list) {
! 617: if (info.mtime == 0)
! 618: info.mtime = (u_int32_t)sb->st_mtime;
! 619: list_stats(pipin ? "stdout" : out, method, &info);
! 620: } else if (verbose > 0) {
! 621: verbose_info(out, info.total_in, info.total_out,
! 622: info.hlen);
! 623: }
1.1 mickey 624: }
625:
1.21 millert 626: return (error);
1.1 mickey 627: }
628:
629: void
1.24 deraadt 630: setfile(const char *name, struct stat *fs)
1.1 mickey 631: {
1.18 mickey 632: struct timeval tv[2];
1.1 mickey 633:
634: fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
635:
636: TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
637: TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
638: if (utimes(name, tv))
639: warn("utimes: %s", name);
640:
641: /*
642: * Changing the ownership probably won't succeed, unless we're root
643: * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
644: * the mode; current BSD behavior is to remove all setuid bits on
645: * chown. If chown fails, lose setuid/setgid bits.
646: */
647: if (chown(name, fs->st_uid, fs->st_gid)) {
648: if (errno != EPERM)
649: warn("chown: %s", name);
650: fs->st_mode &= ~(S_ISUID|S_ISGID);
651: }
652: if (chmod(name, fs->st_mode))
653: warn("chown: %s", name);
654:
1.8 millert 655: if (fs->st_flags && chflags(name, fs->st_flags))
1.1 mickey 656: warn("chflags: %s", name);
657: }
658:
659: int
1.24 deraadt 660: permission(const char *fname)
1.1 mickey 661: {
662: int ch, first;
663:
664: if (!isatty(fileno(stderr)))
665: return (0);
666: (void)fprintf(stderr, "overwrite %s? ", fname);
667: first = ch = getchar();
668: while (ch != '\n' && ch != EOF)
669: ch = getchar();
670: return (first == 'y');
1.35 millert 671: }
672:
673: /*
1.36 millert 674: * Check infile for a known suffix and return the suffix portion or NULL.
675: */
676: const char *
677: check_suffix(const char *infile)
678: {
679: int i;
680: char *suf, *sep, *separators = ".-_";
681: static char *suffixes[] = { "Z", "gz", "z", "tgz", "taz", NULL };
682:
683: for (sep = separators; *sep != '\0'; sep++) {
684: if ((suf = strrchr(infile, *sep)) == NULL)
685: continue;
686: suf++;
687:
688: for (i = 0; suffixes[i] != NULL; i++) {
689: if (strcmp(suf, suffixes[i]) == 0)
690: return (suf - 1);
691: }
692: }
693: return (NULL);
694: }
695:
696: /*
1.35 millert 697: * Set outfile based on the suffix. In most cases we just strip
698: * off the suffix but things like .tgz and .taz are special.
699: */
700: char *
1.36 millert 701: set_outfile(const char *infile, char *outfile, size_t osize)
1.35 millert 702: {
1.36 millert 703: const char *s;
704: char *cp;
705:
706: if ((s = check_suffix(infile)) == NULL)
1.35 millert 707: return (NULL);
708:
1.36 millert 709: (void)strlcpy(outfile, infile, osize);
710: cp = outfile + (s - infile) + 1;
711: /*
712: * Convert tgz and taz -> tar, else drop the suffix.
713: */
714: if (strcmp(cp, "tgz") == 0) {
715: cp[1] = 'a';
716: cp[2] = 'r';
717: } else if (strcmp(cp, "taz") == 0)
718: cp[2] = 'r';
719: else
720: cp[-1] = '\0';
721: return (outfile);
1.37 ! millert 722: }
! 723:
! 724: /*
! 725: * Print output for the -l option.
! 726: */
! 727: void
! 728: list_stats(const char *name, const struct compressor *method,
! 729: struct z_info *info)
! 730: {
! 731: static off_t compressed_total, uncompressed_total, header_total;
! 732: static u_int nruns;
! 733: char *timestr;
! 734:
! 735: if (nruns == 0) {
! 736: if (verbose >= 0) {
! 737: if (verbose > 0)
! 738: fputs("method crc date time ", stdout);
! 739: puts("compressed uncompr. ratio uncompressed_name");
! 740: }
! 741: }
! 742: nruns++;
! 743:
! 744: if (name != NULL) {
! 745: if (verbose > 0) {
! 746: timestr = ctime(&info->mtime) + 4;
! 747: timestr[12] = '\0';
! 748: printf("%.5s %08x %s ", method->name, info->crc, timestr);
! 749: }
! 750: printf("%9lld %9lld %4.1f%% %s\n",
! 751: (long long)(info->total_in + info->hlen),
! 752: (long long)info->total_out,
! 753: (info->total_out - info->total_in) *
! 754: 100.0 / info->total_out, name);
! 755: compressed_total += info->total_in;
! 756: uncompressed_total += info->total_out;
! 757: header_total += info->hlen;
! 758: } else if (verbose >= 0) {
! 759: if (nruns < 3) /* only do totals for > 1 files */
! 760: return;
! 761: if (verbose > 0)
! 762: fputs(" ", stdout);
! 763: printf("%9lld %9lld %4.1f%% (totals)\n",
! 764: (long long)(compressed_total + header_total),
! 765: (long long)uncompressed_total,
! 766: (uncompressed_total - compressed_total) *
! 767: 100.0 / uncompressed_total);
! 768: }
! 769: }
! 770:
! 771: void
! 772: verbose_info(const char *file, off_t compressed, off_t uncompressed,
! 773: u_int32_t hlen)
! 774: {
! 775: if (testmode) {
! 776: fputs("OK\n", stderr);
! 777: return;
! 778: }
! 779: if (!pipin) {
! 780: fprintf(stderr, "\t%4.1f%% -- replaced with %s\n",
! 781: (uncompressed - compressed) * 100.0 / uncompressed, file);
! 782: }
! 783: compressed += hlen;
! 784: fprintf(stderr, "%lld bytes in, %lld bytes out\n",
! 785: (long long)(decomp ? compressed : uncompressed),
! 786: (long long)(decomp ? uncompressed : compressed));
1.1 mickey 787: }
788:
1.31 millert 789: __dead void
790: usage(int status)
1.1 mickey 791: {
792: fprintf(stderr,
1.31 millert 793: "usage: %s [-cdfghOqrtvV] [-b bits] [-S suffix] [-[1-9]] [file ...]\n",
1.18 mickey 794: __progname);
1.31 millert 795: exit(status);
1.1 mickey 796: }