file_cmds-184.tar.gz
[apple/file_cmds.git] / compress / compress.c
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.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1992, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38 #endif
39
40 #if 0
41 #ifndef lint
42 static char sccsid[] = "@(#)compress.c 8.2 (Berkeley) 1/7/94";
43 #endif
44 #endif
45
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD: src/usr.bin/compress/compress.c,v 1.21 2003/06/14 13:41:31 trhodes Exp $");
48
49 #include <sys/param.h>
50 #include <sys/stat.h>
51 #include <sys/time.h>
52
53 #include <err.h>
54 #include <errno.h>
55 #include <stdarg.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60
61 #include "zopen.h"
62
63 void compress(const char *, const char *, int);
64 void cwarn(const char *, ...) __printflike(1, 2);
65 void cwarnx(const char *, ...) __printflike(1, 2);
66 void decompress(const char *, const char *, int);
67 int permission(const char *);
68 void setfile(const char *, struct stat *);
69 void usage(int);
70
71 int eval, force, verbose, cat;
72
73 int
74 main(int argc, char *argv[])
75 {
76 enum {COMPRESS, DECOMPRESS} style;
77 size_t len;
78 int bits, ch;
79 char *p, newname[MAXPATHLEN];
80
81 if (argc < 1)
82 usage(1);
83 cat = 0;
84 if ((p = rindex(argv[0], '/')) == NULL)
85 p = argv[0];
86 else
87 ++p;
88 if (!strcmp(p, "uncompress"))
89 style = DECOMPRESS;
90 else if (!strcmp(p, "compress"))
91 style = COMPRESS;
92 else if (!strcmp(p, "zcat")) {
93 cat = 1;
94 style = DECOMPRESS;
95 } else
96 errx(1, "unknown program name");
97
98 bits = 0;
99 while ((ch = getopt(argc, argv, "b:cdfv")) != -1)
100 switch(ch) {
101 case 'b':
102 bits = strtol(optarg, &p, 10);
103 if (*p)
104 errx(1, "illegal bit count -- %s", optarg);
105 break;
106 case 'c':
107 cat = 1;
108 break;
109 case 'd': /* Backward compatible. */
110 style = DECOMPRESS;
111 break;
112 case 'f':
113 force = 1;
114 break;
115 case 'v':
116 verbose = 1;
117 break;
118 case '?':
119 default:
120 usage(style == COMPRESS);
121 }
122 argc -= optind;
123 argv += optind;
124
125 if (argc == 0) {
126 cat = 1;
127 switch(style) {
128 case COMPRESS:
129 (void)compress("/dev/stdin", "/dev/stdout", bits);
130 break;
131 case DECOMPRESS:
132 (void)decompress("/dev/stdin", "/dev/stdout", bits);
133 break;
134 }
135 exit (eval);
136 }
137
138 if (cat == 1 && argc > 1)
139 errx(1, "the -c option permits only a single file argument");
140
141 for (; *argv; ++argv)
142 switch(style) {
143 case COMPRESS:
144 if (strcmp(*argv, "-") == 0) {
145 cat = 1;
146 compress("/dev/stdin", "/dev/stdout", bits);
147 break;
148 } else if (cat) {
149 compress(*argv, "/dev/stdout", bits);
150 break;
151 }
152 if ((p = rindex(*argv, '.')) != NULL &&
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:
170 if (strcmp(*argv, "-") == 0) {
171 cat = 1;
172 decompress("/dev/stdin", "/dev/stdout", bits);
173 break;
174 }
175 len = strlen(*argv);
176 if ((p = rindex(*argv, '.')) == NULL ||
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);
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);
197 }
198 break;
199 }
200 exit (eval);
201 }
202
203 void
204 compress(const char *in, const char *out, int bits)
205 {
206 size_t nr;
207 struct stat isb, sb;
208 FILE *ifp, *ofp;
209 int exists, isreg, oreg;
210 u_char buf[1024];
211
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);
218
219 ifp = ofp = NULL;
220 if ((ifp = fopen(in, "r")) == NULL) {
221 cwarn("%s", in);
222 return;
223 }
224 if (stat(in, &isb)) { /* DON'T FSTAT! */
225 cwarn("%s", in);
226 goto err;
227 }
228 if (!S_ISREG(isb.st_mode))
229 isreg = 0;
230
231 if ((ofp = zopen(out, "w", bits)) == NULL) {
232 cwarn("%s", out);
233 goto err;
234 }
235 while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
236 if (fwrite(buf, 1, nr, ofp) != nr) {
237 cwarn("%s", out);
238 goto err;
239 }
240
241 if (ferror(ifp) || fclose(ifp)) {
242 cwarn("%s", in);
243 goto err;
244 }
245 ifp = NULL;
246
247 if (fclose(ofp)) {
248 cwarn("%s", out);
249 goto err;
250 }
251 ofp = NULL;
252
253 if (!cat && isreg) {
254 if (stat(out, &sb)) {
255 cwarn("%s", out);
256 goto err;
257 }
258
259 if (!force && sb.st_size >= isb.st_size) {
260 if (verbose)
261 (void)fprintf(stderr, "%s: file would grow; left unmodified\n",
262 in);
263 eval = 2;
264 if (unlink(out))
265 cwarn("%s", out);
266 goto err;
267 }
268
269 setfile(out, &isb);
270
271 if (unlink(in))
272 cwarn("%s", in);
273
274 if (verbose) {
275 (void)fprintf(stderr, "%s: ", out);
276 if (isb.st_size > sb.st_size)
277 (void)fprintf(stderr, "%.0f%% compression\n",
278 ((float)sb.st_size / isb.st_size) * 100.0);
279 else
280 (void)fprintf(stderr, "%.0f%% expansion\n",
281 ((float)isb.st_size / sb.st_size) * 100.0);
282 }
283 }
284 return;
285
286 err: if (ofp) {
287 if (!cat && oreg)
288 (void)unlink(out);
289 (void)fclose(ofp);
290 }
291 if (ifp)
292 (void)fclose(ifp);
293 }
294
295 void
296 decompress(const char *in, const char *out, int bits)
297 {
298 size_t nr;
299 struct stat sb;
300 FILE *ifp, *ofp;
301 int exists, isreg, oreg;
302 u_char buf[1024];
303
304 exists = !stat(out, &sb);
305 if (!force && exists && S_ISREG(sb.st_mode) && !cat && !permission(out)) {
306 cwarnx("%s already exists", out);
307 return;
308 }
309 isreg = oreg = !exists || S_ISREG(sb.st_mode);
310
311 ifp = ofp = NULL;
312 if ((ifp = zopen(in, "r", bits)) == NULL) {
313 cwarn("%s", in);
314 return;
315 }
316 if (stat(in, &sb)) {
317 cwarn("%s", in);
318 goto err;
319 }
320 if (!S_ISREG(sb.st_mode))
321 isreg = 0;
322
323 /*
324 * Try to read the first few uncompressed bytes from the input file
325 * before blindly truncating the output file.
326 */
327 if ((nr = fread(buf, 1, sizeof(buf), ifp)) == 0) {
328 cwarn("%s", in);
329 (void)fclose(ifp);
330 return;
331 }
332 if ((ofp = fopen(out, "w")) == NULL ||
333 (nr != 0 && fwrite(buf, 1, nr, ofp) != nr)) {
334 cwarn("%s", out);
335 (void)fclose(ifp);
336 return;
337 }
338
339 while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
340 if (fwrite(buf, 1, nr, ofp) != nr) {
341 cwarn("%s", out);
342 goto err;
343 }
344
345 if (ferror(ifp) || fclose(ifp)) {
346 cwarn("%s", in);
347 goto err;
348 }
349 ifp = NULL;
350
351 if (fclose(ofp)) {
352 cwarn("%s", out);
353 goto err;
354 }
355
356 if (!cat && isreg) {
357 setfile(out, &sb);
358
359 if (unlink(in))
360 cwarn("%s", in);
361 if (verbose) {
362 struct stat isb = sb;
363 stat(out, &sb);
364 (void)fprintf(stderr, "%s: ", out);
365 if (isb.st_size > sb.st_size)
366 (void)fprintf(stderr, "%.0f%% compression\n",
367 ((float)sb.st_size / isb.st_size) * 100.0);
368 else
369 (void)fprintf(stderr, "%.0f%% expansion\n",
370 ((float)isb.st_size / sb.st_size) * 100.0);
371 }
372 }
373 return;
374
375 err: if (ofp) {
376 if (!cat && oreg)
377 (void)unlink(out);
378 (void)fclose(ofp);
379 }
380 if (ifp)
381 (void)fclose(ifp);
382 }
383
384 void
385 setfile(const char *name, struct stat *fs)
386 {
387 static struct timeval tv[2];
388
389 fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
390
391 TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
392 TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
393 if (utimes(name, tv))
394 cwarn("utimes: %s", name);
395
396 /*
397 * Changing the ownership probably won't succeed, unless we're root
398 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
399 * the mode; current BSD behavior is to remove all setuid bits on
400 * chown. If chown fails, lose setuid/setgid bits.
401 */
402 if (chown(name, fs->st_uid, fs->st_gid)) {
403 if (errno != EPERM)
404 cwarn("chown: %s", name);
405 fs->st_mode &= ~(S_ISUID|S_ISGID);
406 }
407 if (chmod(name, fs->st_mode) && errno != ENOTSUP)
408 cwarn("chmod: %s", name);
409
410 if (chflags(name, fs->st_flags) && errno != ENOTSUP)
411 cwarn("chflags: %s", name);
412 }
413
414 int
415 permission(const char *fname)
416 {
417 int ch, first;
418
419 if (!isatty(fileno(stderr)))
420 return (0);
421 (void)fprintf(stderr, "overwrite %s? ", fname);
422 first = ch = getchar();
423 while (ch != '\n' && ch != EOF)
424 ch = getchar();
425 return (first == 'y');
426 }
427
428 void
429 usage(int iscompress)
430 {
431 if (iscompress)
432 (void)fprintf(stderr,
433 "usage: compress [-cfv] [-b bits] [file ...]\n");
434 else
435 (void)fprintf(stderr,
436 "usage: uncompress [-cfv] [-b bits] [file ...]\n");
437 exit(1);
438 }
439
440 void
441 cwarnx(const char *fmt, ...)
442 {
443 va_list ap;
444
445 va_start(ap, fmt);
446 vwarnx(fmt, ap);
447 va_end(ap);
448 eval = 1;
449 }
450
451 void
452 cwarn(const char *fmt, ...)
453 {
454 va_list ap;
455
456 va_start(ap, fmt);
457 vwarn(fmt, ap);
458 va_end(ap);
459 eval = 1;
460 }