Annotation of src/usr.bin/compress/main.c, Revision 1.54
1.54 ! markus 1: /* $OpenBSD: main.c,v 1.53 2004/01/22 18:50:39 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.54 ! markus 39: static const char main_rcsid[] = "$OpenBSD: main.c,v 1.53 2004/01/22 18:50:39 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.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[] = {
1.45 tedu 75: #define M_DEFLATE (&c_table[0])
76: { "deflate", ".gz", "\037\213", gz_open, gz_read, gz_write, gz_close },
77: #define M_COMPRESS (&c_table[1])
78: #ifndef SMALL
1.37 millert 79: { "compress", ".Z", "\037\235", z_open, zread, zwrite, z_close },
1.45 tedu 80: #endif /* SMALL */
1.18 mickey 81: #if 0
82: #define M_LZH (&c_table[2])
1.33 millert 83: { "lzh", ".lzh", "\037\240", lzh_open, lzh_read, lzh_write, lzh_close },
1.18 mickey 84: #define M_ZIP (&c_table[3])
1.33 millert 85: { "zip", ".zip", "PK", zip_open, zip_read, zip_write, zip_close },
1.18 mickey 86: #define M_PACK (&c_table[4])
1.33 millert 87: { "pack", ".pak", "\037\036", pak_open, pak_read, pak_write, pak_close },
1.18 mickey 88: #endif
1.1 mickey 89: { NULL }
90: };
91:
1.45 tedu 92: #ifndef SMALL
93: const struct compressor null_method =
94: { "null", ".nul", "XX", null_open, null_read, null_write, null_close };
95: #endif /* SMALL */
96:
1.18 mickey 97: int permission(const char *);
98: void setfile(const char *, struct stat *);
1.31 millert 99: __dead void usage(int);
1.49 henning 100: int docompress(const char *, char *, const struct compressor *,
1.25 deraadt 101: int, struct stat *);
1.49 henning 102: int dodecompress(const char *, char *, const struct compressor *,
1.25 deraadt 103: int, struct stat *);
1.37 millert 104: const struct compressor *check_method(int);
1.36 millert 105: const char *check_suffix(const char *);
106: char *set_outfile(const char *, char *, size_t);
1.37 millert 107: void list_stats(const char *, const struct compressor *, struct z_info *);
108: void verbose_info(const char *, off_t, off_t, u_int32_t);
1.18 mickey 109:
1.19 millert 110: #define OPTSTRING "123456789ab:cdfghlLnNOo:qrS:tvV"
1.18 mickey 111: const struct option longopts[] = {
1.45 tedu 112: #ifndef SMALL
1.18 mickey 113: { "ascii", no_argument, 0, 'a' },
114: { "stdout", no_argument, 0, 'c' },
115: { "to-stdout", no_argument, 0, 'c' },
116: { "decompress", no_argument, 0, 'd' },
117: { "uncompress", no_argument, 0, 'd' },
118: { "force", no_argument, 0, 'f' },
119: { "help", no_argument, 0, 'h' },
120: { "list", no_argument, 0, 'l' },
121: { "license", no_argument, 0, 'L' },
122: { "no-name", no_argument, 0, 'n' },
123: { "name", no_argument, 0, 'N' },
124: { "quiet", no_argument, 0, 'q' },
125: { "recursive", no_argument, 0, 'r' },
126: { "suffix", required_argument, 0, 'S' },
127: { "test", no_argument, 0, 't' },
128: { "verbose", no_argument, 0, 'v' },
129: { "version", no_argument, 0, 'V' },
130: { "fast", no_argument, 0, '1' },
131: { "best", no_argument, 0, '9' },
1.45 tedu 132: #endif /* SMALL */
1.18 mickey 133: { NULL }
134: };
1.1 mickey 135:
136: int
1.24 deraadt 137: main(int argc, char *argv[])
1.1 mickey 138: {
1.19 millert 139: FTS *ftsp;
140: FTSENT *entry;
141: struct stat osb;
1.18 mickey 142: const struct compressor *method;
1.36 millert 143: const char *s;
144: char *p, *infile;
1.19 millert 145: char outfile[MAXPATHLEN], _infile[MAXPATHLEN], suffix[16];
1.18 mickey 146: char *nargv[512]; /* some estimate based on ARG_MAX */
1.37 millert 147: int bits, exists, oreg, ch, error, i, rc, oflag;
1.1 mickey 148:
1.54 ! markus 149: exists = 0;
1.48 millert 150: bits = oflag = 0;
1.38 millert 151: nosave = -1;
1.1 mickey 152: p = __progname;
153: if (p[0] == 'g') {
154: method = M_DEFLATE;
1.18 mickey 155: bits = 6;
1.1 mickey 156: p++;
157: } else
1.45 tedu 158: #ifdef SMALL
159: method = M_DEFLATE;
160: #else
1.1 mickey 161: method = M_COMPRESS;
1.45 tedu 162: #endif /* SMALL */
1.1 mickey 163:
164: decomp = 0;
165: if (!strcmp(p, "zcat")) {
166: decomp++;
1.20 mickey 167: cat = 1;
1.1 mickey 168: } else {
169: if (p[0] == 'u' && p[1] == 'n') {
170: p += 2;
171: decomp++;
172: }
173:
1.2 mickey 174: if (strcmp(p, "zip") &&
175: strcmp(p, "compress"))
1.1 mickey 176: errx(1, "unknown program name");
177: }
178:
1.18 mickey 179: strlcpy(suffix, method->suffix, sizeof(suffix));
180:
1.19 millert 181: nargv[0] = NULL;
1.36 millert 182: if ((p = getenv("GZIP")) != NULL) {
1.18 mickey 183: char *last;
184:
185: nargv[0] = *argv++;
1.36 millert 186: for (i = 1, (p = strtok_r(p, " ", &last)); p != NULL;
1.18 mickey 187: (p = strtok_r(NULL, " ", &last)), i++)
188: if (i < sizeof(nargv)/sizeof(nargv[1]) - argc - 1)
189: nargv[i] = p;
190: else {
191: errx(1, "GZIP is too long");
192: }
193: argc += i - 1;
194: while ((nargv[i++] = *argv++))
195: ;
196: argv = nargv;
197: }
198:
199: while ((ch = getopt_long(argc, argv, OPTSTRING, longopts, NULL)) != -1)
1.1 mickey 200: switch(ch) {
201: case '1':
202: case '2':
203: case '3':
204: case '4':
205: case '5':
206: case '6':
207: case '7':
208: case '8':
209: case '9':
210: method = M_DEFLATE;
1.18 mickey 211: strlcpy(suffix, method->suffix, sizeof(suffix));
1.1 mickey 212: bits = ch - '0';
213: break;
1.18 mickey 214: case 'a':
215: warnx("option -a is ignored on this system");
216: break;
1.1 mickey 217: case 'b':
218: bits = strtol(optarg, &p, 10);
1.7 denny 219: /*
220: * POSIX 1002.3 says 9 <= bits <= 14 for portable
221: * apps, but says the implementation may allow
222: * greater.
223: */
1.1 mickey 224: if (*p)
225: errx(1, "illegal bit count -- %s", optarg);
226: break;
227: case 'c':
1.20 mickey 228: cat = 1;
1.1 mickey 229: break;
230: case 'd': /* Backward compatible. */
231: decomp++;
232: break;
233: case 'f':
234: force++;
235: break;
236: case 'g':
237: method = M_DEFLATE;
1.18 mickey 238: strlcpy(suffix, method->suffix, sizeof(suffix));
239: bits = 6;
1.1 mickey 240: break;
241: case 'l':
242: list++;
1.37 millert 243: testmode++;
244: decomp++;
1.1 mickey 245: break;
246: case 'n':
1.37 millert 247: nosave = 1;
1.1 mickey 248: break;
249: case 'N':
1.38 millert 250: nosave = 0;
1.1 mickey 251: break;
1.45 tedu 252: #ifndef SMALL
1.1 mickey 253: case 'O':
254: method = M_COMPRESS;
1.18 mickey 255: strlcpy(suffix, method->suffix, sizeof(suffix));
1.1 mickey 256: break;
1.45 tedu 257: #endif /* SMALL */
1.1 mickey 258: case 'o':
1.18 mickey 259: if (strlcpy(outfile, optarg,
260: sizeof(outfile)) >= sizeof(outfile))
261: errx(1, "-o argument is too long");
1.20 mickey 262: oflag = 1;
1.1 mickey 263: break;
264: case 'q':
265: verbose = -1;
266: break;
267: case 'S':
268: p = suffix;
269: if (optarg[0] != '.')
270: *p++ = '.';
1.18 mickey 271: strlcpy(p, optarg, sizeof(suffix) - (p - suffix));
272: p = optarg;
1.1 mickey 273: break;
274: case 't':
1.20 mickey 275: testmode = 1;
1.29 millert 276: decomp++;
1.1 mickey 277: break;
1.51 henning 278: #ifndef SMALL
1.18 mickey 279: case 'V':
1.48 millert 280: printf("%s\n%s\n", main_rcsid, gz_rcsid);
281: printf("%s\n%s\n", z_rcsid, null_rcsid);
1.45 tedu 282: #endif
1.19 millert 283: exit (0);
1.1 mickey 284: case 'v':
285: verbose++;
286: break;
1.52 henning 287: #ifndef SMALL
1.18 mickey 288: case 'L':
289: fputs(copyright, stderr);
290: fputs(license, stderr);
1.45 tedu 291: #endif
1.19 millert 292: exit (0);
1.18 mickey 293: case 'r':
1.19 millert 294: recurse++;
1.18 mickey 295: break;
296:
1.1 mickey 297: case 'h':
1.31 millert 298: usage(0);
299: break;
1.1 mickey 300: default:
1.31 millert 301: usage(1);
1.1 mickey 302: }
303: argc -= optind;
304: argv += optind;
305:
1.19 millert 306: if (argc == 0) {
307: if (nargv[0] == NULL)
308: argv = nargv;
309: /* XXX - make sure we don't oflow nargv in $GZIP case (millert) */
310: argv[0] = "/dev/stdin";
311: argv[1] = NULL;
312: pipin++;
1.39 millert 313: if (!oflag)
314: cat = 1;
1.28 millert 315: } else {
316: for (i = 0; i < argc; i++) {
317: if (argv[i][0] == '-' && argv[i][1] == '\0') {
318: argv[i] = "/dev/stdin";
319: pipin++;
320: cat = 1;
321: }
322: }
1.19 millert 323: }
324: if (oflag && (recurse || argc > 1))
325: errx(1, "-o option may only be used with a single input file");
1.20 mickey 326:
1.34 mickey 327: if ((cat && argc) + testmode + oflag > 1)
1.19 millert 328: errx(1, "may not mix -o, -c, or -t options");
1.38 millert 329: if (nosave == -1)
330: nosave = decomp;
1.19 millert 331:
332: if ((ftsp = fts_open(argv, FTS_PHYSICAL|FTS_NOCHDIR, 0)) == NULL)
333: err(1, NULL);
1.37 millert 334: for (rc = SUCCESS; (entry = fts_read(ftsp)) != NULL;) {
1.19 millert 335: infile = entry->fts_path;
336: switch (entry->fts_info) {
337: case FTS_D:
338: if (!recurse) {
339: warnx("%s is a directory: ignored",
340: infile);
341: fts_set(ftsp, entry, FTS_SKIP);
342: }
343: continue;
344: case FTS_DP:
345: continue;
346: case FTS_NS:
347: /*
348: * If file does not exist and has no suffix,
349: * tack on the default suffix and try that.
350: */
351: /* XXX - is overwriting fts_statp legal? (millert) */
352: if (entry->fts_errno == ENOENT &&
353: strchr(entry->fts_accpath, '.') == NULL &&
354: snprintf(_infile, sizeof(_infile), "%s%s", infile,
355: suffix) < sizeof(_infile) &&
356: stat(_infile, entry->fts_statp) == 0 &&
357: S_ISREG(entry->fts_statp->st_mode)) {
358: infile = _infile;
359: break;
360: }
361: case FTS_ERR:
362: case FTS_DNR:
363: warnx("%s: %s", infile, strerror(entry->fts_errno));
1.37 millert 364: rc = rc ? rc : WARNING;
1.19 millert 365: continue;
366: default:
1.47 henning 367: if (!S_ISREG(entry->fts_statp->st_mode) && !pipin &&
368: !(S_ISLNK(entry->fts_statp->st_mode) && cat)) {
1.27 millert 369: warnx("%s not a regular file%s",
1.28 millert 370: infile, cat ? "" : ": unchanged");
1.37 millert 371: rc = rc ? rc : WARNING;
1.19 millert 372: continue;
1.18 mickey 373: }
1.19 millert 374: break;
1.9 mickey 375: }
1.1 mickey 376:
1.36 millert 377: if (!decomp && !pipin && (s = check_suffix(infile)) != NULL) {
378: warnx("%s already has %s suffix -- unchanged",
379: infile, s);
1.37 millert 380: rc = rc ? rc : WARNING;
1.36 millert 381: continue;
382: }
383:
1.37 millert 384: if (cat)
1.22 deraadt 385: strlcpy(outfile, "/dev/stdout", sizeof outfile);
1.19 millert 386: else if (!oflag) {
387: if (decomp) {
1.35 millert 388: if (set_outfile(infile, outfile,
389: sizeof outfile) == NULL) {
1.53 millert 390: if (!recurse) {
1.19 millert 391: warnx("%s: unknown suffix: "
392: "ignored", infile);
1.53 millert 393: rc = rc ? rc : WARNING;
394: }
1.19 millert 395: continue;
396: }
397: } else {
398: if (snprintf(outfile, sizeof(outfile),
399: "%s%s", infile, suffix) >= sizeof(outfile)) {
400: warnx("%s%s: name too long",
401: infile, suffix);
1.53 millert 402: rc = rc ? rc : WARNING;
1.19 millert 403: continue;
404: }
405: }
1.1 mickey 406: }
407:
1.54 ! markus 408: if (!testmode)
! 409: exists = !stat(outfile, &osb);
1.19 millert 410: if (!force && exists && S_ISREG(osb.st_mode) &&
1.37 millert 411: !permission(outfile)) {
412: rc = rc ? rc : WARNING;
1.1 mickey 413: continue;
1.37 millert 414: }
1.1 mickey 415:
1.19 millert 416: oreg = !exists || S_ISREG(osb.st_mode);
1.1 mickey 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.19 millert 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) {
1.19 millert 427: setfile(outfile, entry->fts_statp);
1.37 millert 428: if (!pipin && unlink(infile) && verbose >= 0)
1.18 mickey 429: warn("input: %s", infile);
1.1 mickey 430: }
1.37 millert 431: break;
432: case WARNING:
433: rc = rc ? rc : WARNING;
434: break;
435: default:
436: rc = FAILURE;
437: if (oreg && unlink(outfile) && errno != ENOENT &&
438: verbose >= 0) {
439: if (force)
440: warn("output: %s", outfile);
441: else
442: err(1, "output: %s", outfile);
443: }
444: break;
1.1 mickey 445: }
1.19 millert 446: }
1.37 millert 447: if (list)
448: list_stats(NULL, NULL, NULL);
1.1 mickey 449:
1.18 mickey 450: exit(rc);
1.1 mickey 451: }
452:
453: int
1.49 henning 454: docompress(const char *in, char *out, const struct compressor *method,
1.24 deraadt 455: int bits, struct stat *sb)
1.1 mickey 456: {
1.18 mickey 457: u_char buf[Z_BUFSIZE];
1.37 millert 458: char *name;
459: int error, ifd, ofd, flags;
1.16 mpech 460: void *cookie;
461: ssize_t nr;
1.37 millert 462: u_int32_t mtime;
463: struct z_info info;
1.1 mickey 464:
1.37 millert 465: mtime = 0;
466: flags = 0;
467: error = SUCCESS;
468: name = NULL;
1.1 mickey 469: cookie = NULL;
1.3 mickey 470:
1.21 millert 471: if ((ifd = open(in, O_RDONLY)) < 0) {
472: if (verbose >= 0)
473: warn("%s", out);
1.37 millert 474: return (FAILURE);
1.21 millert 475: }
476:
1.3 mickey 477: if ((ofd = open(out, O_WRONLY|O_CREAT, S_IWUSR)) < 0) {
478: if (verbose >= 0)
479: warn("%s", out);
1.32 mickey 480: (void) close(ifd);
1.37 millert 481: return (FAILURE);
1.3 mickey 482: }
483:
1.4 mickey 484: if (method != M_COMPRESS && !force && isatty(ofd)) {
1.3 mickey 485: if (verbose >= 0)
486: warnx("%s: won't write compressed data to terminal",
1.25 deraadt 487: out);
1.32 mickey 488: (void) close(ofd);
489: (void) close(ifd);
1.37 millert 490: return (FAILURE);
1.3 mickey 491: }
1.1 mickey 492:
1.38 millert 493: if (!pipin && !nosave) {
1.37 millert 494: name = basename(in);
495: mtime = (u_int32_t)sb->st_mtime;
496: }
497: if ((cookie = (*method->open)(ofd, "w", name, bits, mtime, flags)) == NULL) {
1.32 mickey 498: if (verbose >= 0)
499: warn("%s", in);
500: (void) close(ofd);
501: (void) close(ifd);
1.37 millert 502: return (FAILURE);
1.32 mickey 503: }
1.1 mickey 504:
1.32 mickey 505: while ((nr = read(ifd, buf, sizeof(buf))) > 0)
506: if ((method->write)(cookie, buf, nr) != nr) {
507: if (verbose >= 0)
508: warn("%s", out);
1.37 millert 509: error = FAILURE;
1.32 mickey 510: break;
511: }
1.1 mickey 512:
1.32 mickey 513: if (!error && nr < 0) {
514: if (verbose >= 0)
1.1 mickey 515: warn("%s", in);
1.37 millert 516: error = FAILURE;
1.1 mickey 517: }
518:
1.37 millert 519: if ((method->close)(cookie, &info)) {
1.1 mickey 520: if (!error && verbose >= 0)
521: warn("%s", out);
1.37 millert 522: error = FAILURE;
1.1 mickey 523: }
524:
1.18 mickey 525: if (close(ifd)) {
526: if (!error && verbose >= 0)
1.32 mickey 527: warn("%s", in);
1.37 millert 528: error = FAILURE;
1.18 mickey 529: }
1.37 millert 530:
531: if (!force && info.total_out >= info.total_in) {
532: if (verbose > 0)
533: fprintf(stderr, "file would grow; left unmodified\n");
1.41 millert 534: error = FAILURE;
1.37 millert 535: }
536:
537: if (!error && verbose > 0)
538: verbose_info(out, info.total_out, info.total_in, info.hlen);
1.44 deraadt 539:
1.21 millert 540: return (error);
1.1 mickey 541: }
542:
1.18 mickey 543: const struct compressor *
1.37 millert 544: check_method(int fd)
1.1 mickey 545: {
1.18 mickey 546: const struct compressor *method;
1.33 millert 547: u_char magic[2];
1.1 mickey 548:
1.33 millert 549: if (read(fd, magic, sizeof(magic)) != 2)
550: return (NULL);
551: for (method = &c_table[0]; method->name != NULL; method++) {
552: if (magic[0] == method->magic[0] &&
553: magic[1] == method->magic[1])
554: return (method);
555: }
1.45 tedu 556: #ifndef SMALL
557: if (force && cat) {
558: null_magic[0] = magic[0];
559: null_magic[1] = magic[1];
560: return (&null_method);
561: }
562: #endif /* SMALL */
1.33 millert 563: return (NULL);
1.1 mickey 564: }
565:
566: int
1.49 henning 567: dodecompress(const char *in, char *out, const struct compressor *method,
1.24 deraadt 568: int bits, struct stat *sb)
1.1 mickey 569: {
1.18 mickey 570: u_char buf[Z_BUFSIZE];
571: int error, ifd, ofd;
1.16 mpech 572: void *cookie;
573: ssize_t nr;
1.37 millert 574: struct z_info info;
1.1 mickey 575:
1.37 millert 576: error = SUCCESS;
1.1 mickey 577: cookie = NULL;
578:
1.3 mickey 579: if ((ifd = open(in, O_RDONLY)) < 0) {
580: if (verbose >= 0)
581: warn("%s", in);
582: return -1;
583: }
584:
585: if (!force && isatty(ifd)) {
586: if (verbose >= 0)
587: warnx("%s: won't read compressed data from terminal",
1.25 deraadt 588: in);
1.3 mickey 589: close (ifd);
590: return -1;
591: }
592:
1.37 millert 593: if ((method = check_method(ifd)) == NULL) {
1.3 mickey 594: if (verbose >= 0)
595: warnx("%s: unrecognized file format", in);
1.13 d 596: close (ifd);
1.3 mickey 597: return -1;
598: }
599:
1.37 millert 600: /* XXX - open constrains outfile to MAXPATHLEN so this is safe */
1.38 millert 601: if ((cookie = (*method->open)(ifd, "r", nosave ? NULL : out,
1.37 millert 602: bits, 0, 1)) == NULL) {
1.32 mickey 603: if (verbose >= 0)
604: warn("%s", in);
605: close (ifd);
1.37 millert 606: return (FAILURE);
1.32 mickey 607: }
608:
1.37 millert 609: if (testmode)
610: ofd = -1;
611: else if ((ofd = open(out, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR)) < 0) {
1.32 mickey 612: if (verbose >= 0)
613: warn("%s", in);
1.37 millert 614: (method->close)(cookie, NULL);
615: return (FAILURE);
1.32 mickey 616: }
617:
1.37 millert 618: while ((nr = (method->read)(cookie, buf, sizeof(buf))) > 0) {
619: if (ofd != -1 && write(ofd, buf, nr) != nr) {
1.21 millert 620: if (verbose >= 0)
1.32 mickey 621: warn("%s", out);
1.37 millert 622: error = FAILURE;
1.32 mickey 623: break;
1.21 millert 624: }
1.37 millert 625: }
1.1 mickey 626:
1.32 mickey 627: if (!error && nr < 0) {
628: if (verbose >= 0)
629: warnx("%s: %s", in,
630: errno == EINVAL ? "crc error" : strerror(errno));
1.37 millert 631: error = errno == EINVAL ? WARNING : FAILURE;
1.1 mickey 632: }
633:
1.37 millert 634: if ((method->close)(cookie, &info)) {
1.1 mickey 635: if (!error && verbose >= 0)
1.32 mickey 636: warnx("%s", in);
1.37 millert 637: error = FAILURE;
638: }
639:
1.38 millert 640: if (!nosave) {
1.40 millert 641: if (info.mtime != 0) {
642: sb->st_mtimespec.tv_sec =
643: sb->st_atimespec.tv_sec = info.mtime;
644: sb->st_mtimespec.tv_nsec =
645: sb->st_atimespec.tv_nsec = 0;
646: } else
647: nosave = 1; /* no timestamp to restore */
648:
649: if (cat && strcmp(out, "/dev/stdout") != 0)
650: cat = 0; /* have a real output name */
1.1 mickey 651: }
652:
1.37 millert 653: if (ofd != -1 && close(ofd)) {
1.1 mickey 654: if (!error && verbose >= 0)
1.18 mickey 655: warn("%s", out);
1.37 millert 656: error = FAILURE;
657: }
658:
659: if (!error) {
660: if (list) {
661: if (info.mtime == 0)
662: info.mtime = (u_int32_t)sb->st_mtime;
1.43 millert 663: list_stats(out, method, &info);
1.37 millert 664: } else if (verbose > 0) {
665: verbose_info(out, info.total_in, info.total_out,
666: info.hlen);
667: }
1.1 mickey 668: }
669:
1.21 millert 670: return (error);
1.1 mickey 671: }
672:
673: void
1.24 deraadt 674: setfile(const char *name, struct stat *fs)
1.1 mickey 675: {
1.18 mickey 676: struct timeval tv[2];
1.1 mickey 677:
1.40 millert 678: if (!pipin || !nosave) {
679: TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
680: TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
681: if (utimes(name, tv))
682: warn("utimes: %s", name);
683: }
1.1 mickey 684:
1.40 millert 685: /*
686: * If input was a pipe we don't have any info to restore but we
687: * must set the mode since the current mode on the file is 0200.
688: */
689: if (pipin) {
690: mode_t mask = umask(022);
691: chmod(name, DEFFILEMODE & ~mask);
692: umask(mask);
693: return;
694: }
1.1 mickey 695:
696: /*
697: * Changing the ownership probably won't succeed, unless we're root
698: * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
699: * the mode; current BSD behavior is to remove all setuid bits on
700: * chown. If chown fails, lose setuid/setgid bits.
701: */
1.40 millert 702: fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
1.1 mickey 703: if (chown(name, fs->st_uid, fs->st_gid)) {
704: if (errno != EPERM)
705: warn("chown: %s", name);
706: fs->st_mode &= ~(S_ISUID|S_ISGID);
707: }
708: if (chmod(name, fs->st_mode))
709: warn("chown: %s", name);
710:
1.8 millert 711: if (fs->st_flags && chflags(name, fs->st_flags))
1.1 mickey 712: warn("chflags: %s", name);
713: }
714:
715: int
1.24 deraadt 716: permission(const char *fname)
1.1 mickey 717: {
718: int ch, first;
719:
720: if (!isatty(fileno(stderr)))
721: return (0);
722: (void)fprintf(stderr, "overwrite %s? ", fname);
723: first = ch = getchar();
724: while (ch != '\n' && ch != EOF)
725: ch = getchar();
726: return (first == 'y');
1.35 millert 727: }
728:
729: /*
1.36 millert 730: * Check infile for a known suffix and return the suffix portion or NULL.
731: */
732: const char *
733: check_suffix(const char *infile)
734: {
735: int i;
736: char *suf, *sep, *separators = ".-_";
737: static char *suffixes[] = { "Z", "gz", "z", "tgz", "taz", NULL };
738:
739: for (sep = separators; *sep != '\0'; sep++) {
740: if ((suf = strrchr(infile, *sep)) == NULL)
741: continue;
742: suf++;
743:
744: for (i = 0; suffixes[i] != NULL; i++) {
745: if (strcmp(suf, suffixes[i]) == 0)
746: return (suf - 1);
747: }
748: }
749: return (NULL);
750: }
751:
752: /*
1.35 millert 753: * Set outfile based on the suffix. In most cases we just strip
754: * off the suffix but things like .tgz and .taz are special.
755: */
756: char *
1.36 millert 757: set_outfile(const char *infile, char *outfile, size_t osize)
1.35 millert 758: {
1.36 millert 759: const char *s;
760: char *cp;
761:
762: if ((s = check_suffix(infile)) == NULL)
1.35 millert 763: return (NULL);
764:
1.36 millert 765: (void)strlcpy(outfile, infile, osize);
766: cp = outfile + (s - infile) + 1;
767: /*
768: * Convert tgz and taz -> tar, else drop the suffix.
769: */
770: if (strcmp(cp, "tgz") == 0) {
771: cp[1] = 'a';
772: cp[2] = 'r';
773: } else if (strcmp(cp, "taz") == 0)
774: cp[2] = 'r';
775: else
776: cp[-1] = '\0';
777: return (outfile);
1.37 millert 778: }
779:
780: /*
781: * Print output for the -l option.
782: */
783: void
784: list_stats(const char *name, const struct compressor *method,
785: struct z_info *info)
786: {
787: static off_t compressed_total, uncompressed_total, header_total;
788: static u_int nruns;
789: char *timestr;
1.43 millert 790:
1.37 millert 791: if (nruns == 0) {
792: if (verbose >= 0) {
793: if (verbose > 0)
1.48 millert 794: fputs("method crc date time ", stdout);
795: puts("compressed uncompressed ratio uncompressed_name");
1.37 millert 796: }
797: }
798: nruns++;
799:
800: if (name != NULL) {
1.48 millert 801: if (strcmp(name, "/dev/stdout") == 0)
802: name += 5;
1.37 millert 803: if (verbose > 0) {
804: timestr = ctime(&info->mtime) + 4;
805: timestr[12] = '\0';
1.48 millert 806: if (timestr[4] == ' ')
807: timestr[4] = '0';
808: printf("%-7.7s %08x %s ", method->name, info->crc,
809: timestr);
1.37 millert 810: }
1.48 millert 811: printf("%10lld %10lld %4.1f%% %s\n",
1.37 millert 812: (long long)(info->total_in + info->hlen),
813: (long long)info->total_out,
814: (info->total_out - info->total_in) *
815: 100.0 / info->total_out, name);
816: compressed_total += info->total_in;
817: uncompressed_total += info->total_out;
818: header_total += info->hlen;
819: } else if (verbose >= 0) {
820: if (nruns < 3) /* only do totals for > 1 files */
821: return;
822: if (verbose > 0)
1.48 millert 823: fputs(" ", stdout);
824: printf("%10lld %10lld %4.1f%% (totals)\n",
1.37 millert 825: (long long)(compressed_total + header_total),
826: (long long)uncompressed_total,
827: (uncompressed_total - compressed_total) *
828: 100.0 / uncompressed_total);
829: }
830: }
831:
832: void
833: verbose_info(const char *file, off_t compressed, off_t uncompressed,
834: u_int32_t hlen)
835: {
836: if (testmode) {
837: fputs("OK\n", stderr);
838: return;
839: }
840: if (!pipin) {
841: fprintf(stderr, "\t%4.1f%% -- replaced with %s\n",
842: (uncompressed - compressed) * 100.0 / uncompressed, file);
843: }
844: compressed += hlen;
1.44 deraadt 845: fprintf(stderr, "%lld bytes in, %lld bytes out\n",
1.37 millert 846: (long long)(decomp ? compressed : uncompressed),
847: (long long)(decomp ? uncompressed : compressed));
1.1 mickey 848: }
849:
1.31 millert 850: __dead void
851: usage(int status)
1.1 mickey 852: {
853: fprintf(stderr,
1.31 millert 854: "usage: %s [-cdfghOqrtvV] [-b bits] [-S suffix] [-[1-9]] [file ...]\n",
1.18 mickey 855: __progname);
1.31 millert 856: exit(status);
1.1 mickey 857: }