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