NetBSD/usr.bin/compress/compress.c

453 lines
9.7 KiB
C
Raw Normal View History

/* $NetBSD: compress.c,v 1.21 2004/07/09 12:14:37 wiz Exp $ */
1994-06-13 18:48:14 +04:00
/*-
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
1993-03-21 12:45:37 +03:00
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
The Regents of the University of California. All rights reserved.\n");
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)compress.c 8.2 (Berkeley) 1/7/94";
#else
__RCSID("$NetBSD: compress.c,v 1.21 2004/07/09 12:14:37 wiz Exp $");
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include <sys/param.h>
1994-06-13 18:48:14 +04:00
#include <sys/time.h>
1993-03-21 12:45:37 +03:00
#include <sys/stat.h>
1994-06-13 18:48:14 +04:00
#include <err.h>
1993-03-21 12:45:37 +03:00
#include <errno.h>
#include <stdarg.h>
1993-03-21 12:45:37 +03:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
1994-06-13 18:48:14 +04:00
#include <unistd.h>
1993-03-21 12:45:37 +03:00
void compress(char *, char *, int);
void cwarn(const char *, ...) __attribute__((__format__(__printf__,1,2)));
void cwarnx(const char *, ...) __attribute__((__format__(__printf__,1,2)));
void decompress(char *, char *, int);
int permission(char *);
void setfile(char *, struct stat *);
void usage(int);
1993-03-21 12:45:37 +03:00
int main(int, char *[]);
extern FILE *zopen(const char *fname, const char *mode, int bits);
1994-12-24 19:27:18 +03:00
1994-06-13 18:48:14 +04:00
int eval, force, verbose;
int isstdout, isstdin;
1993-03-21 12:45:37 +03:00
1994-06-13 18:48:14 +04:00
int
main(int argc, char **argv)
1993-03-21 12:45:37 +03:00
{
enum {COMPRESS, DECOMPRESS} style = COMPRESS;
1994-06-13 18:48:14 +04:00
size_t len;
int bits, cat, ch;
char *p, newname[MAXPATHLEN];
1993-03-21 12:45:37 +03:00
if ((p = strrchr(argv[0], '/')) == NULL)
1994-06-13 18:48:14 +04:00
p = argv[0];
1993-03-21 12:45:37 +03:00
else
1994-06-13 18:48:14 +04:00
++p;
if (!strcmp(p, "uncompress"))
style = DECOMPRESS;
else if (!strcmp(p, "compress"))
style = COMPRESS;
else if (!strcmp(p, "zcat")) {
style = DECOMPRESS;
cat = 1;
}
1994-06-13 18:48:14 +04:00
else
errx(1, "unknown program name");
1993-03-21 12:45:37 +03:00
1994-06-13 18:48:14 +04:00
bits = cat = 0;
1997-10-18 17:04:29 +04:00
while ((ch = getopt(argc, argv, "b:cdfv")) != -1)
1993-03-21 12:45:37 +03:00
switch(ch) {
case 'b':
1994-06-13 18:48:14 +04:00
bits = strtol(optarg, &p, 10);
if (*p)
errx(1, "illegal bit count -- %s", optarg);
1993-03-21 12:45:37 +03:00
break;
case 'c':
1994-06-13 18:48:14 +04:00
cat = 1;
1993-03-21 12:45:37 +03:00
break;
1994-06-13 18:48:14 +04:00
case 'd': /* Backward compatible. */
style = DECOMPRESS;
1993-03-21 12:45:37 +03:00
break;
case 'f':
force = 1;
break;
case 'v':
1994-06-13 18:48:14 +04:00
verbose = 1;
1993-03-21 12:45:37 +03:00
break;
case '?':
default:
1994-06-13 18:48:14 +04:00
usage(style == COMPRESS);
1993-03-21 12:45:37 +03:00
}
argc -= optind;
argv += optind;
1994-06-13 18:48:14 +04:00
if (argc == 0) {
switch(style) {
case COMPRESS:
isstdout = 1;
isstdin = 1;
1994-06-13 18:48:14 +04:00
(void)compress("/dev/stdin", "/dev/stdout", bits);
break;
case DECOMPRESS:
isstdout = 1;
isstdin = 1;
1994-06-13 18:48:14 +04:00
(void)decompress("/dev/stdin", "/dev/stdout", bits);
break;
1993-03-21 12:45:37 +03:00
}
1994-06-13 18:48:14 +04:00
exit (eval);
}
1993-03-21 12:45:37 +03:00
1994-06-13 18:48:14 +04:00
if (cat == 1 && argc > 1)
errx(1, "the -c option permits only a single file argument");
1993-03-21 12:45:37 +03:00
for (; *argv; ++argv) {
isstdout = 0;
1994-06-13 18:48:14 +04:00
switch(style) {
case COMPRESS:
if (cat) {
isstdout = 1;
1994-06-13 18:48:14 +04:00
compress(*argv, "/dev/stdout", bits);
break;
}
if ((p = strrchr(*argv, '.')) != NULL &&
1994-06-13 18:48:14 +04:00
!strcmp(p, ".Z")) {
cwarnx("%s: name already has trailing .Z",
*argv);
break;
}
len = strlen(*argv);
if (len > sizeof(newname) - 3) {
cwarnx("%s: name too long", *argv);
break;
}
memmove(newname, *argv, len);
newname[len] = '.';
newname[len + 1] = 'Z';
newname[len + 2] = '\0';
compress(*argv, newname, bits);
break;
case DECOMPRESS:
len = strlen(*argv);
if ((p = strrchr(*argv, '.')) == NULL ||
1994-06-13 18:48:14 +04:00
strcmp(p, ".Z")) {
if (len > sizeof(newname) - 3) {
cwarnx("%s: name too long", *argv);
break;
}
memmove(newname, *argv, len);
newname[len] = '.';
newname[len + 1] = 'Z';
newname[len + 2] = '\0';
decompress(newname,
cat ? "/dev/stdout" : *argv, bits);
if (cat)
isstdout = 1;
1994-06-13 18:48:14 +04:00
} else {
if (len - 2 > sizeof(newname) - 1) {
cwarnx("%s: name too long", *argv);
break;
}
memmove(newname, *argv, len - 2);
newname[len - 2] = '\0';
decompress(*argv,
cat ? "/dev/stdout" : newname, bits);
if (cat)
isstdout = 1;
1994-06-13 18:48:14 +04:00
}
break;
1993-03-21 12:45:37 +03:00
}
}
1994-06-13 18:48:14 +04:00
exit (eval);
1993-03-21 12:45:37 +03:00
}
1994-06-13 18:48:14 +04:00
void
compress(char *in, char *out, int bits)
1993-03-21 12:45:37 +03:00
{
int nr;
1994-06-13 18:48:14 +04:00
struct stat isb, sb;
FILE *ifp, *ofp;
int exists, isreg, oreg;
u_char buf[BUFSIZ];
1994-06-13 18:48:14 +04:00
if (!isstdout) {
exists = !stat(out, &sb);
if (!force && exists && S_ISREG(sb.st_mode) && !permission(out))
return;
oreg = !exists || S_ISREG(sb.st_mode);
} else
oreg = 0;
1994-06-13 18:48:14 +04:00
ifp = ofp = NULL;
if ((ifp = fopen(in, "r")) == NULL) {
cwarn("%s", in);
return;
}
if (!isstdin) {
if (stat(in, &isb)) { /* DON'T FSTAT! */
cwarn("%s", in);
goto err;
}
if (!S_ISREG(isb.st_mode))
isreg = 0;
else
isreg = 1;
} else
1994-06-13 18:48:14 +04:00
isreg = 0;
1993-03-21 12:45:37 +03:00
1994-06-13 18:48:14 +04:00
if ((ofp = zopen(out, "w", bits)) == NULL) {
cwarn("%s", out);
goto err;
}
while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
if (fwrite(buf, 1, nr, ofp) != nr) {
cwarn("%s", out);
goto err;
}
1993-03-21 12:45:37 +03:00
1994-06-13 18:48:14 +04:00
if (ferror(ifp) || fclose(ifp)) {
cwarn("%s", in);
goto err;
}
ifp = NULL;
1993-03-21 12:45:37 +03:00
1994-06-13 18:48:14 +04:00
if (fclose(ofp)) {
cwarn("%s", out);
goto err;
}
ofp = NULL;
1993-03-21 12:45:37 +03:00
if (isreg && oreg) {
1994-06-13 18:48:14 +04:00
if (stat(out, &sb)) {
cwarn("%s", out);
goto err;
}
1993-03-21 12:45:37 +03:00
1994-06-13 18:48:14 +04:00
if (!force && sb.st_size >= isb.st_size) {
if (verbose)
(void)printf("%s: file would grow; left unmodified\n", in);
if (unlink(out))
cwarn("%s", out);
goto err;
}
1993-03-21 12:45:37 +03:00
1994-06-13 18:48:14 +04:00
setfile(out, &isb);
1993-03-21 12:45:37 +03:00
1994-06-13 18:48:14 +04:00
if (unlink(in))
cwarn("%s", in);
1993-03-21 12:45:37 +03:00
1994-06-13 18:48:14 +04:00
if (verbose) {
(void)printf("%s: ", out);
if (isb.st_size > sb.st_size)
(void)printf("%.0f%% compression\n",
1997-10-19 19:18:43 +04:00
((double)sb.st_size / isb.st_size) * 100.0);
1994-06-13 18:48:14 +04:00
else
(void)printf("%.0f%% expansion\n",
1997-10-19 19:18:43 +04:00
((double)isb.st_size / sb.st_size) * 100.0);
1994-06-13 18:48:14 +04:00
}
1993-03-21 12:45:37 +03:00
}
1994-06-13 18:48:14 +04:00
return;
1993-03-21 12:45:37 +03:00
1994-06-13 18:48:14 +04:00
err: if (ofp) {
if (oreg)
(void)unlink(out);
(void)fclose(ofp);
}
if (ifp)
(void)fclose(ifp);
1993-03-21 12:45:37 +03:00
}
1994-06-13 18:48:14 +04:00
void
decompress(char *in, char *out, int bits)
1993-03-21 12:45:37 +03:00
{
int nr;
1994-06-13 18:48:14 +04:00
struct stat sb;
FILE *ifp, *ofp;
int exists, isreg, oreg;
u_char buf[BUFSIZ];
1994-06-13 18:48:14 +04:00
if (!isstdout) {
exists = !stat(out, &sb);
if (!force && exists && S_ISREG(sb.st_mode) && !permission(out))
return;
oreg = !exists || S_ISREG(sb.st_mode);
} else
oreg = 0;
1994-06-13 18:48:14 +04:00
ifp = ofp = NULL;
if ((ofp = fopen(out, "w")) == NULL) {
cwarn("%s", out);
return;
1993-03-21 12:45:37 +03:00
}
1994-06-13 18:48:14 +04:00
if ((ifp = zopen(in, "r", bits)) == NULL) {
cwarn("%s", in);
goto err;
1993-03-21 12:45:37 +03:00
}
if (!isstdin) {
if (stat(in, &sb)) {
cwarn("%s", in);
goto err;
}
if (!S_ISREG(sb.st_mode))
isreg = 0;
else
isreg = 1;
} else
1994-06-13 18:48:14 +04:00
isreg = 0;
1993-03-21 12:45:37 +03:00
1994-06-13 18:48:14 +04:00
while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
if (fwrite(buf, 1, nr, ofp) != nr) {
cwarn("%s", out);
goto err;
}
1993-03-21 12:45:37 +03:00
1994-06-13 18:48:14 +04:00
if (ferror(ifp) || fclose(ifp)) {
cwarn("%s", in);
goto err;
1993-03-21 12:45:37 +03:00
}
1994-06-13 18:48:14 +04:00
ifp = NULL;
1993-03-21 12:45:37 +03:00
1994-06-13 18:48:14 +04:00
if (fclose(ofp)) {
cwarn("%s", out);
goto err;
1993-03-21 12:45:37 +03:00
}
if (isreg && oreg) {
1994-06-13 18:48:14 +04:00
setfile(out, &sb);
1993-03-21 12:45:37 +03:00
1994-06-13 18:48:14 +04:00
if (unlink(in))
cwarn("%s", in);
1993-03-21 12:45:37 +03:00
}
1994-06-13 18:48:14 +04:00
return;
1993-03-21 12:45:37 +03:00
1994-06-13 18:48:14 +04:00
err: if (ofp) {
if (oreg)
(void)unlink(out);
(void)fclose(ofp);
1993-03-21 12:45:37 +03:00
}
1994-06-13 18:48:14 +04:00
if (ifp)
(void)fclose(ifp);
1993-03-21 12:45:37 +03:00
}
1994-06-13 18:48:14 +04:00
void
setfile(char *name, struct stat *fs)
1993-03-21 12:45:37 +03:00
{
1994-06-13 18:48:14 +04:00
static struct timeval tv[2];
1993-03-21 12:45:37 +03:00
1994-06-13 18:48:14 +04:00
fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
1993-03-21 12:45:37 +03:00
1994-06-13 18:48:14 +04:00
TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
if (utimes(name, tv))
cwarn("utimes: %s", name);
/*
* Changing the ownership probably won't succeed, unless we're root
* or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
* the mode; current BSD behavior is to remove all setuid bits on
* chown. If chown fails, lose setuid/setgid bits.
*/
if (chown(name, fs->st_uid, fs->st_gid)) {
if (errno != EPERM)
cwarn("chown: %s", name);
fs->st_mode &= ~(S_ISUID|S_ISGID);
1993-03-21 12:45:37 +03:00
}
1994-06-13 18:48:14 +04:00
if (chmod(name, fs->st_mode))
cwarn("chown: %s", name);
1993-03-21 12:45:37 +03:00
/*
* Restore the file's flags. However, do this only if the original
* file had any flags set; this avoids a warning on file-systems that
* do not support flags.
*/
if (fs->st_flags != 0 && chflags(name, fs->st_flags))
1994-06-13 18:48:14 +04:00
cwarn("chflags: %s", name);
1993-03-21 12:45:37 +03:00
}
int
permission(char *fname)
1993-03-21 12:45:37 +03:00
{
1994-06-13 18:48:14 +04:00
int ch, first;
if (!isatty(fileno(stderr)))
return (0);
(void)fprintf(stderr, "overwrite %s? ", fname);
first = ch = getchar();
while (ch != '\n' && ch != EOF)
ch = getchar();
return (first == 'y');
1993-03-21 12:45:37 +03:00
}
1994-06-13 18:48:14 +04:00
void
usage(int iscompress)
1993-03-21 12:45:37 +03:00
{
1994-06-13 18:48:14 +04:00
if (iscompress)
(void)fprintf(stderr,
"usage: compress [-cdfv] [-b bits] [file ...]\n");
1994-06-13 18:48:14 +04:00
else
(void)fprintf(stderr,
"usage: uncompress [-cdfv] [-b bits] [file ...]\n");
1993-03-21 12:45:37 +03:00
exit(1);
}
void
1994-06-13 18:48:14 +04:00
cwarnx(const char *fmt, ...)
1993-03-21 12:45:37 +03:00
{
1994-06-13 18:48:14 +04:00
va_list ap;
1994-06-13 18:48:14 +04:00
va_start(ap, fmt);
vwarnx(fmt, ap);
va_end(ap);
eval = 1;
1993-03-21 12:45:37 +03:00
}
void
1994-06-13 18:48:14 +04:00
cwarn(const char *fmt, ...)
1993-03-21 12:45:37 +03:00
{
1994-06-13 18:48:14 +04:00
va_list ap;
1994-06-13 18:48:14 +04:00
va_start(ap, fmt);
vwarn(fmt, ap);
va_end(ap);
eval = 1;
1993-03-21 12:45:37 +03:00
}