summaryrefslogtreecommitdiff
path: root/tools/cpio/src/cpio.c
blob: e5090e4c9395a1fd6c5d20cf5d5dee5ee141d7a9 (plain)
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
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
/*
 * cpio - copy file archives in and out
 *
 * Gunnar Ritter, Freiburg i. Br., Germany, April 2003.
 */
/*
 * Copyright (c) 2003 Gunnar Ritter
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any damages
 * arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute
 * it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
 *    appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source distribution.
 */

/*
 * Sccsid @(#)cpio.c	1.304 (gritter) 2/14/09
 */

#include <sys/types.h>
#include <sys/stat.h>
#ifdef	__linux__
#if !defined (__UCLIBC__) && !defined (__dietlibc__)
#include <linux/fs.h>
#endif	/* !__UCLIBC__, !__dietlibc__ */
#include <linux/fd.h>
#undef	WNOHANG
#undef	WUNTRACED
#undef	P_ALL
#undef	P_PID
#undef	P_PGID
#ifdef	__dietlibc__
#undef	NR_OPEN
#undef	PATH_MAX
#endif	/* __dietlibc__ */
#endif	/* __linux__ */
#include <sys/wait.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include "sigset.h"
#include <time.h>
#include <utime.h>
#include <pwd.h>
#include <grp.h>
#include <limits.h>
#include <stdio.h>
#include <libgen.h>
#include <errno.h>
#include <inttypes.h>
#include <stdarg.h>
#include <locale.h>
#include <ctype.h>
#include "memalign.h"

int	sysv3;

#if USE_ZLIB
#include <zlib.h>
#endif	/* USE_ZLIB */

#if USE_BZLIB
#include <bzlib.h>
#endif	/* USE_BZLIB */

#include <sys/ioctl.h>

#if defined (__linux__) || defined (__sun) || defined (__FreeBSD__) || \
	defined (__hpux) || defined (_AIX) || defined (__NetBSD__) || \
	defined (__OpenBSD__) || defined (__DragonFly__) || \
	defined (__CYGWIN__)
#include <sys/mtio.h>
#endif

#include <iblok.h>
#include <sfile.h>
#include <atoll.h>

#ifdef	_AIX
#include <sys/sysmacros.h>
#endif	/* _AIX */

#ifndef	major
#include <sys/mkdev.h>
#endif	/* !major */

#include "cpio.h"
#include "blast.h"

#ifdef	__GLIBC__
#ifdef	_IO_putc_unlocked
#undef	putc
#define	putc(c, f)	_IO_putc_unlocked(c, f)
#undef	putchar
#define	putchar(c)	_IO_putc_unlocked(c, stdout)
#endif	/* _IO_putc_unlocked */
#endif	/* __GLIBC__ */

/*
 * The cpio code assumes that all following S_IFMT bits are the same as
 * those of the mode fields in cpio headers. The only real Unix system
 * known to deviate from this de facto standard is UNICOS which uses
 * 0130000 for S_IFLNK. But this software does not run on UNICOS for
 * a variety of other reasons anyway, so this should not be of much
 * concern.
 */
#if	S_IFIFO	!= 0010000 || \
	S_IFCHR	!= 0020000 || \
	S_IFDIR	!= 0040000 || \
	S_IFBLK	!= 0060000 || \
	S_IFREG	!= 0100000 || \
	S_IFLNK	!= 0120000 || \
	S_IFSOCK!= 0140000 || \
	S_IFMT	!= 0170000
#error non-standard S_IFMT bits
#endif

/*
 * File types that are not present on all systems but that we want to
 * recognize nevertheless.
 */
#ifndef	S_IFDOOR
#define	S_IFDOOR	0150000		/* Solaris door */
#endif
#ifndef	S_IFNAM
#define	S_IFNAM		0050000		/* XENIX special named file */
#endif
#ifndef	S_INSEM
#define	S_INSEM		0x1		/* XENIX semaphore subtype of IFNAM */
#endif
#ifndef	S_INSHD
#define	S_INSHD		0x2		/* XENIX shared data subtype of IFNAM */
#endif
#ifndef	S_IFNWK
#define	S_IFNWK		0110000		/* HP-UX network special file */
#endif

#if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || \
	defined (__DragonFly__) || defined (__APPLE__)
/*
 * For whatever reason, FreeBSD casts the return values of major() and
 * minor() to signed values so that normal limit comparisons will fail.
 */
static unsigned long
mymajor(long dev)
{
	return major(dev) & 0xFFFFFFFFUL;
}
#undef	major
#define	major(a)	mymajor(a)
static unsigned long
myminor(long dev)
{
	return minor(dev) & 0xFFFFFFFFUL;
}
#undef	minor
#define	minor(a)	myminor(a)
#endif	/* __FreeBSD__, __NetBSD__, __OpenBSD__, __DragonFly__, __APPLE__ */

/*
 * Device and inode counts in cpio formats are too small to store the
 * information used to detect hard links on today's systems. Keep track
 * of all devices and inodes and store fake counts in the archive.
 */
struct ilink {
	struct ilink	*l_nxt;	/* next link to same i-node */
	char		*l_nam;	/* link name */
	size_t		l_siz;	/* link name size */
};

struct islot {
	struct islot	*i_lln;	/* left link */
	struct islot 	*i_rln;	/* right link */
	struct ilink	*i_lnk;	/* links list */
	struct stat	*i_st;	/* stat information */
	char		*i_name;/* name of first link encountered */
	ino_t		i_ino;	/* real inode number */
	uint32_t	i_fino;	/* fake inode number */
	nlink_t		i_nlk;	/* number of remaining links */
};

struct dslot {
	struct dslot	*d_nxt;	/* next device */
	struct islot	*d_isl;	/* inode slots */
	uint32_t	d_cnt;	/* used inode number count */
	uint32_t	d_fake;	/* faked device id */
	dev_t		d_dev;	/* real device id */
};

union types2 {
	uint8_t		byte[2];
	uint16_t	sword;
};

union types4 {
	uint8_t		byte[4];
	uint16_t	sword[2];
	uint32_t	lword;
};

/*
 * Store and retrieve integers in a defined endian order.
 */
static uint16_t
ple16(const char *cp)
{
	return (uint16_t)(cp[0]&0377) +
		((uint16_t)(cp[1]&0377) << 8);
}

static uint16_t
pbe16(const char *cp)
{
	return (uint16_t)(cp[1]&0377) +
		((uint16_t)(cp[0]&0377) << 8);
}

static uint32_t
ple32(const char *cp)
{
	return (uint32_t)(cp[0]&0377) +
		((uint32_t)(cp[1]&0377) << 8) +
		((uint32_t)(cp[2]&0377) << 16) +
		((uint32_t)(cp[3]&0377) << 24);
}

static uint32_t
pbe32(const char *cp)
{
	return (uint32_t)(cp[3]&0377) +
		((uint32_t)(cp[2]&0377) << 8) +
		((uint32_t)(cp[1]&0377) << 16) +
		((uint32_t)(cp[0]&0377) << 24);
}

static uint32_t
pme32(const char *cp)
{
	return (uint32_t)(cp[2]&0377) +
		((uint32_t)(cp[3]&0377) << 8) +
		((uint32_t)(cp[0]&0377) << 16) +
		((uint32_t)(cp[1]&0377) << 24);
}

static uint64_t
ple64(const char *cp)
{
	return (uint64_t)(cp[0]&0377) +
		((uint64_t)(cp[1]&0377) << 8) +
		((uint64_t)(cp[2]&0377) << 16) +
		((uint64_t)(cp[3]&0377) << 24) +
		((uint64_t)(cp[4]&0377) << 32) +
		((uint64_t)(cp[5]&0377) << 40) +
		((uint64_t)(cp[6]&0377) << 48) +
		((uint64_t)(cp[7]&0377) << 56);
}

static uint64_t
pbe64(const char *cp)
{
	return (uint64_t)(cp[7]&0377) +
		((uint64_t)(cp[6]&0377) << 8) +
		((uint64_t)(cp[5]&0377) << 16) +
		((uint64_t)(cp[4]&0377) << 24) +
		((uint64_t)(cp[3]&0377) << 32) +
		((uint64_t)(cp[2]&0377) << 40) +
		((uint64_t)(cp[1]&0377) << 48) +
		((uint64_t)(cp[0]&0377) << 56);
}

static void
le16p(uint16_t n, char *cp)
{
	cp[0] = (n&0x00ff);
	cp[1] = (n&0xff00) >> 8;
}

static void
be16p(uint16_t n, char *cp)
{
	cp[1] = (n&0x00ff);
	cp[0] = (n&0xff00) >> 8;
}

static void
le32p(uint32_t n, char *cp)
{
	cp[0] = (n&0x000000ff);
	cp[1] = (n&0x0000ff00) >> 8;
	cp[2] = (n&0x00ff0000) >> 16;
	cp[3] = (n&0xff000000) >> 24;
}

static void
be32p(uint32_t n, char *cp)
{
	cp[3] = (n&0x000000ff);
	cp[2] = (n&0x0000ff00) >> 8;
	cp[1] = (n&0x00ff0000) >> 16;
	cp[0] = (n&0xff000000) >> 24;
}

static void
me32p(uint32_t n, char *cp)
{
	cp[2] = (n&0x000000ff);
	cp[3] = (n&0x0000ff00) >> 8;
	cp[0] = (n&0x00ff0000) >> 16;
	cp[1] = (n&0xff000000) >> 24;
}

static void
le64p(uint64_t n, char *cp)
{
	cp[0] = (n&0x00000000000000ffLL);
	cp[1] = (n&0x000000000000ff00LL) >> 8;
	cp[2] = (n&0x0000000000ff0000LL) >> 16;
	cp[3] = (n&0x00000000ff000000LL) >> 24;
	cp[4] = (n&0x000000ff00000000LL) >> 32;
	cp[5] = (n&0x0000ff0000000000LL) >> 40;
	cp[6] = (n&0x00ff000000000000LL) >> 48;
	cp[7] = (n&0xff00000000000000LL) >> 56;
}

static void
be64p(uint64_t n, char *cp)
{
	cp[7] = (n&0x00000000000000ffLL);
	cp[6] = (n&0x000000000000ff00LL) >> 8;
	cp[5] = (n&0x0000000000ff0000LL) >> 16;
	cp[4] = (n&0x00000000ff000000LL) >> 24;
	cp[3] = (n&0x000000ff00000000LL) >> 32;
	cp[2] = (n&0x0000ff0000000000LL) >> 40;
	cp[1] = (n&0x00ff000000000000LL) >> 48;
	cp[0] = (n&0xff00000000000000LL) >> 56;
}

#define	TNAMSIZ	100
#define	TPFXSIZ	155
#define	TMAGSIZ	6
#define	TTIMSIZ	12

/*
 * Structure of an archive header.
 */
union bincpio {
	char		data[4096];
#define	SIZEOF_hdr_cpio	26
	struct hdr_cpio {
		char		c_magic[2];
		char		c_dev[2];
		char		c_ino[2];
		char		c_mode[2];
		char		c_uid[2];
		char		c_gid[2];
		char		c_nlink[2];
		char		c_rdev[2];
		char		c_mtime[4];
		char		c_namesize[2];
		char		c_filesize[4];
	} Hdr;
#define	SIZEOF_cray_hdr	152
	struct cray_hdr {	/* with thanks to Cray-Cyber.org */
		char		C_magic[8];
		char		C_dev[8];
		char		C_ino[8];
		char		C_mode[8];
		char		C_uid[8];
		char		C_gid[8];
		char		C_nlink[8];
		char		C_rdev[8];
				/*
				 * The C_param field was introduced with
				 * UNICOS 6 and is simply not present in
				 * the earlier format. Following fields
				 * are the same for both revisions again.
				 */
#define	CRAY_PARAMSZ	(8*8)
		char		C_param[CRAY_PARAMSZ];
		char		C_mtime[8];
		char		C_namesize[8];
		char		C_filesize[8];
	} Crayhdr;
#define	SIZEOF_c_hdr	76
	struct c_hdr {
		char		c_magic[6];
		char		c_dev[6];
		char		c_ino[6];
		char		c_mode[6];
		char		c_uid[6];
		char		c_gid[6];
		char		c_nlink[6];
		char		c_rdev[6];
		char		c_mtime[11];
		char		c_namesz[6];
		char		c_filesz[11];
	} Cdr;
#define	SIZEOF_d_hdr	86
	struct d_hdr {
		char		d_magic[6];
		char		d_dev[6];
		char		d_ino[6];
		char		d_mode[6];
		char		d_uid[6];
		char		d_gid[6];
		char		d_nlink[6];
		char		d_rmaj[8];
		char		d_rmin[8];
		char		d_mtime[11];
		char		d_namesz[6];
		char		d_filesz[11];
	} Ddr;
#define	SIZEOF_Exp_cpio_hdr	110
	struct Exp_cpio_hdr {
		char		E_magic[6];
		char		E_ino[8];
		char		E_mode[8];
		char		E_uid[8];
		char		E_gid[8];
		char		E_nlink[8];
		char		E_mtime[8];
		char		E_filesize[8];
		char		E_maj[8];
		char		E_min[8];
		char		E_rmaj[8];
		char		E_rmin[8];
		char		E_namesize[8];
		char		E_chksum[8];
	} Edr;
	struct tar_header {
		char		t_name[TNAMSIZ];
		char		t_mode[8];
		char		t_uid[8];
		char		t_gid[8];
		char		t_size[12];
		char		t_mtime[TTIMSIZ];
		char		t_chksum[8];
		char		t_linkflag;
		char		t_linkname[TNAMSIZ];
		char		t_magic[TMAGSIZ];
		char		t_version[2];
		char		t_uname[32];
		char		t_gname[32];
		char		t_devmajor[8];
		char		t_devminor[8];
		char		t_prefix[TPFXSIZ];
	} Tdr;
#define	SIZEOF_bar_header	84
	struct bar_header {
		char		b_mode[8];
		char		b_uid[8];
		char		b_gid[8];
		char		b_size[12];
		char		b_mtime[12];
		char		b_chksum[8];
		char		b_rdev[8];
		char		b_linkflag;
		char		b_bar_magic[2];
		char		b_volume_num[4];
		char		b_compressed;
		char		b_date[12];
	} Bdr;
#define	SIZEOF_zip_header	30
	struct zip_header {
		char		z_signature[4];
		char		z_version[2];
		char		z_gflag[2];
		char		z_cmethod[2];
		char		z_modtime[2];
		char		z_moddate[2];
		char		z_crc32[4];
		char		z_csize[4];
		char		z_nsize[4];
		char		z_namelen[2];
		char		z_extralen[2];
	} Zdr;
};

#define	BCUT	0177777
#define	OCUT	0777777
#define	ECUT	0xFFFFFFFFUL

static const char	trailer[] = "TRAILER!!!";

/*
 * Structure of per-file extra data for zip format.
 */
union zextra {
	char	data[1];
#define	SIZEOF_zextra_gn	4
	struct	zextra_gn {
		char	ze_gn_tag[2];
		char	ze_gn_tsize[2];
	} Ze_gn;
#define	SIZEOF_zextra_64	32		/* regular size */
#define	SIZEOF_zextra_64_a	28		/* size without startn field */
#define	SIZEOF_zextra_64_b	20		/* size without reloff field */
	struct	zextra_64 {
		char	ze_64_tag[2];
		char	ze_64_tsize[2];
		char	ze_64_nsize[8];
		char	ze_64_csize[8];
		char	ze_64_reloff[8];
		char	ze_64_startn[4];
	} Ze_64;
#define	SIZEOF_zextra_pk	16
	struct	zextra_pk {
		char	ze_pk_tag[2];
		char	ze_pk_tsize[2];
		char	ze_pk_atime[4];
		char	ze_pk_mtime[4];
		char	ze_pk_uid[2];
		char	ze_pk_gid[2];
	} Ze_pk;
#define	SIZEOF_zextra_ek	17
	struct	zextra_et {
		char	ze_et_tag[2];
		char	ze_et_tsize[2];
		char	ze_et_flags[1];
		char	ze_et_mtime[4];
		char	ze_et_atime[4];
		char	ze_et_ctime[4];
	} Ze_et;
#define	SIZEOF_zextra_i1	16
	struct	zextra_i1 {
	char	ze_i1_tag[2];
		char	ze_i1_tsize[2];
		char	ze_i1_atime[4];
		char	ze_i1_mtime[4];
		char	ze_i1_uid[2];
		char	ze_i1_gid[2];
	} Ze_i1;
#define	SIZEOF_zextra_i2	8
	struct	zextra_i2 {
		char	ze_i2_tag[2];
		char	ze_i2_tsize[2];
		char	ze_i2_uid[2];
		char	ze_i2_gid[2];
	} Ze_i2;
#define	SIZEOF_zextra_as	16
	struct	zextra_as {
		char	ze_as_tag[2];
		char	ze_as_tsize[2];
		char	ze_as_crc[4];
		char	ze_as_mode[2];
		char	ze_as_sizdev[4];
		char	ze_as_uid[2];
		char	ze_as_gid[2];
	} Ze_as;
#define	SIZEOF_zextra_cp	40
	struct	zextra_cp {
		char	ze_cp_tag[2];
		char	ze_cp_tsize[2];
		char	ze_cp_dev[4];
		char	ze_cp_ino[4];
		char	ze_cp_mode[4];
		char	ze_cp_uid[4];
		char	ze_cp_gid[4];
		char	ze_cp_nlink[4];
		char	ze_cp_rdev[4];
		char	ze_cp_mtime[4];
		char	ze_cp_atime[4];
	} Ze_cp;
};

static struct	zipstuff {		/* stuff for central directory at EOF */
	struct zipstuff	*zs_next;
	char	*zs_name;		/* file name */
	long long	zs_size;	/* file size */
	long long	zs_relative;	/* offset of local header */
	long long	zs_csize;	/* compressed size */
	uint32_t	zs_crc32;	/* CRC */
	time_t		zs_mtime;	/* modification time */
	enum cmethod	zs_cmethod;	/* compression method */
	int		zs_gflag;	/* general flag */
	mode_t		zs_mode;	/* file mode */
} *zipbulk;

/*
 * Structure of the central zip directory at the end of the file. This
 * (obligatory) part of a zip file is written by this implementation,
 * but is completely ignored on copy-in. This means that we miss the
 * mode_t stored in zc_extralen if zc_versionmade[1] is 3 (Unix). We
 * have to do this since it contains the S_IFMT bits, thus telling us
 * whether something is a symbolic link and resulting in different
 * behavior - but as the input had to be seekable in order to do this,
 * we had to store entire archives in temporary files if input came
 * from a pipe to be consistent.
 */
#define	SIZEOF_zipcentral	46
struct	zipcentral {
	char	zc_signature[4];
	char	zc_versionmade[2];
	char	zc_versionextr[2];
	char	zc_gflag[2];
	char	zc_cmethod[2];
	char	zc_modtime[2];
	char	zc_moddate[2];
	char	zc_crc32[4];
	char	zc_csize[4];
	char	zc_nsize[4];
	char	zc_namelen[2];
	char	zc_extralen[2];
	char	zc_commentlen[2];
	char	zc_disknstart[2];
	char	zc_internal[2];
	char	zc_external[4];
	char	zc_relative[4];
};

#define	SIZEOF_zip64end		56
struct	zip64end {
	char	z6_signature[4];
	char	z6_recsize[8];
	char	z6_versionmade[2];
	char	z6_versionextr[2];
	char	z6_thisdiskn[4];
	char	z6_alldiskn[4];
	char	z6_thisentries[8];
	char	z6_allentries[8];
	char	z6_dirsize[8];
	char	z6_startsize[8];
};

#define	SIZEOF_zip64loc		20
struct	zip64loc {
	char	z4_signature[4];
	char	z4_startno[4];
	char	z4_reloff[8];
	char	z4_alldiskn[4];
};

#define	SIZEOF_zipend		22
struct	zipend {
	char	ze_signature[4];
	char	ze_thisdiskn[2];
	char	ze_alldiskn[2];
	char	ze_thisentries[2];
	char	ze_allentries[2];
	char	ze_dirsize[4];
	char	ze_startsize[4];
	char	ze_commentlen[2];
};

#define	SIZEOF_zipddesc		16
struct	zipddesc {
	char	zd_signature[4];
	char	zd_crc32[4];
	char	zd_csize[4];
	char	zd_nsize[4];
};

#define	SIZEOF_zipddesc64	24
struct	zipddesc64 {
	char	zd_signature[4];
	char	zd_crc32[4];
	char	zd_csize[8];
	char	zd_nsize[8];
};

/*
 * Magic numbers and strings.
 */
static const uint16_t	mag_bin = 070707;
/*static const uint16_t	mag_bbs = 0143561;*/
static const char	mag_asc[6] = "070701";
static const char	mag_crc[6] = "070702";
static const char	mag_odc[6] = "070707";
static const long	mag_sco = 0x7ffffe00;

static const char	mag_ustar[6] = "ustar\0";
static const char	mag_gnutar[8] = "ustar  \0";
static const char	mag_bar[2] = "V\0";

static const char	mag_zipctr[4] = "PK\1\2";
static const char	mag_zipsig[4] = "PK\3\4";
static const char	mag_zipend[4] = "PK\5\6";
static const char	mag_zip64e[4] = "PK\6\6";
static const char	mag_zip64l[4] = "PK\6\7";
static const char	mag_zipdds[4] = "PK\7\10";
#define			mag_zip64f	0x0001
#define			mag_zipcpio	0x0707

/*
 * Fields for the extended pax header.
 */
static enum paxrec {
	PR_NONE		= 0000,
	PR_ATIME	= 0001,
	PR_GID		= 0002,
	PR_LINKPATH	= 0004,
	PR_MTIME	= 0010,
	PR_PATH		= 0020,
	PR_SIZE		= 0040,
	PR_UID		= 0100,
	PR_SUN_DEVMAJOR	= 0200,
	PR_SUN_DEVMINOR	= 0400
} paxrec, globrec;

/*
 * Prototype structure, collecting user-defined information
 * about a file.
 */
struct prototype {
	mode_t	pt_mode;	/* type and permission bits */
	uid_t	pt_uid;		/* owner */
	gid_t	pt_gid;		/* group owner */
	time_t	pt_atime;	/* time of last access */
	time_t	pt_mtime;	/* time of last modification */
	dev_t	pt_rdev;	/* device major/minor */
	enum {
		PT_NONE	 = 0000,
		PT_TYPE	 = 0001,
		PT_OWNER = 0002,
		PT_GROUP = 0004,
		PT_MODE  = 0010,
		PT_ATIME = 0020,
		PT_MTIME = 0040,
		PT_RDEV  = 0100
	}	pt_spec;	/* specified information */
};

static struct stat	globst;

/*
 * This sets a sanity check limit on path names. If a longer path name
 * occurs in an archive, it is treated as corrupt. This is because no
 * known Unix system can handle path names of arbitrary length; limits
 * are typically between 1024 and 4096. Trying to extract longer path
 * names would fail anyway and will cpio eventually fail to allocate
 * memory.
 */
#define			SANELIMIT	0177777

char			*progname;	/* argv[0] to main() */
static struct dslot	*devices;	/* devices table */
static struct dslot	*markeddevs;	/* unusable device numbers */
static char		*blkbuf;	/* block buffer */
int			blksiz;		/* block buffer size */
static int		blktop;		/* top of filled part of buffer */
static int		curpos;		/* position in blkbuf */
static uint32_t		fakedev;	/* fake device for single link inodes */
static uint32_t		fakeino;	/* fake inode for single link inodes */
static uint32_t		harddev;	/* fake device used for hard links */
static unsigned long long	maxsize;/* maximum size for format */
static unsigned long long	maxrdev;/* maximum st_rdev for format */
static unsigned long long	maxmajor;/* maximum major(st_rdev) for format */
static unsigned long long	maxminor;/* maximum minor(st_rdev) for format */
static unsigned long long	maxuid;	/* maximum user id for format */
static unsigned long long	maxgid;	/* maximum group id for format */
static unsigned long long	maxnlink;/* maximum link count for format */
static int		mt;		/* magtape file descriptor */
static int		mfl;		/* magtape flags */
static struct stat	mtst;		/* fstat() on mt */
int			aflag;		/* reset access times */
int			Aflag;		/* append to archive */
int			bflag;		/* swap bytes */
int			Bflag;		/* 5120 blocking */
int			cflag;		/* ascii format */
int			Cflag;		/* user-defined blocking */
int			dflag;		/* create directories */
int			Dflag;		/* do not ask for next device */
int			eflag;		/* DEC format */
int			cray_eflag;	/* do not archive if values too large */
const char		*Eflag;		/* filename for files to be extracted */
int			fflag;		/* pattern excludes */
int			Hflag;		/* header format */
const char		*Iflag;		/* input archive name */
int			kflag;		/* skipt corrupted parts */
int			Kflag;		/* IRIX-style large file support */
int			lflag;		/* link of possible */
int			Lflag;		/* follow symbolic links */
int			mflag;		/* retain modification times */
const char		*Mflag;		/* message when switching media */
const char		*Oflag;		/* output archive name */
int			Pflag;		/* prototype file list */
int			rflag;		/* rename files */
const char		*Rflag;		/* reassign ownerships */
static uid_t		Ruid;		/* uid to assign */
static gid_t		Rgid;		/* gid to assign */
int			sflag;		/* swap half word bytes */
int			Sflag;		/* swap word bytes */
int			tflag;		/* print toc */
int			uflag;		/* overwrite files unconditionally */
int			hp_Uflag;	/* use umask when creating files */
int			vflag;		/* verbose */
int			Vflag;		/* special verbose */
int			sixflag;	/* 6th Edition archives */
int			action;		/* -i -o -p */
long long		errcnt;		/* error status */
static unsigned long long	maxpath;/* maximum path length with -i */
static uint32_t		maxino;		/* maximum inode number with -i */
static uid_t		myuid;		/* user id of caller */
static gid_t		mygid;		/* group id of caller */
static long long	blocks;		/* copying statistics: full blocks */
static long long	bytes;		/* copying statistics: partial blocks */
static long long	nwritten;	/* bytes written to archive */
static off_t		aoffs;		/* offset in archive */
static off_t		poffs;		/* physical offset in archive */
static int		tapeblock = -1;	/* physical tape block size */
struct glist		*patterns;	/* patterns for -i */
static int		tty;		/* terminal file descriptor */
static const char	*cur_ofile;	/* current original file */
static const char	*cur_tfile;	/* current temporary file */
static mode_t		umsk;		/* user's umask */
static int		zipclevel;	/* zip compression level */
static struct islot	*inull;		/* splay tree null element */
int			printsev;	/* print message severity strings */
static int		compressed_bar;	/* this is a compressed bar archive */
static int		formatforced;	/* -k -i -Hfmt forces a format */
static long long	lineno;		/* input line number */

int			pax_dflag;	/* directory matches only itself */
int			pax_kflag;	/* do not overwrite files */
int			pax_nflag;	/* select first archive member only */
int			pax_sflag;	/* substitute file names */
int			pax_uflag;	/* add only recent files to archive */
int			pax_Xflag;	/* do not cross device boundaries */
static enum {
	PO_NONE		= 0,
	PO_LINKDATA	= 01,		/* include link data in type 2 */
	PO_TIMES	= 02,		/* create atime and mtime fields */
} pax_oflag;				/* recognized -o options */

static void	copyout(int (*)(const char *, struct stat *));
static size_t	ofiles_cpio(char **, size_t *);
static void	dooutp(void);
static int	outfile(const char *, struct stat *);
static int	addfile(const char *, struct stat *, uint32_t, uint32_t, int,
			const char *);
static void	iflush(struct islot *, uint32_t);
static void	lflush(void);
static int	bigendian(void);
static void	getbuf(char **, size_t *, size_t);
static void	prdot(int);
static void	newmedia(int);
static void	mclose(void);
static ssize_t	mwrite(int);
static void	bwrite(const char *, size_t);
static void	bflush(void);
static int	sum(int, const char *, struct stat *, char *);
static int	rstime(const char *, struct stat *, const char *);
static struct islot	*isplay(ino_t, struct islot *);
static struct islot	*ifind(ino_t, struct islot **);
static void	iput(struct islot *, struct islot **);
static struct dslot	*dfind(struct dslot **, dev_t);
static void	done(int);
static void	dopass(const char *);
static int	passdata(struct file *, const char *, int);
static int	passfile(const char *, struct stat *);
static int	filein(struct file *, int (*)(struct file *, const char *, int),
			char *);
static int	linkunlink(const char *, const char *);
static void	tunlink(char **);
static int	filet(struct file *, int (*)(struct file *, const char *, int));
static void	filev(struct file *);
static int	typec(struct stat *);
static void	permbits(mode_t);
static void	prtime_cpio(time_t);
static void	getpath(const char *, char **, char **, size_t *, size_t *);
static void	setpath(const char *, char **, char **,
			size_t, size_t *, size_t *);
static int	imdir(char *);
static int	setattr(const char *, struct stat *);
static int	setowner(const char *, struct stat *);
static int	canlink(const char *, struct stat *, int);
static void	doinp(void);
static void	storelink(struct file *);
static void	flushlinks(struct file *);
static void	flushnode(struct islot *, struct file *);
static void	flushrest(int);
static void	flushtree(struct islot *, int);
static int	inpone(struct file *, int);
static int	readhdr(struct file *, union bincpio *);
static void	whathdr(void);
static int	infile(struct file *);
static int	skipfile(struct file *);
static int	skipdata(struct file *f,
			int (*)(struct file *, const char *, int));
static int	indata(struct file *, const char *, int);
static int	totrailer(void);
static long long	rdoct(const char *, int);
static long long	rdhex(const char *, int);
static ssize_t	mread(void);
static void	mstat(void);
static int	skippad(unsigned long long, int);
static int	allzero(const char *, int);
static const char	*getuser(uid_t);
static const char	*getgroup(gid_t);
static struct glist	*want(struct file *, struct glist **);
static void	patfile(void);
static int	ckodd(long long, int, const char *, const char *);
static int	rname(char **, size_t *);
static int	redirect(const char *, const char *);
static char	*tnameof(struct tar_header *, char *);
static int	tmkname(struct tar_header *, const char *);
static void	tlinkof(struct tar_header *, struct file *);
static int	tmklink(struct tar_header *, const char *);
static int	tlflag(struct stat *);
static void	tchksum(union bincpio *);
static int	tcssum(union bincpio *, int);
static int	trdsum(union bincpio *);
static mode_t	tifmt(int);
static void	bchksum(union bincpio *);
static int	bcssum(union bincpio *);
static void	blinkof(const char *, struct file *, int);
static void	dump_barhdr(void);
static int	zcreat(const char *, mode_t);
static int	zclose(int);
static void	markdev(dev_t);
static int	marked(dev_t);
static void	cantsup(int, const char *);
static void	onint(int);
static int	zipread(struct file *, const char *, int, int);
static void	zipreaddesc(struct file *);
static int	cantunzip(struct file *, const char *);
static time_t	gdostime(const char *, const char *);
static void	mkdostime(time_t, char *, char *);
static ssize_t	ziprxtra(struct file *, struct zip_header *);
static void	ziptrailer(void);
static void	zipdefer(const char *, struct stat *, long long,
			uint32_t, long long, const struct zip_header *);
