|
root / base / usr / src / cmd / bart
bart Plain Text 2934 lines 73.6 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
#
# 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.
#
# Copyright (c) 2018, Joyent, Inc.

PROG=	bart
SRCS=	rules.c create.c compare.c main.c lutbl.c
OBJS=	rules.o create.o compare.o main.o lutbl.o
BART=	bart

include ../Makefile.cmd

LDLIBS +=	-lsec -lmd
CERRWARN +=	-Wno-parentheses
CERRWARN +=	$(CNOWARN_UNINIT)

SMOFF +=	indenting

#
# for messaging catalog
#

POFILE=	bart.po
DCFILE=	bart.dc

ROOTBINBART=	$(BART:%=$(ROOTBIN)/%)
ROOTLIBDIFFH=	$(DIFFH:%=$(ROOTLIB)/%)

.KEEP_STATE:

all:	$(PROG)

$(PROG):	$(OBJS)
	$(LINK.c) -o $(PROG) $(OBJS) $(LDLIBS)
	$(POST_PROCESS)

clean:
	$(RM) $(OBJS)


#
# Use private rule
#
$(POFILE):
	$(RM) $@
	$(COMPILE.cpp)   $(SRCS) > bart.po.i
	$(XGETTEXT) $(XGETFLAGS) bart.po.i
	sed "/^domain/d"        messages.po     > $@
	$(RM)  bart.po.i messages.po

$(DCFILE):
	$(RM) $@
	$(COMPILE.cpp)   $(SRCS) > bart.dc.i
	$(XGETTEXT) -c TRANSLATION_NOTE_FOR_DC -t bart.dc.i
	sed "/^domain/d"        messages.po     > $@
	$(RM)  bart.dc.i messages.po

install:	all $(ROOTBINBART) $(ROOTLIBDIFFH)

clean:

lint:	lint_SRCS

include ../Makefile.targ
/*
 * 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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#ifndef	_BART_H
#define	_BART_H

#ifdef	__cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
#include <sys/stat.h>
#include <strings.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <md5.h>
#include <ftw.h>
#include <libintl.h>
#include "msgs.h"

#define	EXIT		0
#define	WARNING_EXIT	1
#define	FATAL_EXIT	2

#define	NO_EXCLUDE	0
#define	EXCLUDE_SKIP	1
#define	EXCLUDE_PRUNE	2

#define	CHECK		0
#define	NOCHECK		1

#define	CHECK_KEYWORD(s)	(strcmp(s, "CHECK") == 0)
#define	IGNORE_KEYWORD(s)	(strcmp(s, "IGNORE") == 0)

#define	ALL_KEYWORD		"all"
#define	CONTENTS_KEYWORD	"contents"
#define	TYPE_KEYWORD		"type"
#define	SIZE_KEYWORD		"size"
#define	MODE_KEYWORD		"mode"
#define	ACL_KEYWORD		"acl"
#define	UID_KEYWORD		"uid"
#define	GID_KEYWORD		"gid"
#define	MTIME_KEYWORD		"mtime"
#define	LNMTIME_KEYWORD		"lnmtime"
#define	DIRMTIME_KEYWORD	"dirmtime"
#define	DEST_KEYWORD		"dest"
#define	DEVNODE_KEYWORD		"devnode"
#define	ADD_KEYWORD		"add"
#define	DELETE_KEYWORD		"delete"

#define	MANIFEST_VER	"! Version 1.0\n"
#define	FORMAT_STR	"# Format:\n\
#fname D size mode acl dirmtime uid gid\n\
#fname P size mode acl mtime uid gid\n\
#fname S size mode acl mtime uid gid\n\
#fname F size mode acl mtime uid gid contents\n\
#fname L size mode acl lnmtime uid gid dest\n\
#fname B size mode acl mtime uid gid devnode\n\
#fname C size mode acl mtime uid gid devnode\n"

/*
 * size of buffer - used in several places
 */
#define	BUF_SIZE	65536

/*
 * size of ACL buffer - used in several places
 */
#define	ACL_SIZE	1024

/*
 * size of MISC buffer - used in several places
 */
#define	MISC_SIZE	20

/*
 * size of TYPE buffer - used in several places
 */
#define	TYPE_SIZE	2

struct tree_modifier {
	char			*mod_str;
	boolean_t		include;
	boolean_t		is_dir;
	struct tree_modifier	*next;
};

struct attr_keyword {
	char    *ak_name;
	int	ak_flags;
};


#define	ATTR_ALL ((uint_t)~0)
#define	ATTR_CONTENTS 0x0001
#define	ATTR_TYPE 0x0002
#define	ATTR_SIZE 0x0004
#define	ATTR_MODE 0x0008
#define	ATTR_UID 0x0010
#define	ATTR_GID 0x0020
#define	ATTR_ACL 0x0040
#define	ATTR_DEST 0x0080
#define	ATTR_DEVNODE 0x0100
#define	ATTR_MTIME 0x0200
#define	ATTR_LNMTIME 0x0400
#define	ATTR_DIRMTIME 0x0800
#define	ATTR_ADD 0x1000
#define	ATTR_DELETE 0x2000

struct rule {
	char			subtree[PATH_MAX];
	uint_t			attr_list;
	struct tree_modifier	*modifiers;
	struct rule		*next;
	struct rule		*prev;
};

struct dir_component {
	char			dirname[PATH_MAX];
	struct dir_component	*next;
};


struct attr_keyword *attr_keylookup(char *);
void usage(void);
int bart_create(int, char **);
int bart_compare(int, char **);
struct rule *check_rules(const char *, char);
int exclude_fname(const char *, char, struct rule *);
struct rule *get_first_subtree(void);
struct rule *get_next_subtree(struct rule *);
void process_glob_ignores(char *, uint_t *);
void *safe_calloc(size_t);
char *safe_strdup(char *);
int read_rules(FILE *, char *, uint_t, int);
int read_line(FILE *, char *, int, int, char **, char *);
#ifdef	__cplusplus
}
#endif

#endif	/* _BART_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 2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <unistd.h>
#include "bart.h"

static int compare_manifests(FILE *rulesfile, char *control, char *test,
    boolean_t prog_fmt, uint_t flags);
static void extract_fname_ftype(char *line, char *fname, char *type);
static int report_add(char *fname, char *type);
static int report_delete(char *fname, char *type);
static int evaluate_differences(char *control_line, char *test_line,
    boolean_t prog_fmt, int flags);
static void report_error(char *fname, char *type, char *ctrl_val,
    char *test_val, boolean_t prog_fmt);
static int read_manifest_line(FILE *fd, char *buf, int buf_size, int start_pos,
    char **line, char *fname);
static void parse_line(char *line, char *fname, char *type, char *size,
    char *mode, char *acl, char *mtime, char *uid, char *gid, char *contents,
    char *devnode, char *dest);
static void init_default_flags(uint_t *flags);
static void get_token(char *line, int *curr_pos, int line_len, char *buf,
    int buf_size);

int
bart_compare(int argc, char **argv)
{
	char			*control_fname, *test_fname;
	int			c;
	FILE			*rules_fd = NULL;
	uint_t			glob_flags;
	boolean_t		prog_fmt = B_FALSE;

	init_default_flags(&glob_flags);

	while ((c = getopt(argc, argv, "pr:i:")) != EOF) {
		switch (c) {
		case 'p':
			prog_fmt = B_TRUE;
			break;

		case 'r':
			if (optarg == NULL)
				usage();

			if (strcmp(optarg, "-") == 0)
				rules_fd = stdin;
			else
				rules_fd = fopen(optarg, "r");
			if (rules_fd == NULL) {
				perror(optarg);
				usage();
			}
			break;

		case 'i':
			process_glob_ignores(optarg, &glob_flags);
			break;

		case '?':
		default:
			usage();
		}
	}

	/* Make sure we have the right number of args */
	if ((optind + 2) != argc)
		usage();
	argv += optind;
	control_fname = argv[0];
	test_fname = argv[1];
	/* At this point, the filenames are sane, so do the comparison */
	return (compare_manifests(rules_fd, control_fname, test_fname,
	    prog_fmt, glob_flags));
}

static int
compare_manifests(FILE *rulesfile, char *control, char *test,
    boolean_t prog_fmt, uint_t flags)
{
	FILE	*control_fd, *test_fd;
	char	*control_line, *test_line, control_buf[BUF_SIZE],
	    test_buf[BUF_SIZE], control_fname[PATH_MAX],
	    control_type[TYPE_SIZE], test_fname[PATH_MAX],
	    test_type[TYPE_SIZE];
	int	control_pos, test_pos, ret, fname_cmp, return_status;

	return_status = EXIT;

	return_status = read_rules(rulesfile, "", flags, 0);

	control_fd = fopen(control, "r");
	if (control_fd == NULL) {
		perror(control);
		return (FATAL_EXIT);
	}

	test_fd = fopen(test, "r");
	if (test_fd == NULL) {
		perror(test);
		return (FATAL_EXIT);
	}

	control_pos = read_manifest_line(control_fd, control_buf,
	    BUF_SIZE, 0, &control_line, control);
	test_pos = read_manifest_line(test_fd, test_buf, BUF_SIZE, 0,
	    &test_line, test);

	while ((control_pos != -1) && (test_pos != -1)) {
		ret = strcmp(control_line, test_line);
		if (ret == 0) {
			/* Lines compare OK, just read the next lines.... */
			control_pos = read_manifest_line(control_fd,
			    control_buf, BUF_SIZE, control_pos, &control_line,
			    control);
			test_pos = read_manifest_line(test_fd, test_buf,
			    BUF_SIZE, test_pos, &test_line, test);
			continue;
		}

		/*
		 * Something didn't compare properly.
		 */
		extract_fname_ftype(control_line, control_fname, control_type);
		extract_fname_ftype(test_line, test_fname, test_type);
		fname_cmp = strcmp(control_fname, test_fname);

		if (fname_cmp == 0) {
			/*
			 * Filenames were the same, see what was
			 * different and continue.
			 */
			if (evaluate_differences(control_line, test_line,
			    prog_fmt, flags) != 0)
				return_status = WARNING_EXIT;

			control_pos = read_manifest_line(control_fd,
			    control_buf, BUF_SIZE, control_pos, &control_line,
			    control);
			test_pos = read_manifest_line(test_fd, test_buf,
			    BUF_SIZE, test_pos, &test_line, test);
		} else if (fname_cmp > 0) {
			/* Filenames were different, a files was ADDED */
			if (report_add(test_fname, test_type)) {
				report_error(test_fname, ADD_KEYWORD, NULL,
				    NULL, prog_fmt);
				return_status = WARNING_EXIT;
			}
			test_pos = read_manifest_line(test_fd, test_buf,
			    BUF_SIZE, test_pos, &test_line, test);
		} else if (fname_cmp < 0) {
			/* Filenames were different, a files was DELETED */
			if (report_delete(control_fname, control_type)) {
				report_error(control_fname, DELETE_KEYWORD,
				    NULL, NULL, prog_fmt);
				return_status = WARNING_EXIT;
			}
			control_pos = read_manifest_line(control_fd,
			    control_buf, BUF_SIZE, control_pos, &control_line,
			    control);
		}
	}

	/*
	 * Entering this while loop means files were DELETED from the test
	 * manifest.
	 */
	while (control_pos != -1) {
		(void) sscanf(control_line, "%1023s", control_fname);
		if (report_delete(control_fname, control_type)) {
			report_error(control_fname, DELETE_KEYWORD, NULL,
			    NULL, prog_fmt);
			return_status = WARNING_EXIT;
		}
		control_pos = read_manifest_line(control_fd, control_buf,
		    BUF_SIZE, control_pos, &control_line, control);
	}

	/*
	 * Entering this while loop means files were ADDED to the test
	 * manifest.
	 */
	while (test_pos != -1) {
		(void) sscanf(test_line, "%1023s", test_fname);
		if (report_add(test_fname, test_type)) {
			report_error(test_fname, ADD_KEYWORD, NULL,
			    NULL, prog_fmt);
			return_status = WARNING_EXIT;
		}
		test_pos = read_manifest_line(test_fd, test_buf,
		    BUF_SIZE, test_pos, &test_line, test);
	}

	(void) fclose(control_fd);
	(void) fclose(test_fd);

	/* For programmatic mode, add a newline for cosmetic reasons */
	if (prog_fmt && (return_status != 0))
		(void) printf("\n");

	return (return_status);
}

