]>
git.cameronkatri.com Git - apple_cmds.git/blob - file_cmds/compress/compress.c
97c70f291e278016d2e8e84133f16c35ee81569f
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>
62 void compress(const char *, const char *, int);
63 void cwarn(const char *, ...) __printflike(1, 2);
64 void cwarnx(const char *, ...) __printflike(1, 2);
65 void decompress(const char *, const char *, int);
66 int permission(const char *);
67 void setfile(const char *, struct stat
*);
70 int eval
, force
, verbose
, cat
;
73 main(int argc
, char *argv
[])
75 enum {COMPRESS
, DECOMPRESS
} style
;
78 char *p
, newname
[MAXPATHLEN
];
83 if ((p
= rindex(argv
[0], '/')) == NULL
)
87 if (!strcmp(p
, "uncompress"))
89 else if (!strcmp(p
, "compress"))
91 else if (!strcmp(p
, "zcat")) {
95 errx(1, "unknown program name");
98 while ((ch
= getopt(argc
, argv
, "b:cdfv")) != -1)
101 bits
= strtol(optarg
, &p
, 10);
103 errx(1, "illegal bit count -- %s", optarg
);
108 case 'd': /* Backward compatible. */
119 usage(style
== COMPRESS
);
128 (void)compress("/dev/stdin", "/dev/stdout", bits
);
131 (void)decompress("/dev/stdin", "/dev/stdout", bits
);
138 * The UNIX standard requires that `uncompress -c` be able to have multiple file parameters given.
141 for (; *argv
; ++argv
)
144 if (strcmp(*argv
, "-") == 0) {
146 compress("/dev/stdin", "/dev/stdout", bits
);
149 compress(*argv
, "/dev/stdout", bits
);
152 if ((p
= rindex(*argv
, '.')) != NULL
&&
154 cwarnx("%s: name already has trailing .Z",
159 if (len
> sizeof(newname
) - 3) {
160 cwarnx("%s: name too long", *argv
);
163 memmove(newname
, *argv
, len
);
165 newname
[len
+ 1] = 'Z';
166 newname
[len
+ 2] = '\0';
167 compress(*argv
, newname
, bits
);
170 if (strcmp(*argv
, "-") == 0) {
172 decompress("/dev/stdin", "/dev/stdout", bits
);
176 if ((p
= rindex(*argv
, '.')) == NULL
||
178 if (len
> sizeof(newname
) - 3) {
179 cwarnx("%s: name too long", *argv
);
182 memmove(newname
, *argv
, len
);
184 newname
[len
+ 1] = 'Z';
185 newname
[len
+ 2] = '\0';
187 cat
? "/dev/stdout" : *argv
, bits
);
189 if (len
- 2 > sizeof(newname
) - 1) {
190 cwarnx("%s: name too long", *argv
);
193 memmove(newname
, *argv
, len
- 2);
194 newname
[len
- 2] = '\0';
196 cat
? "/dev/stdout" : newname
, bits
);
204 compress(const char *in
, const char *out
, int bits
)
208 FILE *ifp
= NULL
, *ofp
= NULL
;
209 int exists
, isreg
, oreg
;
212 exists
= !stat(out
, &sb
);
213 if (!force
&& exists
&& S_ISREG(sb
.st_mode
) && !cat
&& !permission(out
)) {
214 cwarnx("%s already exists", out
);
217 isreg
= oreg
= !exists
|| S_ISREG(sb
.st_mode
);
219 if ((ifp
= fopen(in
, "r")) == NULL
) {
223 if (stat(in
, &isb
)) { /* DON'T FSTAT! */
227 if (!S_ISREG(isb
.st_mode
))
230 if ((ofp
= zopen(out
, "w", bits
)) == NULL
) {
234 while ((nr
= fread(buf
, 1, sizeof(buf
), ifp
)) != 0)
235 if (fwrite(buf
, 1, nr
, ofp
) != nr
) {
240 if (ferror(ifp
) || fclose(ifp
)) {
253 if (stat(out
, &sb
)) {
258 if (!force
&& sb
.st_size
>= isb
.st_size
) {
260 (void)fprintf(stderr
, "%s: file would grow; left unmodified\n",
274 (void)fprintf(stderr
, "%s: ", out
);
275 if (isb
.st_size
> sb
.st_size
)
276 (void)fprintf(stderr
, "%.0f%% compression\n",
277 ((float)sb
.st_size
/ isb
.st_size
) * 100.0);
279 (void)fprintf(stderr
, "%.0f%% expansion\n",
280 ((float)isb
.st_size
/ sb
.st_size
) * 100.0);
295 decompress(const char *in
, const char *out
, int bits
)
300 int exists
, isreg
, oreg
;
303 exists
= !stat(out
, &sb
);
304 if (!force
&& exists
&& S_ISREG(sb
.st_mode
) && !cat
&& !permission(out
)) {
305 cwarnx("%s already exists", out
);
308 isreg
= oreg
= !exists
|| S_ISREG(sb
.st_mode
);
311 if ((ifp
= zopen(in
, "r", bits
)) == NULL
) {
319 if (!S_ISREG(sb
.st_mode
))
323 * Try to read the first few uncompressed bytes from the input file
324 * before blindly truncating the output file.
326 if ((nr
= fread(buf
, 1, sizeof(buf
), ifp
)) == 0) {
331 if ((ofp
= fopen(out
, "w")) == NULL
||
332 (nr
!= 0 && fwrite(buf
, 1, nr
, ofp
) != nr
)) {
338 while ((nr
= fread(buf
, 1, sizeof(buf
), ifp
)) != 0)
339 if (fwrite(buf
, 1, nr
, ofp
) != nr
) {
344 if (ferror(ifp
) || fclose(ifp
)) {
361 struct stat isb
= sb
;
363 (void)fprintf(stderr
, "%s: ", out
);
364 if (isb
.st_size
> sb
.st_size
)
365 (void)fprintf(stderr
, "%.0f%% compression\n",
366 ((float)sb
.st_size
/ isb
.st_size
) * 100.0);
368 (void)fprintf(stderr
, "%.0f%% expansion\n",
369 ((float)isb
.st_size
/ sb
.st_size
) * 100.0);
384 setfile(const char *name
, struct stat
*fs
)
386 struct attrlist ts_req
= {};
388 struct timespec mtime
;
389 struct timespec atime
;
392 fs
->st_mode
&= S_ISUID
|S_ISGID
|S_IRWXU
|S_IRWXG
|S_IRWXO
;
394 ts_req
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
395 ts_req
.commonattr
= ATTR_CMN_MODTIME
| ATTR_CMN_ACCTIME
;
396 set_ts
.mtime
= fs
->st_mtimespec
;
397 set_ts
.atime
= fs
->st_atimespec
;
399 if (setattrlist(name
, &ts_req
, &set_ts
, sizeof(set_ts
), 0))
400 cwarn("setattrlist: %s", name
);
403 * Changing the ownership probably won't succeed, unless we're root
404 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
405 * the mode; current BSD behavior is to remove all setuid bits on
406 * chown. If chown fails, lose setuid/setgid bits.
408 if (chown(name
, fs
->st_uid
, fs
->st_gid
)) {
410 cwarn("chown: %s", name
);
411 fs
->st_mode
&= ~(S_ISUID
|S_ISGID
);
413 if (chmod(name
, fs
->st_mode
) && errno
!= ENOTSUP
)
414 cwarn("chmod: %s", name
);
416 if (chflags(name
, fs
->st_flags
) && errno
!= ENOTSUP
)
417 cwarn("chflags: %s", name
);
421 permission(const char *fname
)
424 char resp
[] = {'\0', '\0'};
426 if (!isatty(fileno(stderr
)))
428 (void)fprintf(stderr
, "overwrite %s? ", fname
);
430 /* Load user specified locale */
431 setlocale(LC_MESSAGES
, "");
433 first
= ch
= getchar();
434 while (ch
!= '\n' && ch
!= EOF
)
437 /* only care about first character */
440 return (rpmatch(resp
) == 1);
444 usage(int iscompress
)
447 (void)fprintf(stderr
,
448 "usage: compress [-cfv] [-b bits] [file ...]\n");
450 (void)fprintf(stderr
,
451 "usage: uncompress [-cfv] [file ...]\n");
456 cwarnx(const char *fmt
, ...)
467 cwarn(const char *fmt
, ...)