static int	zipwrite(int, const char *, struct stat *,
			union bincpio *, size_t, uint32_t, uint32_t,
			uint32_t *, long long *);
static int	zipwtemp(int, const char *, struct stat *,
			union bincpio *, size_t, uint32_t, uint32_t,
			uint32_t *, long long *);
#if USE_ZLIB
static int	zipwdesc(int, const char *, struct stat *,
			union bincpio *, size_t, uint32_t, uint32_t,
			uint32_t *, long long *);
#endif	/* USE_ZLIB */
static int	zipwxtra(const char *, struct stat *, uint32_t, uint32_t);
static void	zipinfo(struct file *);
static void	readK2hdr(struct file *);
static int	readgnuname(char **, size_t *, long);
static void	writegnuname(const char *, long, int);
static void	tgetpax(struct tar_header *, struct file *);
static enum paxrec	tgetrec(char **, char **, char **);
static void	wrpax(const char *, const char *, struct stat *);
static void	addrec(char **, long *, long *,
			const char *, const char *, long long);
static void	paxnam(struct tar_header *, const char *);
static char	*sequence(void);
static char	*joinpath(const char *, char *);
static int	utf8(const char *);
static char	*getproto(char *, struct prototype *);

size_t		(*ofiles)(char **, size_t *) = ofiles_cpio;
void		(*prtime)(time_t) = prtime_cpio;

int
main(int argc, char **argv)
{
	myuid = getuid();
	mygid = getgid();
	umask(umsk = umask(0));
	progname = basename(argv[0]);
	setlocale(LC_CTYPE, "");
	setlocale(LC_TIME, "");
	inull = scalloc(1, sizeof *inull);
	inull->i_lln = inull->i_rln = inull;
	flags(argc, argv);
	switch (action) {
	case 'i':
		if (sigset(SIGINT, SIG_IGN) != SIG_IGN)
			sigset(SIGINT, onint);
		doinp();
		break;
	case 'o':
		dooutp();
		break;
	case 'p':
		if (sigset(SIGINT, SIG_IGN) != SIG_IGN)
			sigset(SIGINT, onint);
		dopass(argv[optind]);
		break;
	}
	if (tflag)
		fflush(stdout);
	else if (Vflag)
		prdot(1);
	if (pax != PAX_TYPE_CPIO)
		pax_onexit();
	//fprintf(stderr, "%llu blocks\n", blocks + ((bytes + 0777) >> 9));
	mclose();
	if (errcnt && sysv3 == 0)
		fprintf(stderr, "%llu error(s)\n", errcnt);
	return errcnt ? sysv3 ? 1 : 2 : 0;
}

static size_t
ofiles_cpio(char **name, size_t *namsiz)
{
	static struct iblok	*ip;

	if (ip == NULL)
		ip = ib_alloc(0, 0);
	return ib_getlin(ip, name, namsiz, srealloc);
}

/*
 * Read the file name list for -o and -p and do initial processing
 * for each name.
 */
static void
copyout(int (*copyfn)(const char *, struct stat *))
{
	char	*name = NULL, *np;
	size_t	namsiz = 0, namlen;
	struct stat	st;
	struct prototype	pt;

	while ((namlen = ofiles(&name, &namsiz)) != 0) {
		lineno++;
		if (name[namlen-1] == '\n')
			name[--namlen] = '\0';
		if (Pflag)
			np = getproto(name, &pt);
		else
			np = name;
		while (np[0] == '.' && np[1] == '/') {
			np += 2;
			while (*np == '/')
				np++;
			if (*np == '\0') {
				np = name;
				break;
			}
		}
		if (lstat(np, &st) < 0) {
			if (Pflag && *np && ((pt.pt_spec &
			    (PT_TYPE|PT_OWNER|PT_GROUP|PT_MODE|PT_RDEV) &&
					((pt.pt_mode&S_IFMT) == S_IFBLK ||
					 (pt.pt_mode&S_IFMT) == S_IFCHR)) ||
				      (pt.pt_spec &
		            (PT_TYPE|PT_OWNER|PT_GROUP|PT_MODE) &&
			    		((pt.pt_mode&S_IFMT) == S_IFDIR ||
					 (pt.pt_mode&S_IFMT) == S_IFIFO ||
					 (pt.pt_mode&S_IFMT) == S_IFREG)))) {
				memset(&st, 0, sizeof st);
				st.st_mode = pt.pt_mode;
				st.st_blksize = 4096;
				st.st_nlink = 1;
				goto missingok;
			}
			else if (sysv3 < 0)
				msg(2, 0, "< %s > ?\n", np);
			else if (sysv3 > 0)
				msg(2, 0, "Cannot obtain information "
						"about file:  \"%s\".\n",
						np);
			else
				emsg(2, "Error with lstat of \"%s\"", np);
			errcnt++;
			continue;
		}
	missingok:
		if (Lflag && (st.st_mode&S_IFMT) == S_IFLNK) {
			if (stat(np, &st) < 0) {
				emsg(2, "Cannot follow \"%s\"", np);
				errcnt++;
				continue;
			}
		}
		/*
		 * These file types are essentially useless in an archive
		 * since they are recreated by any process that needs them.
		 * We thus ignore them and do not even issue a warning,
		 * because that would only displace more important messages
		 * on a terminal and confuse people who just want to copy
		 * directory hierarchies.--But for pax, POSIX.1-2001 requires
		 * us to fail!
		 */
		if ((st.st_mode&S_IFMT) == S_IFSOCK ||
				(st.st_mode&S_IFMT) == S_IFDOOR) {
			if (pax >= PAX_TYPE_PAX2001) {
				msg(2, 0, "Cannot handle %s \"%s\".\n",
					(st.st_mode&S_IFMT) == S_IFSOCK ?
						"socket" : "door", np);
				errcnt++;
			}
			continue;
		}
		if (Pflag) {
			if (pt.pt_spec & PT_TYPE)
				if ((st.st_mode&S_IFMT) != (pt.pt_mode&S_IFMT))
					msg(4, 0, "line %lld: types "
						"do not match\n", lineno);
			if (pt.pt_spec & PT_OWNER)
				st.st_uid = pt.pt_uid;
			if (pt.pt_spec & PT_GROUP)
				st.st_gid = pt.pt_gid;
			if (pt.pt_spec & PT_MODE) {
				st.st_mode &= ~(mode_t)07777;
				st.st_mode |= pt.pt_mode;
			}
			if (pt.pt_spec & PT_ATIME)
				st.st_atime = pt.pt_atime;
			if (pt.pt_spec & PT_MTIME)
				st.st_mtime = pt.pt_mtime;
			if (pt.pt_spec & PT_RDEV) {
				if ((st.st_mode&S_IFMT) != S_IFBLK &&
				    (st.st_mode&S_IFMT) != S_IFCHR)
					msg(4, 0, "line %lld: device type "
						"specified for non-device "
						"file\n", lineno);
				st.st_rdev = pt.pt_rdev;
			}
		}
		if (pax_track(np, st.st_mtime) == 0)
			continue;
		if ((fmttype == FMT_ZIP ||
					fmttype & TYP_BAR ||
					fmttype == FMT_GNUTAR)
				&& (st.st_mode&S_IFMT) == S_IFDIR &&
				name[namlen-1] != '/') {
			if (namlen+2 >= namsiz) {
				size_t	diff = np - name;
				name = srealloc(name, namsiz = namlen+2);
				np = &name[diff];
			}
			name[namlen++] = '/';
			name[namlen] = '\0';
		}
		errcnt += copyfn(np, &st);
	}
}

/*
 * Execution for -o.
 */
static void
dooutp(void)
{
	if (Oflag) {
		if ((mt = Aflag ? open(Oflag, O_RDWR, 0666) :
					creat(Oflag, 0666)) < 0) {
			if (sysv3) {
				emsg(013, "Cannot open <%s> for %s.", Oflag,
					Aflag ? "append" : "output");
				done(1);
			} else
				msg(3, -2, "Cannot open \"%s\" for %s\n", Oflag,
					Aflag ? "append" : "output");
		}
	} else
		mt = dup(1);
	mstat();
	blkbuf = svalloc(blksiz, 1);
	if (Aflag) {
		if (totrailer() != 0)
			return;
	} else if (fmttype == FMT_NONE)
		fmttype = bigendian() ? FMT_BINBE : FMT_BINLE;
	if (fmttype & TYP_BINARY) {
		maxino = 0177777;
		fakeino = 0177777;
		maxpath = 256;
		if (fmttype & TYP_SGI) {
			maxsize = 0x7FFFFFFFFFFFFFFFLL;
			maxmajor = 037777;
			maxminor = 0777777;
		} else {
			maxsize = 0x7FFFFFFFLL;
			maxrdev = 0177777;
		}
		maxuid = 0177777;
		maxgid = 0177777;
		maxnlink = 0177777;
	} else if (fmttype == FMT_ODC) {
		maxino = 0777777;
		fakeino = 0777777;
		maxpath = 256;
		maxsize = 077777777777LL;
		maxrdev = 0777777;
		maxuid = 0777777;
		maxgid = 0777777;
		maxnlink = 0777777;
	} else if (fmttype == FMT_DEC) {
		maxino = 0777777;
		fakeino = 0777777;
		maxpath = 256;
		maxsize = 077777777777LL;
		maxmajor = 077777777;
		maxminor = 077777777;
		maxuid = 0777777;
		maxgid = 0777777;
		maxnlink = 0777777;
	} else if (fmttype & TYP_NCPIO) {
		maxino = 0xFFFFFFFFUL;
		fakeino = 0xFFFFFFFFUL;
		maxpath = 1024;
		maxsize = fmttype&TYP_SCO ? 0x7FFFFFFFFFFFFFFFLL : 0xFFFFFFFFUL;
		maxmajor = 0xFFFFFFFFUL;
		maxminor = 0xFFFFFFFFUL;
		maxuid = 0xFFFFFFFFUL;
		maxgid = 0xFFFFFFFFUL;
		maxnlink = 0xFFFFFFFFUL;
	} else if (fmttype & TYP_CRAY) {
		maxino = 0xFFFFFFFFUL;
		fakeino = 0xFFFFFFFFUL;
		maxpath = SANELIMIT;
		maxsize = 0x7FFFFFFFFFFFFFFFLL;
		maxrdev = 0x7FFFFFFFFFFFFFFFLL;
		maxuid = 0x7FFFFFFFFFFFFFFFLL;
		maxgid = 0x7FFFFFFFFFFFFFFFLL;
		maxnlink = 0x7FFFFFFFFFFFFFFFLL;
	} else if (fmttype == FMT_GNUTAR) {
		maxino = 0xFFFFFFFFUL;
		fakeino = 0xFFFFFFFFUL;
		maxpath = SANELIMIT;
		maxsize = 0x7FFFFFFFFFFFFFFFLL;
		maxmajor = 0x7FFFFFFFFFFFFFFFLL;
		maxminor = 0x7FFFFFFFFFFFFFFFLL;
		maxuid = 0x7FFFFFFFFFFFFFFFLL;
		maxgid = 0x7FFFFFFFFFFFFFFFLL;
		maxnlink = 0x7FFFFFFFFFFFFFFFLL;
	} else if (fmttype & TYP_PAX) {
		maxino = 0xFFFFFFFFUL;
		fakeino = 0xFFFFFFFFUL;
		maxpath = SANELIMIT;
		maxsize = 0x7FFFFFFFFFFFFFFFLL;
		maxmajor = fmttype==FMT_SUN ? 0x7FFFFFFFFFFFFFFFLL : 07777777;
		maxminor = fmttype==FMT_SUN ? 0x7FFFFFFFFFFFFFFFLL : 07777777;
		maxuid = 0x7FFFFFFFFFFFFFFFLL;
		maxgid = 0x7FFFFFFFFFFFFFFFLL;
		maxnlink = 0x7FFFFFFFFFFFFFFFLL;
		if (pax_oflag & PO_TIMES)
			globrec |= PR_ATIME|PR_MTIME;
	} else if (fmttype & TYP_BAR) {
		maxino = 0xFFFFFFFFUL;
		fakeino = 0xFFFFFFFFUL;
		maxpath = 512 - SIZEOF_bar_header - 1;
		maxsize = 077777777777LL;
		maxrdev = 07777777;
		maxuid = 07777777;
		maxgid = 07777777;
		maxnlink = 0x7FFFFFFFFFFFFFFFLL;
		if (nwritten == 0)
			dump_barhdr();
	} else if (fmttype & TYP_USTAR) {
		maxino = 0xFFFFFFFFUL;
		fakeino = 0xFFFFFFFFUL;
		maxpath = 256;
		maxsize = 077777777777LL;
		maxmajor = 07777777;
		maxminor = 07777777;
		maxuid = 07777777;
		maxgid = 07777777;
		maxnlink = 0x7FFFFFFFFFFFFFFFLL;
	} else if (fmttype & TYP_OTAR) {
		maxino = 0xFFFFFFFFUL;
		fakeino = 0xFFFFFFFFUL;
		maxpath = 99;
		maxsize = 077777777777LL;
		maxuid = 07777777;
		maxgid = 07777777;
		maxnlink = 0x7FFFFFFFFFFFFFFFLL;
	} else if (fmttype == FMT_ZIP) {
		maxino = 0xFFFFFFFFUL;
		fakeino = 0xFFFFFFFFUL;
		maxpath = 60000;
		maxsize = 0x7FFFFFFFFFFFFFFFLL;
		maxrdev = 0xFFFFFFFFUL;
		maxuid = 0xFFFFFFFFUL;
		maxgid = 0xFFFFFFFFUL;
		maxnlink = 0xFFFFFFFFUL;
	} else
		abort();
	fakedev = 0177777;
	harddev = 1;
	copyout(outfile);
	if (fmttype & TYP_NCPIO)
		lflush();
	if (fmttype & TYP_CPIO) {
		struct stat	st;

		memset(&st, 0, sizeof st);
		st.st_nlink = 1;
		outfile(trailer, &st);
	}
	if (fmttype & TYP_TAR) {
		char b[512];

		memset(b, 0, sizeof b);
		bwrite(b, sizeof b);
		bwrite(b, sizeof b);
	}
	if (fmttype == FMT_ZIP)
		ziptrailer();
	bflush();
}

/*
 * Handle a single file for -o, do sanity checks and detect hard links.
 */
static int
outfile(const char *file, struct stat *st)
{
	uint32_t dev, ino;
	size_t pathsz;

	if ((st->st_mode&S_IFMT) == S_IFREG)
		if (mtst.st_dev == st->st_dev && mtst.st_ino == st->st_ino)
			return 0;
	if (st->st_size > maxsize) {
		msg(2, 0, "Size of %c%s%c >%lluGB. Not dumped\n",
				sysv3 ? '<' : '"',
				file,
				sysv3 ? '>' : '"',
				(maxsize+1) / (1024*1024*1024));
		return 1;
	}
	if (((st->st_mode&S_IFMT)==S_IFBLK||(st->st_mode&S_IFMT)==S_IFCHR) &&
		(maxrdev &&
			(unsigned long long)st->st_rdev > maxrdev ||
		maxmajor &&
			(unsigned long long)major(st->st_rdev) > maxmajor ||
		maxminor &&
			(unsigned long long)minor(st->st_rdev) > maxminor)) {
		cantsup(1, file);
		return 1;
	}
	if ((unsigned long long)st->st_uid > maxuid) {
		if (cray_eflag) {
			cantsup(1, file);
			return 1;
		}
		cantsup(0, file);
		st->st_uid = 60001;
		if ((st->st_mode&S_IFMT) == S_IFREG && st->st_mode & 0111)
			st->st_mode &= ~(mode_t)S_ISUID;
		if ((unsigned long long)st->st_gid > maxgid) {
			st->st_gid = 60001;
			if ((st->st_mode&S_IFMT)==S_IFREG && st->st_mode&0010)
				st->st_mode &= ~(mode_t)S_ISGID;
		}
	} else if ((unsigned long long)st->st_gid > maxgid) {
		if (cray_eflag) {
			cantsup(1, file);
			return 1;
		}
		cantsup(0, file);
		st->st_gid = 60001;
		if ((st->st_mode&S_IFMT) == S_IFREG && st->st_mode & 0010)
			st->st_mode &= ~(mode_t)S_ISGID;
	}
	if ((pathsz = strlen(file)) > maxpath) {
		msg(2, 0, "%s: file name too long\n", file);
		return 1;
	}
	/*
	 * Detect hard links and compute fake inode counts. The mechanism
	 * is as follows: If a file has more than one link, a fake device
	 * number starting at one is used for its device, and a fake inode
	 * number is used starting at one too.
	 *
	 * The information on links of directories is useless, so it is
	 * dropped and handled like a file with a single link only: Fake
	 * devices are allocated just below the format's limit, fake
	 * i-nodes the same.
	 *
	 * This way even the binary cpio format can have up to ~4G files.
	 */
	if (maxino && st->st_nlink > 1 && (st->st_mode&S_IFMT) != S_IFDIR) {
		struct dslot *ds, *dp;
		struct islot *ip;

		dev = 1;
		ds = devices;
		dp = NULL;
nextdev:
		for (; ds; dp = ds, ds = ds->d_nxt, dev++ /* see below! */)
			if (ds->d_dev == st->st_dev)
				break;
		if (markeddevs && marked(dev)) {
			dev++;
			goto nextdev;
		}
		if (dev >= fakedev)
			msg(4, 1, "Too many devices in archive, exiting\n");
		if (ds == NULL) {
			ds = scalloc(1, sizeof *ds);
			ds->d_dev = st->st_dev;
			ds->d_fake = dev;
			if (devices == NULL)
				devices = ds;
			else
				dp->d_nxt = ds;
		}
		harddev = dev;
		if ((ip = ifind(st->st_ino, &ds->d_isl)) == NULL) {
			if (ds->d_cnt >= maxino) {
				/* corresponds to for loop above */
				dev++, dp = ds, ds = ds->d_nxt;
				goto nextdev;
			}
			ip = scalloc(1, sizeof *ip);
			ip->i_ino = st->st_ino;
			ip->i_fino = ++ds->d_cnt;
			ip->i_nlk = st->st_nlink;
			if (fmttype & TYP_TAR)
				ip->i_name = sstrdup(file);
			if (fmttype & TYP_NCPIO) {
				ip->i_st = smalloc(sizeof *ip->i_st);
				*ip->i_st = *st;
			}
			iput(ip, &ds->d_isl);
		}
		ino = ip->i_fino;
		if (fmttype & TYP_NCPIO) {
			/*
			 * In SVR4 ascii cpio format, files with multiple
			 * links are stored with a zero size except for the
			 * last link, which contains the actual file content.
			 * As one cannot know which is the last link in
			 * advance since some links may be outside the
			 * archive content, all links have to be collected
			 * and written out at once.
			 */
			struct ilink *il, *ik;

			switch (ip->i_nlk) {
			case 1:
				/*
				 * This was the last link to a file. Write
				 * all previous links and break to write
				 * the actual file content. Free the pointers
				 * in islot; islot remains within the tree
				 * with a remaining link count of zero.
				 */
				ip->i_nlk--;
				free(ip->i_st);
				ip->i_st = NULL;
				for (il = ip->i_lnk, ik = NULL; il;
						ik = il, il = il->l_nxt,
						ik ? free(ik), 0 : 0) {
					errcnt += addfile(il->l_nam, st,
							dev, ino, 1, 0);
					free(il->l_nam);
				}
				break;
			case 0:
				/*
				 * This file got a link during operation, or
				 * -L was specified and we encountered a link
				 * more than once. Start with a fresh link
				 * count again.
				 */
				ip->i_nlk = st->st_nlink;
				ip->i_lnk = NULL;
				ip->i_st = smalloc(sizeof *ip->i_st);
				*ip->i_st = *st;
				/*FALLTHRU*/
			default:
				/*
				 * There are more links to this file. Store
				 * only the name and return.
				 */
				ip->i_nlk--;
				if (ip->i_lnk) {
					for (il = ip->i_lnk; il->l_nxt;
							il = il->l_nxt);
					il->l_nxt = scalloc(1,sizeof*il->l_nxt);
					il = il->l_nxt;
				} else {
					ip->i_lnk = scalloc(1,sizeof*ip->i_lnk);
					il = ip->i_lnk;
				}
				il->l_nam = smalloc(pathsz + 1);
				strcpy(il->l_nam, file);
				return 0;
			}
		} else if (fmttype & TYP_TAR) {
			if (strcmp(ip->i_name, file))
				return addfile(file, st, dev, ino, 1,
						ip->i_name);
		}
	} else {	/* single-linked or directory */
		dev = fakedev;
		while (markeddevs && marked(dev))
			dev--;
		if ((ino = fakeino--) == 0) {
			if (--dev <= harddev)
				msg(4, 1, "Too many devices in archive, "
						"exiting\n");
			fakedev = dev;
			ino = maxino;
			fakeino = ino - 1;
		}
	}
	return addfile(file, st, dev, ino, 0, 0);
}

/*
 * Add a single file to the archive with -o.
 */