static void
parse_line(char *line, char *fname, char *type, char *size, char *mode,
    char *acl, char *mtime, char *uid, char *gid, char *contents, char *devnode,
    char *dest)
{
	int		pos, line_len;

	line_len = strlen(line);
	pos = 0;

	get_token(line, &pos, line_len, fname, PATH_MAX);
	get_token(line, &pos, line_len, type, TYPE_SIZE);
	get_token(line, &pos, line_len, size, MISC_SIZE);
	get_token(line, &pos, line_len, mode, MISC_SIZE);
	get_token(line, &pos, line_len, acl, ACL_SIZE);
	get_token(line, &pos, line_len, mtime, MISC_SIZE);
	get_token(line, &pos, line_len, uid, MISC_SIZE);
	get_token(line, &pos, line_len, gid, MISC_SIZE);

	/* Reset these fields... */

	*contents = '\0';
	*devnode = '\0';
	*dest = '\0';

	/* Handle filetypes which have a last field..... */
	if (type[0] == 'F')
		get_token(line, &pos, line_len, contents, PATH_MAX);
	else if ((type[0] == 'B') || (type[0] == 'C'))
		get_token(line, &pos, line_len, devnode, PATH_MAX);
	else if (type[0] == 'L')
		get_token(line, &pos, line_len, dest, PATH_MAX);
}

static void
get_token(char *line, int *curr_pos, int line_len, char *buf, int buf_size)
{
	int	cnt = 0;

	while (isspace(line[*curr_pos]) && (*curr_pos < line_len))
		(*curr_pos)++;

	while (!isspace(line[*curr_pos]) &&
	    (*curr_pos < line_len) && (cnt < (buf_size-1))) {
		buf[cnt] = line[*curr_pos];
		(*curr_pos)++;
		cnt++;
	}
	buf[cnt] = '\0';
}

/*
 * Utility function: extract fname and type from this line
 */
static void
extract_fname_ftype(char *line, char *fname, char *type)
{
	int		line_len, pos;

	pos = 0;
	line_len = strlen(line);

	get_token(line, &pos, line_len, fname, PATH_MAX);
	get_token(line, &pos, line_len, type, TYPE_SIZE);
}

/*
 * Utility function: tells us whether or not this addition should be reported
 *
 * Returns 0 if the discrepancy is ignored, non-zero if the discrepancy is
 * reported.
 */
static int
report_add(char *fname, char *type)
{
	struct rule	*rule_ptr;

	rule_ptr = check_rules(fname, type[0]);
	if ((rule_ptr != NULL) && (rule_ptr->attr_list & ATTR_ADD))
		return (1);
	else
		return (0);
}

/*
 * Utility function: tells us whether or not this deletion should be reported
 *
 * Returns 0 if the discrepancy is ignored, non-zero if the discrepancy is
 * reported.
 */
static int
report_delete(char *fname, char *type)
{
	struct rule	*rule_ptr;

	rule_ptr = check_rules(fname, type[0]);

	if ((rule_ptr != NULL) && (rule_ptr->attr_list & ATTR_DELETE))
		return (1);
	else
		return (0);
}

/*
 * This function takes in the two entries, which have been flagged as
 * different, breaks them up and reports discrepancies.  Note, discrepancies
 * are affected by the 'CHECK' and 'IGNORE' stanzas which may apply to
 * these entries.
 *
 * Returns the number of discrepancies reported.
 */
static int
evaluate_differences(char *control_line, char *test_line,
    boolean_t prog_fmt, int flags)
{
	char		ctrl_fname[PATH_MAX], test_fname[PATH_MAX],
	    ctrl_type[TYPE_SIZE], test_type[TYPE_SIZE],
	    ctrl_size[MISC_SIZE], ctrl_mode[MISC_SIZE],
	    ctrl_acl[ACL_SIZE], ctrl_mtime[MISC_SIZE],
	    ctrl_uid[MISC_SIZE], ctrl_gid[MISC_SIZE],
	    ctrl_dest[PATH_MAX], ctrl_contents[PATH_MAX],
	    ctrl_devnode[PATH_MAX], test_size[MISC_SIZE],
	    test_mode[MISC_SIZE], test_acl[ACL_SIZE],
	    test_mtime[MISC_SIZE], test_uid[MISC_SIZE],
	    test_gid[MISC_SIZE], test_dest[PATH_MAX],
	    test_contents[PATH_MAX], test_devnode[PATH_MAX],
	    *tag;
	int		ret_val;
	struct rule	*rule_ptr;

	ret_val = 0;

	parse_line(control_line, ctrl_fname, ctrl_type, ctrl_size, ctrl_mode,
	    ctrl_acl, ctrl_mtime, ctrl_uid, ctrl_gid, ctrl_contents,
	    ctrl_devnode, ctrl_dest);

	/*
	 * Now we know the fname and type, let's get the rule that matches this
	 * manifest entry.  If there is a match, make sure to setup the
	 * correct reporting flags.
	 */
	rule_ptr = check_rules(ctrl_fname, ctrl_type[0]);
	if (rule_ptr != NULL)
		flags = rule_ptr->attr_list;

	parse_line(test_line, test_fname, test_type, test_size, test_mode,
	    test_acl, test_mtime, test_uid, test_gid, test_contents,
	    test_devnode, test_dest);

	/*
	 * Report the errors based upon which keywords have been set by
	 * the user.
	 */
	if ((flags & ATTR_TYPE) && (ctrl_type[0] != test_type[0])) {
		report_error(ctrl_fname, TYPE_KEYWORD, ctrl_type,
		    test_type, prog_fmt);
		ret_val++;
	}

	if ((flags & ATTR_SIZE) && (strcmp(ctrl_size, test_size) != 0)) {
		report_error(ctrl_fname, SIZE_KEYWORD, ctrl_size,
		    test_size, prog_fmt);
		ret_val++;
	}

	if ((flags & ATTR_MODE) && (strcmp(ctrl_mode, test_mode) != 0)) {
		report_error(ctrl_fname, MODE_KEYWORD, ctrl_mode,
		    test_mode, prog_fmt);
		ret_val++;
	}

	if ((flags & ATTR_ACL) && (strcmp(ctrl_acl, test_acl) != 0)) {
		report_error(ctrl_fname, ACL_KEYWORD, ctrl_acl,
		    test_acl, prog_fmt);
		ret_val++;
	}

	if ((flags & ATTR_MTIME) && (ctrl_type[0] == test_type[0])) {
		if (strcmp(ctrl_mtime, test_mtime) != 0) {
			switch (ctrl_type[0]) {
			case 'D':
				tag = "dirmtime";
				break;
			case 'L':
				tag = "lnmtime";
				break;
			default:
				tag = "mtime";
				break;
			}
			if (flags == 0) {
				report_error(ctrl_fname, tag, ctrl_mtime,
				    test_mtime, prog_fmt);
			ret_val++;
		}
	}

	if ((ctrl_type[0] == 'F') && (flags & ATTR_MTIME) &&
	    (strcmp(ctrl_mtime, test_mtime) != 0)) {
		report_error(ctrl_fname, MTIME_KEYWORD, ctrl_mtime, test_mtime,
		    prog_fmt);
		ret_val++;
	}

	if ((ctrl_type[0] == 'D') && (flags & ATTR_DIRMTIME) &&
	    (strcmp(ctrl_mtime, test_mtime) != 0)) {
		report_error(ctrl_fname, DIRMTIME_KEYWORD, ctrl_mtime,
		    test_mtime, prog_fmt);
		ret_val++;
	}

	if ((ctrl_type[0] == 'L') && (flags & ATTR_LNMTIME) &&
	    (strcmp(ctrl_mtime, test_mtime) != 0)) {
		report_error(ctrl_fname, LNMTIME_KEYWORD, ctrl_mtime,
		    test_mtime, prog_fmt);
		ret_val++;
	}
	} else if ((flags & ATTR_MTIME) &&
	    (strcmp(ctrl_mtime, test_mtime) != 0)) {
		report_error(ctrl_fname, MTIME_KEYWORD, ctrl_mtime,
		    test_mtime, prog_fmt);
		ret_val++;
	}

	if ((flags & ATTR_UID) && (strcmp(ctrl_uid, test_uid) != 0)) {
		report_error(ctrl_fname, UID_KEYWORD, ctrl_uid,
		    test_uid, prog_fmt);
		ret_val++;
	}

	if ((flags & ATTR_GID) && (strcmp(ctrl_gid, test_gid) != 0)) {
		report_error(ctrl_fname, GID_KEYWORD, ctrl_gid,
		    test_gid, prog_fmt);
		ret_val++;
	}

	if ((flags & ATTR_DEVNODE) &&
	    (strcmp(ctrl_devnode, test_devnode) != 0)) {
		report_error(ctrl_fname, DEVNODE_KEYWORD, ctrl_devnode,
		    test_devnode, prog_fmt);
		ret_val++;
	}

	if ((flags & ATTR_DEST) && (strcmp(ctrl_dest, test_dest) != 0)) {
		report_error(ctrl_fname, DEST_KEYWORD, ctrl_dest,
		    test_dest, prog_fmt);
		ret_val++;
	}

	if ((flags & ATTR_CONTENTS) &&
	    (strcmp(ctrl_contents, test_contents)) != 0) {
		report_error(ctrl_fname, CONTENTS_KEYWORD, ctrl_contents,
		    test_contents, prog_fmt);
		ret_val++;
	}

	return (ret_val);
}

