2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/param.h>
47 static void compress(const char *, const char *, int);
48 static void cwarn(const char *, ...) __printflike(1, 2);
49 static void cwarnx(const char *, ...) __printflike(1, 2);
50 static void decompress(const char *, const char *, int);
51 static int permission(const char *);
52 static void setfile(const char *, struct stat *);
53 static void usage(int);
55 static int eval, force, verbose;
58 main(int argc, char *argv[])
60 enum {COMPRESS, DECOMPRESS} style;
63 char *p, newname[MAXPATHLEN];
66 if ((p = strrchr(argv[0], '/')) == NULL)
70 if (!strcmp(p, "uncompress"))
72 else if (!strcmp(p, "compress"))
74 else if (!strcmp(p, "zcat")) {
78 errx(1, "unknown program name");
81 while ((ch = getopt(argc, argv, "b:cdfv")) != -1)
84 bits = strtol(optarg, &p, 10);
86 errx(1, "illegal bit count -- %s", optarg);
91 case 'd': /* Backward compatible. */
102 usage(style == COMPRESS);
110 (void)compress("/dev/stdin", "/dev/stdout", bits);
113 (void)decompress("/dev/stdin", "/dev/stdout", bits);
119 if (cat == 1 && style == COMPRESS && argc > 1)
120 errx(1, "the -c option permits only a single file argument");
122 for (; *argv; ++argv)
125 if (strcmp(*argv, "-") == 0) {
126 compress("/dev/stdin", "/dev/stdout", bits);
129 compress(*argv, "/dev/stdout", bits);
132 if ((p = strrchr(*argv, '.')) != NULL &&
134 cwarnx("%s: name already has trailing .Z",
139 if (len > sizeof(newname) - 3) {
140 cwarnx("%s: name too long", *argv);
143 memmove(newname, *argv, len);
145 newname[len + 1] = 'Z';
146 newname[len + 2] = '\0';
147 compress(*argv, newname, bits);
150 if (strcmp(*argv, "-") == 0) {
151 decompress("/dev/stdin", "/dev/stdout", bits);
155 if ((p = strrchr(*argv, '.')) == NULL ||
157 if (len > sizeof(newname) - 3) {
158 cwarnx("%s: name too long", *argv);
161 memmove(newname, *argv, len);
163 newname[len + 1] = 'Z';
164 newname[len + 2] = '\0';
166 cat ? "/dev/stdout" : *argv, bits);
168 if (len - 2 > sizeof(newname) - 1) {
169 cwarnx("%s: name too long", *argv);
172 memmove(newname, *argv, len - 2);
173 newname[len - 2] = '\0';
175 cat ? "/dev/stdout" : newname, bits);
183 compress(const char *in, const char *out, int bits)
188 int exists, isreg, oreg;
191 exists = !stat(out, &sb);
192 if (!force && exists && S_ISREG(sb.st_mode) && !permission(out))
194 isreg = oreg = !exists || S_ISREG(sb.st_mode);
197 if ((ifp = fopen(in, "r")) == NULL) {
201 if (stat(in, &isb)) { /* DON'T FSTAT! */
205 if (!S_ISREG(isb.st_mode))
208 if ((ofp = zopen(out, "w", bits)) == NULL) {
212 while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
213 if (fwrite(buf, 1, nr, ofp) != nr) {
218 if (ferror(ifp) || fclose(ifp)) {
231 if (stat(out, &sb)) {
236 if (!force && sb.st_size >= isb.st_size) {
238 (void)fprintf(stderr, "%s: file would grow; left unmodified\n",
252 (void)fprintf(stderr, "%s: ", out);
253 if (isb.st_size > sb.st_size)
254 (void)fprintf(stderr, "%.0f%% compression\n",
255 ((float)sb.st_size / isb.st_size) * 100.0);
257 (void)fprintf(stderr, "%.0f%% expansion\n",
258 ((float)isb.st_size / sb.st_size) * 100.0);
273 decompress(const char *in, const char *out, int bits)
278 int exists, isreg, oreg;
281 exists = !stat(out, &sb);
282 if (!force && exists && S_ISREG(sb.st_mode) && !permission(out))
284 isreg = oreg = !exists || S_ISREG(sb.st_mode);
287 if ((ifp = zopen(in, "r", bits)) == NULL) {
295 if (!S_ISREG(sb.st_mode))
299 * Try to read the first few uncompressed bytes from the input file
300 * before blindly truncating the output file.
302 if ((nr = fread(buf, 1, sizeof(buf), ifp)) == 0) {
307 if ((ofp = fopen(out, "w")) == NULL ||
308 (nr != 0 && fwrite(buf, 1, nr, ofp) != nr)) {
316 while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
317 if (fwrite(buf, 1, nr, ofp) != nr) {
322 if (ferror(ifp) || fclose(ifp)) {
351 setfile(const char *name, struct stat *fs)
353 static struct timespec tspec[2];
355 fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
357 tspec[0] = fs->st_atim;
358 tspec[1] = fs->st_mtim;
359 if (utimensat(AT_FDCWD, name, tspec, 0))
360 cwarn("utimensat: %s", name);
363 * Changing the ownership probably won't succeed, unless we're root
364 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
365 * the mode; current BSD behavior is to remove all setuid bits on
366 * chown. If chown fails, lose setuid/setgid bits.
368 if (chown(name, fs->st_uid, fs->st_gid)) {
370 cwarn("chown: %s", name);
371 fs->st_mode &= ~(S_ISUID|S_ISGID);
373 if (chmod(name, fs->st_mode) && errno != EOPNOTSUPP)
374 cwarn("chmod: %s", name);
376 if (chflags(name, fs->st_flags) && errno != EOPNOTSUPP)
377 cwarn("chflags: %s", name);
381 permission(const char *fname)
385 if (!isatty(fileno(stderr)))
387 (void)fprintf(stderr, "overwrite %s? ", fname);
388 first = ch = getchar();
389 while (ch != '\n' && ch != EOF)
391 return (first == 'y');
395 usage(int iscompress)
398 (void)fprintf(stderr,
399 "usage: compress [-cfv] [-b bits] [file ...]\n");
401 (void)fprintf(stderr,
402 "usage: uncompress [-c] [-b bits] [file ...]\n");
407 cwarnx(const char *fmt, ...)
418 cwarn(const char *fmt, ...)