static int
addfile(const char *realfile, struct stat *st,
		uint32_t dev, uint32_t ino, int zerolink, const char *linkname)
{
	union bincpio bc;
	int fd = -1;
	long long size;
	int pad, i;
	ssize_t rsz = 0, wsz = 0, hsz, fsz, psz;
	long long remsz, relative, nlink;
	long long Kbase = 0, Krest = 0, Ksize = 0;
	struct hdr_cpio	K2hdr;
	uint32_t	crc = 0;
	long long	csize = 0;
	char	*file;
	char	*symblink = NULL;
	int	failure = 1;

	file = sstrdup(realfile);
	if (pax != PAX_TYPE_CPIO && strcmp(file, trailer)) {
		size_t	junk = 0;
		if (pax_sflag && pax_sname(&file, &junk) == 0)
			goto cleanup;
		if (rflag && rname(&file, &junk) == 0)
			goto cleanup;
	}
	fsz = strlen(file);
	relative = nwritten;
	memset(bc.data, 0, sizeof bc.data);
	if (fmttype == FMT_PAX && pax_oflag & PO_LINKDATA &&
			(st->st_mode&S_IFMT) == S_IFREG)
		size = st->st_size;
	else if (zerolink)
		size = 0;
	else if ((st->st_mode&S_IFMT) == S_IFREG)
		size = st->st_size;
	else if ((st->st_mode&S_IFMT) == S_IFLNK) {
		i = st->st_size ? st->st_size : PATH_MAX;
		symblink = smalloc(i+1);
		if ((size = readlink(realfile, symblink, i)) < 0) {
			emsg(3, "Cannot read symbolic link \"%s\"", realfile);
			goto cleanup;
		}
		symblink[size] = '\0';
	} else
		size = 0;
	nlink = ((unsigned long long)st->st_nlink>maxnlink ?
			maxnlink : st->st_nlink);
	if (fmttype & TYP_NCPIO) {
		long	size1;
		if (fmttype & TYP_SCO && size >= mag_sco) {
			char	*ofile = file;
			size1 = mag_sco;
			fsz += 22;
			file = smalloc(fsz + 1);
			snprintf(file, fsz + 1, "%s%csize=%016llx",
					ofile, 0, size);
			free(ofile);
		} else
			size1 = size;
		pad = 4;
		sprintf(bc.data, "%*.*s%08lx%08lx%08lx%08lx%08lx%08lx"
				"%08lx%08lx%08lx%08lx%08lx%08lx%08lx",
			(int)(fmttype&TYP_CRC? sizeof mag_crc:sizeof mag_asc),
			(int)(fmttype&TYP_CRC? sizeof mag_crc:sizeof mag_asc),
			fmttype & TYP_CRC ? mag_crc : mag_asc,
			(long)ino & ECUT,
			(long)st->st_mode & ECUT,
			(long)st->st_uid & ECUT,
			(long)st->st_gid & ECUT,
			(long)nlink & ECUT,
			(long)st->st_mtime & ECUT,
			(long)size1 & ECUT,
			(long)major(dev) & ECUT,
			(long)minor(dev) & ECUT,
			(long)major(st->st_rdev) & ECUT,
			(long)minor(st->st_rdev) & ECUT,
			(long)++fsz,
			0L);
		hsz = SIZEOF_Exp_cpio_hdr;
		if ((psz = (hsz + fsz) % pad) != 0)
			psz = pad - psz;
	} else if (fmttype == FMT_ODC) {
		pad = 1;
		sprintf(bc.data, "%*.*s%06lo%06lo%06lo%06lo%06lo%06lo%06lo"
				"%011lo%06lo%011lo",
			(int)sizeof mag_odc, (int)sizeof mag_odc, mag_odc,
			(long)dev & OCUT,
			(long)ino & OCUT,
			(long)st->st_mode & OCUT,
			(long)st->st_uid & OCUT,
			(long)st->st_gid & OCUT,
			(long)nlink & OCUT,
			(long)st->st_rdev & OCUT,
			(long)st->st_mtime,
			(long)++fsz,
			(long)size);
		hsz = SIZEOF_c_hdr;
		if ((psz = (hsz + fsz) % pad) != 0)
			psz = pad - psz;
	} else if (fmttype == FMT_DEC) {
		pad = 1;
		sprintf(bc.data, "%*.*s%06lo%06lo%06lo%06lo%06lo%06lo"
				"%08lo%08lo%011lo%06lo%011lo",
			(int)sizeof mag_odc, (int)sizeof mag_odc, mag_odc,
			(long)dev & OCUT,
			(long)ino & OCUT,
			(long)st->st_mode & OCUT,
			(long)st->st_uid & OCUT,
			(long)st->st_gid & OCUT,
			(long)nlink & OCUT,
			(long)major(st->st_rdev) & 077777777,
			(long)minor(st->st_rdev) & 077777777,
			(long)st->st_mtime,
			(long)++fsz,
			(long)size);
		hsz = SIZEOF_d_hdr;
		if ((psz = (hsz + fsz) % pad) != 0)
			psz = pad - psz;
	} else if (fmttype & TYP_BINARY) {
		/*
		 * To avoid gcc's stupid 'comparison is always false due to
		 * limited range of data type' warning.
		 */
		unsigned long long	gcccrap;
		pad = 2;
		if (fmttype & TYP_BE) {
			be16p(mag_bin, bc.Hdr.c_magic);
			be16p(dev, bc.Hdr.c_dev);
			be16p(ino, bc.Hdr.c_ino);
			be16p(st->st_mode, bc.Hdr.c_mode);
			be16p(st->st_uid, bc.Hdr.c_uid);
			be16p(st->st_gid, bc.Hdr.c_gid);
			be16p(nlink, bc.Hdr.c_nlink);
			be16p(st->st_rdev, bc.Hdr.c_rdev);
			be32p(st->st_mtime, bc.Hdr.c_mtime);
			be16p(++fsz, bc.Hdr.c_namesize);
		} else {
			le16p(mag_bin, bc.Hdr.c_magic);
			le16p(dev, bc.Hdr.c_dev);
			le16p(ino, bc.Hdr.c_ino);
			le16p(st->st_mode, bc.Hdr.c_mode);
			le16p(st->st_uid, bc.Hdr.c_uid);
			le16p(st->st_gid, bc.Hdr.c_gid);
			le16p(nlink, bc.Hdr.c_nlink);
			le16p(st->st_rdev, bc.Hdr.c_rdev);
			me32p(st->st_mtime, bc.Hdr.c_mtime);
			le16p(++fsz, bc.Hdr.c_namesize);
		}
		if (fmttype & TYP_SGI && size > 0x7FFFFFFFLL) {
			Krest = size & 0x7FFFFFFFLL;
			Kbase = size - Krest;
			Ksize = 0x100000000LL - (Kbase >> 31);
			if (fmttype & TYP_BE)
				be32p(Ksize, bc.Hdr.c_filesize);
			else
				me32p(Ksize, bc.Hdr.c_filesize);
			K2hdr = bc.Hdr;
			if (fmttype & TYP_BE)
				be32p(Krest, K2hdr.c_filesize);
			else
				me32p(Krest, K2hdr.c_filesize);
		} else {
			if (fmttype & TYP_BE)
				be32p(size, bc.Hdr.c_filesize);
			else
				me32p(size, bc.Hdr.c_filesize);
		}
		if (fmttype & TYP_SGI &&
				(((st->st_mode&S_IFMT) == S_IFBLK ||
				 (st->st_mode&S_IFMT) == S_IFCHR) &&
				((unsigned long long)major(st->st_rdev)>0xFF ||
				 (unsigned long long)minor(st->st_rdev)>0xFF) ||
				(gcccrap = st->st_rdev) > 0177777)) {
			uint32_t	rdev;
			rdev = (minor(st->st_rdev) & 0x0003FFFF) +
				((major(st->st_rdev)<<18) & 0xFFFC0000);
			if (fmttype & TYP_BE) {
				be16p(0xFFFF, bc.Hdr.c_rdev);
				be32p(rdev, bc.Hdr.c_filesize);
			} else {
				le16p(0xFFFF, bc.Hdr.c_rdev);
				me32p(rdev, bc.Hdr.c_filesize);
			}
		}
		hsz = SIZEOF_hdr_cpio;
		psz = (hsz + fsz) % 2;
	} else if (fmttype & TYP_CRAY) {
		int	diff5 = fmttype==FMT_CRAY5 ? CRAY_PARAMSZ : 0;
		mode_t	mo;
		pad = 1;
		be64p(mag_bin, bc.Crayhdr.C_magic);
		be64p(dev, bc.Crayhdr.C_dev);
		be64p(ino, bc.Crayhdr.C_ino);
		if ((st->st_mode&S_IFMT) == S_IFLNK)	/* non-standard */
			mo = st->st_mode&07777|0130000;	/* S_IFLNK on Cray */
		else
			mo = st->st_mode;
		be64p(mo, bc.Crayhdr.C_mode);
		be64p(st->st_uid, bc.Crayhdr.C_uid);
		be64p(st->st_gid, bc.Crayhdr.C_gid);
		be64p(nlink, bc.Crayhdr.C_nlink);
		be64p(st->st_rdev, bc.Crayhdr.C_rdev);
		be64p(st->st_mtime, bc.Crayhdr.C_mtime - diff5);
		be64p(++fsz, bc.Crayhdr.C_namesize - diff5);
		be64p(size, bc.Crayhdr.C_filesize - diff5);
		hsz = SIZEOF_cray_hdr - diff5;
		psz = 0;
	} else if (fmttype & TYP_BAR) {
		int	c, n = 0;
		pad = 512;
		sprintf(bc.Bdr.b_mode, "%7.7o",(int)st->st_mode&(07777|S_IFMT));
		sprintf(bc.Bdr.b_uid, "%7.7lo", (long)st->st_uid);
		sprintf(bc.Bdr.b_gid, "%7.7lo", (long)st->st_gid);
		sprintf(bc.Bdr.b_size, "%11.11llo",
				(st->st_mode&S_IFMT) == S_IFREG && !zerolink ?
				(long long)st->st_size&077777777777LL : 0LL);
		sprintf(bc.Bdr.b_mtime, "%11.11lo", (long)st->st_mtime);
		sprintf(bc.Bdr.b_rdev, "%7.7lo", (long)st->st_rdev);
		strcpy(&bc.data[SIZEOF_bar_header], file);
		c = tlflag(st);
		if (zerolink == 0) {
			bc.Bdr.b_linkflag = c;
			if (c == '2') {
				strncpy(&bc.data[SIZEOF_bar_header+fsz+1],
						symblink,
						512-SIZEOF_bar_header-fsz);
				n = size;
			}
		} else {
			bc.Bdr.b_linkflag = '1';
			strncpy(&bc.data[SIZEOF_bar_header+fsz+1], linkname,
					512-SIZEOF_bar_header-fsz-1);
			n = strlen(linkname);
		}
		if (n > 512-SIZEOF_bar_header-fsz-1) {
			msg(3, 0, "%s: linked name too long\n", realfile);
			goto cleanup;
		}
		bchksum(&bc);
		hsz = 512;
		psz = 0;
		fsz = 0;
	} else if (fmttype & TYP_TAR) {
		const char	*cp;
		int	c;
		/*
		 * Many SVR4 cpio derivatives expect the mode field
		 * to contain S_IFMT bits. The meaning of these bits
		 * in the mode field of the ustar header is left
		 * unspecified by IEEE Std 1003.1, 1996, 10.1.1.
		 */
		int	mmask = fmttype == FMT_USTAR || fmttype == FMT_PAX ?
				07777 : 07777|S_IFMT;

		paxrec = globrec;
		pad = 512;
		if (tmkname(&bc.Tdr, file) != 0)
			goto cleanup;
		sprintf(bc.Tdr.t_mode, "%7.7o", (int)st->st_mode & mmask);
		if (fmttype == FMT_GNUTAR && st->st_uid > 07777777) {
			be64p(st->st_uid, bc.Tdr.t_uid);
			bc.Tdr.t_uid[0] |= 0200;
		} else {
			sprintf(bc.Tdr.t_uid, "%7.7lo",
					(long)st->st_uid&07777777);
			if (fmttype & TYP_PAX && st->st_uid > 07777777)
				paxrec |= PR_UID;
		}
		if (fmttype == FMT_GNUTAR && st->st_gid > 07777777) {
			be64p(st->st_gid, bc.Tdr.t_gid);
			bc.Tdr.t_gid[0] |= 0200;
		} else {
			sprintf(bc.Tdr.t_gid, "%7.7lo",
					(long)st->st_gid&07777777);
			if (fmttype & TYP_PAX && st->st_gid > 07777777)
				paxrec |= PR_GID;
		}
		if (fmttype == FMT_GNUTAR && (st->st_mode&S_IFMT) == S_IFREG &&
				st->st_size > 077777777777LL && !zerolink) {
			bc.Tdr.t_size[0] = '\200';
			be64p(st->st_size, &bc.Tdr.t_size[4]);
		} else {
			sprintf(bc.Tdr.t_size, "%11.11llo",
				(st->st_mode&S_IFMT) == S_IFREG &&
				(!zerolink || fmttype == FMT_PAX &&
				 	pax_oflag & PO_LINKDATA) ?
				(long long)st->st_size&077777777777LL : 0LL);
			if (fmttype & TYP_PAX &&
					(st->st_mode&S_IFMT) == S_IFREG &&
					st->st_size > 077777777777LL &&
					(!zerolink || fmttype == FMT_PAX &&
					 	pax_oflag & PO_LINKDATA))
				paxrec |= PR_SIZE;
		}
		sprintf(bc.Tdr.t_mtime, "%11.11lo", (long)st->st_mtime);
		if ((c = tlflag(st)) < 0) {
			if ((st->st_mode&S_IFMT) != S_IFDIR) {
				msg(2, 0, "%s is not a file. Not dumped\n",
						realfile);
				errcnt++;
			} else
				failure = 0;
			goto cleanup;
		}
		if (zerolink == 0) {
			bc.Tdr.t_linkflag = c;
			if (c == '2') {
				if (tmklink(&bc.Tdr, symblink) != 0)
					goto cleanup;
			}
		} else {
			bc.Tdr.t_linkflag = '1';
			if (tmklink(&bc.Tdr, linkname) != 0)
				goto cleanup;
		}
		if (fmttype & TYP_USTAR) {
			if (fmttype == FMT_GNUTAR)
				strcpy(bc.Tdr.t_magic, mag_gnutar);
			else {
				strcpy(bc.Tdr.t_magic, mag_ustar);
				bc.Tdr.t_version[0] = bc.Tdr.t_version[1] = '0';
			}
			if ((cp = getuser(st->st_uid)) != NULL)
				sprintf(bc.Tdr.t_uname, "%.31s", cp);
			if ((cp = getgroup(st->st_gid)) != NULL)
				sprintf(bc.Tdr.t_gname, "%.31s", cp);
			else
				msg(1, 0, "could not get group information "
						"for %s\n", realfile);
			if (fmttype == FMT_GNUTAR &&
					(unsigned long long)major(st->st_rdev)
					> 077777777) {
				be64p(major(st->st_rdev), bc.Tdr.t_devmajor);
				bc.Tdr.t_devmajor[0] |= 0200;
			} else {
				if (fmttype == FMT_SUN &&
					(unsigned long long)major(st->st_rdev)
						> 077777777 &&
						((st->st_mode&S_IFMT)==S_IFBLK||
						 (st->st_mode&S_IFMT)==S_IFCHR))
					paxrec |= PR_SUN_DEVMAJOR;
				sprintf(bc.Tdr.t_devmajor, "%7.7o",
					(int)major(st->st_rdev)&07777777);
			}
			if (fmttype == FMT_GNUTAR &&
					(unsigned long long)minor(st->st_rdev)
					> 077777777) {
				be64p(minor(st->st_rdev), bc.Tdr.t_devminor);
				bc.Tdr.t_devminor[0] |= 0200;
			} else {
				if (fmttype == FMT_SUN &&
					(unsigned long long)minor(st->st_rdev)
						> 077777777 &&
						((st->st_mode&S_IFMT)==S_IFBLK||
						 (st->st_mode&S_IFMT)==S_IFCHR))
					paxrec |= PR_SUN_DEVMINOR;
				sprintf(bc.Tdr.t_devminor, "%7.7o",
					(int)minor(st->st_rdev)&07777777);
			}
		}
		tchksum(&bc);
		hsz = 512;
		psz = 0;
		fsz = 0;
	} else if (fmttype == FMT_ZIP) {
		pad = 1;
		memcpy(bc.Zdr.z_signature, mag_zipsig, sizeof mag_zipsig);
		bc.Zdr.z_version[0] = 10;
		mkdostime(st->st_mtime, bc.Zdr.z_modtime, bc.Zdr.z_moddate);
		if ((st->st_mode&S_IFMT) == S_IFREG ||
				(st->st_mode&S_IFMT) == S_IFLNK) {
			le32p(size, bc.Zdr.z_csize);
			le32p(size, bc.Zdr.z_nsize);
			csize = size;
		}
		le16p(fsz, bc.Zdr.z_namelen);
		le16p(SIZEOF_zextra_cp, bc.Zdr.z_extralen);
		hsz = SIZEOF_zip_header;
		psz = 0;
	} else
		abort();
	/*
	 * Start writing the file to the archive.
	 */
	if ((st->st_mode&S_IFMT) == S_IFREG && st->st_size != 0 &&
			(zerolink == 0 || fmttype == FMT_PAX &&
			 	pax_oflag & PO_LINKDATA)) {
		char	*buf;
		size_t	bufsize;
		int	readerr = 0;

		if ((fd = open(realfile, O_RDONLY)) < 0) {
			if (sysv3 < 0)
				msg(0, 0, "< %s > ?\n", realfile);
			else if (sysv3 > 0)
				fprintf(stderr, "<%s> ?\n", realfile);
			else
				msg(0, 0, "\"%s\" ?\n", realfile);
			goto cleanup;
		}
		if (fmttype == FMT_ZIP) {
			if (zipwrite(fd, file, st, &bc, fsz, dev, ino,
						&crc, &csize) < 0)
				goto cleanup2;
			goto done;
		}
		if (fmttype & TYP_CRC)
			if (sum(fd, realfile, st, bc.Edr.E_chksum) < 0)
				goto cleanup2;
		if (fmttype & TYP_PAX && paxrec != PR_NONE)
			wrpax(file, symblink?symblink:linkname, st);
		bwrite(bc.data, hsz);
		if (fsz)
			bwrite(file, fsz);
		if (psz)
			bwrite(&bc.data[hsz], psz);
		if (Kbase)
			remsz = Kbase;
		else
			remsz = st->st_size;
		getbuf(&buf, &bufsize, st->st_blksize);
	again:	while (remsz > 0) {
			if (fd < 0 || (rsz = read(fd, &buf[wsz],
							bufsize - wsz)) < 0) {
				if (readerr == 0) {
					emsg(3, "Cannot read \"%s\"", realfile);
					if (fd >= 0)
						errcnt++;
					readerr = 1;
				}
				if (fd >= 0 && lseek(fd, bufsize - wsz,
							SEEK_CUR) < 0) {
					close(fd);
					fd = -1;
				}
				rsz = bufsize - wsz;
				if (rsz > remsz)
					rsz = remsz;
				memset(&buf[wsz], 0, rsz);
			}
			if (rsz > remsz)
				rsz = remsz;
			wsz += rsz;
			remsz -= rsz;
			bwrite(buf, wsz);
			memset(buf, 0, wsz);
			size = wsz;
			wsz = 0;
		}
		wsz = size;
		if (Kbase) {
			if ((i = Ksize % pad) != 0)
				bwrite(&bc.data[hsz], i);
			bwrite((char *)&K2hdr, hsz);
			if (fsz)
				bwrite(file, fsz);
			if (psz)
				bwrite(&bc.data[hsz], psz);
			remsz = Krest;
			Kbase = 0;
			wsz = 0;
			goto again;
		} else if (Ksize)
			wsz = Krest;
	} else if ((fmttype == FMT_ZIP || fmttype & TYP_CPIO) &&
			(st->st_mode&S_IFMT) == S_IFLNK) {
		wsz = size;
		if (fmttype == FMT_ZIP) {
			crc = zipcrc(0, (unsigned char *)symblink, wsz);
			le32p(crc, bc.Zdr.z_crc32);
			bwrite(bc.data, SIZEOF_zip_header);
			bwrite(file, fsz);
			zipwxtra(file, st, dev, ino);
			bwrite(symblink, wsz);
		} else {
			bwrite(bc.data, hsz);
			if (fsz)
				bwrite(file, fsz);
			if (psz)
				bwrite(&bc.data[hsz], psz);
			bwrite(symblink, wsz);
		}
	} else {
		if (fmttype & TYP_PAX && paxrec != PR_NONE)
			wrpax(file, symblink?symblink:linkname, st);
		bwrite(bc.data, hsz);
		if (fsz)
			bwrite(file, fsz);
		if (psz)
			bwrite(&bc.data[hsz], psz);
		if (fmttype == FMT_ZIP)
			zipwxtra(file, st, dev, ino);
	}
done:	if (fmttype == FMT_ZIP) {
		zipdefer(file, st, relative, crc, csize, &bc.Zdr);
	}
	if ((i = wsz % pad) != 0)
		bwrite(&bc.data[hsz], pad - i);
	if (vflag && strcmp(file, trailer))
		fprintf(stderr, "%s\n", file);
	else if (Vflag)
		prdot(0);
	failure = 0;
cleanup2:
	if ((st->st_mode&S_IFMT) == S_IFREG) {
		if (fd >= 0)
			close(fd);
		if (aflag)
			errcnt += rstime(realfile, st, "access");
	}
cleanup:
	free(file);
	free(symblink);
	return failure;
}

/*
 * Flush a SVR4 cpio format inode tree for -o.
 */
static void
iflush(struct islot *ip, uint32_t dev)
{
	if (ip == inull)
		return;
	iflush(ip->i_lln, dev);
	iflush(ip->i_rln, dev);
	if (ip->i_nlk > 0 && ip->i_st) {
		struct ilink *il;

		for (il = ip->i_lnk; il->l_nxt; il = il->l_nxt)
			errcnt += addfile(il->l_nam, ip->i_st,
					dev, ip->i_fino, 1, 0);
		errcnt += addfile(il->l_nam, ip->i_st, dev, ip->i_fino, 0, 0);
	}
}

/*
 * Flush the SVR4 cpio link forest for -o.
 */
static void
lflush(void)
{
	struct dslot *ds;

	for (ds = devices; ds; ds = ds->d_nxt)
		iflush(ds->d_isl, ds->d_fake);
}

int
setfmt(char *s)
{
	int	i, j;

	struct {
		const char	*ucs;
		const char	*lcs;
		int	type;
		int	bits;
	} fs[] = {
		{ "NEWC",	"newc",		FMT_ASC,	00 },
		{ "SCO",	"sco",		FMT_SCOASC,	00 },
		{ "CRC",	"crc",		FMT_CRC,	00 },
		{ "SCOCRC",	"scocrc",	FMT_SCOCRC,	00 },
		{ "ODC",	"odc",		FMT_ODC,	00 },
		{ "DEC",	"dec",		FMT_DEC,	00 },
		{ "BIN",	"bin",		FMT_NONE,	00 },
		{ "BBS",	"bbs",		TYP_BE,		00 },
		{ "SGI",	"sgi",		FMT_SGIBE,	00 },
		{ "CRAY",	"cray",		FMT_CRAY,	00 },
		{ "CRAY5",	"cray5",	FMT_CRAY5,	00 },
		{ "TAR",	"tar",		FMT_TAR,	00 },
		{ "USTAR",	"ustar",	FMT_USTAR,	00 },
		{ "PAX:",	"pax:",		FMT_PAX,	00 },
		{ "SUN",	"sun",		FMT_SUN,	00 },
		{ "GNU",	"gnu",		FMT_GNUTAR,	00 },
		{ "OTAR",	"otar",		FMT_OTAR,	00 },
		{ "BAR",	"bar",		FMT_BAR,	00 },
		{ "ZIP:",	"zip:",		FMT_ZIP,	00 },
		{ NULL,		NULL,		0,		00 }
	};
	for (i = 0; fs[i].ucs; i++) {
		for (j = 0; s[j] &&
				(s[j] == fs[i].ucs[j] || s[j] == fs[i].lcs[j]);
				j++)
			if (fs[i].ucs[j] == ':')
				break;
		if (s[j] == '\0' &&
				(fs[i].ucs[j] == '\0' || fs[i].ucs[j] == ':') ||
				s[j] == ':' && fs[i].ucs[j] == ':') {
			fmttype = fs[i].type;
			if (fmttype == FMT_ZIP && s[j] == ':') {
#if	USE_ZLIB
				if (strcmp(&s[j+1], "en") == 0)
					zipclevel = 00;
				else if (strcmp(&s[j+1], "ex") == 0)
					zipclevel = 01;
				else if (strcmp(&s[j+1], "ef") == 0)
					zipclevel = 02;
				else if (strcmp(&s[j+1], "es") == 0)
					zipclevel = 03;
				else
#endif	/* USE_ZLIB */
				if (strcmp(&s[j+1], "e0") == 0)
					zipclevel = 04;
				else
#if	USE_BZLIB
				if (strcmp(&s[j+1], "bz2") == 0)
					zipclevel = 07;
				else
#endif	/* USE_BZLIB */
					continue;
			} else if (fmttype == FMT_NONE)
				fmttype = bigendian() ? FMT_BINBE : FMT_BINLE;
			else if (fmttype == TYP_BE)
				fmttype = bigendian() ? FMT_BINLE : FMT_BINBE;
			else if (fmttype == FMT_PAX && s[j] == ':') {
				if (pax_options(&s[j+1], 0) < 0)
					continue;
			}
			return 0;
		}
	}
	msg(3, 0, "Invalid header \"%s\" specified.\n", s);
	return -1;
}

static int
bigendian(void)
{
	union {
		char	u_c[2];
		int16_t	u_i;
	} u;
	u.u_i = 1;
	return u.u_c[1] == 1;
}

int
setreassign(const char *s)
{
	struct passwd	*pwd;
	int	val = 0;

	if (myuid != 0) {
		msg(3, 0, "R option only valid for super-user.\n");
		val = -1;
	}
	if ((pwd = getpwnam(s)) == NULL) {
		msg(3, 0, "\"%s\" is not a valid user id\n", s);
		val = -1;
	} else {
		Ruid = pwd->pw_uid;
		Rgid = pwd->pw_gid;
	}
	return val;
}

void *
srealloc(void *m, size_t n)
{
	if ((m = realloc(m, n)) == NULL) {
		write(2, "Out of memory.\n", 15);
		_exit(sysv3 ? 2 : 3);
	}
	return m;
}

void *
smalloc(size_t n)
{
	return srealloc(NULL, n);
}

void *
scalloc(size_t nmemb, size_t size)
{
	void	*vp;

	if ((vp = calloc(nmemb, size)) == NULL) {
		write(2, "Out of memory.\n", 15);
		_exit(sysv3 ? 2 : 3);
	}
	return vp;
}

void *
svalloc(size_t n, int force)
{
	static long	pagesize;
	void	*vp;

	if (pagesize == 0)
		if ((pagesize = sysconf(_SC_PAGESIZE)) < 0)
			pagesize = 4096;
	if ((vp = memalign(pagesize, n)) == NULL && force) {
		write(2, "Out of memory.\n", 15);
		_exit(sysv3 ? 2 : 3);
	}
	return vp;
}

/*
 * A single static buffer is used for intermediate copying from file
 * data to the tape buffer and vice versa, for creating checksums, and
 * for data transfer with -p.
 */
static void
getbuf(char **bufp, size_t *sizep, size_t best)
{
	static char	*buf;
	static size_t	size;

	if (size != best) {
		if (buf)
			free(buf);
		size = best;
		if (size == 0 || (buf = svalloc(size, 0)) == NULL)
			buf = svalloc(size = 512, 1);
	}
	*bufp = buf;
	*sizep = size;
}

static void
sevprnt(int sev)
{
	if (printsev) switch (sev) {
	case 1:
		fprintf(stderr, "INFORM: ");
		break;
	case 2:
		fprintf(stderr, "WARNING: ");
		break;
	case 3:
		fprintf(stderr, "ERROR: ");
		break;
	case 4:
		fprintf(stderr, "HALT: ");
		break;
	}
}

void
msg(int sev, int err, const char *fmt, ...)
{
	va_list	ap;

	/*
	 * The error message should appear near the file it refers to.
	 */
	if (tflag)
		fflush(stdout);
	else if (Vflag)
		prdot(1);
	if (sysv3 >= 0 && sev >= (printsev ? 0 : -1))
		fprintf(stderr, "%s: ", progname);
	sevprnt(sev);
	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
	if (err > 0)
		done(err);
	else if (err == -2) {
		if (sysv3)
			done(1);
		usage();
	}
}

void
emsg(int sev, const char *fmt, ...)
{
	char	_fmt[60];
	int	i, fl = sev & 030, n;
	va_list	ap;

	sev &= ~030;
	i = errno;
	if (tflag)
		fflush(stdout);
	else if (Vflag)
		prdot(1);
	fprintf(stderr, "%s: ", progname);
	sevprnt(sev);
	va_start(ap, fmt);
	if (sysv3) {
		if (fmt[(n=strlen(fmt))-1] == '"' && fmt[n-2] == 's' &&
				fmt[n-3] == '%' && fmt[n-4] == '"' &&
				n < sizeof _fmt) {
			strcpy(_fmt, fmt);
			_fmt[n-1] = '>';
			_fmt[n-4] = '<';
			fmt = _fmt;
		}
	}
	vfprintf(stderr, fmt, ap);
	va_end(ap);
	if (sysv3 > 0 && sev >= 0 && fl & 010)
		fprintf(stderr, "\n\t%s\n", strerror(i));
	else if (sysv3 < 0) {
		if (fl & 020)
			putc('\n', stderr);
		else
			fprintf(stderr, " (errno:%d)\n", i);
	} else
		fprintf(stderr, ", errno %d, %s\n", i, strerror(i));
}

static void
prdot(int flush)
{
	static int	column;

	if (flush && column != 0 || column >= 50) {
		write(action == 'o' && !Oflag ? 2 : 1, "\n", 1);
		column = 0;
	}
	if (!flush) {
		write(action == 'o' && !Oflag ? 2 : 1, ".", 1);
		column++;
	}
}

/*
 * Ask the user for new media if applicable, or exit.
 */
static void
newmedia(int err)
{
	static int	mediacnt = 1;
	static char answer[PATH_MAX+1];
	const char	*mesf = action == 'i' ?
		(Iflag && !sysv3 ? Iflag : "input") :
		(Oflag && !sysv3 ? Oflag : "output");
	char c;
	int i, j;

	if (mfl == 0 && close(mt) < 0) {
		emsg(3, "Close error on \"%s\"", mesf);
		errcnt++;
	}
	mfl = -1;
	if ((mtst.st_mode&S_IFMT)!=S_IFCHR && (mtst.st_mode&S_IFMT)!=S_IFBLK ||
			Dflag) {
		if (action == 'o') {
			switch (err) {
			case 0:
				break;
			case EFBIG:
				msg(3, 0, "ulimit reached for output file.\n");
				break;
			case ENOSPC:
				msg(3, 0, "No space left for output file.\n");
				break;
			default:
				msg(3, 0, "I/O error - cannot continue, "
						"errno %d, %s\n",
						err, strerror(err));
			}
		}
		return;
	}
	if (err == ENOSPC || err == ENXIO || err == 0)
		msg(-2, 0, sysv3 ? "Reached end of medium on %s.\a\n" :
				"End of medium on \"%s\".\a\n", mesf);
	else
		msg(3, 0, "I/O error on \"%s\", errno %d, %s\a\n", mesf,
				err, strerror(err));
	mediacnt++;
	while (mfl < 0) {
		if (Iflag || Oflag)
			msg(-2, 0, Mflag ? Mflag :
					"Change to part %d and press "
					"RETURN key. [q] ", mediacnt);
		else
			msg(-2, 0, sysv3 ? "If you want to go on, "
					"type device/file name when ready.\n" :
					"To continue, type device/file name "
					"when ready.\n");
		if (tty == 0)
			if ((tty = open("/dev/tty", O_RDWR)) < 0 ||
					fcntl(tty, F_SETFD, FD_CLOEXEC) < 0) {
			cantrt:	errcnt++;
				msg(4, 1, "Cannot read tty.\n");
			}
		i = 0;
		while ((j = read(tty, &c, 1)) == 1 && c != '\n')
			if (i < sizeof answer - 1)
				answer[i++] = c;
		if (j != 1)
			goto cantrt;
		answer[i] = 0;
		if (Iflag || Oflag) {
			if (answer[0] == '\0')
				snprintf(answer, sizeof answer, Iflag ? Iflag :
						Oflag);
			else if (answer[0] == 'q')
				exit(errcnt != 0 ? sysv3 ? 1 : 2 : 0);
			else if (Iflag)
				Iflag = sstrdup(answer);
			else if (Oflag)
				Oflag = sstrdup(answer);
		} else if (answer[0] == '\0')
			return;
		if ((mt = action == 'i' ? open(answer, O_RDONLY) :
					creat(answer, 0666)) < 0) {
			if (sysv3)
				msg(-2, 0, "That didn't work, "
						"cannot open \"%s\"\n%s\n",
						answer, strerror(errno));
			else
				emsg(3, "Cannot open \"%s\"", answer);
		}
		else
			mfl = 0;
	}
	mstat();
}

static void
mclose(void)
{
	if (action == 'o' && mt >= 0 && close(mt) < 0) {
		emsg(3, "Close error on \"%s\"",
				Oflag && !sysv3 ? Oflag : "output");
		errcnt++;
	}
}

/*
 * Write the archive buffer to tape.
 */
static ssize_t
mwrite(int max)
{
	ssize_t wo, wt = 0;

	do {
		if ((wo = write(mt, blkbuf + wt, (max?max:blksiz) - wt)) < 0) {
			if (errno == EINTR) {
				continue;
			} else {
				newmedia(errno);
				if (mfl == 0) {
					if (fmttype & TYP_BAR)
						dump_barhdr();
					continue;
				}
				else
					done(1);
			}
		}
		poffs += wo;
		wt += wo;
	} while (wt < (max?max:blksiz));
	blocks += wt >> 9;
	bytes += wt & 0777;
	return wt;
}

/*
 * Buffered writes to tape.
 */
static void
bwrite(const char *data, size_t sz)
{
	size_t di;

	nwritten += sz;
	while (curpos + sz > blksiz) {
		di = blksiz - curpos;
		sz -= di;
		memcpy(&blkbuf[curpos], data, di);
		mwrite(0);
		data += di;
		curpos = 0;
	}
	memcpy(&blkbuf[curpos], data, sz);
	curpos += sz;
}

/*
 * Flush the tape write buffer.
 */
static void
bflush(void)
{
	if (curpos) {
		memset(&blkbuf[curpos], 0, blksiz - curpos);
		mwrite(fmttype==FMT_ZIP && (mtst.st_mode&S_IFMT) == S_IFREG ?
			curpos : 0);
	}
	curpos = 0;
}

/*
 * CRC format checksum calculation with -i.
 */
static int
sum(int fd, const char *fn, struct stat *sp, char *tg)
{
	char	*buf;
	size_t	bufsize;
	uint32_t	size = sp->st_size, sum = 0;
	ssize_t	rd;
	char	c;

	getbuf(&buf, &bufsize, sp->st_blksize);
	/*
	 * Unfortunately, SVR4 cpio derivatives (as on Solaris 8 and
	 * UnixWare 2.1) compute the checksum of signed char values,
	 * whereas GNU cpio and the pax implementations of AT&T and
	 * BSD use unsigned chars. Since there is no 'open' standard
	 * for the SVR4 CRC format, the SVR4 implementation should be
	 * taken as a de facto reference and we thus create SVR4 type
	 * checksums.
	 */
	while ((rd = read(fd, buf, size>bufsize ? bufsize : size )) > 0) {
		size -= rd;
		do
			sum += ((signed char *)buf)[--rd];
		while (rd);
	}
	if (rd < 0) {
		msg(3, 0, "Error computing checksum\n");
		return 1;
	}
	if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
		emsg(3, "Cannot reset file after checksum");
		return 1;
	}
	c = tg[8];
	sprintf(tg, "%08lx", (long)sum);
	tg[8] = c;
	return 0;
}

static int
rstime(const char *fn, struct stat *st, const char *which)
{
	struct utimbuf	utb;

	utb.actime = st->st_atime;
	utb.modtime = st->st_mtime;
	if (pax != PAX_TYPE_CPIO &&
			(pax_preserve&(PAX_P_ATIME|PAX_P_MTIME)) != 0 &&
			(pax_preserve&PAX_P_EVERY) == 0) {
		struct stat	xst;
		if (stat(fn, &xst) < 0)
			goto fail;
		if (pax_preserve&PAX_P_ATIME)
			utb.actime = xst.st_atime;
		if (pax_preserve&PAX_P_MTIME)
			utb.modtime = xst.st_mtime;
	}
	if (utime(fn, &utb) < 0) {
	fail:	emsg(2, "Unable to reset %s time for \"%s\"", which, fn);
		return 1;
	}
	return 0;
}

/*
 * Top-down splay function for inode tree.
 */
static struct islot *
isplay(ino_t ino, struct islot *x)
{
	struct islot	hdr;
	struct islot	*leftmax, *rightmin;
	struct islot	*y;

	hdr.i_lln = hdr.i_rln = inull;
	leftmax = rightmin = &hdr;
	inull->i_ino = ino;
	while (ino != x->i_ino) {
		if (ino < x->i_ino) {
			if (ino < x->i_lln->i_ino) {
				y = x->i_lln;
				x->i_lln = y->i_rln;
				y->i_rln = x;
				x = y;
			}
			if (x->i_lln == inull)
				break;
			rightmin->i_lln = x;
			rightmin = x;
			x = x->i_lln;
		} else {
			if (ino > x->i_rln->i_ino) {
				y = x->i_rln;
				x->i_rln = y->i_lln;
				y->i_lln = x;
				x = y;
			}
			if (x->i_rln == inull)
				break;
			leftmax->i_rln = x;
			leftmax = x;
			x = x->i_rln;
		}
	}
	leftmax->i_rln = x->i_lln;
	rightmin->i_lln = x->i_rln;
	x->i_lln = hdr.i_rln;
	x->i_rln = hdr.i_lln;
	inull->i_ino = !ino;
	return x;
}

/*
 * Find the inode number ino.
 */
static struct islot *
ifind(ino_t ino, struct islot **it)
{
	if (*it == NULL)
		return NULL;
	*it = isplay(ino, *it);
	return (*it)->i_ino == ino ? *it : NULL;
}

/*
 * Put ik into the tree.
 */
static void
iput(struct islot *ik, struct islot **it)
{
	if ((*it) == NULL) {
		ik->i_lln = ik->i_rln = inull;
		(*it) = ik;
	} else {
		/* ifind() is always called before */
		/*(*it) = isplay(ik->i_ino, (*it));*/
		if (ik->i_ino < (*it)->i_ino) {
			ik->i_lln = (*it)->i_lln;
			ik->i_rln = (*it);
			(*it)->i_lln = inull;
			(*it) = ik;
		} else if ((*it)->i_ino < ik->i_ino) {
			ik->i_rln = (*it)->i_rln;
			ik->i_lln = (*it);
			(*it)->i_rln = inull;
			(*it) = ik;
		}
	}
}

/*
 * Find the device dev or add it to the device/inode forest if not
 * already present.
 */
static struct dslot *
dfind(struct dslot **root, dev_t dev)
{
	struct dslot	*ds, *dp;

	for (ds = *root, dp = NULL; ds; dp = ds, ds = ds->d_nxt)
		if (ds->d_dev == dev)
			break;
	if (ds == NULL) {
		ds = scalloc(1, sizeof *ds);
		ds->d_dev = dev;
		if (*root == NULL)
			*root = ds;
		else
			dp->d_nxt = ds;
	}
	return ds;
}

/*
 * Exit on fatal error conditions.
 */