/*
 * Function responsible for reporting errors.
 */
static void
report_error(char *fname, char *type, char *ctrl_val, char *test_val,
    boolean_t prog_fmt)
{
	static char	last_fname[PATH_MAX] = "";

	if (!prog_fmt) {
		/* Verbose mode */
		if (strcmp(fname, last_fname) != 0) {
			(void) printf("%s:\n", fname);
			(void) strlcpy(last_fname, fname, sizeof (last_fname));
		}

		if (strcmp(type, ADD_KEYWORD) == 0 ||
		    strcmp(type, DELETE_KEYWORD) == 0)
			(void) printf("  %s\n", type);
		else
			(void) printf("  %s  control:%s  test:%s\n", type,
			    ctrl_val, test_val);
	} else {
		/* Programmatic mode */
		if (strcmp(fname, last_fname) != 0) {
			/* Ensure a line is not printed for the initial case */
			if (strlen(last_fname) != 0)
				(void) printf("\n");
			(void) strlcpy(last_fname, fname, sizeof (last_fname));
			(void) printf("%s ", fname);
		}

		(void) printf("%s ", type);
		if (strcmp(type, ADD_KEYWORD) != 0 &&
		    strcmp(type, DELETE_KEYWORD) != 0) {
			(void) printf("%s ", ctrl_val);
			(void) printf("%s ", test_val);
		}
	}
}

/*
 * Function responsible for reading in a line from the manifest.
 * Takes in the file ptr and a buffer, parses the buffer  and sets the 'line'
 * ptr correctly.  In the case when the buffer is fully parsed, this function
 * reads more data from the file ptr and refills the buffer.
 */
static int
read_manifest_line(FILE *fd, char *buf, int buf_size, int start_pos,
    char **line, char *fname)
{
	int	end_pos, len, iscomment = 0, filepos;

	/*
	 * Initialization case: make sure the manifest version is OK
	 */
	if (start_pos == 0) {
		end_pos = 0;
		buf[0] = '\0';
		filepos = ftell(fd);
		(void) fread((void *) buf, (size_t)buf_size, (size_t)1, fd);

		*line = buf;

		if (filepos == 0) {
			if (strncmp(buf, MANIFEST_VER,
			    strlen(MANIFEST_VER)) != 0)
				(void) fprintf(stderr, MISSING_VER, fname);
			if ((*line[0] == '!') || (*line[0] == '#'))
				iscomment++;

			while (iscomment) {
				while ((buf[end_pos] != '\n') &&
				    (buf[end_pos] != '\0') &&
				    (end_pos < buf_size))
					end_pos++;

				if (end_pos >= buf_size)
					return (-1);

				end_pos++;
				*line = &(buf[end_pos]);
				iscomment = 0;
				if ((*line[0] == '!') || (*line[0] == '#'))
					iscomment++;
			}
		}

		while ((buf[end_pos] != '\n') && (buf[end_pos] != '\0') &&
		    (end_pos < buf_size))
			end_pos++;

		if (end_pos < buf_size) {
			if (buf[end_pos] == '\n') {
				buf[end_pos] = '\0';
				return (end_pos);
			}

			if (buf[end_pos] == '\0')
				return (-1);
		}

		(void) fprintf(stderr, MANIFEST_ERR);
		exit(FATAL_EXIT);
	}

	end_pos = (start_pos+1);
	*line = &(buf[end_pos]);

	/* Read the buffer until EOL or the buffer is empty */
	while ((buf[end_pos] != '\n') && (buf[end_pos] != '\0') &&
	    (end_pos < buf_size))
		end_pos++;

	if (end_pos < buf_size) {
		/* Found the end of the line, normal exit */
		if (buf[end_pos] == '\n') {
			buf[end_pos] = '\0';
			return (end_pos);
		}

		/* No more input to read */
		if (buf[end_pos] == '\0')
			return (-1);
	}

	/*
	 * The following code takes the remainder of the buffer and
	 * puts it at the beginning.  The space after the remainder, which
	 * is now at the beginning, is blanked.
	 * At this point, read in more data and continue to find the EOL....
	 */
	len = end_pos - (start_pos + 1);
	(void) memcpy(buf, &(buf[start_pos+1]), (size_t)len);
	(void) memset(&buf[len], '\0', (buf_size - len));
	(void) fread((void *) &buf[len], (size_t)(buf_size-len), (size_t)1, fd);
	*line = buf;
	end_pos = len;

	/* Read the buffer until EOL or the buffer is empty */
	while ((buf[end_pos] != '\n') && (buf[end_pos] != '\0') &&
	    (end_pos < buf_size))
		end_pos++;

	if (end_pos < buf_size) {
		/* Found the end of the line, normal exit */
		if (buf[end_pos] == '\n') {
			buf[end_pos] = '\0';
			return (end_pos);
		}

		/* No more input to read */
		if (buf[end_pos] == '\0')
			return (-1);
	}

	(void) fprintf(stderr, MANIFEST_ERR);
	exit(FATAL_EXIT);

	/* NOTREACHED */
}

static void
init_default_flags(uint_t *flags)
{
	/* Default behavior: everything is checked *except* dirmtime */
	*flags = ATTR_ALL & ~(ATTR_DIRMTIME);
}
/*
 * 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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <signal.h>
#include <unistd.h>
#include <sys/acl.h>
#include <sys/statvfs.h>
#include <sys/wait.h>
#include "bart.h"
#include <aclutils.h>

static int	sanitize_reloc_root(char *root, size_t bufsize);
static int	create_manifest_filelist(char **argv, char *reloc_root);
static int	create_manifest_rule(char *reloc_root, FILE *rule_fp);
static void	output_manifest(void);
static int	eval_file(const char *fname, const struct stat64 *statb,
	struct FTW *ftwx);
static char	*sanitized_fname(const char *, boolean_t);
static char	*get_acl_string(const char *fname, const struct stat64 *statb,
    int *err_code);
static int	generate_hash(int fdin, char *hash_str);
static int	read_filelist(char *reloc_root, char **argv, char *buf,
    size_t bufsize);
static int	walker(const char *name, const struct stat64 *sp,
    int type, struct FTW *ftwx);

/*
 * The following globals are necessary due to the "walker" function
 * provided by nftw().  Since there is no way to pass them through to the
 * walker function, they must be global.
 */
static int		compute_chksum = 1, eval_err = 0;
static struct rule	*subtree_root;
static char		reloc_root[PATH_MAX];
static struct statvfs64	parent_vfs;

int
bart_create(int argc, char **argv)
{
	boolean_t	filelist_input;
	int		ret, c, output_pipe[2];
	FILE 		*rules_fd = NULL;
	pid_t		pid;

	filelist_input = B_FALSE;
	reloc_root[0] = '\0';

	while ((c = getopt(argc, argv, "Inr:R:")) != EOF) {
		switch (c) {
		case 'I':
			if (rules_fd != NULL) {
				(void) fprintf(stderr, "%s", INPUT_ERR);
				usage();
			}
			filelist_input = B_TRUE;
			break;

		case 'n':
			compute_chksum = 0;
			break;

		case 'r':
			if (strcmp(optarg, "-") == 0)
				rules_fd = stdin;
			else
				rules_fd = fopen(optarg, "r");
			if (rules_fd == NULL) {
				perror(optarg);
				usage();
			}
			break;

		case 'R':
			(void) strlcpy(reloc_root, optarg, sizeof (reloc_root));
			ret = sanitize_reloc_root(reloc_root,
			    sizeof (reloc_root));
			if (ret == 0)
				usage();
			break;

		case '?':
		default :
			usage();
		}
	}
	argv += optind;

	if (pipe(output_pipe) < 0) {
		perror("");
		exit(FATAL_EXIT);
	}

	pid = fork();
	if (pid < 0) {
		perror(NULL);
		exit(FATAL_EXIT);
	}

	/*
	 * Break the creation of a manifest into two parts: the parent process
	 * generated the data whereas the child process sorts the data.
	 *
	 * The processes communicate through the pipe.
	 */
	if (pid > 0) {
		/*
		 * Redirect the stdout of this process so it goes into
		 * output_pipe[0].  The output of this process will be read
		 * by the child, which will sort the output.
		 */
		if (dup2(output_pipe[0], STDOUT_FILENO) != STDOUT_FILENO) {
			perror(NULL);
			exit(FATAL_EXIT);
		}
		(void) close(output_pipe[0]);
		(void) close(output_pipe[1]);

		if (filelist_input == B_TRUE) {
			ret = create_manifest_filelist(argv, reloc_root);
		} else {
			ret = create_manifest_rule(reloc_root, rules_fd);
		}

		/* Close stdout so the sort in the child proc will complete */
		(void) fclose(stdout);
	} else {
		/*
		 * Redirect the stdin of this process so its read in from
		 * the pipe, which is the parent process in this case.
		 */
		if (dup2(output_pipe[1], STDIN_FILENO) != STDIN_FILENO) {
			perror(NULL);
			exit(FATAL_EXIT);
		}
		(void) close(output_pipe[0]);

		output_manifest();
	}

	/* Wait for the child proc (the sort) to complete */
	(void) wait(0);

	return (ret);
}

