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
|
/*
* $Id: environ.c,v 1.23 2004/06/18 16:19:37 andrew_belov Exp $
* ---------------------------------------------------------------------------
* This module contains platform-specific routines along with a set of hacks
* to implement the common functions of Borland compilers and their behavior.
*
*/
#include "arj.h"
#if TARGET!=UNIX
#include <conio.h>
#include <io.h>
#include <process.h>
#endif
#include <fcntl.h>
#if COMPILER==BCC
#include <dir.h>
#elif COMPILER==MSC||COMPILER==MSVC||COMPILER==ICC
#include <direct.h>
#endif
#if TARGET!=UNIX
#include <share.h>
#endif
#if COMPILER==MSC||COMPILER==MSVC||COMPILER==ICC||COMPILER==HIGHC||defined(__EMX__)||(TARGET==OS2&&defined(LIBC))
#include <sys/types.h>
#include <sys/stat.h>
#if COMPILER==HIGHC&&!defined(LIBC)
#define SH_DENYRW _SH_DENYRW
#define SH_DENYWR _SH_DENYWR
#define SH_DENYNO _SH_DENYNO
#endif
#elif TARGET!=UNIX
#include <share.h>
#endif
#if COMPILER==BCC&&TARGET==DOS
#include <sys/stat.h> /* S_* only */
#endif
#if TARGET==UNIX
#ifdef SUNOS
#include <sys/statvfs.h>
#include <termio.h>
#endif
#ifdef __sco__
#include <sys/statvfs.h>
#endif
#include <unistd.h>
#include <fnmatch.h>
#include <signal.h> /* fork()+spawnvp() control */
#include <utime.h>
#include <sys/time.h> /* LIBC high-resolution timing */
#include <sys/resource.h> /* Priority control */
#include <sys/wait.h>
#if defined(linux)
#include <sys/ioctl.h>
#include <sys/statfs.h>
#include <sys/statvfs.h>
#elif defined(__FreeBSD__)||defined(__NetBSD__)
#include <sys/param.h>
#include <sys/mount.h>
#elif defined(__QNXNTO__)
#include <sys/statvfs.h>
#else
#include <sys/statfs.h>
#endif
#endif
#ifdef TILED
#include <dos.h>
#endif
#if TARGET==DOS
#include "win95dos.h"
#endif
#if TARGET==WIN32&&!defined(F_OK)
#define F_OK 0 /* For MSVCRT */
#endif
/* IBM toolkit -> EMX wrapper */
#ifdef __EMX__
#define DosCaseMap DosMapCase
#define DosQCurDisk DosQueryCurrentDisk
#define DosQFileInfo DosQueryFileInfo
#define DosQFSInfo DosQueryFSInfo
#define DosSelectDisk DosSetDefaultDisk
#define DosSetPrty DosSetPriority
#endif
DEBUGHDR(__FILE__) /* Debug information block */
/*
* DOS legacy
*/
#if TARGET==DOS
/* INT 24h */
#define INT24 0x24
#define INT24_IGNORE 0
#define INT24_RETRY 1
#define INT24_ABORT 2
#define INT24_FAIL 3
#define INT24_DPF_WRITING 0x0100 /* Device processing flag in AX */
#define INT24_IO_ERROR 0x8000
/* Character device IOCTL statements */
#define CHDI_STDOUT 0x0001
#define CHDI_STDIN 0x0002
#define CHDI_NUL 0x0004
#define CHDI_CLOCK 0x0008
#define CHDI_SPECIAL 0x0010
#define CHDI_BINARY 0x0020
#define CHDI_EOF_ON_INPUT 0x0040
#define CHDI_SET 0x0080
#define CHDI_OPENCLOSE 0x0800
#define CHDI_OUTPUT_TILL_BUSY 0x2000
#define CHDI_CAN_IOCTL 0x4000
/* File IOCTL statements */
#define CHDF_NOT_WRITTEN 0x0040
#define CHDF_NOT_FILE 0x0080
#define CHDF_EXT_INT24 0x0100 /* DOS 4.x only */
#define CHDF_NOT_REMOVABLE 0x0800
#define CHDF_NO_TIMESTAMPING 0x4000
#define CHDF_IS_REMOTE 0x8000
/* This macro checks if DOS version is less than 3.10 */
#define is_dos_31 (_osmajor<3||_osmajor==3&&_osminor<10)
/* For MS C, a macro to query the current time */
#if COMPILER==MSC
#define g_timer(t) t=*(long FAR *)0x0000046CL
#endif
#endif /* TARGET==DOS */
/* An OS/2 & Win32 macro to join timestamps */
#if TARGET==OS2||TARGET==WIN32
#define make_ftime(fd, ft) ((unsigned long)*(USHORT *)&fd<<16)+(unsigned long)*(USHORT *)&ft
#endif
/* Allowable DOS file attributes (HSRA) */
#if TARGET!=UNIX
#define STD_FILE_ATTR (FATTR_ARCH|FATTR_SYSTEM|FATTR_HIDDEN|FATTR_RDONLY)
#endif
/* Attribute comparison for UNIX */
#if TARGET==UNIX
#define match_unix_attrs(attr, pattern) (((pattern)==0)||(((attr)&(pattern))==(pattern)))
#endif
/* Command-line input limit */
#if TARGET==DOS
#define INPUT_LIMIT 127
#endif
/* UNIX file time requests */
#if TARGET==UNIX
#define UFTREQ_FTIME 0
#define UFTREQ_ATIME 1
#define UFTREQ_CTIME 2
#endif
/*
* Exported variables
*/
/* Line feed string */
#if SFX_LEVEL>=ARJSFX||defined(REARJ)
char simple_lf[]="\n";
#endif
/* Carriage return */
#if SFX_LEVEL>=ARJ||TARGET==DOS&&(SFX_LEVEL>=ARJSFX||defined(REARJ))
char simple_cr[]="\r";
#endif
/* The following systems are considered equal to host OS under which ARJ is
run. The contents of this list greatly depend upon the host OS itself... */
#if SFX_LEVEL>=ARJSFX
#if TARGET==DOS
int friendly_systems[]={OS_DOS, OS_WIN95, OS_WINNT, -1};
#elif TARGET==OS2
int friendly_systems[]={OS_DOS, OS_OS2, OS_WIN95, OS_WINNT, -1};
#elif TARGET==WIN32
int friendly_systems[]={OS_DOS, OS_WIN95, OS_WINNT, -1};
#elif TARGET==UNIX
int friendly_systems[]={OS_UNIX, OS_NEXT, -1};
#endif
#endif
/* Standard devices */
#if SFX_LEVEL>=ARJSFX
#if TARGET==UNIX
char dev_null[]="/dev/null"; /* NULL device */
char dev_con[]="/dev/tty"; /* Console device */
#else
char dev_null[]="NUL"; /* NULL device */
char dev_con[]="CON"; /* Console device */
#endif
#endif
/* Wildcard used to select all files */
#if SFX_LEVEL>=ARJSFX||defined(REARJ)
#if TARGET==DOS
char all_wildcard[]="*.*";
#else
char all_wildcard[]="*";
#endif
#endif
/* Win32 can't use setftime_on_stream - this is required for ARJSFXJR: */
#if SFX_LEVEL==ARJSFXJR&&TARGET==WIN32
#define NEED_SETFTIME_HACK
#endif
/*
* Internal variables
*/
/* Attribute format */
#if SFX_LEVEL>=ARJSFX
static char attrib_buf[]="---W"; /* ASHR if all set */
#endif
/* Arbitrary disk drive for LFN testing */
#if SFX_LEVEL>=ARJSFX||defined(REARJ)
static char drive_c[]="C:"; /* Although it's incorrect... */
#endif
/* Directory specifiers for file search */
#if SFX_LEVEL>=ARJ||defined(REARJ)
/* Subdirectory/root search wildcard */
#if TARGET==DOS
char root_wildcard[]="\\*.*";
#elif TARGET==OS2||TARGET==WIN32
char root_wildcard[]="\\*";
#elif TARGET==UNIX
char root_wildcard[]="/*";
#endif
char up_dir_spec[]=".."; /* Parent directory specifier */
char pathsep_str[]={PATHSEP_DEFAULT, '\0'};
#endif
#if SFX_LEVEL>=ARJ||defined(REARJ)||(SFX_LEVEL>=ARJSFXV&&TARGET==UNIX)
char cur_dir_spec[]="."; /* Current directory specifier */
#endif
/* FCB mask (used to fill in FCBs) */
#if SFX_LEVEL>=ARJSFXV&&TARGET==DOS
static char fcb_mask[]="???????????";
#endif
/* Queues for detecting child session shutdown */
#if TARGET==OS2&&defined(REARJ)
static char rearj_q_fmt[]="\\QUEUES\\REARJ#%u";
#endif
/* Name of file that is currently being opened */
#if COMPILER==MSC&&TARGET==DOS
static char *f_file_ptr=NULL;
#endif
/* For the case if the environment doesn't allow to query EXE name, we'll store
our own ones. */
#if TARGET==DOS
#if SFX_LEVEL>=ARJ
static char default_exe[]="arj" EXE_EXTENSION;
#elif SFX_LEVEL>=ARJSFXJR
static char default_exe[]="arjsfx" EXE_EXTENSION;
#elif defined(REARJ)
static char default_exe[]="rearj" EXE_EXTENSION;
#endif
#endif
/*
* Frequently used Borland routines
*/
/* Returns 0 for A:, 1 for B:, etc. */
#if defined(HAVE_DRIVES)&&((SFX_LEVEL>=ARJSFX||defined(REARJ))&&COMPILER!=BCC)
int getdisk()
{
#if TARGET==DOS
int rc;
_dos_getdrive((unsigned int *)&rc);
return(rc-1);
#elif TARGET==OS2
#ifndef __32BIT__
USHORT rc;
ULONG total;
DosQCurDisk(&rc, &total);
return(rc-1);
#else
ULONG rc, total;
DosQCurDisk(&rc, &total);
return(rc-1);
#endif
#elif TARGET==WIN32
char cur_dir[CCHMAXPATH];
if(GetCurrentDirectory(sizeof(cur_dir), cur_dir)&&cur_dir[1]==':')
return(cur_dir[0]-'A');
else
return(-1);
#endif
}
#endif
/* Performs heap checking if required */
#if SFX_LEVEL>=ARJ
int verify_heap()
{
#if COMPILER==BCC
return(heapcheck()==_HEAPCORRUPT);
#elif COMPILER==MSC||COMPILER==MSVC
int rc;
rc=_heapchk();
return(rc!=_HEAPOK&&rc!=_HEAPEMPTY);
#elif COMPILER==ICC&&defined(DEBUG)
_heap_check();
return(0);
#else
return(0); /* Not implemented otherwise */
#endif
}
#endif
/* Performs far heap verification (if there is any) */
#if SFX_LEVEL>=ARJ
int verify_far_heap()
{
#if COMPILER==BCC
return(farheapcheck());
#elif COMPILER==MSC
return(_fheapchk());
#elif !defined(TILED)
return(verify_heap());
#else
return(0); /* Don't even bother of it */
#endif
}
#endif
/* Returns the available stack space */
#if SFX_LEVEL>=ARJ
static unsigned int get_stack_space()
{
#if defined(__BORLANDC__)
return(stackavail());
#elif defined(__TURBOC__)
return(_stklen+_SP);
#elif COMPILER==MSC
return(stackavail());
#else
return(32767);
#endif
}
#endif
/* Changes the current drive to 0=A:, 1=B:, and so on... */
#if defined(HAVE_DRIVES)&&(defined(REARJ)&&COMPILER!=BCC)
int setdisk(int drive)
{
#if TARGET==DOS
int numdrives;
_dos_setdrive(drive+1, &numdrives);
return(numdrives);
#elif TARGET==OS2
#ifdef __32BIT__
ULONG rc;
#else
USHORT rc;
#endif
ULONG total;
rc=DosSelectDisk(drive+1);
if(rc)
return(0);
DosQCurDisk(&rc, &total);
return(total);
#elif TARGET==WIN32
char t[4];
t[0]=drive+'A';
t[1]=':';
t[2]='\0';
SetCurrentDirectory(t);
return(25); /* Dummy value */
#endif
}
#endif
/* Returns the number of bytes available in the far heap (quite slow). This is
an advisory function for legacy parts of ARJ. Avoid it by all means. */
#if SFX_LEVEL>=ARJSFXV&&COMPILER!=BCC
long farcoreleft()
{
#if TARGET==DOS
void _huge *hp;
static long rc=736L;
long s_rc;
s_rc=rc;
rc+=2L;
do
hp=halloc(rc-=2L, 1024);
while(hp==NULL&&rc>0L);
if(hp!=NULL)
hfree(hp);
if(rc<s_rc)
return(rc*1024L);
do
{
hp=halloc(rc+=16L, 1024);
if(hp!=NULL)
hfree(hp);
} while(hp!=NULL);
return((rc-16L)*1024L);
#elif TARGET==OS2
#ifdef __32BIT__
ULONG rc;
DosQuerySysInfo(QSV_MAXPRMEM, QSV_MAXPRMEM, (PVOID)&rc, sizeof(rc));
return(rc);
#else
ULONG rc;
DosMemAvail(&rc);
return(rc);
#endif
#elif TARGET==WIN32
MEMORYSTATUS memstat;
memstat.dwLength=sizeof(memstat);
GlobalMemoryStatus(&memstat);
return(max(0x7FFFFFFFUL, memstat.dwAvailVirtual));
#else
return(0x7FFFFFFF);
#endif
}
#endif
/* Returns the current time of day */
#if (SFX_LEVEL>=ARJSFXV||defined(REARJ)||defined(REGISTER))&&COMPILER!=BCC
void arj_gettime(struct time *ts)
{
#if TARGET==DOS
struct dostime_t dts;
_dos_gettime(&dts);
ts->ti_hour=dts.hour;
ts->ti_min=dts.minute;
ts->ti_sec=dts.second;
ts->ti_hund=dts.hsecond;
#elif TARGET==OS2
DATETIME dts;
DosGetDateTime(&dts);
ts->ti_hour=dts.hours;
ts->ti_min=dts.minutes;
ts->ti_sec=dts.seconds;
ts->ti_hund=dts.hundredths;
#elif TARGET==WIN32
SYSTEMTIME st;
GetLocalTime(&st);
ts->ti_hour=st.wHour;
ts->ti_min=st.wMinute;
ts->ti_sec=st.wSecond;
ts->ti_hund=st.wMilliseconds/10;
#else
time_t t;
struct timeval v;
struct tm *tms;
do
{
t=time(NULL);
gettimeofday(&v, NULL);
} while(time(NULL)!=t);
tms=localtime(&t);
ts->ti_hour=tms->tm_hour;
ts->ti_min=tms->tm_min;
ts->ti_sec=tms->tm_sec;
ts->ti_hund=v.tv_usec/10000;
#endif
}
#endif
/* Returns the current date */
#if defined(REARJ)&&COMPILER!=BCC
void arj_getdate(struct date *ds)
{
#if TARGET==DOS
struct dosdate_t dds;
_dos_getdate(&dds);
ds->da_year=1980+dds.year;
ds->da_day=dds.day;
ds->da_mon=dds.month;
#elif TARGET==OS2
DATETIME dts;
DosGetDateTime(&dts);
ds->da_year=dts.year;
ds->da_day=dts.day;
ds->da_mon=dts.month;
#elif TARGET==WIN32
SYSTEMTIME st;
GetLocalTime(&st);
ds->da_year=st.wYear;
ds->da_day=st.wDay;
ds->da_mon=st.wMonth;
#else
time_t t;
struct tm *tms;
t=time(NULL);
tms=localtime(&t);
ds->da_year=tms->tm_year+1900;
ds->da_day=tms->tm_mday;
ds->da_mon=tms->tm_mon+1;
#endif
}
#endif
/* Gets address of DOS DTA */
#if SFX_LEVEL>=ARJSFXV&&COMPILER!=BCC&&TARGET==DOS
static char FAR *getdta()
{
union REGS regs;
struct SREGS sregs;
regs.h.ah=0x2F;
intdosx(®s, ®s, &sregs);
return(MK_FP(sregs.es, regs.x.bx));
}
#endif
/* Sets address of DOS DTA */
#if SFX_LEVEL>=ARJSFXV&&COMPILER!=BCC&&TARGET==DOS
static void setdta(char FAR *dta)
{
union REGS regs;
struct SREGS sregs;
regs.h.ah=0x1A;
sregs.ds=FP_SEG(dta); regs.x.dx=FP_OFF(dta);
intdosx(®s, ®s, &sregs);
}
#endif
/*
* OS/2 farcalloc()/farfree() routines
*/
#if TARGET==OS2&&defined(TILED)
/* farcalloc() for 0-based segments */
#if SFX_LEVEL>=ARJ&&defined(ASM8086)
void FAR *farcalloc_based(unsigned long num, unsigned long size)
{
USHORT total;
SEL selector;
void FAR *rc;
total=(USHORT)num*size;
if(DosAllocSeg(total, &selector, SEG_NONSHARED))
return(NULL);
rc=(void FAR *)MAKEP(selector, 0);
far_memset(rc, 0, total);
return(rc);
}
#endif
/* farfree() for 0-based segments */
#if SFX_LEVEL>=ARJ&&defined(ASM8086)
void farfree_based(void FAR *ptr)
{
DosFreeSeg(SELECTOROF(ptr));
}
#endif
#endif
/* Sets the process priority. */
#if TARGET!=DOS&&SFX_LEVEL>=ARJ
void set_priority(struct priority *priority)
{
#if TARGET==OS2
DosSetPrty(PRTYS_THREAD, priority->class, priority->delta, 0);
#elif TARGET==WIN32
static HANDLE ph=0, th=0;
static DWORD w32_classes[4]={IDLE_PRIORITY_CLASS,
NORMAL_PRIORITY_CLASS,
HIGH_PRIORITY_CLASS,
REALTIME_PRIORITY_CLASS};
if(!ph)
ph=GetCurrentProcess();
if(!th)
th=GetCurrentThread();
if(priority->class<=4)
SetPriorityClass(ph, w32_classes[priority->class-1]);
SetThreadPriority(th, priority->delta);
#else
#if defined(HAVE_SETPRIORITY)
setpriority(PRIO_PROCESS, 0, 21-priority->class);
#else
#error Priority functions missing
#endif
#endif
}
#endif
/*
* This section is specific to Windows 95 LFN API.
*/
/* Just a customized interrupt call procedure */
#if (SFX_LEVEL>=ARJSFX||defined(REARJ))&&TARGET==DOS
static int call_dos_int(unsigned int funcnum, union REGS *regs, struct SREGS *sregs)
{
regs->x.ax=funcnum;
#ifdef ASM8086
asm{
pushf
pop ax
or ax, 1
push ax
popf
};
#else
/* Provoke the carry flag */
regs->x.cflag=(regs->x.ax&0x7FFF)+0x8000;
#endif
intdosx(regs, regs, sregs);
_doserrno=(regs->x.cflag!=0)?(regs->x.ax):0;
return(regs->x.cflag);
}
#endif
/* Test the specified volume for long filename support */
#if (SFX_LEVEL>=ARJSFX||defined(REARJ))&&TARGET==DOS
static int w95_test_for_lfn(char *drive)
{
union REGS regs;
struct SREGS sregs;
char filesystem[40]; /* Ralf Brown says 32 */
char FAR *fsptr, FAR *dptr;
fsptr=(char FAR *)filesystem;
dptr=(char FAR *)drive;
memset(&sregs, 0, sizeof(sregs));
sregs.es=FP_SEG(fsptr); regs.x.di=FP_OFF(fsptr);
regs.x.cx=sizeof(filesystem);
sregs.ds=FP_SEG(dptr); regs.x.dx=FP_OFF(dptr);
return(call_dos_int(W95_GET_VOLUME_INFO, ®s, &sregs)==0&®s.x.bx&0x4000);
}
#endif
/* Return equivalent canonical short filename for a long filename */
#if (SFX_LEVEL>=ARJSFX||defined(REARJ))&&TARGET==DOS
static int w95_get_shortname(char *longname, char *shortname, int cb_shortname)
{
union REGS regs;
struct SREGS sregs;
char FAR *lnptr, FAR *snptr;
memset(&sregs, 0, sizeof(sregs));
if(cb_shortname>=CCHMAXPATH_DOS)
{
lnptr=(char FAR *)longname;
snptr=(char FAR *)shortname;
shortname[0]='\0';
sregs.ds=FP_SEG(lnptr); regs.x.si=FP_OFF(lnptr);
sregs.es=FP_SEG(snptr); regs.x.di=FP_OFF(snptr);
regs.x.cx=W95_GET_SHORTNAME; /* No SUBST expansion, subfunc #1 */
if(!call_dos_int(W95_TRUENAME, ®s, &sregs))
return(strlen(shortname));
else
return(0);
}
else
return(0);
}
#endif
/* Changes directory under Windows 95 */
#if defined(REARJ)&&TARGET==DOS
static int w95_chdir(char *longname)
{
union REGS regs;
struct SREGS sregs;
char FAR *lnptr;
memset(&sregs, 0, sizeof(sregs));
lnptr=(char FAR *)longname;
sregs.ds=FP_SEG(lnptr); regs.x.dx=FP_OFF(lnptr);
return(call_dos_int(W95_CHDIR, ®s, &sregs)?-1:0);
}
#endif
/* Return equivalent canonical long filename for a short filename */
#if (SFX_LEVEL>=ARJ)&&TARGET==DOS
static int w95_get_longname(char *shortname, char *longname, int cb_longname)
{
union REGS regs;
struct SREGS sregs;
char FAR *lnptr, FAR *snptr;
memset(&sregs, 0, sizeof(sregs));
if(cb_longname>=CCHMAXPATH_W95)
{
longname[0]='\0';
lnptr=(char FAR *)longname;
snptr=(char FAR *)shortname;
sregs.ds=FP_SEG(snptr); regs.x.si=FP_OFF(snptr);
sregs.es=FP_SEG(lnptr); regs.x.di=FP_OFF(lnptr);
regs.x.cx=W95_GET_LONGNAME; /* No SUBST expansion, subfunc #2 */
if(!call_dos_int(W95_TRUENAME, ®s, &sregs))
return(strlen(longname));
else
return(0);
}
else
return(0);
}
#endif
/* Returns 1 if the current OS is Windows NT, 0 if Windows 95 */
#if SFX_LEVEL>=ARJ&&TARGET==DOS
int test_for_winnt()
{
return(0); /* Implemented in ARJ32 */
}
#endif
/* Returns the name of current directory */
#if defined(REARJ)&&TARGET==DOS
static char *w95_cwd(char *dest)
{
union REGS regs;
struct SREGS sregs;
char FAR *dptr;
dptr=(char FAR *)dest;
memset(&sregs, 0, sizeof(sregs));
dest[0]=getdisk()+'A';
dest[1]=':';
dest[2]=PATHSEP_DEFAULT;
regs.h.dl=0;
sregs.ds=FP_SEG(dptr); regs.x.si=FP_OFF(dptr)+3;
return(call_dos_int(W95_CWD, ®s, &sregs)?NULL:dest);
}
#endif
/* Create a directory with longname. Return -1 if failed. */
#if (SFX_LEVEL>=ARJSFX||defined(REARJ))&&TARGET==DOS
static int w95_mkdir(char *longname)
{
union REGS regs;
struct SREGS sregs;
char FAR *lnptr;
lnptr=(char FAR *)longname;
memset(&sregs, 0, sizeof(sregs)); /* BUG?! No register cleanup in ARJ */
sregs.ds=FP_SEG(lnptr); regs.x.dx=FP_OFF(lnptr);
return(call_dos_int(W95_MKDIR, ®s, &sregs)?-1:0);
}
#endif
/* Remove a directory with longname. Return -1 if failed. */
#if (SFX_LEVEL>=ARJ||defined(REARJ))&&TARGET==DOS
static int w95_rmdir(char *longname)
{
union REGS regs;
struct SREGS sregs;
char FAR *lnptr;
lnptr=(char FAR *)longname;
memset(&sregs, 0, sizeof(sregs));
sregs.ds=FP_SEG(lnptr); regs.x.dx=FP_OFF(lnptr);
return(call_dos_int(W95_RMDIR, ®s, &sregs)?-1:0);
}
#endif
/* Delete a file */
#if (SFX_LEVEL>=ARJSFX||defined(REARJ))&&TARGET==DOS
static int w95_unlink(char *longname)
{
union REGS regs;
struct SREGS sregs;
char FAR *lnptr;
lnptr=(char FAR *)longname;
memset(&sregs, 0, sizeof(sregs));
sregs.ds=FP_SEG(lnptr); regs.x.dx=FP_OFF(lnptr);
#ifndef REARJ
regs.x.cx=FATTR_ARCH|FATTR_SYSTEM|FATTR_RDONLY;
#else
regs.x.cx=0;
#endif
regs.x.si=W95_WILDCARDS_DISABLED; /* Forbid wildcard usage */
return(call_dos_int(W95_UNLINK, ®s, &sregs)?-1:0);
}
#endif
/* Rename a file */
#if (SFX_LEVEL>=ARJ||defined(REARJ))&&TARGET==DOS
static int w95_rename(char *longname1, char *longname2)
{
union REGS regs;
struct SREGS sregs;
char FAR *lnptr1, FAR *lnptr2;
lnptr1=(char FAR *)longname1;
lnptr2=(char FAR *)longname2;
memset(&sregs, 0, sizeof(sregs));
sregs.ds=FP_SEG(lnptr1); regs.x.dx=FP_OFF(lnptr1);
sregs.es=FP_SEG(lnptr2); regs.x.di=FP_OFF(lnptr2);
return(call_dos_int(W95_RENAME, ®s, &sregs)?-1:0);
}
#endif
/* Query or change attributes */
#if (SFX_LEVEL>=ARJSFX||defined(REARJ))&&TARGET==DOS
static int w95_chmod(char *longname, int action, int pmode)
{
union REGS regs;
struct SREGS sregs;
char FAR *lnptr;
lnptr=(char FAR *)longname;
memset(&sregs, 0, sizeof(sregs));
regs.x.bx=action;
regs.x.cx=pmode;
sregs.ds=FP_SEG(lnptr); regs.x.dx=FP_OFF(lnptr);
return(call_dos_int(W95_CHMOD, ®s, &sregs)?-1:regs.x.cx);
}
#endif
/* access() function for LFNs - test if the file has the given access mode */
#if (SFX_LEVEL>=ARJSFX||defined(REARJ))&&TARGET==DOS
static int w95_access(char *longname, int mode)
{
if((w95_chmod(longname, W95_GETATTR, 0))==-1)
return(-1);
else
{
if((!(mode&2))||!(mode&FATTR_RDONLY))
return(0);
else
{
errno=EACCES;
return(-1);
}
}
}
#endif
/* findfirst() function as implemented in Borland Turbo C++ */
#if (SFX_LEVEL>=ARJSFXV||defined(REARJ))&&TARGET==DOS
static int w95_findfirst(char *path, struct new_ffblk *new_ffblk, int attrib)
{
union REGS regs;
struct SREGS sregs;
struct W95_FFBLK w95_ffblk, FAR *fb_ptr;
char FAR *p_ptr;
memset(&sregs, 0, sizeof(sregs));
fb_ptr=(struct W95_FFBLK FAR *)&w95_ffblk;
p_ptr=(char FAR *)path;
sregs.ds=FP_SEG(p_ptr); regs.x.dx=FP_OFF(p_ptr);
sregs.es=FP_SEG(fb_ptr); regs.x.di=FP_OFF(fb_ptr);
regs.x.cx=attrib;
regs.x.si=W95_DT_DOS; /* Use DOS date/time format */
if(!call_dos_int(W95_FINDFIRST, ®s, &sregs))
{
#if SFX_LEVEL>=ARJ||defined(REARJ)
new_ffblk->ff_handle=regs.x.ax; /* Preserve handle for findclose */
strcpy(new_ffblk->ff_name, w95_ffblk.ff_longname);
new_ffblk->ff_atime=w95_ffblk.ff_atime;
new_ffblk->ff_ctime=w95_ffblk.ff_ctime;
#endif
new_ffblk->ff_attrib=(char)w95_ffblk.ff_attrib;
new_ffblk->ff_ftime=w95_ffblk.ff_ftime;
new_ffblk->ff_fsize=w95_ffblk.ff_fsize;
#if SFX_LEVEL==ARJSFXV
memset(&sregs, 0, sizeof(sregs));
regs.x.bx=regs.x.ax; /* Transfer FF handle */
call_dos_int(W95_FINDCLOSE, ®s, &sregs);
#endif
return(0);
}
else
return(-1);
}
#endif
/* findnext() function as implemented in Borland Turbo C++ */
#if (SFX_LEVEL>=ARJ||defined(REARJ))&&TARGET==DOS
static int w95_findnext(struct new_ffblk *new_ffblk)
{
union REGS regs;
struct SREGS sregs;
struct W95_FFBLK w95_ffblk, FAR *fb_ptr;
memset(&sregs, 0, sizeof(sregs));
fb_ptr=(struct W95_FFBLK FAR *)&w95_ffblk;
sregs.es=FP_SEG(fb_ptr);
regs.x.di=FP_OFF(fb_ptr);
regs.x.bx=new_ffblk->ff_handle;
regs.x.si=W95_DT_DOS; /* Not present in original ARJ! */
if(!call_dos_int(W95_FINDNEXT, ®s, &sregs))
{
new_ffblk->ff_attrib=(char)w95_ffblk.ff_attrib;
strcpy(new_ffblk->ff_name, w95_ffblk.ff_longname);
new_ffblk->ff_ftime=w95_ffblk.ff_ftime;
new_ffblk->ff_atime=w95_ffblk.ff_atime;
new_ffblk->ff_ctime=w95_ffblk.ff_ctime;
new_ffblk->ff_fsize=w95_ffblk.ff_fsize;
return(0);
}
else
return(-1);
}
#endif
/* Close search (specific to Windows 95) */
#if (SFX_LEVEL>=ARJSFXV||defined(REARJ))&&TARGET==DOS
static void w95_findclose(struct new_ffblk *new_ffblk)
{
union REGS regs;
struct SREGS sregs;
memset(&sregs, 0, sizeof(sregs));
regs.x.bx=new_ffblk->ff_handle;
call_dos_int(W95_FINDCLOSE, ®s, &sregs);
}
#endif
/* Create a file with the same options as given for _open, return handle */
#if (SFX_LEVEL>=ARJSFX||defined(REARJ))&&TARGET==DOS
static int w95_creat(char *longname, int access)
{
union REGS regs;
struct SREGS sregs;
char FAR *lnptr;
lnptr=(char FAR *)longname;
memset(&sregs, 0, sizeof(sregs));
sregs.ds=FP_SEG(lnptr); regs.x.si=FP_OFF(lnptr);
regs.x.bx=access&(O_RDONLY|O_WRONLY);
regs.x.cx=32;
regs.x.dx=0;
regs.x.di=1;
/* Translate FCNTL actions into Win95 actions */
if(access&O_CREAT) regs.x.dx|=W95_A_CREAT;
if(access&O_TRUNC) regs.x.dx|=W95_A_TRUNC;
if(access&O_EXCL) regs.x.dx|=W95_A_EXCL;
return(call_dos_int(W95_OPEN, ®s, &sregs)?-1:regs.x.ax);
}
#endif
/* Stamp date/time of last access on handle. Note that Win95 does not support
time of last access. */
#if (SFX_LEVEL>=ARJSFXV)&&TARGET==DOS
static int w95_set_dta(int handle, unsigned long ftime)
{
union REGS regs;
struct SREGS sregs;
memset(&sregs, 0, sizeof(sregs));
regs.x.bx=handle;
regs.x.cx=0;
regs.x.dx=(unsigned short)ftime>>16;
return(call_dos_int(W95_SET_DTA, ®s, &sregs)?-1:0);
}
#endif
/* Stamp date/time of last access on handle. Note that Win95 does not support
time of last access. */
#if (SFX_LEVEL>=ARJSFXV)&&TARGET==DOS
static int w95_set_dtc(int handle, unsigned long ftime)
{
union REGS regs;
struct SREGS sregs;
memset(&sregs, 0, sizeof(sregs));
regs.x.bx=handle;
regs.x.cx=(unsigned short)(ftime%65536L);
regs.x.dx=(unsigned short)ftime>>16;
regs.x.si=0; /* Number of 1/100ths */
return(call_dos_int(W95_SET_DTC, ®s, &sregs)?-1:0);
}
#endif
/*
* Now, some less OS-dependent routines.
*/
/* Return pointer to first character following a drivespec/relative pathspec
so names like "\VIRUS.COM" will be transformed to safe "VIRUS.COM" */
#if SFX_LEVEL>=ARJSFX
#if SFX_LEVEL>=ARJSFXV
static char *validate_path(char *name, int action)
#else
static char *validate_path(char *name)
#endif
{
#if SFX_LEVEL>=ARJSFXV
if(action!=VALIDATE_NOTHING)
{
#endif
#ifdef HAVE_DRIVES
if(name[0]!='\0'&&name[1]==':')
name+=2; /* Skip over drivespecs */
#endif
#if SFX_LEVEL>=ARJSFXV
if(action!=VALIDATE_DRIVESPEC)
{
#endif
while (name[0]!='\0'&&
(name[0]=='.'||name[0]==PATHSEP_DEFAULT||name[0]==PATHSEP_UNIX)) {
if(name[0]=='.')
{
if(name[1]=='.'&&(name[2]==PATHSEP_DEFAULT||name[2]==PATHSEP_UNIX))
name++; /* "..\\" relative path */
if(name[1]==PATHSEP_DEFAULT||name[1]==PATHSEP_UNIX)
name++; /* ".\\" relative path */
}
if(name[0]==PATHSEP_DEFAULT||name[0]==PATHSEP_UNIX)
name++; /* "\\" - revert to root */
}
#if SFX_LEVEL>=ARJSFXV
}
}
#endif
return(name);
}
#endif
/* Convert the extended finddata record to OS-independent internal storage
format */
#if SFX_LEVEL>=ARJ
static void finddata_to_properties(struct file_properties *properties, struct new_ffblk *new_ffblk)
{
#if TARGET==UNIX
int u;
#endif
properties->ftime=new_ffblk->ff_ftime;
properties->atime=new_ffblk->ff_atime;
properties->ctime=new_ffblk->ff_ctime;
properties->fsize=new_ffblk->ff_fsize;
properties->attrib=(ATTRIB)new_ffblk->ff_attrib;
#if TARGET!=UNIX
properties->type=(new_ffblk->ff_attrib&FATTR_DIREC)?ARJT_DIR:ARJT_BINARY;
properties->isarchive=(new_ffblk->ff_attrib&FATTR_ARCH)?1:0;
#else
u=uftype(new_ffblk->ff_attrib);
if(u&FATTR_DT_DIR)
properties->type=ARJT_DIR;
else if(u&FATTR_DT_UXSPECIAL)
properties->type=ARJT_UXSPECIAL;
else
properties->type=ARJT_BINARY;
/* Can't check fot non-DT_REG since these may be requested by find_file() */
properties->isarchive=1;
/* Hardlink data */
properties->l_search=new_ffblk->l_search;
properties->l_search.ref=0;
properties->islink=0;
#endif
}
#endif
/* Return pointer to the first path delimiter in given string, or NULL if
nothing found */
#if SFX_LEVEL>=ARJSFX
static char *find_delimiter(char *path, int datatype)
{
if(path[0]=='\0')
return(NULL);
while(path[0]!='\0')
{
if(path[0]==PATHSEP_DEFAULT||path[0]==PATHSEP_UNIX)
return(path);
path++;
}
if(datatype==ARJT_DIR)
return(path);
else
return(NULL);
}
#endif
/*
* Secondary-level file management routines. Usually they must either be
* redirected to Win95 LFN API, or serviced with the C RTL functions.
*/
/* chmod() - Borland's implementation */
#if SFX_LEVEL>=ARJSFX||defined(REARJ)
int file_chmod(char *name, int action, int attrs)
{
#if TARGET==DOS
if(lfn_supported!=LFN_NOT_SUPPORTED)
return(w95_chmod(name, action, attrs));
else
#if COMPILER==BCC
return(_chmod(name, action, attrs));
#elif COMPILER==MSC
{
int rc;
if(action)
return(_dos_setfileattr(name, attrs));
else
{
rc=-1;
_dos_getfileattr(name, &rc);
return(rc);
}
}
#endif
#elif TARGET==OS2
#ifdef __32BIT__
FILESTATUS3 fstatus;
int rc;
rc=DosQueryPathInfo(name, FIL_STANDARD, (PBYTE)&fstatus, sizeof(fstatus));
if(action)
{
fstatus.attrFile=attrs;
return(DosSetPathInfo(name, FIL_STANDARD, (PBYTE)&fstatus, sizeof(fstatus), 0)?-1:0);
}
else
return(rc?-1:fstatus.attrFile);
#else
USHORT rc;
if(action)
return(DosSetFileMode(name, attrs, 0L)?-1:0);
else
return(DosQFileMode(name, &rc, 0L)?-1:rc);
#endif
#elif TARGET==WIN32
DWORD rc;
if(!action)
{
rc=GetFileAttributes(name);
if(rc==0xFFFFFFFF)
return(-1);
else
return((int)rc);
}
else
return(!SetFileAttributes(name, attrs));
#else
struct stat st;
if(action)
{
if(!lstat(name, &st)&&S_ISLNK(st.st_mode))
return(0); /* ASR fix 15/06/2003: don't touch symlinks anymore */
else
return(chmod(name, attrs));
}
else
return(lstat(name, &st)?-1:st.st_mode);
#endif
}
#endif
/* Manages the access rights for a stream - required to handle the
SFX archives properly */
#if SFX_LEVEL>=ARJ&&TARGET==UNIX
int file_acc(FILE *stream)
{
struct stat st;
return(fstat(fileno(stream), &st)?-1:st.st_mode);
}
/* Duplicates the "R" access bits into "X" for creating a SFX */
void make_executable(FILE *stream)
{
int stm;
stm=file_acc(stream);
fchmod(fileno(stream), stm|((stm>>2)&0111));
}
#endif
/* access() */
#if SFX_LEVEL>=ARJSFX||defined(REARJ)||defined(REGISTER)
static int file_access(char *name, int mode)
{
#if TARGET==DOS
#ifndef REGISTER
if(lfn_supported!=LFN_NOT_SUPPORTED)
return(w95_access(name, mode));
else
#endif
return(access(name, mode));
#elif TARGET==OS2||TARGET==WIN32||TARGET==UNIX
return(access(name, mode));
#else
#error No access() defined
#endif
}
#endif
#if TARGET==UNIX&&(SFX_LEVEL>=ARJSFXV||defined(REARJ))
/* Locates the first matching dirent, like findnext() but for the first dirent
as well (the UNIX-way). May trash the new_ffblk structures without returning
success, but who cares? */
static int roll_dirent(struct new_ffblk *new_ffblk)
{
struct dirent *ent;
int a, l, m;
struct stat st, resolved_st;
int attrs;
dev_t real_dev;
ino_t real_inode;
a=ufattr(attrs=new_ffblk->attrs);
strcpy(new_ffblk->ff_name, new_ffblk->dir);
if(!strcmp(new_ffblk->ff_name, cur_dir_spec))
new_ffblk->ff_name[l=0]='\0';
else
{
l=strlen(new_ffblk->ff_name);
if(l==0||new_ffblk->ff_name[l-1]!=PATHSEP_DEFAULT)
new_ffblk->ff_name[l++]=PATHSEP_DEFAULT;
}
/* Now, check all files until we find the matching one */
while((ent=readdir(new_ffblk->ff_handle))!=NULL)
{
strcpy(new_ffblk->ff_name+l, ent->d_name);
if(a!=0)
m=match_unix_attrs(file_chmod(new_ffblk->ff_name, 0, 0), a);
else
m=1;
/* Wildcard matching and attribute check */
if(m&&match_wildcard(ent->d_name, new_ffblk->wildcard))
{
if(lstat(new_ffblk->ff_name, &st))
return(-1);
real_dev=st.st_dev;
real_inode=st.st_ino;
#if SFX_LEVEL>=ARJ
/* Search in the device pool to see if the user forbids archiving for
this device */
if(!is_dev_allowed(real_dev))
continue;
#endif
/* Redo stat if it's a link and we do not handle links (so need it to be
resolved) */
if(!(attrs&FATTR_DT_UXSPECIAL)&&stat(new_ffblk->ff_name, &st))
continue;
/* Sockets aren't supported, check for other types as well */
#if !defined(S_ISSOCK)
#define S_ISSOCK(m) 0
#endif
if(!S_ISSOCK(st.st_mode)&&
(((uftype(attrs)==FATTR_DT_ANY||(uftype(attrs)&FATTR_DT_REG))&&S_ISREG(st.st_mode))||
((attrs&FATTR_DT_DIR)&&S_ISDIR(st.st_mode))||
(attrs&FATTR_DT_UXSPECIAL)))
{
/* Reestablish the "unqualified" filename */
strcpy(new_ffblk->ff_name, ent->d_name);
/* If it was a link, try to retrieve the mode of file which it points to.
Otherwise, chmod() will trash the mode of original file upon restore */
if(S_ISLNK(st.st_mode)&&stat(new_ffblk->ff_name, &resolved_st)!=-1)
{
new_ffblk->ff_attrib=resolved_st.st_mode&FATTR_UFMASK;
real_dev=resolved_st.st_dev;
real_inode=resolved_st.st_ino;
}
else
new_ffblk->ff_attrib=st.st_mode&FATTR_UFMASK;
/* Convert the remaining structures to new_ffblk and leave the search */
new_ffblk->ff_ftype=st.st_mode;
if(S_ISREG(st.st_mode))
new_ffblk->ff_attrib|=FATTR_DT_REG;
else if(S_ISDIR(st.st_mode))
new_ffblk->ff_attrib|=FATTR_DT_DIR;
else
new_ffblk->ff_attrib|=FATTR_DT_UXSPECIAL;
new_ffblk->ff_ftime=st.st_mtime;
/* Prevent REARJ from complaining about directory size mismatch */
new_ffblk->ff_fsize=(S_ISREG(st.st_mode))?st.st_size:0;
new_ffblk->ff_atime=st.st_atime;
new_ffblk->ff_ctime=st.st_ctime;
/* Special structures for hard links */
new_ffblk->l_search.dev=real_dev;
new_ffblk->l_search.inode=real_inode;
new_ffblk->l_search.refcount=S_ISDIR(st.st_mode)?1:st.st_nlink;
break;
}
}
}
return(ent==NULL?-1:0);
}
#endif
#if TARGET==WIN32
/* Strip NT timestamps -> DOS timestamps */
#if SFX_LEVEL>=ARJSFX||defined(REARJ)
static unsigned long dosify_time(FILETIME *pft)
{
FILETIME lft;
WORD dd=0x1111, dt=0x1111;
FileTimeToLocalFileTime(pft, &lft);
FileTimeToDosDateTime(&lft, &dd, &dt);
return(make_ftime(dd, dt));
}
#endif
/* Expand DOS timestamps into NT UTC timestamps */
#if SFX_LEVEL>=ARJSFXJR||defined(REARJ)
static void ntify_time(FILETIME *pft, unsigned long dft)
{
FILETIME lft;
DosDateTimeToFileTime((WORD)(dft>>16), (WORD)(dft&0xFFFF), &lft);
LocalFileTimeToFileTime(&lft, pft);
}
#endif
#endif
/* findfirst() - Borland's implementation */
#if SFX_LEVEL>=ARJSFXV||defined(REARJ)
int lfn_findfirst(char *path, struct new_ffblk *new_ffblk, int attrib)
{
#if TARGET==DOS
/* ASR fix 04/09/2001 for the R11 timestamp format */
new_ffblk->ff_atime=new_ffblk->ff_ctime=0;
if(lfn_supported==LFN_SUPPORTED)
return(w95_findfirst(path, new_ffblk, attrib));
else
#if COMPILER==BCC
return(findfirst(path, (struct ffblk *)new_ffblk, attrib));
#elif COMPILER==MSC
return(_dos_findfirst(path, attrib, (struct find_t *)new_ffblk));
#endif
#elif TARGET==OS2
HDIR handle=HDIR_CREATE;
#ifdef __32BIT__
FILEFINDBUF3 findbuf;
ULONG fcount=1L;
#else
FILEFINDBUF findbuf;
USHORT fcount=1;
#endif
#ifdef __32BIT__
if(DosFindFirst(path, &handle, attrib, &findbuf, sizeof(findbuf), &fcount, FIL_STANDARD))
return(-1);
#else
if(DosFindFirst(path, &handle, attrib, &findbuf, sizeof(findbuf), &fcount, 0L))
return(-1);
#endif
/* Do the conversion */
new_ffblk->ff_attrib=findbuf.attrFile;
new_ffblk->ff_ftime=make_ftime(findbuf.fdateLastWrite, findbuf.ftimeLastWrite);
new_ffblk->ff_fsize=findbuf.cbFile;
strcpy(new_ffblk->ff_name, findbuf.achName);
new_ffblk->ff_atime=make_ftime(findbuf.fdateLastAccess, findbuf.ftimeLastAccess);
new_ffblk->ff_ctime=make_ftime(findbuf.fdateCreation, findbuf.ftimeCreation);
#if SFX_LEVEL<=ARJSFXV&&!defined(REARJ)
DosFindClose(handle);
#else
new_ffblk->ff_handle=handle;
#endif
return(0);
#elif TARGET==WIN32
WIN32_FIND_DATA wfd;
HANDLE hf;
if((hf=FindFirstFile(path, &wfd))==INVALID_HANDLE_VALUE)
return(-1);
if(wfd.nFileSizeHigh>0||(long)wfd.nFileSizeLow<0)
return(-1);
new_ffblk->ff_attrib=wfd.dwFileAttributes;
new_ffblk->ff_ftime=dosify_time(&wfd.ftLastWriteTime);
new_ffblk->ff_atime=dosify_time(&wfd.ftLastAccessTime);
new_ffblk->ff_ctime=dosify_time(&wfd.ftCreationTime);
new_ffblk->ff_fsize=wfd.nFileSizeLow;
strcpy(new_ffblk->ff_name, wfd.cFileName);
#if SFX_LEVEL<=ARJSFXV&&!defined(REARJ)
FindClose(hf);
#else
new_ffblk->ff_handle=hf;
#endif
return(0);
#elif TARGET==UNIX
split_name(path, new_ffblk->dir, new_ffblk->wildcard);
if(new_ffblk->wildcard[0]=='\0')
strcpy(new_ffblk->wildcard, all_wildcard);
if(new_ffblk->dir[0]=='\0')
strcpy(new_ffblk->dir, cur_dir_spec);
if((new_ffblk->ff_handle=opendir(new_ffblk->dir))==NULL)
return(-1);
new_ffblk->attrs=attrib;
if(roll_dirent(new_ffblk)==-1)
{
closedir(new_ffblk->ff_handle);
new_ffblk->ff_handle=NULL; /* Invalidate the new_ffblk */
return(-1);
}
#if SFX_LEVEL<=ARJSFXV&&!defined(REARJ)
closedir(new_ffblk->ff_handle);
#endif
return(0);
#endif
}
#endif
/* findnext() - Borland's implementation */
#if SFX_LEVEL>=ARJ||defined(REARJ)
int lfn_findnext(struct new_ffblk *new_ffblk)
{
#if TARGET==DOS
if(lfn_supported==LFN_SUPPORTED)
return(w95_findnext(new_ffblk));
else
#if COMPILER==BCC
return(findnext((struct ffblk *)new_ffblk));
#elif COMPILER==MSC
return(_dos_findnext((struct find_t *)new_ffblk));
#endif
#elif TARGET==OS2
HDIR handle;
#ifdef __32BIT__
FILEFINDBUF3 findbuf;
ULONG fcount=1L;
#else
FILEFINDBUF findbuf;
USHORT fcount=1;
#endif
handle=new_ffblk->ff_handle;
if(DosFindNext(handle, &findbuf, sizeof(findbuf), &fcount))
return(-1);
/* Do the conversion */
new_ffblk->ff_attrib=findbuf.attrFile;
new_ffblk->ff_ftime=make_ftime(findbuf.fdateLastWrite, findbuf.ftimeLastWrite);
new_ffblk->ff_fsize=findbuf.cbFile;
strcpy(new_ffblk->ff_name, findbuf.achName);
new_ffblk->ff_atime=make_ftime(findbuf.fdateLastAccess, findbuf.ftimeLastAccess);
new_ffblk->ff_ctime=make_ftime(findbuf.fdateCreation, findbuf.ftimeCreation);
return(0);
#elif TARGET==WIN32
WIN32_FIND_DATA wfd;
if(!FindNextFile(new_ffblk->ff_handle, &wfd))
return(-1);
if(wfd.nFileSizeHigh>0||(long)wfd.nFileSizeLow<0)
return(-1);
new_ffblk->ff_attrib=wfd.dwFileAttributes;
new_ffblk->ff_ftime=dosify_time(&wfd.ftLastWriteTime);
new_ffblk->ff_atime=dosify_time(&wfd.ftLastAccessTime);
new_ffblk->ff_ctime=dosify_time(&wfd.ftCreationTime);
new_ffblk->ff_fsize=wfd.nFileSizeLow;
strcpy(new_ffblk->ff_name, wfd.cFileName);
return(0);
#elif TARGET==UNIX
if(roll_dirent(new_ffblk)==-1)
return(-1);
return(0);
#endif
}
#endif
/* findclose() */
#if SFX_LEVEL>=ARJ||defined(REARJ)
void lfn_findclose(struct new_ffblk *new_ffblk)
{
#if TARGET==DOS
if(lfn_supported==LFN_SUPPORTED)
w95_findclose(new_ffblk);
#elif TARGET==OS2
DosFindClose(new_ffblk->ff_handle);
#elif TARGET==WIN32
FindClose(new_ffblk->ff_handle);
#elif TARGET==UNIX
if(new_ffblk->ff_handle!=NULL)
closedir(new_ffblk->ff_handle);
new_ffblk->ff_handle=NULL; /* Invalidate it! */
#endif
}
#endif
/* Convert the given symbols to upper case, depending on locale */
#if SFX_LEVEL>=ARJSFXV||defined(REARJ)
void toupper_loc(unsigned char *ptr, int length)
{
#if TARGET==DOS&&defined(ASM8086)
struct
{
int co_date;
char co_curr[5];
char co_thsep[2];
char co_desep[2];
char co_dtsep[2];
char co_tmsep[2];
char co_currstyle;
char co_digits;
char co_time;
long co_case;
char co_dasep[2];
char co_fill[10];
} cty;
static int cty_state=0; /* 0 if not queried yet */
static char((FAR *ctycnv)()); /* Case map routine */
union REGS regs;
char c;
if(cty_state==0)
{
cty_state=-1;
if(_osmajor>=3)
{
regs.x.dx=(unsigned int)&cty;
regs.x.ax=0x3800;
intdos(®s, ®s);
if(!regs.x.cflag)
{
cty_state=1; /* Accept any further calls */
ctycnv=(char (FAR *)())cty.co_case;
}
}
}
if(cty_state>0)
{
while(length>0)
{
if(ptr[0]>='a'&&ptr[0]<='z')
ptr[0]-=0x20; /* Convert to upper case */
else
if(ptr[0]>=0x80)
{
c=ptr[0];
asm mov al, c;
ctycnv();
asm mov c, al;
ptr[0]=c;
}
ptr++;
length--;
}
}
else
{
while(length>0)
{
if(ptr[0]>='a'&&ptr[0]<='z')
ptr[0]-=('a'-'A'); /* Convert to upper case */
ptr++;
length--;
}
}
#elif TARGET==OS2
COUNTRYCODE cc;
cc.country=cc.codepage=0;
DosCaseMap(length, &cc, ptr);
#else
unsigned char x;
/* ASR fix 10/04/2002: this fixes a GCC v 3.0 optimization bug */
while(length-->0)
{
x=*ptr;
*ptr++=toupper(x);
}
#endif
}
#endif
/* Sums two unixtime values (for systems with different implementation of
time_t and for Year 2106 compliance) */
#if SFX_LEVEL>=ARJ
time_t sum_time(time_t t1, time_t t2)
{
return(t1+t2);
}
#endif
/* Subtracts two unixtime values (for systems with different implementation of
time_t and for Year 2106 compliance) */
#ifdef REARJ
time_t sub_time(time_t t1, time_t t2)
{
return(t1-t2);
}
#endif
/* Depending of LFN support, converts the path specification to UPPERCASE */
#if SFX_LEVEL>=ARJSFX||defined(REARJ)
void case_path(char *s)
{
#if TARGET==DOS
if(lfn_supported==LFN_NOT_SUPPORTED)
strupper(s);
#else
/* Case-preserving or case-sensitive systems have nothing to do here */
#endif
}
#endif
/* Looks for duplicate drive specifications in the arguments, returns 1 if
found one, or 0 if none were found. */
#if SFX_LEVEL>=ARJ&&TARGET!=UNIX
int find_dupl_drivespecs(char **argtable, int args)
{
int cur_arg, cx_arg;
if(args<=1)
{
if(listchars_allowed==0||argtable[0][0]!=listchar)
return(0);
else
return(1);
}
else
{
for(cur_arg=0; cur_arg<args; cur_arg++)
{
if(argtable[cur_arg][1]!=':'&&strcmp(argtable[cur_arg], nonexist_name))
return(1);
}
for(cur_arg=0; cur_arg<args; cur_arg++)
for(cx_arg=0; cx_arg<args; cx_arg++)
{
if(cx_arg!=cur_arg)
{
if(argtable[cx_arg][0]==argtable[cur_arg][0])
return(1);
}
}
return(0);
}
}
#endif
/* Returns 0 if monopolized R/O access to the file can be granted, -1 if not */
#if SFX_LEVEL>=ARJSFXV
int file_test_access(char *name)
{
#if TARGET==DOS
char tmp_name[CCHMAXPATH];
#endif
int handle;
#if TARGET==DOS
if(_osmajor>=3&&!disable_sharing)
{
strcpy(tmp_name, name);
if(lfn_supported!=LFN_NOT_SUPPORTED)
w95_get_shortname(name, tmp_name, sizeof(tmp_name));
#if COMPILER==BCC
if((handle=_open(tmp_name, O_BINARY|O_DENYALL|O_RDONLY))==-1)
return(-1);
#elif COMPILER==MSC
if((handle=sopen(tmp_name, O_BINARY|O_RDONLY, SH_DENYRW))==-1)
return(-1);
#endif
close(handle);
}
return(0);
#elif TARGET==OS2||TARGET==WIN32
if((handle=sopen(name, O_BINARY|O_RDONLY, SH_DENYRW))==-1)
return(-1);
close(handle);
return(0);
#elif TARGET==UNIX
struct flock flk;
int rc;
if((handle=open(name, O_RDONLY))==-1)
return(-1);
memset(&flk, 0, sizeof(flk));
rc=fcntl(handle, F_GETLK, &flk);
close(handle);
return(((rc==-1&&errno!=EINVAL&&errno!=ENOSYS)||
(rc!=1&&flk.l_type==F_RDLCK))?-1:0);
#endif
}
#endif
/* Detect long filename support */
#if SFX_LEVEL>=ARJSFX||defined(REARJ)
int detect_lfns()
{
#if TARGET==DOS
if(_osmajor<7)
return(lfn_supported=0);
else
return(lfn_supported=w95_test_for_lfn(drive_c));
#else
return(lfn_supported=1); /* Setting to 0 will disable DTA/DTC
handling! */
#endif
}
#endif
/* Detect extended attribute support */
#if SFX_LEVEL>=ARJSFXV
int detect_eas()
{
#if TARGET==OS2
ea_supported=1;
#elif TARGET==WIN32
OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize=sizeof(osvi);
GetVersionEx(&osvi);
return(ea_supported=(osvi.dwPlatformId==VER_PLATFORM_WIN32_NT));
#else
ea_supported=0;
#endif
return(ea_supported);
}
#endif
/* Null subroutine for compiler bugs */
void nullsub(int arg, ...)
{
}
/* Delay */
#if SFX_LEVEL>=ARJSFX||defined(REARJ)||defined(REGISTER)||defined(ARJDISP)
void arj_delay(unsigned int seconds)
{
#if TARGET==DOS
#if COMPILER==MSC
long c_time, e_time, t_time;
/* This is inappropriate for some rare machines with non-standard timer
arrangement, e.g. Tandy 2000 but at least it doesn't involve
reprogramming the timer ports. */
e_time=(long)seconds*182L/10L;
while(e_time--)
{
g_timer(t_time);
c_time=t_time;
do
{
g_timer(c_time);
} while(c_time==t_time);
}
#else
sleep(seconds);
#endif
#elif TARGET==OS2
DosSleep(seconds*1000L);
#elif TARGET==WIN32
Sleep(seconds*1000L);
#else
sleep(seconds);
#endif
}
#endif
/* Collect memory statistics (for debugging purposes) */
#if SFX_LEVEL>=ARJSFXV
void mem_stats()
{
if(debug_enabled&&strchr(debug_opt, 'v')!=NULL)
{
#if SFX_LEVEL>=ARJ
msg_cprintf(0, M_MEMSTATS, coreleft(), verify_heap(), farcoreleft(), verify_far_heap(), get_stack_space());
#else
msg_cprintf(0, M_MEMSTATS, coreleft(), farcoreleft());
#endif
}
}
#endif
/* Interrupt 24h handler */
#if SFX_LEVEL>=ARJSFXV&&TARGET==DOS
#if COMPILER==BCC
void interrupt int24_fatal_handler(unsigned int bp, unsigned int di,
unsigned int si, unsigned int ds,
unsigned int es, unsigned int dx,
unsigned int cx, unsigned int bx,
unsigned int ax)
#elif COMPILER==MSC
void _interrupt _far int24_fatal_handler( unsigned int es, unsigned int ds,
unsigned int di, unsigned int si,
unsigned int bp, unsigned int sp,
unsigned int bx, unsigned int dx,
unsigned int cx, unsigned int ax,
unsigned int ip, unsigned int cs,
unsigned int flags)
#endif
{
ax=INT24_FAIL;
}
#endif
/* Check for the existence of file given; INT 24h handler is reset to ensure
that the FAIL action is automatically generated instead of prompting the
user for action. */
#if SFX_LEVEL>=ARJSFX||defined(REARJ)||defined(REGISTER)
int file_exists(char *name)
{
int rc;
#if TARGET==DOS
#if COMPILER==BCC
void interrupt (*oldhdl)();
#elif COMPILER==MSC
void (_interrupt _far *oldhdl)();
#endif
#if SFX_LEVEL>=ARJSFXV
if(is_dos_31)
rc=file_access(name, 0);
else
{
oldhdl=getvect(INT24);
setvect(INT24, int24_fatal_handler);
rc=file_access(name, 0);
setvect(INT24, oldhdl);
}
#else
rc=file_access(name, 0);
#endif
#elif TARGET==OS2
rc=file_access(name, 0);
#else
rc=file_access(name, F_OK);
#endif
return(!rc);
}
#endif
/* An extended fopen that supports long filenames and uses sharing. */
#if SFX_LEVEL>=ARJSFX||defined(REARJ)
FILE *file_open(char *name, char *mode)
{
#if TARGET==DOS
char tmp_name[CCHMAXPATH];
#endif
#if SFX_LEVEL>=ARJSFXV
char tmp_mode[10];
#endif
#if COMPILER!=BCC&&SFX_LEVEL>=ARJSFXV
int shflag;
#endif
#if SFX_LEVEL>=ARJSFXV||TARGET==DOS
int handle;
#endif
#if SFX_LEVEL>=ARJSFXV
int oflag;
#endif
#if TARGET==DOS
strcpy(tmp_name, name);
if(lfn_supported!=LFN_NOT_SUPPORTED)
{
if(strchr(mode, 'a')!=NULL||strchr(mode, 'w')!=NULL)
{
if(w95_access(name, 0))
{
/* If the file does not exist, continue here, otherwise get its short
name and process with fopen. */
if((handle=w95_creat(name, O_CREAT|O_TRUNC|O_WRONLY))==-1)
return(NULL);
else
close(handle);
/* At this point, the file has been created but has a zero length. Now
we can query its short name and use DOS functions to access it. */
}
}
w95_get_shortname(name, tmp_name, sizeof(tmp_name));
}
#endif
#if SFX_LEVEL>=ARJSFXV
if(disable_sharing)
return(fopen(name, mode));
else
{
strcpyn(tmp_mode, mode, 9);
switch(tmp_mode[0])
{
case 'r': oflag=0; break;
case 'w': oflag=O_CREAT|O_TRUNC; break;
case 'a': oflag=O_CREAT|O_APPEND; break;
default: return(NULL);
}
if(tmp_mode[1]=='+'||(tmp_mode[1]!='\0'&&tmp_mode[2]=='+'))
oflag|=O_RDWR;
else
oflag|=(tmp_mode[0]=='r')?O_RDONLY:O_WRONLY;
#if TARGET!=UNIX
if(tmp_mode[1]=='b'||tmp_mode[2]=='b')
oflag|=O_BINARY;
#endif
#if TARGET==DOS
if(_osmajor>=3) /* Sharing modes for DOS v 3.0+ */
{
#if COMPILER==BCC
if(tmp_mode[1]=='+'||tmp_mode[1]!='\0'&&tmp_mode[2]=='+')
oflag|=O_DENYWRITE;
else
oflag|=O_DENYNONE;
#else
shflag=(tmp_mode[1]=='+'||tmp_mode[1]!='\0'&&tmp_mode[2]=='+')?SH_DENYWR:SH_DENYNO;
#endif
}
#if COMPILER==BCC
handle=open(tmp_name, oflag, S_IREAD|S_IWRITE);
#elif COMPILER==MSC /* Advanced mode - designed for OS/2 */
if((handle=sopen(tmp_name, oflag, shflag, S_IREAD|S_IWRITE))==-1)
{
if(errno!=EACCES)
return(NULL);
/* Provoke INT 24h call */
f_file_ptr=name;
handle=open(tmp_name, oflag, S_IREAD|S_IWRITE);
f_file_ptr=NULL;
if(handle==-1) /* User abort */
return(NULL);
close(handle);
/* Try to reopen the file in shared mode */
if((handle=sopen(tmp_name, oflag, shflag, S_IREAD|S_IWRITE))==-1)
if((handle=open(tmp_name, oflag, S_IREAD|S_IWRITE))==-1)
return(NULL);
}
#endif
if(handle!=-1)
return(fdopen(handle, tmp_mode));
else
return(NULL);
#elif TARGET==OS2||TARGET==WIN32
{
shflag=(mode[1]=='+'||mode[1]!='\0'&&mode[2]=='+')?SH_DENYWR:SH_DENYNO;
handle=sopen(name, oflag, shflag, S_IREAD|S_IWRITE);
if(handle!=-1)
return((FILE *)fdopen(handle, mode));
else
return(NULL);
}
#elif TARGET==UNIX
{
struct flock flk;
/* Disengage any links so we don't follow them */
if(mode[0]=='w')
unlink(name);
/* Deny write activities if mixed-mode access */
if(mode[1]=='+'||(mode[1]!='\0'&&mode[2]=='+'))
{
memset(&flk, 0, sizeof(flk));
flk.l_type=F_WRLCK;
/* ASR fix 01/10/2003 -- re-fix to handle umask 022 correctly */
if((handle=open(name, oflag, 0644))==-1)
return(NULL);
if(fcntl(handle, F_SETLK, &flk)==-1&&errno!=EINVAL)
{
close(handle);
return(NULL);
}
return(fdopen(handle, mode));
}
else
return(fopen(name, mode));
}
#endif
}
#else
#if TARGET==DOS
return(fopen(tmp_name, mode));
#else
return(fopen(name, mode));
#endif
#endif
}
#endif
/* Convert the given path to uppercase and validate it */
#if SFX_LEVEL>=ARJSFXV
void default_case_path(char *dest, char *src)
{
case_path(strcpy(dest, validate_path(src, validate_style)));
}
#endif
/* Checks stdin against EOF (for raw input mode) */
#if (SFX_LEVEL>=ARJSFXV||defined(ARJDISP))&&TARGET==DOS
static void check_stdin()
{
int rc;
union REGS regs;
regs.x.ax=0x4400; /* IOCTL - query device */
regs.x.bx=0; /* STDIN */
intdos(®s, ®s);
rc=regs.x.dx;
#ifndef ARJDISP
if(!(rc&CHDI_NUL))
{
if(rc&CHDI_SET)
return;
if(!eof(0))
return;
}
error(M_CANTREAD);
#endif
}
#endif
/* Reads character from console */
#if SFX_LEVEL>=ARJSFXV||defined(ARJDISP)
int uni_getch()
{
#if TARGET==DOS
union REGS regs;
check_stdin();
regs.h.ah=0x08; /* Read character without echo */
intdos(®s, ®s);
return((int)(regs.h.al==0x0D?0x0A:regs.h.al));
#elif TARGET==OS2
KBDKEYINFO keyinfo;
KbdCharIn(&keyinfo, IO_WAIT, 0);
return(keyinfo.chChar);
#elif TARGET==WIN32
return(getch());
#elif defined(SUNOS)
static struct termio cookedmode,rawmode;
static int cookedok;
int key;
if (!cookedok)
{
ioctl(0,TCGETA,&cookedmode);
ioctl(0,TCGETA,&rawmode);
rawmode.c_iflag=IXON|IXOFF;
rawmode.c_oflag&=~OPOST;
rawmode.c_lflag=0;
rawmode.c_cc[VEOF]=1;
cookedok=1;
}
ioctl(0,TCSETAW,&rawmode);
key=getchar();
ioctl(0,TCSETAW,&cookedmode);
return key;
#else
return(getchar());
#endif
}
#endif
/* Returns current system time, in # of ticks since midnight */
#if SFX_LEVEL>=ARJSFXV||defined(REGISTER)
unsigned long get_ticks()
{
struct time sttime;
arj_gettime(&sttime);
return(((unsigned long)sttime.ti_hour*3600L+(unsigned long)sttime.ti_min*60L+
(unsigned long)sttime.ti_sec)*100L+(unsigned long)sttime.ti_hund);
}
#endif
/* Retrieves current directory */
#if SFX_LEVEL>=ARJSFX&&SFX_LEVEL<=ARJSFXV
void file_getcwd(char *buf, int len)
{
getcwd(buf, len);
}
#elif defined(REARJ)
char *file_getcwd(char *buf)
{
#if TARGET==DOS
if(lfn_supported!=LFN_NOT_SUPPORTED)
return(w95_cwd(buf));
else
return(getcwd(buf, CCHMAXPATH_W95));
#else
#if COMPILER==ICC
return(_getcwd(buf, CCHMAXPATH));
#else
return(getcwd(buf, CCHMAXPATH));
#endif
#endif
}
#endif
/* Returns switch character used by OS */
#ifdef REARJ
char get_sw_char()
{
#if TARGET==DOS
union REGS regs;
regs.x.ax=0x3700;
intdos(®s, ®s);
return(regs.h.dl);
#elif TARGET==OS2
return('/');
#else
return('-');
#endif
}
#endif
/* Returns the amount of free space for disk on which the given file is
located. */
#if SFX_LEVEL>=ARJSFXV||defined(REARJ)
unsigned long file_getfree(char *name)
{
#if TARGET==DOS
#if COMPILER==BCC
struct dfree dtable;
#elif COMPILER==MSC
struct diskfree_t dtable;
#endif
char drive=0;
while(name[0]==' ') /* Skip over leading spaces, if any */
name++;
if(name[1]==':')
drive=toupper(name[0])-0x40;
#if COMPILER==BCC
getdfree(drive, &dtable);
if(dtable.df_sclus==65535)
return(MAXLONG);
else
{
return((LONG_MAX/((long)dtable.df_bsec*dtable.df_sclus)<dtable.df_avail)?
LONG_MAX:
(long)dtable.df_bsec*dtable.df_sclus*dtable.df_avail);
}
#elif COMPILER==MSC
if(_dos_getdiskfree((unsigned int)drive, &dtable))
return(ULONG_MAX);
else
{
return((LONG_MAX/((long)dtable.bytes_per_sector*dtable.sectors_per_cluster)<dtable.avail_clusters)?
LONG_MAX:
(long)dtable.bytes_per_sector*dtable.sectors_per_cluster*dtable.avail_clusters);
}
#endif
#elif TARGET==OS2
USHORT drive=0;
FSALLOCATE fsinfo;
while(name[0]==' ')
name++;
if(name[1]==':')
drive=toupper(name[0])-0x40;
if(DosQFSInfo(drive, FSIL_ALLOC, (PBYTE)&fsinfo, sizeof(fsinfo)))
return(LONG_MAX);
else
return((LONG_MAX/(fsinfo.cSectorUnit*fsinfo.cbSector)<fsinfo.cUnitAvail)?
LONG_MAX:
fsinfo.cSectorUnit*fsinfo.cbSector*fsinfo.cUnitAvail);
#elif TARGET==WIN32
char tmp[4], *ptmp=NULL;
DWORD bps, spclu, fclu, clu;
while(name[0]==' ')
name++;
if(name[1]==':')
{
ptmp=tmp;
tmp[0]=toupper(name[0]);
tmp[1]=':';
tmp[2]='\\';
tmp[3]='\0';
}
if(!GetDiskFreeSpace(ptmp, &spclu, &bps, &fclu, &clu))
return(LONG_MAX);
else
return((LONG_MAX/(spclu*bps)<fclu)?LONG_MAX:spclu*bps*fclu);
#elif TARGET==UNIX
#if defined(__QNXNTO__)||defined(__sco__)||defined(SUNOS)
struct statvfs vfs;
if(statvfs(name, &vfs)==-1)
return(LONG_MAX);
return((LONG_MAX/512<vfs.f_bavail)?LONG_MAX:vfs.f_bavail*512);
#else
struct statfs sfs;
if(statfs(name, &sfs)==-1)
return(LONG_MAX);
return((LONG_MAX/512<sfs.f_bavail)?LONG_MAX:sfs.f_bavail*512);
#endif
#endif
}
#endif
/* findfirst() for external use */
#if SFX_LEVEL>=ARJSFXV
int file_find(char *name, struct file_properties *properties)
{
struct new_ffblk new_ffblk;
int attrib=STD_FI_ATTRS;
#if TARGET==UNIX
int u;
#endif
if(lfn_findfirst(name, &new_ffblk, attrib)!=0)
return(-1);
else
{
#if SFX_LEVEL>=ARJ
lfn_findclose(&new_ffblk);
finddata_to_properties(properties, &new_ffblk);
#else
properties->ftime=new_ffblk.ff_ftime;
properties->fsize=new_ffblk.ff_fsize;
properties->attrib=(ATTRIB)new_ffblk.ff_attrib;
#if TARGET!=UNIX
properties->type=(new_ffblk.ff_attrib&FATTR_DIREC)?ARJT_DIR:ARJT_BINARY;
properties->isarchive=(new_ffblk.ff_attrib&FATTR_ARCH)?1:0;
#else
u=uftype(new_ffblk.ff_attrib);
if(u&FATTR_DT_DIR)
properties->type=ARJT_DIR;
else if(u&FATTR_DT_UXSPECIAL)
properties->type=ARJT_UXSPECIAL;
else
properties->type=ARJT_BINARY;
properties->isarchive=1;
properties->l_search=new_ffblk.l_search;
properties->l_search.ref=0;
properties->islink=0;
#endif
#endif
return(0);
}
}
#endif
/* Returns the size of the given file */
#if SFX_LEVEL>=ARJ||defined(REARJ)
long file_getfsize(char *name)
{
#if SFX_LEVEL>=ARJ
struct new_ffblk new_ffblk;
int attrib=STD_FI_ATTRS;
if(lfn_findfirst(name, &new_ffblk, attrib)!=0)
return(0L);
else
{
lfn_findclose(&new_ffblk);
return(new_ffblk.ff_fsize);
}
#else
FILE *stream;
long rc;
if((stream=file_open(name, m_rb))==NULL)
return(0);
#if TARGET!=UNIX
rc=filelength(fileno(stream));
#else
fseek(stream, 0L, SEEK_END);
rc=ftell(stream);
#endif
fclose(stream);
return(rc);
#endif
}
#endif
/* Returns last modification time for the given file */
#if SFX_LEVEL>=ARJSFX||defined(REARJ)
unsigned long file_getftime(char *name)
{
#if TARGET==DOS
#if SFX_LEVEL>=ARJSFXV
struct new_ffblk new_ffblk;
int attrib=STD_FI_ATTRS;
if(lfn_findfirst(name, &new_ffblk, attrib)!=0)
return(0L);
else
{
#if SFX_LEVEL>=ARJ
lfn_findclose(&new_ffblk); /* Done automatically by ARJSFXV */
#endif
return(new_ffblk.ff_ftime);
}
#else
FILE *stream;
unsigned long rc;
if((stream=file_open(name, m_rb))==NULL)
return(0L);
#if COMPILER==BCC
getftime(fileno(stream), (struct ftime *)&rc);
#else
_dos_getftime(fileno(stream), (unsigned int *)&rc+1, (unsigned int *)&rc);
#endif
fclose(stream);
return(rc);
#endif
#elif TARGET==OS2
HFILE hf;
#ifdef __32BIT__
ULONG action;
#else
USHORT action;
#endif
FILESTATUS fstatus;
if(DosOpen(name, &hf, &action, 0L, 0, FILE_OPEN, OPEN_ACCESS_READONLY|OPEN_SHARE_DENYNONE, 0L))
return(0L);
DosQFileInfo(hf, FIL_STANDARD, &fstatus, sizeof(fstatus));
DosClose(hf);
return(make_ftime(fstatus.fdateLastWrite, fstatus.ftimeLastWrite));
#elif TARGET==WIN32
FILETIME ftime, atime, ctime;
HANDLE hf;
if((hf=CreateFile(name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0))==INVALID_HANDLE_VALUE)
return(0L);
if(!GetFileTime(hf, &ctime, &atime, &ftime))
return(0L);
CloseHandle(hf);
return(dosify_time(&ftime));
#elif TARGET==UNIX
struct stat st;
if(lstat(name, &st)==-1)
return(0L);
return(st.st_mtime);
#endif
}
#endif
/* Queries the volume label for the specified drive. The destination buffer
will contain "" if the volume label is missing. */
#if SFX_LEVEL>=ARJ&&defined(HAVE_VOL_LABELS)
int file_getlabel(char *label, char drive, ATTRIB *attrib, unsigned long *ftime)
{
#if TARGET==DOS
struct new_ffblk new_ffblk;
char wildcard[10];
if(drive=='\0')
wildcard[0]='\0';
else
{
wildcard[0]=drive;
wildcard[1]=':';
wildcard[2]='\0';
}
strcat(wildcard, root_wildcard);
if(lfn_findfirst(wildcard, &new_ffblk, FATTR_LABEL))
return(0); /* Pretty incorrect but, if no files
are present, it won't be called */
if(_osmajor>2)
{
while(!(new_ffblk.ff_attrib&FATTR_LABEL))
{
if(!lfn_findnext(&new_ffblk))
return(0);
}
lfn_findclose(&new_ffblk);
}
strcpy(label, new_ffblk.ff_name);
*attrib=(ATTRIB)new_ffblk.ff_attrib;
*ftime=new_ffblk.ff_ftime;
return(0);
#elif TARGET==OS2
FSINFO fsinfo;
USHORT rc;
rc=DosQFSInfo(drive=='\0'?0:drive-0x40, FSIL_VOLSER, (PBYTE)&fsinfo, sizeof(fsinfo));
if(rc)
return((rc==ERROR_NO_VOLUME_LABEL)?0:-1);
strcpy(label, fsinfo.vol.szVolLabel);
return(0);
#elif TARGET==WIN32
DWORD dummy;
return(!GetVolumeInformation(NULL, label, CCHMAXPATH, NULL, &dummy, &dummy, NULL, 0));
#endif
}
#endif
/* Read a line from the stdin w/echo, returning the number of bytes read. */
#if SFX_LEVEL>=ARJSFX||defined(REARJ)
int read_line(char *buf, int size)
{
#if TARGET==DOS
union REGS regs;
#if SFX_LEVEL>=ARJSFX||defined(REARJ)
char tmp_buf[160]; /* Actually, DOS limit is 128 */
#endif
unsigned int ioctl_set, ioctl_isbinary, ioctl_response;
int chars_read;
int cchar;
regs.x.ax=0x4400; /* IOCTL - query device */
regs.x.bx=0; /* STDIN */
intdos(®s, ®s);
ioctl_response=regs.x.dx;
ioctl_set=(ioctl_response&CHDI_SET)?1:0;
ioctl_isbinary=(ioctl_response&CHDI_BINARY)?1:0;
#ifdef REARJ
ioctl_isbinary=ioctl_set&ioctl_isbinary;
#endif
if(ioctl_set&&ioctl_isbinary||_osmajor<3)
{
#if SFX_LEVEL>=ARJSFXV
tmp_buf[0]=min(size, INPUT_LIMIT); /* Number of input positions */
tmp_buf[1]=tmp_buf[0]-1; /* Number of recallable positions */
regs.h.ah=0x0A;
regs.x.dx=(unsigned int)tmp_buf;
intdos(®s, ®s);
chars_read=(int)tmp_buf[1];
if(tmp_buf[chars_read+2]=='\x0D')
tmp_buf[chars_read+2]='\0'; /* Convert to ASCIIZ */
strcpy(buf, tmp_buf+2);
nputlf(); /* v 2.72+ fixup to live happily
under Unices */
#elif defined(REARJ)
tmp_buf[0]=min(size, INPUT_LIMIT); /* Number of input positions */
cgets(tmp_buf);
chars_read=(int)tmp_buf[1];
strcpy(buf, tmp_buf+2);
#else
error(M_RAW_INPUT_REJECTED);
#endif
}
else
{
for(chars_read=0; (cchar=fgetc(stdin))!=EOF&&cchar!=LF; chars_read++)
{
if(chars_read<size-1)
buf[chars_read]=(char)cchar;
}
if(cchar==-1)
error(M_CANTREAD);
buf[chars_read]='\0';
}
msg_cprintf(0, cr); /* ASR fix 03/02/2003 for COLOR_OUTPUT */
return(chars_read);
#elif (TARGET==OS2&&(COMPILER==MSC||defined(LIBC)))||TARGET==WIN32||TARGET==UNIX
int chars_read;
int cchar;
for(chars_read=0; (cchar=fgetc(stdin))!=EOF&&cchar!=LF; chars_read++)
{
if(chars_read<size-1)
buf[chars_read]=(char)cchar;
}
if(cchar==-1)
error(M_CANTREAD);
buf[chars_read]='\0';
return(chars_read);
#else /* A platform-neutral solution */
char *p;
#ifdef DEBUG
debug_report(dbg_cur_file, __LINE__, 'W');
#endif
fgets(buf, size, stdin); /* DIRTY HACK */
if((p=strchr(buf, '\n'))!=NULL)
*p='\0';
if((p=strchr(buf, '\r'))!=NULL)
*p='\0';
return(strlen(buf));
#endif
}
#endif
/* Returns file access mode in character format */
#if SFX_LEVEL>=ARJSFX
void get_mode_str(char *str, unsigned int mode)
{
#if TARGET==UNIX
int i;
str[0]='-';
for(i=0; i<3; i++)
{
str[(2-i)*3+1]=(mode&4)?'r':'-';
str[(2-i)*3+2]=(mode&2)?'w':'-';
str[(2-i)*3+3]=(mode&1)?'x':'-';
mode>>=3;
}
str[10]=' ';
str[11]=(mode&FATTR_SGID)?'G':'-';
str[12]=(mode&FATTR_SUID)?'U':'-';
str[13]=(mode&FATTR_SVTX)?'A':'-';
str[14]='\0';
#else
strcpy(str, attrib_buf);
if(mode&FATTR_ARCH)
str[0]='A';
if(mode&FATTR_SYSTEM)
str[1]='S';
if(mode&FATTR_HIDDEN)
str[2]='H';
if(mode&FATTR_RDONLY)
str[3]='R';
#endif
}
#endif
/* Retrieves an environment string */
#if TARGET==OS2&&SFX_LEVEL>=ARJSFX
static char FAR *get_env_str(char *t)
{
#ifdef __32BIT__
PSZ rc;
if(DosScanEnv(t, &rc))
return(NULL);
return((char FAR *)rc);
#else
USHORT selector;
USHORT cmd_offset;
char FAR *env_ptr;
int i;
DosGetEnv(&selector, &cmd_offset);
env_ptr=MAKEP(selector, 0);
while(*env_ptr!='\0')
{
for(i=0; t[i]!='\0'; i++)
if(env_ptr[i]=='\0'||env_ptr[i]!=t[i])
break;
if(t[i]=='\0'&&env_ptr[i]=='=')
return(env_ptr+i+1);
env_ptr+=i;
while(*env_ptr++!='\0');
}
return(NULL);
#endif
}
#endif
/* Reserves memory and gets an environment string */
#if TARGET==OS2&&SFX_LEVEL>=ARJSFX
char *malloc_env_str(char *t)
{
char FAR *str, FAR *sptr;
char *rc, *rptr;
int i;
if((str=get_env_str(t))==NULL)
return(NULL);
i=1;
for(sptr=str; *sptr!='\0'; sptr++)
i++;
if((rc=(char *)malloc(i))==NULL)
return(NULL); /* Env. string was too long */
rptr=rc;
for(sptr=str; *sptr!='\0'; sptr++)
*rptr++=*sptr;
*rptr='\0';
return(rc);
}
#endif
/* A small exported stub for C RTL to be happy */
#if TARGET==OS2&&COMPILER==MSC&&SFX_LEVEL>=ARJ
char *getenv(const char *str)
{
return(NULL); /* No such a string */
}
#endif
/* Executes the given program directly, returning its RC */
#if defined(REARJ)||(SFX_LEVEL>=ARJSFX&&TARGET==OS2)
int exec_pgm(char *cmdline)
{
char tmp_cmd[CMDLINE_LENGTH+CCHMAXPATH];
#if TARGET==OS2 /* && SFX_LEVEL>=ARJSFX */
char *params;
int p_pos;
#ifdef REARJ
#ifdef __32BIT__
PPIB ppib=NULL;
PTIB ptib=NULL;
ULONG child_pid, session_pid;
REQUESTDATA qr;
ULONG qrc;
ULONG cb_res;
#else
PID child_pid, session_pid;
PIDINFO pidi;
QUEUERESULT qr;
USHORT qrc;
USHORT cb_res;
#endif
STARTDATA sd;
char qname[CCHMAXPATH];
HQUEUE queue;
USHORT errcode;
PUSHORT res;
BYTE elem_priority;
char *argv[PARAMS_MAX];
int arg;
static char param_sep[]=" ";
#else
RESULTCODES rc;
#endif
#ifndef REARJ
memset(tmp_cmd, 0, sizeof(tmp_cmd));
if((params=strchr(cmdline, ' '))!=NULL)
{
p_pos=params-cmdline;
if(p_pos>0)
memcpy(tmp_cmd, cmdline, p_pos);
strcpy(tmp_cmd+p_pos+1, params);
}
else
strcpy(tmp_cmd, cmdline);
if(!DosExecPgm(NULL, 0, EXEC_SYNC, tmp_cmd, NULL, &rc, tmp_cmd))
return(rc.codeResult);
else
return(-1);
#else
strncpy(tmp_cmd, cmdline, sizeof(tmp_cmd)-1);
tmp_cmd[sizeof(tmp_cmd)-1]='\0';
argv[0]=strtok(tmp_cmd, param_sep);
if(argv[0]==NULL)
return(-1);
for(arg=1; arg<PARAMS_MAX; arg++)
if((argv[arg]=strtok(NULL, param_sep))==NULL)
break;
if(arg>=PARAMS_MAX)
return(-1);
if((errcode=spawnvp(P_WAIT, argv[0], argv))!=0xFFFF)
return(errcode);
else
{
/* Try running the program asynchronously using the SESMGR */
memset(tmp_cmd, 0, sizeof(tmp_cmd));
if((params=strchr(cmdline, ' '))!=NULL)
{
p_pos=params-cmdline;
if(p_pos>0)
memcpy(tmp_cmd, cmdline, p_pos);
strcpy(tmp_cmd+p_pos+1, params);
}
else
strcpy(tmp_cmd, cmdline);
#ifdef __32BIT__
DosGetInfoBlocks(&ptib, &ppib); /* Fixed for PJ24109 */
sprintf(qname, rearj_q_fmt, ppib->pib_ulpid);
#else
DosGetPID(&pidi);
sprintf(qname, rearj_q_fmt, pidi.pid);
#endif
if(DosCreateQueue(&queue, QUE_FIFO, qname))
return(-1);
memset(&sd, 0, sizeof(sd));
sd.Length=sizeof(sd);
sd.Related=TRUE;
sd.FgBg=TRUE; /* Start in background */
sd.PgmName=tmp_cmd;
sd.PgmInputs=params;
sd.TermQ=qname;
sd.InheritOpt=1;
if(DosStartSession(&sd, &session_pid, &child_pid))
return(-1);
while((qrc=DosReadQueue(queue, &qr, &cb_res, (PVOID FAR *)&res, 0, DCWW_WAIT,
&elem_priority, 0))==0)
{
errcode=res[1];
#ifdef __32BIT__
DosFreeMem(res);
if(qr.ulData==0)
break;
#else
DosFreeSeg(SELECTOROF(res));
if(qr.usEventCode==0)
break;
#endif
}
DosCloseQueue(queue);
return(errcode);
}
#endif
#else
char *argv[PARAMS_MAX];
int arg;
static char param_sep[]=" ";
#if TARGET==UNIX
int pid, rc, status;
struct sigaction ign_set, intr, quit;
#endif
strncpy(tmp_cmd, cmdline, sizeof(tmp_cmd)-1);
tmp_cmd[sizeof(tmp_cmd)-1]='\0';
argv[0]=strtok(tmp_cmd, param_sep);
if(argv[0]==NULL)
return(-1);
for(arg=1; arg<PARAMS_MAX; arg++)
{
if((argv[arg]=strtok(NULL, param_sep))==NULL)
break;
#if TARGET==UNIX
if(argv[arg][0]=='"'&&argv[arg][strlen(argv[arg])-1]=='"')
{
argv[arg]++;
argv[arg][strlen(argv[arg])-1]='\0';
}
#endif
}
if(arg>=PARAMS_MAX)
return(-1);
#if TARGET!=UNIX
return(spawnvp(P_WAIT, argv[0], argv));
#else
if((pid=fork())==-1)
return(-1);
if(pid==0)
{
exit(execvp(argv[0], argv));
}
else
{
/* Prevent signals from appearing */
ign_set.sa_handler=SIG_IGN;
ign_set.sa_flags=0;
sigemptyset(&ign_set.sa_mask);
sigaction(SIGINT, &ign_set, &intr);
sigaction(SIGQUIT, &ign_set, &quit);
/* Wait for the child */
do
rc=waitpid(pid, &status, 0);
while(rc==-1&&errno==EINTR);
/* Restore signals */
sigaction(SIGINT, &intr, NULL);
sigaction(SIGQUIT, &quit, NULL);
return(status);
}
#endif
#endif
}
#endif
/* Executes a program or a shell command */
#if SFX_LEVEL>=ARJSFX&&TARGET==OS2
int system_cmd(char *cmd)
{
char tmp_cmd[CMDLINE_LENGTH+CCHMAXPATH];
char *comspec;
comspec=malloc_env_str("COMSPEC");
strcpy(tmp_cmd, (comspec==NULL)?"cmd" EXE_EXTENSION:comspec);
free_env_str(comspec);
strcat(tmp_cmd, " /C");
strcat(tmp_cmd, cmd);
return((exec_pgm(tmp_cmd)==-1)?-1:0);
}
#endif
/* Retrieves the name of executable */
#if (SFX_LEVEL>=ARJSFXJR||defined(REARJ))
#ifndef SKIP_GET_EXE_NAME
void get_exe_name(char *dest)
{
#if TARGET==DOS
char FAR *env_ptr;
unsigned short s_seg;
unsigned int i;
unsigned short psp_seg;
if(_osmajor<3)
{
if(_osmajor!=2||_osminor!=11)
strcpy(dest, default_exe);
else
{
#if COMPILER==BCC
s_seg=*(unsigned short FAR *)MK_FP(_psp, 2)-56;
#else /* Microsoft C changes the MCB order */
s_seg=(*(unsigned short FAR *)0x00400013<<6)-56;
#endif
env_ptr=MK_FP(s_seg, 9); /* Not actually ENV but it works! */
i=0;
while(*env_ptr!='\0'&&i<FILENAME_MAX)
dest[i++]=*(env_ptr++);
dest[i]='\0';
if(dest[i-4]!='.'||dest[i-3]!='E'||dest[i-2]!='X'||dest[i-1]!='E')
strcpy(dest, default_exe);
}
}
else
{
#ifdef ASM8086
asm{
push ax
push bx
mov ah, 62h
int 21h
mov psp_seg, bx
pop bx
pop ax
}
#else
{
union REGS regs;
regs.x.ax=0x6200;
intdos(®s, ®s);
psp_seg=regs.x.bx;
}
#endif
env_ptr=MK_FP(*(unsigned short FAR *)MK_FP(psp_seg, 0x2C), 0);
while(*(short FAR *)(env_ptr++)!=0);
env_ptr+=3;
i=0;
while(*env_ptr!='\0'&&i<FILENAME_MAX)
dest[i++]=*(env_ptr++);
dest[i]='\0';
}
#elif TARGET==OS2
#ifdef __32BIT__
PTIB ptib;
PIB *ppib;
DosGetInfoBlocks(&ptib, &ppib);
DosQueryModuleName(ppib->pib_hmte, CCHMAXPATH, dest);
#else
USHORT selector;
USHORT cmd_offset;
char FAR *sptr;
DosGetEnv(&selector, &cmd_offset);
sptr=MAKEP(selector, 0);
while(*(SHORT FAR *)sptr!=0)
sptr++;
sptr+=2;
while(*sptr!='\0')
*dest++=*sptr++;
*dest='\0';
#endif
#elif TARGET==WIN32
GetModuleFileName(NULL, dest, CCHMAXPATH);
#endif
}
#else /* SKIP_GET_EXE_NAME */
void get_exe_name(char *dest, char *arg)
{
char *ps, *pe;
int l;
int len;
if(strchr(arg, PATHSEP_DEFAULT)!=NULL)
{
strcpy(dest, arg);
return;
}
len=FILENAME_MAX-2-strlen(arg);
ps=getenv("PATH");
do
{
pe=strchr(ps, ':');
if(pe==NULL)
pe=ps+(l=strlen(ps));
else
l=pe-ps;
if(l>=len)
l=len-1;
memcpy(dest, ps, l);
if(dest[l-1]!=PATHSEP_DEFAULT)
dest[l++]=PATHSEP_DEFAULT;
strcpy(dest+l, arg);
if(!access(dest, F_OK)||errno==EINVAL)
return;
ps=pe+1;
} while(*pe!='\0');
/* Notes to porters: below are the totally unlikely "last-chance" values.
The DOS legacy parts of ARJ open its executable when looking for SFX
modules and help screens. In most cases we are happy with argv[0] and
PATH lookup. When that fails, we assume the locations below, and if
they are missing altogether, the corresponding code will gracefully
terminate. */
#if SFX_LEVEL==ARJ
strcpy(dest, "/usr/local/bin/arj");
#elif SFX_LEVEL==ARJSFXV
strcpy(dest, "./arjsfxv");
#elif SFX_LEVEL==ARJSFX
strcpy(dest, "./arjsfx");
#elif SFX_LEVEL==ARJSFXJR
strcpy(dest, "./arjsfxjr");
#elif defined(REARJ)
strcpy(dest, "/usr/local/bin/rearj");
#else
dest[0]='\0';
#endif
}
#endif
#endif
/* Read a line from console without echoing it (used only when prompting for
passwords) */
#if SFX_LEVEL>=ARJSFXV
int read_line_noecho(char *buf, int size)
{
int chars_read;
int cchar;
chars_read=0;
cchar=uni_getch();
while(cchar!=LF&&cchar!=CR)
{
if(cchar=='\b')
{
if(chars_read!=0)
chars_read--;
}
else if(chars_read<size-1)
buf[chars_read++]=cchar;
cchar=uni_getch();
}
buf[chars_read]='\0';
nputlf();
return(chars_read);
}
#endif
/* Returns the number of bytes per allocation unit for the drive that contains
the given file. NOTE: this is an advisory value to align multivolume
archives tightly when the free space is a constraint. Not to be used in
converting allocation units to bytes. */
#if SFX_LEVEL>=ARJSFXV||defined(REARJ)
unsigned int get_bytes_per_cluster(char *name)
{
#if TARGET==DOS
#if COMPILER==BCC
struct dfree dtable;
#elif COMPILER==MSC
struct diskfree_t dtable;
#endif
char drive=0;
while(name[0]==' ') /* Skip over leading spaces, if any */
name++;
if(name[1]==':')
drive=toupper(name[0])-0x40;
#if COMPILER==BCC
getdfree(drive, &dtable);
if(dtable.df_sclus==65535)
return(1024);
else
return(dtable.df_bsec*dtable.df_sclus);
#elif COMPILER==MSC
if(_dos_getdiskfree((unsigned int)drive, &dtable))
return(1024);
else
return(dtable.bytes_per_sector*dtable.sectors_per_cluster);
#endif
#elif TARGET==OS2
USHORT drive=0;
FSALLOCATE fsinfo;
unsigned long rc;
while(name[0]==' ')
name++;
if(name[1]==':')
drive=toupper(name[0])-0x40;
if(DosQFSInfo(drive, FSIL_ALLOC, (PBYTE)&fsinfo, sizeof(fsinfo)))
return(ULONG_MAX);
rc=(unsigned long)fsinfo.cbSector*fsinfo.cSectorUnit;
return(rc>UINT_MAX?UINT_MAX:rc);
#elif TARGET==WIN32
char fpn[CCHMAXPATH];
LPTSTR fnc;
DWORD bps, spclu, fclu, clu;
if(!GetFullPathName(name, CCHMAXPATH, fpn, &fnc)||fpn[1]!=':')
return(ULONG_MAX);
fpn[3]='\0';
if(!GetDiskFreeSpace(fpn, &spclu, &bps, &fclu, &clu))
return(ULONG_MAX);
else
return(bps*spclu);
#elif TARGET==UNIX
#ifdef linux
struct statvfs vfs;
if(statvfs(name, &vfs)==-1)
return(512);
else
return(vfs.f_bsize);
#else
return(512);
#endif
#endif
}
#endif
/* Returns canonical Windows 95 longname for a given file */
#if SFX_LEVEL>=ARJ&&TARGET==DOS
void get_canonical_longname(char *cname, char *name)
{
int name_ofs; /* Offset of LFN within name[] */
char *cname_ptr, *name_ptr;
if(name[1]==':'||name[0]==PATHSEP_DEFAULT||name[0]==PATHSEP_UNIX||name[0]=='.')
name_ofs=0;
else
{
w95_get_longname(cur_dir_spec, cname, CCHMAXPATH);
name_ofs=strlen(cname);
}
w95_get_longname(name, cname, CCHMAXPATH);
if(name_ofs==0)
return;
cname_ptr=cname;
name_ptr=name+name_ofs;
if(name_ptr[0]==PATHSEP_DEFAULT||name_ptr[0]==PATHSEP_UNIX)
name_ptr++;
while(name_ptr[0]!='\0')
{
(cname_ptr++)[0]=(name_ptr++)[0];
}
cname_ptr[0]='\0';
}
#endif
/* Returns canonical short name for a given Windows 95 long filename */
#if SFX_LEVEL>=ARJ&&TARGET==DOS
void get_canonical_shortname(char *cname, char *name)
{
int name_ofs; /* Offset of LFN within name[] */
char *cname_ptr, *name_ptr;
if(name[1]==':'||name[0]==PATHSEP_DEFAULT||name[0]==PATHSEP_UNIX||name[0]=='.')
name_ofs=0;
else
{
w95_get_shortname(cur_dir_spec, cname, CCHMAXPATH);
name_ofs=strlen(cname);
}
w95_get_shortname(name, cname, CCHMAXPATH);
if(name_ofs==0)
return;
cname_ptr=cname;
name_ptr=name+name_ofs;
if(name_ptr[0]==PATHSEP_DEFAULT||name_ptr[0]==PATHSEP_UNIX)
name_ptr++;
while(name_ptr[0]!='\0')
{
(cname_ptr++)[0]=(name_ptr++)[0];
}
cname_ptr[0]='\0';
}
#endif
/* Displays a string pointed to by a FAR pointer on the stdout. Used only by
internal functions of ENVIRON.C. */
#if SFX_LEVEL>=ARJSFXV&&TARGET==DOS
static void far_ttyout(char FAR *str)
{
union REGS regs;
char FAR *str_ptr;
str_ptr=str;
regs.h.ah=2; /* Jung does it in the loop but we
don't. */
while(str_ptr[0]!='\0')
{
regs.h.dl=(str_ptr++)[0];
intdos(®s, ®s);
}
}
#endif
/* Reads one character from keyboard without echo. Used only by internal
functions of ENVIRON.C */
#if SFX_LEVEL>=ARJSFXV&&TARGET==DOS
static int flush_and_getch()
{
union REGS regs;
regs.x.ax=0xC08; /* Clear buffer and then getch */
intdos(®s, ®s);
return((int)regs.h.al);
}
#endif
/* "Intelligent" interrupt 24h handler */
#if SFX_LEVEL>=ARJSFXV&&TARGET==DOS
#if COMPILER==BCC
static void interrupt int24_smart_handler(unsigned int bp, unsigned int di,
unsigned int si, unsigned int ds,
unsigned int es, unsigned int dx,
unsigned int cx, unsigned int bx,
unsigned int ax)
#elif COMPILER==MSC
void _interrupt _far int24_smart_handler( unsigned int es, unsigned int ds,
unsigned int di, unsigned int si,
unsigned int bp, unsigned int sp,
unsigned int bx, unsigned int dx,
unsigned int cx, unsigned int ax,
unsigned int ip, unsigned int cs,
unsigned int flags)
#endif
{
char msg[40];
char query[64];
#if COMPILER==BCC
struct DOSERROR exterr;
#elif COMPILER==MSC
union REGS regs;
#endif
char dev[12];
char *action;
FMSG *final_response;
int user_action;
char response;
char drive;
int errcode;
#if COMPILER==MSC
int dos_errcode;
#endif
int dev_ctr;
#if COMPILER==MSC
_enable();
dos_errcode=errcode=di&0xFF;
#endif
action=malloc_fmsg(ax&INT24_DPF_WRITING?M_WRITING:M_READING);
#if SFX_LEVEL>=ARJ
if(errcode>0x12)
errcode=0x12; /* Ignore DOS 4.x error codes */
if(_osmajor>=3)
{
#if COMPILER==BCC
errcode=dosexterr(&exterr);
#elif COMPILER==MSC
regs.h.ah=0x59;
regs.x.bx=0;
intdos(®s, ®s);
errcode=regs.x.ax;
#endif
if(errcode<0x13||errcode>0x25)
errcode=0x25; /* Ignore DOS 2.x/4.x error codes */
errcode-=0x13;
}
msg_strcpyn((FMSG *)msg, DOS_MSGS[errcode], sizeof(msg)-1);
#else
if(errcode>0x0D)
errcode=0x0D;
#endif
if(ax&INT24_IO_ERROR)
{
if(((char *)MK_FP(bp, si))[5]&0x80)
far_ttyout((char FAR *)M_CRIT_ERROR);
else
{
for(dev_ctr=0; dev_ctr<8; dev_ctr++)
dev[dev_ctr]=((char *)MK_FP(bp, si))[10+dev_ctr];
dev[dev_ctr]='\0';
msg_sprintf(query, M_DEVICE, msg, action, dev);
far_ttyout((char FAR *)query);
}
}
else
{
#if COMPILER==MSC
if(f_file_ptr!=NULL&&dos_errcode==0x0D||dos_errcode==0x0E)
msg_sprintf(query, M_FOPEN, msg, f_file_ptr);
else
#endif
{
drive=(char)ax+'A';
msg_sprintf(query, M_DRIVE, msg, action, drive);
}
far_ttyout((char FAR *)query);
}
user_action=1;
if(ignore_errors)
{
free_fmsg(action);
ax=INT24_FAIL;
}
else
{
query[1]=CR;
query[2]=LF;
query[3]='\0';
do
{
far_ttyout((char FAR *)M_OK_TO_RETRY);
query[0]=response=(char)flush_and_getch();
far_ttyout((char FAR *)query);
}
while((final_response=msg_strchr(M_REPLIES, (char)toupper(response)))==NULL||
(user_action=final_response-M_REPLIES)>2);
free_fmsg(action);
if(user_action==0)
ax=INT24_RETRY;
else if(user_action==2||is_dos_31) /* DOS 2.x/3.0 do not support FAIL */
ax=INT24_ABORT;
else
{
ax=INT24_FAIL;
user_wants_fail=-1;
}
}
}
#endif
/* Installs the "smart" handler */
#if SFX_LEVEL>=ARJSFXV
void install_smart_handler()
{
#if TARGET==DOS
setvect(INT24, int24_smart_handler);
#elif TARGET==OS2
if(ignore_errors)
{
#ifdef __32BIT__
DosError(FERR_DISABLEHARDERR|FERR_DISABLEEXCEPTION);
#else
DosError(HARDERROR_DISABLE);
DosError(EXCEPTION_DISABLE);
#endif
}
#elif TARGET==UNIX
/* No hard error daemon here */
#endif
}
#endif
/* Checks if the given file handle actually represents a file */
#if SFX_LEVEL>=ARJSFXV
int is_file(FILE *stream)
{
#if TARGET==DOS
#if COMPILER==BCC
return(ioctl(fileno(stream), 0)&CHDF_NOT_FILE?1:0);
#elif COMPILER==MSC
union REGS regs;
regs.x.ax=0x4400;
regs.x.bx=fileno(stream);
intdos(®s, ®s);
return(regs.x.dx&CHDF_NOT_FILE?1:0);
#endif
#elif TARGET==OS2||TARGET==WIN32
return(fileno(stream)<2?1:0); /* Another dirty hack... */
#else
struct stat st;
fstat(fileno(stream), &st);
/* ASR fix 02/05/2003: ineed, we shouldn't check for FATTR_DT_REG here! */
return(st.st_mode==S_IFREG);
#endif
}
#endif
/* Returns 1 if the given file is stored on removable media */
#if SFX_LEVEL>=ARJSFXV
int file_is_removable(char *name)
{
#if TARGET!=UNIX
char drive=0;
while(name[0]==' ')
name++;
if(name[1]==':')
drive=toupper(name[0])-0x40;
else
drive=getdisk()+1;
#if TARGET==DOS
if(_osmajor<3) /* DOS 2.x - A: and B: are removable */
return(drive==1||drive==2);
else
#if COMPILER==BCC
return(ioctl(drive, 8)==0);
#elif COMPILER==MSC
{
union REGS regs;
regs.x.ax=0x4408;
regs.x.bx=(unsigned int)drive;
intdos(®s, ®s);
return(regs.x.ax==0);
}
#endif
#elif TARGET==OS2||TARGET==WIN32
return(drive==1||drive==2);
#endif
#else
return(0); /* BUGBUG: fix for floppies! */
#endif
}
#endif
/* Checks if the given file handle represends a terminal or console */
#if SFX_LEVEL>=ARJSFXV||defined(REARJ)
int is_tty(FILE *stream)
{
return(isatty(fileno(stream))==0?0:1);
}
#endif
/* mkdir() - Borland's implementation */
#if SFX_LEVEL>=ARJSFX||defined(REARJ)
int file_mkdir(char *name)
{
#if TARGET==DOS
if(lfn_supported!=LFN_NOT_SUPPORTED)
return(w95_mkdir(name));
else
return(mkdir(name));
#elif TARGET==OS2||TARGET==WIN32
#ifdef __EMX__
return(mkdir(name, 0));
#else
return(mkdir(name));
#endif
#else
return(mkdir(name, 0755));
#endif
}
#endif
#if (TARGET==OS2||TARGET==WIN32)&&!defined(TILED)
/* The "workhorse" routine for wildcard matching */
static int wild_proc(char *wild, char *name, UINT32 *trail, unsigned int first, unsigned int nesting)
{
#define set_trail(w,r) trail[((w)*MAXWILDAST+(r))>>5]|=(1<<(((w)*MAXWILDAST+(r))&31))
#define check_trail(w,r) (trail[((w)*MAXWILDAST+(r))>>5]&(1<<(((w)*MAXWILDAST+(r))&31)))
int rc=1;
if(nesting>=MAXWILDAST)
return(1);
while(*wild)
{
switch(*wild)
{
case '?':
if(*name=='\0')
return(1);
wild++;
break;
case '*':
if(!check_trail(first, nesting)&&!wild_proc(wild+1, name, trail, first, nesting+1))
return(0);
if(*name=='\0')
return(1);
set_trail(first, nesting);
break;
default:
if(toupper(*name)!=toupper(*wild))
return(1);
wild++;
}
name++;
first++;
}
return(*name!=*wild);
#undef set_trail
#undef check_trail
}
#endif
/* Returns non-zero if the given filename matches the given wildcard */
#if SFX_LEVEL>=ARJSFX||defined(REARJ)
int match_wildcard(char *name, char *wcard)
{
#if TARGET==DOS
if(!stricmp(wcard, all_wildcard))
return(1);
while(wcard[0]!='\0')
{
switch(wcard[0])
{
case '*':
while(name[0]!='\0'&&name[0]!='.')
name++;
while(wcard[0]!='\0'&&wcard[0]!='.')
wcard++;
break;
case '.':
if(name[0]!='\0')
{
if(name[0]!=wcard[0])
return(0);
name++;
}
wcard++;
break;
case '?':
if(name[0]!='\0'&&name[0]!='.')
name++;
wcard++;
break;
default:
if(toupper(name[0])!=toupper(wcard[0]))
return(0);
name++;
wcard++;
}
}
return(name[0]=='\0'?1:0);
#elif TARGET==OS2&&defined(TILED)
char dest[CCHMAXPATH];
/* This is a speedy and compact hack for 16-bit mode */
DosEditName(0x0001, name, wcard, dest, CCHMAXPATH);
return(!stricmp(dest, name));
#elif TARGET==OS2||TARGET==WIN32
char tmp_wild[CCHMAXPATH];
/* Bit map: (maximum # of wildcards) x (maximum pattern length) */
UINT32 trail[(MAXWILDAST*CCHMAXPATHCOMP+31)>>5];
int l;
memcpy(tmp_wild, wcard, (l=strlen(wcard))+1);
memset(trail, 0, sizeof(trail));
if(tmp_wild[l-1]=='.')
{
if(strchr(name, '.')!=NULL)
return(1);
tmp_wild[l-1]='\0';
}
return(!wild_proc(tmp_wild, name, trail, 0, 0));
#else
int rc;
rc=fnmatch(wcard, name, 0);
#ifndef TOLERANT_FNMATCH
if(rc)
rc=strcmp_os(wcard, name);
#endif
return(!rc);
#endif
}
#endif
/* rmdir() */
#if SFX_LEVEL>=ARJ||defined(REARJ)
int file_rmdir(char *name)
{
#if TARGET!=UNIX
if(clear_archive_bit)
dos_chmod(name, FATTR_NOARCH);
#endif
#if TARGET==DOS
if(lfn_supported!=LFN_NOT_SUPPORTED)
return(w95_rmdir(name));
else
return(rmdir(name));
#else
return(rmdir(name));
#endif
}
#endif
/* unlink() */
#if SFX_LEVEL>=ARJSFX||defined(REARJ)
int file_unlink(char *name)
{
#if SFX_LEVEL>=ARJSFXV&&TARGET!=UNIX
if(file_test_access(name))
return(-1);
#if SFX_LEVEL>=ARJ
if(clear_archive_bit)
dos_chmod(name, 0);
#else
if(overwrite_ro)
dos_chmod(name, 0);
#endif
#endif
#if TARGET==DOS
if(lfn_supported!=LFN_NOT_SUPPORTED)
return(w95_unlink(name));
else
return(unlink(name));
#else
return(unlink(name));
#endif
}
#endif
/* rename() */
#if SFX_LEVEL>=ARJ||defined(REARJ)
int file_rename(char *oldname, char *newname)
{
#if TARGET==DOS
if(lfn_supported!=LFN_NOT_SUPPORTED)
return(w95_rename(oldname, newname));
else
return(rename(oldname, newname));
#else
return(rename(oldname, newname));
#endif
}
#endif
/* This INT 24h handler always forces the operation that causes it to fail */
#if SFX_LEVEL>=ARJ&&TARGET==DOS
#if COMPILER==BCC
static void interrupt int24_autofail_handler(unsigned int bp, unsigned int di,
unsigned int si, unsigned int ds,
unsigned int es, unsigned int dx,
unsigned int cx, unsigned int bx,
unsigned int ax)
#elif COMPILER==MSC
void _interrupt _far int24_autofail_handler(unsigned int es, unsigned int ds,
unsigned int di, unsigned int si,
unsigned int bp, unsigned int sp,
unsigned int bx, unsigned int dx,
unsigned int cx, unsigned int ax,
unsigned int ip, unsigned int cs,
unsigned int flags)
#endif
{
char msg[40];
char query[64];
#if COMPILER==BCC
struct DOSERROR exterr;
#elif COMPILER==MSC
union REGS regs;
#endif
char *action;
char drive;
int errcode;
#if COMPILER==MSC
_enable();
#endif
action=malloc_fmsg(ax&INT24_DPF_WRITING?M_WRITING:M_READING);
errcode=di&0xFF;
if(errcode>0x12)
errcode=0x12; /* Ignore DOS 4.x error codes */
if(_osmajor>=3)
{
#if COMPILER==BCC
errcode=dosexterr(&exterr);
#elif COMPILER==MSC
regs.h.ah=0x59;
regs.x.bx=0;
intdos(®s, ®s);
errcode=regs.x.ax;
#endif
if(errcode<0x13||errcode>0x25)
errcode=0x25; /* Ignore DOS 2.x/4.x error codes */
errcode-=0x13;
}
msg_strcpyn((FMSG *)msg, DOS_MSGS[errcode], sizeof(msg)-1);
drive=(char)(ax&0xFF)+'A';
msg_sprintf(query, M_DRIVE, msg, action, drive);
far_ttyout(query);
free_fmsg(action);
ax=INT24_FAIL;
}
#endif
/* Clears the archive attribute */
#if SFX_LEVEL>=ARJ
int dos_clear_arch_attr(char *name)
{
#if TARGET!=UNIX
int cur_attr, result;
#if TARGET==DOS
#if COMPILER==BCC
void interrupt (*oldhdl)();
#elif COMPILER==MSC
void (_interrupt _far *oldhdl)();
#endif
#endif
cur_attr=file_chmod(name, 0, 0)&STD_FILE_ATTR;
if((cur_attr&~FATTR_ARCH)==cur_attr)
return(0);
cur_attr&=~FATTR_ARCH;
#if TARGET==DOS
if(is_dos_31)
result=file_chmod(name, 1, cur_attr);
else
{
oldhdl=getvect(INT24);
setvect(INT24, int24_autofail_handler);
result=file_chmod(name, 1, cur_attr);
setvect(INT24, oldhdl);
}
#elif TARGET==OS2||TARGET==WIN32
result=file_chmod(name, 1, cur_attr);
#endif
return(result==-1?-1:0);
#else
return(0);
#endif
}
#endif
/* Flushes the disk cache and resets the drive (W95) */
#if SFX_LEVEL>=ARJ&&TARGET==DOS
static int w95_resetdrive(int drive)
{
union REGS regs;
struct SREGS sregs;
memset(&sregs, 0, sizeof(sregs));
regs.x.cx=W95_FLUSH_CACHE;
regs.x.dx=drive;
return(call_dos_int(W95_RESETDRIVE, ®s, &sregs)?-1:0);
}
#endif
/* Flushes the disk cache and resets the drive (W95) */
#if SFX_LEVEL>=ARJ
int reset_drive(char *name)
{
#if TARGET==DOS
int drive;
while(name[0]==' ')
name++;
if(name[1]==':')
drive=(int)(toupper(name[0]))-0x40;
else
drive=getdisk()+1;
if(_osmajor<7) /* DOS 2.x - A: and B: are removable */
{
bdos(0x0D, 0, 0);
return(1);
}
else
{
if(w95_test_for_lfn(drive_c))
{
w95_resetdrive(drive);
return(1);
}
else
return(0);
}
#elif TARGET==OS2
HFILE hf;
#ifdef __32BIT__
ULONG action;
#else
USHORT action;
#endif
if(DosOpen(name, &hf, &action, 0L, 0, FILE_OPEN, OPEN_ACCESS_READONLY|OPEN_SHARE_DENYNONE, 0L))
return(0);
#ifdef TILED
DosBufReset(hf);
#else
DosResetBuffer(hf);
#endif
DosClose(hf);
return(0);
#elif TARGET==WIN32
return(0);
#elif TARGET==UNIX
sync();
return(0);
#endif
}
#endif
/* Checks for pending keystrokes and clears the keyboard buffer. */
#if SFX_LEVEL>=ARJSFXV||defined(REARJ)
int fetch_keystrokes()
{
#if TARGET==DOS
union REGS regs;
int rc=0;
while(kbhit())
{
regs.h.ah=7;
intdos(®s, ®s);
rc=1;
}
return(rc);
#elif TARGET==OS2
#if COMPILER==MSC
int rc=0;
KBDKEYINFO keyinfo;
while(kbhit())
{
rc=1;
KbdCharIn(&keyinfo, IO_WAIT, 0);
}
return(rc);
#else
KBDKEYINFO keyinfo;
int rc, pressed;
int frc=0;
do
{
rc=KbdPeek(&keyinfo, 0);
pressed=rc?0:(keyinfo.fbStatus&KBDTRF_FINAL_CHAR_IN);
if(pressed)
{
frc=1;
KbdCharIn(&keyinfo, IO_WAIT, 0);
}
} while(pressed);
return(frc);
#endif
#else
return(0); /* Not implemented */
#endif
}
#endif
/* Changes the current directory and/or drive */
#if SFX_LEVEL>=ARJSFX&&SFX_LEVEL<=ARJSFXV
void file_chdir(char *dir)
{
#ifdef HAVE_DRIVES
int l;
char *tmp_dir;
if((l=strlen(dir))>2)
{
if(dir[1]==':')
if((dir[l-1]==PATHSEP_DOS||dir[l-1]==PATHSEP_UNIX)&&(dir[1]!=':'||l!=3))
{
tmp_dir=malloc(l);
if(tmp_dir==NULL) /* ASR fix 29/10/2000 */
return;
strcpy(tmp_dir, dir);
tmp_dir[l-1]='\0';
chdir(tmp_dir);
free(tmp_dir);
}
}
#else
chdir(dir);
#endif
}
#elif defined(REARJ) /* REARJ modification (LFN-capable) */
int file_chdir(char *dir)
{
#ifdef HAVE_DRIVES
char dest_disk;
if(dir[1]==':')
{
dest_disk=toupper(dir[0])-'A';
if(setdisk(dest_disk)<dest_disk)
return(1);
if(dir[2]=='\0')
return(1);
}
#if TARGET==DOS
if(lfn_supported)
return(w95_chdir(dir)?1:0);
else
return(chdir(dir)?1:0);
#elif TARGET==OS2||TARGET==WIN32
return(chdir(dir)?1:0);
#endif
#else
return(chdir(dir));
#endif
}
#endif
/* This routine needs to be moved on top of file_unlink. */
#if SFX_LEVEL>=ARJSFX||defined(REARJ)
int dos_chmod(char *name, int attrib)
{
#if TARGET!=UNIX
return(file_chmod(name, 1, attrib&STD_FILE_ATTR)==-1?-1:0);
#else
return(file_chmod(name, 1, attrib)==-1?-1:0);
#endif
}
#endif
/* Changes the size of the file (Borland-specific implementation) */
#if SFX_LEVEL>=ARJ
int file_chsize(FILE *stream, unsigned long size)
{
#if TARGET!=UNIX
return(chsize(fileno(stream), size));
#else
return(ftruncate(fileno(stream), size));
#endif
}
#endif
/* Sets last modification date/time for a file specified by handle */
#if (SFX_LEVEL==ARJSFXJR||SFX_LEVEL>=ARJ)&&TARGET!=UNIX
int file_setftime_on_hf(int hf, unsigned long ftime)
{
#if TARGET==DOS
#if COMPILER==BCC
return(setftime(hf, (struct ftime *)&ftime));
#elif COMPILER==MSC
return(_dos_setftime(hf, *((unsigned int *)&ftime+1), *(unsigned int *)&ftime));
#endif
#elif TARGET==OS2
HFILE hfc;
FILESTATUS fstatus;
hfc=(HFILE)hf; /* Dirty hack but it works */
DosQFileInfo(hfc, FIL_STANDARD, &fstatus, sizeof(fstatus));
*(USHORT *)&fstatus.fdateLastWrite=ftime>>16;
*(USHORT *)&fstatus.ftimeLastWrite=ftime&0xFFFF;
return(DosSetFileInfo(hfc, FIL_STANDARD, (PBYTE)&fstatus, sizeof(fstatus)));
#elif TARGET==WIN32
return(-1); /* Nobody wants to play it under
Win32 (fileno is NOT a HANDLE) */
#endif
}
#endif
/* Updates the modification date/time field in the file's directory entry for
a file that has been already open. */
#if SFX_LEVEL>=ARJ&&TARGET!=UNIX
int file_setftime_on_stream(FILE *stream, unsigned long ftime)
{
return(file_setftime_on_hf(fileno(stream), ftime));
}
#endif
/* UNIX file time request router */
#if TARGET==UNIX&&(SFX_LEVEL>=ARJSFX||defined(REARJ))
static int ftime_router(char *name, int idx, unsigned long ftime)
{
struct stat st;
struct utimbuf ut;
int rc;
#if SFX_LEVEL>=ARJ
struct stat resolved_st;
int res_cnt, protect_resolved=0;
char resolved_name[CCHMAXPATH];
#endif
if(lstat(name, &st)==-1)
return(-1);
/* If it is a symlink, protect the original file */
if(S_ISLNK(st.st_mode))
{
#if SFX_LEVEL>=ARJ
if(!symlink_accuracy)
return(-1);
res_cnt=readlink(name, resolved_name, sizeof(resolved_name)-1);
if(res_cnt>0)
{
resolved_name[res_cnt]='\0';
if(stat(name, &resolved_st)!=-1)
protect_resolved=1;
}
#else
return(-1);
#endif
}
ut.actime=(idx==UFTREQ_ATIME)?ftime:st.st_atime;
ut.modtime=(idx==UFTREQ_FTIME)?ftime:st.st_mtime;
rc=utime(name, &ut);
#if SFX_LEVEL>=ARJ
if(protect_resolved)
{
ut.actime=resolved_st.st_atime;
ut.modtime=resolved_st.st_mtime;
rc=utime(resolved_name, &ut);
}
#endif
return(rc);
}
#endif
/* Updates the last access date field in the file's directory entry (unlike
file_setftime, standard parameters are passed). */
#if SFX_LEVEL>=ARJSFXV
int file_setatime(char *name, unsigned long ftime)
{
#if TARGET==DOS
FILE *stream;
int result;
if(lfn_supported!=LFN_NOT_SUPPORTED)
{
if((stream=fopen(name, m_rbp))==NULL)
if((stream=fopen(name, m_rb))==NULL)
return(-1);
result=w95_set_dta(fileno(stream), ftime);
fclose(stream);
return(result);
}
else
return(0);
#elif TARGET==OS2
#ifdef __32BIT__
FILESTATUS3 fstatus;
#else
FILESTATUS fstatus;
#endif
#ifdef __32BIT__
DosQueryPathInfo(name, FIL_STANDARD, (PBYTE)&fstatus, sizeof(fstatus));
#else
DosQPathInfo(name, FIL_STANDARD, (PBYTE)&fstatus, sizeof(fstatus), 0L);
#endif
*(USHORT *)&fstatus.fdateLastAccess=ftime>>16;
*(USHORT *)&fstatus.ftimeLastAccess=ftime&0xFFFF;
#ifdef __32BIT__
return(DosSetPathInfo(name, FIL_STANDARD, (PBYTE)&fstatus, sizeof(fstatus), 0));
#else
return(DosSetPathInfo(name, FIL_STANDARD, (PBYTE)&fstatus, sizeof(fstatus), 0, 0L));
#endif
#elif TARGET==WIN32
HANDLE hf;
FILETIME satime, sftime, sctime;
if((hf=CreateFile(name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0))==INVALID_HANDLE_VALUE)
return(-1);
GetFileTime(hf, &sctime, &satime, &sftime);
ntify_time(&satime, ftime);
SetFileTime(hf, &sctime, &satime, &sftime);
CloseHandle(hf);
return(0);
#elif TARGET==UNIX
return(ftime_router(name, UFTREQ_ATIME, ftime));
#endif
}
#endif
/* Updates the creation date/time field in the file's directory entry (unlike
file_setftime, standard parameters are passed). */
#if SFX_LEVEL>=ARJSFXV
int file_setctime(char *name, unsigned long ftime)
{
#if TARGET==DOS
FILE *stream;
int result;
#endif
#if TARGET==DOS
if(lfn_supported!=LFN_NOT_SUPPORTED)
{
if((stream=fopen(name, m_rbp))==NULL)
if((stream=fopen(name, m_rb))==NULL)
return(-1);
result=w95_set_dtc(fileno(stream), ftime);
fclose(stream);
return(result);
}
else
return(0);
#elif TARGET==OS2
#ifdef __32BIT__
FILESTATUS3 fstatus;
#else
FILESTATUS fstatus;
#endif
#ifdef __32BIT__
DosQueryPathInfo(name, FIL_STANDARD, (PBYTE)&fstatus, sizeof(fstatus));
#else
DosQPathInfo(name, FIL_STANDARD, (PBYTE)&fstatus, sizeof(fstatus), 0L);
#endif
*(USHORT *)&fstatus.fdateCreation=ftime>>16;
*(USHORT *)&fstatus.ftimeCreation=ftime&0xFFFF;
#ifdef __32BIT__
return(DosSetPathInfo(name, FIL_STANDARD, (PBYTE)&fstatus, sizeof(fstatus), 0));
#else
return(DosSetPathInfo(name, FIL_STANDARD, (PBYTE)&fstatus, sizeof(fstatus), 0, 0L));
#endif
#elif TARGET==WIN32
HANDLE hf;
FILETIME satime, sftime, sctime;
if((hf=CreateFile(name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0))==INVALID_HANDLE_VALUE)
return(-1);
GetFileTime(hf, &sctime, &satime, &sftime);
ntify_time(&sctime, ftime);
SetFileTime(hf, &sctime, &satime, &sftime);
CloseHandle(hf);
return(0);
#elif TARGET==UNIX
return(ftime_router(name, UFTREQ_CTIME, ftime));
#endif
}
#endif
/* Updates the modification date/time field in the file's directory entry */
#if SFX_LEVEL>=ARJSFX||defined(REARJ)||defined(NEED_SETFTIME_HACK)
int file_setftime(char *name, unsigned long ftime)
{
#if TARGET==DOS
FILE *stream;
int result;
if((stream=fopen(name, m_rbp))==NULL)
#if SFX_LEVEL>=ARJSFXV
if((stream=fopen(name, m_rb))==NULL)
#endif
return(-1);
#if COMPILER==BCC
result=setftime(fileno(stream), (struct ftime *)&ftime);
#elif COMPILER==MSC
result=_dos_setftime(fileno(stream), *((unsigned int *)&ftime+1), *(unsigned int *)&ftime);
#endif
fclose(stream);
return(result);
#elif TARGET==OS2
#ifdef __32BIT__
FILESTATUS3 fstatus;
#else
FILESTATUS fstatus;
#endif
#ifdef __32BIT__
DosQueryPathInfo(name, FIL_STANDARD, (PBYTE)&fstatus, sizeof(fstatus));
#else
DosQPathInfo(name, FIL_STANDARD, (PBYTE)&fstatus, sizeof(fstatus), 0L);
#endif
*(USHORT *)&fstatus.fdateLastWrite=ftime>>16;
*(USHORT *)&fstatus.ftimeLastWrite=ftime&0xFFFF;
#ifdef __32BIT__
return(DosSetPathInfo(name, FIL_STANDARD, (PBYTE)&fstatus, sizeof(fstatus), 0));
#else
return(DosSetPathInfo(name, FIL_STANDARD, (PBYTE)&fstatus, sizeof(fstatus), 0, 0L));
#endif
#elif TARGET==WIN32
HANDLE hf;
FILETIME satime, sftime, sctime;
if((hf=CreateFile(name, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0))==INVALID_HANDLE_VALUE)
return(-1);
GetFileTime(hf, &sctime, &satime, &sftime);
ntify_time(&sftime, ftime);
SetFileTime(hf, &sctime, &satime, &sftime);
CloseHandle(hf);
return(0);
#elif TARGET==UNIX
return(ftime_router(name, UFTREQ_FTIME, ftime));
#endif
}
#endif
/* Sets the volume label */
#if SFX_LEVEL>=ARJSFXV&&defined(HAVE_DRIVES)
int file_setlabel(char *label, char drive, ATTRIB attrib, unsigned long ftime)
{
#if TARGET==DOS
union REGS regs;
char fnm[64];
struct xfcb xfcb;
char dta[64];
char FAR *saved_dta;
int handle;
if(_osmajor<2)
return(-1);
/* Set DTA so it'll point to an internal structure */
saved_dta=getdta();
setdta((char FAR *)dta);
/* Fill the extended FCB structure */
xfcb.xfcb_flag=0xFF;
xfcb.xfcb_attr=FATTR_LABEL;
if(drive!='\0')
xfcb.xfcb_fcb.fcb_drive=toupper(drive)-0x40;
strcpy(xfcb.xfcb_fcb.fcb_name, fcb_mask);
/* Find first matching file (i.e., volume label) using this FCB. Note that
the current directory does not need to be root. */
regs.h.ah=0x11; /* findfirst() for FCB */
regs.x.dx=(unsigned int)&xfcb;
intdos(®s, ®s);
setdta(saved_dta); /* Temporarily restore the DTA */
if(_osmajor==2)
{
/* If there's a label, just rename it */
if(regs.h.al==0)
{
#if COMPILER==BCC
parsfnm(label, (struct fcb *)(dta+0x17), 3);
#elif COMPILER==MSC
regs.x.ax=0x2903;
regs.x.si=(unsigned int)label;
regs.x.di=(unsigned int)(dta+0x17);
intdos(®s, ®s);
#endif
regs.h.ah=0x17; /* rename() for FCB */
regs.x.dx=(unsigned int)dta;
intdos(®s, ®s);
}
/* Otherwise, create a new file */
else
{
#if COMPILER==BCC
parsfnm(label, &xfcb.xfcb_fcb, 3);
#elif COMPILER==MSC
regs.x.ax=0x2903;
regs.x.si=(unsigned int)label;
regs.x.di=(unsigned int)&xfcb.xfcb_fcb;
intdos(®s, ®s);
#endif
regs.h.ah=0x16; /* create file using FCB */
regs.x.dx=(unsigned int)&xfcb;
intdos(®s, ®s);
/* Close file if it has been created successfully */
if(regs.h.al==0)
{
regs.h.ah=0x10; /* fclose() for FCB */
regs.x.dx=(unsigned int)&xfcb;
intdos(®s, ®s);
}
}
return((int)regs.h.al);
}
/* DOS 3.x */
else
{
if(regs.h.al==0) /* Check RC from findfirst() */
{
regs.h.ah=0x13; /* unlink() for FCB */
regs.x.dx=(unsigned int)dta;
intdos(®s, ®s);
}
if(drive!='\0')
{
fnm[0]=drive;
fnm[1]=':';
fnm[2]='\0';
}
else
fnm[0]='\0';
strcat(fnm, label);
if((handle=_creat(fnm, FATTR_LABEL))==EOF)
return(-1);
#if COMPILER==BCC
setftime(handle, (struct ftime *)&ftime);
#elif COMPILER==MSC
return(_dos_setftime(handle, *((unsigned int *)&ftime+1), *(unsigned int *)&ftime));
#endif
_close(handle);
return(0);
}
#elif TARGET==OS2
VOLUMELABEL vl;
strcpyn(vl.szVolLabel, label, 11);
vl.cch=strlen(vl.szVolLabel);
return(DosSetFSInfo(drive=='\0'?0:drive-0x40, FSIL_VOLSER, (PBYTE)&vl, sizeof(vl)));
#elif TARGET==WIN32
char vn[4];
vn[0]=drive;
vn[1]=':';
vn[2]='\\';
vn[3]='\0';
return(!SetVolumeLabel(vn, label));
#endif
}
#endif
/* Sets the file type */
#if SFX_LEVEL>=ARJ
int file_settype(FILE *stream, int istext)
{
#if TARGET!=UNIX
fflush(stream);
#if COMPILER==HIGHC&&!defined(LIBC)
setmode(stream, istext?_O_TEXT:_O_BINARY);
#else
setmode(fileno(stream), istext?O_TEXT:O_BINARY);
#endif
#endif
return(1);
}
#endif
/* Sets the file API mode */
#if SFX_LEVEL>=ARJ
void set_file_apis(int is_ansi)
{
#if TARGET==WIN32
if(is_ansi)
SetFileApisToANSI();
else
SetFileApisToOEM();
#endif
}
#endif
/* Creates a subdirectory tree (qmode specifies if the user is to be asked).
Returns 1 if an error occured */
#if SFX_LEVEL>=ARJSFX
#if SFX_LEVEL>=ARJSFXV
int create_subdir_tree(char *path, int qmode, int datatype)
#else
int create_subdir_tree(char *path, int datatype)
#endif
{
char tmp_path[CCHMAXPATH]; /* Jung uses 256 but it's incorrect */
char *tmp_ptr;
int access_status;
int no_queries; /* 1 automatically forces "yes" */
#if SFX_LEVEL>=ARJSFXV
tmp_ptr=validate_path(path, VALIDATE_ALL);
no_queries=qmode||prompt_for_mkdir;
#else
tmp_ptr=validate_path(path);
no_queries=yes_on_all_queries||make_directories;
#endif
while((tmp_ptr=find_delimiter(tmp_ptr, datatype))!=NULL)
{
strcpyn(tmp_path, path, tmp_ptr-path+1);
if((access_status=file_chmod(tmp_path, 0, 0))==-1)
{
if(!no_queries)
{
#if SFX_LEVEL>=ARJSFXV
msg_sprintf(misc_buf, M_QUERY_CREATE_DIR, path);
/* If the user forbids creation, return 1 */
if((no_queries=query_action(0, QUERY_CREATE_DIR, misc_buf))==0)
return(1);
#else
msg_cprintf(0, M_QUERY_CREATE_DIR, path);
if((no_queries=query_action())==0)
return(1);
#endif
}
if(file_mkdir(tmp_path)&&errno!=ENOENT)
{
/* Creation failed, inform the user about it */
#if SFX_LEVEL>=ARJSFXV
msg_cprintf(H_ERR, M_CANT_MKDIR, tmp_path);
#else
msg_cprintf(H_ERR, M_CANT_MKDIR, tmp_path);
#endif
#if SFX_LEVEL>=ARJ
error_report();
#endif
return(1);
}
}
else
{
/* If a file with the same name exists, jerk off... */
#if TARGET!=UNIX
if(!(access_status&FATTR_DIREC))
#else
struct stat st;
if((stat(tmp_path, &st)==-1)||!S_ISDIR(st.st_mode))
#endif
{
#if SFX_LEVEL>=ARJSFXV
msg_cprintf(H_ERR, M_CANT_MKDIR, tmp_path);
#else
msg_cprintf(H_ERR, M_CANT_MKDIR, tmp_path);
#endif
return(1);
}
}
if(*tmp_ptr!='\0')
tmp_ptr++;
}
return(0);
}
#endif
/* Returns 1 if the given filename is valid, 0 otherwise */
#if SFX_LEVEL>=ARJSFXV
int is_filename_valid(char *name)
{
return(name[0]=='\0'?0:1);
}
#endif
/* Returns 1 if the given file is a directory, 0 otherwise */
#if SFX_LEVEL>=ARJSFXV
int is_directory(char *name)
{
int attrib;
attrib=file_chmod(name, 0, 0);
if(attrib==-1)
return(0);
else
#if TARGET!=UNIX
return(attrib&FATTR_DIREC?1:0);
#else
return(S_ISDIR(attrib));
#endif
}
#endif
/* Allocate memory and create a wildcard that expands to all files of the
subdirectory given. */
#if SFX_LEVEL>=ARJ
char *malloc_subdir_wc(char *name)
{
char tmp_wc[WC_RESERVE];
char *wc_ptr;
tmp_wc[0]=PATHSEP_DEFAULT;
strcpy(tmp_wc+1, all_wildcard);
wc_ptr=malloc_msg(strlen(tmp_wc)+strlen(name)+2);
strcpy(wc_ptr, name);
strcat(wc_ptr, tmp_wc);
return(wc_ptr);
}
#endif
/* Copies one file to another, performing a check if needed. Issues a native
API if the target platform supports it (note: Win32 does not provide a
way to verify the file so we avoid the CopyFile function under Win32) */
#if SFX_LEVEL>=ARJ
int file_copy(char *dest, char *src, int chk)
{
#if TARGET==OS2
#ifdef __32BIT__
BOOL32 vf;
APIRET rc;
#else
BOOL vf;
USHORT rc;
#endif
if(chk)
{
#ifdef __32BIT__
DosQueryVerify(&vf);
#else
DosQVerify(&vf);
#endif
DosSetVerify(1);
}
#ifdef __32BIT__
rc=DosCopy(src, dest, DCPY_EXISTING);
#else
rc=DosCopy(src, dest, DCPY_EXISTING, 0);
#endif
if(chk)
{
msg_cprintf(0, M_TESTING, dest);
DosSetVerify(vf);
}
switch(rc)
{
case NO_ERROR:
break;
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
case ERROR_ACCESS_DENIED:
case ERROR_SHARING_VIOLATION:
msg_cprintf(H_ERR, M_CANTOPEN, src);
break;
case ERROR_DISK_FULL:
msg_cprintf(0, M_DISK_FULL);
break;
default:
msg_cprintf(0, M_CRC_ERROR);
break;
}
if(rc)
{
nputlf();
return(-1);
}
return(0);
#else
FILE *istream, *ostream;
char *buf, *dbuf;
unsigned int bytes_read;
istream=file_open(src, m_rb);
if(istream==NULL)
{
error_report();
msg_cprintf(H_ERR, M_CANTOPEN, src);
nputlf();
return(-1);
}
ostream=file_open(dest, m_wb);
if(ostream==NULL)
{
fclose(istream);
error_report();
msg_cprintf(H_ERR, M_CANTOPEN, dest);
nputlf();
return(-1);
}
buf=malloc_msg(PROC_BLOCK_SIZE);
mem_stats();
while((bytes_read=fread(buf, 1, PROC_BLOCK_SIZE, istream))>0)
{
if(fwrite(buf, 1, bytes_read, ostream)!=bytes_read)
{
msg_cprintf(0, M_DISK_FULL);
nputlf();
break;
}
}
free(buf);
if(fclose(ostream))
{
fclose(istream);
return(-1);
}
if(fclose(istream))
return(-1);
if(file_is_removable(dest))
reset_drive(dest);
if(bytes_read==0&&chk)
{
msg_cprintf(0, M_TESTING, dest);
istream=file_open(src, m_rb);
if(istream==NULL)
{
error_report();
msg_cprintf(H_ERR, M_CANTOPEN, src);
nputlf();
return(-1);
}
ostream=file_open(dest, m_rb);
if(ostream==NULL)
{
fclose(istream);
error_report();
msg_cprintf(H_ERR, M_CANTOPEN, dest);
nputlf();
return(-1);
}
buf=malloc_msg(PROC_BLOCK_SIZE/2);
dbuf=malloc_msg(PROC_BLOCK_SIZE/2);
while((bytes_read=fread(buf, 1, PROC_BLOCK_SIZE/2, istream))>0)
{
if(fread(dbuf, 1, PROC_BLOCK_SIZE/2, ostream)!=bytes_read)
break;
if(memcmp(buf, dbuf, bytes_read))
break;
}
free(buf);
free(dbuf);
fclose(ostream);
fclose(istream);
msg_cprintf(0, (FMSG *)vd_space);
msg_cprintf(0, bytes_read==0?M_OK:M_CRC_ERROR);
}
return((bytes_read>0)?-1:0);
#endif
}
#endif
#if SFX_LEVEL>=ARJ||defined(REARJ)
/* ASR fix 02/04/2003: Reduce memory consumption on 16-bit systems. */
#ifdef TILED
static char *pack_fname(char *f)
{
char *rc;
if((rc=(char *)realloc(f, strlen(f)+1))==NULL)
return(f);
return(rc);
}
#else
#define pack_fname(f) (f)
#endif
/* Recursive subdirectory search helper */
#ifndef REARJ
int wild_subdir(struct flist_root *root, struct flist_root *search_flist, char *name, int expand_wildcards, int recurse_subdirs, int file_type, FILE_COUNT *count)
#else
int wild_subdir(struct flist_root *root, char *name, int file_type, int expand_wildcards, int recurse_subdirs, FILE_COUNT *count)
#endif
{
int attr_mask; /* Narrowing criteria */
struct new_ffblk *pnew_ffblk=NULL; /* OS-dependent data block */
char *subdir_spec=NULL; /* Subdirectory name */
char *tmp_name=NULL;
int result=0, rc;
char *subdir_wildcard=NULL; /* Subdirectory entries */
if(recurse_subdirs)
{
attr_mask=STD_DIR_ATTR;
if(file_type!=FETCH_DEFAULT)
#if TARGET!=UNIX
attr_mask|=FATTR_HIDDEN|FATTR_SYSTEM|FATTR_RDONLY;
#else
attr_mask|=FATTR_DT_REG|FATTR_DT_UXSPECIAL;
#endif
#ifndef REARJ
#if TARGET==UNIX
if(filter_attrs)
{
if(file_attr_mask&TAG_SYSTEM)
attr_mask|=FATTR_SYSTEM;
if(file_attr_mask&TAG_HIDDEN)
attr_mask|=FATTR_HIDDEN;
if(file_attr_mask&TAG_RDONLY)
attr_mask|=FATTR_RDONLY;
#if TARGET==UNIX
if(file_attr_mask&TAG_UXSPECIAL)
attr_mask|=FATTR_DT_UXSPECIAL;
if(file_attr_mask&TAG_DIREC)
attr_mask|=FATTR_DT_DIR;
#endif
}
#endif
#endif
if((subdir_spec=(char *)malloc(strlen(name)+WC_RESERVE))==NULL)
{
result=-1;
goto l_error;
}
split_name(name, subdir_spec, NULL);
strcat(subdir_spec, all_wildcard);
case_path(subdir_spec);
subdir_spec=pack_fname(subdir_spec);
if((pnew_ffblk=(struct new_ffblk *)malloc(sizeof(struct new_ffblk)))==NULL)
{
result=-1;
goto l_error;
}
rc=lfn_findfirst(subdir_spec, pnew_ffblk, attr_mask);
while(rc==0)
{
if(
/* Entries like "." and ".." are skipped */
pnew_ffblk->ff_attrib&STD_DIR_ATTR&&strcmp(pnew_ffblk->ff_name, cur_dir_spec)&&strcmp(pnew_ffblk->ff_name, up_dir_spec)
#if TARGET==UNIX&&SFX_LEVEL>=ARJ
/* Disallow circular symlinks - the symlink will be stored but will
not be recursed into */
&&link_search(&sl_entries, &pnew_ffblk->l_search, NULL, 0)==FLS_NONE
#endif
)
{
/* Reallocate these */
if((subdir_wildcard=(char *)realloc(subdir_wildcard, CCHMAXPATHCOMP))==NULL||
(tmp_name=(char *)realloc(tmp_name, CCHMAXPATH+20))==NULL)
{
result=-1;
goto l_error;
}
split_name(name, tmp_name, subdir_wildcard);
subdir_wildcard=pack_fname(subdir_wildcard);
if(strlen(tmp_name)+strlen(pnew_ffblk->ff_name)+strlen(subdir_wildcard)+2>=CCHMAXPATHCOMP)
msg_cprintf(H_ERR, M_MAXPATH_EXCEEDED, CCHMAXPATH, tmp_name);
else
{
strcat(tmp_name, pnew_ffblk->ff_name);
strcat(tmp_name, pathsep_str);
strcat(tmp_name, subdir_wildcard);
case_path(tmp_name);
tmp_name=pack_fname(tmp_name);
#ifdef REARJ
if(wild_list(root, tmp_name, file_type, expand_wildcards, recurse_subdirs, count))
{
result=-1;
goto l_error;
}
#else
if(wild_list(root, search_flist, tmp_name, expand_wildcards, recurse_subdirs, file_type, count))
{
result=-1;
goto l_error;
}
#endif
}
}
rc=lfn_findnext(pnew_ffblk);
}
lfn_findclose(pnew_ffblk);
}
l_error:
if(pnew_ffblk!=NULL)
free(pnew_ffblk);
if(subdir_spec!=NULL)
free(subdir_spec);
if(tmp_name!=NULL)
free(tmp_name);
if(subdir_wildcard!=NULL)
free(subdir_wildcard);
return(result);
}
/* Findfirst/findnext, wildcard expansion and so on... */
#ifndef REARJ
int wild_list(struct flist_root *root, struct flist_root *search_flist, char *name, int expand_wildcards, int recurse_subdirs, int file_type, FILE_COUNT *count)
#else
int wild_list(struct flist_root *root, char *name, int file_type, int expand_wildcards, int recurse_subdirs, FILE_COUNT *count)
#endif
{
int attr_mask; /* Narrowing criteria */
struct new_ffblk *pnew_ffblk=NULL; /* OS-dependent data block */
int result=0, rc; /* findfirst/findnext() result */
char *tmp_name=NULL;
int pathspec_len; /* Maximum path length */
#ifndef REARJ
struct file_properties properties; /* Universal block */
#endif
pathspec_len=strlen(name);
if(pathspec_len<CCHMAXPATH)
pathspec_len=CCHMAXPATH;
flush_kbd();
if(!expand_wildcards)
{
if((tmp_name=(char *)strdup(name))==NULL)
{
result=-1;
goto l_error;
}
case_path(tmp_name);
#ifdef REARJ
if(add_entry(root, tmp_name, count))
{
result=-1;
goto l_error;
}
#else
if(flist_add(root, search_flist, tmp_name, count, NULL))
{
result=-1;
goto l_error;
}
#endif
}
else
{
#ifndef REARJ
/* First, perform recursive subdirectory search if needed */
if(recursion_order==RO_LAST)
result=wild_subdir(root, search_flist, name, expand_wildcards, recurse_subdirs, file_type, count);
#endif
if((tmp_name=(char *)malloc(pathspec_len+50))==NULL)
{
result=-1;
goto l_error;
}
/* Search for the ordinary files */
attr_mask=0;
if(file_type!=FETCH_DEFAULT)
#if TARGET!=UNIX
attr_mask|=FATTR_HIDDEN|FATTR_SYSTEM|FATTR_RDONLY;
#else
attr_mask|=FATTR_DT_REG|FATTR_DT_UXSPECIAL;
#endif
if(file_type==FETCH_DIRS)
attr_mask|=STD_DIR_ATTR;
#if !defined(REARJ)&&TARGET!=UNIX
if(filter_attrs)
{
if(file_attr_mask&TAG_DIREC)
attr_mask|=FATTR_DIREC;
if(file_attr_mask&TAG_SYSTEM)
attr_mask|=FATTR_SYSTEM;
if(file_attr_mask&TAG_HIDDEN)
attr_mask|=FATTR_HIDDEN;
if(file_attr_mask&TAG_RDONLY)
attr_mask|=FATTR_RDONLY;
}
#endif
#if TARGET==UNIX
if(file_attr_mask&TAG_UXSPECIAL)
attr_mask|=FATTR_DT_UXSPECIAL;
if(file_attr_mask&TAG_DIREC)
attr_mask|=FATTR_DT_DIR;
#endif
if((pnew_ffblk=(struct new_ffblk *)malloc(sizeof(struct new_ffblk)))==NULL)
{
result=-1;
goto l_error;
}
rc=lfn_findfirst(name, pnew_ffblk, attr_mask);
while(rc==0)
{
/* Entries like "." and ".." are skipped but symlinks AREN'T */
if((!(pnew_ffblk->ff_attrib&STD_DIR_ATTR)||(strcmp(pnew_ffblk->ff_name, cur_dir_spec)&&strcmp(pnew_ffblk->ff_name, up_dir_spec))))
{
split_name(name, tmp_name, NULL);
/* ASR fix for 2.76.04 */
if(strlen(tmp_name)+strlen(pnew_ffblk->ff_name)>=CCHMAXPATH)
msg_cprintf(H_ERR, M_MAXPATH_EXCEEDED, CCHMAXPATH, pnew_ffblk->ff_name);
else
{
strcat(tmp_name, pnew_ffblk->ff_name);
case_path(tmp_name);
#ifdef REARJ
if(add_entry(root, tmp_name, count))
{
result=-1;
goto l_error;
}
#else
finddata_to_properties(&properties, pnew_ffblk);
if(flist_add(root, search_flist, tmp_name, count, &properties))
{
result=-1;
goto l_error;
}
#endif
}
}
rc=lfn_findnext(pnew_ffblk);
}
lfn_findclose(pnew_ffblk); /* BUG: Original REARJ v 2.28 didn't
close the search -- fixed in 2.42 */
if(tmp_name!=NULL)
{
free(tmp_name);
tmp_name=NULL;
}
if(pnew_ffblk!=NULL)
{
free(pnew_ffblk);
pnew_ffblk=NULL;
}
/* Last, perform recursive subdirectory search if needed */
#ifndef REARJ
if(recursion_order==RO_FIRST)
result=wild_subdir(root, search_flist, name, expand_wildcards, recurse_subdirs, file_type, count);
#else
result=wild_subdir(root, name, file_type, expand_wildcards, recurse_subdirs, count);
#endif
}
/* Return 0 if no errors occured */
l_error:
if(tmp_name!=NULL)
free(tmp_name);
if(pnew_ffblk!=NULL)
free(pnew_ffblk);
return(result);
}
#endif
|