static void
done(int i)
{
	if (tflag)
		fflush(stdout);
	errcnt += i;
	mclose();
	if (errcnt && !sysv3)
		fprintf(stderr, "%llu errors\n", errcnt);
	exit(sysv3 ? 2 : 3);
}

static char	*pcopy, *pcend;
static size_t	psz, pslen, pss;

/*
 * Execution for -p.
 */
static void
dopass(const char *target)
{
	struct stat	st;

	if (access(target, W_OK) < 0) {
		emsg(033, sysv3 ? "cannot write in <%s>" :
				"Error during access() of \"%s\"", target);
		if (sysv3)
			done(1);
		usage();
	}
	if (stat(target, &st) < 0) {
		emsg(023, "Error during stat() of \"%s\"", target);
		done(1);
	}
	if ((st.st_mode&S_IFMT) != S_IFDIR)
		msg(3, 1, sysv3 ? "<%s> not a directory.\n" :
				"\"%s\" is not a directory\n", target);
	getpath(target, &pcopy, &pcend, &psz, &pslen);
	copyout(passfile);
}

/*
 * Callback for sfile().
 */
/*ARGSUSED*/
void
writerr(void *vp, int count, int written)
{
}

/*
 * Copy file data of regular files with -p.
 */
static int
passdata(struct file *f, const char *tgt, int tfd)
{
	char	*buf;
	size_t	bufsize;
	ssize_t	rd = 0;

	if (f->f_fd < 0)	/* is a zero-sized unreadable file */
		return 0;
#ifdef	__linux__
	if (f->f_st.st_size > 0) {
		long long	sent;

		sent = sfile(tfd, f->f_fd, f->f_st.st_mode, f->f_st.st_size);
		blocks += (sent + 0777) >> 9;
		if (sent == f->f_st.st_size)
			return 0;
		if (sent < 0)
			goto rerr;
	}
#endif	/* __linux__ */
	getbuf(&buf, &bufsize, f->f_st.st_blksize);
	while ((rd = read(f->f_fd, buf, bufsize)) > 0) {
		blocks += (rd + 0777) >> 9;
		if (write(tfd, buf, rd) != rd) {
			emsg(3, "Cannot write \"%s\"", tgt);
			return -1;
		}
	}
	if (rd < 0) {
#ifdef	__linux__
	rerr:
#endif	/* __linux__ */
		emsg(3, "Cannot read \"%s\"", f->f_name);
		return -1;
	}
	return 0;
}

/*
 * Handle a single file for -p.
 */
static int
passfile(const char *fn, struct stat *st)
{
	struct file	f;
	ssize_t	sz;
	int	val;
	char	*newfn;
	size_t	newsz = 0;

	newfn = sstrdup(fn);
	if (pax != PAX_TYPE_CPIO) {
		if (pax_sflag && pax_sname(&newfn, &newsz) == 0)
			return 0;
		if (rflag && rname(&newfn, &newsz) == 0)
			return 0;
	}
	setpath(newfn, &pcopy, &pcend, pslen, &psz, &pss);
	free(newfn);
	memset(&f, 0, sizeof f);
	f.f_name = sstrdup(fn);
	f.f_nsiz = strlen(fn) + 1;
	f.f_st = *st;
	f.f_fd = -1;
	if ((st->st_mode&S_IFMT) == S_IFLNK) {
		sz = st->st_size ? st->st_size : PATH_MAX;
		f.f_lnam = smalloc(sz+1);
		if ((sz = readlink(fn, f.f_lnam, sz+1)) < 0) {
				emsg(3, "Cannot read symbolic link \"%s\"", fn);
				free(f.f_lnam);
				return 1;
		}
		f.f_lnam[sz] = '\0';
	} else if ((st->st_mode&S_IFMT) == S_IFREG) {
		if ((f.f_fd = open(fn, O_RDONLY)) < 0) {
			if (sysv3)
				msg(2, 0, "Cannot open file \"%s\".\n", fn);
			else
				emsg(2, "Cannot open \"%s\", skipped", fn);
			if (st->st_size != 0)
				return 1;
		}
	}
	val = filein(&f, passdata, pcopy);
	if (f.f_lnam)
		free(f.f_lnam);
	free(f.f_name);
	if (f.f_fd >= 0)
		close(f.f_fd);
	if (val <= 1 && aflag && (st->st_mode&S_IFMT) == S_IFREG)
		errcnt += rstime(fn, st, "access");
	return val != 0;
}

/*
 * Processing of a single file common to -i and -p. Return value: 0 if
 * successful, 1 at failure if data was read, 2 at failure if no data
 * was read.
 */
static int
filein(struct file *f, int (*copydata)(struct file *, const char *, int),
	char *tgt)
{
	struct stat	nst;
	char	*temp = NULL;
	size_t	len;
	int	fd, i, j, new;
	int	failure = 2;

	if (fmttype == FMT_ZIP && (f->f_st.st_mode&S_IFMT) != S_IFREG &&
			(f->f_st.st_mode&S_IFMT) != S_IFLNK &&
			(f->f_csize > 0 || f->f_gflag & FG_DESC))
		skipfile(f);
	if ((new = lstat(tgt, &nst)) == 0) {
		if (action == 'p' && f->f_st.st_dev == nst.st_dev &&
				f->f_st.st_ino == nst.st_ino) {
			msg(3, 0, sysv3 ?
				"Attempt to pass file to self!\n" :
				"Attempt to pass a file to itself.\n");
			return 1;
		}
		if ((f->f_st.st_mode&S_IFMT) == S_IFDIR) {
			if ((nst.st_mode&S_IFMT) == S_IFDIR)
				return setattr(tgt, &f->f_st);
			rmdir(tgt);
		} else {
			if (pax_kflag) {
				failure = 0;
				goto skip;
			}
			if (uflag == 0 && f->f_st.st_mtime <= nst.st_mtime) {
				if (pax == PAX_TYPE_CPIO)
					msg(-1, 0, sysv3 ?
					"current <%s> newer or same age\n" :
					"Existing \"%s\" same age or newer\n",
						tgt);
				else
					failure = 0;
				goto skip;
			}
		}
	} else {
		if (imdir(tgt) < 0)
			goto skip;
	}
	if (Vflag && !vflag)
		prdot(0);
	if ((f->f_st.st_mode&S_IFMT) != S_IFDIR && lflag) {
		if (Lflag) {
			char	*symblink, *name;
			struct stat	xst;
			name = f->f_name;
			for (;;) {
				if (lstat(name, &xst) < 0) {
					emsg(3, "Cannot lstat \"%s\"", name);
					if (name != f->f_name)
						free(name);
					goto cantlink;
				}
				if ((xst.st_mode&S_IFMT) != S_IFLNK)
					break;
				i = xst.st_size ? xst.st_size : PATH_MAX;
				symblink = smalloc(i+1);
				if ((j = readlink(name, symblink, i)) < 0) {
					emsg(3, "Cannot read symbolic link "
						"\"%s\"", name);
					free(symblink);
					if (name != f->f_name)
						free(name);
					goto cantlink;
				}
				symblink[j] = '\0';
				symblink = joinpath(name, symblink);
				if (name != f->f_name)
					free(name);
				name = symblink;
			}
			if (linkunlink(name, tgt) == 0) {
				if (vflag)
					fprintf(stderr, "%s\n", tgt);
				if (name != f->f_name)
					free(name);
				return 0;
			}
			if (name != f->f_name)
				free(name);
		} else if (linkunlink(f->f_name, tgt) == 0) {
			if (vflag)
				fprintf(stderr, "%s\n", tgt);
			return 0;
		}
cantlink:	errcnt += 1;
	}
	if ((f->f_st.st_mode&S_IFMT) != S_IFDIR && f->f_st.st_nlink > 1 &&
			(fmttype & TYP_CPIO || fmttype == FMT_ZIP
			 || action == 'p') &&
			(i = canlink(tgt, &f->f_st, 1)) != 0) {
		if (i < 0)
			goto skip;
		/*
		 * At this point, hard links in SVR4 cpio format have
		 * been reordered and data is associated with the first
		 * link; remaining links have st_size == 0 so don't
		 * overwrite the data here.
		 */
		if (fmttype & TYP_NCPIO && f->f_st.st_size == 0 ||
				(f->f_st.st_mode&S_IFMT) != S_IFREG) {
			if (vflag)
				fprintf(stderr, "%s\n", f->f_name);
			return 0;
		}
		/*
		 * Make sure we can creat() this file later.
		 */
		chmod(tgt, 0600);
	} else if (fmttype & TYP_TAR && f->f_st.st_nlink > 1) {
		if (linkunlink(f->f_lnam, f->f_name) == 0) {
			if (fmttype & TYP_USTAR && f->f_st.st_size > 0)
				chmod(tgt, 0600);
			else {
				if (vflag)
					fprintf(stderr, "%s\n", f->f_name);
				return 0;
			}
		} else {
			goto restore;
		}
	} else if (new == 0 && (f->f_st.st_mode&S_IFMT) != S_IFDIR) {
		len = strlen(tgt);
		temp = smalloc(len + 7);
		strcpy(temp, tgt);
		strcpy(&temp[len], "XXXXXX");
		if ((fd = mkstemp(temp)) < 0 || close(fd) < 0) {
			emsg(3, "Cannot create temporary file");
			if (fd < 0) {
				free(temp);
				temp = NULL;
			}
			goto skip;
		}
		cur_ofile = tgt;
		cur_tfile = temp;
		if (rename(tgt, temp) < 0) {
			emsg(3, "Cannot rename current \"%s\"", tgt);
			tunlink(&temp);
			goto skip;
		}
	}
	switch (f->f_st.st_mode & S_IFMT) {
	case S_IFDIR:
		if (!dflag) {
			if (action == 'p')
				msg(-1, 0, "Use -d option to copy \"%s\"\n",
						f->f_name);
			goto restore;
		}
		if (mkdir(tgt, 0777) < 0 && errno != EEXIST) {
			emsg(-1, "Cannot create directory \"%s\"", tgt);
			goto restore;
		}
		break;
	case S_IFLNK:
		if (symlink(f->f_lnam, tgt) < 0) {
			emsg(3, "Cannot create \"%s\"", tgt);
			goto restore;
		}
		break;
	case S_IFREG:
		if (temp && f->f_fd < 0)
			goto restore;
		cur_ofile = tgt;
		if ((fd = (compressed_bar ? zcreat : creat)(tgt,
						f->f_st.st_mode & 0777)) < 0) {
			emsg(3, "Cannot create \"%s\"", tgt);
			goto skip;
		}
		failure = 1;
		if (copydata(f, tgt, fd) != 0) {
			close(fd);
			goto restore;
		}
		if ((compressed_bar ? zclose : close)(fd) < 0) {
			emsg(3, "Close error on \"%s\"", tgt);
			goto restore;
		}
		break;
	case S_IFBLK:
	case S_IFCHR:
	case S_IFIFO:
	case S_IFNAM:
	case S_IFNWK:
		if (mknod(tgt, f->f_st.st_mode&(S_IFMT|0777),
					f->f_st.st_rdev) < 0) {
			emsg(3, "Cannot mknod() \"%s\"", tgt);
			goto restore;
		}
		break;
	default:
		msg(-1, 0, "Impossible file type\n");
		goto skip;
	}
	if (vflag)
		fprintf(stderr, "%s\n", f->f_name);
	tunlink(&temp);
	return setattr(tgt, &f->f_st);
skip:	if (copydata == indata)
		skipdata(f, copydata);
restore:
	if (temp) {
		if (rename(temp, tgt) < 0) {
			emsg(3, "Cannot recover original version of \"%s\"",
					tgt);
			tunlink(&temp);
		} else
			fprintf(stderr, "Restoring existing \"%s\"\n", tgt);
		cur_tfile = cur_ofile = NULL;
	}
	return failure;
}

static int
linkunlink(const char *path1, const char *path2)
{
	int	twice = 0;

	do {
		if (link(path1, path2) == 0) {
			if (vflag && pax == PAX_TYPE_CPIO)
				printf("%s linked to %s\n", path1, path2);
			return 0;
		}
		if (errno == EEXIST && unlink(path2) < 0)
			emsg(3, sysv3 ? "cannot unlink <%s>" :
					"Error cannot unlink \"%s\"", path2);
	} while (twice++ == 0);
	emsg(023, sysv3 ? "Cannot link <%s> & <%s>" :
			"Cannot link \"%s\" and \"%s\"", path1, path2);
	return -1;
}

static void
tunlink(char **fn)
{
	cur_tfile = cur_ofile = NULL;
	if (*fn) {
		if (unlink(*fn) < 0)
			emsg(3, "Cannot unlink() temp file \"%s\"", *fn);
		free(*fn);
		*fn = NULL;
	}
}

/*
 * For -it[v] option.
 */
static int
filet(struct file *f, int (*copydata)(struct file *, const char *, int))
{
	if (vflag && (
		(fmttype == FMT_ZIP && f->f_gflag & FG_DESC &&
		(f->f_cmethod == C_DEFLATED || f->f_cmethod == C_ENHDEFLD) &&
				(f->f_gflag & FG_CRYPT) == 0) ||
		f->f_Kbase)) {
		/*
		 * Need to read zip data descriptor located after the
		 * file contents in order to know the file size. Or
		 * need to read second header of -K archive.
		 */
		int	i;
		i = skipdata(f, copydata);
		filev(f);
		return i;
	}
	if (vflag)
		filev(f);
	else
		puts(f->f_name);
	return skipdata(f, copydata);
}

static void
filev(struct file *f)
{
	const char	*cp;
	long	c;

	if (pax == PAX_TYPE_CPIO && fmttype & TYP_TAR && f->f_st.st_nlink > 1)
		printf("%s linked to %s\n", f->f_lnam, f->f_name);
	if (sysv3)
		printf("%-6o", (int)f->f_st.st_mode&(07777|S_IFMT));
	else {
		c = typec(&f->f_st);
		putchar(c);
		permbits(f->f_st.st_mode);
	}
	if (fmttype == FMT_ZIP && vflag > 1)
		zipinfo(f);
	else {
		if (sysv3 == 0)
			printf(" %3d", fmttype&TYP_TAR &&
					(f->f_st.st_mode&S_IFMT) == S_IFLNK ?
					2 : (int)f->f_st.st_nlink);
		if ((cp = getuser(f->f_st.st_uid)) != NULL)
			printf(sysv3 ? " %-6s" : " %-8s", cp);
		else
			printf(sysv3 ? " %-6lu" : " %-8lu",
					(long)f->f_st.st_uid);
		if (sysv3 == 0) {
			if ((cp = getgroup(f->f_st.st_gid)) != NULL)
				printf(" %-8s", cp);
			else
				printf(" %-8lu", (long)f->f_st.st_gid);
		}
	}
	if (sysv3 || (f->f_st.st_mode&S_IFMT)!=S_IFCHR &&
			(f->f_st.st_mode&S_IFMT)!=S_IFBLK &&
			(f->f_st.st_mode&S_IFMT)!=S_IFNAM &&
			(f->f_st.st_mode&S_IFMT)!=S_IFNWK)
		printf(pax != PAX_TYPE_CPIO ? "%8llu" :
				sysv3 ? "%7llu" : " %-7llu", f->f_dsize);
	else
		printf(" %3lu,%3lu", (long)f->f_rmajor, (long)f->f_rminor);
	prtime(f->f_st.st_mtime);
	printf("%s", f->f_name);
	if ((f->f_st.st_mode&S_IFMT) == S_IFLNK)
		printf(" -> %s", f->f_lnam);
	if (pax != PAX_TYPE_CPIO && (f->f_st.st_mode&S_IFMT) != S_IFDIR &&
			f->f_st.st_nlink>1) {
		if (fmttype & TYP_TAR)
			printf(" == %s", f->f_lnam);
		else
			canlink(f->f_name, &f->f_st, 0);
	}
	putchar('\n');
}

static int
typec(struct stat *st)
{
	switch (st->st_mode&S_IFMT) {
	case S_IFREG:
		return '-';
	case S_IFDIR:
		return 'd';
	case S_IFLNK:
		return 'l';
	case S_IFCHR:
		return 'c';
	case S_IFBLK:
		return 'b';
	case S_IFIFO:
		return 'p';
	case S_IFNWK:
		return 'n';
	case S_IFNAM:
		switch (st->st_rdev) {
		case S_INSEM:
			return 's';
		case S_INSHD:
			return 'm';
		}
		/*FALLTHRU*/
	default:
		msg(-1, 0, "Impossible file type\n");
		errcnt++;
		return '-';
	}
}

static void
permbits(mode_t mode)
{
	mode_t	mask = 0700, shft = 6;

	while (mask) {
		if (((mode & mask) >> shft) & 04)
			putchar('r');
		else
			putchar('-');
		if (((mode & mask) >> shft) & 02)
			putchar('w');
		else
			putchar('-');
		if (mask == 0700 && mode & 04000)
			putchar('s');
		else if (mask == 0070 && (mode & 02010) == 02010)
			putchar('s');
		else if (mask == 0070 && mode & 02000)
			putchar('l');
		else if (mask == 0007 && mode & 01000)
			putchar('t');
		else if (((mode & mask) >> shft) & 01)
			putchar('x');
		else
			putchar('-');
		mask >>= 3;
		shft -= 3;
	}
}

static void
prtime_cpio(time_t t)
{
	char	b[30];

	strftime(b, sizeof b, sysv3 ? "%b %e %H:%M:%S %Y" : "%b %e %H:%M %Y",
			localtime(&t));
	printf(sysv3 ? "  %s  " : " %s, ", b);
}

/*
 * Prepare path to add names of files below it later.
 */
static void
getpath(const char *path, char **file, char **filend, size_t *sz, size_t *slen)
{
	*sz = 14 + strlen(path) + 2;
	*file = smalloc(*sz);
	*filend = *file;
	if (path[0] == '/' && path[1] == '\0')
		*(*filend)++ = '/';
	else {
		const char	*cp = path;
		while ((*(*filend)++ = *cp++) != '\0');
		(*filend)[-1] = '/';
	}
	*slen = *filend - *file;
}

/*
 * Concatenate base (prepared with getpath()) and file.
 */
static void
setpath(const char *base, char **file, char **filend,
		size_t slen, size_t *sz, size_t *ss)
{
	if (slen + (*ss = strlen(base)) >= *sz) {
		*sz += slen + *ss + 15;
		*file = srealloc(*file, *sz);
		*filend = &(*file)[slen];
	}
	strcpy(*filend, base);
}

/*
 * Create intermediate directories with -m.
 */
static int
imdir(char *fn)
{
	struct stat	st;
	char	*cp;
	int	dfl = dflag != 0;

	for (cp = fn; *cp == '/'; cp++);
	do {
		while (*cp && *cp != '/')
			cp++;
		if (*cp == '/') {
			*cp = '\0';
			if (stat(fn, &st) < 0) {
				if (dfl) {
					if (mkdir(fn, 0777) < 0) {
						*cp = '/';
						emsg(-1, "Cannot create "
							"directory for \"%s\"",
							fn);
						return -1;
					}
				} else {
					msg(-1, 0, sysv3 ?
						"missing 'd' option\n" :
						"Missing -d option\n");
					dfl = 2;
				}
			}
			*cp = '/';
			while (*cp == '/')
				cp++;
		}
	} while (*cp);
	return 0;
}

/*
 * Set file attributes and make sure that suid/sgid bits are only restored
 * if the owner is the same as on the tape.
 */
static int
setattr(const char *fn, struct stat *st)
{
	mode_t	mode = st->st_mode & 07777;
	uid_t	uid = Rflag ? Ruid : myuid;
	gid_t	gid = Rflag ? Rgid : mygid;

	if ((pax != PAX_TYPE_CPIO || myuid == 0) &&
			(pax == PAX_TYPE_CPIO ||
			 pax_preserve&(PAX_P_OWNER|PAX_P_EVERY))) {
		if (setowner(fn, st) != 0)
			return 1;
	}
	if (pax != PAX_TYPE_CPIO &&
			(pax_preserve&(PAX_P_OWNER|PAX_P_EVERY)) == 0)
		mode &= ~(mode_t)(S_ISUID|S_ISGID);
	if (myuid != 0 || Rflag) {
		if (st->st_uid != uid || st->st_gid != gid) {
			mode &= ~(mode_t)S_ISUID;
			if ((st->st_mode&S_IFMT) != S_IFDIR && mode & 0010)
				mode &= ~(mode_t)S_ISGID;
			if ((st->st_mode&S_IFMT) == S_IFDIR && st->st_gid!=gid)
				mode &= ~(mode_t)S_ISGID;
		}
	}
	if ((st->st_mode&S_IFMT) == S_IFLNK)
		return 0;
	if (hp_Uflag)
		mode &= ~umsk|S_IFMT;
	if (pax != PAX_TYPE_CPIO &&
			(pax_preserve&(PAX_P_MODE|PAX_P_OWNER|PAX_P_EVERY))==0)
		mode &= 01777|S_IFMT;
	if ((pax == PAX_TYPE_CPIO || pax_preserve&(PAX_P_MODE|PAX_P_EVERY)) &&
			chmod(fn, mode) < 0) {
		emsg(2, "Cannot chmod() \"%s\"", fn);
		return 1;
	}
	if (pax != PAX_TYPE_CPIO ?
			(pax_preserve&(PAX_P_ATIME|PAX_P_MTIME|PAX_P_EVERY)) !=
			(PAX_P_ATIME|PAX_P_MTIME) : mflag)
		return rstime(fn, st, "modification");
	else
		return 0;
}

static int
setowner(const char *fn, struct stat *st)
{
	uid_t	uid = Rflag ? Ruid : myuid ? myuid : st->st_uid;
	gid_t	gid = Rflag ? Rgid : st->st_gid;

	if (((st->st_mode&S_IFMT)==S_IFLNK?lchown:chown)(fn, uid, gid) < 0) {
		emsg(2, "Cannot chown() \"%s\"", fn);
		return 1;
	}
	if (pax >= PAX_TYPE_PAX2001 && myuid && myuid != st->st_uid &&
			pax_preserve & (PAX_P_OWNER|PAX_P_EVERY)) {
		/*
		 * Do not even try to preserve user ownership in this case.
		 * It would either fail, or, without _POSIX_CHOWN_RESTRICTED,
		 * leave us with a file we do not own and which we thus could
		 * not chmod() later.
		 */
		errno = EPERM;
		emsg(2, "Cannot chown() \"%s\"", fn);
		return 1;
	}
	return 0;
}

/*
 * For -i and -p: Check if device/inode have been created already and
 * if so, link path (or print the link name).
 */
static int
canlink(const char *path, struct stat *st, int really)
{
	struct dslot	*ds;
	struct islot	*ip;

	ds = dfind(&devices, st->st_dev);
	if ((ip = ifind(st->st_ino, &ds->d_isl)) == NULL ||
			ip->i_name == NULL) {
		if (ip == NULL) {
			ip = scalloc(1, sizeof *ip);
			ip->i_ino = st->st_ino;
			if ((fmttype&(TYP_NCPIO|TYP_TAR)) == 0)
				ip->i_nlk = st->st_nlink;
			iput(ip, &ds->d_isl);
		}
		ip->i_name = sstrdup(path);
	} else {
		if ((fmttype&(TYP_NCPIO|TYP_TAR)) == 0) {
			/*
			 * If an archive inode has more links than given in
			 * st_nlink, write a new disk inode. See the comments
			 * to storelink() for the rationale.
			 */
			if (--ip->i_nlk == 0) {
				free(ip->i_name);
				ip->i_name = sstrdup(path);
				ip->i_nlk = st->st_nlink;
				return 0;
			}
		}
		if (really) {
			/*
			 * If there was file data before the last link with
			 * SVR4 cpio formats, do not create a hard link.
			 */
			if (fmttype & TYP_NCPIO && ip->i_nlk == 0)
				return 0;
			if (linkunlink(ip->i_name, path) == 0)
				return 1;
			else
				return -1;
		} else {
			printf(" == %s", ip->i_name);
			return 1;
		}
	}
	return 0;
}

/*
 * Execution for -i.
 */
static void
doinp(void)
{
	union bincpio	bc;
	struct file	f;
	int	n;

	memset(&f, 0, sizeof f);
	if (Eflag)
		patfile();
	if (Iflag) {
		if ((mt = open(Iflag, O_RDONLY)) < 0) {
			if (sysv3) {
				emsg(3, "Cannot open <%s> for input.", Iflag);
				done(1);
			}
			else
				msg(3, -2, "Cannot open \"%s\" for input\n",
						Iflag);
		}
	} else
		mt = dup(0);
	mstat();
	blkbuf = svalloc(blksiz, 1);
	if ((n = mread()) == 0)
		unexeoa();
	else if (n < 0) {
		emsg(3, "Read error on \"%s\"",
			Iflag && !sysv3 ? Iflag : "input");
		done(1);
	}
	if (kflag == 0 && sixflag == 0)
		fmttype = FMT_NONE;
	if (fmttype == FMT_NONE)
		whathdr();
	else
		formatforced = 1;
	while (readhdr(&f, &bc) == 0) {
		if (fmttype & TYP_NCPIO && f.f_st.st_nlink > 1 &&
				(f.f_st.st_mode&S_IFMT) != S_IFDIR) {
			if (f.f_st.st_size != 0)
				flushlinks(&f);
			else
				storelink(&f);
		} else
			inpone(&f, 1);
		f.f_Kbase = f.f_Krest = f.f_Ksize = 0;
		f.f_oflag = 0;
	}
	if (fmttype & TYP_NCPIO)
		flushrest(f.f_pad);
}

/*
 * SVR4 cpio link handling with -i; called if we assume that more links
 * to this file will follow.
 */
static void
storelink(struct file *f)
{
	struct dslot	*ds;
	struct islot	*ip;
	struct ilink	*il, *iz;

	ds = dfind(&devices, f->f_st.st_dev);
	if ((ip = ifind(f->f_st.st_ino, &ds->d_isl)) == NULL) {
		ip = scalloc(1, sizeof *ip);
		ip->i_ino = f->f_st.st_ino;
		ip->i_nlk = f->f_st.st_nlink;
		ip->i_st = smalloc(sizeof *ip->i_st);
		*ip->i_st = f->f_st;
		iput(ip, &ds->d_isl);
	} else {
		if (ip->i_nlk == 0) {
			/*
			 * More links to an inode than given in the first
			 * st_nlink occurence are found within this archive.
			 * This happens if -L was specified and soft links
			 * point to a file with multiple hard links. We do
			 * the same as SVR4 cpio implementations here and
			 * associate these links with a new inode, since it
			 * is related to the way a file with a single hard
			 * link but referenced by soft links is unpacked.
			 */
			ip->i_nlk = f->f_st.st_nlink;
			if (ip->i_name)
				free(ip->i_name);
			ip->i_name = NULL;
			ip->i_st = smalloc(sizeof *ip->i_st);
			*ip->i_st = f->f_st;
		} else if (ip->i_nlk <= 2) {
			/*
			 * We get here if
			 * - a file with multiple links is empty;
			 * - a broken implementation has stored file data
			 *   before the last link;
			 * - a linked file has been added to the archive later
			 *   again (does not happen with our implementation).
			 */
			flushnode(ip, f);
			return;
		} else
			ip->i_nlk--;
	}
	for (il = ip->i_lnk, iz = NULL; il; il = il->l_nxt)
		iz = il;
	il = scalloc(1, sizeof *il);
	il->l_siz = strlen(f->f_name) + 1;
	il->l_nam = smalloc(il->l_siz);
	strcpy(il->l_nam, f->f_name);
	if (iz)
		iz->l_nxt = il;
	else
		ip->i_lnk = il;
}

/*
 * Flush all links of a file with SVR4 cpio format and -i.
 */
static void
flushlinks(struct file *f)
{
	struct dslot	*ds;
	struct islot	*ip;

	ds = dfind(&devices, f->f_st.st_dev);
	ip = ifind(f->f_st.st_ino, &ds->d_isl);
	flushnode(ip, f);
}

/*
 * Data of a multi-linked file shall be transferred now for SVR4 cpio
 * format and -i.
 */
static void
flushnode(struct islot *ip, struct file *f)
{
	struct file	nf;
	struct ilink	*il, *iz;

	f->f_dsize = f->f_st.st_size;
	if (ip && ip->i_nlk > 0) {
		/*
		 * Write out the file data with the first link the user
		 * wants to be extracted, but show the same display size
		 * for all links.
		 */
		for (il = ip->i_lnk, iz = NULL; il;
				iz = il, il = il->l_nxt, iz ? free(iz), 0 : 0) {
			memset(&nf, 0, sizeof nf);
			nf.f_name = il->l_nam;
			nf.f_nsiz = il->l_siz;
			nf.f_st = f->f_st;
			nf.f_chksum = f->f_chksum;
			nf.f_pad = f->f_pad;
			nf.f_dsize = f->f_dsize;
			if (inpone(&nf, 0) == 0) {
				f->f_chksum = 0;
				f->f_st.st_size = 0;
			}
			free(il->l_nam);
		}
		ip->i_lnk = NULL;
		if (ip->i_st)
			free(ip->i_st);
		ip->i_st = NULL;
	}
	if (f->f_name)
		inpone(f, 1);
	if (ip) {
		if (ip->i_nlk <= 2 && ip->i_name) {
			free(ip->i_name);
			ip->i_name = NULL;
		}
		ip->i_nlk = 0;
	}
}

/*
 * This writes all remaining multiply linked files, i. e. those with
 * st_size == 0 and st_nlink > number of links within the archive.
 */
static void
flushrest(int pad)
{
	struct dslot	*ds;

	for (ds = devices; ds; ds = ds->d_nxt)
		if (ds->d_isl != NULL)
			flushtree(ds->d_isl, pad);
}

static void
flushtree(struct islot *ip, int pad)
{
	struct file	f;

	if (ip == inull)
		return;
	flushtree(ip->i_lln, pad);
	flushtree(ip->i_rln, pad);
	if (ip->i_nlk > 0 && ip->i_st) {
		memset(&f, 0, sizeof f);
		f.f_st = *ip->i_st;
		f.f_pad = pad;
		flushnode(ip, &f);
	}
}

/*
 * See if the user wants to have this file with -i, and extract it or
 * skip its data on the tape.
 */
static int
inpone(struct file *f, int shallskip)
{
	struct glist	*gp = NULL, *gb = NULL;
	int	val = -1, selected = 0;

	if ((patterns == NULL || (gp = want(f, &gb)) != NULL ^ fflag) &&
			pax_track(f->f_name, f->f_st.st_mtime)) {
		selected = 1;
		if ((pax_sflag == 0 || pax_sname(&f->f_name, &f->f_nsiz)) &&
				(rflag == 0 || rname(&f->f_name, &f->f_nsiz))) {
			errcnt += infile(f);
			val = 0;
		}
	} else if (shallskip)
		errcnt += skipfile(f);
	if (gp != NULL && selected) {
		gp->g_gotcha = 1;
		if (gp->g_nxt && gp->g_nxt->g_art)
			gp->g_nxt->g_gotcha = 1;
		else if (gp->g_art && gb)
			gb->g_gotcha = 1;
	}
	return val;
}

