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