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