/*
 * Read the header for a file with -i. Return values: 0 okay, 1 end of
 * archive, -1 failure.
 */
static int
readhdr(struct file *f, union bincpio *bp)
{
	long	namlen, l1, l2, rd, hsz;
	static long	attempts;
	long long	skipped = 0;

	if (fmttype & TYP_TAR) {
		if (f->f_name)
			f->f_name[0] = '\0';
		if (f->f_lnam)
			f->f_lnam[0] = '\0';
	}
	paxrec = globrec;
retry:	f->f_st = globst;
retry2:	if (fmttype & TYP_BINARY) {
		hsz = SIZEOF_hdr_cpio;
		if (attempts == 0 && bread(bp->data, hsz) != hsz)
			unexeoa();
		if ((fmttype&TYP_BE ? pbe16(bp->Hdr.c_magic) :
				ple16(bp->Hdr.c_magic)) != mag_bin)
			goto badhdr;
		if (fmttype & TYP_BE) {
			f->f_st.st_dev = pbe16(bp->Hdr.c_dev)&0177777;
			f->f_st.st_ino = pbe16(bp->Hdr.c_ino)&0177777;
			f->f_st.st_mode = pbe16(bp->Hdr.c_mode);
		} else {
			f->f_st.st_dev = ple16(bp->Hdr.c_dev)&0177777;
			f->f_st.st_ino = ple16(bp->Hdr.c_ino)&0177777;
			f->f_st.st_mode = ple16(bp->Hdr.c_mode);
		}
		if (sixflag) {
			/*
			 * relevant Unix 6th Edition style mode bits
			 * (according to /usr/sys/inode.h, untested:)
			 *
			 * 060000	IFMT	type of file
			 * 040000	IFDIR	directory
			 * 020000	IFCHR	character special
			 * 060000	IFBLK	block special, 0 is regular
			 * 007777		permission bits as today
			 */
			f->f_st.st_mode &= 067777;
			if ((f->f_st.st_mode & 060000) == 0)
				f->f_st.st_mode |= S_IFREG;
		}
		if (fmttype & TYP_BE) {
			f->f_st.st_uid = pbe16(bp->Hdr.c_uid)&0177777;
			f->f_st.st_gid = pbe16(bp->Hdr.c_gid)&0177777;
			f->f_st.st_nlink = pbe16(bp->Hdr.c_nlink)&0177777;
			f->f_st.st_mtime = pbe32(bp->Hdr.c_mtime);
			f->f_st.st_rdev = pbe16(bp->Hdr.c_rdev);
			namlen = pbe16(bp->Hdr.c_namesize);
			f->f_st.st_size = pbe32(bp->Hdr.c_filesize)&0xFFFFFFFF;
		} else {
			f->f_st.st_uid = ple16(bp->Hdr.c_uid)&0177777;
			f->f_st.st_gid = ple16(bp->Hdr.c_gid)&0177777;
			f->f_st.st_nlink = ple16(bp->Hdr.c_nlink)&0177777;
			f->f_st.st_mtime = pme32(bp->Hdr.c_mtime);
			f->f_st.st_rdev = ple16(bp->Hdr.c_rdev);
			namlen = ple16(bp->Hdr.c_namesize);
			f->f_st.st_size = pme32(bp->Hdr.c_filesize)&0xFFFFFFFF;
		}
		f->f_rmajor = major(f->f_st.st_rdev);
		f->f_rminor = minor(f->f_st.st_rdev);
		if ((f->f_st.st_rdev&0xFFFF) == 0xFFFF &&
				f->f_st.st_size > 0 &&
				((f->f_st.st_mode&S_IFMT) == S_IFBLK ||
				 (f->f_st.st_mode&S_IFMT) == S_IFCHR &&
				 (!formatforced || fmttype & TYP_SGI))) {
			fmttype |= TYP_SGI;
			f->f_rmajor = (f->f_st.st_size&0xFFFC0000)>>18;
			f->f_rminor = f->f_st.st_size&0x0003FFFF;
			f->f_st.st_rdev = makedev(f->f_rmajor, f->f_rminor);
			f->f_st.st_size = 0;
		}
		if ((f->f_st.st_mode&S_IFMT) == S_IFREG &&
				f->f_st.st_size&0x80000000 &&
				(!formatforced || fmttype & TYP_SGI)) {
			fmttype |= TYP_SGI;
			f->f_Ksize = f->f_st.st_size&0xFFFFFFFF;
			f->f_Kbase = (0x100000000LL-f->f_st.st_size) *
				0x80000000;
			f->f_st.st_size = 0;
		}
		f->f_pad = 2;
		rd = SIZEOF_hdr_cpio;
	} else if (fmttype == FMT_ODC) {
		hsz = SIZEOF_c_hdr;
		if (attempts == 0 && bread(bp->data, hsz) != hsz)
			unexeoa();
		if(memcmp(bp->Cdr.c_magic, mag_odc, sizeof mag_odc))
			goto badhdr;
		f->f_st.st_dev = rdoct(bp->Cdr.c_dev, 6);
		f->f_st.st_ino = rdoct(bp->Cdr.c_ino, 6);
		f->f_st.st_mode = rdoct(bp->Cdr.c_mode, 6);
		f->f_st.st_uid = rdoct(bp->Cdr.c_uid, 6);
		f->f_st.st_gid = rdoct(bp->Cdr.c_gid, 6);
		f->f_st.st_nlink = rdoct(bp->Cdr.c_nlink, 6);
		f->f_st.st_rdev = rdoct(bp->Cdr.c_rdev, 6);
		f->f_rmajor = major(f->f_st.st_rdev);
		f->f_rminor = minor(f->f_st.st_rdev);
		f->f_st.st_mtime = rdoct(bp->Cdr.c_mtime, 11);
		namlen = rdoct(bp->Cdr.c_namesz, 6);
		f->f_st.st_size = rdoct(bp->Cdr.c_filesz, 11);
		f->f_pad = 1;
		rd = SIZEOF_c_hdr;
	} else if (fmttype == FMT_DEC) {
		hsz = SIZEOF_d_hdr;
		if (attempts == 0 && bread(bp->data, hsz) != hsz)
			unexeoa();
		if(memcmp(bp->Ddr.d_magic, mag_odc, sizeof mag_odc))
			goto badhdr;
		f->f_st.st_dev = rdoct(bp->Ddr.d_dev, 6);
		f->f_st.st_ino = rdoct(bp->Ddr.d_ino, 6);
		f->f_st.st_mode = rdoct(bp->Ddr.d_mode, 6);
		f->f_st.st_uid = rdoct(bp->Ddr.d_uid, 6);
		f->f_st.st_gid = rdoct(bp->Ddr.d_gid, 6);
		f->f_st.st_nlink = rdoct(bp->Ddr.d_nlink, 6);
		f->f_rmajor = rdoct(bp->Ddr.d_rmaj, 8);
		f->f_rminor = rdoct(bp->Ddr.d_rmin, 8);
		f->f_st.st_rdev = makedev(f->f_rmajor, f->f_rminor);
		f->f_st.st_mtime = rdoct(bp->Ddr.d_mtime, 11);
		namlen = rdoct(bp->Ddr.d_namesz, 6);
		f->f_st.st_size = rdoct(bp->Ddr.d_filesz, 11);
		f->f_pad = 1;
		rd = SIZEOF_d_hdr;
	} else if (fmttype & TYP_NCPIO) {
		hsz = SIZEOF_Exp_cpio_hdr;
		if (attempts == 0 && bread(bp->data, hsz) != hsz)
			unexeoa();
		if (memcmp(bp->Edr.E_magic, mag_asc, sizeof mag_asc) &&
		    memcmp(bp->Edr.E_magic, mag_crc, sizeof mag_crc))
			goto badhdr;
		f->f_st.st_ino = rdhex(bp->Edr.E_ino, 8);
		f->f_st.st_mode = rdhex(bp->Edr.E_mode, 8);
		f->f_st.st_uid = rdhex(bp->Edr.E_uid, 8);
		f->f_st.st_gid = rdhex(bp->Edr.E_gid, 8);
		f->f_st.st_nlink = rdhex(bp->Edr.E_nlink, 8);
		f->f_st.st_mtime = rdhex(bp->Edr.E_mtime, 8);
		f->f_st.st_size = rdhex(bp->Edr.E_filesize, 8);
		l1 = rdhex(bp->Edr.E_maj, 8);
		l2 = rdhex(bp->Edr.E_min, 8);
		f->f_st.st_dev = makedev(l1, l2);
		f->f_rmajor = rdhex(bp->Edr.E_rmaj, 8);
		f->f_rminor = rdhex(bp->Edr.E_rmin, 8);
		f->f_st.st_rdev = makedev(f->f_rmajor, f->f_rminor);
		namlen = rdhex(bp->Edr.E_namesize, 8);
		f->f_chksum = rdhex(bp->Edr.E_chksum, 8);
		f->f_pad = 4;
		rd = SIZEOF_Exp_cpio_hdr;
	} else if (fmttype & TYP_CRAY) {
		int	diff5 = fmttype==FMT_CRAY5 ? CRAY_PARAMSZ : 0;
		hsz = SIZEOF_cray_hdr - diff5;
		if (attempts == 0 && bread(bp->data, hsz) != hsz)
			unexeoa();
		if (pbe64(bp->Crayhdr.C_magic) != mag_bin)
			goto badhdr;
		f->f_st.st_dev = pbe64(bp->Crayhdr.C_dev);
		f->f_st.st_ino = pbe64(bp->Crayhdr.C_ino);
		f->f_st.st_mode = pbe64(bp->Crayhdr.C_mode);
		/*
		 * Some of the S_IFMT bits on Cray UNICOS 9 differ from
		 * the common Unix values, namely:
		 *
		 * S_IFLNK	0130000		symbolic link
		 * S_IFOFD	0110000		off line, with data
		 * S_IFOFL	0120000		off line, with no data
		 *
		 * We treat the latter ones as regular files.
		 */
		if ((f->f_st.st_mode&S_IFMT) == 0130000)
			f->f_st.st_mode = f->f_st.st_mode&07777|S_IFLNK;
		else if ((f->f_st.st_mode&S_IFMT) == 0110000 ||
				(f->f_st.st_mode&S_IFMT) == 0120000)
			f->f_st.st_mode = f->f_st.st_mode&07777|S_IFREG;
		f->f_st.st_uid = pbe64(bp->Crayhdr.C_uid);
		f->f_st.st_gid = pbe64(bp->Crayhdr.C_gid);
		f->f_st.st_nlink = pbe64(bp->Crayhdr.C_nlink);
		f->f_st.st_mtime = pbe64(bp->Crayhdr.C_mtime - diff5);
		f->f_st.st_rdev = pbe64(bp->Crayhdr.C_rdev);
		f->f_rmajor = major(f->f_st.st_rdev);
		f->f_rminor = minor(f->f_st.st_rdev);
		f->f_st.st_size = pbe64(bp->Crayhdr.C_filesize - diff5);
		namlen = pbe64(bp->Crayhdr.C_namesize - diff5);
		f->f_pad = 1;
		rd = SIZEOF_cray_hdr - diff5;
	} else if (fmttype & TYP_BAR) {
		hsz = 512;
		if (attempts == 0 && bread(bp->data, hsz) != hsz)
			unexeoa();
		if (bp->data[SIZEOF_bar_header] == '\0') {
			if (kflag && !allzero(bp->data, hsz))
				goto badhdr;
			return 1;
		}
		if (trdsum(bp) != 0 && kflag == 0)
			goto badhdr;
		bp->data[512] = bp->data[513] = '\0';
		if (f->f_name == NULL || f->f_name[0] == '\0') {
			if (f->f_nsiz < 512) {
				free(f->f_name);
				f->f_name = smalloc(f->f_nsiz = 512);
			}
			strcpy(f->f_name, &bp->data[SIZEOF_bar_header]);
		}
		namlen = strlen(f->f_name) + 1;
		f->f_st.st_mode = rdoct(bp->Bdr.b_mode, 8);
		f->f_st.st_uid = rdoct(bp->Bdr.b_uid, 8);
		f->f_st.st_gid = rdoct(bp->Bdr.b_gid, 8);
		f->f_st.st_size = rdoct(bp->Bdr.b_size, 12);
		f->f_st.st_mtime = rdoct(bp->Bdr.b_mtime, 12);
		f->f_st.st_rdev = rdoct(bp->Bdr.b_rdev, 8);
		f->f_rmajor = major(f->f_st.st_rdev);
		f->f_rminor = minor(f->f_st.st_rdev);
		switch (bp->Bdr.b_linkflag) {
		case '0':
			f->f_st.st_mode &= 07777;
			if (f->f_name[namlen-2] == '/')
				f->f_st.st_mode |= S_IFDIR;
			else
				f->f_st.st_mode |= S_IFREG;
			break;
		case '1':
			if (f->f_lnam == NULL || f->f_lnam[0] == '\0')
				blinkof(bp->data, f, namlen);
			f->f_st.st_nlink = 2;
			if ((f->f_st.st_mode&S_IFMT) == 0)
				f->f_st.st_mode |= S_IFREG;
			break;
		case '2':
			if (f->f_lnam == NULL || f->f_lnam[0] == '\0')
				blinkof(bp->data, f, namlen);
			f->f_st.st_mode &= 07777;
			f->f_st.st_mode |= S_IFLNK;
		}
		f->f_pad = 512;
		rd = 512;
	} else if (fmttype & TYP_TAR) {
		hsz = 512;
		if (attempts == 0 && bread(bp->data, hsz) != hsz)
			unexeoa();
		if (bp->Tdr.t_name[0] == '\0') {
			if (kflag && !allzero(bp->data, hsz))
				goto badhdr;
			return 1;
		}
		if (fmttype == FMT_GNUTAR) {
			if (memcmp(bp->Tdr.t_magic, mag_gnutar, 8))
				goto badhdr;
		} else if (fmttype & TYP_USTAR) {
			if (memcmp(bp->Tdr.t_magic, mag_ustar, 6))
				goto badhdr;
		}
		if (trdsum(bp) != 0 && kflag == 0)
			goto badhdr;
		if ((fmttype & TYP_PAX) == 0 && (fmttype == FMT_GNUTAR ||
				strcmp(bp->Tdr.t_name, "././@LongLink") == 0)) {
			switch (bp->Tdr.t_linkflag) {
			case 'L':
				if (readgnuname(&f->f_name, &f->f_nsiz,
						rdoct(bp->Tdr.t_size, 12)) < 0)
					goto badhdr;
				goto retry;
			case 'K':
				if (readgnuname(&f->f_lnam, &f->f_lsiz,
						rdoct(bp->Tdr.t_size, 12)) < 0)
					goto badhdr;
				goto retry;
			}
		}
		if (fmttype & TYP_USTAR && fmttype != FMT_GNUTAR) {
			switch (bp->Tdr.t_linkflag) {
			case 'g':
			case 'x':
				if (formatforced && fmttype != FMT_PAX)
					break;
				fmttype = FMT_PAX;
				tgetpax(&bp->Tdr, f);
				goto retry2;
			case 'X':
				if (formatforced && fmttype != FMT_SUN)
					break;
				fmttype = FMT_SUN;
				tgetpax(&bp->Tdr, f);
				goto retry2;
			}
		}
		if (f->f_name == NULL || f->f_name[0] == '\0') {
			if (f->f_nsiz < TNAMSIZ+TPFXSIZ+2) {
				free(f->f_name);
				f->f_name= smalloc(f->f_nsiz=TNAMSIZ+TPFXSIZ+2);
			}
			tnameof(&bp->Tdr, f->f_name);
		}
		namlen = strlen(f->f_name) + 1;
		f->f_st.st_mode = rdoct(bp->Tdr.t_mode, 8) & 07777;
		if (paxrec & PR_UID)
			/*EMPTY*/;
		else if (fmttype == FMT_GNUTAR && bp->Tdr.t_uid[0] & 0200)
			f->f_st.st_uid = pbe64(bp->Tdr.t_uid) &
				0x7FFFFFFFFFFFFFFFLL;
		else
			f->f_st.st_uid = rdoct(bp->Tdr.t_uid, 8);
		if (paxrec & PR_GID)
			/*EMPTY*/;
		else if (fmttype == FMT_GNUTAR && bp->Tdr.t_gid[0] & 0200)
			f->f_st.st_gid = pbe64(bp->Tdr.t_gid) &
				0x7FFFFFFFFFFFFFFFLL;
		else
			f->f_st.st_gid = rdoct(bp->Tdr.t_gid, 8);
		if (paxrec & PR_SIZE)
			/*EMPTY*/;
		else if (fmttype == FMT_GNUTAR && bp->Tdr.t_size[0] == '\200')
			f->f_st.st_size = pbe64(&bp->Tdr.t_size[4]);
		else
			f->f_st.st_size = rdoct(bp->Tdr.t_size, 12);
		if ((paxrec & PR_MTIME) == 0)
			f->f_st.st_mtime = rdoct(bp->Tdr.t_mtime, TTIMSIZ);
		if (bp->Tdr.t_linkflag == '1') {
			if (f->f_lnam == NULL || f->f_lnam[0] == '\0')
				tlinkof(&bp->Tdr, f);
			f->f_st.st_mode |= S_IFREG;	/* for -tv */
			f->f_st.st_nlink = 2;
		} else if (bp->Tdr.t_linkflag == '2') {
			if (f->f_lnam == NULL || f->f_lnam[0] == '\0')
				tlinkof(&bp->Tdr, f);
			f->f_st.st_mode |= S_IFLNK;
		} else if (tflag == 0 && (f->f_name)[namlen-2] == '/')
			f->f_st.st_mode |= S_IFDIR;
		else
			f->f_st.st_mode |= tifmt(bp->Tdr.t_linkflag);
		if (paxrec & PR_SUN_DEVMAJOR)
			/*EMPTY*/;
		else if (fmttype == FMT_GNUTAR && bp->Tdr.t_devmajor[0] & 0200)
			f->f_rmajor = pbe64(bp->Tdr.t_devmajor) &
				0x7FFFFFFFFFFFFFFFLL;
		else
			f->f_rmajor = rdoct(bp->Tdr.t_devmajor, 8);
		if (paxrec & PR_SUN_DEVMINOR)
			/*EMPTY*/;
		else if (fmttype == FMT_GNUTAR && bp->Tdr.t_devminor[0] & 0200)
			f->f_rminor = pbe64(bp->Tdr.t_devminor) &
				0x7FFFFFFFFFFFFFFFLL;
		else
			f->f_rminor = rdoct(bp->Tdr.t_devminor, 8);
		f->f_st.st_rdev = makedev(f->f_rmajor, f->f_rminor);
		f->f_pad = 512;
		rd = 512;
	} else if (fmttype == FMT_ZIP) {
		hsz = SIZEOF_zip_header;
		if (attempts == 0 && bread(bp->data, hsz) != hsz)
			unexeoa();
		if (memcmp(bp->Zdr.z_signature, mag_zipctr,
					sizeof mag_zipctr) == 0 ||
				memcmp(bp->Zdr.z_signature, mag_zipend,
					sizeof mag_zipend) == 0 ||
				memcmp(bp->Zdr.z_signature, mag_zip64e,
					sizeof mag_zip64e) == 0 ||
				memcmp(bp->Zdr.z_signature, mag_zip64l,
					sizeof mag_zip64l) == 0)
			return 1;
		if (memcmp(bp->Zdr.z_signature, mag_zipsig, sizeof mag_zipsig))
			goto badhdr;
		f->f_st.st_uid = myuid;
		f->f_st.st_gid = mygid;
		f->f_st.st_nlink = 1;
		f->f_st.st_mtime =
			gdostime(bp->Zdr.z_modtime, bp->Zdr.z_moddate);
		f->f_st.st_size = ple32(bp->Zdr.z_nsize);
		f->f_csize = ple32(bp->Zdr.z_csize);
		f->f_cmethod = ple16(bp->Zdr.z_cmethod);
		f->f_chksum = ple32(bp->Zdr.z_crc32);
		f->f_gflag = ple16(bp->Zdr.z_gflag);
		rd = SIZEOF_zip_header;
		namlen = ple16(bp->Zdr.z_namelen)&0177777;
		if (f->f_nsiz < namlen+1) {
			free(f->f_name);
			f->f_name = smalloc(f->f_nsiz = namlen+1);
		}
		if (bread(f->f_name, namlen) != namlen)
			goto corrupt;
		(f->f_name)[namlen] = '\0';
		if (f->f_name[namlen-1] == '/')
			f->f_st.st_mode = 0777&~(mode_t)umsk|S_IFDIR;
		else
			f->f_st.st_mode = 0666&~(mode_t)umsk|S_IFREG;
		rd += namlen;
		if ((l1 = ziprxtra(f, &bp->Zdr)) < 0)
			goto corrupt;
		rd += l1;
		f->f_pad = 1;
	} else
		abort();
	if (fmttype & TYP_CPIO) {
		if (f->f_st.st_nlink <= 0 || namlen <= 0 || namlen >= SANELIMIT)
			goto corrupt;
		if (namlen > f->f_nsiz) {
			if (f->f_name)
				free(f->f_name);
			f->f_name = smalloc(f->f_nsiz = namlen);
		}
		if (bread(f->f_name, namlen) != namlen)
			unexeoa();
		if (f->f_name[namlen-1] != '\0')
			goto corrupt;
		rd += namlen;
		skippad(rd, f->f_pad);
		if (fmttype & TYP_NCPIO &&
				(!formatforced || fmttype & TYP_SCO)) {
			/*
			 * The UnixWare format is a hack, but not a bad
			 * one. It is backwards compatible as far as
			 * possible; old implementations can use the -k
			 * option and will then get only the beginning
			 * of a large file, but all other files in the
			 * archive.
			 */
			if (namlen >= 24 && memcmp(&f->f_name[namlen-23],
						"\0size=", 6) == 0) {
				fmttype |= TYP_SCO;
				f->f_st.st_size = rdhex(&f->f_name[namlen-17],
						16);
			}
		}
	}
	if (attempts) {
		if (tflag)
			fflush(stdout);
		msg(0, 0, sysv3 < 0 ?
			"Re-synced after skipping %lld bytes.\n" :
			"Re-synchronized on magic number/header.\n",
			skipped);
	}
	attempts = 0;
	if (f->f_st.st_atime == 0 || (pax_preserve&(PAX_P_ATIME|PAX_P_EVERY)) ==
			PAX_P_ATIME)
		f->f_st.st_atime = f->f_st.st_mtime;
	if ((f->f_dsize = f->f_st.st_size) < 0)
		goto badhdr;
	if (fmttype & TYP_CPIO && strcmp(f->f_name, trailer) == 0)
		return 1;
	return 0;
corrupt:
	if (kflag) {
		if (tflag)
			fflush(stdout);
		msg(3, 0, "Corrupt header, file(s) may be lost.\n");
	}
badhdr:
	if (kflag) {
		int	next = fmttype & TYP_TAR ? 512 : 1;
		if (attempts++ == 0) {
			if (tflag)
				fflush(stdout);
			msg(1, 0, sysv3 < 0 ? "Out of phase; resyncing.\n" :
				"Searching for magic number/header.\n");
			errcnt++;
		}
		for (l1 = next; l1 < hsz; l1++)
			bp->data[l1-next] = bp->data[l1];
		if (bread(&bp->data[l1-next], next) != next)
			unexeoa();
		skipped++;
		goto retry;
	}
	if (tflag)
		fflush(stdout);
	msg(3, 1, sysv3 < 0 ? "Out of phase--get help\n" :
			sysv3 > 0 ? "Out of sync, bad magic number/header.\n" :
			"Bad magic number/header.\n");
	/*NOTREACHED*/
	return -1;
}

/*
 * Determine the kind of archive on tape.
 */
static void
whathdr(void)
{
again:	if (blktop >= SIZEOF_hdr_cpio &&
			ple16(blkbuf) == mag_bin) {
		fmttype = FMT_BINLE;
	} else if (blktop >= SIZEOF_hdr_cpio &&
			pbe16(blkbuf) == mag_bin) {
		fmttype = FMT_BINBE;
	} else if (blktop >= SIZEOF_c_hdr &&
			memcmp(blkbuf, mag_odc, sizeof mag_odc) == 0) {
		/*
		 * The DEC format is rubbish. Instead of introducing a new
		 * archive magic, its engineers reused the POSIX/odc magic
		 * and changed the fields. But there's a workaround: For a
		 * real odc archive, the byte following the file name is a
		 * \0 byte (unless the archive is damaged, of course). If
		 * it is not a \0 byte, but a \0 byte appears at the
		 * corresponding location for the DEC format, this is
		 * treated as a DEC archive. It must be noted that the
		 * Tru64 UNIX 5.1 cpio command is too stupid even for
		 * doing that - it will spill out a list of complaints
		 * if an extended archive is read but -e is not given.
		 */
		int	ns1, ns2;
		ns1 = rdoct(((struct c_hdr *)blkbuf)->c_namesz, 6);
		ns2 = rdoct(((struct d_hdr *)blkbuf)->d_namesz, 6);
		if (blktop >= SIZEOF_d_hdr &&
				(ns1 >= blktop - SIZEOF_c_hdr ||
				 blkbuf[SIZEOF_c_hdr+ns1-1] != '\0') &&
				ns2 <= blktop - SIZEOF_d_hdr &&
				blkbuf[SIZEOF_d_hdr+ns2-1] == '\0')
			fmttype = FMT_DEC;
		else
			fmttype = FMT_ODC;
	} else if (blktop >= SIZEOF_Exp_cpio_hdr &&
			memcmp(blkbuf, mag_asc, sizeof mag_asc) == 0) {
		fmttype = FMT_ASC;
	} else if (blktop >= SIZEOF_Exp_cpio_hdr &&
			memcmp(blkbuf, mag_crc, sizeof mag_crc) == 0) {
		fmttype = FMT_CRC;
	} else if (blktop >= SIZEOF_cray_hdr &&
			pbe64(blkbuf) == mag_bin) {
		/*
		 * Old and new Cray headers are identical in the first
		 * 64 header bytes (including the magic). cpio(5) on
		 * UNICOS does not describe what the new param field
		 * is for.
		 *
		 * An archive is treated as old format if the mtime
		 * and namesize fields make sense and all characters
		 * of the name are non-null.
		 */
		struct cray_hdr	*Cp = (struct cray_hdr *)blkbuf;
		long long	mtime, namesize;
		fmttype = FMT_CRAY;
		mtime = pbe64(Cp->C_mtime - CRAY_PARAMSZ);
		namesize = pbe64(Cp->C_namesize - CRAY_PARAMSZ);
		if (mtime > 0 && mtime < (1LL<<31) &&
				namesize > 0 && namesize < 2048 &&
				blktop > SIZEOF_cray_hdr-
					CRAY_PARAMSZ+namesize+1) {
			int	i;
			for (i = 0; i < namesize; i++)
				if (blkbuf[SIZEOF_cray_hdr-
						CRAY_PARAMSZ+i] == '\0')
					break;
			if (i == namesize-1)
				fmttype = FMT_CRAY5;
		}
	} else if (blktop >= 512 &&
			memcmp(&blkbuf[257], mag_ustar, 6) == 0 &&
			tcssum((union bincpio *)blkbuf, 1) == 0) {
		fmttype = FMT_USTAR;
	} else if (blktop >= 512 &&
			memcmp(&blkbuf[257], mag_gnutar, 8) == 0 &&
			tcssum((union bincpio *)blkbuf, 1) == 0) {
		fmttype = FMT_GNUTAR;
	} else if (blktop >= 512 && blkbuf[0] &&	/* require filename
							   to avoid match on
							   /dev/zero etc. */
			memcmp(&blkbuf[257], "\0\0\0\0\0", 5) == 0 &&
			tcssum((union bincpio *)blkbuf, 0) == 0) {
		fmttype = FMT_OTAR;
	} else if (blktop >= SIZEOF_zip_header &&
			(memcmp(blkbuf, mag_zipctr, sizeof mag_zipctr) == 0 ||
			 memcmp(blkbuf, mag_zipsig, sizeof mag_zipsig) == 0 ||
			 memcmp(blkbuf, mag_zipend, sizeof mag_zipend) == 0 ||
			 memcmp(blkbuf, mag_zip64e, sizeof mag_zip64e) == 0 ||
			 memcmp(blkbuf, mag_zip64l, sizeof mag_zip64l) == 0)) {
		fmttype = FMT_ZIP;
	} else if (blktop >= 512 && memcmp(blkbuf,"\0\0\0\0\0\0\0\0",8) == 0 &&
			memcmp(&blkbuf[65], mag_bar, sizeof mag_bar) == 0 &&
			bcssum((union bincpio *)blkbuf) == 0) {
		fmttype = FMT_BAR;
		compressed_bar = blkbuf[71] == '1';
		curpos = 512;
	} else if (!Aflag && blktop > 3 && memcmp(blkbuf, "BZh", 3) == 0 &&
			redirect("bzip2", "-cd") == 0) {
		goto zip;
	} else if (!Aflag && blktop > 2 && memcmp(blkbuf, "\37\235", 2) == 0 &&
			redirect("zcat", NULL) == 0) {
		goto zip;
	} else if (!Aflag && blktop > 2 && memcmp(blkbuf, "\37\213", 2) == 0 &&
			redirect("gzip", "-cd") == 0) {
		goto zip;
	} else if (!Aflag && blktop > 4 &&
			memcmp(blkbuf, "\355\253\356\333", 4) == 0 &&
			redirect("rpm2cpio", "-") == 0) {
		goto zip;
	} else {
		msg(3, 0, sysv3 ? "This is not a cpio file, bad header.\n" :
				"Not a cpio file, bad header.\n");
		done(1);
	}
	return;
zip:
	blktop = curpos = 0;
	blocks = 0;
	bytes = 0;
	mfl = 0;
	mstat();
	if (mread() == 0)
		unexeoa();
	goto again;
}

/*
 * Processing of a single file with -i.
 */
static int
infile(struct file *f)
{
	int	val;

	if ((fmttype & TYP_CPIO || fmttype == FMT_ZIP && f->f_st.st_size) &&
			(f->f_st.st_mode&S_IFMT) == S_IFLNK) {
		if (f->f_lsiz < f->f_st.st_size+1)
			f->f_lnam = srealloc(f->f_lnam,
					f->f_lsiz = f->f_st.st_size+1);
		if (bread(f->f_lnam, f->f_st.st_size) != f->f_st.st_size)
			unexeoa();
		f->f_lnam[f->f_st.st_size] = '\0';
		skippad(f->f_st.st_size, f->f_pad);
	}
	if (tflag)
		val = filet(f, indata);
	else
		val = filein(f, indata, f->f_name);
	return val != 0;
}