/*
 * Handle the -R option and sets 'root' to be the absolute path of the
 * relocatable root.  This is useful when the user specifies '-R ../../foo'.
 *
 * Return code is whether or not the location spec'd by the -R flag is a
 * directory or not.
 */
static int
sanitize_reloc_root(char *root, size_t bufsize)
{
	char		pwd[PATH_MAX];

	/*
	 * First, save the current directory and go to the location
	 * specified with the -R option.
	 */
	(void) getcwd(pwd, sizeof (pwd));
	if (chdir(root) < 0) {
		/* Failed to change directory, something is wrong.... */
		perror(root);
		return (0);
	}

	/*
	 * Save the absolute path of the relocatable root directory.
	 */
	(void) getcwd(root, bufsize);

	/*
	 * Now, go back to where we started, necessary for picking up a rules
	 * file.
	 */
	if (chdir(pwd) < 0) {
		/* Failed to change directory, something is wrong.... */
		perror(root);
		return (0);
	}

	/*
	 * Make sure the path returned does not have a trailing /. This
	 * can only happen when the entire pathname is "/".
	 */
	if (strcmp(root, "/") == 0)
		root[0] = '\0';

	/*
	 * Since the earlier chdir() succeeded, return success.
	 */
	return (1);
}

/*
 * This is the worker bee which creates the manifest based upon the command
 * line options supplied by the user.
 *
 * NOTE: create_manifest() eventually outputs data to a pipe, which is read in
 * by the child process.  The child process is running output_manifest(), which
 * is responsible for generating sorted output.
 */
static int
create_manifest_rule(char *reloc_root, FILE *rule_fp)
{
	struct rule	*root;
	int		ret_status = EXIT;
	uint_t		flags;

	if (compute_chksum)
		flags = ATTR_CONTENTS;
	else
		flags = 0;
	ret_status = read_rules(rule_fp, reloc_root, flags, 1);

	/* Loop through every single subtree */
	for (root = get_first_subtree(); root != NULL;
	    root = get_next_subtree(root)) {

		/*
		 * Check to see if this subtree should have contents
		 * checking turned on or off.
		 *
		 * NOTE: The 'compute_chksum' and 'parent_vfs'
		 * are a necessary hack: the variables are used in
		 * walker(), both directly and indirectly.  Since
		 * the parameters to walker() are defined by nftw(),
		 * the globals are really a backdoor mechanism.
		 */
		ret_status = statvfs64(root->subtree, &parent_vfs);
		if (ret_status < 0) {
			perror(root->subtree);
			continue;
		}

		/*
		 * Walk the subtree and invoke the callback function walker()
		 * Use FTW_ANYERR to get FTW_NS and FTW_DNR entries *and*
		 * to continue past those errors.
		 */
		subtree_root = root;
		(void) nftw64(root->subtree, &walker, 20, FTW_PHYS|FTW_ANYERR);

		/*
		 * Ugly but necessary:
		 *
		 * walker() must return 0, or the tree walk will stop,
		 * so warning flags must be set through a global.
		 */
		if (eval_err == WARNING_EXIT)
			ret_status = WARNING_EXIT;

	}
	return (ret_status);
}

static int
create_manifest_filelist(char **argv, char *reloc_root)
{
	int	ret_status = EXIT;
	char	input_fname[PATH_MAX];

	while (read_filelist(reloc_root, argv,
	    input_fname, sizeof (input_fname)) != -1) {

		struct stat64	stat_buf;
		int		ret;

		ret = lstat64(input_fname, &stat_buf);
		if (ret < 0) {
			ret_status = WARNING_EXIT;
			perror(input_fname);
		} else {
			ret = eval_file(input_fname, &stat_buf, NULL);

			if (ret == WARNING_EXIT)
				ret_status = WARNING_EXIT;
		}
	}

	return (ret_status);
}

/*
 * output_manifest() the child process.  It reads in the output from
 * create_manifest() and sorts it.
 */
static void
output_manifest(void)
{
	char	*env[] = {"LC_CTYPE=C", "LC_COLLATE=C", "LC_NUMERIC=C", NULL};
	time_t		time_val;
	struct tm	*tm;
	char		time_buf[1024];

	(void) printf("%s", MANIFEST_VER);
	time_val = time((time_t)0);
	tm = localtime(&time_val);
	(void) strftime(time_buf, sizeof (time_buf), "%A, %B %d, %Y (%T)", tm);
	(void) printf("! %s\n", time_buf);
	(void) printf("%s", FORMAT_STR);
	(void) fflush(stdout);
	/*
	 * Simply run sort and read from the the current stdin, which is really
	 * the output of create_manifest().
	 * Also, make sure the output is unique, since a given file may be
	 * included by several stanzas.
	 */
	if (execle("/bin/sort", "sort", "-u", NULL, env) < 0) {
		perror("");
		exit(FATAL_EXIT);
	}

	/*NOTREACHED*/
}

/*
 * Callback function for nftw()
 */
static int
walker(const char *name, const struct stat64 *sp, int type, struct FTW *ftwx)
{
	int			ret;
	struct statvfs64	path_vfs;
	boolean_t		dir_flag = B_FALSE;
	struct rule		*rule;

	switch (type) {
	case FTW_F:	/* file 		*/
		rule = check_rules(name, 'F');
		if (rule != NULL) {
			if (rule->attr_list & ATTR_CONTENTS)
				compute_chksum = 1;
			else
				compute_chksum = 0;
		}
		break;
	case FTW_SL:	/* symbolic link, FTW_PHYS	*/
	case FTW_SLN:	/* symbolic link, ~FTW_PHYS	*/
		break;
	case FTW_DP:	/* end of directory, FTW_DEPTH	*/
	case FTW_D:	/* enter directory, ~FTW_DEPTH	*/
		dir_flag = B_TRUE;
		ret = statvfs64(name, &path_vfs);
		if (ret < 0)
			eval_err = WARNING_EXIT;
		break;
	case FTW_NS:	/* unstatable file	*/
		(void) fprintf(stderr, UNKNOWN_FILE, name);
		eval_err = WARNING_EXIT;
		return (0);
	case FTW_DNR:	/* unreadable directory	*/
		(void) fprintf(stderr, CANTLIST_DIR, name);
		eval_err = WARNING_EXIT;
		return (0);
	default:
		(void) fprintf(stderr, INTERNAL_ERR, name);
		eval_err = WARNING_EXIT;
		return (0);
	}

	/* This is the function which really processes the file */
	ret = eval_file(name, sp, ftwx);

	/*
	 * Since the parameters to walker() are constrained by nftw(),
	 * need to use a global to reflect a WARNING.  Sigh.
	 */
	if (ret == WARNING_EXIT)
		eval_err = WARNING_EXIT;

	/*
	 * This is a case of a directory which crosses into a mounted
	 * filesystem of a different type, e.g., UFS -> NFS.
	 * BART should not walk the new filesystem (by specification), so
	 * set this consolidation-private flag so the rest of the subtree
	 * under this directory is not waled.
	 */
	if (dir_flag &&
	    (strcmp(parent_vfs.f_basetype, path_vfs.f_basetype) != 0))
		ftwx->quit = FTW_PRUNE;

	return (0);
}

/*
 * This file does the per-file evaluation and is run to generate every entry
 * in the manifest.
 *
 * All output is written to a pipe which is read by the child process,
 * which is running output_manifest().
 */
static int
eval_file(const char *fname, const struct stat64 *statb, struct FTW *ftwx)
{
	int	fd, ret, err_code, i, result;
	char	last_field[PATH_MAX], ftype, *acl_str;
	char	*quoted_name;

	err_code = EXIT;

	switch (statb->st_mode & S_IFMT) {
	/* Regular file */
	case S_IFREG: ftype = 'F'; break;

	/* Directory */
	case S_IFDIR: ftype = 'D'; break;

	/* Block Device */
	case S_IFBLK: ftype = 'B'; break;

	/* Character Device */
	case S_IFCHR: ftype = 'C'; break;

	/* Named Pipe */
	case S_IFIFO: ftype = 'P'; break;

	/* Socket */
	case S_IFSOCK: ftype = 'S'; break;

	/* Door */
	case S_IFDOOR: ftype = 'O'; break;

	/* Symbolic link */
	case S_IFLNK: ftype = 'L'; break;

	default: ftype = '-'; break;
	}

	/* First, make sure this file should be cataloged */

	if ((subtree_root != NULL) &&
	    ((result = exclude_fname(fname, ftype, subtree_root)) !=
	    NO_EXCLUDE)) {
		if ((result == EXCLUDE_PRUNE) && (ftwx != (struct FTW *)NULL))
			ftwx->quit = FTW_PRUNE;
		return (err_code);
	}
	for (i = 0; i < PATH_MAX; i++)
		last_field[i] = '\0';

	/*
	 * Regular files, compute the MD5 checksum and put it into 'last_field'
	 * UNLESS instructed to ignore the checksums.
	 */
	if (ftype == 'F') {
		if (compute_chksum) {
			fd = open(fname, O_RDONLY|O_LARGEFILE);
			if (fd < 0) {
				err_code = WARNING_EXIT;
				perror(fname);

				/* default value since the computution failed */
				(void) strcpy(last_field, "-");
			} else {
				if (generate_hash(fd, last_field) != 0) {
					err_code = WARNING_EXIT;
					(void) fprintf(stderr, CONTENTS_WARN,
					    fname);
					(void) strcpy(last_field, "-");
				}
			}
			(void) close(fd);
		}
		/* Instructed to ignore checksums, just put in a '-' */
		else
			(void) strcpy(last_field, "-");
	}

	/*
	 * For symbolic links, put the destination of the symbolic link into
	 * 'last_field'
	 */
	if (ftype == 'L') {
		ret = readlink(fname, last_field, sizeof (last_field));
		if (ret < 0) {
			err_code = WARNING_EXIT;
			perror(fname);

			/* default value since the computation failed */
			(void) strcpy(last_field, "-");
		}
		else
			(void) strlcpy(last_field,
			    sanitized_fname(last_field, B_FALSE),
			    sizeof (last_field));

		/*
		 * Boundary condition: possible for a symlink to point to
		 * nothing [ ln -s '' link_name ].  For this case, set the
		 * destination to "\000".
		 */
		if (strlen(last_field) == 0)
			(void) strcpy(last_field, "\\000");
	}

	acl_str = get_acl_string(fname, statb, &err_code);

	/* Sanitize 'fname', so its in the proper format for the manifest */
	quoted_name = sanitized_fname(fname, B_TRUE);

	/* Start to build the entry.... */
	(void) printf("%s %c %d %o %s %x %d %d", quoted_name, ftype,
	    (int)statb->st_size, (int)statb->st_mode, acl_str,
	    (int)statb->st_mtime, (int)statb->st_uid, (int)statb->st_gid);

	/* Finish it off based upon whether or not it's a device node */
	if ((ftype == 'B') || (ftype == 'C'))
		(void) printf(" %x\n", (int)statb->st_rdev);
	else if (strlen(last_field) > 0)
		(void) printf(" %s\n", last_field);
	else
		(void) printf("\n");

	/* free the memory consumed */
	free(acl_str);
	free(quoted_name);

	return (err_code);
}

