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