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
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
|
/* $NetBSD: gzip.c,v 1.89 2006/11/13 21:57:59 mrg Exp $ */
/*-
* Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green
* All rights reserved.
*
* 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. The name of the author may not 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 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 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/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green\n\
All rights reserved.\n");
__RCSID("$FreeBSD$");
#endif /* not lint */
/*
* gzip.c -- GPL free gzip using zlib.
*
* RFC 1950 covers the zlib format
* RFC 1951 covers the deflate format
* RFC 1952 covers the gzip format
*
* TODO:
* - use mmap where possible
* - handle some signals better (remove outfile?)
* - make bzip2/compress -v/-t/-l support work as well as possible
*/
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <inttypes.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <zlib.h>
#include <fts.h>
#include <libgen.h>
#include <stdarg.h>
#include <getopt.h>
#include <time.h>
#ifndef PRIdOFF
#define PRIdOFF PRId64
#endif
/* what type of file are we dealing with */
enum filetype {
FT_GZIP,
#ifndef NO_BZIP2_SUPPORT
FT_BZIP2,
#endif
#ifndef NO_COMPRESS_SUPPORT
FT_Z,
#endif
FT_LAST,
FT_UNKNOWN
};
#ifndef NO_BZIP2_SUPPORT
#include <bzlib.h>
#define BZ2_SUFFIX ".bz2"
#define BZIP2_MAGIC "\102\132\150"
#endif
#ifndef NO_COMPRESS_SUPPORT
#define Z_SUFFIX ".Z"
#define Z_MAGIC "\037\235"
#endif
#define GZ_SUFFIX ".gz"
#define BUFLEN (64 * 1024)
#define GZIP_MAGIC0 0x1F
#define GZIP_MAGIC1 0x8B
#define GZIP_OMAGIC1 0x9E
#define GZIP_TIMESTAMP (off_t)4
#define GZIP_ORIGNAME (off_t)10
#define HEAD_CRC 0x02
#define EXTRA_FIELD 0x04
#define ORIG_NAME 0x08
#define COMMENT 0x10
#define OS_CODE 3 /* Unix */
typedef struct {
const char *zipped;
int ziplen;
const char *normal; /* for unzip - must not be longer than zipped */
} suffixes_t;
static suffixes_t suffixes[] = {
#define SUFFIX(Z, N) {Z, sizeof Z - 1, N}
SUFFIX(GZ_SUFFIX, ""), /* Overwritten by -S .xxx */
#ifndef SMALL
SUFFIX(GZ_SUFFIX, ""),
SUFFIX(".z", ""),
SUFFIX("-gz", ""),
SUFFIX("-z", ""),
SUFFIX("_z", ""),
SUFFIX(".taz", ".tar"),
SUFFIX(".tgz", ".tar"),
#ifndef NO_BZIP2_SUPPORT
SUFFIX(BZ2_SUFFIX, ""),
#endif
#ifndef NO_COMPRESS_SUPPORT
SUFFIX(Z_SUFFIX, ""),
#endif
SUFFIX(GZ_SUFFIX, ""), /* Overwritten by -S "" */
#endif /* SMALL */
#undef SUFFIX
};
#define NUM_SUFFIXES (sizeof suffixes / sizeof suffixes[0])
static const char gzip_version[] = "FreeBSD gzip 20070711";
#ifndef SMALL
static const char gzip_copyright[] = \
" Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green\n"
" All rights reserved.\n"
"\n"
" Redistribution and use in source and binary forms, with or without\n"
" modification, are permitted provided that the following conditions\n"
" are met:\n"
" 1. Redistributions of source code must retain the above copyright\n"
" notice, this list of conditions and the following disclaimer.\n"
" 2. Redistributions in binary form must reproduce the above copyright\n"
" notice, this list of conditions and the following disclaimer in the\n"
" documentation and/or other materials provided with the distribution.\n"
" 3. The name of the author may not be used to endorse or promote products\n"
" derived from this software without specific prior written permission.\n"
"\n"
" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n"
" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n"
" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n"
" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\n"
" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,\n"
" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n"
" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED\n"
" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\n"
" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\n"
" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\n"
" SUCH DAMAGE.";
#endif
static int cflag; /* stdout mode */
static int dflag; /* decompress mode */
static int lflag; /* list mode */
static int numflag = 6; /* gzip -1..-9 value */
#ifndef SMALL
static int fflag; /* force mode */
static int kflag; /* don't delete input files */
static int nflag; /* don't save name/timestamp */
static int Nflag; /* don't restore name/timestamp */
static int qflag; /* quiet mode */
static int rflag; /* recursive mode */
static int tflag; /* test */
static int vflag; /* verbose mode */
#else
#define qflag 0
#define tflag 0
#endif
static int exit_value = 0; /* exit value */
static char *infile; /* name of file coming in */
static void maybe_err(const char *fmt, ...)
__attribute__((__format__(__printf__, 1, 2)));
#ifndef NO_BZIP2_SUPPORT
static void maybe_errx(const char *fmt, ...)
__attribute__((__format__(__printf__, 1, 2)));
#endif
static void maybe_warn(const char *fmt, ...)
__attribute__((__format__(__printf__, 1, 2)));
static void maybe_warnx(const char *fmt, ...)
__attribute__((__format__(__printf__, 1, 2)));
static enum filetype file_gettype(u_char *);
#ifdef SMALL
#define gz_compress(if, of, sz, fn, tm) gz_compress(if, of, sz)
#endif
static off_t gz_compress(int, int, off_t *, const char *, uint32_t);
static off_t gz_uncompress(int, int, char *, size_t, off_t *, const char *);
static off_t file_compress(char *, char *, size_t);
static off_t file_uncompress(char *, char *, size_t);
static void handle_pathname(char *);
static void handle_file(char *, struct stat *);
static void handle_stdin(void);
static void handle_stdout(void);
static void print_ratio(off_t, off_t, FILE *);
static void print_list(int fd, off_t, const char *, time_t);
static void usage(void);
static void display_version(void);
#ifndef SMALL
static void display_license(void);
#endif
static const suffixes_t *check_suffix(char *, int);
static ssize_t read_retry(int, void *, size_t);
#ifdef SMALL
#define unlink_input(f, sb) unlink(f)
#else
static off_t cat_fd(unsigned char *, size_t, off_t *, int fd);
static void prepend_gzip(char *, int *, char ***);
static void handle_dir(char *);
static void print_verbage(const char *, const char *, off_t, off_t);
static void print_test(const char *, int);
static void copymodes(int fd, const struct stat *, const char *file);
static int check_outfile(const char *outfile);
#endif
#ifndef NO_BZIP2_SUPPORT
static off_t unbzip2(int, int, char *, size_t, off_t *);
#endif
#ifndef NO_COMPRESS_SUPPORT
static FILE *zdopen(int);
static off_t zuncompress(FILE *, FILE *, char *, size_t, off_t *);
#endif
int main(int, char **p);
#ifdef SMALL
#define getopt_long(a,b,c,d,e) getopt(a,b,c)
#else
static const struct option longopts[] = {
{ "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' },
{ "keep", no_argument, 0, 'k' },
{ "list", 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' },
{ "ascii", no_argument, 0, 'a' },
{ "license", no_argument, 0, 'L' },
{ NULL, no_argument, 0, 0 },
};
#endif
int
main(int argc, char **argv)
{
const char *progname = getprogname();
#ifndef SMALL
char *gzip;
int len;
#endif
int ch;
/* XXX set up signals */
#ifndef SMALL
if ((gzip = getenv("GZIP")) != NULL)
prepend_gzip(gzip, &argc, &argv);
#endif
/*
* XXX
* handle being called `gunzip', `zcat' and `gzcat'
*/
if (strcmp(progname, "gunzip") == 0)
dflag = 1;
else if (strcmp(progname, "zcat") == 0 ||
strcmp(progname, "gzcat") == 0)
dflag = cflag = 1;
#ifdef SMALL
#define OPT_LIST "123456789cdhltV"
#else
#define OPT_LIST "123456789acdfhklLNnqrS:tVv"
#endif
while ((ch = getopt_long(argc, argv, OPT_LIST, longopts, NULL)) != -1) {
switch (ch) {
case '1': case '2': case '3':
case '4': case '5': case '6':
case '7': case '8': case '9':
numflag = ch - '0';
break;
case 'c':
cflag = 1;
break;
case 'd':
dflag = 1;
break;
case 'l':
lflag = 1;
dflag = 1;
break;
case 'V':
display_version();
/* NOTREACHED */
#ifndef SMALL
case 'a':
fprintf(stderr, "%s: option --ascii ignored on this system\n", progname);
break;
case 'f':
fflag = 1;
break;
case 'k':
kflag = 1;
break;
case 'L':
display_license();
/* NOT REACHED */
case 'N':
nflag = 0;
Nflag = 1;
break;
case 'n':
nflag = 1;
Nflag = 0;
break;
case 'q':
qflag = 1;
break;
case 'r':
rflag = 1;
break;
case 'S':
len = strlen(optarg);
if (len != 0) {
suffixes[0].zipped = optarg;
suffixes[0].ziplen = len;
} else {
suffixes[NUM_SUFFIXES - 1].zipped = "";
suffixes[NUM_SUFFIXES - 1].ziplen = 0;
}
break;
case 't':
cflag = 1;
tflag = 1;
dflag = 1;
break;
case 'v':
vflag = 1;
break;
#endif
default:
usage();
/* NOTREACHED */
}
}
argv += optind;
argc -= optind;
if (argc == 0) {
if (dflag) /* stdin mode */
handle_stdin();
else /* stdout mode */
handle_stdout();
} else {
do {
handle_pathname(argv[0]);
} while (*++argv);
}
#ifndef SMALL
if (qflag == 0 && lflag && argc > 1)
print_list(-1, 0, "(totals)", 0);
#endif
exit(exit_value);
}
/* maybe print a warning */
void
maybe_warn(const char *fmt, ...)
{
va_list ap;
if (qflag == 0) {
va_start(ap, fmt);
vwarn(fmt, ap);
va_end(ap);
}
if (exit_value == 0)
exit_value = 1;
}
/* ... without an errno. */
void
maybe_warnx(const char *fmt, ...)
{
va_list ap;
if (qflag == 0) {
va_start(ap, fmt);
vwarnx(fmt, ap);
va_end(ap);
}
if (exit_value == 0)
exit_value = 1;
}
/* maybe print an error */
void
maybe_err(const char *fmt, ...)
{
va_list ap;
if (qflag == 0) {
va_start(ap, fmt);
vwarn(fmt, ap);
va_end(ap);
}
exit(2);
}
#ifndef NO_BZIP2_SUPPORT
/* ... without an errno. */
void
maybe_errx(const char *fmt, ...)
{
va_list ap;
if (qflag == 0) {
va_start(ap, fmt);
vwarnx(fmt, ap);
va_end(ap);
}
exit(2);
}
#endif
#ifndef SMALL
/* split up $GZIP and prepend it to the argument list */
static void
prepend_gzip(char *gzip, int *argc, char ***argv)
{
char *s, **nargv, **ac;
int nenvarg = 0, i;
/* scan how many arguments there are */
for (s = gzip;;) {
while (*s == ' ' || *s == '\t')
s++;
if (*s == 0)
goto count_done;
nenvarg++;
while (*s != ' ' && *s != '\t')
if (*s++ == 0)
goto count_done;
}
count_done:
/* punt early */
if (nenvarg == 0)
return;
*argc += nenvarg;
ac = *argv;
nargv = (char **)malloc((*argc + 1) * sizeof(char *));
if (nargv == NULL)
maybe_err("malloc");
/* stash this away */
*argv = nargv;
/* copy the program name first */
i = 0;
nargv[i++] = *(ac++);
/* take a copy of $GZIP and add it to the array */
s = strdup(gzip);
if (s == NULL)
maybe_err("strdup");
for (;;) {
/* Skip whitespaces. */
while (*s == ' ' || *s == '\t')
s++;
if (*s == 0)
goto copy_done;
nargv[i++] = s;
/* Find the end of this argument. */
while (*s != ' ' && *s != '\t')
if (*s++ == 0)
/* Argument followed by NUL. */
goto copy_done;
/* Terminate by overwriting ' ' or '\t' with NUL. */
*s++ = 0;
}
copy_done:
/* copy the original arguments and a NULL */
while (*ac)
nargv[i++] = *(ac++);
nargv[i] = NULL;
}
#endif
/* compress input to output. Return bytes read, -1 on error */
static off_t
gz_compress(int in, int out, off_t *gsizep, const char *origname, uint32_t mtime)
{
z_stream z;
char *outbufp, *inbufp;
off_t in_tot = 0, out_tot = 0;
ssize_t in_size;
int i, error;
uLong crc;
#ifdef SMALL
static char header[] = { GZIP_MAGIC0, GZIP_MAGIC1, Z_DEFLATED, 0,
0, 0, 0, 0,
0, OS_CODE };
#endif
outbufp = malloc(BUFLEN);
inbufp = malloc(BUFLEN);
if (outbufp == NULL || inbufp == NULL) {
maybe_err("malloc failed");
goto out;
}
memset(&z, 0, sizeof z);
z.zalloc = Z_NULL;
z.zfree = Z_NULL;
z.opaque = 0;
#ifdef SMALL
memcpy(outbufp, header, sizeof header);
i = sizeof header;
#else
if (nflag != 0) {
mtime = 0;
origname = "";
}
i = snprintf(outbufp, BUFLEN, "%c%c%c%c%c%c%c%c%c%c%s",
GZIP_MAGIC0, GZIP_MAGIC1, Z_DEFLATED,
*origname ? ORIG_NAME : 0,
mtime & 0xff,
(mtime >> 8) & 0xff,
(mtime >> 16) & 0xff,
(mtime >> 24) & 0xff,
numflag == 1 ? 4 : numflag == 9 ? 2 : 0,
OS_CODE, origname);
if (i >= BUFLEN)
/* this need PATH_MAX > BUFLEN ... */
maybe_err("snprintf");
if (*origname)
i++;
#endif
z.next_out = (unsigned char *)outbufp + i;
z.avail_out = BUFLEN - i;
error = deflateInit2(&z, numflag, Z_DEFLATED,
(-MAX_WBITS), 8, Z_DEFAULT_STRATEGY);
if (error != Z_OK) {
maybe_warnx("deflateInit2 failed");
in_tot = -1;
goto out;
}
crc = crc32(0L, Z_NULL, 0);
for (;;) {
if (z.avail_out == 0) {
if (write(out, outbufp, BUFLEN) != BUFLEN) {
maybe_warn("write");
out_tot = -1;
goto out;
}
out_tot += BUFLEN;
z.next_out = (unsigned char *)outbufp;
z.avail_out = BUFLEN;
}
if (z.avail_in == 0) {
in_size = read(in, inbufp, BUFLEN);
if (in_size < 0) {
maybe_warn("read");
in_tot = -1;
goto out;
}
if (in_size == 0)
break;
crc = crc32(crc, (const Bytef *)inbufp, (unsigned)in_size);
in_tot += in_size;
z.next_in = (unsigned char *)inbufp;
z.avail_in = in_size;
}
error = deflate(&z, Z_NO_FLUSH);
if (error != Z_OK && error != Z_STREAM_END) {
maybe_warnx("deflate failed");
in_tot = -1;
goto out;
}
}
/* clean up */
for (;;) {
size_t len;
ssize_t w;
error = deflate(&z, Z_FINISH);
if (error != Z_OK && error != Z_STREAM_END) {
maybe_warnx("deflate failed");
in_tot = -1;
goto out;
}
len = (char *)z.next_out - outbufp;
w = write(out, outbufp, len);
if (w == -1 || (size_t)w != len) {
maybe_warn("write");
out_tot = -1;
goto out;
}
out_tot += len;
z.next_out = (unsigned char *)outbufp;
z.avail_out = BUFLEN;
if (error == Z_STREAM_END)
break;
}
if (deflateEnd(&z) != Z_OK) {
maybe_warnx("deflateEnd failed");
in_tot = -1;
goto out;
}
i = snprintf(outbufp, BUFLEN, "%c%c%c%c%c%c%c%c",
(int)crc & 0xff,
(int)(crc >> 8) & 0xff,
(int)(crc >> 16) & 0xff,
(int)(crc >> 24) & 0xff,
(int)in_tot & 0xff,
(int)(in_tot >> 8) & 0xff,
(int)(in_tot >> 16) & 0xff,
(int)(in_tot >> 24) & 0xff);
if (i != 8)
maybe_err("snprintf");
if (write(out, outbufp, i) != i) {
maybe_warn("write");
in_tot = -1;
} else
out_tot += i;
out:
if (inbufp != NULL)
free(inbufp);
if (outbufp != NULL)
free(outbufp);
if (gsizep)
*gsizep = out_tot;
return in_tot;
}
/*
* uncompress input to output then close the input. return the
* uncompressed size written, and put the compressed sized read
* into `*gsizep'.
*/
static off_t
gz_uncompress(int in, int out, char *pre, size_t prelen, off_t *gsizep,
const char *filename)
{
z_stream z;
char *outbufp, *inbufp;
off_t out_tot = -1, in_tot = 0;
uint32_t out_sub_tot = 0;
enum {
GZSTATE_MAGIC0,
GZSTATE_MAGIC1,
GZSTATE_METHOD,
GZSTATE_FLAGS,
GZSTATE_SKIPPING,
GZSTATE_EXTRA,
GZSTATE_EXTRA2,
GZSTATE_EXTRA3,
GZSTATE_ORIGNAME,
GZSTATE_COMMENT,
GZSTATE_HEAD_CRC1,
GZSTATE_HEAD_CRC2,
GZSTATE_INIT,
GZSTATE_READ,
GZSTATE_CRC,
GZSTATE_LEN,
} state = GZSTATE_MAGIC0;
int flags = 0, skip_count = 0;
int error = Z_STREAM_ERROR, done_reading = 0;
uLong crc = 0;
ssize_t wr;
int needmore = 0;
#define ADVANCE() { z.next_in++; z.avail_in--; }
if ((outbufp = malloc(BUFLEN)) == NULL) {
maybe_err("malloc failed");
goto out2;
}
if ((inbufp = malloc(BUFLEN)) == NULL) {
maybe_err("malloc failed");
goto out1;
}
memset(&z, 0, sizeof z);
z.avail_in = prelen;
z.next_in = (unsigned char *)pre;
z.avail_out = BUFLEN;
z.next_out = (unsigned char *)outbufp;
z.zalloc = NULL;
z.zfree = NULL;
z.opaque = 0;
in_tot = prelen;
out_tot = 0;
for (;;) {
if ((z.avail_in == 0 || needmore) && done_reading == 0) {
ssize_t in_size;
if (z.avail_in > 0) {
memmove(inbufp, z.next_in, z.avail_in);
}
z.next_in = (unsigned char *)inbufp;
in_size = read(in, z.next_in + z.avail_in,
BUFLEN - z.avail_in);
if (in_size == -1) {
maybe_warn("failed to read stdin");
goto stop_and_fail;
} else if (in_size == 0) {
done_reading = 1;
}
z.avail_in += in_size;
needmore = 0;
in_tot += in_size;
}
if (z.avail_in == 0) {
if (done_reading && state != GZSTATE_MAGIC0) {
maybe_warnx("%s: unexpected end of file",
filename);
goto stop_and_fail;
}
goto stop;
}
switch (state) {
case GZSTATE_MAGIC0:
if (*z.next_in != GZIP_MAGIC0) {
if (in_tot > 0) {
maybe_warnx("%s: trailing garbage "
"ignored", filename);
goto stop;
}
maybe_warnx("input not gziped (MAGIC0)");
goto stop_and_fail;
}
ADVANCE();
state++;
out_sub_tot = 0;
crc = crc32(0L, Z_NULL, 0);
break;
case GZSTATE_MAGIC1:
if (*z.next_in != GZIP_MAGIC1 &&
*z.next_in != GZIP_OMAGIC1) {
maybe_warnx("input not gziped (MAGIC1)");
goto stop_and_fail;
}
ADVANCE();
state++;
break;
case GZSTATE_METHOD:
if (*z.next_in != Z_DEFLATED) {
maybe_warnx("unknown compression method");
goto stop_and_fail;
}
ADVANCE();
state++;
break;
case GZSTATE_FLAGS:
flags = *z.next_in;
ADVANCE();
skip_count = 6;
state++;
break;
case GZSTATE_SKIPPING:
if (skip_count > 0) {
skip_count--;
ADVANCE();
} else
state++;
break;
case GZSTATE_EXTRA:
if ((flags & EXTRA_FIELD) == 0) {
state = GZSTATE_ORIGNAME;
break;
}
skip_count = *z.next_in;
ADVANCE();
state++;
break;
case GZSTATE_EXTRA2:
skip_count |= ((*z.next_in) << 8);
ADVANCE();
state++;
break;
case GZSTATE_EXTRA3:
if (skip_count > 0) {
skip_count--;
ADVANCE();
} else
state++;
break;
case GZSTATE_ORIGNAME:
if ((flags & ORIG_NAME) == 0) {
state++;
break;
}
if (*z.next_in == 0)
state++;
ADVANCE();
break;
case GZSTATE_COMMENT:
if ((flags & COMMENT) == 0) {
state++;
break;
}
if (*z.next_in == 0)
state++;
ADVANCE();
break;
case GZSTATE_HEAD_CRC1:
if (flags & HEAD_CRC)
skip_count = 2;
else
skip_count = 0;
state++;
break;
case GZSTATE_HEAD_CRC2:
if (skip_count > 0) {
skip_count--;
ADVANCE();
} else
state++;
break;
case GZSTATE_INIT:
if (inflateInit2(&z, -MAX_WBITS) != Z_OK) {
maybe_warnx("failed to inflateInit");
goto stop_and_fail;
}
state++;
break;
case GZSTATE_READ:
error = inflate(&z, Z_FINISH);
switch (error) {
/* Z_BUF_ERROR goes with Z_FINISH... */
case Z_BUF_ERROR:
case Z_STREAM_END:
case Z_OK:
break;
case Z_NEED_DICT:
maybe_warnx("Z_NEED_DICT error");
goto stop_and_fail;
case Z_DATA_ERROR:
maybe_warnx("data stream error");
goto stop_and_fail;
case Z_STREAM_ERROR:
maybe_warnx("internal stream error");
goto stop_and_fail;
case Z_MEM_ERROR:
maybe_warnx("memory allocation error");
goto stop_and_fail;
default:
maybe_warn("unknown error from inflate(): %d",
error);
}
wr = BUFLEN - z.avail_out;
if (wr != 0) {
crc = crc32(crc, (const Bytef *)outbufp, (unsigned)wr);
if (
#ifndef SMALL
/* don't write anything with -t */
tflag == 0 &&
#endif
write(out, outbufp, wr) != wr) {
maybe_warn("error writing to output");
goto stop_and_fail;
}
out_tot += wr;
out_sub_tot += wr;
}
if (error == Z_STREAM_END) {
inflateEnd(&z);
state++;
}
z.next_out = (unsigned char *)outbufp;
z.avail_out = BUFLEN;
break;
case GZSTATE_CRC:
{
uLong origcrc;
if (z.avail_in < 4) {
if (!done_reading) {
needmore = 1;
continue;
}
maybe_warnx("truncated input");
goto stop_and_fail;
}
origcrc = ((unsigned)z.next_in[0] & 0xff) |
((unsigned)z.next_in[1] & 0xff) << 8 |
((unsigned)z.next_in[2] & 0xff) << 16 |
((unsigned)z.next_in[3] & 0xff) << 24;
if (origcrc != crc) {
maybe_warnx("invalid compressed"
" data--crc error");
goto stop_and_fail;
}
}
z.avail_in -= 4;
z.next_in += 4;
if (!z.avail_in && done_reading) {
goto stop;
}
state++;
break;
case GZSTATE_LEN:
{
uLong origlen;
if (z.avail_in < 4) {
if (!done_reading) {
needmore = 1;
continue;
}
maybe_warnx("truncated input");
goto stop_and_fail;
}
origlen = ((unsigned)z.next_in[0] & 0xff) |
((unsigned)z.next_in[1] & 0xff) << 8 |
((unsigned)z.next_in[2] & 0xff) << 16 |
((unsigned)z.next_in[3] & 0xff) << 24;
if (origlen != out_sub_tot) {
maybe_warnx("invalid compressed"
" data--length error");
goto stop_and_fail;
}
}
z.avail_in -= 4;
z.next_in += 4;
if (error < 0) {
maybe_warnx("decompression error");
goto stop_and_fail;
}
state = GZSTATE_MAGIC0;
break;
}
continue;
stop_and_fail:
out_tot = -1;
stop:
break;
}
if (state > GZSTATE_INIT)
inflateEnd(&z);
free(inbufp);
out1:
free(outbufp);
out2:
if (gsizep)
*gsizep = in_tot;
return (out_tot);
}
#ifndef SMALL
/*
* set the owner, mode, flags & utimes using the given file descriptor.
* file is only used in possible warning messages.
*/
static void
copymodes(int fd, const struct stat *sbp, const char *file)
{
struct timeval times[2];
struct stat sb;
/*
* If we have no info on the input, give this file some
* default values and return..
*/
if (sbp == NULL) {
mode_t mask = umask(022);
(void)fchmod(fd, DEFFILEMODE & ~mask);
(void)umask(mask);
return;
}
sb = *sbp;
/* if the chown fails, remove set-id bits as-per compress(1) */
if (fchown(fd, sb.st_uid, sb.st_gid) < 0) {
if (errno != EPERM)
maybe_warn("couldn't fchown: %s", file);
sb.st_mode &= ~(S_ISUID|S_ISGID);
}
/* we only allow set-id and the 9 normal permission bits */
sb.st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
if (fchmod(fd, sb.st_mode) < 0)
maybe_warn("couldn't fchmod: %s", file);
/* only try flags if they exist already */
if (sb.st_flags != 0 && fchflags(fd, sb.st_flags) < 0)
maybe_warn("couldn't fchflags: %s", file);
TIMESPEC_TO_TIMEVAL(×[0], &sb.st_atimespec);
TIMESPEC_TO_TIMEVAL(×[1], &sb.st_mtimespec);
if (futimes(fd, times) < 0)
maybe_warn("couldn't utimes: %s", file);
}
#endif
/* what sort of file is this? */
static enum filetype
file_gettype(u_char *buf)
{
if (buf[0] == GZIP_MAGIC0 &&
(buf[1] == GZIP_MAGIC1 || buf[1] == GZIP_OMAGIC1))
return FT_GZIP;
else
#ifndef NO_BZIP2_SUPPORT
if (memcmp(buf, BZIP2_MAGIC, 3) == 0 &&
buf[3] >= '0' && buf[3] <= '9')
return FT_BZIP2;
else
#endif
#ifndef NO_COMPRESS_SUPPORT
if (memcmp(buf, Z_MAGIC, 2) == 0)
return FT_Z;
else
#endif
return FT_UNKNOWN;
}
#ifndef SMALL
/* check the outfile is OK. */
static int
check_outfile(const char *outfile)
{
struct stat sb;
int ok = 1;
if (lflag == 0 && stat(outfile, &sb) == 0) {
if (fflag)
unlink(outfile);
else if (isatty(STDIN_FILENO)) {
char ans[10] = { 'n', '\0' }; /* default */
fprintf(stderr, "%s already exists -- do you wish to "
"overwrite (y or n)? " , outfile);
(void)fgets(ans, sizeof(ans) - 1, stdin);
if (ans[0] != 'y' && ans[0] != 'Y') {
fprintf(stderr, "\tnot overwriting\n");
ok = 0;
} else
unlink(outfile);
} else {
maybe_warnx("%s already exists -- skipping", outfile);
ok = 0;
}
}
return ok;
}
static void
unlink_input(const char *file, const struct stat *sb)
{
struct stat nsb;
if (kflag)
return;
if (stat(file, &nsb) != 0)
/* Must be gone alrady */
return;
if (nsb.st_dev != sb->st_dev || nsb.st_ino != sb->st_ino)
/* Definitely a different file */
return;
unlink(file);
}
#endif
static const suffixes_t *
check_suffix(char *file, int xlate)
{
const suffixes_t *s;
int len = strlen(file);
char *sp;
for (s = suffixes; s != suffixes + NUM_SUFFIXES; s++) {
/* if it doesn't fit in "a.suf", don't bother */
if (s->ziplen >= len)
continue;
sp = file + len - s->ziplen;
if (strcmp(s->zipped, sp) != 0)
continue;
if (xlate)
strcpy(sp, s->normal);
return s;
}
return NULL;
}
/*
* compress the given file: create a corresponding .gz file and remove the
* original.
*/
static off_t
file_compress(char *file, char *outfile, size_t outsize)
{
int in;
int out;
off_t size, insize;
#ifndef SMALL
struct stat isb, osb;
const suffixes_t *suff;
#endif
in = open(file, O_RDONLY);
if (in == -1) {
maybe_warn("can't open %s", file);
return -1;
}
if (cflag == 0) {
#ifndef SMALL
if (fstat(in, &isb) == 0) {
if (isb.st_nlink > 1 && fflag == 0) {
maybe_warnx("%s has %d other link%s -- "
"skipping", file, isb.st_nlink - 1,
isb.st_nlink == 1 ? "" : "s");
close(in);
return -1;
}
}
if (fflag == 0 && (suff = check_suffix(file, 0))
&& suff->zipped[0] != 0) {
maybe_warnx("%s already has %s suffix -- unchanged",
file, suff->zipped);
close(in);
return -1;
}
#endif
/* Add (usually) .gz to filename */
if ((size_t)snprintf(outfile, outsize, "%s%s",
file, suffixes[0].zipped) >= outsize)
memcpy(outfile - suffixes[0].ziplen - 1,
suffixes[0].zipped, suffixes[0].ziplen + 1);
#ifndef SMALL
if (check_outfile(outfile) == 0) {
close(in);
return -1;
}
#endif
}
if (cflag == 0) {
out = open(outfile, O_WRONLY | O_CREAT | O_EXCL, 0600);
if (out == -1) {
maybe_warn("could not create output: %s", outfile);
fclose(stdin);
return -1;
}
} else
out = STDOUT_FILENO;
insize = gz_compress(in, out, &size, basename(file), (uint32_t)isb.st_mtime);
(void)close(in);
/*
* If there was an error, insize will be -1.
* If we compressed to stdout, just return the size.
* Otherwise stat the file and check it is the correct size.
* We only blow away the file if we can stat the output and it
* has the expected size.
*/
if (cflag != 0)
return insize == -1 ? -1 : size;
#ifndef SMALL
if (fstat(out, &osb) != 0) {
maybe_warn("couldn't stat: %s", outfile);
goto bad_outfile;
}
if (osb.st_size != size) {
maybe_warnx("output file: %s wrong size (%" PRIdOFF
" != %" PRIdOFF "), deleting",
outfile, osb.st_size, size);
goto bad_outfile;
}
copymodes(out, &isb, outfile);
#endif
if (close(out) == -1)
maybe_warn("couldn't close output");
/* output is good, ok to delete input */
unlink_input(file, &isb);
return size;
#ifndef SMALL
bad_outfile:
if (close(out) == -1)
maybe_warn("couldn't close output");
maybe_warnx("leaving original %s", file);
unlink(outfile);
return size;
#endif
}
/* uncompress the given file and remove the original */
static off_t
file_uncompress(char *file, char *outfile, size_t outsize)
{
struct stat isb, osb;
off_t size;
ssize_t rbytes;
unsigned char header1[4];
enum filetype method;
int rv, fd, ofd, zfd = -1;
#ifndef SMALL
time_t timestamp = 0;
unsigned char name[PATH_MAX + 1];
#endif
/* gather the old name info */
fd = open(file, O_RDONLY);
if (fd < 0) {
maybe_warn("can't open %s", file);
goto lose;
}
strlcpy(outfile, file, outsize);
if (check_suffix(outfile, 1) == NULL && !(cflag || lflag)) {
maybe_warnx("%s: unknown suffix -- ignored", file);
goto lose;
}
rbytes = read(fd, header1, sizeof header1);
if (rbytes != sizeof header1) {
/* we don't want to fail here. */
#ifndef SMALL
if (fflag)
goto lose;
#endif
if (rbytes == -1)
maybe_warn("can't read %s", file);
else
goto unexpected_EOF;
goto lose;
}
method = file_gettype(header1);
#ifndef SMALL
if (fflag == 0 && method == FT_UNKNOWN) {
maybe_warnx("%s: not in gzip format", file);
goto lose;
}
#endif
#ifndef SMALL
if (method == FT_GZIP && Nflag) {
unsigned char ts[4]; /* timestamp */
rv = pread(fd, ts, sizeof ts, GZIP_TIMESTAMP);
if (rv >= 0 && (size_t)rv < sizeof ts)
goto unexpected_EOF;
if (rv == -1) {
if (!fflag)
maybe_warn("can't read %s", file);
goto lose;
}
timestamp = ts[3] << 24 | ts[2] << 16 | ts[1] << 8 | ts[0];
if (header1[3] & ORIG_NAME) {
rbytes = pread(fd, name, sizeof name, GZIP_ORIGNAME);
if (rbytes < 0) {
maybe_warn("can't read %s", file);
goto lose;
}
if (name[0] != 0) {
/* preserve original directory name */
char *dp = strrchr(file, '/');
if (dp == NULL)
dp = file;
else
dp++;
snprintf(outfile, outsize, "%.*s%.*s",
(int) (dp - file),
file, (int) rbytes, name);
}
}
}
#endif
lseek(fd, 0, SEEK_SET);
if (cflag == 0 || lflag) {
if (fstat(fd, &isb) != 0)
goto lose;
#ifndef SMALL
if (isb.st_nlink > 1 && lflag == 0 && fflag == 0) {
maybe_warnx("%s has %d other links -- skipping",
file, isb.st_nlink - 1);
goto lose;
}
if (nflag == 0 && timestamp)
isb.st_mtime = timestamp;
if (check_outfile(outfile) == 0)
goto lose;
#endif
}
if (cflag == 0 && lflag == 0) {
zfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0600);
if (zfd == STDOUT_FILENO) {
/* We won't close STDOUT_FILENO later... */
zfd = dup(zfd);
close(STDOUT_FILENO);
}
if (zfd == -1) {
maybe_warn("can't open %s", outfile);
goto lose;
}
} else
zfd = STDOUT_FILENO;
#ifndef NO_BZIP2_SUPPORT
if (method == FT_BZIP2) {
/* XXX */
if (lflag) {
maybe_warnx("no -l with bzip2 files");
goto lose;
}
size = unbzip2(fd, zfd, NULL, 0, NULL);
} else
#endif
#ifndef NO_COMPRESS_SUPPORT
if (method == FT_Z) {
FILE *in, *out;
/* XXX */
if (lflag) {
maybe_warnx("no -l with Lempel-Ziv files");
goto lose;
}
if ((in = zdopen(fd)) == NULL) {
maybe_warn("zdopen for read: %s", file);
goto lose;
}
out = fdopen(dup(zfd), "w");
if (out == NULL) {
maybe_warn("fdopen for write: %s", outfile);
fclose(in);
goto lose;
}
size = zuncompress(in, out, NULL, 0, NULL);
/* need to fclose() if ferror() is true... */
if (ferror(in) | fclose(in)) {
maybe_warn("failed infile fclose");
unlink(outfile);
(void)fclose(out);
}
if (fclose(out) != 0) {
maybe_warn("failed outfile fclose");
unlink(outfile);
goto lose;
}
} else
#endif
#ifndef SMALL
if (method == FT_UNKNOWN) {
if (lflag) {
maybe_warnx("no -l for unknown filetypes");
goto lose;
}
size = cat_fd(NULL, 0, NULL, fd);
} else
#endif
{
if (lflag) {
print_list(fd, isb.st_size, outfile, isb.st_mtime);
close(fd);
return -1; /* XXX */
}
size = gz_uncompress(fd, zfd, NULL, 0, NULL, file);
}
if (close(fd) != 0)
maybe_warn("couldn't close input");
if (zfd != STDOUT_FILENO && close(zfd) != 0)
maybe_warn("couldn't close output");
if (size == -1) {
if (cflag == 0)
unlink(outfile);
maybe_warnx("%s: uncompress failed", file);
return -1;
}
/* if testing, or we uncompressed to stdout, this is all we need */
#ifndef SMALL
if (tflag)
return size;
#endif
/* if we are uncompressing to stdin, don't remove the file. */
if (cflag)
return size;
/*
* if we create a file...
*/
/*
* if we can't stat the file don't remove the file.
*/
ofd = open(outfile, O_RDWR, 0);
if (ofd == -1) {
maybe_warn("couldn't open (leaving original): %s",
outfile);
return -1;
}
if (fstat(ofd, &osb) != 0) {
maybe_warn("couldn't stat (leaving original): %s",
outfile);
close(ofd);
return -1;
}
if (osb.st_size != size) {
maybe_warnx("stat gave different size: %" PRIdOFF
" != %" PRIdOFF " (leaving original)",
size, osb.st_size);
close(ofd);
unlink(outfile);
return -1;
}
unlink_input(file, &isb);
#ifndef SMALL
copymodes(ofd, &isb, outfile);
#endif
close(ofd);
return size;
unexpected_EOF:
maybe_warnx("%s: unexpected end of file", file);
lose:
if (fd != -1)
close(fd);
if (zfd != -1 && zfd != STDOUT_FILENO)
close(fd);
return -1;
}
#ifndef SMALL
static off_t
cat_fd(unsigned char * prepend, size_t count, off_t *gsizep, int fd)
{
char buf[BUFLEN];
off_t in_tot;
ssize_t w;
in_tot = count;
w = write(STDOUT_FILENO, prepend, count);
if (w == -1 || (size_t)w != count) {
maybe_warn("write to stdout");
return -1;
}
for (;;) {
ssize_t rv;
rv = read(fd, buf, sizeof buf);
if (rv == 0)
break;
if (rv < 0) {
maybe_warn("read from fd %d", fd);
break;
}
if (write(STDOUT_FILENO, buf, rv) != rv) {
maybe_warn("write to stdout");
break;
}
in_tot += rv;
}
if (gsizep)
*gsizep = in_tot;
return (in_tot);
}
#endif
static void
handle_stdin(void)
{
unsigned char header1[4];
off_t usize, gsize;
enum filetype method;
ssize_t bytes_read;
#ifndef NO_COMPRESS_SUPPORT
FILE *in;
#endif
#ifndef SMALL
if (fflag == 0 && lflag == 0 && isatty(STDIN_FILENO)) {
maybe_warnx("standard input is a terminal -- ignoring");
return;
}
#endif
if (lflag) {
struct stat isb;
/* XXX could read the whole file, etc. */
if (fstat(STDIN_FILENO, &isb) < 0) {
maybe_warn("fstat");
return;
}
print_list(STDIN_FILENO, isb.st_size, "stdout", isb.st_mtime);
return;
}
bytes_read = read_retry(STDIN_FILENO, header1, sizeof header1);
if (bytes_read == -1) {
maybe_warn("can't read stdin");
return;
} else if (bytes_read != sizeof(header1)) {
maybe_warnx("(stdin): unexpected end of file");
return;
}
method = file_gettype(header1);
switch (method) {
default:
#ifndef SMALL
if (fflag == 0) {
maybe_warnx("unknown compression format");
return;
}
usize = cat_fd(header1, sizeof header1, &gsize, STDIN_FILENO);
break;
#endif
case FT_GZIP:
usize = gz_uncompress(STDIN_FILENO, STDOUT_FILENO,
(char *)header1, sizeof header1, &gsize, "(stdin)");
break;
#ifndef NO_BZIP2_SUPPORT
case FT_BZIP2:
usize = unbzip2(STDIN_FILENO, STDOUT_FILENO,
(char *)header1, sizeof header1, &gsize);
break;
#endif
#ifndef NO_COMPRESS_SUPPORT
case FT_Z:
if ((in = zdopen(STDIN_FILENO)) == NULL) {
maybe_warnx("zopen of stdin");
return;
}
usize = zuncompress(in, stdout, (char *)header1, sizeof header1, &gsize);
fclose(in);
break;
#endif
}
#ifndef SMALL
if (vflag && !tflag && usize != -1 && gsize != -1)
print_verbage(NULL, NULL, usize, gsize);
if (vflag && tflag)
print_test("(stdin)", usize != -1);
#endif
}
static void
handle_stdout(void)
{
off_t gsize, usize;
struct stat sb;
time_t systime;
uint32_t mtime;
int ret;
#ifndef SMALL
if (fflag == 0 && isatty(STDOUT_FILENO)) {
maybe_warnx("standard output is a terminal -- ignoring");
return;
}
#endif
/* If stdin is a file use it's mtime, otherwise use current time */
ret = fstat(STDIN_FILENO, &sb);
#ifndef SMALL
if (ret < 0) {
maybe_warn("Can't stat stdin");
return;
}
#endif
if (S_ISREG(sb.st_mode))
mtime = (uint32_t)sb.st_mtime;
else {
systime = time(NULL);
#ifndef SMALL
if (systime == -1) {
maybe_warn("time");
return;
}
#endif
mtime = (uint32_t)systime;
}
usize = gz_compress(STDIN_FILENO, STDOUT_FILENO, &gsize, "", mtime);
#ifndef SMALL
if (vflag && !tflag && usize != -1 && gsize != -1)
print_verbage(NULL, NULL, usize, gsize);
#endif
}
/* do what is asked for, for the path name */
static void
handle_pathname(char *path)
{
char *opath = path, *s = NULL;
ssize_t len;
int slen;
struct stat sb;
/* check for stdout/stdin */
if (path[0] == '-' && path[1] == '\0') {
if (dflag)
handle_stdin();
else
handle_stdout();
return;
}
retry:
if (stat(path, &sb) != 0) {
/* lets try <path>.gz if we're decompressing */
if (dflag && s == NULL && errno == ENOENT) {
len = strlen(path);
slen = suffixes[0].ziplen;
s = malloc(len + slen + 1);
if (s == NULL)
maybe_err("malloc");
memcpy(s, path, len);
memcpy(s + len, suffixes[0].zipped, slen + 1);
path = s;
goto retry;
}
maybe_warn("can't stat: %s", opath);
goto out;
}
if (S_ISDIR(sb.st_mode)) {
#ifndef SMALL
if (rflag)
handle_dir(path);
else
#endif
maybe_warnx("%s is a directory", path);
goto out;
}
if (S_ISREG(sb.st_mode))
handle_file(path, &sb);
else
maybe_warnx("%s is not a regular file", path);
out:
if (s)
free(s);
}
/* compress/decompress a file */
static void
handle_file(char *file, struct stat *sbp)
{
off_t usize, gsize;
char outfile[PATH_MAX];
infile = file;
if (dflag) {
usize = file_uncompress(file, outfile, sizeof(outfile));
#ifndef SMALL
if (vflag && tflag)
print_test(file, usize != -1);
#endif
if (usize == -1)
return;
gsize = sbp->st_size;
} else {
gsize = file_compress(file, outfile, sizeof(outfile));
if (gsize == -1)
return;
usize = sbp->st_size;
}
#ifndef SMALL
if (vflag && !tflag)
print_verbage(file, (cflag) ? NULL : outfile, usize, gsize);
#endif
}
#ifndef SMALL
/* this is used with -r to recursively descend directories */
static void
handle_dir(char *dir)
{
char *path_argv[2];
FTS *fts;
FTSENT *entry;
path_argv[0] = dir;
path_argv[1] = 0;
fts = fts_open(path_argv, FTS_PHYSICAL | FTS_NOCHDIR, NULL);
if (fts == NULL) {
warn("couldn't fts_open %s", dir);
return;
}
while ((entry = fts_read(fts))) {
switch(entry->fts_info) {
case FTS_D:
case FTS_DP:
continue;
case FTS_DNR:
case FTS_ERR:
case FTS_NS:
maybe_warn("%s", entry->fts_path);
continue;
case FTS_F:
handle_file(entry->fts_path, entry->fts_statp);
}
}
(void)fts_close(fts);
}
#endif
/* print a ratio - size reduction as a fraction of uncompressed size */
static void
print_ratio(off_t in, off_t out, FILE *where)
{
int percent10; /* 10 * percent */
off_t diff;
char buff[8];
int len;
diff = in - out/2;
if (diff <= 0)
/*
* Output is more than double size of input! print -99.9%
* Quite possibly we've failed to get the original size.
*/
percent10 = -999;
else {
/*
* We only need 12 bits of result from the final division,
* so reduce the values until a 32bit division will suffice.
*/
while (in > 0x100000) {
diff >>= 1;
in >>= 1;
}
if (in != 0)
percent10 = ((u_int)diff * 2000) / (u_int)in - 1000;
else
percent10 = 0;
}
len = snprintf(buff, sizeof buff, "%2.2d.", percent10);
/* Move the '.' to before the last digit */
buff[len - 1] = buff[len - 2];
buff[len - 2] = '.';
fprintf(where, "%5s%%", buff);
}
#ifndef SMALL
/* print compression statistics, and the new name (if there is one!) */
static void
print_verbage(const char *file, const char *nfile, off_t usize, off_t gsize)
{
if (file)
fprintf(stderr, "%s:%s ", file,
strlen(file) < 7 ? "\t\t" : "\t");
print_ratio(usize, gsize, stderr);
if (nfile)
fprintf(stderr, " -- replaced with %s", nfile);
fprintf(stderr, "\n");
fflush(stderr);
}
/* print test results */
static void
print_test(const char *file, int ok)
{
if (exit_value == 0 && ok == 0)
exit_value = 1;
fprintf(stderr, "%s:%s %s\n", file,
strlen(file) < 7 ? "\t\t" : "\t", ok ? "OK" : "NOT OK");
fflush(stderr);
}
#endif
/* print a file's info ala --list */
/* eg:
compressed uncompressed ratio uncompressed_name
354841 1679360 78.8% /usr/pkgsrc/distfiles/libglade-2.0.1.tar
*/
static void
print_list(int fd, off_t out, const char *outfile, time_t ts)
{
static int first = 1;
#ifndef SMALL
static off_t in_tot, out_tot;
uint32_t crc = 0;
#endif
off_t in = 0, rv;
if (first) {
#ifndef SMALL
if (vflag)
printf("method crc date time ");
#endif
if (qflag == 0)
printf(" compressed uncompressed "
"ratio uncompressed_name\n");
}
first = 0;
/* print totals? */
#ifndef SMALL
if (fd == -1) {
in = in_tot;
out = out_tot;
} else
#endif
{
/* read the last 4 bytes - this is the uncompressed size */
rv = lseek(fd, (off_t)(-8), SEEK_END);
if (rv != -1) {
unsigned char buf[8];
uint32_t usize;
rv = read(fd, (char *)buf, sizeof(buf));
if (rv == -1)
maybe_warn("read of uncompressed size");
else if (rv != sizeof(buf))
maybe_warnx("read of uncompressed size");
else {
usize = buf[4] | buf[5] << 8 |
buf[6] << 16 | buf[7] << 24;
in = (off_t)usize;
#ifndef SMALL
crc = buf[0] | buf[1] << 8 |
buf[2] << 16 | buf[3] << 24;
#endif
}
}
}
#ifndef SMALL
if (vflag && fd == -1)
printf(" ");
else if (vflag) {
char *date = ctime(&ts);
/* skip the day, 1/100th second, and year */
date += 4;
date[12] = 0;
printf("%5s %08x %11s ", "defla"/*XXX*/, crc, date);
}
in_tot += in;
out_tot += out;
#endif
printf("%12llu %12llu ", (unsigned long long)out, (unsigned long long)in);
print_ratio(in, out, stdout);
printf(" %s\n", outfile);
}
/* display the usage of NetBSD gzip */
static void
usage(void)
{
fprintf(stderr, "%s\n", gzip_version);
fprintf(stderr,
#ifdef SMALL
"usage: %s [-" OPT_LIST "] [<file> [<file> ...]]\n",
#else
"usage: %s [-123456789acdfhklLNnqrtVv] [-S .suffix] [<file> [<file> ...]]\n"
" -1 --fast fastest (worst) compression\n"
" -2 .. -8 set compression level\n"
" -9 --best best (slowest) compression\n"
" -c --stdout write to stdout, keep original files\n"
" --to-stdout\n"
" -d --decompress uncompress files\n"
" --uncompress\n"
" -f --force force overwriting & compress links\n"
" -h --help display this help\n"
" -k --keep don't delete input files during operation\n"
" -l --list list compressed file contents\n"
" -N --name save or restore original file name and time stamp\n"
" -n --no-name don't save original file name or time stamp\n"
" -q --quiet output no warnings\n"
" -r --recursive recursively compress files in directories\n"
" -S .suf use suffix .suf instead of .gz\n"
" --suffix .suf\n"
" -t --test test compressed file\n"
" -V --version display program version\n"
" -v --verbose print extra statistics\n",
#endif
getprogname());
exit(0);
}
#ifndef SMALL
/* display the license information of FreeBSD gzip */
static void
display_license(void)
{
fprintf(stderr, "%s (based on NetBSD gzip 20060927)\n", gzip_version);
fprintf(stderr, "%s\n", gzip_copyright);
exit(0);
}
#endif
/* display the version of NetBSD gzip */
static void
display_version(void)
{
fprintf(stderr, "%s\n", gzip_version);
exit(0);
}
#ifndef NO_BZIP2_SUPPORT
#include "unbzip2.c"
#endif
#ifndef NO_COMPRESS_SUPPORT
#include "zuncompress.c"
#endif
static ssize_t
read_retry(int fd, void *buf, size_t sz)
{
char *cp = buf;
size_t left = MIN(sz, (size_t) SSIZE_MAX);
while (left > 0) {
ssize_t ret;
ret = read(fd, cp, left);
if (ret == -1) {
return ret;
} else if (ret == 0) {
break; /* EOF */
}
cp += ret;
left -= ret;
}
return sz - left;
}
|