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