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