]> git.saurik.com Git - apple/file_cmds.git/blame - compress/compress.c
file_cmds-321.100.10.0.1.tar.gz
[apple/file_cmds.git] / compress / compress.c
CommitLineData
44a7a5ab
A
1/*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
44a7a5ab
A
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.
16 *
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
27 * SUCH DAMAGE.
28 */
29
00337e45 30#include <sys/cdefs.h>
44a7a5ab 31#ifndef lint
00337e45 32__used static const char copyright[] =
c59d3020
A
33"@(#) Copyright (c) 1992, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
35#endif
44a7a5ab 36
44a7a5ab 37#if 0
c59d3020 38#ifndef lint
44a7a5ab 39static char sccsid[] = "@(#)compress.c 8.2 (Berkeley) 1/7/94";
44a7a5ab 40#endif
c59d3020
A
41#endif
42
43#include <sys/cdefs.h>
e19e38b2 44__FBSDID("$FreeBSD: src/usr.bin/compress/compress.c,v 1.23 2010/12/11 08:32:16 joel Exp $");
44a7a5ab
A
45
46#include <sys/param.h>
44a7a5ab 47#include <sys/stat.h>
c59d3020 48#include <sys/time.h>
867b3d41 49#include <sys/attr.h>
44a7a5ab
A
50
51#include <err.h>
52#include <errno.h>
c59d3020 53#include <stdarg.h>
44a7a5ab
A
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include <unistd.h>
48fce603 58#include <locale.h>
44a7a5ab 59
c59d3020 60#include "zopen.h"
44a7a5ab 61
c59d3020
A
62void compress(const char *, const char *, int);
63void cwarn(const char *, ...) __printflike(1, 2);
64void cwarnx(const char *, ...) __printflike(1, 2);
65void decompress(const char *, const char *, int);
66int permission(const char *);
67void setfile(const char *, struct stat *);
68void usage(int);
44a7a5ab 69
c59d3020 70int eval, force, verbose, cat;
44a7a5ab
A
71
72int
c59d3020 73main(int argc, char *argv[])
44a7a5ab 74{
c59d3020 75 enum {COMPRESS, DECOMPRESS} style;
44a7a5ab 76 size_t len;
c59d3020 77 int bits, ch;
44a7a5ab
A
78 char *p, newname[MAXPATHLEN];
79
864a4b6e
A
80 if (argc < 1)
81 usage(1);
c59d3020
A
82 cat = 0;
83 if ((p = rindex(argv[0], '/')) == NULL)
44a7a5ab
A
84 p = argv[0];
85 else
86 ++p;
87 if (!strcmp(p, "uncompress"))
88 style = DECOMPRESS;
c59d3020
A
89 else if (!strcmp(p, "compress"))
90 style = COMPRESS;
91 else if (!strcmp(p, "zcat")) {
92 cat = 1;
93 style = DECOMPRESS;
94 } else
44a7a5ab
A
95 errx(1, "unknown program name");
96
c59d3020 97 bits = 0;
44a7a5ab
A
98 while ((ch = getopt(argc, argv, "b:cdfv")) != -1)
99 switch(ch) {
100 case 'b':
101 bits = strtol(optarg, &p, 10);
102 if (*p)
103 errx(1, "illegal bit count -- %s", optarg);
104 break;
105 case 'c':
106 cat = 1;
107 break;
108 case 'd': /* Backward compatible. */
109 style = DECOMPRESS;
110 break;
111 case 'f':
112 force = 1;
113 break;
114 case 'v':
115 verbose = 1;
116 break;
117 case '?':
118 default:
119 usage(style == COMPRESS);
120 }
121 argc -= optind;
122 argv += optind;
123
124 if (argc == 0) {
c59d3020 125 cat = 1;
44a7a5ab
A
126 switch(style) {
127 case COMPRESS:
44a7a5ab
A
128 (void)compress("/dev/stdin", "/dev/stdout", bits);
129 break;
130 case DECOMPRESS:
44a7a5ab
A
131 (void)decompress("/dev/stdin", "/dev/stdout", bits);
132 break;
133 }
134 exit (eval);
135 }
136
74e6a095
A
137 /*
138 * The UNIX standard requires that `uncompress -c` be able to have multiple file parameters given.
139 */
44a7a5ab 140
c59d3020 141 for (; *argv; ++argv)
44a7a5ab
A
142 switch(style) {
143 case COMPRESS:
c59d3020
A
144 if (strcmp(*argv, "-") == 0) {
145 cat = 1;
146 compress("/dev/stdin", "/dev/stdout", bits);
147 break;
148 } else if (cat) {
44a7a5ab
A
149 compress(*argv, "/dev/stdout", bits);
150 break;
151 }
c59d3020 152 if ((p = rindex(*argv, '.')) != NULL &&
44a7a5ab
A
153 !strcmp(p, ".Z")) {
154 cwarnx("%s: name already has trailing .Z",
155 *argv);
156 break;
157 }
158 len = strlen(*argv);
159 if (len > sizeof(newname) - 3) {
160 cwarnx("%s: name too long", *argv);
161 break;
162 }
163 memmove(newname, *argv, len);
164 newname[len] = '.';
165 newname[len + 1] = 'Z';
166 newname[len + 2] = '\0';
167 compress(*argv, newname, bits);
168 break;
169 case DECOMPRESS:
c59d3020
A
170 if (strcmp(*argv, "-") == 0) {
171 cat = 1;
172 decompress("/dev/stdin", "/dev/stdout", bits);
173 break;
174 }
44a7a5ab 175 len = strlen(*argv);
c59d3020 176 if ((p = rindex(*argv, '.')) == NULL ||
44a7a5ab
A
177 strcmp(p, ".Z")) {
178 if (len > sizeof(newname) - 3) {
179 cwarnx("%s: name too long", *argv);
180 break;
181 }
182 memmove(newname, *argv, len);
183 newname[len] = '.';
184 newname[len + 1] = 'Z';
185 newname[len + 2] = '\0';
186 decompress(newname,
187 cat ? "/dev/stdout" : *argv, bits);
44a7a5ab
A
188 } else {
189 if (len - 2 > sizeof(newname) - 1) {
190 cwarnx("%s: name too long", *argv);
191 break;
192 }
193 memmove(newname, *argv, len - 2);
194 newname[len - 2] = '\0';
195 decompress(*argv,
196 cat ? "/dev/stdout" : newname, bits);
44a7a5ab
A
197 }
198 break;
199 }
44a7a5ab
A
200 exit (eval);
201}
202
203void
c59d3020 204compress(const char *in, const char *out, int bits)
44a7a5ab 205{
c59d3020 206 size_t nr;
44a7a5ab 207 struct stat isb, sb;
4d0bb651 208 FILE *ifp = NULL, *ofp = NULL;
44a7a5ab 209 int exists, isreg, oreg;
c59d3020 210 u_char buf[1024];
44a7a5ab 211
c59d3020
A
212 exists = !stat(out, &sb);
213 if (!force && exists && S_ISREG(sb.st_mode) && !cat && !permission(out)) {
214 cwarnx("%s already exists", out);
215 return;
216 }
217 isreg = oreg = !exists || S_ISREG(sb.st_mode);
44a7a5ab 218
44a7a5ab
A
219 if ((ifp = fopen(in, "r")) == NULL) {
220 cwarn("%s", in);
221 return;
222 }
c59d3020
A
223 if (stat(in, &isb)) { /* DON'T FSTAT! */
224 cwarn("%s", in);
225 goto err;
226 }
227 if (!S_ISREG(isb.st_mode))
44a7a5ab
A
228 isreg = 0;
229
230 if ((ofp = zopen(out, "w", bits)) == NULL) {
231 cwarn("%s", out);
232 goto err;
233 }
234 while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
235 if (fwrite(buf, 1, nr, ofp) != nr) {
236 cwarn("%s", out);
237 goto err;
238 }
239
240 if (ferror(ifp) || fclose(ifp)) {
241 cwarn("%s", in);
242 goto err;
243 }
244 ifp = NULL;
245
246 if (fclose(ofp)) {
247 cwarn("%s", out);
248 goto err;
249 }
250 ofp = NULL;
251
c59d3020 252 if (!cat && isreg) {
44a7a5ab
A
253 if (stat(out, &sb)) {
254 cwarn("%s", out);
255 goto err;
256 }
257
258 if (!force && sb.st_size >= isb.st_size) {
259 if (verbose)
c59d3020
A
260 (void)fprintf(stderr, "%s: file would grow; left unmodified\n",
261 in);
262 eval = 2;
44a7a5ab
A
263 if (unlink(out))
264 cwarn("%s", out);
265 goto err;
266 }
267
268 setfile(out, &isb);
269
270 if (unlink(in))
271 cwarn("%s", in);
272
273 if (verbose) {
c59d3020 274 (void)fprintf(stderr, "%s: ", out);
44a7a5ab 275 if (isb.st_size > sb.st_size)
c59d3020
A
276 (void)fprintf(stderr, "%.0f%% compression\n",
277 ((float)sb.st_size / isb.st_size) * 100.0);
44a7a5ab 278 else
c59d3020
A
279 (void)fprintf(stderr, "%.0f%% expansion\n",
280 ((float)isb.st_size / sb.st_size) * 100.0);
44a7a5ab
A
281 }
282 }
283 return;
284
285err: if (ofp) {
c59d3020 286 if (!cat && oreg)
44a7a5ab
A
287 (void)unlink(out);
288 (void)fclose(ofp);
289 }
290 if (ifp)
291 (void)fclose(ifp);
292}
293
294void
c59d3020 295decompress(const char *in, const char *out, int bits)
44a7a5ab 296{
c59d3020 297 size_t nr;
44a7a5ab
A
298 struct stat sb;
299 FILE *ifp, *ofp;
300 int exists, isreg, oreg;
c59d3020 301 u_char buf[1024];
44a7a5ab 302
c59d3020
A
303 exists = !stat(out, &sb);
304 if (!force && exists && S_ISREG(sb.st_mode) && !cat && !permission(out)) {
305 cwarnx("%s already exists", out);
44a7a5ab
A
306 return;
307 }
c59d3020 308 isreg = oreg = !exists || S_ISREG(sb.st_mode);
44a7a5ab 309
4d0bb651 310 ofp = NULL;
44a7a5ab 311 if ((ifp = zopen(in, "r", bits)) == NULL) {
c59d3020
A
312 cwarn("%s", in);
313 return;
314 }
315 if (stat(in, &sb)) {
44a7a5ab
A
316 cwarn("%s", in);
317 goto err;
318 }
c59d3020 319 if (!S_ISREG(sb.st_mode))
44a7a5ab
A
320 isreg = 0;
321
c59d3020
A
322 /*
323 * Try to read the first few uncompressed bytes from the input file
324 * before blindly truncating the output file.
325 */
326 if ((nr = fread(buf, 1, sizeof(buf), ifp)) == 0) {
327 cwarn("%s", in);
328 (void)fclose(ifp);
329 return;
330 }
331 if ((ofp = fopen(out, "w")) == NULL ||
332 (nr != 0 && fwrite(buf, 1, nr, ofp) != nr)) {
333 cwarn("%s", out);
334 (void)fclose(ifp);
335 return;
336 }
337
44a7a5ab
A
338 while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
339 if (fwrite(buf, 1, nr, ofp) != nr) {
340 cwarn("%s", out);
341 goto err;
342 }
343
344 if (ferror(ifp) || fclose(ifp)) {
345 cwarn("%s", in);
346 goto err;
347 }
348 ifp = NULL;
349
350 if (fclose(ofp)) {
351 cwarn("%s", out);
352 goto err;
353 }
354
c59d3020 355 if (!cat && isreg) {
44a7a5ab
A
356 setfile(out, &sb);
357
358 if (unlink(in))
359 cwarn("%s", in);
c59d3020
A
360 if (verbose) {
361 struct stat isb = sb;
362 stat(out, &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);
367 else
368 (void)fprintf(stderr, "%.0f%% expansion\n",
369 ((float)isb.st_size / sb.st_size) * 100.0);
370 }
44a7a5ab
A
371 }
372 return;
373
374err: if (ofp) {
c59d3020 375 if (!cat && oreg)
44a7a5ab
A
376 (void)unlink(out);
377 (void)fclose(ofp);
378 }
379 if (ifp)
380 (void)fclose(ifp);
381}
382
383void
c59d3020 384setfile(const char *name, struct stat *fs)
44a7a5ab 385{
867b3d41
A
386 struct attrlist ts_req = {};
387 struct {
388 struct timespec mtime;
389 struct timespec atime;
390 } set_ts;
44a7a5ab
A
391
392 fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
393
867b3d41
A
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;
398
399 if (setattrlist(name, &ts_req, &set_ts, sizeof(set_ts), 0))
400 cwarn("setattrlist: %s", name);
44a7a5ab
A
401
402 /*
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.
407 */
408 if (chown(name, fs->st_uid, fs->st_gid)) {
409 if (errno != EPERM)
410 cwarn("chown: %s", name);
411 fs->st_mode &= ~(S_ISUID|S_ISGID);
412 }
864a4b6e 413 if (chmod(name, fs->st_mode) && errno != ENOTSUP)
c59d3020 414 cwarn("chmod: %s", name);
44a7a5ab 415
864a4b6e 416 if (chflags(name, fs->st_flags) && errno != ENOTSUP)
44a7a5ab
A
417 cwarn("chflags: %s", name);
418}
419
420int
c59d3020 421permission(const char *fname)
44a7a5ab
A
422{
423 int ch, first;
48fce603 424 char resp[] = {'\0', '\0'};
44a7a5ab
A
425
426 if (!isatty(fileno(stderr)))
427 return (0);
428 (void)fprintf(stderr, "overwrite %s? ", fname);
48fce603
A
429
430 /* Load user specified locale */
431 setlocale(LC_MESSAGES, "");
432
44a7a5ab
A
433 first = ch = getchar();
434 while (ch != '\n' && ch != EOF)
435 ch = getchar();
48fce603
A
436
437 /* only care about first character */
438 resp[0] = first;
439
440 return (rpmatch(resp) == 1);
44a7a5ab
A
441}
442
443void
c59d3020 444usage(int iscompress)
44a7a5ab
A
445{
446 if (iscompress)
447 (void)fprintf(stderr,
448 "usage: compress [-cfv] [-b bits] [file ...]\n");
449 else
450 (void)fprintf(stderr,
74e6a095 451 "usage: uncompress [-cfv] [file ...]\n");
44a7a5ab
A
452 exit(1);
453}
454
455void
44a7a5ab 456cwarnx(const char *fmt, ...)
44a7a5ab
A
457{
458 va_list ap;
c59d3020 459
44a7a5ab 460 va_start(ap, fmt);
44a7a5ab
A
461 vwarnx(fmt, ap);
462 va_end(ap);
463 eval = 1;
464}
465
466void
44a7a5ab 467cwarn(const char *fmt, ...)
44a7a5ab
A
468{
469 va_list ap;
c59d3020 470
44a7a5ab 471 va_start(ap, fmt);
44a7a5ab
A
472 vwarn(fmt, ap);
473 va_end(ap);
474 eval = 1;
475}