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. 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 $
34 #include <sys/param.h>
48 static void compress(const char *, const char *, int);
49 static void cwarn(const char *, ...) __printflike(1, 2);
50 static void cwarnx(const char *, ...) __printflike(1, 2);
51 static void decompress(const char *, const char *, int);
52 static int permission(const char *);
53 static void setfile(const char *, struct stat
*);
54 static void usage(int) __dead2
;
56 static int eval
, force
, verbose
;
59 main(int argc
, char **argv
)
61 enum {COMPRESS
, DECOMPRESS
} style
;
64 char *p
, newname
[MAXPATHLEN
];
67 if ((p
= strrchr(argv
[0], '/')) == NULL
)
71 if (!strcmp(p
, "uncompress"))
73 else if (!strcmp(p
, "compress"))
75 else if (!strcmp(p
, "zcat")) {
79 errx(1, "unknown program name");
82 while ((ch
= getopt(argc
, argv
, "b:cdfv")) != -1)
85 bits
= strtol(optarg
, &p
, 10);
87 errx(1, "illegal bit count -- %s", optarg
);
92 case 'd': /* Backward compatible. */
103 usage(style
== COMPRESS
);
111 (void)compress("/dev/stdin", "/dev/stdout", bits
);
114 (void)decompress("/dev/stdin", "/dev/stdout", bits
);
120 if (cat
== 1 && argc
> 1)
121 errx(1, "the -c option permits only a single file argument");
123 for (; *argv
; ++argv
)
126 if (strcmp(*argv
, "-") == 0) {
127 compress("/dev/stdin", "/dev/stdout", bits
);
130 compress(*argv
, "/dev/stdout", bits
);
133 if ((p
= strrchr(*argv
, '.')) != NULL
&&
135 cwarnx("%s: name already has trailing .Z",
140 if (len
> sizeof(newname
) - 3) {
141 cwarnx("%s: name too long", *argv
);
144 memmove(newname
, *argv
, len
);
146 newname
[len
+ 1] = 'Z';
147 newname
[len
+ 2] = '\0';
148 compress(*argv
, newname
, bits
);
151 if (strcmp(*argv
, "-") == 0) {
152 decompress("/dev/stdin", "/dev/stdout", bits
);
156 if ((p
= strrchr(*argv
, '.')) == NULL
||
158 if (len
> sizeof(newname
) - 3) {
159 cwarnx("%s: name too long", *argv
);
162 memmove(newname
, *argv
, len
);
164 newname
[len
+ 1] = 'Z';
165 newname
[len
+ 2] = '\0';
167 cat
? "/dev/stdout" : *argv
, bits
);
169 if (len
- 2 > sizeof(newname
) - 1) {
170 cwarnx("%s: name too long", *argv
);
173 memmove(newname
, *argv
, len
- 2);
174 newname
[len
- 2] = '\0';
176 cat
? "/dev/stdout" : newname
, bits
);
184 compress(const char *in
, const char *out
, int bits
)
189 int exists
, isreg
, oreg
;
192 exists
= !stat(out
, &sb
);
193 if (!force
&& exists
&& S_ISREG(sb
.st_mode
) && !permission(out
))
195 isreg
= oreg
= !exists
|| S_ISREG(sb
.st_mode
);
198 if ((ifp
= fopen(in
, "r")) == NULL
) {
202 if (stat(in
, &isb
)) { /* DON'T FSTAT! */
206 if (!S_ISREG(isb
.st_mode
))
209 if ((ofp
= zopen(out
, "w", bits
)) == NULL
) {
213 while ((nr
= fread(buf
, 1, sizeof(buf
), ifp
)) != 0)
214 if (fwrite(buf
, 1, nr
, ofp
) != nr
) {
219 if (ferror(ifp
) || fclose(ifp
)) {
232 if (stat(out
, &sb
)) {
237 if (!force
&& sb
.st_size
>= isb
.st_size
) {
239 (void)fprintf(stderr
, "%s: file would grow; left unmodified\n",
253 (void)fprintf(stderr
, "%s: ", out
);
254 if (isb
.st_size
> sb
.st_size
)
255 (void)fprintf(stderr
, "%.0f%% compression\n",
256 ((float)sb
.st_size
/ isb
.st_size
) * 100.0);
258 (void)fprintf(stderr
, "%.0f%% expansion\n",
259 ((float)isb
.st_size
/ sb
.st_size
) * 100.0);
274 decompress(const char *in
, const char *out
, int bits
)
279 int exists
, isreg
, oreg
;
282 exists
= !stat(out
, &sb
);
283 if (!force
&& exists
&& S_ISREG(sb
.st_mode
) && !permission(out
))
285 isreg
= oreg
= !exists
|| S_ISREG(sb
.st_mode
);
288 if ((ofp
= fopen(out
, "w")) == NULL
) {
293 if ((ifp
= zopen(in
, "r", bits
)) == NULL
) {
301 if (!S_ISREG(sb
.st_mode
))
304 while ((nr
= fread(buf
, 1, sizeof(buf
), ifp
)) != 0)
305 if (fwrite(buf
, 1, nr
, ofp
) != nr
) {
310 if (ferror(ifp
) || fclose(ifp
)) {
339 setfile(const char *name
, struct stat
*fs
)
341 static struct timeval tv
[2];
343 fs
->st_mode
&= S_ISUID
|S_ISGID
|S_IRWXU
|S_IRWXG
|S_IRWXO
;
345 TIMESPEC_TO_TIMEVAL(&tv
[0], &fs
->st_atimespec
);
346 TIMESPEC_TO_TIMEVAL(&tv
[1], &fs
->st_mtimespec
);
347 if (utimes(name
, tv
))
348 cwarn("utimes: %s", name
);
351 * Changing the ownership probably won't succeed, unless we're root
352 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
353 * the mode; current BSD behavior is to remove all setuid bits on
354 * chown. If chown fails, lose setuid/setgid bits.
356 if (chown(name
, fs
->st_uid
, fs
->st_gid
)) {
358 cwarn("chown: %s", name
);
359 fs
->st_mode
&= ~(S_ISUID
|S_ISGID
);
361 if (chmod(name
, fs
->st_mode
) && errno
!= EOPNOTSUPP
)
362 cwarn("chmod: %s", name
);
364 if (chflags(name
, fs
->st_flags
) && errno
!= EOPNOTSUPP
)
365 cwarn("chflags: %s", name
);
369 permission(const char *fname
)
373 if (!isatty(fileno(stderr
)))
375 (void)fprintf(stderr
, "overwrite %s? ", fname
);
376 first
= ch
= getchar();
377 while (ch
!= '\n' && ch
!= EOF
)
379 return (first
== 'y');
383 usage(int iscompress
)
386 (void)fprintf(stderr
,
387 "usage: compress [-cfv] [-b bits] [file ...]\n");
389 (void)fprintf(stderr
,
390 "usage: uncompress [-c] [-b bits] [file ...]\n");
395 cwarnx(const char *fmt
, ...)
406 cwarn(const char *fmt
, ...)