/*
 * Fetch the data of regular files from tape and write it to tfd or, if
 * tfd < 0, discard it.
 */
static int
indata(struct file *f, const char *tgt, int tfd)
{
	char	*buf;
	size_t	bufsize;
	struct stat	ts;
	long long	size;
	ssize_t	rd;
	uint32_t	ssum = 0, usum = 0;
	int	val = 0;
	int	doswap = 0;

	size = fmttype == FMT_ZIP ? f->f_csize :
		f->f_Kbase ? f->f_Kbase : f->f_st.st_size;
	doswap = ((bflag|sflag) == 0 ||
			ckodd(size, 2, "bytes", f->f_name) == 0) &
		((bflag|Sflag) == 0 ||
			 ckodd(size, 4, "halfwords", f->f_name) == 0) &
		(bflag|sflag|Sflag);
	if (fmttype == FMT_ZIP && f->f_cmethod != C_STORED)
		return zipread(f, tgt, tfd, doswap);
	if (tfd < 0 || fstat(tfd, &ts) < 0)
		ts.st_blksize = 4096;
	getbuf(&buf, &bufsize, ts.st_blksize);
again:	while (size) {
		if ((rd = bread(buf, size > bufsize ? bufsize : size)) <= 0)
			unexeoa();
		if (doswap)
			swap(buf, rd, bflag || sflag, bflag || Sflag);
		if (tfd >= 0 && write(tfd, buf, rd) != rd) {
			emsg(3, "Cannot write \"%s\"", tgt);
			tfd = -1;
			val = -1;
		}
		size -= rd;
		if (fmttype & TYP_CRC)
			do {
				rd--;
				ssum += ((signed char *)buf)[rd];
				usum += ((unsigned char *)buf)[rd];
			} while (rd);
	}
	if (f->f_Kbase) {
		skippad(f->f_Ksize, f->f_pad);
		readK2hdr(f);
		size = f->f_Krest;
		goto again;
	}
	skippad(fmttype==FMT_ZIP ? f->f_csize :
			f->f_Ksize ? f->f_Krest : f->f_st.st_size, f->f_pad);
	if (fmttype & TYP_CRC) {
		if (f->f_chksum != ssum && f->f_chksum != usum) {
			msg(3, 0, "\"%s\" - checksum error\n", f->f_name);
			if (kflag)
				errcnt++;
			else
				val = -1;
		}
	}
	return val;
}

/*
 * Skip the data for a file on tape.
 */
static int
skipfile(struct file *f)
{
	char	b[4096];
	long long	size;
	ssize_t	rd;

	if (fmttype & TYP_TAR && ((f->f_st.st_mode&S_IFMT) == S_IFLNK ||
			f->f_st.st_nlink > 1)) {
		if (fmttype & TYP_USTAR && (!formatforced ||
				fmttype == FMT_PAX) &&
				f->f_st.st_nlink > 1 &&
				f->f_st.st_size > 0)
			/*EMPTY*/;
		else
			return 0;
	}
	/*
	 * SVR4 cpio derivatives always ignore the size of these,
	 * even if not reading tar formats. We do the same for now,
	 * although POSIX (for -H odc format) says the contrary.
	 */
	if (fmttype != FMT_ZIP && ((f->f_st.st_mode&S_IFMT) == S_IFDIR ||
			(f->f_st.st_mode&S_IFMT) == S_IFCHR ||
			(f->f_st.st_mode&S_IFMT) == S_IFBLK ||
			(f->f_st.st_mode&S_IFMT) == S_IFIFO ||
			(f->f_st.st_mode&S_IFMT) == S_IFNAM ||
			(f->f_st.st_mode&S_IFMT) == S_IFNWK))
		return 0;
	if (fmttype == FMT_ZIP && f->f_gflag & FG_DESC)
		return zipread(f, f->f_name, -1, 0);
	size = fmttype == FMT_ZIP ? f->f_csize :
		f->f_Kbase ? f->f_Kbase : f->f_st.st_size;
again:	while (size) {
		if ((rd = bread(b, size > sizeof b ? sizeof b : size)) <= 0)
			unexeoa();
		size -= rd;
	}
	if (f->f_Kbase) {
		skippad(f->f_Ksize, f->f_pad);
		readK2hdr(f);
		size = f->f_Krest;
		goto again;
	}
	skippad(fmttype==FMT_ZIP ? f->f_csize :
			f->f_Ksize ? f->f_Krest : f->f_st.st_size, f->f_pad);
	return 0;
}

/*
 * Skip data also, but perform checks as if copying.
 */
static int
skipdata(struct file *f, int (*copydata)(struct file *, const char *, int))
{
	switch (f->f_st.st_mode&S_IFMT) {
	case S_IFLNK:
		break;
	case S_IFDIR:
	case S_IFBLK:
	case S_IFCHR:
	case S_IFIFO:
	case S_IFNAM:
	case S_IFNWK:
		if (fmttype != FMT_ZIP)
			break;
		/*FALLTHRU*/
	case S_IFREG:
	default:
		if (fmttype & TYP_TAR && f->f_st.st_nlink > 1 &&
				((fmttype & TYP_USTAR) == 0 ||
				 formatforced && fmttype != FMT_PAX))
			break;
		if (copydata(f, f->f_name, -1) != 0)
			return 1;
	}
	return 0;
}

/*
 * Seek to a position in the archive measured from the begin of
 * operation. Handle media-dependent seeks. n must be a multiple
 * of the tape device's physical block size.
 */
static int
tseek(off_t n)
{
	int	fault;

	if (tapeblock > 0) {
		int	i = (n - poffs) / tapeblock;
#if defined (__linux__) || defined (__sun) || defined (__FreeBSD__) || \
	defined (__hpux) || defined (_AIX) || defined (__NetBSD__) || \
	defined (__OpenBSD__) || defined (__DragonFly__) || \
	defined (__CYGWIN__)
		struct mtop	mo;
		mo.mt_op = i > 0 ? MTFSR : MTBSR;
		mo.mt_count = i > 0 ? i : -i;
		fault = ioctl(mt, MTIOCTOP, &mo);
#endif
	} else
		fault = lseek(mt, n - poffs, SEEK_CUR) == (off_t)-1 ? -1 : 0;
	if (fault == 0)
		poffs = n;
	return fault;
}

/*
 * Advance to the trailer on the tape (for -A).
 */
static int
totrailer(void)
{
	union bincpio	bc;
	struct file	f;
	off_t	ooffs, noffs, diff;
	int	i;

	if (mread() == 0)
		unexeoa();
	if (fmttype == FMT_NONE)
		whathdr();
	memset(&f, 0, sizeof f);
	for (;;) {
		ooffs = aoffs;
		if ((i = readhdr(&f, &bc)) < 0)
			goto fail;
		if (i > 0)
			break;
		markdev(f.f_st.st_dev);
		if (skipfile(&f) != 0)
			goto fail;
		if (fmttype == FMT_ZIP)
			zipdefer(f.f_name, &f.f_st, ooffs,
					f.f_chksum, f.f_csize, &bc.Zdr);
		pax_track(f.f_name, f.f_st.st_mtime);
	}
	/*
	 * Now seek to the position of the trailer, but retain the
	 * block alignment.
	 */
	diff = ooffs % blksiz;
	noffs = ooffs - diff;
	if (diff ? tseek(noffs) == 0 && mread() >= diff && tseek(noffs) == 0
			: tseek(ooffs) == 0) {
		free(f.f_name);
		free(f.f_lnam);
		nwritten = ooffs;
		curpos = diff;
		return 0;
	}
fail:	msg(4, 1, "Unable to append to this archive\n");
	/*NOTREACHED*/
	return 1;
}

static long long
rdoct(const char *data, int len)
{
	int	i;
	long long	val = 0;

	for (i = 0; i < len && data[i] == ' '; i++);
	for ( ; i < len && data[i] && data[i] != ' '; i++) {
		val <<= 3;
		val += data[i] - '0';
	}
	return val;
}

static long long
rdhex(const char *data, int len)
{
	int	i;
	long long	val = 0;

	for (i = 0; i < len && data[i] == ' '; i++);
	for ( ; i < len && data[i] && data[i] != ' '; i++) {
		val <<= 4;
		val += data[i] > '9' ? data[i] > 'F' ? data[i] - 'a' + 10 :
			data[i] - 'A' + 10 : data[i] - '0';
	}
	return val;
}

void
unexeoa(void)
{
	if (sysv3) {
		fprintf(stderr,
		"Can't read input:  end of file encountered "
			"prior to expected end of archive.\n");
		done(1);
	}
	else
		msg(3, 1, "Unexpected end-of-archive encountered.\n");
}

static char	*peekdata;
static size_t	peekbot, peektop, peeksize;

void
bunread(const char *data, size_t sz)
{
	peekdata = srealloc(peekdata, peeksize += sz);
	memcpy(&peekdata[peekbot], data, sz);
	peektop += sz;
	aoffs -= sz;
}

/*
 * Buffered read of data from tape. sz is the amount of data required
 * by the archive format; if it cannot be retrieved, processing fails.
 */
ssize_t
bread(char *data, size_t sz)
{
	ssize_t	rd = 0;

	if (peekdata) {
		rd = sz>peektop-peekbot ? peektop-peekbot : sz;
		memcpy(&data[0], &peekdata[peekbot], rd);
		sz -= rd;
		peekbot += rd;
		if (peekbot == peektop) {
			free(peekdata);
			peekdata = 0;
			peeksize = 0;
			peekbot = peektop = 0;
		}
	}
	while (sz) {
		if (blktop - curpos >= sz) {
			memcpy(&data[rd], &blkbuf[curpos], sz);
			curpos += sz;
			rd += sz;
			aoffs += rd;
			return rd;
		}
		if (blktop > curpos) {
			memcpy(&data[rd], &blkbuf[curpos], blktop - curpos);
			rd += blktop - curpos;
			sz -= blktop - curpos;
			curpos = blktop;
		}
		if (mfl < 0) {
			if (mfl == -1) {
				emsg(3, "I/O error on \"%s\"",
						Iflag ? Iflag : "input");
				if (kflag == 0)
					break;
				if (tapeblock < 0 && (
						(mtst.st_mode&S_IFMT)==S_IFBLK||
						(mtst.st_mode&S_IFMT)==S_IFCHR||
						(mtst.st_mode&S_IFMT)==S_IFREG
						) && lseek(mt, blksiz, SEEK_CUR)
							== (off_t)-1) {
					emsg(3, "Cannot lseek()");
					done(1);
				}
			}
			if (kflag == 0 || mfl == -2) {
				if ((mtst.st_mode&S_IFMT)!=S_IFCHR &&
						(mtst.st_mode&S_IFMT)!=S_IFBLK)
					break;
				newmedia(mfl == -1 ? errno : 0);
				if (mfl == -1) {
					mfl = -2;
					break;
				}
				if (fmttype & TYP_BAR)
					curpos = 512;
			}
		}
		mread();
	}
	aoffs += rd;
	return rd;
}

/*
 * Read a block of data from tape.
 */
static ssize_t
mread(void)
{
	ssize_t	ro, rt = 0;

	do {
		if ((ro = read(mt, blkbuf + rt, blksiz - rt)) <= 0) {
			if (ro < 0) {
				if (errno == EINTR)
					continue;
				mfl = -1;
			} else
				mfl = -2;
			if (rt > 0) {
				rt += ro;
				break;
			}
			curpos = blktop = 0;
			return ro;
		}
		rt += ro;
		poffs += ro;
		if (tapeblock == 0) {
			tapeblock = ro;
			if (!Bflag && !Cflag)
				blksiz = ro;
		}
	} while (rt < blksiz);
	curpos = 0;
	blocks += rt >> 9;
	bytes += rt & 0777;
	blktop = rt;
	return rt;
}

/*
 * Look what kind of tape or other archive media we are working on and
 * set the buffer size appropriately (if not specified by the user).
 */
static void
mstat(void)
{
	if (fstat(mt, &mtst) < 0) {
		emsg(3, "Error during stat() of archive");
		done(1);
	}
#if defined (__linux__) 
	if ((mtst.st_mode&S_IFMT) == S_IFCHR) {
		struct mtget	mg;
		if (ioctl(mt, MTIOCGET, &mg) == 0)
			tapeblock = (mg.mt_dsreg&MT_ST_BLKSIZE_MASK) >>
					MT_ST_BLKSIZE_SHIFT;
	} else if ((mtst.st_mode&S_IFMT) == S_IFBLK) {
		/*
		 * If using a block device, write blocks of the floppy
		 * disk sector with direct i/o. This enables signals
		 * after each block is written instead of being ~40
		 * seconds in uninterruptible sleep when calling close()
		 * later. For block devices other than floppies, use the 
		 * kernel defined i/o block size. For floppies, use direct
		 * i/o even when reading since it is faster.
		 */
		struct floppy_struct	fs;
		int	floppy = -1;
		int	blkbsz;

		if (blksiz == 0) {
			if ((floppy = ioctl(mt, FDGETPRM, &fs)) == 0)
				blksiz = fs.sect * FD_SECTSIZE(&fs);
#ifdef	BLKBSZGET
			else if (ioctl(mt, BLKBSZGET, &blkbsz) == 0)
				blksiz = blkbsz;
#endif	/* BLKBSZGET */
		}
#ifdef	O_DIRECT
		if ((action == 'o' || floppy == 0) && blksiz != 0) {
			int	flags;
			if ((flags = fcntl(mt, F_GETFL)) != -1)
				fcntl(mt, F_SETFL, flags | O_DIRECT);
		}
	}
#endif	/* O_DIRECT */
#elif defined (__sun)
	if ((mtst.st_mode&S_IFMT) == S_IFCHR) {
		struct mtdrivetype_request	mr;
		static struct mtdrivetype	md;
		mr.size = sizeof md;
		mr.mtdtp = &md;
		if (ioctl(mt,  MTIOCGETDRIVETYPE, &mr) == 0)
			tapeblock = md.bsize;
	}
#elif defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) \
		|| defined (__DragonFly__)
	if ((mtst.st_mode&S_IFMT) == S_IFCHR) {
		struct mtget	mg;
		if (ioctl(mt, MTIOCGET, &mg) == 0)
			tapeblock = mg.mt_blksiz;
	}
#elif defined (__CYGWIN__)
	if ((mtst.st_mode&S_IFMT) == S_IFCHR) {
		struct mtget	mg;
		if (ioctl(mt, MTIOCGET, &mg) == 0)
			tapeblock = (mg.mt_dsreg&MT_ST_BLKSIZE_MASK) >>
					MT_ST_BLKSIZE_SHIFT;
	}
#elif defined (__hpux) || defined (_AIX)
#endif	/* SVR4.2MP */
	if (blksiz == 0)
		switch (mtst.st_mode&S_IFMT) {
		case S_IFREG:
		case S_IFBLK:
			blksiz = 4096;
			break;
		case S_IFCHR:
			if (action == 'o' && !Aflag) {
				if (pax != PAX_TYPE_CPIO) {
					if (fmttype & TYP_PAX)
						blksiz = 5120;
					else if (fmttype & TYP_TAR)
						blksiz = 10240;
					else if (fmttype & TYP_CPIO)
						blksiz = 5120;
					else
						blksiz = 512;
				} else
					blksiz = tapeblock>0 ? tapeblock : 512;
			} else
				blksiz = tapeblock > 0 && tapeblock % 1024 ?
					tapeblock : tapeblock > 10240 ?
					tapeblock : 10240;
			break;
		default:
			blksiz = 512;
		}
}

/*
 * Skip tape data such that size becomes aligned to pad.
 */
static int
skippad(unsigned long long size, int pad)
{
	char	b[512];
	int	to;

	if ((to = size % pad) != 0) {
		if (bread(b, pad - to) != pad - to)
			unexeoa();
	}
	return 0;
}

static int
allzero(const char *bp, int n)
{
	int	i;

	for (i = 0; i < n; i++)
		if (bp[i] != '\0')
			return 0;
	return 1;
}

#define	CACHESIZE	16

static const char *
getuser(uid_t uid)
{
	static struct {
		char	*name;
		uid_t	uid;
	} cache[CACHESIZE];
	static int	last;
	int	i;
	struct passwd	*pwd;
	const char	*name;

	for (i = 0; i < CACHESIZE && cache[i].name; i++)
		if (cache[i].uid == uid)
			goto found;
	if ((pwd = getpwuid(uid)) != NULL)
		name = pwd->pw_name;
	else
		name = "";
	if (i >= CACHESIZE) {
		if (last >= CACHESIZE)
			last = 0;
		i = last++;
	}
	if (cache[i].name)
		free(cache[i].name);
	cache[i].name = sstrdup(name);
	cache[i].uid = uid;
found:	return cache[i].name[0] ? cache[i].name : NULL;
}

static const char *
getgroup(gid_t gid)
{
	static struct {
		char	*name;
		gid_t	gid;
	} cache[CACHESIZE];
	static int	last;
	int	i;
	struct group	*grp;
	const char	*name;

	for (i = 0; i < CACHESIZE && cache[i].name; i++)
		if (cache[i].gid == gid)
			goto found;
	if ((grp = getgrgid(gid)) != NULL)
		name = grp->gr_name;
	else
		name = "";
	if (i >= CACHESIZE) {
		if (last >= CACHESIZE)
			last = 0;
		i = last++;
	}
	if (cache[i].name)
		free(cache[i].name);
	cache[i].name = sstrdup(name);
	cache[i].gid = gid;
found:	return cache[i].name[0] ? cache[i].name : NULL;
}

/*
 * Return a version of the passed string that contains at most one '%d'
 * and no other printf formats.
 */
char *
oneintfmt(const char *op)
{
	char	*new, *np;
	int	no = 0;

	np = new = smalloc(2 * strlen(op) + 1);
	do {
		if (*op == '%') {
			*np++ = *op++;
			if (*op != '%')
				if (*op != 'd' || no++)
					*np++ = '%';
		}
		*np++ = *op;
	} while (*op++);
	return new;
}

char *
sstrdup(const char *op)
{
	char	*np;

	np = smalloc(strlen(op) + 1);
	strcpy(np, op);
	return np;
}

/*
 * Add this pattern to the extraction list with -i.
 */
void
addg(const char *pattern, int art)
{
	struct glist	*gp;

	gp = scalloc(1, sizeof *gp);
	if (pax == PAX_TYPE_CPIO && pattern[0] == '!') {
		gp->g_not = 1;
		pattern++;
	}
	gp->g_pat = sstrdup(pattern);
	gp->g_art = art;
	if (pax != PAX_TYPE_CPIO) {
		struct glist	*gb = NULL, *gc;
		for (gc = patterns; gc; gc = gc->g_nxt)
			gb = gc;
		if (gb)
			gb->g_nxt = gp;
		else
			patterns = gp;
	} else {
		gp->g_nxt = patterns;
		patterns = gp;
	}
}

/*
 * Check if the file name s matches any of the given patterns.
 */
static struct glist *
want(struct file *f, struct glist **gb)
{
	extern int	gmatch(const char *, const char *);
	struct glist	*gp;

	for (gp = patterns; gp; gp = gp->g_nxt) {
		if ((gmatch(f->f_name, gp->g_pat) != 0) ^ gp->g_not &&
				(pax_nflag == 0 || gp->g_gotcha == 0)) {
			return gp;
		}
		*gb = gp;
	}
	return NULL;
}

static void
patfile(void)
{
	struct iblok	*ip;
	char	*name = NULL;
	size_t	namsiz = 0, namlen;

	if ((ip = ib_open(Eflag, 0)) == NULL)
		msg(3, -2, "Cannot open \"%s\" to read patterns\n", Eflag);
	while ((namlen = ib_getlin(ip, &name, &namsiz, srealloc)) != 0) {
		if (name[namlen-1] == '\n')
			name[--namlen] = '\0';
		addg(name, 0);
	}
	ib_close(ip);
}

void
swap(char *b, size_t sz, int s8, int s16)
{
	uint8_t	u8;
	uint16_t	u16;
	union types2	*t2;
	union types4	*t4;
	int	i;

	if (s8) {
		for (i = 0; i < (sz >> 1); i++) {
			t2 = &((union types2 *)b)[i];
			u8 = t2->byte[0];
			t2->byte[0] = t2->byte[1];
			t2->byte[1] = u8;
		}
	}
	if (s16) {
		for (i = 0; i < (sz >> 2); i++) {
			t4 = &((union types4 *)b)[i];
			u16 = t4->sword[0];
			t4->sword[0] = t4->sword[1];
			t4->sword[1] = u16;
		}
	}
}

static int
ckodd(long long size, int mod, const char *str, const char *fn)
{
	if (size % mod) {
		msg(3, 0, "Cannot swap %s of \"%s\", odd number of %s\n",
				str, fn, str);
		errcnt++;
		return 1;
	}
	return 0;
}

/*
 * Interactive rename (-r option).
 */
static int
rname(char **oldp, size_t *olds)
{
	char	*new = NULL;
	size_t	newsize = 0;
	int	i, r;
	char	c;

	fprintf(stderr, "Rename \"%s\"? ", *oldp);
	if (tty == 0)
		if ((tty = open("/dev/tty", O_RDWR)) < 0 ||
				fcntl(tty, F_SETFD, FD_CLOEXEC) < 0)
		err:	msg(3, 1, "Cannot read tty.\n");
	i = 0;
	while ((r = read(tty, &c, 1)) == 1 && c != '\n') {
		if (i+1 >= newsize)
			new = srealloc(new, newsize += 32);
		new[i++] = c;
	}
	if (r <= 0)
		goto err;
	if (new == NULL)
		return 0;
	new[i] = '\0';
	if (new[0] == '.' && new[1] == '\0') {
		free(new);
	} else {
		free(*oldp);
		*oldp = new;
		*olds = newsize;
	}
	return 1;
}

/*
 * Filter data from tape through the commands given in arg?.
 */
static int
redirect(const char *arg0, const char *arg1)
{
	int	pd[2];

	if (pipe(pd) < 0)
		return -1;
	switch (fork()) {
	case 0:
		if (tapeblock>=0 || lseek(mt, -blktop, SEEK_CUR) == (off_t)-1) {
			int	xpd[2];
			if (pipe(xpd) == 0 && fork() == 0) {
				ssize_t	rd, wo, wt;
				close(xpd[0]);
				do {
					wo = wt = 0;
					do {
						if ((wo = write(xpd[1],
								blkbuf + wt,
								blktop - wt))
									<= 0) {
							if (errno == EINTR)
								continue;
							_exit(0);
						}
						wt += wo;
					} while (wt < blktop);
				} while ((rd = mread()) >= 0);
				if (rd < 0) {
					emsg(3, "Read error on \"%s\"",
							Iflag && !sysv3 ?
							Iflag : "input");
				}
				_exit(0);
			} else {
				close(xpd[1]);
				dup2(xpd[0], 0);
				close(xpd[0]);
			}
		} else {
			dup2(mt, 0);
		}
		close(mt);
		dup2(pd[1], 1);
		close(pd[0]);
		close(pd[1]);
		execlp(arg0, arg0, arg1, NULL);
		fprintf(stderr, "%s: could not exec %s: %s\n",
				progname, arg0, strerror(errno));
		_exit(0177);
		/*NOTREACHED*/
	default:
		dup2(pd[0], mt);
		close(pd[0]);
		close(pd[1]);
		tapeblock = -1;
		break;
	case -1:
		return -1;
	}
	return 0;
}

/*
 * Get the name stored in a tar header. buf is expected to be at least
 * TPFXSIZ+TNAMSIZ+2 bytes.
 */
static char *
tnameof(struct tar_header *hp, char *buf)
{
	const char	*cp;
	char	*bp = buf;

	if (fmttype & TYP_USTAR && fmttype != FMT_GNUTAR &&
			hp->t_prefix[0] != '\0') {
		cp = hp->t_prefix;
		while (cp < &hp->t_prefix[TPFXSIZ] && *cp)
			*bp++ = *cp++;
		if (bp > buf)
			*bp++ = '/';
	}
	cp = hp->t_name;
	while (cp < &hp->t_name[TNAMSIZ] && *cp)
		*bp++ = *cp++;
	*bp = '\0';
	return buf;
}

/*
 * Store fn as file name in a tar header.
 */
static int
tmkname(struct tar_header *hp, const char *fn)
{
	const char	*cp, *cs = NULL;

	for (cp = fn; *cp; cp++) {
		if (fmttype & TYP_USTAR && *cp == '/' && cp[1] != '\0' &&
				cp > fn && cp-fn <= TPFXSIZ)
			cs = cp;
	}
	if (fmttype == FMT_GNUTAR && cp - fn > 99) {
		writegnuname(fn, cp - fn + 1, 'L');
		cp = &fn[99];
	} else if (cp - (cs ? &cs[1] : fn) > TNAMSIZ) {
		if (fmttype & TYP_PAX && utf8(fn)) {
			paxrec |= PR_PATH;
			strcpy(hp->t_name, sequence());
			return 0;
		}
		msg(3, 0, "%s: file name too long\n", fn);
		return -1;
	}
	if (cs && cp - fn > TNAMSIZ) {
		memcpy(hp->t_prefix, fn, cs - fn);
		if (cs - fn < TPFXSIZ)
			hp->t_prefix[cs - fn] = '\0';
		memcpy(hp->t_name, &cs[1], cp - &cs[1]);
		if (cp - &cs[1] < TNAMSIZ)
			hp->t_name[cp - &cs[1]] = '\0';
	} else {
		memcpy(hp->t_name, fn, cp - fn);
		if (cp - fn < TNAMSIZ)
			hp->t_name[cp - fn] = '\0';
	}
	return 0;
}

/*
 * Get the link name of a tar header.
 */
static void
tlinkof(struct tar_header *hp, struct file *f)
{
	const char	*cp;
	char	*bp;


	if (f->f_lsiz < TNAMSIZ+1)
		f->f_lnam = srealloc(f->f_lnam, f->f_lsiz = TNAMSIZ+1);
	cp = hp->t_linkname;
	bp = f->f_lnam;
	while (cp < &hp->t_linkname[TNAMSIZ] && *cp)
		*bp++ = *cp++;
	*bp = '\0';
}

/*
 * Create the link name in a tar header.
 */
static int
tmklink(struct tar_header *hp, const char *fn)
{
	const char	*cp;

	for (cp = fn; *cp; cp++);
	if (fmttype == FMT_GNUTAR && cp - fn > 99) {
		writegnuname(fn, cp - fn + 1, 'K');
		cp = &fn[99];
	} else if (cp - fn > TNAMSIZ) {
		if (fmttype & TYP_PAX && utf8(fn)) {
			paxrec |= PR_LINKPATH;
			strcpy(hp->t_linkname, sequence());
			return 0;
		}
		msg(3, 0, "%s: linked name too long\n", fn);
		return -1;
	}
	memcpy(hp->t_linkname, fn, cp - fn);
	if (cp - fn < TNAMSIZ)
		hp->t_linkname[cp - fn] = '\0';
	return 0;
}

static int
tlflag(struct stat *st)
{
	if (fmttype & TYP_BAR) {
		switch (st->st_mode & S_IFMT) {
		case S_IFREG:
		case S_IFDIR:
			return '0';
		case S_IFLNK:
			return '2';
		default:
			return '3';
		}
	} else if (fmttype & TYP_USTAR) {
		switch (st->st_mode & S_IFMT) {
		case S_IFREG:
			return '0';
		case S_IFLNK:
			return '2';
		case S_IFCHR:
			return '3';
		case S_IFBLK:
			return '4';
		case S_IFDIR:
			return '5';
		case S_IFIFO:
			return '6';
		default:
			return -1;
		}
	} else {
		switch (st->st_mode & S_IFMT) {
		case S_IFREG:
			return '\0';
		case S_IFLNK:
			return '2';
		default:
			return -1;
		}
	}
}

/*
 * Ustar checksums are created using unsigned chars, as specified by
 * POSIX. Traditional tar implementations use signed chars. Some
 * implementations (notably SVR4 cpio derivatives) use signed chars
 * even for ustar archives, but this is clearly an implementation bug.
 */
static void
tchksum(union bincpio *bp)
{
	uint32_t	sum;
	char	*cp;

	memset(bp->Tdr.t_chksum, ' ', sizeof bp->Tdr.t_chksum);
	sum = 0;
	if (fmttype & TYP_USTAR)
		for (cp = bp->data; cp < &bp->data[512]; cp++)
			sum += *((unsigned char *)cp);
	else
		for (cp = bp->data; cp < &bp->data[512]; cp++)
			sum += *((signed char *)cp);
	sprintf(bp->Tdr.t_chksum, "%7.7o", sum);
}

static int
tcssum(union bincpio *bp, int ustar)
{
	uint32_t	ssum = 0, usum = 0, osum;
	char	ochk[sizeof bp->Tdr.t_chksum];
	char	*cp;

	osum = rdoct(bp->Tdr.t_chksum, 8);
	memcpy(ochk, bp->Tdr.t_chksum, sizeof ochk);
	memset(bp->Tdr.t_chksum, ' ', sizeof bp->Tdr.t_chksum);
	for (cp = bp->data; cp < &bp->data[512]; cp++) {
		ssum += *((signed char *)cp);
		usum += *((unsigned char *)cp);
	}
	memcpy(bp->Tdr.t_chksum, ochk, sizeof bp->Tdr.t_chksum);
	return ssum != osum && usum != osum;
}

static int
trdsum(union bincpio *bp)
{
	int	i;

	if (fmttype & TYP_BAR)
		i = bcssum(bp);
	else
		i = tcssum(bp, fmttype & TYP_USTAR);
	if (i)
		msg(3, 0, "Bad header - checksum error.\n");
	return i;
}

static mode_t
tifmt(int c)
{
	switch (c) {
	default:
	case '\0':
	case '0':
		return S_IFREG;
	case '2':
		return S_IFLNK;
	case '3':
		return S_IFCHR;
	case '4':
		return S_IFBLK;
	case '5':
		return S_IFDIR;
	case '6':
		return S_IFIFO;
	}
}

/*
 * bar format support functions.
 */
static void
bchksum(union bincpio *bp)
{
	uint32_t	sum;
	char	*cp;

	memset(bp->Bdr.b_chksum, ' ', sizeof bp->Bdr.b_chksum);
	sum = 0;
	for (cp = bp->data; cp < &bp->data[512]; cp++)
		sum += *((signed char *)cp);
	sprintf(bp->Bdr.b_chksum, "%7.7o", sum);
}

static int
bcssum(union bincpio *bp)
{
	uint32_t	sum, osum;
	char	ochk[sizeof bp->Bdr.b_chksum];
	char	*cp;

	osum = rdoct(bp->Bdr.b_chksum, 8);
	memcpy(ochk, bp->Bdr.b_chksum, sizeof ochk);
	memset(bp->Bdr.b_chksum, ' ', sizeof bp->Bdr.b_chksum);
	sum = 0;
	for (cp = bp->data; cp < &bp->data[512]; cp++)
		sum += *((signed char *)cp);
	memcpy(bp->Bdr.b_chksum, ochk, sizeof bp->Bdr.b_chksum);
	return sum != osum;
}

