|
root / base / usr / src / cmd / backup
backup Plain Text 18794 lines 452.7 KB
    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
 7173
 7174
 7175
 7176
 7177
 7178
 7179
 7180
 7181
 7182
 7183
 7184
 7185
 7186
 7187
 7188
 7189
 7190
 7191
 7192
 7193
 7194
 7195
 7196
 7197
 7198
 7199
 7200
 7201
 7202
 7203
 7204
 7205
 7206
 7207
 7208
 7209
 7210
 7211
 7212
 7213
 7214
 7215
 7216
 7217
 7218
 7219
 7220
 7221
 7222
 7223
 7224
 7225
 7226
 7227
 7228
 7229
 7230
 7231
 7232
 7233
 7234
 7235
 7236
 7237
 7238
 7239
 7240
 7241
 7242
 7243
 7244
 7245
 7246
 7247
 7248
 7249
 7250
 7251
 7252
 7253
 7254
 7255
 7256
 7257
 7258
 7259
 7260
 7261
 7262
 7263
 7264
 7265
 7266
 7267
 7268
 7269
 7270
 7271
 7272
 7273
 7274
 7275
 7276
 7277
 7278
 7279
 7280
 7281
 7282
 7283
 7284
 7285
 7286
 7287
 7288
 7289
 7290
 7291
 7292
 7293
 7294
 7295
 7296
 7297
 7298
 7299
 7300
 7301
 7302
 7303
 7304
 7305
 7306
 7307
 7308
 7309
 7310
 7311
 7312
 7313
 7314
 7315
 7316
 7317
 7318
 7319
 7320
 7321
 7322
 7323
 7324
 7325
 7326
 7327
 7328
 7329
 7330
 7331
 7332
 7333
 7334
 7335
 7336
 7337
 7338
 7339
 7340
 7341
 7342
 7343
 7344
 7345
 7346
 7347
 7348
 7349
 7350
 7351
 7352
 7353
 7354
 7355
 7356
 7357
 7358
 7359
 7360
 7361
 7362
 7363
 7364
 7365
 7366
 7367
 7368
 7369
 7370
 7371
 7372
 7373
 7374
 7375
 7376
 7377
 7378
 7379
 7380
 7381
 7382
 7383
 7384
 7385
 7386
 7387
 7388
 7389
 7390
 7391
 7392
 7393
 7394
 7395
 7396
 7397
 7398
 7399
 7400
 7401
 7402
 7403
 7404
 7405
 7406
 7407
 7408
 7409
 7410
 7411
 7412
 7413
 7414
 7415
 7416
 7417
 7418
 7419
 7420
 7421
 7422
 7423
 7424
 7425
 7426
 7427
 7428
 7429
 7430
 7431
 7432
 7433
 7434
 7435
 7436
 7437
 7438
 7439
 7440
 7441
 7442
 7443
 7444
 7445
 7446
 7447
 7448
 7449
 7450
 7451
 7452
 7453
 7454
 7455
 7456
 7457
 7458
 7459
 7460
 7461
 7462
 7463
 7464
 7465
 7466
 7467
 7468
 7469
 7470
 7471
 7472
 7473
 7474
 7475
 7476
 7477
 7478
 7479
 7480
 7481
 7482
 7483
 7484
 7485
 7486
 7487
 7488
 7489
 7490
 7491
 7492
 7493
 7494
 7495
 7496
 7497
 7498
 7499
 7500
 7501
 7502
 7503
 7504
 7505
 7506
 7507
 7508
 7509
 7510
 7511
 7512
 7513
 7514
 7515
 7516
 7517
 7518
 7519
 7520
 7521
 7522
 7523
 7524
 7525
 7526
 7527
 7528
 7529
 7530
 7531
 7532
 7533
 7534
 7535
 7536
 7537
 7538
 7539
 7540
 7541
 7542
 7543
 7544
 7545
 7546
 7547
 7548
 7549
 7550
 7551
 7552
 7553
 7554
 7555
 7556
 7557
 7558
 7559
 7560
 7561
 7562
 7563
 7564
 7565
 7566
 7567
 7568
 7569
 7570
 7571
 7572
 7573
 7574
 7575
 7576
 7577
 7578
 7579
 7580
 7581
 7582
 7583
 7584
 7585
 7586
 7587
 7588
 7589
 7590
 7591
 7592
 7593
 7594
 7595
 7596
 7597
 7598
 7599
 7600
 7601
 7602
 7603
 7604
 7605
 7606
 7607
 7608
 7609
 7610
 7611
 7612
 7613
 7614
 7615
 7616
 7617
 7618
 7619
 7620
 7621
 7622
 7623
 7624
 7625
 7626
 7627
 7628
 7629
 7630
 7631
 7632
 7633
 7634
 7635
 7636
 7637
 7638
 7639
 7640
 7641
 7642
 7643
 7644
 7645
 7646
 7647
 7648
 7649
 7650
 7651
 7652
 7653
 7654
 7655
 7656
 7657
 7658
 7659
 7660
 7661
 7662
 7663
 7664
 7665
 7666
 7667
 7668
 7669
 7670
 7671
 7672
 7673
 7674
 7675
 7676
 7677
 7678
 7679
 7680
 7681
 7682
 7683
 7684
 7685
 7686
 7687
 7688
 7689
 7690
 7691
 7692
 7693
 7694
 7695
 7696
 7697
 7698
 7699
 7700
 7701
 7702
 7703
 7704
 7705
 7706
 7707
 7708
 7709
 7710
 7711
 7712
 7713
 7714
 7715
 7716
 7717
 7718
 7719
 7720
 7721
 7722
 7723
 7724
 7725
 7726
 7727
 7728
 7729
 7730
 7731
 7732
 7733
 7734
 7735
 7736
 7737
 7738
 7739
 7740
 7741
 7742
 7743
 7744
 7745
 7746
 7747
 7748
 7749
 7750
 7751
 7752
 7753
 7754
 7755
 7756
 7757
 7758
 7759
 7760
 7761
 7762
 7763
 7764
 7765
 7766
 7767
 7768
 7769
 7770
 7771
 7772
 7773
 7774
 7775
 7776
 7777
 7778
 7779
 7780
 7781
 7782
 7783
 7784
 7785
 7786
 7787
 7788
 7789
 7790
 7791
 7792
 7793
 7794
 7795
 7796
 7797
 7798
 7799
 7800
 7801
 7802
 7803
 7804
 7805
 7806
 7807
 7808
 7809
 7810
 7811
 7812
 7813
 7814
 7815
 7816
 7817
 7818
 7819
 7820
 7821
 7822
 7823
 7824
 7825
 7826
 7827
 7828
 7829
 7830
 7831
 7832
 7833
 7834
 7835
 7836
 7837
 7838
 7839
 7840
 7841
 7842
 7843
 7844
 7845
 7846
 7847
 7848
 7849
 7850
 7851
 7852
 7853
 7854
 7855
 7856
 7857
 7858
 7859
 7860
 7861
 7862
 7863
 7864
 7865
 7866
 7867
 7868
 7869
 7870
 7871
 7872
 7873
 7874
 7875
 7876
 7877
 7878
 7879
 7880
 7881
 7882
 7883
 7884
 7885
 7886
 7887
 7888
 7889
 7890
 7891
 7892
 7893
 7894
 7895
 7896
 7897
 7898
 7899
 7900
 7901
 7902
 7903
 7904
 7905
 7906
 7907
 7908
 7909
 7910
 7911
 7912
 7913
 7914
 7915
 7916
 7917
 7918
 7919
 7920
 7921
 7922
 7923
 7924
 7925
 7926
 7927
 7928
 7929
 7930
 7931
 7932
 7933
 7934
 7935
 7936
 7937
 7938
 7939
 7940
 7941
 7942
 7943
 7944
 7945
 7946
 7947
 7948
 7949
 7950
 7951
 7952
 7953
 7954
 7955
 7956
 7957
 7958
 7959
 7960
 7961
 7962
 7963
 7964
 7965
 7966
 7967
 7968
 7969
 7970
 7971
 7972
 7973
 7974
 7975
 7976
 7977
 7978
 7979
 7980
 7981
 7982
 7983
 7984
 7985
 7986
 7987
 7988
 7989
 7990
 7991
 7992
 7993
 7994
 7995
 7996
 7997
 7998
 7999
 8000
 8001
 8002
 8003
 8004
 8005
 8006
 8007
 8008
 8009
 8010
 8011
 8012
 8013
 8014
 8015
 8016
 8017
 8018
 8019
 8020
 8021
 8022
 8023
 8024
 8025
 8026
 8027
 8028
 8029
 8030
 8031
 8032
 8033
 8034
 8035
 8036
 8037
 8038
 8039
 8040
 8041
 8042
 8043
 8044
 8045
 8046
 8047
 8048
 8049
 8050
 8051
 8052
 8053
 8054
 8055
 8056
 8057
 8058
 8059
 8060
 8061
 8062
 8063
 8064
 8065
 8066
 8067
 8068
 8069
 8070
 8071
 8072
 8073
 8074
 8075
 8076
 8077
 8078
 8079
 8080
 8081
 8082
 8083
 8084
 8085
 8086
 8087
 8088
 8089
 8090
 8091
 8092
 8093
 8094
 8095
 8096
 8097
 8098
 8099
 8100
 8101
 8102
 8103
 8104
 8105
 8106
 8107
 8108
 8109
 8110
 8111
 8112
 8113
 8114
 8115
 8116
 8117
 8118
 8119
 8120
 8121
 8122
 8123
 8124
 8125
 8126
 8127
 8128
 8129
 8130
 8131
 8132
 8133
 8134
 8135
 8136
 8137
 8138
 8139
 8140
 8141
 8142
 8143
 8144
 8145
 8146
 8147
 8148
 8149
 8150
 8151
 8152
 8153
 8154
 8155
 8156
 8157
 8158
 8159
 8160
 8161
 8162
 8163
 8164
 8165
 8166
 8167
 8168
 8169
 8170
 8171
 8172
 8173
 8174
 8175
 8176
 8177
 8178
 8179
 8180
 8181
 8182
 8183
 8184
 8185
 8186
 8187
 8188
 8189
 8190
 8191
 8192
 8193
 8194
 8195
 8196
 8197
 8198
 8199
 8200
 8201
 8202
 8203
 8204
 8205
 8206
 8207
 8208
 8209
 8210
 8211
 8212
 8213
 8214
 8215
 8216
 8217
 8218
 8219
 8220
 8221
 8222
 8223
 8224
 8225
 8226
 8227
 8228
 8229
 8230
 8231
 8232
 8233
 8234
 8235
 8236
 8237
 8238
 8239
 8240
 8241
 8242
 8243
 8244
 8245
 8246
 8247
 8248
 8249
 8250
 8251
 8252
 8253
 8254
 8255
 8256
 8257
 8258
 8259
 8260
 8261
 8262
 8263
 8264
 8265
 8266
 8267
 8268
 8269
 8270
 8271
 8272
 8273
 8274
 8275
 8276
 8277
 8278
 8279
 8280
 8281
 8282
 8283
 8284
 8285
 8286
 8287
 8288
 8289
 8290
 8291
 8292
 8293
 8294
 8295
 8296
 8297
 8298
 8299
 8300
 8301
 8302
 8303
 8304
 8305
 8306
 8307
 8308
 8309
 8310
 8311
 8312
 8313
 8314
 8315
 8316
 8317
 8318
 8319
 8320
 8321
 8322
 8323
 8324
 8325
 8326
 8327
 8328
 8329
 8330
 8331
 8332
 8333
 8334
 8335
 8336
 8337
 8338
 8339
 8340
 8341
 8342
 8343
 8344
 8345
 8346
 8347
 8348
 8349
 8350
 8351
 8352
 8353
 8354
 8355
 8356
 8357
 8358
 8359
 8360
 8361
 8362
 8363
 8364
 8365
 8366
 8367
 8368
 8369
 8370
 8371
 8372
 8373
 8374
 8375
 8376
 8377
 8378
 8379
 8380
 8381
 8382
 8383
 8384
 8385
 8386
 8387
 8388
 8389
 8390
 8391
 8392
 8393
 8394
 8395
 8396
 8397
 8398
 8399
 8400
 8401
 8402
 8403
 8404
 8405
 8406
 8407
 8408
 8409
 8410
 8411
 8412
 8413
 8414
 8415
 8416
 8417
 8418
 8419
 8420
 8421
 8422
 8423
 8424
 8425
 8426
 8427
 8428
 8429
 8430
 8431
 8432
 8433
 8434
 8435
 8436
 8437
 8438
 8439
 8440
 8441
 8442
 8443
 8444
 8445
 8446
 8447
 8448
 8449
 8450
 8451
 8452
 8453
 8454
 8455
 8456
 8457
 8458
 8459
 8460
 8461
 8462
 8463
 8464
 8465
 8466
 8467
 8468
 8469
 8470
 8471
 8472
 8473
 8474
 8475
 8476
 8477
 8478
 8479
 8480
 8481
 8482
 8483
 8484
 8485
 8486
 8487
 8488
 8489
 8490
 8491
 8492
 8493
 8494
 8495
 8496
 8497
 8498
 8499
 8500
 8501
 8502
 8503
 8504
 8505
 8506
 8507
 8508
 8509
 8510
 8511
 8512
 8513
 8514
 8515
 8516
 8517
 8518
 8519
 8520
 8521
 8522
 8523
 8524
 8525
 8526
 8527
 8528
 8529
 8530
 8531
 8532
 8533
 8534
 8535
 8536
 8537
 8538
 8539
 8540
 8541
 8542
 8543
 8544
 8545
 8546
 8547
 8548
 8549
 8550
 8551
 8552
 8553
 8554
 8555
 8556
 8557
 8558
 8559
 8560
 8561
 8562
 8563
 8564
 8565
 8566
 8567
 8568
 8569
 8570
 8571
 8572
 8573
 8574
 8575
 8576
 8577
 8578
 8579
 8580
 8581
 8582
 8583
 8584
 8585
 8586
 8587
 8588
 8589
 8590
 8591
 8592
 8593
 8594
 8595
 8596
 8597
 8598
 8599
 8600
 8601
 8602
 8603
 8604
 8605
 8606
 8607
 8608
 8609
 8610
 8611
 8612
 8613
 8614
 8615
 8616
 8617
 8618
 8619
 8620
 8621
 8622
 8623
 8624
 8625
 8626
 8627
 8628
 8629
 8630
 8631
 8632
 8633
 8634
 8635
 8636
 8637
 8638
 8639
 8640
 8641
 8642
 8643
 8644
 8645
 8646
 8647
 8648
 8649
 8650
 8651
 8652
 8653
 8654
 8655
 8656
 8657
 8658
 8659
 8660
 8661
 8662
 8663
 8664
 8665
 8666
 8667
 8668
 8669
 8670
 8671
 8672
 8673
 8674
 8675
 8676
 8677
 8678
 8679
 8680
 8681
 8682
 8683
 8684
 8685
 8686
 8687
 8688
 8689
 8690
 8691
 8692
 8693
 8694
 8695
 8696
 8697
 8698
 8699
 8700
 8701
 8702
 8703
 8704
 8705
 8706
 8707
 8708
 8709
 8710
 8711
 8712
 8713
 8714
 8715
 8716
 8717
 8718
 8719
 8720
 8721
 8722
 8723
 8724
 8725
 8726
 8727
 8728
 8729
 8730
 8731
 8732
 8733
 8734
 8735
 8736
 8737
 8738
 8739
 8740
 8741
 8742
 8743
 8744
 8745
 8746
 8747
 8748
 8749
 8750
 8751
 8752
 8753
 8754
 8755
 8756
 8757
 8758
 8759
 8760
 8761
 8762
 8763
 8764
 8765
 8766
 8767
 8768
 8769
 8770
 8771
 8772
 8773
 8774
 8775
 8776
 8777
 8778
 8779
 8780
 8781
 8782
 8783
 8784
 8785
 8786
 8787
 8788
 8789
 8790
 8791
 8792
 8793
 8794
 8795
 8796
 8797
 8798
 8799
 8800
 8801
 8802
 8803
 8804
 8805
 8806
 8807
 8808
 8809
 8810
 8811
 8812
 8813
 8814
 8815
 8816
 8817
 8818
 8819
 8820
 8821
 8822
 8823
 8824
 8825
 8826
 8827
 8828
 8829
 8830
 8831
 8832
 8833
 8834
 8835
 8836
 8837
 8838
 8839
 8840
 8841
 8842
 8843
 8844
 8845
 8846
 8847
 8848
 8849
 8850
 8851
 8852
 8853
 8854
 8855
 8856
 8857
 8858
 8859
 8860
 8861
 8862
 8863
 8864
 8865
 8866
 8867
 8868
 8869
 8870
 8871
 8872
 8873
 8874
 8875
 8876
 8877
 8878
 8879
 8880
 8881
 8882
 8883
 8884
 8885
 8886
 8887
 8888
 8889
 8890
 8891
 8892
 8893
 8894
 8895
 8896
 8897
 8898
 8899
 8900
 8901
 8902
 8903
 8904
 8905
 8906
 8907
 8908
 8909
 8910
 8911
 8912
 8913
 8914
 8915
 8916
 8917
 8918
 8919
 8920
 8921
 8922
 8923
 8924
 8925
 8926
 8927
 8928
 8929
 8930
 8931
 8932
 8933
 8934
 8935
 8936
 8937
 8938
 8939
 8940
 8941
 8942
 8943
 8944
 8945
 8946
 8947
 8948
 8949
 8950
 8951
 8952
 8953
 8954
 8955
 8956
 8957
 8958
 8959
 8960
 8961
 8962
 8963
 8964
 8965
 8966
 8967
 8968
 8969
 8970
 8971
 8972
 8973
 8974
 8975
 8976
 8977
 8978
 8979
 8980
 8981
 8982
 8983
 8984
 8985
 8986
 8987
 8988
 8989
 8990
 8991
 8992
 8993
 8994
 8995
 8996
 8997
 8998
 8999
 9000
 9001
 9002
 9003
 9004
 9005
 9006
 9007
 9008
 9009
 9010
 9011
 9012
 9013
 9014
 9015
 9016
 9017
 9018
 9019
 9020
 9021
 9022
 9023
 9024
 9025
 9026
 9027
 9028
 9029
 9030
 9031
 9032
 9033
 9034
 9035
 9036
 9037
 9038
 9039
 9040
 9041
 9042
 9043
 9044
 9045
 9046
 9047
 9048
 9049
 9050
 9051
 9052
 9053
 9054
 9055
 9056
 9057
 9058
 9059
 9060
 9061
 9062
 9063
 9064
 9065
 9066
 9067
 9068
 9069
 9070
 9071
 9072
 9073
 9074
 9075
 9076
 9077
 9078
 9079
 9080
 9081
 9082
 9083
 9084
 9085
 9086
 9087
 9088
 9089
 9090
 9091
 9092
 9093
 9094
 9095
 9096
 9097
 9098
 9099
 9100
 9101
 9102
 9103
 9104
 9105
 9106
 9107
 9108
 9109
 9110
 9111
 9112
 9113
 9114
 9115
 9116
 9117
 9118
 9119
 9120
 9121
 9122
 9123
 9124
 9125
 9126
 9127
 9128
 9129
 9130
 9131
 9132
 9133
 9134
 9135
 9136
 9137
 9138
 9139
 9140
 9141
 9142
 9143
 9144
 9145
 9146
 9147
 9148
 9149
 9150
 9151
 9152
 9153
 9154
 9155
 9156
 9157
 9158
 9159
 9160
 9161
 9162
 9163
 9164
 9165
 9166
 9167
 9168
 9169
 9170
 9171
 9172
 9173
 9174
 9175
 9176
 9177
 9178
 9179
 9180
 9181
 9182
 9183
 9184
 9185
 9186
 9187
 9188
 9189
 9190
 9191
 9192
 9193
 9194
 9195
 9196
 9197
 9198
 9199
 9200
 9201
 9202
 9203
 9204
 9205
 9206
 9207
 9208
 9209
 9210
 9211
 9212
 9213
 9214
 9215
 9216
 9217
 9218
 9219
 9220
 9221
 9222
 9223
 9224
 9225
 9226
 9227
 9228
 9229
 9230
 9231
 9232
 9233
 9234
 9235
 9236
 9237
 9238
 9239
 9240
 9241
 9242
 9243
 9244
 9245
 9246
 9247
 9248
 9249
 9250
 9251
 9252
 9253
 9254
 9255
 9256
 9257
 9258
 9259
 9260
 9261
 9262
 9263
 9264
 9265
 9266
 9267
 9268
 9269
 9270
 9271
 9272
 9273
 9274
 9275
 9276
 9277
 9278
 9279
 9280
 9281
 9282
 9283
 9284
 9285
 9286
 9287
 9288
 9289
 9290
 9291
 9292
 9293
 9294
 9295
 9296
 9297
 9298
 9299
 9300
 9301
 9302
 9303
 9304
 9305
 9306
 9307
 9308
 9309
 9310
 9311
 9312
 9313
 9314
 9315
 9316
 9317
 9318
 9319
 9320
 9321
 9322
 9323
 9324
 9325
 9326
 9327
 9328
 9329
 9330
 9331
 9332
 9333
 9334
 9335
 9336
 9337
 9338
 9339
 9340
 9341
 9342
 9343
 9344
 9345
 9346
 9347
 9348
 9349
 9350
 9351
 9352
 9353
 9354
 9355
 9356
 9357
 9358
 9359
 9360
 9361
 9362
 9363
 9364
 9365
 9366
 9367
 9368
 9369
 9370
 9371
 9372
 9373
 9374
 9375
 9376
 9377
 9378
 9379
 9380
 9381
 9382
 9383
 9384
 9385
 9386
 9387
 9388
 9389
 9390
 9391
 9392
 9393
 9394
 9395
 9396
 9397
 9398
 9399
 9400
 9401
 9402
 9403
 9404
 9405
 9406
 9407
 9408
 9409
 9410
 9411
 9412
 9413
 9414
 9415
 9416
 9417
 9418
 9419
 9420
 9421
 9422
 9423
 9424
 9425
 9426
 9427
 9428
 9429
 9430
 9431
 9432
 9433
 9434
 9435
 9436
 9437
 9438
 9439
 9440
 9441
 9442
 9443
 9444
 9445
 9446
 9447
 9448
 9449
 9450
 9451
 9452
 9453
 9454
 9455
 9456
 9457
 9458
 9459
 9460
 9461
 9462
 9463
 9464
 9465
 9466
 9467
 9468
 9469
 9470
 9471
 9472
 9473
 9474
 9475
 9476
 9477
 9478
 9479
 9480
 9481
 9482
 9483
 9484
 9485
 9486
 9487
 9488
 9489
 9490
 9491
 9492
 9493
 9494
 9495
 9496
 9497
 9498
 9499
 9500
 9501
 9502
 9503
 9504
 9505
 9506
 9507
 9508
 9509
 9510
 9511
 9512
 9513
 9514
 9515
 9516
 9517
 9518
 9519
 9520
 9521
 9522
 9523
 9524
 9525
 9526
 9527
 9528
 9529
 9530
 9531
 9532
 9533
 9534
 9535
 9536
 9537
 9538
 9539
 9540
 9541
 9542
 9543
 9544
 9545
 9546
 9547
 9548
 9549
 9550
 9551
 9552
 9553
 9554
 9555
 9556
 9557
 9558
 9559
 9560
 9561
 9562
 9563
 9564
 9565
 9566
 9567
 9568
 9569
 9570
 9571
 9572
 9573
 9574
 9575
 9576
 9577
 9578
 9579
 9580
 9581
 9582
 9583
 9584
 9585
 9586
 9587
 9588
 9589
 9590
 9591
 9592
 9593
 9594
 9595
 9596
 9597
 9598
 9599
 9600
 9601
 9602
 9603
 9604
 9605
 9606
 9607
 9608
 9609
 9610
 9611
 9612
 9613
 9614
 9615
 9616
 9617
 9618
 9619
 9620
 9621
 9622
 9623
 9624
 9625
 9626
 9627
 9628
 9629
 9630
 9631
 9632
 9633
 9634
 9635
 9636
 9637
 9638
 9639
 9640
 9641
 9642
 9643
 9644
 9645
 9646
 9647
 9648
 9649
 9650
 9651
 9652
 9653
 9654
 9655
 9656
 9657
 9658
 9659
 9660
 9661
 9662
 9663
 9664
 9665
 9666
 9667
 9668
 9669
 9670
 9671
 9672
 9673
 9674
 9675
 9676
 9677
 9678
 9679
 9680
 9681
 9682
 9683
 9684
 9685
 9686
 9687
 9688
 9689
 9690
 9691
 9692
 9693
 9694
 9695
 9696
 9697
 9698
 9699
 9700
 9701
 9702
 9703
 9704
 9705
 9706
 9707
 9708
 9709
 9710
 9711
 9712
 9713
 9714
 9715
 9716
 9717
 9718
 9719
 9720
 9721
 9722
 9723
 9724
 9725
 9726
 9727
 9728
 9729
 9730
 9731
 9732
 9733
 9734
 9735
 9736
 9737
 9738
 9739
 9740
 9741
 9742
 9743
 9744
 9745
 9746
 9747
 9748
 9749
 9750
 9751
 9752
 9753
 9754
 9755
 9756
 9757
 9758
 9759
 9760
 9761
 9762
 9763
 9764
 9765
 9766
 9767
 9768
 9769
 9770
 9771
 9772
 9773
 9774
 9775
 9776
 9777
 9778
 9779
 9780
 9781
 9782
 9783
 9784
 9785
 9786
 9787
 9788
 9789
 9790
 9791
 9792
 9793
 9794
 9795
 9796
 9797
 9798
 9799
 9800
 9801
 9802
 9803
 9804
 9805
 9806
 9807
 9808
 9809
 9810
 9811
 9812
 9813
 9814
 9815
 9816
 9817
 9818
 9819
 9820
 9821
 9822
 9823
 9824
 9825
 9826
 9827
 9828
 9829
 9830
 9831
 9832
 9833
 9834
 9835
 9836
 9837
 9838
 9839
 9840
 9841
 9842
 9843
 9844
 9845
 9846
 9847
 9848
 9849
 9850
 9851
 9852
 9853
 9854
 9855
 9856
 9857
 9858
 9859
 9860
 9861
 9862
 9863
 9864
 9865
 9866
 9867
 9868
 9869
 9870
 9871
 9872
 9873
 9874
 9875
 9876
 9877
 9878
 9879
 9880
 9881
 9882
 9883
 9884
 9885
 9886
 9887
 9888
 9889
 9890
 9891
 9892
 9893
 9894
 9895
 9896
 9897
 9898
 9899
 9900
 9901
 9902
 9903
 9904
 9905
 9906
 9907
 9908
 9909
 9910
 9911
 9912
 9913
 9914
 9915
 9916
 9917
 9918
 9919
 9920
 9921
 9922
 9923
 9924
 9925
 9926
 9927
 9928
 9929
 9930
 9931
 9932
 9933
 9934
 9935
 9936
 9937
 9938
 9939
 9940
 9941
 9942
 9943
 9944
 9945
 9946
 9947
 9948
 9949
 9950
 9951
 9952
 9953
 9954
 9955
 9956
 9957
 9958
 9959
 9960
 9961
 9962
 9963
 9964
 9965
 9966
 9967
 9968
 9969
 9970
 9971
 9972
 9973
 9974
 9975
 9976
 9977
 9978
 9979
 9980
 9981
 9982
 9983
 9984
 9985
 9986
 9987
 9988
 9989
 9990
 9991
 9992
 9993
 9994
 9995
 9996
 9997
 9998
 9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077
18078
18079
18080
18081
18082
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098
18099
18100
18101
18102
18103
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143
18144
18145
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173
18174
18175
18176
18177
18178
18179
18180
18181
18182
18183
18184
18185
18186
18187
18188
18189
18190
18191
18192
18193
18194
18195
18196
18197
18198
18199
18200
18201
18202
18203
18204
18205
18206
18207
18208
18209
18210
18211
18212
18213
18214
18215
18216
18217
18218
18219
18220
18221
18222
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244
18245
18246
18247
18248
18249
18250
18251
18252
18253
18254
18255
18256
18257
18258
18259
18260
18261
18262
18263
18264
18265
18266
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
18623
18624
18625
18626
18627
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640
18641
18642
18643
18644
18645
18646
18647
18648
18649
18650
18651
18652
18653
18654
18655
18656
18657
18658
18659
18660
18661
18662
18663
18664
18665
18666
18667
18668
18669
18670
18671
18672
18673
18674
18675
18676
18677
18678
18679
18680
18681
18682
18683
18684
18685
18686
18687
18688
18689
18690
18691
18692
18693
18694
18695
18696
18697
18698
18699
18700
18701
18702
18703
18704
18705
18706
18707
18708
18709
18710
18711
18712
18713
18714
18715
18716
18717
18718
18719
18720
18721
18722
18723
18724
18725
18726
18727
18728
18729
18730
18731
18732
18733
18734
18735
18736
18737
18738
18739
18740
18741
18742
18743
18744
18745
18746
18747
18748
18749
18750
18751
18752
18753
18754
18755
18756
18757
18758
18759
18760
18761
18762
18763
18764
18765
18766
18767
18768
18769
18770
18771
18772
18773
18774
18775
18776
18777
18778
18779
18780
18781
18782
18783
18784
18785
18786
18787
18788
18789
18790
18791
18792
18793
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 1993,1998,2002 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# cmd/backup/Makefile
#

include Makefile.backup

COMMONPRODSUBDIRS=	dump restore
UFSPRODSUBDIRS=
LIBSUBDIRS=	lib
LIBDONE=	$(LIBSUBDIRS)/.target_done
SUBDIRS=	$(LIBSUBDIRS) $(COMMONPRODSUBDIRS) $(UFSPRODSUBDIRS)
UFSSUBDIRS=	$(LIBSUBDIRS) $(COMMONPRODSUBDIRS) $(UFSPRODSUBDIRS)

UFSINSDIRS=	$(UFSROOTETC) $(UFSROOTUSR) $(UFSROOTUSRLIB) \
		$(UFSROOTUSRLIBFS) $(UFSROOTUSRSBIN)
PODIRS=		dump restore

.KEEP_STATE:

.DONE:
	@rm -f $(LIBDONE)

all debug lint check: $(COMMONPRODSUBDIRS) $(UFSPRODSUBDIRS)

_msg:	$(PODIRS)

clean clobber: $(SUBDIRS) .DONE

install:	$(UFSINSDIRS) $(UFSSUBDIRS)

$(UFSINSDIRS):
	$(INS.dir)

$(COMMONPRODSUBDIRS) $(UFSPRODSUBDIRS): $(LIBSUBDIRS) FRC
	$(DO_SUBDIR)

$(LIBSUBDIRS): FRC
	$(DO_SUBDIR)

FRC:
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# cmd/backup/Makefile.backup
#
# Copyright (c) 2018, Joyent, Inc.

include $(SRC)/cmd/Makefile.cmd

# -c: continuation line indentation (broken for multi-line continuations)
# -h: heuristic checks (sometimes wrong)
# -p: extra-picky
# -v: verbose
# -C: ignore header block comments
# -P: check for non-POSIX types
#CSTYLEFLAGS=	-c -h -p -v -P
CSTYLEFLAGS=	-h -p -P

CERRWARN +=	-Wno-implicit-function-declaration
CERRWARN +=	-Wno-parentheses
CERRWARN +=	$(CNOWARN_UNINIT)
CERRWARN +=	-Wno-clobbered

# not linted
SMATCH=off

UFSROOTETC=		$(ROOT)/etc
UFSROOTUSR=		$(ROOT)/usr
UFSROOTUSRLIB=		$(UFSROOTUSR)/lib
UFSROOTUSRLIBFS=	$(UFSROOTUSRLIB)/fs
UFSROOTUSRLIBFSTYPE=	$(UFSROOTUSRLIBFS)/$(FSTYPE)
UFSROOTUSRSBIN=		$(UFSROOTUSR)/sbin

UFSROOTPKGUSRLIBFSTYPE=	$(PROG:%=$(UFSROOTUSRLIBFSTYPE)/%)
UFSROOTPKGETC=		$(PROT:%=$(UFSROOTETC)/%)

$(UFSROOTETC)/%: % | $(UFSROOTETC)
	$(INS.file)

$(UFSROOTUSRLIBFSTYPE)/%: % | $(UFSROOTUSRLIBFSTYPE)
	$(INS.file)

# set up TARGET macro for all Makefile here
all:		TARGET= all
install:	TARGET= install
clean:		TARGET= clean
clobber:	TARGET= clobber
debug:		TARGET= debug
check:		TARGET= check
_msg:		TARGET= _msg

# default dump library is the -O one
DUMPLIB=	libdump.a

# define the DO_SUBDIR macro, so that it can be changed here for all Makefiles
DO_SUBDIR=	cd $@; pwd; $(MAKE) $(TARGET)
DO_LIBDIR=	@cd $(@D); pwd; $(MAKE) $(TARGET)
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

# support for debugging
debug:		DUMPLIB= libdump_g.a
debug:		CFLAGS= $(CCGDEBUG) -DDEBUG -DFDEBUG -DTDEBUG $(SBFLAGS)
DEBUGDIR=	.debug
DEBUGOBJS=	$(OBJECTS:%=$(DEBUGDIR)/%)
DEBUGPRODUCTS=	$(UFSDEBUGPRODUCT)
UFSDEBUGPRODUCT= $(PRODUCT:%=%_g)
UFSDEBUGPROG=	$(PROG:%=%_g)

debug:		$(UFSDEBUGPROG)

$(UFSDEBUGPROG): $(DEBUGDIR) $$(DEBUGOBJS) $$(LIBDUMP)
	$(LINK.c) -o $@ $(DEBUGOBJS) $(LDLIBS)
	$(POST_PROCESS)

$(DEBUGDIR)/%.o: %.c
	$(COMPILE.c) -o $@ $<
	$(POST_PROCESS_O)

$(DEBUGDIR):
	-@mkdir -p $@

include $(SRC)/cmd/Makefile.targ
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# cmd/backup/dump/Makefile
#
# Copyright (c) 1980 Regents of the University of California.
# All rights reserved.  The Berkeley software License Agreement
# specifies the terms and conditions for redistribution.
#
#	dump.h			header file
#	dumpfstab.c		reads fstab/vfstab, mtab/mnttab
#	dumpitime.c		reads /etc/dumpdates
#	dumpmain.c		driver
#	dumponline.c		online dump support
#	dumpoptr.c		operator interface
#	dumptape.c		handles the mag tape and opening/closing
#	dumptraverse.c		traverses the file system
#	lftw.c			fixed version of ftw(3)
#	partial.c		partial filesystem dump support
#	unctime.c		undo ctime
#
# CPPFLAGS:
#	DEBUG			use local directory to find ddate and dumpdates
#	PARTIAL			enable partial filesystem dump support
#	TDEBUG			trace out the process forking
#
#	CAUTION: FSTYPE must be defined before including ../Makefile.backup,
#		 otherwise UFSROOTPKGUSRLIBFSTYPE doesn't get expanded
#		 properly and the magic doesn't happen.

FSTYPE=		ufs

include ../Makefile.backup

#CFLAGS +=	-DTDEBUG
UFSDUMP=	ufsdump

PROG=		$(UFSDUMP)
PRODUCT=	$(PROG)
ROLLDIR=	../../fs.d/ufs/roll_log
ROLLOBJS=	$(ROLLDIR)/$(MACH)/roll_log.o
ROLLSRCS=	$(ROLLDIR)/roll_log.c

LOBJECTS=	dumponline.o dumpfstab.o dumpitime.o dumpmain.o \
		dumpoptr.o dumptape.o dumptraverse.o unctime.o \
		partial.o lftw.o
OBJECTS=	$(LOBJECTS) roll_log.o
SRCS=		$(LOBJECTS:.o=.c)

POFILES=	$(OBJECTS:.o=.po) ../lib/libdump.po
POFILE=		ufsdump.po
ROLLPOFILES=	roll_log.po

# XXX This is only needed for the check: target.  It would be nice to
# XXX automatically generate the list when needed.
HEADERS=	../../../head/protocols/dumprestore.h \
	../../fs.d/ufs/roll_log/roll_log.h ../include/byteorder.h \
	../include/memutils.h \
	../include/rmt.h dump.h dumpusg.h

CLOBBERFILES +=	$(PRODUCT) $(DEBUGPRODUCTS) dumpdates

LOCAL=		.
GENERAL=	../include
GLOBAL=		../../../head
CPPFLAGS=	-I$(LOCAL) -I$(GENERAL) -I$(GLOBAL) -DPARTIAL \
		-I$(ROLLDIR) $(CPPFLAGS.master) \
		-D_LARGEFILE64_SOURCE=1
CERRWARN +=	-Wno-implicit-function-declaration
CERRWARN +=	-Wno-parentheses
CERRWARN +=	$(CNOWARN_UNINIT)
CERRWARN +=	-Wno-clobbered
CERRWARN +=	-Wno-extra
LIBDUMP=	../lib/libdump.a
LINTLIBDUMP=	../lib/llib-ldump.ln -lkstat
LDLIBS +=	$(BSTATIC) -L../lib -ldump $(BDYNAMIC) \
		-lsocket -lnsl -ladm -lm -lkstat

UFSROOTDUMPDATES=	$(UFSROOTETC)/dumpdates
UFSROOTLINK=		$(UFSROOTUSRSBIN)/$(PROG)
LINKVALUE=		../lib/fs/$(FSTYPE)/$(PROG)
LIBDIR=			$(UFSROOTUSRLIBFS)/$(FSTYPE)
$(UFSROOTDUMPDATES):	FILEMODE=	0664
$(LIBDIR):		DIRMODE=	0755

FILEMODE=	04555

.KEEP_STATE:

all:		$(PRODUCT)

$(PROG):	$(OBJECTS) $$(LIBDUMP)
	$(LINK.c) -o $@ $(OBJECTS) $(LDLIBS)
	$(POST_PROCESS)

$(LIBDUMP): FRC
	$(DO_LIBDIR)

$(POFILE):	$(POFILES)
	$(RM) $@; cat $(POFILES) > $@

FRC:

install:	all $(LIBDIR) $(UFSROOTPKGUSRLIBFSTYPE) dumpdates \
		$(UFSROOTDUMPDATES) $(UFSROOTLINK)

dumpdates:
	cp /dev/null dumpdates

#XXX Should actually note return value from close(2), particularly regarding
#XXX tape descriptors.  Some drives don't actually force anything to media
#XXX except when they are writing a file mark....

# grep is looking for non-space/tab
lint:	$(SRCS) $(ROLLSRCS) $$(LINTLIBDUMP)
	$(LINT.c) $(SRCS) $(ROLLSRCS) $(LINTLIBDUMP) 2>&1 \
	| sed -f lint.sed | grep '^[^ 	]'

$(LINTLIBDUMP):	FRC
	cd ../lib; pwd; $(MAKE) lint
	pwd

check:	FRC
	$(CSTYLE) $(CSTYLEFLAGS) $(SRCS) $(ROLLSRCS) $(HEADERS)
	$(HDRCHK) $(HDRCHKFLAGS) $(HEADERS)

clean:
	$(RM) $(OBJECTS) $(ROLLOBJS) $(DEBUGOBJS) *.ln

$(LIBDIR):
	$(INS.dir)

$(UFSROOTLINK):
	-$(RM) $@; $(SYMLINK) $(LINKVALUE) $(UFSROOTLINK)

roll_log.o:
	cd $(ROLLDIR) ; pwd ; $(MAKE)
	cp $(ROLLOBJS) .
	pwd

$(ROLLPOFILES): $(ROLLSRCS)
	cd $(ROLLDIR) ; pwd ; $(MAKE) `echo $(ROLLPOFILES) | sed -e 's@$(ROLLDIR)/@@g'`
	cp $(ROLLDIR)/*.po .
	pwd

../lib/libdump.po:
	cd ../lib ; pwd ; $(MAKE)  libdump.po

include ../Makefile.targ
Copyright (c) 1980 Regents of the University of California.  
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above
       copyright notice, this list of conditions and the following
       disclaimer in the documentation and/or other materials provided
       with the distribution.
    3. All advertising materials mentioning features or use of this
       software must display the following acknowledgement:
           This product includes software developed by the University
           of California, Berkeley and its contributors.
    4. Neither the name of the University nor the names of its
       contributors may be used to endorse or promote products derived
       from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
PORTIONS OF BACKUP COMMAND FUNCTIONALITY
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Copyright (c) 1980 Regents of the University of California.
 * All rights reserved. The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#ifndef _DUMP_H
#define	_DUMP_H

#include <stdio.h>
#include <locale.h>
#include <sys/types.h>
#include <ctype.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <fcntl.h>
#include <utmpx.h>
#include <signal.h>
#include <stdlib.h>
#include <time.h>
#include <sys/param.h>	/* for MAXBSIZE */
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/vnode.h>	/* needed by inode.h */
#include <setjmp.h>
#include <sys/mman.h>
#include <assert.h>
#include <dumpusg.h>
#include <kstat.h>
#include <sys/fssnap_if.h>
#include <libgen.h>
#include <limits.h>

#ifdef	__cplusplus
extern "C" {
#endif

#define	SUPPORTS_MTB_TAPE_FORMAT
#include <protocols/dumprestore.h>
#include <memutils.h>
#include <note.h>

#define	NI		16
#define	MAXINOPB	(MAXBSIZE / sizeof (struct dinode))
#define	MAXNINDIR	(MAXBSIZE / sizeof (daddr32_t))

#ifndef roundup
#define	roundup(x, y)	((((x)+((y)-1))/(y))*(y))
#endif
#ifndef MIN
#define	MIN(a, b)	(((a) < (b)) ? (a) : (b))
#endif
#ifndef MAX
#define	MAX(a, b)	(((a) > (b)) ? (a) : (b))
#endif

/*
 * Define an overflow-free version of howmany so that we don't
 * run into trouble with large files.
 */
#define	d_howmany(x, y)	((x) / (y) + ((x) % (y) != 0))

#define	MWORD(m, i)	(m[(ino_t)(i-1)/NBBY])
#define	MBIT(i)		((1<<((ino_t)(i-1)%NBBY))&0xff)
#define	BIS(i, w)	(MWORD(w, i) |= MBIT(i))
#define	BIC(i, w)	(MWORD(w, i) &= ~MBIT(i))
#define	BIT(i, w)	(MWORD(w, i) & MBIT(i))

extern uint_t	msiz;
extern uchar_t	*clrmap;
extern uchar_t	*dirmap;
extern uchar_t	*filmap;
extern uchar_t	*nodmap;
extern uchar_t	*shamap;
extern uchar_t	*activemap;

/*
 *	All calculations done in 0.1" units!
 */

extern char	*disk;		/* name of the disk file */
extern char	*dname;		/* name to put in /etc/dumpdates */
extern int	disk_dynamic;	/* true if disk refers to dynamic storage */
extern char	*tape;		/* name of the tape file */
extern char	*host;	/* name of the remote tape host (may be "user@host") */
extern char	*dumpdev;	/* hostname:device for current volume */
extern char	*sdumpdev; /* short form of dumpdev (no user name if remote) */
extern char	*increm; /* name of file containing incremental information */
extern char	*filesystem;	/* name of the file system */
extern char	*myname;	/* argv[0] without leading path components */
extern char	lastincno;	/* increment number of previous dump */
extern char	incno;		/* increment number */
extern char	*tlabel;	/* what goes in tape header c_label field */
extern int	uflag;		/* update flag */
extern int	fi;		/* disk file descriptor */
extern int	to;		/* tape file descriptor */
extern int	mapfd;		/* block disk device descriptor for mmap */
extern int	pipeout;	/* true => output to standard output */
extern int	tapeout;	/* true => output to a tape drive */
extern ino_t	ino;		/* current inumber; used globally */
extern off_t	pos;		/* starting offset within ino; used globally */
extern int	leftover; /* number of tape recs left over from prev vol */
extern int	nsubdir;	/* counts subdirs, for deciding to dump a dir */
extern int	newtape;	/* new tape flag */
extern int	nadded;		/* number of added sub directories */
extern int	dadded;		/* directory added flag */
extern int	density;	/* density in 0.1" units */
extern ulong_t	tsize;		/* tape size in 0.1" units */
extern u_offset_t esize;	/* estimated tape size, blocks */
extern u_offset_t o_esize;	/* number of header blocks (overhead) */
extern u_offset_t f_esize;	/* number of TP_BSIZE blocks for files/maps */
extern uint_t	etapes;		/* estimated number of tapes */
extern uint_t	ntrec;		/* 1K records per tape block */
extern int	tenthsperirg;	/* 1/10" per tape inter-record gap */
extern dev_t	partial_dev;	/* id of BLOCK device used in partial mode */
extern pid_t	dumppid;	/* process-ID of top-level process */

extern int	verify;		/* verify each volume */
extern int	doingverify;	/* true => doing a verify pass */
extern int	active;		/* recopy active files */
extern int	doingactive;	/* true => redumping active files */
extern int	archive;	/* true => saving a archive in archivefile */
extern char	*archivefile;	/* name of archivefile */
extern int	archive_opened;	/* have opened/created the archivefile */
extern int	notify;		/* notify operator flag */
extern int	diskette;	/* true if dumping to a diskette */
extern int	cartridge;	/* true if dumping to a cartridge tape */
extern uint_t	tracks;		/* number of tracks on a cartridge tape */
extern int	printsize;	/* just print estimated size and exit */
extern int	offline;	/* take tape offline after rewinding */
extern int	autoload; /* wait for next tape to autoload; implies offline */
extern int	autoload_tries;	/* number of times to check on autoload */
extern int	autoload_period; /* seconds, tries*period = total wait time */
extern int	doposition;	/* move to specified... */
extern daddr32_t filenum;	/* position of dump on 1st volume */
extern int	dumpstate;	/* dump output state (see below) */
extern int	dumptoarchive;	/* mark records to be archived */

extern int	blockswritten;	/* number of blocks written on current tape */
extern uint_t	tapeno;		/* current tape number */

extern struct fs *sblock;	/* the file system super block */
extern int	shortmeta;	/* current file has small amount of metadata */
extern union u_shadow c_shadow_save[1];

extern time_t	*telapsed;	/* time spent writing previous tapes */
extern time_t	*tstart_writing; /* when we started writing the latest tape */
extern time_t	*tschedule; /* when next to give a remaining-time estimate */

extern char	*debug_chdir;	/* non-NULL means to mkdir this/pid, */
			/* and chdir there, once for each separate child */

/*
 * Defines for the msec part of
 * inode-based times, since we're
 * not part of the kernel.
 */
#define	di_atspare	di_ic.ic_atspare
#define	di_mtspare	di_ic.ic_mtspare
#define	di_ctspare	di_ic.ic_ctspare

#define	HOUR	(60L*60L)
#define	DAY	(24L*HOUR)
#define	YEAR	(365L*DAY)

/*
 *	Dump output states
 */
#define	DS_INIT		0
#define	DS_START	1
#define	DS_CLRI		2
#define	DS_BITS		3
#define	DS_DIRS		4
#define	DS_FILES	5
#define	DS_END		6
#define	DS_DONE		7

/*
 *	Exit status codes
 */
#define	X_FINOK		0	/* normal exit */
#define	X_REWRITE	2	/* restart writing from the check point */
#define	X_ABORT		3	/* abort all of dump; no checkpoint restart */
#define	X_VERIFY	4	/* verify the reel just written */
#define	X_RESTART	5	/* abort all progress so far; attempt restart */

#define	NINCREM	"/etc/dumpdates"	/* new format incremental info */

#define	TAPE	"/dev/rmt/0b"		/* default tape device */
#define	OPGRENT	"sys"			/* group entry to notify */
#define	DIALUP	"ttyd"			/* prefix for dialups */

#define	DISKETTE	"/dev/rfd0c"

#define	NBUF		64		/* number of output buffers */
#define	MAXNTREC	256		/* max tape blocking factor (in Kb) */

/*
 *	The contents of the file NINCREM are maintained both on
 *	a linked list and then (eventually) arrayified.
 */
struct	idates {
	char	id_name[MAXNAMLEN+3];
	char	id_incno;
	time32_t id_ddate;
};

extern size_t	nidates;		/* number of records (might be zero) */
extern struct	idates	**idatev;	/* the arrayfied version */
#define	ITITERATE(i, ip)	\
	for (i = 0; i < nidates && (ip = idatev[i]) != NULL; i++)

/*
 * Function declarations
 */
#ifdef __STDC__
/*
 * dumpfstab.c
 */
extern void mnttabread(void);
extern struct mntent *mnttabsearch(char *, int);
extern void setmnttab(void);
extern struct mntent *getmnttab(void);
/*
 * dumpitime.c
 */
extern char *prdate(time_t);
extern void inititimes(void);
extern void getitime(void);
extern void putitime(void);
extern void est(struct dinode *);
extern time32_t is_fssnap_dump(char *);
extern void bmapest(uchar_t *);
/*
 * dumplabel.c
 */
extern void getlabel(void);
/*
 * dumpmain.c
 */
extern void child_chdir(void);
extern char *unrawname(char *);
extern void sigAbort(int);
extern char *rawname(char *);
extern char *lf_rawname(char *);
extern time32_t timeclock(time32_t);
#ifdef signal
extern void (*nsignal(int, void (*)(int)))(int);
#endif
extern int safe_file_open(const char *file, int mode, int perms);
extern int safe_device_open(const char *file, int mode, int perms);
extern FILE *safe_fopen(const char *filename, const char *smode, int perms);
/*
 * dumponline.c
 */
extern void allocino(void);
extern void freeino(void);
extern void saveino(ino_t, struct dinode *);
extern void resetino(ino_t);
extern long getigen(ino_t);
extern int lf_ismounted(char *, char *);
extern int isoperator(uid_t, gid_t);
extern int lockfs(char *, char *);
extern int openi(ino_t, long, char *);
extern caddr_t mapfile(int, off_t, off_t, int);
extern void unmapfile(void);
extern void stattoi(struct stat *, struct dinode *);
extern void dumpfile(int, caddr_t, off_t, off_t, off_t, int, int);
extern void activepass(void);
/*
 * dumpoptr.c
 */
extern int query(char *);
extern int query_once(char *, int);
extern void interrupt(int);
extern void broadcast(char *);
extern void timeest(int, int);
/*PRINTFLIKE1*/
extern void msg(const char *, ...);
/*PRINTFLIKE1*/
extern void msgtail(const char *, ...);
extern void lastdump(int);
extern char *getresponse(char *, char *);
/*
 * dumptape.c
 */
extern void alloctape(void);
extern void reset(void);
extern void spclrec(void);
extern void taprec(uchar_t *, int, int);
extern void dmpblk(daddr32_t, size_t, off_t);
extern void toslave(void (*)(ino_t), ino_t);
extern void doinode(ino_t);
extern void dospcl(ino_t);
extern void flushcmds(void);
extern void flusht(void);
extern void nextdevice(void);
extern int isrewind(int);
extern void trewind(void);
extern void close_rewind(void);
extern void changevol(void);
extern void otape(int);
extern void dumpabort(void);
extern void dumpailing(void);
extern void Exit(int);
extern void positiontape(char *);
/*
 * dumptraverse.c
 */
extern void pass(void (*)(struct dinode *), uchar_t *);
extern void mark(struct dinode *);
extern void active_mark(struct dinode *);
extern void markshad(struct dinode *);
extern void estshad(struct dinode *);
extern void freeshad();
extern void add(struct dinode *);
extern void dirdump(struct dinode *);
extern void dump(struct dinode *);
extern void lf_dump(struct dinode *);
extern void dumpblocks(ino_t);
extern void bitmap(uchar_t *, int);
extern struct dinode *getino(ino_t);
extern void bread(diskaddr_t, uchar_t *, size_t);
extern int hasshortmeta(struct dinode **ip);
/*
 * lftw.c
 */
extern int lftw(const char *,
	int (*)(const char *, const struct stat *, int), int);
extern int lf_lftw(const char *,
	int (*)(const char *, const struct stat64 *, int), int);
/*
 * partial.c
 */
extern void partial_check(void);
extern void lf_partial_check(void);
extern int partial_mark(int, char **);
/*
 * unctime.c
 */
extern time_t unctime(char *);
#else	/* !STDC */
/*
 * dumpfstab.c
 */
extern void mnttabread();
extern struct mntent *mnttabsearch();
extern void setmnttab();
extern struct mntent *getmnttab();
/*
 * dumpitime.c
 */
extern char *prdate();
extern void inititimes();
extern void getitime();
extern void putitime();
extern void est();
extern time32_t is_fssnap_dump();
extern void bmapest();
/*
 * dumplabel.c
 */
extern void getlabel();
/*
 * dumpmain.c
 */
extern void child_chdir();
extern char *unrawname();
extern void sigAbort();
extern char *rawname();
extern char *lf_rawname();
extern time_t timeclock();
#ifdef signal
extern void nsignal();
#endif
extern int safe_file_open();
extern int safe_device_open();
extern FILE *safe_fopen();
/*
 * dumponline.c
 */
extern void allocino();
extern void freeino();
extern void saveino();
extern void resetino();
extern long getigen();
extern int lf_ismounted();
extern int isoperator();
extern ulong_t lockfs();
extern int openi();
extern caddr_t mapfile();
extern void unmapfile();
extern void stattoi();
extern void dumpfile();
extern void activepass();
/*
 * dumpoptr.c
 */
extern int query();
extern int query_once();
extern void interrupt();
extern void broadcast();
extern void timeest();
extern void msg();
extern void msgtail();
extern void lastdump();
extern char *getresponse();
/*
 * dumptape.c
 */
extern void alloctape();
extern void reset();
extern void spclrec();
extern void taprec();
extern void dmpblk();
extern void toslave();
extern void doinode();
extern void dospcl();
extern void flushcmds();
extern void flusht();
extern void nextdevice();
extern int isrewind();
extern void trewind();
extern void close_rewind();
extern void changevol();
extern void otape();
extern void dumpabort();
extern void dumpailing();
extern void Exit();
extern void positiontape();
/*
 * dumptraverse.c
 */
extern void pass();
extern void mark();
extern void active_mark();
extern void markshad();
extern void estshad();
extern void freeshad();
extern void add();
extern void dirdump();
extern void dump();
extern void lf_dump();
extern void dumpblocks();
extern void bitmap();
extern struct dinode *getino();
extern void bread();
extern int hasshortmeta();
/*
 * lftw.c
 */
extern int lftw();
extern int lf_lftw();
/*
 * partial.c
 */
extern void partial_check();
extern void lf_partial_check();
extern int partial_mark();
/*
 * unctime.c
 */
extern time_t unctime();
#endif /* __STDC__ */

/* Insufficiently-featureful system header files... */
NOTE(ALIGNMENT(mmap, 8))


#ifdef	__cplusplus
}
#endif

#endif /* _DUMP_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1996,1998 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#include "dump.h"

/*
 * File system mount table input routines.  We handle a
 * a combination of BSD and SVR4 formats by coding functions
 * to explicitly read the SVR4 vfstab file and using
 * #define's to build a routine to read both BSD files
 * (fstab and mtab) and SVR4's mnttab file.  Internally
 * we keep everything in the common (mtab/mnttab) format.
 */
static struct pmntent {
	struct mntent	*pm_mnt;
	struct pmntent	*pm_next;
} *mnttable;

/* Note that nothing is ever free()'d, so this is safe */
#define	mntstrdup(s)	((s) ? strdup((s)) : "")

#ifdef __STDC__
static struct mntent *mygetmntent(FILE *, char *);
static struct pmntent *addmtab(char *, struct pmntent *);
static struct mntent *allocmntent(struct mntent *);
#else /* !__STDC__ */
static struct pmntent *addmtab();
static struct mntent *mygetmntent();
static struct mntent *allocmntent();
static int idatesort();
#endif

static struct mntent *
mygetmntent(f, name)
	FILE *f;
	char *name;
{
	static struct mntent mt;
	int status;

	if ((status = getmntent(f, &mt)) == 0)
		return (&mt);

	switch (status) {
	case EOF:	break;		/* normal exit condition */
	case MNT_TOOLONG:
		msg(gettext("%s has a line that is too long\n"), name);
		break;
	case MNT_TOOMANY:
		msg(gettext("%s has a line with too many entries\n"), name);
		break;
	case MNT_TOOFEW:
		msg(gettext("%s has a line with too few entries\n"), name);
		break;
	default:
		msg(gettext(
			"Unknown return code, %d, from getmntent() on %s\n"),
			status, name);
		break;
	}

	return (NULL);
}

/*
 * Read in SVR4 vfstab-format table.
 */
static struct pmntent *
addvfstab(tablename, pm)
	char	*tablename;
	struct pmntent *pm;
{
	struct mnttab *mnt;
	struct vfstab vfs;
	FILE	*tp;
	int status;

	assert(((mnttable == NULL) && (pm == NULL)) || (pm != NULL));

	/*
	 * No need to secure this, as tablename is hard-coded to VFSTAB,
	 * and that file is in /etc.  If random people have write-permission
	 * there, then there are more problems than any degree of paranoia
	 * on our part can fix.
	 */
	tp = fopen(tablename, "r");
	if (tp == (FILE *)0) {
		msg(gettext("Cannot open %s for dump table information.\n"),
			tablename);
		return ((struct pmntent *)0);
	}
	while ((status = getvfsent(tp, &vfs)) == 0) {
		if (vfs.vfs_fstype == (char *)0 ||
		    strcmp(vfs.vfs_fstype, MNTTYPE_42) != 0)
			continue;

		mnt = (struct mnttab *)xmalloc(sizeof (*mnt));
		mnt->mnt_fsname = mntstrdup(vfs.vfs_special);
		mnt->mnt_dir = mntstrdup(vfs.vfs_mountp);
		mnt->mnt_type = mntstrdup(vfs.vfs_fstype);
		mnt->mnt_opts = mntstrdup(vfs.vfs_mntopts);

		if (mnttable == (struct pmntent *)0)
			/*
			 * Guaranteed by caller that pm will also be NULL,
			 * so no memory leak to worry about.
			 */
			mnttable = pm = (struct pmntent *)xmalloc(sizeof (*pm));
		else {
			/* Guaranteed pm not NULL by caller and local logic */
			pm->pm_next = (struct pmntent *)xmalloc(sizeof (*pm));
			pm = pm->pm_next;
		}
		pm->pm_mnt = mnt;
		pm->pm_next = (struct pmntent *)0;
	}

	switch (status) {
	case EOF:	break;		/* normal exit condition */
	case VFS_TOOLONG:
		msg(gettext("%s has a line that is too long\n"), tablename);
		break;
	case VFS_TOOMANY:
		msg(gettext("%s has a line with too many entries\n"),
			tablename);
		break;
	case VFS_TOOFEW:
		msg(gettext("%s has a line with too few entries\n"), tablename);
		break;
	default:
		msg(gettext(
			"Unknown return code, %d, from getvfsent() on %s\n"),
			status, tablename);
		break;
	}
	(void) fclose(tp);
	return (pm);
}

static struct mntent *
allocmntent(mnt)
	struct mntent *mnt;
{
	struct mntent *new;

	new = (struct mntent *)xmalloc(sizeof (*mnt));
	new->mnt_fsname = mntstrdup(mnt->mnt_fsname);	/* mnt_special */
	new->mnt_dir = mntstrdup(mnt->mnt_dir);		/* mnt_mountp  */
	new->mnt_type = mntstrdup(mnt->mnt_type);	/* mnt_fstype  */
	new->mnt_opts = mntstrdup(mnt->mnt_opts);	/* mnt_mntopts */
	return (new);
}

void
mnttabread()
{
	struct pmntent *pm = (struct pmntent *)0;

	if (mnttable != (struct pmntent *)0)
		return;
	/*
	 * Read in the file system mount tables.  Order
	 * is important as the first matched entry is used
	 * if the target device/filesystem is not mounted.
	 * We try fstab or vfstab first, then mtab or mnttab.
	 */
	pm = addvfstab(VFSTAB, pm);
	(void) addmtab(MOUNTED, pm);
}

static struct pmntent *
addmtab(tablename, pm)
	char	*tablename;
	struct pmntent *pm;
{
	struct mntent *mnt;
	FILE	*tp;

	tp = setmntent(tablename, "r");
	if (tp == (FILE *)0) {
		msg(gettext("Cannot open %s for dump table information.\n"),
			tablename);
		return ((struct pmntent *)0);
	}
	while (mnt = mygetmntent(tp, tablename)) {
		if (mnt->mnt_type == (char *)0 ||
		    strcmp(mnt->mnt_type, MNTTYPE_42) != 0)
			continue;

		mnt = allocmntent(mnt);
		if (mnttable == (struct pmntent *)0)
			/*
			 * Guaranteed by caller that pm will also be NULL,
			 * so no memory leak to worry about.
			 */
			mnttable = pm = (struct pmntent *)xmalloc(sizeof (*pm));
		else {
			/* Guaranteed pm not NULL by caller and local logic */
			pm->pm_next = (struct pmntent *)xmalloc(sizeof (*pm));
			pm = pm->pm_next;
		}
		pm->pm_mnt = mnt;
		pm->pm_next = (struct pmntent *)0;
	}
	(void) endmntent(tp);
	return (pm);
}

/*
 * Search in fstab and potentially mtab for a file name.
 * If "mounted" is non-zero, the target file system must
 * be mounted in order for the search to succeed.
 * This file name can be either the special or the path file name.
 *
 * The entries in either fstab or mtab are the BLOCK special names,
 * not the character special names.
 * The caller of mnttabsearch assures that the character device
 * is dumped (that is much faster)
 *
 * The file name can omit the leading '/'.
 */
struct mntent *
mnttabsearch(key, mounted)
	char	*key;
	int	mounted;
{
	struct pmntent *pm;
	struct mntent *mnt;
	struct mntent *first = (struct mntent *)0;
	char *s;
	char *gotreal;
	char path[MAXPATHLEN];

	for (pm = mnttable; pm; pm = pm->pm_next) {
		s = NULL;
		mnt = pm->pm_mnt;
		if (strcmp(mnt->mnt_dir, key) == 0)
			goto found;
		if (strcmp(mnt->mnt_fsname, key) == 0)
			goto found;
		if ((s = rawname(mnt->mnt_fsname)) != NULL &&
		    strcmp(s, key) == 0)
			goto found;

		gotreal = realpath(mnt->mnt_dir, path);
		if (gotreal && strcmp(path, key) == 0)
			goto found;
		if (key[0] != '/') {
			if (*mnt->mnt_fsname == '/' &&
			    strcmp(mnt->mnt_fsname + 1, key) == 0)
				goto found;
			if (*mnt->mnt_dir == '/' &&
			    strcmp(mnt->mnt_dir + 1, key) == 0)
				goto found;
			if (gotreal && *path == '/' &&
			    strcmp(path + 1, key) == 0)
				goto found;
		}
		if (s != NULL && s != mnt->mnt_fsname)
			free(s);
		continue;
found:
		/* Pointer comparison, not string comparison */
		if (s != NULL && s != mnt->mnt_fsname)
			free(s);
		/*
		 * Found a match; return immediately if
		 * it is mounted (valid), otherwise just
		 * record if it's the first matched entry.
		 */
		if (lf_ismounted(mnt->mnt_fsname, mnt->mnt_dir) > 0)
			return (mnt);
		else if (first == (struct mntent *)0)
			first = mnt;
	}
	/*
	 * If we get here, there were either
	 * no matches, or no matched entries
	 * were mounted.  Return failure if
	 * we were supposed to find a mounted
	 * entry, otherwise return the first
	 * matched entry (or null).
	 */
	if (mounted)
		return ((struct mntent *)0);
	return (first);
}

static struct pmntent *current;
static int set;

void
#ifdef __STDC__
setmnttab(void)
#else
setmnttab()
#endif
{
	current = mnttable;
	set = 1;
}

struct mntent *
#ifdef __STDC__
getmnttab(void)
#else
getmnttab()
#endif
{
	struct pmntent *pm;

	if (!set)
		setmnttab();
	pm = current;
	if (current) {
		current = current->pm_next;
		return (pm->pm_mnt);
	}
	return ((struct mntent *)0);
}
/*
 * Copyright 1996-1998, 2002-2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/

/*
 * Copyright (c) 1980 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#include "dump.h"

#ifndef LOCK_EX
static	struct flock fl;
#define	flock(fd, flag) (fl.l_type = (flag), fcntl(fd, F_SETLKW, &fl))
#define	LOCK_EX F_WRLCK
#define	LOCK_SH F_RDLCK
#define	LOCK_UN F_UNLCK
#endif

char		*disk;
char		*dname;
char		*increm;

char		incno;
char		lastincno;
int		uflag;
uint_t		msiz;
u_offset_t	o_esize;
u_offset_t	f_esize;

/*
 * Print a date.  A date of 0 is the beginning of time (the "epoch").
 * If the 2nd argument is non-zero, it is ok to format the date in
 * locale-specific form, otherwise we use ctime.  We must use ctime
 * for dates such as those in the dumpdates file, which must be
 * locale-independent.
 */
char *
prdate(time_t d)
{
	static char buf[256];
	struct tm *tm;
	char *p;

	if (d == 0)
		return (gettext("the epoch"));

	tm = localtime(&d);
	if (strftime(buf, sizeof (buf), "%c", tm) != 0) {
		p = buf;
	} else {
		/* Wouldn't fit in buf, fall back */
		p = ctime(&d);
		p[24] = '\0';	/* lose trailing newline */
	}
	return (p);
}

struct	idates	**idatev = 0;
size_t	nidates = 0;
static	int	idates_in = 0;		/* we have read the increment file */
static	int	recno;

#ifdef __STDC__
static void readitimes(FILE *);
static void recout(FILE	*, struct idates *);
static int getrecord(FILE *, struct idates *);
static int makeidate(struct idates *, char *);
#else
static void readitimes();
static void recout();
static int getrecord();
static int makeidate();
#endif

void
inititimes(void)
{
	FILE *df;
	int saverr;

	if (idates_in)
		return;
	if (increm == NULL || *increm == '\0') {
		msg(gettext("inititimes: No dump record file name defined\n"));
		dumpabort();
		/*NOTREACHED*/
	}
	/*
	 * No need to secure this, as increm is hard-coded to NINCREM,
	 * and that file is in /etc.  If random people have write-permission
	 * there, then there are more problems than any degree of paranoia
	 * on our part can fix.
	 */
	if ((df = fopen(increm, "r")) == NULL) {
		saverr = errno;
		if (errno == ENOENT)
			msg(gettext(
			    "Warning - dump record file `%s' does not exist\n"),
			    increm);
		else {
			msg(gettext("Cannot open dump record file `%s': %s\n"),
			    increm, strerror(saverr));
			dumpabort();
			/*NOTREACHED*/
		}
		return;
	}
	if (uflag && access(increm, W_OK) < 0) {
		msg(gettext("Cannot access dump record file `%s' for update\n"),
		    increm);
		dumpabort();
		/*NOTREACHED*/
	}
	(void) flock(fileno(df), LOCK_SH);
	readitimes(df);
	(void) fclose(df);
}

static void
readitimes(FILE *df)
{
	struct idates *idp;

	recno = 0;
	for (;;) {
		idp = (struct idates *)xcalloc(1, sizeof (*idp));
		if (getrecord(df, idp) < 0) {
			free((char *)idp);
			break;
		}
		nidates++;
		idatev = (struct idates **)xrealloc((void *)idatev,
		    nidates * (size_t)sizeof (*idatev));
		idatev[nidates - 1] = idp;
	}
	/* LINTED: assigned value is used in inititimes */
	idates_in = 1;
}

void
getitime(void)
{
	struct	idates	*ip;
	int	i;
	char	*fname;

	/*
	 * if an alternate name was specified via the N flag, use it instead
	 * of the disk name.
	 */
	if (dname != NULL)
		fname = dname;
	else
		fname = disk;

#ifdef FDEBUG

	/* XGETTEXT:  #ifdef FDEBUG only */
	msg(gettext("Looking for name %s in increm = %s for delta = %c\n"),
	    fname, increm, (uchar_t)incno);
#endif
	spcl.c_ddate = 0;
	lastincno = '0';

	inititimes();
	if (idatev == 0)
		return;
	/*
	 *	Go find the entry with the same name for a lower increment
	 *	and older date
	 */
	ITITERATE(i, ip) {
		if (strncmp(fname, ip->id_name, sizeof (ip->id_name)) != 0)
			continue;
		if (ip->id_incno >= incno)
			continue;
		if (ip->id_ddate <= spcl.c_ddate)
			continue;
		spcl.c_ddate = ip->id_ddate;
		lastincno = ip->id_incno;
	}
}

void
putitime(void)
{
	FILE		*df;
	struct	idates	*itwalk;
	int		i;
	int		fd, saverr;
	char		*fname;

	if (uflag == 0)
		return;
	if ((df = safe_fopen(increm, "r+", 0664)) == (FILE *)NULL) {
		msg("%s: %s\n", increm, strerror(errno));
		(void) unlink(increm);
		dumpabort();
		/*NOTREACHED*/
	}
	fd = fileno(df);
	(void) flock(fd, LOCK_EX);

	/*
	 * if an alternate name was specified via the N flag, use it instead
	 * of the disk name.
	 */
	if (dname != NULL)
		fname = dname;
	else
		fname = disk;

	if (idatev != 0) {
		for (i = 0; i < nidates && idatev[i] != 0; i++)
			free((char *)idatev[i]);
		free((char *)idatev);
	}
	idatev = 0;
	nidates = 0;
	readitimes(df);
	if (fseek(df, 0L, 0) < 0) {   /* rewind() was redefined in dumptape.c */
		saverr = errno;
		msg(gettext("%s: %s error:\n"),
		    increm, "fseek", strerror(saverr));
		dumpabort();
		/*NOTREACHED*/
	}
	spcl.c_ddate = 0;
	/* LINTED: won't dereference idatev if it is NULL (see readitimes) */
	ITITERATE(i, itwalk) {
		if (strncmp(fname, itwalk->id_name,
		    sizeof (itwalk->id_name)) != 0)
			continue;
		if (itwalk->id_incno != incno)
			continue;
		goto found;
	}
	/*
	 *	Add one more entry to idatev
	 */
	nidates++;
	idatev = (struct idates **)xrealloc((void *)idatev,
	    nidates * (size_t)sizeof (struct idates *));
	itwalk = idatev[nidates - 1] =
	    (struct idates *)xcalloc(1, sizeof (*itwalk));
found:
	(void) strncpy(itwalk->id_name, fname, sizeof (itwalk->id_name));
	itwalk->id_name[sizeof (itwalk->id_name) - 1] = '\0';
	itwalk->id_incno = incno;
	itwalk->id_ddate = spcl.c_date;

	ITITERATE(i, itwalk) {
		recout(df, itwalk);
	}
	if (ftruncate64(fd, ftello64(df))) {
		saverr = errno;
		msg(gettext("%s: %s error:\n"),
		    increm, "ftruncate64", strerror(saverr));
		dumpabort();
		/*NOTREACHED*/
	}
	(void) fclose(df);
	msg(gettext("Level %c dump on %s\n"),
	    (uchar_t)incno, prdate(spcl.c_date));
}

static void
recout(FILE *file, struct idates *what)
{
	time_t ddate = what->id_ddate;
	/* must use ctime, so we can later use unctime() */
	(void) fprintf(file, DUMPOUTFMT, what->id_name,
	    (uchar_t)what->id_incno, ctime(&ddate));
}

static int
getrecord(FILE *df, struct idates *idatep)
{
	char		buf[BUFSIZ];

	if ((fgets(buf, BUFSIZ, df)) != buf)
		return (-1);
	recno++;
	if (makeidate(idatep, buf) < 0) {
		msg(gettext(
		    "Malformed entry in dump record file `%s', line %d\n"),
		    increm, recno);
		if (strcmp(increm, NINCREM)) {
			msg(gettext("`%s' not a dump record file\n"), increm);
			dumpabort();
			/*NOTREACHED*/
		}
		return (-1);
	}

#ifdef FDEBUG
	msg("getrecord: %s %c %s\n",
	    idatep->id_name,
	    (uchar_t)idatep->id_incno,
	    prdate(idatep->id_ddate));
#endif
	return (0);
}

static int
makeidate(struct idates	*ip, char *buf)
{
	char	un_buf[128];	/* size must be >= second one in DUMPINFMT */

	/*
	 * MAXNAMLEN has different values in dirent.h and ufs_fsdir.h,
	 * and we need to ensure that the length in DUMPINFMT matches
	 * what we allow for.  Can't just use MAXNAMLEN in the test,
	 * because there's no convenient way to substitute it into
	 * DUMPINFMT.
	 * XXX There's got to be a better way.
	 */
	/*LINTED [assertion always true]*/
	assert(sizeof (ip->id_name) == (255 + 3));

	if (sscanf(buf, DUMPINFMT, ip->id_name, &ip->id_incno, un_buf) != 3)
		return (-1);
	/* LINTED casting from 64-bit to 32-bit time */
	ip->id_ddate = (time32_t)unctime(un_buf);
	if (ip->id_ddate < 0)
		return (-1);
	return (0);
}

/*
 * This is an estimation of the number of tp_bsize blocks in the file.
 * It estimates the number of blocks in files with holes by assuming
 * that all of the blocks accounted for by di_blocks are data blocks
 * (when some of the blocks are usually used for indirect pointers);
 * hence the estimate may be high.
 */
void
est(struct dinode *ip)
{
	u_offset_t s, t;

	/*
	 * ip->di_size is the size of the file in bytes.
	 * ip->di_blocks stores the number of sectors actually in the file.
	 * If there are more sectors than the size would indicate, this just
	 *	means that there are indirect blocks in the file or unused
	 *	sectors in the last file block; we can safely ignore these
	 *	(s = t below).
	 * If the file is bigger than the number of sectors would indicate,
	 *	then the file has holes in it.	In this case we must use the
	 *	block count to estimate the number of data blocks used, but
	 *	we use the actual size for estimating the number of indirect
	 *	dump blocks (t vs. s in the indirect block calculation).
	 */
	o_esize++;
	s = (unsigned)(ip->di_blocks) / (unsigned)(tp_bsize / DEV_BSIZE);
	/* LINTED: spurious complaint about sign-extending 32 to 64 bits */
	t = d_howmany(ip->di_size, (unsigned)tp_bsize);
	if (s > t)
		s = t;
	if (ip->di_size > (u_offset_t)((unsigned)(sblock->fs_bsize) * NDADDR)) {
		/* calculate the number of indirect blocks on the dump tape */
		/* LINTED: spurious complaint sign-extending 32 to 64 bits */
		s += d_howmany(t -
		    (unsigned)(NDADDR * sblock->fs_bsize / tp_bsize),
		    (unsigned)TP_NINDIR);
	}
	f_esize += s;
}

/*ARGSUSED*/
void
bmapest(uchar_t *map)
{
	o_esize++;
	/* LINTED: spurious complaint sign-extending 32 to 64 bits */
	f_esize += d_howmany(msiz * sizeof (map[0]), (unsigned)tp_bsize);
}


/*
 * Check to see if what we are trying to dump is a fs snapshot
 * If so, we can use the snapshot's create time to populate
 * the dumpdates file, instead of the time of the dump.
 */
time32_t
is_fssnap_dump(char *disk)
{
	struct stat st;
	char *last;
	int snapnum;
	kstat_ctl_t *kslib;
	kstat_t *ksnum;
	kstat_named_t *numval;

	last = basename(disk);
	if ((strstr(disk, SNAP_NAME) == NULL) || (stat(disk, &st) == -1) ||
	    (isdigit(last[0]) == 0))
		return (0);

	snapnum = atoi(last);

	if ((kslib = kstat_open()) == NULL)
		return (0);

	ksnum = kstat_lookup(kslib, SNAP_NAME, snapnum, FSSNAP_KSTAT_NUM);
	if (ksnum == NULL) {
		(void) kstat_close(kslib);
		return (0);
	}

	if (kstat_read(kslib, ksnum, NULL) == -1) {
		(void) kstat_close(kslib);
		return (0);
	}

	numval = kstat_data_lookup(ksnum, FSSNAP_KSTAT_NUM_CREATETIME);
	if (numval == NULL) {
		(void) kstat_close(kslib);
		return (0);
	}

	(void) kstat_close(kslib);
	/* LINTED casting from long to 32-bit time */
	return (time32_t)(numval->value.l & INT_MAX);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/

/*
 * Copyright (c) 1980 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#include "dump.h"
#include <rmt.h>
#include <sys/mtio.h>
#include <limits.h>
#include <priv_utils.h>
#include "roll_log.h"
#include <unistd.h>

char		*filesystem;
char		*host;
char		*debug_chdir;
char		*dumpdev;
char		*sdumpdev;
char		*tlabel;
time_t		*telapsed;
time_t		*tstart_writing;

uchar_t		*clrmap;
uchar_t		*dirmap;
uchar_t		*filmap;
uchar_t		*nodmap;
uchar_t		*shamap;
uchar_t		*activemap;

int		archive;
int		autoload;
int		autoload_period;
int		autoload_tries;
int		archive_opened;
int		doingverify;
int		disk_dynamic;
int		dumpstate;
int		dumptoarchive;
int		fi;
int		leftover;
int		nadded;
int		offline;
int		verify;

uint_t		etapes;
ulong_t		tsize;
ino_t		ino;
off_t		pos;
pid_t		dumppid;
u_offset_t	esize;
union u_shadow	c_shadow_save[1];

int	notify = 0;		/* notify operator flag */
int	blockswritten = 0;	/* number of blocks written on current tape */
uint_t	tapeno = 0;		/* current tape number */
daddr32_t filenum = 0;		/* current file number on tape */
int	density = 0;		/* density in bytes/0.1" */
int	tenthsperirg;		/* inter-record-gap in 0.1"'s */
uint_t	ntrec = 0;		/* # tape blocks in each tape record */
uint_t	saved_ntrec = 0;	/* saved value of ntrec */
uint_t	forceflag = 0;		/* forced to change tp_bsize */
int	cartridge = 0;		/* assume non-cartridge tape */
uint_t	tracks;			/* # tracks on a cartridge tape */
int	diskette = 0;		/* assume not dumping to a diskette */
int	printsize = 0;		/* just print estimated size and exit */
int	mapfd = -1;		/* if >= 0, file descriptor for mmap */
int32_t	tp_bsize = TP_BSIZE_MIN; /* tape block record size (frag size) */
#ifdef DEBUG
int	xflag;			/* debugging switch */
#endif

char	*myname;

/*
 * This should be struct fs, but there are trailing bits on disk
 * that we also need to read in as part of it.  It's an array of
 * longs instead of char to force proper alignment.
 */
static long sblock_buf[SBSIZE/sizeof (long)];

#ifdef __STDC__
static char *mb(u_offset_t);
static void nextstate(int);
#else
static char *mb();
static void nextstate();
#endif

extern	jmp_buf checkpoint_buf;	/* context for return from checkpoint */
#define	FUDGE_FACTOR	0x2000000

int
main(int argc, char *argv[])
{
	char		*arg;
	int		bflag = 0, i, error = 0, saverr;
	double		fetapes = 0.0;
	struct	mnttab	*dt;
	char		msgbuf[3000], *msgp;
	char		kbsbuf[BUFSIZ];
	u_offset_t	esize_shift = 0;
	int32_t	new_mult = 0;
	time32_t	snapdate;

	host = NULL;

	if (myname = strrchr(argv[0], '/'))
		myname++;
	else
		myname = argv[0];

	if (strcmp("hsmdump", myname) == 0) {
		msg(gettext("hsmdump emulation is no longer supported.\n"));
		Exit(X_ABORT);
	}

	tape = DEFTAPE;
	autoload_period = 12;
	autoload_tries = 12;	/* traditional default of ~2.5 minutes */

	(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN "SYS_TEST"
#endif  /* TEXT_DOMAIN */
	(void) textdomain(TEXT_DOMAIN);

	/*
	 * If someone strips the set-uid bit, dump will still work for local
	 * tapes.  Fail when we try to access a remote tape.
	 */
	(void) __init_suid_priv(0, PRIV_NET_PRIVADDR, (char *)NULL);

	if (sysinfo(SI_HOSTNAME, spcl.c_host, sizeof (spcl.c_host)) < 0) {
		saverr = errno;
		msg(gettext("Could not get host name: %s\n"),
		    strerror(saverr));
		bzero(spcl.c_host, sizeof (spcl.c_host));
	}

	dumppid = getpid();
	tsize = 0;	/* no default size, detect EOT dynamically */

	archive_opened = 0;
	disk = NULL;
	dname = NULL;
	disk_dynamic = 0;
	increm = NINCREM;
	incno = '9';
	uflag = 0;
	arg = "u";
	tlabel = "none";
	if (argc > 1) {
		argv++;
		argc--;
		arg = *argv;
		if (*arg == '-')
			arg++;
	}
	while (*arg)
	switch (*arg++) {		/* BE CAUTIOUS OF FALLTHROUGHS */
	case 'M':
		/*
		 * This undocumented option causes each process to
		 * mkdir debug_chdir/getpid(), and chdir to it.  This is
		 * to ease the collection of profiling information and
		 * core dumps.
		 */
		if (argc > 1) {
			argv++;
			argc--;
			debug_chdir = *argv;
			msg(gettext(
			    "Each process shall try to chdir to %s/<pid>\n"),
			    debug_chdir);
			child_chdir();
		} else {
			msg(gettext("Missing move-to-dir (M) name\n"));
			dumpabort();
			/*NOTREACHED*/
		}
		break;

	case 'w':
		lastdump('w');		/* tell us only what has to be done */
		exit(0);
		break;

	case 'W':			/* what to do */
		lastdump('W');		/* tell state of what has been done */
		exit(0);		/* do nothing else */
		break;

	case 'T':
		if (argc > 1) {
			int count;
			int multiplier;
			char units;

			argv++;
			argc--;
			count = atoi(*argv);
			if (count < 1) {
				msg(gettext(
				    "Unreasonable autoload timeout period\n"));
				dumpabort();
				/*NOTREACHED*/
			}
			units = *(*argv + strlen(*argv) - 1);
			switch (units) {
			case 's':
				multiplier = 1;
				break;
			case 'h':
				multiplier = 3600;
				break;
			case '0': case '1': case '2': case '3': case '4':
			case '5': case '6': case '7': case '8': case '9':
			case 'm':
				multiplier = 60;
				break;
			default:
				msg(gettext(
				    "Unknown timeout units indicator `%c'\n"),
				    units);
				dumpabort();
				/*NOTREACHED*/
			}
			autoload_tries = 1 +
			    ((count * multiplier) / autoload_period);
		} else {
			msg(gettext("Missing autoload timeout period\n"));
			dumpabort();
			/*NOTREACHED*/
		}
		break;

	case 'f':			/* output file */
		if (argc > 1) {
			argv++;
			argc--;
			tape = *argv;
			if (*tape == '\0') {
				msg(gettext("Bad output device name\n"));
				dumpabort();
				/*NOTREACHED*/
			}
		} else {
			msg(gettext("Missing output device name\n"));
			dumpabort();
			/*NOTREACHED*/
		}
		if (strcmp(tape, "-") == 0 && verify) {
			msg(gettext(
			"Cannot verify when dumping to standard out.\n"));
			dumpabort();
			/*NOTREACHED*/
		}
		break;

	case 'd':			/* density, in bits per inch */
		if (argc > 1) {
			argv++;
			argc--;
			density = atoi(*argv) / 10;
			if (density <= 0) {
				msg(gettext(
				    "Density must be a positive integer\n"));
				dumpabort();
				/*NOTREACHED*/
			}
		} else {
			msg(gettext("Missing density\n"));
			dumpabort();
			/*NOTREACHED*/
		}
		break;

	case 's':			/* tape size, feet */
		if (argc > 1) {
			argv++;
			argc--;
			tsize = atol(*argv);
			if ((*argv[0] == '-') || (tsize == 0)) {
				msg(gettext(
			    "Tape size must be a positive integer\n"));
				dumpabort();
				/*NOTREACHED*/
			}
		} else {
			msg(gettext("Missing tape size\n"));
			dumpabort();
			/*NOTREACHED*/
		}
		break;

	case 't':			/* tracks */
		if (argc > 1) {
			argv++;
			argc--;
			tracks = atoi(*argv);
		} else {
			msg(gettext("Missing track count\n"));
			dumpabort();
			/*NOTREACHED*/
		}
		break;

	case 'b':			/* blocks per tape write */
		if (argc > 1) {
			argv++;
			argc--;
			bflag++;
			/*
			 * We save the ntrec in case we need to change
			 * tp_bsize later, we will have to recalculate
			 * it.
			 */
			saved_ntrec = ntrec = atoi(*argv);
			if (ntrec == 0 || (ntrec&1) || ntrec > (MAXNTREC*2)) {
				msg(gettext(
		    "Block size must be a positive, even integer <= %d\n"),
				    MAXNTREC*2);
				dumpabort();
				/*NOTREACHED*/
			}
			ntrec /= (tp_bsize/DEV_BSIZE);
		} else {
			msg(gettext("Missing blocking factor\n"));
			dumpabort();
			/*NOTREACHED*/
		}
		break;

	case 'c':			/* Tape is cart. not 9-track */
	case 'C':			/* 'C' to be consistent with 'D' */
		cartridge++;
		break;

	case '0':			/* dump level */
	case '1':
	case '2':
	case '3':
	case '4':
	case '5':
	case '6':
	case '7':
	case '8':
	case '9':
		incno = arg[-1];
		break;

	case 'u':			/* update /etc/dumpdates */
		uflag++;
		break;

	case 'n':			/* notify operators */
		notify++;
		break;

	case 'a':			/* create archive file */
		archive = 1;
		if (argc > 1) {
			argv++;
			argc--;
			if (**argv == '\0') {
				msg(gettext("Bad archive file name\n"));
				dumpabort();
				/*NOTREACHED*/
			}
			archivefile = strdup(*argv);
			if (archivefile == NULL) {
				saverr = errno;
				msg(gettext("Cannot allocate memory: %s\n"),
				    strerror(saverr));
				dumpabort();
				/*NOTREACHED*/
			}
		} else {
			msg(gettext("Missing archive file name\n"));
			dumpabort();
			/*NOTREACHED*/
		}
		break;

	case 'v':
		verify++;
		doingverify++;
		if (strcmp(tape, "-") == 0) {
			msg(gettext(
			"Cannot verify when dumping to standard out.\n"));
			dumpabort();
			/*NOTREACHED*/
		}
		break;

	case 'D':
		diskette++;
		break;

	case 'N':
		if (argc > 1) {
			argv++;
			argc--;
			if (**argv == '\0') {
				msg(gettext("Missing name for dumpdates "
				    "entry.\n"));
				dumpabort();
				/*NOTREACHED*/
			}
			dname = *argv;
			if (strlen(dname) > MAXNAMLEN + 2) {
				msg(gettext("Dumpdates entry name too "
				    "long.\n"));
				dumpabort();
				/*NOTREACHED*/
			}
			for (i = 0; i < strlen(dname); i++) {
				if (isspace(*(dname+i))) {
					msg(gettext("Dumpdates entry name may "
					    "not contain white space.\n"));
					dumpabort();
					/*NOTREACHED*/
				}
			}
		} else {
			msg(gettext("Missing name for dumpdates entry.\n"));
			dumpabort();
			/*NOTREACHED*/
		}
		break;
	case 'L':
		if (argc > 1) {
			argv++;
			argc--;
			if (**argv == '\0') {
				msg(gettext("Missing tape label name\n"));
				dumpabort();
				/*NOTREACHED*/
			}
			tlabel = *argv;
			if (strlen(tlabel) > (sizeof (spcl.c_label) - 1)) {
				tlabel[sizeof (spcl.c_label) - 1] = '\0';
				msg(gettext(
		    "Truncating label to maximum supported length: `%s'\n"),
				    tlabel);
			}
		} else {
			msg(gettext("Missing tape label name\n"));
			dumpabort();
			/*NOTREACHED*/
		}
		break;

	case 'l':
		autoload++;
		break;

	case 'o':
		offline++;
		break;

	case 'S':
		printsize++;
		break;

#ifdef DEBUG
	case 'z':
		xflag++;
		break;
#endif

	default:
		msg(gettext("Bad option `%c'\n"), arg[-1]);
		dumpabort();
		/*NOTREACHED*/
	}
	if (argc > 1) {
		argv++;
		argc--;
		if (**argv == '\0') {
			msg(gettext("Bad disk name\n"));
			dumpabort();
			/*NOTREACHED*/
		}
		disk = *argv;
		disk_dynamic = 0;
	}
	if (disk == NULL) {
		(void) fprintf(stderr, gettext(
	"Usage: %s [0123456789fustdWwnNDCcbavloS [argument]] filesystem\n"),
		    myname);
		Exit(X_ABORT);
	}
	if (!filenum)
		filenum = 1;

	if (signal(SIGINT, interrupt) == SIG_IGN)
		(void) signal(SIGINT, SIG_IGN);

	if (strcmp(tape, "-") == 0) {
		pipeout++;
		tape = gettext("standard output");
		dumpdev = sdumpdev = strdup(tape);
		if (dumpdev == NULL) {
			saverr = errno;
			msg(gettext("Cannot allocate memory: %s\n"),
			    strerror(saverr));
			dumpabort();
			/*NOTREACHED*/
		}
		/*CONSTANTCONDITION*/
		assert(sizeof (spcl.c_label) > 5);
		(void) strcpy(spcl.c_label, "none");
	} else if (*tape == '+') {
		nextdevice();
		(void) strcpy(spcl.c_label, tlabel);
	} else {
		/* if not already set, set diskette to default */
		if (diskette && strcmp(tape, DEFTAPE) == 0)
			tape = DISKETTE;
		nextdevice();
		(void) strcpy(spcl.c_label, tlabel);
	}
	if (cartridge && diskette) {
		error = 1;
		msg(gettext("Cannot select both cartridge and diskette\n"));
	}
	if (density && diskette) {
		error = 1;
		msg(gettext("Cannot select density of diskette\n"));
	}
	if (tracks && diskette) {
		error = 1;
		msg(gettext("Cannot select number of tracks of diskette\n"));
	}
	if (error) {
		dumpabort();
		/*NOTREACHED*/
	}

	/*
	 * Determine how to default tape size and density
	 *
	 *		density				tape size
	 * 9-track	1600 bpi (160 bytes/.1")	2300 ft.
	 * 9-track	6250 bpi (625 bytes/.1")	2300 ft.
	 *
	 * Most Sun-2's came with 4 track (20MB) cartridge tape drives,
	 * while most other machines (Sun-3's and non-Sun's) come with
	 * 9 track (45MB) cartridge tape drives.  Some Sun-2's came with
	 * 9 track drives, but there is no way for the software to detect
	 * which drive type is installed.  Sigh...  We make the gross
	 * assumption that #ifdef mc68010 will test for a Sun-2.
	 *
	 * cartridge	8000 bpi (100 bytes/.1")	425 * tracks ft.
	 */
	if (density == 0)
		density = cartridge ? 100 : 625;
	if (tracks == 0)
		tracks = 9;
	if (!bflag) {
		if (cartridge)
			ntrec = CARTRIDGETREC;
		else if (diskette)
			ntrec = NTREC;
		else if (density >= 625)
			ntrec = HIGHDENSITYTREC;
		else
			ntrec = NTREC;
		/*
		 * save ntrec in case we have to change tp_bsize later.
		 */
		saved_ntrec = (ntrec * (tp_bsize/DEV_BSIZE));
	}
	if (!diskette) {
		tsize *= 12L*10L;
		if (cartridge)
			tsize *= tracks;
	}
	rmtinit(msg, Exit);
	if (host) {
		char	*cp = strchr(host, '@');
		if (cp == (char *)0)
			cp = host;
		else
			cp++;

		if (rmthost(host, ntrec) == 0) {
			msg(gettext("Cannot connect to tape host `%s'\n"), cp);
			dumpabort();
			/*NOTREACHED*/
		}
	}
	if (signal(SIGHUP, sigAbort) == SIG_IGN)
		(void) signal(SIGHUP, SIG_IGN);
	if (signal(SIGTRAP, sigAbort) == SIG_IGN)
		(void) signal(SIGTRAP, SIG_IGN);
	if (signal(SIGFPE, sigAbort) == SIG_IGN)
		(void) signal(SIGFPE, SIG_IGN);
	if (signal(SIGBUS, sigAbort) == SIG_IGN)
		(void) signal(SIGBUS, SIG_IGN);
	if (signal(SIGSEGV, sigAbort) == SIG_IGN)
		(void) signal(SIGSEGV, SIG_IGN);
	if (signal(SIGTERM, sigAbort) == SIG_IGN)
		(void) signal(SIGTERM, SIG_IGN);
	if (signal(SIGUSR1, sigAbort) == SIG_IGN)
		(void) signal(SIGUSR1, SIG_IGN);
	if (signal(SIGPIPE, sigAbort) == SIG_IGN)
		(void) signal(SIGPIPE, SIG_IGN);

	mnttabread();		/* /etc/fstab, /etc/mtab snarfed */

	/*
	 *	disk can be either the full special file name,
	 *	the suffix of the special file name,
	 *	the special name missing the leading '/',
	 *	the file system name with or without the leading '/'.
	 *	NB:  we attempt to avoid dumping the block device
	 *	(using rawname) because specfs and the vm system
	 *	are not necessarily in sync.
	 */

	/*
	 * Attempt to roll the log if its root user before doing the dump.
	 * There's nothing the user can do if we are unable to roll the log,
	 * so we'll silently ignore failures.
	 */
	if (getuid() == 0 && rl_roll_log(disk) != RL_SUCCESS &&
	    disk[0] != '/') {
		/* Try it again with leading '/'. */
		char	*slashed;

		slashed = (char *)malloc(strlen(disk) + 2);
		if (slashed != (char *)NULL) {
			(void) sprintf(slashed, "%c%s", '/', disk);
			(void) rl_roll_log(slashed);
			free(slashed);
		}
	}
	dt = mnttabsearch(disk, 0);
	if (dt != 0) {
		filesystem = dt->mnt_mountp;
		if (disk_dynamic) {
			/* LINTED: disk is not NULL */
			free(disk);
		}
		disk = rawname(dt->mnt_special);
		disk_dynamic = (disk != dt->mnt_special);

		(void) strncpy(spcl.c_dev, dt->mnt_special,
		    sizeof (spcl.c_dev));
		spcl.c_dev[sizeof (spcl.c_dev) - 1] = '\0';
		(void) strncpy(spcl.c_filesys, dt->mnt_mountp,
		    sizeof (spcl.c_filesys));
		spcl.c_filesys[sizeof (spcl.c_filesys) - 1] = '\0';
	} else {
		(void) strncpy(spcl.c_dev, disk, sizeof (spcl.c_dev));
		spcl.c_dev[sizeof (spcl.c_dev) - 1] = '\0';
#ifdef PARTIAL
		/* check for partial filesystem dump */
		partial_check();
		dt = mnttabsearch(disk, 1);
		if (dt != 0) {
			filesystem = dt->mnt_mountp;
			if (disk_dynamic)
				free(disk);
			disk = rawname(dt->mnt_special);
			disk_dynamic = (disk != dt->mnt_special);

			(void) strncpy(spcl.c_filesys,
			    "a partial file system", sizeof (spcl.c_filesys));
			spcl.c_filesys[sizeof (spcl.c_filesys) - 1] = '\0';
		}
		else
#endif /* PARTIAL */
		{
			char *old_disk = disk;

			(void) strncpy(spcl.c_filesys,
			    "an unlisted file system",
			    sizeof (spcl.c_filesys));
			spcl.c_filesys[sizeof (spcl.c_filesys) - 1] = '\0';

			disk = rawname(old_disk);
			if (disk != old_disk) {
				if (disk_dynamic)
					free(old_disk);
				disk_dynamic = 1;
			}
			/*
			 * If disk == old_disk, then disk_dynamic's state
			 * does not change.
			 */
		}
	}

	fi = open64(disk, O_RDONLY);

	if (fi < 0) {
		saverr = errno;
		msg(gettext("Cannot open dump device `%s': %s\n"),
		    disk, strerror(saverr));
		Exit(X_ABORT);
	}

	if (sscanf(&incno, "%1d", &spcl.c_level) != 1) {
		msg(gettext("Bad dump level `%c' specified\n"), incno);
		dumpabort();
		/*NOTREACHED*/
	}
	getitime();		/* /etc/dumpdates snarfed */

	sblock = (struct fs *)&sblock_buf;
	sync();

	bread((diskaddr_t)SBLOCK, (uchar_t *)sblock, (long)SBSIZE);
	if ((sblock->fs_magic != FS_MAGIC) &&
	    (sblock->fs_magic != MTB_UFS_MAGIC)) {
		msg(gettext(
	    "Warning - super-block on device `%s' is corrupt - run fsck\n"),
		    disk);
		dumpabort();
		/*NOTREACHED*/
	}

	if (sblock->fs_magic == FS_MAGIC &&
	    (sblock->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
	    sblock->fs_version != UFS_VERSION_MIN)) {
		msg(gettext("Unrecognized UFS version: %d\n"),
		    sblock->fs_version);
		dumpabort();
		/*NOTREACHED*/
	}

	if (sblock->fs_magic == MTB_UFS_MAGIC &&
	    (sblock->fs_version < MTB_UFS_VERSION_MIN ||
	    sblock->fs_version > MTB_UFS_VERSION_1)) {
		msg(gettext("Unrecognized UFS version: %d\n"),
		    sblock->fs_version);
		dumpabort();
		/*NOTREACHED*/
	}

	/*
	 * Try to set up for using mmap(2).  It only works on the block
	 * device, but if we can use it, things go somewhat faster.  If
	 * we can't open it, we'll silently fall back to the old method
	 * (read/memcpy). We also only try this if it's been cleanly
	 * unmounted. Dumping a live filesystem this way runs into
	 * buffer consistency problems. Of course, we don't support
	 * running dump on a mounted filesystem, but some people do it
	 * anyway.
	 */
	if (sblock->fs_clean == FSCLEAN) {
		char *block = unrawname(disk);

		if (block != NULL) {
			mapfd = open(block, O_RDONLY, 0);
			free(block);
		}
	}

restart:
	bread((diskaddr_t)SBLOCK, (uchar_t *)sblock, (long)SBSIZE);
	if ((sblock->fs_magic != FS_MAGIC) &&
	    (sblock->fs_magic != MTB_UFS_MAGIC)) {	/* paranoia */
		msg(gettext("bad super-block magic number, run fsck\n"));
		dumpabort();
		/*NOTREACHED*/
	}

	if (sblock->fs_magic == FS_MAGIC &&
	    (sblock->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
	    sblock->fs_version != UFS_VERSION_MIN)) {
		msg(gettext("Unrecognized UFS version: %d\n"),
		    sblock->fs_version);
		dumpabort();
		/*NOTREACHED*/
	}

	if (sblock->fs_magic == MTB_UFS_MAGIC &&
	    (sblock->fs_version < MTB_UFS_VERSION_MIN ||
	    sblock->fs_version > MTB_UFS_VERSION_1)) {
		msg(gettext("Unrecognized UFS version: %d\n"),
		    sblock->fs_version);
		dumpabort();
		/*NOTREACHED*/
	}

	if (!doingactive)
		allocino();

	/* XXX should sanity-check the super block before trusting/using it */

	/* LINTED XXX time truncated - tolerate until tape format changes */
	spcl.c_date = (time32_t)time((time_t *)NULL);
	bcopy(&(spcl.c_shadow), c_shadow_save, sizeof (c_shadow_save));

	snapdate = is_fssnap_dump(disk);
	if (snapdate)
		spcl.c_date = snapdate;

	if (!printsize) {
		msg(gettext("Date of this level %c dump: %s\n"),
		    incno, prdate(spcl.c_date));
		msg(gettext("Date of last level %c dump: %s\n"),
		    (uchar_t)lastincno, prdate(spcl.c_ddate));
		msg(gettext("Dumping %s "), disk);
		if (filesystem != 0)
			msgtail("(%.*s:%s) ",
			    /* LINTED unsigned -> signed cast ok */
			    (int)sizeof (spcl.c_host), spcl.c_host, filesystem);
		msgtail(gettext("to %s.\n"), sdumpdev);
	}

	esize = f_esize = o_esize = 0;
	msiz = roundup(d_howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY),
	    TP_BSIZE_MAX);
	if (!doingactive) {
		clrmap = (uchar_t *)xcalloc(msiz, sizeof (*clrmap));
		filmap = (uchar_t *)xcalloc(msiz, sizeof (*filmap));
		dirmap = (uchar_t *)xcalloc(msiz, sizeof (*dirmap));
		nodmap = (uchar_t *)xcalloc(msiz, sizeof (*nodmap));
		shamap = (uchar_t *)xcalloc(msiz, sizeof (*shamap));
		activemap = (uchar_t *)xcalloc(msiz, sizeof (*activemap));
	} else {
		if (clrmap == NULL || filmap == NULL || dirmap == NULL ||
		    nodmap == NULL || shamap == NULL || activemap == NULL) {
			msg(gettext(
	    "Internal error: NULL map pointer while re-dumping active files"));
			dumpabort();
			/*NOTREACHED*/
		}
		bzero(clrmap, msiz);
		bzero(filmap, msiz);
		bzero(dirmap, msiz);
		bzero(nodmap, msiz);
		bzero(shamap, msiz);
		/* retain active map */
	}

	dumpstate = DS_INIT;
	dumptoarchive = 1;

	/*
	 * Read cylinder group inode-used bitmaps to avoid reading clear inodes.
	 */
	{
		uchar_t *clrp = clrmap;
		struct cg *cgp =
		    (struct cg *)xcalloc((uint_t)sblock->fs_cgsize, 1);

		for (i = 0; i < sblock->fs_ncg; i++) {
			bread(fsbtodb(sblock, cgtod(sblock, i)),
			    (uchar_t *)cgp, sblock->fs_cgsize);
			bcopy(cg_inosused(cgp), clrp,
			    (int)sblock->fs_ipg / NBBY);
			clrp += sblock->fs_ipg / NBBY;
		}
		free((char *)cgp);
		/* XXX right-shift clrmap one bit.  why? */
		for (i = 0; clrp > clrmap; i <<= NBBY) {
			i |= *--clrp & ((1<<NBBY) - 1);
			*clrp = i >> 1;
		}
	}

	if (!printsize) {
		msgp = gettext("Mapping (Pass I) [regular files]\n");
		msg(msgp);
	}

	ino = 0;
#ifdef PARTIAL
	if (partial_mark(argc, argv)) {
#endif /* PARTIAL */
		if (!doingactive)
			pass(mark, clrmap);	/* mark updates 'x'_esize */
		else
			pass(active_mark, clrmap);	/* updates 'x'_esize */
#ifdef PARTIAL
	}
#endif /* PARTIAL */
	do {
		if (!printsize) {
			msgp = gettext("Mapping (Pass II) [directories]\n");
			msg(msgp);
		}
		nadded = 0;
		ino = 0;
		pass(add, dirmap);
	} while (nadded);

	ino = 0; /* adjust estimated size for shadow inodes */
	pass(markshad, nodmap);
	ino = 0;
	pass(estshad, shamap);
	freeshad();

	bmapest(clrmap);
	bmapest(nodmap);
	esize = o_esize + f_esize;
	if (diskette) {
		/* estimate number of floppies */
		if (tsize != 0)
			fetapes = (double)(esize + ntrec) / (double)tsize;
	} else if (cartridge) {
		/*
		 * Estimate number of tapes, assuming streaming stops at
		 * the end of each block written, and not in mid-block.
		 * Assume no erroneous blocks; this can be compensated for
		 * with an artificially low tape size.
		 */
		tenthsperirg = 16;	/* actually 15.48, says Archive */
		if (tsize != 0)
			fetapes = ((double)esize /* blocks */
			    * (tp_bsize		/* bytes/block */
			    * (1.0/density))	/* 0.1" / byte */
			    +
			    (double)esize	/* blocks */
			    * (1.0/ntrec)	/* streaming-stops per block */
			    * tenthsperirg)	/* 0.1" / streaming-stop */
			    * (1.0 / tsize);	/* tape / 0.1" */
	} else {
		/* Estimate number of tapes, for old fashioned 9-track tape */
#ifdef sun
		/* sun has long irg's */
		tenthsperirg = (density == 625) ? 6 : 12;
#else
		tenthsperirg = (density == 625) ? 5 : 8;
#endif
		if (tsize != 0)
			fetapes = ((double)esize /* blocks */
			    * (tp_bsize		/* bytes / block */
			    * (1.0/density))	/* 0.1" / byte */
			    +
			    (double)esize	/* blocks */
			    * (1.0/ntrec)	/* IRG's / block */
			    * tenthsperirg)	/* 0.1" / IRG */
			    * (1.0 / tsize);	/* tape / 0.1" */
	}

	etapes = fetapes;	/* truncating assignment */
	etapes++;
	/* count the nodemap on each additional tape */
	for (i = 1; i < etapes; i++)
		bmapest(nodmap);
	/*
	 * If the above bmapest is called, it changes o_esize and f_esize.
	 * So we will recalculate esize here anyway to make sure.
	 * Also, add tape headers and trailer records.
	 */
	esize = o_esize + f_esize + etapes + ntrec;

	/*
	 * If the estimated number of tp_bsize tape blocks is greater than
	 * INT_MAX we have to adjust tp_bsize and ntrec to handle
	 * the larger dump.  esize is an estimate, so we 'fudge'
	 * INT_MAX a little.  If tp_bsize is adjusted, it will be adjusted
	 * to the size needed for this dump (2048, 4096, 8192, ...)
	 */
	if (esize > (INT_MAX - FUDGE_FACTOR)) { /* esize is too big */
		forceflag++;
		esize_shift =
		    ((esize + (INT_MAX - FUDGE_FACTOR) - 1)/
		    ((u_offset_t)(INT_MAX - FUDGE_FACTOR))) - 1;
		if ((esize_shift > ESIZE_SHIFT_MAX) || (ntrec == 0)) {
			msgp = gettext(
	"Block factor %d ('b' flag) is too small for this size dump.");
			msg(msgp, saved_ntrec);
			dumpabort();
			/*NOTREACHED*/
		}
		/*
		 * recalculate esize from:
		 * o_esize - header tape records
		 * (f_esize + (num_mult -1)) >> esize_shift - new non-header
		 *	tape records for files/maps
		 * etapes - TS_TAPE records
		 * ntrec - TS_END records
		 *
		 * ntrec is adjusted so a tape record is still 'b' flag
		 * number of DEV_BSIZE (512) in size
		 */
		new_mult = (tp_bsize << esize_shift)/tp_bsize;
		tp_bsize = (tp_bsize << esize_shift);
		esize = o_esize + ((f_esize +
		    (new_mult - 1)) >> esize_shift) + etapes + ntrec;
		ntrec = (saved_ntrec/(tp_bsize/DEV_BSIZE));
	}
	if (forceflag != 0) {
		msgp = gettext(
		    "Forcing larger tape block size (%d).\n");
		msg(msgp, tp_bsize);
	}
	alloctape();			/* allocate tape buffers */

	assert((tp_bsize / DEV_BSIZE != 0) && (tp_bsize % DEV_BSIZE == 0));
	/*
	 * If all we wanted was the size estimate,
	 * just print it out and exit.
	 */
	if (printsize) {
		(void) printf("%llu\n", esize * tp_bsize);
		Exit(0);
	}

	if (tsize != 0) {
		if (diskette)
			msgp = gettext(
			    "Estimated %lld blocks (%s) on %3.2f diskettes.\n");
		else
			msgp = gettext(
			    "Estimated %lld blocks (%s) on %3.2f tapes.\n");

		msg(msgp,
		    (esize*(tp_bsize/DEV_BSIZE)), mb(esize), fetapes);
	} else {
		msgp = gettext("Estimated %lld blocks (%s).\n");
		msg(msgp, (esize*(tp_bsize/DEV_BSIZE)), mb(esize));
	}

	dumpstate = DS_CLRI;

	otape(1);			/* bitmap is the first to tape write */
	*telapsed = 0;
	(void) time(tstart_writing);

	/* filmap indicates all non-directory inodes */
	{
		uchar_t *np, *fp, *dp;
		np = nodmap;
		dp = dirmap;
		fp = filmap;
		for (i = 0; i < msiz; i++)
			*fp++ = *np++ ^ *dp++;
	}

	while (dumpstate != DS_DONE) {
		/*
		 * When we receive EOT notification from
		 * the writer, the signal handler calls
		 * rollforward and then jumps here.
		 */
		(void) setjmp(checkpoint_buf);
		switch (dumpstate) {
		case DS_INIT:
			/*
			 * We get here if a tape error occurred
			 * after releasing the name lock but before
			 * the volume containing the last of the
			 * dir info was completed.  We have to start
			 * all over in this case.
			 */
			{
				char *rmsg = gettext(
		"Warning - output error occurred after releasing name lock\n\
\tThe dump will restart\n");
				msg(rmsg);
				goto restart;
			}
			/* NOTREACHED */
		case DS_START:
		case DS_CLRI:
			ino = UFSROOTINO;
			dumptoarchive = 1;
			bitmap(clrmap, TS_CLRI);
			nextstate(DS_BITS);
			/* FALLTHROUGH */
		case DS_BITS:
			ino = UFSROOTINO;
			dumptoarchive = 1;
			if (BIT(UFSROOTINO, nodmap))	/* empty dump check */
				bitmap(nodmap, TS_BITS);
			nextstate(DS_DIRS);
			if (!doingverify) {
				msgp = gettext(
				    "Dumping (Pass III) [directories]\n");
				msg(msgp);
			}
			/* FALLTHROUGH */
		case DS_DIRS:
			dumptoarchive = 1;
			pass(dirdump, dirmap);
			nextstate(DS_FILES);
			if (!doingverify) {
				msgp = gettext(
				    "Dumping (Pass IV) [regular files]\n");
				msg(msgp);
			}
			/* FALLTHROUGH */
		case DS_FILES:
			dumptoarchive = 0;

			pass(lf_dump, filmap);

			flushcmds();
			dumpstate = DS_END;	/* don't reset ino */
			/* FALLTHROUGH */
		case DS_END:
			dumptoarchive = 1;
			spcl.c_type = TS_END;
			for (i = 0; i < ntrec; i++) {
				spclrec();
			}
			flusht();
			break;
		case DS_DONE:
			break;
		default:
			msg(gettext("Internal state error\n"));
			dumpabort();
			/*NOTREACHED*/
		}
	}

	if ((! doingactive) && (! active))
		trewind();
	if (verify && !doingverify) {
		msgp = gettext("Finished writing last dump volume\n");
		msg(msgp);
		Exit(X_VERIFY);
	}
	if (spcl.c_volume > 1)
		(void) snprintf(msgbuf, sizeof (msgbuf),
		    gettext("%lld blocks (%s) on %ld volumes"),
		    ((uint64_t)spcl.c_tapea*(tp_bsize/DEV_BSIZE)),
		    mb((u_offset_t)(unsigned)(spcl.c_tapea)),
		    spcl.c_volume);
	else
		(void) snprintf(msgbuf, sizeof (msgbuf),
		    gettext("%lld blocks (%s) on 1 volume"),
		    ((uint64_t)spcl.c_tapea*(tp_bsize/DEV_BSIZE)),
		    mb((u_offset_t)(unsigned)(spcl.c_tapea)));
	if (timeclock((time_t)0) != (time_t)0) {
		(void) snprintf(kbsbuf, sizeof (kbsbuf),
		    gettext(" at %ld KB/sec"),
		    (long)(((float)spcl.c_tapea / (float)timeclock((time_t)0))
		    * 1000.0));
		(void) strcat(msgbuf, kbsbuf);
	}
	(void) strcat(msgbuf, "\n");
	msg(msgbuf);
	(void) timeclock((time_t)-1);

	if (archive)
		msg(gettext("Archiving dump to `%s'\n"), archivefile);
	if (active && !verify) {
		nextstate(DS_INIT);
		activepass();
		goto restart;
	}
	msgp = gettext("DUMP IS DONE\n");
	msg(msgp);
	broadcast(msgp);
	if (! doingactive)
		putitime();
	Exit(X_FINOK);

	/*NOTREACHED*/
	return (0);
}

void
sigAbort(int sig)
{
	char	*sigtype;

	switch (sig) {
	case SIGHUP:
		sigtype = "SIGHUP";
		break;
	case SIGTRAP:
		sigtype = "SIGTRAP";
		break;
	case SIGFPE:
		sigtype = "SIGFPE";
		break;
	case SIGBUS:
		msg(gettext("%s  ABORTING!\n"), "SIGBUS()");
		(void) signal(SIGUSR2, SIG_DFL);
		abort();
		/*NOTREACHED*/
	case SIGSEGV:
		msg(gettext("%s  ABORTING!\n"), "SIGSEGV()");
		(void) signal(SIGUSR2, SIG_DFL);
		abort();
		/*NOTREACHED*/
	case SIGALRM:
		sigtype = "SIGALRM";
		break;
	case SIGTERM:
		sigtype = "SIGTERM";
		break;
	case SIGPIPE:
		msg(gettext("Broken pipe\n"));
		dumpabort();
		/*NOTREACHED*/
	default:
		sigtype = "SIGNAL";
		break;
	}
	msg(gettext("%s()  try rewriting\n"), sigtype);
	if (pipeout) {
		msg(gettext("Unknown signal, Cannot recover\n"));
		dumpabort();
		/*NOTREACHED*/
	}
	msg(gettext("Rewriting attempted as response to unknown signal.\n"));
	(void) fflush(stderr);
	(void) fflush(stdout);
	close_rewind();
	Exit(X_REWRITE);
}

/* Note that returned value is malloc'd if != cp && != NULL */
char *
rawname(char *cp)
{
	struct stat64 st;
	char *dp;
	extern char *getfullrawname();

	if (stat64(cp, &st) < 0 || (st.st_mode & S_IFMT) != S_IFBLK)
		return (cp);

	dp = getfullrawname(cp);
	if (dp == 0)
		return (0);
	if (*dp == '\0') {
		free(dp);
		return (0);
	}

	if (stat64(dp, &st) < 0 || (st.st_mode & S_IFMT) != S_IFCHR) {
		free(dp);
		return (cp);
	}

	return (dp);
}

static char *
mb(u_offset_t blks)
{
	static char buf[16];

	if (blks < 1024)
		(void) snprintf(buf, sizeof (buf), "%lldKB", blks);
	else
		(void) snprintf(buf, sizeof (buf), "%.2fMB",
		    ((double)(blks*tp_bsize)) / (double)(1024*1024));
	return (buf);
}

#ifdef signal
void (*nsignal(int sig, void (*act)(int)))(int)
{
	struct sigaction sa, osa;

	sa.sa_handler = act;
	(void) sigemptyset(&sa.sa_mask);
	sa.sa_flags = SA_RESTART;
	if (sigaction(sig, &sa, &osa) < 0)
		return ((void (*)(int))-1);
	return (osa.sa_handler);
}
#endif

static void
nextstate(int state)
{
	/* LINTED assigned value never used - kept for documentary purposes */
	dumpstate = state;
	/* LINTED assigned value never used - kept for documentary purposes */
	ino = 0;
	/* LINTED assigned value never used - kept for documentary purposes */
	pos = 0;
	leftover = 0;
}

/*
 * timeclock() function, for keeping track of how much time we've spent
 * writing to the tape device.  it always returns the amount of time
 * already spent, in milliseconds.  if you pass it a positive, then that's
 * telling it that we're writing, so the time counts.  if you pass it a
 * zero, then that's telling it we're not writing; perhaps we're waiting
 * for user input.
 *
 * a state of -1 resets everything.
 */
time32_t
timeclock(time32_t state)
{
	static int *currentState = NULL;
	static struct timeval *clockstart;
	static time32_t *emilli;

	struct timeval current[1];
	int fd, saverr;

#ifdef DEBUG
	fprintf(stderr, "pid=%d timeclock ", getpid());
	if (state == (time32_t)-1)
		fprintf(stderr, "cleared\n");
	else if (state > 0)
		fprintf(stderr, "ticking\n");
	else
		fprintf(stderr, "paused\n");
#endif /* DEBUG */

	/* if we haven't setup the shared memory, init */
	if (currentState == (int *)NULL) {
		if ((fd = open("/dev/zero", O_RDWR)) < 0) {
			saverr = errno;
			msg(gettext("Cannot open `%s': %s\n"),
			    "/dev/zero", strerror(saverr));
			dumpabort();
			/*NOTREACHED*/
		}
		/*LINTED [mmap always returns an aligned value]*/
		currentState = (int *)mmap((char *)0, getpagesize(),
		    PROT_READ|PROT_WRITE, MAP_SHARED, fd, (off_t)0);
		if (currentState == (int *)-1) {
			saverr = errno;
			msg(gettext(
			    "Cannot memory map monitor variables: %s\n"),
			    strerror(saverr));
			dumpabort();
			/*NOTREACHED*/
		}
		(void) close(fd);

		/* LINTED currentState is sufficiently aligned */
		clockstart = (struct timeval *)(currentState + 1);
		emilli = (time32_t *)(clockstart + 1);
		/* Note everything is initialized to zero via /dev/zero */
	}

	if (state == (time32_t)-1) {
		bzero(clockstart, sizeof (*clockstart));
		*currentState = 0;
		*emilli = (time32_t)0;
		return (0);
	}

	(void) gettimeofday(current, NULL);

	if (*currentState != 0) {
		current->tv_usec += 1000000;
		current->tv_sec--;

		/* LINTED: result will fit in a time32_t */
		*emilli += (current->tv_sec - clockstart->tv_sec) * 1000;
		/* LINTED: result will fit in a time32_t */
		*emilli += (current->tv_usec - clockstart->tv_usec) / 1000;
	}

	if (state != 0)
		bcopy(current, clockstart, sizeof (current));

	*currentState = state;

	return (*emilli);
}

static int
statcmp(const struct stat64 *left, const struct stat64 *right)
{
	int result = 1;

	if ((left->st_dev == right->st_dev) &&
	    (left->st_ino == right->st_ino) &&
	    (left->st_mode == right->st_mode) &&
	    (left->st_nlink == right->st_nlink) &&
	    (left->st_uid == right->st_uid) &&
	    (left->st_gid == right->st_gid) &&
	    (left->st_rdev == right->st_rdev) &&
	    (left->st_ctim.tv_sec == right->st_ctim.tv_sec) &&
	    (left->st_ctim.tv_nsec == right->st_ctim.tv_nsec) &&
	    (left->st_mtim.tv_sec == right->st_mtim.tv_sec) &&
	    (left->st_mtim.tv_nsec == right->st_mtim.tv_nsec)) {
		/*
		 * Unlike in the ufsrestore version
		 * st_blocks and st_blksiz are not
		 * compared. The reason for this is
		 * problems with zfs dump files. Zfs
		 * changes it's statistics in those
		 * fields.
		 */
		result = 0;
	}

	return (result);
}

/*
 * Safely open a file or device.
 */
static int
safe_open_common(const char *filename, int mode, int perms, int device)
{
	int fd;
	int working_mode;
	int saverr;
	char *errtext;
	struct stat64 pre_stat, pre_lstat;
	struct stat64 post_stat, post_lstat;

	/*
	 * Don't want to be spoofed into trashing something we
	 * shouldn't, thus the following rigamarole.  If it doesn't
	 * exist, we create it and proceed.  Otherwise, require that
	 * what's there be a real file with no extraneous links and
	 * owned by whoever ran us.
	 *
	 * The silliness with using both lstat() and fstat() is to avoid
	 * race-condition games with someone replacing the file with a
	 * symlink after we've opened it.  If there was an flstat(),
	 * we wouldn't need the fstat().
	 *
	 * The initial open with the hard-coded flags is ok even if we
	 * are intending to open only for reading.  If it succeeds,
	 * then the file did not exist, and we'll synthesize an appropriate
	 * complaint below.  Otherwise, it does exist, so we won't be
	 * truncating it with the open.
	 */
	if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_LARGEFILE,
	    perms)) < 0) {
		if (errno == EEXIST) {
			if (lstat64(filename, &pre_lstat) < 0) {
				return (-1);
			}

			if (stat64(filename, &pre_stat) < 0) {
				return (-1);
			}

			working_mode = mode & (O_WRONLY|O_RDWR|O_RDONLY);
			working_mode |= O_LARGEFILE;
			if ((fd = open(filename, working_mode)) < 0) {
				if (errno == ENOENT) {
					errtext = gettext(
"Unexpected condition detected: %s used to exist, but doesn't any longer\n");
					msg(errtext, filename);
					syslog(LOG_WARNING, errtext, filename);
					errno = ENOENT;
				}
				return (-1);
			}

			if (lstat64(filename, &post_lstat) < 0) {
				saverr = errno;
				(void) close(fd);
				errno = saverr;
				return (-1);
			}

			if (fstat64(fd, &post_stat) < 0) {
				saverr = errno;
				(void) close(fd);
				errno = saverr;
				return (-1);
			}

			/*
			 * Can't just use memcmp(3C), because the access
			 * time is updated by open(2).
			 */
			if (statcmp(&pre_lstat, &post_lstat) != 0) {
				errtext = gettext("Unexpected change detected: "
				    "%s's lstat(2) information changed\n");
				msg(errtext, filename);
				syslog(LOG_WARNING, errtext, filename);
				errno = EPERM;
				return (-1);
			}

			if (statcmp(&pre_stat, &post_stat) != 0) {
				errtext = gettext("Unexpected change detected: "
				    "%s's stat(2) information changed\n"),
				    msg(errtext, filename);
				syslog(LOG_WARNING, errtext, filename);
				errno = EPERM;
				return (-1);
			}

			/*
			 * If inode, device, or type are wrong, bail out.
			 * Note using post_stat instead of post_lstat for the
			 * S_ISCHR() test.  This is to allow the /dev ->
			 * /devices bit to work, as long as the final target
			 * is a character device (i.e., raw disk or tape).
			 */
			if (device && !(S_ISCHR(post_stat.st_mode)) &&
			    !(S_ISFIFO(post_stat.st_mode)) &&
			    !(S_ISREG(post_lstat.st_mode))) {
				errtext = gettext("Unexpected condition "
				    "detected: %s is not a supported device\n"),
				    msg(errtext, filename);
				syslog(LOG_WARNING, errtext, filename);
				(void) close(fd);
				errno = EPERM;
				return (-1);
			} else if (!device &&
			    (!S_ISREG(post_lstat.st_mode) ||
			    (post_stat.st_ino != post_lstat.st_ino) ||
			    (post_stat.st_dev != post_lstat.st_dev))) {
				errtext = gettext("Unexpected condition "
				    "detected: %s is not a regular file\n"),
				    msg(errtext, filename);
				syslog(LOG_WARNING, errtext, filename);
				(void) close(fd);
				errno = EPERM;
				return (-1);
			}

			/*
			 * Bad link count implies someone's linked our
			 * target to something else, which we probably
			 * shouldn't step on.
			 */
			if (post_lstat.st_nlink != 1) {
				errtext = gettext("Unexpected condition "
				    "detected: %s must have exactly one "
				    "link\n"), msg(errtext, filename);
				syslog(LOG_WARNING, errtext, filename);
				(void) close(fd);
				errno = EPERM;
				return (-1);
			}
			/*
			 * Root might make a file, but non-root might
			 * need to open it.  If the permissions let us
			 * get this far, then let it through.
			 */
			if (post_lstat.st_uid != getuid() &&
			    post_lstat.st_uid != 0) {
				errtext = gettext("Unsupported "
				    "condition detected: %s "
				    "must be owned by uid %ld or 0\n"),
				    msg(errtext, filename, (long)getuid());
				syslog(LOG_WARNING, errtext, filename,
				    (long)getuid());
				(void) close(fd);
				errno = EPERM;
				return (-1);
			}
			if (mode & O_TRUNC) {
				if (ftruncate(fd, (off_t)0) < 0) {
					msg("ftruncate(%s): %s\n",
					    filename, strerror(errno));
					(void) close(fd);
					return (-1);
				}
			}
		} else {
			/*
			 * Didn't exist, but couldn't open it.
			 */
			return (-1);
		}
	} else {
		/*
		 * If truncating open succeeded for a read-only open,
		 * bail out, as we really shouldn't have succeeded.
		 */
		if (mode & O_RDONLY) {
			/* Undo the O_CREAT */
			(void) unlink(filename);
			msg("open(%s): %s\n",
			    filename, strerror(ENOENT));
			(void) close(fd);
			errno = ENOENT;
			return (-1);
		}
	}

	return (fd);
}

/*
 * Safely open a file.
 */
int
safe_file_open(const char *filename, int mode, int perms)
{
	return (safe_open_common(filename, mode, perms, 0));
}

/*
 * Safely open a device.
 */
int
safe_device_open(const char *filename, int mode, int perms)
{
	return (safe_open_common(filename, mode, perms, 1));
}

/*
 * STDIO version of safe_open
 */
FILE *
safe_fopen(const char *filename, const char *smode, int perms)
{
	int fd;
	int bmode;

	/*
	 * accepts only modes  "r", "r+", and "w"
	 */
	if (smode[0] == 'r') {
		if (smode[1] == '\0') {
			bmode = O_RDONLY;
		} else if ((smode[1] == '+') && (smode[2] == '\0')) {
			bmode = O_RDWR;
		}
	} else if ((smode[0] == 'w') && (smode[1] == '\0')) {
		bmode = O_WRONLY;
	} else {
		msg(gettext("internal error: safe_fopen: invalid mode `%s'\n"),
		    smode);
		return (NULL);
	}

	fd = safe_file_open(filename, bmode, perms);

	/*
	 * caller is expected to report error.
	 */
	if (fd >= 0)
		return (fdopen(fd, smode));

	return ((FILE *)NULL);
}

void
child_chdir(void)
{
	char name[MAXPATHLEN];

	if (debug_chdir != NULL) {
		snprintf(name, sizeof (name), "%s/%ld",
		    debug_chdir, (long)getpid());
		if (mkdir(name, 0755) < 0)
			msg("mkdir(%s): %s", name, strerror(errno));
		if (chdir(name) < 0)
			msg("chdir(%s): %s", name, strerror(errno));
	}
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1991,1996,1998 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#include "dump.h"
#include <math.h>
#include <limits.h>

/*
 * Uncomment if using mmap'ing of files for pre-fetch.
 * #define ENABLE_MMAP 1
 */

struct inodesc {
	ino_t	id_inumber;		/* inode number */
	long	id_gen;			/* generation number */
	struct inodesc *id_next;	/* next on linked list */
};

char	*archivefile;
char	*tape;

int	active;
int	doingactive;
int	doposition;
int	pipeout;
int	tapeout;
int	to;

struct fs	*sblock;
union u_spcl	u_spcl;

static struct inodesc	ilist;		/* list of used inodesc structs */
static struct inodesc	*last;		/* last inodesc init'd or matched */
static struct inodesc	*freeinodesc;	/* free list of inodesc structs */
static struct inodesc	**ialloc;	/* allocated chunks, for freeing */
static int		nchunks;	/* number of allocations */

#ifdef ENABLE_MMAP /* XXX part of mmap support */
/*
 * If an mmap'ed file is truncated as it is being dumped or
 * faulted in, we are delivered a SIGBUS.
 */
static jmp_buf	truncate_buf;
static void	(*savebus)();
static int	incopy;

static void onsigbus(int);

#endif	/* ENABLE_MMAP */

#ifdef DEBUG
extern int xflag;
#endif

#ifdef ENABLE_MMAP /* XXX part of mmap support */
static void
onsigbus(int sig)
{
	if (!incopy) {
		dumpabort();
		/*NOTREACHED*/
	}
	incopy = 0;
	longjmp(truncate_buf, 1);
	/*NOTREACHED*/
}
#endif	/* ENABLE_MMAP */

void
allocino(void)
{
	ino_t maxino;
	size_t nused;

	maxino = (unsigned)(sblock->fs_ipg * sblock->fs_ncg);
	if (maxino > ULONG_MAX) {
		msg(gettext("allocino: filesystem too large\n"));
		dumpabort();
		/*NOTREACHED*/
	}
	/* LINTED maxino guaranteed to fit into a size_t by above test */
	nused =  maxino - sblock->fs_cstotal.cs_nifree;
	freeinodesc = (struct inodesc *)xcalloc(nused, sizeof (*freeinodesc));
	if (freeinodesc == (struct inodesc *)0) {
		msg(gettext("%s: out of memory\n"), "allocino");
		dumpabort();
		/*NOTREACHED*/
	}
	last = &ilist;
	ialloc =
	    (struct inodesc **)xmalloc(2*sizeof (*ialloc));
	ialloc[0] = freeinodesc;
	ialloc[1] = (struct inodesc *)0;
	nchunks = 1;
}

void
freeino(void)
{
	int i;

	if (ialloc == (struct inodesc **)0)
		return;
	for (i = 0; i < nchunks; i++)
		if (ialloc[i] != 0)
			free(ialloc[i]);
	free(ialloc);
	ialloc = (struct inodesc **)0;
}

void
resetino(ino_t ino)
{
	last = ilist.id_next;
	while (last && last->id_inumber < ino)
		last = last->id_next;
}

char *
unrawname(char *cp)
{
	char *dp;
	extern char *getfullblkname();

	dp = getfullblkname(cp);
	if (dp == 0)
		return (0);
	if (*dp == '\0') {
		free(dp);
		return (0);
	}
	if (dp == cp)		/* caller wants to always free() dp */
		dp = strdup(cp);

	return (dp);
}

/*
 * Determine if specified device is mounted at
 * specified mount point.  Returns 1 if mounted,
 * 0 if not mounted, -1 on error.
 */
int
lf_ismounted(char *devname, char *dirname)
{
	struct stat64 st;
	char	*blockname;	/* name of block device */
	dev_t	dev;
	int	saverr;

	if ((blockname = unrawname(devname)) == NULL) {
		msg(gettext("Cannot obtain block name from `%s'\n"), devname);
		return (-1);
	}
	if (stat64(blockname, &st) < 0) {
		saverr = errno;
		msg(gettext("Cannot obtain status of device `%s': %s\n"),
		    blockname, strerror(saverr));
		free(blockname);
		return (-1);
	}
	free(blockname);
	dev = st.st_rdev;
	if (stat64(dirname, &st) < 0) {
		saverr = errno;
		msg(gettext("Cannot obtain status of device `%s': %s\n"),
		    dirname, strerror(saverr));
		return (-1);
	}
	if (dev == st.st_dev)
		return (1);
	return (0);
}

#ifdef ENABLE_MMAP /* XXX mapped-file support */
#define	MINMAPSIZE	1024*1024
#define	MAXMAPSIZE	1024*1024*32

static caddr_t	mapbase;	/* base of mapped data */
static caddr_t	mapend;		/* last byte of mapped data */
static size_t	mapsize;	/* amount of mapped data */
/*
 * Map a file prior to dumping and start faulting in its
 * pages.  Stop if we catch a signal indicating our turn
 * to dump has arrived.  If the file is truncated out from
 * under us, immediately return.
 * NB:  the base of the mapped data may not coincide
 * exactly to the requested offset, due to alignment
 * constraints.
 */
caddr_t
mapfile(int fd, off_t offset, off_t bytes, int fetch)
{
	/*LINTED [c used during pre-fetch faulting]*/
	volatile char c, *p;
	int stride = (int)sysconf(_SC_PAGESIZE);
	extern int caught;		/* pre-fetch until set */
	caddr_t	mapstart;		/* beginning of file's mapped data */
	off_t	mapoffset;		/* page-aligned offset */
	int	saverr;

	mapbase = mapend = (caddr_t)0;

	if (bytes == 0)
		return ((caddr_t)0);
	/*
	 * mmap the file for reading
	 */
	mapoffset = offset & ~(stride - 1);
	/* LINTED: "bytes" will always fit into a size_t */
	mapsize = bytes + (offset - mapoffset);
	if (mapsize > MAXMAPSIZE)
		mapsize = MAXMAPSIZE;
	while ((mapbase = mmap((caddr_t)0, mapsize, PROT_READ,
	    MAP_SHARED, fd, mapoffset)) == (caddr_t)-1 &&
	    errno == ENOMEM && mapsize >= MINMAPSIZE) {
		/*
		 * Due to address space limitations, we
		 * may not be able to map as much as we want.
		 */
		mapsize /= 2;	/* exponential back-off */
	}

	if (mapbase == (caddr_t)-1) {
		saverr = errno;
		msg(gettext("Cannot map file at inode `%lu' into memory: %s\n"),
		    ino, strerror(saverr));
		/* XXX why not call dumpailing() here? */
		if (!query(gettext(
	    "Do you want to attempt to continue? (\"yes\" or \"no\") "))) {
			dumpabort();
			/*NOTREACHED*/
		}
		mapbase = (caddr_t)0;
		return ((caddr_t)0);
	}

	(void) madvise(mapbase, mapsize, MADV_SEQUENTIAL);
	mapstart = mapbase + (offset - mapoffset);
	mapend = mapbase + (mapsize - 1);

	if (!fetch)
		return (mapstart);

	if (setjmp(truncate_buf) == 0) {
		savebus = signal(SIGBUS, onsigbus);
		/*
		 * Touch each page to pre-fetch by faulting.  At least
		 * one of c or *p must be declared volatile, lest the
		 * optimizer eliminate the assignment in the loop.
		 */
		incopy = 1;
		for (p = mapbase; !caught && p <= mapend; p += stride) {
			/* LINTED: c is used for its side-effects */
			c = *p;
		}
		incopy = 0;
	}
#ifdef DEBUG
	else
		/* XGETTEXT:  #ifdef DEBUG only */
		msg(gettext(
		    "FILE TRUNCATED (fault): Interrupting pre-fetch\n"));
#endif
	(void) signal(SIGBUS, savebus);
	return (mapstart);
}

void
unmapfile(void)
{
	if (mapbase) {
		/* XXX we're unmapping it, so what does this gain us? */
		(void) msync(mapbase, mapsize, MS_ASYNC|MS_INVALIDATE);
		(void) munmap(mapbase, mapsize);
		mapbase = (caddr_t)0;
	}
}
#endif	/* ENABLE_MMAP */

void
activepass(void)
{
	static int passno = 1;			/* active file pass number */
	char *ext, *old;
	char buf[3000];
	static char defext[] = ".retry";

	if (pipeout) {
		msg(gettext("Cannot re-dump active files to `%s'\n"), tape);
		dumpabort();
		/*NOTREACHED*/
	}

	if (active > 1)
		(void) snprintf(buf, sizeof (buf), gettext(
		    "%d files were active and will be re-dumped\n"), active);
	else
		(void) snprintf(buf, sizeof (buf), gettext(
		    "1 file was active and will be re-dumped\n"));
	msg(buf);

	doingactive++;
	active = 0;
	reset();			/* reset tape params */
	spcl.c_ddate = spcl.c_date;	/* chain with last dump/pass */

	/*
	 * If archiving, create a new
	 * archive file.
	 */
	if (archivefile) {
		old = archivefile;

		ext = strstr(old, defext);
		if (ext != (char *)NULL)
			*ext = '\0'; /* just want the base name */

		/* The two is for the trailing \0 and rounding up log10() */
		archivefile = xmalloc(strlen(old) + strlen(defext) +
		    (int)log10((double)passno) + 2);

		/* Always fits */
		(void) sprintf(archivefile, "%s%s%d", old, defext, passno);
		free(old);
	}

	if (tapeout) {
		if (isrewind(to)) {
			/*
			 * A "rewind" tape device.  When we do
			 * the close, we will lose our position.
			 * Be nice and switch volumes.
			 */
			(void) snprintf(buf, sizeof (buf), gettext(
			    "Warning - cannot dump active files to rewind "
			    "device `%s'\n"), tape);
			msg(buf);
			close_rewind();
			changevol();
		} else {
			trewind();
			doposition = 0;
			filenum++;
		}
	} else {
		/*
		 * Not a tape.  Do a volume switch.
		 * This will advance to the next file
		 * if using a sequence of files, next
		 * diskette if using diskettes, or
		 * let the user move the old file out
		 * of the way.
		 */
		close_rewind();
		changevol();	/* switch files */
	}
	(void) snprintf(buf, sizeof (buf), gettext(
	    "Dumping active files (retry pass %d) to `%s'\n"), passno, tape);
	msg(buf);
	passno++;
}
/*
 * Copyright (c) 1998 by Sun Microsystems, Inc.
 * All rights reserved.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/

/*
 * Copyright (c) 1980 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#include <errno.h>
#include "dump.h"

time_t *tschedule;
static unsigned int timeout;		/* current timeout */
static char *attnmessage, *saveattn;	/* attention message */

static void alarmcatch(int);
static int idatesort(const void *, const void *);

#ifdef DEBUG
extern int xflag;
#endif

/*
 *	Query the operator; This fascist piece of code requires
 *	an exact response.
 *	It is intended to protect dump aborting by inquisitive
 *	people banging on the console terminal to see what is
 *	happening which might cause dump to croak, destroying
 *	a large number of hours of work.
 *
 *	Every time += 2 minutes we reprint the message, alerting others
 *	that dump needs attention.
 */
int
query(char *question)
{
	int def = -1;

	while (def == -1)
		def = query_once(question, -1);
	return (def);
}

static int in_query_once;
static jmp_buf sjalarmbuf;

/* real simple check-sum */
static int
addem(char *s)
{
	int total = 0;

	if (s == (char *)NULL)
		return (total);
	while (*s)
		total += *s++;
	return (total);
}

int
query_once(char	*question, int def)
{
	static char *lastmsg;
	static int lastmsgsum;
	int	msgsum;
	char	replybuffer[BUFSIZ];
	int	back;
	time32_t timeclockstate;
	pollfd_t pollset;
	struct sigvec sv;

	/* special hook to flush timeout cache */
	if (question == NULL) {
		lastmsg = (char *)NULL;
		lastmsgsum = 0;
		return (0);
	}

	attnmessage = question;
	/*
	 * Only reset the state if the message changed somehow
	 */
	msgsum = addem(question);
	if (lastmsg != question || lastmsgsum != msgsum) {
		timeout = 0;
		if (telapsed && tstart_writing)
			*telapsed += time((time_t *)0) - *tstart_writing;
		lastmsg = question;
		lastmsgsum = msgsum;
	}
	timeclockstate = timeclock((time_t)0);
	if (setjmp(sjalarmbuf) != 0) {
		if (def != -1) {
			if (def)
				msgtail(gettext("YES\n"));
			else
				msgtail(gettext("NO\n"));
		}
		back = def;
		goto done;
	}
	alarmcatch(SIGALRM);
	in_query_once = 1;
	pollset.fd = -1;
	pollset.events = 0;
	pollset.revents = 0;
	if (isatty(fileno(stdin))) {
		pollset.fd = fileno(stdin);
		pollset.events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND;
	} else {
		dumpabort();
		/*NOTREACHED*/
	}
	for (;;) {
		if (poll(&pollset, 1, -1) < 0) {
			if (errno == EINTR)
				continue;
			perror("poll(stdin)");
			dumpabort();
			/*NOTREACHED*/
		}
		if (pollset.revents == 0)
			continue;	/* sanity check */
		if (fgets(replybuffer, sizeof (replybuffer), stdin) == NULL) {
			if (ferror(stdin)) {
				clearerr(stdin);
				continue;
			} else {
				dumpabort();
				/*NOTREACHED*/
			}
		}
		timeout = 0;
		if (strcasecmp(replybuffer, gettext("yes\n")) == 0) {
			back = 1;
			lastmsg = (char *)NULL;
			lastmsgsum = 0;
			goto done;
		} else if (strcasecmp(replybuffer, gettext("no\n")) == 0) {
			back = 0;
			lastmsg = (char *)NULL;
			lastmsgsum = 0;
			goto done;
		} else {
			msg(gettext("\"yes\" or \"no\"?\n"));
			in_query_once = 0;
			alarmcatch(SIGALRM);
			in_query_once = 1;
		}
	}
done:
	/*
	 * Turn off the alarm, and reset the signal to trap out..
	 */
	(void) alarm(0);
	attnmessage = NULL;
	sv.sv_handler = sigAbort;
	sv.sv_flags = SA_RESTART;
	(void) sigemptyset(&sv.sa_mask);
	(void) sigvec(SIGALRM, &sv, (struct sigvec *)0);
	if (tstart_writing)
		(void) time(tstart_writing);
	(void) timeclock(timeclockstate);
	in_query_once = 0;
	return (back);
}
/*
 *	Alert the console operator, and enable the alarm clock to
 *	sleep for time += 2 minutes in case nobody comes to satisfy dump
 *	If the alarm goes off while in the query_once for loop, we just
 *	longjmp back there and return the default answer.
 */
static void
alarmcatch(int signal __unused)
{
	struct sigvec sv;

	if (in_query_once) {
		longjmp(sjalarmbuf, 1);
	}
	if (timeout) {
		msgtail("\n");
	}

	timeout += 120;
	msg(gettext("NEEDS ATTENTION: %s"), attnmessage);
	sv.sv_handler = alarmcatch;
	sv.sv_flags = SA_RESTART;
	(void) sigemptyset(&sv.sa_mask);
	(void) sigvec(SIGALRM, &sv, (struct sigvec *)0);
	(void) alarm(timeout);
}

/*
 *	Here if an inquisitive operator interrupts the dump program
 */
/*ARGSUSED*/
void
interrupt(int sig)
{
	if (!saveattn) {
		saveattn = attnmessage;
	}
	msg(gettext("Interrupt received.\n"));
	if (query(gettext(
	    "Do you want to abort dump?: (\"yes\" or \"no\") "))) {
		dumpabort();
		/*NOTREACHED*/
	}
	if (saveattn) {
		attnmessage = saveattn;
		saveattn = NULL;
		alarmcatch(SIGALRM);
	}
}

/*
 *	We use wall(8) to do the actual broadcasting, so
 *	that we don't have to worry about duplicated code
 *	only getting fixed in one place.  This also saves
 *	us from having to worry about process groups,
 *	controlling terminals, and the like.
 */
void
broadcast(char *message)
{
	time_t	clock;
	pid_t	pid;
	int	saverr;
	int	fildes[2];
	FILE	*wall;
	struct tm *localclock;

	if (!notify)
		return;

	if (pipe(fildes) < 0) {
		saverr = errno;
		msg(gettext("pipe: %s\n"), strerror(saverr));
		return;
	}

	switch (pid = fork()) {
	case -1:
		return;
	case 0:
		close(fildes[0]);
		if (dup2(fildes[1], 0) < 0) {
			saverr = errno;
			msg(gettext("dup2: %s\n"), strerror(saverr));
			exit(1);
		}
		execl("/usr/sbin/wall", "wall", "-g", OPGRENT, (char *)NULL);
		saverr = errno;
		msg(gettext("execl: %s\n"), strerror(saverr));
		exit(1);
	default:
		break;		/* parent */
	}

	close(fildes[1]);
	wall = fdopen(fildes[0], "r+");
	if (wall == (FILE *)NULL) {
		saverr = errno;
		msg(gettext("fdopen: %s\n"), strerror(saverr));
		return;
	}

	clock = time((time_t *)0);
	localclock = localtime(&clock);

	(void) fprintf(wall, gettext(
"\n\007\007\007Message from the dump program to all operators at \
%d:%02d ...\n\n%s"),
	    localclock->tm_hour, localclock->tm_min, message);
	fclose(wall);

	while (wait((int *)0) != pid) {
		continue;
		/*LINTED [empty loop body]*/
	}
}

/*
 *	print out an estimate of the amount of time left to do the dump
 */
#define	EST_SEC	600			/* every 10 minutes */
void
timeest(int force, int blkswritten)
{
	time_t tnow, deltat;
	char *msgp;

	if (tschedule == NULL)
		return;
	if (*tschedule == 0)
		*tschedule = time((time_t *)0) + EST_SEC;
	(void) time(&tnow);
	if ((force || tnow >= *tschedule) && blkswritten) {
		*tschedule = tnow + EST_SEC;
		if (!force && blkswritten < 50 * ntrec)
			return;
		deltat = (*telapsed + (tnow - *tstart_writing))
		    * ((double)esize / blkswritten - 1.0);
		msgp = gettext("%3.2f%% done, finished in %d:%02d\n");
		msg(msgp, (blkswritten*100.0)/esize,
		    deltat/3600, (deltat%3600)/60);
	}
}

#include <stdarg.h>

/* VARARGS1 */
void
msg(const char *fmt, ...)
{
	char buf[1024], *cp;
	size_t size;
	va_list args;

	va_start(args, fmt);
	(void) strcpy(buf, "  DUMP: ");
	cp = &buf[strlen(buf)];
#ifdef TDEBUG
	(void) sprintf(cp, "pid=%d ", getpid());
	cp = &buf[strlen(buf)];
#endif
	/* don't need -1, vsnprintf does it right */
	/* LINTED pointer arithmetic result fits in size_t */
	size = ((size_t)sizeof (buf)) - (size_t)(cp - buf);
	(void) vsnprintf(cp, size, fmt, args);
	(void) fputs(buf, stderr);
	(void) fflush(stdout);
	(void) fflush(stderr);
	va_end(args);
}

/* VARARGS1 */
void
msgtail(const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	(void) vfprintf(stderr, fmt, args);
	va_end(args);
}

#define	MINUTES(x)	((x) * 60)

/*
 *	Tell the operator what has to be done;
 *	we don't actually do it
 */
void
lastdump(int arg)	/* w ==> just what to do; W ==> most recent dumps */
{
	char *lastname;
	char *date;
	int i;
	time_t tnow, ddate;
	struct mntent *dt;
	int dumpme = 0;
	struct idates *itwalk;

	(void) time(&tnow);
	mnttabread();		/* /etc/fstab input */
	inititimes();		/* /etc/dumpdates input */

	/* Don't use msg(), this isn't a tell-the-world kind of thing */
	if (arg == 'w')
		(void) fprintf(stdout, gettext("Dump these file systems:\n"));
	else
		(void) fprintf(stdout, gettext(
		    "Last dump(s) done (Dump '>' file systems):\n"));

	if (idatev != NULL) {
		qsort((char *)idatev, nidates, sizeof (*idatev), idatesort);
		lastname = "??";
		ITITERATE(i, itwalk) {
			if (strncmp(lastname, itwalk->id_name,
			    sizeof (itwalk->id_name)) == 0)
				continue;
			/* must be ctime(), per ufsdump(5) */
			ddate = itwalk->id_ddate;
			date = (char *)ctime(&ddate);
			date[16] = '\0';	/* blow away seconds and year */
			lastname = itwalk->id_name;
			dt = mnttabsearch(itwalk->id_name, 0);
			if ((time_t)(itwalk->id_ddate) < (tnow - DAY)) {
				dumpme = 1;
			}

			if ((arg == 'w') && dumpme) {
				/*
				 * Handle the w option: print out file systems
				 * which haven't been backed up within a day.
				 */
				(void) printf(gettext("%8s\t(%6s)\n"),
				    itwalk->id_name, dt ? dt->mnt_dir : "");
			}
			if (arg == 'W') {
				/*
				 * Handle the W option: print out ALL
				 * filesystems including recent dump dates and
				 * dump levels.  Mark the backup-needing
				 * filesystems with a >.
				 */
				(void) printf(gettext(
			    "%c %8s\t(%6s) Last dump: Level %c, Date %s\n"),
				    dumpme ? '>' : ' ',
				    itwalk->id_name,
				    dt ? dt->mnt_dir : "",
				    (uchar_t)itwalk->id_incno,
				    date);
			}
			dumpme = 0;
		}
	}
}

static int
idatesort(const void *v1, const void *v2)
{
	struct idates **p1 = (struct idates **)v1;
	struct idates **p2 = (struct idates **)v2;
	int diff;

	diff = strcoll((*p1)->id_name, (*p2)->id_name);
	if (diff == 0) {
		/*
		 * Time may eventually become unsigned, so can't
		 * rely on subtraction to give a useful result.
		 * Note that we are sorting dates into reverse
		 * order, so that we will report based on the
		 * most-recent record for a particular filesystem.
		 */
		if ((*p1)->id_ddate > (*p2)->id_ddate)
			diff = -1;
		else if ((*p1)->id_ddate < (*p2)->id_ddate)
			diff = 1;
	}
	return (diff);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/

/*
 * Portions of this source code were derived from Berkeley 4.3 BSD
 * under license from the Regents of the University of California.
 */

#include "dump.h"
#include <rmt.h>
#include <setjmp.h>
#include <sys/fdio.h>
#include <sys/mkdev.h>
#include <assert.h>
#include <limits.h>

#define	SLEEPMS		50

int newtape;
static uint_t writesize;	/* size of malloc()ed buffer for tape */
static ino_t inos[TP_NINOS];	/* starting inodes on each tape */

/*
 * The req structure is used to pass commands from the parent
 * process through the pipes to the slave processes.  It comes
 * in two flavors, depending on which mode dump is operating under:
 * an inode request (on-line mode) and a disk block request ("old" mode).
 */
/*
 * The inode request structure is used during on-line mode.
 * The master passes inode numbers and starting offsets to
 * the slaves.  The tape writer passes out the current inode,
 * offset, and number of tape records written after completing a volume.
 */
struct ireq {
	ino_t	inumber;	/* inode number to open/dump */
	long	igen;		/* inode generation number */
	off_t	offset;		/* starting offset in inode */
	int	count;		/* count for 1st spclrec */
};
/*
 * The block request structure is used in off-line mode to pass
 * commands to dump disk blocks from the parent process through
 * the pipes to the slave processes.
 */
struct breq {
	diskaddr_t dblk;		/* disk address to read */
	size_t	size;		/* number of bytes to read from disk */
	ulong_t	spclrec[1];	/* actually longer */
};

struct req {
	short	aflag;		/* write data to archive process as well */
	short	tflag;		/* begin new tape */
	union	reqdata {
		struct ireq ino;	/* used for on-line mode */
		struct breq blks;	/* used for off-line mode */
	} data;
};

#define	ir_inumber	data.ino.inumber
#define	ir_igen		data.ino.igen
#define	ir_offset	data.ino.offset
#define	ir_count	data.ino.count

#define	br_dblk		data.blks.dblk
#define	br_size		data.blks.size
#define	br_spcl		data.blks.spclrec

static int reqsiz = 0;	/* alloctape will initialize */

#define	SLAVES 3
struct slaves {
	int	sl_slavefd;	/* pipe from master to slave */
	pid_t	sl_slavepid;	/* slave pid; used by killall() */
	ino_t	sl_inos;	/* inos, if this record starts tape */
	int	sl_offset;	/* logical blocks written for object */
	int	sl_count;	/* logical blocks left in spclrec */
	int	sl_tapea;	/* header number, if starting tape */
	int	sl_firstrec;	/* number of first block on tape */
	int	sl_state;	/* dump output state */
	struct	req *sl_req;	/* instruction packet to slave */
};
static struct slaves slaves[SLAVES];	/* one per slave */
static struct slaves *slp;	/* pointer to current slave */
static struct slaves chkpt;	/* checkpointed data */

struct bdesc {
	char	*b_data;	/* pointer to buffer data */
	int	b_flags;	/* flags (see below) */
};

/*
 * The following variables are in shared memory, and must be
 * explicitly checkpointed and/or reset.
 */
static caddr_t shared;		/* pointer to block of shared memory */
static struct bdesc *bufp;	/* buffer descriptors */
static struct bdesc **current;	/* output buffer to fill */
static int *tapea;		/* logical record count */

#ifdef INSTRUMENT
static int	*readmissp;	/* number of times writer was idle */
static int	*idle;		/* number of times slaves were idle */
#endif	/* INSTRUMENT */

/*
 * Buffer flags
 */
#define	BUF_EMPTY	0x0	/* nothing in buffer */
#define	BUF_FULL	0x1	/* data in buffer */
#define	BUF_SPCLREC	0x2	/* contains special record */
#define	BUF_ARCHIVE	0x4	/* dump to archive */

static int recsout;		/* number of req's sent to slaves */
static int totalrecsout;	/* total number of req's sent to slaves */
static int rotor;		/* next slave to be instructed */
static pid_t master;		/* pid of master, for sending error signals */
static int writer = -1;		/* fd of tape writer */
static pid_t writepid;		/* pid of tape writer */
static int arch;		/* fd of output archiver */
static pid_t archivepid;	/* pid of output archiver */
static int archivefd;		/* fd of archive file (proper) */
static offset_t lf_archoffset;	/* checkpointed offset into archive file */

int caught;			/* caught signal -- imported by mapfile() */

#ifdef DEBUG
extern	int xflag;
#endif

static void cmdwrterr(void);
static void cmdrderr(void);
static void freetape(void);
static void bufclear(void);
static pid_t setuparchive(void);
static pid_t setupwriter(void);
static void nextslave(void);
static void tperror(int);
static void rollforward(int);
static void nap(int);
static void alrm(int);
static void just_rewind(void);
static void killall(void);
static void proceed(int);
static void die(int);
static void enslave(void);
static void wait_our_turn(void);
static void dumpoffline(int, pid_t, int);
static void onxfsz(int);
static void dowrite(int);
static void checkpoint(struct bdesc *, int);
static ssize_t atomic(int (*)(), int, char *, int);

static size_t tapesize;

/*
 * Allocate buffers and shared memory variables.  Tape buffers are
 * allocated on page boundaries for tape write() efficiency.
 */
void
alloctape(void)
{
	struct slaves *slavep;
	ulong_t pgoff = (unsigned)(getpagesize() - 1); /* 2**n - 1 */
	int	mapfd;
	char	*obuf;
	int	saverr;
	int	i, j;

	writesize = ntrec * tp_bsize;
	if (!printsize)
		msg(gettext("Writing %d Kilobyte records\n"),
		    writesize / TP_BSIZE_MIN);

	/*
	 * set up shared memory seg for here and child
	 */
	mapfd = open("/dev/zero", O_RDWR);
	if (mapfd == -1) {
		saverr = errno;
		msg(gettext("Cannot open `%s': %s\n"),
		    "/dev/zero", strerror(saverr));
		dumpabort();
		/*NOTREACHED*/
	}
	/*
	 * Allocate space such that buffers are page-aligned and
	 * pointers are aligned on 4-byte boundaries (for SPARC).
	 * This code assumes that (NBUF * writesize) is a multiple
	 * of the page size and that pages are aligned on 4-byte
	 * boundaries.  Space is allocated as follows:
	 *
	 *    (NBUF * writesize) for the actual buffers
	 *    (pagesize - 1) for padding so the buffers are page-aligned
	 *    (NBUF * ntrec * sizeof (struct bdesc)) for each buffer
	 *    (n * sizeof (int)) for [n] debugging variables/pointers
	 *    (n * sizeof (int)) for [n] miscellaneous variables/pointers
	 */
	tapesize =
	    (NBUF * writesize)				/* output buffers */
		/* LINTED: pgoff fits into a size_t */
	    + (size_t)pgoff				/* page alignment */
							/* buffer descriptors */
	    + (((size_t)sizeof (struct bdesc)) * NBUF * ntrec)
#ifdef INSTRUMENT
	    + (2 * (size_t)sizeof (int *))		/* instrumentation */
#endif
							/* shared variables */
	    + (size_t)sizeof (struct bdesc **)
	    + (size_t)sizeof (int *)
	    + (3 * (size_t)sizeof (time_t));

	shared = mmap((char *)0, tapesize, PROT_READ|PROT_WRITE,
	    MAP_SHARED, mapfd, (off_t)0);
	if (shared == (caddr_t)-1) {
		saverr = errno;
		msg(gettext("Cannot memory map output buffers: %s\n"),
		    strerror(saverr));
		dumpabort();
		/*NOTREACHED*/
	}
	(void) close(mapfd);

	/*
	 * Buffers and buffer headers
	 */
	obuf = (char *)(((ulong_t)shared + pgoff) & ~pgoff);
	/* LINTED obuf and writesize are aligned */
	bufp = (struct bdesc *)(obuf + NBUF*writesize);
	/*
	 * Shared memory variables
	 */
	current = (struct bdesc **)&bufp[NBUF*ntrec];
	tapea = (int *)(current + 1);
	/* LINTED pointer alignment ok */
	telapsed = (time_t *)(tapea + 1);
	tstart_writing = telapsed + 1;
	tschedule = tstart_writing + 1;
#ifdef INSTRUMENT
	/*
	 * Debugging and instrumentation variables
	 */
	readmissp = (int *)(tschedule + 1);
	idle = readmissp + 1;
#endif
	for (i = 0, j = 0; i < NBUF * ntrec; i++, j += tp_bsize) {
		bufp[i].b_data = &obuf[j];
	}

	reqsiz = sizeof (struct req) + tp_bsize - sizeof (long);
	for (slavep = slaves; slavep < &slaves[SLAVES]; slavep++)
		slavep->sl_req = (struct req *)xmalloc(reqsiz);

	chkpt.sl_offset = 0;		/* start at offset 0 */
	chkpt.sl_count = 0;
	chkpt.sl_inos = UFSROOTINO;	/* in root inode */
	chkpt.sl_firstrec = 1;
	chkpt.sl_tapea = 0;
}

static void
freetape(void)
{
	if (shared == NULL)
		return;
	(void) timeclock((time_t)0);
	(void) munmap(shared, tapesize);
	shared = NULL;
}

/*
 * Reset tape state variables -- called
 * before a pass to dump active files.
 */
void
reset(void)
{
	bufclear();

#ifdef INSTRUMENT
	(*readmissp) = 0;
	(*idle) = 0;
#endif

	spcl.c_flags = 0;
	spcl.c_volume = 0;
	tapeno = 0;

	chkpt.sl_offset = 0;		/* start at offset 0 */
	chkpt.sl_count = 0;
	chkpt.sl_inos = UFSROOTINO;	/* in root inode */
	chkpt.sl_firstrec = 1;
	chkpt.sl_tapea = 0;
}

static void
bufclear(void)
{
	struct bdesc *bp;
	int i;

	for (i = 0, bp = bufp; i < NBUF * ntrec; i++, bp++)
		bp->b_flags = BUF_EMPTY;
	if ((caddr_t)current < shared ||
	    (caddr_t)current > (shared + tapesize)) {
		msg(gettext(
	    "bufclear: current pointer out of range of shared memory\n"));
		dumpabort();
		/*NOTREACHED*/
	}
	if ((*current != NULL) &&
	    (*current < &bufp[0] || *current > &bufp[NBUF*ntrec])) {
		/* ANSI string catenation, to shut cstyle up */
		msg(gettext("bufclear: current buffer pointer (0x%x) "
		    "out of range of buffer\naddresses (0x%x - 0x%x)\n"),
		    *current, &bufp[0], &bufp[NBUF*ntrec]);
		dumpabort();
		/*NOTREACHED*/
	}
	*current = bufp;
}

/*
 * Start a process to collect information describing the dump.
 * This data takes two forms:
 *    the bitmap and directory information being written to
 *	the front of the tape (the "archive" file)
 *    information describing each directory and inode (to
 *	be included in the database tmp file)
 * Write the data to the files as it is received so huge file
 * systems don't cause dump to consume large amounts of memory.
 */
static pid_t
setuparchive(void)
{
	struct slaves *slavep;
	int cmd[2];
	pid_t pid;
	ssize_t size;
	char *data;
	char *errmsg;
	int flags, saverr;
	int punt = 0;

	/*
	 * Both the archive and database tmp files are
	 * checkpointed by taking their current offsets
	 * (sizes) after completing each volume.  Restoring
	 * from a checkpoint involves truncating to the
	 * checkpointed size.
	 */
	if (archive && !doingactive) {
		/* It's allowed/expected to exist, so can't use O_EXCL */
		archivefd = safe_file_open(archivefile, O_WRONLY, 0600);
		if (archivefd < 0) {
			saverr = errno;
			msg(gettext("Cannot open archive file `%s': %s\n"),
			    archivefile, strerror(saverr));
			dumpabort();
			/*NOTREACHED*/
		}

		archive_opened = 1;

		if (lseek64(archivefd, lf_archoffset, 0) < 0) {
			saverr = errno;
			msg(gettext(
			    "Cannot position archive file `%s' : %s\n"),
			    archivefile, strerror(saverr));
			dumpabort();
			/*NOTREACHED*/
		}
		if (ftruncate64(archivefd, lf_archoffset) < 0) {
			saverr = errno;
			msg(gettext(
			    "Cannot truncate archive file `%s' : %s\n"),
			    archivefile, strerror(saverr));
			dumpabort();
			/*NOTREACHED*/
		}
	}

	if (pipe(cmd) < 0) {
		saverr = errno;
		msg(gettext("%s: %s error: %s\n"),
		    "setuparchive", "pipe", strerror(saverr));
		return (0);
	}
	sighold(SIGINT);
	if ((pid = fork()) < 0) {
		saverr = errno;
		msg(gettext("%s: %s error: %s\n"),
		    "setuparchive", "fork", strerror(saverr));
		return (0);
	}
	if (pid > 0) {
		sigrelse(SIGINT);
		/* parent process */
		(void) close(cmd[0]);
		arch = cmd[1];
		return (pid);
	}
	/*
	 * child process
	 */
	(void) signal(SIGINT, SIG_IGN);		/* master handles this */
#ifdef TDEBUG
	(void) sleep(4);	/* allow time for parent's message to get out */
	/* XGETTEXT:  #ifdef TDEBUG only */
	msg(gettext("Archiver has pid = %ld\n"), (long)getpid());
#endif
	freeino();	/* release unneeded resources */
	freetape();
	for (slavep = &slaves[0]; slavep < &slaves[SLAVES]; slavep++) {
		if (slavep->sl_slavefd != -1) {
			(void) close(slavep->sl_slavefd);
			slavep->sl_slavefd = -1;
		}
	}
	(void) close(to);
	(void) close(fi);
	to = fi = -1;
	(void) close(cmd[1]);
	data = xmalloc(tp_bsize);
	for (;;) {
		size = atomic((int(*)())read, cmd[0], (char *)&flags,
		    sizeof (flags));
		if ((unsigned)size != sizeof (flags))
			break;
		size = atomic((int(*)())read, cmd[0], data, tp_bsize);
		if (size == tp_bsize) {
			if (archive && flags & BUF_ARCHIVE && !punt &&
			    (size = write(archivefd, data, tp_bsize))
			    != tp_bsize) {
				struct stat64 stats;

				if (size != -1) {
					errmsg = strdup(gettext(
					    "Output truncated"));
					if (errmsg == NULL)
						errmsg = "";
				} else {
					errmsg = strerror(errno);
				}

				if (fstat64(archivefd, &stats) < 0)
					stats.st_size = -1;

				/* cast to keep lint&printf happy */
				msg(gettext(
		    "Cannot write archive file `%s' at offset %lld: %s\n"),
				    archivefile, (longlong_t)stats.st_size,
				    errmsg);
				msg(gettext(
		    "Archive file will be deleted, dump will continue\n"));
				punt++;
				if ((size != -1) && (*errmsg != '\0')) {
					free(errmsg);
				}
			}
		} else {
			break;
		}
	}
	(void) close(cmd[0]);
	if (archive) {
		(void) close(archivefd);
		archivefd = -1;
	}
	if (punt) {
		(void) unlink(archivefile);
		Exit(X_ABORT);
	}
	Exit(X_FINOK);
	/* NOTREACHED */
	return (0);
}

/*
 * Start a process to read the output buffers and write the data
 * to the output device.
 */
static pid_t
setupwriter(void)
{
	struct slaves *slavep;
	int cmd[2];
	pid_t pid;
	int saverr;

	caught = 0;
	if (pipe(cmd) < 0) {
		saverr = errno;
		msg(gettext("%s: %s error: %s\n"),
		    "setupwriter", "pipe", strerror(saverr));
		return (0);
	}
	sighold(SIGINT);
	if ((pid = fork()) < 0) {
		saverr = errno;
		msg(gettext("%s: %s error: %s\n"),
		    "setupwriter", "fork", strerror(saverr));
		return (0);
	}
	if (pid > 0) {
		/*
		 * Parent process
		 */
		sigrelse(SIGINT);
		(void) close(cmd[0]);
		writer = cmd[1];
		return (pid);
	}
	/*
	 * Child (writer) process
	 */
	(void) signal(SIGINT, SIG_IGN);		/* master handles this */
#ifdef TDEBUG
	(void) sleep(4);	/* allow time for parent's message to get out */
	/* XGETTEXT:  #ifdef TDEBUG only */
	msg(gettext("Writer has pid = %ld\n"), (long)getpid());
#endif
	child_chdir();
	freeino();	/* release unneeded resources */
	for (slavep = &slaves[0]; slavep < &slaves[SLAVES]; slavep++) {
		if (slavep->sl_slavefd != -1) {
			(void) close(slavep->sl_slavefd);
			slavep->sl_slavefd = -1;
		}
	}
	(void) close(fi);
	fi = -1;
	(void) close(cmd[1]);
	dowrite(cmd[0]);
	if (arch >= 0) {
		(void) close(arch);
		arch = -1;
	}
	(void) close(cmd[0]);
	Exit(X_FINOK);
	/* NOTREACHED */
	return (0);
}

void
spclrec(void)
{
	int s, i;
	int32_t *ip;
	int flags = BUF_SPCLREC;

	if ((BIT(ino, shamap)) && (spcl.c_type == TS_INODE)) {
		spcl.c_type = TS_ADDR;
		/* LINTED: result fits in a short */
		spcl.c_dinode.di_mode &= ~S_IFMT;
		/* LINTED: result fits in a short */
		spcl.c_dinode.di_mode |= IFSHAD;
	}

	/*
	 * Only TS_INODEs should have short metadata, if this
	 * isn't such a spclrec, clear the metadata flag and
	 * the c_shadow contents.
	 */
	if (!(spcl.c_type == TS_INODE && (spcl.c_flags & DR_HASMETA))) {
		spcl.c_flags &= ~DR_HASMETA;
		bcopy(c_shadow_save, &(spcl.c_shadow),
		    sizeof (spcl.c_shadow));
	}

	if (spcl.c_type == TS_END) {
		spcl.c_count = 1;
		spcl.c_flags |= DR_INODEINFO;
		bcopy((char *)inos, (char *)spcl.c_inos, sizeof (inos));
	} else if (spcl.c_type == TS_TAPE) {
		spcl.c_flags |= DR_NEWHEADER;
		if (doingactive)
			spcl.c_flags |= DR_REDUMP;
	} else if (spcl.c_type != TS_INODE)
		flags = BUF_SPCLREC;
	spcl.c_tapea = *tapea;
	/* LINTED for now, max inode # is 2**31 (ufs max size is 4TB) */
	spcl.c_inumber = (ino32_t)ino;
	spcl.c_magic = (tp_bsize == TP_BSIZE_MIN) ? NFS_MAGIC : MTB_MAGIC;
	spcl.c_checksum = 0;
	ip = (int32_t *)&spcl;
	s = CHECKSUM;
	assert((tp_bsize % sizeof (*ip)) == 0);
	i = tp_bsize / sizeof (*ip);
	assert((i%8) == 0);
	i /= 8;
	do {
		s -= *ip++; s -= *ip++; s -= *ip++; s -= *ip++;
		s -= *ip++; s -= *ip++; s -= *ip++; s -= *ip++;
	} while (--i > 0);
	spcl.c_checksum = s;
	taprec((uchar_t *)&spcl, flags, sizeof (spcl));
	if (spcl.c_type == TS_END)
		spcl.c_flags &= ~DR_INODEINFO;
	else if (spcl.c_type == TS_TAPE)
		spcl.c_flags &= ~(DR_NEWHEADER|DR_REDUMP|DR_TRUEINC);
}

/*
 * Fill appropriate buffer
 */
void
taprec(uchar_t *dp, int flags, int size)
{
	if (size > tp_bsize) {
		msg(gettext(
		    "taprec: Unexpected buffer size, expected %d, got %d.\n"),
		    tp_bsize, size);
		dumpabort();
		/*NOTREACHED*/
	}

	while ((*current)->b_flags & BUF_FULL)
		nap(10);

	bcopy(dp, (*current)->b_data, (size_t)size);
	if (size < tp_bsize) {
		bzero((*current)->b_data + size, tp_bsize - size);
	}

	if (dumptoarchive)
		flags |= BUF_ARCHIVE;

	/* no locking as we assume only one reader and one writer active */
	(*current)->b_flags = (flags | BUF_FULL);
	if (++*current >= &bufp[NBUF*ntrec])
		(*current) = &bufp[0];
	(*tapea)++;
}

void
dmpblk(daddr32_t blkno, size_t size, off_t offset)
{
	diskaddr_t dblkno;

	assert((offset >> DEV_BSHIFT) <= INT32_MAX);
	dblkno = fsbtodb(sblock, blkno) + (offset >> DEV_BSHIFT);
	size = (size + DEV_BSIZE-1) & ~(DEV_BSIZE-1);
	slp->sl_req->br_dblk = dblkno;
	slp->sl_req->br_size = size;
	if (dumptoarchive) {
		/* LINTED: result fits in a short */
		slp->sl_req->aflag |= BUF_ARCHIVE;
	}
	toslave((void(*)())0, ino);
}

/*ARGSUSED*/
static void
tperror(int sig)
{
	char buf[3000];

	if (pipeout) {
		msg(gettext("Write error on %s\n"), tape);
		msg(gettext("Cannot recover\n"));
		dumpabort();
		/* NOTREACHED */
	}
	if (!doingverify) {
		broadcast(gettext("WRITE ERROR!\n"));
		(void) snprintf(buf, sizeof (buf),
		    gettext("Do you want to restart?: (\"yes\" or \"no\") "));
		if (!query(buf)) {
			dumpabort();
			/*NOTREACHED*/
		}
		if (tapeout && (isrewind(to) || offline)) {
			/* ANSI string catenation, to shut cstyle up */
			msg(gettext("This tape will rewind.  After "
			    "it is rewound,\nreplace the faulty tape "
			    "with a new one;\nthis dump volume will "
			    "be rewritten.\n"));
		}
	} else {
		broadcast(gettext("TAPE VERIFICATION ERROR!\n"));
		(void) snprintf(buf, sizeof (buf), gettext(
		    "Do you want to rewrite?: (\"yes\" or \"no\") "));
		if (!query(buf)) {
			dumpabort();
			/*NOTREACHED*/
		}
		msg(gettext(
		    "This tape will be rewritten and then verified\n"));
	}
	killall();
	trewind();
	Exit(X_REWRITE);
}

/*
 * Called by master from pass() to send a request to dump files/blocks
 * to one of the slaves.  Slaves return whether the file was active
 * when it was being dumped.  The tape writer process sends checkpoint
 * info when it completes a volume.
 */
void
toslave(void (*fn)(), ino_t inumber)
{
	int	wasactive;

	if (recsout >= SLAVES) {
		if ((unsigned)atomic((int(*)())read, slp->sl_slavefd,
		    (char *)&wasactive, sizeof (wasactive)) !=
		    sizeof (wasactive)) {
			cmdrderr();
			dumpabort();
			/*NOTREACHED*/
		}
		if (wasactive) {
			active++;
			msg(gettext(
			    "The file at inode `%lu' was active and will "
			    "be recopied\n"),
			    slp->sl_req->ir_inumber);
			/* LINTED: 32-bit to 8-bit assignment ok */
			BIS(slp->sl_req->ir_inumber, activemap);
		}
	}
	slp->sl_req->aflag = 0;
	if (dumptoarchive) {
		/* LINTED: result fits in a short */
		slp->sl_req->aflag |= BUF_ARCHIVE;
	}
	if (fn)
		(*fn)(inumber);

	if (atomic((int(*)())write, slp->sl_slavefd, (char *)slp->sl_req,
	    reqsiz) != reqsiz) {
		cmdwrterr();
		dumpabort();
		/*NOTREACHED*/
	}
	++recsout;
	nextslave();
}

void
dospcl(ino_t inumber)
{
	/* LINTED for now, max inode # is 2**31 (ufs max size is 1TB) */
	spcl.c_inumber = (ino32_t)inumber;
	slp->sl_req->br_dblk = 0;
	bcopy((char *)&spcl, (char *)slp->sl_req->br_spcl, tp_bsize);
}

static void
nextslave(void)
{
	if (++rotor >= SLAVES) {
		rotor = 0;
	}
	slp = &slaves[rotor];
}

void
flushcmds(void)
{
	int i;
	int wasactive;

	/*
	 * Retrieve all slave status
	 */
	if (recsout < SLAVES) {
		slp = slaves;
		rotor = 0;
	}
	for (i = 0; i < (recsout < SLAVES ? recsout : SLAVES); i++) {
		if ((unsigned)atomic((int(*)())read, slp->sl_slavefd,
		    (char *)&wasactive, sizeof (wasactive)) !=
		    sizeof (wasactive)) {
			cmdrderr();
			dumpabort();
			/*NOTREACHED*/
		}
		if (wasactive) {
			active++;
			msg(gettext(
			    "inode %d was active and will be recopied\n"),
			    slp->sl_req->ir_inumber);
			/* LINTED: 32-bit to 8-bit assignment ok */
			BIS(slp->sl_req->ir_inumber, activemap);
		}
		nextslave();
	}
}

void
flusht(void)
{
	sigset_t block_set, oset;	/* hold SIGUSR1 and atomically sleep */

	(void) sigemptyset(&block_set);
	(void) sigaddset(&block_set, SIGUSR1);
	(void) sigprocmask(SIG_BLOCK, &block_set, &oset);
	(void) kill(writepid, SIGUSR1);	/* tell writer to flush */
	(void) sigpause(SIGUSR1);	/* wait for SIGUSR1 from writer */
	/*NOTREACHED*/
}

jmp_buf	checkpoint_buf;

/*
 * Roll forward to the next volume after receiving
 * an EOT signal from writer.  Get checkpoint data
 * from writer and return if done, otherwise fork
 * a new process and jump back to main state loop
 * to begin the next volume.  Installed as the master's
 * signal handler for SIGUSR1.
 */
/*ARGSUSED*/
static void
rollforward(int sig)
{
	int status;
	(void) sighold(SIGUSR1);

	/*
	 * Writer sends us checkpoint information after
	 * each volume.  A returned state of DS_DONE with no
	 * unwritten (left-over) records differentiates a
	 * clean flush from one in which EOT was encountered.
	 */
	if ((unsigned)atomic((int(*)())read, writer, (char *)&chkpt,
	    sizeof (struct slaves)) != sizeof (struct slaves)) {
		cmdrderr();
		dumpabort();
		/*NOTREACHED*/
	}
	if (atomic((int(*)())read, writer, (char *)&spcl,
	    TP_BSIZE_MIN) != TP_BSIZE_MIN) {
		cmdrderr();
		dumpabort();
		/*NOTREACHED*/
	}
	ino = chkpt.sl_inos - 1;
	pos = chkpt.sl_offset;
	leftover = chkpt.sl_count;
	dumpstate = chkpt.sl_state;
	blockswritten = ++chkpt.sl_tapea;

	if (dumpstate == DS_DONE) {
		if (archivepid) {
			/*
			 * If archiving (either archive or
			 * database), signal the archiver
			 * to finish up.  This must happen
			 * before the writer exits in order
			 * to avoid a race.
			 */
			(void) kill(archivepid, SIGUSR1);
		}
		(void) signal(SIGUSR1, SIG_IGN);
		(void) sigrelse(SIGUSR1);
		(void) kill(writepid, SIGUSR1);	/* tell writer to exit */

		lf_archoffset = 0LL;
		longjmp(checkpoint_buf, 1);
		/*NOTREACHED*/
	}

	if (leftover) {
		(void) memmove(spcl.c_addr,
		    &spcl.c_addr[spcl.c_count-leftover], leftover);
		bzero(&spcl.c_addr[leftover], TP_NINDIR-leftover);
	}
	if (writepid) {
		(void) kill(writepid, SIGUSR1);	/* tell writer to exit */
		(void) close(writer);
		writer = -1;
	}
	if (archivepid) {
		(void) waitpid(archivepid, &status, 0);	/* wait for archiver */
#ifdef TDEBUG

		/* XGETTEXT:  #ifdef TDEBUG only */
		msg(gettext("Archiver %ld returns with status %d\n"),
		    (long)archivepid, status);
#endif
		archivepid = 0;
	}
	/*
	 * Checkpoint archive file
	 */
	if (!doingverify && archive) {
		lf_archoffset = lseek64(archivefd, (off64_t)0, 2);
		if (lf_archoffset < 0) {
			int saverr = errno;
			msg(gettext("Cannot position archive file `%s': %s\n"),
			    archivefile, strerror(saverr));
			dumpabort();
			/*NOTREACHED*/
		}
		(void) close(archivefd);
		archivefd = -1;
	}
	resetino(ino);

	if (dumpstate == DS_START) {
		msg(gettext(
		    "Tape too short: changing volumes and restarting\n"));
		reset();
	}

	if (!pipeout) {
		if (verify && !doingverify)
			trewind();
		else {
			close_rewind();
			changevol();
		}
	}

	(void) sigrelse(SIGUSR1);
	otape(0);
	longjmp(checkpoint_buf, 1);
	/*NOTREACHED*/
}

static void
nap(int ms)
{
	struct timeval tv;

	tv.tv_sec = ms / 1000;
	tv.tv_usec = (ms - tv.tv_sec * 1000) * 1000;
	(void) select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv);
}

static jmp_buf alrm_buf;

/*ARGSUSED*/
static void
alrm(int sig)
{
	longjmp(alrm_buf, 1);
	/*NOTREACHED*/
}

void
nextdevice(void)
{
	char	*cp;

	if (host != NULL)	/* we set the host only once in ufsdump */
		return;

	host = NULL;
	if (strchr(tape, ':')) {
		if (diskette) {
			msg(gettext("Cannot do remote dump to diskette\n"));
			Exit(X_ABORT);
		}
		host = tape;
		tape = strchr(host, ':');
		*tape++ = 0;
		cp = strchr(host, '@');	/* user@host? */
		if (cp != (char *)0)
			cp++;
		else
			cp = host;
	} else
		cp = spcl.c_host;
	/*
	 * dumpdev is provided for use in prompts and is of
	 * the form:
	 *	hostname:device
	 * sdumpdev is of the form:
	 *	hostname:device
	 * for remote devices, and simply:
	 *	device
	 * for local devices.
	 */
	if (dumpdev != (char *)NULL) {
		/* LINTED: dumpdev is not NULL */
		free(dumpdev);
	}
	/*LINTED [cast to smaller integer]*/
	dumpdev = xmalloc((size_t)((sizeof (spcl.c_host) + strlen(tape) + 2)));
	/* LINTED unsigned -> signed cast ok */
	(void) sprintf(dumpdev, "%.*s:%s", (int)sizeof (spcl.c_host), cp, tape);
	if (cp == spcl.c_host)
		sdumpdev = strchr(dumpdev, ':') + 1;
	else
		sdumpdev = dumpdev;
}

/*
 * Gross hack due to misfeature of mt tape driver that causes
 * the device to rewind if we generate any signals.  Guess
 * whether tape is rewind device or not -- for local devices
 * we can just look at the minor number.  For rmt devices,
 * make an educated guess.
 */
int
isrewind(int f)
{
	struct stat64 sbuf;
	char    *c;
	int	unit;
	int	rewind;

	if (host) {
		c = strrchr(tape, '/');
		if (c == NULL)
			c = tape;
		else
			c++;
		/*
		 * If the last component begins or ends with an 'n', it is
		 * assumed to be a non-rewind device.
		 */
		if (c[0] == 'n' || c[strlen(c)-1] == 'n')
			rewind = 0;
		else if ((strstr(tape, "mt") || strstr(tape, "st")) &&
		    sscanf(tape, "%*[a-zA-Z/]%d", &unit) == 1 &&
		    (unit & MT_NOREWIND))
			rewind = 0;
		else
			rewind = 1;
	} else {
		if (fstat64(f, &sbuf) < 0) {
			msg(gettext(
			    "Cannot obtain status of output device `%s'\n"),
			    tape);
			dumpabort();
			/*NOTREACHED*/
		}
		rewind = minor(sbuf.st_rdev) & MT_NOREWIND ? 0 : 1;
	}
	return (rewind);
}

static void
just_rewind(void)
{
	struct slaves *slavep;
	char *rewinding = gettext("Tape rewinding\n");

	for (slavep = &slaves[0]; slavep < &slaves[SLAVES]; slavep++) {
		if (slavep->sl_slavepid > 0)	/* signal normal exit */
			(void) kill(slavep->sl_slavepid, SIGTERM);
		if (slavep->sl_slavefd >= 0) {
			(void) close(slavep->sl_slavefd);
			slavep->sl_slavefd = -1;
		}
	}

	/* wait for any signals from slaves */
	while (waitpid(0, (int *)0, 0) >= 0)
		/*LINTED [empty body]*/
		continue;

	if (pipeout)
		return;

	if (doingverify) {
		/*
		 * Space to the end of the tape.
		 * Backup first in case we already read the EOF.
		 */
		if (host) {
			(void) rmtioctl(MTBSR, 1);
			if (rmtioctl(MTEOM, 1) < 0)
				(void) rmtioctl(MTFSF, 1);
		} else {
			static struct mtop bsr = { MTBSR, 1 };
			static struct mtop eom = { MTEOM, 1 };
			static struct mtop fsf = { MTFSF, 1 };

			(void) ioctl(to, MTIOCTOP, &bsr);
			if (ioctl(to, MTIOCTOP, &eom) < 0)
				(void) ioctl(to, MTIOCTOP, &fsf);
		}
	}

	/*
	 * Guess whether the tape is rewinding so we can tell
	 * the operator if it's going to take a long time.
	 */
	if (tapeout && isrewind(to)) {
		/* tape is probably rewinding */
		msg(rewinding);
	}
}

void
trewind(void)
{
	(void) timeclock((time_t)0);
	if (offline && (!verify || doingverify)) {
		close_rewind();
	} else {
		just_rewind();
		if (host)
			rmtclose();
		else {
			(void) close(to);
			to = -1;
		}
	}
}

void
close_rewind(void)
{
	char *rewinding = gettext("Tape rewinding\n");

	(void) timeclock((time_t)0);
	just_rewind();
	/*
	 * The check in just_rewind won't catch the case in
	 * which the current volume is being taken off-line
	 * and is not mounted on a no-rewind device (and is
	 * not the last volume, which is not taken off-line).
	 */
	if (tapeout && !isrewind(to) && offline) {
		/* tape is probably rewinding */
		msg(rewinding);
	}
	if (host) {
		if (offline || autoload)
			(void) rmtioctl(MTOFFL, 0);
		rmtclose();
	} else {
		if (offline || autoload) {
			static struct mtop offl = { MTOFFL, 0 };

			(void) ioctl(to, MTIOCTOP, &offl);
			if (diskette)
				(void) ioctl(to, FDEJECT, 0);
		}
		(void) close(to);
		to = -1;
	}
}

void
changevol(void)
{
	char buf1[3000], buf2[3000];
	char volname[LBLSIZE+1];

	/*CONSTANTCONDITION*/
	assert(sizeof (spcl.c_label) < sizeof (volname));

	filenum = 1;
	nextdevice();
	(void) strcpy(spcl.c_label, tlabel);
	if (host) {
		char	*rhost = host;
		char	*cp = strchr(host, '@');
		if (cp == (char *)0)
			cp = host;
		else
			cp++;

		if (rmthost(rhost, ntrec) == 0) {
			msg(gettext("Cannot connect to tape host `%s'\n"), cp);
			dumpabort();
			/*NOTREACHED*/
		}
		if (rhost != host)
			free(rhost);
	}

	/*
	 * Make volume switching as automatic as possible
	 * while avoiding overwriting volumes.  We will
	 * switch automatically under the following condition:
	 *    1) The user specified autoloading from the
	 *	command line.
	 * At one time, we (in the guise of hsmdump) had the
	 * concept of a sequence of devices to rotate through,
	 * but that's never been a ufsdump feature.
	 */
	if (autoload) {
		int tries;

		/*
		 * Stop the clock for throughput calculations.
		 */
		if ((telapsed != NULL) && (tstart_writing != NULL)) {
			*telapsed += time((time_t *)NULL) - *tstart_writing;
		}

		(void) snprintf(volname, sizeof (volname), "#%d", tapeno+1);
		(void) snprintf(buf1, sizeof (buf1), gettext(
		    "Mounting volume %s on %s\n"), volname, dumpdev);
		msg(buf1);
		broadcast(buf1);

		/*
		 * Wait for the tape to autoload.  Note that the delay
		 * period doesn't take into account however long it takes
		 * for the open to fail (measured at 21 seconds for an
		 * Exabyte 8200 under 2.7 on an Ultra 2).
		 */
		for (tries = 0; tries < autoload_tries; tries++) {
			if (host) {
				if (rmtopen(tape, O_RDONLY) >= 0) {
					rmtclose();
					return;
				}
			} else {
				int f, m;

				m = (access(tape, F_OK) == 0) ? 0 : O_CREAT;
				if ((f = doingverify ?
				    safe_device_open(tape, O_RDONLY, 0600) :
				    safe_device_open(tape, O_RDONLY|m, 0600))
				    >= 0) {
					(void) close(f);
					return;
				}
			}
			(void) sleep(autoload_period);
		}
		/*
		 * Autoload timed out, ask the operator to do it.
		 * Note that query() will update *telapsed, and we
		 * shouldn't charge for the autoload time.  So, since
		 * we updated *telapsed ourselves above, we just set
		 * tstart_writing to the current time, and query()
		 * will end up making a null-effect change.  This,
		 * of course, assumes that our caller will be resetting
		 * *tstart_writing.  This is currently the case.
		 * If tstart_writing is NULL (should never happen),
		 * we're ok, since time(2) will accept a NULL pointer.
		 */
		(void) time(tstart_writing);
	}

	if (strncmp(spcl.c_label, "none", 5)) {
		(void) strncpy(volname, spcl.c_label, sizeof (spcl.c_label));
		volname[sizeof (spcl.c_label)] = '\0';
	} else
		(void) snprintf(volname, sizeof (volname), "#%d", tapeno+1);

	timeest(1, spcl.c_tapea);
	(void) snprintf(buf1, sizeof (buf1), gettext(
	    "Change Volumes: Mount volume `%s' on `%s'\n"), volname, dumpdev);
	msg(buf1);
	broadcast(gettext("CHANGE VOLUMES!\7\7\n"));
	(void) snprintf(buf1, sizeof (buf1), gettext(
	    "Is the new volume (%s) mounted on `%s' and ready to go?: %s"),
	    volname, dumpdev, gettext("(\"yes\" or \"no\") "));
	while (!query(buf1)) {
		(void) snprintf(buf2, sizeof (buf2), gettext(
		    "Do you want to abort dump?: (\"yes\" or \"no\") "));
		if (query(buf2)) {
			dumpabort();
			/*NOTREACHED*/
		}
	}
}

/*
 *	We implement taking and restoring checkpoints on the tape level.
 *	When each tape is opened, a new process is created by forking; this
 *	saves all of the necessary context in the parent.  The child
 *	continues the dump; the parent waits around, saving the context.
 *	If the child returns X_REWRITE, then it had problems writing that tape;
 *	this causes the parent to fork again, duplicating the context, and
 *	everything continues as if nothing had happened.
 */

void
otape(int top)
{
	static struct mtget mt;
	char buf[3000];
	pid_t parentpid;
	pid_t childpid;
	pid_t waitproc;
	int status;
	struct sigvec sv, osv;

	sv.sv_flags = SA_RESTART;
	(void) sigemptyset(&sv.sa_mask);
	sv.sv_handler = SIG_IGN;
	(void) sigvec(SIGINT, &sv, (struct sigvec *)0);

	parentpid = getpid();

	if (verify) {
		if (doingverify)
			doingverify = 0;
		else
			Exit(X_VERIFY);
	}
restore_check_point:

	sv.sv_handler = interrupt;
	(void) sigvec(SIGINT, &sv, (struct sigvec *)0);
	(void) fflush(stderr);
	/*
	 *	All signals are inherited...
	 */
	sighold(SIGINT);
	childpid = fork();
	if (childpid < 0) {
		msg(gettext(
		    "Context-saving fork failed in parent %ld\n"),
		    (long)parentpid);
		Exit(X_ABORT);
	}
	if (childpid != 0) {
		/*
		 *	PARENT:
		 *	save the context by waiting
		 *	until the child doing all of the work returns.
		 *	let the child catch user interrupts
		 */
		sv.sv_handler = SIG_IGN;
		(void) sigvec(SIGINT, &sv, (struct sigvec *)0);
		sigrelse(SIGINT);
#ifdef TDEBUG

		/* XGETTEXT:  #ifdef TDEBUG only */
		msg(gettext(
		    "Volume: %d; parent process: %ld child process %ld\n"),
		    tapeno+1, (long)parentpid, (long)childpid);
#endif /* TDEBUG */
		for (;;) {
			waitproc = waitpid(0, &status, 0);
			if (waitproc == childpid)
				break;
			msg(gettext(
	"Parent %ld waiting for child %ld had another child %ld return\n"),
			    (long)parentpid, (long)childpid, (long)waitproc);
		}
		if (WIFSIGNALED(status)) {
			msg(gettext("Process %ld killed by signal %d: %s\n"),
			    (long)childpid, WTERMSIG(status),
			    strsignal(WTERMSIG(status)));
			status = X_ABORT;
		} else
			status = WEXITSTATUS(status);
#ifdef TDEBUG
		switch (status) {
		case X_FINOK:
			/* XGETTEXT:  #ifdef TDEBUG only */
			msg(gettext(
			    "Child %ld finishes X_FINOK\n"), (long)childpid);
			break;
		case X_ABORT:
			/* XGETTEXT:  #ifdef TDEBUG only */
			msg(gettext(
			    "Child %ld finishes X_ABORT\n"), (long)childpid);
			break;
		case X_REWRITE:
			/* XGETTEXT:  #ifdef TDEBUG only */
			msg(gettext(
			    "Child %ld finishes X_REWRITE\n"), (long)childpid);
			break;
		case X_RESTART:
			/* XGETTEXT:  #ifdef TDEBUG only */
			msg(gettext(
			    "Child %ld finishes X_RESTART\n"), (long)childpid);
			break;
		case X_VERIFY:
			/* XGETTEXT:  #ifdef TDEBUG only */
			msg(gettext(
			    "Child %ld finishes X_VERIFY\n"), (long)childpid);
			break;
		default:
			/* XGETTEXT:  #ifdef TDEBUG only */
			msg(gettext("Child %ld finishes unknown %d\n"),
			    (long)childpid, status);
			break;
		}
#endif /* TDEBUG */
		switch (status) {
		case X_FINOK:
			/* wait for children */
			while (waitpid(0, (int *)0, 0) >= 0)
				/*LINTED [empty body]*/
				continue;
			Exit(X_FINOK);
			/*NOTREACHED*/
		case X_ABORT:
			Exit(X_ABORT);
			/*NOTREACHED*/
		case X_VERIFY:
			doingverify++;
			goto restore_check_point;
			/*NOTREACHED*/
		case X_REWRITE:
			doingverify = 0;
			changevol();
			goto restore_check_point;
			/* NOTREACHED */
		case X_RESTART:
			doingverify = 0;
			if (!top) {
				Exit(X_RESTART);
			}
			if (!offline)
				autoload = 0;
			changevol();
			sv.sv_handler = interrupt;
			(void) sigvec(SIGINT, &sv, (struct sigvec *)0);
			return;
			/* NOTREACHED */
		default:
			msg(gettext("Bad return code from dump: %d\n"), status);
			Exit(X_ABORT);
			/*NOTREACHED*/
		}
		/*NOTREACHED*/
	} else {	/* we are the child; just continue */
		child_chdir();
		sigrelse(SIGINT);
#ifdef TDEBUG
		(void) sleep(4); /* time for parent's message to get out */
		/* XGETTEXT:  #ifdef TDEBUG only */
		msg(gettext(
		    "Child on Volume %d has parent %ld, my pid = %ld\n"),
		    tapeno+1, (long)parentpid, (long)getpid());
#endif
		(void) snprintf(buf, sizeof (buf), gettext(
"Cannot open `%s'.  Do you want to retry the open?: (\"yes\" or \"no\") "),
		    dumpdev);
		if (doingverify) {
			/* 1 for stdout */
			while ((to = host ? rmtopen(tape, O_RDONLY) :
			    pipeout ? 1 :
			    safe_device_open(tape, O_RDONLY, 0600)) < 0) {
				perror(tape);
				if (autoload) {
					if (!query_once(buf, 1)) {
						dumpabort();
						/*NOTREACHED*/
					}
				} else {
					if (!query(buf)) {
						dumpabort();
						/*NOTREACHED*/
					}
				}
			}

			/*
			 * If we're using the non-rewinding tape device,
			 * the tape will be left positioned after the
			 * EOF mark.  We need to back up to the beginning
			 * of this tape file (cross two tape marks in the
			 * reverse direction and one in the forward
			 * direction) before the verify pass.
			 */
			if (host) {
				if (rmtioctl(MTBSF, 2) >= 0)
					(void) rmtioctl(MTFSF, 1);
				else
					(void) rmtioctl(MTNBSF, 1);
			} else {
				static struct mtop bsf = { MTBSF, 2 };
				static struct mtop fsf = { MTFSF, 1 };
				static struct mtop nbsf = { MTNBSF, 1 };

				if (ioctl(to, MTIOCTOP, &bsf) >= 0)
					(void) ioctl(to, MTIOCTOP, &fsf);
				else
					(void) ioctl(to, MTIOCTOP, &nbsf);
			}
		} else {
			/*
			 * XXX Add logic to test for "tape" being a
			 * XXX device or a non-existent file.
			 * Current behaviour is that it must exist,
			 * and we over-write whatever's there.
			 * This can be bad if tape == "/etc/passwd".
			 */
			if (!pipeout && doposition && (tapeno == 0)) {
				positiontape(buf);
				if (setjmp(alrm_buf)) {
					/*
					 * The tape is rewinding;
					 * we're screwed.
					 */
					msg(gettext(
					    "Cannot position tape using "
					    "rewind device!\n"));
					dumpabort();
					/*NOTREACHED*/
				} else {
					sv.sv_handler = alrm;
					(void) sigvec(SIGALRM, &sv, &osv);
					(void) alarm(15);
				}
				while ((to = host ? rmtopen(tape, O_WRONLY) :
				    safe_device_open(tape, O_WRONLY, 0600)) < 0)
					(void) sleep(10);
				(void) alarm(0);
				(void) sigvec(SIGALRM, &osv,
				    (struct sigvec *)0);
			} else {
				int m;
				m = (access(tape, F_OK) == 0) ? 0 : O_CREAT;
				/*
				 * Only verify the tape label if label
				 * verification is on and we are at BOT
				 */
				if (pipeout)
					to = 1;
				else while ((to = host ?
				    rmtopen(tape, O_WRONLY) :
				    safe_device_open(tape, O_WRONLY|m, 0600))
				    < 0)
					if (!query_once(buf, 1)) {
						dumpabort();
						/*NOTREACHED*/
					}
			}
		}
		if (!pipeout) {
			tapeout = host ? rmtstatus(&mt) >= 0 :
			    ioctl(to, MTIOCGET, &mt) >= 0;	/* set state */
			/*
			 * Make sure the tape is positioned
			 * where it is supposed to be
			 */
			if (tapeout && (tapeno > 0) &&
			    (mt.mt_fileno != (filenum-1))) {
				(void) snprintf(buf, sizeof (buf), gettext(
				    "Warning - tape positioning error!\n\
\t%s current file %ld, should be %ld\n"),
				    tape, mt.mt_fileno+1, filenum);
				msg(buf);
				dumpailing();
			}
		}
		tapeno++;		/* current tape sequence */
		if (tapeno < TP_NINOS)
			inos[tapeno] = chkpt.sl_inos;
		spcl.c_firstrec = chkpt.sl_firstrec;
		spcl.c_tapea = (*tapea) = chkpt.sl_tapea;
		spcl.c_volume++;

		enslave();	/* Share tape buffers with slaves */

#ifdef DEBUG
		if (xflag) {
			/* XGETTEXT:  #ifdef DEBUG only */
			msg(gettext("Checkpoint state:\n"));
			msg("    blockswritten %u\n", blockswritten);
			msg("    ino %u\n", ino);
			msg("    pos %u\n", pos);
			msg("    left %u\n", leftover);
			msg("    tapea %u\n", (*tapea));
			msg("    state %d\n", dumpstate);
		}
#endif
		spcl.c_type = TS_TAPE;
		spcl.c_tpbsize = tp_bsize;
		if (leftover == 0) {
			spcl.c_count = 0;
			spclrec();
			newtape = 0;
		} else
			newtape++;	/* new volume indication */
		if (doingverify) {
			msg(gettext("Starting verify pass\n"));
		} else if (tapeno > 1) {
			msg(gettext(
			    "Volume %d begins with blocks from inode %lu\n"),
			    tapeno, chkpt.sl_inos);
		}
		(void) timeclock((time_t)1);
		(void) time(tstart_writing);
		timeest(0, spcl.c_tapea);
	}
}

void
dumpabort(void)
{

	if (master && master != getpid())
		/*
		 * signal master to call dumpabort
		 */
		(void) kill(master, SIGTERM);
	else {
		killall();

		if (archivefile && archive_opened)
			(void) unlink(archivefile);
		msg(gettext("The ENTIRE dump is aborted.\n"));
	}
	Exit(X_ABORT);
}

void
dumpailing(void)
{

	broadcast(gettext("DUMP IS AILING!\n"));
	if (!query(gettext(
	    "Do you want to attempt to continue? (\"yes\" or \"no\") "))) {
		dumpabort();
		/*NOTREACHED*/
	}
}

void
Exit(int status)
{
	/*
	 * Clean up message system
	 */
#ifdef TDEBUG

	/* XGETTEXT:  #ifdef TDEBUG only */
	msg(gettext("pid = %ld exits with status %d\n"),
	    (long)getpid(), status);
#endif /* TDEBUG */
	exit(status);
}

static void
killall(void)
{
	struct slaves *slavep;

	for (slavep = &slaves[0]; slavep < &slaves[SLAVES]; slavep++)
		if (slavep->sl_slavepid > 0) {
			(void) kill(slavep->sl_slavepid, SIGKILL);
#ifdef TDEBUG

			/* XGETTEXT:  #ifdef TDEBUG only */
			msg(gettext("Slave child %ld killed\n"),
			    (long)slavep->sl_slavepid);
#endif
		}
	if (writepid) {
		(void) kill(writepid, SIGKILL);
#ifdef TDEBUG

		/* XGETTEXT:  #ifdef TDEBUG only */
		msg(gettext("Writer child %ld killed\n"), (long)writepid);
#endif
	}
	if (archivepid) {
		(void) kill(archivepid, SIGKILL);
#ifdef TDEBUG

		/* XGETTEXT:  #ifdef TDEBUG only */
		msg(gettext("Archiver child %ld killed\n"), (long)archivepid);
#endif
	}
}

/*ARGSUSED*/
static void
proceed(int sig)
{
	caught++;
}

/*ARGSUSED*/
static void
die(int sig)
{
	Exit(X_FINOK);
}

static void
enslave(void)
{
	int cmd[2];			/* file descriptors */
	int i;
	struct sigvec sv;
	struct slaves *slavep;
	int saverr;

	sv.sv_flags = SA_RESTART;
	(void) sigemptyset(&sv.sa_mask);
	master = getpid();
	/*
	 * slave sends SIGTERM on dumpabort
	 */
	sv.sv_handler = (void(*)(int))dumpabort;
	(void) sigvec(SIGTERM, &sv, (struct sigvec *)0);
	sv.sv_handler = tperror;
	(void) sigvec(SIGUSR2, &sv, (struct sigvec *)0);
	sv.sv_handler = proceed;
	(void) sigvec(SIGUSR1, &sv, (struct sigvec *)0);
	totalrecsout += recsout;
	caught = 0;
	recsout = 0;
	rotor = 0;
	bufclear();
	for (slavep = &slaves[0]; slavep < &slaves[SLAVES]; slavep++)
		slavep->sl_slavefd = -1;
	archivefd = arch = writer = -1;
	for (i = 0; i < SLAVES; i++) {
		if (pipe(cmd) < 0) {
			saverr = errno;
			msg(gettext(
			    "Cannot create pipe for slave process: %s\n"),
			    strerror(saverr));
			dumpabort();
			/*NOTREACHED*/
		}
		sighold(SIGUSR2);
		sighold(SIGINT);
		sighold(SIGTERM);
		if ((slaves[i].sl_slavepid = fork()) < 0) {
			saverr = errno;
			msg(gettext("Cannot create slave process: %s\n"),
			    strerror(saverr));
			dumpabort();
			/*NOTREACHED*/
		}
		slaves[i].sl_slavefd = cmd[1];
		if (slaves[i].sl_slavepid == 0) {   /* Slave starts up here */
			pid_t next;		    /* pid of neighbor */

			sv.sv_handler = SIG_DFL;
			(void) sigvec(SIGUSR2, &sv, (struct sigvec *)0);
			sv.sv_handler = SIG_IGN;	/* master handler INT */
			(void) sigvec(SIGINT, &sv, (struct sigvec *)0);
			sv.sv_handler = die;		/* normal slave exit */
			(void) sigvec(SIGTERM, &sv, (struct sigvec *)0);

			child_chdir();
			sigrelse(SIGUSR2);
			sigrelse(SIGINT);
			sigrelse(SIGTERM);

			freeino();	/* release unneeded resources */
#ifdef TDEBUG
		(void) sleep(4); /* time for parent's message to get out */
		/* XGETTEXT:  #ifdef TDEBUG only */
		msg(gettext("Neighbor has pid = %ld\n"), (long)getpid());
#endif
			/* Closes cmd[1] as a side-effect */
			for (slavep = &slaves[0];
			    slavep < &slaves[SLAVES];
			    slavep++)
				if (slavep->sl_slavefd >= 0) {
					(void) close(slavep->sl_slavefd);
					slavep->sl_slavefd = -1;
				}
			(void) close(to);
			(void) close(fi);	    /* Need our own seek ptr */
			to = -1;

			fi = open(disk, O_RDONLY);

			if (fi < 0) {
				saverr = errno;
				msg(gettext(
				    "Cannot open dump device `%s': %s\n"),
				    disk, strerror(saverr));
				dumpabort();
				/*NOTREACHED*/
			}

			if ((unsigned)atomic((int(*)())read, cmd[0],
			    (char *)&next, sizeof (next)) != sizeof (next)) {
				cmdrderr();
				dumpabort();
				/*NOTREACHED*/
			}
			dumpoffline(cmd[0], next, i);
			Exit(X_FINOK);
		}
		/* Parent continues here */
		sigrelse(SIGUSR2);
		sigrelse(SIGINT);
		sigrelse(SIGTERM);
		(void) close(cmd[0]);
	}

	if (archive) {
		archivepid = setuparchive();
		if (!archivepid) {
			dumpabort();
			/*NOTREACHED*/
		}
	}

	writepid = setupwriter();
	if (!writepid) {
		dumpabort();
		/*NOTREACHED*/
	}

	if (arch >= 0) {
		(void) close(arch);		/* only writer has this open */
		arch = -1;
	}

	/* Tell each slave who follows it */
	for (i = 0; i < SLAVES; i++) {
		if ((unsigned)atomic((int(*)())write, slaves[i].sl_slavefd,
		    (char *)&(slaves[(i + 1) % SLAVES].sl_slavepid),
		    sizeof (int)) != sizeof (int)) {
			cmdwrterr();
			dumpabort();
			/*NOTREACHED*/
		}
	}
	sv.sv_handler = rollforward;		/* rcvd from writer on EOT */
	(void) sigvec(SIGUSR1, &sv, (struct sigvec *)0);
	slp = slaves;
	(void) kill(slp->sl_slavepid, SIGUSR1);
	master = 0;
}

static void
wait_our_turn(void)
{
	(void) sighold(SIGUSR1);

	if (!caught) {
#ifdef INSTRUMENT
		(*idle)++;
#endif
		(void) sigpause(SIGUSR1);
	}
	caught = 0;
	(void) sigrelse(SIGUSR1);
}

static void
dumpoffline(int cmd, pid_t next, int mynum)
{
	struct req *p = slaves[mynum].sl_req;
	ulong_t i;
	uchar_t *cp;
	uchar_t *blkbuf;
	int notactive = 0;

	blkbuf = xmalloc(sblock->fs_bsize);

	/*CONSTANTCONDITION*/
	assert(sizeof (spcl) == TP_BSIZE_MIN);

	while (atomic((int(*)())read, cmd, (char *)p, reqsiz) == reqsiz) {
		if (p->br_dblk) {
			bread(p->br_dblk, (uchar_t *)blkbuf, p->br_size);
		} else {
			bcopy((char *)p->br_spcl, (char *)&spcl,
			    sizeof (spcl));
			ino = spcl.c_inumber;
		}
		dumptoarchive = p->aflag & BUF_ARCHIVE;
		wait_our_turn();
		if (p->br_dblk) {
			for (i = p->br_size, cp = blkbuf;
			    i > 0;
			    /* LINTED character pointers aren't signed */
			    cp += i > tp_bsize ? tp_bsize : i,
			    i -= i > tp_bsize ? tp_bsize : i) {
				/* LINTED unsigned to signed conversion ok */
				taprec(cp, 0, i > tp_bsize ? tp_bsize : (int)i);
			}
		} else
			spclrec();
		(void) kill(next, SIGUSR1);	/* Next slave's turn */
		/*
		 * Note that we lie about file activity since we don't
		 * check for it.
		 */
		if ((unsigned)atomic((int(*)())write, cmd, (char *)&notactive,
		    sizeof (notactive)) != sizeof (notactive)) {
			cmdwrterr();
			dumpabort();
			/*NOTREACHED*/
		}
	}

	free(blkbuf);
}

static int count;		/* tape blocks written since last spclrec */

/*ARGSUSED*/
static void
onxfsz(int sig)
{
	msg(gettext("File size limit exceeded writing output volume %d\n"),
	    tapeno);
	(void) kill(master, SIGUSR2);
	Exit(X_REWRITE);
}

static long	lastnonaddr;		/* last DS_{INODE,CLRI,BITS} written */
static long	lastnonaddrm;		/* and the mode thereof */
/*
 * dowrite -- the main body of the output writer process
 */
static void
dowrite(int cmd)
{
	struct bdesc *last =
	    &bufp[(NBUF*ntrec)-1];		/* last buffer in pool */
	struct bdesc *bp = bufp;		/* current buf in tape block */
	struct bdesc *begin = bufp;		/* first buf of tape block */
	struct bdesc *end = bufp + (ntrec-1);	/* last buf of tape block */
	int siz;				/* bytes written (block) */
	int trecs;				/* records written (block)  */
	long asize = 0;				/* number of 0.1" units... */
						/* ...written on current tape */
	char *tp, *rbuf = NULL;
	char *recmap = spcl.c_addr;		/* current tape record map */
	char *endmp;				/* end of valid map data */
	char *mp;				/* current map entry */
	union u_spcl *sp;

	(void) signal(SIGXFSZ, onxfsz);

	bzero((char *)&spcl, sizeof (spcl));
	count = 0;

	if (doingverify) {
		rbuf = (char *)malloc((uint_t)writesize);
		if (rbuf == 0) {
			/* Restart from checkpoint */
			(void) kill(master, SIGUSR2);
			Exit(X_REWRITE);
		}
	}

	for (;;) {
		/* START: wait until all buffers in tape block are full */
		if ((bp->b_flags & BUF_FULL) == 0) {
			if (caught) {		/* master signalled flush */
				(void) sighold(SIGUSR1);
				caught = 0;
				/* signal ready */
				(void) kill(master, SIGUSR1);
				chkpt.sl_count = 0;	/* signal not at EOT */
				checkpoint(bp-1, cmd);	/* send data */
				(void) sigpause(SIGUSR1);
				break;
			}
#ifdef INSTRUMENT
			(*readmissp)++;
#endif
			nap(50);
			continue;
		}
		if (bp < end) {
			bp++;
			continue;
		}
		/* END: wait until all buffers in tape block are full */

		tp = begin->b_data;
		(void) sighold(SIGUSR1);
		if (host) {
			if (!doingverify)
				siz = rmtwrite(tp, writesize);
			else if ((siz = rmtread(rbuf, writesize)) ==
			    writesize && bcmp(rbuf, tp, writesize))
				siz = -1;
		} else {
			if (!doingverify)
				siz = write(to, tp, writesize);
			else if ((siz = read(to, rbuf, writesize)) ==
			    writesize && bcmp(rbuf, tp, writesize))
				siz = -1;
			if (siz < 0 && diskette && errno == ENOSPC)
				siz = 0;	/* really EOF */
		}
		(void) sigrelse(SIGUSR1);
		if (siz < 0 ||
		    (pipeout && siz != writesize)) {
			char buf[3000];

			/*
			 * Isn't i18n wonderful?
			 */
			if (doingverify) {
				if (diskette)
					(void) snprintf(buf, sizeof (buf),
					    gettext(
		    "Verification error %ld blocks into diskette %d\n"),
					    asize * 2, tapeno);
				else if (tapeout)
					(void) snprintf(buf, sizeof (buf),
					    gettext(
		    "Verification error %ld feet into tape %d\n"),
					    (cartridge ? asize/tracks :
					    asize)/120L,
					    tapeno);
				else
					(void) snprintf(buf, sizeof (buf),
					    gettext(
		    "Verification error %ld blocks into volume %d\n"),
					    asize * 2, tapeno);

			} else {
				if (diskette)
					(void) snprintf(buf, sizeof (buf),
					    gettext(
			"Write error %ld blocks into diskette %d\n"),
					    asize * 2, tapeno);
				else if (tapeout)
					(void) snprintf(buf, sizeof (buf),
					    gettext(
			"Write error %ld feet into tape %d\n"),
					    (cartridge ? asize/tracks :
					    asize)/120L, tapeno);
				else
					(void) snprintf(buf, sizeof (buf),
					    gettext(
			"Write error %ld blocks into volume %d\n"),
					    asize * 2, tapeno);
			}

			msg(buf);
			/* Restart from checkpoint */
#ifdef TDEBUG

			/* XGETTEXT:  #ifdef TDEBUG only */
			msg(gettext("sending SIGUSR2 to pid %ld\n"), master);
#endif
			(void) kill(master, SIGUSR2);
			Exit(X_REWRITE);
		}
		trecs = siz / tp_bsize;
		if (diskette)
			asize += trecs;	/* asize == blocks written */
		else
			asize += (siz/density + tenthsperirg);
		if (trecs)
			chkpt.sl_firstrec++;
		for (bp = begin; bp < begin + trecs; bp++) {
			if ((arch >= 0) && (bp->b_flags & BUF_ARCHIVE)) {
				if ((unsigned)atomic((int(*)())write, arch,
				    (char *)&bp->b_flags, sizeof (bp->b_flags))
				    != sizeof (bp->b_flags)) {
					cmdwrterr();
					dumpabort();
					/*NOTREACHED*/
				}
				if (atomic((int(*)())write, arch, bp->b_data,
				    tp_bsize) != tp_bsize) {
					cmdwrterr();
					dumpabort();
					/*NOTREACHED*/
				}
			}
			if (bp->b_flags & BUF_SPCLREC) {
				/*LINTED [bp->b_data is aligned]*/
				sp = (union u_spcl *)bp->b_data;
				if (sp->s_spcl.c_type != TS_ADDR) {
					lastnonaddr = sp->s_spcl.c_type;
					lastnonaddrm =
					    sp->s_spcl.c_dinode.di_mode;
					if (sp->s_spcl.c_type != TS_TAPE)
						chkpt.sl_offset = 0;
				}
				chkpt.sl_count = sp->s_spcl.c_count;
				bcopy((char *)sp, (char *)&spcl, sizeof (spcl));
				mp = recmap;
				endmp = &recmap[spcl.c_count];
				count = 0;
			} else {
				chkpt.sl_offset++;
				chkpt.sl_count--;
				count++;
				mp++;
			}
			/*
			 * Adjust for contiguous hole
			 */
			for (; mp < endmp; mp++) {
				if (*mp)
					break;
				chkpt.sl_offset++;
				chkpt.sl_count--;
			}
		}
		/*
		 * Check for end of tape
		 */
		if (trecs < ntrec ||
		    (!pipeout && tsize > 0 && asize > tsize)) {
			if (tapeout)
				msg(gettext("End-of-tape detected\n"));
			else
				msg(gettext("End-of-file detected\n"));
			(void) sighold(SIGUSR1);
			caught = 0;
			(void) kill(master, SIGUSR1);	/* signal EOT */
			checkpoint(--bp, cmd);	/* send checkpoint data */
			(void) sigpause(SIGUSR1);
			break;
		}
		for (bp = begin; bp <= end; bp++)
			bp->b_flags = BUF_EMPTY;
		if (end + ntrec > last) {
			bp = begin = bufp;
			timeest(0, spcl.c_tapea);
		} else
			bp = begin = end+1;
		end = begin + (ntrec-1);
	}

	if (rbuf != NULL)
		free(rbuf);
}

/*
 * Send checkpoint info back to master.  This information
 * consists of the current inode number, number of logical
 * blocks written for that inode (or bitmap), the last logical
 * block number written, the number of logical blocks written
 * to this volume, the current dump state, and the current
 * special record map.
 */
static void
checkpoint(struct bdesc *bp, int cmd)
{
	int	state, type;
	ino_t	ino;

	if (++bp >= &bufp[NBUF*ntrec])
		bp = bufp;

	/*
	 * If we are dumping files and the record following
	 * the last written to tape is a special record, use
	 * it to get an accurate indication of current state.
	 */
	if ((bp->b_flags & BUF_SPCLREC) && (bp->b_flags & BUF_FULL) &&
	    lastnonaddr == TS_INODE) {
		/*LINTED [bp->b_data is aligned]*/
		union u_spcl *nextspcl = (union u_spcl *)bp->b_data;

		if (nextspcl->s_spcl.c_type == TS_INODE) {
			chkpt.sl_offset = 0;
			chkpt.sl_count = 0;
		} else if (nextspcl->s_spcl.c_type == TS_END) {
			chkpt.sl_offset = 0;
			chkpt.sl_count = 1;	/* EOT indicator */
		}
		ino = nextspcl->s_spcl.c_inumber;
		type = nextspcl->s_spcl.c_type;
	} else {
		/*
		 * If not, use what we have.
		 */
		ino = spcl.c_inumber;
		type = spcl.c_type;
	}

	switch (type) {		/* set output state */
	case TS_ADDR:
		switch (lastnonaddr) {
		case TS_INODE:
		case TS_TAPE:
			if ((lastnonaddrm & IFMT) == IFDIR ||
			    (lastnonaddrm & IFMT) == IFATTRDIR)
				state = DS_DIRS;
			else
				state = DS_FILES;
			break;
		case TS_CLRI:
			state = DS_CLRI;
			break;
		case TS_BITS:
			state = DS_BITS;
			break;
		}
		break;
	case TS_INODE:
		if ((spcl.c_dinode.di_mode & IFMT) == IFDIR ||
		    (spcl.c_dinode.di_mode & IFMT) == IFATTRDIR)
			state = DS_DIRS;
		else
			state = DS_FILES;
		break;
	case 0:			/* EOT on 1st record */
	case TS_TAPE:
		state = DS_START;
		ino = UFSROOTINO;
		break;
	case TS_CLRI:
		state = DS_CLRI;
		break;
	case TS_BITS:
		state = DS_BITS;
		break;
	case TS_END:
		if (spcl.c_type == TS_END)
			state = DS_DONE;
		else
			state = DS_END;
		break;
	}

	/*
	 * Checkpoint info to be processed by rollforward():
	 *	The inode with which the next volume should begin
	 *	The last inode number on this volume
	 *	The last logical block number on this volume
	 *	The current output state
	 *	The offset within the current inode (already in sl_offset)
	 *	The number of records left from last spclrec (in sl_count)
	 *	The physical block the next vol begins with (in sl_firstrec)
	 */
	chkpt.sl_inos = ino;
	chkpt.sl_tapea = spcl.c_tapea + count;
	chkpt.sl_state = state;

	if ((unsigned)atomic((int(*)())write, cmd, (char *)&chkpt,
	    sizeof (chkpt)) != sizeof (chkpt)) {
		cmdwrterr();
		dumpabort();
		/*NOTREACHED*/
	}
	if ((unsigned)atomic((int(*)())write, cmd, (char *)&spcl,
	    sizeof (spcl)) != sizeof (spcl)) {
		cmdwrterr();
		dumpabort();
		/*NOTREACHED*/
	}
#ifdef DEBUG
	if (xflag) {
		/* XGETTEXT:  #ifdef DEBUG only */
		msg(gettext("sent chkpt to master:\n"));
		msg("    ino %u\n", chkpt.sl_inos);
		msg("    1strec %u\n", chkpt.sl_firstrec);
		msg("    lastrec %u\n", chkpt.sl_tapea);
		msg("    written %u\n", chkpt.sl_offset);
		msg("    left %u\n", chkpt.sl_count);
		msg("    state %d\n", chkpt.sl_state);
	}
#endif
}

/*
 * Since a read from a pipe may not return all we asked for,
 * or a write may not write all we ask if we get a signal,
 * loop until the count is satisfied (or error).
 */
static ssize_t
atomic(int (*func)(), int fd, char *buf, int count)
{
	ssize_t got = 0, need = count;

	/* don't inherit random value if immediately get zero back from func */
	errno = 0;
	while (need > 0) {
		got = (*func)(fd, buf, MIN(need, 4096));
		if (got < 0 && errno == EINTR)
			continue;
		if (got <= 0)
			break;
		buf += got;
		need -= got;
	}
	/* if we got what was asked for, return count, else failure (got) */
	return ((need != 0) ? got : count);
}

void
positiontape(char *msgbuf)
{
	/* Static as never change, no need to waste stack space */
	static struct mtget mt;
	static struct mtop rew = { MTREW, 1 };
	static struct mtop fsf = { MTFSF, 1 };
	char *info = strdup(gettext("Positioning `%s' to file %ld\n"));
	char *fail = strdup(gettext("Cannot position tape to file %d\n"));
	int m;

	/* gettext()'s return value is volatile, hence the strdup()s */

	m = (access(tape, F_OK) == 0) ? 0 : O_CREAT;

	/*
	 * To avoid writing tape marks at inappropriate places, we open the
	 * device read-only, position it, close it, and reopen it for writing.
	 */
	while ((to = host ? rmtopen(tape, O_RDONLY) :
	    safe_device_open(tape, O_RDONLY|m, 0600)) < 0) {
		if (autoload) {
			if (!query_once(msgbuf, 1)) {
				dumpabort();
				/*NOTREACHED*/
			}
		} else {
			if (!query(msgbuf)) {
				dumpabort();
				/*NOTREACHED*/
			}
		}
	}

	if (host) {
		if (rmtstatus(&mt) >= 0 &&
		    rmtioctl(MTREW, 1) >= 0 &&
		    filenum > 1) {
			msg(info, dumpdev, filenum);
			if (rmtioctl(MTFSF, filenum-1) < 0) {
				msg(fail, filenum);
				dumpabort();
				/*NOTREACHED*/
			}
		}
		rmtclose();
	} else {
		if (ioctl(to, MTIOCGET, &mt) >= 0 &&
		    ioctl(to, MTIOCTOP, &rew) >= 0 &&
		    filenum > 1) {
			msg(info, dumpdev, filenum);
			fsf.mt_count = filenum - 1;
			if (ioctl(to, MTIOCTOP, &fsf) < 0) {
				msg(fail, filenum);
				dumpabort();
				/*NOTREACHED*/
			}
		}
		(void) close(to);
		to = -1;
	}

	free(info);
	free(fail);
}

static void
cmdwrterr(void)
{
	int saverr = errno;
	msg(gettext("Error writing command pipe: %s\n"), strerror(saverr));
}

static void
cmdrderr(void)
{
	int saverr = errno;
	msg(gettext("Error reading command pipe: %s\n"), strerror(saverr));
}
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/

/*
 * Copyright (c) 1980 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#include "dump.h"
#include <sys/file.h>
#include <sys/mman.h>

static void lf_dmpindir(daddr32_t, int, u_offset_t *);
static void indir(daddr32_t, int, u_offset_t *);
static void lf_blksout(daddr32_t *, u_offset_t);
static void lf_dumpinode(struct dinode *);
static void dsrch(daddr32_t, ulong_t, u_offset_t);
void lf_dump(struct dinode *);

int	dadded;
int	nsubdir;
int	shortmeta;
static	char msgbuf[256];

void
pass(void (*fn)(struct dinode *), uchar_t *map)
{
	int bits;
	ino_t maxino;

	maxino = (unsigned)(sblock->fs_ipg * sblock->fs_ncg - 1);
	/*
	 * Handle pass restarts.  We don't check for UFSROOTINO just in
	 * case we need to restart on the root inode.
	 */
	if (ino != 0) {
		bits = ~0;
		if (map != NULL) {
			/* LINTED: lint seems to think map is signed */
			map += (ino / NBBY);
			bits = *map++;
		}
		bits >>= (ino % NBBY);
		resetino(ino);
		goto restart;
	}
	while (ino < maxino) {
		if ((ino % NBBY) == 0) {
			bits = ~0;
			if (map != NULL)
				bits = *map++;
		}
restart:
		ino++;
		/*
		 * Ignore any inode less than UFSROOTINO and inodes that
		 * we have already done on a previous pass.
		 */
		if ((ino >= UFSROOTINO) && (bits & 1)) {
			/*
			 * The following test is merely an optimization
			 * for common case where "add" will just return.
			 */
			if (!(fn == add && BIT(ino, nodmap)))
				(*fn)(getino(ino));
		}
		bits >>= 1;
	}
}

void
mark(struct dinode *ip)
{
	int f;

	f = ip->di_mode & IFMT;
	if (f == 0 || ip->di_nlink <= 0) {
		/* LINTED: 32-bit to 8-bit assignment ok */
		BIC(ino, clrmap);
		return;
	}
	/* LINTED: 32-bit to 8-bit assignment ok */
	BIS(ino, clrmap);
	if (f == IFDIR || f == IFATTRDIR) {
		/* LINTED: 32-bit to 8-bit assignment ok */
		BIS(ino, dirmap);
	}
	if (ip->di_ctime >= spcl.c_ddate) {
		if (f == IFSHAD)
			return;
		/* LINTED: 32-bit to 8-bit assignment ok */
		BIS(ino, nodmap);
		/* attribute changes impact the root */
		if (f == IFATTRDIR)
			BIS(UFSROOTINO, nodmap);
		if (f != IFREG && f != IFDIR && f != IFATTRDIR && f != IFLNK) {
			o_esize += 1;
			return;
		}
		est(ip);
	}
}

void
active_mark(struct dinode *ip)
{
	int f;

	f = ip->di_mode & IFMT;
	if (f == 0 || ip->di_nlink <= 0) {
		/* LINTED: 32-bit to 8-bit assignment ok */
		BIC(ino, clrmap);
		return;
	}
	/* LINTED: 32-bit to 8-bit assignment ok */
	BIS(ino, clrmap);
	if (f == IFDIR || f == IFATTRDIR) {
		/* LINTED: 32-bit to 8-bit assignment ok */
		BIS(ino, dirmap);
	}
	if (BIT(ino, activemap)) {
		/* LINTED: 32-bit to 8-bit assignment ok */
		BIS(ino, nodmap);
		/* attribute changes impact the root */
		if (f == IFATTRDIR)
			BIS(UFSROOTINO, nodmap);
		if (f != IFREG && f != IFDIR && f != IFATTRDIR && f != IFLNK) {
			o_esize += 1;
			return;
		}
		est(ip);
	}
}

static struct shcount {
	struct shcount *higher, *lower;
	ino_t ino;
	unsigned long count;
} shcounts = {
	NULL, NULL,
	0,
	0
};
static struct shcount *shc = NULL;

void
markshad(struct dinode *ip)
{
	ino_t shadow;

	if (ip->di_shadow == 0)
		return;
	if (shc == NULL)
		shc = &shcounts;

	shadow = (ino_t)(unsigned)(ip->di_shadow);
	while ((shadow > shc->ino) && (shc->higher))
		shc = shc->higher;
	while ((shadow < shc->ino) && (shc->lower))
		shc = shc->lower;
	if (shadow != shc->ino) {
		struct shcount *new;

		new = (struct shcount *)xcalloc(1, sizeof (*new));
		new->higher = shc->higher;
		if (shc->higher != NULL)
			shc->higher->lower = new;
		shc->higher = new;
		new->lower = shc;
		shc = new;
		shc->ino = shadow;
	}

	/* LINTED: 32-bit to 8-bit assignment ok */
	BIS(shadow, shamap);
	shc->count++;
}

void
estshad(struct dinode *ip)
{
	u_offset_t esizeprime;
	u_offset_t tmpesize;

	if (ip->di_size <= sizeof (union u_shadow))
		return;

	while ((ino > shc->ino) && (shc->higher))
		shc = shc->higher;
	while ((ino < shc->ino) && (shc->lower))
		shc = shc->lower;
	if (ino != shc->ino)
		return; /* xxx panic? complain? */

	tmpesize = (o_esize + f_esize);
	esizeprime = tmpesize;
	est(ip);
	esizeprime = tmpesize - esizeprime;
	esizeprime *= shc->count - 1;
	f_esize += esizeprime;
}

void
freeshad()
{
	if (shc == NULL)
		return;

	while (shc->higher)
		shc = shc->higher;
	while (shc->lower) {
		shc = shc->lower;
		if (shc->higher) /* else panic? */
			(void) free(shc->higher);
	}
	/*
	 * This should be unnecessary, but do it just to be safe.
	 * Note that shc might be malloc'd or static, so can't free().
	 */
	bzero(shc, sizeof (*shc));
}

void
add(struct dinode *ip)
{
	int i;
	u_offset_t filesize;

	if (BIT(ino, nodmap))
		return;
	if ((ip->di_mode & IFMT) != IFDIR &&
	    (ip->di_mode & IFMT) != IFATTRDIR) {
		(void) snprintf(msgbuf, sizeof (msgbuf), gettext(
		    "Warning - directory at inode `%lu' vanished!\n"), ino);
		msg(msgbuf);
		/* LINTED: 32-bit to 8-bit assignment ok */
		BIC(ino, dirmap);
		return;
	}
	nsubdir = 0;
	dadded = 0;
	filesize = ip->di_size;
	for (i = 0; i < NDADDR; i++) {
		if (ip->di_db[i] != 0)
			/* LINTED dblksize/blkoff does a safe cast here */
			dsrch(ip->di_db[i], (ulong_t)dblksize(sblock, ip, i),
			    filesize);
		filesize -= (unsigned)(sblock->fs_bsize);
	}
	for (i = 0; i < NIADDR; i++) {
		if (ip->di_ib[i] != 0)
			indir(ip->di_ib[i], i, &filesize);
	}
	if (dadded) {
		nadded++;
		if (!BIT(ino, nodmap)) {
			/* LINTED: 32-bit to 8-bit assignment ok */
			BIS(ino, nodmap);
			if ((ip->di_mode & IFMT) == IFATTRDIR) {
				/* attribute changes "auto-percolate" to root */
				BIS(UFSROOTINO, nodmap);
			}
			est(ip);
		}
	}
	if (nsubdir == 0) {
		if (!BIT(ino, nodmap)) {
			/* LINTED: 32-bit to 8-bit assignment ok */
			BIC(ino, dirmap);
		}
	}
}

static void
indir(daddr32_t d, int n, u_offset_t *filesize)
{
	int i;
	daddr32_t idblk[MAXNINDIR];

	if ((unsigned)(sblock->fs_bsize) > sizeof (idblk)) {
		msg(gettext(
"Inconsistency detected: filesystem block size larger than valid maximum.\n"));
		dumpabort();
		/*NOTREACHED*/
	}

	if ((unsigned)NINDIR(sblock) > MAXNINDIR) {
		/*CSTYLED*/
		msg(gettext(
"Inconsistency detected: inode has more indirect \
blocks than valid maximum.\n"));
		dumpabort();
		/*NOTREACHED*/
	}

	if (dadded || *filesize == 0)
		return;

#ifdef	lint
	idblk[0] = '\0';
#endif	/* lint */

	/* xxx sanity check sblock contents before trusting them */
	bread(fsbtodb(sblock, d), (uchar_t *)idblk, (size_t)sblock->fs_bsize);
	if (n <= 0) {
		for (i = 0; i < NINDIR(sblock); i++) {
			d = idblk[i];
			if (d != 0)
				dsrch(d, (ulong_t)(uint32_t)sblock->fs_bsize,
				    *filesize);
			*filesize -= (unsigned)(sblock->fs_bsize);
		}
	} else {
		n--;
		for (i = 0; i < NINDIR(sblock); i++) {
			d = idblk[i];
			if (d != 0)
				indir(d, n, filesize);
		}
	}
}

void
dirdump(struct dinode *ip)
{
	/* watchout for dir inodes deleted and maybe reallocated */
	if (((ip->di_mode & IFMT) != IFDIR &&
	    (ip->di_mode & IFMT) != IFATTRDIR) || ip->di_nlink < 2) {
		(void) snprintf(msgbuf, sizeof (msgbuf), gettext(
		    "Warning - directory at inode `%lu' vanished!\n"), ino);
		msg(msgbuf);
		return;
	}
	lf_dump(ip);
}

static u_offset_t loffset; /* current offset in file (ufsdump) */

static void
lf_dumpmeta(struct dinode *ip)
{
	if ((ip->di_shadow == 0) || shortmeta)
		return;

	lf_dumpinode(getino((ino_t)(unsigned)(ip->di_shadow)));
}

int
hasshortmeta(struct dinode **ip)
{
	ino_t savino;
	int rc;

	if ((*ip)->di_shadow == 0)
		return (0);
	savino = ino;
	*ip = getino((ino_t)(unsigned)((*ip)->di_shadow));
	rc = ((*ip)->di_size <= sizeof (union u_shadow));
	*ip = getino(ino = savino);
	return (rc);
}

void
lf_dumpinode(struct dinode *ip)
{
	int i;
	u_offset_t size;

	i = ip->di_mode & IFMT;

	if (i == 0 || ip->di_nlink <= 0)
		return;

	spcl.c_dinode = *ip;
	spcl.c_count = 0;

	if ((i != IFDIR && i != IFATTRDIR && i != IFREG && i != IFLNK &&
	    i != IFSHAD) || ip->di_size == 0) {
		toslave(dospcl, ino);
		return;
	}

	size = NDADDR * (unsigned)(sblock->fs_bsize);
	if (size > ip->di_size)
		size = ip->di_size;

	lf_blksout(&ip->di_db[0], size);

	size = ip->di_size - size;
	if (size > 0) {
		for (i = 0; i < NIADDR; i++) {
			lf_dmpindir(ip->di_ib[i], i, &size);
			if (size == 0)
				break;
		}
	}
}

void
lf_dump(struct dinode *ip)
{

	if ((!BIT(ino, nodmap)) && (!BIT(ino, shamap)))
		return;

	shortmeta = hasshortmeta(&ip);
	if (shortmeta) {
		ip = getino((ino_t)(unsigned)(ip->di_shadow));
		/* assume spcl.c_shadow is smaller than 1 block */
		bread(fsbtodb(sblock, ip->di_db[0]),
		    (uchar_t *)spcl.c_shadow.c_shadow, sizeof (spcl.c_shadow));
		spcl.c_flags |= DR_HASMETA;
	} else {
		spcl.c_flags &= ~DR_HASMETA;
	}
	ip = getino(ino);

	loffset = 0;

	if (newtape) {
		spcl.c_type = TS_TAPE;
	} else if (pos)
		spcl.c_type = TS_ADDR;
	else
		spcl.c_type = TS_INODE;

	newtape = 0;
	lf_dumpinode(ip);
	lf_dumpmeta(ip);
	pos = 0;
}

static void
lf_dmpindir(daddr32_t blk, int lvl, u_offset_t *size)
{
	int i;
	u_offset_t cnt;
	daddr32_t idblk[MAXNINDIR];

	if ((unsigned)(sblock->fs_bsize) > sizeof (idblk)) {
		msg(gettext(
"Inconsistency detected: filesystem block size larger than valid maximum.\n"));
		dumpabort();
		/*NOTREACHED*/
	}

	if ((unsigned)NINDIR(sblock) > MAXNINDIR) {
		msg(gettext(
"Inconsistency detected: inode has more indirect \
blocks than valid maximum.\n"));
		dumpabort();
		/*NOTREACHED*/
	}

	if (blk != 0)
		bread(fsbtodb(sblock, blk), (uchar_t *)idblk,
		    (size_t)sblock->fs_bsize);
	else
		bzero((char *)idblk, (size_t)sblock->fs_bsize);
	if (lvl <= 0) {
		cnt = (u_offset_t)(unsigned)NINDIR(sblock) *
		    (u_offset_t)(unsigned)(sblock->fs_bsize);
		if (cnt > *size)
			cnt = *size;
		*size -= cnt;
		lf_blksout(&idblk[0], cnt);
		return;
	}
	lvl--;
	for (i = 0; i < NINDIR(sblock); i++) {
		lf_dmpindir(idblk[i], lvl, size);
		if (*size == 0)
			return;
	}
}

static void
lf_blksout(daddr32_t *blkp, u_offset_t bytes)
{
	u_offset_t i;
	u_offset_t tbperfsb = (unsigned)(sblock->fs_bsize / tp_bsize);

	u_offset_t j, k, count;

	u_offset_t bytepos, diff;
	u_offset_t bytecnt = 0;
	off_t byteoff = 0;	/* bytes to skip within first f/s block */
	off_t fragoff = 0;	/* frags to skip within first f/s block */

	u_offset_t tpblkoff = 0; /* tape blocks to skip in first f/s block */
	u_offset_t tpblkskip = 0;	/* total tape blocks to skip  */
	u_offset_t skip;		/* tape blocks to skip this pass */

	if (pos) {
		/*
		 * We get here if a slave throws a signal to the
		 * master indicating a partially dumped file.
		 * Begin by figuring out what was undone.
		 */
		bytepos = (offset_t)pos * tp_bsize;

		if ((loffset + bytes) <= bytepos) {
			/* This stuff was dumped already, forget it. */
			loffset += (u_offset_t)tp_bsize *
			    /* LINTED: spurious complaint on sign-extending */
			    d_howmany(bytes, (u_offset_t)tp_bsize);
			return;
		}

		if (loffset < bytepos) {
			/*
			 * Some of this was dumped, some wasn't.
			 * Figure out what was done and skip it.
			 */
			diff = bytepos - loffset;
			/* LINTED: spurious complaint on sign-extending */
			tpblkskip = d_howmany(diff, (u_offset_t)tp_bsize);
			/* LINTED room after EOT is only a few MB */
			blkp += (int)(diff / sblock->fs_bsize);

			bytecnt = diff % (unsigned)(sblock->fs_bsize);
			/* LINTED: result fits, due to modulus */
			byteoff = bytecnt % (off_t)(sblock->fs_fsize);
			/* LINTED: spurious complaint on sign-extending */
			tpblkoff = d_howmany(bytecnt,
			    (u_offset_t)(unsigned)tp_bsize);
			/* LINTED: result fits, due to modulus */
			fragoff = bytecnt / (off_t)(sblock->fs_fsize);
			bytecnt = (unsigned)(sblock->fs_bsize) - bytecnt;
		}
	}

	loffset += bytes;

	while (bytes > 0) {
		if (bytes < TP_NINDIR*tp_bsize)
			/* LINTED: spurious complaint on sign-extending */
			count = d_howmany(bytes, (u_offset_t)tp_bsize);
		else
			count = TP_NINDIR;
		if (tpblkskip) {
			if (tpblkskip < TP_NINDIR) {
				bytes -= (tpblkskip * (u_offset_t)tp_bsize);
				skip = tpblkskip;
				tpblkskip = 0;
			} else {
				bytes -= (offset_t)TP_NINDIR*tp_bsize;
				tpblkskip -= TP_NINDIR;
				continue;
			}
		} else
			skip = 0;
		assert(tbperfsb >= tpblkoff);
		assert((count - skip) <= TP_NINDIR);
		for (j = 0, k = 0; j < count - skip; j++, k++) {
			spcl.c_addr[j] = (blkp[k] != 0);
			for (i = tbperfsb - tpblkoff; --i > 0; j++)
				spcl.c_addr[j+1] = spcl.c_addr[j];
			tpblkoff = 0;
		}
		/* LINTED (count - skip) will always fit into an int32_t */
		spcl.c_count = count - skip;
		toslave(dospcl, ino);
		bytecnt = MIN(bytes, bytecnt ?
		    bytecnt : (unsigned)(sblock->fs_bsize));
		j = 0;
		while (j < count - skip) {
			if (*blkp != 0) {
				/* LINTED: fragoff fits into 32 bits */
				dmpblk(*blkp+(int32_t)fragoff,
				    /* LINTED: bytecnt fits into 32 bits */
				    (size_t)bytecnt, byteoff);
			}
			blkp++;
			bytes -= bytecnt;
			/* LINTED: spurious complaint on sign-extending */
			j += d_howmany(bytecnt, (u_offset_t)tp_bsize);
			bytecnt = MIN(bytes, (unsigned)(sblock->fs_bsize));
			byteoff = 0;
			fragoff = 0;
		}
		spcl.c_type = TS_ADDR;
		bytecnt = 0;
	}
	pos = 0;
}

void
bitmap(uchar_t *map, int typ)
{
	int i;
	u_offset_t count;
	uchar_t *cp;

	if (!newtape)
		spcl.c_type = typ;
	else
		newtape = 0;
	for (i = 0; i < TP_NINDIR; i++)
		spcl.c_addr[i] = 1;
	/* LINTED: spurious complaint on sign-extending */
	count = d_howmany(msiz * sizeof (map[0]), tp_bsize) - pos;
	for (cp = &map[pos * tp_bsize]; count > 0;
	    count -= (u_offset_t)(unsigned)spcl.c_count) {
		if (leftover) {
			spcl.c_count = leftover;
			leftover = 0;
		} else {
			/* LINTED value always less than INT32_MAX */
			spcl.c_count = count > TP_NINDIR ? TP_NINDIR : count;
		}
		spclrec();
		for (i = 0; i < spcl.c_count; i++, cp += tp_bsize)
			taprec(cp, 0, tp_bsize);
		spcl.c_type = TS_ADDR;
	}
}

static void
dsrch(daddr32_t d, ulong_t size, u_offset_t filesize)
{
	struct direct *dp;
	struct dinode *ip;
	ulong_t loc;
	char dblk[MAXBSIZE];

	if (dadded || filesize == 0)
		return;
	if (filesize > (u_offset_t)size)
		filesize = (u_offset_t)size;
	if (sizeof (dblk) < roundup(filesize, DEV_BSIZE)) {
		msg(gettext(
"Inconsistency detected: filesystem block size larger than valid maximum.\n"));
		dumpabort();
		/*NOTREACHED*/
	}

#ifdef	lint
	dblk[0] = '\0';
#endif	/* lint */

	/* LINTED ufs disk addresses always fit into 32 bits */
	bread(fsbtodb(sblock, d), (uchar_t *)dblk,
	    /* LINTED from sizeof check above, roundup() <= max(size_t) */
	    (size_t)(roundup(filesize, DEV_BSIZE)));
	loc = 0;
	while ((u_offset_t)loc < filesize) {
		/*LINTED [dblk is char[], loc (dp->d_reclen) % 4 == 0]*/
		dp = (struct direct *)(dblk + loc);
		if (dp->d_reclen == 0) {
			(void) snprintf(msgbuf, sizeof (msgbuf), gettext(
			    "Warning - directory at inode `%lu' is "
			    "corrupted\n"), ino);
			msg(msgbuf);
			break;
		}
		loc += dp->d_reclen;
		if (dp->d_ino == 0)
			continue;
		if (dp->d_name[0] == '.') {
			if (dp->d_name[1] == '\0') {
				if ((ino_t)(dp->d_ino) != ino) {
					(void) snprintf(msgbuf, sizeof (msgbuf),
					    gettext(
			"Warning - directory at inode `%lu' is corrupted:\n\
\t\".\" points to inode `%lu' - run fsck\n"),
					    ino, dp->d_ino);
					msg(msgbuf);
				}
				continue;
			}
			if (dp->d_name[1] == '.' && dp->d_name[2] == '\0') {
				if (!BIT(dp->d_ino, dirmap) &&
				    ((ip = getino(ino)) == NULL ||
				    (ip->di_mode & IFMT) != IFATTRDIR)) {
					(void) snprintf(msgbuf, sizeof (msgbuf),
					    gettext(
			"Warning - directory at inode `%lu' is corrupted:\n\
\t\"..\" points to non-directory inode `%lu' - run fsck\n"),
					    ino, dp->d_ino);
					msg(msgbuf);
				}
				continue;
			}
		}
		if (BIT(dp->d_ino, nodmap)) {
			dadded++;
			return;
		}
		if (BIT(dp->d_ino, dirmap))
			nsubdir++;
	}
}

#define	CACHESIZE 32

struct dinode *
getino(ino_t ino)
{
	static ino_t minino, maxino;
	static struct dinode itab[MAXINOPB];
	static struct dinode icache[CACHESIZE];
	static ino_t icacheval[CACHESIZE], lasti = 0;
	static int cacheoff = 0;
	int i;

	if (ino >= minino && ino < maxino) {
		lasti = ino;
		return (&itab[ino - minino]);
	}

	/* before we do major i/o, check for a secondary cache hit */
	for (i = 0; i < CACHESIZE; i++)
		if (icacheval[i] == ino)
			return (icache + i);

	/* we need to do major i/o.  throw the last inode retrieved into */
	/* the cache.  note: this copies garbage the first time it is    */
	/* used, but no harm done.					 */
	icacheval[cacheoff] = lasti;
	bcopy(itab + (lasti - minino), icache + cacheoff, sizeof (itab[0]));
	lasti = ino;
	if (++cacheoff >= CACHESIZE)
		cacheoff = 0;

#define	INOPERDB (DEV_BSIZE / sizeof (struct dinode))
	minino = ino &~ (INOPERDB - 1);
	maxino = ((itog(sblock, ino) + 1) * (unsigned)(sblock->fs_ipg));
	if (maxino > minino + MAXINOPB)
		maxino = minino + MAXINOPB;
	bread(
	    /* LINTED: can't make up for broken system macros here */
	    (fsbtodb(sblock, itod(sblock, ino)) + itoo(sblock, ino) / INOPERDB),
	    /* LINTED: (max - min) * size fits into a size_t */
	    (uchar_t *)itab, (size_t)((maxino - minino) * sizeof (*itab)));
	return (&itab[ino - minino]);
}

#define	BREADEMAX 32

#ifdef NO__LONGLONG__
#define	DEV_LSEEK(fd, offset, whence) \
	lseek((fd), (((off_t)(offset))*DEV_BSIZE), (whence))
#else
#define	DEV_LSEEK(fd, offset, whence) \
	llseek((fd), (((offset_t)((offset)))*DEV_BSIZE), (whence))
#endif

#define	BREAD_FAIL(buf, size)	{ \
		breaderrors += 1; \
		bzero(buf, (size_t)size); \
	}



void
bread(diskaddr_t da, uchar_t *ba, size_t cnt)
{
	caddr_t maddr;
	uchar_t *dest;
	int saverr;
	int n;
	size_t len;
	off64_t filoff;
	off64_t mapoff;
	off64_t displacement;

	static size_t pagesize = 0;
	static int breaderrors = 0;

	/* mechanics for caching small bread requests.  these are */
	/* often small ACLs that are used over and over.	  */
	static uchar_t bcache[DEV_BSIZE * CACHESIZE];
	static diskaddr_t bcacheval[CACHESIZE];
	static int cacheoff = 0;
	int i;

	if ((cnt >= DEV_BSIZE) && (mapfd != -1)) {
		if (pagesize == 0)
			pagesize = getpagesize();
		/*
		 * We depend on mmap(2)'s guarantee that mapping a
		 * partial page will cause the remainder of the page
		 * to be zero-filled.
		 */
		filoff = ((off64_t)da) * DEV_BSIZE;
		displacement = filoff & (pagesize - 1);
		mapoff = filoff - displacement;
		/* LINTED offset will fit into 32 bits */
		len = (size_t)roundup(cnt + (filoff - mapoff), pagesize);
		maddr = mmap64(NULL, len, PROT_READ, MAP_SHARED, mapfd, mapoff);
		if (maddr != MAP_FAILED) {
			(void) memcpy(ba, maddr + displacement, cnt);
			(void) munmap(maddr, len);
			return;
		}
	}

	if (DEV_LSEEK(fi, da, L_SET) < 0) {
		saverr = errno;
		msg(gettext("bread: dev_seek error: %s\n"), strerror(saverr));
		/* Don't know where we are, return the least-harmful data */
		BREAD_FAIL(ba, cnt);
		return;
	}

	if (read(fi, ba, (size_t)cnt) == (size_t)cnt)
		return;

	while (cnt != 0) {

		if (da >= fsbtodb(sblock, sblock->fs_size)) {
			msg(gettext(
			    "Warning - block %llu is beyond the end of `%s'\n"),
			    da, disk);
			BREAD_FAIL(ba, cnt);
			break;
		}

		if (DEV_LSEEK(fi, da, L_SET) < 0) {
			msg(gettext("%s: %s error\n"), "bread", "DEV_LSEEK2");
			BREAD_FAIL(ba, cnt);
			break;
		}

		if (cnt < DEV_BSIZE) {
			/* small read.  check for cache hit. */
			for (i = 0; i < CACHESIZE; i++)
				if (bcacheval[i] == da) {
					bcopy(bcache + (i * DEV_BSIZE),
					    ba, cnt);
					return;
				}

			/* no cache hit; throw this one into the cache... */
			len = cnt;
			dest = bcache + (cacheoff * DEV_BSIZE);
			bcacheval[cacheoff] = da;
			if (++cacheoff >= CACHESIZE)
				cacheoff = 0;
		} else {
			len = DEV_BSIZE;
			dest = ba;
		}

		n = read(fi, dest, DEV_BSIZE);
		if (n != DEV_BSIZE) {
			n = MAX(n, 0);
			bzero(dest+n, DEV_BSIZE-n);
			breaderrors += 1;
			msg(gettext(
			    "Warning - cannot read sector %llu of `%s'\n"),
			    da, disk);
		}
		if (dest != ba)
			bcopy(dest, ba, len);

		da++;
		/* LINTED character pointers aren't signed */
		ba += len;
		cnt -= len;
	}

	if (breaderrors > BREADEMAX) {
		msg(gettext(
		    "More than %d block read errors from dump device `%s'\n"),
		    BREADEMAX, disk);
		dumpailing();
		breaderrors = 0;
	}
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_DUMPUSG_H
#define	_DUMPUSG_H

/*
 * Translate from BSD to System V, where possible.
 */
/*
 * System-V specific header files
 */
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <sys/statvfs.h>
#include <sys/systeminfo.h>
#include <sys/vfstab.h>
#include <sys/fs/ufs_inode.h>
#include <sys/fs/ufs_fs.h>
#include <sys/fs/ufs_fsdir.h>
#include <sys/fs/ufs_acl.h>

#include <sys/mnttab.h>
#include <sys/vfstab.h>

#ifdef	__cplusplus
extern "C" {
#endif

/*
 * make mnttab look like mtab
 */
#define	MOUNTED		MNTTAB
#define	mntent		mnttab
#define	mnt_fsname	mnt_special
#define	mnt_dir		mnt_mountp
#define	mnt_type	mnt_fstype
#define	mnt_opts	mnt_mntopts
#define	MNTTYPE_42	"ufs"
#define	MNTINFO_DEV	"dev"

#define	setmntent	fopen
#define	endmntent	fclose

/*
 * Function translations
 */
#define	gethostname(name, len)	\
	    ((sysinfo(SI_HOSTNAME, (name), (len)) < 0) ? -1 : 0)
#define	signal			nsignal		/* defined in dumpmain.c */
#define	sigvec			sigaction	/* both struct and func */
#define	sv_flags		sa_flags
#define	sv_handler		sa_handler
#define	sv_mask			sa_mask
#define	sigmask(x)		x
#define	setreuid(r, e)		seteuid(e)
#define	statfs			statvfs		/* both struct and func */
#undef	setjmp
#define	setjmp(b)		sigsetjmp((b), 1)
#define	longjmp			siglongjmp
#define	jmp_buf			sigjmp_buf

#if !__STDC__
extern int seteuid();
#endif

/*
 * Inode related translations
 */
#define	ROOTINO		UFSROOTINO
#define	di_rdev		di_ordev

/*
 * For stat-inode translation.
 * Don't forget the translation from
 * nanosecs to usecs (or vica versa)
 */
#define	st_spare1	st_atim.tv_nsec
#define	st_spare2	st_mtim.tv_nsec
#define	st_spare3	st_ctim.tv_nsec

#define	TMCONV	1000

#ifdef	__cplusplus
}
#endif

#endif /* _DUMPUSG_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1988 AT&T	*/
/*	  All Rights Reserved	*/

/* LINTLIBRARY */
/*
 *	ftw - file tree walk
 *
 *	int ftw (path, fn, depth)  char *path; int (*fn)(); int depth;
 *
 *	Given a path name, ftw starts from the file given by that path
 *	name and visits each file and directory in the tree beneath
 *	that file.  If a single file has multiple links within the
 *	structure, it will be visited once for each such link.
 *	For each object visited, fn is called with three arguments.
 *	The first contains the path name of the object, the second
 *	contains a pointer to a stat buffer which will usually hold
 *	appropriate information for the object and the third will
 *	contain an integer value giving additional information:
 *
 *		FTW_F	The object is a file for which stat was
 *			successful.  It does not guarantee that the
 *			file can actually be read.
 *
 *		FTW_D	The object is a directory for which stat and
 *			open for read were both successful.
 *
 *		FTW_DNR	The object is a directory for which stat
 *			succeeded, but which cannot be read.  Because
 *			the directory cannot be read, fn will not be
 *			called for any descendants of this directory.
 *
 *		FTW_NS	Stat failed on the object because of lack of
 *			appropriate permission.  This indication will
 *			be given, for example, for each file in a
 *			directory with read but no execute permission.
 *			Because stat failed, it is not possible to
 *			determine whether this object is a file or a
 *			directory.  The stat buffer passed to fn will
 *			contain garbage.  Stat failure for any reason
 *			other than lack of permission will be
 *			considered an error and will cause ftw to stop
 *			and return -1 to its caller.
 *
 *	If fn returns nonzero, ftw stops and returns the same value
 *	to its caller.  If ftw gets into other trouble along the way,
 *	it returns -1 and leaves an indication of the cause in errno.
 *
 *	The third argument to ftw does not limit the depth to which
 *	ftw will go.  Rather, it limits the depth to which ftw will
 *	go before it starts recycling file descriptors.  In general,
 *	it is necessary to use a file descriptor for each level of the
 *	tree, but they can be recycled for deep trees by saving the
 *	position, closing, re-opening, and seeking.  It is possible
 *	to start recycling file descriptors by sensing when we have
 *	run out, but in general this will not be terribly useful if
 *	fn expects to be able to open files.  We could also figure out
 *	how many file descriptors are available and guarantee a certain
 *	number to fn, but we would not know how many to guarantee,
 *	and we do not want to impose the extra overhead on a caller who
 *	knows how many are available without having to figure it out.
 *
 *	It is possible for ftw to die with a memory fault in the event
 *	of a file system so deeply nested that the stack overflows.
 */

#include <sys/fs/ufs_inode.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <malloc.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <ftw.h>

static int pwdfd;

static int lf_xftw(
	const char *,
	int (*)(const char *, const struct stat64 *, int),
	int,
	int (*)(const char *, struct stat64 *));

int
lf_lftw(
	const char *path,
	int (*fn)(const char *, const struct stat64 *, int),
	int depth)
{
	int rc;

	if ((pwdfd = open(".", O_RDONLY)) < 0) {
		return (-1);
	} else {
		rc = (lf_xftw(path, fn, depth, lstat64));
		(void) close(pwdfd);
		return (rc);
	}
}

static int
#ifdef __STDC__
lf_xftw(
	const char *path,
	int (*fn)(const char *, const struct stat64 *, int),
	int depth,
	int (*statfn)(const char *, struct stat64 *))
#else
lf_xftw(char *path, int (*fn)(), int depth, int (*statfn)())
#endif
{
	int n;
	int rc, sublen, saverr, attrfd;
	DIR *dirp;
	char *subpath, *component;
	struct stat64 sb;
	struct dirent *dp;
	extern dev_t partial_dev;

	/*
	 * Try to get file status.
	 * If unsuccessful, errno will say why.
	 */
	if ((*statfn)(path, &sb) < 0)
		return (errno == EACCES? (*fn)(path, &sb, FTW_NS): -1);
	/*
	 *	The stat succeeded, so we know the object exists.
	 *	Make sure it is not a mount point for another filesystem.
	 *	The following check must be made here because:
	 *
	 *		+ namefs can be mounted on anything, but a directory
	 *		+ all other filesystems must be mounted on a directory
	 */
	if (sb.st_dev != partial_dev) {
		return (0);
	}
	/*
	 *	Check for presence of attributes on file
	 */
	if (pathconf(path, _PC_XATTR_EXISTS) == 1) {
		attrfd = attropen64(path, ".", O_RDONLY|O_NONBLOCK);
	} else {
		attrfd = -1;
	}
	/*
	 *	If not a directory, call the user function and return.
	 */
	if ((sb.st_mode & S_IFMT) != S_IFDIR &&
	    (sb.st_mode & IFMT) != IFATTRDIR) {
		rc = (*fn)(path, &sb, FTW_F);
		if (rc == 0 && attrfd != -1) {
			(void) fchdir(attrfd);
			rc = lf_xftw(".", fn, depth-1, statfn);
			(void) fchdir(pwdfd);
			(void) close(attrfd);
		}
		return (rc);
	}
	/*
	 *	The object was a directory and not a mount point.
	 *
	 *	Open a file to read the directory
	 */
	dirp = opendir(path);

	/*
	 *	Call the user function, telling it whether
	 *	the directory can be read.  If it can't be read
	 *	call the user function or indicate an error,
	 *	depending on the reason it couldn't be read.
	 */
	if (dirp == NULL)
		rc = (errno == EACCES? (*fn)(path, &sb, FTW_DNR): -1);
	else
		rc = (*fn)(path, &sb, FTW_D);
	/*
	 *	If the directory has attributes, process the
	 *	attributes before processing the directory contents.
	 */
	if (rc == 0 && attrfd != -1) {
		(void) fchdir(attrfd);
		rc = lf_xftw(".", fn, depth-1, statfn);
		(void) fchdir(pwdfd);
		(void) close(attrfd);
	}
	if (rc != 0 || dirp == NULL)
		return (rc);

	/* Allocate a buffer to hold generated pathnames. */
	/* LINTED: the length will fit into a signed integer */
	n = (int)strlen(path);
	sublen = n + MAXNAMLEN + 1; /* +1 for appended / */
	subpath = malloc((unsigned)(sublen+1));	/* +1 for NUL */
	if (subpath == NULL) {
		saverr = errno;
		(void) closedir(dirp);
		errno = saverr;
		return (-1);
	}

	/* Create a prefix to which we will append component names */
	(void) strcpy(subpath, path);
	if (subpath[0] != '\0' && subpath[n-1] != '/')
		subpath[n++] = '/';
	component = &subpath[n];
	/* LINTED: result will fit into a 32-bit int */
	sublen -= component - subpath;

	/*
	 *	Read the directory one component at a time.
	 *	We must ignore "." and "..", but other than that,
	 *	just create a path name and call self to check it out.
	 */
	while ((dp = readdir(dirp)) != NULL) {
		if (strcmp(dp->d_name, ".") != 0 &&
		    strcmp(dp->d_name, "..") != 0) {
			long here;

			/* Append component name to the working path */
			(void) strncpy(component, dp->d_name, sublen);
			component[sublen - 1] = '\0';

			/*
			 *	If we are about to exceed our depth,
			 *	remember where we are and close a file.
			 */
			if (depth <= 1) {
				here = telldir(dirp);
				(void) closedir(dirp);
			}

			/*
			 *	Do a recursive call to process the file.
			 *	(watch this, sports fans)
			 */
			rc = lf_xftw(subpath, fn, depth-1, statfn);
			if (rc != 0) {
				free(subpath);
				if (depth > 1)
					(void) closedir(dirp);
				return (rc);
			}

			/*
			 *	If we closed the file, try to reopen it.
			 */
			if (depth <= 1) {
				dirp = opendir(path);
				if (dirp == NULL) {
					free(subpath);
					return (-1);
				}
				seekdir(dirp, here);
			}
		}
	}

	/*
	 *	We got out of the subdirectory loop.  The return from
	 *	the final readdir is in dp.  Clean up.
	 */
	free(subpath);
	(void) closedir(dirp);
	return (0);
}
/stdio.h",.*always ignored: fclose/d
/stdio.h",.*always ignored: fprintf/d
/stdio.h",.*always ignored: printf/d
/stdio.h",.*always ignored: snprintf/d
/time.h",.*sometimes ignored: time/d
/string.h",.*sometimes ignored: memcpy/d
/signal.h",.*always ignored: sighold/d
/signal.h",.*always ignored: sigrelse/d
/unistd.h",.*always ignored: close/d
/unistd.h",.*always ignored: execl/d
/unistd.h",.*always ignored: sleep/d
/myrcmd.c",.*assigned value never used: retval .*(22[0-9])/d
/dumptape.c",.*use of a pointer.*questionable/d
/dumponline.c",.*name used but not defined: log10 /d
/dumponline.c",.*name used but not defined: getfullblkname /d
/dumpmain.c",.*name used but not defined: getfullrawname /d
/^	obuf defined/d
/roll_log.c",.*include file.*unnecessary:/d
/partial.c",.*name used but not defined: ulimit/d
/dirent.h",.*different definitions of macro: MAXNAMLEN/d
/limits.h",.*different definitions of macro: SYS_NMLN/d
/lint suppression directive not used/d
/memutils.c",.*deallocating NULL pointer/d
/memutils.c",.*assigned value never used: allocated at/d
/dumpmain.c",.*assigned value never used:/d
/dumptape.c",.*deallocating NULL pointer/d
/dumpmain.c",.*deallocating NULL pointer/d
/partial.c",.*deallocating NULL pointer/d
/dumpitime.c",.*reference to deallocated memory/d
/dumpitime.c",.*reference using a NULL pointer/d
/assigned value never used: idates_in/d
/roll_log.c",.*assigned value never used/d
/dumptape.c",.*statement not reached/d
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1998,2001 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#include "dump.h"
#include <ftw.h>
#include <ulimit.h>

dev_t partial_dev;
static int partial;

static dev_t devfromopts(struct mntent *);
static int lf_mark_root(dev_t, char *);
static int lf_ftw_mark(const char *, const struct stat64 *, int);
static void markino(ino_t);

void
partial_check(void)
{
	struct mntent *mnt;
	struct stat64 st;

	if (stat64(disk, &st) < 0 ||
	    (st.st_mode & S_IFMT) == S_IFCHR ||
	    (st.st_mode & S_IFMT) == S_IFBLK)
		return;

	partial_dev = st.st_dev;

	setmnttab();
	while (mnt = getmnttab()) {
		st.st_dev = devfromopts(mnt);
		if (st.st_dev == NODEV &&
		    stat64(mnt->mnt_dir, &st) < 0)
			continue;
		if (partial_dev == st.st_dev) {
			if (disk_dynamic) {
				/* LINTED: disk is not NULL */
				free(disk);
			}
			disk = rawname(mnt->mnt_fsname);
			disk_dynamic = (disk != mnt->mnt_fsname);

			partial = 1;
			incno = '0';
			uflag = 0;
			return;
		}
	}
	msg(gettext("`%s' is not on a locally mounted filesystem\n"), disk);
	dumpabort();
	/*NOTREACHED*/
}

/*
 *  The device id for the mount should be available in
 *  the mount option string as "dev=%04x".  If it's there
 *  extract the device id and avoid having to stat.
 */
static dev_t
devfromopts(struct mntent *mnt)
{
	char *str;

	str = hasmntopt(mnt, MNTINFO_DEV);
	if (str != NULL && (str = strchr(str, '=')))
		return ((dev_t)strtol(str + 1, (char **)NULL, 16));

	return (NODEV);
}

int
partial_mark(int argc, char **argv)
{
	char *path;
	struct stat64 st;

	if (partial == 0)
		return (1);

	while (--argc >= 0) {
		path = *argv++;

		if (stat64(path, &st) < 0 ||
		    st.st_dev != partial_dev) {
			msg(gettext("`%s' is not on dump device `%s'\n"),
			    path, disk);
			dumpabort();
			/*NOTREACHED*/
		}

		if (lf_mark_root(partial_dev, path)) {
			msg(gettext(
			    "Cannot find filesystem mount point for `%s'\n"),
			    path);
			dumpabort();
			/*NOTREACHED*/
		}

		/* LINTED this ulimit will always be < INT_MAX */
		if (lf_lftw(path, lf_ftw_mark, (int)ulimit(UL_GDESLIM, 0) / 2)
		    < 0) {
			int saverr = errno;
			msg(gettext("Error in %s (%s)\n"),
			    "ftw", strerror(saverr));
			dumpabort();
			/*NOTREACHED*/
		}
	}

	return (0);
}

/* mark directories between target and root */
static int
lf_mark_root(dev_t dev, char *path)
{
	struct stat64 st;
	char dotdot[MAXPATHLEN + 16];
	char *slash;

	if (strlen(path) > sizeof (dotdot))
		return (1);

	(void) strcpy(dotdot, path);

	if (stat64(dotdot, &st) < 0)
		return (1);

	/* if target is a regular file, find directory */
	if ((st.st_mode & S_IFMT) != S_IFDIR)
		if (slash = strrchr(dotdot, '/'))
			/* "/file" -> "/" */
			if (slash == dotdot)
				slash[1] = 0;
			/* "dir/file" -> "dir" */
			else
				slash[0] = 0;
		else
			/* "file" -> "." */
			(void) strcpy(dotdot, ".");

	/* keep marking parent until we hit mount point */
	do {
		if (stat64(dotdot, &st) < 0 ||
		    (st.st_mode & S_IFMT) != S_IFDIR ||
		    st.st_dev != dev)
			return (1);
		markino(st.st_ino);
		if (strlen(dotdot) > (sizeof (dotdot) - 4))
			return (1);
		(void) strcat(dotdot, "/..");
	} while (st.st_ino != 2);

	return (0);
}

/*ARGSUSED*/
static int
lf_ftw_mark(const char *name, const struct stat64 *st, int flag)
{
	if (flag != FTW_NS) {
		/* LINTED ufs only uses the lower 32 bits */
		markino((ino_t)st->st_ino);
	}
	return (0);
}

static void
markino(ino_t i)
{
	struct dinode *dp;

	dp = getino(ino = i);
	mark(dp);
}
/*
 * Copyright (c) 1998,2001 by Sun Microsystems, Inc.
 * All rights reserved.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/

/*
 * Copyright (c) 1980 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#include <sys/types.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
/*
 * Convert a ctime(3) format string into a system format date.
 * Return the date thus calculated.
 *
 * Return -1 if the string is not in ctime format.
 */

/*
 * Offsets into the ctime string to various parts.
 */

#define	E_MONTH		4
#define	E_DAY		8
#define	E_HOUR		11
#define	E_MINUTE	14
#define	E_SECOND	17
#define	E_YEAR		20

#ifdef __STDC__
static int lookup(char *);
static time_t emitl(struct tm *);
#else
static int lookup();
static time_t emitl();
#endif

time_t
unctime(str)
	char *str;
{
	struct tm then;
	char dbuf[30];

	/* Definition of ctime(3) is 24 characters + newline + NUL */
	(void) strncpy(dbuf, str, 24);
	dbuf[24] = '\0';
	dbuf[E_MONTH+3] = '\0';
	then.tm_mon = lookup(&dbuf[E_MONTH]);
	if (then.tm_mon < 0) {
		return (-1);
	}
	then.tm_mday = atoi(&dbuf[E_DAY]);
	then.tm_hour = atoi(&dbuf[E_HOUR]);
	then.tm_min = atoi(&dbuf[E_MINUTE]);
	then.tm_sec = atoi(&dbuf[E_SECOND]);
	then.tm_year = atoi(&dbuf[E_YEAR]) - 1900;
	return (emitl(&then));
}

static char months[] =
	"JanFebMarAprMayJunJulAugSepOctNovDec";

static int
lookup(str)
	char *str;
{
	char *cp, *cp2;

	for (cp = months, cp2 = str; *cp != 0; cp += 3)
		if (strncmp(cp, cp2, 3) == 0)
			/* LINTED ptr arith will give < INT_MAX result */
			return (((int)(cp-months)) / 3);
	return (-1);
}
/*
 * Routine to convert a localtime(3) format date back into
 * a system format date.
 */
static time_t
emitl(dp)
	struct tm *dp;
{
	dp->tm_isdst = -1;
	return (mktime(dp));
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 1990-1998, 2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_BYTEORDER_H
#define	_BYTEORDER_H

#include <stdio.h>
#include <sys/fs/ufs_fsdir.h>
#include <sys/fs/ufs_acl.h>

#ifdef	__cplusplus
extern "C" {
#endif

#define	SUPPORTS_MTB_TAPE_FORMAT
#include <protocols/dumprestore.h>
#include <assert.h>

struct byteorder_ctx {
	int initialized;
	int Bcvt;
};

#ifdef __STDC__
extern struct byteorder_ctx *byteorder_create(void);
extern void byteorder_destroy(struct byteorder_ctx *);
extern void byteorder_banner(struct byteorder_ctx *, FILE *);
extern void swabst(char *, uchar_t *);
extern uint32_t swabl(uint32_t);
extern int normspcl(struct byteorder_ctx *, struct s_spcl *, int *, int, int);
extern void normdirect(struct byteorder_ctx *, struct direct *);
extern void normacls(struct byteorder_ctx *, ufs_acl_t *, int);
#else /* __STDC__ */
extern struct byteorder_ctx *byteorder_create();
extern void byteorder_destroy();
extern void byteorder_banner();
extern void swabst();
extern uint32_t swabl();
extern int normspcl();
extern void normdirect();
extern void normacls();
#endif /* __STDC__ */

#ifdef	__cplusplus
}
#endif

#endif /* _BYTEORDER_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef _MEMUTILS_H
#define	_MEMUTILS_H

#include <note.h>

#ifdef	__cplusplus
extern "C" {
#endif

#if defined(__STDC__)
extern void *xmalloc(size_t);
extern void *xcalloc(size_t, size_t);
extern void *xrealloc(void *, size_t);
#else
extern void *xmalloc();
extern void *xcalloc();
extern void *xrealloc();
#endif /* __STDC__ */

NOTE(ALIGNMENT(xmalloc, 8))
NOTE(ALIGNMENT(xcalloc, 8))
NOTE(ALIGNMENT(xrealloc, 8))

#ifdef	__cplusplus
}
#endif

#endif /* _MEMUTILS_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1998 by Sun Microsystems, Inc.
 * All rights reserved.
 *
 * Internal definitions for the myrcmd.c rcmd(3) replacement module.
 */

#ifndef _MYRCMD_H
#define	_MYRCMD_H

#ifdef	__cplusplus
extern "C" {
#endif

/* Failure return values */
#define	MYRCMD_EBAD		-1
#define	MYRCMD_NOHOST		-2
#define	MYRCMD_ENOPORT		-3
#define	MYRCMD_ENOSOCK		-4
#define	MYRCMD_ENOCONNECT	-5

/*
 * On a failure, the output that would have normally gone to stderr is
 * now placed in the global string "myrcmd_stderr".  Callers should check
 * to see if there is anything in the string before trying to print it.
 */
extern char myrcmd_stderr[];

#ifdef __STDC__
extern int myrcmd(char **ahost, unsigned short rport, char *locuser,
	char *remuser, char *cmd);
#else
extern int myrcmd();
#endif /* __STDC__ */

#ifdef	__cplusplus
}
#endif

#endif /* _MYRCMD_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1991,1998 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#ifndef _RMT_H
#define	_RMT_H

#include <sys/mtio.h>

#ifdef	__cplusplus
extern "C" {
#endif

#ifdef __STDC__
extern void rmtinit(void (*)(const char *, ...), void (*)(int));
extern int rmthost(char *, uint_t);
extern int rmtopen(char *, int);
extern void rmtclose(void);
extern int rmtstatus(struct mtget *);
extern int rmtread(char *, uint_t);
extern int rmtwrite(char *, uint_t);
extern int rmtseek(int, int);
extern int rmtioctl(int, long);
#else
extern void rmtinit();
extern int rmthost();
extern int rmtopen();
extern void rmtclose();
extern int rmtstatus();
extern int rmtread();
extern int rmtwrite();
extern int rmtseek();
extern int rmtioctl();
#endif

#ifdef	__cplusplus
}
#endif

#endif /* _RMT_H */
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

LIBRARY=	libdump.a

# Has to be before include of Makefile.backup
# This should be POFILE=libdump.po, but that causes make to
# fall over due to some seriously weird interactions in the
# various indirectly-included makefiles.  So, since this works
# and is otherwise harmless, we fake it.
PROG=		libdump

# Include library definitions, then backup definitions, as in general
# we want the flags and such from our tree.

include ../../../lib/Makefile.lib
include ../Makefile.backup

SRCDIR =	.

# Specifically request the construction of a static library.
# This library is not installed in the proto area.
LIBS=	$(LIBRARY)

HDRS=		../include/byteorder.h \
	../include/memutils.h ../include/myrcmd.h \
	../../../head/protocols/dumprestore.h \
	../include/rmt.h

YFILE=		getdate.y
YSRC=		getdate.c

CLOBBERFILES +=	$(LIBS) $(GLIB)

LOBJS=		rmtlib.o myrcmd.o \
		$(YSRC:%.c=%.o) \
		byteorder.o memutils.o $(RPC_CLNT:%.c=%.o) $(RPC_XDR:%.c=%.o)

OBJECTS=	$(LOBJS)
POFILES=	$(OBJECTS:.o=.po)
GENERAL=	../include
GLOBAL=		../../../head
CPPFLAGS=	-I$(GENERAL) -I$(GLOBAL) $(CPPFLAGS.master)
CERRWARN +=	-Wno-implicit-function-declaration
CERRWARN +=	-Wno-parentheses
CERRWARN +=	-Wno-unused-variable
CLEANFILES=	$(OBJECTS) $(DEBUGS) $(YSRC) $(LIBRARY)

# because of labels from yacc
objs/getdate.o : CERRWARN +=	-Wno-unused-label

# support for -g library
GLIB=		libdump_g.a
DEBUGS=		$(OBJECTS:%=.debug/%)
$(GLIB):	AROBJS = $(DEBUGS)
$(GLIB):	DIR = .debug
$(GLIB):	CFLAGS= $(CCGDEBUG) -DDEBUG -DYYDEBUG $(SBFLAGS)

.KEEP_STATE:

all:	$(LIBS)

debug:	$(LIBS) $(GLIB)

.debug:
	-@mkdir -p $@

.debug/%.o: %.c
	$(COMPILE.c) -o $@ $<
	$(POST_PROCESS_O)

$(GLIB): .debug $$(DEBUGS)
	$(BUILD.AR)
	$(POST_PROCESS_A)

$(OBJECTS):	$(HDRS)

install:	all

$(POFILE):	$(POFILES)
	$(RM) $@; cat $(POFILES) > $@

check:	$(HDRS)
	$(CSTYLE) $(CSTYLEFLAGS) `echo $(SRCS) | sed -e s/getdate.c//` $(HDRS)
	$(HDRCHK) $(HDRCHKFLAGS) $(HDRS)

# include library targets
include ../../../lib/Makefile.targ

_msg:		$(POFILE)
/*
 * Copyright 1996, 1998, 2002-2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/

/*
 * Copyright (c) 1983 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/vnode.h>
#include <locale.h>
#include <stdlib.h>
#include <sys/fs/ufs_inode.h>
#include <sys/fs/ufs_fsdir.h>
#include <sys/fs/ufs_acl.h>
#include <byteorder.h>

struct byteorder_ctx *
byteorder_create(void)
{
	struct byteorder_ctx *rc;

	/* LINTED: assignment value is used */
	if ((rc = (struct byteorder_ctx *)calloc(1, sizeof (*rc))) == NULL)
		return (NULL);
	return (rc);
}

void
byteorder_destroy(struct byteorder_ctx *ctx)
{
	if (ctx != NULL)
		(void) free((char *)ctx);
}

void
byteorder_banner(struct byteorder_ctx *ctx, FILE *filep)
{
	if ((! ctx->initialized) || (filep == NULL))
		return;

	if (ctx->Bcvt)
		(void) fprintf(filep, gettext("Note: doing byte swapping\n"));
}

/*
 * Control string (cp) is a sequence of optional numeric repeat counts
 * and format specifiers.  s/w/h indicate a 16-bit quantity is to be
 * byte-swapped, l indicates a 32-bit quantity.  A repeat count is
 * identical in effect to having the following format character appear
 * N times (e.g., "3h" is equivalent to "hhh").
 *
 * The byte-swapping is performed in-place, in the buffer sp.
 */
void
swabst(char *cp, uchar_t *sp)
{
	int n = 0;
	uchar_t c;

	while (*cp) {
		switch (*cp) {
		case '0': case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9':
			n = (n * 10) + (*cp++ - '0');
			continue;

		case 's': case 'w': case 'h':
			/* LINTED: type punning ok here */
			c = sp[0]; sp[0] = sp[1]; sp[1] = c;
			sp++;
			break;

		case 'l':
			c = sp[0]; sp[0] = sp[3]; sp[3] = c;
			c = sp[2]; sp[2] = sp[1]; sp[1] = c;
			sp += 3;
		}
		/* Any other character, like 'b' counts as byte. */
		sp++;
		if (n <= 1) {
			n = 0; cp++;
		} else
			n--;
	}
}

uint32_t
swabl(uint32_t x)
{
	uint32_t l = x;

	swabst("l", (uchar_t *)&l);
	/* LINTED: type punning ok here */
	return (l);
}

static int
checksum(struct byteorder_ctx *ctx, int *b, int size)
{
	uint_t i, j;

	if (! ctx->initialized)
		return (-1);

	/*
	 * We should only be called on to checksum u_spcl's, so make
	 * sure that's what we got.
	 */
	if ((unsigned)size < tp_bsize)
		return (-1);

	j = tp_bsize / sizeof (int);
	i = 0;
	if (!ctx->Bcvt) {
		do
			i += (uint_t)*b++;
		while (--j);
	} else {
		/*
		 * What happens if we want to read restore tapes
		 * for a 16bit int machine???
		 */
		do
			i += swabl(*b++);
		while (--j);
	}

	return (i != CHECKSUM);
}

/*
 * normspcl() checks that a spclrec is valid.  it does byte/quad
 * swapping if necessary, and checks the checksum.  it does NOT convert
 * from the old filesystem format; gethead() in tape.c does that.
 *
 * ctx is the context for this package
 * sp is a pointer to a current-format spclrec, that may need to be
 *	byteswapped.
 * cs is a pointer to the thing we want to checksum.  if we're
 *	converting from the old filesystem format, it might be different
 *	from sp.
 * css is the size of the thing we want to checksum.
 * magic is the magic number we compare against.
 */

int
normspcl(struct byteorder_ctx *ctx, struct s_spcl *sp, int *cs,
    int css, int magic)
{
	u_offset_t sv;

	if ((! ctx->initialized) && (sp->c_magic != magic)) {
		if (swabl(sp->c_magic) != (uint32_t)magic)
			return (-1);
		ctx->Bcvt = 1;
	}
	ctx->initialized = 1;

	if (checksum(ctx, cs, css))
		return (-1);

	/*
	 * Unless our caller is actively trying to break us, a
	 * successful checksum() means that *sp is at least as
	 * big as what we think it should be as far as byte
	 * swapping goes.  Therefore, we don't need to do any
	 * more size checks here.
	 */

	/* handle byte swapping */
	if (ctx->Bcvt) {
		/*
		 * byteswap
		 *	c_type, c_date, c_ddate, c_volume, c_tapea, c_inumber,
		 *	c_magic, c_checksum,
		 *	all of c_dinode, and c_count.
		 */

		swabst("8l4s31l", (uchar_t *)sp);

		/*
		 * byteswap
		 *	c_flags, c_firstrec, and c_spare.
		 */

		swabst("34l", (uchar_t *)&(sp->c_flags));

		/* byteswap the inodes if necessary. */

#ifndef	lint	/* lint won't shut up about sprintf below */
		if (sp->c_flags & DR_INODEINFO) {
			char buffy[BUFSIZ];
			/* Can't overflow, max len is %d format (20)+`l'+\0 */
			/* LINTED lint can't tell diff between %ld and %dl */
			(void) sprintf(buffy, "%dl", TP_NINOS);
			swabst(buffy, (uchar_t *)sp->c_data.s_inos);
		}
#endif	/* lint */

		/* if no metadata, byteswap the level */

		if (! (sp->c_flags & DR_HASMETA))
			swabst("1l", (uchar_t *)&(sp->c_level));
	}

	/* handle quad swapping (note -- we no longer perform this check */
	/*	we now do quad swapping iff we're doing byte swapping.)  */

	/*
	 * 	the following code is being changed during the large file
	 *	project. This code needed to be changed because ic_size
	 *	is no longer a quad, it has been changed to ic_lsize, which is
	 *	an offset_t, and the field "val" doesn't exist anymore.
	 */

/*
 * This is the old code. (before large file project.)
 *
 *	sv = sp->c_dinode.di_ic.ic_size.val;
 *
 *	if (ctx->Bcvt) {
 *		long foo;
 *
 *		foo = sv[1];
 *		sv[1] = sv[0];
 *		sv[0] = foo;
 *	}
 */

	/* swap the upper 32 bits of ic_lsize with the lower 32 bits */

	if (ctx->Bcvt) {
		sv = sp->c_dinode.di_ic.ic_lsize;
		sv = (sv << 32) | (sv >> 32);
		sp->c_dinode.di_ic.ic_lsize = sv;
	}

	if (sp->c_magic != magic)
		return (-1);
	return (0);
}

void
normdirect(ctx, d)
	struct byteorder_ctx *ctx;
	struct direct *d;
{
	assert(ctx->initialized);

	if (ctx->Bcvt)
		swabst("l2s", (uchar_t *)d);
}

void
normacls(struct byteorder_ctx *ctx, ufs_acl_t *acl, int n)
{
	static int complained = 0;
	int i;
	uid32_t uid;

	assert(ctx->initialized);

	if (! ctx->Bcvt)
		return;

	for (i = 0; i < n; i++) {
		swabst("1s", (uchar_t *)&(acl[i].acl_tag));  /* u_short */
		swabst("1s", (uchar_t *)&(acl[i].acl_perm)); /* o_mode_t */

		/* LINTED explicitly checking for truncation below */
		uid = (uid32_t)(acl[i].acl_who);
		if (!complained && ((uid_t)uid) != acl[i].acl_who) {
			/*
			 * The problem is that acl_who is a uid_t,
			 * and we know that the on-tape version is
			 * definitely 32 bits.  To avoid getting
			 * burned if/when uid_t becomes bigger
			 * than that, we need to do the explicit
			 * conversion and check.
			 */
			(void) fprintf(stderr,
			    "Some ACL uids have been truncated\n");
			complained = 1;
		}
		swabst("1l", (uchar_t *)&(uid));	/* uid32_t */
	}
}
%{
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/* $OrigRevision: 2.1 $
**
**  Originally written by Steven M. Bellovin <smb@research.att.com> while
**  at the University of North Carolina at Chapel Hill.  Later tweaked by
**  a couple of people on Usenet.  Completely overhauled by Rich $alz
**  <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990;
**  send any email to Rich.
**
**  This grammar has eight shift/reduce conflicts.
**
**  This code is in the public domain and has no copyright.
*/
/* SUPPRESS 287 on yaccpar_sccsid *//* Unusd static variable */
/* SUPPRESS 288 on yyerrlab *//* Label unused */
#include <stdio.h>
#include <ctype.h>

#include <sys/types.h>
#define NEED_TZSET
struct timeb {
    time_t		time;		/* Seconds since the epoch	*/
    unsigned short	millitm;	/* Field not used		*/
    short		timezone;
    short		dstflag;	/* Field not used		*/
};
#include <time.h>

#include <locale.h>
#include <string.h>
#include <stdlib.h>
#include <note.h>
#include <libintl.h>

#if	!defined(lint) && !defined(SABER)
static char RCS[] =
	"$Header: /home/laramie/berliner/ws/backup/usr/src/cmd/backup/lib/getdate.y,v 1.5 1992/06/09 21:46:21 sam Exp $";
#endif	/* !defined(lint) && !defined(SABER) */


#define EPOCH		1970
#define HOURN(x)	(x * 60)
#define SECSPERDAY	(24L * 60L * 60L)

#define CHECK_TM(y) (((y) % 100) < 70 ? (y) + 2000 : (y) + 1900)

/*
**  An entry in the lexical lookup table.
*/
typedef struct _TABLE {
    char	*name;
    int		type;
    time_t	value;
} TABLE;


/*
**  Daylight-savings mode:  on, off, or not yet known.
*/
typedef enum _DSTMODE {
    DSTon, DSToff, DSTmaybe
} DSTMODE;

/*
**  Meridian:  am, pm, or 24-hour style.
*/
typedef enum _MERIDIAN {
    MERam, MERpm, MER24
} MERIDIAN;


/*
**  Global variables.  We could get rid of most of these by using a good
**  union as the yacc stack.  (This routine was originally written before
**  yacc had the %union construct.)  Maybe someday; right now we only use
**  the %union very rarely.
*/
static char	*yyInput;
static DSTMODE	yyDSTmode;
static time_t	yyDayOrdinal;
static time_t	yyDayNumber;
static int	yyHaveDate;
static int	yyHaveDay;
static int	yyHaveRel;
static int	yyHaveTime;
static int	yyHaveZone;
static time_t	yyTimezone;
static time_t	yyDay;
static time_t	yyHour;
static time_t	yyMinutes;
static time_t	yyMonth;
static time_t	yySeconds;
static time_t	yyYear;
static MERIDIAN	yyMeridian;
static time_t	yyRelMonth;
static time_t	yyRelSeconds;

static char	*domainname = "hsm_libdump";	/* for dgettext() */

#define yylex 1					/* suppress yacc's definition */
%}

%union {
    time_t		Number;
    enum _MERIDIAN	Meridian;
}

%token	tAGO tDAY tDAYZONE tID tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
%token	tSEC_UNIT tSNUMBER tUNUMBER tZONE

%type	<Number>	tDAY tDAYZONE tMINUTE_UNIT tMONTH tMONTH_UNIT
%type	<Number>	tSEC_UNIT tSNUMBER tUNUMBER tZONE
%type	<Meridian>	tMERIDIAN o_merid

%%

spec	: /* NULL */
	| spec item
	;

item	: time {
	    yyHaveTime++;
	}
	| zone {
	    yyHaveZone++;
	}
	| date {
	    yyHaveDate++;
	}
	| day {
	    yyHaveDay++;
	}
	| rel {
	    yyHaveRel++;
	}
	| number
	;

time	: tUNUMBER tMERIDIAN {
	    yyHour = $1;
	    yyMinutes = 0;
	    yySeconds = 0;
	    yyMeridian = $2;
	}
	| tUNUMBER ':' tUNUMBER o_merid {
	    yyHour = $1;
	    yyMinutes = $3;
	    yySeconds = 0;
	    yyMeridian = $4;
	}
	| tUNUMBER ':' tUNUMBER tSNUMBER {
	    yyHour = $1;
	    yyMinutes = $3;
	    yyMeridian = MER24;
	    yyDSTmode = DSToff;
	    yyTimezone = - ($4 % 100 + ($4 / 100) * 60);
	}
	| tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid {
	    yyHour = $1;
	    yyMinutes = $3;
	    yySeconds = $5;
	    yyMeridian = $6;
	}
	| tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER {
	    yyHour = $1;
	    yyMinutes = $3;
	    yySeconds = $5;
	    yyMeridian = MER24;
	    yyDSTmode = DSToff;
	    yyTimezone = - ($6 % 100 + ($6 / 100) * 60);
	}
	;

zone	: tZONE {
	    yyTimezone = $1;
	    yyDSTmode = DSToff;
	}
	| tDAYZONE {
	    yyTimezone = $1;
	    yyDSTmode = DSTon;
	}
	;

day	: tDAY {
	    yyDayOrdinal = 1;
	    yyDayNumber = $1;
	}
	| tDAY ',' {
	    yyDayOrdinal = 1;
	    yyDayNumber = $1;
	}
	| tUNUMBER tDAY {
	    yyDayOrdinal = $1;
	    yyDayNumber = $2;
	}
	;

date	: tUNUMBER '/' tUNUMBER {
	    yyMonth = $1;
	    yyDay = $3;
	}
	| tUNUMBER '/' tUNUMBER '/' tUNUMBER {
	    yyMonth = $1;
	    yyDay = $3;
	    yyYear = $5;
	}
	| tMONTH tUNUMBER {
	    yyMonth = $1;
	    yyDay = $2;
	}
	| tMONTH tUNUMBER ',' tUNUMBER {
	    yyMonth = $1;
	    yyDay = $2;
	    yyYear = $4;
	}
	| tUNUMBER tMONTH {
	    yyMonth = $2;
	    yyDay = $1;
	}
	| tUNUMBER tMONTH tUNUMBER {
	    yyMonth = $2;
	    yyDay = $1;
	    yyYear = $3;
	}
	;

rel	: relunit tAGO {
	    yyRelSeconds = -yyRelSeconds;
	    yyRelMonth = -yyRelMonth;
	}
	| relunit
	;

relunit	: tUNUMBER tMINUTE_UNIT {
	    yyRelSeconds += $1 * $2 * 60L;
	}
	| tSNUMBER tMINUTE_UNIT {
	    yyRelSeconds += $1 * $2 * 60L;
	}
	| tMINUTE_UNIT {
	    yyRelSeconds += $1 * 60L;
	}
	| tSNUMBER tSEC_UNIT {
	    yyRelSeconds += $1;
	}
	| tUNUMBER tSEC_UNIT {
	    yyRelSeconds += $1;
	}
	| tSEC_UNIT {
	    yyRelSeconds++;
	}
	| tSNUMBER tMONTH_UNIT {
	    yyRelMonth += $1 * $2;
	}
	| tUNUMBER tMONTH_UNIT {
	    yyRelMonth += $1 * $2;
	}
	| tMONTH_UNIT {
	    yyRelMonth += $1;
	}
	;

number	: tUNUMBER {
	    if (yyHaveTime && yyHaveDate && !yyHaveRel)
		yyYear = $1;
	    else {
		yyHaveTime++;
		if ($1 < 100) {
		    yyHour = $1;
		    yyMinutes = 0;
		}
		else {
		    yyHour = $1 / 100;
		    yyMinutes = $1 % 100;
		}
		yySeconds = 0;
		yyMeridian = MER24;
	    }
	}
	;

o_merid	: /* NULL */ {
	    $$ = MER24;
	}
	| tMERIDIAN {
	    $$ = $1;
	}
	;

%%

/* Month and day table. */
static TABLE	MonthDayTable[] = {
    { "january",	tMONTH,  1 },
    { "february",	tMONTH,  2 },
    { "march",		tMONTH,  3 },
    { "april",		tMONTH,  4 },
    { "may",		tMONTH,  5 },
    { "june",		tMONTH,  6 },
    { "july",		tMONTH,  7 },
    { "august",		tMONTH,  8 },
    { "september",	tMONTH,  9 },
    { "sept",		tMONTH,  9 },
    { "october",	tMONTH, 10 },
    { "november",	tMONTH, 11 },
    { "december",	tMONTH, 12 },
    { "sunday",		tDAY, 0 },
    { "monday",		tDAY, 1 },
    { "tuesday",	tDAY, 2 },
    { "tues",		tDAY, 2 },
    { "wednesday",	tDAY, 3 },
    { "wednes",		tDAY, 3 },
    { "thursday",	tDAY, 4 },
    { "thur",		tDAY, 4 },
    { "thurs",		tDAY, 4 },
    { "friday",		tDAY, 5 },
    { "saturday",	tDAY, 6 },
    { NULL }
};

/* Time units table. */
static TABLE	UnitsTable[] = {
    { "year",		tMONTH_UNIT,	12 },
    { "month",		tMONTH_UNIT,	1 },
    { "fortnight",	tMINUTE_UNIT,	14 * 24 * 60 },
    { "week",		tMINUTE_UNIT,	7 * 24 * 60 },
    { "day",		tMINUTE_UNIT,	1 * 24 * 60 },
    { "hour",		tMINUTE_UNIT,	60 },
    { "minute",		tMINUTE_UNIT,	1 },
    { "min",		tMINUTE_UNIT,	1 },
    { "second",		tSEC_UNIT,	1 },
    { "sec",		tSEC_UNIT,	1 },
    { NULL }
};

/* Assorted relative-time words. */
static TABLE	OtherTable[] = {
    { "tomorrow",	tMINUTE_UNIT,	1 * 24 * 60 },
    { "yesterday",	tMINUTE_UNIT,	-1 * 24 * 60 },
    { "today",		tMINUTE_UNIT,	0 },
    { "now",		tMINUTE_UNIT,	0 },
    { "last",		tUNUMBER,	-1 },
    { "this",		tMINUTE_UNIT,	0 },
    { "next",		tUNUMBER,	2 },
    { "first",		tUNUMBER,	1 },
/*  { "second",		tUNUMBER,	2 }, */
    { "third",		tUNUMBER,	3 },
    { "fourth",		tUNUMBER,	4 },
    { "fifth",		tUNUMBER,	5 },
    { "sixth",		tUNUMBER,	6 },
    { "seventh",	tUNUMBER,	7 },
    { "eighth",		tUNUMBER,	8 },
    { "ninth",		tUNUMBER,	9 },
    { "tenth",		tUNUMBER,	10 },
    { "eleventh",	tUNUMBER,	11 },
    { "twelfth",	tUNUMBER,	12 },
    { "ago",		tAGO,	1 },
    { NULL }
};

/* The timezone table. */
static TABLE	TimezoneTable[] = {
    { "gmt",	tZONE,     HOURN( 0) },	/* Greenwich Mean */
    { "ut",	tZONE,     HOURN( 0) },	/* Universal (Coordinated) */
    { "utc",	tZONE,     HOURN( 0) },
    { "wet",	tZONE,     HOURN( 0) },	/* Western European */
    { "bst",	tDAYZONE,  HOURN( 0) },	/* British Summer */
    { "wat",	tZONE,     HOURN( 1) },	/* West Africa */
    { "at",	tZONE,     HOURN( 2) },	/* Azores */
#if	0
    /* For completeness.  BST is also British Summer, and GST is
     * also Guam Standard. */
    { "bst",	tZONE,     HOURN( 3) },	/* Brazil Standard */
    { "gst",	tZONE,     HOURN( 3) },	/* Greenland Standard */
#endif
    { "nft",	tZONE,     HOURN(3.5) },	/* Newfoundland */
    { "nst",	tZONE,     HOURN(3.5) },	/* Newfoundland Standard */
    { "ndt",	tDAYZONE,  HOURN(3.5) },	/* Newfoundland Daylight */
    { "ast",	tZONE,     HOURN( 4) },	/* Atlantic Standard */
    { "adt",	tDAYZONE,  HOURN( 4) },	/* Atlantic Daylight */
    { "est",	tZONE,     HOURN( 5) },	/* Eastern Standard */
    { "edt",	tDAYZONE,  HOURN( 5) },	/* Eastern Daylight */
    { "cst",	tZONE,     HOURN( 6) },	/* Central Standard */
    { "cdt",	tDAYZONE,  HOURN( 6) },	/* Central Daylight */
    { "mst",	tZONE,     HOURN( 7) },	/* Mountain Standard */
    { "mdt",	tDAYZONE,  HOURN( 7) },	/* Mountain Daylight */
    { "pst",	tZONE,     HOURN( 8) },	/* Pacific Standard */
    { "pdt",	tDAYZONE,  HOURN( 8) },	/* Pacific Daylight */
    { "yst",	tZONE,     HOURN( 9) },	/* Yukon Standard */
    { "ydt",	tDAYZONE,  HOURN( 9) },	/* Yukon Daylight */
    { "hst",	tZONE,     HOURN(10) },	/* Hawaii Standard */
    { "hdt",	tDAYZONE,  HOURN(10) },	/* Hawaii Daylight */
    { "cat",	tZONE,     HOURN(10) },	/* Central Alaska */
    { "ahst",	tZONE,     HOURN(10) },	/* Alaska-Hawaii Standard */
    { "nt",	tZONE,     HOURN(11) },	/* Nome */
    { "idlw",	tZONE,     HOURN(12) },	/* International Date Line West */
    { "cet",	tZONE,     -HOURN(1) },	/* Central European */
    { "met",	tZONE,     -HOURN(1) },	/* Middle European */
    { "mewt",	tZONE,     -HOURN(1) },	/* Middle European Winter */
    { "mest",	tDAYZONE,  -HOURN(1) },	/* Middle European Summer */
    { "swt",	tZONE,     -HOURN(1) },	/* Swedish Winter */
    { "sst",	tDAYZONE,  -HOURN(1) },	/* Swedish Summer */
    { "fwt",	tZONE,     -HOURN(1) },	/* French Winter */
    { "fst",	tDAYZONE,  -HOURN(1) },	/* French Summer */
    { "eet",	tZONE,     -HOURN(2) },	/* Eastern Europe, USSR Zone 1 */
    { "bt",	tZONE,     -HOURN(3) },	/* Baghdad, USSR Zone 2 */
    { "it",	tZONE,     -HOURN(3.5) },/* Iran */
    { "zp4",	tZONE,     -HOURN(4) },	/* USSR Zone 3 */
    { "zp5",	tZONE,     -HOURN(5) },	/* USSR Zone 4 */
    { "ist",	tZONE,     -HOURN(5.5) },/* Indian Standard */
    { "zp6",	tZONE,     -HOURN(6) },	/* USSR Zone 5 */
#if	0
    /* For completeness.  NST is also Newfoundland Stanard, nad SST is
     * also Swedish Summer. */
    { "nst",	tZONE,     -HOURN(6.5) },/* North Sumatra */
    { "sst",	tZONE,     -HOURN(7) },	/* South Sumatra, USSR Zone 6 */
#endif	/* 0 */
    { "wast",	tZONE,     -HOURN(7) },	/* West Australian Standard */
    { "wadt",	tDAYZONE,  -HOURN(7) },	/* West Australian Daylight */
    { "jt",	tZONE,     -HOURN(7.5) },/* Java (3pm in Cronusland!) */
    { "cct",	tZONE,     -HOURN(8) },	/* China Coast, USSR Zone 7 */
    { "jst",	tZONE,     -HOURN(9) },	/* Japan Standard, USSR Zone 8 */
    { "cast",	tZONE,     -HOURN(9.5) },/* Central Australian Standard */
    { "cadt",	tDAYZONE,  -HOURN(9.5) },/* Central Australian Daylight */
    { "east",	tZONE,     -HOURN(10) },	/* Eastern Australian Standard */
    { "eadt",	tDAYZONE,  -HOURN(10) },	/* Eastern Australian Daylight */
    { "gst",	tZONE,     -HOURN(10) },	/* Guam Standard, USSR Zone 9 */
    { "nzt",	tZONE,     -HOURN(12) },	/* New Zealand */
    { "nzst",	tZONE,     -HOURN(12) },	/* New Zealand Standard */
    { "nzdt",	tDAYZONE,  -HOURN(12) },	/* New Zealand Daylight */
    { "idle",	tZONE,     -HOURN(12) },	/* International Date Line East */
    {  NULL  }
};

/* Military timezone table. */
static TABLE	MilitaryTable[] = {
    { "a",	tZONE,	HOURN(  1) },
    { "b",	tZONE,	HOURN(  2) },
    { "c",	tZONE,	HOURN(  3) },
    { "d",	tZONE,	HOURN(  4) },
    { "e",	tZONE,	HOURN(  5) },
    { "f",	tZONE,	HOURN(  6) },
    { "g",	tZONE,	HOURN(  7) },
    { "h",	tZONE,	HOURN(  8) },
    { "i",	tZONE,	HOURN(  9) },
    { "k",	tZONE,	HOURN( 10) },
    { "l",	tZONE,	HOURN( 11) },
    { "m",	tZONE,	HOURN( 12) },
    { "n",	tZONE,	HOURN(- 1) },
    { "o",	tZONE,	HOURN(- 2) },
    { "p",	tZONE,	HOURN(- 3) },
    { "q",	tZONE,	HOURN(- 4) },
    { "r",	tZONE,	HOURN(- 5) },
    { "s",	tZONE,	HOURN(- 6) },
    { "t",	tZONE,	HOURN(- 7) },
    { "u",	tZONE,	HOURN(- 8) },
    { "v",	tZONE,	HOURN(- 9) },
    { "w",	tZONE,	HOURN(-10) },
    { "x",	tZONE,	HOURN(-11) },
    { "y",	tZONE,	HOURN(-12) },
    { "z",	tZONE,	HOURN(  0) },
    { NULL }
};




static int
yyerror(const char *s __unused)
{
	return (0);
}


static time_t
ToSeconds(Hours, Minutes, Seconds, Meridian)
    time_t	Hours;
    time_t	Minutes;
    time_t	Seconds;
    MERIDIAN	Meridian;
{
    if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 59)
	return -1;
    switch (Meridian) {
    case MER24:
	if (Hours < 0 || Hours > 23)
	    return -1;
	return (Hours * 60L + Minutes) * 60L + Seconds;
    case MERam:
	if (Hours < 1 || Hours > 12)
	    return -1;
	if (Hours != 12)
	    return (Hours * 60L + Minutes) * 60L + Seconds;
	else
	    return Minutes * 60L + Seconds;
    case MERpm:
	if (Hours < 1 || Hours > 12)
	    return -1;
	if (Hours != 12)
	    return ((Hours + 12) * 60L + Minutes) * 60L + Seconds;
	else
	    return (720L + Minutes) * 60L + Seconds;
    }
    /* NOTREACHED */
    return (-1);
}


static time_t
Convert(Month, Day, Year, Hours, Minutes, Seconds, Meridian, DSTmode)
    time_t	Month;
    time_t	Day;
    time_t	Year;
    time_t	Hours;
    time_t	Minutes;
    time_t	Seconds;
    MERIDIAN	Meridian;
    DSTMODE	DSTmode;
{
    static int	DaysInMonth[12] = {
	31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    };
    time_t	tod;
    time_t	Julian;
    time_t	i;

    if (Year < 0)
	Year = -Year;
    if (Year < 138)
	Year += 1900;
    DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)
		    ? 29 : 28;
    if (Year < EPOCH || Year > 2037
     || Month < 1 || Month > 12
     /* LINTED Month is a time_t so intermediate results aren't truncated */
     || Day < 1 || Day > DaysInMonth[(int)--Month])
	return -1;

    for (Julian = Day - 1, i = 0; i < Month; i++)
	Julian += DaysInMonth[i];
    for (i = EPOCH; i < Year; i++)
	Julian += 365 + (i % 4 == 0);
    Julian *= SECSPERDAY;
    Julian += yyTimezone * 60L;
    if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0)
	return -1;
    Julian += tod;
    if (DSTmode == DSTon
     || (DSTmode == DSTmaybe && localtime(&Julian)->tm_isdst))
	Julian -= 60 * 60;
    return Julian;
}


static time_t
DSTcorrect(Start, Future)
    time_t	Start;
    time_t	Future;
{
    time_t	StartDay;
    time_t	FutureDay;

    StartDay = (localtime(&Start)->tm_hour + 1) % 24;
    FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
    return (Future - Start) + (StartDay - FutureDay) * 60L * 60L;
}


static time_t
RelativeDate(Start, DayOrdinal, DayNumber)
    time_t	Start;
    time_t	DayOrdinal;
    time_t	DayNumber;
{
    struct tm	*tm;
    time_t	now;

    now = Start;
    tm = localtime(&now);
    now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7);
    now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
    return DSTcorrect(Start, now);
}


static time_t
RelativeMonth(Start, RelMonth)
    time_t	Start;
    time_t	RelMonth;
{
    struct tm	*tm;
    time_t	Month;
    time_t	Year;

    if (RelMonth == 0)
	return 0;
    tm = localtime(&Start);
    Month = 12 * tm->tm_year + tm->tm_mon + RelMonth;
    Year = Month / 12;
    Month = Month % 12 + 1;
    return DSTcorrect(Start,
	    Convert(Month, (time_t)tm->tm_mday, Year,
		(time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,
		MER24, DSTmaybe));
}


static int
LookupWord(buff)
    char		*buff;
{
    char	*p;
    char	*q;
    TABLE	*tp;
    uint_t	i;
    int		abbrev;

    /* Make it lowercase. */
    for (p = buff; *p; p++)
	if (isupper((u_char)*p))
	    *p = tolower(*p);

    if (strcmp(buff, "am") == 0 || strcmp(buff, "a.m.") == 0) {
	yylval.Meridian = MERam;
	return tMERIDIAN;
    }
    if (strcmp(buff, "pm") == 0 || strcmp(buff, "p.m.") == 0) {
	yylval.Meridian = MERpm;
	return tMERIDIAN;
    }

    /* See if we have an abbreviation for a month. */
    if (strlen(buff) == 3)
	abbrev = 1;
    else if (strlen(buff) == 4 && buff[3] == '.') {
	abbrev = 1;
	buff[3] = '\0';
    }
    else
	abbrev = 0;

    for (tp = MonthDayTable; tp->name; tp++) {
	if (abbrev) {
	    if (strncmp(buff, tp->name, 3) == 0) {
		yylval.Number = tp->value;
		return tp->type;
	    }
	}
	else if (strcmp(buff, tp->name) == 0) {
	    yylval.Number = tp->value;
	    return tp->type;
	}
    }

    for (tp = TimezoneTable; tp->name; tp++)
	if (strcmp(buff, tp->name) == 0) {
	    yylval.Number = tp->value;
	    return tp->type;
	}

    for (tp = UnitsTable; tp->name; tp++)
	if (strcmp(buff, tp->name) == 0) {
	    yylval.Number = tp->value;
	    return tp->type;
	}

    /* Strip off any plural and try the units table again. */
    i = strlen(buff) - 1;
    if (buff[i] == 's') {
	buff[i] = '\0';
	for (tp = UnitsTable; tp->name; tp++)
	    if (strcmp(buff, tp->name) == 0) {
		yylval.Number = tp->value;
		return tp->type;
	    }
    }

    for (tp = OtherTable; tp->name; tp++)
	if (strcmp(buff, tp->name) == 0) {
	    yylval.Number = tp->value;
	    return tp->type;
	}

    /* Military timezones. */
    if (buff[1] == '\0' && isalpha((u_char)*buff)) {
	for (tp = MilitaryTable; tp->name; tp++)
	    if (strcmp(buff, tp->name) == 0) {
		yylval.Number = tp->value;
		return tp->type;
	    }
    }

    /* Drop out any periods and try the timezone table again. */
    for (i = 0, p = q = buff; *q; q++)
	if (*q != '.')
	    *p++ = *q;
	else
	    i++;
    *p = '\0';
    if (i)
	for (tp = TimezoneTable; tp->name; tp++)
	    if (strcmp(buff, tp->name) == 0) {
		yylval.Number = tp->value;
		return tp->type;
	    }

    return tID;
}

void
pdateerr(p)
    char	*p;
{
    char	*name = "DATEMSK";	/* env variable for date format */
    char	*value;
    char	fmt[256], line[256];
    FILE	*fp;
    time_t	now;
    struct tm	*tm;

    value = getenv(name);
    if (value == (char *)0) {
	fprintf(stderr,
	    dgettext(domainname, "%s: Environment variable %s not set\n"),
		p, name);
	return;
    }
    switch (getdate_err) {
	case 0:
	default:
	    fprintf(stderr,
		dgettext(domainname, "%s: Unkown getdate() error\n"), p);
	    break;
	case 1:
	    fprintf(stderr,
		dgettext(domainname, "%s: %s null or undefined\n"), p, name);
	    break;
	case 2:
	    fprintf(stderr, dgettext(domainname,
		"%s: Cannot read template file %s\n"), p, value);
	    break;
	case 3:
	    fprintf(stderr, dgettext(domainname,
		"%s: Failed to get file status information\n"), p);
	    break;
	case 4:
	    fprintf(stderr, dgettext(domainname,
		"%s: Template file %s not a regular file\n"), p, value);
	    break;
	case 5:
	    fprintf(stderr, dgettext(domainname,
		"%s: Error reading template file %s\n"), p, value);
	    break;
	case 6:
	    fprintf(stderr, dgettext(domainname,
		"%s: %s failed\n"), p, "malloc()");
	    break;
	case 7:
	    fprintf(stderr, dgettext(domainname,
		"%s: Bad date/time format\n"), p);
	    fp = fopen(value, "r");
	    if (fp == (FILE *)0)
		break;
	    now = time((time_t *)0);
	    tm = localtime(&now);
	    fprintf(stderr, dgettext(domainname,
		"The following are examples of valid formats:\n"));
	    while (fgets(fmt, sizeof (fmt), fp)) {
		if (strchr(fmt, '%') == (char *)0)
		    continue;
		fprintf(stderr, "    ");
	        (void) strftime(line, sizeof (line), fmt, tm);
		fprintf(stderr, "%s", line);
	    }
	    (void) fclose(fp);
	    break;
	case 8:
	    (void) fprintf(stderr, dgettext(domainname,
		"%s: Invalid date specification\n"), p);
	    break;
    }
}

#undef yylex
static int
yylex()
{
    char	c;
    char	*p;
    char	buff[20];
    int		Count;
    int		sign;

    for ( ; ; ) {
	while (isspace((u_char)*yyInput))
	    yyInput++;

	if (isdigit((u_char)(c = *yyInput)) || c == '-' || c == '+') {
	    if (c == '-' || c == '+') {
		sign = c == '-' ? -1 : 1;
		if (!isdigit((u_char)*++yyInput))
		    /* skip the '-' sign */
		    continue;
	    }
	    else
		sign = 0;
	    yylval.Number = 0;
	    while (isdigit((u_char)(c = *yyInput++))) {
		int n;
		char digit = c;
		(void) sscanf(&digit, "%1d", &n);
		yylval.Number = 10 * yylval.Number + n;
	    }
	    yyInput--;
	    if (sign < 0)
		yylval.Number = -yylval.Number;
	    return sign ? tSNUMBER : tUNUMBER;
	}
	if (isalpha((u_char)c)) {
	    for (p = buff; isalpha((u_char)(c = *yyInput++)) || c == '.'; )
		if (p < &buff[sizeof (buff) - 1])
		    *p++ = c;
	    *p = '\0';
	    yyInput--;
	    return LookupWord(buff);
	}
	if (c != '(')
	    return *yyInput++;
	Count = 0;
	do {
	    c = *yyInput++;
	    if (c == '\0')
		return c;
	    if (c == '(')
		Count++;
	    else if (c == ')')
		Count--;
	} while (Count > 0);
    }
}


time_t
getreldate(p, now)
    char		*p;
    struct timeb	*now;
{
    struct tm		*tm;
    struct timeb	ftz;
    time_t		Start;
    time_t		tod;

    if (strcmp(setlocale(LC_TIME, NULL), "C")) {
	static char localedate[24];
	struct tm ltm;

	tm = getdate(p);
	if (getdate_err == 1 /* NODATEMASK */) {
	    char buffy[BUFSIZ];
	    time_t current;

	    printf(gettext("environment variable %s not set\n"), "DATEMSK");
	    do {
		time(&current);
		tm = localtime(&current);
		memcpy(&ltm, tm, sizeof(ltm));
		tm = &ltm;

		(void) fputs(gettext("Enter date as mmddhhmm[yy]: "), stdout);
		(void) fflush(stdout);
		if (fgets(buffy, sizeof (buffy), stdin) == NULL) {
			(void) printf(gettext("Encountered EOF on stdin\n"));
			return(-1);
		}
	    } while (sscanf(buffy, "%2d%2d%2d%2d%2d",
		&(tm->tm_mon), &(tm->tm_mday), &(tm->tm_hour),
		&(tm->tm_min), &(tm->tm_year)) < 4);

	    (tm->tm_mon)--;
	} else if (tm == NULL)
	    return(-1);

	(void)sprintf(localedate, "%d:%2.2d %d/%d %d",
	    tm->tm_hour, tm->tm_min, tm->tm_mon + 1,
	    tm->tm_mday, CHECK_TM(tm->tm_year));
	p = localedate;
    }

    yyInput = p;
    if (now == NULL) {
	now = &ftz;
	(void) time(&ftz.time);
	/* Set the timezone global. */
	tzset();
	/* LINTED timezone is time_t so intermediate results aren't truncated */
	ftz.timezone = (int) timezone / 60;
    }

    tm = localtime(&now->time);
    yyYear = tm->tm_year;
    yyMonth = tm->tm_mon + 1;
    yyDay = tm->tm_mday;
    yyTimezone = now->timezone;
    yyDSTmode = DSTmaybe;
    yyHour = tm->tm_hour;
    yyMinutes = tm->tm_min;
    yySeconds = tm->tm_sec;
    yyMeridian = MER24;
    yyRelSeconds = 0;
    yyRelMonth = 0;
    yyHaveDate = 0;
    yyHaveDay = 0;
    yyHaveRel = 0;
    yyHaveTime = 0;
    yyHaveZone = 0;

    if (yyparse()
     || yyHaveTime > 1 || yyHaveZone > 1 || yyHaveDate > 1 || yyHaveDay > 1)
	return -1;

    if (yyHaveDate || yyHaveTime || yyHaveDay) {
	Start = Convert(yyMonth, yyDay, yyYear, yyHour, yyMinutes, yySeconds,
		    yyMeridian, yyDSTmode);
	if (Start < 0)
	    return -1;
    }
    else {
	Start = now->time;
	if (!yyHaveRel)
	    Start -= ((tm->tm_hour * 60L) + tm->tm_min * 60L) + tm->tm_sec;
    }

    Start += yyRelSeconds;
    Start += RelativeMonth(Start, yyRelMonth);

    if (yyHaveDay && !yyHaveDate) {
	tod = RelativeDate(Start, yyDayOrdinal, yyDayNumber);
	Start += tod;
    }

    /* Have to do *something* with a legitimate -1 so it's distinguishable
     * from the error return value.  (Alternately could set errno on error.) */
    return Start == -1 ? 0 : Start;
}

#if	defined(TEST)

/* ARGSUSED */
main(ac, av)
    int		ac;
    char	*av[];
{
    char	buff[128];
    time_t	d;

    (void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN "SYS_TEST"
#endif
    (void) textdomain(TEXT_DOMAIN);

    (void) printf(gettext("Enter date, or blank line to exit.\n\t> "));
    (void) fflush(stdout);
    while (gets(buff) && buff[0]) {
	d = getreldate(buff, (struct timeb *)NULL);
	if (d == -1)
	    (void) printf(gettext("Bad format - couldn't convert.\n"));
	else {
	    (void) cftime(buff, "%c\n", &d);
	    (void) printf("%s", buff);
	}
	(void) printf("\t> ");
	(void) fflush(stdout);
    }
    exit(0);
    /* NOTREACHED */
}
#endif	/* defined(TEST) */
/^"operator_xdr.c.*variable unused in function/d
/yaccpar/d
/getdate.y.*include file.*unnecessary/d
/different definitions.*CLK_TCK/d
/lint suppression directive not used/d
/no corresponding .h file/d
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1998 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#include <stdlib.h>
#include <errno.h>
#include <libintl.h>
#include <string.h>
#include "memutils.h"

extern void msg(const char *, ...);
extern void dumpabort(void);

void *
xmalloc(bytes)
	size_t bytes;
{
	void *cp;

	cp = malloc(bytes);
	if (cp == NULL) {
		int saverr = errno;
		msg(gettext("Cannot allocate memory: %s\n"), strerror(saverr));
		dumpabort();
	}
	return (cp);
}

void *
xcalloc(nelem, size)
	size_t nelem;
	size_t size;
{
	void *cp;

	cp = calloc(nelem, size);
	if (cp == NULL) {
		int saverr = errno;
		msg(gettext("Cannot allocate memory: %s\n"), strerror(saverr));
		dumpabort();
	}
	return (cp);
}

void *
xrealloc(allocated, newsize)
	void *allocated;
	size_t newsize;
{
	void *cp;

	/* LINTED realloc knows what to do with a NULL pointer */
	cp = realloc(allocated, newsize);
	if (cp == NULL) {
		int saverr = errno;
		msg(gettext("Cannot allocate memory: %s\n"), strerror(saverr));
		dumpabort();
	}
	return (cp);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 1999 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

/*
 * Portions of this source code were derived from Berkeley 4.3 BSD
 * under license from the Regents of the University of California.
 */

#include <myrcmd.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <pwd.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/file.h>
#include <signal.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <netdb.h>
#include <fcntl.h>
#include <libintl.h>

#include <memutils.h>

#define	index(s, c)		strchr(s, c)
char	*strchr();

char	*inet_ntoa();

char myrcmd_stderr[1024];

int
myrcmd(char **ahost, unsigned short rport, char *locuser, char *remuser,
    char *cmd)
{
	uint_t loclen, remlen, cmdlen;
	int s, timo, retval;
	int tries = 0;
	pid_t pid;
	struct sockaddr_in sin;
	char c;
	int lport;
	int saverr;
	struct hostent *hp;
	sigset_t oldmask;
	sigset_t newmask;
	struct sigaction oldaction;
	struct sigaction newaction;
	static struct hostent numhp;
	static char numhostname[32];	/* big enough for "255.255.255.255" */
	struct in_addr numaddr;
	struct in_addr *numaddrlist[2];

	myrcmd_stderr[0] = '\0';	/* empty error string */
	pid = getpid();
	hp = gethostbyname(*ahost);
	if (hp == 0) {
		char *straddr;

		bzero((char *)numaddrlist, sizeof (numaddrlist));
		if ((numaddr.s_addr = inet_addr(*ahost)) == (in_addr_t)-1) {
			(void) snprintf(myrcmd_stderr, sizeof (myrcmd_stderr),
			    gettext("%s: unknown host\n"), *ahost);
			return (MYRCMD_NOHOST);
		} else {
			bzero((char *)&numhp, sizeof (numhp));
			bzero(numhostname, sizeof (numhostname));

			if ((straddr = inet_ntoa(numaddr)) == (char *)0) {
				(void) snprintf(myrcmd_stderr,
				    sizeof (myrcmd_stderr),
				    gettext("%s: unknown host\n"), *ahost);
				return (MYRCMD_NOHOST);
			}
			(void) strncpy(numhostname, straddr,
			    sizeof (numhostname));
			numhostname[sizeof (numhostname) - 1] = '\0';
			numhp.h_name = numhostname;
			numhp.h_addrtype = AF_INET;
			numhp.h_length = sizeof (numaddr);
			numaddrlist[0] = &numaddr;
			numaddrlist[1] = NULL;
			numhp.h_addr_list = (char **)numaddrlist;
			hp = &numhp;
		}
	}
	*ahost = hp->h_name;

	/* This provides a bounds-test for the bcopy()s below. */
	if ((unsigned)(hp->h_length) > sizeof (sin.sin_addr)) {
		(void) snprintf(myrcmd_stderr, sizeof (myrcmd_stderr),
		    gettext("rcmd: address size: %d larger than limit %d\n"),
		    hp->h_length, sizeof (sin.sin_addr));
		return (MYRCMD_EBAD);
	}

	/* ignore SIGPIPE */
	bzero((char *)&newaction, sizeof (newaction));
	newaction.sa_handler = SIG_IGN;
	newaction.sa_flags = SA_ONSTACK;
	(void) sigaction(SIGPIPE, &newaction, &oldaction);

	/* block SIGURG */
	bzero((char *)&newmask, sizeof (newmask));
	(void) sigaddset(&newmask, SIGURG);
	(void) sigprocmask(SIG_BLOCK, &newmask, &oldmask);
again:
	timo = 1;
	/*
	 * Use 0 as lport means that rresvport() will bind to a port in
	 * the anonymous priviledged port range.
	 */
	lport = 0;
	for (;;) {
		s = rresvport(&lport);
		if (s < 0) {
			int err;

			if (errno == EAGAIN) {
				(void) snprintf(myrcmd_stderr,
				    sizeof (myrcmd_stderr),
				    gettext("socket: All ports in use\n"));
				err = MYRCMD_ENOPORT;
			} else {
				saverr = errno;
				(void) snprintf(myrcmd_stderr,
				    sizeof (myrcmd_stderr),
				    gettext("rcmd: socket: %s\n"),
				    strerror(saverr));
				err = MYRCMD_ENOSOCK;
			}
			/* restore original SIGPIPE handler */
			(void) sigaction(SIGPIPE, &oldaction,
			    (struct sigaction *)0);

			/* restore original signal mask */
			(void) sigprocmask(SIG_SETMASK, &oldmask,
			    (sigset_t *)0);
			return (err);
		}
		/* Can't fail, according to fcntl(2) */
		(void) fcntl(s, F_SETOWN, pid);
		sin.sin_family = hp->h_addrtype;
		bcopy(hp->h_addr_list[0], (caddr_t)&sin.sin_addr, hp->h_length);
		sin.sin_port = rport;
		if (connect(s, (struct sockaddr *)&sin, sizeof (sin)) >= 0)
			break;
		saverr = errno;
		(void) close(s);
		if (saverr == EADDRINUSE) {
			continue;
		}
		if (saverr == ECONNREFUSED && timo <= 16) {
			sleep(timo);
			timo *= 2;
			continue;
		}
		if (hp->h_addr_list[1] != NULL) {
			saverr = errno;

			fprintf(stderr,
			    gettext("connect to address %s: "),
			    inet_ntoa(sin.sin_addr));
			errno = saverr;
			perror(0);
			hp->h_addr_list++;
			bcopy(hp->h_addr_list[0], (caddr_t)&sin.sin_addr,
			    hp->h_length);
			fprintf(stderr, gettext("Trying %s...\n"),
				inet_ntoa(sin.sin_addr));
			continue;
		}
		(void) snprintf(myrcmd_stderr, sizeof (myrcmd_stderr),
		    "%s: %s\n", hp->h_name, strerror(saverr));
		/* restore original SIGPIPE handler */
		(void) sigaction(SIGPIPE, &oldaction,
		    (struct sigaction *)0);

		/* restore original signal mask */
		(void) sigprocmask(SIG_SETMASK, &oldmask, (sigset_t *)0);
		return (MYRCMD_ENOCONNECT);
	}
	if (write(s, "", 1) < 0) {
		(void) close(s);
		return (MYRCMD_ENOCONNECT);
	}

	loclen = strlen(locuser) + 1;
	remlen = strlen(remuser) + 1;
	cmdlen = strlen(cmd) + 1;

	if (((retval = write(s, locuser, loclen)) != loclen) ||
	    ((retval = write(s, remuser, remlen)) != remlen) ||
	    ((retval = write(s, cmd, cmdlen)) != cmdlen)) {
		if (retval == -1)
			(void) snprintf(myrcmd_stderr, sizeof (myrcmd_stderr),
			    "write: %s\n", strerror(errno));
		else
			(void) snprintf(myrcmd_stderr, sizeof (myrcmd_stderr),
			    gettext("write unexpectedly truncated\n"));
		goto bad;
	}
	retval = read(s, &c, 1);
	if (retval != 1) {
		if (retval == 0) {
			/*
			 * Solaris 2.0 bug alert.  Sometimes, if the
			 * tapehost is a Solaris 2.0 system, the connection
			 * will be dropped at this point.  Let's try again,
			 * three times, before we throw in the towel.
			 */
			if (++tries < 3) {
				(void) close(s);
				goto again;
			}
			(void) snprintf(myrcmd_stderr, sizeof (myrcmd_stderr),
			    gettext("Protocol error, %s closed connection\n"),
			    *ahost);
		} else if (retval < 0) {
			(void) snprintf(myrcmd_stderr, sizeof (myrcmd_stderr),
			    "%s: %s\n", *ahost, strerror(errno));
		} else {
			(void) snprintf(myrcmd_stderr, sizeof (myrcmd_stderr),
			    gettext("Protocol error, %s sent %d bytes\n"),
			    *ahost, retval);
		}
		goto bad;
	}
	if (c != 0) {
		char *cp = myrcmd_stderr;
		char *ecp = &myrcmd_stderr[sizeof (myrcmd_stderr) - 1];

		while (read(s, &c, 1) == 1) {
			*cp++ = c;
			if (c == '\n' || cp >= ecp)
				break;
		}
		*cp = '\0';
		goto bad;
	}
	/* restore original SIGPIPE handler */
	(void) sigaction(SIGPIPE, &oldaction, (struct sigaction *)0);

	/* restore original signal mask */
	(void) sigprocmask(SIG_SETMASK, &oldmask, (sigset_t *)0);
	return (s);
bad:
	(void) close(s);
	/* restore original SIGPIPE handler */
	(void) sigaction(SIGPIPE, &oldaction, (struct sigaction *)0);

	/* restore original signal mask */
	(void) sigprocmask(SIG_SETMASK, &oldmask, (sigset_t *)0);
	return (MYRCMD_EBAD);
}
/*LINTLIBRARY*/
/*PROTOLIB1*/
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */
/*
 * Copyright (c) 1980 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#include <myrcmd.h>
#include <stdio.h>
#include <locale.h>
#include <ctype.h>
#include <pwd.h>
#include <string.h>
#include <signal.h>
#include <sys/mtio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include <locale.h>
#include <stdlib.h>
#include <errno.h>
#include <rmt.h>
#include <libintl.h>

#define	sigvec		sigaction
#define	sv_handler	sa_handler

#include <netinet/in.h>

extern	int32_t	tp_bsize;

#define	TS_CLOSED	0
#define	TS_OPEN		1

static int	rmtstate = TS_CLOSED;
static int	rmtape = -1;
static int	rmtversion = 0;
static char	*rmtpeer, *rmtpeer_malloc;
static uint_t	ntrec;			/* blocking factor on tape */

static char *domainname = "hsm_libdump";	/* for dgettext() */

#ifdef __STDC__
static void rmtmsg(const char *, ...);	/* package print routine */
static void rmtconnaborted(int);
static void rmtgetconn(void);
static int rmtstatus_extended(struct mtget *);
static int rmtioctl_extended(int, long);
static int map_extended_ioctl(int);
static int okname(char *);
static int rmtcall(char *, char *);
static int rmtreply(char *);
static int rmtpush(char *, uint_t);
static void rmtgets(char *, int);

static void (*print)(const char *, ...);	/* print routine */
static void (*Exit)(int);			/* exit routine */
#else
static void rmtmsg();
static void rmtconnaborted();
static void rmtgetconn();
static int okname();
static int rmtstatus_extended();
static int rmtioctl_extended();
static int map_extended_ioctl();
static int rmtcall();
static int rmtreply();
static int rmtpush();
static void rmtgets();

static void (*print)();
static void (*Exit)();
#endif

/*
 * Get a program-specific print and exit routine into
 * the package.  This is primarily for dump's benefit.
 * This routine is optional -- if not called the two
 * default to fprintf(stderr) and exit.
 */
#ifdef __STDC__
void
rmtinit(
	void (*errmsg)(const char *, ...),	/* print routine */
	void (*errexit)(int))			/* exit routine */
#else
void
rmtinit(void (*errmsg)(), void (*errexit)())
#endif
{
	print = errmsg;
	Exit = errexit;
}

int
rmthost(char *host, uint_t blocksize)
{
	struct sigvec sv;

#ifdef __STDC__
	if (print == (void (*)(const char *, ...))0)
#else
	if (print == (void (*)())0)
#endif
		print = rmtmsg;
#ifdef __STDC__
	if (Exit == (void (*)(int))0)
#else
	if (Exit == (void (*)())0)
#endif
		Exit = exit;
	if (rmtape >= 0 && rmtstate != TS_OPEN) {
		(void) close(rmtape);
		rmtape = -1;
	}
	if (rmtpeer_malloc)
		(void) free(rmtpeer_malloc);
	rmtpeer = rmtpeer_malloc = strdup(host);
	if (rmtpeer == (char *)0)
		return (0);
	ntrec = blocksize;
	sv.sa_flags = SA_RESTART;
	(void) sigemptyset(&sv.sa_mask);
	sv.sv_handler = rmtconnaborted;
	(void) sigvec(SIGPIPE, &sv, (struct sigvec *)0);
	rmtgetconn();
	if (rmtape < 0)
		return (0);
	return (1);
}

/*ARGSUSED*/
static void
rmtconnaborted(int sig)
{
	print(dgettext(domainname, "Lost connection to remote host.\n"));
	Exit(1);
}

static void
#ifdef __STDC__
rmtgetconn(void)
#else
rmtgetconn()
#endif
{
	static struct servent *sp = 0;
	static struct passwd *pwd = 0;
	char *tuser, *host, *device;
	uint_t size;

	if (sp == 0) {
		sp = getservbyname("shell", "tcp");
		if (sp == 0) {
			print(dgettext(domainname,
				"shell/tcp: unknown service\n"));
			Exit(1);
		}
		pwd = getpwuid(getuid());
		if (pwd == 0) {
			print(dgettext(domainname,
				"Cannot find password entry for uid %d\n"),
				getuid());
			Exit(1);
		}
	}
	/* Was strrchr(), be consistent with dump */
	host = strchr(rmtpeer, '@');
	if (host) {
		tuser = rmtpeer;
		*host++ = 0;
		rmtpeer = host;
		if (!okname(tuser))
			Exit(1);
	} else {
		host = rmtpeer;
		tuser = pwd->pw_name;
	}
	/* Was strrchr() - be consistent with dump and restore */
	device = strchr(host, ':');
	if (device)
		*device = 0;	/* throw away device name */
	/*
	 * myrcmd() replaces the contents of rmtpeer with a pointer
	 * to a static copy of the canonical host name.  However,
	 * since we never refer to rmtpeer again (other than to
	 * overwrite it in the next rmthost() invocation), we don't
	 * really care.
	 */
	/* LINTED sp->s_port is an int, even though port numbers are 1..65535 */
	rmtape = myrcmd(&rmtpeer, (ushort_t)sp->s_port, pwd->pw_name,
			tuser, "/etc/rmt");
	if (rmtape < 0) {
		if (*myrcmd_stderr)
			print("%s", myrcmd_stderr);
	} else {
		size = ntrec * tp_bsize;
		while (size > tp_bsize &&
		    setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, (char *)&size,
		    sizeof (size)) < 0)
			size -= tp_bsize;
	}
}

static int
okname(char *cp0)
{
	char *cp;
	uchar_t c;

	for (cp = cp0; *cp; cp++) {
		c = (uchar_t)*cp;
		if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
			print(dgettext(domainname,
				"invalid user name %s\n"), cp0);
			return (0);
		}
	}
	return (1);
}

int
rmtopen(char *tape, int mode)
{
	struct mtget mt;
	char buf[256];
	int fd;

	(void) snprintf(buf, sizeof (buf), "O%s\n%d\n", tape, mode);
	rmtstate = TS_OPEN;
	fd = rmtcall(tape, buf);
	if (fd != -1) {
		/* see if the rmt server supports the extended protocol */
		rmtversion = rmtioctl(-1, 0);

		/*
		 * Some rmt daemons apparently close the connection
		 * when they get a bogus ioctl.  See 1210852 (ignore
		 * the evaluation).  Make sure we can still talk to
		 * the device, re-opening it if necessary.
		 */
		if (rmtversion < 1) {
			if (rmtstatus(&mt) < 0) {
				rmtclose();
				rmtgetconn();
				rmtversion = 0;
			}
		}
	}
	return (fd);
}

void
#ifdef __STDC__
rmtclose(void)
#else
rmtclose()
#endif
{
	if (rmtstate != TS_OPEN)
		return;
	(void) rmtcall("close", "C\n");
	rmtstate = TS_CLOSED;
}

int
rmtstatus(struct mtget *mt)
{
	char *buf = (char *)mt;
	int n, i, cc;

	if (rmtversion > 0)
		return (rmtstatus_extended(mt));

	n = rmtcall("status", "S");
	if (n < 0) {
		return (-1);
	}
	if ((unsigned)n > sizeof (*mt)) {
		print(dgettext(domainname,
		    "rmtstatus: expected response size %d, got %d\n"),
		    sizeof (struct mtget), n);
		print(dgettext(domainname,
		    "This means the remote rmt daemon is not compatible.\n"));
		rmtconnaborted(0);
	}
	i = 0;
	while (i < n) {
		cc = read(rmtape, buf+i, n - i);
		if (cc <= 0)
			rmtconnaborted(0);
		i += cc;
	}
	return (n);
}

static int
rmtstatus_extended(struct mtget *mt)
{
	if ((mt->mt_type = rmtcall("status", "sT")) == -1)
		return (-1);
	mt->mt_dsreg = rmtcall("status", "sD");
	mt->mt_erreg = rmtcall("status", "sE");
	mt->mt_resid = rmtcall("status", "sR");
	mt->mt_fileno = rmtcall("status", "sF");
	mt->mt_blkno = rmtcall("status", "sB");
	mt->mt_flags = rmtcall("status", "sf");
	mt->mt_bf = rmtcall("status", "sb");
	return (0);
}

int
rmtread(char *buf, uint_t count)
{
	char line[30];
	int n, i, cc;

	(void) snprintf(line, sizeof (line), "R%d\n", count);
	n = rmtcall("read", line);
	if (n < 0) {
		return (-1);
	}
	if (n > count) {
		print(dgettext(domainname,
		    "rmtread: expected response size %d, got %d\n"),
		    count, n);
		print(dgettext(domainname,
		    "This means the remote rmt daemon is not compatible.\n"));
		rmtconnaborted(0);
	}
	i = 0;
	while (i < n) {
		cc = read(rmtape, buf+i, n - i);
		if (cc <= 0)
			rmtconnaborted(0);
		i += cc;
	}
	return (n);
}

int
rmtwrite(char *buf, uint_t count)
{
	int retval;
	char line[64];		/* numbers can get big */

	(void) snprintf(line, sizeof (line), "W%d\n", count);
	retval = rmtpush(line, strlen(line));
	if (retval <= 0)
		return (-1);

	retval = rmtpush(buf, count);
	if (retval <= 0)
		return (-1);

	return (rmtreply("write"));
}

int
rmtpush(char *buf, uint_t count)
{
	int retval;

	do {
		retval = write(rmtape, buf, count);
		buf += retval;
		count -= retval;
	} while (count && retval > 0);

	return (retval);
}

int
rmtseek(int offset, int pos)
{
	char line[80];

	(void) snprintf(line, sizeof (line), "L%d\n%d\n", offset, pos);
	return (rmtcall("seek", line));
}

int
rmtioctl(int cmd, long count)
{
	char buf[256];
	int xcmd;

	if (count < 0)
		return (-1);

	if ((xcmd = map_extended_ioctl(cmd)) != -1)
		return (rmtioctl_extended(xcmd, count));

	(void) snprintf(buf, sizeof (buf), "I%d\n%ld\n", cmd, count);
	return (rmtcall("ioctl", buf));
}

/*
 * Map from the standard Sun ioctl commands into the extended version,
 * if possible.
 */
static int
map_extended_ioctl(int cmd)
{
	int xcmd;

	if (rmtversion <= 0)
		return (-1);		/* extended protocol not supported */

	switch (cmd) {
	case MTRETEN:
		xcmd = 2;
		break;
	case MTERASE:
		xcmd = 3;
		break;
	case MTEOM:
		xcmd = 4;
		break;
	case MTNBSF:
		xcmd = 5;
		break;
	default:
		xcmd = -1;		/* not supported */
		break;
	}
	return (xcmd);
}

static int
rmtioctl_extended(int cmd, long count)
{
	char buf[256];

	(void) snprintf(buf, sizeof (buf), "i%d\n%ld\n", cmd, count);
	return (rmtcall("ioctl", buf));
}

static int
rmtcall(char *cmd, char *buf)
{
	if (rmtpush(buf, strlen(buf)) != strlen(buf))
		rmtconnaborted(0);
	return (rmtreply(cmd));
}

static int
rmtreply(char *cmd)
{
	char code[30], emsg[BUFSIZ];

	rmtgets(code, sizeof (code));
	if (*code == 'E' || *code == 'F') {
		rmtgets(emsg, sizeof (emsg));
		/*
		 * don't print error message for ioctl or status;
		 * or if we are opening up a full path (i.e. device)
		 * and the tape is not loaded (EIO error)
		 */
		if (strcmp(cmd, "ioctl") != 0 &&
		    strcmp(cmd, "status") != 0 &&
		    !(cmd[0] == '/' && atoi(code + 1) == EIO))
			print("%s: %s\n", cmd, emsg);
		errno = atoi(code + 1);
		if (*code == 'F') {
			rmtstate = TS_CLOSED;
			return (-1);
		}
		return (-1);
	}
	if (*code != 'A') {
		print(dgettext(domainname,
			"Protocol to remote tape server botched (code %s?).\n"),
			code);
		rmtconnaborted(0);
	}
	return (atoi(code + 1));
}

static void
rmtgets(char *cp, int len)
{
	int i, n;

	n = recv(rmtape, cp, len-1, MSG_PEEK);
	for (i = 0; i < n; i++)
		if (cp[i] == '\n')
			break;
	n = i + 1;			/* characters to read at once */
	for (i = 0; i < len; i += n, n = 1) {
		n = read(rmtape, cp, n);
		if (n <= 0)
			rmtconnaborted(0);
		cp += n;
		if (cp[-1] == '\n') {
			cp[-1] = '\0';
			return;
		}
	}
	print(dgettext(domainname,
		"Protocol to remote tape server botched (in rmtgets).\n"));
	rmtconnaborted(0);
}

#ifdef __STDC__
#include <stdarg.h>

/* VARARGS1 */
static void
rmtmsg(const char *fmt, ...)
{
	va_list	args;

	va_start(args, fmt);
	(void) vfprintf(stderr, fmt, args);
	(void) fflush(stderr);
}
#else
#include <varargs.h>

/* VARARGS */
static void
rmtmsg(va_dcl)
{
	va_list	args;
	char	*fmt;

	va_start(args);
	fmt = va_arg(args, char *);
	(void) vfprintf(stderr, fmt, args);
	(void) fflush(stderr);
}
#endif
#!/bin/sh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"%Z%%M%	%I%	%E% SMI"

echo_file usr/src/lib/Makefile.lib
echo_file usr/src/lib/Makefile.targ
echo_file usr/src/head/protocols/dumprestore.h
echo_file usr/src/cmd/fs.d/Makefile.fstype
find_files "s.*" usr/src/cmd/fs.d/ufs/roll_log
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# cmd/backup/restore/Makefile
#
# Copyright (c) 1983 Regents of the University of California.
# All rights reserved.  The Berkeley software License Agreement
# specifies the terms and conditions for redistribution.
#
#	CAUTION:  FSTYPE must be defined before including ../Makefile.backup,
#		  otherwise UTFROOTPKGUSRLIBFSTYPE doesn't get expanded
#		  properly and the magic doesn't happen.

FSTYPE=		ufs

UFSRESTORE=	ufsrestore

PROG=		$(UFSRESTORE)
ROOTFS_PROG=	$(PROG)
PRODUCT=	$(PROG)

include ../Makefile.backup

OBJECTS=	dirs.o interactive.o main.o restore.o \
		symtab.o tape.o utilities.o
SRCS=		$(OBJECTS:%.o=%.c)

POFILES=	$(OBJECTS:%.o=%.po) ../lib/libdump.po
POFILE=		ufsrestore.po

# XXX This is only needed for the check: target.  It would be nice to
# XXX automatically generate the list when needed.
HEADERS=	../../../head/protocols/dumprestore.h \
	../include/byteorder.h ../include/memutils.h \
	../include/rmt.h restore.h

CLOBBERFILES +=	$(PRODUCT) $(DEBUGPRODUCTS)

LOCAL=		.
GENERAL=	../include
GLOBAL=		../../../head
CPPFLAGS=	-I$(LOCAL) -I$(GENERAL) -I$(GLOBAL) \
			$(CPPFLAGS.master) -D_LARGEFILE64_SOURCE=1
# Hammerhead: Suppress pointer-related warnings in legacy restore code
CERRWARN +=	-Wno-incompatible-pointer-types
CERRWARN +=	-Wno-int-to-pointer-cast
LIBDUMP=	../lib/libdump.a
LINTLIBDUMP=	../lib/llib-ldump.ln
LDLIBS +=	$(BSTATIC) -L../lib -ldump $(BDYNAMIC) -lsocket -lnsl -lsec

UFSROOTLINK=	$(UFSROOTUSRSBIN)/$(PROG)
LINKVALUE=	../lib/fs/$(FSTYPE)/$(PROG)

FILEMODE=	04555

.KEEP_STATE:

all:		$(ROOTFS_PROG)

$(PROG):	$(OBJECTS) $$(LIBDUMP)
	$(LINK.c) -o $@ $(OBJECTS) $(LDLIBS)
	$(POST_PROCESS)

$(LIBDUMP): FRC
	$(DO_LIBDIR)

FRC:

install:	all $(UFSROOTPKGUSRLIBFSTYPE) $(UFSROOTLINK)

lint:	$(SRCS) $(LINTLIBDUMP)
	$(LINT.c) $(SRCS) $(LINTLIBDUMP) 2>&1 \
	| sed -f lint.sed | grep -v '^[ 	]'

$(LINTLIBDUMP):	FRC
	cd ../lib; pwd; $(MAKE) lint
	pwd

check:
	$(CSTYLE) $(CSTYLEFLAGS) $(SRCS) $(HEADERS)
	$(HDRCHK) $(HDRCHKFLAGS) $(HEADERS)

clean:
	$(RM) $(OBJECTS) $(DEBUGOBJS) *.ln

$(UFSROOTLINK):
	-$(RM) $@; $(SYMLINK) $(LINKVALUE) $(UFSROOTLINK)

$(POFILE):	$(POFILES)
	$(RM) $@; cat $(POFILES) > $@

../lib/libdump.po:
	cd ../lib ; pwd ; $(MAKE)  libdump.po

include ../Makefile.targ
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/

/*
 * Copyright (c) 1983 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#include "restore.h"
#include <byteorder.h>
#include <stdlib.h>
#include <unistd.h>
#include <utime.h>

struct context curfile;

/*
 * Symbol table of directories read from tape.
 */
#define	HASHSIZE	1000
#define	INOHASH(val) (val % HASHSIZE)
struct inotab {
	struct inotab *t_next;
	ino_t	t_ino;
	offset_t t_seekpt;
	offset_t t_size;
	struct inotab *t_xattr;
};
static struct inotab *inotab[HASHSIZE];
static struct inotab *xattrlist = NULL;

/*
 * Information retained about directories.
 */
static struct modeinfo {
	ino_t	ino;
	time_t	timep[2];
	mode_t	mode;
	uid_t	uid;
	gid_t	gid;
	size_t	metasize;
} node;

/*
 * Global variables for this file.
 */
static off64_t	g_seekpt;		/* some people have a local seekpt */
static FILE	*df, *mf;
static char	dirfile[MAXPATHLEN] = "#";	/* No file */
static char	modefile[MAXPATHLEN] = "#";	/* No file */

static RST_DIR	*dirp;

#define	INIT_TEMPFILE(name, type) \
	if (name[0] == '#') { \
		if (tmpdir == (char *)NULL) /* can't happen; be paranoid */ \
			tmpdir = "/tmp"; \
		(void) snprintf(name, sizeof (name), \
		    "%s/rst" type "%ld.XXXXXX", tmpdir, dumpdate); \
		(void) mktemp(name); \
	}

#define	INIT_DIRFILE()	INIT_TEMPFILE(dirfile, "dir")
#define	INIT_MODEFILE()	INIT_TEMPFILE(modefile, "mode")

/*
 * Format of old style directories.
 */
#define	ODIRSIZ 14
struct odirect {
	ushort_t d_ino;
	char	d_name[ODIRSIZ];
};

static ino_t search(ino_t, char	*);
static void putdir(char *, size_t);
static void putent(struct direct *);
static void skipmetadata(FILE *, size_t);
static void flushent(void);
static void dcvt(struct odirect *, struct direct *);
static RST_DIR *rst_initdirfile(char *);
static offset_t rst_telldir(RST_DIR *);
static void rst_seekdir(RST_DIR *, offset_t, offset_t);
static struct inotab *allocinotab(ino_t, struct dinode *, off64_t);
static void nodeflush(void);
static struct inotab *inotablookup(ino_t);

/*
 *	Extract directory contents, building up a directory structure
 *	on disk for extraction by name.
 *	If genmode is requested, save mode, owner, and times for all
 *	directories on the tape.
 */
void
extractdirs(int genmode)
{
	int ts;
	struct dinode *ip;
	int saverr;
	struct inotab *itp;
	struct direct nulldir;
	static char dotname[] = "."; /* dirlookup/psearch writes to its arg */

	vprintf(stdout, gettext("Extract directories from tape\n"));
	INIT_DIRFILE();
	if ((df = safe_fopen(dirfile, "w", 0600)) == (FILE *)NULL) {
		saverr = errno;
		(void) fprintf(stderr,
		    gettext("%s: %s - cannot create directory temporary\n"),
		    progname, dirfile);
		errno = saverr;
		perror("fopen");
		done(1);
	}
	if (genmode != 0) {
		INIT_MODEFILE();
		if ((mf = safe_fopen(modefile, "w", 0600)) == (FILE *)NULL) {
			saverr = errno;
			(void) fprintf(stderr,
			    gettext("%s: %s - cannot create modefile \n"),
			    progname, modefile);
			errno = saverr;
			perror("fopen");
			done(1);
		}
	}
	nulldir.d_ino = 0;
	nulldir.d_namlen = 1;
	(void) strcpy(nulldir.d_name, "/");
	/* LINTED DIRSIZ will always fit into a ushort_t */
	nulldir.d_reclen = (ushort_t)DIRSIZ(&nulldir);
	/* LINTED sign extension ok in assert */
	assert(DIRSIZ(&nulldir) == (ulong_t)nulldir.d_reclen);
	for (;;) {
		curfile.name = gettext("<directory file - name unknown>");
		curfile.action = USING;
		ip = curfile.dip;
		ts = curfile.ts;
		if (ts != TS_END && ts != TS_INODE) {
			getfile(null, null);
			continue;
		}
		if (ts == TS_INODE && ip == NULL) {
			(void) fprintf(stderr, gettext(
"%s: extractdirs: Failed internal consistency check, curfile.dip is NULL\n"),
			    progname);
			done(1);
		}
		if ((ts == TS_INODE && (ip->di_mode & IFMT) != IFDIR &&
		    (ip->di_mode & IFMT) != IFATTRDIR) ||
		    (ts == TS_END)) {
			(void) fflush(df);
			/* XXX Legitimate error, bad complaint string */
			if (ferror(df))
				panic("%s: %s\n", dirfile, strerror(errno));
			(void) fclose(df);
			rst_closedir(dirp);
			dirp = rst_initdirfile(dirfile);
			if (dirp == NULL)
				perror("initdirfile");
			if (mf != NULL) {
				(void) fflush(mf);
				/* XXX Legitimate error, bad complaint string */
				if (ferror(mf))
					panic("%s: %s\n",
					    modefile, strerror(errno));
				(void) fclose(mf);
			}
			if (dirlookup(dotname) == 0) {
				(void) fprintf(stderr, gettext(
				    "Root directory is not on tape\n"));
				done(1);
			}
			return;
		}
		itp = allocinotab(curfile.ino, ip, g_seekpt);
		getfile(putdir, null);
		if (mf != NULL)
			nodeflush();

		putent(&nulldir);
		flushent();
		itp->t_size = g_seekpt - itp->t_seekpt;
	}
}

/*
 * skip over all the directories on the tape
 */
void
skipdirs()
{
	while (curfile.dip != NULL &&
	    ((curfile.dip->di_mode & IFMT) == IFDIR ||
	    (curfile.dip->di_mode & IFMT) == IFATTRDIR)) {
		skipfile();
	}
}

/*
 *	Recursively find names and inumbers of all files in subtree
 *	pname and pass them off to be processed.
 */
void
treescan(char *pname, ino_t ino, long (*todo)())
{
	struct inotab *itp;
	struct direct *dp;
	uint_t loclen;
	offset_t bpt;
	char locname[MAXCOMPLEXLEN];

	itp = inotablookup(ino);
	if (itp == NULL) {
		/*
		 * Pname is name of a simple file or an unchanged directory.
		 */
		(void) (*todo)(pname, ino, LEAF);
		return;
	}
	/*
	 * Pname is a dumped directory name.
	 */
	if ((*todo)(pname, ino, NODE) == FAIL)
		return;
	/*
	 * begin search through the directory
	 * skipping over "." and ".."
	 */
	loclen = complexcpy(locname, pname, MAXCOMPLEXLEN);
	locname[loclen-1] = '/';
	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
	dp = rst_readdir(dirp); /* "." */

	if (dp != NULL && strcmp(dp->d_name, ".") == 0)
		dp = rst_readdir(dirp); /* ".." */
	else
		(void) fprintf(stderr,
		    gettext("Warning: `.' missing from directory %s\n"),
		    pname);
	if (dp != NULL && strcmp(dp->d_name, "..") == 0)
		dp = rst_readdir(dirp); /* first real entry */
	else
		(void) fprintf(stderr,
		    gettext("Warning: `..' missing from directory %s\n"),
		    pname);
	bpt = rst_telldir(dirp);
	/*
	 * a zero inode signals end of directory
	 */
	while (dp != NULL && dp->d_ino != 0) {
		locname[loclen] = '\0';
		if ((loclen + dp->d_namlen) >= (sizeof (locname) - 2)) {
			(void) fprintf(stderr,
			    gettext(
			    "%s%s: ignoring name that exceeds %d char\n"),
			    locname, dp->d_name, MAXCOMPLEXLEN);
		} else {
			/* Always fits by if() condition */
			(void) strcpy(locname + loclen, dp->d_name);
			/* put a double null on string for lookupname() */
			locname[loclen+dp->d_namlen+1] = '\0';
			treescan(locname, dp->d_ino, todo);
			rst_seekdir(dirp, bpt, itp->t_seekpt);
		}
		dp = rst_readdir(dirp);
		bpt = rst_telldir(dirp);
	}
	if (dp == NULL)
		(void) fprintf(stderr,
		    gettext("corrupted directory: %s.\n"), locname);
}

/*
 * Scan the directory table looking for extended attribute trees.
 * Recursively find names and inumbers in each tree and pass them
 * off to be processed.  If the always parameter is not set, only
 * process the attribute tree if the attribute tree parent is to
 * be extracted.
 */
void
attrscan(int always, long (*todo)())
{
	struct inotab *itp;
	struct entry *ep, *parent;
	struct direct *dp;
	char name[MAXCOMPLEXLEN];
	int len;

	for (itp = xattrlist; itp != NULL; itp = itp->t_xattr) {
		rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
		if ((dp = rst_readdir(dirp)) != NULL &&	/* "." */
		    (dp = rst_readdir(dirp)) != NULL &&	/* ".." */
		    strcmp(dp->d_name, "..") == 0) {
			if ((parent = lookupino(dp->d_ino)) != NULL) {
				if (!always &&
				    (parent->e_flags & (NEW|EXTRACT)) == 0)
					continue;
				len = complexcpy(name, myname(parent),
				    MAXCOMPLEXLEN - 3);
				name[len] = '.';
				name[len+1] = '\0';
				name[len+2] = '\0';
				inattrspace = 1;
				if ((ep = lookupino(itp->t_ino)) == NULL) {
					ep = addentry(name, itp->t_ino,
					    NODE|ROOT);
				}
				ep->e_flags |= XATTRROOT;
				treescan(name, itp->t_ino, todo);
				inattrspace = 0;
			} else {
				(void) fprintf(stderr,
				    gettext("Warning: orphaned attribute "
				    "directory\n"));
			}
		} else {
			(void) fprintf(stderr, gettext("Warning: `..' missing "
			    "from attribute directory\n"));
		}
	}
}

/*
 * Search the directory tree rooted at inode ROOTINO
 * for the path pointed at by n.  Note that n must be
 * modifiable, although it is returned in the same
 * condition it was given to us in.
 */
ino_t
psearch(char *n)
{
	char *cp, *cp1;
	ino_t ino;
	char c;

	ino = ROOTINO;
	if (*(cp = n) == '/')
		cp++;
next:
	cp1 = cp + 1;
	while (*cp1 != '/' && *cp1)
		cp1++;
	c = *cp1;
	*cp1 = 0;
	ino = search(ino, cp);
	if (ino == 0) {
		*cp1 = c;
		return (0);
	}
	*cp1 = c;
	if (c == '/') {
		cp = cp1+1;
		goto next;
	}
	return (ino);
}

/*
 * search the directory inode ino
 * looking for entry cp
 */
static ino_t
search(ino_t inum, char *cp)
{
	struct direct *dp;
	struct inotab *itp;
	uint_t len;

	itp = inotablookup(inum);
	if (itp == NULL)
		return (0);
	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
	len = strlen(cp);
	do {
		dp = rst_readdir(dirp);
		if (dp == NULL || dp->d_ino == 0)
			return (0);
	} while (dp->d_namlen != len || strncmp(dp->d_name, cp, len) != 0);
	return (dp->d_ino);
}

/*
 * Put the directory entries in the directory file
 */
static void
putdir(char *buf, size_t size)
{
	struct direct cvtbuf;
	struct odirect *odp;
	struct odirect *eodp;
	struct direct *dp;
	size_t loc, i;

	if (cvtflag) {
		/*LINTED [buf is char[] in getfile, size % fs_fsize == 0]*/
		eodp = (struct odirect *)&buf[size];
		/*LINTED [buf is char[] in getfile]*/
		for (odp = (struct odirect *)buf; odp < eodp; odp++)
			if (odp->d_ino != 0) {
				dcvt(odp, &cvtbuf);
				putent(&cvtbuf);
			}
	} else {
		loc = 0;
		while (loc < size) {
			/*LINTED [buf is char[] in getfile, loc % 4 == 0]*/
			dp = (struct direct *)(buf + loc);
			normdirect(byteorder, dp);
			i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
			if (dp->d_reclen == 0 || (long)dp->d_reclen > i) {
				loc += i;
				continue;
			}
			loc += dp->d_reclen;
			if (dp->d_ino != 0) {
				putent(dp);
			}
		}
	}
}

/*
 * These variables are "local" to the following two functions.
 */
static char dirbuf[DIRBLKSIZ];
static int32_t dirloc = 0;
static int32_t prev = 0;

/*
 * add a new directory entry to a file.
 */
static void
putent(struct direct *dp)
{
	/* LINTED DIRSIZ will always fit in a ushort_t */
	dp->d_reclen = (ushort_t)DIRSIZ(dp);
	/* LINTED sign extension ok in assert */
	assert(DIRSIZ(dp) == (ulong_t)dp->d_reclen);
	if (dirloc + (long)dp->d_reclen > DIRBLKSIZ) {
		/*LINTED [prev += dp->d_reclen, prev % 4 == 0]*/
		((struct direct *)(dirbuf + prev))->d_reclen =
		    DIRBLKSIZ - prev;
		(void) fwrite(dirbuf, 1, DIRBLKSIZ, df);
		if (ferror(df))
			panic("%s: %s\n", dirfile, strerror(errno));
		dirloc = 0;
	}
	bcopy((char *)dp, dirbuf + dirloc, (size_t)dp->d_reclen);
	prev = dirloc;
	dirloc += dp->d_reclen;
}

/*
 * flush out a directory that is finished.
 */
static void
flushent(void)
{

	/* LINTED prev += dp->d_reclen, prev % 4 == 0 */
	((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
	(void) fwrite(dirbuf, (size_t)dirloc, 1, df);
	if (ferror(df))
		panic("%s: %s\n", dirfile, strerror(errno));
	g_seekpt = ftello64(df);
	dirloc = 0;
}

static void
dcvt(struct odirect *odp, struct direct *ndp)
{

	(void) bzero((char *)ndp, sizeof (*ndp));
	ndp->d_ino =  odp->d_ino;
	/* Note that odp->d_name may not be null-terminated */
	/* LINTED assertion always true */
	assert(sizeof (ndp->d_name) > sizeof (odp->d_name));
	(void) strncpy(ndp->d_name, odp->d_name, sizeof (odp->d_name));
	ndp->d_name[sizeof (odp->d_name)] = '\0';
	/* LINTED: strlen will fit into d_namlen */
	ndp->d_namlen = strlen(ndp->d_name);

	/* LINTED sign extension ok in assert */
	assert(DIRSIZ(ndp) == (ulong_t)ndp->d_reclen);
	/* LINTED DIRSIZ always fits in ushort_t */
	ndp->d_reclen = (ushort_t)DIRSIZ(ndp);
}

/*
 * Initialize the directory file
 */
static RST_DIR *
rst_initdirfile(char *name)
{
	RST_DIR *dp;
	int fd;

	if ((fd = open(name, O_RDONLY | O_LARGEFILE)) == -1)
		return ((RST_DIR *)0);
	if ((dp = (RST_DIR *)malloc(sizeof (*dp))) == NULL) {
		(void) close(fd);
		return ((RST_DIR *)0);
	}
	dp->dd_fd = fd;
	dp->dd_loc = 0;
	dp->dd_refcnt = 1;
	return (dp);
}

/*
 * Simulate the opening of a directory
 */
RST_DIR *
rst_opendir(char *name)
{
	struct inotab *itp;
	ino_t ino;

	if ((ino = dirlookup(name)) > 0 &&
	    (itp = inotablookup(ino)) != NULL) {
		rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
		dirp->dd_refcnt++;
		return (dirp);
	}
	return ((RST_DIR *)0);
}

/*
 * Releases the hidden state created by rst_opendir().
 * Specifically, the dirp it provided to the caller is malloc'd.
 */
void
rst_closedir(RST_DIR *cdirp)
{
	if ((cdirp != NULL) && (--(cdirp->dd_refcnt) < 1))
		free(cdirp);
}

/*
 * return a pointer into a directory
 */
static offset_t
rst_telldir(RST_DIR *tdirp)
{
	offset_t pos = llseek(tdirp->dd_fd, (offset_t)0, SEEK_CUR);

	if (pos == (offset_t)-1) {
		perror("Could not determine position in directory file");
		done(1);
	}

	return ((pos - tdirp->dd_size) + tdirp->dd_loc);
}

/*
 * Seek to an entry in a directory.
 * Only values returned by ``rst_telldir'' should be passed to rst_seekdir.
 * This routine handles many directories in a single file.
 * It takes the base of the directory in the file, plus
 * the desired seek offset into it.
 */
static void
rst_seekdir(RST_DIR *sdirp, offset_t loc, offset_t base)
{

	if (loc == rst_telldir(sdirp))
		return;
	loc -= base;
	if (loc < 0)
		(void) fprintf(stderr,
		    gettext("bad seek pointer to rst_seekdir %d\n"), loc);
	(void) llseek(sdirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), 0);
	sdirp->dd_loc = loc & (DIRBLKSIZ - 1);
	if (sdirp->dd_loc != 0)
		sdirp->dd_size = read(sdirp->dd_fd, sdirp->dd_buf, DIRBLKSIZ);
}

/*
 * get next entry in a directory.
 */
struct direct *
rst_readdir(RST_DIR *rdirp)
{
	struct direct *dp;

	for (;;) {
		if (rdirp->dd_loc == 0) {
			rdirp->dd_size = read(rdirp->dd_fd, rdirp->dd_buf,
			    DIRBLKSIZ);
			if (rdirp->dd_size <= 0) {
				dprintf(stderr,
				    gettext("error reading directory\n"));
				return ((struct direct *)0);
			}
		}
		if (rdirp->dd_loc >= rdirp->dd_size) {
			rdirp->dd_loc = 0;
			continue;
		}
		/*LINTED [rvalue will be aligned on int boundary]*/
		dp = (struct direct *)(rdirp->dd_buf + rdirp->dd_loc);
		if (dp->d_reclen == 0 ||
		    (long)dp->d_reclen > (DIRBLKSIZ + 1 - rdirp->dd_loc)) {
			dprintf(stderr,
			    gettext("corrupted directory: bad reclen %d\n"),
			    dp->d_reclen);
			return ((struct direct *)0);
		}
		rdirp->dd_loc += dp->d_reclen;
		if (dp->d_ino == 0 && strcmp(dp->d_name, "/") != 0)
			continue;
		if ((ino_t)(dp->d_ino) >= maxino) {
			dprintf(stderr,
			    gettext("corrupted directory: bad inum %lu\n"),
			    dp->d_ino);
			continue;
		}
		return (dp);
	}
}

/*
 * Set the mode, owner, and times for all new or changed directories
 */
void
setdirmodes(void)
{
	FILE *smf;
	struct entry *ep;
	char *cp, *metadata = NULL;
	size_t metasize = 0;
	int override = -1;
	int saverr;
	static int complained_chown = 0;
	static int complained_chmod = 0;
	int dfd;

	vprintf(stdout, gettext("Set directory mode, owner, and times.\n"));
	/* XXX if modefile[0] == '#', shouldn't we just bail here? */
	/* XXX why isn't it set already? */
	INIT_MODEFILE();
	smf = fopen64(modefile, "r");
	if (smf == NULL) {
		perror("fopen");
		(void) fprintf(stderr,
		    gettext("cannot open mode file %s\n"), modefile);
		(void) fprintf(stderr,
		    gettext("directory mode, owner, and times not set\n"));
		return;
	}
	clearerr(smf);
	for (;;) {
		(void) fread((char *)&node, 1, sizeof (node), smf);
		if (feof(smf))
			break;
		ep = lookupino(node.ino);
		if (command == 'i' || command == 'x') {
			if (ep == NIL) {
				skipmetadata(smf, node.metasize);
				continue;
			}
			if (ep->e_flags & EXISTED) {
				if (override < 0) {
					if (reply(gettext(
				"Directories already exist, set modes anyway"))
					    == FAIL)
						override = 0;
					else
						override = 1;
				}
				if (override == 0) {
					/* LINTED: result fits into short */
					ep->e_flags &= ~NEW;
					skipmetadata(smf, node.metasize);
					continue;
				}
			}
			if (node.ino == ROOTINO &&
			    reply(gettext("set owner/mode for '.'")) == FAIL) {
				skipmetadata(smf, node.metasize);
				continue;
			}
		}
		if (ep == NIL) {
			panic(gettext("cannot find directory inode %d\n"),
			    node.ino);
			skipmetadata(smf, node.metasize);
			continue;
		}
		cp = myname(ep);
		resolve(myname(ep), &dfd, &cp);
		if (dfd != AT_FDCWD) {
			if (fchdir(dfd) < 0) {
				saverr = errno;
				(void) fprintf(stderr,
				gettext("Can not set attribute context: %s\n"),
				    strerror(saverr));
				(void) close(dfd);
				continue;
			}
		}
		if (chmod(cp, node.mode) < 0 && !complained_chmod) {
			saverr = errno;
			(void) fprintf(stderr,
			gettext("Can not set directory permissions: %s\n"),
			    strerror(saverr));
			complained_chmod = 1;
		}
		if (node.metasize != 0) {
			if (node.metasize > metasize)
				metadata = realloc(metadata,
				    metasize = node.metasize);
			if (metadata == NULL) {
				(void) fprintf(stderr,
				    gettext("Cannot malloc metadata\n"));
				done(1);
			}
			(void) fread(metadata, 1, node.metasize, smf);
			metaproc(cp, metadata, node.metasize);
		}

		/*
		 * BUG 4302943
		 * Since the ACLs must be set before fixing the ownership,
		 * chown should be called only after metaproc
		 */
		if (chown(cp, node.uid, node.gid) < 0 && !complained_chown) {
			saverr = errno;
			(void) fprintf(stderr,
			    gettext("Can not set directory ownership: %s\n"),
			    strerror(saverr));
			complained_chown = 1;
		}
		utime(cp, (struct utimbuf *)node.timep);
		/* LINTED: result fits into short */
		ep->e_flags &= ~NEW;
		if (dfd != AT_FDCWD) {
			fchdir(savepwd);
			(void) close(dfd);
		}
	}
	if (ferror(smf))
		panic(gettext("error setting directory modes\n"));
	if (metadata != NULL)
		(void) free(metadata);
	(void) fclose(smf);
}

void
skipmetadata(FILE *f, size_t size)
{
	/* XXX should we bail if this doesn't work? */
	/* LINTED unsigned -> signed conversion ok here */
	(void) fseeko(f, (off_t)size, SEEK_CUR);
}

/*
 * Generate a literal copy of a directory.
 */
int
genliteraldir(char *name, ino_t ino)
{
	struct inotab *itp;
	int ofile, dp;
	off64_t i;
	size_t size;
	char buf[BUFSIZ];

	itp = inotablookup(ino);
	if (itp == NULL) {
		(void) fprintf(stderr,
		    gettext("Cannot find directory inode %d named %s\n"),
		    ino, name);
		return (FAIL);
	}
	if ((ofile = creat(name, 0666)) < 0) {
		(void) fprintf(stderr, "%s: ", name);
		(void) fflush(stderr);
		perror(gettext("cannot create file"));
		return (FAIL);
	}
	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
	dp = dup(dirp->dd_fd);
	if (dp < 0) {
		perror(gettext("dup(2) failed"));
		(void) close(ofile);
		(void) unlink(name);
		return (FAIL);
	}
	for (i = itp->t_size; i != 0; i -= size) {
		/* LINTED cast is safe due to comparison */
		size = i < BUFSIZ ? (size_t)i : BUFSIZ;
		/* XXX instead of done(), clean up and return FAIL? */
		if (read(dp, buf, size) == -1) {
			(void) fprintf(stderr, gettext(
			    "read error extracting inode %d, name %s\n"),
			    curfile.ino, curfile.name);
			perror("read");
			done(1);
		}
		if (write(ofile, buf, size) == -1) {
			(void) fprintf(stderr, gettext(
			    "write error extracting inode %d, name %s\n"),
			    curfile.ino, curfile.name);
			perror("write");
			done(1);
		}
	}
	(void) close(dp);
	(void) close(ofile);
	return (GOOD);
}

/*
 * Determine the type of an inode
 */
int
inodetype(ino_t ino)
{
	struct inotab *itp;

	itp = inotablookup(ino);
	if (itp == NULL)
		return (LEAF);
	return (NODE);
}

/*
 * Allocate and initialize a directory inode entry.
 * If requested, save its pertinent mode, owner, and time info.
 */
static struct inotab *
allocinotab(ino_t ino, struct dinode *dip, off64_t seekpt)
{
	struct inotab	*itp;

	itp = (struct inotab *)calloc(1, sizeof (*itp));
	if (itp == 0) {
		(void) fprintf(stderr,
		    gettext("no memory for directory table\n"));
		done(1);
	}
	itp->t_next = inotab[INOHASH(ino)];
	inotab[INOHASH(ino)] = itp;
	itp->t_ino = ino;
	itp->t_seekpt = seekpt;
	if ((dip->di_mode & IFMT) == IFATTRDIR) {
		itp->t_xattr = xattrlist;
		xattrlist = itp;
	}
	if (mf == NULL)
		return (itp);
	node.ino = ino;
	node.timep[0] = dip->di_atime;
	node.timep[1] = dip->di_mtime;
	node.mode = dip->di_mode;
	node.uid =
	    dip->di_suid == UID_LONG ? dip->di_uid : (uid_t)dip->di_suid;
	node.gid =
	    dip->di_sgid == GID_LONG ? dip->di_gid : (gid_t)dip->di_sgid;
	return (itp);
}

void
nodeflush()
{
	char *metadata;

	if (mf == NULL) {
		(void) fprintf(stderr, gettext(
		    "Inconsistency detected: modefile pointer is NULL\n"));
		done(1);
	}
	metaget(&metadata, &(node.metasize));
	(void) fwrite((char *)&node, 1, sizeof (node), mf);
	if (node.metasize != 0)
		(void) fwrite(metadata, 1, node.metasize, mf);
	if (ferror(mf))
		panic("%s: %s\n", modefile, strerror(errno));
}

/*
 * Look up an inode in the table of directories
 */
static struct inotab *
inotablookup(ino_t ino)
{
	struct inotab *itp;

	for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
		if (itp->t_ino == ino)
			return (itp);
	return ((struct inotab *)0);
}

/*
 * Clean up and exit
 */
void
done(int exitcode)
{
	closemt(ALLOW_OFFLINE);		/* don't force offline on exit */
	if (modefile[0] != '#')
		(void) unlink(modefile);
	if (dirfile[0] != '#')
		(void) unlink(dirfile);
	exit(exitcode);
}
/*
 * Copyright 1998,2001-2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/

/*
 * Copyright (c) 1985 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#include <setjmp.h>
#include <euc.h>
#include <widec.h>
#include "restore.h"
#include <ctype.h>
#include <limits.h>
#include <sys/wait.h>

extern eucwidth_t wp;

#define	round(a, b) ((((a) + (b) - 1) / (b)) * (b))

/*
 * Things to handle interruptions.
 */
static jmp_buf reset;
static int reset_OK;
static char *nextarg = NULL;

static int dontexpand;	/* co-routine state set in getnext, used in expandarg */

static void getcmd(char *, char *, size_t, char *, size_t, struct arglist *);
static void expandarg(char *, struct arglist *);
static void printlist(char *, ino_t, char *, int);
static void formatf(struct arglist *);
static char *copynext(char *, char *, size_t);
static int fcmp(struct afile *, struct afile *);
static char *fmtentry(struct afile *);
static void setpagercmd(void);
static uint_t setpagerargs(char **);

/*
 * Read and execute commands from the terminal.
 */
void
runcmdshell(void)
{
	struct entry *np;
	ino_t ino;
	static struct arglist alist = { 0, 0, 0, 0, 0 };
	char curdir[MAXCOMPLEXLEN];
	char name[MAXCOMPLEXLEN];
	char cmd[BUFSIZ];

#ifdef	lint
	curdir[0] = '\0';
#endif	/* lint */

	canon("/", curdir, sizeof (curdir));
loop:
	if (setjmp(reset) != 0) {
		for (; alist.head < alist.last; alist.head++)
			freename(alist.head->fname);
		nextarg = NULL;
		volno = 0;
		goto loop;	/* make sure jmpbuf is up-to-date */
	}
	reset_OK = 1;
	getcmd(curdir, cmd, sizeof (cmd), name, sizeof (name), &alist);

	/*
	 * Using strncmp() to catch unique prefixes.
	 */
	switch (cmd[0]) {
	/*
	 * Add elements to the extraction list.
	 */
	case 'a':
		if (strncmp(cmd, "add", strlen(cmd)) != 0)
			goto bad;
		if (name[0] == '\0')
			break;
		ino = dirlookup(name);
		if (ino == 0)
			break;
		if (mflag)
			pathcheck(name);
		treescan(name, ino, addfile);
		break;
	/*
	 * Change working directory.
	 */
	case 'c':
		if (strncmp(cmd, "cd", strlen(cmd)) != 0)
			goto bad;
		if (name[0] == '\0')
			break;
		ino = dirlookup(name);
		if (ino == 0)
			break;
		if (inodetype(ino) == LEAF) {
			(void) fprintf(stderr,
			    gettext("%s: not a directory\n"), name);
			break;
		}

		/* No need to canon(name), getcmd() did it for us */
		(void) strncpy(curdir, name, sizeof (curdir));
		curdir[sizeof (curdir) - 1] = '\0';
		break;
	/*
	 * Delete elements from the extraction list.
	 */
	case 'd':
		if (strncmp(cmd, "delete", strlen(cmd)) != 0)
			goto bad;
		if (name[0] == '\0')
			break;
		np = lookupname(name);
		if (np == NIL || (np->e_flags & NEW) == 0) {
			(void) fprintf(stderr,
			    gettext("%s: not on extraction list\n"), name);
			break;
		}
		treescan(name, np->e_ino, deletefile);
		break;
	/*
	 * Extract the requested list.
	 */
	case 'e':
		if (strncmp(cmd, "extract", strlen(cmd)) != 0)
			goto bad;
		attrscan(0, addfile);
		createfiles();
		createlinks();
		setdirmodes();
		if (dflag)
			checkrestore();
		volno = 0;
		break;
	/*
	 * List available commands.
	 */
	case 'h':
		if (strncmp(cmd, "help", strlen(cmd)) != 0)
			goto bad;
		/*FALLTHROUGH*/
	case '?':
		/* ANSI string catenation, to shut cstyle up */
		(void) fprintf(stderr, "%s",
		    gettext("Available commands are:\n"
"\tls [arg] - list directory\n"
"\tmarked [arg] - list items marked for extraction from directory\n"
"\tcd arg - change directory\n"
"\tpwd - print current directory\n"
"\tadd [arg] - add `arg' to list of files to be extracted\n"
"\tdelete [arg] - delete `arg' from list of files to be extracted\n"
"\textract - extract requested files\n"
"\tsetmodes - set modes of requested directories\n"
"\tquit - immediately exit program\n"
"\twhat - list dump header information\n"
"\tverbose - toggle verbose flag (useful with ``ls'')\n"
"\tpaginate - toggle pagination flag (affects ``ls'' and ``marked'')\n"
"\tsetpager - set pagination command and arguments\n"
"\thelp or `?' - print this list\n"
"If no `arg' is supplied, the current directory is used\n"));
		break;
	/*
	 * List a directory.
	 */
	case 'l':
	case 'm':
		if ((strncmp(cmd, "ls", strlen(cmd)) != 0) &&
		    (strncmp(cmd, "marked", strlen(cmd)) != 0))
			goto bad;
		if (name[0] == '\0')
			break;
		ino = dirlookup(name);
		if (ino == 0)
			break;
		printlist(name, ino, curdir, *cmd == 'm');
		break;
	/*
	 * Print current directory or enable pagination.
	 */
	case 'p':
		if (strlen(cmd) < 2)
			goto ambiguous;
		if (strncmp(cmd, "pwd", strlen(cmd)) == 0) {
			if (curdir[1] == '\0') {
				(void) fprintf(stderr, "/\n");
			} else {
				(void) fprintf(stderr, "%s\n", &curdir[1]);
			}
		} else if (strncmp(cmd, "paginate", strlen(cmd)) == 0) {
			if (paginating) {
				(void) fprintf(stderr,
				    gettext("paging disabled\n"));
				paginating = 0;
				break;
			}
			if (vflag) {
				(void) fprintf(stderr,
				    gettext("paging enabled (%s)\n"),
				    pager_catenated);
			} else {
				(void) fprintf(stderr,
				    gettext("paging enabled\n"));
			}
			if (dflag) {
				int index = 0;

				while (index < pager_len) {
					(void) fprintf(stderr,
					    ">>>pager_vector[%d] = `%s'\n",
					    index,
					    pager_vector[index] ?
					    pager_vector[index] : "(null)");
					index += 1;
				}
			}
			paginating = 1;
		} else {
			goto bad;
		}
		break;
	/*
	 * Quit.
	 */
	case 'q':
		if (strncmp(cmd, "quit", strlen(cmd)) != 0)
			goto bad;
		reset_OK = 0;
		return;
	case 'x':
		if (strncmp(cmd, "xit", strlen(cmd)) != 0)
			goto bad;
		reset_OK = 0;
		return;
	/*
	 * Toggle verbose mode.
	 */
	case 'v':
		if (strncmp(cmd, "verbose", strlen(cmd)) != 0)
			goto bad;
		if (vflag) {
			(void) fprintf(stderr, gettext("verbose mode off\n"));
			vflag = 0;
			break;
		}
		(void) fprintf(stderr, gettext("verbose mode on\n"));
		vflag = 1;
		break;
	/*
	 * Just restore requested directory modes, or set pagination command.
	 */
	case 's':
		if (strlen(cmd) < 4)
			goto ambiguous;
		if (strncmp(cmd, "setmodes", strlen(cmd)) == 0) {
			setdirmodes();
		} else if (strncmp(cmd, "setpager", strlen(cmd)) == 0) {
			setpagercmd();
		} else {
			goto bad;
		}
		break;
	/*
	 * Print out dump header information.
	 */
	case 'w':
		if (strncmp(cmd, "what", strlen(cmd)) != 0)
			goto bad;
		printdumpinfo();
		break;
	/*
	 * Turn on debugging.
	 */
	case 'D':
		if (strncmp(cmd, "Debug", strlen(cmd)) != 0)
			goto bad;
		if (dflag) {
			(void) fprintf(stderr, gettext("debugging mode off\n"));
			dflag = 0;
			break;
		}
		(void) fprintf(stderr, gettext("debugging mode on\n"));
		dflag++;
		break;
	/*
	 * Unknown command.
	 */
	default:
	bad:
		(void) fprintf(stderr,
		    gettext("%s: unknown command; type ? for help\n"), cmd);
		break;
	ambiguous:
		(void) fprintf(stderr,
		    gettext("%s: ambiguous command; type ? for help\n"), cmd);
		break;
	}
	goto loop;
}

static char input[MAXCOMPLEXLEN]; /* shared by getcmd() and setpagercmd() */
#define	rawname input	/* save space by reusing input buffer */

/*
 * Read and parse an interactive command.
 * The first word on the line is assigned to "cmd". If
 * there are no arguments on the command line, then "curdir"
 * is returned as the argument. If there are arguments
 * on the line they are returned one at a time on each
 * successive call to getcmd. Each argument is first assigned
 * to "name". If it does not start with "/" the pathname in
 * "curdir" is prepended to it. Finally "canon" is called to
 * eliminate any embedded ".." components.
 */
/* ARGSUSED */
static void
getcmd(char *curdir, char *cmd, size_t cmdsiz, char *name, size_t namesiz,
    struct arglist *ap)
{
	char *cp;
	char output[MAXCOMPLEXLEN];

	/*
	 * Check to see if still processing arguments.
	 */
	if (ap->head != ap->last) {
		(void) strncpy(name, ap->head->fname, namesiz);
		name[namesiz - 1] = '\0';
		/* double null terminate string */
		if ((strlen(name) + 2) > namesiz) {
			fprintf(stderr, gettext("name is too long, ignoring"));
			memset(name, 0, namesiz);
		} else {
			name[strlen(name) + 1] = '\0';
		}
		freename(ap->head->fname);
		ap->head++;
		return;
	}
	if (nextarg != NULL)
		goto getnext;
	/*
	 * Read a command line and trim off trailing white space.
	 */
readagain:
	do {
		(void) fprintf(stderr, "%s > ", progname);
		(void) fflush(stderr);
		(void) fgets(input, sizeof (input), terminal);
	} while (!feof(terminal) && input[0] == '\n');
	if (feof(terminal)) {
		(void) strncpy(cmd, "quit", cmdsiz);
		return;
	}
	/* trim off trailing white space and newline */
	for (cp = &input[strlen(input) - 2];
	    cp >= &input[0] && isspace((uchar_t)*cp);
	    cp--) {
		continue;
		/*LINTED [empty loop body]*/
	}
	*++cp = '\0';
	if ((strlen(input) + 2) > MAXCOMPLEXLEN) {
		fprintf(stderr, gettext("command is too long\n"));
		goto readagain;
	} else {
		/* double null terminate string */
		*(cp + 1) = '\0';
	}

	if (cp == &input[0])
		goto readagain;

	/*
	 * Copy the command into "cmd".
	 */
	cp = copynext(input, cmd, cmdsiz);
	ap->cmd = cmd;
	/*
	 * If no argument, use curdir as the default.
	 */
	if (*cp == '\0') {
		(void) strncpy(name, curdir, namesiz);
		name[namesiz - 1] = '\0';
		/* double null terminate string */
		if ((strlen(name) + 2) > namesiz) {
			fprintf(stderr, gettext("name is too long, ignoring"));
			memset(name, 0, namesiz);
		} else {
			name[strlen(name) + 1] = '\0';
		}
		return;
	}
	nextarg = cp;
	/*
	 * Find the next argument.
	 */
getnext:
	cp = copynext(nextarg, rawname, sizeof (rawname));
	if (*cp == '\0')
		nextarg = NULL;
	else
		nextarg = cp;
	/*
	 * If it an absolute pathname, canonicalize it and return it.
	 */
	if (rawname[0] == '/') {
		canon(rawname, name, namesiz);
	} else {
		/*
		 * For relative pathnames, prepend the current directory to
		 * it then canonicalize and return it.
		 */
		(void) snprintf(output, sizeof (output), "%s/%s",
		    curdir, rawname);
		canon(output, name, namesiz);
	}
	expandarg(name, ap);
	/*
	 * ap->head->fname guaranteed to be double null-terminated and
	 * no more than MAXCOMPLEXLEN characters long.
	 */
	assert(namesiz >= (MAXCOMPLEXLEN));
	(void) strcpy(name, ap->head->fname);
	/* double null terminate string */
	name[strlen(name) + 1] = '\0';
	freename(ap->head->fname);
	ap->head++;
#undef	rawname
}

/*
 * Strip off the next token of the input.
 */
static char *
copynext(char *input, char *output, size_t outsize)
{
	char *cp, *bp, *limit;
	char quote;

	dontexpand = 0;
	/* skip to argument */
	for (cp = input; *cp != '\0' && isspace((uchar_t)*cp); cp++) {
		continue;
		/*LINTED [empty loop body]*/
	}
	bp = output;
	limit = output + outsize - 1; /* -1 for the trailing \0 */
	while (!isspace((uchar_t)*cp) && *cp != '\0' && bp < limit) {
		/*
		 * Handle back slashes.
		 */
		if (*cp == '\\') {
			if (*++cp == '\0') {
				(void) fprintf(stderr, gettext(
				    "command lines cannot be continued\n"));
				continue;
			}
			*bp++ = *cp++;
			continue;
		}
		/*
		 * The usual unquoted case.
		 */
		if (*cp != '\'' && *cp != '"') {
			*bp++ = *cp++;
			continue;
		}
		/*
		 * Handle single and double quotes.
		 */
		quote = *cp++;
		dontexpand = 1;
		while (*cp != quote && *cp != '\0' && bp < limit)
			*bp++ = *cp++;
		if (*cp++ == '\0') {
			(void) fprintf(stderr,
			    gettext("missing %c\n"), (uchar_t)quote);
			cp--;
			continue;
		}
	}
	*bp = '\0';
	if ((strlen(output) + 2) > outsize) {
		fprintf(stderr, gettext(
		    "name is too long, ignoring"));
		memset(output, 0, outsize);
	} else {
		/* double null terminate string */
		*(bp + 1) = '\0';
	}
	return (cp);
}

/*
 * Canonicalize file names to always start with ``./'' and
 * remove any imbedded "." and ".." components.
 *
 * The pathname "canonname" is returned double null terminated.
 */
void
canon(char *rawname, char *canonname, size_t limit)
{
	char *cp, *np, *prefix;
	uint_t len;

	assert(limit > 3);
	if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
		prefix = "";
	else if (rawname[0] == '/')
		prefix = ".";
	else
		prefix = "./";
	(void) snprintf(canonname, limit, "%s%s", prefix, rawname);
	/*
	 * Eliminate multiple and trailing '/'s
	 */
	for (cp = np = canonname; *np != '\0'; cp++) {
		*cp = *np++;
		while (*cp == '/' && *np == '/')
			np++;
	}
	*cp = '\0';
	if ((strlen(canonname) + 2) > limit) {
		fprintf(stderr,
		    gettext("canonical name is too long, ignoring name\n"));
		memset(canonname, 0, limit);
	} else {
		/* double null terminate string */
		*(cp + 1) = '\0';
	}

	if (*--cp == '/')
		*cp = '\0';
	/*
	 * Eliminate extraneous "." and ".." from pathnames.  Uses
	 * memmove(), as strcpy() might do the wrong thing for these
	 * small overlaps.
	 */
	np = canonname;
	while (*np != '\0') {
		np++;
		cp = np;
		while (*np != '/' && *np != '\0')
			np++;
		if (np - cp == 1 && *cp == '.') {
			cp--;
			len = strlen(np);
			(void) memmove(cp, np, len);
			*(cp + len) = '\0';
			/* double null terminate string */
			*(cp + len + 1) = '\0';
			np = cp;
		}
		if (np - cp == 2 && strncmp(cp, "..", 2) == 0) {
			cp--;
			/* find beginning of name */
			while (cp > &canonname[1] && *--cp != '/') {
				continue;
				/*LINTED [empty loop body]*/
			}
			len = strlen(np);
			(void) memmove(cp, np, len);
			*(cp + len) = '\0';
			/* double null terminate string */
			*(cp + len + 1) = '\0';
			np = cp;
		}
	}
}

/*
 * globals (file name generation)
 *
 * "*" in params matches r.e ".*"
 * "?" in params matches r.e. "."
 * "[...]" in params matches character class
 * "[...a-z...]" in params matches a through z.
 */
static void
expandarg(char *arg, struct arglist *ap)
{
	static struct afile single;
	int size;

	ap->head = ap->last = (struct afile *)0;
	if (dontexpand)
		size = 0;
	else
		size = expand(arg, 0, ap);
	if (size == 0) {
		struct entry *ep;

		ep = lookupname(arg);
		single.fnum = ep ? ep->e_ino : 0;
		single.fname = savename(arg);
		ap->head = &single;
		ap->last = ap->head + 1;
		return;
	}
	if ((ap->last - ap->head) > ULONG_MAX) {
		(void) fprintf(stderr,
		    gettext("Argument expansion too large to sort\n"));
	} else {
		/* LINTED pointer arith just range-checked */
		qsort((char *)ap->head, (size_t)(ap->last - ap->head),
		    sizeof (*ap->head),
		    (int (*)(const void *, const void *)) fcmp);
	}
}

/*
 * Do an "ls" style listing of a directory
 */
static void
printlist(char *name, ino_t ino, char *basename, int marked_only)
{
	struct afile *fp;
	struct direct *dp;
	static struct arglist alist = { 0, 0, 0, 0, "ls" };
	struct afile single;
	struct entry *np;
	RST_DIR *dirp;
	int list_entry;

	if ((dirp = rst_opendir(name)) == NULL) {
		single.fnum = ino;
		if (strncmp(name, basename, strlen(basename)) == 0)
			single.fname = savename(name + strlen(basename) + 1);
		else
			single.fname = savename(name);
		alist.head = &single;
		alist.last = alist.head + 1;
		if (alist.base != NULL) {
			free(alist.base);
			alist.base = NULL;
		}
	} else {
		alist.head = (struct afile *)0;
		(void) fprintf(stderr, "%s:\n", name);
		while (dp = rst_readdir(dirp)) {
			if (dp == NULL || dp->d_ino == 0) {
				rst_closedir(dirp);
				dirp = NULL;
				break;
			}
			if (!dflag && BIT(dp->d_ino, dumpmap) == 0)
				continue;
			if (vflag == 0 &&
			    (strcmp(dp->d_name, ".") == 0 ||
			    strcmp(dp->d_name, "..") == 0))
				continue;
			list_entry = 1;
			if (marked_only) {
				np = lookupino(dp->d_ino);
				if ((np == NIL) || ((np->e_flags & NEW) == 0))
					list_entry = 0;
			}
			if (list_entry) {
				if (!mkentry(dp->d_name, dp->d_ino, &alist)) {
					rst_closedir(dirp);
					return;
				}
			}
		}
	}
	if (alist.head != 0) {
		if ((alist.last - alist.head) > ULONG_MAX) {
			(void) fprintf(stderr,
			    gettext("Directory too large to sort\n"));
		} else {
			qsort((char *)alist.head,
			    /* LINTED range-checked */
			    (size_t)(alist.last - alist.head),
			    sizeof (*alist.head),
			    (int (*)(const void *, const void *)) fcmp);
		}
		formatf(&alist);
		for (fp = alist.head; fp < alist.last; fp++)
			freename(fp->fname);
		alist.head = NULL;
		/*
		 * Don't free alist.base, as we'll probably be called
		 * again, and might as well re-use what we've got.
		 */
	}
	if (dirp != NULL) {
		(void) fprintf(stderr, "\n");
		rst_closedir(dirp);
	}
}

/*
 * Print out a pretty listing of a directory
 */
static void
formatf(struct arglist *ap)
{
	struct afile *fp;
	struct entry *np;
	/* LINTED: result fits into an int */
	int nentry = (int)(ap->last - ap->head);
	int i, j;
	uint_t len, w, width = 0, columns, lines;
	char *cp;
	FILE *output = stderr;

	if (ap->head == ap->last)
		return;

	if (paginating) {
		int fds[2];

		if (pipe(fds) < 0) {
			perror(gettext("could not create pipe"));
			goto no_page;
		}

		switch (fork()) {
		case -1:
			perror(gettext("could not fork"));
			goto no_page;
		case 0:
			/*
			 * Make sure final output still ends up in
			 * the same place.
			 */
			(void) dup2(fileno(stderr), fileno(stdout));
			(void) close(fds[0]);
			(void) dup2(fds[1], fileno(stdin));
			execvp(pager_vector[0], pager_vector);
			perror(gettext("execvp of pager failed"));
			exit(1);
			/*NOTREACHED*/
		default:
			(void) close(fds[1]);
			output = fdopen(fds[0], "w");
			if (output != (FILE *)NULL) {
				break;
			}
			perror(gettext("could not open pipe to pager"));
			output = stderr;
		no_page:
			(void) fprintf(stderr,
			    gettext("pagination disabled\n"));
			paginating = 0;
		}
	}

	for (fp = ap->head; fp < ap->last; fp++) {
		fp->ftype = inodetype(fp->fnum);
		np = lookupino(fp->fnum);
		if (np != NIL)
			fp->fflags = np->e_flags;
		else
			fp->fflags = 0;
		len = strlen(fmtentry(fp));
		if (len > width)
			width = len;
	}
	width += 2;
	columns = 80 / width;
	if (columns == 0)
		columns = 1;
	lines = (nentry + columns - 1) / columns;
	for (i = 0; i < lines && !ferror(output); i++) {
		for (j = 0; j < columns && !ferror(output); j++) {
			fp = ap->head + j * lines + i;
			cp = fmtentry(fp);
			(void) fprintf(output, "%s", cp);
			if (fp + lines >= ap->last) {
				(void) fprintf(output, "\n");
				break;
			}
			w = strlen(cp);
			while (w < width) {
				w++;
				if (fprintf(output, " ") < 0)
					break;
			}
		}
	}

	if (paginating) {
		(void) fclose(output);
		(void) wait((int *)NULL);
	}
}

/*
 * Comparison routine for qsort.
 */
static int
fcmp(struct afile *f1, struct afile *f2)
{

	return (strcoll(f1->fname, f2->fname));
}

/*
 * Format a directory entry.
 */
static char *
fmtentry(struct afile *fp)
{
	static char fmtres[MAXCOMPLEXLEN];
	static int precision = 0;
	ino_t i;
	char *cp, *dp, *limit;

	if (!vflag) {
		/* MAXCOMPLEXLEN assumed to be >= 1 */
		fmtres[0] = '\0';
	} else {
		if (precision == 0) {
			for (i = maxino; i != 0; i /= 10)
				precision++;
			if (sizeof (fmtres) < (unsigned)(precision + 2)) {
				(void) fprintf(stderr, gettext(
"\nInternal check failed, minimum width %d exceeds available size %d\n"),
				    (precision + 2), sizeof (fmtres));
				done(1);
			}
		}
		(void) snprintf(fmtres, sizeof (fmtres), "%*ld ",
		    precision, fp->fnum);
	}
	dp = &fmtres[strlen(fmtres)];
	limit = fmtres + sizeof (fmtres) - 1;
	if (dflag && BIT(fp->fnum, dumpmap) == 0)
		*dp++ = '^';
	else if ((fp->fflags & NEW) != 0)
		*dp++ = '*';
	else
		*dp++ = ' ';
	for (cp = fp->fname; *cp && dp < limit; cp++)
		/* LINTED: precedence ok, can't fix system macro */
		if (!vflag && (!ISPRINT(*cp, wp)))
			*dp++ = '?';
		else
			*dp++ = *cp;
	if (fp->ftype == NODE && dp < limit)
		*dp++ = '/';
	*dp++ = 0;
	return (fmtres);
}

/*
 * respond to interrupts
 */
/* ARGSUSED */
void
onintr(int sig)
{
	char	buf[300];

	if (command == 'i' && reset_OK)
		longjmp(reset, 1);

	(void) snprintf(buf, sizeof (buf),
	    gettext("%s interrupted, continue"), progname);
	if (reply(buf) == FAIL)
		done(1);
}
/*
 * Set up pager_catenated and pager_vector.
 */
void
initpagercmd(void)
{
	char *cp;

	cp = getenv("PAGER");
	if (cp != NULL)
		pager_catenated = strdup(cp);
	if ((pager_catenated == NULL) || (*pager_catenated == '\0')) {
		if (pager_catenated != NULL)
			free(pager_catenated);
		pager_catenated = strdup(DEF_PAGER);
	}
	if (pager_catenated == NULL) {
		(void) fprintf(stderr, gettext("out of memory\n"));
		done(1);
	}

	pager_vector = (char **)malloc(sizeof (char *));
	if (pager_vector == NULL) {
		(void) fprintf(stderr, gettext("out of memory\n"));
		done(1);
	}

	pager_len = 1;
	cp = pager_catenated;
	(void) setpagerargs(&cp);
}


/*
 * Resets pager_catenated and pager_vector from user input.
 */
void
setpagercmd(void)
{
	uint_t catenate_length;
	int index;

	/*
	 * We'll get called immediately after setting a pager, due to
	 * our interaction with getcmd()'s internal state.  Don't do
	 * anything when that happens.
	 */
	if (*input == '\0')
		return;

	if (pager_len > 0) {
		for (index = 0; pager_vector[index] != (char *)NULL; index += 1)
			free(pager_vector[index]);
		free(pager_vector);
		free(pager_catenated);
	}

	pager_vector = (char **)malloc(2 * sizeof (char *));
	if (pager_vector == NULL) {
		(void) fprintf(stderr, gettext("out of memory\n"));
		done(1);
	}

	pager_len = 2;
	pager_vector[0] = strdup(input);
	if (pager_vector[0] == NULL) {
		(void) fprintf(stderr, gettext("out of memory\n"));
		done(1);
	}
	if (dflag)
		(void) fprintf(stderr, gettext("got command `%s'\n"), input);
	catenate_length = setpagerargs(&nextarg) + strlen(pager_vector[0]) + 1;
	pager_catenated = (char *)malloc(catenate_length *
	    (size_t)sizeof (char));
	if (pager_catenated == (char *)NULL) {
		(void) fprintf(stderr, gettext("out of memory\n"));
		done(1);
	}
	for (index = 0; pager_vector[index] != (char *)NULL; index += 1) {
		if (index > 0)
			(void) strcat(pager_catenated, " ");
		(void) strcat(pager_catenated, pager_vector[index]);
	}
}


/*
 * Extract arguments for the pager command from getcmd()'s input buffer.
 */
static uint_t
setpagerargs(char **source)
{
	char	word[MAXCOMPLEXLEN];
	char	*cp = *source;
	uint_t	length = 0;

	while ((cp != (char *)NULL) && (*cp != '\0')) {
		cp = copynext(cp, word, sizeof (word));
		if (dflag)
			fprintf(stderr, gettext("got word `%s'\n"), word);
		pager_vector = (char **)realloc(pager_vector,
		    (size_t)sizeof (char *) * (pager_len + 1));
		if (pager_vector == (char **)NULL) {
			(void) fprintf(stderr, gettext("out of memory\n"));
			done(1);
		}
		pager_vector[pager_len - 1] = strdup(word);
		if (pager_vector[pager_len - 1] == (char *)NULL) {
			(void) fprintf(stderr, gettext("out of memory\n"));
			done(1);
		}
		length += strlen(word) + 1;
		pager_len += 1;
	}
	pager_vector[pager_len - 1] = (char *)NULL;
	*source = cp;
	return (length);
}
/stdio.h".*sometimes ignored: fprintf/d
/stdio.h".*always ignored: snprintf/d
/stdio.h".*always ignored: printf/d
/string.h".*sometimes ignored: memcpy/d
/time.h".*sometimes ignored: time/d
/myrcmd.c",.*assigned value never used: retval .*(22[0-9])/d
/tape.c",.*assigned value never used: mt .*(22[0-9])/d
/main.c",.*modification using a NULL pointer/d
/dirs.c",.*name used but not defined: utime /d
/lint suppression directive/d
/byteorder.c",.*inconsistent use of a value type/d
/byteorder.c",.*assigned value never used: allocated at.*byteorder.c/d
/unistd.h".*always ignored: sleep/d
/unistd.h".*always ignored: execvp/d
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/

/*
 * Copyright (c) 1983 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

/*
 *	Modified to recursively extract all files within a subtree
 *	(supressed by the h option) and recreate the heirarchical
 *	structure of that subtree and move extracted files to their
 *	proper homes (supressed by the m option).
 *	Includes the s (skip files) option for use with multiple
 *	dumps on a single tape.
 *	8/29/80		by Mike Litzkow
 *
 *	Modified to work on the new file system and to recover from
 *	tape read errors.
 *	1/19/82		by Kirk McKusick
 *
 *	Full incremental restore running entirely in user code and
 *	interactive tape browser.
 *	1/19/83		by Kirk McKusick
 */

#include "restore.h"
#include <signal.h>
#include <byteorder.h>
#include <priv_utils.h>

#include <euc.h>
#include <getwidth.h>
#include <sys/mtio.h>
eucwidth_t wp;

int	bflag = 0, dflag = 0, vflag = 0, yflag = 0;
int	hflag = 1, mflag = 1, paginating = 0, offline = 0, autoload = 0;
int	autoload_tries;
int	autoload_period;
int	cvtflag = 0;		/* Converting from old dump format */
char	command = '\0';
long	dumpnum = 1;
int	volno = 0;
uint_t	ntrec;			/* blocking factor, in KB */
uint_t	saved_ntrec;		/* saved blocking factor, in KB */
ssize_t	tape_rec_size = 0;	/* tape record size (ntrec * tp_bsize) */
size_t	newtapebuf_size = 0;	/* save size of last call to newtapebuf */
char	*progname;
char	*dumpmap;
char	*clrimap;
char	*c_label;		/* if non-NULL, we must see this tape label */
ino_t	maxino;
time_t	dumptime;
time_t	dumpdate;
FILE 	*terminal;
char	*tmpdir;
char	*pager_catenated;
char	**pager_vector;
int	pager_len;
int	inattrspace = 0;
int	savepwd;
int32_t	tp_bsize = TP_BSIZE_MIN;
struct byteorder_ctx *byteorder;

static void set_tmpdir(void);

int
main(int argc, char *argv[])
{
	static struct arglist alist = { 0, 0, 0, 0, 0 };
	int  count;
	char *cp;
	char *fname;
	ino_t ino;
	char *inputdev;
	char *archivefile = 0;
	char *symtbl = RESTORESYMTABLE;
	char name[MAXPATHLEN];
	int  fflag = 0;
	struct sigaction sa, osa;
	int multiplier;
	char units;

	if ((progname = strrchr(argv[0], '/')) != NULL)
		progname++;
	else
		progname = argv[0];

	if (strcmp("hsmrestore", progname) == 0) {
		(void) fprintf(stderr,
		    gettext("hsmrestore emulation is no longer supported.\n"));
		done(1);
	}

	/*
	 * Convert the effective uid of 0 to the single privilege
	 * we really want.  When running with all privileges, this
	 * is a no-op.  When the set-uid bit is stripped restore
	 * still works for local tapes.  Fail when trying to access
	 * a remote tape in that case and not immediately.
	 */
	(void) __init_suid_priv(0, PRIV_NET_PRIVADDR, (char *)NULL);

	inputdev = DEFTAPE;

	/*
	 * This doesn't work because ufsrestore is statically linked:
	 * (void) setlocale(LC_ALL, "");
	 * The problem seems to be with LC_COLLATE, so set all the
	 * others explicitly.  Bug 1157128 was created against the I18N
	 * library.  When that bug is fixed this should go back to the way
	 * it was.
	 * XXX 1157128 was closed as a dup of 1099747.  That bug was fixed by
	 * disallowing setlocale() to anything other than "C".  "" is
	 * allowed, but only if none of the envars LC_ALL, LC_COLLATE, or LANG
	 * select anything other than "C".
	 */
	(void) setlocale(LC_CTYPE, "");
	(void) setlocale(LC_NUMERIC, "");
	(void) setlocale(LC_TIME, "");
	(void) setlocale(LC_MONETARY, "");
	(void) setlocale(LC_MESSAGES, "");
#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN "SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);
	getwidth(&wp);
	if ((byteorder = byteorder_create()) == NULL) {
		(void) fprintf(stderr,
		    gettext("Cannot create byteorder context\n"));
		done(1);
	}

	if ((savepwd = open(".", O_RDONLY)) < 0) {
		(void) fprintf(stderr,
		    gettext("Cannot save current directory context\n"));
		done(1);
	}

	set_tmpdir();

	autoload_period = 12;
	autoload_tries = 12;	/* traditional default of ~2.5 minutes */

	sa.sa_handler = onintr;
	sa.sa_flags = SA_RESTART;
	(void) sigemptyset(&sa.sa_mask);

	(void) sigaction(SIGINT, &sa, &osa);
	if (osa.sa_handler == SIG_IGN)
		(void) sigaction(SIGINT, &osa, (struct sigaction *)0);

	(void) sigaction(SIGTERM, &sa, &osa);
	if (osa.sa_handler == SIG_IGN)
		(void) sigaction(SIGTERM, &osa, (struct sigaction *)0);
	if (argc < 2) {
usage:
		(void) fprintf(stderr, gettext("Usage:\n\
\t%s tabcdfhsvyLloT [file file ...]\n\
\t%s xabcdfhmsvyLloT [file file ...]\n\
\t%s iabcdfhmsvyLloT\n\
\t%s rabcdfsvyLloT\n\
\t%s RabcdfsvyLloT\n\n\
a requires an archive file name\n\
b requires a blocking factor\n\
f requires a dump file\n\
s requires a file number\n\
L requires a tape label\n\
If set, the envar TMPDIR selects where temporary files are kept\n"),
		    progname, progname, progname, progname, progname);
		done(1);
	}

	argv++;			/* the bag-of-options */
	argc -= 2;		/* count of parameters to the options  */
	command = '\0';
	c_label = (char *)NULL;	/* any tape's acceptable */
	for (cp = *argv++; *cp; cp++) {
		switch (*cp) {		/* BE CAUTIOUS OF FALLTHROUGHS */
		case 'T':
			if (argc < 1) {
				(void) fprintf(stderr, gettext(
				    "Missing autoload timeout period\n"));
				done(1);
			}

			count = atoi(*argv);
			if (count < 1) {
				(void) fprintf(stderr, gettext(
			    "Unreasonable autoload timeout period `%s'\n"),
					*argv);
				done(1);
			}
			units = *(*argv + strlen(*argv) - 1);
			switch (units) {
			case 's':
				multiplier = 1;
				break;
			case 'h':
				multiplier = 3600;
				break;
			case '0': case '1': case '2': case '3': case '4':
			case '5': case '6': case '7': case '8': case '9':
			case 'm':
				multiplier = 60;
				break;
			default:
				(void) fprintf(stderr, gettext(
				    "Unknown timeout units indicator `%c'\n"),
				    units);
				done(1);
			}
			autoload_tries = 1 +
			    ((count * multiplier) / autoload_period);
			argv++;
			argc--;
			break;
		case 'l':
			autoload++;
			break;
		case 'o':
			offline++;
			break;
		case '-':
			break;
		case 'a':
			if (argc < 1) {
				(void) fprintf(stderr,
					gettext("missing archive file name\n"));
				done(1);
			}
			archivefile = *argv++;
			if (*archivefile == '\0') {
				(void) fprintf(stderr,
				    gettext("empty archive file name\n"));
				done(1);
			}
			argc--;
			break;
		case 'c':
			cvtflag++;
			break;
		case 'd':
			dflag++;
			break;
		case 'D':
			/*
			 * This used to be the Dflag, but it doesn't
			 * hurt to always check, so was removed.  This
			 * case is here for backward compatability.
			 */
			break;
		case 'h':
			hflag = 0;
			break;
		case 'm':
			mflag = 0;
			break;
		case 'v':
			vflag++;
			break;
		case 'y':
			yflag++;
			break;
		case 'f':
			if (argc < 1) {
				(void) fprintf(stderr,
				    gettext("missing device specifier\n"));
				done(1);
			}
			inputdev = *argv++;
			if (*inputdev == '\0') {
				(void) fprintf(stderr,
				    gettext("empty device specifier\n"));
				done(1);
			}
			fflag++;
			argc--;
			break;
		case 'b':
			/*
			 * change default tape blocksize
			 */
			bflag++;
			if (argc < 1) {
				(void) fprintf(stderr,
					gettext("missing block size\n"));
				done(1);
			}
			saved_ntrec = ntrec = atoi(*argv++);
			if (ntrec == 0 || (ntrec&1)) {
				(void) fprintf(stderr, gettext(
			    "Block size must be a positive, even integer\n"));
				done(1);
			}
			ntrec /= (tp_bsize/DEV_BSIZE);
			argc--;
			break;
		case 's':
			/*
			 * dumpnum (skip to) for multifile dump tapes
			 */
			if (argc < 1) {
				(void) fprintf(stderr,
					gettext("missing dump number\n"));
				done(1);
			}
			dumpnum = atoi(*argv++);
			if (dumpnum <= 0) {
				(void) fprintf(stderr, gettext(
			    "Dump number must be a positive integer\n"));
				done(1);
			}
			argc--;
			break;
		case 't':
		case 'R':
		case 'r':
		case 'x':
		case 'i':
			if (command != '\0') {
				(void) fprintf(stderr, gettext(
				    "%c and %c are mutually exclusive\n"),
				    (uchar_t)*cp, (uchar_t)command);
				goto usage;
			}
			command = *cp;
			break;
		case 'L':
			if (argc < 1 || **argv == '\0') {
				(void) fprintf(stderr,
				    gettext("Missing tape label name\n"));
				done(1);
			}
			c_label = *argv++; /* must get tape with this label */
			if (strlen(c_label) > (sizeof (spcl.c_label) - 1)) {
				c_label[sizeof (spcl.c_label) - 1] = '\0';
				(void) fprintf(stderr, gettext(
		    "Truncating label to maximum supported length: `%s'\n"),
				    c_label);
			}
			argc--;
			break;

		default:
			(void) fprintf(stderr,
			    gettext("Bad key character %c\n"), (uchar_t)*cp);
			goto usage;
		}
	}
	if (command == '\0') {
		(void) fprintf(stderr,
		    gettext("must specify i, t, r, R, or x\n"));
		goto usage;
	}
	setinput(inputdev, archivefile);
	if (argc == 0) {	/* re-use last argv slot for default */
		argc = 1;
		*--argv = mflag ? "." : "2";
	}
	switch (command) {

	/*
	 * Interactive mode.
	 */
	case 'i':
		setup();
		extractdirs(1);
		initsymtable((char *)0);
		initpagercmd();
		runcmdshell();
		done(0);
		/* NOTREACHED */
	/*
	 * Incremental restoration of a file system.
	 */
	case 'r':
		setup();
		if (dumptime > 0) {
			/*
			 * This is an incremental dump tape.
			 */
			vprintf(stdout, gettext("Begin incremental restore\n"));
			initsymtable(symtbl);
			extractdirs(1);
			removeoldleaves();
			vprintf(stdout, gettext("Calculate node updates.\n"));
			strcpy(name, ".");
			name[2] = '\0';
			treescan(name, ROOTINO, nodeupdates);
			attrscan(1, nodeupdates);
			findunreflinks();
			removeoldnodes();
		} else {
			/*
			 * This is a level zero dump tape.
			 */
			vprintf(stdout, gettext("Begin level 0 restore\n"));
			initsymtable((char *)0);
			extractdirs(1);
			vprintf(stdout,
			    gettext("Calculate extraction list.\n"));
			strcpy(name, ".");
			name[2] = '\0';
			treescan(name, ROOTINO, nodeupdates);
			attrscan(1, nodeupdates);
		}
		createleaves(symtbl);
		createlinks();
		setdirmodes();
		checkrestore();
		if (dflag) {
			vprintf(stdout,
			    gettext("Verify the directory structure\n"));
			strcpy(name, ".");
			name[2] = '\0';
			treescan(name, ROOTINO, verifyfile);
		}
		dumpsymtable(symtbl, (long)1);
		done(0);
		/* NOTREACHED */
	/*
	 * Resume an incremental file system restoration.
	 */
	case 'R':
		setupR();
		initsymtable(symtbl);
		skipmaps();
		skipdirs();
		createleaves(symtbl);
		createlinks();
		setdirmodes();
		checkrestore();
		dumpsymtable(symtbl, (long)1);
		done(0);
		/* NOTREACHED */
	/*
	 * List contents of tape.
	 */
	case 't':
		setup();
		extractdirs(0);
		initsymtable((char *)0);
		if (vflag)
			printdumpinfo();
		while (argc--) {
			canon(*argv++, name, sizeof (name));
			name[strlen(name)+1] = '\0';
			ino = dirlookup(name);
			if (ino == 0)
				continue;
			treescan(name, ino, listfile);
		}
		done(0);
		/* NOTREACHED */
	/*
	 * Batch extraction of tape contents.
	 */
	case 'x':
		setup();
		extractdirs(1);
		initsymtable((char *)0);
		while (argc--) {
			if (mflag) {
				canon(*argv++, name, sizeof (name));
				if (expand(name, 0, &alist) == 0) {
					/* no meta-characters to expand */
					ino = dirlookup(name);
					if (ino == 0)
						continue;
					pathcheck(name);
				} else {
					/* add each of the expansions */
					while ((alist.last - alist.head) > 0) {
						fname = alist.head->fname;
						ino = dirlookup(fname);
						if (ino != 0) {
							pathcheck(fname);
							treescan(fname, ino,
							    addfile);
						}
						freename(fname);
						alist.head++;
					}
					alist.head = (struct afile *)NULL;
					continue; /* argc loop */
				}
			} else {
				ino = (ino_t)atol(*argv);
				if ((*(*argv++) == '-') || ino < ROOTINO) {
					(void) fprintf(stderr, gettext(
					    "bad inode number: %ld\n"),
					    ino);
					done(1);
				}
				name[0] = '\0';
			}
			treescan(name, ino, addfile);
			attrscan(0, addfile);
		}
		createfiles();
		createlinks();
		setdirmodes();
		if (dflag)
			checkrestore();
		done(0);
		/* NOTREACHED */
	}
	return (0);
}

/*
 * Determine where the user wants us to put our temporary files,
 * and make sure we can actually do so.  Bail out if there's a problem.
 */
void
set_tmpdir(void)
{
	int fd;
	char name[MAXPATHLEN];

	tmpdir = getenv("TMPDIR");
	if ((tmpdir == (char *)NULL) || (*tmpdir == '\0'))
		tmpdir = "/tmp";

	if (*tmpdir != '/') {
		(void) fprintf(stderr,
		    gettext("TMPDIR is not an absolute path (`%s').\n"),
		    tmpdir);
		done(1);
	}

	/*
	 * The actual use of tmpdir is in dirs.c, and is of the form
	 * tmpdir + "/rst" + type (three characters) + "%ld.XXXXXX" +
	 * a trailing NUL, where %ld is an arbitrary time_t.
	 *
	 * Thus, the magic 31 is strlen(itoa(MAX_TIME_T)) + "/rst" +
	 * ".XXXXXX" + '\0'.  A time_t is 64 bits, so MAX_TIME_T is
	 * LONG_MAX - nineteen digits.  In theory, so many things in
	 * ufsrestore will break once time_t's value goes beyond 32
	 * bits that it's not worth worrying about this particular
	 * instance at this time, but we've got to start somewhere.
	 *
	 * Note that the use of a pid below is just for testing the
	 * validity of the named directory.
	 */
	if (strlen(tmpdir) > (MAXPATHLEN - 31)) {
		(void) fprintf(stderr, gettext("TMPDIR too long\n"));
		done(1);
	}

	/* Guaranteed to fit by above test (sizeof(time_t) >= sizeof(pid_t)) */
	(void) snprintf(name, sizeof (name), "%s/rstdir.%ld", tmpdir, getpid());

	/*
	 * This is effectively a stripped-down version of safe_open(),
	 * because if the file exists, we want to fail.
	 */
	fd = open(name, O_CREAT|O_EXCL|O_RDWR, 0600);
	if (fd < 0) {
		perror(gettext("Can not create temporary file"));
		done(1);
	}

	(void) close(fd);
	if (unlink(name) < 0) {
		perror(gettext("Can not delete temporary file"));
		done(1);
	}
}
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/

/*
 * Copyright (c) 1983 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#include "restore.h"
/* undef MAXNAMLEN to prevent compiler warnings about redef in dirent.h */
#undef MAXNAMLEN
#include <dirent.h>

static char *keyval(int);
static void removexattrs(struct entry *);
static void movexattrs(char *, char *);

/*
 * This implements the 't' option.
 * List entries on the tape.
 */
long
listfile(char *name, ino_t ino, int type)
{
	long descend = hflag ? GOOD : FAIL;

	if (BIT(ino, dumpmap) == 0) {
		return (descend);
	}
	vprintf(stdout, "%s", type == LEAF ? gettext("leaf") : gettext("dir "));
	(void) fprintf(stdout, "%10lu\t%s\n", ino, name);
	return (descend);
}

/*
 * This implements the 'x' option.
 * Request that new entries be extracted.
 */
long
addfile(char *name, ino_t ino, int type)
{
	struct entry *ep;
	long descend = hflag ? GOOD : FAIL;
	char buf[100];

	/* Don't know if ino_t is long or long long, so be safe w/ *printf() */

	if (BIT(ino, dumpmap) == 0) {
		if (mflag) {
			dprintf(stdout, gettext(
			    "%s: not on the volume\n"), name);
		} else {
			dprintf(stdout, gettext(
			    "inode %llu: not on the volume\n"),
			    (u_longlong_t)ino);
		}
		return (descend);
	}
	if (!mflag) {
		(void) snprintf(buf, sizeof (buf), "./%llu", (u_longlong_t)ino);
		buf[sizeof (buf) - 1] = '\0';
		name = buf;
		if (type == NODE) {
			(void) genliteraldir(name, ino);
			return (descend);
		}
	}
	ep = lookupino(ino);
	if (ep != NIL) {
		if (strcmp(name, myname(ep)) == 0) {
			/* LINTED: result fits into a short */
			ep->e_flags |= NEW;
			return (descend);
		}
		type |= LINK;
	}
	ep = addentry(name, ino, type);
	if (type == NODE)
		newnode(ep);
	/* LINTED: result fits into a short */
	ep->e_flags |= NEW;
	return (descend);
}

/*
 * This is used by the 'i' option to undo previous requests made by addfile.
 * Delete entries from the request queue.
 */
/* ARGSUSED */
long
deletefile(char *name, ino_t ino, int type)
{
	long descend = hflag ? GOOD : FAIL;
	struct entry *ep;

	if (BIT(ino, dumpmap) == 0) {
		return (descend);
	}
	ep = lookupino(ino);
	if (ep != NIL) {
		/* LINTED: result fits into a short */
		ep->e_flags &= ~NEW;
	}
	return (descend);
}

/*
 * The following four routines implement the incremental
 * restore algorithm. The first removes old entries, the second
 * does renames and calculates the extraction list, the third
 * cleans up link names missed by the first two, and the final
 * one deletes old directories.
 *
 * Directories cannot be immediately deleted, as they may have
 * other files in them which need to be moved out first. As
 * directories to be deleted are found, they are put on the
 * following deletion list. After all deletions and renames
 * are done, this list is actually deleted.
 */
static struct entry *removelist;

/*
 *	Remove unneeded leaves from the old tree.
 *	Remove directories from the lookup chains.
 */
void
removeoldleaves(void)
{
	struct entry *ep;
	ino_t i;

	vprintf(stdout, gettext("Mark entries to be removed.\n"));
	for (i = ROOTINO + 1; i < maxino; i++) {
		if (BIT(i, clrimap))
			continue;
		ep = lookupino(i);
		if (ep == NIL)
			continue;
		while (ep != NIL) {
			dprintf(stdout, gettext("%s: REMOVE\n"), myname(ep));
			removexattrs(ep->e_xattrs);
			if (ep->e_type == LEAF) {
				removeleaf(ep);
				freeentry(ep);
			} else {
				mktempname(ep);
				deleteino(ep->e_ino);
				/*
				 * once the inode is deleted from the symbol
				 * table, the e_next field is reusable
				 */
				ep->e_next = removelist;
				removelist = ep;
			}
			ep = ep->e_links;
		}
	}
}

/*
 *	For each directory entry on the incremental tape, determine which
 *	category it falls into as follows:
 *	KEEP - entries that are to be left alone.
 *	NEW - new entries to be added.
 *	EXTRACT - files that must be updated with new contents.
 *	LINK - new links to be added.
 *	Renames are done at the same time.
 */
long
nodeupdates(char *name, ino_t ino, int type)
{
	struct entry *ep, *np, *ip;
	long descend = GOOD;
	int lookuptype = 0;
	int key = 0;
		/* key values */
#define	ONTAPE	0x1	/* inode is on the tape */
#define	INOFND	0x2	/* inode already exists */
#define	NAMEFND	0x4	/* name already exists */
#define	MODECHG	0x8	/* mode of inode changed */

	/*
	 * This routine is called once for each element in the
	 * directory hierarchy, with a full path name.
	 * The "type" value is incorrectly specified as LEAF for
	 * directories that are not on the dump tape.
	 *
	 * Check to see if the file is on the tape.
	 */
	if (BIT(ino, dumpmap))
		key |= ONTAPE;
	/*
	 * Check to see if the name exists, and if the name is a link.
	 */
	np = lookupname(name);
	if (np != NIL) {
		key |= NAMEFND;
		ip = lookupino(np->e_ino);
		if (ip == NULL) {
			(void) fprintf(stderr,
			    gettext("corrupted symbol table\n"));
			done(1);
		}
		if (ip != np)
			lookuptype = LINK;
	}
	/*
	 * Check to see if the inode exists, and if one of its links
	 * corresponds to the name (if one was found).
	 */
	ip = lookupino(ino);
	if (ip != NIL) {
		key |= INOFND;
		for (ep = ip->e_links; ep != NIL; ep = ep->e_links) {
			if (ep == np) {
				/*
				 * Need to set the NEW flag on the hard link
				 * so it gets created because we extract the
				 * "parent".  If the NAMEFND key is set, remove
				 * the leaf.
				 */
				if (ip->e_flags & EXTRACT) {
					if (key & NAMEFND) {
						removeleaf(np);
						freeentry(np);
						np = NIL;
						key &= ~NAMEFND;
					}
					ep->e_flags |= NEW;
				} else {
					ip = ep;
				}
				break;
			}
		}
	}
	/*
	 * If both a name and an inode are found, but they do not
	 * correspond to the same file, then both the inode that has
	 * been found and the inode corresponding to the name that
	 * has been found need to be renamed. The current pathname
	 * is the new name for the inode that has been found. Since
	 * all files to be deleted have already been removed, the
	 * named file is either a now-unneeded link, or it must live
	 * under a new name in this dump level. If it is a link, it
	 * can be removed. If it is not a link, it is given a
	 * temporary name in anticipation that it will be renamed
	 * when it is later found by inode number.
	 */
	if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) {
		if (lookuptype == LINK) {
			removeleaf(np);
			freeentry(np);
		} else {
			dprintf(stdout,
			    gettext("name/inode conflict, mktempname %s\n"),
			    myname(np));
			mktempname(np);
		}
		np = NIL;
		key &= ~NAMEFND;
	}
	if ((key & ONTAPE) &&
	    (((key & INOFND) && ip->e_type != type) ||
	    ((key & NAMEFND) && np->e_type != type)))
		key |= MODECHG;

	/*
	 * Decide on the disposition of the file based on its flags.
	 * Note that we have already handled the case in which
	 * a name and inode are found that correspond to different files.
	 * Thus if both NAMEFND and INOFND are set then ip == np.
	 */
	switch (key) {

	/*
	 * A previously existing file has been found.
	 * Mark it as KEEP so that other links to the inode can be
	 * detected, and so that it will not be reclaimed by the search
	 * for unreferenced names.
	 */
	case INOFND|NAMEFND:
		/* LINTED: result fits into a short */
		ip->e_flags |= KEEP;
		dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
		    flagvalues(ip));
		break;

	/*
	 * A file on the tape has a name which is the same as a name
	 * corresponding to a different file in the previous dump.
	 * Since all files to be deleted have already been removed,
	 * this file is either a now-unneeded link, or it must live
	 * under a new name in this dump level. If it is a link, it
	 * can simply be removed. If it is not a link, it is given a
	 * temporary name in anticipation that it will be renamed
	 * when it is later found by inode number (see INOFND case
	 * below). The entry is then treated as a new file.
	 */
	case ONTAPE|NAMEFND:
	case ONTAPE|NAMEFND|MODECHG:
		if (lookuptype == LINK || key == (ONTAPE|NAMEFND)) {
			removeleaf(np);
			freeentry(np);
		} else {
			/*
			 * Create a temporary node only if MODECHG.
			 */
			mktempname(np);
		}
		/*FALLTHROUGH*/

	/*
	 * A previously non-existent file.
	 * Add it to the file system, and request its extraction.
	 * If it is a directory, create it immediately.
	 * (Since the name is unused there can be no conflict)
	 */
	case ONTAPE:
		ep = addentry(name, ino, type);
		if (type == NODE)
			newnode(ep);
		/* LINTED: result fits into a short */
		ep->e_flags |= NEW|KEEP;
		dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
		    flagvalues(ep));
		break;

	/*
	 * A file with the same inode number, but a different
	 * name has been found. If the other name has not already
	 * been found (indicated by the KEEP flag, see above) then
	 * this must be a new name for the file, and it is renamed.
	 * If the other name has been found then this must be a
	 * link to the file. Hard links to directories are not
	 * permitted, and are either deleted or converted to
	 * symbolic links. Finally, if the file is on the tape,
	 * a request is made to extract it.
	 */
	case ONTAPE|INOFND:
		if (type == LEAF && (ip->e_flags & KEEP) == 0) {
			/* LINTED: result fits into a short */
			ip->e_flags |= EXTRACT;
		}
		/*FALLTHROUGH*/
	case INOFND:
		if ((ip->e_flags & KEEP) == 0) {
			renameit(myname(ip), name);
			moveentry(ip, name);
			/* LINTED: result fits into a short */
			ip->e_flags |= KEEP;
			dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
			    flagvalues(ip));
			break;
		}
		if (ip->e_type == NODE) {
			descend = FAIL;
			(void) fprintf(stderr, gettext(
			    "deleted hard link %s to directory %s\n"),
			    name, myname(ip));
			break;
		}
		ep = addentry(name, ino, type|LINK);
		/* LINTED: result fits into a short */
		ep->e_flags |= NEW;
		dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
		    flagvalues(ep));
		break;

	/*
	 * A previously known file which is to be updated.
	 */
	case ONTAPE|INOFND|NAMEFND:
		/*
		 * Extract leaf nodes.
		 */
		if (type == LEAF) {
			/* LINTED: result fits into a short */
			np->e_flags |= EXTRACT;
		}
		/* LINTED: result fits into a short */
		np->e_flags |= KEEP;
		dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
		    flagvalues(np));
		break;

	/*
	 * An inode is being reused in a completely different way.
	 * Normally an extract can simply do an "unlink" followed
	 * by a "creat". Here we must do effectively the same
	 * thing. The complications arise because we cannot really
	 * delete a directory since it may still contain files
	 * that we need to rename, so we delete it from the symbol
	 * table, and put it on the list to be deleted eventually.
	 * Conversely if a directory is to be created, it must be
	 * done immediately, rather than waiting until the
	 * extraction phase.
	 */
	case ONTAPE|INOFND|MODECHG:
	case ONTAPE|INOFND|NAMEFND|MODECHG:
		if (ip->e_flags & KEEP) {
			badentry(ip, gettext("cannot KEEP and change modes"));
			break;
		}
		if (ip->e_type == LEAF) {
			/* changing from leaf to node */
			removeleaf(ip);
			freeentry(ip);
			ip = addentry(name, ino, type);
			newnode(ip);
		} else {
			/* changing from node to leaf */
			if ((ip->e_flags & TMPNAME) == 0)
				mktempname(ip);
			deleteino(ip->e_ino);
			ip->e_next = removelist;
			removelist = ip;
			ip = addentry(name, ino, type);
		}
		/* LINTED: result fits into a short */
		ip->e_flags |= NEW|KEEP;
		dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
		    flagvalues(ip));
		break;

	/*
	 * A hard link to a directory that has been removed.
	 * Ignore it.
	 */
	case NAMEFND:
		dprintf(stdout, gettext("[%s] %s: Extraneous name\n"),
		    keyval(key), name);
		descend = FAIL;
		break;

	/*
	 * If we find a directory entry for a file that is not on
	 * the tape, then we must have found a file that was created
	 * while the dump was in progress. Since we have no contents
	 * for it, we discard the name knowing that it will be on the
	 * next incremental tape.
	 */
	case 0:
		(void) fprintf(stderr,
		    gettext("%s: (inode %lu) not found on volume\n"),
		    name, ino);
		break;

	/*
	 * If any of these arise, something is grievously wrong with
	 * the current state of the symbol table.
	 */
	case INOFND|NAMEFND|MODECHG:
	case NAMEFND|MODECHG:
	case INOFND|MODECHG:
		(void) fprintf(stderr, "[%s] %s: %s\n",
		    keyval(key), name, gettext("inconsistent state"));
		done(1);
		/* NOTREACHED */

	/*
	 * These states "cannot" arise for any state of the symbol table.
	 */
	case ONTAPE|MODECHG:
	case MODECHG:
	default:
		(void) fprintf(stderr, "[%s] %s: %s\n",
		    keyval(key), name, gettext("impossible state"));
		done(1);
		/* NOTREACHED */
	}
	return (descend);
}

/*
 * Calculate the active flags in a key.
 */
static char *
keyval(int key)
{
	static char keybuf[32];

	/* Note longest case is everything except |NIL */

	(void) strcpy(keybuf, "|NIL");
	keybuf[0] = '\0';
	if (key & ONTAPE)
		(void) strcat(keybuf, "|ONTAPE");
	if (key & INOFND)
		(void) strcat(keybuf, "|INOFND");
	if (key & NAMEFND)
		(void) strcat(keybuf, "|NAMEFND");
	if (key & MODECHG)
		(void) strcat(keybuf, "|MODECHG");
	return (&keybuf[1]);
}

/*
 * Find unreferenced link names.
 */
void
findunreflinks(void)
{
	struct entry *ep, *np;
	ino_t i;

	vprintf(stdout, gettext("Find unreferenced names.\n"));
	for (i = ROOTINO; i < maxino; i++) {
		ep = lookupino(i);
		if (ep == NIL || ep->e_type == LEAF || BIT(i, dumpmap) == 0)
			continue;
		for (np = ep->e_entries; np != NIL; np = np->e_sibling) {
			if (np->e_flags == 0) {
				dprintf(stdout, gettext(
				    "%s: remove unreferenced name\n"),
				    myname(np));
				removeleaf(np);
				freeentry(np);
			}
		}
	}
	/*
	 * Any leaves remaining in removed directories are unreferenced.
	 */
	for (ep = removelist; ep != NIL; ep = ep->e_next) {
		for (np = ep->e_entries; np != NIL; np = np->e_sibling) {
			if (np->e_type == LEAF) {
				if (np->e_flags != 0)
					badentry(np, gettext(
					    "unreferenced with flags"));
				dprintf(stdout, gettext(
				    "%s: remove unreferenced name\n"),
				    myname(np));
				removeleaf(np);
				freeentry(np);
			}
		}
	}
}

/*
 * Remove old nodes (directories).
 * Note that this routine runs in O(N*D) where:
 *	N is the number of directory entries to be removed.
 *	D is the maximum depth of the tree.
 * If N == D this can be quite slow. If the list were
 * topologically sorted, the deletion could be done in
 * time O(N).
 */
void
removeoldnodes(void)
{
	struct entry *ep, **prev;
	long change;

	vprintf(stdout, gettext("Remove old nodes (directories).\n"));
	do	{
		change = 0;
		prev = &removelist;
		for (ep = removelist; ep != NIL; ep = *prev) {
			if (ep->e_entries != NIL) {
				prev = &ep->e_next;
				continue;
			}
			*prev = ep->e_next;
			removenode(ep);
			freeentry(ep);
			change++;
		}
	} while (change);
	for (ep = removelist; ep != NIL; ep = ep->e_next)
		badentry(ep, gettext("cannot remove, non-empty"));
}

/*
 * This is the routine used to extract files for the 'r' command.
 * Extract new leaves.
 */
void
createleaves(char *symtabfile)
{
	struct entry *ep;
	char name[MAXCOMPLEXLEN];
	ino_t first;
	int curvol;

	if (command == 'R') {
		vprintf(stdout, gettext("Continue extraction of new leaves\n"));
	} else {
		vprintf(stdout, gettext("Extract new leaves.\n"));
		dumpsymtable(symtabfile, volno);
	}
	first = lowerbnd(ROOTINO);
	curvol = volno;
	while (curfile.ino < maxino) {
		first = lowerbnd(first);
		/*
		 * If the next available file is not the one which we
		 * expect then we have missed one or more files. Since
		 * we do not request files that were not on the tape,
		 * the lost files must have been due to a tape read error,
		 * or a file that was removed while the dump was in progress.
		 *
		 * The loop will terminate with first == maxino, if not
		 * sooner.  Due to the e_flags manipulation, lowerbnd()
		 * will never return its argument.
		 */
		while (first < curfile.ino) {
			ep = lookupino(first);
			if (ep == NIL) {
				(void) fprintf(stderr,
				    gettext("%d: bad first\n"), first);
				done(1);
			}
			(void) fprintf(stderr,
			    gettext("%s: not found on volume\n"),
			    myname(ep));
			/* LINTED: result fits into a short */
			ep->e_flags &= ~(NEW|EXTRACT);
			first = lowerbnd(first);
		}
		/*
		 * If we find files on the tape that have no corresponding
		 * directory entries, then we must have found a file that
		 * was created while the dump was in progress. Since we have
		 * no name for it, we discard it knowing that it will be
		 * on the next incremental tape.
		 */
		if (first != curfile.ino) {
			(void) fprintf(stderr,
			    gettext("expected next file %d, got %d\n"),
			    first, curfile.ino);
			skipfile();
			goto next;
		}
		ep = lookupino(curfile.ino);
		if (ep == NIL) {
			(void) fprintf(stderr,
			    gettext("unknown file on volume\n"));
			done(1);
		}
		if ((ep->e_flags & (NEW|EXTRACT)) == 0)
			badentry(ep, gettext("unexpected file on volume"));
		/*
		 * If the file is to be extracted, then the old file must
		 * be removed since its type may change from one leaf type
		 * to another (eg "file" to "character special"). But we
		 * also need to preserve any existing extended attributes;
		 * so first rename the file, then move its attributes, then
		 * remove it.
		 */
		if ((ep->e_flags & EXTRACT) != 0) {
			char *sname = savename(ep->e_name);
			complexcpy(name, myname(ep), MAXCOMPLEXLEN);
			mktempname(ep);
			(void) extractfile(name);
			movexattrs(myname(ep), name);
			removeleaf(ep);
			freename(ep->e_name);
			ep->e_name = sname;
			ep->e_namlen = strlen(ep->e_name);
			/* LINTED: result fits into a short */
			ep->e_flags &= ~REMOVED;
		} else {
			(void) extractfile(myname(ep));
		}
		/* LINTED: result fits into a short */
		ep->e_flags &= ~(NEW|EXTRACT);
		/*
		 * We checkpoint the restore after every tape reel, so
		 * as to simplify the amount of work required by the
		 * 'R' command.
		 */
	next:
		if (curvol != volno) {
			dumpsymtable(symtabfile, volno);
			skipmaps();
			curvol = volno;
		}
	}
}

/*
 * This is the routine used to extract files for the 'x' and 'i' commands.
 * Efficiently extract a subset of the files on a tape.
 */
void
createfiles(void)
{
	ino_t first, next, last;
	struct entry *ep;
	int curvol, nextvol;

	vprintf(stdout, gettext("Extract requested files\n"));
	first = lowerbnd(ROOTINO);
	last = upperbnd(maxino - 1);
	nextvol = volnumber(first);
	if (nextvol == 0) {
		curfile.action = SKIP;
		getvol(1);
		skipmaps();
		skipdirs();
	}
	for (;;) {
		first = lowerbnd(first);
		last = upperbnd(last);
		/*
		 * Check to see if any files remain to be extracted
		 */
		if (first > last)
			return;
		/*
		 * If a map of inode numbers to tape volumes is
		 * available, then select the next volume to be read.
		 */
		if (nextvol > 0) {
			nextvol = volnumber(first);
			if (nextvol != volno) {
				curfile.action = UNKNOWN;
				getvol(nextvol);
				skipmaps();
			}
		}
		/*
		 * Reject any volumes with inodes greater than
		 * the last one needed. This will only be true
		 * if the above code has not selected a volume.
		 */
		while (curfile.ino > last) {
			curfile.action = SKIP;
			getvol(0);
			skipmaps();
			skipdirs();
		}
		/*
		 * Decide on the next inode needed.
		 * Skip across the inodes until it is found
		 * or an out of order volume change is encountered
		 */
		next = lowerbnd(curfile.ino);
		do	{
			curvol = volno;
			while (next > curfile.ino && volno == curvol)
				skipfile();
			skipmaps();
			skipdirs();
		} while (volno == curvol + 1);
		/*
		 * If volume change out of order occurred the
		 * current state must be recalculated
		 */
		if (volno != curvol)
			continue;
		/*
		 * If the current inode is greater than the one we were
		 * looking for then we missed the one we were looking for.
		 * Since we only attempt to extract files listed in the
		 * dump map, the lost files must have been due to a tape
		 * read error, or a file that was removed while the dump
		 * was in progress. Thus we report all requested files
		 * between the one we were looking for, and the one we
		 * found as missing, and delete their request flags.
		 */
		while (next < curfile.ino) {
			ep = lookupino(next);
			if (ep == NIL) {
				(void) fprintf(stderr,
				    gettext("corrupted symbol table\n"));
				done(1);
			}
			(void) fprintf(stderr,
			    gettext("%s: not found on volume\n"),
			    myname(ep));
			/* LINTED: result fits into a short */
			ep->e_flags &= ~NEW;
			next = lowerbnd(next);
		}
		/*
		 * The current inode is the one that we are looking for,
		 * so extract it per its requested name.
		 */
		if (next == curfile.ino && next <= last) {
			ep = lookupino(next);
			if (ep == NIL) {
				(void) fprintf(stderr,
				    gettext("corrupted symbol table\n"));
				done(1);
			}
			(void) extractfile(myname(ep));
			/* LINTED: result fits into a short */
			ep->e_flags &= ~NEW;
			if (volno != curvol)
				skipmaps();
		}
	}
}

/*
 * Add links.
 */
void
createlinks(void)
{
	struct entry *np, *ep;
	ino_t i;
	int dfd;
	char *to, *from;
	int saverr;

	vprintf(stdout, gettext("Add links\n"));
	for (i = ROOTINO; i < maxino; i++) {
		ep = lookupino(i);
		if (ep == NIL)
			continue;
		to = savename(myname(ep));
		for (np = ep->e_links; np != NIL; np = np->e_links) {
			if ((np->e_flags & NEW) == 0)
				continue;
			resolve(myname(np), &dfd, &from);
			if (dfd != AT_FDCWD) {
				if (fchdir(dfd) < 0) {
					saverr = errno;
					(void) fprintf(stderr,
					    gettext(
					    "%s->%s: link failed: %s\n"),
					    from, to, strerror(saverr));
					(void) close(dfd);
					continue;
				}
			}
			if (ep->e_type == NODE) {
				(void) lf_linkit(to, from, SYMLINK);
			} else {
				(void) lf_linkit(to, from, HARDLINK);
			}
			/* LINTED: result fits into a short */
			np->e_flags &= ~NEW;
			if (dfd != AT_FDCWD) {
				fchdir(savepwd);
				(void) close(dfd);
			}
		}
		freename(to);
	}
}

/*
 * Check the symbol table.
 * We do this to insure that all the requested work was done, and
 * that no temporary names remain.
 */
void
checkrestore(void)
{
	struct entry *ep;
	ino_t i;

	vprintf(stdout, gettext("Check the symbol table.\n"));
	for (i = ROOTINO; i < maxino; i++) {
		for (ep = lookupino(i); ep != NIL; ep = ep->e_links) {
			/* LINTED: result fits into a short */
			ep->e_flags &= ~KEEP;
			if (ep->e_type == NODE) {
				/* LINTED: result fits into a short */
				ep->e_flags &= ~(NEW|EXISTED);
			}
			if ((ep->e_flags & ~(XATTR|XATTRROOT)) != 0)
				badentry(ep, gettext("incomplete operations"));
		}
	}
}

/*
 * Compare with the directory structure on the tape
 * A paranoid check that things are as they should be.
 */
long
verifyfile(char *name, ino_t ino, int type)
{
	struct entry *np, *ep;
	long descend = GOOD;

	ep = lookupname(name);
	if (ep == NIL) {
		(void) fprintf(stderr,
		    gettext("Warning: missing name %s\n"), name);
		return (FAIL);
	}
	np = lookupino(ino);
	if (np != ep)
		descend = FAIL;
	for (; np != NIL; np = np->e_links)
		if (np == ep)
			break;
	if (np == NIL) {
		(void) fprintf(stderr, gettext("missing inumber %d\n"), ino);
		done(1);
	}
	if (ep->e_type == LEAF && type != LEAF)
		badentry(ep, gettext("type should be LEAF"));
	return (descend);
}

/*
 * This routine does not actually remove any attribute files, it
 * just removes entries from the symbol table.  The attribute files
 * themselves are assumed to be removed automatically when the
 * parent file is removed.
 */
static void
removexattrs(struct entry *ep)
{
	struct entry *np = ep;

	if (ep == NIL)
		return;
	for (np = ep->e_entries; np != NIL; np = np->e_sibling) {
		if (np->e_type == NODE) {
			removexattrs(np);
		} else {
			np->e_flags |= REMOVED;
			freeentry(np);
		}
	}
	ep->e_flags |= REMOVED;
	freeentry(ep);
}

/*
 * Move all the extended attributes associated with orig to
 * the file named by the second argument (targ).
 */
static void
movexattrs(char *orig, char *targ)
{
	char *to, *from;
	int fromfd, fromdir, tofd, todir, tfd;
	DIR *dirp = NULL;
	struct dirent *dp = NULL;

	fromfd = tofd = fromdir = todir = tfd = -1;

	resolve(orig, &tfd, &from);
	if (tfd == AT_FDCWD && pathconf(orig, _PC_XATTR_EXISTS) != 1) {
		/* no attributes to move */
		return;
	}
	if ((fromfd = openat64(tfd, from, O_RDONLY|O_NONBLOCK)) == -1) {
		fprintf(stderr, gettext("%s: cannot move attributes: "), from);
		perror("");
		if (tfd != AT_FDCWD) (void) close(tfd);
		goto out;
	}

	if (fpathconf(fromfd, _PC_XATTR_EXISTS) != 1) {
		/* no attributes to move */
		if (tfd != AT_FDCWD) (void) close(tfd);
		goto out;
	}
	if ((fromdir = openat64(fromfd, ".",
	    O_RDONLY|O_NONBLOCK|O_XATTR)) == -1) {
		fprintf(stderr, gettext("%s: cannot access attributes: "),
		    from);
		perror("");
		if (tfd != AT_FDCWD)
			(void) close(tfd);
		goto out;
	}
	if (tfd != AT_FDCWD) (void) close(tfd);

	resolve(targ, &tfd, &to);
	if ((tofd = openat64(tfd, to, O_RDONLY|O_NONBLOCK)) == -1 ||
	    (todir = openat64(tofd, ".", O_RDONLY|O_NONBLOCK|O_XATTR)) == -1) {
		fprintf(stderr, gettext("%s: cannot create attributes: "), to);
		perror("");
		goto out;
	}
	if (tfd != AT_FDCWD) (void) close(tfd);
	(void) close(tofd);

	if ((tfd = dup(fromdir)) == -1 ||
	    (dirp = fdopendir(tfd)) == NULL) {
		fprintf(stderr,
		gettext("%s: cannot allocate DIR structure to attribute "
		    "directory: "), from);
		perror("");
		if (tfd != -1) (void) close(tfd);
		goto out;
	}

	while ((dp = readdir(dirp)) != NULL) {
		if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') ||
		    (dp->d_name[0] == '.' && dp->d_name[1] == '.' &&
		    dp->d_name[2] == '\0'))
			continue;
		if ((renameat(fromdir, dp->d_name, todir, dp->d_name)) == -1) {
			fprintf(stderr,
			    gettext("%s: cannot move attribute %s: "),
			    from, dp->d_name);
			goto out;
		}
	}
out:
	if (fromfd != -1)
		(void) close(fromfd);
	if (tofd != -1)
		(void) close(tofd);
	if (dirp != NULL)
		(void) closedir(dirp);
	if (fromdir != -1)
		(void) close(fromdir);
	if (todir != -1)
		(void) close(todir);
}
/*
 * Copyright (c) 1983 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/

/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef _RESTORE_H
#define	_RESTORE_H

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/vnode.h>
#include <locale.h>
#include <stdlib.h>
#include <sys/fs/ufs_inode.h>
#include <sys/fs/ufs_fs.h>
#include <sys/fs/ufs_fsdir.h>
#include <note.h>

#ifdef	__cplusplus
extern "C" {
#endif

#define	ROOTINO	UFSROOTINO
#define	SUPPORTS_MTB_TAPE_FORMAT
#include <protocols/dumprestore.h>
#include <memutils.h>
#include <assert.h>

/*
 * Flags
 */
extern int	cvtflag;	/* convert from old to new tape format */
extern int	bflag;		/* set input block size */
extern int	dflag;		/* print out debugging info */
extern int	hflag;		/* restore heirarchies */
extern int	mflag;		/* restore by name instead of inode number */
extern int	vflag;		/* print out actions taken */
extern int	yflag;		/* always try to recover from tape errors */
extern int	paginating;	/* paginate bulk interactive output */
extern int	offline;	/* take tape offline when closing */
extern int	autoload;	/* wait for tape to autoload; implies offline */
/*
 * Global variables
 */
extern int	autoload_tries;	/* number of times to check on autoload */
extern int	autoload_period; /* seconds, tries*period = total wait time */
extern struct byteorder_ctx *byteorder;
extern char	*progname;	/* our name */
extern char	*dumpmap;	/* map of inodes on this dump tape */
extern char	*clrimap;	/* map of inodes to be deleted */
extern char	*c_label;	/* label we expect to see on the tape */
extern ino_t	maxino;		/* highest numbered inode in this file system */
extern long	dumpnum;	/* location of the dump on this tape */
extern int	volno;		/* current volume being read */
extern uint_t	ntrec;		/* number of tp_bsize records per tape block */
extern uint_t	saved_ntrec;	/* number of tp_bsize records per tape block */
extern ssize_t	tape_rec_size;	/* tape record size (tp_bsize * ntrec) */
extern time_t	dumptime;	/* time that this dump begins */
extern time_t	dumpdate;	/* time that this dump was made */
extern char	command;	/* opration being performed */
extern FILE	*terminal;	/* file descriptor for the terminal input */
extern char	*tmpdir;	/* where to put the rst{dir,mode}... files */
extern char	*pager_catenated; /* pager command and args */
extern char	**pager_vector;	/* pager_catenated split up for execve() */
extern int	pager_len;	/* # elements in pager_vector; includes NULL */
extern int	inattrspace;	/* true if currently scanning attribute space */
extern int	savepwd;	/* this is where restore is running from */

/*
 * Each file in the file system is described by one of these entries
 * Note that the e_next field is used by the symbol table hash lists
 * and then reused by the remove code after the entry is removed from
 * the symbol table.
 */
struct entry {
	char	*e_name;		/* the current name of this entry */
	ushort_t e_namlen;		/* length of this name */
	char	e_type;			/* type of this entry, see below */
	short	e_flags;		/* status flags, see below */
	ino_t	e_ino;			/* inode number in previous file sys */
	long	e_index;		/* unique index (for dumpped table) */
	struct	entry *e_parent;	/* pointer to parent directory (..) */
	struct	entry *e_sibling;	/* next element in this directory (.) */
	struct	entry *e_links;		/* hard links to this inode */
	struct	entry *e_entries;	/* for directories, their entries */
	struct	entry *e_xattrs;	/* pointer to extended attribute root */
	struct	entry *e_next;		/* hash chain list and removelist */
};
/* types */
#define	LEAF 1			/* non-directory entry */
#define	NODE 2			/* directory entry */
#define	LINK 4			/* synthesized type, stripped by addentry */
#define	ROOT 8			/* synthesized type, stripped by addentry */
/* flags */
#define	EXTRACT		0x0001	/* entry is to be replaced from the tape */
#define	NEW		0x0002	/* a new entry to be extracted */
#define	KEEP		0x0004	/* entry is not to change */
#define	REMOVED		0x0010	/* entry has been removed */
#define	TMPNAME		0x0020	/* entry has been given a temporary name */
#define	EXISTED		0x0040	/* directory already existed during extract */
#define	XATTR		0x0080	/* file belongs in an attribute tree */
#define	XATTRROOT	0x0100	/* directory is root of an attribute tree */
/*
 * functions defined on entry structs
 */
extern struct entry *lookupino(ino_t);
extern struct entry *lookupname(char *);
extern struct entry *addentry(char *, ino_t, int);
extern void deleteino(ino_t);
extern char *myname(struct entry *);
extern void freeentry(struct entry *);
extern void moveentry(struct entry *, char *);
extern char *savename(char *);
extern void freename(char *);
extern void dumpsymtable(char *, int);
extern void initsymtable(char *);
extern void mktempname(struct entry *);
extern char *gentempname(struct entry *);
extern void newnode(struct entry *);
extern void removenode(struct entry *);
extern void removeleaf(struct entry *);
extern ino_t lowerbnd(ino_t);
extern ino_t upperbnd(ino_t);
extern void badentry(struct entry *, char *);
extern char *flagvalues(struct entry *);
extern ino_t dirlookup(char *);
#define	NIL ((struct entry *)(0))

/*
 * Definitions for library routines operating on directories.
 * These definitions are used only for reading fake directory
 * entries from restore's temporary file "restoresymtable"
 * These have little to do with real directory entries.
 */
#if !defined(DEV_BSIZE)
#define	DEV_BSIZE	512
#endif
#define	DIRBLKSIZ	DEV_BSIZE
typedef struct _rstdirdesc {
	int	dd_fd;
	int	dd_refcnt;  /* so rst_{open,close}dir() avoid leaking memory */
	off64_t	dd_loc;
	off64_t	dd_size;
	char	dd_buf[DIRBLKSIZ];
} RST_DIR;

/*
 * Constants associated with entry structs
 */
#define	HARDLINK	1
#define	SYMLINK		2
#define	TMPHDR		"RSTTMP"

/*
 * The entry describes the next file available on the tape
 */
extern struct context {
	char	*name;		/* name of file */
	ino_t	ino;		/* inumber of file */
	struct	dinode *dip;	/* pointer to inode */
	int	action;		/* action being taken on this file */
	int	ts;		/* TS_* type of tape record */
} curfile;
/* actions */
#define	USING	1	/* extracting from the tape */
#define	SKIP	2	/* skipping */
#define	UNKNOWN 3	/* disposition or starting point is unknown */

/*
 * Structure and routines associated with listing directories
 * and expanding meta-characters in pathnames.
 */
struct afile {
	ino_t	fnum;		/* inode number of file */
	char	*fname;		/* file name */
	short	fflags;		/* extraction flags, if any */
	char	ftype;		/* file type, e.g. LEAF or NODE */
};
struct arglist {
	struct afile	*head;	/* start of argument list */
	struct afile	*last;	/* end of argument list */
	struct afile	*base;	/* current list arena */
	int		nent;	/* maximum size of list */
	char		*cmd;	/* the current command */
};

/*
 * Other exported routines
 */
#ifdef __STDC__
extern int mkentry(char *, ino_t, struct arglist *);
extern int expand(char *, int, struct arglist *);
extern ino_t psearch(char *);
extern void metaget(char **data, size_t *size);
extern void metaproc(char *, char *, size_t);
extern long listfile(char *, ino_t, int);
extern long addfile(char *, ino_t, int);
extern long deletefile(char *, ino_t, int);
extern long nodeupdates(char *, ino_t, int);
extern long verifyfile(char *, ino_t, int);
extern void extractdirs(int genmode);
extern void skipdirs(void);
extern void treescan(char *, ino_t, long (*)(char *, ino_t, int));
extern RST_DIR *rst_opendir(char *);
extern void rst_closedir(RST_DIR *);
extern struct direct *rst_readdir(RST_DIR *);
extern void setdirmodes(void);
extern int genliteraldir(char *, ino_t);
extern int inodetype(ino_t);
extern void done(int) __NORETURN;
extern void runcmdshell(void);
extern void canon(char *, char *, size_t);
extern void onintr(int);
extern void removeoldleaves(void);
extern void findunreflinks(void);
extern void removeoldnodes(void);
extern void createleaves(char *);
extern void createfiles(void);
extern void createlinks(void);
extern void checkrestore(void);
extern void setinput(char *, char *);
extern void newtapebuf(size_t);
extern void setup(void);
extern void setupR(void);
extern void getvol(int);
extern void printdumpinfo(void);
extern int extractfile(char *);
extern void skipmaps(void);
extern void skipfile(void);
extern void getfile(void (*)(char *, size_t), void (*)(char *, size_t));
extern void null(char *, size_t);
extern void findtapeblksize(int);
extern void flsht(void);
extern void closemt(int);
extern int readhdr(struct s_spcl *);
extern int gethead(struct s_spcl *);
extern int volnumber(ino_t);
extern void findinode(struct s_spcl *);
extern void pathcheck(char *);
extern void renameit(char *, char *);
extern int linkit(char *, char *, int);
extern int lf_linkit(char *, char *, int);
extern int reply(char *);
/*PRINTFLIKE1*/
extern void panic(const char *, ...);
extern char *lctime(time_t *);
extern int safe_open(int, const char *file, int mode, int perms);
extern FILE *safe_fopen(const char *filename, const char *smode, int perms);
extern void reset_dump(void);
extern void get_next_device(void);
extern void initpagercmd(void);
extern void resolve(char *, int *, char **);
extern int complexcopy(char *, char *, int);
#else	/* !STDC */
extern int mkentry();
extern int expand();
extern ino_t psearch();
extern void metaget();
extern void metaproc();
extern long listfile();
extern long addfile();
extern long deletefile();
extern long nodeupdates();
extern long verifyfile();
extern void extractdirs();
extern void skipdirs();
extern void treescan();
extern RST_DIR *rst_opendir();
extern void rst_closedir();
extern struct direct *rst_readdir();
extern void setdirmodes();
extern int genliteraldir();
extern int inodetype();
extern void done();
extern void runcmdshell();
extern void canon();
extern void onintr();
extern void removeoldleaves();
extern void findunreflinks();
extern void removeoldnodes();
extern void createleaves();
extern void createfiles();
extern void createlinks();
extern void checkrestore();
extern void setinput();
extern void newtapebuf();
extern void setup();
extern void setupR();
extern void getvol();
extern void printdumpinfo();
extern int extractfile();
extern void skipmaps();
extern void skipfile();
extern void getfile();
extern void null();
extern void findtapeblksize();
extern void flsht();
extern void closemt();
extern int readhdr();
extern int gethead();
extern int volnumber();
extern void findinode();
extern void pathcheck();
extern void renameit();
extern int linkit();
extern int lf_linkit();
extern int reply();
extern void panic();
extern char *lctime();
extern int safe_open();
extern FILE *safe_fopen();
extern void reset_dump();
extern void get_next_device();
extern void initpagercmd();
extern void resolve();
extern int complexcopy();
#endif	/* STDC */

/*
 * Useful macros
 */
#define	MWORD(m, i)	((m)[(ino_t)((i)-1)/NBBY])
#define	MBIT(i)		(1<<((ino_t)((i)-1)%NBBY))
#define	BIS(i, w)	(MWORD((w), (i)) |=  MBIT(i))
#define	BIC(i, w)	(MWORD((w), (i)) &= ~MBIT(i))
#define	BIT(i, w)	(MWORD((w), (i)) & MBIT(i))

/*
 * Macro used to get to the last segment of a complex string
 */
#define	LASTPART(s)	{int len = strlen(s)+1;\
				while (s[len] != '\0')\
					{s += len; len = strlen(s)+1; }\
			}

/*
 * Define maximum length of complex string.  For now we use
 * MAXPATHLEN * 2 since recursion is not (yet) supported.
 * (add 3 for the 3 NULL characters in a two-part path)
 * Note that each component of a complex string is still
 * limited to MAXPATHLEN length.
 */
#define	MAXCOMPLEXLEN	(MAXPATHLEN*2 + 3)

/*
 * Define an overflow-free version of howmany so that we don't
 * run into trouble with large files.
 */
#define	d_howmany(x, y)	((x) / (y) + ((x) % (y) != 0))

/*
 * Defines used by findtapeblksize()
 */
#define	TAPE_FILE	0
#define	ARCHIVE_FILE	1

#undef	setjmp
#define	setjmp(b)		sigsetjmp((b), 1)
#define	longjmp			siglongjmp
#define	jmp_buf			sigjmp_buf
#define	chown			lchown

/*
 * Defaults
 */
#define	TAPE	"/dev/rmt/0b"		/* default tape device */
#define	RESTORESYMTABLE	"./restoresymtable"

#define	dprintf		if (dflag) (void) fprintf
#define	vprintf		if (vflag) (void) fprintf

#define	GOOD 1
#define	FAIL 0

#define	ALLOW_OFFLINE	0
#define	FORCE_OFFLINE	1		/* offline drive for autoload */

#define	DEF_PAGER	"/usr/bin/more"

#ifdef	__cplusplus
}
#endif

#endif /* _RESTORE_H */
/*
 * Copyright (c) 1983 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/

/*
 * Copyright (c) 1996,1998,2001 by Sun Microsystems, Inc.
 * All rights reserved.
 */

/*
 * These routines maintain the symbol table which tracks the state
 * of the file system being restored. They provide lookup by either
 * name or inode number. They also provide for creation, deletion,
 * and renaming of entries. Because of the dynamic nature of pathnames,
 * names should not be saved, but always constructed just before they
 * are needed, by calling "myname".
 */

#include "restore.h"
#include <limits.h>

/*
 * The following variables define the inode symbol table.
 * The primary hash table is dynamically allocated based on
 * the number of inodes in the file system (maxino), scaled by
 * HASHFACTOR. The variable "entry" points to the hash table;
 * the variable "entrytblsize" indicates its size (in entries).
 */
#define	HASHFACTOR 5
static struct entry **entry;
static uint_t entrytblsize;

static void addino(ino_t, struct entry *);
static struct entry *lookupparent(char *);
static void removeentry(struct entry *);

/*
 * Look up an entry by inode number
 */
struct entry *
lookupino(ino_t inum)
{
	struct entry *ep;

	if (inum < ROOTINO || inum >= maxino)
		return (NIL);
	for (ep = entry[inum % entrytblsize]; ep != NIL; ep = ep->e_next)
		if (ep->e_ino == inum)
			return (ep);
	return (NIL);
}

/*
 * We now ignore inodes that are out of range.  This
 * allows us to attempt to proceed in the face of
 * a corrupted archive, albeit with future complaints
 * about failed inode lookups.  We only complain once
 * about range problems, to avoid irritating the user
 * without providing any useful information.  Failed
 * lookups have the bogus name, which is useful, so
 * they always happen.
 */
static int complained_about_range = 0;

/*
 * Add an entry into the entry table
 */
static void
addino(ino_t inum, struct entry *np)
{
	struct entry **epp;

	if (inum < ROOTINO || inum >= maxino) {
		if (!complained_about_range) {
			panic(gettext("%s: out of range %d\n"),
			    "addino", inum);
			complained_about_range = 1;
		}
		return;
	}
	epp = &entry[inum % entrytblsize];
	np->e_ino = inum;
	np->e_next = *epp;
	*epp = np;
	if (dflag)
		for (np = np->e_next; np != NIL; np = np->e_next)
			if (np->e_ino == inum)
				badentry(np, gettext("duplicate inum"));
}

/*
 * Delete an entry from the entry table.  We assume our caller
 * arranges for the necessary memory reclamation, if needed.
 */
void
deleteino(ino_t inum)
{
	struct entry *next;
	struct entry **prev;

	if (inum < ROOTINO || inum >= maxino) {
		if (!complained_about_range) {
			panic(gettext("%s: out of range %d\n"),
			    "deleteino", inum);
			complained_about_range = 1;
		}
		return;
	}

	prev = &entry[inum % entrytblsize];
	for (next = *prev; next != NIL; next = next->e_next) {
		if (next->e_ino == inum) {
			next->e_ino = 0;
			*prev = next->e_next;
			return;
		}
		prev = &next->e_next;
	}
}

/*
 * Look up an entry by name.
 *	NOTE: this function handles "complex" pathnames (as returned
 *	by myname()) for extended file attributes.  The name string
 *	provided to this function should be terminated with *two*
 *	NULL characters.
 */
struct entry *
lookupname(char *name)
{
	struct entry *ep;
	char *np, *cp;
	char buf[MAXPATHLEN];

	if (strlen(name) > (sizeof (buf) - 1)) {
		(void) fprintf(stderr, gettext("%s: ignoring too-long name\n"),
		    "lookupname");
		return (NIL);
	}

	cp = name;
	for (ep = lookupino(ROOTINO); ep != NIL; ep = ep->e_entries) {
		np = buf;
		while (*cp != '/' && *cp != '\0')
			*np++ = *cp++;
		*np = '\0';
		for (; ep != NIL; ep = ep->e_sibling)
			if (strcmp(ep->e_name, buf) == 0)
				break;
		if (*cp++ == '\0') {
			if (*cp != '\0') {
				ep = ep->e_xattrs;
				/*
				 * skip over the "./" prefix on all
				 * extended attribute paths
				 */
				cp += 2;
			}
			if (*cp == '\0')
				return (ep);
		}
		if (ep == NIL)
			break;
	}
	return (NIL);
}

/*
 * Look up the parent of a pathname.  This routine accepts complex
 * names so the provided name argument must terminate with two NULLs.
 */
static struct entry *
lookupparent(char *name)
{
	struct entry *ep;
	char *tailindex, savechar, *lastpart;
	int xattrparent = 0;

	/* find the last component of the complex name */
	lastpart = name;
	LASTPART(lastpart);
	tailindex = strrchr(lastpart, '/');
	if (tailindex == 0) {
		if (lastpart == name)
			return (NIL);
		/*
		 * tailindex normaly points to the '/' character
		 * dividing the path, but in the case of an extended
		 * attribute transition it will point to the NULL
		 * separator in front of the attribute path.
		 */
		tailindex = lastpart - 1;
		xattrparent = 1;
	} else {
		*tailindex = '\0';
	}
	savechar = *(tailindex+1);
	*(tailindex+1) = '\0';
	ep = lookupname(name);
	if (ep != NIL && !xattrparent && ep->e_type != NODE)
		panic(gettext("%s is not a directory\n"), name);
	if (!xattrparent) *tailindex = '/';
	*(tailindex+1) = savechar;
	return (ep);
}

/*
 * Determine the current pathname of a node or leaf.
 * The returned pathname will be multiple strings with NULL separators:
 *
 *	./<path>/entry\0<path>/attrentry\0<path>/...\0\0
 *	^	        ^		  ^	    ^
 *   return pntr    entry attr	    recursive attr  terminator
 *
 * Guaranteed to return a name that fits within MAXCOMPLEXLEN and is
 * terminated with two NULLs.
 */
char *
myname(struct entry *ep)
{
	char *cp;
	struct entry *root = lookupino(ROOTINO);
	static char namebuf[MAXCOMPLEXLEN];

	cp = &namebuf[MAXCOMPLEXLEN - 3];
	*(cp + 1) = '\0';
	*(cp + 2) = '\0';
	while (cp > &namebuf[ep->e_namlen]) {
		cp -= ep->e_namlen;
		bcopy(ep->e_name, cp, (size_t)ep->e_namlen);
		if (ep == root)
			return (cp);
		if (ep->e_flags & XATTRROOT)
			*(--cp) = '\0';
		else
			*(--cp) = '/';
		ep = ep->e_parent;
	}
	panic(gettext("%s%s: pathname too long\n"), "...", cp);
	return (cp);
}

/*
 * Unused symbol table entries are linked together on a freelist
 * headed by the following pointer.
 */
static struct entry *freelist = NIL;

/*
 * add an entry to the symbol table
 */
struct entry *
addentry(char *name, ino_t inum, int type)
{
	struct entry *np, *ep;
	char *cp;

	if (freelist != NIL) {
		np = freelist;
		freelist = np->e_next;
		(void) bzero((char *)np, (size_t)sizeof (*np));
	} else {
		np = (struct entry *)calloc(1, sizeof (*np));
		if (np == NIL) {
			(void) fprintf(stderr,
			    gettext("no memory to extend symbol table\n"));
			done(1);
		}
	}
	np->e_type = type & ~(LINK|ROOT);
	if (inattrspace)
		np->e_flags |= XATTR;
	ep = lookupparent(name);
	if (ep == NIL) {
		if (inum != ROOTINO || lookupino(ROOTINO) != NIL) {
			(void) fprintf(stderr, gettext(
			    "%s: bad name %s\n"), "addentry", name);
			assert(0);
			done(1);
		}
		np->e_name = savename(name);
		/* LINTED: savename guarantees that strlen fits in e_namlen */
		np->e_namlen = strlen(name);
		np->e_parent = np;
		addino(ROOTINO, np);
		return (np);
	}

	if (np->e_flags & XATTR) {
		/*
		 * skip to the last part of the complex string: it
		 * containes the extended attribute file name.
		 */
		LASTPART(name);
	}
	cp = strrchr(name, '/');
	if (cp == NULL)
		cp = name;
	else
		cp++;

	np->e_name = savename(cp);
	/* LINTED: savename guarantees that strlen will fit */
	np->e_namlen = strlen(np->e_name);
	np->e_parent = ep;
	/*
	 * Extended attribute root directories must be linked to their
	 * "parents" via the e_xattrs field.  Other entries are simply
	 * added to their parent directories e_entries list.
	 */
	if ((type & ROOT) && (np->e_flags & XATTR)) {
		/* link this extended attribute root dir to its "parent" */
		ep->e_xattrs = np;
	} else {
		/* add this entry to the entry list of the parent dir */
		np->e_sibling = ep->e_entries;
		ep->e_entries = np;
	}
	if (type & LINK) {
		ep = lookupino(inum);
		if (ep == NIL) {
			/* XXX just bail on this one and continue? */
			(void) fprintf(stderr,
			    gettext("link to non-existent name\n"));
			done(1);
		}
		np->e_ino = inum;
		np->e_links = ep->e_links;
		ep->e_links = np;
	} else if (inum != 0) {
		ep = lookupino(inum);
		if (ep != NIL)
			panic(gettext("duplicate entry\n"));
		else
			addino(inum, np);
	}
	return (np);
}

/*
 * delete an entry from the symbol table
 */
void
freeentry(struct entry *ep)
{
	struct entry *np;
	ino_t inum;

	if ((ep->e_flags & REMOVED) == 0)
		badentry(ep, gettext("not marked REMOVED"));
	if (ep->e_type == NODE) {
		if (ep->e_links != NIL)
			badentry(ep, gettext("freeing referenced directory"));
		if (ep->e_entries != NIL)
			badentry(ep, gettext("freeing non-empty directory"));
	}
	if (ep->e_ino != 0) {
		np = lookupino(ep->e_ino);
		if (np == NIL)
			badentry(ep, gettext("lookupino failed"));
		if (np == ep) {
			inum = ep->e_ino;
			deleteino(inum);
			if (ep->e_links != NIL)
				addino(inum, ep->e_links);
		} else {
			for (; np != NIL; np = np->e_links) {
				if (np->e_links == ep) {
					np->e_links = ep->e_links;
					break;
				}
			}
			if (np == NIL)
				badentry(ep, gettext("link not found"));
		}
	}
	removeentry(ep);
	freename(ep->e_name);
	ep->e_next = freelist;
	freelist = ep;
}

/*
 * Relocate an entry in the tree structure
 */
void
moveentry(struct entry *ep, char *newname)
{
	struct entry *np;
	char *cp;

	np = lookupparent(newname);
	if (np == NIL)
		badentry(ep, gettext("cannot move ROOT"));
	if (np != ep->e_parent) {
		removeentry(ep);
		ep->e_parent = np;
		ep->e_sibling = np->e_entries;
		np->e_entries = ep;
	}
	/* find the last component of the complex name */
	LASTPART(newname);
	cp = strrchr(newname, '/') + 1;
	if (cp == (char *)1)
		cp = newname;
	freename(ep->e_name);
	ep->e_name = savename(cp);
	/* LINTED: savename guarantees that strlen will fit */
	ep->e_namlen = strlen(cp);
	if (strcmp(gentempname(ep), ep->e_name) == 0) {
		/* LINTED: result fits in a short */
		ep->e_flags |= TMPNAME;
	} else {
		/* LINTED: result fits in a short */
		ep->e_flags &= ~TMPNAME;
	}
}

/*
 * Remove an entry in the tree structure
 */
static void
removeentry(struct entry *ep)
{
	struct entry *np;

	np = ep->e_parent;
	if (ep->e_flags & XATTRROOT) {
		if (np->e_xattrs == ep)
			np->e_xattrs = NIL;
		else
			badentry(ep, gettext(
			    "parent does not reference this xattr tree"));
	} else if (np->e_entries == ep) {
		np->e_entries = ep->e_sibling;
	} else {
		for (np = np->e_entries; np != NIL; np = np->e_sibling) {
			if (np->e_sibling == ep) {
				np->e_sibling = ep->e_sibling;
				break;
			}
		}
		if (np == NIL)
			badentry(ep, gettext(
			    "cannot find entry in parent list"));
	}
}

/*
 * Table of unused string entries, sorted by length.
 *
 * Entries are allocated in STRTBLINCR sized pieces so that names
 * of similar lengths can use the same entry. The value of STRTBLINCR
 * is chosen so that every entry has at least enough space to hold
 * a "struct strtbl" header. Thus every entry can be linked onto an
 * apprpriate free list.
 *
 * NB. The macro "allocsize" below assumes that "struct strhdr"
 *	has a size that is a power of two. Also, an extra byte is
 *	allocated for the string to provide space for the two NULL
 *	string terminator required for extended attribute paths.
 */
struct strhdr {
	struct strhdr *next;
};

#define	STRTBLINCR	((size_t)sizeof (struct strhdr))
#define	allocsize(size)	(((size) + 2 + STRTBLINCR - 1) & ~(STRTBLINCR - 1))

static struct strhdr strtblhdr[allocsize(MAXCOMPLEXLEN) / STRTBLINCR];

/*
 * Allocate space for a name. It first looks to see if it already
 * has an appropriate sized entry, and if not allocates a new one.
 */
char *
savename(char *name)
{
	struct strhdr *np;
	size_t len, as;
	char *cp;

	if (name == NULL) {
		(void) fprintf(stderr, gettext("bad name\n"));
		done(1);
	}
	len = strlen(name);
	if (len > MAXPATHLEN) {
		(void) fprintf(stderr, gettext("name too long\n"));
		done(1);
	}
	as = allocsize(len);
	np = strtblhdr[as / STRTBLINCR].next;
	if (np != NULL) {
		strtblhdr[as / STRTBLINCR].next = np->next;
		cp = (char *)np;
	} else {
		/* Note that allocsize() adds 2 for the trailing \0s */
		cp = malloc(as);
		if (cp == NULL) {
			(void) fprintf(stderr,
			    gettext("no space for string table\n"));
			done(1);
		}
	}
	(void) strcpy(cp, name);
	/* add an extra null for complex (attribute) name support */
	cp[len+1] = '\0';
	return (cp);
}

/*
 * Free space for a name. The resulting entry is linked onto the
 * appropriate free list.
 */
void
freename(char *name)
{
	struct strhdr *tp, *np;

	/* NULL case should never happen, but might as well be careful */
	if (name != NULL) {
		tp = &strtblhdr[allocsize(strlen(name)) / STRTBLINCR];
		/*LINTED [name points to at least sizeof (struct strhdr)]*/
		np = (struct strhdr *)name;
		np->next = tp->next;
		tp->next = np;
	}
}

/*
 * Useful quantities placed at the end of a dumped symbol table.
 */
struct symtableheader {
	int	volno;
	uint_t	stringsize;
	uint_t	entrytblsize;
	time_t	dumptime;
	time_t	dumpdate;
	ino_t	maxino;
	uint_t	ntrec;
};

/*
 * dump a snapshot of the symbol table
 */
void
dumpsymtable(char *filename, int checkpt)
{
	struct entry *ep, *tep;
	ino_t i;
	struct entry temp, *tentry;
	int mynum = 1;
	uint_t stroff;
	FILE *fp;
	struct symtableheader hdr;

	vprintf(stdout, gettext("Check pointing the restore\n"));
	if ((fp = safe_fopen(filename, "w", 0600)) == (FILE *)NULL) {
		perror("fopen");
		(void) fprintf(stderr,
		    gettext("cannot create save file %s for symbol table\n"),
		    filename);
		done(1);
	}
	clearerr(fp);
	/*
	 * Assign an index to each entry
	 * Write out the string entries
	 */
	for (i = ROOTINO; i < maxino; i++) {
		for (ep = lookupino(i); ep != NIL; ep = ep->e_links) {
			ep->e_index = mynum++;
			(void) fwrite(ep->e_name, sizeof (ep->e_name[0]),
			    (size_t)allocsize(ep->e_namlen), fp);
		}
	}
	/*
	 * Convert e_name pointers to offsets, other pointers
	 * to indices, and output
	 */
	tep = &temp;
	stroff = 0;
	for (i = ROOTINO; !ferror(fp) && i < maxino; i++) {
		for (ep = lookupino(i);
		    !ferror(fp) && ep != NIL;
		    ep = ep->e_links) {
			bcopy((char *)ep, (char *)tep, sizeof (*tep));
			/* LINTED: type pun ok */
			tep->e_name = (char *)stroff;
			stroff += allocsize(ep->e_namlen);
			tep->e_parent = (struct entry *)ep->e_parent->e_index;
			if (ep->e_links != NIL)
				tep->e_links =
				    (struct entry *)ep->e_links->e_index;
			if (ep->e_sibling != NIL)
				tep->e_sibling =
				    (struct entry *)ep->e_sibling->e_index;
			if (ep->e_entries != NIL)
				tep->e_entries =
				    (struct entry *)ep->e_entries->e_index;
			if (ep->e_xattrs != NIL)
				tep->e_xattrs =
				    (struct entry *)ep->e_xattrs->e_index;
			if (ep->e_next != NIL)
				tep->e_next =
				    (struct entry *)ep->e_next->e_index;
			(void) fwrite((char *)tep, sizeof (*tep), 1, fp);
		}
	}
	/*
	 * Convert entry pointers to indices, and output
	 */
	for (i = 0; !ferror(fp) && i < (ino_t)entrytblsize; i++) {
		if (entry[i] == NIL)
			tentry = NIL;
		else
			tentry = (struct entry *)entry[i]->e_index;
		(void) fwrite((char *)&tentry, sizeof (tentry), 1, fp);
	}

	if (!ferror(fp)) {
		/* Ought to have a checksum or magic number */
		hdr.volno = checkpt;
		hdr.maxino = maxino;
		hdr.entrytblsize = entrytblsize;
		hdr.stringsize = stroff;
		hdr.dumptime = dumptime;
		hdr.dumpdate = dumpdate;
		hdr.ntrec = ntrec;
		(void) fwrite((char *)&hdr, sizeof (hdr), 1, fp);
	}

	if (ferror(fp)) {
		perror("fwrite");
		panic(gettext("output error to file %s writing symbol table\n"),
		    filename);
	}
	(void) fclose(fp);
}

/*
 * Initialize a symbol table from a file
 */
void
initsymtable(char *filename)
{
	char *base;
	off64_t tblsize;
	struct entry *ep;
	struct entry *baseep, *lep;
	struct symtableheader hdr;
	struct stat64 stbuf;
	uint_t i;
	int fd;

	vprintf(stdout, gettext("Initialize symbol table.\n"));
	if (filename == NULL) {
		if ((maxino / HASHFACTOR) > UINT_MAX) {
			(void) fprintf(stderr,
			    gettext("file system too large\n"));
			done(1);
		}
		/* LINTED: result fits in entrytblsize */
		entrytblsize = maxino / HASHFACTOR;
		entry = calloc((size_t)entrytblsize, sizeof (*entry));
		if (entry == NULL) {
			(void) fprintf(stderr,
			    gettext("no memory for entry table\n"));
			done(1);
		}
		ep = calloc(1, sizeof (*ep));
		if (ep == NULL) {
			(void) fprintf(stderr,
			    gettext("no memory for entry\n"));
			done(1);
		}
		ep->e_type = NODE;
		ep->e_name = savename(".");
		ep->e_namlen = 1;
		ep->e_parent = ep;
		addino(ROOTINO, ep);
		/* LINTED: result fits in a short */
		ep->e_flags |= NEW;
		return;
	}
	if ((fd = open(filename, O_RDONLY|O_LARGEFILE)) < 0) {
		perror("open");
		(void) fprintf(stderr,
		    gettext("cannot open symbol table file %s\n"), filename);
		done(1);
	}
	if (fstat64(fd, &stbuf) < 0) {
		perror("stat");
		(void) fprintf(stderr,
		    gettext("cannot stat symbol table file %s\n"), filename);
		(void) close(fd);
		done(1);
	}
	/*
	 * The symbol table file is too small so say we can't read it.
	 */
	if (stbuf.st_size < sizeof (hdr)) {
		(void) fprintf(stderr,
		    gettext("cannot read symbol table file %s\n"), filename);
		(void) close(fd);
		done(1);
	}
	tblsize = stbuf.st_size - sizeof (hdr);
	if (tblsize > ULONG_MAX) {
		(void) fprintf(stderr,
		    gettext("symbol table file too large\n"));
		(void) close(fd);
		done(1);
	}
	/* LINTED tblsize fits in a size_t */
	base = calloc((size_t)sizeof (char), (size_t)tblsize);
	if (base == NULL) {
		(void) fprintf(stderr,
		    gettext("cannot allocate space for symbol table\n"));
		(void) close(fd);
		done(1);
	}
	/* LINTED tblsize fits in a size_t */
	if (read(fd, base, (size_t)tblsize) < 0 ||
	    read(fd, (char *)&hdr, sizeof (hdr)) < 0) {
		perror("read");
		(void) fprintf(stderr,
		    gettext("cannot read symbol table file %s\n"), filename);
		(void) close(fd);
		done(1);
	}
	(void) close(fd);
	switch (command) {
	case 'r':
	case 'M':
		/*
		 * For normal continuation, insure that we are using
		 * the next incremental tape
		 */
		if (hdr.dumpdate != dumptime) {
			if (hdr.dumpdate < dumptime)
				(void) fprintf(stderr, gettext(
				    "Incremental volume too low\n"));
			else
				(void) fprintf(stderr, gettext(
				    "Incremental volume too high\n"));
			done(1);
		}
		break;
	case 'R':
		/*
		 * For restart, insure that we are using the same tape
		 */
		curfile.action = SKIP;
		dumptime = hdr.dumptime;
		dumpdate = hdr.dumpdate;
		if (!bflag)
			newtapebuf(hdr.ntrec);
		getvol(hdr.volno);
		break;
	default:
		(void) fprintf(stderr,
		    gettext("initsymtable called from command %c\n"),
		    (uchar_t)command);
		done(1);
		/*NOTREACHED*/
	}
	maxino = hdr.maxino;
	entrytblsize = hdr.entrytblsize;
	/*LINTED [pointer cast alignment]*/
	entry = (struct entry **)
	    (base + tblsize - (entrytblsize * sizeof (*entry)));
	if (((ulong_t)entry % 4) != 0) {
		(void) fprintf(stderr,
		    gettext("Symbol table file corrupted\n"));
		done(1);
	}
	/*LINTED [rvalue % 4 == 0] */
	baseep = (struct entry *)
	    (base + hdr.stringsize - sizeof (*baseep));
	if (((ulong_t)baseep % 4) != 0) {
		(void) fprintf(stderr,
		    gettext("Symbol table file corrupted\n"));
		done(1);
	}
	lep = (struct entry *)entry;
	for (i = 0; i < entrytblsize; i++) {
		if (entry[i] == NIL)
			continue;
		entry[i] = &baseep[(long)entry[i]];
	}
	for (ep = &baseep[1]; ep < lep; ep++) {
		ep->e_name = base + (long)ep->e_name;
		ep->e_parent = &baseep[(long)ep->e_parent];
		if (ep->e_sibling != NIL)
			ep->e_sibling = &baseep[(long)ep->e_sibling];
		if (ep->e_links != NIL)
			ep->e_links = &baseep[(long)ep->e_links];
		if (ep->e_entries != NIL)
			ep->e_entries = &baseep[(long)ep->e_entries];
		if (ep->e_xattrs != NIL)
			ep->e_xattrs = &baseep[(long)ep->e_xattrs];
		if (ep->e_next != NIL)
			ep->e_next = &baseep[(long)ep->e_next];
	}
}
/*
 * Copyright (c) 1983 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/

/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <setjmp.h>
#include "restore.h"
#include <byteorder.h>
#include <rmt.h>
#include <sys/mtio.h>
#include <utime.h>
#include <sys/errno.h>
#include <sys/fdio.h>
#include <sys/sysmacros.h>	/* for expdev */
#include <assert.h>
#include <limits.h>
#include <priv_utils.h>
#include <aclutils.h>

#define	MAXINO	65535		/* KLUDGE */

#define	MAXTAPES	128

static size_t	fssize = MAXBSIZE; /* preferred size of writes to filesystem */
int mt = -1;
static int	continuemap = 0;
char		magtape[BUFSIZ];
int		pipein = 0;
char		*host;		/* used in dumprmt.c */
daddr32_t	rec_position;
static char	*archivefile;	/* used in metamucil.c */
static int	bct;		/* block # index into tape record buffer */
static int	numtrec;	/* # of logical blocks in current tape record */
static char	*tbf = NULL;
static size_t	tbfsize = 0;
static int	recsread;
union		u_spcl u_spcl;
static union	u_spcl endoftapemark;
static struct	s_spcl dumpinfo;
static long	blksread;	/* # of logical blocks actually read/touched */
static long	tapea;		/* current logical block # on tape */
static uchar_t	tapesread[MAXTAPES];
static jmp_buf	restart;
static int	gettingfile = 0;	/* restart has a valid frame */
static int	ofile;
static char	*map, *beginmap;
static char	*endmap;
static char	lnkbuf[MAXPATHLEN + 2];
static int	pathlen;
static int	inodeinfo;	/* Have starting volume information */
static int	hostinfo;	/* Have dump host information */

static int autoload_tape(void);
static void setdumpnum(void);
static void metacheck(struct s_spcl *);
static void xtrmeta(char *, size_t);
static void metaskip(char *, size_t);
static void xtrfile(char *, size_t);
static void xtrskip(char *, size_t);
static void xtrlnkfile(char *, size_t);
static void xtrlnkskip(char *, size_t);
static void xtrmap(char *, size_t);
static void xtrmapskip(char *, size_t);
static void readtape(char *);
static int checkvol(struct s_spcl *, int);
static void accthdr(struct s_spcl *);
static int ishead(struct s_spcl *);
static int checktype(struct s_spcl *, int);
static void metaset(char *name);

/*
 * Set up an input source
 */
void
setinput(char *source, char *archive)
{

	flsht();
	archivefile = archive;
	if (bflag == 0) {
		ntrec = ((CARTRIDGETREC > HIGHDENSITYTREC) ?
		    (NTREC > CARTRIDGETREC ? NTREC : CARTRIDGETREC) :
		    (NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC));
		saved_ntrec = (ntrec * (tp_bsize/DEV_BSIZE));
	}
	newtapebuf(ntrec);
	terminal = stdin;

	if (source == NULL) {
		/* A can't-happen */
		(void) fprintf(stderr,
		    gettext("Internal consistency check failed.\n"));
		done(1);
	}

	if (strchr(source, ':')) {
		char *tape;

		host = source;
		tape = strchr(host, ':');
		*tape++ = '\0';
		if (strlen(tape) > (sizeof (magtape) - 1)) {
			(void) fprintf(stderr, gettext("Tape name too long\n"));
			done(1);
		}
		(void) strcpy(magtape, tape);
		if (rmthost(host, ntrec) == 0)
			done(1);
	} else {
		if (strlen(source) > (sizeof (magtape) - 1)) {
			(void) fprintf(stderr, gettext("Tape name too long\n"));
			done(1);
		}
		/* Not remote, no need for privileges */
		__priv_relinquish();
		host = NULL;
		if (strcmp(source, "-") == 0) {
			/*
			 * Since input is coming from a pipe we must establish
			 * our own connection to the terminal.
			 */
			terminal = fopen("/dev/tty", "r");
			if (terminal == NULL) {
				int saverr = errno;
				char *msg =
				    gettext("Cannot open(\"/dev/tty\")");
				errno = saverr;
				perror(msg);
				terminal = fopen("/dev/null", "r");
				if (terminal == NULL) {
					saverr = errno;
					msg = gettext(
					    "Cannot open(\"/dev/null\")");
					errno = saverr;
					perror(msg);
					done(1);
				}
			}
			pipein++;
			if (archive) {
				(void) fprintf(stderr, gettext(
	    "Cannot specify an archive file when reading from a pipe\n"));
				done(1);
			}
		}
		(void) strcpy(magtape, source);
	}
}

void
newtapebuf(size_t size)
{
	size_t nsize;

	nsize = size * tp_bsize;
	ntrec = size;
	if (nsize <= tbfsize)
		return;
	if (tbf != NULL)
		free(tbf);
	tbf = (char *)malloc(nsize);
	if (tbf == NULL) {
		(void) fprintf(stderr,
		    gettext("Cannot allocate space for buffer\n"));
		done(1);
	}
	tbfsize = nsize;
}

/*
 * Verify that the tape drive can be accessed and
 * that it actually is a dump tape.
 */
void
setup(void)
{
	int i, j;
	int32_t *ip;
	struct stat stbuf;
	size_t mapsize;
	char *syment = RESTORESYMTABLE;

	vprintf(stdout, gettext("Verify volume and initialize maps\n"));
	if (archivefile) {
		mt = open(archivefile, O_RDONLY|O_LARGEFILE);
		if (mt < 0) {
			perror(archivefile);
			done(1);
		}
		volno = 0;
	} else if (host) {
		if ((mt = rmtopen(magtape, O_RDONLY)) < 0) {
			perror(magtape);
			done(1);
		}
		volno = 1;
	} else {
		if (pipein)
			mt = 0;
		else if ((mt = open(magtape, O_RDONLY|O_LARGEFILE)) < 0) {
			perror(magtape);
			done(1);
		}
		volno = 1;
	}
	setdumpnum();
	flsht();
	if (!pipein && !bflag)
		if (archivefile)
			findtapeblksize(ARCHIVE_FILE);
		else
			findtapeblksize(TAPE_FILE);
	if (bflag == 1) {
		tape_rec_size = saved_ntrec * DEV_BSIZE;
	}

	/*
	 * Get the first header.  If c_magic is NOT NFS_MAGIC or if
	 * the checksum is in error, it will fail.  The magic could then
	 * be either OFS_MAGIC or MTB_MAGIC.  If OFS_MAGIC, assume we
	 * have an old dump, and try to convert it.  If it is MTB_MAGIC, we
	 * procees this after.
	 */
	if ((gethead(&spcl) == FAIL) && (spcl.c_magic != MTB_MAGIC)) {
		bct--; /* push back this block */
		blksread--;
		tapea--;
		cvtflag++;
		if (gethead(&spcl) == FAIL) {
			(void) fprintf(stderr,
			    gettext("Volume is not in dump format\n"));
			done(1);
		}
		(void) fprintf(stderr,
		    gettext("Converting to new file system format.\n"));
	}
	/*
	 * The above gethead will have failed if the magic is
	 * MTB_MAGIC. If that is true, we need to adjust tp_bsize.
	 * We have assumed to this time that tp_bsize was 1024, if
	 * this is a newer dump, get the real tp_bsize from the header,
	 * and recalculate ntrec, numtrec.
	 */
	if (spcl.c_magic == MTB_MAGIC) {
		tp_bsize = spcl.c_tpbsize;
		if ((tp_bsize % TP_BSIZE_MIN != 0) ||
		    (tp_bsize > TP_BSIZE_MAX)) {
			(void) fprintf(stderr,
			    gettext("Volume is not in dump format\n"));
			done(1);
		}
		ntrec = (tape_rec_size/tp_bsize);
		numtrec = ntrec;
		newtapebuf(ntrec);
		bct--; /* push back this block */
		blksread--;
		tapea--;
		/* we have to re-do this in case checksum is wrong */
		if (gethead(&spcl) == FAIL) {
			(void) fprintf(stderr,
			    gettext("Volume is not in dump format\n"));
			done(1);
		}
	}
	if (vflag)
		byteorder_banner(byteorder, stdout);
	if (pipein) {
		endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC :
		    ((tp_bsize == TP_BSIZE_MIN) ? NFS_MAGIC : MTB_MAGIC);
		endoftapemark.s_spcl.c_type = TS_END;

		/*
		 * include this since the `resync' loop in findinode
		 * expects to find a header with the c_date field
		 * filled in.
		 */
		endoftapemark.s_spcl.c_date = spcl.c_date;

		ip = (int32_t *)&endoftapemark;
		/*LINTED [assertion always true]*/
		assert((sizeof (endoftapemark) % sizeof (int32_t)) == 0);
		j = sizeof (endoftapemark) / sizeof (int32_t);
		i = 0;
		do
			i += *ip++;
		while (--j)
			;
		endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
	}
	if (vflag && command != 't')
		printdumpinfo();
	dumptime = spcl.c_ddate;
	dumpdate = spcl.c_date;
	if (stat(".", &stbuf) < 0) {
		perror(gettext("cannot stat ."));
		done(1);
	}
	if (stbuf.st_blksize >= tp_bsize && stbuf.st_blksize <= MAXBSIZE) {
		/* LINTED: value fits in a size_t */
		fssize = stbuf.st_blksize;
	} else {
		fssize = MAXBSIZE;
	}

	if (checkvol(&spcl, 1) == FAIL) {
		(void) fprintf(stderr,
		    gettext("This is not volume 1 of the dump\n"));
		done(1);
	}
	if (readhdr(&spcl) == FAIL)
		panic(gettext("no header after volume mark!\n"));

	findinode(&spcl);	/* sets curfile, resyncs the tape if need be */
	if (checktype(&spcl, TS_CLRI) == FAIL) {
		(void) fprintf(stderr,
		    gettext("Cannot find file removal list\n"));
		done(1);
	}
	maxino = (unsigned)((spcl.c_count * tp_bsize * NBBY) + 1);
	dprintf(stdout, "maxino = %lu\n", maxino);
	/*
	 * Allocate space for at least MAXINO inodes to allow us
	 * to restore partial dump tapes written before dump was
	 * fixed to write out the entire inode map.
	 */
	if (maxino > ULONG_MAX) {
		(void) fprintf(stderr,
		    gettext("file system too large\n"));
		done(1);
	}
	/* LINTED maxino size-checked above */
	mapsize = (size_t)d_howmany(maxino > MAXINO ? maxino : MAXINO, NBBY);
	beginmap = map = calloc((size_t)1, mapsize);
	if (map == (char *)NIL) {
		(void) fprintf(stderr,
		    gettext("no memory for file removal list\n"));
		done(1);
	}
	endmap = map + mapsize;
	clrimap = map;
	curfile.action = USING;
	continuemap = 1;
	getfile(xtrmap, xtrmapskip);
	if (MAXINO > maxino)
		maxino = MAXINO;
	if (checktype(&spcl, TS_BITS) == FAIL) {
		/* if we have TS_CLRI then no TS_BITS then a TS_END */
		/* then we have an empty dump file */
		if (gethead(&spcl) == GOOD &&
		    checktype(&spcl, TS_END) == GOOD) {
			if ((command == 'r') || (command == 'R')) {
				initsymtable(syment);
				dumpsymtable(syment, volno);
			}
			done(0);
		}
		/* otherwise we have an error */
		(void) fprintf(stderr, gettext("Cannot find file dump list\n"));
		done(1);
	}
	/* LINTED maxino size-checked above */
	mapsize = (size_t)d_howmany(maxino, NBBY);
	beginmap = map = calloc((size_t)1, mapsize);
	if (map == (char *)NULL) {
		(void) fprintf(stderr,
		    gettext("no memory for file dump list\n"));
		done(1);
	}
	endmap = map + mapsize;
	dumpmap = map;
	curfile.action = USING;
	continuemap = 1;
	getfile(xtrmap, xtrmapskip);
	continuemap = 0;
}

/*
 * Initialize fssize variable for 'R' command to work.
 */
void
setupR(void)
{
	struct stat stbuf;

	if (stat(".", &stbuf) < 0) {
		perror(gettext("cannot stat ."));
		done(1);
	}
	if (stbuf.st_blksize >= tp_bsize && stbuf.st_blksize <= MAXBSIZE) {
		/* LINTED: value fits in a size_t */
		fssize = stbuf.st_blksize;
	} else {
		fssize = MAXBSIZE;
	}
}

/*
 * Prompt user to load a new dump volume.
 * "Nextvol" is the next suggested volume to use.
 * This suggested volume is enforced when doing full
 * or incremental restores, but can be overrridden by
 * the user when only extracting a subset of the files.
 *
 * first_time is used with archive files and can have 1 of 3 states:
 *	FT_STATE_1	Tape has not been read yet
 *	FT_STATE_2	Tape has been read but not positioned past directory
 *			information
 *	FT_STATE_3	Tape has been read and is reading file information
 */
#define	FT_STATE_1	1
#define	FT_STATE_2	2
#define	FT_STATE_3	3

void
getvol(int nextvol)
{
	int newvol;
	long savecnt, savetapea, wantnext;
	long i;
	union u_spcl tmpspcl;
#define	tmpbuf tmpspcl.s_spcl
	char buf[TP_BSIZE_MAX];
	static int first_time = FT_STATE_1;

	if (tbf == NULL) {
		(void) fprintf(stderr, gettext(
		    "Internal consistency failure in getvol: tbf is NULL\n"));
		done(1);
	}

	if (nextvol == 1) {
		for (i = 0;  i < MAXTAPES;  i++)
			tapesread[i] = 0;
		gettingfile = 0;
	}
	if (pipein) {
		if (nextvol != 1)
			panic(gettext("changing volumes on pipe input\n"));
		if (volno == 1)
			return;
		goto gethdr;
	}
	savecnt = blksread;	/* ignore volume verification tape i/o */
	savetapea = tapea;
again:
	if (pipein)
		done(1); /* pipes do not get a second chance */
	if (command == 'R' || command == 'r' || curfile.action != SKIP) {
		wantnext = 1;
		newvol = nextvol;
	} else {
		wantnext = 0;
		newvol = 0;
	}

	if (autoload) {
		if ((volno == 1) && (nextvol == 1)) {
			tapesread[volno-1]++;
			return;
		}
		if (autoload_tape()) {
			wantnext = 1;
			newvol = nextvol;
			goto gethdr;
		}
	}

	while (newvol <= 0) {
		int n = 0;

		for (i = 0;  i < MAXTAPES;  i++)
			if (tapesread[i])
				n++;
		if (n == 0) {
			(void) fprintf(stderr, "%s", gettext(
"You have not read any volumes yet.\n\
Unless you know which volume your file(s) are on you should start\n\
with the last volume and work towards the first.\n"));
		} else {
			(void) fprintf(stderr,
			    gettext("You have read volumes"));
			(void) strcpy(tbf, ": ");
			for (i = 0; i < MAXTAPES; i++)
				if (tapesread[i]) {
					(void) fprintf(stderr, "%s%ld",
					    tbf, i+1);
					(void) strcpy(tbf, ", ");
				}
			(void) fprintf(stderr, "\n");
		}
		do {
			(void) fprintf(stderr,
			    gettext("Specify next volume #: "));
			(void) fflush(stderr);
			/* LINTED tbfsize is limited to a few MB */
			(void) fgets(tbf, (int)tbfsize, terminal);
		} while (!feof(terminal) && tbf[0] == '\n');
		if (feof(terminal))
			done(1);
		newvol = atoi(tbf);
		if (newvol <= 0) {
			(void) fprintf(stderr, gettext(
			    "Volume numbers are positive numerics\n"));
		}
		if (newvol > MAXTAPES) {
			(void) fprintf(stderr, gettext(
			    "This program can only deal with %d volumes\n"),
			    MAXTAPES);
			newvol = 0;
		}
	}
	if (newvol == volno) {
		tapesread[volno-1]++;
		return;
	}
	closemt(ALLOW_OFFLINE);
	/*
	 * XXX: if we are switching devices, we should probably try
	 * the device once without prompting to enable unattended
	 * operation.
	 */
	if (host)
		(void) fprintf(stderr, gettext(
"Mount volume %d\nthen enter volume name on host %s (default: %s) "),
		    newvol, host,  magtape);
	else
		(void) fprintf(stderr, gettext(
		    "Mount volume %d\nthen enter volume name (default: %s) "),
		    newvol, magtape);
	(void) fflush(stderr);
	/* LINTED tbfsize is limited to a few MB */
	(void) fgets(tbf, (int)tbfsize, terminal);
	if (feof(terminal))
		done(1);
	/*
	 * XXX We don't allow rotating among tape hosts, just drives.
	 */
	if (tbf[0] != '\n') {
		(void) strncpy(magtape, tbf, sizeof (magtape));
		magtape[sizeof (magtape) - 1] = '\0';
		/* LINTED unsigned -> signed conversion ok */
		i = (int)strlen(magtape);
		if (magtape[i - 1] == '\n')
			magtape[i - 1] = '\0';
	}
	if ((host != NULL && (mt = rmtopen(magtape, O_RDONLY)) == -1) ||
	    (host == NULL &&
	    (mt = open(magtape, O_RDONLY|O_LARGEFILE)) == -1)) {
		int error = errno;
		(void) fprintf(stderr, gettext("Cannot open %s: %s\n"),
		    magtape, strerror(error));
		volno = -1;
		goto again;
	}
gethdr:
	volno = newvol;
	setdumpnum();
	flsht();
	if (!pipein && !bflag && archivefile && (first_time == FT_STATE_1)) {
		first_time = FT_STATE_2;
		findtapeblksize(TAPE_FILE);
	}
	if (readhdr(&tmpbuf) == FAIL) {
		(void) fprintf(stderr,
		    gettext("volume is not in dump format\n"));
		volno = 0;
		goto again;
	}
	if (checkvol(&tmpbuf, volno) == FAIL) {
		(void) fprintf(stderr, gettext("Wrong volume (%d)\n"),
		    tmpbuf.c_volume);
		volno = 0;
		goto again;
	}

	if (((time_t)(tmpbuf.c_date) != dumpdate) ||
	    ((time_t)(tmpbuf.c_ddate) != dumptime)) {
		char *tmp_ct;
		time_t lc_date = (time_t)tmpbuf.c_date;

		/*
		 * This is used to save the return value from lctime(),
		 * since that's volatile across lctime() invocations.
		 */
		tmp_ct = strdup(lctime(&lc_date));
		if (tmp_ct == (char *)0) {
			(void) fprintf(stderr, gettext(
			    "Cannot allocate space for time string\n"));
			done(1);
		}

		(void) fprintf(stderr,
		    gettext("Wrong dump date\n\tgot: %s\twanted: %s"),
		    tmp_ct,  lctime(&dumpdate));
		volno = 0;
		free(tmp_ct);
		goto again;
	}
	tapesread[volno-1]++;
	blksread = savecnt;
	tapea = savetapea;
	/*
	 * If continuing from the previous volume, skip over any
	 * blocks read already at the end of the previous volume.
	 *
	 * If coming to this volume at random, skip to the beginning
	 * of the next record.
	 */
	if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
		if (!wantnext) {
			if (archivefile && first_time == FT_STATE_2) {
				first_time = FT_STATE_3;
			}
			recsread = tmpbuf.c_firstrec;
			tapea = tmpbuf.c_tapea;
			dprintf(stdout,
			    "restore skipping %d records\n",
			    tmpbuf.c_count);
			for (i = tmpbuf.c_count; i > 0; i--)
				readtape(buf);
		} else if (tmpbuf.c_firstrec != 0) {
			savecnt = blksread;
			savetapea = tapea;

			if (archivefile && first_time == FT_STATE_2) {
				/*
				 * subtract 2, 1 for archive file's TS_END
				 * and 1 for tape's TS_TAPE
				 */
				first_time = FT_STATE_3;
				i = tapea - tmpbuf.c_tapea - 2;
			} else {
				i = tapea - tmpbuf.c_tapea;
			}
			if (i > 0)
				dprintf(stdout, gettext(
				    "restore skipping %d duplicate records\n"),
				    i);
			else if (i < 0)
				dprintf(stdout, gettext(
				    "restore duplicate record botch (%d)\n"),
				    i);
			while (--i >= 0)
				readtape(buf);
			blksread = savecnt;
			tapea = savetapea + 1; /* <= (void) gethead() below */
		}
	}
	if (curfile.action == USING) {
		if (volno == 1)
			panic(gettext("active file into volume 1\n"));
		return;
	}
	(void) gethead(&spcl);
	findinode(&spcl); /* do we always restart files in full? */
	if (gettingfile) { /* i.e. will we lose metadata? */
		gettingfile = 0;
		longjmp(restart, 1); /* will this set f1 & f2? */
	}
}

/*
 * handle multiple dumps per tape by skipping forward to the
 * appropriate one.  Note we don't use absolute positioning,
 * as that may take a very long time.
 */
static void
setdumpnum(void)
{
	struct mtop tcom;
	int retval;

	if (dumpnum == 1 || volno != 1)
		return;
	if (pipein) {
		(void) fprintf(stderr,
		    gettext("Cannot have multiple dumps on pipe input\n"));
		done(1);
	}
	tcom.mt_op = MTFSF;
	tcom.mt_count = dumpnum - 1;
	if (host)
		retval = rmtioctl(MTFSF, dumpnum - 1);
	else
		retval = ioctl(mt, (int)MTIOCTOP, (char *)&tcom);
	if (retval < 0)
		perror("ioctl MTFSF");
}

void
printdumpinfo(void)
{
	int i;
	time_t date;
	static char *epoch = NULL;

	if (epoch == NULL) {
		epoch = strdup(gettext("the epoch\n"));
		if (epoch == NULL) {
			(void) fprintf(stderr, gettext("Out of memory\n"));
			return;
		}
	}

	date = (time_t)dumpinfo.c_date;
	(void) fprintf(stdout,
	    gettext("Dump   date: %s"), lctime(&date));

	date = (time_t)dumpinfo.c_ddate;
	(void) fprintf(stdout, gettext("Dumped from: %s"),
	    (dumpinfo.c_ddate == 0) ? epoch : lctime(&date));
	if (hostinfo) {
		(void) fprintf(stdout,
		    gettext("Level %d dump of %s on %.*s:%s\n"),
		    dumpinfo.c_level, dumpinfo.c_filesys,
		    sizeof (dumpinfo.c_host), dumpinfo.c_host, dumpinfo.c_dev);
		(void) fprintf(stdout,
		    gettext("Label: %.*s\n"),
		    sizeof (dumpinfo.c_label), dumpinfo.c_label);
	}
	if (inodeinfo) {
		(void) fprintf(stdout,
		    gettext("Starting inode numbers by volume:\n"));
		for (i = 1; i <= dumpinfo.c_volume; i++)
			(void) fprintf(stdout, gettext("\tVolume %d: %6d\n"),
			    i, dumpinfo.c_inos[i]);
	}
}

int
extractfile(char *name)
{
	static int complained_chown = 0;
	static int complained_lchown = 0;
	static int complained_chmod = 0;
	static int complained_utime = 0;
	static int complained_mknod = 0;
	mode_t mode;
	time_t timep[2];
	struct entry *ep;
	uid_t uid;
	gid_t gid;
	char *errmsg;
	int result, saverr;
	dev_t full_dev;
	int dfd;
	char *rname;

	curfile.name = name;
	curfile.action = USING;
	timep[0] = (time_t)curfile.dip->di_atime;
	timep[1] = (time_t)curfile.dip->di_mtime;
	mode = curfile.dip->di_mode;

	uid = curfile.dip->di_suid == UID_LONG ?
	    curfile.dip->di_uid : (uid_t)curfile.dip->di_suid;
	gid = curfile.dip->di_sgid == GID_LONG ?
	    curfile.dip->di_gid : (gid_t)curfile.dip->di_sgid;

	resolve(name, &dfd, &rname);
	if (dfd != AT_FDCWD) {
		if (fchdir(dfd) < 0) {
			saverr = errno;
			(void) fprintf(stderr, gettext(
			    "%s: unable to set attribute context: %s\n"),
			    rname, strerror(saverr));
			skipfile();
			(void) close(dfd);
			return (FAIL);
		}
	}

	switch (mode & IFMT) {

	default:
		(void) fprintf(stderr, gettext("%s: unknown file mode 0%lo\n"),
		    rname, (ulong_t)(mode&IFMT));
		skipfile();
		result = FAIL;
		break;

	case IFSOCK:
		vprintf(stdout, gettext("skipped socket %s\n"), rname);
		skipfile();
		result = GOOD;
		break;

	case IFDIR:
		if (mflag) {
			ep = lookupname(name);
			if (ep == NIL || ep->e_flags & EXTRACT) {
				panic(gettext(
				    "directory %s was not restored\n"),
				    rname);
				skipfile();
				result = FAIL;
				break;
			}
			skipfile();
			result = GOOD;
			break;
		}
		vprintf(stdout, gettext("extract file %s\n"), rname);
		result = genliteraldir(rname, curfile.ino);
		break;

	case IFLNK:
		lnkbuf[0] = '\0';
		pathlen = 0;
		getfile(xtrlnkfile, xtrlnkskip);
		if (pathlen == 0) {
			vprintf(stdout, gettext(
			    "%s: zero length symbolic link (ignored)\n"),
			    rname);
			result = GOOD;
			break;
		}
		if ((result = lf_linkit(lnkbuf, rname, SYMLINK)) != GOOD)
			break;

		/* 1254700: set uid/gid (previously missing)  */
		if (lchown(rname, uid, gid) < 0 && !complained_lchown) {
			/* Just a warning */
			saverr = errno;
			errmsg = gettext(
			    "Unable to restore ownership of symlink %s: %s\n");
			(void) fprintf(stderr, errmsg,
			    rname, strerror(saverr));
			(void) fprintf(stderr, gettext(
			    "Additional such failures will be ignored.\n"));
			complained_lchown = 1;
		}
		metaset(rname);
		result = GOOD;
		break;

	case IFCHR:
	case IFBLK:
	case IFIFO:
		vprintf(stdout, gettext("extract special file %s\n"), rname);
		/* put device rdev into dev_t expanded format */
		/* XXX does this always do the right thing? */
		/* XXX does dump do the right thing? */
		if (((curfile.dip->di_ordev & 0xFFFF0000) == 0) ||
		    ((curfile.dip->di_ordev & 0xFFFF0000) == 0xFFFF0000)) {
			full_dev = expdev((unsigned)(curfile.dip->di_ordev));
		} else {
			/* LINTED sign extension ok */
			full_dev = (unsigned)(curfile.dip->di_ordev);
		}

		if (mknod(rname, mode, full_dev) < 0) {
			struct stat64 s[1];

			saverr = errno;
			if ((stat64(rname, s)) ||
			    ((s->st_mode & S_IFMT) != (mode & S_IFMT)) ||
			    (s->st_rdev != full_dev)) {
				if (saverr != EPERM || !complained_mknod) {
					(void) fprintf(stderr, "%s: ", rname);
					(void) fflush(stderr);
					errno = saverr;
					perror(gettext(
					    "cannot create special file"));
					if (saverr == EPERM) {
						(void) fprintf(stderr, gettext(
			    "Additional such failures will be ignored.\n"));
						complained_mknod = 1;
					}
				}
				skipfile();
				result = FAIL;
				break;
			}
		}
		if (chown(rname, uid, gid) < 0 && !complained_chown) {
			/* Just a warning */
			saverr = errno;
			errmsg = gettext(
			    "Unable to restore ownership of %s: %s\n");
			(void) fprintf(stderr, errmsg,
			    rname, strerror(saverr));
			(void) fprintf(stderr, gettext(
			    "Additional such failures will be ignored.\n"));
			complained_chown = 1;
		}
		if (chmod(rname, mode) < 0 && !complained_chmod) {
			saverr = errno;
			errmsg = gettext(
			    "Unable to restore permissions on %s: %s\n");
			(void) fprintf(stderr, errmsg,
			    rname, strerror(saverr));
			(void) fprintf(stderr, gettext(
			    "Additional such failures will be ignored.\n"));
			complained_chmod = 1;
		}
		skipfile();
		metaset(rname); /* skipfile() got the metadata, if any */
		if (utime(rname, (struct utimbuf *)timep) < 0 &&
		    !complained_utime) {
			saverr = errno;
			errmsg = gettext(
			    "Unable to restore times on %s: %s\n");
			(void) fprintf(stderr, errmsg,
			    rname, strerror(saverr));
			(void) fprintf(stderr, gettext(
			    "Additional such failures will be ignored.\n"));
			complained_utime = 1;
		}
		result = GOOD;
		break;

	case IFREG:
		vprintf(stdout, gettext("extract file %s\n"), rname);

		/*
		 * perform a restrictive creat(2) initally, we'll
		 * fchmod(2) according to the archive later after
		 * we've written the blocks.
		 */
		ofile = creat64(rname, 0600);

		if (ofile < 0) {
			saverr = errno;
			errmsg = gettext("cannot create file");
			(void) fprintf(stderr, "%s: ", rname);
			(void) fflush(stderr);
			errno = saverr;
			perror(errmsg);
			skipfile();
			result = FAIL;
			break;
		}
		if (fchown(ofile, uid, gid) < 0 && !complained_chown) {
			/* Just a warning */
			saverr = errno;
			errmsg = gettext(
			    "Unable to restore ownership of %s: %s\n");
			(void) fprintf(stderr, errmsg,
			    rname, strerror(saverr));
			(void) fprintf(stderr, gettext(
			    "Additional such failures will be ignored.\n"));
			complained_chown = 1;
		}

		getfile(xtrfile, xtrskip);
		metaset(rname);

		/*
		 * the fchmod(2) has to come after getfile() as some POSIX
		 * implementations clear the S_ISUID and S_ISGID bits of the
		 * file after every write(2).
		 */
		if (fchmod(ofile, mode) < 0 && !complained_chmod) {
			saverr = errno;
			errmsg = gettext(
			    "Unable to restore permissions on %s: %s\n");
			(void) fprintf(stderr, errmsg,
			    rname, strerror(saverr));
			(void) fprintf(stderr, gettext(
			    "Additional such failures will be ignored.\n"));
			complained_chmod = 1;
		}

		/*
		 * Some errors don't get reported until we close(2), so
		 * check for them.
		 * XXX unlink the file if an error is reported?
		 */
		if (close(ofile) < 0) {
			saverr = errno;
			errmsg = gettext("error closing file");
			(void) fprintf(stderr, "%s: ", rname);
			(void) fflush(stderr);
			errno = saverr;
			perror(errmsg);
			result = FAIL;
			break;
		}
		if (utime(rname, (struct utimbuf *)timep) < 0 &&
		    !complained_utime) {
			saverr = errno;
			errmsg = gettext(
			    "Unable to restore times on %s: %s\n");
			(void) fprintf(stderr, errmsg,
			    rname, strerror(saverr));
			(void) fprintf(stderr, gettext(
			    "Additional such failures will be ignored.\n"));
			complained_utime = 1;
		}

		result = GOOD;
		break;
	}
	if (dfd != AT_FDCWD) {
		fchdir(savepwd);
		(void) close(dfd);
	}
	return (result);
}

/*
 * skip over bit maps on the tape
 */
void
skipmaps(void)
{
	continuemap = 1;
	while (checktype(&spcl, TS_CLRI) == GOOD ||
	    checktype(&spcl, TS_BITS) == GOOD)
		skipfile();
	continuemap = 0;
}

/*
 * skip over a file on the tape
 */
void
skipfile(void)
{
	curfile.action = SKIP;
	getfile(null, null);
}
/*
 * Do the file extraction, calling the supplied functions
 * with the blocks
 */
void
getfile(void (*f1)(), void (*f2)())
{
	int i;
	size_t curblk = 0;
	offset_t size = (offset_t)spcl.c_dinode.di_size;
	static char clearedbuf[MAXBSIZE];
	char buf[TP_BSIZE_MAX];
	char *bufptr;
	char junk[TP_BSIZE_MAX];

	assert(MAXBSIZE >= tp_bsize);

	metaset(NULL);	/* flush old metadata */
	if (checktype(&spcl, TS_END) == GOOD) {
		panic(gettext("ran off end of volume\n"));
		return;
	}
	if (ishead(&spcl) == FAIL) {
		panic(gettext("not at beginning of a file\n"));
		return;
	}
	metacheck(&spcl); /* check for metadata in header */
	if (!gettingfile && setjmp(restart) != 0) {
		gettingfile = 0;	/* paranoia; longjmp'er should do */
		return;
	}
	gettingfile++;
loop:
	if ((spcl.c_dinode.di_mode & IFMT) == IFSHAD) {
		f1 = xtrmeta;
		f2 = metaskip;
	}
	for (i = 0, bufptr = buf; i < spcl.c_count; i++) {
		if ((i >= TP_NINDIR) || (spcl.c_addr[i])) {
			readtape(bufptr);
			bufptr += tp_bsize;
			curblk++;
			if (curblk == (fssize / tp_bsize)) {
				(*f1)(buf, size > tp_bsize ?
				    (size_t)(fssize) :
					/* LINTED size <= tp_bsize */
				    (curblk - 1) * tp_bsize + (size_t)size);
				curblk = 0;
				bufptr = buf;
			}
		} else {
			if (curblk > 0) {
				(*f1)(buf, size > tp_bsize ?
				    (size_t)(curblk * tp_bsize) :
					/* LINTED size <= tp_bsize */
				    (curblk - 1) * tp_bsize + (size_t)size);
				curblk = 0;
				bufptr = buf;
			}
			(*f2)(clearedbuf, size > tp_bsize ?
					/* LINTED size <= tp_bsize */
			    (long)tp_bsize : (size_t)size);
		}
		if ((size -= tp_bsize) <= 0) {
			for (i++; i < spcl.c_count; i++)
				if ((i >= TP_NINDIR) || (spcl.c_addr[i]))
					readtape(junk);
			break;
		}
	}
	if (curblk > 0) {
		/*
		 * Ok to cast size to size_t here. The above for loop reads
		 * data into the buffer then writes it to the output file. The
		 * call to f1 here is to write out the data that's in the
		 * buffer that has not yet been written to the file.
		 * This will be less than N-KB of data, since the
		 * above loop writes to the file in filesystem-
		 * blocksize chunks.
		 */
		/* LINTED: size fits into a size_t at this point */
		(*f1)(buf, (curblk * tp_bsize) + (size_t)size);

		curblk = 0;
		bufptr = buf;
	}
	if ((readhdr(&spcl) == GOOD) && (checktype(&spcl, TS_ADDR) == GOOD)) {
		if (continuemap)
			size = (offset_t)spcl.c_count * tp_bsize;
							/* big bitmap */
		else if ((size <= 0) &&
		    ((spcl.c_dinode.di_mode & IFMT) == IFSHAD)) {
			/* LINTED unsigned to signed conversion ok */
			size = spcl.c_dinode.di_size;
		}
		if (size > 0)
			goto loop;
	}
	if (size > 0)
		dprintf(stdout,
		    gettext("Missing address (header) block for %s\n"),
		    curfile.name);
	findinode(&spcl);
	gettingfile = 0;
}

/*
 * The next routines are called during file extraction to
 * put the data into the right form and place.
 */
static void
xtrfile(char *buf, size_t size)
{
	if (write(ofile, buf, (size_t)size) == -1) {
		int saverr = errno;
		(void) fprintf(stderr,
		    gettext("write error extracting inode %d, name %s\n"),
		    curfile.ino, curfile.name);
		errno = saverr;
		perror("write");
		done(1);
	}
}

/*
 * Even though size is a size_t, it's seeking to a relative
 * offset.  Thus, the seek could go beyond 2 GB, so lseek64 is needed.
 */

/*ARGSUSED*/
static void
xtrskip(char *buf, size_t size)
{
	if (lseek64(ofile, (offset_t)size, 1) == -1) {
		int saverr = errno;
		(void) fprintf(stderr,
		    gettext("seek error extracting inode %d, name %s\n"),
		    curfile.ino, curfile.name);
		errno = saverr;
		perror("lseek64");
		done(1);
	}
}

/* these are local to the next five functions */
static char *metadata = NULL;
static size_t metasize = 0;

static void
metacheck(struct s_spcl *head)
{
	if (! (head->c_flags & DR_HASMETA))
		return;
	if ((metadata = malloc(metasize = (size_t)sizeof (head->c_shadow)))
	    == NULL) {
		(void) fprintf(stderr,
		    gettext("Cannot malloc for metadata\n"));
		done(1);
	}
	bcopy(&(head->c_shadow), metadata, metasize);
}

static void
xtrmeta(char *buf, size_t size)
{
	if ((metadata == NULL) && ((spcl.c_dinode.di_mode & IFMT) != IFSHAD))
		return;
	if ((metadata = realloc(metadata, metasize + size)) == NULL) {
		(void) fprintf(stderr,
		    gettext("Cannot malloc for metadata\n"));
		done(1);
	}
	bcopy(buf, metadata + metasize, size);
	metasize += size;
}

/* ARGSUSED */
static void
metaskip(char *buf, size_t size)
{
	if (metadata == NULL)
		return;
	if ((metadata = realloc(metadata, metasize + size)) == NULL) {
		(void) fprintf(stderr,
		    gettext("Cannot malloc for metadata\n"));
		done(1);
	}
	bzero(metadata + metasize, size);
	metasize += size;
}

static void
metaset(char *name)
{
	if (metadata == NULL)
		return;
	if (name != NULL)
		metaproc(name, metadata, metasize);
	(void) free(metadata);
	metadata = NULL;
	metasize = 0;
}

void
metaget(char **data, size_t *size)
{
	*data = metadata;
	*size = metasize;
}

static void
fsd_acl(char *name, char *aclp, unsigned size)
{
	static aclent_t *aclent = NULL;
	ufs_acl_t *diskacl;
	static int n = 0;
	acl_t *set_aclp;
	uint_t i;
	int saverr, j;

	if (aclp == NULL) {
		if (aclent != NULL)
			free(aclent);
		aclent = NULL;
		n = 0;
		return;
	}

	/*LINTED [aclp is malloc'd]*/
	diskacl = (ufs_acl_t *)aclp;
	/* LINTED: result fits in an int */
	j = size / sizeof (*diskacl);
	normacls(byteorder, diskacl, j);

	i = n;
	n += j;
	aclent = realloc(aclent, n * (size_t)sizeof (*aclent));
	if (aclent == NULL) {
		(void) fprintf(stderr, gettext("Cannot malloc acl list\n"));
		done(1);
	}

	j = 0;
	while (i < n) {
		aclent[i].a_type = diskacl[j].acl_tag;
		aclent[i].a_id = diskacl[j].acl_who;
		aclent[i].a_perm = diskacl[j].acl_perm;
		++i;
		++j;
	}

	set_aclp = acl_to_aclp(ACLENT_T, aclent, n);
	if (set_aclp == NULL) {
		(void) fprintf(stderr, gettext("Cannot build acl_t\n"));
		done(1);
	}

	if (acl_set(name, set_aclp) == -1) {
		static int once = 0;

		/*
		 * Treat some errors from the acl subsystem specially to
		 * avoid being too noisy:
		 *
		 * ENOSYS - ACLs not supported on this file system
		 * EPERM  - not the owner or not privileged
		 *
		 * The following is also supported for backwards compat.
		 * since acl(2) used to return the wrong errno:
		 *
		 * EINVAL - not the owner of the object
		 */
		if (errno == ENOSYS || errno == EPERM || errno == EINVAL) {
			if (once == 0) {
				saverr = errno;
				++once;
				fprintf(stderr,
				    gettext("setacl failed: %s\n"),
				    strerror(saverr));
			}
		} else {
			saverr = errno;
			fprintf(stderr, gettext("setacl on %s failed: %s\n"),
			    name, strerror(saverr));
		}
	}
	acl_free(set_aclp);
}

static struct fsdtypes {
	int type;
	void (*function)();
} fsdtypes[] = {
	{FSD_ACL, fsd_acl},
	{FSD_DFACL, fsd_acl},
	{0, NULL}
};

void
metaproc(char *name, char *mdata, size_t msize)
{
	struct fsdtypes *fsdtype;
	ufs_fsd_t *fsd;
	char *c;

	/*
	 * for the whole shadow inode, dispatch each piece
	 * to the appropriate function.
	 */
	c = mdata;
	/* LINTED (c - mdata) fits into a size_t */
	while ((size_t)(c - mdata) < msize) {
		/*LINTED [mdata is malloc'd]*/
		fsd = (ufs_fsd_t *)c;
		assert((fsd->fsd_size % 4) == 0);
		/* LINTED: lint thinks pointers are signed */
		c += FSD_RECSZ(fsd, fsd->fsd_size);
		if ((fsd->fsd_type == FSD_FREE) ||
		    ((unsigned)(fsd->fsd_size) <= sizeof (ufs_fsd_t)) ||
		    (c > (mdata + msize)))
			break;
		for (fsdtype = fsdtypes; fsdtype->type; fsdtype++)
			if (fsdtype->type == fsd->fsd_type)
				(*fsdtype->function)(name, fsd->fsd_data,
				    (unsigned)(fsd->fsd_size) -
				    sizeof (fsd->fsd_type) -
				    sizeof (fsd->fsd_size));
		/* ^^^ be sure to change if fsd ever changes ^^^ */
	}

	/* reset the state of all the functions */
	for (fsdtype = fsdtypes; fsdtype->type; fsdtype++)
		(*fsdtype->function)(NULL, NULL, 0);
}

static void
xtrlnkfile(char *buf, size_t size)
{
	/* LINTED: signed/unsigned mix ok */
	pathlen += size;
	if (pathlen > MAXPATHLEN) {
		(void) fprintf(stderr,
		    gettext("symbolic link name: %s->%s%s; too long %d\n"),
		    curfile.name, lnkbuf, buf, pathlen);
		done(1);
	}
	buf[size] = '\0';
	(void) strcat(lnkbuf, buf);
	/* add an extra NULL to make this a legal complex string */
	lnkbuf[pathlen+1] = '\0';
}

/*ARGSUSED*/
static void
xtrlnkskip(char *buf, size_t size)
{
	(void) fprintf(stderr,
	    gettext("unallocated block in symbolic link %s\n"),
	    curfile.name);
	done(1);
}

static void
xtrmap(char *buf, size_t size)
{
	if ((map+size) > endmap) {
		int64_t mapsize, increment;
		int64_t diff;

		if (spcl.c_type != TS_ADDR) {
			(void) fprintf(stderr,
			    gettext("xtrmap: current record not TS_ADDR\n"));
			done(1);
		}
		if ((spcl.c_count < 0) || (spcl.c_count > TP_NINDIR)) {
			(void) fprintf(stderr,
			    gettext("xtrmap: illegal c_count field (%d)\n"),
			    spcl.c_count);
			done(1);
		}

		increment = d_howmany(
		    ((spcl.c_count * tp_bsize * NBBY) + 1), NBBY);
		mapsize = endmap - beginmap + increment;
		if (mapsize > UINT_MAX) {
			(void) fprintf(stderr,
			    gettext("xtrmap: maximum bitmap size exceeded"));
			done(1);
		}

		diff = map - beginmap;
		/* LINTED mapsize checked above */
		beginmap = realloc(beginmap, (size_t)mapsize);
		if (beginmap == NULL) {
			(void) fprintf(stderr,
			    gettext("xtrmap: realloc failed\n"));
			done(1);
		}
		map = beginmap + diff;
		endmap = beginmap + mapsize;
		/* LINTED endmap - map cannot exceed 32 bits */
		bzero(map, (size_t)(endmap - map));
		maxino = NBBY * mapsize + 1;
	}

	bcopy(buf, map, size);
	/* LINTED character pointers aren't signed */
	map += size;
}

/*ARGSUSED*/
static void
xtrmapskip(char *buf, size_t size)
{
	(void) fprintf(stderr, gettext("hole in map\n"));
	done(1);
}

/*ARGSUSED*/
void
null(char *buf, size_t size)
{
}

/*
 * Do the tape i/o, dealing with volume changes
 * etc..
 */
static void
readtape(char *b)
{
	int i;
	int rd, newvol;
	int cnt;
	struct s_spcl *sp;
	int32_t	expected_magic;

	if (tbf == NULL) {
		(void) fprintf(stderr, gettext(
		    "Internal consistency failure in readtape: tbf is NULL\n"));
		done(1);
	}
	expected_magic = ((tp_bsize == TP_BSIZE_MIN) ? NFS_MAGIC : MTB_MAGIC);

top:
	if (bct < numtrec) {
		/*
		 * check for old-dump floppy EOM -- it may appear in
		 * the middle of a buffer.  The Dflag used to be used for
		 * this, but since it doesn't hurt to always do this we
		 * got rid of the Dflag.
		 */
		/*LINTED [tbf = malloc()]*/
		sp = &((union u_spcl *)&tbf[bct*tp_bsize])->s_spcl;
		if (sp->c_magic == expected_magic && sp->c_type == TS_EOM &&
		    (time_t)(sp->c_date) == dumpdate &&
		    (time_t)(sp->c_ddate) == dumptime) {
			for (i = 0; i < ntrec; i++)
				/*LINTED [tbf = malloc()]*/
				((struct s_spcl *)
				    &tbf[i*tp_bsize])->c_magic = 0;
			bct = 0;
			rd = 0;
			i = 0;
			goto nextvol;
		}
		bcopy(&tbf[(bct++*tp_bsize)], b, (size_t)tp_bsize);
		blksread++;
		tapea++;
		return;
	}
	/*LINTED [assertion always true]*/
	assert(sizeof (union u_spcl) == TP_BSIZE_MAX);
	for (i = 0; i < ntrec; i++)
		/*LINTED [tbf = malloc()]*/
		((struct s_spcl *)&tbf[i*sizeof (struct s_spcl)])->c_magic = 0;
	if (numtrec == 0) {
		/* LINTED unsigned/signed assignment ok */
		numtrec = ntrec;
	}
	/* LINTED unsigned/signed assignment ok */
	cnt = ntrec*tp_bsize;
	rd = 0;
getmore:
	if (host)
		i = rmtread(&tbf[rd], cnt);
	else
		i = read(mt, &tbf[rd], cnt);
	/*
	 * Check for mid-tape short read error.
	 * If found, return rest of buffer.
	 */
	if (numtrec < ntrec && i != 0) {
		/* LINTED unsigned/signed assignment ok */
		numtrec = ntrec;
		goto top;
	}
	/*
	 * Handle partial block read.
	 */
	if (i > 0 && i != ntrec*tp_bsize) {
		if (pipein) {
			rd += i;
			cnt -= i;
			if (cnt > 0)
				goto getmore;
			i = rd;
		} else {
			if (i % tp_bsize != 0)
				panic(gettext(
				    "partial block read: %d should be %d\n"),
				    i, ntrec * tp_bsize);
			numtrec = i / tp_bsize;
			if (numtrec == 0)
				/*
				 * it's possible to read only 512 bytes
				 * from a QIC device...
				 */
				i = 0;
		}
	}
	/*
	 * Handle read error.
	 */
	if (i < 0) {
		switch (curfile.action) {
		default:
			(void) fprintf(stderr, gettext(
			    "Read error while trying to set up volume\n"));
			break;
		case UNKNOWN:
			(void) fprintf(stderr, gettext(
			    "Read error while trying to resynchronize\n"));
			break;
		case USING:
			(void) fprintf(stderr, gettext(
			    "Read error while restoring %s\n"),
			    curfile.name);
			break;
		case SKIP:
			(void) fprintf(stderr, gettext(
			    "Read error while skipping over inode %d\n"),
			    curfile.ino);
			break;
		}
		if (!yflag && !reply(gettext("continue")))
			done(1);
		/* LINTED: unsigned->signed conversion ok */
		i = (int)(ntrec*tp_bsize);
		bzero(tbf, (size_t)i);
		if ((host != 0 && rmtseek(i, 1) < 0) ||
		    (host == 0 && (lseek64(mt, (offset_t)i, 1) ==
		    (off64_t)-1))) {
			perror(gettext("continuation failed"));
			done(1);
		}
	}
	/*
	 * Handle end of tape.  The Dflag used to be used, but since it doesn't
	 * hurt to always check we got rid if it.
	 */

	/*
	 * if the first record in the buffer just read is EOM,
	 * change volumes.
	 */
	/*LINTED [tbf = malloc()]*/
	sp = &((union u_spcl *)tbf)->s_spcl;
	if (i != 0 && sp->c_magic == expected_magic && sp->c_type == TS_EOM &&
	    (time_t)(sp->c_date) == dumpdate &&
	    (time_t)(sp->c_ddate) == dumptime) {
		i = 0;
	}
nextvol:
	if (i == 0) {
		if (!pipein) {
			newvol = volno + 1;
			volno = 0;
			numtrec = 0;
			getvol(newvol);
			readtape(b); /* XXX tail recursion, not goto top? */
			return;
		}
		/* XXX if panic returns, should we round rd up? */
		/* XXX if we do, then we should zero the intervening space */
		if (rd % tp_bsize != 0)
			panic(gettext("partial block read: %d should be %d\n"),
			    rd, ntrec * tp_bsize);
		bcopy((char *)&endoftapemark, &tbf[rd], (size_t)tp_bsize);
	}
	bct = 0;
	bcopy(&tbf[(bct++*tp_bsize)], b, (size_t)tp_bsize);
	blksread++;
	recsread++;
	tapea++;
	rec_position++;
}

void
findtapeblksize(int arfile)
{
	int	i;

	if (tbf == NULL) {
		(void) fprintf(stderr, gettext(
		    "Internal consistency failure in findtapeblksize: "
		    "tbf is NULL\n"));
		assert(tbf != NULL);
		done(1);
	}

	for (i = 0; i < ntrec; i++)
		/*LINTED [tbf = malloc()]*/
		((struct s_spcl *)&tbf[i * tp_bsize])->c_magic = 0;
	bct = 0;
	if (host && arfile == TAPE_FILE)
		tape_rec_size = rmtread(tbf, ntrec * tp_bsize);
	else
		tape_rec_size = read(mt, tbf, ntrec * tp_bsize);
	recsread++;
	rec_position++;
	if (tape_rec_size == (ssize_t)-1) {
		int saverr = errno;
		char *errmsg = gettext("Media read error");
		errno = saverr;
		perror(errmsg);
		done(1);
	}
	if (tape_rec_size % tp_bsize != 0) {
		(void) fprintf(stderr, gettext(
	    "Record size (%d) is not a multiple of dump block size (%d)\n"),
		    tape_rec_size, tp_bsize);
		done(1);
	}
	ntrec = (int)tape_rec_size / tp_bsize;
	/* LINTED unsigned/signed assignment ok */
	numtrec = ntrec;
	vprintf(stdout, gettext("Media block size is %d\n"), ntrec*2);
}

void
flsht(void)
{
	/* LINTED unsigned/signed assignment ok */
	bct = ntrec+1;
}

void
closemt(int mode)
{
	/*
	 * If mode == FORCE_OFFLINE then we're not done but
	 * we need to change tape. So, rewind and unload current
	 * tape before loading the new one.
	 */

	static struct mtop mtop = { MTOFFL, 0 };

	if (mt < 0)
		return;
	if (offline || mode == FORCE_OFFLINE)
		(void) fprintf(stderr, gettext("Rewinding tape\n"));
	if (host) {
		if (offline || mode == FORCE_OFFLINE)
			(void) rmtioctl(MTOFFL, 1);
		rmtclose();
	} else if (pipein) {
		char buffy[MAXBSIZE];

		while (read(mt, buffy, sizeof (buffy)) > 0) {
			continue;
			/*LINTED [assertion always true]*/
		}
		(void) close(mt);
	} else {
		/*
		 * Only way to tell if this is a floppy is to issue an ioctl
		 * but why waste one - if the eject fails, tough!
		 */
		if (offline || mode == FORCE_OFFLINE)
			(void) ioctl(mt, MTIOCTOP, &mtop);
		(void) ioctl(mt, FDEJECT, 0);
		(void) close(mt);
	}
	mt = -1;
}

static int
checkvol(struct s_spcl *b, int t)
{

	if (b->c_volume != t)
		return (FAIL);
	return (GOOD);
}

int
readhdr(struct s_spcl *b)
{

	if (gethead(b) == FAIL) {
		dprintf(stdout, gettext("readhdr fails at %ld blocks\n"),
		    blksread);
		return (FAIL);
	}
	return (GOOD);
}

/*
 * read the tape into buf, then return whether or
 * or not it is a header block.
 */
int
gethead(struct s_spcl *buf)
{
	int i;
	union u_ospcl {
		char dummy[TP_BSIZE_MIN];
		struct	s_ospcl {
			int32_t	c_type;
			int32_t	c_date;
			int32_t	c_ddate;
			int32_t	c_volume;
			int32_t	c_tapea;
			ushort_t c_inumber;
			int32_t	c_magic;
			int32_t	c_checksum;
			struct odinode {
				unsigned short odi_mode;
				ushort_t odi_nlink;
				ushort_t odi_uid;
				ushort_t odi_gid;
				int32_t	odi_size;
				int32_t	odi_rdev;
				char	odi_addr[36];
				int32_t	odi_atime;
				int32_t	odi_mtime;
				int32_t	odi_ctime;
			} c_dinode;
			int32_t	c_count;
			char	c_baddr[256];
		} s_ospcl;
	} u_ospcl;

	if (cvtflag) {
		readtape((char *)(&u_ospcl.s_ospcl));
		bzero((char *)buf, (size_t)TP_BSIZE_MIN);
		buf->c_type = u_ospcl.s_ospcl.c_type;
		buf->c_date = u_ospcl.s_ospcl.c_date;
		buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
		buf->c_volume = u_ospcl.s_ospcl.c_volume;
		buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
		buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
		buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
		buf->c_magic = u_ospcl.s_ospcl.c_magic;
		buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
		/* LINTED: unsigned/signed combination ok */
		buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
		buf->c_dinode.di_size =
		    (unsigned)(u_ospcl.s_ospcl.c_dinode.odi_size);
		buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
		buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
		buf->c_dinode.di_suid = UID_LONG;
		buf->c_dinode.di_sgid = GID_LONG;
		buf->c_dinode.di_ordev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
		buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
		buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
		buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
		buf->c_count = u_ospcl.s_ospcl.c_count;
		bcopy(u_ospcl.s_ospcl.c_baddr, buf->c_addr,
		    sizeof (u_ospcl.s_ospcl.c_baddr));

		/*CONSTANTCONDITION*/
		assert(sizeof (u_ospcl.s_ospcl) < sizeof (union u_spcl));

		/* we byte-swap the new spclrec, but checksum the old	*/
		/* (see comments in normspcl())				*/
		if (normspcl(byteorder, buf,
		    (int *)(&u_ospcl.s_ospcl), sizeof (u_ospcl.s_ospcl),
		    OFS_MAGIC))
			return (FAIL);
		buf->c_magic =
		    ((tp_bsize == TP_BSIZE_MIN) ? NFS_MAGIC : MTB_MAGIC);
	} else {
		readtape((char *)buf);
		if (normspcl(byteorder, buf, (int *)buf, tp_bsize,
		    ((tp_bsize == TP_BSIZE_MIN) ? NFS_MAGIC : MTB_MAGIC)))
			return (FAIL);
	}

	switch (buf->c_type) {

	case TS_CLRI:
	case TS_BITS:
		/*
		 * Have to patch up missing information in bit map headers
		 */
		buf->c_inumber = 0;
		buf->c_dinode.di_size = (offset_t)buf->c_count * tp_bsize;
		for (i = 0; i < buf->c_count && i < TP_NINDIR; i++)
			buf->c_addr[i] = 1;
		break;

	case TS_TAPE:
	case TS_END:
		if (dumpinfo.c_date == 0) {
			dumpinfo.c_date = spcl.c_date;
			dumpinfo.c_ddate = spcl.c_ddate;
		}
		if (!hostinfo && spcl.c_host[0] != '\0') {
			bcopy(spcl.c_label, dumpinfo.c_label,
			    sizeof (spcl.c_label));
			bcopy(spcl.c_filesys, dumpinfo.c_filesys,
			    sizeof (spcl.c_filesys));
			bcopy(spcl.c_dev, dumpinfo.c_dev,
			    sizeof (spcl.c_dev));
			bcopy(spcl.c_host, dumpinfo.c_host,
			    sizeof (spcl.c_host));
			dumpinfo.c_level = spcl.c_level;
			hostinfo++;
			if (c_label != NULL &&
			    strncmp(c_label, spcl.c_label,
			    sizeof (spcl.c_label))
			    != 0) {
				(void) fprintf(stderr, gettext(
		    "Incorrect tape label.  Expected `%s', got `%.*s'\n"),
				    c_label,
				    sizeof (spcl.c_label), spcl.c_label);
				done(1);
			}
		}
		if (!inodeinfo && (spcl.c_flags & DR_INODEINFO)) {
			dumpinfo.c_volume = spcl.c_volume;
			bcopy(spcl.c_inos, dumpinfo.c_inos,
			    sizeof (spcl.c_inos));
			inodeinfo++;
		}
		buf->c_inumber = 0;
		break;

	case TS_INODE:
	case TS_ADDR:
		break;

	default:
		panic(gettext("%s: unknown inode type %d\n"),
		    "gethead", buf->c_type);
		return (FAIL);
	}
	if (dflag)
		accthdr(buf);
	return (GOOD);
}

/*
 * Check that a header is where it belongs and predict the next header
 */
static void
accthdr(struct s_spcl *header)
{
	static ino_t previno = (ino_t)(unsigned)-1;
	static int prevtype;
	static long predict;
	int blks, i;

	if (header->c_type == TS_TAPE) {
		if (header->c_firstrec)
			(void) fprintf(stderr,
			    gettext("Volume header begins with record %d"),
			    header->c_firstrec);
		else
			(void) fprintf(stderr, gettext("Volume header"));
		(void) fprintf(stderr, "\n");
		previno = (ino_t)(unsigned)-1;
		return;
	}
	if (previno == (ino_t)(unsigned)-1)
		goto newcalc;
	switch (prevtype) {
	case TS_BITS:
		(void) fprintf(stderr, gettext("Dump mask header"));
		break;
	case TS_CLRI:
		(void) fprintf(stderr, gettext("Remove mask header"));
		break;
	case TS_INODE:
		(void) fprintf(stderr,
		    gettext("File header, ino %d at record %d"),
		    previno, rec_position);
		break;
	case TS_ADDR:
		(void) fprintf(stderr,
		    gettext("File continuation header, ino %d"),
		    previno);
		break;
	case TS_END:
		(void) fprintf(stderr, gettext("End of media header"));
		break;
	}
	if (predict != blksread - 1)
		(void) fprintf(stderr,
		    gettext("; predicted %ld blocks, got %ld blocks"),
		    predict, blksread - 1);
	(void) fprintf(stderr, "\n");
newcalc:
	blks = 0;
	if (header->c_type != TS_END)
		for (i = 0; i < header->c_count; i++)
			if ((i >= TP_NINDIR) || (header->c_addr[i] != 0))
				blks++;
	predict = blks;
	blksread = 0;
	prevtype = header->c_type;
	previno = header->c_inumber;
}

/*
 * Try to determine which volume a file resides on.
 */
int
volnumber(ino_t inum)
{
	int i;

	if (inodeinfo == 0)
		return (0);
	for (i = 1; i <= dumpinfo.c_volume; i++)
		if (inum < (ino_t)(unsigned)(dumpinfo.c_inos[i]))
			break;
	return (i - 1);
}

/*
 * Find an inode header.
 * Note that *header must be stable storage, as curfile will end up with
 * pointers into it.
 */
void
findinode(struct s_spcl *header)
{
	long skipcnt = 0;
	int i;
	char buf[TP_BSIZE_MAX];

	curfile.name = gettext("<name unknown>");
	curfile.action = UNKNOWN;
	curfile.dip = (struct dinode *)NULL;
	curfile.ino = 0;
	curfile.ts = 0;
	if (ishead(header) == FAIL) {
		skipcnt++;
		while (gethead(header) == FAIL ||
		    (time_t)(header->c_date) != dumpdate)
			skipcnt++;
	}
	for (;;) {
		if (checktype(header, TS_ADDR) == GOOD) {
			/*
			 * Skip up to the beginning of the next record
			 */
			for (i = 0; i < header->c_count; i++)
				if ((i >= TP_NINDIR) || (header->c_addr[i]))
					readtape(buf);
			(void) gethead(header);
			continue;
		}
		if (checktype(header, TS_INODE) == GOOD) {
			curfile.dip = &header->c_dinode;
			if (curfile.dip->di_suid != UID_LONG)
				curfile.dip->di_uid = curfile.dip->di_suid;
			if (curfile.dip->di_sgid != GID_LONG)
				curfile.dip->di_gid = curfile.dip->di_sgid;
			curfile.ino = header->c_inumber;
			curfile.ts = TS_INODE;
			break;
		}
		if (checktype(header, TS_END) == GOOD) {
			curfile.ino = maxino;
			curfile.ts = TS_END;
			break;
		}
		if (checktype(header, TS_CLRI) == GOOD) {
			curfile.name = gettext("<file removal list>");
			curfile.ts = TS_CLRI;
			break;
		}
		if (checktype(header, TS_BITS) == GOOD) {
			curfile.name = gettext("<file dump list>");
			curfile.ts = TS_BITS;
			break;
		}
		while (gethead(header) == FAIL)
			skipcnt++;
	}
	if (skipcnt > 0)
		(void) fprintf(stderr,
		    gettext("resync restore, skipped %d blocks\n"),
		    skipcnt);
}

/*
 * return whether or not the buffer contains a header block
 */
static int
ishead(struct s_spcl *buf)
{
	if (buf->c_magic !=
	    ((tp_bsize == TP_BSIZE_MIN) ? NFS_MAGIC : MTB_MAGIC))
		return (FAIL);
	return (GOOD);
}

static int
checktype(struct s_spcl *b, int t)
{
	if (b->c_type != t)
		return (FAIL);
	return (GOOD);
}

/*
 * If autoloading is enabled, attempt to do it.  If we succeed,
 * return non-zero.
 */
static int
autoload_tape(void)
{
	int result = 0;		/* assume failure */
	int tries;
	int fd;

	if (autoload) {
		/*
		 * Wait for the tape to autoload.  Note that the delay
		 * period doesn't take into account however long it takes
		 * for the open to fail (measured at 21 seconds for an
		 * Exabyte 8200 under 2.7 on an Ultra 2).
		 */

		/* rewind tape and offline drive before loading new tape */
		closemt(FORCE_OFFLINE);
		(void) fprintf(stderr,
		    gettext("Attempting to autoload next volume\n"));
		for (tries = 0; tries < autoload_tries; tries++) {
			if (host) {
				if (rmtopen(magtape, O_RDONLY) >= 0) {
					rmtclose();
					result = 1;
					break;
				}
			} else {
				if ((fd = open(magtape, O_RDONLY|O_LARGEFILE,
				    0600)) >= 0) {
					(void) close(fd);
					result = 1;
					break;
				}
			}
			(void) sleep(autoload_period);
		}
		if (result == 0) {
			/* Assume caller will deal with manual change-over */
			(void) fprintf(stderr,
			    gettext("Autoload timed out\n"));
		} else {
			if ((host != NULL &&
			    (mt = rmtopen(magtape, O_RDONLY)) == -1) ||
			    (host == NULL &&
			    (mt = open(magtape, O_RDONLY|O_LARGEFILE)) == -1)) {
				(void) fprintf(stderr, gettext(
				    "Autoload could not re-open tape\n"));
				result = 0;
			} else {
				(void) fprintf(stderr, gettext(
				    "Tape loaded\n"));
			}
		}
	}

	return (result);
}
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/

/*
 * Copyright (c) 1983 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#include "restore.h"
#include <ctype.h>
#include <errno.h>
#include <syslog.h>
#include <limits.h>
/* LINTED: this file really is necessary */
#include <euc.h>
#include <widec.h>

/*
 * Insure that all the components of a pathname exist. Note that
 * lookupname() and addentry() both expect complex names as
 * input arguments, so a double NULL needs to be added to each name.
 */
void
pathcheck(char *name)
{
	char *cp, save;
	struct entry *ep;
	char *start;

	start = strchr(name, '/');
	if (start == 0)
		return;
	for (cp = start; *cp != '\0'; cp++) {
		if (*cp != '/')
			continue;
		*cp = '\0';
		save = *(cp+1);
		*(cp+1) = '\0';
		ep = lookupname(name);
		if (ep == NIL) {
			ep = addentry(name, psearch(name), NODE);
			newnode(ep);
		}
		/* LINTED: result fits in a short */
		ep->e_flags |= NEW|KEEP;
		*cp = '/';
		*(cp+1) = save;
	}
}

/*
 * Change a name to a unique temporary name.
 */
void
mktempname(struct entry *ep)
{
	char *newname;

	if (ep->e_flags & TMPNAME)
		badentry(ep, gettext("mktempname: called with TMPNAME"));
	/* LINTED: result fits in a short */
	ep->e_flags |= TMPNAME;
	newname = savename(gentempname(ep));
	renameit(myname(ep), newname);
	freename(ep->e_name);
	ep->e_name = newname;
	/* LINTED: savename guarantees strlen will fit */
	ep->e_namlen = strlen(ep->e_name);
}

/*
 * Generate a temporary name for an entry.
 */
char *
gentempname(struct entry *ep)
{
	static char name[MAXPATHLEN];
	struct entry *np;
	long i = 0;

	for (np = lookupino(ep->e_ino); np != NIL && np != ep; np = np->e_links)
		i++;
	if (np == NIL)
		badentry(ep, gettext("not on ino list"));
	(void) snprintf(name, sizeof (name), "%s%ld%lu", TMPHDR, i, ep->e_ino);
	return (name);
}

/*
 * Rename a file or directory.
 */
void
renameit(char *fp, char *tp)
{
	int fromfd, tofd;
	char *from, *to;
	char tobuf[MAXPATHLEN];
	char *pathend;

	resolve(fp, &fromfd, &from);
	/*
	 * The to pointer argument is assumed to be either a fully
	 * specified path (starting with "./") or a simple temporary
	 * file name (starting with TMPHDR).  If passed a simple temp
	 * file name, we need to set up the descriptors explicitly.
	 */
	if (strncmp(tp, TMPHDR, sizeof (TMPHDR) - 1) == 0) {
		tofd = fromfd;
		if ((pathend = strrchr(from, '/')) != NULL) {
			strncpy(tobuf, from, pathend - from + 1);
			tobuf[pathend - from + 1] = '\0';
			strlcat(tobuf, tp, sizeof (tobuf));
			to = tobuf;
		} else {
			to = tp;
		}
	} else
		resolve(tp, &tofd, &to);
	if (renameat(fromfd, from, tofd, to) < 0) {
		int saverr = errno;
		(void) fprintf(stderr,
		    gettext("Warning: cannot rename %s to %s: %s\n"),
		    from, to, strerror(saverr));
		(void) fflush(stderr);
	} else {
		vprintf(stdout, gettext("rename %s to %s\n"), from, to);
	}
	if (fromfd != AT_FDCWD) (void) close(fromfd);
	if (tofd != AT_FDCWD) (void) close(tofd);
}

/*
 * Create a new node (directory). Note that, because we have no
 * mkdirat() function, fchdir() must be used set up the appropriate
 * name space context prior to the call to mkdir() if we are
 * operating in attribute space.
 */
void
newnode(struct entry *np)
{
	char *cp;
	int dfd;

	if (np->e_type != NODE)
		badentry(np, gettext("newnode: not a node"));
	resolve(myname(np), &dfd, &cp);
	if (dfd != AT_FDCWD) {
		if (fchdir(dfd) < 0) {
			int saverr = errno;
			(void) fprintf(stderr,
			    gettext("Warning: cannot create %s: %s"),
			    cp, strerror(saverr));
			(void) fflush(stderr);
			(void) close(dfd);
			return;
		}
	}
	if (mkdir(cp, 0777) < 0) {
		int saverr = errno;
		/* LINTED: result fits in a short */
		np->e_flags |= EXISTED;
		(void) fprintf(stderr, gettext("Warning: "));
		(void) fflush(stderr);
		(void) fprintf(stderr, "%s: %s\n", cp, strerror(saverr));
	} else {
		vprintf(stdout, gettext("Make node %s\n"), cp);
	}
	if (dfd != AT_FDCWD) {
		fchdir(savepwd);
		(void) close(dfd);
	}
}

/*
 * Remove an old node (directory). See comment above on newnode()
 * for explanation of fchdir() use below.
 */
void
removenode(struct entry *ep)
{
	char *cp;
	int dfd;

	if (ep->e_type != NODE)
		badentry(ep, gettext("removenode: not a node"));
	if (ep->e_entries != NIL)
		badentry(ep, gettext("removenode: non-empty directory"));
	/* LINTED: result fits in a short */
	ep->e_flags |= REMOVED;
	/* LINTED: result fits in a short */
	ep->e_flags &= ~TMPNAME;
	resolve(myname(ep), &dfd, &cp);
	if (dfd != AT_FDCWD) {
		if (fchdir(dfd) < 0) {
			int saverr = errno;
			(void) fprintf(stderr,
			    gettext("Warning: cannot remove %s: %s"),
			    cp, strerror(saverr));
			(void) fflush(stderr);
			(void) close(dfd);
			return;
		}
	}
	if (rmdir(cp) < 0) {	/* NOTE: could use unlinkat (..,REMOVEDIR) */
		int saverr = errno;
		(void) fprintf(stderr, gettext("Warning: %s: %s\n"),
		    cp, strerror(saverr));
		(void) fflush(stderr);
	} else {
		vprintf(stdout, gettext("Remove node %s\n"), cp);
	}
	if (dfd != AT_FDCWD) {
		(void) fchdir(savepwd);
		(void) close(dfd);
	}
}

/*
 * Remove a leaf.
 */
void
removeleaf(struct entry *ep)
{
	char *cp;
	int dfd;

	if (ep->e_type != LEAF)
		badentry(ep, gettext("removeleaf: not a leaf"));
	/* LINTED: result fits in a short */
	ep->e_flags |= REMOVED;
	/* LINTED: result fits in a short */
	ep->e_flags &= ~TMPNAME;
	resolve(myname(ep), &dfd, &cp);
	if (unlinkat(dfd, cp, 0) < 0) {
		int saverr = errno;
		(void) fprintf(stderr, gettext("Warning: %s: %s\n"),
		    cp, strerror(saverr));
		(void) fflush(stderr);
	} else {
		vprintf(stdout, gettext("Remove leaf %s\n"), cp);
	}
	if (dfd != AT_FDCWD)
		(void) close(dfd);
}

/*
 * Create a link.
 *	This function assumes that the context has already been set
 *	for the link file to be created (i.e., we have "fchdir-ed"
 *	into attribute space already if this is an attribute link).
 */
int
lf_linkit(char *existing, char *new, int type)
{
	char linkbuf[MAXPATHLEN];
	struct stat64 s1[1], s2[1];
	char *name;
	int dfd, l, result;

	resolve(existing, &dfd, &name);
	if (dfd == -1) {
		(void) fprintf(stderr, gettext(
		    "Warning: cannot restore %s link %s->%s\n"),
		    (type == SYMLINK ? "symbolic" : "hard"), new, existing);
		result = FAIL;
		goto out;
	}
	if (type == SYMLINK) {
		if (symlink(name, new) < 0) {
			/* No trailing \0 from readlink(2) */
			if (((l = readlink(new, linkbuf, sizeof (linkbuf)))
			    > 0) &&
			    (l == strlen(name)) &&
			    (strncmp(linkbuf, name, l) == 0)) {
				vprintf(stdout,
				    gettext("Symbolic link %s->%s ok\n"),
				    new, name);
				result = GOOD;
				goto out;
			} else {
				int saverr = errno;
				(void) fprintf(stderr, gettext(
			    "Warning: cannot create symbolic link %s->%s: %s"),
				    new, name, strerror(saverr));
				(void) fflush(stderr);
				result = FAIL;
				goto out;
			}
		}
	} else if (type == HARDLINK) {
		if (link(name, new) < 0) {
			int saverr = errno;
			if ((stat64(name, s1) == 0) &&
			    (stat64(new, s2) == 0) &&
			    (s1->st_dev == s2->st_dev) &&
			    (s1->st_ino == s2->st_ino)) {
				vprintf(stdout,
				    gettext("Hard link %s->%s ok\n"),
				    new, name);
				result = GOOD;
				goto out;
			} else {
				(void) fprintf(stderr, gettext(
			    "Warning: cannot create hard link %s->%s: %s\n"),
				    new, name, strerror(saverr));
				(void) fflush(stderr);
				result = FAIL;
				goto out;
			}
		}
	} else {
		panic(gettext("%s: unknown type %d\n"), "linkit", type);
		result = FAIL;
		goto out;
	}
	result = GOOD;
	if (type == SYMLINK)
		vprintf(stdout, gettext("Create symbolic link %s->%s\n"),
		    new, name);
	else
		vprintf(stdout, gettext("Create hard link %s->%s\n"),
		    new, name);
out:
	if (dfd != AT_FDCWD) {
		(void) close(dfd);
	}
	return (result);
}

/*
 * Find lowest-numbered inode (above "start") that needs to be extracted.
 * Caller knows that a return value of maxino means there's nothing left.
 */
ino_t
lowerbnd(ino_t start)
{
	struct entry *ep;

	for (; start < maxino; start++) {
		ep = lookupino(start);
		if (ep == NIL || ep->e_type == NODE)
			continue;
		if (ep->e_flags & (NEW|EXTRACT))
			return (start);
	}
	return (start);
}

/*
 * Find highest-numbered inode (below "start") that needs to be extracted.
 */
ino_t
upperbnd(ino_t start)
{
	struct entry *ep;

	for (; start > ROOTINO; start--) {
		ep = lookupino(start);
		if (ep == NIL || ep->e_type == NODE)
			continue;
		if (ep->e_flags & (NEW|EXTRACT))
			return (start);
	}
	return (start);
}

/*
 * report on a badly formed entry
 */
void
badentry(struct entry *ep, char *msg)
{

	(void) fprintf(stderr, gettext("bad entry: %s\n"), msg);
	(void) fprintf(stderr, gettext("name: %s\n"), myname(ep));
	(void) fprintf(stderr, gettext("parent name %s\n"),
	    myname(ep->e_parent));
	if (ep->e_sibling != NIL)
		(void) fprintf(stderr, gettext("sibling name: %s\n"),
		    myname(ep->e_sibling));
	if (ep->e_entries != NIL)
		(void) fprintf(stderr, gettext("next entry name: %s\n"),
		    myname(ep->e_entries));
	if (ep->e_links != NIL)
		(void) fprintf(stderr, gettext("next link name: %s\n"),
		    myname(ep->e_links));
	if (ep->e_xattrs != NIL)
		(void) fprintf(stderr, gettext("attribute root name: %s\n"),
		    myname(ep->e_xattrs));
	if (ep->e_next != NIL)
		(void) fprintf(stderr, gettext("next hashchain name: %s\n"),
		    myname(ep->e_next));
	(void) fprintf(stderr, gettext("entry type: %s\n"),
	    ep->e_type == NODE ? gettext("NODE") : gettext("LEAF"));
	(void) fprintf(stderr, gettext("inode number: %lu\n"), ep->e_ino);
	panic(gettext("flags: %s\n"), flagvalues(ep));
	/* Our callers are expected to handle our returning. */
}

/*
 * Construct a string indicating the active flag bits of an entry.
 */
char *
flagvalues(struct entry *ep)
{
	static char flagbuf[BUFSIZ];

	(void) strlcpy(flagbuf, gettext("|NIL"), sizeof (flagbuf));
	flagbuf[0] = '\0';
	if (ep->e_flags & REMOVED)
		(void) strlcat(flagbuf, gettext("|REMOVED"), sizeof (flagbuf));
	if (ep->e_flags & TMPNAME)
		(void) strlcat(flagbuf, gettext("|TMPNAME"), sizeof (flagbuf));
	if (ep->e_flags & EXTRACT)
		(void) strlcat(flagbuf, gettext("|EXTRACT"), sizeof (flagbuf));
	if (ep->e_flags & NEW)
		(void) strlcat(flagbuf, gettext("|NEW"), sizeof (flagbuf));
	if (ep->e_flags & KEEP)
		(void) strlcat(flagbuf, gettext("|KEEP"), sizeof (flagbuf));
	if (ep->e_flags & EXISTED)
		(void) strlcat(flagbuf, gettext("|EXISTED"), sizeof (flagbuf));
	if (ep->e_flags & XATTR)
		(void) strlcat(flagbuf, gettext("|XATTR"), sizeof (flagbuf));
	if (ep->e_flags & XATTRROOT)
		(void) strlcat(flagbuf, gettext("|XATTRROOT"),
		    sizeof (flagbuf));
	return (&flagbuf[1]);
}

/*
 * Check to see if a name is on a dump tape.
 */
ino_t
dirlookup(char *name)
{
	ino_t ino;

	ino = psearch(name);
	if (ino == 0 || BIT(ino, dumpmap) == 0)
		(void) fprintf(stderr, gettext("%s is not on volume\n"), name);
	return (ino);
}

/*
 * Elicit a reply.
 */
int
reply(char *question)
{
	char *yesorno = gettext("yn"); /* must be two characters, "yes" first */
	int c;

	do	{
		(void) fprintf(stderr, "%s? [%s] ", question, yesorno);
		(void) fflush(stderr);
		c = getc(terminal);
		while (c != '\n' && getc(terminal) != '\n') {
			if (ferror(terminal)) {
				(void) fprintf(stderr, gettext(
				    "Error reading response\n"));
				(void) fflush(stderr);
				return (FAIL);
			}
			if (feof(terminal))
				return (FAIL);
		}
		if (isupper(c))
			c = tolower(c);
	} while (c != yesorno[0] && c != yesorno[1]);
	if (c == yesorno[0])
		return (GOOD);
	return (FAIL);
}

/*
 * handle unexpected inconsistencies
 */
/*
 * Note that a panic w/ EOF on the tty means all panics will return...
 */
#include <stdarg.h>

/* VARARGS1 */
void
panic(const char *msg, ...)
{
	va_list	args;

	va_start(args, msg);
	(void) vfprintf(stderr, msg, args);
	va_end(args);
	if (reply(gettext("abort")) == GOOD) {
		if (reply(gettext("dump core")) == GOOD)
			abort();
		done(1);
	}
}

/*
 * Locale-specific version of ctime
 */
char *
lctime(time_t *tp)
{
	static char buf[256];
	struct tm *tm;

	tm = localtime(tp);
	(void) strftime(buf, sizeof (buf), "%c\n", tm);
	return (buf);
}

static int
statcmp(const struct stat *left, const struct stat *right)
{
	int result = 1;

	if ((left->st_dev == right->st_dev) &&
	    (left->st_ino == right->st_ino) &&
	    (left->st_mode == right->st_mode) &&
	    (left->st_nlink == right->st_nlink) &&
	    (left->st_uid == right->st_uid) &&
	    (left->st_gid == right->st_gid) &&
	    (left->st_rdev == right->st_rdev) &&
	    (left->st_ctim.tv_sec == right->st_ctim.tv_sec) &&
	    (left->st_ctim.tv_nsec == right->st_ctim.tv_nsec) &&
	    (left->st_mtim.tv_sec == right->st_mtim.tv_sec) &&
	    (left->st_mtim.tv_nsec == right->st_mtim.tv_nsec) &&
	    (left->st_blksize == right->st_blksize) &&
	    (left->st_blocks == right->st_blocks)) {
		result = 0;
	}

	return (result);
}

/*
 * Safely open a file.
 */
int
safe_open(int dfd, const char *filename, int mode, int perms)
{
	static int init_syslog = 1;
	int fd;
	int working_mode;
	int saverr;
	char *errtext;
	struct stat pre_stat, pre_lstat;
	struct stat post_stat, post_lstat;

	if (init_syslog) {
		openlog(progname, LOG_CONS, LOG_DAEMON);
		init_syslog = 0;
	}

	/*
	 * Don't want to be spoofed into trashing something we
	 * shouldn't, thus the following rigamarole.  If it doesn't
	 * exist, we create it and proceed.  Otherwise, require that
	 * what's there be a real file with no extraneous links and
	 * owned by whoever ran us.
	 *
	 * The silliness with using both lstat() and fstat() is to avoid
	 * race-condition games with someone replacing the file with a
	 * symlink after we've opened it.  If there was an flstat(),
	 * we wouldn't need the fstat().
	 *
	 * The initial open with the hard-coded flags is ok even if we
	 * are intending to open only for reading.  If it succeeds,
	 * then the file did not exist, and we'll synthesize an appropriate
	 * complaint below.  Otherwise, it does exist, so we won't be
	 * truncating it with the open.
	 */
	if ((fd = openat(dfd, filename,
	    O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_LARGEFILE, perms)) < 0) {
		if (errno == EEXIST) {
			if (fstatat(dfd, filename, &pre_lstat,
			    AT_SYMLINK_NOFOLLOW) < 0) {
				saverr = errno;
				(void) close(fd);
				errno = saverr;
				return (-1);
			}

			if (fstatat(dfd, filename, &pre_stat, 0) < 0) {
				saverr = errno;
				(void) close(fd);
				errno = saverr;
				return (-1);
			}

			working_mode = mode & (O_WRONLY|O_RDWR|O_RDONLY);
			working_mode |= O_LARGEFILE;

			if ((fd = openat(dfd, filename, working_mode)) < 0) {
				if (errno == ENOENT) {
					errtext = gettext(
"Unexpected condition detected: %s used to exist, but doesn't any longer\n");
					(void) fprintf(stderr, errtext,
					    filename);
					syslog(LOG_WARNING, errtext, filename);
					errno = ENOENT;
				}
				return (-1);
			}

			if (fstatat(fd, NULL, &post_lstat,
			    AT_SYMLINK_NOFOLLOW) < 0) {
				saverr = errno;
				(void) close(fd);
				errno = saverr;
				return (-1);
			}

			if (fstatat(fd, NULL, &post_stat, 0) < 0) {
				saverr = errno;
				(void) close(fd);
				errno = saverr;
				return (-1);
			}

			if (statcmp(&pre_lstat, &post_lstat) != 0) {
				errtext = gettext(
"Unexpected condition detected: %s's lstat(2) information changed\n");
				(void) fprintf(stderr, errtext, filename);
				syslog(LOG_WARNING, errtext, filename);
				errno = EPERM;
				return (-1);
			}

			if (statcmp(&pre_stat, &post_stat) != 0) {
				errtext = gettext(
"Unexpected condition detected: %s's stat(2) information changed\n");
				(void) fprintf(stderr, errtext, filename);
				syslog(LOG_WARNING, errtext, filename);
				errno = EPERM;
				return (-1);
			}

			/*
			 * If inode, device, or type are wrong, bail out.
			 */
			if ((!S_ISREG(post_lstat.st_mode) ||
			    (post_stat.st_ino != post_lstat.st_ino) ||
			    (post_stat.st_dev != post_lstat.st_dev))) {
				errtext = gettext(
	    "Unexpected condition detected: %s is not a regular file\n");
				(void) fprintf(stderr, errtext, filename);
				syslog(LOG_WARNING, errtext, filename);
				(void) close(fd);
				errno = EPERM;
				return (-1);
			}

			/*
			 * Bad link count implies someone's linked our
			 * target to something else, which we probably
			 * shouldn't step on.
			 */
			if (post_lstat.st_nlink != 1) {
				errtext = gettext(
	    "Unexpected condition detected: %s must have exactly one link\n");
				(void) fprintf(stderr, errtext, filename);
				syslog(LOG_WARNING, errtext, filename);
				(void) close(fd);
				errno = EPERM;
				return (-1);
			}
			/*
			 * Root might make a file, but non-root might
			 * need to open it.  If the permissions let us
			 * get this far, then let it through.
			 */
			if (post_lstat.st_uid != getuid() &&
			    post_lstat.st_uid != 0) {
				errtext = gettext(
"Unsupported condition detected: %s must be owned by uid %ld or 0\n");
				(void) fprintf(stderr, errtext, filename,
				    (long)getuid());
				syslog(LOG_WARNING, errtext, filename,
				    (long)getuid());
				(void) close(fd);
				errno = EPERM;
				return (-1);
			}
			if (mode & (O_WRONLY|O_TRUNC)) {
				if (ftruncate(fd, (off_t)0) < 0) {
					(void) fprintf(stderr,
					    "ftruncate(%s): %s\n",
					    filename, strerror(errno));
					(void) close(fd);
					return (-1);
				}
			}
		} else {
			/*
			 * Didn't exist, but couldn't open it.
			 */
			return (-1);
		}
	} else {
		/*
		 * If truncating open succeeded for a read-only open,
		 * bail out, as we really shouldn't have succeeded.
		 */
		if (mode & O_RDONLY) {
			/* Undo the O_CREAT */
			(void) unlinkat(dfd, filename, 0);
			(void) fprintf(stderr, "open(%s): %s\n",
			    filename, strerror(ENOENT));
			(void) close(fd);
			errno = ENOENT;
			return (-1);
		}
	}

	return (fd);
}

/*
 * STDIO version of safe_open.  Equivalent to fopen64(...).
 */
FILE *
safe_fopen(const char *filename, const char *smode, int perms)
{
	int fd;
	int bmode;

	/*
	 * accepts only modes  "r", "r+", and "w"
	 */
	if (smode[0] == 'r') {
		if (smode[1] == '\0') {
			bmode = O_RDONLY;
		} else if ((smode[1] == '+') && (smode[2] == '\0')) {
			bmode = O_RDWR;
		}
	} else if ((smode[0] == 'w') && (smode[1] == '\0')) {
		bmode = O_WRONLY;
	} else {
		(void) fprintf(stderr,
		    gettext("internal error: safe_fopen: invalid mode `%s'\n"),
		    smode);
		return (NULL);
	}

	fd = safe_open(AT_FDCWD, filename, bmode, perms);

	/*
	 * caller is expected to report error.
	 */
	if (fd >= 0)
		return (fdopen(fd, smode));

	return ((FILE *)NULL);
}

/*
 * Read the contents of a directory.
 */
int
mkentry(char *name, ino_t ino, struct arglist *ap)
{
	struct afile *fp;

	if (ap->base == NULL) {
		ap->nent = 20;
		ap->base = (struct afile *)calloc((unsigned)ap->nent,
		    sizeof (*(ap->base)));
		if (ap->base == NULL) {
			(void) fprintf(stderr,
			    gettext("%s: out of memory\n"), ap->cmd);
			return (FAIL);
		}
	}
	if (ap->head == NULL)
		ap->head = ap->last = ap->base;
	fp = ap->last;
	fp->fnum = ino;
	fp->fname = savename(name);
	fp++;
	if (fp == ap->head + ap->nent) {
		ap->base = (struct afile *)realloc((char *)ap->base,
		    (size_t)(2 * ap->nent * (size_t)sizeof (*(ap->base))));
		if (ap->base == NULL) {
			(void) fprintf(stderr,
			    gettext("%s: out of memory\n"), ap->cmd);
			return (FAIL);
		}
		ap->head = ap->base;
		fp = ap->head + ap->nent;
		ap->nent *= 2;
	}
	ap->last = fp;
	return (GOOD);
}

static int gmatch(wchar_t *, wchar_t *);
static int addg(struct direct *, char *, char *, struct arglist *);

/*
 * XXX  This value is ASCII (but not language) dependent.  In
 * ASCII, it is the DEL character (unlikely to appear in paths).
 * If you are compiling on an EBCDIC-based machine, re-define
 * this (0x7f is '"') to be something like 0x7 (DEL).  It's
 * either this hack or re-write the expand() algorithm...
 */
#define	DELIMCHAR	((char)0x7f)

/*
 * Expand a file name.
 * "as" is the pattern to expand.
 * "rflg" non-zero indicates that we're recursing.
 * "ap" is where to put the results of the expansion.
 *
 * Our caller guarantees that "as" is at least the string ".".
 */
int
expand(char *as, int rflg, struct arglist *ap)
{
	int		count, size;
	char		dir = 0;
	char		*rescan = 0;
	RST_DIR		*dirp;
	char		*s, *cs;
	int		sindex, rindexa, lindex;
	struct direct	*dp;
	char		slash;
	char		*rs;
	char		c;
	wchar_t		w_fname[PATH_MAX+1];
	wchar_t		w_pname[PATH_MAX+1];

	/*
	 * check for meta chars
	 */
	s = cs = as;
	slash = 0;
	while (*cs != '*' && *cs != '?' && *cs != '[') {
		if (*cs++ == 0) {
			if (rflg && slash)
				break;
			else
				return (0);
		} else if (*cs == '/') {
			slash++;
		}
	}
	for (;;) {
		if (cs == s) {
			s = "";
			break;
		} else if (*--cs == '/') {
			*cs = 0;
			if (s == cs)
				s = "/";
			break;
		}
	}
	if ((dirp = rst_opendir(s)) != NULL)
		dir++;
	count = 0;
	if (*cs == 0)
		*cs++ = DELIMCHAR;
	if (dir) {
		/*
		 * check for rescan
		 */
		rs = cs;
		do {
			if (*rs == '/') {
				rescan = rs;
				*rs = 0;
			}
		} while (*rs++);
		/* LINTED: result fits into an int */
		sindex = (int)(ap->last - ap->head);
		(void) mbstowcs(w_pname, cs, PATH_MAX);
		w_pname[PATH_MAX - 1] = 0;
		while ((dp = rst_readdir(dirp)) != NULL && dp->d_ino != 0) {
			if (!dflag && BIT(dp->d_ino, dumpmap) == 0)
				continue;
			if ((*dp->d_name == '.' && *cs != '.'))
				continue;
			(void) mbstowcs(w_fname, dp->d_name, PATH_MAX);
			w_fname[PATH_MAX - 1] = 0;
			if (gmatch(w_fname, w_pname)) {
				if (addg(dp, s, rescan, ap) < 0) {
					rst_closedir(dirp);
					return (-1);
				}
				count++;
			}
		}
		if (rescan) {
			rindexa = sindex;
			/* LINTED: result fits into an int */
			lindex = (int)(ap->last - ap->head);
			if (count) {
				count = 0;
				while (rindexa < lindex) {
					size = expand(ap->head[rindexa].fname,
					    1, ap);
					if (size < 0) {
						rst_closedir(dirp);
						return (size);
					}
					count += size;
					rindexa++;
				}
			}
			/* LINTED: lint is confused about pointer size/type */
			bcopy((void *)(&ap->head[lindex]),
			    (void *)(&ap->head[sindex]),
			    (size_t)((ap->last - &ap->head[rindexa])) *
			    sizeof (*ap->head));
			ap->last -= lindex - sindex;
			*rescan = '/';
		}
		rst_closedir(dirp);
	}
	s = as;
	while ((c = *s) != '\0')
		*s++ = (c != DELIMCHAR ? c : '/');

	return (count);
}

/*
 * Check for a name match
 */
static int
gmatch(wchar_t *s, wchar_t *p)
{
	long	scc;	/* source character to text */
	wchar_t	c;	/* pattern character to match */
	char	ok;	/* [x-y] range match status */
	long	lc;	/* left character of [x-y] range */

	scc = *s++;
	switch (c = *p++) {

	case '[':
		ok = 0;
		lc = -1;
		while (c = *p++) {
			if (c == ']') {
				return (ok ? gmatch(s, p) : 0);
			} else if (c == '-') {
				wchar_t rc = *p++;
				/*
				 * Check both ends must belong to
				 * the same codeset.
				 */
				if (wcsetno(lc) != wcsetno(rc)) {
					/*
					 * If not, ignore the '-'
					 * operator and [x-y] is
					 * treated as if it were
					 * [xy].
					 */
					if (scc == lc)
						ok++;
					if (scc == (lc = rc))
						ok++;
				} else if (lc <= scc && scc <= rc)
					ok++;
			} else {
				lc = c;
				if (scc == lc)
					ok++;
			}
		}
		/* No closing bracket => failure */
		return (0);

	default:
		if (c != scc)
			return (0);
		/*FALLTHROUGH*/

	case '?':
		return (scc ? gmatch(s, p) : 0);

	case '*':
		if (*p == 0)
			return (1);
		s--;
		while (*s) {
			if (gmatch(s++, p))
				return (1);
		}
		return (0);

	case 0:
		return (scc == 0);
	}
}

/*
 * Construct a matched name.
 */
static int
addg(struct direct *dp, char *as1, char *as3, struct arglist *ap)
{
	char	*s1, *s2, *limit;
	int	c;
	char	buf[MAXPATHLEN + 1];

	s2 = buf;
	limit = buf + sizeof (buf) - 1;
	s1 = as1;
	while ((c = *s1++) != '\0' && s2 < limit) {
		if (c == DELIMCHAR) {
			*s2++ = '/';
			break;
		}
		/* LINTED narrowing cast */
		*s2++ = (char)c;
	}
	s1 = dp->d_name;
	while ((*s2 = *s1++) != '\0' && s2 < limit)
		s2++;
	s1 = as3;
	if (s1 != NULL && s2 < limit) {
		*s2++ = '/';

		while ((*s2++ = *++s1) != '\0' && s2 < limit) {
			continue;
			/*LINTED [empty loop body]*/
		}
	}
	*s2 = '\0';
	if (mkentry(buf, dp->d_ino, ap) == FAIL)
		return (-1);
	return (0);
}


/*
 * Resolve a "complex" pathname (as generated by myname()) into
 * a file descriptor and a relative path.  The file descriptor
 * will reference the hidden directory containing the attribute
 * named by the relative path.  If the provided path is not
 * complex, the returned file descriptor will be AT_FDCWD and rpath
 * will equal path.
 *
 * This function is intended to be used to transform a complex
 * pathname into a pair of handles that can be used to actually
 * manipulate the named file.  Since extended attributes have
 * an independant name space, a file descriptor for a directory
 * in the attribute name space is necessary to actually manipulate
 * the attribute file (via the path-relative xxxat() system calls
 * or a call to fchdir()).
 *
 * In the event of an error, the returned file descriptor will be
 * -1.  It is expected that callers will either check for this
 * condition directly, or attempt to use the descriptor, fail, and
 * generate an appropriate context-specific error message.
 *
 * This function is pretty much a no-op for "simple" (non-attribute)
 * paths.
 */
void
resolve(char *path, int *fd, char **rpath)
{
	int	tfd;

	*fd = tfd = AT_FDCWD;
	*rpath = path;
	path = *rpath + strlen(*rpath) +1;
	while (*path != '\0' &&
	    (*fd = openat64(tfd, *rpath, O_RDONLY)) > 0) {
		if (tfd != AT_FDCWD)
			(void) close(tfd);
		tfd = *fd;
		*rpath = path;
		path = *rpath + strlen(*rpath) +1;
	}
	if (*fd == AT_FDCWD)
		return;
	if (*fd < 0 || (*fd = openat64(tfd, ".", O_RDONLY|O_XATTR)) < 0) {
		int saverr = errno;
		(void) fprintf(stderr,
		    gettext("Warning: cannot fully resolve %s: %s"),
		    path, strerror(saverr));
		(void) fflush(stderr);
	}
	if (tfd != AT_FDCWD) (void) close(tfd);
}

/*
 * Copy a complex pathname to another string.  Note that the
 * length returned by this function is the number of characters
 * up to (but not including) the final NULL.
 */
int
complexcpy(char *s1, char *s2, int max)
{
	int	nullseen = 0;
	int	len = 0;

	while (len++ < max) {
		*s1++ = *s2;
		if (*s2++ == '\0') {
			if (nullseen)
				return (len-1);
			else
				nullseen = 1;
		} else {
			nullseen = 0;
		}
	}
	*s1 = '\0';
	if (nullseen == 0)
		*--s1 = '\0';
	fprintf(stderr,
	    gettext("Warning: unterminated source string in complexcpy\n"));
	return (max-1);
}