1 /* $NetBSD: compress.c,v 1.24 2008/07/21 14:19:22 lukem Exp $ */
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/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)compress.c 8.2 (Berkeley) 1/7/94";
42 __RCSID("$NetBSD: compress.c,v 1.24 2008/07/21 14:19:22 lukem Exp $");
46 #include <sys/param.h>
58 void compress(const char *, const char *, int);
59 void cwarn(const char *, ...) __attribute__((__format__(__printf__
,1,2)));
60 void cwarnx(const char *, ...) __attribute__((__format__(__printf__
,1,2)));
61 void decompress(const char *, const char *, int);
62 int permission(const char *);
63 void setfile(const char *, struct stat
*);
66 int main(int, char *[]);
67 extern FILE *zopen(const char *fname
, const char *mode
, int bits
);
69 int eval
, force
, verbose
;
70 int isstdout
, isstdin
;
73 main(int argc
, char **argv
)
75 enum {COMPRESS
, DECOMPRESS
} style
= COMPRESS
;
78 char *p
, newname
[MAXPATHLEN
];
80 if ((p
= strrchr(argv
[0], '/')) == NULL
)
84 if (!strcmp(p
, "uncompress"))
86 else if (!strcmp(p
, "compress"))
88 else if (!strcmp(p
, "zcat")) {
93 errx(1, "unknown program name");
96 while ((ch
= getopt(argc
, argv
, "b:cdfv")) != -1)
99 bits
= strtol(optarg
, &p
, 10);
101 errx(1, "illegal bit count -- %s", optarg
);
106 case 'd': /* Backward compatible. */
117 usage(style
== COMPRESS
);
127 (void)compress("/dev/stdin", "/dev/stdout", bits
);
132 (void)decompress("/dev/stdin", "/dev/stdout", bits
);
138 if (cat
== 1 && argc
> 1)
139 errx(1, "the -c option permits only a single file argument");
141 for (; *argv
; ++argv
) {
147 compress(*argv
, "/dev/stdout", bits
);
150 if ((p
= strrchr(*argv
, '.')) != NULL
&&
152 cwarnx("%s: name already has trailing .Z",
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';
165 compress(*argv
, newname
, bits
);
169 if ((p
= strrchr(*argv
, '.')) == NULL
||
171 if (len
> sizeof(newname
) - 3) {
172 cwarnx("%s: name too long", *argv
);
175 memmove(newname
, *argv
, len
);
177 newname
[len
+ 1] = 'Z';
178 newname
[len
+ 2] = '\0';
180 cat
? "/dev/stdout" : *argv
, bits
);
184 if (len
- 2 > sizeof(newname
) - 1) {
185 cwarnx("%s: name too long", *argv
);
188 memmove(newname
, *argv
, len
- 2);
189 newname
[len
- 2] = '\0';
191 cat
? "/dev/stdout" : newname
, bits
);
202 compress(const char *in
, const char *out
, int bits
)
206 const char *error
= NULL
;
208 int exists
, isreg
, oreg
;
212 exists
= !stat(out
, &sb
);
213 if (!force
&& exists
&& S_ISREG(sb
.st_mode
) && !permission(out
))
215 oreg
= !exists
|| S_ISREG(sb
.st_mode
);
220 if ((ifp
= fopen(in
, "r")) == NULL
) {
226 if (stat(in
, &isb
)) { /* DON'T FSTAT! */
230 if (!S_ISREG(isb
.st_mode
))
237 if ((ofp
= zopen(out
, "w", bits
)) == NULL
) {
242 while ((nr
= fread(buf
, 1, sizeof(buf
), ifp
)) != 0)
243 if (fwrite(buf
, 1, nr
, ofp
) != nr
) {
264 if (stat(out
, &sb
)) {
269 if (!force
&& sb
.st_size
>= isb
.st_size
) {
271 (void)printf("%s: file would grow; left unmodified\n", in
);
281 (void)printf("%s: ", out
);
282 if (isb
.st_size
> sb
.st_size
)
283 (void)printf("%.0f%% compression\n",
284 ((double)sb
.st_size
/ isb
.st_size
) * 100.0);
286 (void)printf("%.0f%% expansion\n",
287 ((double)isb
.st_size
/ sb
.st_size
) * 100.0);
301 decompress(const char *in
, const char *out
, int bits
)
306 int exists
, isreg
, oreg
;
310 exists
= !stat(out
, &sb
);
311 if (!force
&& exists
&& S_ISREG(sb
.st_mode
) && !permission(out
))
313 oreg
= !exists
|| S_ISREG(sb
.st_mode
);
318 if ((ofp
= fopen(out
, "w")) == NULL
) {
323 if ((ifp
= zopen(in
, "r", bits
)) == NULL
) {
332 if (!S_ISREG(sb
.st_mode
))
340 while ((nr
= fread(buf
, 1, sizeof(buf
), ifp
)) != 0)
341 if (fwrite(buf
, 1, nr
, ofp
) != nr
) {
380 setfile(const char *name
, struct stat
*fs
)
382 static struct timeval tv
[2];
384 fs
->st_mode
&= S_ISUID
|S_ISGID
|S_IRWXU
|S_IRWXG
|S_IRWXO
;
386 TIMESPEC_TO_TIMEVAL(&tv
[0], &fs
->st_atimespec
);
387 TIMESPEC_TO_TIMEVAL(&tv
[1], &fs
->st_mtimespec
);
388 if (utimes(name
, tv
))
389 cwarn("utimes: %s", name
);
392 * Changing the ownership probably won't succeed, unless we're root
393 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
394 * the mode; current BSD behavior is to remove all setuid bits on
395 * chown. If chown fails, lose setuid/setgid bits.
397 if (chown(name
, fs
->st_uid
, fs
->st_gid
)) {
399 cwarn("chown: %s", name
);
400 fs
->st_mode
&= ~(S_ISUID
|S_ISGID
);
402 if (chmod(name
, fs
->st_mode
))
403 cwarn("chown: %s", name
);
406 * Restore the file's flags. However, do this only if the original
407 * file had any flags set; this avoids a warning on file-systems that
408 * do not support flags.
410 if (fs
->st_flags
!= 0 && chflags(name
, fs
->st_flags
))
411 cwarn("chflags: %s", name
);
415 permission(const char *fname
)
419 if (!isatty(fileno(stderr
)))
421 (void)fprintf(stderr
, "overwrite %s? ", fname
);
422 first
= ch
= getchar();
423 while (ch
!= '\n' && ch
!= EOF
)
425 return (first
== 'y');
429 usage(int iscompress
)
432 (void)fprintf(stderr
,
433 "usage: compress [-cdfv] [-b bits] [file ...]\n");
435 (void)fprintf(stderr
,
436 "usage: uncompress [-cdfv] [-b bits] [file ...]\n");
441 cwarnx(const char *fmt
, ...)
452 cwarn(const char *fmt
, ...)