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