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