static void
blinkof(const char *cp, struct file *f, int namlen)
{
	if (f->f_lsiz < 512)
		f->f_lnam = srealloc(f->f_lnam, f->f_lsiz = 512);
	strcpy(f->f_lnam, &cp[SIZEOF_bar_header + namlen]);
}

static void
dump_barhdr(void)
{
	union bincpio	bc;
	static int	volno;
	static time_t	now = -1;

	memset(&bc, 0, 512);
	sprintf(bc.Bdr.b_uid, "%d", myuid & 07777777);
	sprintf(bc.Bdr.b_gid, "%d", mygid & 07777777);
	bc.Bdr.b_size[0] = '0';
	memcpy(bc.Bdr.b_bar_magic, mag_bar, sizeof bc.Bdr.b_bar_magic);
	sprintf(bc.Bdr.b_volume_num, "%d", ++volno & 0777);
	bc.Bdr.b_compressed = '0';
	if (now == (time_t)-1)
		time(&now);
	sprintf(bc.Bdr.b_date, "%llo", now & 077777777777LL);
	bchksum(&bc);
	bwrite(bc.data, 512);
}

/*
 * Support for compressed bar format (any regular file is piped through zcat).
 */
static pid_t	zpid;

static int
zcreat(const char *name, mode_t mode)
{
	int	pd[2];
	int	fd;

	if (pipe(pd) < 0)
		return -1;
	if ((fd = creat(name, mode)) < 0) {
		fd = errno;
		close(pd[0]);
		close(pd[1]);
		errno = fd;
		return -1;
	}
	switch (zpid = fork()) {
	case -1:
		return -1;
	case 0:
		dup2(pd[0], 0);
		dup2(fd, 1);
		close(pd[0]);
		close(pd[1]);
		close(fd);
		execlp("zcat", "zcat", NULL);
		_exit(0177);
		/*NOTREACHED*/
	}
	close(pd[0]);
	close(fd);
	sigset(SIGPIPE, SIG_IGN);
	return pd[1];
}

static int
zclose(int fd)
{
	int	c, s;

	c = close(fd);
	while (waitpid(zpid, &s, 0) != zpid);
	return c != 0 || s != 0 ? -1 : 0;
}

/*
 * If using the -A option, device numbers that appear in the archive
 * are not reused for appended files. This avoids wrong hardlink
 * connections on extraction.
 *
 * In fact, this should be done even if we did not fake device and
 * inode numbers, since it is not guaranteed that the archive was
 * created on the same machine, or even on the same machine, inode
 * number could have changed after a file was unlinked.
 */
static void
markdev(dev_t dev)
{
	struct dslot	*dp, *dq = NULL;;

	for (dp = markeddevs; dp; dp = dp->d_nxt) {
		if (dp->d_dev == dev)
			return;
		dq = dp;
	}
	dp = scalloc(1, sizeof *dp);
	dp->d_dev = dev;
	if (markeddevs == NULL)
		markeddevs = dp;
	else
		dq->d_nxt = dp;
}

static int
marked(dev_t dev)
{
	struct dslot	*dp;

	for (dp = markeddevs; dp; dp = dp->d_nxt)
		if (dp->d_dev == dev)
			return 1;
	return 0;
}

static void
cantsup(int err, const char *file)
{
	if (sysv3)
		msg(err ? 3 : 2, 0,
			"format can't support expanded types on %s\n",
			file);
	else
		msg(0, 0, "%s format can't support expanded types on %s\n",
			err ? "Error" : "Warning", file);
	errcnt++;
}

static void
onint(int signo)
{
	if (cur_ofile && cur_tfile && rename(cur_tfile, cur_ofile) < 0)
		emsg(3, "Cannot recover original \"%s\"", cur_ofile);
	if (cur_ofile && cur_tfile == NULL && unlink(cur_ofile) < 0)
		emsg(3, "Cannot remove incomplete \"%s\"", cur_ofile);
	exit(signo | 0200);
}

/*
 * Read the compressed zip data part for a file.
 */
static int
zipread(struct file *f, const char *tgt, int tfd, int doswap)
{
	int	val = 0;
	uint32_t	crc = 0;

	if (f->f_gflag & FG_DESC) {
		if (f->f_cmethod != C_DEFLATED && f->f_cmethod != C_ENHDEFLD ||
				f->f_gflag & FG_CRYPT)
			msg(4, 1, "Cannot handle zip data descriptor\n");
		f->f_csize = 0x7FFFFFFFFFFFFFFFLL;
		f->f_st.st_size = 0x7FFFFFFFFFFFFFFFLL;
	} else if (tfd < 0)
		return skipfile(f);
	if (f->f_gflag & FG_CRYPT)
		return cantunzip(f, "encrypted");
	switch (f->f_cmethod) {
	case C_DEFLATED:
	case C_ENHDEFLD:
		val = zipinflate(f, tgt, tfd, doswap, &crc);
		break;
	case C_SHRUNK:
		val = zipunshrink(f, tgt, tfd, doswap, &crc);
		break;
	case C_IMPLODED:
		val = zipexplode(f, tgt, tfd, doswap, &crc);
		break;
	case C_REDUCED1:
	case C_REDUCED2:
	case C_REDUCED3:
	case C_REDUCED4:
		val = zipexpand(f, tgt, tfd, doswap, &crc);
		break;
	case C_TOKENIZED:
		return cantunzip(f, "tokenized");
	case C_DCLIMPLODED:
		val = zipblast(f, tgt, tfd, doswap, &crc);
		break;
	case C_BZIP2:
#if USE_BZLIB
		val = zipunbz2(f, tgt, tfd, doswap, &crc);
		break;
#else	/* !USE_BZLIB */
		return cantunzip(f, "bzip2 compressed");
#endif	/* !USE_BZLIB */
	default:
		return cantunzip(f, "compressed");
	}
	if (f->f_gflag & FG_DESC)
		zipreaddesc(f);
	if (val == 0 && crc != f->f_chksum) {
		msg(3, 0, "\"%s\" - checksum error\n", f->f_name);
		return -1;
	}
	return val;
}

/*
 * Read a zip data descriptor (i. e. a field after the compressed data
 * that contains the actual values for sizes and crc).
 */
static void
zipreaddesc(struct file *f)
{
	if (f->f_oflag & OF_ZIP64) {
		struct	zipddesc64	zd64;
		bread((char *)&zd64, SIZEOF_zipddesc64);
		if (memcmp(zd64.zd_signature, mag_zipdds, sizeof mag_zipdds))
			msg(4, 1, "Invalid zip data descriptor\n");
		f->f_chksum = ple32(zd64.zd_crc32);
		f->f_dsize = f->f_st.st_size = ple64(zd64.zd_nsize) &
			0xFFFFFFFFFFFFFFFFULL;
		f->f_csize = ple64(zd64.zd_csize) &
			0xFFFFFFFFFFFFFFFFULL;
	} else {
		struct	zipddesc zd;
		bread((char *)&zd, SIZEOF_zipddesc);
		if (memcmp(zd.zd_signature, mag_zipdds, sizeof mag_zipdds))
			msg(4, 1, "Invalid zip data descriptor\n");
		f->f_chksum = ple32(zd.zd_crc32);
		f->f_dsize = f->f_st.st_size = ple32(zd.zd_nsize)&0xFFFFFFFFUL;
		f->f_csize = ple32(zd.zd_csize)&0xFFFFFFFFUL;
	}
}

static int
cantunzip(struct file *f, const char *method)
{
	msg(3, 0, "Cannot unzip %s file \"%s\"\n", method, f->f_name);
	errcnt++;
	return skipfile(f);
}

/*
 * PC-DOS time format:
 *
 * 31            24      20        15        10           4       0
 * | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
 * |               |               |               |               |
 * |year         |months |days     |hours    |minutes    |biseconds|
 *
 * refers to local time on the machine it was created on.
 */

static time_t
gdostime(const char *tp, const char *dp)
{
	uint32_t	v;
	struct tm	tm;

	v = (int)(tp[0]&0377) +
		((int)(tp[1]&0377) << 8) +
		((int)(dp[0]&0377) << 16) +
		((int)(dp[1]&0377) << 24);
	memset(&tm, 0, sizeof tm);
	tm.tm_sec = (v&0x1F) << 1;
	tm.tm_min = (v&0x7E0) >> 5;
	tm.tm_hour = (v&0xF800) >> 11;
	tm.tm_mday = ((v&0x1F0000) >> 16);
	tm.tm_mon = ((v&0x1E00000) >> 21) - 1;
	tm.tm_year = ((v&0xFE000000) >> 25) + 80;
	tm.tm_isdst = -1;
	return mktime(&tm);
}

static void
mkdostime(time_t t, char *tp, char *dp)
{
	uint32_t	v;
	struct tm	*tm;

	tm = localtime(&t);
	v = (tm->tm_sec >> 1) + (tm->tm_sec&1) + (tm->tm_min << 5) +
		(tm->tm_hour << 11) + (tm->tm_mday << 16) +
		((tm->tm_mon+1) << 21) + ((tm->tm_year - 80) << 25);
	le16p(v&0x0000ffff, tp);
	le16p((v&0xffff0000) >> 16, dp);
}

/*
 * Read and interpret the zip extra field for a file.
 */
static ssize_t
ziprxtra(struct file *f, struct zip_header *z)
{
	union zextra	*x, *xp;
	short	tag, size;
	ssize_t	len;

	len = ple16(z->z_extralen)&0177777;
	if (len > 0) {
		x = smalloc(len);
		if (bread((char *)x, len) != len)
			return -1;
		if (len < 4)
			return len;
		xp = x;
		while (len > 0) {
			if (len < 4)
				return -1;
			tag = ple16(xp->Ze_gn.ze_gn_tag);
			size = (ple16(xp->Ze_gn.ze_gn_tsize)&0177777) + 4;
			switch (tag) {
			case mag_zip64f:	/* ZIP64 extended information */
				if (size != SIZEOF_zextra_64 &&
						size != SIZEOF_zextra_64_a &&
						size != SIZEOF_zextra_64_b)
					break;
				if (f->f_st.st_size == 0xffffffff)
					f->f_st.st_size =
						ple64(xp->Ze_64.ze_64_nsize);
				if (f->f_csize == 0xffffffff)
					f->f_csize =
						ple64(xp->Ze_64.ze_64_csize);
				f->f_oflag |= OF_ZIP64;
				break;
			case 0x000d:	/* PKWARE Unix Extra Field */
				if (size != SIZEOF_zextra_pk)
					break;
				f->f_st.st_atime = ple32(xp->Ze_pk.ze_pk_atime);
				f->f_st.st_mtime = ple32(xp->Ze_pk.ze_pk_mtime);
				f->f_st.st_uid = ple16(xp->Ze_pk.ze_pk_uid) &
					0177777;
				f->f_st.st_gid = ple16(xp->Ze_pk.ze_pk_gid) &
					0177777;
				break;
			case 0x5455:	/* Extended Timestamp Extra Field */
				if (xp->Ze_et.ze_et_flags[0] & 1)
					f->f_st.st_atime =
						ple32(xp->Ze_et.ze_et_atime);
				if (xp->Ze_et.ze_et_flags[0] & 2)
					f->f_st.st_mtime =
						ple32(xp->Ze_et.ze_et_mtime);
				if (xp->Ze_et.ze_et_flags[0] & 3)
					f->f_st.st_ctime =
						ple32(xp->Ze_et.ze_et_ctime);
				break;
			case 0x5855:	/* Info-ZIP Unix Extra Field #1 */
				if (size != SIZEOF_zextra_i1)
					break;
				f->f_st.st_atime = ple32(xp->Ze_i1.ze_i1_atime);
				f->f_st.st_mtime = ple32(xp->Ze_i1.ze_i1_mtime);
				f->f_st.st_uid = ple16(xp->Ze_i1.ze_i1_uid) &
					0177777;
				f->f_st.st_gid = ple16(xp->Ze_i1.ze_i1_gid) &
					0177777;
				break;
			case 0x7855:	/* Info-ZIP Unix Extra Field #2 */
				if (size != SIZEOF_zextra_i2)
					break;
				f->f_st.st_uid = ple16(xp->Ze_i2.ze_i2_uid) &
					0177777;
				f->f_st.st_gid = ple16(xp->Ze_i2.ze_i2_gid) &
					0177777;
				break;
			case 0x756e:	/* ASi Unix Extra Field */
				if (size < SIZEOF_zextra_as)
					break;
				f->f_st.st_mode = ple16(xp->Ze_as.ze_as_mode);
				f->f_st.st_uid = ple16(xp->Ze_as.ze_as_uid) &
					0177777;
				f->f_st.st_gid = ple16(xp->Ze_as.ze_as_gid) &
					0177777;
				if ((f->f_st.st_mode&S_IFMT) == S_IFLNK) {
					if (f->f_lsiz < size-14+1)
						f->f_lnam = srealloc(f->f_lnam,
								f->f_lsiz =
								size-18+1);
					memcpy(f->f_lnam, &((char *)xp)[18],
							size-18);
					f->f_lnam[size-18] = '\0';
					f->f_st.st_size = size-18;
				} else {
					f->f_st.st_rdev =
						ple32(xp->Ze_as.ze_as_sizdev);
					f->f_rmajor = major(f->f_st.st_rdev);
					f->f_rminor = minor(f->f_st.st_rdev);
				}
				break;
			case mag_zipcpio:
				if (size != SIZEOF_zextra_cp)
					break;
				f->f_st.st_dev = ple32(xp->Ze_cp.ze_cp_dev);
				f->f_st.st_ino = ple32(xp->Ze_cp.ze_cp_ino);
				f->f_st.st_mode = ple32(xp->Ze_cp.ze_cp_mode);
				f->f_st.st_uid = ple32(xp->Ze_cp.ze_cp_uid) &
					0xFFFFFFFFUL;
				f->f_st.st_gid = ple32(xp->Ze_cp.ze_cp_gid) &
					0xFFFFFFFFUL;
				f->f_st.st_nlink = ple32(xp->Ze_cp.ze_cp_nlink)&
					0xFFFFFFFFUL;
				f->f_st.st_rdev = ple32(xp->Ze_cp.ze_cp_rdev);
				f->f_rmajor = major(f->f_st.st_rdev);
				f->f_rminor = minor(f->f_st.st_rdev);
				f->f_st.st_mtime = ple32(xp->Ze_cp.ze_cp_mtime);
				f->f_st.st_atime = ple32(xp->Ze_cp.ze_cp_atime);
				break;
			}
			xp = (union zextra *)&((char *)xp)[size];
			len -= size;
		}
		free(x);
	}
	return len;
}

/*
 * Write the central directory and the end of a zip file.
 */
static void
ziptrailer(void)
{
	struct zipstuff	*zs;
	struct zipcentral	zc;
	struct zipend	ze;
	long long	cpstart, cpend, entries = 0;
	size_t	sz;

	cpstart = nwritten;
	for (zs = zipbulk; zs; zs = zs->zs_next) {
		entries++;
		memset(&zc, 0, SIZEOF_zipcentral);
		memcpy(zc.zc_signature, mag_zipctr, 4);
		zc.zc_versionmade[0] = 20;
		zc.zc_versionextr[0] = zs->zs_cmethod == 8 ? 20 : 10;
		mkdostime(zs->zs_mtime, zc.zc_modtime, zc.zc_moddate);
		le32p(zs->zs_crc32, zc.zc_crc32);
		le16p(zs->zs_cmethod, zc.zc_cmethod);
		le16p(zs->zs_gflag, zc.zc_gflag);
		/*
		 * We flag files as created on PC-DOS / FAT filesystem
		 * and thus set PC-DOS attributes here.
		 */
		if ((zs->zs_mode&0222) == 0)
			zc.zc_external[0] |= 0x01;	/* readonly attribute */
		if ((zs->zs_mode&S_IFMT) == S_IFDIR)
			zc.zc_external[0] |= 0x10;	/* directory attr. */
		sz = strlen(zs->zs_name);
		le16p(sz, zc.zc_namelen);
		if (zs->zs_size >= 0xffffffff || zs->zs_csize >= 0xffffffff ||
				zs->zs_relative >= 0xffffffff) {
			struct zextra_64	zf;

			memset(&zf, 0, SIZEOF_zextra_64);
			le16p(mag_zip64f, zf.ze_64_tag);
			le16p(SIZEOF_zextra_64 - 4, zf.ze_64_tsize);
			if ((zs->zs_mode&S_IFMT) == S_IFREG ||
					(zs->zs_mode&S_IFMT) == S_IFLNK) {
				le32p(0xffffffff, zc.zc_csize);
				le32p(0xffffffff, zc.zc_nsize);
				le64p(zs->zs_csize, zf.ze_64_csize);
				le64p(zs->zs_size, zf.ze_64_nsize);
			}
			le64p(zs->zs_relative, zf.ze_64_reloff);
			le32p(0xffffffff, zc.zc_relative);
			le16p(SIZEOF_zextra_64, zc.zc_extralen);
			bwrite((char *)&zc, SIZEOF_zipcentral);
			bwrite(zs->zs_name, sz);
			bwrite((char *)&zf, SIZEOF_zextra_64);
		} else {
			if ((zs->zs_mode&S_IFMT) == S_IFREG ||
					(zs->zs_mode&S_IFMT) == S_IFLNK) {
				le32p(zs->zs_csize, zc.zc_csize);
				le32p(zs->zs_size, zc.zc_nsize);
			}
			le32p(zs->zs_relative, zc.zc_relative);
			bwrite((char *)&zc, SIZEOF_zipcentral);
			bwrite(zs->zs_name, sz);
		}
	}
	cpend = nwritten;
	memset(&ze, 0, SIZEOF_zipend);
	memcpy(ze.ze_signature, mag_zipend, 4);
	if (cpend >= 0xffffffff || entries >= 0xffff) {
		struct zip64end	z6;
		struct zip64loc	z4;

		memset(&z6, 0, SIZEOF_zip64end);
		memcpy(z6.z6_signature, mag_zip64e, 4);
		le64p(SIZEOF_zip64end - 12, z6.z6_recsize);
		z6.z6_versionmade[0] = 20;
		z6.z6_versionextr[0] = 20;
		le64p(entries, z6.z6_thisentries);
		le64p(entries, z6.z6_allentries);
		le64p(cpend - cpstart, z6.z6_dirsize);
		le64p(cpstart, z6.z6_startsize);
		bwrite((char *)&z6, SIZEOF_zip64end);
		memset(&z4, 0, SIZEOF_zip64loc);
		memcpy(z4.z4_signature, mag_zip64l, 4);
		le64p(cpend, z4.z4_reloff);
		le32p(1, z4.z4_alldiskn);
		bwrite((char *)&z4, SIZEOF_zip64loc);
		le16p(0xffff, ze.ze_thisentries);
		le16p(0xffff, ze.ze_allentries);
		le32p(0xffffffff, ze.ze_dirsize);
		le32p(0xffffffff, ze.ze_startsize);
	} else {
		le16p(entries, ze.ze_thisentries);
		le16p(entries, ze.ze_allentries);
		le32p(cpend - cpstart, ze.ze_dirsize);
		le32p(cpstart, ze.ze_startsize);
	}
	bwrite((char *)&ze, SIZEOF_zipend);
}

/*
 * Store the data later needed for the central directory.
 */
static void
zipdefer(const char *fn, struct stat *st, long long relative,
		uint32_t crc, long long csize, const struct zip_header *zh)
{
	struct zipstuff	*zp;

	zp = scalloc(1, sizeof *zp);
	zp->zs_name = sstrdup(fn);
	zp->zs_size = st->st_size;
	zp->zs_mtime = st->st_mtime;
	zp->zs_mode = st->st_mode;
	zp->zs_relative = relative;
	zp->zs_cmethod = ple16(zh->z_cmethod);
	zp->zs_gflag = ple16(zh->z_gflag);
	zp->zs_csize = csize;
	zp->zs_crc32 = crc;
	zp->zs_next = zipbulk;
	zipbulk = zp;
}

#define	ziptrlevel()	( \
	zipclevel == 01 ? 9 : 	/* maximum */	\
	zipclevel == 02 ? 3 : 	/* fast */	\
	zipclevel == 03 ? 1 :	/* super fast */\
	/*zipclevel==00*/ 6  	/* normal */	\
)

/*
 * Write (and compress) data for a regular file to a zip archive.
 */
static int
zipwrite(int fd, const char *fn, struct stat *st, union bincpio *bp, size_t sz,
		uint32_t dev, uint32_t ino, uint32_t *crc, long long *csize)
{
#if USE_ZLIB
	struct z_stream_s	z;
	int	i;
	size_t	osize = 0;
#endif	/* USE_ZLIB */
	char	*ibuf, *obuf = 0;

	if (st->st_size > 196608 || (ibuf = malloc(st->st_size)) == 0) {
#if USE_ZLIB
		if (zipclevel < 04)
			return zipwdesc(fd, fn, st, bp, sz, dev, ino,
					crc, csize);
#endif	/* USE_ZLIB */
		return zipwtemp(fd, fn, st, bp, sz, dev, ino, crc, csize);
	}
	*csize = 0;
	if (read(fd, ibuf, st->st_size) != st->st_size) {
		free(ibuf);
		emsg(3, "Cannot read \"%s\"", fn);
		close(fd);
		return -1;
	}
	*crc = zipcrc(0, (unsigned char *)ibuf, st->st_size);
#if USE_BZLIB
	if (zipclevel == 07) {
		unsigned int	sb;
		if ((obuf = malloc(sb = st->st_size)) == 0)
			goto store;
		if (BZ2_bzBuffToBuffCompress(obuf, &sb, ibuf, st->st_size,
					9, 0, 0) != BZ_OK)
			goto store;
		*csize = sb;
		bp->Zdr.z_cmethod[0] = C_BZIP2;
		bp->Zdr.z_version[0] = 0x2e;
		goto out;
	}
#endif /* USE_BZLIB */
	if (zipclevel > 03)
		goto store;
#if USE_ZLIB
	memset(&z, 0, sizeof z);
	if (deflateInit2(&z, ziptrlevel(), Z_DEFLATED, -15,
			8, Z_DEFAULT_STRATEGY) < 0)
		goto store;
	z.next_in = (unsigned char *)ibuf;
	z.avail_in = z.total_in = st->st_size;
	do {
		if (z.avail_out == 0) {
			if ((obuf = realloc(obuf, osize += 4096)) == 0) {
				deflateEnd(&z);
				goto store;
			}
			z.next_out = (unsigned char *)&obuf[*csize];
			z.avail_out = osize - *csize;
		}
		if ((i = deflate(&z, z.avail_in ? Z_NO_FLUSH : Z_FINISH)) < 0) {
			deflateEnd(&z);
			goto store;
		}
		*csize = osize - z.avail_out;
	} while (z.avail_in || i != Z_STREAM_END);
	deflateEnd(&z);
	if (*csize < st->st_size) {
		bp->Zdr.z_cmethod[0] = C_DEFLATED;
		bp->Zdr.z_gflag[0] |= zipclevel << 1;
		bp->Zdr.z_version[0] = 20;
	} else
#endif	/* USE_ZLIB */
	store:	*csize = st->st_size;
#if USE_BZLIB
out:
#endif /* USE_BZLIB */
	le32p(*crc, bp->Zdr.z_crc32);
	le32p(*csize, bp->Zdr.z_csize);
	bwrite((char *)bp, SIZEOF_zip_header);
	bwrite(fn, sz);
	zipwxtra(fn, st, dev, ino);
	switch (bp->Zdr.z_cmethod[0]) {
	case C_DEFLATED:
	case C_BZIP2:
		bwrite(obuf, *csize);
		break;
	default:
		bwrite(ibuf, *csize);
	}
	free(ibuf);
	free(obuf);
	close(fd);
	return 0;
}

/*
 * Write and compress data to a zip archive for a file that is to large
 * too be kept in memory. If there is an error with the temporary file
 * (e. g. no space left on device), the file is stored in uncompressed
 * form.
 */
static int
zipwtemp(int fd, const char *fn, struct stat *st, union bincpio *bp, size_t sz,
		uint32_t dev, uint32_t ino, uint32_t *crc, long long *csize)
{
	static int	tf = -1;
	static char	tlate[] = "/var/tmp/cpioXXXXXX";
	char	ibuf[32768];
#if USE_ZLIB || USE_BZLIB
	char	obuf[32768];
#endif	/* USE_ZLIB || USE_BZLIB */
	struct zextra_64	zf;
	struct zextra_64	*zfp = 0;
	long long	size = st->st_size;
	const char	*sname;
	int	cuse, sf;
	ssize_t	rd;

	*csize = 0;
	*crc = 0;
#if USE_ZLIB || USE_BZLIB
	if (tf < 0) {
		if ((tf = mkstemp(tlate)) >= 0)
			unlink(tlate);
	} else if (lseek(tf, 0, SEEK_SET) != 0) {
		close(tf);
		tf = -1;
	}
#endif	/* USE_ZLIB || USE_BZLIB */
#if USE_ZLIB
	if (zipclevel < 04) {
		struct z_stream_s	z;
		memset(&z, 0, sizeof z);
		if ((cuse = deflateInit2(&z, ziptrlevel(), Z_DEFLATED,
				-15, 8, Z_DEFAULT_STRATEGY)) < 0)
			goto store;
		do {
			if (z.avail_in == 0 && size > 0) {
				if ((rd = read(fd, ibuf, sizeof ibuf)) <= 0) {
					emsg(3, "Cannot read \"%s\"", fn);
					close(fd);
					ftruncate(tf, 0);
					return -1;
				}
				z.next_in = (unsigned char *)ibuf;
				z.avail_in = z.total_in = rd;
				size -= rd;
				*crc = zipcrc(*crc, (unsigned char *)ibuf, rd);
			}
			if (z.next_out == NULL || (char *)z.next_out > obuf) {
				if (z.next_out && tf >= 0) {
					if (write(tf, obuf,
					    (char *)z.next_out-obuf) !=
					    (char *)z.next_out-obuf) {
						close(tf);
						tf = -1;
					}
					*csize += (char *)z.next_out - obuf;
				}
				z.next_out = (unsigned char *)obuf;
				z.avail_out = sizeof obuf;
			}
			if (cuse >= 0 && cuse != Z_STREAM_END)
				cuse = deflate(&z,
					z.avail_in?Z_NO_FLUSH:Z_FINISH);
			else
				z.avail_in = 0;
		} while (size>0 || (char *)z.next_out>obuf ||
				cuse>=0 && cuse!=Z_STREAM_END);
		deflateEnd(&z);
		goto out;
	}
#endif	/* USE_ZLIB */
#if USE_BZLIB
	if (zipclevel == 07) {
		bz_stream	bs;
		int	ok, on;
		memset(&bs, sizeof bs, 0);
		if ((ok = BZ2_bzCompressInit(&bs, 9, 0, 0)) != BZ_OK)
			goto store;
		cuse = 1;
		do {
			if (bs.avail_in == 0 && size > 0) {
				if ((rd = read(fd, ibuf, sizeof ibuf)) <= 0) {
					emsg(3, "Cannot read \"%s\"", fn);
					close(fd);
					ftruncate(tf, 0);
					return -1;
				}
				bs.next_in = ibuf;
				bs.avail_in = rd;
				size -= rd;
				*crc = zipcrc(*crc, (unsigned char *)ibuf, rd);
			}
			if (bs.next_out == NULL || bs.next_out > obuf) {
				if (bs.next_out && tf >= 0) {
					on = bs.next_out - obuf;
					if (write(tf, obuf, on) != on) {
						close(tf);
						tf = -1;
					}
					*csize += on;
				}
				bs.next_out = obuf;
				bs.avail_out = sizeof obuf;
			}
			if (ok != BZ_STREAM_END) {
				switch (ok = BZ2_bzCompress(&bs,
					bs.avail_in?BZ_RUN:BZ_FINISH)) {
				case BZ_RUN_OK:
				case BZ_FINISH_OK:
				case BZ_STREAM_END:
					break;
				default:
					msg(3, 1, "Compression error %d "
							"on \"%s\"\n", ok, fn);
					close(fd);
					return -1;
				}
			}
		} while (size > 0 || bs.next_out > obuf || ok != BZ_STREAM_END);
		BZ2_bzCompressEnd(&bs);
		goto out;
	}
#endif	/* USE_BZLIB */
store:	cuse = -1;
	while (size > 0) {
		if ((rd = read(fd, ibuf, sizeof ibuf)) <= 0) {
			emsg(3, "Cannot read \"%s\"", fn);
			close(fd);
			return -1;
		}
		size -= rd;
		*crc = zipcrc(*crc, (unsigned char *)ibuf, rd);
	}
out:	if (tf >= 0 && cuse >= 0 && *csize < st->st_size) {
		if (zipclevel == 07) {
			bp->Zdr.z_cmethod[0] = C_BZIP2;
			bp->Zdr.z_version[0] = 0x2e;
		} else {
			bp->Zdr.z_cmethod[0] = C_DEFLATED;
			bp->Zdr.z_gflag[0] |= zipclevel << 1;
			bp->Zdr.z_version[0] = 20;
		}
		sf = tf;
		sname = tlate;
	} else {
		*csize = st->st_size;
		sf = fd;
		sname = fn;
	}
	if ((lseek(sf, 0, SEEK_SET)) != 0) {
		emsg(3, "Cannot rewind \"%s\"", sname);
		errcnt++;
		close(fd);
		ftruncate(tf, 0);
		return -1;
	}
	le32p(*crc, bp->Zdr.z_crc32);
	if (st->st_size >= 0xffffffff || *csize >= 0xffffffff) {
		int	n;
		zfp = &zf;
		memset(&zf, 0, SIZEOF_zextra_64);
		le16p(mag_zip64f, zf.ze_64_tag);
		le16p(SIZEOF_zextra_64 - 4, zf.ze_64_tsize);
		le64p(st->st_size, zf.ze_64_nsize);
		le64p(*csize, zf.ze_64_csize);
		le32p(0xffffffff, bp->Zdr.z_csize);
		le32p(0xffffffff, bp->Zdr.z_nsize);
		n = (ple16(bp->Zdr.z_extralen)&0177777) + SIZEOF_zextra_64;
		le16p(n, bp->Zdr.z_extralen);
	} else
		le32p(*csize, bp->Zdr.z_csize);
	bwrite((char *)bp, SIZEOF_zip_header);
	bwrite(fn, sz);
	if (zfp)
		bwrite((char *)zfp, SIZEOF_zextra_64);
	zipwxtra(fn, st, dev, ino);
	size = *csize;
	while (size) {
		if ((rd=read(sf, ibuf, size>sizeof ibuf?sizeof ibuf:size)) <= 0)
			msg(3, 1, "Cannot read \"%s\"\n", sname);
		bwrite(ibuf, rd);
		size -= rd;
	}
	ftruncate(tf, 0);
	close(fd);
	return 0;
}

