]>
git.saurik.com Git - apple/file_cmds.git/blob - compress/compress.c
17d100f4109e9c008c95dffff5e423978c49fd93
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
30 #include <sys/cdefs.h>
32 __used
static const char copyright
[] =
33 "@(#) Copyright (c) 1992, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
39 static char sccsid
[] = "@(#)compress.c 8.2 (Berkeley) 1/7/94";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD: src/usr.bin/compress/compress.c,v 1.23 2010/12/11 08:32:16 joel Exp $");
46 #include <sys/param.h>
60 void compress(const char *, const char *, int);
61 void cwarn(const char *, ...) __printflike(1, 2);
62 void cwarnx(const char *, ...) __printflike(1, 2);
63 void decompress(const char *, const char *, int);
64 int permission(const char *);
65 void setfile(const char *, struct stat
*);
68 int eval
, force
, verbose
, cat
;
71 main(int argc
, char *argv
[])
73 enum {COMPRESS
, DECOMPRESS
} style
;
76 char *p
, newname
[MAXPATHLEN
];
81 if ((p
= rindex(argv
[0], '/')) == NULL
)
85 if (!strcmp(p
, "uncompress"))
87 else if (!strcmp(p
, "compress"))
89 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
);
126 (void)compress("/dev/stdin", "/dev/stdout", bits
);
129 (void)decompress("/dev/stdin", "/dev/stdout", bits
);
136 * The UNIX standard requires that `uncompress -c` be able to have multiple file parameters given.
139 for (; *argv
; ++argv
)
142 if (strcmp(*argv
, "-") == 0) {
144 compress("/dev/stdin", "/dev/stdout", bits
);
147 compress(*argv
, "/dev/stdout", bits
);
150 if ((p
= rindex(*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
);
168 if (strcmp(*argv
, "-") == 0) {
170 decompress("/dev/stdin", "/dev/stdout", bits
);
174 if ((p
= rindex(*argv
, '.')) == NULL
||
176 if (len
> sizeof(newname
) - 3) {
177 cwarnx("%s: name too long", *argv
);
180 memmove(newname
, *argv
, len
);
182 newname
[len
+ 1] = 'Z';
183 newname
[len
+ 2] = '\0';
185 cat
? "/dev/stdout" : *argv
, bits
);
187 if (len
- 2 > sizeof(newname
) - 1) {
188 cwarnx("%s: name too long", *argv
);
191 memmove(newname
, *argv
, len
- 2);
192 newname
[len
- 2] = '\0';
194 cat
? "/dev/stdout" : newname
, bits
);
202 compress(const char *in
, const char *out
, int bits
)
206 FILE *ifp
= NULL
, *ofp
= NULL
;
207 int exists
, isreg
, oreg
;
210 exists
= !stat(out
, &sb
);
211 if (!force
&& exists
&& S_ISREG(sb
.st_mode
) && !cat
&& !permission(out
)) {
212 cwarnx("%s already exists", out
);
215 isreg
= oreg
= !exists
|| S_ISREG(sb
.st_mode
);
217 if ((ifp
= fopen(in
, "r")) == NULL
) {
221 if (stat(in
, &isb
)) { /* DON'T FSTAT! */
225 if (!S_ISREG(isb
.st_mode
))
228 if ((ofp
= zopen(out
, "w", bits
)) == NULL
) {
232 while ((nr
= fread(buf
, 1, sizeof(buf
), ifp
)) != 0)
233 if (fwrite(buf
, 1, nr
, ofp
) != nr
) {
238 if (ferror(ifp
) || fclose(ifp
)) {
251 if (stat(out
, &sb
)) {
256 if (!force
&& sb
.st_size
>= isb
.st_size
) {
258 (void)fprintf(stderr
, "%s: file would grow; left unmodified\n",
272 (void)fprintf(stderr
, "%s: ", out
);
273 if (isb
.st_size
> sb
.st_size
)
274 (void)fprintf(stderr
, "%.0f%% compression\n",
275 ((float)sb
.st_size
/ isb
.st_size
) * 100.0);
277 (void)fprintf(stderr
, "%.0f%% expansion\n",
278 ((float)isb
.st_size
/ sb
.st_size
) * 100.0);
293 decompress(const char *in
, const char *out
, int bits
)
298 int exists
, isreg
, oreg
;
301 exists
= !stat(out
, &sb
);
302 if (!force
&& exists
&& S_ISREG(sb
.st_mode
) && !cat
&& !permission(out
)) {
303 cwarnx("%s already exists", out
);
306 isreg
= oreg
= !exists
|| S_ISREG(sb
.st_mode
);
309 if ((ifp
= zopen(in
, "r", bits
)) == NULL
) {
317 if (!S_ISREG(sb
.st_mode
))
321 * Try to read the first few uncompressed bytes from the input file
322 * before blindly truncating the output file.
324 if ((nr
= fread(buf
, 1, sizeof(buf
), ifp
)) == 0) {
329 if ((ofp
= fopen(out
, "w")) == NULL
||
330 (nr
!= 0 && fwrite(buf
, 1, nr
, ofp
) != nr
)) {
336 while ((nr
= fread(buf
, 1, sizeof(buf
), ifp
)) != 0)
337 if (fwrite(buf
, 1, nr
, ofp
) != nr
) {
342 if (ferror(ifp
) || fclose(ifp
)) {
359 struct stat isb
= sb
;
361 (void)fprintf(stderr
, "%s: ", out
);
362 if (isb
.st_size
> sb
.st_size
)
363 (void)fprintf(stderr
, "%.0f%% compression\n",
364 ((float)sb
.st_size
/ isb
.st_size
) * 100.0);
366 (void)fprintf(stderr
, "%.0f%% expansion\n",
367 ((float)isb
.st_size
/ sb
.st_size
) * 100.0);
382 setfile(const char *name
, struct stat
*fs
)
384 static struct timeval tv
[2];
386 fs
->st_mode
&= S_ISUID
|S_ISGID
|S_IRWXU
|S_IRWXG
|S_IRWXO
;
388 TIMESPEC_TO_TIMEVAL(&tv
[0], &fs
->st_atimespec
);
389 TIMESPEC_TO_TIMEVAL(&tv
[1], &fs
->st_mtimespec
);
390 if (utimes(name
, tv
))
391 cwarn("utimes: %s", name
);
394 * Changing the ownership probably won't succeed, unless we're root
395 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
396 * the mode; current BSD behavior is to remove all setuid bits on
397 * chown. If chown fails, lose setuid/setgid bits.
399 if (chown(name
, fs
->st_uid
, fs
->st_gid
)) {
401 cwarn("chown: %s", name
);
402 fs
->st_mode
&= ~(S_ISUID
|S_ISGID
);
404 if (chmod(name
, fs
->st_mode
) && errno
!= ENOTSUP
)
405 cwarn("chmod: %s", name
);
407 if (chflags(name
, fs
->st_flags
) && errno
!= ENOTSUP
)
408 cwarn("chflags: %s", name
);
412 permission(const char *fname
)
416 if (!isatty(fileno(stderr
)))
418 (void)fprintf(stderr
, "overwrite %s? ", fname
);
419 first
= ch
= getchar();
420 while (ch
!= '\n' && ch
!= EOF
)
422 return (first
== 'y');
426 usage(int iscompress
)
429 (void)fprintf(stderr
,
430 "usage: compress [-cfv] [-b bits] [file ...]\n");
432 (void)fprintf(stderr
,
433 "usage: uncompress [-cfv] [file ...]\n");
438 cwarnx(const char *fmt
, ...)
449 cwarn(const char *fmt
, ...)