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