GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
/* $OpenBSD: main.c,v 1.94 2016/09/03 13:26:50 tedu Exp $ */ |
||
2 |
|||
3 |
/* |
||
4 |
* Copyright (c) 1992, 1993 |
||
5 |
* The Regents of the University of California. All rights reserved. |
||
6 |
* Copyright (c) 1997-2002 Michael Shalayeff |
||
7 |
* |
||
8 |
* Redistribution and use in source and binary forms, with or without |
||
9 |
* modification, are permitted provided that the following conditions |
||
10 |
* are met: |
||
11 |
* 1. Redistributions of source code must retain the above copyright |
||
12 |
* notice, this list of conditions and the following disclaimer. |
||
13 |
* 2. Redistributions in binary form must reproduce the above copyright |
||
14 |
* notice, this list of conditions and the following disclaimer in the |
||
15 |
* documentation and/or other materials provided with the distribution. |
||
16 |
* 3. Neither the name of the University nor the names of its contributors |
||
17 |
* may be used to endorse or promote products derived from this software |
||
18 |
* without specific prior written permission. |
||
19 |
* |
||
20 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
21 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
22 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
23 |
* IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT, |
||
24 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
25 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
||
26 |
* SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
||
27 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||
28 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
||
29 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
||
30 |
* THE POSSIBILITY OF SUCH DAMAGE. |
||
31 |
*/ |
||
32 |
|||
33 |
#include <sys/time.h> |
||
34 |
#include <sys/stat.h> |
||
35 |
|||
36 |
#include <getopt.h> |
||
37 |
#include <err.h> |
||
38 |
#include <errno.h> |
||
39 |
#include <fts.h> |
||
40 |
#include <libgen.h> |
||
41 |
#include <stdio.h> |
||
42 |
#include <stdlib.h> |
||
43 |
#include <stdbool.h> |
||
44 |
#include <string.h> |
||
45 |
#include <unistd.h> |
||
46 |
#include <limits.h> |
||
47 |
#include <fcntl.h> |
||
48 |
#include <paths.h> |
||
49 |
#include "compress.h" |
||
50 |
|||
51 |
#define min(a,b) ((a) < (b)? (a) : (b)) |
||
52 |
|||
53 |
int cat, decomp, pipin, force, verbose, testmode, list, recurse, storename; |
||
54 |
extern char *__progname; |
||
55 |
|||
56 |
const struct compressor { |
||
57 |
const char *name; |
||
58 |
const char *suffix; |
||
59 |
const u_char *magic; |
||
60 |
const char *comp_opts; |
||
61 |
const char *decomp_opts; |
||
62 |
const char *cat_opts; |
||
63 |
void *(*ropen)(int, char *, int); |
||
64 |
int (*read)(void *, char *, int); |
||
65 |
#ifndef SMALL |
||
66 |
void *(*wopen)(int, char *, int, u_int32_t); |
||
67 |
int (*write)(void *, const char *, int); |
||
68 |
#endif |
||
69 |
int (*close)(void *, struct z_info *, const char *, struct stat *); |
||
70 |
} c_table[] = { |
||
71 |
#define M_DEFLATE (&c_table[0]) |
||
72 |
{ |
||
73 |
"deflate", |
||
74 |
".gz", |
||
75 |
"\037\213", |
||
76 |
"123456789ab:cdfhLlNnOo:qrS:tVv", |
||
77 |
"cfhLlNno:qrtVv", |
||
78 |
"fhqr", |
||
79 |
gz_ropen, |
||
80 |
gz_read, |
||
81 |
#ifndef SMALL |
||
82 |
gz_wopen, |
||
83 |
gz_write, |
||
84 |
#endif |
||
85 |
gz_close |
||
86 |
}, |
||
87 |
#define M_COMPRESS (&c_table[1]) |
||
88 |
#ifndef SMALL |
||
89 |
{ |
||
90 |
"compress", |
||
91 |
".Z", |
||
92 |
"\037\235", |
||
93 |
"123456789ab:cdfghlNnOo:qrS:tv", |
||
94 |
"cfhlNno:qrtv", |
||
95 |
"fghqr", |
||
96 |
z_ropen, |
||
97 |
zread, |
||
98 |
z_wopen, |
||
99 |
zwrite, |
||
100 |
z_close |
||
101 |
}, |
||
102 |
#endif /* SMALL */ |
||
103 |
{ NULL } |
||
104 |
}; |
||
105 |
|||
106 |
#ifndef SMALL |
||
107 |
const struct compressor null_method = { |
||
108 |
"null", |
||
109 |
".nul", |
||
110 |
"XX", |
||
111 |
"123456789ab:cdfghlNnOo:qrS:tv", |
||
112 |
"cfhlNno:qrtv", |
||
113 |
"fghqr", |
||
114 |
null_ropen, |
||
115 |
null_read, |
||
116 |
null_wopen, |
||
117 |
null_write, |
||
118 |
null_close |
||
119 |
}; |
||
120 |
#endif /* SMALL */ |
||
121 |
|||
122 |
int permission(const char *); |
||
123 |
__dead void usage(int); |
||
124 |
int docompress(const char *, char *, const struct compressor *, |
||
125 |
int, struct stat *); |
||
126 |
int dodecompress(const char *, char *, struct stat *); |
||
127 |
const struct compressor *check_method(int); |
||
128 |
const char *check_suffix(const char *); |
||
129 |
char *set_outfile(const char *, char *, size_t); |
||
130 |
void list_stats(const char *, const struct compressor *, struct z_info *); |
||
131 |
void verbose_info(const char *, off_t, off_t, u_int32_t); |
||
132 |
|||
133 |
const struct option longopts[] = { |
||
134 |
#ifndef SMALL |
||
135 |
{ "ascii", no_argument, 0, 'a' }, |
||
136 |
{ "stdout", no_argument, 0, 'c' }, |
||
137 |
{ "to-stdout", no_argument, 0, 'c' }, |
||
138 |
{ "decompress", no_argument, 0, 'd' }, |
||
139 |
{ "uncompress", no_argument, 0, 'd' }, |
||
140 |
{ "force", no_argument, 0, 'f' }, |
||
141 |
{ "help", no_argument, 0, 'h' }, |
||
142 |
{ "list", no_argument, 0, 'l' }, |
||
143 |
{ "license", no_argument, 0, 'L' }, |
||
144 |
{ "no-name", no_argument, 0, 'n' }, |
||
145 |
{ "name", no_argument, 0, 'N' }, |
||
146 |
{ "quiet", no_argument, 0, 'q' }, |
||
147 |
{ "recursive", no_argument, 0, 'r' }, |
||
148 |
{ "suffix", required_argument, 0, 'S' }, |
||
149 |
{ "test", no_argument, 0, 't' }, |
||
150 |
{ "verbose", no_argument, 0, 'v' }, |
||
151 |
{ "version", no_argument, 0, 'V' }, |
||
152 |
{ "fast", no_argument, 0, '1' }, |
||
153 |
{ "best", no_argument, 0, '9' }, |
||
154 |
#endif /* SMALL */ |
||
155 |
{ NULL } |
||
156 |
}; |
||
157 |
|||
158 |
int |
||
159 |
main(int argc, char *argv[]) |
||
160 |
{ |
||
161 |
FTS *ftsp; |
||
162 |
FTSENT *entry; |
||
163 |
const struct compressor *method; |
||
164 |
const char *optstr, *s; |
||
165 |
2810 |
char *p, *infile; |
|
166 |
1405 |
char outfile[PATH_MAX], _infile[PATH_MAX], suffix[16]; |
|
167 |
int bits, ch, error, rc, cflag, oflag; |
||
168 |
|||
169 |
✗✓ | 1405 |
if (pledge("stdio rpath wpath cpath fattr chown flock", NULL) == -1) |
170 |
err(1, "pledge"); |
||
171 |
|||
172 |
bits = cflag = oflag = 0; |
||
173 |
1405 |
storename = -1; |
|
174 |
1405 |
p = __progname; |
|
175 |
✓✗ | 1405 |
if (p[0] == 'g') { |
176 |
method = M_DEFLATE; |
||
177 |
bits = 6; |
||
178 |
1405 |
p++; |
|
179 |
1405 |
} else { |
|
180 |
#ifdef SMALL |
||
181 |
method = M_DEFLATE; |
||
182 |
#else |
||
183 |
method = M_COMPRESS; |
||
184 |
#endif /* SMALL */ |
||
185 |
} |
||
186 |
1405 |
optstr = method->comp_opts; |
|
187 |
|||
188 |
1405 |
decomp = 0; |
|
189 |
1405 |
pmode = MODE_COMP; |
|
190 |
✗✓ | 1405 |
if (!strcmp(p, "zcat")) { |
191 |
decomp++; |
||
192 |
cflag = 1; |
||
193 |
pmode = MODE_CAT; |
||
194 |
} else { |
||
195 |
✓✓✓✗ |
1455 |
if (p[0] == 'u' && p[1] == 'n') { |
196 |
50 |
p += 2; |
|
197 |
50 |
decomp++; |
|
198 |
50 |
pmode = MODE_DECOMP; |
|
199 |
50 |
} |
|
200 |
|||
201 |
✗✓✗✗ |
1405 |
if (strcmp(p, "zip") && |
202 |
strcmp(p, "compress")) |
||
203 |
errx(1, "unknown program name"); |
||
204 |
} |
||
205 |
|||
206 |
1405 |
strlcpy(suffix, method->suffix, sizeof(suffix)); |
|
207 |
|||
208 |
✓✗✗✓ |
2810 |
if (method == M_DEFLATE && (p = getenv("GZIP")) != NULL) { |
209 |
char *evbuf, *last, **nargv = NULL; |
||
210 |
int argc_extra = 0, nargc = 0; |
||
211 |
|||
212 |
if ((evbuf = strdup(p)) == NULL) |
||
213 |
err(1, NULL); |
||
214 |
for ((p = strtok_r(evbuf, " ", &last)); p != NULL; |
||
215 |
(p = strtok_r(NULL, " ", &last))) { |
||
216 |
if (nargc + 1 >= argc_extra) { |
||
217 |
argc_extra += 1024; |
||
218 |
nargv = reallocarray(nargv, |
||
219 |
argc + argc_extra + 1, sizeof(char *)); |
||
220 |
if (nargv == NULL) |
||
221 |
err(1, NULL); |
||
222 |
} |
||
223 |
nargv[++nargc] = p; |
||
224 |
} |
||
225 |
if (nargv != NULL) { |
||
226 |
nargv[0] = *argv++; |
||
227 |
while ((nargv[++nargc] = *argv++)) |
||
228 |
; |
||
229 |
argv = nargv; |
||
230 |
argc = nargc; |
||
231 |
} |
||
232 |
} |
||
233 |
|||
234 |
1405 |
optstr += pmode; |
|
235 |
✓✓ | 5332 |
while ((ch = getopt_long(argc, argv, optstr, longopts, NULL)) != -1) |
236 |
✗✗✗✗ ✗✗✗✗ ✗✗✗✓ ✓✓✗✗ ✗✗✗✗ ✗✗✓✗ ✓✗✗✗ ✗ |
2522 |
switch (ch) { |
237 |
case '1': |
||
238 |
case '2': |
||
239 |
case '3': |
||
240 |
case '4': |
||
241 |
case '5': |
||
242 |
case '6': |
||
243 |
case '7': |
||
244 |
case '8': |
||
245 |
case '9': |
||
246 |
method = M_DEFLATE; |
||
247 |
strlcpy(suffix, method->suffix, sizeof(suffix)); |
||
248 |
bits = ch - '0'; |
||
249 |
break; |
||
250 |
case 'a': |
||
251 |
warnx("option -a is ignored on this system"); |
||
252 |
break; |
||
253 |
case 'b': |
||
254 |
bits = strtol(optarg, &p, 10); |
||
255 |
/* |
||
256 |
* POSIX 1002.3 says 9 <= bits <= 14 for portable |
||
257 |
* apps, but says the implementation may allow |
||
258 |
* greater. |
||
259 |
*/ |
||
260 |
if (*p) |
||
261 |
errx(1, "illegal bit count -- %s", optarg); |
||
262 |
break; |
||
263 |
case 'c': |
||
264 |
cflag = 1; |
||
265 |
89 |
break; |
|
266 |
case 'd': /* Backward compatible. */ |
||
267 |
71 |
decomp++; |
|
268 |
71 |
break; |
|
269 |
case 'f': |
||
270 |
24 |
force++; |
|
271 |
24 |
break; |
|
272 |
case 'g': |
||
273 |
method = M_DEFLATE; |
||
274 |
strlcpy(suffix, method->suffix, sizeof(suffix)); |
||
275 |
bits = 6; |
||
276 |
break; |
||
277 |
case 'l': |
||
278 |
list++; |
||
279 |
testmode = 1; |
||
280 |
decomp++; |
||
281 |
break; |
||
282 |
case 'n': |
||
283 |
storename = 0; |
||
284 |
break; |
||
285 |
case 'N': |
||
286 |
storename = 1; |
||
287 |
break; |
||
288 |
#ifndef SMALL |
||
289 |
case 'O': |
||
290 |
method = M_COMPRESS; |
||
291 |
strlcpy(suffix, method->suffix, sizeof(suffix)); |
||
292 |
break; |
||
293 |
#endif /* SMALL */ |
||
294 |
case 'o': |
||
295 |
if (strlcpy(outfile, optarg, |
||
296 |
sizeof(outfile)) >= sizeof(outfile)) |
||
297 |
errx(1, "-o argument is too long"); |
||
298 |
oflag = 1; |
||
299 |
break; |
||
300 |
case 'q': |
||
301 |
verbose = -1; |
||
302 |
break; |
||
303 |
case 'S': |
||
304 |
p = suffix; |
||
305 |
if (optarg[0] != '.') |
||
306 |
*p++ = '.'; |
||
307 |
strlcpy(p, optarg, sizeof(suffix) - (p - suffix)); |
||
308 |
p = optarg; |
||
309 |
break; |
||
310 |
case 't': |
||
311 |
1161 |
testmode = 1; |
|
312 |
1161 |
decomp++; |
|
313 |
1161 |
break; |
|
314 |
case 'V': |
||
315 |
exit (0); |
||
316 |
case 'v': |
||
317 |
1177 |
verbose++; |
|
318 |
1177 |
break; |
|
319 |
case 'L': |
||
320 |
exit (0); |
||
321 |
case 'r': |
||
322 |
recurse++; |
||
323 |
break; |
||
324 |
|||
325 |
case 'h': |
||
326 |
usage(0); |
||
327 |
break; |
||
328 |
default: |
||
329 |
usage(1); |
||
330 |
} |
||
331 |
1405 |
argc -= optind; |
|
332 |
1405 |
argv += optind; |
|
333 |
|||
334 |
✓✓✓✓ |
1560 |
if (cflag || testmode || (!oflag && argc == 0)) |
335 |
✗✓ | 1338 |
if (pledge("stdio rpath flock cpath wpath", NULL) == -1) |
336 |
err(1, "pledge"); |
||
337 |
|||
338 |
✓✓ | 1405 |
if (argc == 0) { |
339 |
169 |
argv = calloc(2, sizeof(char *)); |
|
340 |
✗✓ | 169 |
if (argv == NULL) |
341 |
err(1, NULL); |
||
342 |
169 |
argv[0] = "-"; |
|
343 |
argc = 1; |
||
344 |
169 |
} |
|
345 |
✗✓✗✗ |
1405 |
if (oflag && (recurse || argc > 1)) |
346 |
errx(1, "-o option may only be used with a single input file"); |
||
347 |
|||
348 |
✗✓ | 1405 |
if ((cat && argc) + testmode + oflag > 1) |
349 |
errx(1, "may not mix -o, -c, or -t options"); |
||
350 |
/* |
||
351 |
* By default, when compressing store the original name and timestamp |
||
352 |
* in the header. Do not restore these when decompressing unless |
||
353 |
* the -N option is given. |
||
354 |
*/ |
||
355 |
✓✗ | 1405 |
if (storename == -1) |
356 |
1405 |
storename = !decomp; |
|
357 |
|||
358 |
✗✓ | 1405 |
if ((ftsp = fts_open(argv, FTS_PHYSICAL|FTS_NOCHDIR, 0)) == NULL) |
359 |
err(1, NULL); |
||
360 |
✓✓ | 4226 |
for (rc = SUCCESS; (entry = fts_read(ftsp)) != NULL;) { |
361 |
1416 |
cat = cflag; |
|
362 |
1416 |
pipin = 0; |
|
363 |
1416 |
infile = entry->fts_path; |
|
364 |
✓✓✓✗ |
1585 |
if (infile[0] == '-' && infile[1] == '\0') { |
365 |
infile = "stdin"; |
||
366 |
169 |
pipin++; |
|
367 |
✓✗ | 169 |
if (!oflag) |
368 |
169 |
cat = 1; |
|
369 |
} |
||
370 |
else |
||
371 |
✗✗✗✗ ✗✓ |
1247 |
switch (entry->fts_info) { |
372 |
case FTS_D: |
||
373 |
if (!recurse) { |
||
374 |
warnx("%s is a directory: ignored", |
||
375 |
infile); |
||
376 |
fts_set(ftsp, entry, FTS_SKIP); |
||
377 |
} |
||
378 |
continue; |
||
379 |
case FTS_DP: |
||
380 |
continue; |
||
381 |
case FTS_NS: |
||
382 |
/* |
||
383 |
* If file does not exist and has no suffix, |
||
384 |
* tack on the default suffix and try that. |
||
385 |
*/ |
||
386 |
if (entry->fts_errno == ENOENT) { |
||
387 |
p = strrchr(entry->fts_accpath, '.'); |
||
388 |
if ((p == NULL || |
||
389 |
strcmp(p, suffix) != 0) && |
||
390 |
snprintf(_infile, sizeof(_infile), |
||
391 |
"%s%s", infile, suffix) < |
||
392 |
sizeof(_infile) && |
||
393 |
stat(_infile, entry->fts_statp) == |
||
394 |
0 && |
||
395 |
S_ISREG(entry->fts_statp->st_mode)) { |
||
396 |
infile = _infile; |
||
397 |
break; |
||
398 |
} |
||
399 |
} |
||
400 |
case FTS_ERR: |
||
401 |
case FTS_DNR: |
||
402 |
warnx("%s: %s", infile, |
||
403 |
strerror(entry->fts_errno)); |
||
404 |
rc = rc ? rc : WARNING; |
||
405 |
continue; |
||
406 |
default: |
||
407 |
✗✓ | 1247 |
if (!S_ISREG(entry->fts_statp->st_mode) && |
408 |
!(S_ISLNK(entry->fts_statp->st_mode) && |
||
409 |
cat)) { |
||
410 |
warnx("%s not a regular file%s", |
||
411 |
infile, cat ? "" : ": unchanged"); |
||
412 |
rc = rc ? rc : WARNING; |
||
413 |
continue; |
||
414 |
} |
||
415 |
break; |
||
416 |
} |
||
417 |
|||
418 |
✓✓✗✓ |
1461 |
if (!decomp && !pipin && (s = check_suffix(infile)) != NULL) { |
419 |
warnx("%s already has %s suffix -- unchanged", |
||
420 |
infile, s); |
||
421 |
rc = rc ? rc : WARNING; |
||
422 |
continue; |
||
423 |
} |
||
424 |
|||
425 |
✓✗ | 1416 |
if (!oflag) { |
426 |
✓✓ | 1416 |
if (cat) |
427 |
191 |
strlcpy(outfile, "stdout", sizeof(outfile)); |
|
428 |
✓✓ | 1225 |
else if (decomp) { |
429 |
✗✓ | 2404 |
if (set_outfile(infile, outfile, |
430 |
1202 |
sizeof outfile) == NULL) { |
|
431 |
if (!recurse) { |
||
432 |
warnx("%s: unknown suffix: " |
||
433 |
"ignored", infile); |
||
434 |
rc = rc ? rc : WARNING; |
||
435 |
} |
||
436 |
continue; |
||
437 |
} |
||
438 |
} else { |
||
439 |
✗✓ | 46 |
if (snprintf(outfile, sizeof(outfile), |
440 |
23 |
"%s%s", infile, suffix) >= sizeof(outfile)) { |
|
441 |
warnx("%s%s: name too long", |
||
442 |
infile, suffix); |
||
443 |
rc = rc ? rc : WARNING; |
||
444 |
continue; |
||
445 |
} |
||
446 |
} |
||
447 |
} |
||
448 |
|||
449 |
✓✓ | 1416 |
if (verbose > 0 && !pipin && !list) |
450 |
1155 |
fprintf(stderr, "%s:\t", infile); |
|
451 |
|||
452 |
✓✓ | 1416 |
if (decomp) |
453 |
1276 |
error = dodecompress(infile, outfile, entry->fts_statp); |
|
454 |
else |
||
455 |
140 |
error = docompress(infile, outfile, method, bits, entry->fts_statp); |
|
456 |
|||
457 |
✓✓✓ | 1416 |
switch (error) { |
458 |
case SUCCESS: |
||
459 |
✓✓ | 566 |
if (!cat && !testmode) { |
460 |
✓✗ | 350 |
if (!pipin && unlink(infile) && verbose >= 0) |
461 |
warn("input: %s", infile); |
||
462 |
} |
||
463 |
break; |
||
464 |
case WARNING: |
||
465 |
1111 |
rc = rc ? rc : WARNING; |
|
466 |
1111 |
break; |
|
467 |
default: |
||
468 |
rc = FAILURE; |
||
469 |
22 |
break; |
|
470 |
} |
||
471 |
} |
||
472 |
✗✓ | 1405 |
if (list) |
473 |
list_stats(NULL, NULL, NULL); |
||
474 |
fts_close(ftsp); |
||
475 |
exit(rc); |
||
476 |
} |
||
477 |
|||
478 |
int |
||
479 |
docompress(const char *in, char *out, const struct compressor *method, |
||
480 |
int bits, struct stat *sb) |
||
481 |
{ |
||
482 |
#ifndef SMALL |
||
483 |
280 |
u_char buf[Z_BUFSIZE]; |
|
484 |
char *name; |
||
485 |
int error, ifd, ofd, oreg; |
||
486 |
void *cookie; |
||
487 |
ssize_t nr; |
||
488 |
u_int32_t mtime; |
||
489 |
140 |
struct z_info info; |
|
490 |
140 |
struct stat osb; |
|
491 |
|||
492 |
mtime = 0; |
||
493 |
oreg = 0; |
||
494 |
error = SUCCESS; |
||
495 |
name = NULL; |
||
496 |
cookie = NULL; |
||
497 |
|||
498 |
✓✓ | 140 |
if (pipin) |
499 |
95 |
ifd = dup(STDIN_FILENO); |
|
500 |
else |
||
501 |
45 |
ifd = open(in, O_RDONLY); |
|
502 |
✗✓ | 140 |
if (ifd < 0) { |
503 |
if (verbose >= 0) |
||
504 |
warn("%s", in); |
||
505 |
return (FAILURE); |
||
506 |
} |
||
507 |
|||
508 |
✓✓ | 140 |
if (cat) |
509 |
117 |
ofd = dup(STDOUT_FILENO); |
|
510 |
else { |
||
511 |
✗✓ | 23 |
if (stat(out, &osb) == 0) { |
512 |
oreg = S_ISREG(osb.st_mode); |
||
513 |
if (!force && oreg && !permission(out)) { |
||
514 |
(void) close(ifd); |
||
515 |
return (WARNING); |
||
516 |
} |
||
517 |
} |
||
518 |
23 |
ofd = open(out, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR); |
|
519 |
} |
||
520 |
✗✓ | 140 |
if (ofd < 0) { |
521 |
if (verbose >= 0) |
||
522 |
warn("%s", out); |
||
523 |
(void) close(ifd); |
||
524 |
return (FAILURE); |
||
525 |
} |
||
526 |
|||
527 |
✓✓✗✓ |
279 |
if (method != M_COMPRESS && !force && isatty(ofd)) { |
528 |
if (verbose >= 0) |
||
529 |
warnx("%s: won't write compressed data to terminal", |
||
530 |
out); |
||
531 |
(void) close(ofd); |
||
532 |
(void) close(ifd); |
||
533 |
return (FAILURE); |
||
534 |
} |
||
535 |
|||
536 |
✓✓ | 140 |
if (!pipin && storename) { |
537 |
45 |
name = basename(in); |
|
538 |
45 |
mtime = (u_int32_t)sb->st_mtime; |
|
539 |
45 |
} |
|
540 |
✗✓ | 140 |
if ((cookie = method->wopen(ofd, name, bits, mtime)) == NULL) { |
541 |
if (verbose >= 0) |
||
542 |
warn("%s", out); |
||
543 |
if (oreg) |
||
544 |
(void) unlink(out); |
||
545 |
(void) close(ofd); |
||
546 |
(void) close(ifd); |
||
547 |
return (FAILURE); |
||
548 |
} |
||
549 |
|||
550 |
✓✓ | 6401 |
while ((nr = read(ifd, buf, sizeof(buf))) > 0) |
551 |
✓✗ | 6261 |
if (method->write(cookie, buf, nr) != nr) { |
552 |
if (verbose >= 0) |
||
553 |
warn("%s", out); |
||
554 |
error = FAILURE; |
||
555 |
break; |
||
556 |
} |
||
557 |
|||
558 |
✗✓ | 140 |
if (!error && nr < 0) { |
559 |
if (verbose >= 0) |
||
560 |
warn("%s", in); |
||
561 |
error = FAILURE; |
||
562 |
} |
||
563 |
|||
564 |
✗✓ | 140 |
if (method->close(cookie, &info, out, sb)) { |
565 |
if (!error && verbose >= 0) |
||
566 |
warn("%s", out); |
||
567 |
error = FAILURE; |
||
568 |
} |
||
569 |
|||
570 |
✗✓ | 140 |
if (close(ifd)) { |
571 |
if (!error && verbose >= 0) |
||
572 |
warn("%s", in); |
||
573 |
error = FAILURE; |
||
574 |
} |
||
575 |
|||
576 |
✓✓✗✓ |
162 |
if (!force && !cat && info.total_out >= info.total_in) { |
577 |
if (verbose > 0) |
||
578 |
fprintf(stderr, "file would grow; left unmodified\n"); |
||
579 |
(void) unlink(out); |
||
580 |
error = WARNING; |
||
581 |
} |
||
582 |
|||
583 |
✗✓ | 140 |
if (error) { |
584 |
if (oreg) |
||
585 |
(void) unlink(out); |
||
586 |
✓✓ | 140 |
} else if (verbose > 0) |
587 |
22 |
verbose_info(out, info.total_out, info.total_in, info.hlen); |
|
588 |
|||
589 |
140 |
return (error); |
|
590 |
#else |
||
591 |
warnx("compression not supported"); |
||
592 |
return (FAILURE); |
||
593 |
#endif |
||
594 |
140 |
} |
|
595 |
|||
596 |
const struct compressor * |
||
597 |
check_method(int fd) |
||
598 |
{ |
||
599 |
const struct compressor *method; |
||
600 |
2552 |
u_char magic[2]; |
|
601 |
|||
602 |
✗✓ | 1276 |
if (read(fd, magic, sizeof(magic)) != 2) |
603 |
return (NULL); |
||
604 |
✓✗ | 2552 |
for (method = &c_table[0]; method->name != NULL; method++) { |
605 |
✓✗✓✗ |
2552 |
if (magic[0] == method->magic[0] && |
606 |
1276 |
magic[1] == method->magic[1]) |
|
607 |
1276 |
return (method); |
|
608 |
} |
||
609 |
#ifndef SMALL |
||
610 |
if (force && cat) { |
||
611 |
null_magic[0] = magic[0]; |
||
612 |
null_magic[1] = magic[1]; |
||
613 |
return (&null_method); |
||
614 |
} |
||
615 |
#endif /* SMALL */ |
||
616 |
return (NULL); |
||
617 |
1276 |
} |
|
618 |
|||
619 |
int |
||
620 |
dodecompress(const char *in, char *out, struct stat *sb) |
||
621 |
{ |
||
622 |
const struct compressor *method; |
||
623 |
2552 |
u_char buf[Z_BUFSIZE]; |
|
624 |
1276 |
char oldname[PATH_MAX]; |
|
625 |
int error, oreg, ifd, ofd; |
||
626 |
void *cookie; |
||
627 |
ssize_t nr; |
||
628 |
1276 |
struct z_info info; |
|
629 |
1276 |
struct stat osb; |
|
630 |
|||
631 |
oreg = 0; |
||
632 |
error = SUCCESS; |
||
633 |
cookie = NULL; |
||
634 |
|||
635 |
✓✓ | 1276 |
if (pipin) |
636 |
74 |
ifd = dup(STDIN_FILENO); |
|
637 |
else |
||
638 |
1202 |
ifd = open(in, O_RDONLY); |
|
639 |
✗✓ | 1276 |
if (ifd < 0) { |
640 |
if (verbose >= 0) |
||
641 |
warn("%s", in); |
||
642 |
return -1; |
||
643 |
} |
||
644 |
|||
645 |
✓✓✗✓ |
2529 |
if (!force && isatty(ifd)) { |
646 |
if (verbose >= 0) |
||
647 |
warnx("%s: won't read compressed data from terminal", |
||
648 |
in); |
||
649 |
close (ifd); |
||
650 |
return -1; |
||
651 |
} |
||
652 |
|||
653 |
✗✓ | 1276 |
if ((method = check_method(ifd)) == NULL) { |
654 |
if (verbose >= 0) |
||
655 |
warnx("%s: unrecognized file format", in); |
||
656 |
close (ifd); |
||
657 |
return -1; |
||
658 |
} |
||
659 |
|||
660 |
/* XXX - open constrains outfile to MAXPATHLEN so this is safe */ |
||
661 |
1276 |
oldname[0] = '\0'; |
|
662 |
✗✓ | 1276 |
if ((cookie = method->ropen(ifd, oldname, 1)) == NULL) { |
663 |
if (verbose >= 0) |
||
664 |
warn("%s", in); |
||
665 |
close (ifd); |
||
666 |
return (FAILURE); |
||
667 |
} |
||
668 |
✗✓✗✗ |
1276 |
if (storename && oldname[0] != '\0') { |
669 |
char *cp = strrchr(out, '/'); |
||
670 |
if (cp != NULL) { |
||
671 |
*(cp + 1) = '\0'; |
||
672 |
strlcat(out, oldname, PATH_MAX); |
||
673 |
} else |
||
674 |
strlcpy(out, oldname, PATH_MAX); |
||
675 |
cat = 0; /* XXX should -c override? */ |
||
676 |
} |
||
677 |
|||
678 |
✓✓ | 1276 |
if (testmode) |
679 |
1161 |
ofd = -1; |
|
680 |
else { |
||
681 |
✓✓ | 115 |
if (cat) |
682 |
71 |
ofd = dup(STDOUT_FILENO); |
|
683 |
else { |
||
684 |
✓✓ | 44 |
if (stat(out, &osb) == 0) { |
685 |
10 |
oreg = S_ISREG(osb.st_mode); |
|
686 |
✗✓✗✗ |
10 |
if (!force && oreg && !permission(out)) { |
687 |
(void) close(ifd); |
||
688 |
return (WARNING); |
||
689 |
} |
||
690 |
} |
||
691 |
44 |
ofd = open(out, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR); |
|
692 |
} |
||
693 |
✗✓ | 115 |
if (ofd < 0) { |
694 |
if (verbose >= 0) |
||
695 |
warn("%s", in); |
||
696 |
method->close(cookie, NULL, NULL, NULL); |
||
697 |
return (FAILURE); |
||
698 |
} |
||
699 |
} |
||
700 |
|||
701 |
✓✓ | 52725 |
while ((nr = method->read(cookie, buf, sizeof(buf))) > 0) { |
702 |
✓✓✓✗ |
102864 |
if (ofd != -1 && write(ofd, buf, nr) != nr) { |
703 |
if (verbose >= 0) |
||
704 |
warn("%s", out); |
||
705 |
error = FAILURE; |
||
706 |
break; |
||
707 |
} |
||
708 |
} |
||
709 |
|||
710 |
✓✓ | 1276 |
if (!error && nr < 0) { |
711 |
✓✗ | 1133 |
if (verbose >= 0) |
712 |
1133 |
warnx("%s: %s", in, |
|
713 |
✓✓ | 2288 |
errno == EINVAL ? "crc error" : strerror(errno)); |
714 |
1133 |
error = errno == EINVAL ? WARNING : FAILURE; |
|
715 |
1133 |
} |
|
716 |
|||
717 |
✗✓ | 1276 |
if (method->close(cookie, &info, NULL, NULL)) { |
718 |
if (!error && verbose >= 0) |
||
719 |
warnx("%s", in); |
||
720 |
error = FAILURE; |
||
721 |
} |
||
722 |
✗✓ | 1276 |
if (storename && !cat) { |
723 |
if (info.mtime != 0) { |
||
724 |
sb->st_mtimespec.tv_sec = |
||
725 |
sb->st_atimespec.tv_sec = info.mtime; |
||
726 |
sb->st_mtimespec.tv_nsec = |
||
727 |
sb->st_atimespec.tv_nsec = 0; |
||
728 |
} else |
||
729 |
storename = 0; /* no timestamp to restore */ |
||
730 |
} |
||
731 |
✓✓ | 1276 |
if (error == SUCCESS) |
732 |
143 |
setfile(out, ofd, sb); |
|
733 |
|||
734 |
✓✓✗✓ |
1391 |
if (ofd != -1 && close(ofd)) { |
735 |
if (!error && verbose >= 0) |
||
736 |
warn("%s", out); |
||
737 |
error = FAILURE; |
||
738 |
} |
||
739 |
|||
740 |
✓✓ | 1276 |
if (!error) { |
741 |
✗✓ | 143 |
if (list) { |
742 |
if (info.mtime == 0) |
||
743 |
info.mtime = (u_int32_t)sb->st_mtime; |
||
744 |
list_stats(out, method, &info); |
||
745 |
✓✓ | 143 |
} else if (verbose > 0) { |
746 |
44 |
verbose_info(out, info.total_in, info.total_out, |
|
747 |
22 |
info.hlen); |
|
748 |
22 |
} |
|
749 |
} |
||
750 |
|||
751 |
/* On error, clean up the file we created but preserve errno. */ |
||
752 |
✗✓ | 1276 |
if (error && oreg) |
753 |
unlink(out); |
||
754 |
|||
755 |
1276 |
return (error); |
|
756 |
1276 |
} |
|
757 |
|||
758 |
void |
||
759 |
setfile(const char *name, int fd, struct stat *fs) |
||
760 |
{ |
||
761 |
3118 |
struct timespec ts[2]; |
|
762 |
|||
763 |
✓✓ | 1559 |
if (name == NULL || cat || testmode) |
764 |
1492 |
return; |
|
765 |
|||
766 |
/* |
||
767 |
* If input was a pipe we don't have any info to restore but we |
||
768 |
* must set the mode since the current mode on the file is 0200. |
||
769 |
*/ |
||
770 |
✗✓ | 67 |
if (pipin) { |
771 |
mode_t mask = umask(022); |
||
772 |
fchmod(fd, DEFFILEMODE & ~mask); |
||
773 |
umask(mask); |
||
774 |
return; |
||
775 |
} |
||
776 |
|||
777 |
/* |
||
778 |
* Changing the ownership probably won't succeed, unless we're root |
||
779 |
* or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid bits are not |
||
780 |
* allowed. |
||
781 |
*/ |
||
782 |
67 |
fs->st_mode &= ACCESSPERMS; |
|
783 |
✗✓ | 67 |
if (fchown(fd, fs->st_uid, fs->st_gid)) { |
784 |
if (errno != EPERM) |
||
785 |
warn("fchown: %s", name); |
||
786 |
fs->st_mode &= ~(S_ISUID|S_ISGID); |
||
787 |
} |
||
788 |
✗✓ | 67 |
if (fchmod(fd, fs->st_mode)) |
789 |
warn("fchmod: %s", name); |
||
790 |
|||
791 |
✗✓✗✗ |
67 |
if (fs->st_flags && fchflags(fd, fs->st_flags)) |
792 |
warn("fchflags: %s", name); |
||
793 |
|||
794 |
67 |
ts[0] = fs->st_atim; |
|
795 |
67 |
ts[1] = fs->st_mtim; |
|
796 |
✗✓ | 67 |
if (futimens(fd, ts)) |
797 |
warn("futimens: %s", name); |
||
798 |
1626 |
} |
|
799 |
|||
800 |
int |
||
801 |
permission(const char *fname) |
||
802 |
{ |
||
803 |
int ch, first; |
||
804 |
|||
805 |
if (!isatty(fileno(stderr))) |
||
806 |
return (0); |
||
807 |
(void)fprintf(stderr, "overwrite %s? ", fname); |
||
808 |
first = ch = getchar(); |
||
809 |
while (ch != '\n' && ch != EOF) |
||
810 |
ch = getchar(); |
||
811 |
return (first == 'y'); |
||
812 |
} |
||
813 |
|||
814 |
/* |
||
815 |
* Check infile for a known suffix and return the suffix portion or NULL. |
||
816 |
*/ |
||
817 |
const char * |
||
818 |
check_suffix(const char *infile) |
||
819 |
{ |
||
820 |
int i; |
||
821 |
char *suf, *sep, *separators = ".-_"; |
||
822 |
static char *suffixes[] = { "Z", "gz", "z", "tgz", "taz", NULL }; |
||
823 |
|||
824 |
✓✓ | 4011 |
for (sep = separators; *sep != '\0'; sep++) { |
825 |
✓✓ | 1337 |
if ((suf = strrchr(infile, *sep)) == NULL) |
826 |
continue; |
||
827 |
1203 |
suf++; |
|
828 |
|||
829 |
✓✓ | 4832 |
for (i = 0; suffixes[i] != NULL; i++) { |
830 |
✓✓ | 2415 |
if (strcmp(suf, suffixes[i]) == 0) |
831 |
1202 |
return (suf - 1); |
|
832 |
} |
||
833 |
} |
||
834 |
45 |
return (NULL); |
|
835 |
1247 |
} |
|
836 |
|||
837 |
/* |
||
838 |
* Set outfile based on the suffix. In most cases we just strip |
||
839 |
* off the suffix but things like .tgz and .taz are special. |
||
840 |
*/ |
||
841 |
char * |
||
842 |
set_outfile(const char *infile, char *outfile, size_t osize) |
||
843 |
{ |
||
844 |
const char *s; |
||
845 |
char *cp; |
||
846 |
|||
847 |
✗✓ | 2404 |
if ((s = check_suffix(infile)) == NULL) |
848 |
return (NULL); |
||
849 |
|||
850 |
1202 |
(void)strlcpy(outfile, infile, osize); |
|
851 |
1202 |
cp = outfile + (s - infile) + 1; |
|
852 |
/* |
||
853 |
* Convert tgz and taz -> tar, else drop the suffix. |
||
854 |
*/ |
||
855 |
✓✓ | 1202 |
if (strcmp(cp, "tgz") == 0) { |
856 |
3 |
cp[1] = 'a'; |
|
857 |
3 |
cp[2] = 'r'; |
|
858 |
1202 |
} else if (strcmp(cp, "taz") == 0) |
|
859 |
cp[2] = 'r'; |
||
860 |
else |
||
861 |
cp[-1] = '\0'; |
||
862 |
1202 |
return (outfile); |
|
863 |
1202 |
} |
|
864 |
|||
865 |
/* |
||
866 |
* Print output for the -l option. |
||
867 |
*/ |
||
868 |
void |
||
869 |
list_stats(const char *name, const struct compressor *method, |
||
870 |
struct z_info *info) |
||
871 |
{ |
||
872 |
static off_t compressed_total, uncompressed_total, header_total; |
||
873 |
static u_int nruns; |
||
874 |
char *timestr; |
||
875 |
|||
876 |
if (nruns == 0) { |
||
877 |
if (verbose >= 0) { |
||
878 |
if (verbose > 0) |
||
879 |
fputs("method crc date time ", stdout); |
||
880 |
puts("compressed uncompressed ratio uncompressed_name"); |
||
881 |
} |
||
882 |
} |
||
883 |
nruns++; |
||
884 |
|||
885 |
if (name != NULL) { |
||
886 |
if (verbose > 0) { |
||
887 |
time_t t = info->mtime; /* XXX 32 bit mtime */ |
||
888 |
|||
889 |
timestr = ctime(&t) + 4; |
||
890 |
timestr[12] = '\0'; |
||
891 |
if (timestr[4] == ' ') |
||
892 |
timestr[4] = '0'; |
||
893 |
printf("%-7.7s %08x %s ", method->name, info->crc, |
||
894 |
timestr); |
||
895 |
} |
||
896 |
printf("%10lld %10lld %4.1f%% %s\n", |
||
897 |
(long long)(info->total_in + info->hlen), |
||
898 |
(long long)info->total_out, |
||
899 |
((long long)info->total_out - (long long)info->total_in) * |
||
900 |
100.0 / info->total_out, name); |
||
901 |
compressed_total += info->total_in; |
||
902 |
uncompressed_total += info->total_out; |
||
903 |
header_total += info->hlen; |
||
904 |
} else if (verbose >= 0) { |
||
905 |
if (nruns < 3) /* only do totals for > 1 files */ |
||
906 |
return; |
||
907 |
if (verbose > 0) |
||
908 |
fputs(" ", stdout); |
||
909 |
printf("%10lld %10lld %4.1f%% (totals)\n", |
||
910 |
(long long)(compressed_total + header_total), |
||
911 |
(long long)uncompressed_total, |
||
912 |
(uncompressed_total - compressed_total) * |
||
913 |
100.0 / uncompressed_total); |
||
914 |
} |
||
915 |
} |
||
916 |
|||
917 |
void |
||
918 |
verbose_info(const char *file, off_t compressed, off_t uncompressed, |
||
919 |
u_int32_t hlen) |
||
920 |
{ |
||
921 |
✓✓ | 88 |
if (testmode) { |
922 |
22 |
fputs("OK\n", stderr); |
|
923 |
22 |
return; |
|
924 |
} |
||
925 |
✗✓ | 22 |
if (!pipin) { |
926 |
fprintf(stderr, "\t%4.1f%% -- replaced with %s\n", |
||
927 |
(uncompressed - compressed) * 100.0 / uncompressed, file); |
||
928 |
} |
||
929 |
22 |
compressed += hlen; |
|
930 |
22 |
fprintf(stderr, "%lld bytes in, %lld bytes out\n", |
|
931 |
22 |
(long long)(decomp ? compressed : uncompressed), |
|
932 |
22 |
(long long)(decomp ? uncompressed : compressed)); |
|
933 |
66 |
} |
|
934 |
|||
935 |
__dead void |
||
936 |
usage(int status) |
||
937 |
{ |
||
938 |
const bool gzip = (__progname[0] == 'g'); |
||
939 |
|||
940 |
switch (pmode) { |
||
941 |
case MODE_COMP: |
||
942 |
fprintf(stderr, "usage: %s [-123456789cdf%sh%slNnOqrt%sv] " |
||
943 |
"[-b bits] [-o filename] [-S suffix]\n" |
||
944 |
" %*s [file ...]\n", __progname, |
||
945 |
!gzip ? "g" : "", gzip ? "L" : "", gzip ? "V" : "", |
||
946 |
(int)strlen(__progname), ""); |
||
947 |
break; |
||
948 |
case MODE_DECOMP: |
||
949 |
fprintf(stderr, "usage: %s [-cfh%slNnqrt%sv] [-o filename] " |
||
950 |
"[file ...]\n", __progname, |
||
951 |
gzip ? "L" : "", gzip ? "V" : ""); |
||
952 |
break; |
||
953 |
case MODE_CAT: |
||
954 |
fprintf(stderr, "usage: %s [-f%shqr] [file ...]\n", |
||
955 |
__progname, gzip ? "" : "g"); |
||
956 |
break; |
||
957 |
} |
||
958 |
exit(status); |
||
959 |
} |
Generated by: GCOVR (Version 3.3) |