/*
 * When creating a manifest, make sure all '?', tabs, space, newline, '/'
 * and '[' are all properly quoted.  Convert them to a "\ooo" where the 'ooo'
 * represents their octal value. For filesystem objects, as opposed to symlink
 * targets, also canonicalize the pathname.
 */
static char *
sanitized_fname(const char *fname, boolean_t canon_path)
{
	const char *ip;
	unsigned char ch;
	char *op, *quoted_name;

	/* Initialize everything */
	quoted_name = safe_calloc((4 * PATH_MAX) + 1);
	ip = fname;
	op = quoted_name;

	if (canon_path) {
		/*
		 * In the case when a relocatable root was used, the relocatable
		 * root should *not* be part of the manifest.
		 */
		ip += strlen(reloc_root);

		/*
		 * In the case when the '-I' option was used, make sure
		 * the quoted_name starts with a '/'.
		 */
		if (*ip != '/')
			*op++ = '/';
	}

	/* Now walk through 'fname' and build the quoted string */
	while ((ch = *ip++) != 0) {
		switch (ch) {
		/* Quote the following characters */
		case ' ':
		case '*':
		case '\n':
		case '?':
		case '[':
		case '\\':
		case '\t':
			op += sprintf(op, "\\%.3o", (unsigned char)ch);
			break;

		/* Otherwise, simply append them */
		default:
			*op++ = ch;
			break;
		}
	}

	*op = 0;

	return (quoted_name);
}

/*
 * Function responsible for generating the ACL information for a given
 * file.  Note, the string is put into buffer malloc'd by this function.
 * It's the responsibility of the caller to free the buffer.  This function
 * should never return a NULL pointer.
 */
static char *
get_acl_string(const char *fname, const struct stat64 *statb, int *err_code)
{
	acl_t		*aclp;
	char		*acltext;
	int		error;

	if (S_ISLNK(statb->st_mode)) {
		return (safe_strdup("-"));
	}

	/*
	 *  Include trivial acl's
	 */
	error = acl_get(fname, 0, &aclp);

	if (error != 0) {
		*err_code = WARNING_EXIT;
		(void) fprintf(stderr, "%s: %s\n", fname, acl_strerror(error));
		return (safe_strdup("-"));
	} else {
		acltext = acl_totext(aclp, 0);
		acl_free(aclp);
		if (acltext == NULL)
			return (safe_strdup("-"));
		else
			return (acltext);
	}
}


/*
 *
 * description:	This routine reads stdin in BUF_SIZE chunks, uses the bits
 *		to update the md5 hash buffer, and outputs the chunks
 *		to stdout.  When stdin is exhausted, the hash is computed,
 *		converted to a hexadecimal string, and returned.
 *
 * returns:	The md5 hash of stdin, or NULL if unsuccessful for any reason.
 */
static int
generate_hash(int fdin, char *hash_str)
{
	unsigned char buf[BUF_SIZE];
	unsigned char hash[MD5_DIGEST_LENGTH];
	int i, amtread;
	MD5_CTX ctx;

	MD5Init(&ctx);

	for (;;) {
		amtread = read(fdin, buf, sizeof (buf));
		if (amtread == 0)
			break;
		if (amtread <  0)
			return (1);

		/* got some data.  Now update hash */
		MD5Update(&ctx, buf, amtread);
	}

	/* done passing through data, calculate hash */
	MD5Final(hash, &ctx);

	for (i = 0; i < MD5_DIGEST_LENGTH; i++)
		(void) sprintf(hash_str + (i*2), "%2.2x", hash[i]);

	return (0);
}

/*
 * Used by 'bart create' with the '-I' option.  Return each entry into a 'buf'
 * with the appropriate exit code: '0' for success and '-1' for failure.
 */
static int
read_filelist(char *reloc_root, char **argv, char *buf, size_t bufsize)
{
	static int		argv_index = -1;
	static boolean_t	read_stdinput = B_FALSE;
	char			temp_buf[PATH_MAX];
	char 			*cp;

	/*
	 * INITIALIZATION:
	 * Setup this code so it knows whether or not to read sdtin.
	 * Also, if reading from argv, setup the index, "argv_index"
	 */
	if (argv_index == -1) {
		argv_index = 0;

		/* In this case, no args after '-I', so read stdin */
		if (argv[0] == NULL)
			read_stdinput = B_TRUE;
	}

	buf[0] = '\0';

	if (read_stdinput) {
		if (fgets(temp_buf, PATH_MAX, stdin) == NULL)
			return (-1);
		cp = strtok(temp_buf, "\n");
	} else {
		cp = argv[argv_index++];
	}

	if (cp == NULL)
		return (-1);

	/*
	 * Unlike similar code elsewhere, avoid adding a leading
	 * slash for relative pathnames.
	 */
	(void) snprintf(buf, bufsize,
	    (reloc_root[0] == '\0' || cp[0] == '/') ? "%s%s" : "%s/%s",
	    reloc_root, cp);

	return (0);
}
/*
 * 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.
 */
#include <dirent.h>
#include <fnmatch.h>
#include "bart.h"


struct cmd_keyword {
	char    *ck_name;
	void    (*ck_func)(void);
};

static struct attr_keyword attr_keywords[] = {
	{ ALL_KEYWORD,	~0 },
	{ CONTENTS_KEYWORD,	ATTR_CONTENTS },
	{ TYPE_KEYWORD,	ATTR_TYPE },
	{ SIZE_KEYWORD,	ATTR_SIZE },
	{ MODE_KEYWORD,	ATTR_MODE },
	{ ACL_KEYWORD,	ATTR_ACL },
	{ UID_KEYWORD,	ATTR_UID },
	{ GID_KEYWORD,	ATTR_GID },
	{ MTIME_KEYWORD,	ATTR_MTIME },
	{ LNMTIME_KEYWORD,	ATTR_LNMTIME },
	{ DIRMTIME_KEYWORD,	ATTR_DIRMTIME },
	{ DEST_KEYWORD,	ATTR_DEST },
	{ DEVNODE_KEYWORD,	ATTR_DEVNODE },
	{ ADD_KEYWORD,	ATTR_ADD },
	{ DELETE_KEYWORD,	ATTR_DELETE },
	{ NULL }
};

struct attr_keyword *
attr_keylookup(char *word)
{
	struct attr_keyword	*akp;

	for (akp = attr_keywords; ; akp++) {
		if (akp->ak_name == NULL)
			break;
		if (strcasecmp(word, akp->ak_name) == 0)
			return (akp);
	}
	return (NULL);
}
/*
 * 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.
 */
#include <signal.h>
#include <unistd.h>
#include <locale.h>
#include <sys/acl.h>
#include "bart.h"

int
main(int argc, char **argv)
{
	/* Make sure we are in the correct locale */
	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);

	/* Superficial check of the arguments.  Note usage() exits the pgm */
	if (argc < 2)
		usage();

	/*
	 * OK, hand it off to bart_create() or bart_compare().
	 *
	 * Since the command line was 'bart create ..',  or 'bart compare ..',
	 * those subcommands should start parsing options at &argv[1], and
	 *  (argc-1) to be consistent.
	 */
	if (strcmp(argv[1], "create") == 0)
		return (bart_create((argc-1), (argv+1)));
	else if (strcmp(argv[1], "compare") == 0) {
		return (bart_compare((argc-1), (argv+1)));

	} else usage();

	return (FATAL_EXIT);
}
void
usage()
{
	(void) fprintf(stderr, USAGE_MSG);
	exit(FATAL_EXIT);
}
void *
safe_calloc(size_t size)
{
	char	*ptr;

	ptr = calloc((size_t)1, size);
	if (ptr == NULL)
		exit(FATAL_EXIT);
	else return (ptr);

	/* NOTREACHED */
}
/*
 * 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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#ifndef	_MSGS_H
#define	_MSGS_H

#ifdef	__cplusplus
extern "C" {
#endif

#define	SYNTAX_ERR	gettext("ERROR: Ignoring rules syntax error: %s\n")
#define	SYNTAX_ABORT	gettext("ABORTING: Syntax error(s) in the rules file\n")
#define	UNKNOWN_FILE	gettext("WARNING: unknown directory entry: %s\n")
#define	CANTLIST_DIR	gettext("WARNING: cannot list directory: %s\n")
#define	INTERNAL_ERR	gettext("WARNING: internal error at %s\n")
#define	INVALID_SUBTREE	gettext("WARNING: Ignoring invalid subtree: %s\n")
#define	RULES_ERR	gettext("ERROR: -r option requires a filename\n")
#define	INPUT_ERR \
	gettext("ERROR: Cannot use -I and -r options together\n")
#define	MISSING_VER	\
	gettext("WARNING: %s has missing/invalid version string\n")
#define	MANIFEST_ERR	gettext("ERROR: Manifest corrupt, cannot continue\n")
#define	CONTENTS_WARN	gettext("WARNING: Checksum failed: %s\n")
#define	USAGE_MSG gettext("Usage:\n"\
	"\tbart create [-n] [-R root] [-r rules|-]\n"\
	"\tbart create [-n] [-R root] [-I | -I filelist]\n"\
	"\tbart compare [-r rules|-] [-i keywords] [-p] "\
		"control-manifest test-manifest\n")

#ifdef	__cplusplus
}
#endif

#endif	/* _MSGS_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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <dirent.h>
#include <fnmatch.h>
#include <string.h>
#include "bart.h"

static int count_slashes(const char *);
static struct rule *gen_rulestruct(void);
static struct tree_modifier *gen_tree_modifier(void);
static struct dir_component *gen_dir_component(void);
static void init_rule(uint_t, struct rule *);
static void add_modifier(struct rule *, char *);
static struct rule *add_subtree_rule(char *, char *, int, int *);
static struct rule *add_single_rule(char *);
static void dirs_cleanup(struct dir_component *);
static void add_dir(struct dir_component **, char *);
static char *lex(FILE *);
static int match_subtree(const char *, char *);
static struct rule *get_last_entry(boolean_t);

static int	lex_linenum;	/* line number in current input file	*/
static struct rule	*first_rule = NULL, *current_rule = NULL;

