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