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