/*
 * This function is responsible for validating whether or not a given file
 * should be cataloged, based upon the modifiers for a subtree.
 * For example, a line in the rules file: '/home/nickiso *.c' should only
 * catalog the C files (based upon pattern matching) in the subtree
 * '/home/nickiso'.
 *
 * exclude_fname depends on having the modifiers be pre-sorted to put
 * negative directory modifiers first, so that the logic does
 * not need to save complex state information.  This is valid because
 * we are only cataloging things that meet all modifiers (AND logic.)
 *
 * Returns:
 * NO_EXCLUDE
 * EXCLUDE_SKIP
 * EXCLUDE_PRUNE
 */
int
exclude_fname(const char *fname, char fname_type, struct rule *rule_ptr)
{
	char	*pattern, *ptr, *fname_ptr, saved_char;
	char 	fname_cp[PATH_MAX], pattern_cp[PATH_MAX];
	int	num_pattern_slash,  i, num_fname_slash, slashes_to_adv;
	struct  tree_modifier   *mod_ptr;

	/*
	 * If this is create and there are no modifiers, bail.
	 * This will have to change once create handles multiple rules
	 * during walk.
	 */
	if (rule_ptr->modifiers == NULL)
		if (rule_ptr->attr_list == 0)
			return (EXCLUDE_PRUNE);
		else
			return (NO_EXCLUDE);
	/*
	 * Walk through all the modifiers until its they are exhausted OR
	 * until the file should definitely be excluded.
	 */
	for (mod_ptr = rule_ptr->modifiers; mod_ptr != NULL;
	    mod_ptr = mod_ptr->next) {
		/* leading !'s were processed in add_modifier */
		pattern = mod_ptr->mod_str;
		if (mod_ptr->is_dir == B_FALSE) {
			/*
			 * Pattern is a file pattern.
			 *
			 * In the case when a user is trying to filter on
			 * a file pattern and the entry is a directory,
			 * this is not a match.
			 *
			 * If a match is required, skip this file.  If
			 * a match is forbidden, keep looking at modifiers.
			 */
			if (fname_type == 'D') {
				if (mod_ptr->include == B_TRUE)
					return (EXCLUDE_SKIP);
				else
					continue;
			}

			/*
			 * Match patterns against filenames.
			 * Need to be able to handle multi-level patterns,
			 * eg. "SCCS/<star-wildcard>.c", which means
			 * 'only match C files under SCCS directories.
			 *
			 * Determine the number of levels in the filename and
			 * in the pattern.
			 */
			num_pattern_slash = count_slashes(pattern);
			num_fname_slash = count_slashes(fname);

			/* Check for trivial exclude condition */
			if (num_pattern_slash > num_fname_slash) {
				if (mod_ptr->include == B_TRUE)
					return (EXCLUDE_SKIP);
			}

			/*
			 * Do an apples to apples comparison, based upon the
			 * number of levels:
			 *
			 * Assume fname is /A/B/C/D/E and the pattern is D/E.
			 * In that case, 'ptr' will point to "D/E" and
			 * 'slashes_to_adv' will be '4'.
			 */
			(void) strlcpy(fname_cp, fname, sizeof (fname_cp));
			ptr = fname_cp;
			slashes_to_adv = num_fname_slash - num_pattern_slash;
			for (i = 0; i < slashes_to_adv; i++)  {
				ptr = strchr(ptr, '/');
				ptr++;
			}
			if ((pattern[0] == '.') && (pattern[1] == '.') &&
			    (pattern[2] == '/')) {
				pattern = strchr(pattern, '/');
				ptr = strchr(ptr, '/');
			}


			/* OK, now do the fnmatch() compare to the file */
			if (fnmatch(pattern, ptr, FNM_PATHNAME) == 0) {
				/* matches, is it an exclude? */
				if (mod_ptr->include == B_FALSE)
					return (EXCLUDE_SKIP);
			} else if (mod_ptr->include == B_TRUE) {
				/* failed a required filename match */
				return (EXCLUDE_SKIP);
			}
		} else {
			/*
			 * The rule requires directory matching.
			 *
			 * Unlike filename matching, directory matching can
			 * prune.
			 *
			 * First, make copies, since both the pattern and
			 * filename need to be modified.
			 *
			 * When copying 'fname', ignore the relocatable root
			 * since pattern matching is done for the string AFTER
			 * the relocatable root.  For example, if the
			 * relocatable root is "/dir1/dir2/dir3" and the
			 * pattern is "dir3/", we do NOT want to include every
			 * directory in the relocatable root.  Instead, we
			 * only want to include subtrees that look like:
			 * "/dir1/dir2/dir3/....dir3/....."
			 *
			 * NOTE: the 'fname_cp' does NOT have a trailing '/':
			 * necessary for fnmatch().
			 */
			(void) strlcpy(fname_cp,
			    (fname+strlen(rule_ptr->subtree)),
			    sizeof (fname_cp));
			(void) strlcpy(pattern_cp, pattern,
			    sizeof (pattern_cp));

			/*
			 * For non-directory files, remove the trailing
			 * name, e.g., for a file /A/B/C/D where 'D' is
			 * the actual filename, remove the 'D' since it
			 * should *not* be considered in the directory match.
			 */
			if (fname_type != 'D') {
				ptr = strrchr(fname_cp, '/');
				if (ptr != NULL)
					*ptr = '\0';

				/*
				 * Trivial case: a simple filename does
				 * not match a directory by definition,
				 * so skip if match is required,
				 * keep analyzing otherwise.
				 */

				if (strlen(fname_cp) == 0)
					if (mod_ptr->include == B_TRUE)
						return (EXCLUDE_SKIP);
			}

			/* Count the # of slashes in the pattern and fname */
			num_pattern_slash = count_slashes(pattern_cp);
			num_fname_slash = count_slashes(fname_cp);

			/*
			 * fname_cp is too short if this is not a dir
			 */
			if ((num_pattern_slash > num_fname_slash) &&
			    (fname_type != 'D')) {
				if (mod_ptr->include == B_TRUE)
					return (EXCLUDE_SKIP);
			}


			/*
			 * Take the leading '/' from fname_cp before
			 * decrementing the number of slashes.
			 */
			if (fname_cp[0] == '/') {
				(void) strlcpy(fname_cp,
				    strchr(fname_cp, '/') + 1,
				    sizeof (fname_cp));
				num_fname_slash--;
			}

			/*
			 * Begin the loop, walk through the file name until
			 * it can be determined that there is no match.
			 * For example: if pattern is C/D/, and fname_cp is
			 * A/B/C/D/E then compare A/B/ with C/D/, if it doesn't
			 * match, then walk further so that the next iteration
			 * checks B/C/ against C/D/, continue until we have
			 * exhausted options.
			 * In the above case, the 3rd iteration will match
			 * C/D/ with C/D/.
			 */
			while (num_pattern_slash <= num_fname_slash) {
				/* get a pointer to our filename */
				fname_ptr = fname_cp;

				/*
				 * Walk the filename through the slashes
				 * so that we have a component of the same
				 * number of slashes as the pattern.
				 */

				for (i = 0; i < num_pattern_slash; i++) {
					ptr = strchr(fname_ptr, '/');
					fname_ptr = ptr + 1;
				}

				/*
				 * Save the character after our target slash
				 * before breaking the string for use with
				 * fnmatch
				 */
				saved_char = *(++ptr);

				*ptr = '\0';

				/*
				 * Call compare function for the current
				 * component with the pattern we are looking
				 * for.
				 */
				if (fnmatch(pattern_cp, fname_cp,
				    FNM_PATHNAME) == 0) {
					if (mod_ptr->include == B_TRUE) {
						break;
					} else if (fname_type == 'D')
						return (EXCLUDE_PRUNE);
					else
						return (EXCLUDE_SKIP);
				} else if (mod_ptr->include == B_TRUE) {
					if (fname_type == 'D')
						return (EXCLUDE_PRUNE);
					else
						return (EXCLUDE_SKIP);
				}
				/*
				 * We didn't match, so restore the saved
				 * character to the original position.
				 */
				*ptr = saved_char;

				/*
				 * Break down fname_cp, if it was A/B/C
				 * then after this operation it will be B/C
				 * in preparation for the next iteration.
				 */
				(void) strlcpy(fname_cp,
				    strchr(fname_cp, '/') + 1,
				    sizeof (fname_cp));

				/*
				 * Decrement the number of slashes to
				 * compensate for the one removed above.
				 */
				num_fname_slash--;
			} /* end while loop looking down the path */

			/*
			 * If we didn't get a match above then we may be on the
			 * last component of our filename.
			 * This is to handle the following cases
			 *    - filename is A/B/C/D/E and pattern may be D/E/
			 *    - filename is D/E and pattern may be D/E/
			 */
			if (num_pattern_slash == (num_fname_slash + 1)) {

				/* strip the trailing slash from the pattern */
				ptr = strrchr(pattern_cp, '/');
				*ptr = '\0';

				/* fnmatch returns 0 for a match */
				if (fnmatch(pattern_cp, fname_cp,
				    FNM_PATHNAME) == 0) {
					if (mod_ptr->include == B_FALSE) {
						if (fname_type == 'D')
							return (EXCLUDE_PRUNE);
						else
							return (EXCLUDE_SKIP);
					}
				} else if (mod_ptr->include == B_TRUE)
					return (EXCLUDE_SKIP);

			}

		}
	}
	return (NO_EXCLUDE);
}

