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
31 static const char copyright[] =
32 "@(#) Copyright (c) 1992, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)compress.c 8.2 (Berkeley) 1/7/94";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
60 static void compress(const char *, const char *, int);
61 static void cwarn(const char *, ...) __printflike(1, 2);
62 static void cwarnx(const char *, ...) __printflike(1, 2);
63 static void decompress(const char *, const char *, int);
64 static int permission(const char *);
65 static void setfile(const char *, struct stat *);
66 static void usage(int);
68 static int eval, force, verbose;
71 main(int argc, char *argv[])
73 enum {COMPRESS, DECOMPRESS} style;
76 char *p, newname[MAXPATHLEN];
79 if ((p = strrchr(argv[0], '/')) == NULL)
83 if (!strcmp(p, "uncompress"))
85 else if (!strcmp(p, "compress"))
87 else if (!strcmp(p, "zcat")) {
91 errx(1, "unknown program name");
94 while ((ch = getopt(argc, argv, "b:cdfv")) != -1)
97 bits = strtol(optarg, &p, 10);
99 errx(1, "illegal bit count -- %s", optarg);
104 case 'd': /* Backward compatible. */
115 usage(style == COMPRESS);
123 (void)compress("/dev/stdin", "/dev/stdout", bits);
126 (void)decompress("/dev/stdin", "/dev/stdout", bits);
132 if (cat == 1 && argc > 1)
133 errx(1, "the -c option permits only a single file argument");
135 for (; *argv; ++argv)
138 if (strcmp(*argv, "-") == 0) {
139 compress("/dev/stdin", "/dev/stdout", bits);
142 compress(*argv, "/dev/stdout", bits);
145 if ((p = strrchr(*argv, '.')) != NULL &&
147 cwarnx("%s: name already has trailing .Z",
152 if (len > sizeof(newname) - 3) {
153 cwarnx("%s: name too long", *argv);
156 memmove(newname, *argv, len);
158 newname[len + 1] = 'Z';
159 newname[len + 2] = '\0';
160 compress(*argv, newname, bits);
163 if (strcmp(*argv, "-") == 0) {
164 decompress("/dev/stdin", "/dev/stdout", bits);
168 if ((p = strrchr(*argv, '.')) == NULL ||
170 if (len > sizeof(newname) - 3) {
171 cwarnx("%s: name too long", *argv);
174 memmove(newname, *argv, len);
176 newname[len + 1] = 'Z';
177 newname[len + 2] = '\0';
179 cat ? "/dev/stdout" : *argv, bits);
181 if (len - 2 > sizeof(newname) - 1) {
182 cwarnx("%s: name too long", *argv);
185 memmove(newname, *argv, len - 2);
186 newname[len - 2] = '\0';
188 cat ? "/dev/stdout" : newname, bits);
196 compress(const char *in, const char *out, int bits)
201 int exists, isreg, oreg;
204 exists = !stat(out, &sb);
205 if (!force && exists && S_ISREG(sb.st_mode) && !permission(out))
207 isreg = oreg = !exists || S_ISREG(sb.st_mode);
210 if ((ifp = fopen(in, "r")) == NULL) {
214 if (stat(in, &isb)) { /* DON'T FSTAT! */
218 if (!S_ISREG(isb.st_mode))
221 if ((ofp = zopen(out, "w", bits)) == NULL) {
225 while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
226 if (fwrite(buf, 1, nr, ofp) != nr) {
231 if (ferror(ifp) || fclose(ifp)) {
244 if (stat(out, &sb)) {
249 if (!force && sb.st_size >= isb.st_size) {
251 (void)fprintf(stderr, "%s: file would grow; left unmodified\n",
265 (void)fprintf(stderr, "%s: ", out);
266 if (isb.st_size > sb.st_size)
267 (void)fprintf(stderr, "%.0f%% compression\n",
268 ((float)sb.st_size / isb.st_size) * 100.0);
270 (void)fprintf(stderr, "%.0f%% expansion\n",
271 ((float)isb.st_size / sb.st_size) * 100.0);
286 decompress(const char *in, const char *out, int bits)
291 int exists, isreg, oreg;
294 exists = !stat(out, &sb);
295 if (!force && exists && S_ISREG(sb.st_mode) && !permission(out))
297 isreg = oreg = !exists || S_ISREG(sb.st_mode);
300 if ((ifp = zopen(in, "r", bits)) == NULL) {
308 if (!S_ISREG(sb.st_mode))
312 * Try to read the first few uncompressed bytes from the input file
313 * before blindly truncating the output file.
315 if ((nr = fread(buf, 1, sizeof(buf), ifp)) == 0) {
320 if ((ofp = fopen(out, "w")) == NULL ||
321 (nr != 0 && fwrite(buf, 1, nr, ofp) != nr)) {
327 while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
328 if (fwrite(buf, 1, nr, ofp) != nr) {
333 if (ferror(ifp) || fclose(ifp)) {
362 setfile(const char *name, struct stat *fs)
364 static struct timespec tspec[2];
366 fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
368 tspec[0] = fs->st_atim;
369 tspec[1] = fs->st_mtim;
370 if (utimensat(AT_FDCWD, name, tspec, 0))
371 cwarn("utimensat: %s", name);
374 * Changing the ownership probably won't succeed, unless we're root
375 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
376 * the mode; current BSD behavior is to remove all setuid bits on
377 * chown. If chown fails, lose setuid/setgid bits.
379 if (chown(name, fs->st_uid, fs->st_gid)) {
381 cwarn("chown: %s", name);
382 fs->st_mode &= ~(S_ISUID|S_ISGID);
384 if (chmod(name, fs->st_mode) && errno != EOPNOTSUPP)
385 cwarn("chmod: %s", name);
387 if (chflags(name, fs->st_flags) && errno != EOPNOTSUPP)
388 cwarn("chflags: %s", name);
392 permission(const char *fname)
396 if (!isatty(fileno(stderr)))
398 (void)fprintf(stderr, "overwrite %s? ", fname);
399 first = ch = getchar();
400 while (ch != '\n' && ch != EOF)
402 return (first == 'y');
406 usage(int iscompress)
409 (void)fprintf(stderr,
410 "usage: compress [-cfv] [-b bits] [file ...]\n");
412 (void)fprintf(stderr,
413 "usage: uncompress [-c] [-b bits] [file ...]\n");
418 cwarnx(const char *fmt, ...)
429 cwarn(const char *fmt, ...)