]>
Commit | Line | Data |
---|---|---|
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 | #include <sys/attr.h> | |
50 | ||
51 | #include <err.h> | |
52 | #include <errno.h> | |
53 | #include <stdarg.h> | |
54 | #include <stdio.h> | |
55 | #include <stdlib.h> | |
56 | #include <string.h> | |
57 | #include <unistd.h> | |
58 | #include <locale.h> | |
59 | ||
60 | #include "zopen.h" | |
61 | ||
62 | void compress(const char *, const char *, int); | |
63 | void cwarn(const char *, ...) __printflike(1, 2); | |
64 | void cwarnx(const char *, ...) __printflike(1, 2); | |
65 | void decompress(const char *, const char *, int); | |
66 | int permission(const char *); | |
67 | void setfile(const char *, struct stat *); | |
68 | void usage(int); | |
69 | ||
70 | int eval, force, verbose, cat; | |
71 | ||
72 | int | |
73 | main(int argc, char *argv[]) | |
74 | { | |
75 | enum {COMPRESS, DECOMPRESS} style; | |
76 | size_t len; | |
77 | int bits, ch; | |
78 | char *p, newname[MAXPATHLEN]; | |
79 | ||
80 | if (argc < 1) | |
81 | usage(1); | |
82 | cat = 0; | |
83 | if ((p = rindex(argv[0], '/')) == NULL) | |
84 | p = argv[0]; | |
85 | else | |
86 | ++p; | |
87 | if (!strcmp(p, "uncompress")) | |
88 | style = DECOMPRESS; | |
89 | else if (!strcmp(p, "compress")) | |
90 | style = COMPRESS; | |
91 | else if (!strcmp(p, "zcat")) { | |
92 | cat = 1; | |
93 | style = DECOMPRESS; | |
94 | } else | |
95 | errx(1, "unknown program name"); | |
96 | ||
97 | bits = 0; | |
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) { | |
125 | cat = 1; | |
126 | switch(style) { | |
127 | case COMPRESS: | |
128 | (void)compress("/dev/stdin", "/dev/stdout", bits); | |
129 | break; | |
130 | case DECOMPRESS: | |
131 | (void)decompress("/dev/stdin", "/dev/stdout", bits); | |
132 | break; | |
133 | } | |
134 | exit (eval); | |
135 | } | |
136 | ||
137 | /* | |
138 | * The UNIX standard requires that `uncompress -c` be able to have multiple file parameters given. | |
139 | */ | |
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 = NULL, *ofp = NULL; | |
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 | if ((ifp = fopen(in, "r")) == NULL) { | |
220 | cwarn("%s", in); | |
221 | return; | |
222 | } | |
223 | if (stat(in, &isb)) { /* DON'T FSTAT! */ | |
224 | cwarn("%s", in); | |
225 | goto err; | |
226 | } | |
227 | if (!S_ISREG(isb.st_mode)) | |
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 | ||
252 | if (!cat && isreg) { | |
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) | |
260 | (void)fprintf(stderr, "%s: file would grow; left unmodified\n", | |
261 | in); | |
262 | eval = 2; | |
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) { | |
274 | (void)fprintf(stderr, "%s: ", out); | |
275 | if (isb.st_size > sb.st_size) | |
276 | (void)fprintf(stderr, "%.0f%% compression\n", | |
277 | ((float)sb.st_size / isb.st_size) * 100.0); | |
278 | else | |
279 | (void)fprintf(stderr, "%.0f%% expansion\n", | |
280 | ((float)isb.st_size / sb.st_size) * 100.0); | |
281 | } | |
282 | } | |
283 | return; | |
284 | ||
285 | err: if (ofp) { | |
286 | if (!cat && oreg) | |
287 | (void)unlink(out); | |
288 | (void)fclose(ofp); | |
289 | } | |
290 | if (ifp) | |
291 | (void)fclose(ifp); | |
292 | } | |
293 | ||
294 | void | |
295 | decompress(const char *in, const char *out, int bits) | |
296 | { | |
297 | size_t nr; | |
298 | struct stat sb; | |
299 | FILE *ifp, *ofp; | |
300 | int exists, isreg, oreg; | |
301 | u_char buf[1024]; | |
302 | ||
303 | exists = !stat(out, &sb); | |
304 | if (!force && exists && S_ISREG(sb.st_mode) && !cat && !permission(out)) { | |
305 | cwarnx("%s already exists", out); | |
306 | return; | |
307 | } | |
308 | isreg = oreg = !exists || S_ISREG(sb.st_mode); | |
309 | ||
310 | ofp = NULL; | |
311 | if ((ifp = zopen(in, "r", bits)) == NULL) { | |
312 | cwarn("%s", in); | |
313 | return; | |
314 | } | |
315 | if (stat(in, &sb)) { | |
316 | cwarn("%s", in); | |
317 | goto err; | |
318 | } | |
319 | if (!S_ISREG(sb.st_mode)) | |
320 | isreg = 0; | |
321 | ||
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 | ||
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 | ||
355 | if (!cat && isreg) { | |
356 | setfile(out, &sb); | |
357 | ||
358 | if (unlink(in)) | |
359 | cwarn("%s", in); | |
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 | } | |
371 | } | |
372 | return; | |
373 | ||
374 | err: if (ofp) { | |
375 | if (!cat && oreg) | |
376 | (void)unlink(out); | |
377 | (void)fclose(ofp); | |
378 | } | |
379 | if (ifp) | |
380 | (void)fclose(ifp); | |
381 | } | |
382 | ||
383 | void | |
384 | setfile(const char *name, struct stat *fs) | |
385 | { | |
386 | struct attrlist ts_req = {}; | |
387 | struct { | |
388 | struct timespec mtime; | |
389 | struct timespec atime; | |
390 | } set_ts; | |
391 | ||
392 | fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; | |
393 | ||
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); | |
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 | } | |
413 | if (chmod(name, fs->st_mode) && errno != ENOTSUP) | |
414 | cwarn("chmod: %s", name); | |
415 | ||
416 | if (chflags(name, fs->st_flags) && errno != ENOTSUP) | |
417 | cwarn("chflags: %s", name); | |
418 | } | |
419 | ||
420 | int | |
421 | permission(const char *fname) | |
422 | { | |
423 | int ch, first; | |
424 | char resp[] = {'\0', '\0'}; | |
425 | ||
426 | if (!isatty(fileno(stderr))) | |
427 | return (0); | |
428 | (void)fprintf(stderr, "overwrite %s? ", fname); | |
429 | ||
430 | /* Load user specified locale */ | |
431 | setlocale(LC_MESSAGES, ""); | |
432 | ||
433 | first = ch = getchar(); | |
434 | while (ch != '\n' && ch != EOF) | |
435 | ch = getchar(); | |
436 | ||
437 | /* only care about first character */ | |
438 | resp[0] = first; | |
439 | ||
440 | return (rpmatch(resp) == 1); | |
441 | } | |
442 | ||
443 | void | |
444 | usage(int iscompress) | |
445 | { | |
446 | if (iscompress) | |
447 | (void)fprintf(stderr, | |
448 | "usage: compress [-cfv] [-b bits] [file ...]\n"); | |
449 | else | |
450 | (void)fprintf(stderr, | |
451 | "usage: uncompress [-cfv] [file ...]\n"); | |
452 | exit(1); | |
453 | } | |
454 | ||
455 | void | |
456 | cwarnx(const char *fmt, ...) | |
457 | { | |
458 | va_list ap; | |
459 | ||
460 | va_start(ap, fmt); | |
461 | vwarnx(fmt, ap); | |
462 | va_end(ap); | |
463 | eval = 1; | |
464 | } | |
465 | ||
466 | void | |
467 | cwarn(const char *fmt, ...) | |
468 | { | |
469 | va_list ap; | |
470 | ||
471 | va_start(ap, fmt); | |
472 | vwarn(fmt, ap); | |
473 | va_end(ap); | |
474 | eval = 1; | |
475 | } |