static int
count_slashes(const char *in_path)
{
	int num_fname_slash = 0;
	const char *p;
	for (p = in_path; *p != '\0'; p++)
		if (*p == '/')
			num_fname_slash++;
	return (num_fname_slash);
}

static struct rule *
gen_rulestruct(void)
{
	struct rule	*new_rule;

	new_rule = (struct rule *)safe_calloc(sizeof (struct rule));
	return (new_rule);
}

static struct tree_modifier *
gen_tree_modifier(void)
{
	struct tree_modifier	*new_modifier;

	new_modifier = (struct tree_modifier *)safe_calloc
	    (sizeof (struct tree_modifier));
	return (new_modifier);
}

static struct dir_component *
gen_dir_component(void)
{
	struct dir_component	*new_dir;

	new_dir = (struct dir_component *)safe_calloc
	    (sizeof (struct dir_component));
	return (new_dir);
}

/*
 * Set up a default rule when there is no rules file.
 */
static struct rule *
setup_default_rule(char *reloc_root, uint_t flags)
{
	struct	rule	*new_rule;

	new_rule = add_single_rule(reloc_root[0] == '\0' ? "/" : reloc_root);
	init_rule(flags, new_rule);
	add_modifier(new_rule, "*");

	return (new_rule);
}

/*
 * Utility function, used to initialize the flag in a new rule structure.
 */
static void
init_rule(uint_t flags, struct rule *new_rule)
{

	if (new_rule == NULL)
		return;
	new_rule->attr_list = flags;
}

/*
 * Function to read the rulesfile.  Used by both 'bart create' and
 * 'bart compare'.
 */
int
read_rules(FILE *file, char *reloc_root, uint_t in_flags, int create)
{
	char		*s;
	struct rule	*block_begin = NULL, *new_rule, *rp;
	struct attr_keyword *akp;
	int		check_flag, ignore_flag, syntax_err, ret_code;
	int		global_block;

	ret_code = EXIT;

	lex_linenum = 0;
	check_flag = 0;
	ignore_flag = 0;
	syntax_err = 0;
	global_block = 1;

	if (file == NULL) {
		(void) setup_default_rule(reloc_root, in_flags);
		return (ret_code);
	} else if (!create) {
		block_begin = setup_default_rule("/", in_flags);
	}

	while (!feof(file)) {
		/* Read a line from the file */
		s = lex(file);

		/* skip blank lines and comments */
		if (s == NULL || *s == 0 || *s == '#')
			continue;

		/*
		 * Beginning of a subtree and possibly a new block.
		 *
		 * If this is a new block, keep track of the beginning of
		 * the block. if there are directives later on, we need to
		 * apply that directive to all members of the block.
		 *
		 * If the first stmt in the file was an 'IGNORE all' or
		 * 'IGNORE contents', we need to keep track of it and
		 * automatically switch off contents checking for new
		 * subtrees.
		 */
		if (s[0] == '/') {
			/* subtree definition hence not a global block */
			global_block = 0;

			new_rule = add_subtree_rule(s, reloc_root, create,
			    &ret_code);

			s = lex(0);
			while ((s != NULL) && (*s != 0) && (*s != '#')) {
				add_modifier(new_rule, s);
				s = lex(0);
			}

			/* Found a new block, keep track of the beginning */
			if (block_begin == NULL ||
			    (ignore_flag != 0) || (check_flag != 0)) {
				block_begin = new_rule;
				check_flag = 0;
				ignore_flag = 0;
			}

			/* Apply global settings to this block, if any */
			init_rule(in_flags, new_rule);
		} else if (IGNORE_KEYWORD(s) || CHECK_KEYWORD(s)) {
			int check_kw;

			if (IGNORE_KEYWORD(s)) {
				ignore_flag++;
				check_kw = 0;
			} else {
				check_flag++;
				check_kw = 1;
			}

			/* Parse next token */
			s = lex(0);
			while ((s != NULL) && (*s != 0) && (*s != '#')) {
				akp = attr_keylookup(s);
				if (akp == NULL) {
					(void) fprintf(stderr, SYNTAX_ERR, s);
					syntax_err++;
					exit(2);
				}

				/*
				 * For all the flags, check if this is a global
				 * IGNORE/CHECK. If so, set the global flags.
				 *
				 * NOTE: The only time you can have a
				 * global ignore is when its the
				 * stmt before any blocks have been
				 * spec'd.
				 */
				if (global_block) {
					if (check_kw)
						in_flags |= akp->ak_flags;
					else
						in_flags &= ~(akp->ak_flags);
				} else {
					for (rp = block_begin; rp != NULL;
					    rp = rp->next) {
						if (check_kw)
							rp->attr_list |=
							    akp->ak_flags;
						else
							rp->attr_list &=
							    ~(akp->ak_flags);
					}
				}

				/* Parse next token */
				s = lex(0);
			}
		} else {
			(void) fprintf(stderr, SYNTAX_ERR, s);
			s = lex(0);
			while (s != NULL && *s != 0) {
				(void) fprintf(stderr, " %s", s);
				s = lex(0);
			}
			(void) fprintf(stderr, "\n");
			syntax_err++;
		}
	}

	(void) fclose(file);

	if (syntax_err) {
		(void) fprintf(stderr, SYNTAX_ABORT);
		exit(2);
	}

	return (ret_code);
}
/*
 * Add a modifier to the mod_ptr list in each rule, putting negative
 * directory entries
 * first to guarantee walks will be appropriately pruned.
 */
static void
add_modifier(struct rule *rule, char *modifier_str)
{
	int	include, is_dir;
	char	*pattern;
	struct tree_modifier	*new_mod_ptr, *curr_mod_ptr;
	struct rule		*this_rule;

	include = B_TRUE;
	pattern = modifier_str;

	/* see if the pattern is an include or an exclude */
	if (pattern[0] == '!') {
		include = B_FALSE;
		pattern++;
	}

	is_dir = (pattern[0] != '\0' && pattern[strlen(pattern) - 1] == '/');

	for (this_rule = rule; this_rule != NULL; this_rule = this_rule->next) {
		new_mod_ptr = gen_tree_modifier();
		new_mod_ptr->include = include;
		new_mod_ptr->is_dir = is_dir;
		new_mod_ptr->mod_str = safe_strdup(pattern);

		if (is_dir && !include) {
			new_mod_ptr->next = this_rule->modifiers;
			this_rule->modifiers = new_mod_ptr;
		} else if (this_rule->modifiers == NULL)
			this_rule->modifiers = new_mod_ptr;
		else {
			curr_mod_ptr = this_rule->modifiers;
			while (curr_mod_ptr->next != NULL)
				curr_mod_ptr = curr_mod_ptr->next;

			curr_mod_ptr->next = new_mod_ptr;
		}
	}
}

/*
 * This funtion is invoked when reading rulesfiles.  A subtree may have
 * wildcards in it, e.g., '/home/n*', which is expected to match all home
 * dirs which start with an 'n'.
 *
 * This function needs to break down the subtree into its components.  For
 * each component, see how many directories match.  Take the subtree list just
 * generated and run it through again, this time looking at the next component.
 * At each iteration, keep a linked list of subtrees that currently match.
 * Once the final list is created, invoke add_single_rule() to create the
 * rule struct with the correct information.
 *
 * This function returns a ptr to the first element in the block of subtrees
 * which matched the subtree def'n in the rulesfile.
 */
static struct rule *
add_subtree_rule(char *rule, char *reloc_root, int create, int *err_code)
{
	char			full_path[PATH_MAX], pattern[PATH_MAX];
	char			new_dirname[PATH_MAX];
	char			*beg_pattern, *end_pattern, *curr_dirname;
	struct	dir_component	*current_level = NULL, *next_level = NULL;
	struct	dir_component	*tmp_ptr;
	DIR			*dir_ptr;
	struct dirent		*dir_entry;
	struct rule		*begin_rule = NULL;
	int			ret;
	struct stat64		statb;

	(void) snprintf(full_path, sizeof (full_path),
	    (rule[0] == '/') ? "%s%s" : "%s/%s", reloc_root, rule);

	/*
	 * In the case of 'bart compare', don't validate
	 * the subtrees, since the machine running the
	 * comparison may not be the machine which generated
	 * the manifest.
	 */
	if (create == 0)
		return (add_single_rule(full_path));


	/* Insert 'current_level' into the linked list */
	add_dir(&current_level, NULL);

	/* Special case: occurs when -R is "/" and the subtree is "/" */
	if (strcmp(full_path, "/") == 0)
		(void) strcpy(current_level->dirname, "/");

	beg_pattern = full_path;

	while (beg_pattern != NULL) {
		/*
		 * Extract the pathname component starting at 'beg_pattern'.
		 * Take those chars and put them into 'pattern'.
		 */
		while (*beg_pattern == '/')
			beg_pattern++;
		if (*beg_pattern == '\0')	/* end of pathname */
			break;
		end_pattern = strchr(beg_pattern, '/');
		if (end_pattern != NULL)
			(void) strlcpy(pattern, beg_pattern,
			    end_pattern - beg_pattern + 1);
		else
			(void) strlcpy(pattern, beg_pattern, sizeof (pattern));
		beg_pattern = end_pattern;

		/*
		 * At this point, search for 'pattern' as a *subdirectory* of
		 * the dirs in the linked list.
		 */
		while (current_level != NULL) {
			/* curr_dirname used to make the code more readable */
			curr_dirname = current_level->dirname;

			/* Initialization case */
			if (strlen(curr_dirname) == 0)
				(void) strcpy(curr_dirname, "/");

			/* Open up the dir for this element in the list */
			dir_ptr = opendir(curr_dirname);
			dir_entry = NULL;

			if (dir_ptr == NULL) {
				perror(curr_dirname);
				*err_code = WARNING_EXIT;
			} else
				dir_entry = readdir(dir_ptr);

			/*
			 * Now iterate through the subdirs of 'curr_dirname'
			 * In the case of a match against 'pattern',
			 * add the path to the next linked list, which
			 * will be matched on the next iteration.
			 */
			while (dir_entry != NULL) {
				/* Skip the dirs "." and ".." */
				if ((strcmp(dir_entry->d_name, ".") == 0) ||
				    (strcmp(dir_entry->d_name, "..") == 0)) {
					dir_entry = readdir(dir_ptr);
					continue;
				}
				if (fnmatch(pattern, dir_entry->d_name,
				    FNM_PATHNAME) == 0) {
					/*
					 * Build 'new_dirname' which will be
					 * examined on the next iteration.
					 */
					if (curr_dirname[strlen(curr_dirname)-1]
					    != '/')
						(void) snprintf(new_dirname,
						    sizeof (new_dirname),
						    "%s/%s", curr_dirname,
						    dir_entry->d_name);
					else
						(void) snprintf(new_dirname,
						    sizeof (new_dirname),
						    "%s%s", curr_dirname,
						    dir_entry->d_name);

					/* Add to the next lined list */
					add_dir(&next_level, new_dirname);
				}
				dir_entry = readdir(dir_ptr);
			}

			/* Close directory */
			if (dir_ptr != NULL)
				(void) closedir(dir_ptr);

			/* Free this entry and move on.... */
			tmp_ptr = current_level;
			current_level = current_level->next;
			free(tmp_ptr);
		}

		/*
		 * OK, done with this level.  Move to the next level and
		 * advance the ptrs which indicate the component name.
		 */
		current_level = next_level;
		next_level = NULL;
	}

	tmp_ptr = current_level;

	/* Error case: the subtree doesn't exist! */
	if (current_level == NULL) {
		(void) fprintf(stderr, INVALID_SUBTREE, full_path);
		*err_code = WARNING_EXIT;
	}

	/*
	 * Iterate through all the dirnames which match the pattern and
	 * add them to to global list of subtrees which must be examined.
	 */
	while (current_level != NULL) {
		/*
		 * Sanity check for 'bart create', make sure the subtree
		 * points to a valid object.
		 */
		ret = lstat64(current_level->dirname, &statb);
		if (ret < 0) {
			(void) fprintf(stderr, INVALID_SUBTREE,
			    current_level->dirname);
			current_level = current_level->next;
			*err_code = WARNING_EXIT;
			continue;
		}

		if (begin_rule == NULL) {
			begin_rule =
			    add_single_rule(current_level->dirname);
		} else
			(void) add_single_rule(current_level->dirname);

		current_level = current_level->next;
	}

	/*
	 * Free up the memory and return a ptr to the first entry in the
	 * subtree block.  This is necessary for the parser, which may need
	 * to add modifier strings to all the elements in this block.
	 */
	dirs_cleanup(tmp_ptr);

	return (begin_rule);
}


