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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1992, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)compress.c 8.2 (Berkeley) 1/7/94
35 * $FreeBSD: src/usr.bin/compress/compress.c,v 1.7.6.5 2002/07/16 00:56:04 tjr Exp $
36 * $DragonFly: src/usr.bin/compress/compress.c,v 1.2 2003/06/17 04:29:25 dillon Exp $
39 #include <sys/param.h>
53 void compress(const char *, const char *, int);
54 void cwarn(const char *, ...) __printflike(1, 2);
55 void cwarnx(const char *, ...) __printflike(1, 2);
56 void decompress(const char *, const char *, int);
57 int permission(const char *);
58 void setfile(const char *, struct stat *);
61 int eval, force, verbose;
68 enum {COMPRESS, DECOMPRESS} style;
71 char *p, newname[MAXPATHLEN];
74 if ((p = rindex(argv[0], '/')) == NULL)
78 if (!strcmp(p, "uncompress"))
80 else if (!strcmp(p, "compress"))
82 else if (!strcmp(p, "zcat")) {
86 errx(1, "unknown program name");
89 while ((ch = getopt(argc, argv, "b:cdfv")) != -1)
92 bits = strtol(optarg, &p, 10);
94 errx(1, "illegal bit count -- %s", optarg);
99 case 'd': /* Backward compatible. */
110 usage(style == COMPRESS);
118 (void)compress("/dev/stdin", "/dev/stdout", bits);
121 (void)decompress("/dev/stdin", "/dev/stdout", bits);
127 if (cat == 1 && argc > 1)
128 errx(1, "the -c option permits only a single file argument");
130 for (; *argv; ++argv)
133 if (strcmp(*argv, "-") == 0) {
134 compress("/dev/stdin", "/dev/stdout", bits);
137 compress(*argv, "/dev/stdout", bits);
140 if ((p = rindex(*argv, '.')) != NULL &&
142 cwarnx("%s: name already has trailing .Z",
147 if (len > sizeof(newname) - 3) {
148 cwarnx("%s: name too long", *argv);
151 memmove(newname, *argv, len);
153 newname[len + 1] = 'Z';
154 newname[len + 2] = '\0';
155 compress(*argv, newname, bits);
158 if (strcmp(*argv, "-") == 0) {
159 decompress("/dev/stdin", "/dev/stdout", bits);
163 if ((p = rindex(*argv, '.')) == NULL ||
165 if (len > sizeof(newname) - 3) {
166 cwarnx("%s: name too long", *argv);
169 memmove(newname, *argv, len);
171 newname[len + 1] = 'Z';
172 newname[len + 2] = '\0';
174 cat ? "/dev/stdout" : *argv, bits);
176 if (len - 2 > sizeof(newname) - 1) {
177 cwarnx("%s: name too long", *argv);
180 memmove(newname, *argv, len - 2);
181 newname[len - 2] = '\0';
183 cat ? "/dev/stdout" : newname, bits);
191 compress(in, out, bits)
192 const char *in, *out;
198 int exists, isreg, oreg;
201 exists = !stat(out, &sb);
202 if (!force && exists && S_ISREG(sb.st_mode) && !permission(out))
204 isreg = oreg = !exists || S_ISREG(sb.st_mode);
207 if ((ifp = fopen(in, "r")) == NULL) {
211 if (stat(in, &isb)) { /* DON'T FSTAT! */
215 if (!S_ISREG(isb.st_mode))
218 if ((ofp = zopen(out, "w", bits)) == NULL) {
222 while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
223 if (fwrite(buf, 1, nr, ofp) != nr) {
228 if (ferror(ifp) || fclose(ifp)) {
241 if (stat(out, &sb)) {
246 if (!force && sb.st_size >= isb.st_size) {
248 (void)fprintf(stderr, "%s: file would grow; left unmodified\n",
262 (void)fprintf(stderr, "%s: ", out);
263 if (isb.st_size > sb.st_size)
264 (void)fprintf(stderr, "%.0f%% compression\n",
265 ((float)sb.st_size / isb.st_size) * 100.0);
267 (void)fprintf(stderr, "%.0f%% expansion\n",
268 ((float)isb.st_size / sb.st_size) * 100.0);
283 decompress(in, out, bits)
284 const char *in, *out;
290 int exists, isreg, oreg;
293 exists = !stat(out, &sb);
294 if (!force && exists && S_ISREG(sb.st_mode) && !permission(out))
296 isreg = oreg = !exists || S_ISREG(sb.st_mode);
299 if ((ofp = fopen(out, "w")) == NULL) {
304 if ((ifp = zopen(in, "r", bits)) == NULL) {
312 if (!S_ISREG(sb.st_mode))
315 while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
316 if (fwrite(buf, 1, nr, ofp) != nr) {
321 if (ferror(ifp) || fclose(ifp)) {
354 static struct timeval tv[2];
356 fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
358 TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
359 TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
360 if (utimes(name, tv))
361 cwarn("utimes: %s", name);
364 * Changing the ownership probably won't succeed, unless we're root
365 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
366 * the mode; current BSD behavior is to remove all setuid bits on
367 * chown. If chown fails, lose setuid/setgid bits.
369 if (chown(name, fs->st_uid, fs->st_gid)) {
371 cwarn("chown: %s", name);
372 fs->st_mode &= ~(S_ISUID|S_ISGID);
374 if (chmod(name, fs->st_mode) && errno != EOPNOTSUPP)
375 cwarn("chmod: %s", name);
377 if (chflags(name, fs->st_flags) && errno != EOPNOTSUPP)
378 cwarn("chflags: %s", name);
387 if (!isatty(fileno(stderr)))
389 (void)fprintf(stderr, "overwrite %s? ", fname);
390 first = ch = getchar();
391 while (ch != '\n' && ch != EOF)
393 return (first == 'y');
401 (void)fprintf(stderr,
402 "usage: compress [-cfv] [-b bits] [file ...]\n");
404 (void)fprintf(stderr,
405 "usage: uncompress [-c] [-b bits] [file ...]\n");
410 cwarnx(const char *fmt, ...)
421 cwarn(const char *fmt, ...)