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