]>
git.saurik.com Git - apple/file_cmds.git/blob - compress/compress.c
1 /* $NetBSD: compress.c,v 1.16 1998/03/10 12:45:44 kleink 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
38 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
44 static char sccsid
[] = "@(#)compress.c 8.2 (Berkeley) 1/7/94";
46 __RCSID("$NetBSD: compress.c,v 1.16 1998/03/10 12:45:44 kleink Exp $");
50 #include <sys/param.h>
67 void compress
__P((char *, char *, int));
68 void cwarn
__P((const char *, ...));
69 void cwarnx
__P((const char *, ...));
70 void decompress
__P((char *, char *, int));
71 int permission
__P((char *));
72 void setfile
__P((char *, struct stat
*));
73 void usage
__P((int));
75 int main
__P((int, char *[]));
76 extern FILE *zopen
__P((const char *fname
, const char *mode
, int bits
));
78 int eval
, force
, verbose
;
79 int isstdout
, isstdin
;
86 enum {COMPRESS
, DECOMPRESS
} style
= COMPRESS
;
89 char *p
, newname
[MAXPATHLEN
];
91 if ((p
= strrchr(argv
[0], '/')) == NULL
)
95 if (!strcmp(p
, "uncompress"))
97 else if (!strcmp(p
, "compress"))
99 else if (!strcmp(p
, "zcat")) {
104 errx(1, "unknown program name");
107 while ((ch
= getopt(argc
, argv
, "b:cdfv")) != -1)
110 bits
= strtol(optarg
, &p
, 10);
112 errx(1, "illegal bit count -- %s", optarg
);
117 case 'd': /* Backward compatible. */
128 usage(style
== COMPRESS
);
138 (void)compress("/dev/stdin", "/dev/stdout", bits
);
143 (void)decompress("/dev/stdin", "/dev/stdout", bits
);
149 if (cat
== 1 && argc
> 1)
150 errx(1, "the -c option permits only a single file argument");
152 for (; *argv
; ++argv
) {
158 compress(*argv
, "/dev/stdout", bits
);
161 if ((p
= strrchr(*argv
, '.')) != NULL
&&
163 cwarnx("%s: name already has trailing .Z",
168 if (len
> sizeof(newname
) - 3) {
169 cwarnx("%s: name too long", *argv
);
172 memmove(newname
, *argv
, len
);
174 newname
[len
+ 1] = 'Z';
175 newname
[len
+ 2] = '\0';
176 compress(*argv
, newname
, bits
);
180 if ((p
= strrchr(*argv
, '.')) == NULL
||
182 if (len
> sizeof(newname
) - 3) {
183 cwarnx("%s: name too long", *argv
);
186 memmove(newname
, *argv
, len
);
188 newname
[len
+ 1] = 'Z';
189 newname
[len
+ 2] = '\0';
191 cat
? "/dev/stdout" : *argv
, bits
);
195 if (len
- 2 > sizeof(newname
) - 1) {
196 cwarnx("%s: name too long", *argv
);
199 memmove(newname
, *argv
, len
- 2);
200 newname
[len
- 2] = '\0';
202 cat
? "/dev/stdout" : newname
, bits
);
213 compress(in
, out
, bits
)
220 int exists
, isreg
, oreg
;
224 exists
= !stat(out
, &sb
);
225 if (!force
&& exists
&& S_ISREG(sb
.st_mode
) && !permission(out
))
227 oreg
= !exists
|| S_ISREG(sb
.st_mode
);
232 if ((ifp
= fopen(in
, "r")) == NULL
) {
238 if (stat(in
, &isb
)) { /* DON'T FSTAT! */
242 if (!S_ISREG(isb
.st_mode
))
249 if ((ofp
= zopen(out
, "w", bits
)) == NULL
) {
253 while ((nr
= fread(buf
, 1, sizeof(buf
), ifp
)) != 0)
254 if (fwrite(buf
, 1, nr
, ofp
) != nr
) {
259 if (ferror(ifp
) || fclose(ifp
)) {
272 if (stat(out
, &sb
)) {
277 if (!force
&& sb
.st_size
>= isb
.st_size
) {
279 (void)printf("%s: file would grow; left unmodified\n", in
);
291 (void)printf("%s: ", out
);
292 if (isb
.st_size
> sb
.st_size
)
293 (void)printf("%.0f%% compression\n",
294 ((double)sb
.st_size
/ isb
.st_size
) * 100.0);
296 (void)printf("%.0f%% expansion\n",
297 ((double)isb
.st_size
/ sb
.st_size
) * 100.0);
312 decompress(in
, out
, bits
)
319 int exists
, isreg
, oreg
;
323 exists
= !stat(out
, &sb
);
324 if (!force
&& exists
&& S_ISREG(sb
.st_mode
) && !permission(out
))
326 oreg
= !exists
|| S_ISREG(sb
.st_mode
);
331 if ((ofp
= fopen(out
, "w")) == NULL
) {
336 if ((ifp
= zopen(in
, "r", bits
)) == NULL
) {
345 if (!S_ISREG(sb
.st_mode
))
352 while ((nr
= fread(buf
, 1, sizeof(buf
), ifp
)) != 0)
353 if (fwrite(buf
, 1, nr
, ofp
) != nr
) {
358 if (ferror(ifp
) || fclose(ifp
)) {
391 static struct timeval tv
[2];
393 fs
->st_mode
&= S_ISUID
|S_ISGID
|S_IRWXU
|S_IRWXG
|S_IRWXO
;
395 TIMESPEC_TO_TIMEVAL(&tv
[0], &fs
->st_atimespec
);
396 TIMESPEC_TO_TIMEVAL(&tv
[1], &fs
->st_mtimespec
);
397 if (utimes(name
, tv
))
398 cwarn("utimes: %s", name
);
401 * Changing the ownership probably won't succeed, unless we're root
402 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
403 * the mode; current BSD behavior is to remove all setuid bits on
404 * chown. If chown fails, lose setuid/setgid bits.
406 if (chown(name
, fs
->st_uid
, fs
->st_gid
)) {
408 cwarn("chown: %s", name
);
409 fs
->st_mode
&= ~(S_ISUID
|S_ISGID
);
411 if (chmod(name
, fs
->st_mode
))
412 cwarn("chown: %s", name
);
415 * Restore the file's flags. However, do this only if the original
416 * file had any flags set; this avoids a warning on file-systems that
417 * do not support flags.
419 if (fs
->st_flags
!= 0 && chflags(name
, fs
->st_flags
))
420 cwarn("chflags: %s", name
);
429 if (!isatty(fileno(stderr
)))
431 (void)fprintf(stderr
, "overwrite %s? ", fname
);
432 first
= ch
= getchar();
433 while (ch
!= '\n' && ch
!= EOF
)
435 return (first
== 'y');
443 (void)fprintf(stderr
,
444 "usage: compress [-cfv] [-b bits] [file ...]\n");
446 (void)fprintf(stderr
,
447 "usage: uncompress [-c] [-b bits] [file ...]\n");
453 cwarnx(const char *fmt
, ...)
455 cwarnx(fmt
, va_alist
)
474 cwarn(const char *fmt
, ...)