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