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