2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1992, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)compress.c 8.2 (Berkeley) 1/7/94
31 * $FreeBSD: src/usr.bin/compress/compress.c,v 1.7.6.5 2002/07/16 00:56:04 tjr Exp $
32 * $DragonFly: src/usr.bin/compress/compress.c,v 1.4 2004/08/30 18:06:49 eirikn Exp $
35 #include <sys/param.h>
49 void compress(const char *, const char *, int);
50 void cwarn(const char *, ...) __printflike(1, 2);
51 void cwarnx(const char *, ...) __printflike(1, 2);
52 void decompress(const char *, const char *, int);
53 int permission(const char *);
54 void setfile(const char *, struct stat *);
57 int eval, force, verbose;
60 main(int argc, char **argv)
62 enum {COMPRESS, DECOMPRESS} style;
65 char *p, newname[MAXPATHLEN];
68 if ((p = strrchr(argv[0], '/')) == NULL)
72 if (!strcmp(p, "uncompress"))
74 else if (!strcmp(p, "compress"))
76 else if (!strcmp(p, "zcat")) {
80 errx(1, "unknown program name");
83 while ((ch = getopt(argc, argv, "b:cdfv")) != -1)
86 bits = strtol(optarg, &p, 10);
88 errx(1, "illegal bit count -- %s", optarg);
93 case 'd': /* Backward compatible. */
104 usage(style == COMPRESS);
112 (void)compress("/dev/stdin", "/dev/stdout", bits);
115 (void)decompress("/dev/stdin", "/dev/stdout", bits);
121 if (cat == 1 && argc > 1)
122 errx(1, "the -c option permits only a single file argument");
124 for (; *argv; ++argv)
127 if (strcmp(*argv, "-") == 0) {
128 compress("/dev/stdin", "/dev/stdout", bits);
131 compress(*argv, "/dev/stdout", bits);
134 if ((p = strrchr(*argv, '.')) != NULL &&
136 cwarnx("%s: name already has trailing .Z",
141 if (len > sizeof(newname) - 3) {
142 cwarnx("%s: name too long", *argv);
145 memmove(newname, *argv, len);
147 newname[len + 1] = 'Z';
148 newname[len + 2] = '\0';
149 compress(*argv, newname, bits);
152 if (strcmp(*argv, "-") == 0) {
153 decompress("/dev/stdin", "/dev/stdout", bits);
157 if ((p = strrchr(*argv, '.')) == NULL ||
159 if (len > sizeof(newname) - 3) {
160 cwarnx("%s: name too long", *argv);
163 memmove(newname, *argv, len);
165 newname[len + 1] = 'Z';
166 newname[len + 2] = '\0';
168 cat ? "/dev/stdout" : *argv, bits);
170 if (len - 2 > sizeof(newname) - 1) {
171 cwarnx("%s: name too long", *argv);
174 memmove(newname, *argv, len - 2);
175 newname[len - 2] = '\0';
177 cat ? "/dev/stdout" : newname, bits);
185 compress(const char *in, const char *out, int bits)
190 int exists, isreg, oreg;
193 exists = !stat(out, &sb);
194 if (!force && exists && S_ISREG(sb.st_mode) && !permission(out))
196 isreg = oreg = !exists || S_ISREG(sb.st_mode);
199 if ((ifp = fopen(in, "r")) == NULL) {
203 if (stat(in, &isb)) { /* DON'T FSTAT! */
207 if (!S_ISREG(isb.st_mode))
210 if ((ofp = zopen(out, "w", bits)) == NULL) {
214 while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
215 if (fwrite(buf, 1, nr, ofp) != nr) {
220 if (ferror(ifp) || fclose(ifp)) {
233 if (stat(out, &sb)) {
238 if (!force && sb.st_size >= isb.st_size) {
240 (void)fprintf(stderr, "%s: file would grow; left unmodified\n",
254 (void)fprintf(stderr, "%s: ", out);
255 if (isb.st_size > sb.st_size)
256 (void)fprintf(stderr, "%.0f%% compression\n",
257 ((float)sb.st_size / isb.st_size) * 100.0);
259 (void)fprintf(stderr, "%.0f%% expansion\n",
260 ((float)isb.st_size / sb.st_size) * 100.0);
275 decompress(const char *in, const char *out, int bits)
280 int exists, isreg, oreg;
283 exists = !stat(out, &sb);
284 if (!force && exists && S_ISREG(sb.st_mode) && !permission(out))
286 isreg = oreg = !exists || S_ISREG(sb.st_mode);
289 if ((ofp = fopen(out, "w")) == NULL) {
294 if ((ifp = zopen(in, "r", bits)) == NULL) {
302 if (!S_ISREG(sb.st_mode))
305 while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
306 if (fwrite(buf, 1, nr, ofp) != nr) {
311 if (ferror(ifp) || fclose(ifp)) {
340 setfile(const char *name, struct stat *fs)
342 static struct timeval tv[2];
344 fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
346 TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
347 TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
348 if (utimes(name, tv))
349 cwarn("utimes: %s", name);
352 * Changing the ownership probably won't succeed, unless we're root
353 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
354 * the mode; current BSD behavior is to remove all setuid bits on
355 * chown. If chown fails, lose setuid/setgid bits.
357 if (chown(name, fs->st_uid, fs->st_gid)) {
359 cwarn("chown: %s", name);
360 fs->st_mode &= ~(S_ISUID|S_ISGID);
362 if (chmod(name, fs->st_mode) && errno != EOPNOTSUPP)
363 cwarn("chmod: %s", name);
365 if (chflags(name, fs->st_flags) && errno != EOPNOTSUPP)
366 cwarn("chflags: %s", name);
370 permission(const char *fname)
374 if (!isatty(fileno(stderr)))
376 (void)fprintf(stderr, "overwrite %s? ", fname);
377 first = ch = getchar();
378 while (ch != '\n' && ch != EOF)
380 return (first == 'y');
384 usage(int iscompress)
387 (void)fprintf(stderr,
388 "usage: compress [-cfv] [-b bits] [file ...]\n");
390 (void)fprintf(stderr,
391 "usage: uncompress [-c] [-b bits] [file ...]\n");
396 cwarnx(const char *fmt, ...)
407 cwarn(const char *fmt, ...)