/*
 * Add a single entry to the linked list of rules to be read.  Does not do
 * the wildcard expansion of 'add_subtree_rule', so is much simpler.
 */
static struct rule *
add_single_rule(char *path)
{

	/*
	 * If the rules list does NOT exist, then create it.
	 * If the rules list does exist, then traverse the next element.
	 */
	if (first_rule == NULL) {
		first_rule = gen_rulestruct();
		current_rule = first_rule;
	} else {
		current_rule->next = gen_rulestruct();
		current_rule->next->prev = current_rule;
		current_rule = current_rule->next;
	}

	/* Setup the rule struct, handle relocatable roots, i.e. '-R' option */
	(void) strlcpy(current_rule->subtree, path,
	    sizeof (current_rule->subtree));

	return (current_rule);
}

/*
 * Code stolen from filesync utility, used by read_rules() to read in the
 * rulesfile.
 */
static char *
lex(FILE *file)
{
	char c, delim;
	char *p;
	char *s;
	static char *savep;
	static char namebuf[ BUF_SIZE ];
	static char inbuf[ BUF_SIZE ];

	if (file) {			/* read a new line		*/
		p = inbuf + sizeof (inbuf);

		s = inbuf;
		/* read the next input line, with all continuations	*/
		while (savep = fgets(s, p - s, file)) {
			lex_linenum++;

			/* go find the last character of the input line	*/
			while (*s && s[1])
				s++;
			if (*s == '\n')
				s--;

			/* see whether or not we need a continuation	*/
			if (s < inbuf || *s != '\\')
				break;

			continue;
		}

		if (savep == NULL)
			return (0);

		s = inbuf;
	} else {			/* continue with old line	*/
		if (savep == NULL)
			return (0);
		s = savep;
	}
	savep = NULL;

	/* skip over leading white space	*/
	while (isspace(*s))
		s++;
	if (*s == 0)
		return (0);

	/* see if this is a quoted string	*/
	c = *s;
	if (c == '\'' || c == '"') {
		delim = c;
		s++;
	} else
		delim = 0;

	/* copy the token into the buffer	*/
	for (p = namebuf; (c = *s) != 0; s++) {
		/* literal escape		*/
		if (c == '\\') {
			s++;
			*p++ = *s;
			continue;
		}

		/* closing delimiter		*/
		if (c == delim) {
			s++;
			break;
		}

		/* delimiting white space	*/
		if (delim == 0 && isspace(c))
			break;

		/* ordinary characters		*/
		*p++ = *s;
	}


	/* remember where we left off		*/
	savep = *s ? s : 0;

	/* null terminate and return the buffer	*/
	*p = 0;
	return (namebuf);
}

/*
 * Iterate through the dir strcutures and free memory.
 */
static void
dirs_cleanup(struct dir_component *dir)
{
	struct	dir_component	*next;

	while (dir != NULL) {
		next = dir->next;
		free(dir);
		dir = next;
	}
}

/*
 * Create and initialize a new dir structure.  Used by add_subtree_rule() when
 * doing expansion of directory names caused by wildcards.
 */
static void
add_dir(struct dir_component **dir, char *dirname)
{
	struct	dir_component	*new, *next_dir;

	new = gen_dir_component();
	if (dirname != NULL)
		(void) strlcpy(new->dirname, dirname, sizeof (new->dirname));

	if (*dir == NULL)
		*dir = new;
	else {
		next_dir = *dir;
		while (next_dir->next != NULL)
			next_dir = next_dir->next;

		next_dir->next = new;
	}
}

/*
 * Traverse the linked list of rules in a REVERSE order.
 */
static struct rule *
get_last_entry(boolean_t reset)
{
	static struct rule	*curr_root = NULL;

	if (reset) {

		curr_root = first_rule;

		/* RESET: set cur_root to the end of the list */
		while (curr_root != NULL)
			if (curr_root->next == NULL)
				break;
			else
				curr_root = curr_root->next;
	} else
		curr_root = (curr_root->prev);

	return (curr_root);
}

/*
 * Traverse the first entry, used by 'bart create' to iterate through
 * subtrees or individual filenames.
 */
struct rule *
get_first_subtree()
{
	return (first_rule);
}

/*
 * Traverse the next entry, used by 'bart create' to iterate through
 * subtrees or individual filenames.
 */
struct rule *
get_next_subtree(struct rule *entry)
{
	return (entry->next);
}

char *
safe_strdup(char *s)
{
	char *ret;
	size_t len;

	len = strlen(s) + 1;
	ret = safe_calloc(len);
	(void) strlcpy(ret, s, len);
	return (ret);
}

/*
 * Function to match a filename against the subtrees in the link list
 * of 'rule' strcutures.  Upon finding a matching rule, see if it should
 * be excluded.  Keep going until a match is found OR all rules have been
 * exhausted.
 * NOTES: Rules are parsed in reverse;
 * satisfies the spec that "Last rule wins".  Also, the default rule should
 * always match, so this function should NEVER return NULL.
 */
struct rule *
check_rules(const char *fname, char type)
{
	struct rule		*root;

	root = get_last_entry(B_TRUE);
	while (root != NULL) {
		if (match_subtree(fname, root->subtree)) {
			if (exclude_fname(fname, type, root) == NO_EXCLUDE)
				break;
		}
		root = get_last_entry(B_FALSE);
	}

	return (root);
}

/*
 * Function to determine if an entry in a rules file (see bart_rules(5)) applies
 * to a filename. We truncate "fname" such that it has the same number of
 * components as "rule" and let fnmatch(3C) do the rest. A "component" is one
 * part of an fname as delimited by slashes ('/'). So "/A/B/C/D" has four
 * components: "A", "B", "C" and "D".
 *
 * For example:
 *
 * 1. the rule "/home/nickiso" applies to fname "/home/nickiso/src/foo.c" so
 * should match.
 *
 * 2. the rule "/home/nickiso/temp/src" does not apply to fname
 * "/home/nickiso/foo.c" so should not match.
 */
static int
match_subtree(const char *fname, char *rule)
{
	int	match, num_rule_slash;
	char	*ptr, fname_cp[PATH_MAX];

	/* If rule has more components than fname, it cannot match. */
	if ((num_rule_slash = count_slashes(rule)) > count_slashes(fname))
		return (0);

	/* Create a copy of fname that we can truncate. */
	(void) strlcpy(fname_cp, fname, sizeof (fname_cp));

	/*
	 * Truncate fname_cp such that it has the same number of components
	 * as rule. If rule ends with '/', so should fname_cp. ie:
	 *
	 * rule		fname			fname_cp	matches
	 * ----		-----			--------	-------
	 * /home/dir*	/home/dir0/dir1/fileA	/home/dir0	yes
	 * /home/dir/	/home/dir0/dir1/fileA	/home/dir0/	no
	 */
	for (ptr = fname_cp; num_rule_slash > 0; num_rule_slash--, ptr++)
		ptr = strchr(ptr, '/');
	if (*(rule + strlen(rule) - 1) != '/') {
		while (*ptr != '\0') {
			if (*ptr == '/')
				break;
			ptr++;
		}
	}
	*ptr = '\0';

	/* OK, now see if they match. */
	match = fnmatch(rule, fname_cp, FNM_PATHNAME);

	/* No match, return failure */
	if (match != 0)
		return (0);
	else
		return (1);
}

void
process_glob_ignores(char *ignore_list, uint_t *flags)
{
	char	*cp;
	struct attr_keyword *akp;

	if (ignore_list == NULL)
		usage();

	cp = strtok(ignore_list, ",");
	while (cp != NULL) {
		akp = attr_keylookup(cp);
		if (akp == NULL)
			(void) fprintf(stderr, "ERROR: Invalid keyword %s\n",
			    cp);
		else
			*flags &= ~akp->ak_flags;
		cp = strtok(NULL, ",");
	}
}