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