#if USE_ZLIB
/*
 * Write a zip archive entry using the data descriptor structure.
 */
static int
zipwdesc(int fd, const char *fn, struct stat *st, union bincpio *bp, size_t sz,
		uint32_t dev, uint32_t ino, uint32_t *crc, long long *csize)
{
	struct zextra_64	zf;
	struct zextra_64	*zfp = 0;
	char	ibuf[32768], obuf[32768];
	long long	size = st->st_size;
	ssize_t	rd;
	struct z_stream_s	z;
	int	cuse;

	*csize = 0;
	*crc = 0;
	memset(&z, 0, sizeof z);
	if ((cuse = deflateInit2(&z, ziptrlevel(), Z_DEFLATED,
					-15, 8, Z_DEFAULT_STRATEGY)) < 0)
		return zipwtemp(fd, fn, st, bp, sz, dev, ino, crc, csize);
	bp->Zdr.z_cmethod[0] = C_DEFLATED;
	bp->Zdr.z_gflag[0] |= zipclevel << 1 | FG_DESC;
	bp->Zdr.z_version[0] = 20;
	/*
	 * RFC 1951 states that deflate compression needs 5 bytes additional
	 * space per 32k block in the worst case. Thus a compressed size
	 * greater than 4G-1 can be reached if at least 131052 blocks are
	 * used.
	 */
	if (st->st_size >= 131052LL*32768) {
		int	n;
		zfp = &zf;
		memset(&zf, 0, SIZEOF_zextra_64);
		le16p(mag_zip64f, zf.ze_64_tag);
		le16p(SIZEOF_zextra_64 - 4, zf.ze_64_tsize);
		le32p(0xffffffff, bp->Zdr.z_csize);
		le32p(0xffffffff, bp->Zdr.z_nsize);
		n = (ple16(bp->Zdr.z_extralen)&0177777) + SIZEOF_zextra_64;
		le16p(n, bp->Zdr.z_extralen);
	}
	bwrite((char *)bp, SIZEOF_zip_header);
	bwrite(fn, sz);
	if (zfp)
		bwrite((char *)zfp, SIZEOF_zextra_64);
	zipwxtra(fn, st, dev, ino);
	do {
		if (z.avail_in == 0 && size > 0) {
			if ((rd = read(fd, ibuf, sizeof ibuf)) <= 0) {
				emsg(3, "Cannot read \"%s\"", fn);
				st->st_size -= size;
				size = 0;	/* can't simply stop here */
				rd = 0;		/* no data */
			}
			z.next_in = (unsigned char *)ibuf;
			z.avail_in = z.total_in = rd;
			size -= rd;
			*crc = zipcrc(*crc, (unsigned char *)ibuf, rd);
		}
		if (z.next_out == NULL || (char *)z.next_out > obuf) {
			if (z.next_out) {
				bwrite(obuf, (char *)z.next_out - obuf);
				*csize += (char *)z.next_out - obuf;
			}
			z.next_out = (unsigned char *)obuf;
			z.avail_out = sizeof obuf;
		}
		if (cuse >= 0 && cuse != Z_STREAM_END)
			cuse = deflate(&z, z.avail_in?Z_NO_FLUSH:Z_FINISH);
		else
			z.avail_in = 0;
	} while (size > 0 || (char *)z.next_out > obuf ||
			cuse >= 0 && cuse != Z_STREAM_END);
	deflateEnd(&z);
	if (zfp) {
		struct zipddesc64	zd64;
		memcpy(zd64.zd_signature, mag_zipdds, sizeof mag_zipdds);
		le32p(*crc, zd64.zd_crc32);
		le64p(st->st_size, zd64.zd_nsize);
		le64p(*csize, zd64.zd_csize);
		bwrite((char *)&zd64, SIZEOF_zipddesc64);
	} else {
		struct zipddesc	zd;
		memcpy(zd.zd_signature, mag_zipdds, sizeof mag_zipdds);
		le32p(*crc, zd.zd_crc32);
		le32p(st->st_size, zd.zd_nsize);
		le32p(*csize, zd.zd_csize);
		bwrite((char *)&zd, SIZEOF_zipddesc);
	}
	close(fd);
	return 0;
}
#endif	/* USE_ZLIB */

/*
 * Write the extra fields for a file to a zip archive (currently
 * our own field type). Note that the z_extralen field in the file
 * header must correspond to the size of the data written here.
 */
static int
zipwxtra(const char *fn, struct stat *st, uint32_t dev, uint32_t ino)
{
	struct zextra_cp	z;

	memset(&z, 0, SIZEOF_zextra_cp);
	le16p(mag_zipcpio, z.ze_cp_tag);
	le16p(SIZEOF_zextra_cp - 4, z.ze_cp_tsize);
	le32p(dev, z.ze_cp_mode);
	le32p(ino, z.ze_cp_ino);
	le32p(st->st_mode&(S_IFMT|07777), z.ze_cp_mode);
	le32p(st->st_uid, z.ze_cp_uid);
	le32p(st->st_gid, z.ze_cp_gid);
	le32p(st->st_nlink, z.ze_cp_nlink);
	le32p(st->st_rdev, z.ze_cp_rdev);
	le32p(st->st_mtime, z.ze_cp_mtime);
	le32p(st->st_atime, z.ze_cp_atime);
	bwrite((char *)&z, SIZEOF_zextra_cp);
	return SIZEOF_zextra_cp;
}

static void
zipinfo(struct file *f)
{
	const char	*cp;
	char	b[5];
	int	i;

	printf(" %7llu", f->f_csize);
	if (f->f_dsize) {
		i = f->f_csize*100 / f->f_dsize;
		i += f->f_csize*200 / f->f_dsize & 1;
		i = 100 - i;
	} else
		i = 0;
	printf(" %3d%%", i);
	switch (f->f_cmethod) {
	case C_STORED:
		cp = "stor";
		break;
	case C_SHRUNK:
		cp = "shrk";
		break;
	case C_REDUCED1:
		cp = "re:1";
		break;
	case C_REDUCED2:
		cp = "re:2";
		break;
	case C_REDUCED3:
		cp = "re:3";
		break;
	case C_REDUCED4:
		cp = "re:4";
		break;
	case C_IMPLODED:
		b[0] = 'i';
		b[1] = f->f_gflag & FG_BIT1 ? '8' : '4';
		b[2] = ':';
		b[3] = f->f_gflag & FG_BIT2 ? '3' : '2';
		b[4] = '\0';
		cp = b;
		break;
	case C_TOKENIZED:
		cp = "tokn";
		break;
	case C_DEFLATED:
		b[0] = 'd', b[1] = 'e', b[2] = 'f', b[4] = '\0';
		if (f->f_gflag & FG_BIT2)
			b[3] = f->f_gflag & FG_BIT1 ? 'S' : 'F';
		else
			b[3] = f->f_gflag & FG_BIT1 ? 'X' : 'N';
		cp = b;
		break;
	case C_ENHDEFLD:
		cp = "edef";
		break;
	case C_DCLIMPLODED:
		cp = "dcli";
		break;
	case C_BZIP2:
		cp = "bz2 ";
		break;
	default:
		snprintf(b, sizeof b, "%4.4X", f->f_cmethod);
		cp = b;
	}
	printf(" %s", cp);
}

#if USE_BZLIB
int
zipunbz2(struct file *f, const char *tgt, int tfd, int doswap, uint32_t *crc)
{
	bz_stream	bs;
	long long	isize = f->f_csize;
	char	ibuf[4096], obuf[8192];
	int	in, on, val = 0, ok;

	memset(&bs, 0, sizeof bs);

	if ((ok = BZ2_bzDecompressInit(&bs, 0, 0)) != BZ_OK) {
		msg(3, 0, "bzip2 initialization error %d on \"%s\"\n",
				ok, f->f_name);
		errcnt++;
		return skipfile(f);
	}
	while (isize > 0 || ok == BZ_OK) {
		if (bs.avail_in == 0 && isize > 0) {
			in = sizeof ibuf < isize ? sizeof ibuf : isize;
			isize -= in;
			if (bread(ibuf, in) != in)
				unexeoa();
			if (doswap)
				swap(ibuf, in, bflag || sflag, bflag || Sflag);
			bs.next_in = ibuf;
			bs.avail_in = in;
		}
		if (ok == BZ_OK) {
			bs.next_out = obuf;
			bs.avail_out = sizeof obuf;
			switch (ok = BZ2_bzDecompress(&bs)) {
			case BZ_OK:
			case BZ_STREAM_END:
				on = sizeof obuf - bs.avail_out;
				if (tfd >= 0 && write(tfd, obuf, on) != on) {
					emsg(3, "Cannot write \"%s\"", tgt);
					tfd = -1;
					val = 1;
				}
				*crc = zipcrc(*crc, (unsigned char *)obuf, on);
				break;
			default:
				msg(3, 0, "compression error %d on \"%s\"\n",
					ok, f->f_name);
				errcnt++;
				val = 1;
			}
		}
	}
	BZ2_bzDecompressEnd(&bs);
	return val;
}
#endif	/* USE_BZLIB */

struct	blasthow {
	struct file	*bh_f;
	const char	*bh_tgt;
	long long	bh_isize;
	uint32_t	*bh_crc;
	int		bh_tfd;
	int		bh_doswap;
	int		bh_val;
};

static unsigned
blastin(void *how, unsigned char **buf)
{
	const int	chunk = 16384;
	static unsigned char	*hold;
	struct blasthow	*bp = how;
	unsigned	sz;

	if (bp->bh_isize <= 0)
		return 0;
	if (hold == NULL)
		hold = smalloc(chunk);
	sz = bp->bh_isize > chunk ? chunk : bp->bh_isize;
	bp->bh_isize -= sz;
	if (bread((char *)hold, sz) != sz)
		unexeoa();
	if (bp->bh_doswap)
		swap((char *)hold, sz, bflag || sflag, bflag || Sflag);
	*buf = hold;
	return sz;
}

static int
blastout(void *how, unsigned char *buf, unsigned len)
{
	struct blasthow	*bp = how;

	if (bp->bh_tfd >= 0 && write(bp->bh_tfd, buf, len) != len) {
		emsg(3, "Cannot write \"%s\"", bp->bh_tgt);
		bp->bh_tfd = -1;
		bp->bh_val = 1;
	}
	*bp->bh_crc = zipcrc(*bp->bh_crc, buf, len);
	return 0;
}

int
zipblast(struct file *f, const char *tgt, int tfd, int doswap, uint32_t *crc)
{
	struct blasthow	bh;
	int	n;

	bh.bh_f = f;
	bh.bh_tgt = tgt;
	bh.bh_isize = f->f_csize;
	bh.bh_crc = crc;
	bh.bh_tfd = tfd;
	bh.bh_doswap = doswap;
	bh.bh_val = 0;
	switch (n = blast(blastin, &bh, blastout, &bh)) {
	case 0:
		break;
	default:
		msg(3, 0, "compression error %d on \"%s\"\n", n, f->f_name);
		errcnt++;
		bh.bh_val = 1;
	}
	while (bh.bh_isize) {
		char	buf[4096];
		unsigned	n;
		n = bh.bh_isize > sizeof buf ? sizeof buf : bh.bh_isize;
		if (bread(buf, n) != n)
			unexeoa();
		bh.bh_isize -= n;
	}
	return bh.bh_val;
}

/*
 * The SGI -K format was introduced with SGI's IRIX 5.X. It is essentially
 * a slightly extended binary format. The known additions are:
 *
 * - If the major or minor st_rdev device number exceeds the limit of
 *   255 imposed by the 16-bit c_rdev field, this field is set to 0xFFFF
 *   and st_rdev is stored in the c_filesize 32-bit field. The first 14
 *   bits of this field compose the major device number, the minor is
 *   stored in the remaining 18 bits. This enables implementations to
 *   read the modified format without special support if they ignore
 *   the size of device files; otherwise they will try to read a lot
 *   of archive data and fail.
 *
 * - If the file is larger than 2 GB - 1 byte, two nearly identical
 *   archive headers are stored for it. The only difference is in
 *   the c_filesize field:
 *
 *   [first header: file size X times 2 GB]
 *    [first data part: X times 2 GB]
 *   [second header: file size modulo 2 GB]
 *    [second data part: rest of data]
 *
 *   The first header can be recognized by a negative c_filesize. A
 *   value of 0xFFFFFFFF means that 2 GB follow, 0xFFFFFFFE -> 4 GB
 *   0xFFFFFFFD -> 6 GB, 0xFFFFFFFC -> 8 GB, and so forth. The second
 *   is a standard binary cpio header, although the following data is
 *   meant to be appended to the preceding file.
 *
 *   It is important to note that padding follows the number in
 *   c_filesize, not the amount of data written; thus all data parts
 *   with odd c_filesize fields (0xFFFFFFFF = 2+ GB, 0xFFFFFFFD = 6+ GB
 *   etc.) cause the following archive entries to be aligned on an odd
 *   offset. This seems to be an implementation artifact but has to be
 *   followed for compatibility.
 *
 *   This extension seems a bit weird since no known cpio implementation
 *   is able to read these archive entries without special support. Thus
 *   a more straightforward extension (such as storing the size just past
 *   the file name) would well have had the same effect. Nevertheless,
 *   the cpio -K format is useful, so it is implemented here.
 *
 *   --Note that IRIX 6.X tar also has a -K option. This option extends
 *   the tar format in essentially the same way as the second extension
 *   to cpio described above. Unfortunately, the result is definitively
 *   broken. Contrasting to the binary cpio format, the standard POSIX
 *   tar format is well able to hold files of size 0xFFFFFFFF and below
 *   in a regular manner. Thus, a tar -K archive entry is _exactly_ the
 *   same as two regular POSIX tar entries for the same file. And this
 *   situation even occurs on a regular basis with tar -r! For this
 *   reason, we do not implement the IRIX tar -K format and will probably
 *   never do so unless it is changed (the tar format really has a lot
 *   of options to indicate extensions that might be used for this).
 *
 *   Many thanks to Sven Mascheck who made archiving tests on the IRIX
 *   machine.
 */
/*
 * This function reads the second header of a SGI -K format archive.
 */
static void
readK2hdr(struct file *f)
{
	struct file	n;
	union bincpio	bc;

	n.f_name = n.f_lnam = NULL;
	n.f_nsiz = n.f_lsiz = 0;
	readhdr(&n, &bc);
	f->f_Krest = n.f_st.st_size;
	f->f_dsize = f->f_st.st_size = n.f_st.st_size + f->f_Kbase;
	f->f_Kbase = 0;
	free(n.f_name);
	free(n.f_lnam);
}

/*
 * Read the data of a GNU filename extra header.
 */
static int
readgnuname(char **np, size_t *sp, long length)
{
	if (length > SANELIMIT)
		return -1;
	if (*sp == 0 || *sp <= length)
		*np = srealloc(*np, *sp = length+1);
	bread(*np, length);
	(*np)[length] = '\0';
	skippad(length, 512);
	return 0;
}

/*
 * Write a GNU filename extra header and its data.
 */
static void
writegnuname(const char *fn, long length, int flag)
{
	union bincpio	bc;

	memset(bc.data, 0, 512);
	strcpy(bc.Tdr.t_name, "././@LongLink");
	sprintf(bc.Tdr.t_mode, "%7.7o", 0);
	sprintf(bc.Tdr.t_uid, "%7.7o", 0);
	sprintf(bc.Tdr.t_gid, "%7.7o", 0);
	sprintf(bc.Tdr.t_size, "%11.11lo", length);
	sprintf(bc.Tdr.t_mtime, "%11.11lo", 0L);
	bc.Tdr.t_linkflag = flag;
	memcpy(bc.Tdr.t_magic, mag_gnutar, 8);
	strcpy(bc.Tdr.t_uname, "root");
	strcpy(bc.Tdr.t_gname, "root");
	tchksum(&bc);
	bwrite(bc.data, 512);
	bwrite(fn, length);
	length %= 512;
	memset(bc.data, 0, 512 - length);
	bwrite(bc.data, 512 - length);
}

/*
 * POSIX.1-2001 pax format support.
 */
static void
tgetpax(struct tar_header *tp, struct file *f)
{
	char	*keyword, *value;
	char	*block, *bp;
	long long	n;
	enum paxrec	pr;

	n = rdoct(tp->t_size, 12);
	bp = block = smalloc(n+1);
	bread(block, n);
	skippad(n, 512);
	block[n] = '\0';
	while (bp < &block[n]) {
		int	c;
		pr = tgetrec(&bp, &keyword, &value);
		switch (pr) {
		case PR_ATIME:
			f->f_st.st_atime = strtoll(value, NULL, 10);
			break;
		case PR_GID:
			f->f_st.st_gid = strtoll(value, NULL, 10);
			break;
		case PR_LINKPATH:
			c = strlen(value);
			if (f->f_lnam == NULL || f->f_lsiz < c+1) {
				f->f_lsiz = c+1;
				f->f_lnam = srealloc(f->f_lnam, c+1);
			}
			strcpy(f->f_lnam, value);
			break;
		case PR_MTIME:
			f->f_st.st_mtime = strtoll(value, NULL, 10);
			break;
		case PR_PATH:
			c = strlen(value);
			if (f->f_name == NULL || f->f_nsiz < c+1) {
				f->f_nsiz = c+1;
				f->f_name = srealloc(f->f_name, c+1);
			}
			strcpy(f->f_name, value);
			break;
		case PR_SIZE:
			f->f_st.st_size = strtoll(value, NULL, 10);
			break;
		case PR_UID:
			f->f_st.st_uid = strtoll(value, NULL, 10);
			break;
		case PR_SUN_DEVMAJOR:
			f->f_rmajor = strtoll(value, NULL, 10);
			break;
		case PR_SUN_DEVMINOR:
			f->f_rminor = strtoll(value, NULL, 10);
			break;
		}
		paxrec |= pr;
	}
	if (tp->t_linkflag == 'g') {
		globrec = paxrec & ~(PR_LINKPATH|PR_PATH|PR_SIZE);
		globst = f->f_st;
	}
	free(block);
}

static enum paxrec
tgetrec(char **bp, char **keyword, char **value)
{
	char	*x;
	long	n = 0;
	enum paxrec	pr;

	*keyword = "";
	*value = "";
	while (**bp && (n = strtol(*bp, &x, 10)) <= 0 && (*x!=' ' || *x!='\t'))
		do
			(*bp)++;
		while (**bp && **bp != '\n');
	if (*x == '\0' || **bp == '\0') {
		(*bp)++;
		return PR_NONE;
	}
	while (x < &(*bp)[n] && (*x == ' ' || *x == '\t'))
		x++;
	if (x == &(*bp)[n] || *x == '=')
		goto out;
	*keyword = x;
	while (x < &(*bp)[n] && *x != '=')
		x++;
	if (x == &(*bp)[n])
		goto out;
	*x = '\0';
	if (&x[1] < &(*bp)[n])
		*value = &x[1];
	(*bp)[n-1] = '\0';
out:	*bp = &(*bp)[n];
	if (strcmp(*keyword, "atime") == 0)
		pr = PR_ATIME;
	else if (strcmp(*keyword, "gid") == 0)
		pr = PR_GID;
	else if (strcmp(*keyword, "linkpath") == 0)
		pr = PR_LINKPATH;
	else if (strcmp(*keyword, "mtime") == 0)
		pr = PR_MTIME;
	else if (strcmp(*keyword, "path") == 0)
		pr = PR_PATH;
	else if (strcmp(*keyword, "size") == 0)
		pr = PR_SIZE;
	else if (strcmp(*keyword, "uid") == 0)
		pr = PR_UID;
	else if (strcmp(*keyword, "SUN.devmajor") == 0)
		pr = PR_SUN_DEVMAJOR;
	else if (strcmp(*keyword, "SUN.devminor") == 0)
		pr = PR_SUN_DEVMINOR;
	else
		pr = PR_NONE;
	return pr;
}

static void
wrpax(const char *longname, const char *linkname, struct stat *sp)
{
	union bincpio	bc;
	char	*pdata = NULL;
	long	psize = 0, pcur = 0;
	long long	blocks;

	memset(bc.data, 0, 512);
	if (paxrec & PR_ATIME)
		addrec(&pdata, &psize, &pcur, "atime", NULL, sp->st_atime);
	if (paxrec & PR_GID)
		addrec(&pdata, &psize, &pcur, "gid", NULL, sp->st_gid);
	if (paxrec & PR_LINKPATH)
		addrec(&pdata, &psize, &pcur, "linkpath", linkname, 0);
	if (paxrec & PR_MTIME)
		addrec(&pdata, &psize, &pcur, "mtime", NULL, sp->st_mtime);
	if (paxrec & PR_PATH)
		addrec(&pdata, &psize, &pcur, "path", longname, 0);
	if (paxrec & PR_SIZE)
		addrec(&pdata, &psize, &pcur, "size", NULL, sp->st_size);
	if (paxrec & PR_UID)
		addrec(&pdata, &psize, &pcur, "uid", NULL, sp->st_uid);
	if (paxrec & PR_SUN_DEVMAJOR)
		addrec(&pdata, &psize, &pcur, "SUN.devmajor", NULL,
				major(sp->st_rdev));
	if (paxrec & PR_SUN_DEVMINOR)
		addrec(&pdata, &psize, &pcur, "SUN.devminor", NULL,
				minor(sp->st_rdev));
	paxnam(&bc.Tdr, longname);
	sprintf(bc.Tdr.t_mode, "%7.7o", fmttype==FMT_SUN ? 0444|S_IFREG : 0444);
	sprintf(bc.Tdr.t_uid, "%7.7o", 0);
	sprintf(bc.Tdr.t_gid, "%7.7o", 0);
	sprintf(bc.Tdr.t_size, "%11.11lo", pcur);
	sprintf(bc.Tdr.t_mtime, "%11.11o", 0);
	strcpy(bc.Tdr.t_magic, "ustar");
	bc.Tdr.t_version[0] = bc.Tdr.t_version[1] = '0';
	strcpy(bc.Tdr.t_uname, "root");
	strcpy(bc.Tdr.t_gname, "root");
	bc.Tdr.t_linkflag = fmttype==FMT_SUN ? 'X' : 'x';
	tchksum(&bc);
	bwrite(bc.data, 512);
	memset(&pdata[pcur], 0, psize - pcur);
	blocks = (pcur + (512-1)) / 512;
	bwrite(pdata, blocks * 512);
	free(pdata);
}

static void
addrec(char **pdata, long *psize, long *pcur,
		const char *keyword, const char *sval, long long lval)
{
	char	dval[25], xval[25];
	long	od, d, r;

	if (sval == 0) {
		sprintf(xval, "%lld", lval);
		sval = xval;
	}
	r = strlen(keyword) + strlen(sval) + 3;
	d = 0;
	do {
		od = d;
		d = sprintf(dval, "%ld", od + r);
	} while (d != od);
	*psize += d + r + 1 + 512;
	*pdata = srealloc(*pdata, *psize);
	sprintf(&(*pdata)[*pcur], "%s %s=%s\n", dval, keyword, sval);
	*pcur += d + r;
}

static void
paxnam(struct tar_header *hp, const char *name)
{
	char	buf[257], *bp;
	const char	*cp, *np;
	int	bl = 0;
	static int	pid;

	if (pid == 0)
		pid = getpid();
	for (np = name; *np; np++);
	while (np > name && *np != '/') {
		np--;
		bl++;
	}
	if ((np > name || *name == '/') && np-name <= 120)
		for (bp = buf, cp = name; cp < np; bp++, cp++)
			*bp = *cp;
	else {
		*buf = '.';
		bp = &buf[1];
	}
	snprintf(bp, sizeof buf - (bp - buf), "/PaxHeaders.%d/%s",
			pid, bl < 100 ? np>name?&np[1]:name : sequence());
	tmkname(hp, buf);
}

static char *
sequence(void)
{
	static char	buf[25];
	static long long	d;

	sprintf(buf, "%10.10lld", ++d);
	return buf;
}

static int
pax_oneopt(const char *s, int warn)
{
	if (strcmp(s, "linkdata") == 0)
		pax_oflag |= PO_LINKDATA;
	else if (strcmp(s, "times") == 0)
		pax_oflag |= PO_TIMES;
	else {
		if (warn)
			msg(2, 0, "Unknown flag \"-o %s\"\n", s);
		return -1;
	}
	return 0;
}

int
pax_options(char *s, int warn)
{
	char	*o = s, c;
	int	val = 0, word = 0;

	do {
		if (word == 0) {
			if (isspace(*s&0377))
				o = &s[1];
			else
				word = 1;
		}
		if (*s == ',' || *s == '\0') {
			c = *s;
			*s = '\0';
			val |= pax_oneopt(o, warn);
			*s = c;
			o = &s[1];
			word = 0;
		}
	} while (*s++);
	return val;
}

/*
 * Given a symbolic link "base" and the result of readlink "name", form
 * a valid path name for the link target.
 */
static char *
joinpath(const char *base, char *name)
{
	const char	*bp = NULL, *cp;
	char	*new, *np;

	if (*name == '/')
		return name;
	for (cp = base; *cp; cp++)
		if (*cp == '/')
			bp = cp;
	if (bp == NULL)
		return name;
	np = new = smalloc(bp - base + strlen(name) + 2);
	for (cp = base; cp < bp; cp++)
		*np++ = *cp;
	*np++ = '/';
	for (cp = name; *cp; cp++)
		*np++ = *cp;
	*np = '\0';
	free(name);
	return new;
}

static int
utf8(const char *cp)
{
	int	c, n;

	while (*cp) if ((c = *cp++ & 0377) & 0200) {
		if (c == (c & 037 | 0300))
			n = 1;
		else if (c == (c & 017 | 0340))
			n = 2;
		else if (c == (c & 07 | 0360))
			n = 3;
		else if (c == (c & 03 | 0370))
			n = 4;
		else if (c == (c & 01 | 0374))
			n = 5;
		else
			return 0;
		while (n--) {
			c = *cp++ & 0377;
			if (c != (c & 077 | 0200))
				return 0;
		}
	}
	return 1;
}

static time_t
fetchtime(const char *cp)
{
	struct tm	tm;
	time_t	t;
	char	*xp;
	int	n;

	t = strtoll(cp, &xp, 10);
	if (*xp == '\0')
		return t;
	memset(&tm, 0, sizeof tm);
	n = sscanf(cp, "%4d%2d%2dT%2d%2d%2d",
			&tm.tm_year, &tm.tm_mon, &tm.tm_mday,
			&tm.tm_hour, &tm.tm_min, &tm.tm_sec);
	tm.tm_year -= 1900;
	tm.tm_mon--;
	tm.tm_isdst = -1;
	t = mktime(&tm);
	if (n < 3 || t == (time_t)-1)
		msg(4, 1, "line %lld: illegal time \"%s\"\n",
				lineno, cp);
	return t;
}

static char *
nextfield(char *cp, const char *fieldname)
{
	while (*cp && *cp != ':')
		cp++;
	if (*cp == 0)
		msg(4, 1, "line %lld: unterminated \"%s\" field\n",
				lineno, fieldname);
	*cp++ = 0;
	return cp;
}

static char *
getproto(char *np, struct prototype *pp)
{
	char	*tp, *xp;
	long long	t, u;

	memset(pp, 0, sizeof *pp);
	if (*np == ':')
		np++;
	else {
		tp = nextfield(np, "type");
		if (np[1]) 
			goto notype;
		switch (np[0]) {
		case 'b':
			pp->pt_mode |= S_IFBLK;
			break;
		case 'c':
			pp->pt_mode |= S_IFCHR;
			break;
		case 'd':
			pp->pt_mode |= S_IFDIR;
			break;
		case 'f':
			pp->pt_mode |= S_IFREG;
			break;
		case 'p':
			pp->pt_mode |= S_IFIFO;
			break;
		case 's':
			pp->pt_mode |= S_IFLNK;
			break;
		default:
		notype:
			msg(4, 1, "line %lld: unknown type \"%s\"\n",
					lineno, np);
		}
		pp->pt_spec |= PT_TYPE;
		np = tp;
	}
	if (*np == ':')
		np++;
	else {
		struct passwd	*pwd;
		tp = nextfield(np, "owner");
		t = strtoll(np, &xp, 10);
		if (*xp == '\0')
			pp->pt_uid = t;
		else {
			if ((pwd = getpwnam(np)) == NULL)
				msg(4, 1, "line %lld: unknown user \"%s\"\n",
						lineno, np);
			pp->pt_uid = pwd->pw_uid;
		}
		pp->pt_spec |= PT_OWNER;
		np = tp;
	}
	if (*np == ':')
		np++;
	else {
		struct group	*grp;
		tp = nextfield(np, "group");
		t = strtoll(np, &xp, 10);
		if (*xp == '\0')
			pp->pt_gid = t;
		else {
			if ((grp = getgrnam(np)) == NULL)
				msg(4, 1, "line %lld: unknown group \"%s\"\n",
						lineno, np);
			pp->pt_gid = grp->gr_gid;
		}
		pp->pt_spec |= PT_GROUP;
		np = tp;
	}
	if (*np == ':')
		np++;
	else {
		tp = nextfield(np, "mode");
		t = strtol(np, &xp, 8);
		if (t & ~07777 || *xp)
			msg(4, 1, "line %lld: illegal mode \"%s\"\n",
					lineno, np);
		pp->pt_mode |= t;
		pp->pt_spec |= PT_MODE;
		np = tp;
	}
	if (*np == ':')
		np++;
	else {
		tp = nextfield(np, "access time");
		pp->pt_atime = fetchtime(np);
		pp->pt_spec |= PT_ATIME;
		np = tp;
	}
	if (*np == ':')
		np++;
	else {
		tp = nextfield(np, "modification time");
		pp->pt_mtime = fetchtime(np);
		pp->pt_spec |= PT_MTIME;
		np = tp;
	}
	if (*np == ':') {
		np++;
		if (*np++ != ':')
		majmin:	msg(4, 1, "line %lld: need either both major and "
				"minor or none\n",
				lineno);
	} else {
		tp = nextfield(np, "major");
		t = strtoll(np, &xp, 10);
		if (*xp)
			msg(4, 1, "line %lld: illegal major \"%s\"\n",
					lineno, np);
		np = tp;
		if (*np == ':')
			goto majmin;
		tp = nextfield(np, "minor");
		u = strtoll(np, &xp, 10);
		if (*xp)
			msg(4, 1, "line %lld: illegal minor \"%s\"\n",
					lineno, np);
		np = tp;
		pp->pt_rdev = makedev(t, u);
		pp->pt_spec |= PT_RDEV;
	}
	return np;
}