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