Home | History | Annotate | Line # | Download | only in compress
compress.c revision 1.10
      1 /*	$NetBSD: compress.c,v 1.10 1996/06/30 12:41:44 mrg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 static char copyright[] =
     38 "@(#) Copyright (c) 1992, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n";
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)compress.c	8.2 (Berkeley) 1/7/94";
     45 #else
     46 static char rcsid[] = "$NetBSD: compress.c,v 1.10 1996/06/30 12:41:44 mrg Exp $";
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #include <sys/time.h>
     52 #include <sys/stat.h>
     53 
     54 #include <err.h>
     55 #include <errno.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 #include <unistd.h>
     60 
     61 #ifdef __STDC__
     62 #include <stdarg.h>
     63 #else
     64 #include <varargs.h>
     65 #endif
     66 
     67 void	compress __P((char *, char *, int));
     68 void	cwarn __P((const char *, ...));
     69 void	cwarnx __P((const char *, ...));
     70 void	decompress __P((char *, char *, int));
     71 int	permission __P((char *));
     72 void	setfile __P((char *, struct stat *));
     73 void	usage __P((int));
     74 
     75 extern FILE *zopen __P((const char *fname, const char *mode, int bits));
     76 
     77 int eval, force, verbose;
     78 int isstdout, isstdin;
     79 
     80 int
     81 main(argc, argv)
     82 	int argc;
     83 	char *argv[];
     84 {
     85 	enum {COMPRESS, DECOMPRESS} style;
     86 	size_t len;
     87 	int bits, cat, ch;
     88 	char *p, newname[MAXPATHLEN];
     89 
     90 	if ((p = rindex(argv[0], '/')) == NULL)
     91 		p = argv[0];
     92 	else
     93 		++p;
     94 	if (!strcmp(p, "uncompress"))
     95 		style = DECOMPRESS;
     96 	else if (!strcmp(p, "compress"))
     97 		style = COMPRESS;
     98 	else
     99 		errx(1, "unknown program name");
    100 
    101 	bits = cat = 0;
    102 	while ((ch = getopt(argc, argv, "b:cdfv")) != EOF)
    103 		switch(ch) {
    104 		case 'b':
    105 			bits = strtol(optarg, &p, 10);
    106 			if (*p)
    107 				errx(1, "illegal bit count -- %s", optarg);
    108 			break;
    109 		case 'c':
    110 			cat = 1;
    111 			break;
    112 		case 'd':		/* Backward compatible. */
    113 			style = DECOMPRESS;
    114 			break;
    115 		case 'f':
    116 			force = 1;
    117 			break;
    118 		case 'v':
    119 			verbose = 1;
    120 			break;
    121 		case '?':
    122 		default:
    123 			usage(style == COMPRESS);
    124 		}
    125 	argc -= optind;
    126 	argv += optind;
    127 
    128 	if (argc == 0) {
    129 		switch(style) {
    130 		case COMPRESS:
    131 			isstdout = 1;
    132 			isstdin = 1;
    133 			(void)compress("/dev/stdin", "/dev/stdout", bits);
    134 			break;
    135 		case DECOMPRESS:
    136 			isstdout = 1;
    137 			isstdin = 1;
    138 			(void)decompress("/dev/stdin", "/dev/stdout", bits);
    139 			break;
    140 		}
    141 		exit (eval);
    142 	}
    143 
    144 	if (cat == 1 && argc > 1)
    145 		errx(1, "the -c option permits only a single file argument");
    146 
    147 	for (; *argv; ++argv)
    148 		isstdout = 0;
    149 		switch(style) {
    150 		case COMPRESS:
    151 			if (cat) {
    152 				isstdout = 1;
    153 				compress(*argv, "/dev/stdout", bits);
    154 				break;
    155 			}
    156 			if ((p = rindex(*argv, '.')) != NULL &&
    157 			    !strcmp(p, ".Z")) {
    158 				cwarnx("%s: name already has trailing .Z",
    159 				    *argv);
    160 				break;
    161 			}
    162 			len = strlen(*argv);
    163 			if (len > sizeof(newname) - 3) {
    164 				cwarnx("%s: name too long", *argv);
    165 				break;
    166 			}
    167 			memmove(newname, *argv, len);
    168 			newname[len] = '.';
    169 			newname[len + 1] = 'Z';
    170 			newname[len + 2] = '\0';
    171 			compress(*argv, newname, bits);
    172 			break;
    173 		case DECOMPRESS:
    174 			len = strlen(*argv);
    175 			if ((p = rindex(*argv, '.')) == NULL ||
    176 			    strcmp(p, ".Z")) {
    177 				if (len > sizeof(newname) - 3) {
    178 					cwarnx("%s: name too long", *argv);
    179 					break;
    180 				}
    181 				memmove(newname, *argv, len);
    182 				newname[len] = '.';
    183 				newname[len + 1] = 'Z';
    184 				newname[len + 2] = '\0';
    185 				decompress(newname,
    186 				    cat ? "/dev/stdout" : *argv, bits);
    187 				if (cat)
    188 					isstdout = 1;
    189 			} else {
    190 				if (len - 2 > sizeof(newname) - 1) {
    191 					cwarnx("%s: name too long", *argv);
    192 					break;
    193 				}
    194 				memmove(newname, *argv, len - 2);
    195 				newname[len - 2] = '\0';
    196 				decompress(*argv,
    197 				    cat ? "/dev/stdout" : newname, bits);
    198 				if (cat)
    199 					isstdout = 1;
    200 			}
    201 			break;
    202 		}
    203 	exit (eval);
    204 }
    205 
    206 void
    207 compress(in, out, bits)
    208 	char *in, *out;
    209 	int bits;
    210 {
    211 	register int nr;
    212 	struct stat isb, sb;
    213 	FILE *ifp, *ofp;
    214 	int exists, isreg, oreg;
    215 	u_char buf[1024];
    216 
    217 	if (!isstdout) {
    218 		exists = !stat(out, &sb);
    219 		if (!force && exists && S_ISREG(sb.st_mode) && !permission(out))
    220 			return;
    221 		oreg = !exists || S_ISREG(sb.st_mode);
    222 	} else
    223 		oreg = 1;
    224 
    225 	ifp = ofp = NULL;
    226 	if ((ifp = fopen(in, "r")) == NULL) {
    227 		cwarn("%s", in);
    228 		return;
    229 	}
    230 
    231 	if (!isstdin) {
    232 		if (stat(in, &isb)) {		/* DON'T FSTAT! */
    233 			cwarn("%s", in);
    234 			goto err;
    235 		}
    236 		if (!S_ISREG(isb.st_mode))
    237 			isreg = 0;
    238 		else
    239 			isreg = 1;
    240 	} else
    241 		isreg = 0;
    242 
    243 	if ((ofp = zopen(out, "w", bits)) == NULL) {
    244 		cwarn("%s", out);
    245 		goto err;
    246 	}
    247 	while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
    248 		if (fwrite(buf, 1, nr, ofp) != nr) {
    249 			cwarn("%s", out);
    250 			goto err;
    251 		}
    252 
    253 	if (ferror(ifp) || fclose(ifp)) {
    254 		cwarn("%s", in);
    255 		goto err;
    256 	}
    257 	ifp = NULL;
    258 
    259 	if (fclose(ofp)) {
    260 		cwarn("%s", out);
    261 		goto err;
    262 	}
    263 	ofp = NULL;
    264 
    265 	if (isreg) {
    266 		if (stat(out, &sb)) {
    267 			cwarn("%s", out);
    268 			goto err;
    269 		}
    270 
    271 		if (!force && sb.st_size >= isb.st_size) {
    272 			if (verbose)
    273 		(void)printf("%s: file would grow; left unmodified\n", in);
    274 			if (unlink(out))
    275 				cwarn("%s", out);
    276 			goto err;
    277 		}
    278 
    279 		setfile(out, &isb);
    280 
    281 		if (unlink(in))
    282 			cwarn("%s", in);
    283 
    284 		if (verbose) {
    285 			(void)printf("%s: ", out);
    286 			if (isb.st_size > sb.st_size)
    287 				(void)printf("%.0f%% compression\n",
    288 				    ((float)sb.st_size / isb.st_size) * 100.0);
    289 			else
    290 				(void)printf("%.0f%% expansion\n",
    291 				    ((float)isb.st_size / sb.st_size) * 100.0);
    292 		}
    293 	}
    294 	return;
    295 
    296 err:	if (ofp) {
    297 		if (oreg)
    298 			(void)unlink(out);
    299 		(void)fclose(ofp);
    300 	}
    301 	if (ifp)
    302 		(void)fclose(ifp);
    303 }
    304 
    305 void
    306 decompress(in, out, bits)
    307 	char *in, *out;
    308 	int bits;
    309 {
    310 	register int nr;
    311 	struct stat sb;
    312 	FILE *ifp, *ofp;
    313 	int exists, isreg, oreg;
    314 	u_char buf[1024];
    315 
    316 	if (!isstdout) {
    317 		exists = !stat(out, &sb);
    318 		if (!force && exists && S_ISREG(sb.st_mode) && !permission(out))
    319 			return;
    320 		oreg = !exists || S_ISREG(sb.st_mode);
    321 	} else
    322 		oreg = 1;
    323 
    324 	ifp = ofp = NULL;
    325 	if ((ofp = fopen(out, "w")) == NULL) {
    326 		cwarn("%s", out);
    327 		return;
    328 	}
    329 
    330 	if ((ifp = zopen(in, "r", bits)) == NULL) {
    331 		cwarn("%s", in);
    332 		goto err;
    333 	}
    334 	if (!isstdin) {
    335 		if (stat(in, &sb)) {
    336 			cwarn("%s", in);
    337 			goto err;
    338 		}
    339 		if (!S_ISREG(sb.st_mode))
    340 			isreg = 0;
    341 		else
    342 			isreg = 1;
    343 	} else
    344 		isreg = 0;
    345 
    346 	while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
    347 		if (fwrite(buf, 1, nr, ofp) != nr) {
    348 			cwarn("%s", out);
    349 			goto err;
    350 		}
    351 
    352 	if (ferror(ifp) || fclose(ifp)) {
    353 		cwarn("%s", in);
    354 		goto err;
    355 	}
    356 	ifp = NULL;
    357 
    358 	if (fclose(ofp)) {
    359 		cwarn("%s", out);
    360 		goto err;
    361 	}
    362 
    363 	if (isreg) {
    364 		setfile(out, &sb);
    365 
    366 		if (unlink(in))
    367 			cwarn("%s", in);
    368 	}
    369 	return;
    370 
    371 err:	if (ofp) {
    372 		if (oreg)
    373 			(void)unlink(out);
    374 		(void)fclose(ofp);
    375 	}
    376 	if (ifp)
    377 		(void)fclose(ifp);
    378 }
    379 
    380 void
    381 setfile(name, fs)
    382 	char *name;
    383 	register struct stat *fs;
    384 {
    385 	static struct timeval tv[2];
    386 
    387 	fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
    388 
    389 	TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
    390 	TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
    391 	if (utimes(name, tv))
    392 		cwarn("utimes: %s", name);
    393 
    394 	/*
    395 	 * Changing the ownership probably won't succeed, unless we're root
    396 	 * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
    397 	 * the mode; current BSD behavior is to remove all setuid bits on
    398 	 * chown.  If chown fails, lose setuid/setgid bits.
    399 	 */
    400 	if (chown(name, fs->st_uid, fs->st_gid)) {
    401 		if (errno != EPERM)
    402 			cwarn("chown: %s", name);
    403 		fs->st_mode &= ~(S_ISUID|S_ISGID);
    404 	}
    405 	if (chmod(name, fs->st_mode))
    406 		cwarn("chown: %s", name);
    407 
    408 	if (chflags(name, fs->st_flags))
    409 		cwarn("chflags: %s", name);
    410 }
    411 
    412 int
    413 permission(fname)
    414 	char *fname;
    415 {
    416 	int ch, first;
    417 
    418 	if (!isatty(fileno(stderr)))
    419 		return (0);
    420 	(void)fprintf(stderr, "overwrite %s? ", fname);
    421 	first = ch = getchar();
    422 	while (ch != '\n' && ch != EOF)
    423 		ch = getchar();
    424 	return (first == 'y');
    425 }
    426 
    427 void
    428 usage(iscompress)
    429 	int iscompress;
    430 {
    431 	if (iscompress)
    432 		(void)fprintf(stderr,
    433 		    "usage: compress [-cfv] [-b bits] [file ...]\n");
    434 	else
    435 		(void)fprintf(stderr,
    436 		    "usage: uncompress [-c] [-b bits] [file ...]\n");
    437 	exit(1);
    438 }
    439 
    440 void
    441 #if __STDC__
    442 cwarnx(const char *fmt, ...)
    443 #else
    444 cwarnx(fmt, va_alist)
    445 	int eval;
    446 	const char *fmt;
    447 	va_dcl
    448 #endif
    449 {
    450 	va_list ap;
    451 #if __STDC__
    452 	va_start(ap, fmt);
    453 #else
    454 	va_start(ap);
    455 #endif
    456 	vwarnx(fmt, ap);
    457 	va_end(ap);
    458 	eval = 1;
    459 }
    460 
    461 void
    462 #if __STDC__
    463 cwarn(const char *fmt, ...)
    464 #else
    465 cwarn(fmt, va_alist)
    466 	int eval;
    467 	const char *fmt;
    468 	va_dcl
    469 #endif
    470 {
    471 	va_list ap;
    472 #if __STDC__
    473 	va_start(ap, fmt);
    474 #else
    475 	va_start(ap);
    476 #endif
    477 	vwarn(fmt, ap);
    478 	va_end(ap);
    479 	eval = 1;
    480 }
    481