compress.c revision 1.19 1 1.19 wiz /* $NetBSD: compress.c,v 1.19 2002/05/26 22:21:22 wiz 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.19 wiz __RCSID("$NetBSD: compress.c,v 1.19 2002/05/26 22:21:22 wiz 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.19 wiz #include <stdarg.h>
57 1.1 cgd #include <stdio.h>
58 1.1 cgd #include <stdlib.h>
59 1.1 cgd #include <string.h>
60 1.6 cgd #include <unistd.h>
61 1.1 cgd
62 1.19 wiz void compress(char *, char *, int);
63 1.19 wiz void cwarn(const char *, ...) __attribute__((__format__(__printf__,1,2)));
64 1.19 wiz void cwarnx(const char *, ...) __attribute__((__format__(__printf__,1,2)));
65 1.19 wiz void decompress(char *, char *, int);
66 1.19 wiz int permission(char *);
67 1.19 wiz void setfile(char *, struct stat *);
68 1.19 wiz void usage(int);
69 1.8 cgd
70 1.19 wiz int main(int, char *[]);
71 1.19 wiz extern FILE *zopen(const char *fname, const char *mode, int bits);
72 1.1 cgd
73 1.6 cgd int eval, force, verbose;
74 1.10 mrg int isstdout, isstdin;
75 1.1 cgd
76 1.6 cgd int
77 1.19 wiz main(int argc, char **argv)
78 1.1 cgd {
79 1.17 wsanchez enum {COMPRESS, DECOMPRESS} style = COMPRESS;
80 1.6 cgd size_t len;
81 1.6 cgd int bits, cat, ch;
82 1.6 cgd char *p, newname[MAXPATHLEN];
83 1.1 cgd
84 1.13 lukem if ((p = strrchr(argv[0], '/')) == NULL)
85 1.6 cgd p = argv[0];
86 1.6 cgd else
87 1.6 cgd ++p;
88 1.6 cgd if (!strcmp(p, "uncompress"))
89 1.6 cgd style = DECOMPRESS;
90 1.17 wsanchez else if (!strcmp(p, "compress"))
91 1.17 wsanchez style = COMPRESS;
92 1.17 wsanchez else if (!strcmp(p, "zcat")) {
93 1.17 wsanchez style = DECOMPRESS;
94 1.17 wsanchez cat = 1;
95 1.17 wsanchez }
96 1.1 cgd else
97 1.6 cgd errx(1, "unknown program name");
98 1.1 cgd
99 1.6 cgd bits = cat = 0;
100 1.14 lukem while ((ch = getopt(argc, argv, "b:cdfv")) != -1)
101 1.1 cgd switch(ch) {
102 1.1 cgd case 'b':
103 1.6 cgd bits = strtol(optarg, &p, 10);
104 1.6 cgd if (*p)
105 1.6 cgd errx(1, "illegal bit count -- %s", optarg);
106 1.1 cgd break;
107 1.1 cgd case 'c':
108 1.6 cgd cat = 1;
109 1.1 cgd break;
110 1.6 cgd case 'd': /* Backward compatible. */
111 1.6 cgd style = DECOMPRESS;
112 1.1 cgd break;
113 1.1 cgd case 'f':
114 1.1 cgd force = 1;
115 1.1 cgd break;
116 1.6 cgd case 'v':
117 1.1 cgd verbose = 1;
118 1.1 cgd break;
119 1.1 cgd case '?':
120 1.1 cgd default:
121 1.6 cgd usage(style == COMPRESS);
122 1.1 cgd }
123 1.1 cgd argc -= optind;
124 1.1 cgd argv += optind;
125 1.1 cgd
126 1.6 cgd if (argc == 0) {
127 1.6 cgd switch(style) {
128 1.6 cgd case COMPRESS:
129 1.10 mrg isstdout = 1;
130 1.10 mrg isstdin = 1;
131 1.6 cgd (void)compress("/dev/stdin", "/dev/stdout", bits);
132 1.6 cgd break;
133 1.6 cgd case DECOMPRESS:
134 1.10 mrg isstdout = 1;
135 1.10 mrg isstdin = 1;
136 1.6 cgd (void)decompress("/dev/stdin", "/dev/stdout", bits);
137 1.6 cgd break;
138 1.1 cgd }
139 1.6 cgd exit (eval);
140 1.6 cgd }
141 1.6 cgd
142 1.6 cgd if (cat == 1 && argc > 1)
143 1.6 cgd errx(1, "the -c option permits only a single file argument");
144 1.6 cgd
145 1.11 abrown for (; *argv; ++argv) {
146 1.10 mrg isstdout = 0;
147 1.6 cgd switch(style) {
148 1.6 cgd case COMPRESS:
149 1.6 cgd if (cat) {
150 1.10 mrg isstdout = 1;
151 1.6 cgd compress(*argv, "/dev/stdout", bits);
152 1.6 cgd break;
153 1.6 cgd }
154 1.13 lukem if ((p = strrchr(*argv, '.')) != NULL &&
155 1.6 cgd !strcmp(p, ".Z")) {
156 1.6 cgd cwarnx("%s: name already has trailing .Z",
157 1.6 cgd *argv);
158 1.6 cgd break;
159 1.6 cgd }
160 1.6 cgd len = strlen(*argv);
161 1.6 cgd if (len > sizeof(newname) - 3) {
162 1.6 cgd cwarnx("%s: name too long", *argv);
163 1.6 cgd break;
164 1.6 cgd }
165 1.6 cgd memmove(newname, *argv, len);
166 1.6 cgd newname[len] = '.';
167 1.6 cgd newname[len + 1] = 'Z';
168 1.6 cgd newname[len + 2] = '\0';
169 1.6 cgd compress(*argv, newname, bits);
170 1.6 cgd break;
171 1.6 cgd case DECOMPRESS:
172 1.6 cgd len = strlen(*argv);
173 1.13 lukem if ((p = strrchr(*argv, '.')) == NULL ||
174 1.6 cgd strcmp(p, ".Z")) {
175 1.6 cgd if (len > sizeof(newname) - 3) {
176 1.6 cgd cwarnx("%s: name too long", *argv);
177 1.6 cgd break;
178 1.6 cgd }
179 1.6 cgd memmove(newname, *argv, len);
180 1.6 cgd newname[len] = '.';
181 1.6 cgd newname[len + 1] = 'Z';
182 1.6 cgd newname[len + 2] = '\0';
183 1.6 cgd decompress(newname,
184 1.6 cgd cat ? "/dev/stdout" : *argv, bits);
185 1.10 mrg if (cat)
186 1.10 mrg isstdout = 1;
187 1.6 cgd } else {
188 1.6 cgd if (len - 2 > sizeof(newname) - 1) {
189 1.6 cgd cwarnx("%s: name too long", *argv);
190 1.6 cgd break;
191 1.6 cgd }
192 1.6 cgd memmove(newname, *argv, len - 2);
193 1.6 cgd newname[len - 2] = '\0';
194 1.6 cgd decompress(*argv,
195 1.6 cgd cat ? "/dev/stdout" : newname, bits);
196 1.10 mrg if (cat)
197 1.10 mrg isstdout = 1;
198 1.1 cgd }
199 1.6 cgd break;
200 1.1 cgd }
201 1.11 abrown }
202 1.6 cgd exit (eval);
203 1.6 cgd }
204 1.6 cgd
205 1.6 cgd void
206 1.19 wiz compress(char *in, char *out, int bits)
207 1.6 cgd {
208 1.13 lukem int nr;
209 1.6 cgd struct stat isb, sb;
210 1.6 cgd FILE *ifp, *ofp;
211 1.6 cgd int exists, isreg, oreg;
212 1.12 mikel u_char buf[BUFSIZ];
213 1.6 cgd
214 1.10 mrg if (!isstdout) {
215 1.10 mrg exists = !stat(out, &sb);
216 1.10 mrg if (!force && exists && S_ISREG(sb.st_mode) && !permission(out))
217 1.10 mrg return;
218 1.10 mrg oreg = !exists || S_ISREG(sb.st_mode);
219 1.10 mrg } else
220 1.12 mikel oreg = 0;
221 1.6 cgd
222 1.6 cgd ifp = ofp = NULL;
223 1.6 cgd if ((ifp = fopen(in, "r")) == NULL) {
224 1.6 cgd cwarn("%s", in);
225 1.6 cgd return;
226 1.6 cgd }
227 1.10 mrg
228 1.10 mrg if (!isstdin) {
229 1.10 mrg if (stat(in, &isb)) { /* DON'T FSTAT! */
230 1.10 mrg cwarn("%s", in);
231 1.10 mrg goto err;
232 1.10 mrg }
233 1.10 mrg if (!S_ISREG(isb.st_mode))
234 1.10 mrg isreg = 0;
235 1.10 mrg else
236 1.10 mrg isreg = 1;
237 1.10 mrg } else
238 1.6 cgd isreg = 0;
239 1.6 cgd
240 1.6 cgd if ((ofp = zopen(out, "w", bits)) == NULL) {
241 1.6 cgd cwarn("%s", out);
242 1.6 cgd goto err;
243 1.6 cgd }
244 1.6 cgd while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
245 1.6 cgd if (fwrite(buf, 1, nr, ofp) != nr) {
246 1.6 cgd cwarn("%s", out);
247 1.6 cgd goto err;
248 1.6 cgd }
249 1.6 cgd
250 1.6 cgd if (ferror(ifp) || fclose(ifp)) {
251 1.6 cgd cwarn("%s", in);
252 1.6 cgd goto err;
253 1.6 cgd }
254 1.6 cgd ifp = NULL;
255 1.6 cgd
256 1.6 cgd if (fclose(ofp)) {
257 1.6 cgd cwarn("%s", out);
258 1.6 cgd goto err;
259 1.6 cgd }
260 1.6 cgd ofp = NULL;
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 if (unlink(out))
272 1.6 cgd cwarn("%s", out);
273 1.6 cgd goto err;
274 1.6 cgd }
275 1.6 cgd
276 1.6 cgd setfile(out, &isb);
277 1.6 cgd
278 1.6 cgd if (unlink(in))
279 1.6 cgd cwarn("%s", in);
280 1.6 cgd
281 1.6 cgd if (verbose) {
282 1.6 cgd (void)printf("%s: ", out);
283 1.6 cgd if (isb.st_size > sb.st_size)
284 1.6 cgd (void)printf("%.0f%% compression\n",
285 1.15 mycroft ((double)sb.st_size / isb.st_size) * 100.0);
286 1.6 cgd else
287 1.6 cgd (void)printf("%.0f%% expansion\n",
288 1.15 mycroft ((double)isb.st_size / sb.st_size) * 100.0);
289 1.1 cgd }
290 1.1 cgd }
291 1.6 cgd return;
292 1.6 cgd
293 1.6 cgd err: if (ofp) {
294 1.6 cgd if (oreg)
295 1.6 cgd (void)unlink(out);
296 1.6 cgd (void)fclose(ofp);
297 1.1 cgd }
298 1.6 cgd if (ifp)
299 1.6 cgd (void)fclose(ifp);
300 1.1 cgd }
301 1.1 cgd
302 1.6 cgd void
303 1.19 wiz decompress(char *in, char *out, int bits)
304 1.6 cgd {
305 1.13 lukem int nr;
306 1.6 cgd struct stat sb;
307 1.6 cgd FILE *ifp, *ofp;
308 1.6 cgd int exists, isreg, oreg;
309 1.12 mikel u_char buf[BUFSIZ];
310 1.1 cgd
311 1.10 mrg if (!isstdout) {
312 1.10 mrg exists = !stat(out, &sb);
313 1.10 mrg if (!force && exists && S_ISREG(sb.st_mode) && !permission(out))
314 1.10 mrg return;
315 1.10 mrg oreg = !exists || S_ISREG(sb.st_mode);
316 1.10 mrg } else
317 1.12 mikel oreg = 0;
318 1.1 cgd
319 1.6 cgd ifp = ofp = NULL;
320 1.6 cgd if ((ofp = fopen(out, "w")) == NULL) {
321 1.6 cgd cwarn("%s", out);
322 1.6 cgd return;
323 1.6 cgd }
324 1.1 cgd
325 1.6 cgd if ((ifp = zopen(in, "r", bits)) == NULL) {
326 1.6 cgd cwarn("%s", in);
327 1.6 cgd goto err;
328 1.1 cgd }
329 1.10 mrg if (!isstdin) {
330 1.10 mrg if (stat(in, &sb)) {
331 1.10 mrg cwarn("%s", in);
332 1.10 mrg goto err;
333 1.10 mrg }
334 1.10 mrg if (!S_ISREG(sb.st_mode))
335 1.10 mrg isreg = 0;
336 1.10 mrg else
337 1.10 mrg isreg = 1;
338 1.10 mrg } else
339 1.6 cgd isreg = 0;
340 1.1 cgd
341 1.6 cgd while ((nr = fread(buf, 1, sizeof(buf), ifp)) != 0)
342 1.6 cgd if (fwrite(buf, 1, nr, ofp) != nr) {
343 1.6 cgd cwarn("%s", out);
344 1.6 cgd goto err;
345 1.6 cgd }
346 1.1 cgd
347 1.6 cgd if (ferror(ifp) || fclose(ifp)) {
348 1.6 cgd cwarn("%s", in);
349 1.6 cgd goto err;
350 1.1 cgd }
351 1.6 cgd ifp = NULL;
352 1.1 cgd
353 1.6 cgd if (fclose(ofp)) {
354 1.6 cgd cwarn("%s", out);
355 1.6 cgd goto err;
356 1.1 cgd }
357 1.1 cgd
358 1.12 mikel if (isreg && oreg) {
359 1.6 cgd setfile(out, &sb);
360 1.1 cgd
361 1.6 cgd if (unlink(in))
362 1.6 cgd cwarn("%s", in);
363 1.1 cgd }
364 1.6 cgd return;
365 1.1 cgd
366 1.6 cgd err: if (ofp) {
367 1.6 cgd if (oreg)
368 1.6 cgd (void)unlink(out);
369 1.6 cgd (void)fclose(ofp);
370 1.1 cgd }
371 1.6 cgd if (ifp)
372 1.6 cgd (void)fclose(ifp);
373 1.6 cgd }
374 1.1 cgd
375 1.6 cgd void
376 1.19 wiz setfile(char *name, struct stat *fs)
377 1.6 cgd {
378 1.6 cgd static struct timeval tv[2];
379 1.1 cgd
380 1.6 cgd fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
381 1.1 cgd
382 1.6 cgd TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
383 1.6 cgd TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
384 1.6 cgd if (utimes(name, tv))
385 1.6 cgd cwarn("utimes: %s", name);
386 1.1 cgd
387 1.1 cgd /*
388 1.6 cgd * Changing the ownership probably won't succeed, unless we're root
389 1.6 cgd * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
390 1.6 cgd * the mode; current BSD behavior is to remove all setuid bits on
391 1.6 cgd * chown. If chown fails, lose setuid/setgid bits.
392 1.1 cgd */
393 1.6 cgd if (chown(name, fs->st_uid, fs->st_gid)) {
394 1.6 cgd if (errno != EPERM)
395 1.6 cgd cwarn("chown: %s", name);
396 1.6 cgd fs->st_mode &= ~(S_ISUID|S_ISGID);
397 1.1 cgd }
398 1.6 cgd if (chmod(name, fs->st_mode))
399 1.6 cgd cwarn("chown: %s", name);
400 1.1 cgd
401 1.16 kleink /*
402 1.16 kleink * Restore the file's flags. However, do this only if the original
403 1.16 kleink * file had any flags set; this avoids a warning on file-systems that
404 1.16 kleink * do not support flags.
405 1.16 kleink */
406 1.16 kleink if (fs->st_flags != 0 && chflags(name, fs->st_flags))
407 1.6 cgd cwarn("chflags: %s", name);
408 1.1 cgd }
409 1.1 cgd
410 1.6 cgd int
411 1.19 wiz permission(char *fname)
412 1.1 cgd {
413 1.6 cgd int ch, first;
414 1.1 cgd
415 1.6 cgd if (!isatty(fileno(stderr)))
416 1.6 cgd return (0);
417 1.6 cgd (void)fprintf(stderr, "overwrite %s? ", fname);
418 1.6 cgd first = ch = getchar();
419 1.6 cgd while (ch != '\n' && ch != EOF)
420 1.6 cgd ch = getchar();
421 1.6 cgd return (first == 'y');
422 1.1 cgd }
423 1.1 cgd
424 1.6 cgd void
425 1.19 wiz usage(int iscompress)
426 1.1 cgd {
427 1.6 cgd if (iscompress)
428 1.6 cgd (void)fprintf(stderr,
429 1.6 cgd "usage: compress [-cfv] [-b bits] [file ...]\n");
430 1.6 cgd else
431 1.6 cgd (void)fprintf(stderr,
432 1.6 cgd "usage: uncompress [-c] [-b bits] [file ...]\n");
433 1.1 cgd exit(1);
434 1.1 cgd }
435 1.1 cgd
436 1.1 cgd void
437 1.6 cgd cwarnx(const char *fmt, ...)
438 1.1 cgd {
439 1.6 cgd va_list ap;
440 1.19 wiz
441 1.6 cgd va_start(ap, fmt);
442 1.6 cgd vwarnx(fmt, ap);
443 1.6 cgd va_end(ap);
444 1.6 cgd eval = 1;
445 1.1 cgd }
446 1.1 cgd
447 1.1 cgd void
448 1.6 cgd cwarn(const char *fmt, ...)
449 1.1 cgd {
450 1.6 cgd va_list ap;
451 1.19 wiz
452 1.6 cgd va_start(ap, fmt);
453 1.6 cgd vwarn(fmt, ap);
454 1.6 cgd va_end(ap);
455 1.6 cgd eval = 1;
456 1.1 cgd }
457