1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423 10424 10425 10426 10427 10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442 10443 10444 10445 10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538 10539 10540 10541 10542 10543 10544 10545 10546 10547 10548 10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560 10561 10562 10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584 10585 10586 10587 10588 10589 10590 10591 10592 10593 10594 10595 10596 10597 10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622 10623 10624 10625 10626 10627 10628 10629 10630 10631 10632 10633 10634 10635 10636 10637 10638 10639 10640 10641 10642 10643 10644 10645 10646 10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811 10812 10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823 10824 10825 10826 10827 10828 10829 10830 10831 10832 10833 10834 10835 10836 10837 10838 10839 10840 10841 10842 10843 10844 10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920 10921 10922 10923 10924 10925 10926 10927 10928 10929 10930 10931 10932 10933 10934 10935 10936 10937 10938 10939 10940 10941 10942 10943 10944 10945 10946 10947 10948 10949 10950 10951 10952 10953 10954 10955 10956 10957 10958 10959 10960 10961 10962 10963 10964 10965 10966 10967 10968 10969 10970 10971 10972 10973 10974 10975 10976 10977 10978 10979 10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998 10999 11000 11001 11002 11003 11004 11005 11006 11007 11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049 11050 11051 11052 11053 11054 11055 11056 11057 11058 11059 11060 11061 11062 11063 11064 11065 11066 11067 11068 11069 11070 11071 11072 11073 11074 11075 11076 11077 11078 11079 11080 11081 11082 11083 11084 11085 11086 11087 11088 11089 11090 11091 11092 11093 11094 11095 11096 11097 11098 11099 11100 11101 11102 11103 11104 11105 11106 11107 11108 11109 11110 11111 11112 11113 11114 11115 11116 11117 11118 11119 11120 11121 11122 11123 11124 11125 11126 11127 11128 11129 11130 11131 11132 11133 11134 11135 11136 11137 11138 11139 11140 11141 11142 11143 11144 11145 11146 11147 11148 11149 11150 11151 11152 11153 11154 11155 11156 11157 11158 11159 11160 11161 11162 11163 11164 11165 11166 11167 11168 11169 11170 11171 11172 11173 11174 11175 11176 11177 11178 11179 11180 11181 11182 11183 11184 11185 11186 11187 11188 11189 11190 11191 11192 11193 11194 11195 11196 11197 11198 11199 11200 11201 11202 11203 11204 11205 11206 11207 11208 11209 11210 11211 11212 11213 11214 11215 11216 11217 11218 11219 11220 11221 11222 11223 11224 11225 11226 11227 11228 11229 11230 11231 11232 11233 11234 11235 11236 11237 11238 11239 11240 11241 11242 11243 11244 11245 11246 11247 11248 11249 11250 11251 11252 11253 11254 11255 11256 11257 11258 11259 11260 11261 11262 11263 11264 11265 11266 11267 11268 11269 11270 11271 11272 11273 11274 11275 11276 11277 11278 11279 11280 11281 11282 11283 11284 11285 11286 11287 11288 11289 11290 11291 11292 11293 11294 11295 11296 11297 11298 11299 11300 11301 11302 11303 11304 11305 11306 11307 11308 11309 11310 11311 11312 11313 11314 11315 11316 11317 11318 11319 11320 11321 11322 11323 11324 11325 11326 11327 11328 11329 11330 11331 11332 11333 11334 11335 11336 11337 11338 11339 11340 11341 11342 11343 11344 11345 11346 11347 11348 11349 11350 11351 11352 11353 11354 11355 11356 11357 11358 11359 11360 11361 11362 11363 11364 11365 11366 11367 11368 11369 11370 11371 11372 11373 11374 11375 11376 11377 11378 11379 11380 11381 11382 11383 11384 11385 11386 11387 11388 11389 11390 11391 11392 11393 11394 11395 11396 11397 11398 11399 11400 11401 11402 11403 11404 11405 11406 11407 11408 11409 11410 11411 11412 11413 11414 11415 11416 11417 11418 11419 11420 11421 11422 11423 11424 11425 11426 11427 11428 11429 11430 11431 11432 11433 11434 11435 11436 11437 11438 11439 11440 11441 11442 11443 11444 11445 11446 11447 11448 11449 11450 11451 11452 11453 11454 11455 11456 11457 11458 11459 11460 11461 11462 11463 11464 11465 11466 11467 11468 11469 11470 11471 11472 11473 11474 11475 11476 11477 11478 11479 11480 11481 11482 11483 11484 11485 11486 11487 11488 11489 11490 11491 11492 11493 11494 11495 11496 11497 11498 11499 11500 11501 11502 11503 11504 11505 11506 11507 11508 11509 11510 11511 11512 11513 11514 11515 11516 11517 11518 11519 11520 11521 11522 11523 11524 11525 11526 11527 11528 11529 11530 11531 11532 11533 11534 11535 11536 11537 11538 11539 11540 11541 11542 11543 11544 11545 11546 11547 11548 11549 11550 11551 11552 11553 11554 11555 11556 11557 11558 11559 11560 11561 11562 11563 11564 11565 11566 11567 11568 11569 11570 11571 11572 11573 11574 11575 11576 11577 11578 11579 11580 11581 11582 11583 11584 11585 11586 11587 11588 11589 11590 11591 11592 11593 11594 11595 11596 11597 11598 11599 11600 11601 11602 11603 11604 11605 11606 11607 11608 11609 11610 11611 11612 11613 11614 11615 11616 11617 11618 11619 11620 11621 11622 11623 11624 11625 11626 11627 11628 11629 11630 11631 11632 11633 11634 11635 11636 11637 11638 11639 11640 11641 11642 11643 11644 11645 11646 11647 11648 11649 11650 11651 11652 11653 11654 11655 11656 11657 11658 11659 11660 11661 11662 11663 11664 11665 11666 11667 11668 11669 11670 11671 11672 11673 11674 11675 11676 11677 11678 11679 11680 11681 11682 11683 11684 11685 11686 11687 11688 11689 11690 11691 11692 11693 11694 11695 11696 11697 11698 11699 11700 11701 11702 11703 11704 11705 11706 11707 11708 11709 11710 11711 11712 11713 11714 11715 11716 11717 11718 11719 11720 11721 11722 11723 11724 11725 11726 11727 11728 11729 11730 11731 11732 11733 11734 11735 11736 11737 11738 11739 11740 11741 11742 11743 11744 11745 11746 11747 11748 11749 11750 11751 11752 11753 11754 11755 11756 11757 11758 11759 11760 11761 11762 11763 11764 11765 11766 11767 11768 11769 11770 11771 11772 11773 11774 11775 11776 11777 11778 11779 11780 11781 11782 11783 11784 11785 11786 11787 11788 11789 11790 11791 11792 11793 11794 11795 11796 11797 11798 11799 11800 11801 11802 11803 11804 11805 11806 11807 11808 11809 11810 11811 11812 11813 11814 11815 11816 11817 11818 11819 11820 11821 11822 11823 11824 11825 11826 11827 11828 11829 11830 11831 11832 11833 11834 11835 11836 11837 11838 11839 11840 11841 11842 11843 11844 11845 11846 11847 11848 11849 11850 11851 11852 11853 11854 11855 11856 11857 11858 11859 11860 11861 11862 11863 11864 11865 11866 11867 11868 11869 11870 11871 11872 11873 11874 11875 11876 11877 11878 11879 11880 11881 11882 11883 11884 11885 11886 11887 11888 11889 11890 11891 11892 11893 11894 11895 11896 11897 11898 11899 11900 11901 11902 11903 11904 11905 11906 11907 11908 11909 11910 11911 11912 11913 11914 11915 11916 11917 11918 11919 11920 11921 11922 11923 11924 11925 11926 11927 11928 11929 11930 11931 11932 11933 11934 11935 11936 11937 11938 11939 11940 11941 11942 11943 11944 11945 11946 11947 11948 11949 11950 11951 11952 11953 11954 11955 11956 11957 11958 11959 11960 11961 11962 11963 11964 11965 11966 11967 11968 11969 11970 11971 11972 11973 11974 11975 11976 11977 11978 11979 11980 11981 11982 11983 11984 11985 11986 11987 11988 11989 11990 11991 11992 11993 11994 11995 11996 11997 11998 11999 12000 12001 12002 12003 12004 12005 12006 12007 12008 12009 12010 12011 12012 12013 12014 12015 12016 12017 12018 12019 12020 12021 12022 12023 12024 12025 12026 12027 12028 12029 12030 12031 12032 12033 12034 12035 12036 12037 12038 12039 12040 12041 12042 12043 12044 12045 12046 12047 12048 12049 12050 12051 12052 12053 12054 12055 12056 12057 12058 12059 12060 12061 12062 12063 12064 12065 12066 12067 12068 12069 12070 12071 12072 12073 12074 12075 12076 12077 12078 12079 12080 12081 12082 12083 12084 12085 12086 12087 12088 12089 12090 12091 12092 12093 12094 12095 12096 12097 12098 12099 12100 12101 12102 12103 12104 12105 12106 12107 12108 12109 12110 12111 12112 12113 12114 12115 12116 12117 12118 12119 12120 12121 12122 12123 12124 12125 12126 12127 12128 12129 12130 12131 12132 12133 12134 12135 12136 12137 12138 12139 12140 12141 12142 12143 12144 12145 12146 12147 12148 12149 12150 12151 12152 12153 12154 12155 12156 12157 12158 12159 12160 12161 12162 12163 12164 12165 12166 12167 12168 12169 12170 12171 12172 12173 12174 12175 12176 12177 12178 12179 12180 12181 12182 12183 12184 12185 12186 12187 12188 12189 12190 12191 12192 12193 12194 12195 12196 12197 12198 12199 12200 12201 12202 12203 12204 12205 12206 12207 12208 12209 12210 12211 12212 12213 12214 12215 12216 12217 12218 12219 12220 12221 12222 12223 12224 12225 12226 12227 12228 12229 12230 12231 12232 12233 12234 12235 12236 12237 12238 12239 12240 12241 12242 12243 12244 12245 12246 12247 12248 12249 12250 12251 12252 12253 12254 12255 12256 12257 12258 12259 12260 12261 12262 12263 12264 12265 12266 12267 12268 12269 12270 12271 12272 12273 12274 12275 12276 12277 12278 12279 12280 12281 12282 12283 12284 12285 12286 12287 12288 12289 12290 12291 12292 12293 12294 12295 12296 12297 12298 12299 12300 12301 12302 12303 12304 12305 12306 12307 12308 12309 12310 12311 12312 12313 12314 12315 12316 12317 12318 12319 12320 12321 12322 12323 12324 12325 12326 12327 12328 12329 12330 12331 12332 12333 12334 12335 12336 12337 12338 12339 12340 12341 12342 12343 12344 12345 12346 12347 12348 12349 12350 12351 12352 12353 12354 12355 12356 12357 12358 12359 12360 12361 12362 12363 12364 12365 12366 12367 12368 12369 12370 12371 12372 12373 12374 12375 12376 12377 12378 12379 12380 12381 12382 12383 12384 12385 12386 12387 12388 12389 12390 12391 12392 12393 12394 12395 12396 12397 12398 12399 12400 12401 12402 12403 12404 12405 12406 12407 12408 12409 12410 12411 12412 12413 12414 12415 12416 12417 12418 12419 12420 12421 12422 12423 12424 12425 12426 12427 12428 12429 12430 12431 12432 12433 12434 12435 12436 12437 12438 12439 12440 12441 12442 12443 12444 12445 12446 12447 12448 12449 12450 12451 12452 12453 12454 12455 12456 12457 12458 12459 12460 12461 12462 12463 12464 12465 12466 12467 12468 12469 12470 12471 12472 12473 12474 12475 12476 12477 12478 12479 12480 12481 12482 12483 12484 12485 12486 12487 12488 12489 12490 12491 12492 12493 12494 12495 12496 12497 12498 12499 12500 12501 12502 12503 12504 12505 12506 12507 12508 12509 12510 12511 12512 12513 12514 12515 12516 12517 12518 12519 12520 12521 12522 12523 12524 12525 12526 12527 12528 12529 12530 12531 12532 12533 12534 12535 12536 12537 12538 12539 12540 12541 12542 12543 12544 12545 12546 12547 12548 12549 12550 12551 12552 12553 12554 12555 12556 12557 12558 12559 12560 12561 12562 12563 12564 12565 12566 12567 12568 12569 12570 12571 12572 12573 12574 12575 12576 12577 12578 12579 12580 12581 12582 12583 12584 12585 12586 12587 12588 12589 12590 12591 12592 12593 12594 12595 12596 12597 12598 12599 12600 12601 12602 12603 12604 12605 12606 12607 12608 12609 12610 12611 12612 12613 12614 12615 12616 12617 12618 12619 12620 12621 12622 12623 12624 12625 12626 12627 12628 12629 12630 12631 12632 12633 12634 12635 12636 12637 12638 12639 12640 12641 12642 12643 12644 12645 12646 12647 12648 12649 12650 12651 12652 12653 12654 12655 12656 12657 12658 12659 12660 12661 12662 12663 12664 12665 12666 12667 12668 12669 12670 12671 12672 12673 12674 12675 12676 12677 12678 12679 12680 12681 12682 12683 12684 12685 12686 12687 12688 12689 12690 12691 12692 12693 12694 12695 12696 12697 12698 12699 12700 12701 12702 12703 12704 12705 12706 12707 12708 12709 12710 12711 12712 12713 12714 12715 12716 12717 12718 12719 12720 12721 12722 12723 12724 12725 12726 12727 12728 12729 12730 12731 12732 12733 12734 12735 12736 12737 12738 12739 12740 12741 12742 12743 12744 12745 12746 12747 12748 12749 12750 12751 12752 12753 12754 12755 12756 12757 12758 12759 12760 12761 12762 12763 12764 12765 12766 12767 12768 12769 12770 12771 12772 12773 12774 12775 12776 12777 12778 12779 12780 12781 12782 12783 12784 12785 12786 12787 12788 12789 12790 12791 12792 12793 12794 12795 12796 12797 12798 12799 12800 12801 12802 12803 12804 12805 12806 12807 12808 12809 12810 12811 12812 12813 12814 12815 12816 12817 12818 12819 12820 12821 12822 12823 12824 12825 12826 12827 12828 12829 12830 12831 12832 12833 12834 12835 12836 12837 12838 12839 12840 12841 12842 12843 12844 12845 12846 12847 12848 12849 12850 12851 12852 12853 12854 12855 12856 12857 12858 12859 12860 12861 12862 12863 12864 12865 12866 12867 12868 12869 12870 12871 12872 12873 12874 12875 12876 12877 12878 12879 12880 12881 12882 12883 12884 12885 12886 12887 12888 12889 12890 12891 12892 12893 12894 12895 12896 12897 12898 12899 12900 12901 12902 12903 12904 12905 12906 12907 12908 12909 12910 12911 12912 12913 12914 12915 12916 12917 12918 12919 12920 12921 12922 12923 12924 12925 12926 12927 12928 12929 12930 12931 12932 12933 12934 12935 12936 12937 12938 12939 12940 12941 12942 12943 12944 12945 12946 12947 12948 12949 12950 12951 12952 12953 12954 12955 12956 12957 12958 12959 12960 12961 12962 12963 12964 12965 12966 12967 12968 12969 12970 12971 12972 12973 12974 12975 12976 12977 12978 12979 12980 12981 12982 12983 12984 12985 12986 12987 12988 12989 12990 12991 12992 12993 12994 12995 12996 12997 12998 12999 13000 13001 13002 13003 13004 13005 13006 13007 13008 13009 13010 13011 13012 13013 13014 13015 13016 13017 13018 13019 13020 13021 13022 13023 13024 13025 13026 13027 13028 13029 13030 13031 13032 13033 13034 13035 13036 13037 13038 13039 13040 13041 13042 13043 13044 13045 13046 13047 13048 13049 13050 13051 13052 13053 13054 13055 13056 13057 13058 13059 13060 13061 13062 13063 13064 13065 13066 13067 13068 13069 13070 13071 13072 13073 13074 13075 13076 13077 13078 13079 13080 13081 13082 13083 13084 13085 13086 13087 13088 13089 13090 13091 13092 13093 13094 13095 13096 13097 13098 13099 13100 13101 13102 13103 13104 13105 13106 13107 13108 13109 13110 13111 13112 13113 13114 13115 13116 13117 13118 13119 13120 13121 13122 13123 13124 13125 13126 13127 13128 13129 13130 13131 13132 13133 13134 13135 13136 13137 13138 13139 13140 13141 13142 13143 13144 13145 13146 13147 13148 13149 13150 13151 13152 13153 13154 13155 13156 13157 13158 13159 13160 13161 13162 13163 13164 13165 13166 13167 13168 13169 13170 13171 13172 13173 13174 13175 13176 13177 13178 13179 13180 13181 13182 13183 13184 13185 13186 13187 13188 13189 13190 13191 13192 13193 13194 13195 13196 13197 13198 13199 13200 13201 13202 13203 13204 13205 13206 13207 13208 13209 13210 13211 13212 13213 13214 13215 13216 13217 13218 13219 13220 13221 13222 13223 13224 13225 13226 13227 13228 13229 13230 13231 13232 13233 13234 13235 13236 13237 13238 13239 13240 13241 13242 13243 13244 13245 13246 13247 13248 13249 13250 13251 13252 13253 13254 13255 13256 13257 13258 13259 13260 13261 13262 13263 13264 13265 13266 13267 13268 13269 13270 13271 13272 13273 13274 13275 13276 13277 13278 13279 13280 13281 13282 13283 13284 13285 13286 13287 13288 13289 13290 13291 13292 13293 13294 13295 13296 13297 13298 13299 13300 13301 13302 13303 13304 13305 13306 13307 13308 13309 13310 13311 13312 13313 13314 13315 13316 13317 13318 13319 13320 13321 13322 13323 13324 13325 13326 13327 13328 13329 13330 13331 13332 13333 13334 13335 13336 13337 13338 13339 13340 13341 13342 13343 13344 13345 13346 13347 13348 13349 13350 13351 13352 13353 13354 13355 13356 13357 13358 13359 13360 13361 13362 13363 13364 13365 13366 13367 13368 13369 13370 13371 13372 13373 13374 13375 13376 13377 13378 13379 13380 13381 13382 13383 13384 13385 13386 13387 13388 13389 13390 13391 13392 13393 13394 13395 13396 13397 13398 13399 13400 13401 13402 13403 13404 13405 13406 13407 13408 13409 13410 13411 13412 13413 13414 13415 13416 13417 13418 13419 13420 13421 13422 13423 13424 13425 13426 13427 13428 13429 13430 13431 13432 13433 13434 13435 13436 13437 13438 13439 13440 13441 13442 13443 13444 13445 13446 13447 13448 13449 13450 13451 13452 13453 13454 13455 13456 13457 13458 13459 13460 13461 13462 13463 13464 13465 13466 13467 13468 13469 13470 13471 13472 13473 13474 13475 13476 13477 13478 13479 13480 13481 13482 13483 13484 13485 13486 13487 13488 13489 13490 13491 13492 13493 13494 13495 13496 13497 13498 13499 13500 13501 13502 13503 13504 13505 13506 13507 13508 13509 13510 13511 13512 13513 13514 13515 13516 13517 13518 13519 13520 13521 13522 13523 13524 13525 13526 13527 13528 13529 13530 13531 13532 13533 13534 13535 13536 13537 13538 13539 13540 13541 13542 13543 13544 13545 13546 13547 13548 13549 13550 13551 13552 13553 13554 13555 13556 13557 13558 13559 13560 13561 13562 13563 13564 13565 13566 13567 13568 13569 13570 13571 13572 13573 13574 13575 13576 13577 13578 13579 13580 13581 13582 13583 13584 13585 13586 13587 13588 13589 13590 13591 13592 13593 13594 13595 13596 13597 13598 13599 13600 13601 13602 13603 13604 13605 13606 13607 13608 13609 13610 13611 13612 13613 13614 13615 13616 13617 13618 13619 13620 13621 13622 13623 13624 13625 13626 13627 13628 13629 13630 13631 13632 13633 13634 13635 13636 13637 13638 13639 13640 13641 13642 13643 13644 13645 13646 13647 13648 13649 13650 13651 13652 13653 13654 13655 13656 13657 13658 13659 13660 13661 13662 13663 13664 13665 13666 13667 13668 13669 13670 13671 13672 13673 13674 13675 13676 13677 13678 13679 13680 13681 13682 13683 13684 13685 13686 13687 13688 13689 13690 13691 13692 13693 13694 13695 13696 13697 13698 13699 13700 13701 13702 13703 13704 13705 13706 13707 13708 13709 13710 13711 13712 13713 13714 13715 13716 13717 13718 13719 13720 13721 13722 13723 13724 13725 13726 13727 13728 13729 13730 13731 13732 13733 13734 13735 13736 13737 13738 13739 13740 13741 13742 13743 13744 13745 13746 13747 13748 13749 13750 13751 13752 13753 13754 13755 13756 13757 13758 13759 13760 13761 13762 13763 13764 13765 13766 13767 13768 13769 13770 13771 13772 13773 13774 13775 13776 13777 13778 13779 13780 13781 13782 13783 13784 13785 13786 13787 13788 13789 13790 13791 13792 13793 13794 13795 13796 13797 13798 13799 13800 13801 13802 13803 13804 13805 13806 13807 13808 13809 13810 13811 13812 13813 13814 13815 13816 13817 13818 13819 13820 13821 13822 13823 13824 13825 13826 13827 13828 13829 13830 13831 13832 13833 13834 13835 13836 13837 13838 13839 13840 13841 13842 13843 13844 13845 13846 13847 13848 13849 13850 13851 13852 13853 13854 13855 13856 13857 13858 13859 13860 13861 13862 13863 13864 13865 13866 13867 13868 13869 13870 13871 13872 13873 13874 13875 13876 13877 13878 13879 13880 13881 13882 13883 13884 13885 13886 13887 13888 13889 13890 13891 13892 13893 13894 13895 13896 13897 13898 13899 13900 13901 13902 13903 13904 13905 13906 13907 13908 13909 13910 13911 13912 13913 13914 13915 13916 13917 13918 13919 13920 13921 13922 13923 13924 13925 13926 13927 13928 13929 13930 13931 13932 13933 13934 13935 13936 13937 13938 13939 13940 13941 13942 13943 13944 13945 13946 13947 13948 13949 13950 13951 13952 13953 13954 13955 13956 13957 13958 13959 13960 13961 13962 13963 13964 13965 13966 13967 13968 13969 13970 13971 13972 13973 13974 13975 13976 13977 13978 13979 13980 13981 13982 13983 13984 13985 13986 13987 13988 13989 13990 13991 13992 13993 13994 13995 13996 13997 13998 13999 14000 14001 14002 14003 14004 14005 14006 14007 14008 14009 14010 14011 14012 14013 14014 14015 14016 14017 14018 14019 14020 14021 14022 14023 14024 14025 14026 14027 14028 14029 14030 14031 14032 14033 14034 14035 14036 14037 14038 14039 14040 14041 14042 14043 14044 14045 14046 14047 14048 14049 14050 14051 14052 14053 14054 14055 14056 14057 14058 14059 14060 14061 14062 14063 14064 14065 14066 14067 14068 14069 14070 14071 14072 14073 14074 14075 14076 14077 14078 14079 14080 14081 14082 14083 14084 14085 14086 14087 14088 14089 14090 14091 14092 14093 14094 14095 14096 14097 14098 14099 14100 14101 14102 14103 14104 14105 14106 14107 14108 14109 14110 14111 14112 14113 14114 14115 14116 14117 14118 14119 14120 14121 14122 14123 14124 14125 14126 14127 14128 14129 14130 14131 14132 14133 14134 14135 14136 14137 14138 14139 14140 14141 14142 14143 14144 14145 14146 14147 14148 14149 14150 14151 14152 14153 14154 14155 14156 14157 14158 14159 14160 14161 14162 14163 14164 14165 14166 14167 14168 14169 14170 14171 14172 14173 14174 14175 14176 14177 14178 14179 14180 14181 14182 14183 14184 14185 14186 14187 14188 14189 14190 14191 14192 14193 14194 14195 14196 14197 14198 14199 14200 14201 14202 14203 14204 14205 14206 14207 14208 14209 14210 14211 14212 14213 14214 14215 14216 14217 14218 14219 14220 14221 14222 14223 14224 14225 14226 14227 14228 14229 14230 14231 14232 14233 14234 14235 14236 14237 14238 14239 14240 14241 14242 14243 14244 14245 14246 14247 14248 14249 14250 14251 14252 14253 14254 14255 14256 14257 14258 14259 14260 14261 14262 14263 14264 14265 14266 14267 14268 14269 14270 14271 14272 14273 14274 14275 14276 14277 14278 14279 14280 14281 14282 14283 14284 14285 14286 14287 14288 14289 14290 14291 14292 14293 14294 14295 14296 14297 14298 14299 14300 14301 14302 14303 14304 14305 14306 14307 14308 14309 14310 14311 14312 14313 14314 14315 14316 14317 14318 14319 14320 14321 14322 14323 14324 14325 14326 14327 14328 14329 14330 14331 14332 14333 14334 14335 14336 14337 14338 14339 14340 14341 14342 14343 14344 14345 14346 14347 14348 14349 14350 14351 14352 14353 14354 14355 14356 14357 14358 14359 14360 14361 14362 14363 14364 14365 14366 14367 14368 14369 14370 14371 14372 14373 14374 14375 14376 14377 14378 14379 14380 14381 14382 14383 14384 14385 14386 14387 14388 14389 14390 14391 14392 14393 14394 14395 14396 14397 14398 14399 14400 14401 14402 14403 14404 14405 14406 14407 14408 14409 14410 14411 14412 14413 14414 14415 14416 14417 14418 14419 14420 14421 14422 14423 14424 14425 14426 14427 14428 14429 14430 14431 14432 14433 14434 14435 14436 14437 14438 14439 14440 14441 14442 14443 14444 14445 14446 14447 14448 14449 14450 14451 14452 14453 14454 14455 14456 14457 14458 14459 14460 14461 14462 14463 14464 14465 14466 14467 14468 14469 14470 14471 14472 14473 14474 14475 14476 14477 14478 14479 14480 14481 14482 14483 14484 14485 14486 14487 14488 14489 14490 14491 14492 14493 14494 14495 14496 14497 14498 14499 14500 14501 14502 14503 14504 14505 14506 14507 14508 14509 14510 14511 14512 14513 14514 14515 14516 14517 14518 14519 14520 14521 14522 14523 14524 14525 14526 14527 14528 14529 14530 14531 14532 14533 14534 14535 14536 14537 14538 14539 14540 14541 14542 14543 14544 14545 14546 14547 14548 14549 14550 14551 14552 14553 14554 14555 14556 14557 14558 14559 14560 14561 14562 14563 14564 14565 14566 14567 14568 14569 14570 14571 14572 14573 14574 14575 14576 14577 14578 14579 14580 14581 14582 14583 14584 14585 14586 14587 14588 14589 14590 14591 14592 14593 14594 14595 14596 14597 14598 14599 14600 14601 14602 14603 14604 14605 14606 14607 14608 14609 14610 14611 14612 14613 14614 14615 14616 14617 14618 14619 14620 14621 14622 14623 14624 14625 14626 14627 14628 14629 14630 14631 14632 14633 14634 14635 14636 14637 14638 14639 14640 14641 14642 14643 14644 14645 14646 14647 14648 14649 14650 14651 14652 14653 14654 14655 14656 14657 14658 14659 14660 14661 14662 14663 14664 14665 14666 14667 14668 14669 14670 14671 14672 14673 14674 14675 14676 14677 14678 14679 14680 14681 14682 14683 14684 14685 14686 14687 14688 14689 14690 14691 14692 14693 14694 14695 14696 14697 14698 14699 14700 14701 14702 14703 14704 14705 14706 14707 14708 14709 14710 14711 14712 14713 14714 14715 14716 14717 14718 14719 14720 14721 14722 14723 14724 14725 14726 14727 14728 14729 14730 14731 14732 14733 14734 14735 14736 14737 14738 14739 14740 14741 14742 14743 14744 14745 14746 14747 14748 14749 14750 14751 14752 14753 14754 14755 14756 14757 14758 14759 14760 14761 14762 14763 14764 14765 14766 14767 14768 14769 14770 14771 14772 14773 14774 14775 14776 14777 14778 14779 14780 14781 14782 14783 14784 14785 14786 14787 14788 14789 14790 14791 14792 14793 14794 14795 14796 14797 14798 14799 14800 14801 14802 14803 14804 14805 14806 14807 14808 14809 14810 14811 14812 14813 14814 14815 14816 14817 14818 14819 14820 14821 14822 14823 14824 14825 14826 14827 14828 14829 14830 14831 14832 14833 14834 14835 14836 14837 14838 14839 14840 14841 14842 14843 14844 14845 14846 14847 14848 14849 14850 14851 14852 14853 14854 14855 14856 14857 14858 14859 14860 14861 14862 14863 14864 14865 14866 14867 14868 14869 14870 14871 14872 14873 14874 14875 14876 14877 14878 14879 14880 14881 14882 14883 14884 14885 14886 14887 14888 14889 14890 14891 14892 14893 14894 14895 14896 14897 14898 14899 14900 14901 14902 14903 14904 14905 14906 14907 14908 14909 14910 14911 14912 14913 14914 14915 14916 14917 14918 14919 14920 14921 14922 14923 14924 14925 14926 14927 14928 14929 14930 14931 14932 14933 14934 14935 14936 14937 14938 14939 14940 14941 14942 14943 14944 14945 14946 14947 14948 14949 14950 14951 14952 14953 14954 14955 14956 14957 14958 14959 14960 14961 14962 14963 14964 14965 14966 14967 14968 14969 14970 14971 14972 14973 14974 14975 14976 14977 14978 14979 14980 14981 14982 14983 14984 14985 14986 14987 14988 14989 14990 14991 14992 14993 14994 14995 14996 14997 14998 14999 15000 15001 15002 15003 15004 15005 15006 15007 15008 15009 15010 15011 15012 15013 15014 15015 15016 15017 15018 15019 15020 15021 15022 15023 15024 15025 15026 15027 15028 15029 15030 15031 15032 15033 15034 15035 15036 15037 15038 15039 15040 15041 15042 15043 15044 15045 15046 15047 15048 15049 15050 15051 15052 15053 15054 15055 15056 15057 15058 15059 15060 15061 15062 15063 15064 15065 15066 15067 15068 15069 15070 15071 15072 15073 15074 15075 15076 15077 15078 15079 15080 15081 15082 15083 15084 15085 15086 15087 15088 15089 15090 15091 15092 15093 15094 15095 15096 15097 15098 15099 15100 15101 15102 15103 15104 15105 15106 15107 15108 15109 15110 15111 15112 15113 15114 15115 15116 15117 15118 15119 15120 15121 15122 15123 15124 15125 15126 15127 15128 15129 15130 15131 15132 15133 15134 15135 15136 15137 15138 15139 15140 15141 15142 15143 15144 15145 15146 15147 15148 15149 15150 15151 15152 15153 15154 15155 15156 15157 15158 15159 15160 15161 15162 15163 15164 15165 15166 15167 15168 15169 15170 15171 15172 15173 15174 15175 15176 15177 15178 15179 15180 15181 15182 15183 15184 15185 15186 15187 15188 15189 15190 15191 15192 15193 15194 15195 15196 15197 15198 15199 15200 15201 15202 15203 15204 15205 15206 15207 15208 15209 15210 15211 15212 15213 15214 15215 15216 15217 15218 15219 15220 15221 15222 15223 15224 15225 15226 15227 15228 15229 15230 15231 15232 15233 15234 15235 15236 15237 15238 15239 15240 15241 15242 15243 15244 15245 15246 15247 15248 15249 15250 15251 15252 15253 15254 15255 15256 15257 15258 15259 15260 15261 15262 15263 15264 15265 15266 15267 15268 15269 15270 15271 15272 15273 15274 15275 15276 15277 15278 15279 15280 15281 15282 15283 15284 15285 15286 15287 15288 15289 15290 15291 15292 15293 15294 15295 15296 15297 15298 15299 15300 15301 15302 15303 15304 15305 15306 15307 15308 15309 15310 15311 15312 15313 15314 15315 15316 15317 15318 15319 15320 15321 15322 15323 15324 15325 15326 15327 15328 15329 15330 15331 15332 15333 15334 15335 15336 15337 15338 15339 15340 15341 15342 15343 15344 15345 15346 15347 15348 15349 15350 15351 15352 15353 15354 15355 15356 15357 15358 15359 15360 15361 15362 15363 15364 15365 15366 15367 15368 15369 15370 15371 15372 15373 15374 15375 15376 15377 15378 15379 15380 15381 15382 15383 15384 15385 15386 15387 15388 15389 15390 15391 15392 15393 15394 15395 15396 15397 15398 15399 15400 15401 15402 15403 15404 15405 15406 15407 15408 15409 15410 15411 15412 15413 15414 15415 15416 15417 15418 15419 15420 15421 15422 15423 15424 15425 15426 15427 15428 15429 15430 15431 15432 15433 15434 15435 15436 15437 15438 15439 15440 15441 15442 15443 15444 15445 15446 15447 15448 15449 15450 15451 15452 15453 15454 15455 15456 15457 15458 15459 15460 15461 15462 15463 15464 15465 15466 15467 15468 15469 15470 15471 15472 15473 15474 15475 15476 15477 15478 15479 15480 15481 15482 15483 15484 15485 15486 15487 15488 15489 15490 15491 15492 15493 15494 15495 15496 15497 15498 15499 15500 15501 15502 15503 15504 15505 15506 15507 15508 15509 15510 15511 15512 15513 15514 15515 15516 15517 15518 15519 15520 15521 15522 15523 15524 15525 15526 15527 15528 15529 15530 15531 15532 15533 15534 15535 15536 15537 15538 15539 15540 15541 15542 15543 15544 15545 15546 15547 15548 15549 15550 15551 15552 15553 15554 15555 15556 15557 15558 15559 15560 15561 15562 15563 15564 15565 15566 15567 15568 15569 15570 15571 15572 15573 15574 15575 15576 15577 15578 15579 15580 15581 15582 15583 15584 15585 15586 15587 15588 15589 15590 15591 15592 15593 15594 15595 15596 15597 15598 15599 15600 15601 15602 15603 15604 15605 15606 15607 15608 15609 15610 15611 15612 15613 15614 15615 15616 15617 15618 15619 15620 15621 15622 15623 15624 15625 15626 15627 15628 15629 15630 15631 15632 15633 15634 15635 15636 15637 15638 15639 15640 15641 15642 15643 15644 15645 15646 15647 15648 15649 15650 15651 15652 15653 15654 15655 15656 15657 15658 15659 15660 15661 15662 15663 15664 15665 15666 15667 15668 15669 15670 15671 15672 15673 15674 15675 15676 15677 15678 15679 15680 15681 15682 15683 15684 15685 15686 15687 15688 15689 15690 15691 15692 15693 15694 15695 15696 15697 15698 15699 15700 15701 15702 15703 15704 15705 15706 15707 15708 15709 15710 15711 15712 15713 15714 15715 15716 15717 15718 15719 15720 15721 15722 15723 15724 15725 15726 15727 15728 15729 15730 15731 15732 15733 15734 15735 15736 15737 15738 15739 15740 15741 15742 15743 15744 15745 15746 15747 15748 15749 15750 15751 15752 15753 15754 15755 15756 15757 15758 15759 15760 15761 15762 15763 15764 15765 15766 15767 15768 15769 15770 15771 15772 15773 15774 15775 15776 15777 15778 15779 15780 15781 15782 15783 15784 15785 15786 15787 15788 15789 15790 15791 15792 15793 15794 15795 15796 15797 15798 15799 15800 15801 15802 15803 15804 15805 15806 15807 15808 15809 15810 15811 15812 15813 15814 15815 15816 15817 15818 15819 15820 15821 15822 15823 15824 15825 15826 15827 15828 15829 15830 15831 15832 15833 15834 15835 15836 15837 15838 15839 15840 15841 15842 15843 15844 15845 15846 15847 15848 15849 15850 15851 15852 15853 15854 15855 15856 15857 15858 15859 15860 15861 15862 15863 15864 15865 15866 15867 15868 15869 15870 15871 15872 15873 15874 15875 15876 15877 15878 15879 15880 15881 15882 15883 15884 15885 15886 15887 15888 15889 15890 15891 15892 15893 15894 15895 15896 15897 15898 15899 15900 15901 15902 15903 15904 15905 15906 15907 15908 15909 15910 15911 15912 15913 15914 15915 15916 15917 15918 15919 15920 15921 15922 15923 15924 15925 15926 15927 15928 15929 15930 15931 15932 15933 15934 15935 15936 15937 15938 15939 15940 15941 15942 15943 15944 15945 15946 15947 15948 15949 15950 15951 15952 15953 15954 15955 15956 15957 15958 15959 15960 15961 15962 15963 15964 15965 15966 15967 15968 15969 15970 15971 15972 15973 15974 15975 15976 15977 15978 15979 15980 15981 15982 15983 15984 15985 15986 15987 15988 15989 15990 15991 15992 15993 15994 15995 15996 15997 15998 15999 16000 16001 16002 16003 16004 16005 16006 16007 16008 16009 16010 16011 16012 16013 16014 16015 16016 16017 16018 16019 16020 16021 16022 16023 16024 16025 16026 16027 16028 16029 16030 16031 16032 16033 16034 16035 16036 16037 16038 16039 16040 16041 16042 16043 16044 16045 16046 16047 16048 16049 16050 16051 16052 16053 16054 16055 16056 16057 16058 16059 16060 16061 16062 16063 16064 16065 16066 16067 16068 16069 16070 16071 16072 16073 16074 16075 16076 16077 16078 16079 16080 16081 16082 16083 16084 16085 16086 16087 16088 16089 16090 16091 16092 16093 16094 16095 16096 16097 16098 16099 16100 16101 16102 16103 16104 16105 16106 16107 16108 16109 16110 16111 16112 16113 16114 16115 16116 16117 16118 16119 16120 16121 16122 16123 16124 16125 16126 16127 16128 16129 16130 16131 16132 16133 16134 16135 16136 16137 16138 16139 16140 16141 16142 16143 16144 16145 16146 16147 16148 16149 16150 16151 16152 16153 16154 16155 16156 16157 16158 16159 16160 16161 16162 16163 16164 16165 16166 16167 16168 16169 16170 16171 16172 16173 16174 16175 16176 16177 16178 16179 16180 16181 16182 16183 16184 16185 16186 16187 16188 16189 16190 16191 16192 16193 16194 16195 16196 16197 16198 16199 16200 16201 16202 16203 16204 16205 16206 16207 16208 16209 16210 16211 16212 16213 16214 16215 16216 16217 16218 16219 16220 16221 16222 16223 16224 16225 16226 16227 16228 16229 16230 16231 16232 16233 16234 16235 16236 16237 16238 16239 16240 16241 16242 16243 16244 16245 16246 16247 16248 16249 16250 16251 16252 16253 16254 16255 16256 16257 16258 16259 16260 16261 16262 16263 16264 16265 16266 16267 16268 16269 16270 16271 16272 16273 16274 16275 16276 16277 16278 16279 16280 16281 16282 16283 16284 16285 16286 16287 16288 16289 16290 16291 16292 16293 16294 16295 16296 16297 16298 16299 16300 16301 16302 16303 16304 16305 16306 16307 16308 16309 16310 16311 16312 16313 16314 16315 16316 16317 16318 16319 16320 16321 16322 16323 16324 16325 16326 16327 16328 16329 16330 16331 16332 16333 16334 16335 16336 16337 16338 16339 16340 16341 16342 16343 16344 16345 16346 16347 16348 16349 16350 16351 16352 16353 16354 16355 16356 16357 16358 16359 16360 16361 16362 16363 16364 16365 16366 16367 16368 16369 16370 16371 16372 16373 16374 16375 16376 16377 16378 16379 16380 16381 16382 16383 16384 16385 16386 16387 16388 16389 16390 16391 16392 16393 16394 16395 16396 16397 16398 16399 16400 16401 16402 16403 16404 16405 16406 16407 16408 16409 16410 16411 16412 16413 16414 16415 16416 16417 16418 16419 16420 16421 16422 16423 16424 16425 16426 16427 16428 16429 16430 16431 16432 16433 16434 16435 16436 16437 16438 16439 16440 16441 16442 16443 16444 16445 16446 16447 16448 16449 16450 16451 16452 16453 16454 16455 16456 16457 16458 16459 16460 16461 16462 16463 16464 16465 16466 16467 16468 16469 16470 16471 16472 16473 16474 16475 16476 16477 16478 16479 16480 16481 16482 16483 16484 16485 16486 16487 16488 16489 16490 16491 16492 16493 16494 16495 16496 16497 16498 16499 16500 16501 16502 16503 16504 16505 16506 16507 16508 16509 16510 16511 16512 16513 16514 16515 16516 16517 16518 16519 16520 16521 16522 16523 16524 16525 16526 16527 16528 16529 16530 16531 16532 16533 16534 16535 16536 16537 16538 16539 16540 16541 16542 16543 16544 16545 16546 16547 16548 16549 16550 16551 16552 16553 16554 16555 16556 16557 16558 16559 16560 16561 16562 16563 16564 16565 16566 16567 16568 16569 16570 16571 16572 16573 16574 16575 16576 16577 16578 16579 16580 16581 16582 16583 16584 16585 16586 16587 16588 16589 16590 16591 16592 16593 16594 16595 16596 16597 16598 16599 16600 16601 16602 16603 16604 16605 16606 16607 16608 16609 16610 16611 16612 16613 16614 16615 16616 16617 16618 16619 16620 16621 16622 16623 16624 16625 16626 16627 16628 16629 16630 16631 16632 16633 16634 16635 16636 16637 16638 16639 16640 16641 16642 16643 16644 16645 16646 16647 16648 16649 16650 16651 16652 16653 16654 16655 16656 16657 16658 16659 16660 16661 16662 16663 16664 16665 16666 16667 16668 16669 16670 16671 16672 16673 16674 16675 16676 16677 16678 16679 16680 16681 16682 16683 16684 16685 16686 16687 16688 16689 16690 16691 16692 16693 16694 16695 16696 16697 16698 16699 16700 16701 16702 16703 16704 16705 16706 16707 16708 16709 16710 16711 16712 16713 16714 16715 16716 16717 16718 16719 16720 16721 16722 16723 16724 16725 16726 16727 16728 16729 16730 16731 16732 16733 16734 16735 16736 16737 16738 16739 16740 16741 16742 16743 16744 16745 16746 16747 16748 16749 16750 16751 16752 16753 16754 16755 16756 16757 16758 16759 16760 16761 16762 16763 16764 16765 16766 16767 16768 16769 16770 16771 16772 16773 16774 16775 16776 16777 16778 16779 16780 16781 16782 16783 16784 16785 16786 16787 16788 16789 16790 16791 16792 16793 16794 16795 16796 16797 16798 16799 16800 16801 16802 16803 16804 16805 16806 16807 16808 16809 16810 16811 16812 16813 16814 16815 16816 16817 16818 16819 16820 16821 16822 16823 16824 16825 16826 16827 16828 16829 16830 16831 16832 16833 16834 16835 16836 16837 16838 16839 16840 16841 16842 16843 16844 16845 16846 16847 16848 16849 16850 16851 16852 16853 16854 16855 16856 16857 16858 16859 16860 16861 16862 16863 16864 16865 16866 16867 16868 16869 16870 16871 16872 16873 16874 16875 16876 16877 16878 16879 16880 16881 16882 16883 16884 16885 16886 16887 16888 16889 16890 16891 16892 16893 16894 16895 16896 16897 16898 16899 16900 16901 16902 16903 16904 16905 16906 16907 16908 16909 16910 16911 16912 16913 16914 16915 16916 16917 16918 16919 16920 16921 16922 16923 16924 16925 16926 16927 16928 16929 16930 16931 16932 16933 16934 16935 16936 16937 16938 16939 16940 16941 16942 16943 16944 16945 16946 16947 16948 16949 16950 16951 16952 16953 16954 16955 16956 16957 16958 16959 16960 16961 16962 16963 16964 16965 16966 16967 16968 16969 16970 16971 16972 16973 16974 16975 16976 16977 16978 16979 16980 16981 16982 16983 16984 16985 16986 16987 16988 16989 16990 16991 16992 16993 16994 16995 16996 16997 16998 16999 17000 17001 17002 17003 17004 17005 17006 17007 17008 17009 17010 17011 17012 17013 17014 17015 17016 17017 17018 17019 17020 17021 17022 17023 17024 17025 17026 17027 17028 17029 17030 17031 17032 17033 17034 17035 17036 17037 17038 17039 17040 17041 17042 17043 17044 17045 17046 17047 17048 17049 17050 17051 17052 17053 17054 17055 17056 17057 17058 17059 17060 17061 17062 17063 17064 17065 17066 17067 17068 17069 17070 17071 17072 17073 17074 17075 17076 17077 17078 17079 17080 17081 17082 17083 17084 17085 17086 17087 17088 17089 17090 17091 17092 17093 17094 17095 17096 17097 17098 17099 17100 17101 17102 17103 17104 17105 17106 17107 17108 17109 17110 17111 17112 17113 17114 17115 17116 17117 17118 17119 17120 17121 17122 17123 17124 17125 17126 17127 17128 17129 17130 17131 17132 17133 17134 17135 17136 17137 17138 17139 17140 17141 17142 17143 17144 17145 17146 17147 17148 17149 17150 17151 17152 17153 17154 17155 17156 17157 17158 17159 17160 17161 17162 17163 17164 17165 17166 17167 17168 17169 17170 17171 17172 17173 17174 17175 17176 17177 17178 17179 17180 17181 17182 17183 17184 17185 17186 17187 17188 17189 17190 17191 17192 17193 17194 17195 17196 17197 17198 17199 17200 17201 17202 17203 17204 17205 17206 17207 17208 17209 17210 17211 17212 17213 17214 17215 17216 17217 17218 17219 17220 17221 17222 17223 17224 17225 17226 17227 17228 17229 17230 17231 17232 17233 17234 17235 17236 17237 17238 17239 17240 17241 17242 17243 17244 17245 17246 17247 17248 17249 17250 17251 17252 17253 17254 17255 17256 17257 17258 17259 17260 17261 17262 17263 17264 17265 17266 17267 17268 17269 17270 17271 17272 17273 17274 17275 17276 17277 17278 17279 17280 17281 17282 17283 17284 17285 17286 17287 17288 17289 17290 17291 17292 17293 17294 17295 17296 17297 17298 17299 17300 17301 17302 17303 17304 17305 17306 17307 17308 17309 17310 17311 17312 17313 17314 17315 17316 17317 17318 17319 17320 17321 17322 17323 17324 17325 17326 17327 17328 17329 17330 17331 17332 17333 17334 17335 17336 17337 17338 17339 17340 17341 17342 17343 17344 17345 17346 17347 17348 17349 17350 17351 17352 17353 17354 17355 17356 17357 17358 17359 17360 17361 17362 17363 17364 17365 17366 17367 17368 17369 17370 17371 17372 17373 17374 17375 17376 17377 17378 17379 17380 17381 17382 17383 17384 17385 17386 17387 17388 17389 17390 17391 17392 17393 17394 17395 17396 17397 17398 17399 17400 17401 17402 17403 17404 17405 17406 17407 17408 17409 17410 17411 17412 17413 17414 17415 17416 17417 17418 17419 17420 17421 17422 17423 17424 17425 17426 17427 17428 17429 17430 17431 17432 17433 17434 17435 17436 17437 17438 17439 17440 17441 17442 17443 17444 17445 17446 17447 17448 17449 17450 17451 17452 17453 17454 17455 17456 17457 17458 17459 17460 17461 17462 17463 17464 17465 17466 17467 17468 17469 17470 17471 17472 17473 17474 17475 17476 17477 17478 17479 17480 17481 17482 17483 17484 17485 17486 17487 17488 17489 17490 17491 17492 17493 17494 17495 17496 17497 17498 17499 17500 17501 17502 17503 17504 17505 17506 17507 17508 17509 17510 17511 17512 17513 17514 17515 17516 17517 17518 17519 17520 17521 17522 17523 17524 17525 17526 17527 17528 17529 17530 17531 17532 17533 17534 17535 17536 17537 17538 17539 17540 17541 17542 17543 17544 17545 17546 17547 17548 17549 17550 17551 17552 17553 17554 17555 17556 17557 17558 17559 17560 17561 17562 17563 17564 17565 17566 17567 17568 17569 17570 17571 17572 17573 17574 17575 17576 17577 17578 17579 17580 17581 17582 17583 17584 17585 17586 17587 17588 17589 17590 17591 17592 17593 17594 17595 17596 17597 17598 17599 17600 17601 17602 17603 17604 17605 17606 17607 17608 17609 17610 17611 17612 17613 17614 17615 17616 17617 17618 17619 17620 17621 17622 17623 17624 17625 17626 17627 17628 17629 17630 17631 17632 17633 17634 17635 17636 17637 17638 17639 17640 17641 17642 17643 17644 17645 17646 17647 17648 17649 17650 17651 17652 17653 17654 17655 17656 17657 17658 17659 17660 17661 17662 17663 17664 17665 17666 17667 17668 17669 17670 17671 17672 17673 17674 17675 17676 17677 17678 17679 17680 17681 17682 17683 17684 17685 17686 17687 17688 17689 17690 17691 17692 17693 17694 17695 17696 17697 17698 17699 17700 17701 17702 17703 17704 17705 17706 17707 17708 17709 17710 17711 17712 17713 17714 17715 17716 17717 17718 17719 17720 17721 17722 17723 17724 17725 17726 17727 17728 17729 17730 17731 17732 17733 17734 17735 17736 17737 17738 17739 17740 17741 17742 17743 17744 17745 17746 17747 17748 17749 17750 17751 17752 17753 17754 17755 17756 17757 17758 17759 17760 17761 17762 17763 17764 17765 17766 17767 17768 17769 17770 17771 17772 17773 17774 17775 17776
  
     | 
    
      #define FTP_TIMEOUT
/*  C K C F T P  --  FTP Client for C-Kermit  */
char *ckftpv = "FTP Client, 10.0.269, 30 Nov 2022";
/*
  Authors:
    Jeffrey E Altman <jaltman@secure-endpoints.com>
      Secure Endpoints Inc., New York City
    Frank da Cruz <fdc@columbia.edu>,
      The Kermit Project, Columbia University.
    David Goodwin, New Zealand
  Copyright (C) 2000, 2022
    Trustees of Columbia University in the City of New York.
    All rights reserved.  See the C-Kermit COPYING.TXT file or the
    copyright text in the ckcmai.c module for disclaimer and permissions.
  Portions of conditionally included code Copyright Regents of the
    University of California and The Stanford SRP Authentication Project;
    see notices below.
*/
/*
  Pending...
  . Implement recursive NLST downloads by trying to CD to each filename.
    If it works, it's a directory; if not, it's a file -- GET it.  But
    that won't work with servers like wu-ftpd that don't send directory 
    names.  Recursion with MLSD is done.
  . Make syslog entries for session?  Files?
  . Messages are printed to stdout and stderr in random fashion.  We should
    either print everything to stdout, or else be systematic about when
    to use stderr.
  . Implement mail (MAIL, MLFL, MSOM, etc) if any servers support it.
  . Adapt to VMS.  Big job because of its record-oriented file system.
    RMS programmer required.  There are probably also some VMS TCP/IP
    product-specific wrinkles, e.g. attribute preservation in VMS-to-VMS
    transfers using special options for Multinet or other FTP servers
    (find out about STRU VMS).
*/
/*
  Quick FTP command reference:
  RFC765 (1980) and earlier:
    MODE  S(tream), B(lock), C(ompressed)
    STRU  F(ILE), R(ECORD), P(AGE)
    TYPE  A(SCII) <format>,  E(BCDIC) <format>, I(MAGE), L(OCAL) <bytesize>
    PORT  - Port
    PASV  - Passive mode
    USER  - User
    PASS  - Password
    ACCT  - Account
    CWD   - Change Working Directory
    REIN  - Logout but not disconnect
    QUIT  - Bye
    RETR  - Retreive
    STOR  - Store
    APPE  - Append
    ALLO  - Allocate
    REST  - Restart
    RNFR  - Rename from
    RNTO  - Rename to
    ABOR  - Cancel
    DELE  - Delete
    LIST  - Directory
    NLST  - Name List
    SITE  - Site parameters or commands
    STAT  - Status
    HELP  - Help
    NOOP  - Noop
  RFC959 (1985):
    CDUP  - Change to Parent Directory
    SMNT  - Structure Mount
    STOU  - Store Unique
    RMD   - Remove Directory
    MKD   - Make Directory
    PWD   - Print Directory
    SYST  - System
  RFC2389 (1998):
    FEAT  - List Features (done)
    OPTS  - Send options (done)
  RFC2640 (1999):
    LANG  - Specify language for messages (not done)
  Pending (Internet Drafts):
    SIZE  - File size (done)
    MDTM  - File modification date-time (done)
    MLST  - File name and attribute list (single file) (not done)
    MLSD  - File list with attributes (multiple files) (done)
    MAIL, MLFL, MSOM - mail delivery (not done)
  Alphabetical syntax list:
    ABOR <CRLF>
    ACCT <SP> <account-information> <CRLF>
    ALLO <SP> <decimal-integer> [<SP> R <SP> <decimal-integer>] <CRLF>
    APPE <SP> <pathname> <CRLF>
    CDUP <CRLF>
    CWD  <SP> <pathname> <CRLF>
    DELE <SP> <pathname> <CRLF>
    FEAT <CRLF>
    HELP [<SP> <string>] <CRLF>
    LANG [<SP> <language-tag> ] <CRLF>
    LIST [<SP> <pathname>] <CRLF>
    MKD  <SP> <pathname> <CRLF>
    MLSD [<SP> <pathname>] <CRLF>
    MLST [<SP> <pathname>] <CRLF>
    MODE <SP> <mode-code> <CRLF>
    NLST [<SP> <pathname-or-wildcard>] <CRLF>
    NOOP <CRLF>
    OPTS <SP> <commandname> [ <SP> <command-options> ] <CRLF>
    PASS <SP> <password> <CRLF>
    PASV <CRLF>
    PORT <SP> <host-port> <CRLF>
    PWD  <CRLF>
    QUIT <CRLF>
    REIN <CRLF>
    REST <SP> <marker> <CRLF>
    RETR <SP> <pathname> <CRLF>
    RMD  <SP> <pathname> <CRLF>
    RNFR <SP> <pathname> <CRLF>
    RNTO <SP> <pathname> <CRLF>
    SITE <SP> <string> <CRLF>
    SIZE <SP> <pathname> <CRLF>
    SMNT <SP> <pathname> <CRLF>
    STAT [<SP> <pathname>] <CRLF>
    STOR <SP> <pathname> <CRLF>
    STOU <CRLF>
    STRU <SP> <structure-code> <CRLF>
    SYST <CRLF>
    TYPE <SP> <type-code> <CRLF>
    USER <SP> <username> <CRLF>
*/
#include "ckcsym.h"                     /* Standard includes */
#include "ckcdeb.h"
#ifndef NOFTP                           /* NOFTP  = no FTP */
#ifndef SYSFTP                          /* SYSFTP = use external ftp client */
#ifdef TCPSOCKET                        /* Build only if TCP/IP included */
#define CKCFTP_C
/* Note: much of the following duplicates what was done in ckcdeb.h */
/* but let's not mess with it unless it causes trouble. */
#ifdef CK_ANSIC
#include <stdarg.h>
#else /* CK_ANSIC */
#include <varargs.h>
#endif /* CK_ANSIC */
#include <signal.h>
#ifdef OS2
#ifdef OS2ONLY
#define INCL_WINERRORS
#include <os2.h>
#endif /* OS2ONLY */
#include "ckowin.h"
#include "ckocon.h"
#endif /* OS2 */
#ifndef ZILOG
#ifdef NT
#include <setjmpex.h>
#ifdef NTSIG
extern int TlsIndex;
#endif /* NTSIG */
#else /* NT */
#include <setjmp.h>
#endif /* NT */
#else
#include <setret.h>
#endif /* ZILOG */
#include "ckcsig.h"
#ifdef VMS
/* 2010-03-09 SMS.  VAX C needs help to find "sys".  It's easier not to try. */
#include <stat.h>
#else /* def VMS */
#include <sys/stat.h>
#endif /* def VMS [else] */
#include <ctype.h>
#ifndef HPUXPRE65
#include <errno.h>			/* Error number symbols */
#else
#ifndef ERRNO_INCLUDED
#include <errno.h>			/* Error number symbols */
#endif	/* ERRNO_INCLUDED */
#endif	/* HPUXPRE65 */
#ifdef OS2
#ifndef NT
#ifdef __WATCOMC__
#include <sys/time.h>
#endif /* __WATCOMC__ */
#endif /* NT */
#endif /* OS2 */
#ifndef NOTIMEH
#include <time.h>
#endif /* NOTIMEH */
#ifndef EPIPE
#define EPIPE 32                        /* Broken pipe error */
#endif /* EPIPE */
/* Kermit includes */
#include "ckcasc.h"
#include "ckcker.h"
#include "ckucmd.h"
#include "ckuusr.h"
#include "ckcnet.h"                     /* Includes ckctel.h */
#include "ckctel.h"                     /* (then why include it again?) */
#include "ckcxla.h"
#ifdef CK_SSL
#include "ckuath.h"			/* SMS 2007/02/15 */
#endif /* def CK_SSL */
/*
  How to get the struct timeval definition so we can call select().  The
  xxTIMEH symbols are defined in ckcdeb.h, overridden in various makefile
  targets.  The problem is: maybe we have already included some header file
  that defined struct timeval, and maybe we didn't.  If we did, we don't want
  to include another header file that defines it again or the compilation will
  fail.  If we didn't, we have to include the header file where it's defined.
  But in some cases even that won't work because of strict POSIX constraints
  or somesuch, or because this introduces other conflicts (e.g. struct tm
  multiply defined), in which case we have to define it ourselves, but this
  can work only if we didn't already encounter a definition.
*/
#ifndef DCLTIMEVAL
#ifdef SV68R3V6
#define DCLTIMEVAL
#else
#ifdef SCO234
#define DCLTIMEVAL
#endif /* SCO234 */
#endif /* SV68R3V6 */
#endif /* DCLTIMEVAL */
#ifdef DCLTIMEVAL
/* Also maybe in some places the elements must be unsigned... */
struct timeval {
    long tv_sec;
    long tv_usec;
};
#ifdef COMMENT
/* Currently we don't use this... */
struct timezone {
    int tz_minuteswest;
    int tz_dsttime;
};
#endif /* COMMENT */
#else  /* !DCLTIMEVAL */
#ifndef NOSYSTIMEH
#ifdef SYSTIMEH
#include <sys/time.h>
#endif /* SYSTIMEH */
#endif /* NOSYSTIMEH */
#ifndef NOSYSTIMEBH
#ifdef SYSTIMEBH
#include <sys/timeb.h>
#endif /* SYSTIMEBH */
#endif /* NOSYSTIMEBH */
#endif /* DCLTIMEVAL */
/* 2010-03-09 SMS.  VAX C needs help to find "sys".  It's easier not to try. */
#ifdef VMS
#include <types.h>
#else /* def VMS */
#include <sys/types.h>
#endif /* def VMS [else] */
#include <stdio.h>
#include <string.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif /* HAVE_STDLIB_H */
#ifndef NOSETTIME
#ifdef COMMENT
/* This section moved to ckcdeb.h */
#ifdef POSIX
#define UTIMEH
#else
#ifdef HPUX9
#define UTIMEH
#else
#ifdef OS2
#define SYSUTIMEH
#endif /* OS2 */
#endif /* HPUX9 */
#endif /* POSIX */
#endif /* COMMENT */
#ifdef VMS				/* SMS 2007/02/15 */
#include "ckvrtl.h"			/* for utime() */
#else  /* def VMS */
#ifdef SYSUTIMEH
#include <sys/utime.h>
#else
#ifdef UTIMEH
#include <utime.h>
#define SYSUTIMEH
#endif /* UTIMEH */
#endif /* SYSUTIMEH */
#endif /* def VMS */
#endif /* NOSETTIME */
#ifndef SCO_OSR504
#ifdef SELECT_H
#include <sys/select.h>
#endif /* SELECT_H */
#endif /* SCO_OSR504 */
#ifdef OS2
#ifndef NT
#ifdef __WATCOMC__
#include <types.h>
#endif /* __WATCOMC__ */
#endif /* NT */
#endif /* OS2 */
#ifndef INADDR_NONE			/* 2010-03-29 */
#define INADDR_NONE -1
#endif	/* INADDR_NONE */
/* select() dialects... */
#ifdef UNIX
#define BSDSELECT                       /* BSD select() syntax/semantics */
#ifndef FD_SETSIZE
#define FD_SETSIZE 128
#endif	/* FD_SETSIZE */
#ifdef HPUX6				/* For HP-UX 6.5 circa 1989 */
typedef long fd_mask;
#define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
#ifndef howmany
#define howmany(x, y)   (((x)+((y)-1))/(y))
#endif	/* howmany */
#define FD_SET(n, p)    ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
#define FD_CLR(n, p)    ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
#define FD_ISSET(n, p)  ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
#define FD_COPY(f, t)   bcopy(f, t, sizeof(*(f)))
#define FD_ZERO(p)      bzero(p, sizeof(*(p)))
#endif	/* HPUX6 */
#else
#ifdef OS2                              /* OS/2 or Win32 */
#ifdef NT
#define BSDSELECT
#else /* NT */
#define IBMSELECT
#endif /* NT */
#endif /* OS2 */
#endif /* UNIX */
#ifdef VMS
#define BSDSELECT			/* SMS 2007/02/15 */
#endif /* def VMS */
/* Other select() peculiarities */
#ifdef HPUX
#ifndef HPUX10                          /* HP-UX 9.xx and earlier */
#ifndef HPUX1100
/* The three interior args to select() are (int *) rather than (fd_set *) */
#ifndef INTSELECT
#define INTSELECT
#endif /* INTSELECT */
#endif /* HPUX1100 */
#endif /* HPUX10 */
#endif /* HPUX */
#ifdef CK_SOCKS                         /* SOCKS Internet relay package */
#ifdef CK_SOCKS5                        /* SOCKS 5 */
#define accept  SOCKSaccept
#define bind    SOCKSbind
#define connect SOCKSconnect
#define getsockname SOCKSgetsockname
#define listen SOCKSlisten
#else  /* Not SOCKS 5 */
#define accept  Raccept
#define bind    Rbind
#define connect Rconnect
#define getsockname Rgetsockname
#define listen Rlisten
#endif /* CK_SOCKS5 */
#endif /* CK_SOCKS */
#ifndef NOHTTP
extern char * tcp_http_proxy;           /* Name[:port] of http proxy server */
extern int    tcp_http_proxy_errno;
extern char * tcp_http_proxy_user;
extern char * tcp_http_proxy_pwd;
extern char * tcp_http_proxy_agent;
#define HTTPCPYL 1024
static char proxyhost[HTTPCPYL];
#endif /* NOHTTP */
int ssl_ftp_proxy = 0;                  /* FTP over SSL/TLS Proxy Server */
/* Feature selection */
#ifndef USE_SHUTDOWN
/*
  We don't use shutdown() because (a) we always call it just before close()
  so it's redundant and unnecessary, and (b) it introduces a long pause on
  some platforms like SV/68 R3.
*/
/* #define USE_SHUTDOWN */
#endif /* USE_SHUTDOWN */
#ifndef NORESEND
#ifndef NORESTART                       /* Restart / recover */
#ifndef FTP_RESTART
#define FTP_RESTART
#endif /* FTP_RESTART */
#endif /* NORESTART */
#endif /* NORESEND */
#ifndef NOUPDATE                        /* Update mode */
#ifndef DOUPDATE
#define DOUPDATE
#endif /* DOUPDATE */
#endif /* NOUPDATE */
#ifndef UNICODE                         /* Unicode required */
#ifndef NOCSETS                         /* for charset translation */
#define NOCSETS
#endif /* NOCSETS */
#endif /* UNICODE */
#ifndef OS2
#ifndef HAVE_MSECS                      /* Millisecond timer */
#ifdef UNIX
#ifdef GFTIMER
#define HAVE_MSECS
#endif /* GFTIMER */
#endif /* UNIX */
#endif /* HAVE_MSECS */
#endif /* OS2 */
#ifdef PIPESEND                         /* PUT from pipe */
#ifndef PUTPIPE
#define PUTPIPE
#endif /* PUTPIPE */
#endif /* PIPESEND */
#ifndef NOSPL                           /* PUT from array */
#ifndef PUTARRAY
#define PUTARRAY
#endif /* PUTARRAY */
#endif /* NOSPL */
/* Security... */
#ifdef CK_SRP
#define FTP_SRP
#endif /* CK_SRP */
#ifdef CK_KERBEROS
#ifdef KRB4
/*
  There is a conflict between the Key Schedule formats used internally
  within the standalone MIT KRB4 library and that used by Eric Young
  in OpenSSL and his standalone DES library.  Therefore, KRB4 FTP AUTH
  cannot be supported when either of those two packages are used.
*/
#ifdef KRB524
#define FTP_KRB4
#else /* KRB524 */
#ifndef CK_SSL
#ifndef LIBDES
#define FTP_KRB4
#endif /* LIBDES */
#endif /* CK_SSL */
#endif /* KRB524 */
#endif /* KRB4 */
#ifdef KRB5
#ifndef HEIMDAL
#ifndef NOFTP_GSSAPI			/* 299 */
#ifdef HAVE_GSSAPI                      /* fdc 22 November 2022 */
#define FTP_GSSAPI
#endif  /* HAVE_GSSAPI */
#endif	/* NOFTP_GSSAPI */
#endif /* HEIMDAL */
#endif /* KRB5 */
#endif /* CK_KERBEROS */
/* FTP_SECURITY is defined if any of the above is selected */
#ifndef FTP_SECURITY
#ifdef FTP_GSSAPI
#define FTP_SECURITY
#else
#ifdef FTP_KRB4
#define FTP_SECURITY
#else
#ifdef FTP_SRP
#define FTP_SECURITY
#else
#ifdef CK_SSL
#define FTP_SECURITY
#endif /* CK_SSL */
#endif /* FTP_SRP */
#endif /* FTP_KRB4 */
#endif /* FTP_GSSAPI */
#endif /* FTP_SECURITY */
#ifdef CK_DES
#ifdef CK_SSL
#ifndef LIBDES
#define LIBDES
#endif /* LIBDES */
#endif /* CK_SSL */
#endif /* CK_DES */
#ifdef CRYPT_DLL
#ifndef LIBDES
#define LIBDES
#endif /* LIBDES */
#endif /* CRYPT_DLL */
#ifdef FTP_KRB4
#define des_cblock Block
#define des_key_schedule Schedule
#ifdef KRB524
#ifdef NT
#define _WINDOWS
#endif /* NT */
#include "kerberosIV/krb.h"
#else /* KRB524 */
#ifdef SOLARIS
#ifndef sun
/* For some reason lost in history the Makefile Solaris targets have -Usun */
#define sun
#endif /* sun */
#endif /* SOLARIS */
#include "krb.h"
#define krb_get_err_text_entry krb_get_err_text
#endif /* KRB524 */
#endif /* FTP_KRB4 */
#ifdef CK_SSL
#ifdef FTP_KRB4
#ifndef HEADER_DES_H
#define HEADER_DES_H
#endif /* HEADER_DES_H */
#endif /* FTP_KRB4 */
#include "ck_ssl.h"
#endif /* CK_SSL */
#ifdef FTP_SRP
#ifdef HAVE_PWD_H
#include "pwd.h"
#endif /* HAVE_PWD_H */
#include "t_pwd.h"
#include "t_client.h"
#include "krypto.h"
#endif /* FTP_SRP */
#ifdef FTP_GSSAPI
#include <gssapi/gssapi.h>
/* #include <gssapi/gssapi_krb5.h> */
/*
  Need to include the krb5 file, because we're doing manual fallback
  from the v2 mech to the v1 mech.  Once there's real negotiation,
  we can be generic again.
*/
#include <gssapi/gssapi_generic.h>
#include <gssapi/gssapi_krb5.h>
static gss_ctx_id_t gcontext;
#ifdef MACOSX
/** exported constants defined in gssapi_krb5{,_nx}.h **/
/* these are bogus, but will compile */
/*
 * The OID of the draft krb5 mechanism, assigned by IETF, is:
 *      iso(1) org(3) dod(5) internet(1) security(5)
 *      kerberosv5(2) = 1.3.5.1.5.2
 * The OID of the krb5_name type is:
 *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
 *      krb5(2) krb5_name(1) = 1.2.840.113554.1.2.2.1
 * The OID of the krb5_principal type is:
 *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
 *      krb5(2) krb5_principal(2) = 1.2.840.113554.1.2.2.2
 * The OID of the proposed standard krb5 mechanism is:
 *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
 *      krb5(2) = 1.2.840.113554.1.2.2
 * The OID of the proposed standard krb5 v2 mechanism is:
 *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
 *      krb5v2(3) = 1.2.840.113554.1.2.3
 *
 */
/*
 * Encoding rules: The first two values are encoded in one byte as 40
 * * value1 + value2.  Subsequent values are encoded base 128, most
 * significant digit first, with the high bit (\200) set on all octets
 * except the last in each value's encoding.
 */
static CONST gss_OID_desc
ck_krb5_gss_oid_array[] = {
   /* this is the official, rfc-specified OID */
   {9, "\052\206\110\206\367\022\001\002\002"},
   /* this is the unofficial, wrong OID */
   {5, "\053\005\001\005\002"},
   /* this is the v2 assigned OID */
   {9, "\052\206\110\206\367\022\001\002\003"},
   /* these two are name type OID's */
   {10, "\052\206\110\206\367\022\001\002\002\001"},
   {10, "\052\206\110\206\367\022\001\002\002\002"},
   { 0, 0 }
};
static
CONST gss_OID_desc * CONST gss_mech_krb5_v2 = ck_krb5_gss_oid_array+2;
#ifdef MACOSX103
static
CONST gss_OID_desc * CONST gss_mech_krb5 = ck_krb5_gss_oid_array+0;
#endif /* MACOSX103 */
#ifndef MACOSX
static
CONST gss_OID_desc * CONST gss_mech_krb5 = ck_krb5_gss_oid_array+0;
static
CONST gss_OID_desc * CONST gss_mech_krb5_old = ck_krb5_gss_oid_array+1;
static
CONST gss_OID_desc * CONST gss_nt_krb5_name = ck_krb5_gss_oid_array+3;
static
CONST gss_OID_desc * CONST gss_nt_krb5_principal = ck_krb5_gss_oid_array+4;
#endif	/* MACOSX */
/*
 * See krb5/gssapi_krb5.c for a description of the algorithm for
 * encoding an object identifier.
 */
/*
 * The OID of user_name is:
 *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
 *      generic(1) user_name(1) = 1.2.840.113554.1.2.1.1
 * machine_uid_name:
 *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
 *      generic(1) machine_uid_name(2) = 1.2.840.113554.1.2.1.2
 * string_uid_name:
 *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
 *      generic(1) string_uid_name(3) = 1.2.840.113554.1.2.1.3
 * service_name:
 *      iso(1) member-body(2) US(840) mit(113554) infosys(1) gssapi(2)
 *      generic(1) service_name(4) = 1.2.840.113554.1.2.1.4
 * exported_name:
 *      1(iso), 3(org), 6(dod), 1(internet), 5(security), 6(nametypes),
 *          4(gss-api-exported-name)
 * host_based_service_name (v2):
 *      iso (1) org (3), dod (6), internet (1), security (5), nametypes(6),
 *      gss-host-based-services(2)
 */
static gss_OID_desc ck_oids[] = {
   {10, "\052\206\110\206\367\022\001\002\001\001"},
   {10, "\052\206\110\206\367\022\001\002\001\002"},
   {10, "\052\206\110\206\367\022\001\002\001\003"},
   {10, "\052\206\110\206\367\022\001\002\001\004"},
   { 6, "\053\006\001\005\006\004"},
   { 6, "\053\006\001\005\006\002"},
};
static gss_OID ck_gss_nt_user_name = ck_oids+0;
static gss_OID ck_gss_nt_machine_uid_name = ck_oids+1;
static gss_OID ck_gss_nt_string_uid_name = ck_oids+2;
static gss_OID ck_gss_nt_service_name = ck_oids+3;
static gss_OID ck_gss_nt_exported_name = ck_oids+4;
static gss_OID ck_gss_nt_service_name_v2 = ck_oids+5;
#endif /* MACOSX */
#endif /* FTP_GSSAPI */
#ifdef OS2
#ifdef FTP_SRP
#define MAP_KRYPTO
#ifdef SRPDLL
#define MAP_SRP
#endif /* SRPDLL */
#endif /* FTP_SRP */
#ifdef FTP_KRB4
#define MAP_KRB4
#ifdef CK_ENCRYPTION
#define MAP_DES
#endif /* CK_ENCRYPTION */
#endif /* FTP_KRB4 */
#ifdef FTP_GSSAPI
#define MAP_GSSAPI
#define GSS_OIDS
#endif /* FTP_GSSAPI */
#include "ckoath.h"
extern int k95stdout, wherex[], wherey[];
extern unsigned char colorcmd;
#endif /* OS2 */
#ifdef FTP_KRB4
static char ftp_realm[REALM_SZ + 1];
static KTEXT_ST ftp_tkt;
#ifdef OS2
static LEASH_CREDENTIALS ftp_cred;
#else /* OS2 */
static CREDENTIALS ftp_cred;
#endif /* OS2 */
static MSG_DAT ftp_msg_data;
static des_key_schedule ftp_sched;
static int foo[4] = {99,99,99,99};
#endif /* FTP_KRB4 */
/* getreply() function codes */
#define GRF_AUTH 1			/* Reply to AUTH command */
#define GRF_FEAT 2			/* Reply to FEAT command */
/* Operational definitions */
#define DEF_VBM 0                       /* Default verbose mode */
/* #define SETVBM */                    /* (see getreply) */
#define URL_ONEFILE                     /* GET, not MGET, for FTP URL */
#define FTP_BUFSIZ 10240                /* Max size for FTP cmds & replies */
#define SRVNAMLEN 32                    /* Max length for server type name */
#define PWDSIZ 256
#define PASSBUFSIZ 256
#define PROMPTSIZ 256
#ifndef MGETMAX                         /* Max operands for MGET command */
#define MGETMAX 1000
#endif /* MGETMAX */
#ifdef FTP_SRP
#define FUDGE_FACTOR 100
#endif /* FTP_SRP */
/*
  Amount of growth from cleartext to ciphertext.  krb_mk_priv adds this
  number bytes.  Must be defined for each auth type.
  GSSAPI appears to add 52 bytes, but I'm not sure it is a constant--hartmans
  3DES requires 56 bytes.  Lets use 96 just to be sure.
*/
#ifdef FTP_GSSAPI
#ifndef FUDGE_FACTOR
#define FUDGE_FACTOR 96
#endif /* FUDGE_FACTOR */
#endif /* FTP_GSSAPI */
#ifdef FTP_KRB4
#ifndef FUDGE_FACTOR
#define FUDGE_FACTOR 32
#endif /* FUDGE_FACTOR */
#endif /* FTP_KRB4 */
#ifndef FUDGE_FACTOR                    /* In case no auth types define it */
#define FUDGE_FACTOR 0
#endif /* FUDGE_FACTOR */
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 64
#endif /* MAXHOSTNAMELEN */
#define MAX_DNS_NAMELEN (15*(MAXHOSTNAMELEN + 1)+1)
/* Fascist compiler toadying */
#ifndef SENDARG2TYPE
#ifdef COMMENT                          /* Might be needed here and there */
#define SENDARG2TYPE const char *
#else
#define SENDARG2TYPE char *
#endif /* COMMENT */
#endif /* SENDARG2TYPE */
/* Common text messages */
static char *nocx = "?No FTP control connection\n";
static char *fncnam[] = {
  "rename", "overwrite", "backup", "append", "discard", "ask", "update",
  "dates-differ", ""
};
/* Macro definitions */
/* Used to speed up text-mode PUTs */
#define zzout(fd,c) \
((fd<0)?(-1):((nout>=ucbufsiz)?(zzsend(fd,c)):(ucbuf[nout++]=c)))
#define CHECKCONN() if(!connected){printf("%s\n",nocx);return(-9);}
/* Externals */
#ifdef CK_URL
extern struct urldata g_url;
#endif /* CK_URL */
#ifdef DYNAMIC
extern char *zinbuffer, *zoutbuffer;    /* Regular Kermit file i/o */
#else
extern char zinbuffer[], zoutbuffer[];
#endif /* DYNAMIC */
extern char *zinptr, *zoutptr;
extern int zincnt, zoutcnt, zobufsize, fncact;
#ifdef CK_TMPDIR
extern int f_tmpdir;                    /* Directory changed temporarily */
extern char savdir[];                   /* For saving current directory */
extern char * dldir;
#endif /* CK_TMPDIR */
extern char * rfspec, * sfspec, * srfspec, * rrfspec; /* For WHERE command */
extern xx_strp xxstring;
extern struct keytab onoff[], txtbin[], rpathtab[];
extern int nrpathtab, xfiletype, patterns, gnferror, moving, what, pktnum;
extern int success, nfils, sndsrc, quiet, nopush, recursive, inserver, binary;
extern int filepeek, nscanfile, fsecs, xferstat, xfermode, lastxfer, tsecs;
extern int backgrd, spackets, rpackets, spktl, rpktl, xaskmore, cmd_rows;
extern int nolinks, msgflg, keep;
extern CK_OFF_T fsize, ffc, tfc, sendstart, sndsmaller, sndlarger, rs_len;
extern long filcnt, xfsecs, tfcps, cps, oldcps;
#ifdef FTP_TIMEOUT
int ftp_timed_out = 0;
long ftp_timeout = 0;
#endif	/* FTP_TIMEOUT */
#ifdef GFTIMER
extern CKFLOAT fptsecs, fpfsecs, fpxfsecs;
#else
extern long xfsecs;
#endif /* GFTIMER */
extern char filnam[], * filefile, myhost[];
extern char * snd_move, * rcv_move, * snd_rename, * rcv_rename;
extern int g_skipbup, skipbup, sendmode;
extern int g_displa, fdispla, displa;
#ifdef LOCUS
extern int locus, autolocus;
#endif /* LOCUS */
#ifndef NOCSETS
extern int nfilc, dcset7, dcset8, fileorder;
extern struct csinfo fcsinfo[];
extern struct keytab fcstab[];
extern int fcharset;
#endif /* NOCSETS */
extern char sndbefore[], sndafter[], *sndexcept[]; /* Selection criteria */
extern char sndnbefore[], sndnafter[], *rcvexcept[];
extern CHAR feol;
extern char * remdest;
extern int remfile, remappd, rempipe;
#ifndef NOSPL
extern int cmd_quoting;
#ifdef PUTARRAY
extern int sndxlo, sndxhi, sndxin;
extern char sndxnam[];
extern char **a_ptr[];                  /* Array pointers */
extern int a_dim[];                     /* Array dimensions */
#endif /* PUTARRAY */
#endif /* NOSPL */
#ifndef NOMSEND                         /* MPUT and ADD SEND-LIST lists */
extern char *msfiles[];
extern int filesinlist;
extern struct filelist * filehead;
extern struct filelist * filetail;
extern struct filelist * filenext;
extern int addlist;
extern char fspec[];                    /* Most recent filespec */
extern int fspeclen;                    /* Length of fspec[] buffer */
#endif /* NOMSEND */
extern int pipesend;
#ifdef PIPESEND
extern char * sndfilter, * rcvfilter;
#endif /* PIPESEND */
#ifdef CKROOT
extern int ckrooterr;
#endif /* CKROOT */
#ifdef KRB4
extern int krb4_autoget;
_PROTOTYP(char * ck_krb4_realmofhost,(char *));
#endif /* KRB4 */
#ifdef KRB5
extern int krb5_autoget;
extern int krb5_d_no_addresses;
_PROTOTYP(char * ck_krb5_realmofhost,(char *));
#endif /* KRB5 */
#ifdef DCMDBUF
extern char *atmbuf;                    /* Atom buffer (malloc'd) */
extern char *cmdbuf;                    /* Command buffer (malloc'd) */
extern char *line;                      /* Big string buffer #1 */
extern char *tmpbuf;                    /* Big string buffer #2 */
#else
extern char atmbuf[];                   /* The same, but static */
extern char cmdbuf[];
extern char line[];
extern char tmpbuf[];
#endif /* DCMDBUF */
extern char * cmarg, * cmarg2, ** cmlist; /* For setting up file lists */
/* Public variables declared here */
#ifdef NOXFER
int ftpget  =  1;                       /* GET/PUT/REMOTE orientation FTP */
#else
int ftpget  =  2;                       /* GET/PUT/REMOTE orientation AUTO */
#endif /* NOXFER */
int ftpcode = -1;                       /* Last FTP response code */
int ftp_cmdlin = 0;                     /* FTP invoked from command line */
int ftp_fai = 0;                        /* FTP failure count */
int ftp_deb = 0;                        /* FTP debugging */
int ftp_dis = -1;			/* FTP display style */
int ftp_log = 1;                        /* FTP Auto-login */
int sav_log = -1;
int ftp_action = 0;                     /* FTP action from command line */
int ftp_dates = 1;                      /* Set file dates from server */
int ftp_xfermode = XMODE_A;		/* FTP-specific transfer mode */
char ftp_reply_str[FTP_BUFSIZ] = "";    /* Last line of previous reply */
char ftp_srvtyp[SRVNAMLEN] = { NUL, NUL }; /* Server's system type */
char ftp_user_host[MAX_DNS_NAMELEN]= ""; /* FTP hostname specified by user */
char * ftp_host = NULL;                 /* FTP hostname */
char * ftp_logname = NULL;              /* FTP username */
char * ftp_rdir = NULL;                 /* Remote directory from cmdline */
char * ftp_apw = NULL;			/* Anonymous password */
/* Definitions and typedefs needed for prototypes */
#define sig_t my_sig_t
#define sigtype SIGTYP
typedef sigtype (*sig_t)();
/* Prototypes for static functions defined in ckcftp.c */
#ifdef CK_ANSIC
static VOID bytswap( int *, int * );
static VOID cancel_remote( int );
static VOID changetype( int, int );
static VOID dbtime( char *, struct tm * );
static VOID ftscreen( int, char, CK_OFF_T, char * );
static char * ftp_hookup( char *, int, int );
static char * radix_error( int );
static char * shopl( int );
static char * strval( char *, char * );
static int check_data_connection( int, int );
static int chkmodtime( char *, char *, int );
static int dataconn( char * );
static int doftpcwd( char *, int );
static int doftpdir( int );
static int doftpxmkd( char *, int );
static int ftp_login( char * );
static int ftp_rename( char *, char * );
static int ftp_umask( char * );
static int ftp_user( char *, char *, char * );
static int ftpcmd( char *, char *, int, int, int );
static int fts_cpl( int );
static int fts_dpl( int );
static int getfile( char *, char *, int, int, char *, int, int, int );
static int getreply( int, int, int, int, int );
static int ispathsep( int );
static int looping_read(int, register char *, register int );
static int looping_write( int, register CONST char *, int );
static int openftp( char *, int );
static int putfile( int, char *, char *, int, int, char *, char *,
 char *, int, int, int, int, int, int, int );
static int recvrequest(char *, char *, char *, char *, int, int,
 char *, int, int, int );
static int secure_flush( int );
static int secure_getbyte( int, int );
static int secure_read( int fd, char *, int );
static int sendrequest( char *, char *, char *, int, int, int, int );
static int syncdir( char *, int );
static int tmcompare( struct tm *, struct tm * );
static int xlatec( int, int, int, int );
static sigtype cancelrecv( int );
static sigtype cancelsend( int );
static sigtype cmdcancel( int );
#endif  /* CK_ANSIC */
/* Static global variables */
static char ftpsndbuf[FTP_BUFSIZ+64];
static char * fts_sto = NULL;
static int ftpsndret = 0;
static struct _ftpsnd {
    sig_t oldintr, oldintp;
    int            reply;
    int            incs,
                   outcs;
    char *         cmd, * local, * remote;
    int            bytes;
    int            restart;
    int            xlate;
    char *         lmode;
} ftpsnd;
/*
  This is just a first stab -- these strings should match how the
  corresponding FTP servers identify themselves.
*/
#ifdef UNIX
static char * myostype = "UNIX";
#else
#ifdef VMS
/* not yet... */
static char * myostype = "VMS";
#else
#ifdef OS2
#ifdef NT
static char * myostype = "WIN32";
#else
static char * myostype = "OS/2";
#endif /* NT */
#else
static char * myostype = "UNSUPPORTED";
#endif /* OS2  */
#endif /* VMS */
#endif /* UNIX */
static int noinit = 0;                  /* Don't send REST, STRU, MODE */
static int alike = 0;                   /* Client/server like platforms */
static int local = 1;                   /* Shadows Kermit global 'local' */
static int dout = -1;                   /* Data connection file descriptor */
static int dpyactive = 0;               /* Data transfer is active */
static int globaldin = -1;              /* Data connection f.d. */
static int out2screen = 0;              /* GET output is to screen */
static int forcetype = 0;               /* Force text or binary mode */
static int cancelfile = 0;              /* File canceled */
static int cancelgroup = 0;             /* Group canceled */
static int anonymous = 0;               /* Logging in as anonymous */
static int loggedin = 0;                /* Logged in (or not) */
static int puterror = 0;                /* What to do on PUT error */
static int geterror = 0;                /* What to do on GET error */
static int rfrc = 0;                    /* remote_files() return code */
static int okrestart = 0;               /* Server understands REST */
static int printlines = 0;              /* getreply()should print data lines */
static int haveurl = 0;                 /* Invoked by command-line FTP URL */
static int mdtmok = 1;			/* Server supports MDTM */
static int sizeok = 1;
static int featok = 1;
static int mlstok = 1;
static int stouarg = 1;
static int typesent = 0;
static int havesigint = 0;
static long havetype =  0;
static CK_OFF_T havesize = (CK_OFF_T)-1;
static char * havemdtm = NULL;
static int mgetmethod = 0;		/* NLST or MLSD */
static int mgetforced = 0;
static int i, /* j, k, */ x, y, z;      /* Volatile temporaries */
static int c0, c1;                      /* Temp variables for characters */
static char putpath[CKMAXPATH+1] = { NUL, NUL };
static char asnambuf[CKMAXPATH+1] = { NUL, NUL };
#define RFNBUFSIZ 4096			/* Remote filename buffer size */
static unsigned int maxbuf = 0, actualbuf = 0;
static CHAR *ucbuf = NULL;
static int ucbufsiz = 0;
static unsigned int nout = 0;           /* Number of chars in ucbuf */
static jmp_buf recvcancel;
static jmp_buf sendcancel;
static jmp_buf ptcancel;
static jmp_buf jcancel;
static int ptabflg = 0;
/* Protection level symbols */
#define FPL_CLR 1                       /* Clear */
#define FPL_SAF 2                       /* Safe */
#define FPL_PRV 3                       /* Private */
#define FPL_CON 4                       /* Confidential */
/* Symbols for file types returned by MLST/MLSD */
#define FTYP_FILE 1			/* Regular file */
#define FTYP_DIR  2			/* Directory */
#define FTYP_CDIR 3			/* Current directory */
#define FTYP_PDIR 4			/* Parent directory */
/* File type symbols keyed to the file-type symbols from ckcker.h */
#define FTT_ASC XYFT_T                  /* ASCII (text) */
#define FTT_BIN XYFT_B                  /* Binary (image) */
#define FTT_TEN XYFT_X                  /* TENEX (TOPS-20) */
/* Server feature table - sfttab[0] > 0 means server supports FEAT and OPTS */
static int sfttab[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
#define SFT_AUTH  1			/* FTP server feature codes */
#define SFT_LANG  2
#define SFT_MDTM  3
#define SFT_MLST  4
#define SFT_PBSZ  5
#define SFT_PROT  6
#define SFT_REST  7
#define SFT_SIZE  8
#define SFT_TVFS  9
#define SFT_UTF8 10
#define CNV_AUTO  2			/* FTP filename conversion */
#define CNV_CNV   1
#define CNV_LIT   0
/* SET FTP values */
static int                              /* SET FTP values... */
  ftp_aut = 1,                          /* Auto-authentication */
#ifdef FTP_SECURITY
  ftp_cry = 1,                          /* Auto-encryption */
  ftp_cfw = 0,                          /* Credential forwarding */
#endif /* FTP_SECURITY */
  ftp_cpl = FPL_CLR,                    /* Command protection level */
  ftp_dpl = FPL_CLR,                    /* Data protection level */
#ifdef FTP_PROXY
  ftp_prx = 0,                          /* Use proxy */
#endif /* FTP_PROXY */
  sav_psv = -1,                         /* For saving passive mode */
  ftp_psv = 1,                          /* Passive mode */
  ftp_spc = 1,                          /* Send port commands */
  ftp_typ = FTT_ASC,                    /* Type */
  get_auto = 1,                         /* Automatic type switching for GET */
  tenex = 0,                            /* Type is Tenex */
  ftp_usn = 0,                          /* Unique server names */
  ftp_prm = 0,                          /* Permissions */
  ftp_cnv = CNV_AUTO,			/* Filename conversion (2 = auto) */
  ftp_vbm = DEF_VBM,                    /* Verbose mode */
  ftp_vbx = DEF_VBM,                    /* Sticky version of same */
  ftp_err = 0,                          /* Error action */
  ftp_fnc = -1;                         /* Filename collision action */
#ifdef CK_SSL
static int ftp_bug_use_ssl_v2 = 0;      /* use SSLv2 for AUTH SSL */
static int ftp_bug_use_ssl_v3 = 0;	/* use SSLv3 for AUTH SSL */
#endif /* CK_SSL */
static int
#ifdef NOCSETS
  ftp_csr = -1,                         /* Remote (server) character set */
#else
  ftp_csr = FC_UTF8,
#endif /* NOCSETS */
  ftp_xla = 0;                          /* Character-set translation on/off */
int
  ftp_csx = -1,                         /* Remote charset currently in use */
  ftp_csl = -1;                         /* Local charset currently in use */
static int g_ftp_typ = FTT_ASC;         /* For saving and restoring ftp_typ */
char * ftp_nml = NULL;                  /* /NAMELIST */
char * ftp_tmp = NULL;                  /* Temporary string */
static char * ftp_acc = NULL;           /* Account string */
static char * auth_type = NULL;         /* Authentication type */
static char * srv_renam = NULL;         /* Server-rename string */
FILE * fp_nml = NULL;                   /* Namelist file pointer */
static int csocket = -1;                /* Control socket */
static int connected = 0;               /* Connected to FTP server */
/* static unsigned short ftp_port = 0; */ /* FTP port */ 
/* static int ftp_port = 0; */		/* SMS 2007/02/15 */
static int ftp_port = 0;		/* fdc 2007/08/30 */
#ifdef FTPHOST
static int hostcmd = 0;                 /* Has HOST command been sent */
#endif /* FTPHOST */
static int form, mode, stru, bytesize, curtype = FTT_ASC;
static char bytename[8];
/* For parsing replies to FTP server command */
static char *reply_parse, reply_buf[FTP_BUFSIZ], *reply_ptr;
#ifdef FTP_PROXY
static int proxy, unix_proxy
#endif /* FTP_PROXY */
static char pasv[64];                   /* Passive-mode port */
static int passivemode = 0;
static int sendport = 0;
static int servertype = 0;              /* FTP server's OS type */
static int testing = 0;
static char ftpcmdbuf[FTP_BUFSIZ];
/* Macro definitions */
#define UC(b) ckitoa(((int)b)&0xff)
#define nz(x) ((x) == 0 ? 1 : (x))
/* Command tables and definitions */
#define FTP_ACC  1                      /* FTP command keyword codes */
#define FTP_APP  2
#define FTP_CWD  3
#define FTP_CHM  4
#define FTP_CLS  5
#define FTP_DEL  6
#define FTP_DIR  7
#define FTP_GET  8
#define FTP_IDL  9
#define FTP_MDE 10
#define FTP_MDI 11
#define FTP_MGE 12
#define FTP_MKD 13
#define FTP_MOD 14
#define FTP_MPU 15
#define FTP_OPN 16
#define FTP_PUT 17
#define FTP_PWD 18
#define FTP_RGE 19
#define FTP_REN 20
#define FTP_RES 21
#define FTP_HLP 22
#define FTP_RMD 23
#define FTP_STA 24
#define FTP_SIT 25
#define FTP_SIZ 26
#define FTP_SYS 27
#define FTP_UMA 28
#define FTP_GUP 29
#define FTP_USR 30
#define FTP_QUO 31
#define FTP_TYP 32
#define FTP_FEA 33
#define FTP_OPT 34
#define FTP_CHK 35
#define FTP_VDI 36
#define FTP_ENA 37
#define FTP_DIS 38
#define FTP_REP 39
struct keytab gprtab[] = {              /* GET-PUT-REMOTE keywords */
    { "auto",    2, 0 },
    { "ftp",     1, 0 },
    { "kermit",  0, 0  }
};
static struct keytab qorp[] = {         /* QUIT or PROCEED keywords */
    { "proceed", 0, 0 },                /* 0 = proceed */
    { "quit",    1, 0 }                 /* 1 = quit */
};
static struct keytab ftpcmdtab[] = {    /* FTP command table */
    { "account",   FTP_ACC, 0 },
    { "append",    FTP_APP, 0 },
    { "bye",       FTP_CLS, 0 },
    { "cd",        FTP_CWD, 0 },
    { "cdup",      FTP_GUP, 0 },
    { "check",     FTP_CHK, 0 },
    { "chmod",     FTP_CHM, 0 },
    { "close",     FTP_CLS, 0 },
    { "cwd",       FTP_CWD, CM_INV },
    { "delete",    FTP_MDE, 0 },
    { "directory", FTP_DIR, 0 },
    { "disable",   FTP_DIS, 0 },
    { "enable",    FTP_ENA, 0 },
    { "features",  FTP_FEA, 0 },
    { "get",       FTP_GET, 0 },
    { "help",      FTP_HLP, 0 },
    { "idle",      FTP_IDL, 0 },
    { "login",     FTP_USR, CM_INV },
    { "mdelete",   FTP_MDE, CM_INV },
    { "mget",      FTP_MGE, 0 },
    { "mkdir",     FTP_MKD, 0 },
    { "modtime",   FTP_MOD, 0 },
    { "mput",      FTP_MPU, 0 },
    { "open",      FTP_OPN, 0 },
    { "opt",       FTP_OPT, CM_INV|CM_ABR },
    { "opts",      FTP_OPT, CM_INV },
    { "options",   FTP_OPT, 0 },
    { "put",       FTP_PUT, 0 },
    { "pwd",       FTP_PWD, 0 },
    { "quit",      FTP_CLS, CM_INV },
    { "quote",     FTP_QUO, 0 },
    { "reget",     FTP_RGE, 0 },
    { "rename",    FTP_REN, 0 },
    { "reput",     FTP_REP, 0 },
    { "resend",    FTP_REP, CM_INV },
    { "reset",     FTP_RES, 0 },
    { "rmdir",     FTP_RMD, 0 },
    { "send",      FTP_PUT, CM_INV },
    { "site",      FTP_SIT, 0 },
    { "size",      FTP_SIZ, 0 },
    { "status",    FTP_STA, 0 },
    { "system",    FTP_SYS, 0 },
    { "type",      FTP_TYP, 0 },
    { "umask",     FTP_UMA, 0 },
    { "up",        FTP_GUP, CM_INV },
    { "user",      FTP_USR, 0 },
    { "vdirectory",FTP_VDI, 0 },
    { "", 0, 0 }
};
static int nftpcmd = (sizeof(ftpcmdtab) / sizeof(struct keytab)) - 1;
#define OPN_ANO 1			/* FTP OPEN switch codes */
#define OPN_PSW 2
#define OPN_USR 3
#define OPN_ACC 4
#define OPN_ACT 5
#define OPN_PSV 6
#define OPN_TLS 7
#define OPN_NIN 8
#define OPN_NOL 9
#ifdef FTP_SECURITY
#ifdef CK_SSL
#define USETLSTAB
static struct keytab tlstab[] = {       /* FTP SSL/TLS switches */
    { "/ssl",       OPN_TLS, 0    },
    { "/tls",       OPN_TLS, 0    },
    { "", 0, 0 }
};
static int ntlstab = (sizeof(tlstab) / sizeof(struct keytab)) - 1;
#endif /* CK_SSL */
#endif /* FTP_SECURITY */
static struct keytab ftpswitab[] = {    /* FTP command switches */
    { "/account",   OPN_ACC, CM_ARG },
    { "/active",    OPN_ACT, 0      },
    { "/anonymous", OPN_ANO, 0      },
    { "/noinit",    OPN_NIN, 0      },
    { "/nologin",   OPN_NOL, 0      },
    { "/passive",   OPN_PSV, 0      },
    { "/password",  OPN_PSW, CM_ARG },
    { "/user",      OPN_USR, CM_ARG },
    { "", 0, 0 }
};
static int nftpswi = (sizeof(ftpswitab) / sizeof(struct keytab)) - 1;
/* FTP { ENABLE, DISABLE } items */
#define ENA_FEAT 1
#define ENA_MDTM 2
#define ENA_MLST 3
#define ENA_SIZE 4
#define ENA_AUTH 5
static struct keytab ftpenatab[] = {
    { "AUTH",  ENA_AUTH, 0 },
    { "FEAT",  ENA_FEAT, 0 },
    { "MDTM",  ENA_MDTM, 0 },
    { "ML",    ENA_MLST, CM_INV|CM_ABR },
    { "MLS",   ENA_MLST, CM_INV|CM_ABR },
    { "MLSD",  ENA_MLST, CM_INV },
    { "MLST",  ENA_MLST, 0 },
    { "SIZE",  ENA_SIZE, 0 },
    { "", 0, 0 }
};
static int nftpena = (sizeof(ftpenatab) / sizeof(struct keytab)) - 1;
/* SET FTP command keyword indices */
#define FTS_AUT  1                      /* Autoauthentication */
#define FTS_CRY  2                      /* Encryption */
#define FTS_LOG  3                      /* Autologin */
#define FTS_CPL  4                      /* Command protection level */
#define FTS_CFW  5                      /* Credentials forwarding */
#define FTS_DPL  6                      /* Data protection level */
#define FTS_DBG  7                      /* Debugging */
#define FTS_PSV  8                      /* Passive mode */
#define FTS_SPC  9                      /* Send port commands */
#define FTS_TYP 10                      /* (file) Type */
#define FTS_USN 11                      /* Unique server names (for files) */
#define FTS_VBM 12                      /* Verbose mode */
#define FTS_ATP 13                      /* Authentication type */
#define FTS_CNV 14                      /* Filename conversion */
#define FTS_TST 15                      /* Test (progress) messages */
#define FTS_PRM 16                      /* (file) Permissions */
#define FTS_XLA 17                      /* Charset translation */
#define FTS_CSR 18                      /* Server charset */
#define FTS_ERR 19                      /* Error action */
#define FTS_FNC 20                      /* Collision */
#define FTS_SRP 21                      /* SRP options */
#define FTS_GFT 22                      /* GET automatic file-type switching */
#define FTS_DAT 23                      /* Set file dates */
#define FTS_STO 24			/* Server time offset */
#define FTS_APW 25			/* Anonymous password */
#define FTS_DIS 26			/* File-transfer display style */
#define FTS_BUG 27                      /* Bug(s) */
#define FTS_TMO 28			/* Timeout */
/* FTP BUGS */
#define FTB_SV2  1                      /* use SSLv2 */
#define FTB_SV3  2                      /* use SSLv3 */
static struct keytab ftpbugtab[] = {
    { "use-ssl-v2",     FTB_SV2,        0 },
    { "use-ssl-v3",	FTB_SV3,	0 }
};
static int nftpbug = (sizeof(ftpbugtab) / sizeof(struct keytab));
/* FTP PUT options (mutually exclusive, not a bitmask) */
#define PUT_UPD 1                       /* Update */
#define PUT_RES 2                       /* Restart */
#define PUT_SIM 4                       /* Simulation */
#define PUT_DIF 8			/* Dates Differ */
static struct keytab ftpcolxtab[] = { /* SET FTP COLLISION options */
#ifndef MAC
    { "append",    XYFX_A, 0 },         /* append to old file */
#endif /* MAC */
#ifdef COMMENT
    { "ask",       XYFX_Q, 0 },         /* ask what to do (not implemented) */
#endif
    { "backup",    XYFX_B, 0 },         /* rename old file */
#ifndef MAC
    { "dates-differ", XYFX_M, 0 },	/* accept if dates differ */
    { "discard",   XYFX_D, 0 },         /* don't accept new file */
    { "no-supersede", XYFX_D, CM_INV }, /* ditto (MSK compatibility) */
#endif /* MAC */
    { "overwrite", XYFX_X, 0 },         /* overwrite the old file */
    { "rename",    XYFX_R, 0 },         /* rename the incoming file */
#ifndef MAC                             /* This crashes Mac Kermit. */
    { "update",    XYFX_U, 0 },         /* replace if newer */
#endif /* MAC */
    { "", 0, 0 }
};
static int nftpcolx = (sizeof(ftpcolxtab) / sizeof(struct keytab)) - 1;
#ifdef FTP_SECURITY
/* FTP authentication options */
#define FTA_AUTO 0                      /* Auto */
#define FTA_SRP  1                      /* SRP */
#define FTA_GK5  2                      /* Kerberos 5 */
#define FTA_K4   3                      /* Kerberos 4 */
#define FTA_SSL  4                      /* SSL */
#define FTA_TLS  5                      /* TLS */
/* FTP authentication types */
#define FTPATYPS 8
static int ftp_auth_type[FTPATYPS] = {
#ifdef FTP_GSSAPI
    FTA_GK5,                            /* GSSAPI Kerberos 5 */
#endif /* FTP_GK5 */
#ifdef FTP_SRP
    FTA_SRP,                            /* SRP */
#endif /* FTP_SRP */
#ifdef FTP_KRB4
    FTA_K4,                             /* Kerberos 4 */
#endif /* FTP_KRB4 */
#ifdef CK_SSL
    FTA_TLS,                            /* TLS */
    FTA_SSL,                            /* SSL */
#endif /* CK_SSL */
    0
};
static struct keytab ftpauth[] = {      /* SET FTP AUTHTYPE cmd table */
    { "automatic", FTA_AUTO,  CM_INV },
#ifdef FTP_GSSAPI
    { "gssapi-krb5", FTA_GK5, 0 },
#endif /* FTP_GSSAPI */
#ifdef FTP_KRB4
    { "k4",       FTA_K4,     CM_INV },
#endif /* FTP_KRB4 */
#ifdef FTP_GSSAPI
    { "k5",        FTA_GK5,   CM_INV },
#endif /* FTP_GSSAPI */
#ifdef FTP_KRB4
    { "kerberos4", FTA_K4,    0 },
#endif /* FTP_KRB4 */
#ifdef FTP_GSSAPI
    { "kerberos5", FTA_GK5,   CM_INV },
#endif /* FTP_GSSAPI */
#ifdef FTP_KRB4
    { "kerberos_iv",FTA_K4,   CM_INV },
#endif /* FTP_KRB4 */
#ifdef FTP_GSSAPI
    { "kerberos_v", FTA_GK5,  CM_INV },
#endif /* FTP_GSSAPI */
#ifdef FTP_KRB4
    { "krb4",     FTA_K4,     CM_INV },
#endif /* FTP_KRB4 */
#ifdef FTP_GSSAPI
    { "krb5",     FTA_GK5,    CM_INV },
#endif /* FTP_GSSAPI */
#ifdef FTP_SRP
    { "srp",      FTA_SRP,     0 },
#endif /* FTP_SRP */
#ifdef CK_SSL
    { "ssl",      FTA_SSL,     0 },
    { "tls",      FTA_TLS,     0 },
#endif /* CK_SSL */
    { "", 0, 0 }
};
static int nftpauth = (sizeof(ftpauth) / sizeof(struct keytab)) - 1;
#ifdef FTP_SRP
#define SRP_CIPHER 1
#define SRP_HASH   2
static struct keytab ftpsrp[] = {      /* SET FTP SRP command table */
    { "cipher",   SRP_CIPHER,     0 },
    { "hash",     SRP_HASH,       0 },
    { "", 0, 0 }
};
static int nftpsrp = (sizeof(ftpsrp) / sizeof(struct keytab)) - 1;
#endif /* FTP_SRP */
#endif /* FTP_SECURITY */
static struct keytab ftpset[] = {       /* SET FTP commmand table */
    { "anonymous-password",       FTS_APW, 0 },
#ifdef FTP_SECURITY
    { "authtype",                 FTS_ATP, 0 },
    { "autoauthentication",       FTS_AUT, 0 },
    { "autoencryption",           FTS_CRY, 0 },
#endif /* FTP_SECURITY */
    { "autologin",                FTS_LOG, 0 },
    { "bug",                      FTS_BUG, 0 },
#ifndef NOCSETS
    { "character-set-translation",FTS_XLA, 0 },
#endif /* NOCSETS */
    { "collision",                FTS_FNC, 0 },
#ifdef FTP_SECURITY
    { "command-protection-level", FTS_CPL, 0 },
    { "cpl",                      FTS_CPL, CM_INV },
    { "credential-forwarding",    FTS_CFW, 0 },
    { "da",                       FTS_DAT, CM_INV|CM_ABR },
    { "data-protection-level",    FTS_DPL, 0 },
#endif /* FTP_SECURITY */
    { "dates",                    FTS_DAT, 0 },
    { "debug",                    FTS_DBG, 0 },
    { "display",                  FTS_DIS, 0 },
#ifdef FTP_SECURITY
    { "dpl",                      FTS_DPL, CM_INV },
#endif /* FTP_SECURITY */
    { "error-action",             FTS_ERR, 0 },
    { "filenames",                FTS_CNV, 0 },
    { "get-filetype-switching",   FTS_GFT, 0 },
    { "passive-mode",             FTS_PSV, 0 },
    { "pasv",                     FTS_PSV, CM_INV },
    { "permissions",              FTS_PRM, 0 },
    { "progress-messages",        FTS_TST, 0 },
    { "send-port-commands",       FTS_SPC, 0 },
#ifndef NOCSETS
    { "server-character-set",     FTS_CSR, 0 },
#endif /* NOCSETS */
    { "server-time-offset",       FTS_STO, 0 },
#ifdef FTP_SRP
    { "srp",                      FTS_SRP, 0 },
#else
    { "srp",                      FTS_SRP, CM_INV },
#endif /* FTP_SRP */
#ifdef FTP_TIMEOUT
    { "timeout",                  FTS_TMO, 0 },
#endif	/* FTP_TIMEOUT */
    { "type",                     FTS_TYP, 0 },
    { "unique-server-names",      FTS_USN, 0 },
    { "verbose-mode",             FTS_VBM, 0 },
    { "", 0, 0 }
};
static int nftpset = (sizeof(ftpset) / sizeof(struct keytab)) - 1;
/*
  GET and PUT switches are approximately the same as Kermit GET and SEND,
  and use the same SND_xxx definitions, but hijack a couple for FTP use.
  Don't just make up new ones, since the number of SND_xxx options must be
  known in advance for the switch-parsing arrays.
*/
#define SND_USN SND_PRO                 /* /UNIQUE instead of /PROTOCOL */
#define SND_PRM SND_PIP                 /* /PERMISSIONS instead of /PIPES */
#define SND_TEN SND_CAL                 /* /TENEX instead of /CALIBRATE */
static struct keytab putswi[] = {       /* FTP PUT switch table */
    { "/after",                SND_AFT, CM_ARG },
#ifdef PUTARRAY
    { "/array",                SND_ARR, CM_ARG },
#endif /* PUTARRAY */
    { "/as",                   SND_ASN, CM_ARG|CM_INV|CM_ABR },
    { "/as-name",              SND_ASN, CM_ARG },
    { "/ascii",                SND_TXT, CM_INV },
    { "/b",                    SND_BIN, CM_INV|CM_ABR },
    { "/before",               SND_BEF, CM_ARG },
    { "/binary",               SND_BIN, 0 },
#ifdef PUTPIPE
    { "/command",              SND_CMD, CM_PSH },
#endif /* PUTPIPE */
#ifdef COMMENT
/* This works but it's dangerous */
#ifdef DOUPDATE
    { "/dates-differ",         SND_DIF, CM_INV },
#endif /* DOUPDATE */
#endif /* COMMENT */
    { "/delete",               SND_DEL, 0 },
#ifdef UNIXOROSK
    { "/dotfiles",             SND_DOT, 0 },
#endif /* UNIXOROSK */
    { "/error-action",         SND_ERR, CM_ARG },
    { "/except",               SND_EXC, CM_ARG },
    { "/filenames",            SND_NAM, CM_ARG },
#ifdef PIPESEND
#ifndef NOSPL
    { "/filter",               SND_FLT, CM_ARG|CM_PSH },
#endif /* NOSPL */
#endif /* PIPESEND */
#ifdef CKSYMLINK
    { "/followlinks",          SND_LNK, 0 },
#endif /* CKSYMLINK */
#ifdef VMS
    { "/image",                SND_IMG, 0 },
#else
    { "/image",                SND_BIN, CM_INV },
#endif /* VMS */
    { "/larger-than",          SND_LAR, CM_ARG },
    { "/listfile",             SND_FIL, CM_ARG },
#ifndef NOCSETS
    { "/local-character-set",  SND_CSL, CM_ARG },
#endif /* NOCSETS */
#ifdef CK_TMPDIR
    { "/move-to",              SND_MOV, CM_ARG },
#endif /* CK_TMPDIR */
    { "/nobackupfiles",        SND_NOB, 0 },
#ifdef UNIXOROSK
    { "/nodotfiles",           SND_NOD, 0 },
#endif /* UNIXOROSK */
#ifdef CKSYMLINK
    { "/nofollowlinks",        SND_NLK, 0 },
#endif /* CKSYMLINK */
    { "/not-after",            SND_NAF, CM_ARG },
    { "/not-before",           SND_NBE, CM_ARG },
#ifdef UNIX
    { "/permissions",          SND_PRM, CM_ARG },
#else
    { "/permissions",          SND_PRM, CM_ARG|CM_INV },
#endif /* UNIX */
    { "/quiet",                SND_SHH, 0 },
#ifdef FTP_RESTART
    { "/recover",              SND_RES, 0 },
#endif /* FTP_RESTART */
#ifdef RECURSIVE
    { "/recursive",            SND_REC, 0 },
#endif /* RECURSIVE */
    { "/rename-to",            SND_REN, CM_ARG },
#ifdef FTP_RESTART
    { "/restart",              SND_RES, CM_INV },
#endif /* FTP_RESTART */
#ifndef NOCSETS
    { "/server-character-set", SND_CSR, CM_ARG },
#endif /* NOCSETS */
    { "/server-rename-to",     SND_SRN, CM_ARG },
    { "/simulate",             SND_SIM, 0 },
    { "/since",                SND_AFT, CM_INV|CM_ARG },
    { "/smaller-than",         SND_SMA, CM_ARG },
#ifdef COMMENT
    { "/starting-at",          SND_STA, CM_ARG },
#endif /* COMMENT */
#ifdef RECURSIVE
    { "/subdirectories",       SND_REC, CM_INV },
#endif /* RECURSIVE */
    { "/tenex",                SND_TEN, 0 },
    { "/text",                 SND_TXT, 0 },
#ifndef NOCSETS
    { "/transparent",          SND_XPA, 0 },
#endif /* NOCSETS */
    { "/type",                 SND_TYP, CM_ARG },
#ifdef DOUPDATE
    { "/update",               SND_UPD, 0 },
#endif /* DOUPDATE */
    { "/unique-server-names",  SND_USN, 0 },
    { "", 0, 0 }
};
static int nputswi = (sizeof(putswi) / sizeof(struct keytab)) - 1;
static struct keytab getswi[] = {       /* FTP [M]GET switch table */
    { "/after",                SND_AFT, CM_INV },
    { "/as",                   SND_ASN, CM_ARG|CM_INV|CM_ABR },
    { "/as-name",              SND_ASN, CM_ARG },
    { "/ascii",                SND_TXT, CM_INV },
    { "/before",               SND_BEF, CM_INV },
    { "/binary",               SND_BIN, 0 },
    { "/collision",            SND_COL, CM_ARG },
#ifdef PUTPIPE
    { "/command",              SND_CMD, CM_PSH },
#endif /* PUTPIPE */
    { "/delete",               SND_DEL, 0 },
    { "/error-action",         SND_ERR, CM_ARG },
    { "/except",               SND_EXC, CM_ARG },
    { "/filenames",            SND_NAM, CM_ARG },
#ifdef PIPESEND
#ifndef NOSPL
    { "/filter",               SND_FLT, CM_ARG|CM_PSH },
#endif /* NOSPL */
#endif /* PIPESEND */
#ifdef VMS
    { "/image",                SND_IMG, 0 },
#else
    { "/image",                SND_BIN, CM_INV },
#endif /* VMS */
    { "/larger-than",          SND_LAR, CM_ARG },
    { "/listfile",             SND_FIL, CM_ARG },
#ifndef NOCSETS
    { "/local-character-set",  SND_CSL, CM_ARG },
#endif /* NOCSETS */
    { "/match",                SND_PAT, CM_ARG },
    { "/ml",                   SND_MLS, CM_INV|CM_ABR },
    { "/mls",                  SND_MLS, CM_INV|CM_ABR },
    { "/mlsd",                 SND_MLS, 0 },
    { "/mlst",                 SND_MLS, CM_INV },
#ifdef CK_TMPDIR
    { "/move-to",              SND_MOV, CM_ARG },
#endif /* CK_TMPDIR */
    { "/namelist",             SND_NML, CM_ARG },
    { "/nlst",                 SND_NLS, 0 },
    { "/nobackupfiles",        SND_NOB, 0 },
    { "/nodotfiles",           SND_NOD, 0 },
#ifdef DOUPDATE
    { "/dates-differ",         SND_DIF, CM_INV },
#endif /* DOUPDATE */
    { "/not-after",            SND_NAF, CM_INV },
    { "/not-before",           SND_NBE, CM_INV },
    { "/permissions",          SND_PRM, CM_INV },
    { "/quiet",                SND_SHH, 0 },
#ifdef FTP_RESTART
    { "/recover",              SND_RES, 0 },
#endif /* FTP_RESTART */
#ifdef RECURSIVE
    { "/recursive",            SND_REC, 0 },
#endif /* RECURSIVE */
    { "/rename-to",            SND_REN, CM_ARG },
#ifdef FTP_RESTART
    { "/restart",              SND_RES, CM_INV },
#endif /* FTP_RESTART */
#ifndef NOCSETS
    { "/server-character-set", SND_CSR, CM_ARG },
#endif /* NOCSETS */
    { "/server-rename-to",     SND_SRN, CM_ARG },
    { "/smaller-than",         SND_SMA, CM_ARG },
#ifdef RECURSIVE
    { "/subdirectories",       SND_REC, CM_INV },
#endif /* RECURSIVE */
    { "/text",                 SND_TXT, 0 },
    { "/tenex",                SND_TEN, 0 },
#ifndef NOCSETS
    { "/transparent",          SND_XPA, 0 },
#endif /* NOCSETS */
    { "/to-screen",            SND_MAI, 0 },
#ifdef DOUPDATE
    { "/update",               SND_UPD, CM_INV },
#endif /* DOUPDATE */
    { "", 0, 0 }
};
static int ngetswi = (sizeof(getswi) / sizeof(struct keytab)) - 1;
static struct keytab delswi[] = {       /* FTP [M]DELETE switch table */
    { "/error-action",         SND_ERR, CM_ARG },
    { "/except",               SND_EXC, CM_ARG },
    { "/filenames",            SND_NAM, CM_ARG },
    { "/larger-than",          SND_LAR, CM_ARG },
    { "/nobackupfiles",        SND_NOB, 0 },
#ifdef UNIXOROSK
    { "/nodotfiles",           SND_NOD, 0 },
#endif /* UNIXOROSK */
    { "/quiet",                SND_SHH, 0 },
#ifdef RECURSIVE
    { "/recursive",            SND_REC, 0 },
#endif /* RECURSIVE */
    { "/smaller-than",         SND_SMA, CM_ARG },
#ifdef RECURSIVE
    { "/subdirectories",       SND_REC, CM_INV },
#endif /* RECURSIVE */
    { "", 0, 0 }
};
static int ndelswi = (sizeof(delswi) / sizeof(struct keytab)) - 1;
static struct keytab fntab[] = {        /* Filename conversion keyword table */
    { "automatic",    2, CNV_AUTO },
    { "converted",    1, CNV_CNV  },
    { "literal",      0, CNV_LIT  }
};
static int nfntab = (sizeof(fntab) / sizeof(struct keytab));
static struct keytab ftptyp[] = {       /* SET FTP TYPE table */
    { "ascii",        FTT_ASC, 0 },
    { "binary",       FTT_BIN, 0 },
    { "tenex",        FTT_TEN, 0 },
    { "text",         FTT_ASC, CM_INV },
    { "", 0, 0 }
};
static int nftptyp = (sizeof(ftptyp) / sizeof(struct keytab)) - 1;
#ifdef FTP_SECURITY
static struct keytab ftppro[] = {       /* SET FTP PROTECTION-LEVEL table */
    { "clear",        FPL_CLR, 0 },
    { "confidential", FPL_CON, 0 },
    { "private",      FPL_PRV, 0 },
    { "safe",         FPL_SAF, 0 },
    { "", 0, 0 }
};
static int nftppro = (sizeof(ftppro) / sizeof(struct keytab)) - 1;
#endif /* FTP_SECURITY */
/* Definitions for FTP from RFC765. */
/* Reply codes */
#define REPLY_PRELIM    1               /* Positive preliminary */
#define REPLY_COMPLETE  2               /* Positive completion */
#define REPLY_CONTINUE  3               /* Positive intermediate */
#define REPLY_TRANSIENT 4               /* Transient negative completion */
#define REPLY_ERROR     5               /* Permanent negative completion */
#define REPLY_SECURE    6               /* Security encoded message */
/* Form codes and names */
#define FORM_N 1                        /* Non-print */
#define FORM_T 2                        /* Telnet format effectors */
#define FORM_C 3                        /* Carriage control (ASA) */
/* Structure codes and names */
#define STRU_F 1                        /* File (no record structure) */
#define STRU_R 2                        /* Record structure */
#define STRU_P 3                        /* Page structure */
/* Mode types and names */
#define MODE_S 1                        /* Stream */
#define MODE_B 2                        /* Block */
#define MODE_C 3                        /* Compressed */
/* Protection levels and names */
#define PROT_C 1                        /* Clear */
#define PROT_S 2                        /* Safe */
#define PROT_P 3                        /* Private */
#define PROT_E 4                        /* Confidential */
#ifdef COMMENT                          /* Not used */
#ifdef FTP_NAMES
char *strunames[]  =  {"0", "File",     "Record", "Page" };
char *formnames[]  =  {"0", "Nonprint", "Telnet", "Carriage-control" };
char *modenames[]  =  {"0", "Stream",   "Block",  "Compressed" };
char *levelnames[] =  {"0", "Clear",    "Safe",   "Private",  "Confidential" };
#endif /* FTP_NAMES */
/* Record Tokens */
#define REC_ESC '\377'                  /* Record-mode Escape */
#define REC_EOR '\001'                  /* Record-mode End-of-Record */
#define REC_EOF '\002'                  /* Record-mode End-of-File */
/* Block Header */
#define BLK_EOR           0x80          /* Block is End-of-Record */
#define BLK_EOF           0x40          /* Block is End-of-File */
#define BLK_REPLY_ERRORS  0x20          /* Block might have errors */
#define BLK_RESTART       0x10          /* Block is Restart Marker */
#define BLK_BYTECOUNT 2                 /* Bytes in this block */
#endif /* COMMENT */
#define RADIX_ENCODE 0                  /* radix_encode() function codes */
#define RADIX_DECODE 1
/*
  The default setpbsz() value in the Unix FTP client is 1<<20 (1MB).  This
  results in a serious performance degradation due to the increased number
  of page faults and the inability to overlap encrypt/decrypt, file i/o, and
  network i/o.  So instead we set the value to 1<<13 (8K), about half the size
  of the typical TCP window.  Maybe we should add a command to allow the value
  to be changed.
*/
#define DEFAULT_PBSZ 1<<13
/* Prototypes */
_PROTOTYP(int remtxt, (char **) );
_PROTOTYP(char * gskreason, (int) );
_PROTOTYP(static int ftpclose,(void));
_PROTOTYP(static int zzsend, (int, CHAR));
_PROTOTYP(static int getreply,(int,int,int,int,int));
_PROTOTYP(static int radix_encode,(CHAR[], CHAR[], int, int *, int));
_PROTOTYP(static int setpbsz,(unsigned int));
_PROTOTYP(static int recvrequest,(char *,char *,char *,char *,
  int,int,char *,int,int,int));
_PROTOTYP(static int ftpcmd,(char *,char *,int,int,int));
_PROTOTYP(static int fts_cpl,(int));
_PROTOTYP(static int fts_dpl,(int));
#ifdef FTP_SECURITY
_PROTOTYP(static int ftp_auth, (void));
#endif /* FTP_SECURITY */
_PROTOTYP(static int ftp_user, (char *, char *, char *));
_PROTOTYP(static int ftp_login, (char *));
_PROTOTYP(static int ftp_reset, (void));
_PROTOTYP(static int ftp_rename, (char *, char *));
_PROTOTYP(static int ftp_umask, (char *));
_PROTOTYP(static int secure_flush, (int));
#ifdef COMMENT
_PROTOTYP(static int secure_putc, (char, int));
#endif /* COMMENT */
_PROTOTYP(static int secure_write, (int, CHAR *, unsigned int));
_PROTOTYP(static int scommand, (char *));
_PROTOTYP(static int secure_putbuf, (int, CHAR *, unsigned int));
_PROTOTYP(static int secure_getc, (int, int));
_PROTOTYP(static int secure_getbyte, (int, int));
_PROTOTYP(static int secure_read, (int, char *, int));
_PROTOTYP(static int initconn, (void));
_PROTOTYP(static int dataconn, (char *));
_PROTOTYP(static int setprotbuf,(unsigned int));
_PROTOTYP(static int sendrequest, (char *, char *, char *, int,int,int,int));
_PROTOTYP(static char * radix_error,(int));
_PROTOTYP(static char * ftp_hookup,(char *, int, int));
_PROTOTYP(static CHAR * remote_files, (int, CHAR *, CHAR *, int));
_PROTOTYP(static VOID mlsreset, (void));
_PROTOTYP(static VOID secure_error, (char *fmt, ...));
_PROTOTYP(static VOID lostpeer, (void));
_PROTOTYP(static VOID cancel_remote, (int));
_PROTOTYP(static VOID changetype, (int, int));
_PROTOTYP(static sigtype cmdcancel, (int));
#ifdef FTP_SRP
_PROTOTYP(static int srp_reset, ());
_PROTOTYP(static int srp_ftp_auth, (char *,char *,char *));
_PROTOTYP(static int srp_put, (CHAR *, CHAR **, int, int *));
_PROTOTYP(static int srp_get, (CHAR **, CHAR **, int *, int *));
_PROTOTYP(static int srp_encode, (int, CHAR *, CHAR *, unsigned int));
_PROTOTYP(static int srp_decode, (int, CHAR *, CHAR *, unsigned int));
_PROTOTYP(static int srp_selcipher, (char *));
_PROTOTYP(static int srp_selhash, (char *));
#endif /* FTP_SRP */
#ifdef FTP_GSSAPI
_PROTOTYP(static void user_gss_error,(OM_uint32, OM_uint32,char *));
#endif /* FTP_GSSAPI */
/*  D O F T P A R G  --  Do an FTP command-line argument.  */
#ifdef FTP_SECURITY
#ifndef NOICP
#define FT_NOGSS   1
#define FT_NOK4    2
#define FT_NOSRP   3
#define FT_NOSSL   4
#define FT_NOTLS   5
#define FT_CERTFI  6
#define FT_OKCERT  7
#define FT_DEBUG   8
#define FT_KEY     9
#define FT_SECURE 10
#define FT_VERIFY 11
static struct keytab ftpztab[] = {
    { "!gss",    FT_NOGSS,  0 },
    { "!krb4",   FT_NOK4,   0 },
    { "!srp",    FT_NOSRP,  0 },
    { "!ssl",    FT_NOSSL,  0 },
    { "!tls",    FT_NOTLS,  0 },
    { "cert",    FT_CERTFI, CM_ARG },
    { "certsok", FT_OKCERT, 0 },
    { "debug",   FT_DEBUG,  0 },
    { "key",     FT_KEY,    CM_ARG },
    { "nogss",   FT_NOGSS,  0 },
    { "nokrb4",  FT_NOK4,   0 },
    { "nosrp",   FT_NOSRP,  0 },
    { "nossl",   FT_NOSSL,  0 },
    { "notls",   FT_NOTLS,  0 },
#ifdef COMMENT
    { "secure",  FT_SECURE, 0 },
#endif /* COMMENT */
    { "verify",  FT_VERIFY, CM_ARG },
    { "", 0, 0 }
};
static int nftpztab = sizeof(ftpztab) / sizeof(struct keytab) - 1;
/*
  The following cipher and hash tables should be replaced with
  dynamicly created versions based upon the linked library.
*/
#define SRP_BLOWFISH_ECB    1
#define SRP_BLOWFISH_CBC    2
#define SRP_BLOWFISH_CFB64  3
#define SRP_BLOWFISH_OFB64  4
#define SRP_CAST5_ECB       5
#define SRP_CAST5_CBC       6
#define SRP_CAST5_CFB64     7
#define SRP_CAST5_OFB64     8
#define SRP_DES_ECB         9
#define SRP_DES_CBC        10
#define SRP_DES_CFB64      11
#define SRP_DES_OFB64      12
#define SRP_DES3_ECB       13
#define SRP_DES3_CBC       14
#define SRP_DES3_CFB64     15
#define SRP_DES3_OFB64     16
static struct keytab ciphertab[] = {
    { "blowfish_ecb",   SRP_BLOWFISH_ECB,   0 },
    { "blowfish_cbc",   SRP_BLOWFISH_CBC,   0 },
    { "blowfish_cfb64", SRP_BLOWFISH_CFB64, 0 },
    { "blowfish_ofb64", SRP_BLOWFISH_OFB64, 0 },
    { "cast5_ecb",      SRP_CAST5_ECB,      0 },
    { "cast5_cbc",      SRP_CAST5_CBC,      0 },
    { "cast5_cfb64",    SRP_CAST5_CFB64,    0 },
    { "cast5_ofb64",    SRP_CAST5_OFB64,    0 },
    { "des_ecb",        SRP_DES_ECB,        0 },
    { "des_cbc",        SRP_DES_CBC,        0 },
    { "des_cfb64",      SRP_DES_CFB64,      0 },
    { "des_ofb64",      SRP_DES_OFB64,      0 },
    { "des3_ecb",       SRP_DES3_ECB,       0 },
    { "des3_cbc",       SRP_DES3_CBC,       0 },
    { "des3_cfb64",     SRP_DES3_CFB64,     0 },
    { "des3_ofb64",     SRP_DES3_OFB64,     0 },
    { "none",           0, 0 },
    { "", 0, 0 }
};
static int nciphertab = sizeof(ciphertab) / sizeof(struct keytab) - 1;
#define SRP_MD5  1
#define SRP_SHA  2
static struct keytab hashtab[] = {
    { "md5",              SRP_MD5,        0 },
    { "none",             0,              0 },
    { "sha",              SRP_SHA,        0 },
    { "", 0, 0 }
};
static int nhashtab = sizeof(hashtab) / sizeof(struct keytab) - 1;
#endif /* NOICP */
#endif /* FTP_SECURITY */
static char *
strval(s1,s2) char * s1, * s2; {
    if (!s1) s1 = "";
    if (!s2) s2 = "";
    return(*s1 ? s1 : (*s2 ? s2 : "(none)"));
}
#ifndef NOCSETS
static char * rfnptr = NULL;
static int rfnlen = 0;
static char rfnbuf[RFNBUFSIZ];          /* Remote filename translate buffer */
static char * xgnbp = NULL;
static int
strgetc() {                             /* Helper function for xgnbyte() */
    int c;
    if (!xgnbp)
      return(-1);
    if (!*xgnbp)
      return(-1);
    c = (unsigned) *xgnbp++;
    return(((unsigned) c) & 0xff);
}
static int                              /* Helper function for xpnbyte() */
#ifdef CK_ANSIC
strputc(char c)
#else
strputc(c) char c;
#endif /* CK_ANSIC */
{
    rfnlen = rfnptr - rfnbuf;
    if (rfnlen >= (RFNBUFSIZ - 1))
      return(-1);
    *rfnptr++ = c;
    *rfnptr = NUL;
    return(0);
}
static int
#ifdef CK_ANSIC
xprintc(char c)
#else
xprintc(c) char c;
#endif /* CK_ANSIC */
{
    printf("%c",c);
    return(0);
}
static VOID
bytswap(c0,c1) int * c0, * c1; {
    int t;
    t = *c0;
    *c0 = *c1;
    *c1 = t;
}
#endif /* NOCSETS */
#ifdef CKLOGDIAL
char ftplogbuf[CXLOGBUFL] = { NUL, NUL }; /* Connection Log */
int ftplogactive = 0;
long ftplogprev = 0L;
VOID
ftplogend() {
    extern int dialog;
    extern char diafil[];
    long d1, d2, t1, t2;
    char buf[32], * p;
    debug(F111,"ftp cx log active",ckitoa(dialog),ftplogactive);
    debug(F110,"ftp cx log buf",ftplogbuf,0);
    if (!ftplogactive || !ftplogbuf[0]) /* No active record */
      return;
    ftplogactive = 0;                   /* Record is not active */
    d1 = mjd((char *)ftplogbuf);        /* Get start date of this session */
    ckstrncpy(buf,ckdate(),31);         /* Get current date */
    d2 = mjd(buf);                      /* Convert them to mjds */
    p = ftplogbuf;                      /* Get start time */
    p[11] = NUL;
    p[14] = NUL;                        /* Convert to seconds */
    t1 = atol(p+9) * 3600L + atol(p+12) * 60L + atol(p+15);
    p[11] = ':';
    p[14] = ':';
    p = buf;                            /* Get end time */
    p[11] = NUL;
    p[14] = NUL;
    t2 = atol(p+9) * 3600L + atol(p+12) * 60L + atol(p+15);
    t2 = ((d2 - d1) * 86400L) + (t2 - t1); /* Compute elapsed time */
    if (t2 > -1L) {
        ftplogprev = t2;
        p = hhmmss(t2);
        ckstrncat(ftplogbuf,"E=",CXLOGBUFL); /* Append to log record */
        ckstrncat(ftplogbuf,p,CXLOGBUFL);
    } else
      ftplogprev = 0L;
    debug(F101,"ftp cx log dialog","",dialog);
    if (dialog) {                       /* If logging */
        int x;
        x = diaopn(diafil,1,1);         /* Open log in append mode */
        if (x > 0) {
            debug(F101,"ftp cx log open","",x);
            x = zsoutl(ZDIFIL,ftplogbuf); /* Write the record */
            debug(F101,"ftp cx log write","",x);
            x = zclose(ZDIFIL);         /* Close the log */
            debug(F101,"ftp cx log close","",x);
        }
    }
}
VOID
dologftp() {
    ftplogend();                        /* Previous session not closed out? */
    ftplogprev = 0L;
    ftplogactive = 1;                   /* Record is active */
    ckmakxmsg(ftplogbuf,CXLOGBUFL,
              ckdate()," ",strval(ftp_logname,NULL)," ",ckgetpid(),
              " T=FTP N=", strval(ftp_host,NULL)," H=",myhost,
              " P=", ckitoa(ftp_port)," "); /* SMS 2007/02/15 */
    debug(F110,"ftp cx log begin",ftplogbuf,0);
}
#endif /* CKLOGDIAL */
static char * dummy[2] = { NULL, NULL };
static struct keytab modetab[] = {
    { "active",  0, 0 },
    { "passive", 1, 0 }
};
#ifndef NOCMDL
int                                     /* Called from ckuusy.c */
#ifdef CK_ANSIC
doftparg(char c)
#else
doftparg(c) char c;
#endif /* CK_ANSIC */
/* doftparg */ {
    int x, z;
    char *xp;
    extern char **xargv, *xarg0;
    extern int xargc, stayflg, haveftpuid;
    extern char uidbuf[];
    xp = *xargv+1;                      /* Pointer for bundled args */
    while (c) {
        if (ckstrchr("MuDPkcHzm",c)) {  /* Options that take arguments */
            if (*(xp+1)) {
                fatal("?Invalid argument bundling");
            }
            xargv++, xargc--;
            if ((xargc < 1) || (**xargv == '-')) {
                fatal("?Required argument missing");
            }
        }
        switch (c) {                    /* Big switch on arg */
          case 'h':                     /* help */
           printf("C-Kermit's FTP client command-line personality.  Usage:\n");
            printf("  %s [ options ] host [ port ] [-pg files ]\n\n",xarg0);
            printf("Options:\n");
            printf("  -h           = help (this message)\n");
            printf("  -m mode      = \"passive\" (default) or \"active\"\n");
            printf("  -u name      = username for autologin (or -M)\n");
            printf("  -P password  = password for autologin (RISKY)\n");
            printf("  -A           = autologin anonymously\n");
            printf("  -D directory = cd after autologin\n");
            printf("  -b           = force binary mode\n");
            printf("  -a           = force text (\"ascii\") mode (or -T)\n");
            printf("  -d           = debug (double to add timestamps)\n");
            printf("  -n           = no autologin\n");
            printf("  -v           = verbose (default)\n");
            printf("  -q           = quiet\n");
            printf("  -S           = Stay (issue command prompt when done)\n");
            printf("  -Y           = do not execute Kermit init file\n");
            printf("  -p files     = files to put after autologin (or -s)\n");
            printf("  -g files     = files to get after autologin\n");
            printf("  -R           = recursive (for use with -p)\n");
#ifdef FTP_SECURITY
            printf("\nSecurity options:\n");
            printf("  -k realm     = Kerberos 4 realm\n");
            printf("  -f           = Kerboros 5 credentials forwarding\n");
            printf("  -x           = autoencryption mode\n");
            printf("  -c cipher    = SRP cipher type\n");
            printf("  -H hash      = SRP encryption hash\n");
            printf("  -z option    = Security options\n");
#endif /* FTP_SECURITY */
            printf("\n-p or -g, if given, should be last.  Example:\n");
            printf("  ftp -A kermit.columbia.edu -D kermit -ag TESTFILE\n");
            doexit(GOOD_EXIT,-1);
            break;
          case 'R':                     /* Recursive */
            recursive = 1;
            break;
          case 'd':                     /* Debug */
#ifdef DEBUG
            if (deblog) {
                extern int debtim;
                debtim = 1;
            } else {
                deblog = debopn("debug.log",0);
                debok = 1;
            }
#endif /* DEBUG */
            /* fall thru on purpose */
          case 't':                     /* Trace */
            ftp_deb++;
            break;
          case 'n':                     /* No autologin */
            ftp_log = 0;
            break;
          case 'i':                     /* No prompt */
          case 'v':                     /* Verbose */
            break;                      /* (ignored) */
          case 'q':                     /* Quiet */
            quiet = 1;
            break;
          case 'S':                     /* Stay */
            stayflg = 1;
            break;
          case 'M':
          case 'u':                     /* My User Name */
            if ((int)strlen(*xargv) > 63) {
                fatal("username too long");
            }
            ckstrncpy(uidbuf,*xargv,UIDBUFLEN);
            haveftpuid = 1;
            break;
          case 'A':
            ckstrncpy(uidbuf,"anonymous",UIDBUFLEN);
            haveftpuid = 1;
            break;
          case 'T':                     /* Text */
          case 'a':                     /* "ascii" */
          case 'b':                     /* Binary */
            binary = (c == 'b') ? FTT_BIN : FTT_ASC;
            ftp_xfermode = XMODE_M;
            filepeek = 0;
            patterns = 0;
            break;
          case 'g':                     /* Get */
          case 'p':                     /* Put */
          case 's': {                   /* Send (= Put) */
              int havefiles, rc;
              if (ftp_action) {
                  fatal("Only one FTP action at a time please");
              }
              if (*(xp+1)) {
                  fatal("invalid argument bundling after -s");
              }
              nfils = 0;                /* Initialize file counter */
              havefiles = 0;            /* Assume nothing to send  */
              cmlist = xargv + 1;       /* Remember this pointer */
              while (++xargv, --xargc > 0) { /* Traverse the list */
                  if (c == 'g') {
                      havefiles++;
                      nfils++;
                      continue;
                  }
#ifdef RECURSIVE
                  if (!strcmp(*xargv,".")) {
                      havefiles = 1;
                      nfils++;
                      recursive = 1;
                  } else
#endif /* RECURSIVE */
                    if ((rc = zchki(*xargv)) > -1 || (rc == -2)) {
                        if  (rc != -2)
                          havefiles = 1;
                        nfils++;
                    } else if (iswild(*xargv) && nzxpand(*xargv,0) > 0) {
                        havefiles = 1;
                        nfils++;
                    }
              }
              xargc++, xargv--;         /* Adjust argv/argc */
              if (!havefiles) {
                  if (c == 'g') {
                      fatal("No files to put");
                  } else {
                      fatal("No files to get");
                  }
              }
              ftp_action = c;
              break;
          }
          case 'D':                     /* Directory */
            makestr(&ftp_rdir,*xargv);
            break;
          case 'm':                     /* Mode (Active/Passive */
            ftp_psv = lookup(modetab,*xargv,2,NULL);
            if (ftp_psv < 0) fatal("Invalid mode");
            break;
          case 'P':
            makestr(&ftp_tmp,*xargv);   /* You-Know-What */
            break;
          case 'Y':                     /* No initialization file */
            break;                      /* (already done in prescan) */
#ifdef CK_URL
          case 'U': {                   /* URL */
              /* These are set by urlparse() - any not set are NULL */
              if (g_url.hos) {
/*
  Kermit has accepted host:port notation since many years before URLs were
  invented.  Unfortunately, URLs conflict with this notation.  Thus "ftp
  host:449" looks like a URL and results in service = host and host = 449.
  Here we try to catch this situation transparently to the user.
*/
                  if (ckstrcmp(g_url.svc,"ftp",-1,0)
#ifdef CK_SSL
                       && ckstrcmp(g_url.svc,"ftps",-1,0)
#endif /* CK_SSL */
                       ) {
                      if (!g_url.usr &&
                          !g_url.psw &&
                          !g_url.por &&
                          !g_url.pth) {
                          g_url.por = g_url.hos;
                          g_url.hos = g_url.svc;
                          g_url.svc = "ftp";
                      } else {
                          ckmakmsg(tmpbuf,TMPBUFSIZ,"Non-FTP URL: service=",
                                   g_url.svc," host=",g_url.hos);
                          fatal(tmpbuf);
                      }
                  }
                  makestr(&ftp_host,g_url.hos);
                  if (g_url.usr) {
                      haveftpuid = 1;
                      ckstrncpy(uidbuf,g_url.usr,UIDBUFLEN);
                      makestr(&ftp_logname,uidbuf);
                  }
                  if (g_url.psw) {
                      makestr(&ftp_tmp,g_url.psw);
                  }
                  if (g_url.pth) {
                      if (!g_url.usr) {
                          haveftpuid = 1;
                          ckstrncpy(uidbuf,"anonymous",UIDBUFLEN);
                          makestr(&ftp_logname,uidbuf);
                      }
                      if (ftp_action) {
                          fatal("Only one FTP action at a time please");
                      }
                      if (!stayflg)
                        quiet = 1;
                      nfils = 1;
                      dummy[0] = g_url.pth;
                      cmlist = dummy;
                      ftp_action = 'g';
                  }
                  xp = NULL;
                  haveurl = 1;
              }
              break;
          }
#endif /* CK_URL */
#ifdef FTP_SECURITY
          case 'k': {                   /* K4 Realm */
#ifdef FTP_KRB4
              ckstrncpy(ftp_realm,*xargv, REALM_SZ);
#endif /* FTP_KRB4 */
              if (ftp_deb) printf("K4 Realm = [%s]\n",*xargv);
              break;
          }
          case 'f': {
#ifdef FTP_GSSAPI
              ftp_cfw = 1;
              if (ftp_deb) printf("K5 Credentials Forwarding\n");
#else /* FTP_GSSAPI */
              printf("K5 Credentials Forwarding not supported\n");
#endif /* FTP_GSSAPI */
              break;
          }
          case 'x': {
              ftp_cry = 1;
              if (ftp_deb) printf("Autoencryption\n");
              break;
          }
          case 'c': {                   /* Cipher */
#ifdef FTP_SRP
              if (!srp_selcipher(*xargv)) {
                  if (ftp_deb) printf("SRP cipher type: \"%s\"\n",*xargv);
              } else
                printf("?Invalid SRP cipher type: \"%s\"\n",*xargv);
#else /* FTP_SRP */
              printf("?SRP not supported\n");
#endif /* FTP_SRP */
              break;
          }
          case 'H': {
#ifdef FTP_SRP
              if (!srp_selhash(*xargv)) {
                  if (ftp_deb) printf("SRP hash type: \"%s\"\n",*xargv);
              } else
                printf("?Invalid SRP hash type: \"%s\"\n",*xargv);
#else /* FTP_SRP */
              printf("?SRP not supported\n");
#endif /* FTP_SRP */
              break;
          }
          case 'z': {
              /* *xargv contains a value of the form tag=value */
              /* we need to lookup the tag and save the value  */
              char * p = NULL, * q = NULL;
              makestr(&p,*xargv);
              y = ckindex("=",p,0,0,1);
              if (y > 0)
                p[y-1] = '\0';
              x = lookup(ftpztab,p,nftpztab,&z);
              if (x < 0) {
                  printf("?Invalid security option: \"%s\"\n",p);
              } else {
                  if (ftp_deb)
		    printf("Security option: \"%s",p);
                  if (ftpztab[z].flgs & CM_ARG) {
                      if (y <= 0)
                        fatal("?Missing required value");
                      q = &p[y];
                      if (!*q)
                        fatal("?Missing required value");
                      if (ftp_deb)
			printf("=%s\"",q);
                  }
                  switch (ftpztab[z].kwval) { /* -z options w/args */
                    case FT_NOGSS:
#ifdef FTP_GSSAPI
                      for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
                          if (ftp_auth_type[z] == FTA_GK5) {
                              for (y = z;
                                   y < (FTPATYPS-1) && ftp_auth_type[y];
                                   y++
                                   )
                                ftp_auth_type[y] = ftp_auth_type[y+1];
                              ftp_auth_type[FTPATYPS-1] = 0;
                              break;
                          }
                      }
#endif /* FTP_GSSAPI */
                      break;
                    case FT_NOK4:
#ifdef FTP_KRB4
                      for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
                          if (ftp_auth_type[z] == FTA_K4) {
                              for (y = z;
                                   y < (FTPATYPS-1) && ftp_auth_type[y];
                                   y++
                                   )
                                ftp_auth_type[y] = ftp_auth_type[y+1];
                              ftp_auth_type[FTPATYPS-1] = 0;
                              break;
                          }
                      }
#endif /* FTP_KRB4 */
                      break;
                    case FT_NOSRP:
#ifdef FTP_SRP
                      for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
                          if (ftp_auth_type[z] == FTA_SRP) {
                              for (y = z;
                                   y < (FTPATYPS-1) && ftp_auth_type[y];
                                   y++
                                   )
                                ftp_auth_type[y] = ftp_auth_type[y+1];
                              ftp_auth_type[FTPATYPS-1] = 0;
                              break;
                          }
                      }
#endif /* FTP_SRP */
                      break;
                    case FT_NOSSL:
#ifdef CK_SSL
                      for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
                          if (ftp_auth_type[z] == FTA_SSL) {
                              for (y = z;
                                   y < (FTPATYPS-1) && ftp_auth_type[y];
                                   y++
                                   )
                                ftp_auth_type[y] = ftp_auth_type[y+1];
                              ftp_auth_type[FTPATYPS-1] = 0;
                              break;
                          }
                      }
#endif /* CK_SSL */
                      break;
                    case FT_NOTLS:
#ifdef CK_SSL
                      for (z = 0; z < FTPATYPS && ftp_auth_type[z]; z++) {
                          if (ftp_auth_type[z] == FTA_TLS) {
                              for (y = z;
                                   y < (FTPATYPS-1) && ftp_auth_type[y];
                                   y++
                                   )
                                ftp_auth_type[y] = ftp_auth_type[y+1];
                              ftp_auth_type[FTPATYPS-1] = 0;
                              break;
                          }
                      }
#endif /* CK_SSL */
                      break;
                    case FT_CERTFI:
#ifdef CK_SSL
                      makestr(&ssl_rsa_cert_file,q);
#endif /* CK_SSL */
                      break;
                    case FT_OKCERT:
#ifdef CK_SSL
                      ssl_certsok_flag = 1;
#endif /* CK_SSL */
                      break;
                    case FT_DEBUG:
#ifdef DEBUG
                      if (deblog) {
                          extern int debtim;
                          debtim = 1;
                      } else {
                          deblog = debopn("debug.log",0);
                      }
#endif /* DEBUG */
                      break;
                    case FT_KEY:
#ifdef CK_SSL
                      makestr(&ssl_rsa_key_file,q);
#endif /* CK_SSL */
                      break;
                    case FT_SECURE:
                      /* no equivalent */
                      break;
                    case FT_VERIFY:
#ifdef CK_SSL
                      if (!rdigits(q))
                        printf("?Bad number: %s\n",q);
                      ssl_verify_flag = atoi(q);
#endif /* CK_SSL */
                      break;
                  }
              }
              if (ftp_deb) printf("\"\n");
              free(p);
              break;
          }
#endif /* FTP_SECURITY */
          default:
            fatal2(*xargv,
                   "unknown command-line option, type \"ftp -h\" for help"
                   );
        }
        if (!xp) break;
        c = *++xp;                      /* See if options are bundled */
    }
    return(0);
}
#endif /* NOCMDL */
int
ftpisconnected() {
    return(connected);
}
int
ftpisloggedin() {
    return(connected ? loggedin : 0);
}
int
ftpissecure() {
    return((ftp_dpl == FPL_CLR && !ssl_ftp_proxy) ? 0 : 1);
}
static VOID
#ifdef CK_ANSIC
ftscreen(int n, char c, CK_OFF_T z, char *s)
#else
ftscreen(n, c, z, s) int n; char c; CK_OFF_T z; char * s;
#endif  /* CK_ANSIC */
{
    if (displa && fdispla && !backgrd && !quiet && !out2screen) {
        if (!dpyactive) {
            ckscreen(SCR_PT,'S',(CK_OFF_T)0,"");
            dpyactive = 1;
        }
        ckscreen(n,c,z,s);
    }
}
#ifndef OS2
/*  g m s t i m e r  --  Millisecond timer */
long
gmstimer() {
#ifdef HAVE_MSECS
    /* For those versions of ztime() that also set global ztmsec. */
    char *p = NULL;
    long z;
    ztime(&p);
    if (!p) return(0L);
    if (!*p) return(0L);
    z = atol(p+11) * 3600L + atol(p+14) * 60L + atol(p+17);
    return(z * 1000 + ztmsec);
#else
    return((long)time(NULL) * 1000L);
#endif /* HAVE_MSECS */
}
#endif /* OS2 */
/*  d o s e t f t p  --  The SET FTP command  */
int
dosetftp() {
    int cx;
    if ((cx = cmkey(ftpset,nftpset,"","",xxstring)) < 0) /* Set what? */
      return(cx);
    switch (cx) {
      case FTS_FNC:                     /* Filename collision action */
        if ((x = cmkey(ftpcolxtab,nftpcolx,"","",xxstring)) < 0)
          return(x);
        if ((y = cmcfm()) < 0)
          return(y);
        ftp_fnc = x;
        return(1);
      case FTS_CNV:                     /* Filename conversion */
        if ((x = cmkey(fntab,nfntab,"","automatic",xxstring)) < 0)
          return(x);
        if ((y = cmcfm()) < 0)
          return(y);
        ftp_cnv = x;
        return(1);
      case FTS_DBG:                     /* Debug messages */
        return(seton(&ftp_deb));
      case FTS_LOG:                     /* Auto-login */
        return(seton(&ftp_log));
      case FTS_PSV:                     /* Passive mode */
	return(dosetftppsv());
      case FTS_SPC:                     /* Send port commands */
        x = seton(&ftp_spc);
        if (x > 0) sendport = ftp_spc;
        return(x);
      case FTS_TYP:                     /* Type */
        if ((x = cmkey(ftptyp,nftptyp,"","",xxstring)) < 0)
          return(x);
        if ((y = cmcfm()) < 0) return(y);
	ftp_typ = x;
	g_ftp_typ = x;
	tenex = (ftp_typ == FTT_TEN);
        return(1);
      case FTS_USN:                     /* Unique server names */
        return(seton(&ftp_usn));
      case FTS_VBM:                     /* Verbose mode */
        if ((x = seton(&ftp_vbm)) < 0)  /* Per-command copy */
          return(x);
        ftp_vbx = ftp_vbm;              /* Global sticky copy */
        return(x);
      case FTS_TST:                     /* "if (testing)" messages */
        return(seton(&testing));
      case FTS_PRM:                     /* Send permissions */
        return(setonaut(&ftp_prm));
      case FTS_AUT:                     /* Auto-authentication */
        return(seton(&ftp_aut));
      case FTS_ERR:                     /* Error action */
        if ((x = cmkey(qorp,2,"","",xxstring)) < 0)
          return(x);
        if ((y = cmcfm()) < 0)
          return(y);
        ftp_err = x;
        return(success = 1);
#ifndef NOCSETS
      case FTS_XLA:                     /* Translation */
        return(seton(&ftp_xla));
      case FTS_CSR:                     /* Server charset */
        if ((x = cmkey(fcstab,nfilc,"character-set","utf8",xxstring)) < 0)
          return(x);
        if ((y = cmcfm()) < 0)
          return(y);
        ftp_csr = x;
        ftp_xla = 1;                    /* Also enable translation */
        return(success = 1);
#endif /* NOCSETS */
      case FTS_GFT:
        return(seton(&get_auto));       /* GET-filetype-switching */
      case FTS_DAT:
        return(seton(&ftp_dates));      /* Set file dates */
#ifdef FTP_TIMEOUT
      case FTS_TMO:			/* Timeout */
        if ((x = cmnum("Number of seconds","0",10,&z,xxstring)) < 0)
          return(x);
        if ((y = cmcfm()) < 0)
          return(y);
	ftp_timeout = z;
	return(success = 1);
#endif	/* FTP_TIMEOUT */
      case FTS_STO: {			/* Server time offset */
	  char * s, * p = NULL;
	  long k;
	  if ((x = cmfld("[+-]hh[:mm[:ss]]","+0",&s,xxstring)) < 0)
	    return(x);
	  if (!strcmp(s,"+0")) {
	      s = NULL;
	  } else if ((x = delta2sec(s,&k)) < 0) { /* Check format */
	      printf("?Invalid time offset\n");
	      return(-9);
	  }
	  makestr(&p,s);		/* Make a safe copy the string */
	  if ((x = cmcfm()) < 0) {	/* Get confirmation */
	      if (p)
		makestr(&p,NULL);
	      return(x);
	  }
	  fts_sto = p;			/* Confirmed - set the string. */
	  return(success = 1);
      }
      case FTS_APW: {
	  char * s;
	  if ((x = cmtxt("Text", "", &s, xxstring)) < 0)
	    return(x);
	  makestr(&ftp_apw, *s ? s : NULL);
	  return(success = 1);
      }
      case FTS_BUG: {
          if ((x = cmkey(ftpbugtab,nftpbug,"","",xxstring)) < 0) 
	    return(x);
          switch (x) {
#ifdef CK_SSL
          case FTB_SV2:
	    return seton(&ftp_bug_use_ssl_v2);
          case FTB_SV3:
            return seton(&ftp_bug_use_ssl_v3);
#endif /* CK_SSL */
          default:
	    return(-2);
          }
      }
#ifdef FTP_SECURITY
      case FTS_CRY:                     /* Auto-encryption */
        return(seton(&ftp_cry));
      case FTS_CFW:                     /* Credential-forwarding */
        return(seton(&ftp_cfw));
      case FTS_CPL:                     /* Command protection level */
        if ((x = cmkey(ftppro,nftppro,"","",xxstring)) < 0) return(x);
        if ((y = cmcfm()) < 0) return(y);
        success = fts_cpl(x);
        return(success);
      case FTS_DPL:                     /* Data protection level */
        if ((x = cmkey(ftppro,nftppro,"","",xxstring)) < 0) return(x);
        if ((y = cmcfm()) < 0) return(y);
          success = fts_dpl(x);
          return(success);
      case FTS_ATP: {                   /* FTP Auth Type */
          int i, j, atypes[8];
          for (i = 0; i < 8; i++) {
              if ((y = cmkey(ftpauth,nftpauth,"",
                             (i == 0) ? "automatic" : "",
                             xxstring)) < 0) {
                  if (y == -3)
                    break;
                  return(y);
              }
              if (i > 0 && (y == FTA_AUTO)) {
                  printf("?Choice may only be used in first position.\r\n");
                  return(-9);
              }
              for (j = 0; j < i; j++) {
                  if (atypes[j] == y) {
                      printf("\r\n?Choice has already been used.\r\n");
                      return(-9);
                  }
              }
              atypes[i] = y;
              if (y == FTA_AUTO) {
                  i++;
                  break;
              }
          }
          if (i < 8)
            atypes[i] = 0;
          if ((z = cmcfm()) < 0)
            return(z);
          if (atypes[0] == FTA_AUTO) {
              i = 0;
#ifdef FTP_GSSAPI
              ftp_auth_type[i++] = FTA_GK5;
#endif /* FTP_GSSAPI */
#ifdef FTP_SRP
              ftp_auth_type[i++] = FTA_SRP;
#endif /* FTP_SRP */
#ifdef FTP_KRB4
              ftp_auth_type[i++] = FTA_K4;
#endif /* FTP_KRB4 */
#ifdef CK_SSL
              ftp_auth_type[i++] = FTA_TLS;
              ftp_auth_type[i++] = FTA_SSL;
#endif /* CK_SSL */
              ftp_auth_type[i] = 0;
          } else {
              for (i = 0; i < 8; i++)
                ftp_auth_type[i] = atypes[i];
          }
          return(success = 1);
      }
      case FTS_SRP:
#ifdef FTP_SRP
        if ((x = cmkey(ftpsrp,nftpsrp,"","",xxstring)) < 0)
          return(x);
        switch (x) {
          case SRP_CIPHER:
            if ((x = cmkey(ciphertab,nciphertab,"","",xxstring)) < 0)
              return(x);
            if ((z = cmcfm()) < 0)
              return(z);
            success = !srp_selcipher(ciphertab[x].kwd);
            return(success);
          case SRP_HASH:
            if ((x = cmkey(hashtab,nhashtab,"","",xxstring)) < 0)
              return(x);
            if ((z = cmcfm()) < 0)
              return(z);
            success = !srp_selhash(hashtab[x].kwd);
            return(success = 1);
          default:
            if ((z = cmcfm()) < 0)
              return(z);
            return(-2);
        }
#else /* FTP_SRP */
        if ((z = cmcfm()) < 0)
          return(z);
        return(-2);
#endif /* FTP_SRP */
#endif /* FTP_SECURITY */
      case FTS_DIS:
	doxdis(2);			/* 2 == ftp */
        return(success = 1);
      default:
        return(-2);
    }
}
int
ftpbye() {
    int x;
    if (!connected)
      return(1);
    if (testing)
      printf(" ftp closing %s...\n",ftp_host);
    x = ftpclose();
    return((x > -1) ? 1 : 0);
}
/*  o p e n f t p  --  Parse FTP hostname & port and open */
static int
openftp(s,opn_tls) char * s; int opn_tls; {
    char c, * p, * hostname = NULL, *hostsave = NULL, * service = NULL;
    int i, n, havehost = 0, getval = 0, rc = -9, opn_psv = -1, nologin = 0;
    int haveuser = 0;
    struct FDB sw, fl, cm;
    extern int nnetdir;                 /* Network services directory */
    extern int nhcount;                 /* Lookup result */
    extern char *nh_p[];                /* Network directory entry pointers */
    extern char *nh_p2[];               /* Network directory entry nettype */
    if (!s) return(-2);
    if (!*s) return(-2);
    makestr(&hostname,s);
    hostsave = hostname;
    makestr(&ftp_logname,NULL);
    anonymous = 0;
    noinit = 0;
    debug(F110,"ftp open",hostname,0);
    if (sav_psv > -1) {                 /* Restore prevailing active/passive */
        ftp_psv = sav_psv;              /* selection in case it was */
        sav_psv = -1;                   /* temporarily overriden by a switch */
    }
    if (sav_log > -1) {                 /* Ditto for autologin */
        ftp_log = sav_log;
        sav_log = -1;
    }
    cmfdbi(&sw,                         /* Switches */
           _CMKEY,
           "Service name or port;\n or switch",
           "",                          /* default */
           "",                          /* addtl string data */
           nftpswi,                     /* addtl numeric data 1: tbl size */
           4,                           /* addtl numeric data 2: none */
           xxstring,                    /* Processing function */
           ftpswitab,                   /* Keyword table */
           &fl                          /* Pointer to next FDB */
           );
    cmfdbi(&fl,                         /* A host name or address */
           _CMFLD,                      /* fcode */
           "",                          /* help */
           "xYzBoo",                    /* default */
           "",                          /* addtl string data */
           0,                           /* addtl numeric data 1 */
           0,                           /* addtl numeric data 2 */
           xxstring,
           NULL,
           &cm
           );
    cmfdbi(&cm,                         /* Command confirmation */
           _CMCFM,
           "",
           "",
           "",
           0,
           0,
           NULL,
           NULL,
           NULL
           );
    for (n = 0;; n++) {
        rc = cmfdb(&sw);                /* Parse a service name or a switch */
        if (rc < 0)
          goto xopenftp;
        if (cmresult.fcode == _CMCFM) { /* Done? */
            break;
        } else if (cmresult.fcode == _CMFLD) {  /* Port */
            if (ckstrcmp("xYzBoo",cmresult.sresult,-1,1))
              makestr(&service,cmresult.sresult);
            else
              makestr(&service,opn_tls?"ftps":"ftp");
        } else if (cmresult.fcode == _CMKEY) { /* Have a switch */
            c = cmgbrk();               /* get break character */
            getval = (c == ':' || c == '=');
            rc = -9;
            if (getval && !(cmresult.kflags & CM_ARG)) {
                printf("?This switch does not take arguments\n");
                goto xopenftp;
            }
            if (!getval && (cmresult.kflags & CM_ARG)) {
                printf("?This switch requires an argument\n");
                goto xopenftp;
            }
            switch (cmresult.nresult) { /* Switch */
              case OPN_ANO:             /* /ANONYMOUS */
                anonymous++;
		nologin = 0;
                break;
              case OPN_NIN:             /* /NOINIT */
                noinit++;
                break;
              case OPN_NOL:             /* /NOLOGIN */
                nologin++;
		anonymous = 0;
		makestr(&ftp_logname,NULL);
                break;
              case OPN_PSW:             /* /PASSWORD */
                if (!anonymous)         /* Don't log real passwords */
                  debok = 0;
                rc = cmfld("Password for FTP server","",&p,xxstring);
                if (rc == -3) {
                    makestr(&ftp_tmp,NULL);
                } else if (rc < 0) {
                    goto xopenftp;
                } else {
                    makestr(&ftp_tmp,brstrip(p));
		    nologin = 0;
                }
                break;
              case OPN_USR:             /* /USER */
                rc = cmfld("Username for FTP server","",&p,xxstring);
                if (rc == -3) {
                    makestr(&ftp_logname,NULL);
                } else if (rc < 0) {
                    goto xopenftp;
                } else {
		    nologin = 0;
                    anonymous = 0;
		    haveuser = 1;
                    makestr(&ftp_logname,brstrip(p));
                }
                break;
              case OPN_ACC:
                rc = cmfld("Account for FTP server","",&p,xxstring);
                if (rc == -3) {
                    makestr(&ftp_acc,NULL);
                } else if (rc < 0) {
                    goto xopenftp;
                } else {
                    makestr(&ftp_acc,brstrip(p));
                }
                break;
              case OPN_ACT:
                opn_psv = 0;
                break;
              case OPN_PSV:
                opn_psv = 1;
                break;
              case OPN_TLS:
                opn_tls = 1;
                break;
              default:
                break;
            }
        }
        if (n == 0) {                   /* After first time through */
            cmfdbi(&sw,                 /* accept only switches */
                   _CMKEY,
                   "\nCarriage return to confirm to command, or switch",
                   "",
                   "",
                   nftpswi,
                   4,
                   xxstring,
                   ftpswitab,
                   &cm
                   );
        }
    }
#ifdef COMMENT
    debug(F100,"ftp openftp while exit","",0);
    rc = cmcfm();
    debug(F101,"ftp openftp cmcfm rc","",rc);
    if (rc < 0)
      goto xopenftp;
#endif /* COMMENT */
    if (opn_psv > -1) {                 /* /PASSIVE or /ACTIVE switch given */
        sav_psv = ftp_psv;
        ftp_psv = opn_psv;
    }
    if (nologin || haveuser) {		/* /NOLOGIN or /USER switch given */
	sav_log = ftp_log;
	ftp_log = haveuser ? 1 : 0;
    }
    if (*hostname == '=') {             /* Bypass directory lookup */
        hostname++;                     /* if hostname starts with '=' */
        havehost++;
    } else if (isdigit(*hostname)) {    /* or if it starts with a digit */
        havehost++;
    }
    if (!service)
      makestr(&service,opn_tls?"ftps":"ftp");
#ifndef NODIAL
    if (!havehost && nnetdir > 0) {     /* If there is a networks directory */
        lunet(hostname);                /* Look up the name */
        debug(F111,"ftp openftp lunet",hostname,nhcount);
        if (nhcount == 0) {
            if (testing)
              printf(" ftp open trying \"%s %s\"...\n",hostname,service);
            success = ftpopen(hostname,service,opn_tls);
            debug(F101,"ftp openftp A ftpopen success","",success);
            rc = success;
        } else {
            int found = 0;
            for (i = 0; i < nhcount; i++) {
                if (nh_p2[i])           /* If network type specified */
                  if (ckstrcmp(nh_p2[i],"tcp/ip",strlen(nh_p2[i]),0))
                    continue;
                found++;
                makestr(&hostname,nh_p[i]);
                debug(F111,"ftpopen lunet substitution",hostname,i);
                if (testing)
                  printf(" ftp open trying \"%s %s\"...\n",hostname,service);
                success = ftpopen(hostname,service,opn_tls);
                debug(F101,"ftp openftp B ftpopen success","",success);
                rc = success;
                if (success)
                  break;
            }
            if (!found) {               /* E.g. if no network types match */
                if (testing)
                  printf(" ftp open trying \"%s %s\"...\n",hostname,service);
                success = ftpopen(hostname,service,opn_tls);
                debug(F101,"ftp openftp C ftpopen success","",success);
                rc = success;
            }
        }
    } else {
#endif /* NODIAL */
        if (testing)
          printf(" ftp open trying \"%s %s\"...\n",hostname,service);
        success = ftpopen(hostname,service,opn_tls);
        debug(F111,"ftp openftp D ftpopen success",hostname,success);
        debug(F111,"ftp openftp D ftpopen connected",hostname,connected);
        rc = success;
#ifndef NODIAL
    }
#endif /* NODIAL */
  xopenftp:
    debug(F101,"ftp openftp xopenftp rc","",rc);
    if (hostsave) free(hostsave);
    if (service) free(service);
    if (rc < 0 && ftp_logname) {
        free(ftp_logname);
        ftp_logname = NULL;
    }
    if (ftp_tmp) {
        free(ftp_tmp);
        ftp_tmp = NULL;
    }
    return(rc);
}
VOID					/* 12 Aug 2007 */
doftpglobaltype(x) int x; {
    ftp_xfermode = XMODE_M;		/* Set manual FTP transfer mode */
    ftp_typ = x;			/* Used by top-level BINARY and */
    g_ftp_typ = x;			/* ASCII commands. */
    get_auto = 0;
    forcetype = 1;
}
int
doftpacct() {
    int x;
    char * s;
    if ((x = cmtxt("Remote account", "", &s, xxstring)) < 0)
      return(x);
    CHECKCONN();
    makestr(&ftp_acc,brstrip(s));
    if (testing)
      printf(" ftp account: \"%s\"\n",ftp_acc);
    success = (ftpcmd("ACCT",ftp_acc,-1,-1,ftp_vbm) == REPLY_COMPLETE);
    return(success);
}
int
doftpusr() {                            /* Log in as USER */
    extern char uidbuf[];
    extern char pwbuf[];
    extern int  pwflg, pwcrypt;
    int x;
    char *s, * acct = "";
    debok = 0;                          /* Don't log */
    if ((x = cmfld("Remote username or ID",uidbuf,&s,xxstring)) < 0)
      return(x);
    ckstrncpy(line,brstrip(s),LINBUFSIZ); /* brstrip: 15 Jan 2003 */
    if ((x = cmfld("Remote password","",&s,xxstring)) < 0) {
        if (x == -3) { /* no input */
            if ( pwbuf[0] && pwflg ) {
                ckstrncpy(tmpbuf,(char *)pwbuf,TMPBUFSIZ);
#ifdef OS2
                if ( pwcrypt )
                    ck_encrypt((char *)tmpbuf);
#endif /* OS2 */
            }
        } else {
            return(x);
        }
    } else {
        ckstrncpy(tmpbuf,brstrip(s),TMPBUFSIZ);
    }
    if ((x = cmtxt("Remote account\n or Enter or CR to confirm the command",
                   "", &s, xxstring)) < 0)
      return(x);
    CHECKCONN();
    if (*s) {
        x = strlen(tmpbuf);
        if (x > 0) {
            acct = &tmpbuf[x+2];
            ckstrncpy(acct,brstrip(s),TMPBUFSIZ - x - 2);
        }
    }
    if (testing)
      printf(" ftp user \"%s\" password \"%s\"...\n",line,tmpbuf);
    success = ftp_user(line,tmpbuf,acct);
#ifdef CKLOGDIAL
    dologftp();
#endif /* CKLOGDIAL */
    return(success);
}
/* DO (various FTP commands)... */
int
doftptyp(type) int type; {              /* TYPE */
    CHECKCONN();
    ftp_typ = type;
    changetype(ftp_typ,ftp_vbm);
    debug(F101,"doftptyp changed type","",type);
    return(1);
}
static int
doftpxmkd(s,vbm) char * s; int vbm; {   /* MKDIR action */
    int lcs = -1, rcs = -1;
#ifndef NOCSETS
    if (ftp_xla) {
        lcs = ftp_csl;
        if (lcs < 0) lcs = fcharset;
        rcs = ftp_csx;
        if (rcs < 0) rcs = ftp_csr;
    }
#endif /* NOCSETS */
    debug(F110,"ftp doftpmkd",s,0);
    if (ftpcmd("MKD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
      return(success = 1);
    if (ftpcode == 500 || ftpcode == 502) {
        if (!quiet)
          printf("MKD command not recognized, trying XMKD\n");
        if (ftpcmd("XMKD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
          return(success = 1);
    }
    return(success = 0);
}
static int
doftpmkd() {                            /* MKDIR parse */
    int x;
    char * s;
    if ((x = cmtxt("Remote directory name", "", &s, xxstring)) < 0)
      return(x);
    CHECKCONN();
    ckstrncpy(line,s,LINBUFSIZ);
    if (testing)
      printf(" ftp mkdir \"%s\"...\n",line);
    return(success = doftpxmkd(line,-1));
}
static int
doftprmd() {                            /* RMDIR */
    int x, lcs = -1, rcs = -1;
    char * s;
    if ((x = cmtxt("Remote directory", "", &s, xxstring)) < 0)
      return(x);
    CHECKCONN();
    ckstrncpy(line,s,LINBUFSIZ);
    if (testing)
      printf(" ftp rmdir \"%s\"...\n",line);
#ifndef NOCSETS
    if (ftp_xla) {
        lcs = ftp_csl;
        if (lcs < 0) lcs = fcharset;
        rcs = ftp_csx;
        if (rcs < 0) rcs = ftp_csr;
    }
#endif /* NOCSETS */
    if (ftpcmd("RMD",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE)
      return(success = 1);
    if (ftpcode == 500 || ftpcode == 502) {
        if (!quiet)
          printf("RMD command not recognized, trying XMKD\n");
        success = (ftpcmd("XRMD",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
    } else
      success = 0;
    return(success);
}
static int
doftpren() {                            /* RENAME */
    int x;
    char * s;
    if ((x = cmfld("Remote filename","",&s,xxstring)) < 0)
      return(x);
    ckstrncpy(line,s,LINBUFSIZ);
    if ((x = cmfld("New name for remote file","",&s,xxstring)) < 0)
      return(x);
    ckstrncpy(tmpbuf,s,TMPBUFSIZ);
    if ((x = cmcfm()) < 0)
      return(x);
    CHECKCONN();
    if (testing)
      printf(" ftp rename \"%s\" (to) \"%s\"...\n",line,tmpbuf);
    success = ftp_rename(line,tmpbuf);
    return(success);
}
int
doftpres() {                            /* RESET (log out without close) */
    int x;
    if ((x = cmcfm()) < 0)
      return(x);
    CHECKCONN();
    if (testing)
      printf(" ftp reset...\n");
    return(success = ftp_reset());
}
static int
doftpxhlp() {                           /* HELP */
    int x;
    char * s;
    if ((x = cmtxt("Command name", "", &s, xxstring)) < 0)
      return(x);
    CHECKCONN();
    ckstrncpy(line,s,LINBUFSIZ);
    if (testing)
      printf(" ftp help \"%s\"...\n",line);
    /* No need to translate -- all FTP commands are ASCII */
    return(success = (ftpcmd("HELP",line,0,0,1) == REPLY_COMPLETE));
}
static int
doftpdir(cx) int cx; {                  /* [V]DIRECTORY */
    int x, lcs = 0, rcs = 0, xlate = 0;
    char * p, * s, * m = "";
    if (cx == FTP_VDI) {
        switch (servertype) {
          case SYS_VMS:
          case SYS_DOS:
          case SYS_TOPS10:
          case SYS_TOPS20:
            m = "*.*";
            break;
          default:
            m = "*";
        }
    }
    if ((x = cmtxt("Remote filespec",m,&s,xxstring)) < 0)
      return(x);
    if ((x = remtxt(&s)) < 0)
      return(x);
#ifdef NOCSETS
    xlate = 0;
#else
    xlate = ftp_xla;
#endif /* NOCSETS */
    line[0] = NUL;
    ckstrncpy(line,s,LINBUFSIZ);
    s = line;
    CHECKCONN();
#ifndef NOCSETS
    if (xlate) {                        /* SET FTP CHARACTER-SET-TRANSLATION */
        lcs = ftp_csl;                  /* Local charset */
        if (lcs < 0) lcs = fcharset;
        if (lcs < 0) xlate = 0;
    }
    if (xlate) {                        /* Still ON? */
        rcs = ftp_csx;                  /* Remote (Server) charset */
        if (rcs < 0) rcs = ftp_csr;
        if (rcs < 0) xlate = 0;
    }
#endif /* NOCSETS */
    if (testing) {
        p = s;
        if (!p) p = "";
        if (*p)
          printf("Directory of files %s at %s:\n", line, ftp_host);
        else
          printf("Directory of files at %s:\n", ftp_host);
    }
    debug(F111,"doftpdir",s,cx);
    if (cx == FTP_DIR) {
        /* Translation of line[] is done inside recvrequest() */
        /* when it calls ftpcmd(). */
        return(success =
          (recvrequest("LIST","-",s,"wb",0,0,NULL,xlate,lcs,rcs) == 0));
    }
    success = 1;                        /* VDIR - one file at a time... */
    p = (char *)remote_files(1,(CHAR *)s,NULL,0); /* Get the file list */
    cancelgroup = 0;
    if (!ftp_vbm && !quiet)
      printlines = 1;
    while (p && !cancelfile && !cancelgroup) { /* STAT one file */
        if (ftpcmd("STAT",p,lcs,rcs,ftp_vbm) < 0) {
            success = 0;
            break;
        }
        p = (char *)remote_files(0,NULL,NULL,0); /* Get next file */
        debug(F110,"ftp vdir file",s,0);
    }
    return(success);
}
static int
doftppwd() {                            /* PWD */
    int x, lcs = -1, rcs = -1;
#ifndef NOCSETS
    if (ftp_xla) {
        lcs = ftp_csl;
        if (lcs < 0) lcs = fcharset;
        rcs = ftp_csx;
        if (rcs < 0) rcs = ftp_csr;
    }
#endif /* NOCSETS */
    if ((x = cmcfm()) < 0)
      return(x);
    CHECKCONN();
    if (ftpcmd("PWD",NULL,lcs,rcs,1) == REPLY_COMPLETE) {
        success = 1;
    } else if (ftpcode == 500 || ftpcode == 502) {
        if (ftp_deb)
          printf("PWD command not recognized, trying XPWD\n");
        success = (ftpcmd("XPWD",NULL,lcs,rcs,1) == REPLY_COMPLETE);
    }
    return(success);
}
static int
doftpcwd(s,vbm) char * s; int vbm; {    /* CD (CWD) */
    int lcs = -1, rcs = -1;
#ifndef NOCSETS
    if (ftp_xla) {
        lcs = ftp_csl;
        if (lcs < 0) lcs = fcharset;
        rcs = ftp_csx;
        if (rcs < 0) rcs = ftp_csr;
    }
#endif /* NOCSETS */
    debug(F110,"ftp doftpcwd",s,0);
    if (ftpcmd("CWD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
      return(success = 1);
    if (ftpcode == 500 || ftpcode == 502) {
        if (!quiet)
          printf("CWD command not recognized, trying XCWD\n");
        if (ftpcmd("XCWD",s,lcs,rcs,vbm) == REPLY_COMPLETE)
          return(success = 1);
    }
    return(success = 0);
}
static int
doftpcdup() {                           /* CDUP */
    debug(F100,"ftp doftpcdup","",0);
    if (ftpcmd("CDUP",NULL,0,0,1) == REPLY_COMPLETE)
      return(success = 1);
    if (ftpcode == 500 || ftpcode == 502) {
        if (!quiet)
          printf("CDUP command not recognized, trying XCUP\n");
        if (ftpcmd("XCUP",NULL,0,0,1) == REPLY_COMPLETE)
          return(success = 1);
    }
    return(success = 0);
}
/* s y n c d i r  --  Synchronizes client & server directories */
/*
  Call with:
    local = pointer to pathname of local file to be sent.
    sim   = 1 for simulation, 0 for real uploading.
  Returns 0 on failure, 1 on success.
  The 'local' argument is relative to the initial directory of the MPUT,
  i.e. the root of the tree being uploaded.  If the directory of the
  argument file is different from the directory of the previous file
  (which is stored in global putpath[]), this routine does the appropriate
  CWDs, CDUPs, and/or MKDIRs to position the FTP server in the same place.
*/
static int cdlevel = 0, cdsimlvl = 0;	/* Tree-level trackers */
static int
syncdir(local,sim) char * local; int sim; {
    char buf[CKMAXPATH+1];
    char tmp[CKMAXPATH+1];
    char msgbuf[CKMAXPATH+64];
    char c, * p = local, * s = buf, * q = buf, * psep, * ssep;
    int i, k = 0, done = 0, itsadir = 0, saveq;
    debug(F110,"ftp syncdir local (new)",local,0);
    debug(F110,"ftp syncdir putpath (old)",putpath,0);
    itsadir = isdir(local);		/* Is the local file a directory? */
    saveq = quiet;
    while ((*s = *p)) {                 /* Copy the argument filename */
        if (++k == CKMAXPATH)           /* so we can poke it. */
          return(-1);
        if (*s == '/')                  /* Pointer to rightmost dirsep */
          q = s;
        s++;
        p++;
    }
    if (!itsadir)			/* If it's a regular file */
      *q = NUL;                         /* keep just the path part */
    debug(F110,"ftp syncdir buf",buf,0);
    if (!strcmp(buf,putpath)) {         /* Same path as previous file? */
        if (itsadir) {                  /* This file is a directory? */
            if (doftpcwd(local,0)) {    /* Try to CD to it */
                doftpcdup();            /* Worked - CD back up */
            } else if (sim) {           /* Simulating... */
                if (fdispla == XYFD_B) {
                    printf("WOULD CREATE DIRECTORY %s\n",local);
                } else if (fdispla) {
                    ckmakmsg(msgbuf,CKMAXPATH,
                             "WOULD CREATE DIRECTORY",local,NULL,NULL);
                    ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,msgbuf);
                }
                /* See note above */
                return(0);
            } else if (!doftpxmkd(local,0)) { /* Can't CD - try to create */
                return(0);
            } else {			/* Remote directory created OK */
                if (fdispla == XYFD_B) {
                    printf("CREATED DIRECTORY %s\n",local);
                } else if (fdispla) {
                    ckmakmsg(msgbuf,CKMAXPATH+64,
                             "CREATED DIRECTORY ",local,NULL,NULL);
                    ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,msgbuf);
                }
            }
        }
        debug(F110,"ftp syncdir no change",buf,0);
        return(1);                      /* Yes, done. */
    }
    ckstrncpy(tmp,buf,CKMAXPATH+1);     /* Make a safe (pre-poked) copy */
    debug(F110,"ftp syncdir new path",buf,0); /* for later (see end) */
    p = buf;                            /* New */
    s = putpath;                        /* Old */
    debug(F110,"ftp syncdir A (old) s",s,0); /* Previous */
    debug(F110,"ftp syncdir A (new) p",p,0); /* New */
    psep = buf;
    ssep = putpath;
    while (*p != NUL && *s != NUL && *p == *s) {
	if (*p == '/') { psep = p+1; ssep = s+1; }
	p++,s++;
    }
    /*
      psep and ssep point to the first path segment that differs.
      We have to do as many CDUPs as there are path segments in ssep.
      then we have to do as many MKDs and CWDs as there are segments in psep.
    */
    s = ssep;
    p = psep;
    debug(F110,"ftp syncdir B (old) s",s,0); /* Previous */
    debug(F110,"ftp syncdir B (new) p",p,0); /* New */
    /* p and s now point to the leftmost spot where the paths differ */
    if (*s) {                           /* We have to back up */
        k = 1;                          /* How many levels counting this one */
        while ((c = *s++)) {            /* Count dirseps remaining in prev */
            if (c == '/' && *s)
              k++;
        }
	debug(F101,"ftp syncdir levels up","",k);
        for (i = 1; i <= k; i++) {       /* Do that many CDUPs */
            debug(F111,"ftp syncdir CDUP A",p,i);
	    if (fdispla == XYFD_B)
	      printf(" CDUP\n");
            if (sim && cdsimlvl) {
                cdsimlvl--;
            } else {
                if (!doftpcdup()) {
                    quiet = saveq;
                    return(0);
                }
            }
            cdlevel--;
        }
        if (!*p)                        /* If we don't have to go down */
          goto xcwd;                    /* we're done. */
    }
#ifdef COMMENT
    while (p > buf && *p && *p != '/')  /* If in middle of segment */
      p--;                              /* back up to beginning */
    if (*p == '/')                      /* and terminate there */
      p++;
#endif	/* COMMENT */
    debug(F110,"ftp syncdir NEW PATH",p,0);
    s = p;                              /* Point to start of new down path. */
    while (1) {                         /* Loop through characters. */
        if (*s == '/' || !*s) {         /* Have a segment. */
            if (!*s)                    /* If end of string, */
              done++;                   /* after this segment we're done. */
            else
              *s = NUL;                 /* NUL out the separator. */
            if (*p) {                   /* If segment is not empty */
                debug(F110,"ftp syncdir down segment",p,0);
                if (!doftpcwd(p,0)) {   /* Try to CD to it */
                    if (sim) {
                        if (fdispla == XYFD_B) {
                            printf(" WOULD CREATE DIRECTORY %s\n",local);
                        } else if (fdispla) {
                            ckmakmsg(msgbuf,CKMAXPATH,
				     "WOULD CREATE DIRECTORY",
                                     local,NULL,NULL);
                            ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,msgbuf);
                        }
                        cdsimlvl++;
                    } else {
                        if (!doftpxmkd(p,0)) { /* Can't CD - try to create */
			    debug(F110,"ftp syncdir mkdir failed",p,0); 
/*
  Suppose we are executing SEND /RECURSIVE.  Locally we have a directory
  FOO but the remote has a regular file with the same name.  We can't CD
  to it, can't MKDIR it either.  There's no way out but to fail and let
  the user handle the problem.
*/
                            quiet = saveq;
                            return(0);
                        }
			debug(F110,"ftp syncdir mkdir OK",p,0); 
                        if (fdispla == XYFD_B) {
                            printf(" CREATED DIRECTORY %s\n",p);
                        } else if (fdispla) {
                            ckmakmsg(msgbuf,CKMAXPATH,
                                     "CREATED DIRECTORY ",p,NULL,NULL);
                            ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,msgbuf);
                        }
                        if (!doftpcwd(p,0)) { /* Try again to CD */
			    debug(F110,"ftp syncdir CD failed",p,0); 
                            quiet = saveq;
                            return(0);
                        }
                        if (fdispla == XYFD_B) printf(" CWD %s\n",p);
			debug(F110,"ftp syncdir CD OK",p,0); 
                    }
                }
                cdlevel++;
            }
            if (done)                   /* Quit if no next segment */
              break;
            p = s+1;                    /* Point to next segment */
        }
        s++;                            /* Point to next source char */
    }
  xcwd:
    ckstrncpy(putpath,tmp,CKMAXPATH+1); /* All OK - make this the new path */
    quiet = saveq;
    return(1);
}
#ifdef DOUPDATE
#ifdef DEBUG
static VOID
dbtime(s,xx) char * s; struct tm * xx; { /* Write struct tm to debug log */
    if (deblog) {
        debug(F111,"ftp year ",s,xx->tm_year);
        debug(F111,"ftp month",s,xx->tm_mon);
        debug(F111,"ftp day  ",s,xx->tm_mday);
        debug(F111,"ftp hour ",s,xx->tm_hour);
        debug(F111,"ftp min  ",s,xx->tm_min);
        debug(F111,"ftp sec  ",s,xx->tm_sec);
    }
}
#endif /* DEBUG */
/*  t m c o m p a r e  --  Compare two struct tm's */
/*  Like strcmp() but for struct tm's  */
/*  Returns -1 if xx < yy, 0 if they are equal, 1 if xx > yy */
static int
tmcompare(xx,yy) struct tm * xx, * yy; {
    if (xx->tm_year < yy->tm_year)      /* First year less than second */
      return(-1);
    if (xx->tm_year > yy->tm_year)      /* First year greater than second */
      return(1);
    /* Years are equal so compare months */
    if (xx->tm_mon  < yy->tm_mon)       /* And so on... */
      return(-1);
    if (xx->tm_mon  > yy->tm_mon)
      return(1);
    if (xx->tm_mday < yy->tm_mday)
      return(-1);
    if (xx->tm_mday > yy->tm_mday)
      return(1);
    if (xx->tm_hour < yy->tm_hour)
      return(-1);
    if (xx->tm_hour > yy->tm_hour)
      return(1);
    if (xx->tm_min  < yy->tm_min)
      return(-1);
    if (xx->tm_min  > yy->tm_min)
      return(1);
    if (xx->tm_sec  < yy->tm_sec)
      return(-1);
    if (xx->tm_sec  > yy->tm_sec)
      return(1);
    return(0);
}
#endif /* DOUPDATE */
#ifndef HAVE_TIMEGM             /* For platforms that do not have timegm() */
static CONST int MONTHDAYS[] = { /* Number of days in each month. */
    31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
/* Macro for whether a given year is a leap year. */
#define ISLEAP(year) \
(((year) % 4) == 0 && (((year) % 100) != 0 || ((year) % 400) == 0))
#endif /* HAVE_TIMEGM */
/*  m k u t i m e  --  Like mktime() but argument is already UTC */
static time_t
#ifdef CK_ANSIC
mkutime(struct tm * tm)
#else
mkutime(tm) struct tm * tm;
#endif /* CK_ANSIC */
/* mkutime */ {
#ifdef HAVE_TIMEGM
    return(timegm(tm));                 /* Have system service, use it. */
#else
/*
  Contributed by Russ Allbery (rra@stanford.edu), used by permission.
  Given a struct tm representing a calendar time in UTC, convert it to
  seconds since epoch.  Returns (time_t) -1 if the time is not
  convertable.  Note that this function does not canonicalize the provided
  struct tm, nor does it allow out-of-range values or years before 1970.
  Result should be identical with timegm().
*/
    time_t result = 0;
    int i;
    /*
      We do allow some ill-formed dates, but we don't do anything special
      with them and our callers really shouldn't pass them to us.  Do
      explicitly disallow the ones that would cause invalid array accesses
      or other algorithm problems.
    */
#ifdef DEBUG
    if (deblog) {
        debug(F101,"mkutime tm_mon","",tm->tm_mon);
        debug(F101,"mkutime tm_year","",tm->tm_year);
    }
#endif /* DEBUG */
    if (tm->tm_mon < 0 || tm->tm_mon > 11 || tm->tm_year < 70)
      return((time_t) -1);
    /* Convert to time_t. */
    for (i = 1970; i < tm->tm_year + 1900; i++)
      result += 365 + ISLEAP(i);
    for (i = 0; i < tm->tm_mon; i++)
      result += MONTHDAYS[i];
    if (tm->tm_mon > 1 && ISLEAP(tm->tm_year + 1900))
      result++;
    result = 24 * (result + tm->tm_mday - 1) + tm->tm_hour;
    result = 60 * result + tm->tm_min;
    result = 60 * result + tm->tm_sec;
    debug(F101,"mkutime result","",result);
    return(result);
#endif /* HAVE_TIMEGM */
}
/*
  s e t m o d t i m e  --  Set file modification time.
  f = char * filename;
  t = time_t date/time to set (Secs since 19700101 0:00:00 UTC, NOT local)
  UNIX-specific; isolates mainline code from hideous #ifdefs.
  Returns:
    0 on success,
   -1 on error.
*/
static int
#ifdef CK_ANSIC
setmodtime(char * f, time_t t)
#else
setmodtime(f,t) char * f; time_t t;
#endif /* CK_ANSIC */
/* setmodtime */ {
#ifdef NT
    struct _stat sb;
#else /* NT */
    struct stat sb;
#endif /* NT */
    int x, rc = 0;
#ifdef BSD44
    struct timeval tp[2];
#else  /* def BSD44 */
#ifdef V7
    struct utimbuf {
        time_t timep[2];
    } tp;
#else  /* def V7 */
#ifdef SYSUTIMEH
#ifdef NT
    struct _utimbuf tp;
#else /* NT */
    struct utimbuf tp;
#endif /* NT */
#else /* def SYSUTIMEH */
#ifdef VMS
    struct utimbuf tp;
#define SYSUTIMEH               /* Our utimbuf matches this one. */
#else /* def VMS */
    struct utimbuf {
        time_t atime;
        time_t mtime;
    } tp;
#endif /* def VMS [else] */
#endif /* def SYSUTIMEH [else] */
#endif /* def V7 [else] */
#endif /* def BSD44 [else] */
    if (stat(f,&sb) < 0) {
        debug(F111,"setmodtime stat failure",f,errno);
        return(-1);
    }
#ifdef BSD44
    tp[0].tv_sec = sb.st_atime;         /* Access time first */
    tp[1].tv_sec = t;                   /* Update time second */
    debug(F111,"setmodtime BSD44",f,t);
#else
#ifdef V7
    tp.timep[0] = t;                    /* Set modif. time to creation date */
    tp.timep[1] = sb.st_atime;          /* Don't change the access time */
    debug(F111,"setmodtime V7",f,t);
#else
#ifdef SYSUTIMEH
    tp.modtime = t;                     /* Set modif. time to creation date */
    tp.actime = sb.st_atime;            /* Don't change the access time */
    debug(F111,"setmodtime SYSUTIMEH",f,t);
#else
    tp.mtime = t;                       /* Set modif. time to creation date */
    tp.atime = sb.st_atime;             /* Don't change the access time */
    debug(F111,"setmodtime (other)",f,t);
#endif /* SYSUTIMEH */
#endif /* V7 */
#endif /* BSD44 */
    /* Try to set the file date */
#ifdef BSD44
    x = utimes(f,tp);
    debug(F111,"setmodtime utimes()","BSD44",x);
#else
#ifdef IRIX65
    {
      /*
        The following produces the nonsensical warning:
        Argument  of type "const struct utimbuf *" is incompatible with
        parameter of type "const struct utimbuf *".  If you can make it
        go away, be my guest.
      */
        const struct utimbuf * t2 = &tp;
        x = utime(f,t2);
    }
#else
    x = utime(f,&tp);
    debug(F111,"setmodtime utime()","other",x);
#endif /* IRIX65 */
#endif /* BSD44 */
    if (x)
      rc = -1;
    debug(F101,"setmodtime result","",rc);
    return(rc);
}
/*
  c h k m o d t i m e  --  Check/Set file modification time.
  fc = function code:
    0 = Check; returns:
      -1 on error,
       0 if local older than remote,
       1 if modtimes are equal,
       2 if local newer than remote.
    1 = Set (local file's modtime from remote's); returns:
      -1 on error,
       0 on success.
*/
static int
chkmodtime(local,remote,fc) char * local, * remote; int fc; {
#ifdef NT
    struct _stat statbuf;
#else /* NT */
    struct stat statbuf;
#endif /* NT */
    struct tm * tmlocal = NULL;
    struct tm tmremote;
    int rc = 0, havedate = 0, lcs = -1, rcs = -1, flag = 0;
    char * s, timebuf[64];
    debug(F111,"chkmodtime",local,mdtmok);
    if (!mdtmok)			/* Server supports MDTM? */
      return(-1);			/* No don't bother. */
#ifndef NOCSETS
    if (ftp_xla) {
        lcs = ftp_csl;
        if (lcs < 0) lcs = fcharset;
        rcs = ftp_csx;
        if (rcs < 0) rcs = ftp_csr;
    }
#endif /* NOCSETS */
    if (fc == 0) {
        rc = stat(local,&statbuf);
        if (rc == 0) {                  /* Get local file's mod time */
	    /* Convert to struct tm */
            tmlocal = gmtime((time_t *)&statbuf.st_mtime);
#ifdef DEBUG
            if (tmlocal) {
                dbtime(local,tmlocal);
            }
#endif /* DEBUG */
        }
    }
    /* Get remote file's mod time as yyyymmddhhmmss */
    if (havemdtm) {			/* Already got it from MLSD? */
	s = havemdtm;
	flag++;
    } else if (ftpcmd("MDTM",remote,lcs,rcs,0) == REPLY_COMPLETE) {
        char c;
        bzero((char *)&tmremote, sizeof(struct tm));
        s = ftp_reply_str;
        while ((c = *s++)) {            /* Skip past response code */
            if (c == SP) {
                flag++;
                break;
            }
        }
    }
    if (flag) {
	debug(F111,"ftp chkmodtime string",s,flag);
	if (fts_sto) {			/* User gave server time offset? */
	    char * p;
	    debug(F110,"ftp chkmodtime offset",fts_sto,0);
	    ckmakmsg(timebuf,64,s," ",fts_sto,NULL); /* Build delta time */
	    if ((p = cmcvtdate(timebuf,1))) { /* Apply delta time */
		ckstrncpy(timebuf,p,64);      /* Convert to MDTM format */
		timebuf[8]  = timebuf[9];  /* h */
		timebuf[9]  = timebuf[10]; /* h */
		timebuf[10] = timebuf[12]; /* m */
		timebuf[11] = timebuf[13]; /* m */
		timebuf[12] = timebuf[12]; /* s */
		timebuf[13] = timebuf[13]; /* s */
		timebuf[14] = NUL;
		s = timebuf;
		debug(F110,"ftp chkmodtime adjust",s,0);
	    }
	}
        if (flag) {                     /* Convert to struct tm */
            char * pat;
            int y2kbug = 0;             /* Seen in Kerberos 4 FTP servers */
            if (!ckstrcmp(s,"191",3,0)) {
                pat = "%05d%02d%02d%02d%02d%02d";
                y2kbug++;
                debug(F110,"ftp chkmodtime Y2K BUG detected",s,0);
            } else {
                pat = "%04d%02d%02d%02d%02d%02d";
            }
            if (sscanf(s,               /* Parse into struct tm */
                       pat,
                       &(tmremote.tm_year),
                       &(tmremote.tm_mon),
                       &(tmremote.tm_mday),
                       &(tmremote.tm_hour),
                       &(tmremote.tm_min),
                       &(tmremote.tm_sec)
                       ) == 6) {
                tmremote.tm_year -= (y2kbug ? 19000 : 1900);
                debug(F101,"ftp chkmodtime year","",tmremote.tm_year);
                tmremote.tm_mon--;
#ifdef DEBUG
		debug(F100,"SERVER TIME FOLLOWS:","",0);
                dbtime(remote,&tmremote);
#endif /* DEBUG */
                if (havedate > -1)
		  havedate = 1;
            }
        }
    } else {				/* Failed */
	debug(F101,"ftp chkmodtime ftpcode","",ftpcode);
	if (ftpcode == 500 ||		/* Command unrecognized */
	    ftpcode == 502 ||		/* Command not implemented */
	    ftpcode == 202)		/* Command superfluous */
	  mdtmok = 0;			/* Don't ask this server again */
	return(-1);
    }
    if (fc == 0) {                      /* Compare */
        if (havedate == 1) {		/* Only if we have both file dates */
            /*
              Compare with local file's time.  We don't use
              clock time (time_t) here in case of signed/unsigned
              confusion, etc.
            */
	    int xx;
#ifdef COMMENT
#ifdef DEBUG	    
	    if (deblog) {
		dbtime("LOCAL",tmlocal);
		dbtime("REMOT",&tmremote);
	    }
#endif /* DEBUG */
#endif /* COMMENT */
	    xx = tmcompare(tmlocal,&tmremote);
	    debug(F101,"chkmodtime tmcompare","",xx);
            return(xx + 1);
        }
    } else if (ftp_dates) {             /* Set */
        /*
          Here we must convert struct tm to time_t
          without applying timezone conversion, for which
          there is no portable API.  The method is hidden
          in mkutime(), defined above.
        */
        time_t utc;
        utc = mkutime(&tmremote);
        debug(F111,"ftp chkmodtime mkutime",remote,utc);
        if (utc != (time_t)-1)
          return(setmodtime(local,utc));
    }
    return(-1);
}
/* getfile() returns: -1 on error, 0 if file received, 1 if file skipped */
static int
getfile(remote,local,recover,append,pipename,xlate,fcs,rcs)
    char * local, * remote, * pipename; int recover, append, xlate, fcs, rcs;
/* getfile */ {
    int rc = -1;
    ULONG t0, t1;
#ifdef GFTIMER
    CKFLOAT sec;
#else
    int sec = 0;
#endif /* GFTIMER */
    char fullname[CKMAXPATH+1];
    debug(F110,"ftp getfile remote A",remote,0);
    debug(F110,"ftp getfile local A",local,0);
    debug(F110,"ftp getfile pipename",pipename,0);
    if (!remote) remote = "";
#ifdef PATTERNS
    /* Automatic type switching? */
    if (ftp_xfermode == XMODE_A && patterns && get_auto && !forcetype) {
        int x;
        x = matchname(remote,0,servertype);
        debug(F111,"ftp getfile matchname",remote,x);
        switch (x) {
          case 0: ftp_typ = FTT_ASC; break;
          case 1: ftp_typ = tenex ? FTT_TEN : FTT_BIN; break;
          default: if (g_ftp_typ > -1) ftp_typ = g_ftp_typ;
        }
        changetype(ftp_typ,ftp_vbm);
        binary = ftp_typ;               /* For file-transfer display */
    }
#endif /* PATTERNS */
#ifndef NOCSETS
    ftp_csx = -1;                       /* For file-transfer display */
    ftp_csl = -1;                       /* ... */
    if (rcs > -1)                       /* -1 means no translation */
      if (ftp_typ == FTT_ASC)           /* File type is "ascii"? */
        if (fcs < 0)                    /* File charset not forced? */
          fcs = fcharset;               /* use prevailing FILE CHARACTER-SET */
    if (fcs > -1 && rcs > -1) {         /* Set up translation functions */
        debug(F110,"ftp getfile","initxlate",0);
        initxlate(rcs,fcs);             /* NB: opposite order of PUT */
        ftp_csx = rcs;
        ftp_csl = fcs;
    } else
      xlate = 0;
#endif /* NOCSETS */
    if (!local) local = "";
    if (!pipename && !*local)
      local = remote;
    out2screen = !strcmp(local,"-");
    fullname[0] = NUL;
    if (pipename) {
        ckstrncpy(fullname,pipename,CKMAXPATH+1);
    } else {
        zfnqfp(local,CKMAXPATH,fullname);
        if (!fullname[0])
          ckstrncpy(fullname,local,CKMAXPATH+1);
    }
    if (!out2screen && displa && fdispla) { /* Screen */
        ftscreen(SCR_FN,'F',(CK_OFF_T)pktnum,remote);
        ftscreen(SCR_AN,0,(CK_OFF_T)0,fullname);
        ftscreen(SCR_FS,0,fsize,"");
    }
    tlog(F110,ftp_typ ? "ftp get BINARY:" : "ftp get TEXT:", remote, 0);
    tlog(F110," as",fullname,0);
    debug(F111,"ftp getfile size",remote,fsize);
    debug(F111,"ftp getfile local",local,out2screen);
    ckstrncpy(filnam, pipename ? remote : local, CKMAXPATH);
    t0 = gmstimer();                    /* Start time */
    debug(F111,"ftp getfile t0",remote,t0); /* ^^^ */
    rc = recvrequest("RETR",
                     local,
                     remote,
                     append ? "ab" : "wb",
                     0,
                     recover,
                     pipename,
                     xlate,
                     fcs,
                     rcs
                     );
    t1 = gmstimer();                    /* End time */
    debug(F111,"ftp getfile t1",remote,t1);
    debug(F111,"ftp getfile sec",remote,(t1-t0)/1000);
#ifdef GFTIMER
    sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
    fpxfsecs = sec;                     /* (for doxlog()) */
#else
    sec = (t1 - t0) / 1000;
    xfsecs = (int)sec;
#endif /* GFTIMER */
#ifdef FTP_TIMEOUT
    if (ftp_timed_out)
      rc = -4;
#endif	/* FTP_TIMEOUT */
    debug(F111,"ftp recvrequest rc",remote,rc);
    if (cancelfile || cancelgroup) {
        debug(F111,"ftp get canceled",ckitoa(cancelfile),cancelgroup);
        ftscreen(SCR_ST,ST_INT,(CK_OFF_T)0,"");
    } else if (rc > 0) {
        debug(F111,"ftp get skipped",ckitoa(cancelfile),cancelgroup);
        ftscreen(SCR_ST,ST_SKIP,(CK_OFF_T)0,cmarg);
    } else if (rc < 0) {
        switch (ftpcode) {
          case -4:                      /* Network error */
          case -2:                      /* File error */
            ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,ck_errstr());
            break;
          case -3:
            ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,
		     "Failure to make data connection");
            break;
          case -1:			/* (should be covered above) */
            ftscreen(SCR_ST,ST_INT,(CK_OFF_T)0,"");
            break;
          default:
            ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,&ftp_reply_str[4]);
        }
    } else {                            /* Tudo bem */
        ftscreen(SCR_PT,'Z',(CK_OFF_T)0,"");
        if (rc == 0) {
            ftscreen(SCR_ST,ST_OK,(CK_OFF_T)0,""); /* For screen */
            makestr(&rrfspec,remote);     /* For WHERE command */
            makestr(&rfspec,fullname);
        }
    }
    if (rc > -1) {
	if (ftp_dates)			/* If FTP DATES ON... */
	  if (!pipename && !out2screen)	/* and it's a real file */
	    if (rc < 1 && rc != -3)	/* and it wasn't skipped */
	      if (connected)		/* and we still have a connection */
		if (zchki(local) > -1) { /* and the file wasn't discarded */
		    chkmodtime(local,remote,1); /* set local file date */
		    debug(F110,"ftp get set date",local,0);
		}
	filcnt++;			/* Used by \v(filenum) */
    }
#ifdef TLOG
    if (tralog) {
        if (rc > 0) {
            tlog(F100," recovery skipped","",0);
        } else if (rc == 0) {
            tlog(F101," complete, size", "", fsize);
        } else if (cancelfile) {
            tlog(F100," canceled by user","",0);
#ifdef FTP_TIMEOUT
        } else if (ftp_timed_out) {
            tlog(F100," timed out","",0);
#endif	/* FTP_TIMEOUT */
        } else {
            tlog(F110," failed:",ftp_reply_str,0);
        }
        if (!tlogfmt)
          doxlog(what,local,fsize,ftp_typ,rc,"");
    }
#endif /* TLOG */
    return(rc);
}
/* putfile() returns: -1 on error, >0 if file not selected, 0 on success. */
/* Positive return value is Skip Reason, SKP_xxx, from ckcker.h. */
static int
putfile(cx,
    local,remote,force,moving,mvto,rnto,srvrn,x_cnv,x_usn,xft,prm,fcs,rcs,flg)
    char * local, * remote, * mvto, *rnto, *srvrn;
    int cx, force, moving, x_cnv, x_usn, xft, fcs, rcs, flg, prm;
/* putfile */ {
    char asname[CKMAXPATH+1];
    char fullname[CKMAXPATH+1];
    int k = -1, x = 0, y = 0, o = -1, rc = 0, nc = 0;
    int xlate = 0, restart = 0, mt = -1;
    char * s = NULL, * cmd = NULL;
    ULONG t0 = 0, t1 = 0;		/* Times for stats */
    int ofcs = 0, orcs = 0;
#ifdef GFTIMER
    CKFLOAT sec = 0.0;
#else
    int sec = 0;
#endif /* GFTIMER */
    debug(F111,"ftp putfile flg",local,flg);
    debug(F110,"ftp putfile srv_renam",srvrn,0);
    debug(F101,"ftp putfile fcs","",fcs);
    debug(F101,"ftp putfile rcs","",rcs);
    ofcs = fcs;                         /* Save charset args */
    orcs = rcs;
    sendstart = (CK_OFF_T)0;
    restart = flg & PUT_RES;
    if (!remote)
      remote = "";
    /* FTP protocol command to send to server */
    cmd = (cx == FTP_APP) ? "APPE" : (x_usn ? "STOU" : "STOR");
    if (x_cnv == SET_AUTO) {            /* Name conversion is auto */
        if (alike) {                    /* If server & client are alike */
            nc = 0;                     /* no conversion */
        } else {                        /* If they are different */
            if (servertype == SYS_UNIX || servertype == SYS_WIN32)
              nc = -1;                  /* only minimal conversions needed */
            else                        /* otherwise */
              nc = 1;                   /* full conversion */
        }
    } else                              /* Not auto - do what user said */
      nc = x_cnv;
    /* If Transfer Mode is Automatic, determine file type */
    if (ftp_xfermode == XMODE_A && filepeek && !pipesend) {
        if (isdir(local)) {             /* If it's a directory */
            k = FT_BIN;                 /* skip the file scan */
        } else {
            debug(F110,"FTP PUT calling scanfile",local,0);
            k = scanfile(local,&o,nscanfile); /* Scan the file */
        }
        debug(F111,"FTP PUT scanfile",local,k);
        if (k > -1 && !forcetype) {
            ftp_typ = (k == FT_BIN) ? 1 : 0;
            if (xft > -1 && ftp_typ != xft) {
                if (flg & PUT_SIM)
                  tlog(F110,"ftp put SKIP (Type):", local, 0);
                return(SKP_TYP);
            }
            if (ftp_typ == 1 && tenex)  /* User said TENEX? */
              ftp_typ = FTT_TEN;
        }
    }
#ifndef NOCSETS
    ftp_csx = -1;                       /* For file-transfer display */
    ftp_csl = -1;                       /* ... */
    if (rcs > -1) {                     /* -1 means no translation */
        if (ftp_typ == 0) {             /* File type is "ascii"? */
            if (fcs < 0) {              /* File charset not forced? */
                if (k < 0) {            /* If we didn't scan */
                    fcs = fcharset;     /* use prevailing FILE CHARACTER-SET */
                } else {                /* If we did scan, use scan result */
                    switch (k) {
                      case FT_TEXT:     /* Unknown text */
                        fcs = fcharset;
                        break;
                      case FT_7BIT:     /* 7-bit text */
                        fcs = dcset7;
                        break;
                      case FT_8BIT:     /* 8-bit text */
                        fcs = dcset8;
                        break;
                      case FT_UTF8:     /* UTF-8 */
                        fcs = FC_UTF8;
                        break;
                      case FT_UCS2:     /* UCS-2 */
                        fcs = FC_UCS2;
                        if (o > -1)     /* Input file byte order */
                          fileorder = o;
                        break;
                      default:
                        rcs = -1;
                    }
                }
            }
        }
    }
    if (fcs > -1 && rcs > -1) {         /* Set up translation functions */
        debug(F110,"ftp putfile","initxlate",0);
        initxlate(fcs,rcs);
        debug(F111,"ftp putfile rcs",fcsinfo[rcs].keyword,rcs);
        xlate = 1;
        ftp_csx = rcs;
        ftp_csl = fcs;
    }
#endif /* NOCSETS */
    binary = ftp_typ;                   /* For file-transfer display */
    asname[0] = NUL;
    if (recursive) {                    /* If sending recursively, */
        if (!syncdir(local,flg & PUT_SIM)) /* synchronize directories. */
          return(-1);                   /* Don't PUT if it fails. */
        else if (isdir(local))          /* It's a directory */
          return(0);                    /* Don't send it! */
    }
    if (*remote) {                      /* If an as-name template was given */
#ifndef NOSPL
        if (cmd_quoting) {              /* and COMMAND QUOTING is ON */
            y = CKMAXPATH;              /* evaluate it for this file */
            s = asname;
            zzstring(remote,&s,&y);
        } else
#endif /* NOSPL */
          ckstrncpy(asname,remote,CKMAXPATH);   /* (or take it literally) */
    } else {                                    /* No as-name */
        nzltor(local,asname,nc,0,CKMAXPATH);    /* use local name strip path */
        debug(F110,"FTP PUT nzltor",asname,0);
    }
    /* Preliminary messages and log entries */
    fullname[0] = NUL;
    zfnqfp(local,CKMAXPATH,fullname);
    if (!fullname[0]) ckstrncpy(fullname,local,CKMAXPATH+1);
    fullname[CKMAXPATH] = NUL;
    if (displa && fdispla) {            /* Screen */
        ftscreen(SCR_FN,'F',(CK_OFF_T)pktnum,local);
        ftscreen(SCR_AN,0,(CK_OFF_T)0,asname);
        ftscreen(SCR_FS,0,fsize,"");
    }
#ifdef DOUPDATE
    if (flg & (PUT_UPD|PUT_DIF)) {	/* Date-checking modes... */
        mt = chkmodtime(fullname,asname,0);
        debug(F111,"ftp putfile chkmodtime",asname,mt);
        if (mt == 0 && ((flg & PUT_DIF) == 0)) { /* Local is older */
            tlog(F110,"ftp put /update SKIP (Older modtime): ",fullname,0);
	    /* Skip this one */
            ftscreen(SCR_ST,ST_SKIP,(CK_OFF_T)SKP_DAT,fullname);
            filcnt++;
            return(SKP_DAT);
        } else if (mt == 1) {           /* Times are equal */
            tlog(F110,"ftp put /update SKIP (Equal modtime): ",fullname,0);
            ftscreen(SCR_ST,ST_SKIP,(CK_OFF_T)SKP_EQU,fullname); /* Skip it */
            filcnt++;
            return(SKP_DAT);
        }
	/* Local file is newer */
        tlog(F110,ftp_typ ? "ftp put /update BINARY:" :
             "ftp put /update TEXT:", fullname, 0);
    } else if (flg & PUT_RES) {
        tlog(F110,ftp_typ ? "ftp put /recover BINARY:" :
             "ftp put /recover TEXT:", fullname, 0);
    } else {
        tlog(F110,ftp_typ ? "ftp put BINARY:" : "ftp put TEXT:", fullname, 0);
    }
#else
    tlog(F110,ftp_typ ? "ftp put BINARY:" : "ftp put TEXT:", fullname, 0);
#endif /* DOUPDATE */
    tlog(F110," as",asname,0);
#ifndef NOCSETS
    if (xlate) {
        debug(F111,"ftp putfile fcs",fcsinfo[fcs].keyword,fcs);
        tlog(F110," file character set:",fcsinfo[fcs].keyword,0);
        tlog(F110," server character set:",fcsinfo[rcs].keyword,0);
    } else if (!ftp_typ) {
        tlog(F110," character sets:","no conversion",0);
        fcs = ofcs;                     /* Binary file but we still must */
        rcs = orcs;                     /* translate its name */
    }
#endif /* NOCSETS */
    /* PUT THE FILE */
    t0 = gmstimer();                    /* Start time */
    if (flg & PUT_SIM) {                /* rc > 0 is a skip reason code */
        if (flg & (PUT_UPD|PUT_DIF)) {	/* (see SKP_xxx in ckcker.h) */
            rc = (mt < 0) ?             /* Update mode... */
              SKP_XNX :                 /* Remote file doesn't exist */
                SKP_XUP;                /* Remote file is older */
        } else {
            rc = SKP_SIM;               /* "Would be sent", period. */
        }
    } else {
        rc = sendrequest(cmd,local,asname,xlate,fcs,rcs,restart);
    }
    t1 = gmstimer();                    /* End time */
    filcnt++;                           /* File number */
#ifdef GFTIMER
    sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
    fpxfsecs = sec;                     /* (for doxlog()) */
#else
    sec = (t1 - t0) / 1000;
    xfsecs = (int)sec;
#endif /* GFTIMER */
    debug(F111,"ftp sendrequest rc",local,rc);
    if (cancelfile || cancelgroup) {
        debug(F111,"ftp put canceled",ckitoa(cancelfile),cancelgroup);
        ftscreen(SCR_ST,ST_INT,(CK_OFF_T)0,"");
    } else if (rc > 0) {
        debug(F101,"ftp put skipped",local,rc);
        ftscreen(SCR_ST,ST_SKIP,(CK_OFF_T)rc,fullname);
    } else if (rc < 0) {
        debug(F111,"ftp put error",local,ftpcode);
        ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,&ftp_reply_str[4]);
    } else {
        debug(F111,"ftp put not canceled",ckitoa(displa),fdispla);
        ftscreen(SCR_PT,'Z',(CK_OFF_T)0,"");
        debug(F111,"ftp put ST_OK",local,rc);
        ftscreen(SCR_ST,ST_OK,(CK_OFF_T)0,"");
        debug(F110,"ftp put old sfspec",sfspec,0);
        makestr(&sfspec,fullname);      /* For WHERE command */
        debug(F110,"ftp put new sfspec",sfspec,0);
        debug(F110,"ftp put old srfspec",srfspec,0);
        makestr(&srfspec,asname);
        debug(F110,"ftp put new srfspec",srfspec,0);
    }
    /* Final log entries */
#ifdef TLOG
    if (tralog) {
        if (rc > 0) {
          switch (rc) {
            case SKP_XNX:
              tlog(F100," /simulate: WOULD BE SENT:","no remote file",0);
              break;
            case SKP_XUP:
              tlog(F100," /simulate: WOULD BE SENT:","remote file older",0);
              break;
            case SKP_SIM:
              tlog(F100," /simulate: WOULD BE SENT","",0);
              break;
            default:
              tlog(F110," skipped:",gskreason(rc),0);
            }
        } else if (rc == 0) {
            tlog(F101," complete, size", "", fsize);
        } else if (cancelfile) {
            tlog(F100," canceled by user","",0);
        } else {
            tlog(F110," failed:",ftp_reply_str,0);
        }
        if (!tlogfmt)
          doxlog(what,local,fsize,ftp_typ,rc,"");
    }
#endif /* TLOG */
    if (rc < 0)                         /* PUT did not succeed */
      return(-1);                       /* so done. */
    if (flg & PUT_SIM)                  /* Simulating, skip the rest. */
      return(SKP_SIM);
#ifdef UNIX
    /* Set permissions too? */
    if (prm) {                          /* Change permissions? */
        s = zgperm(local);              /* Get perms of local file */
        if (!s) s = "";
        x = strlen(s);
        if (x > 3) s += (x - 3);
        if (rdigits(s)) {
            ckmakmsg(ftpcmdbuf,FTP_BUFSIZ,s," ",asname,NULL);
            x =
              ftpcmd("SITE CHMOD",ftpcmdbuf,fcs,rcs,ftp_vbm) == REPLY_COMPLETE;
            tlog(F110, x ? " chmod" : " chmod failed",
                 s,
                 0
                 );
            if (!x)
              return(-1);
        }
    }
#endif /* UNIX */
    /* Disposition of source file */
    if (moving) {
        x = zdelet(local);
        tlog(F110, (x > -1) ?
             " deleted" : " failed to delete",
             local,
             0
             );
        if (x < 0)
          return(-1);
    } else if (mvto) {
        x = zrename(local,mvto);
        tlog(F110, (x > -1) ?
             " moved source to" : " failed to move source to",
             mvto,
             0
             );
        if (x < 0)
          return(-1);
        /* ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,mvto); */
    } else if (rnto) {
        char * s = rnto;
#ifndef NOSPL
        int y;                          /* Pass it thru the evaluator */
        extern int cmd_quoting;         /* for \v(filename) */
        if (cmd_quoting) {              /* But only if cmd_quoting is on */
            y = CKMAXPATH;
            s = (char *)asname;
            zzstring(rnto,&s,&y);
            s = (char *)asname;
        }
#endif /* NOSPL */
        if (s) if (*s) {
            int x;
            x = zrename(local,s);
            tlog(F110, (x > -1) ?
                 " renamed source file to" :
                 " failed to rename source file to",
                 s,
                 0
                 );
            if (x < 0)
              return(-1);
            /* ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,s); */
        }
    }
    /* Disposition of destination file */
    if (srvrn) {                        /* /SERVER-RENAME: */
        char * s = srvrn;
#ifndef NOSPL
        int y;                          /* Pass it thru the evaluator */
        extern int cmd_quoting; /* for \v(filename) */
        debug(F111,"ftp putfile srvrn",s,1);
        if (cmd_quoting) {              /* But only if cmd_quoting is on */
            y = CKMAXPATH;
            s = (char *)fullname;       /* We can recycle this buffer now */
            zzstring(srvrn,&s,&y);
            s = (char *)fullname;
        }
#endif /* NOSPL */
        debug(F111,"ftp putfile srvrn",s,2);
        if (s) if (*s) {
            int x;
            x = ftp_rename(asname,s);
            debug(F111,"ftp putfile ftp_rename",asname,x);
            tlog(F110, (x > 0) ?
                 " renamed destination file to" :
                 " failed to rename destination file to",
                 s,
                 0
                 );
            if (x < 1)
              return(-1);
        }
    }
    return(0);
}
/* xxout must only be used for ASCII transfers */
static int
#ifdef CK_ANSIC
xxout(char c)
#else
xxout(c) char c;
#endif /* CK_ANSIC */
{
#ifndef OS2
#ifndef VMS
#ifndef MAC
#ifndef OSK
    /* For Unix, DG, Stratus, Amiga, Gemdos, other */
    if (c == '\012') {
	if (zzout(dout,(CHAR)'\015') < 0)
	  return(-1);
	ftpsnd.bytes++;
    }
#else /* OSK */
    if (c == '\015') {
	c = '\012';
	if (zzout(dout,(CHAR)'\015') < 0)
	  return(-1);
	ftpsnd.bytes++;
    }
#endif /* OSK */
#else /* MAC */
    if (c == '\015') {
	c = '\012';
	if (zzout(dout,(CHAR)'\015') < 0)
	  return(-1);
	ftpsnd.bytes++;
    }
#endif /* MAC */
#endif /* VMS */
#endif /* OS2 */
    if (zzout(dout,(CHAR)c) < 0)
      return(-1);
    ftpsnd.bytes++;
    return(0);
}
static int
#ifdef CK_ANSIC
scrnout(char c)
#else
scrnout(c) char c;
#endif /* CK_ANSIC */
{
    return(putchar(c));
}
static int
#ifdef CK_ANSIC
pipeout(char c)
#else
pipeout(c) char c;
#endif /* CK_ANSIC */
{
    return(zmchout(c));
}
static int
ispathsep(c) int c; {
    switch (servertype) {
      case SYS_VMS:
      case SYS_TOPS10:
      case SYS_TOPS20:
        return(((c == ']') || (c == '>') || (c == ':')) ? 1 : 0);
      case SYS_OS2:
      case SYS_WIN32:
      case SYS_DOS:
        return(((c == '\\') || (c == '/') || (c == ':')) ? 1 : 0);
      case SYS_VOS:
        return((c == '>') ? 1 : 0);
      default:
        return((c == '/') ? 1 : 0);
    }
}
static int
iscanceled() {
#ifdef CK_CURSES
    extern int ck_repaint();
#endif /* CK_CURSES */
    int x, rc = 0;
    char c = 0;
    if (cancelfile)
      return(1);
    x = conchk();                       /* Any chars waiting at console? */
    if (x-- > 0) {                      /* Yes...  */
        c = coninc(5);                  /* Get one */
        switch (c) {
          case 032:                     /* Ctrl-X or X */
          case 'z':
          case 'Z': cancelgroup++;      /* fall thru on purpose */
          case 030:                     /* Ctrl-Z or Z */
          case 'x':
          case 'X': cancelfile++; rc++; break;
#ifdef CK_CURSES
          case 'L':
          case 'l':
          case 014:                     /* Ctrl-L or L or Ctrl-W */
          case 027:
            ck_repaint();               /* Refresh screen */
#endif /* CK_CURSES */
        }
    }
    while (x-- > 0)                     /* Soak up any rest */
      c = coninc(1);
    return(rc);
}
#ifdef FTP_TIMEOUT
/* fc = 0 for read; 1 for write */
static int
check_data_connection(fd,fc) int fd, fc; {
    int x;
    struct timeval tv;
    fd_set in, out, err;
    if (ftp_timeout < 1L)
      return(0);
    FD_ZERO(&in);
    FD_ZERO(&out);
    FD_ZERO(&err);
    FD_SET(fd,fc ? &out : &in);
    tv.tv_sec = ftp_timeout;		/* Time limit */
    tv.tv_usec = 0L;
#ifdef INTSELECT
    x = select(FD_SETSIZE,(int *)&in,(int *)&out,(int *)&err,&tv);
#else
    x = select(FD_SETSIZE,&in,&out,&err,&tv);
#endif /* INTSELECT */
    if (x == 0) {
#ifdef EWOULDBLOCK
	errno = EWOULDBLOCK;
#else
#ifdef EAGAIN
	errno = EAGAIN;
#else
	errno = 11;
#endif	/* EAGAIN */
#endif	/* EWOULDBLOCK */
	debug(F100,"ftp check_data_connection TIMOUT","",0);
	return(-3);
    }
    return(0);
}
#endif	/* FTP_TIMEOUT */
/* zzsend - used by buffered output macros. */
static int
#ifdef CK_ANSIC
zzsend(int fd, CHAR c)
#else
zzsend(fd,c) int fd; CHAR c;
#endif /* CK_ANSIC */
{
    int rc;
    debug(F101,"zzsend ucbufsiz","",ucbufsiz);
    debug(F101,"zzsend nout","",nout);
    debug(F111,"zzsend","secure?",ftpissecure());
    if (iscanceled())                   /* Check for cancellation */
      return(-9);
#ifdef FTP_TIMEOUT    
    ftp_timed_out = 0;
    if (check_data_connection(fd,1) < 0) {
	ftp_timed_out = 1;
	return(-3);
    }
#endif	/* FTP_TIMEOUT */
    rc = (!ftpissecure()) ?
      send(fd, (SENDARG2TYPE)ucbuf, nout, 0) :
        secure_putbuf(fd, ucbuf, nout);
    ucbuf[nout] = NUL;
    nout = 0;
    ucbuf[nout++] = c;
    spackets++;
    pktnum++;
    if (rc > -1 && fdispla != XYFD_B) {
        spktl = nout;
        ftscreen(SCR_PT,'D',(CK_OFF_T)spackets,NULL);
    }
    return(rc);
}
/* c m d l i n p u t  --  Command-line PUT */
int
cmdlinput(stay) int stay; {
    int x, rc = 0, done = 0, good = 0, status = 0;
    ULONG t0, t1;                       /* Times for stats */
#ifdef GFTIMER
    CKFLOAT sec;
#else
    int sec = 0;
#endif /* GFTIMER */
    if (quiet) {                        /* -q really means quiet */
        displa = 0;
        fdispla = 0;
    } else {
        displa = 1;
        fdispla = XYFD_B;
    }
    testing = 0;
    out2screen = 0;
    dpyactive = 0;
    what = W_FTP|W_SEND;
#ifndef NOSPL
    cmd_quoting = 0;
#endif /* NOSPL */
    sndsrc = nfils;
    t0 = gmstimer();                    /* Record starting time */
    while (!done && !cancelgroup) {     /* Loop for all files */
        cancelfile = 0;
        x = gnfile();                   /* Get next file from list(s) */
        if (x == 0)                     /* (see gnfile() comments...) */
          x = gnferror;
        switch (x) {
          case 1:                       /* File to send */
            rc = putfile(FTP_PUT,       /* Function (PUT, APPEND) */
                         filnam,        /* Local file to send */
                         filnam,        /* Remote name for file */
                         forcetype,     /* Text/binary mode forced */
                         0,             /* Not moving */
                         NULL,          /* No move-to */
                         NULL,          /* No rename-to */
                         NULL,          /* No server-rename */
                         ftp_cnv,       /* Filename conversion */
                         0,             /* Unique-server-names */
                         -1,            /* All file types */
                         0,             /* No permissions */
                         -1,            /* No character sets */
                         -1,            /* No character sets */
                         0              /* No update or restart */
                         );
            if (rc > -1) {
                good++;
                status = 1;
            }
            if (cancelfile) {
                continue;               /* Or break? */
            }
            if (rc < 0) {
                ftp_fai++;
            }
            continue;                   /* Or break? */
          case 0:                       /* No more files, done */
            done++;
            continue;
          case -2:
          case -1:
            printf("?%s: file not found - \"%s\"\n",
                   puterror ? "Fatal" : "Warning",
                   filnam
                   );
            continue;                   /* or break? */
          case -3:
            printf("?Warning access denied - \"%s\"\n", filnam);
            continue;                   /* or break? */
          case -5:
            printf("?Too many files match\n");
            done++;
            break;
          case -6:
            if (good < 1)
              printf("?No files selected\n");
            done++;
            break;
          default:
            printf("?getnextfile() - unknown failure\n");
            done++;
        }
    }
    if (status > 0) {
        if (cancelgroup)
          status = 0;
        else if (cancelfile && good < 1)
          status = 0;
    }
    success = status;
    x = success;
    if (x > -1) {
        lastxfer = W_FTP|W_SEND;
        xferstat = success;
    }
    t1 = gmstimer();                    /* End time */
#ifdef GFTIMER
    sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
    if (!sec) sec = 0.001;
    fptsecs = sec;
#else
    sec = (t1 - t0) / 1000;
    if (!sec) sec = 1;
#endif /* GFTIMER */
    tfcps = (long) (tfc / sec);
    tsecs = (int)sec;
    lastxfer = W_FTP|W_SEND;
    xferstat = success;
    if (dpyactive)
      ftscreen(status > 0 ? SCR_TC : SCR_CW, 0, (CK_OFF_T)0, "");
    if (!stay)
      doexit(success ? GOOD_EXIT : BAD_EXIT, -1);
    return(success);
}
/*  d o f t p p u t  --  Parse and execute PUT, MPUT, and APPEND  */
int
#ifdef CK_ANSIC
doftpput(int cx, int who)               /* who == 1 for ftp, 0 for kermit */
#else
doftpput(cx,who) int cx, who;
#endif /* CK_ANSIC */
{
    struct FDB sf, fl, sw, cm;
    int n, rc, confirmed = 0, wild = 0, getval = 0, mput = 0, done = 0;
    int x_cnv = 0, x_usn = 0, x_prm = 0, putflags = 0, status = 0, good = 0;
    char * s, * s2;
    int x_csl, x_csr = -1;              /* Local and remote charsets */
    int x_xla = 0;
    int x_recurse = 0;
    char c, * p;                        /* Workers */
#ifdef PUTARRAY
    int range[2];                       /* Array range */
    char ** ap = NULL;                  /* Array pointer */
    int arrayx = -1;                    /* Array index */
#endif /* PUTARRAY */
    ULONG t0 = 0L, t1 = 0L;             /* Times for stats */
#ifdef GFTIMER
    CKFLOAT sec;
#else
    int sec = 0;
#endif /* GFTIMER */
    struct stringint pv[SND_MAX+1];    /* Temporary array for switch values */
    success = 0;                        /* Assume failure */
    forcetype = 0;                      /* No /TEXT or /BINARY given yet */
    out2screen = 0;                     /* Not outputting file to screen */
    putflags = 0;                       /* PUT options */
    x_cnv = ftp_cnv;                    /* Filename conversion */
    x_usn = ftp_usn;                    /* Unique server names */
    x_prm = ftp_prm;                    /* Permissions */
    if (x_prm == SET_AUTO)              /* Permissions AUTO */
      x_prm = alike;
#ifndef NOCSETS
    x_csr = ftp_csr;                    /* Inherit global server charset */
    x_csl = ftp_csl;
    if (x_csl < 0)
      x_csl = fcharset;
    x_xla = ftp_xla;
#endif /* NOCSETS */
    makestr(&filefile,NULL);            /* No filename list file yet. */
    makestr(&srv_renam,NULL);		/* Clear /SERVER-RENAME: */
    makestr(&snd_rename,NULL);		/*  PUT /RENAME */
    makestr(&snd_move,NULL);		/*  PUT /MOVE */
    putpath[0] = NUL;                   /* Initialize for syncdir(). */
    puterror = ftp_err;                 /* Inherit global error action. */
    what = W_SEND|W_FTP;                /* What we're doing (sending w/FTP) */
    asnambuf[0] = NUL;                  /* Clear as-name buffer */
    if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
        ftp_typ = g_ftp_typ;
        /* g_ftp_typ = -1; */
    }
    for (i = 0; i <= SND_MAX; i++) {    /* Initialize switch values */
        pv[i].sval = NULL;              /* to null pointers */
        pv[i].ival = -1;                /* and -1 int values */
        pv[i].wval = (CK_OFF_T)-1;	/* and -1 wide values */
    }
    if (who == 0) {                     /* Called with unprefixed command */
        switch (cx) {
          case XXRSEN:  pv[SND_RES].ival = 1; break;
          case XXCSEN:  pv[SND_CMD].ival = 1; break;
          case XXMOVE:  pv[SND_DEL].ival = 1; break;
          case XXMMOVE: pv[SND_DEL].ival = 1; /* fall thru */
          case XXMSE:   mput++; break;
        }
    } else {
	if (cx == FTP_REP)
	  pv[SND_RES].ival = 1;
        if (cx == FTP_MPU)
          mput++;
    }
    cmfdbi(&sw,                         /* First FDB - command switches */
           _CMKEY,                      /* fcode */
           "Filename, or switch",       /* hlpmsg */
           "",                          /* default */
           "",                          /* addtl string data */
           nputswi,                     /* addtl numeric data 1: tbl size */
           4,                           /* addtl numeric data 2: 4 = cmswi */
           xxstring,                    /* Processing function */
           putswi,                      /* Keyword table */
           &sf                          /* Pointer to next FDB */
           );
    cmfdbi(&fl,                         /* 3rd FDB - local filespec */
           _CMFLD,                      /* fcode */
           "",                          /* hlpmsg */
           "",                          /* default */
           "",                          /* addtl string data */
           0,                           /* addtl numeric data 1 */
           0,                           /* addtl numeric data 2 */
           xxstring,
           NULL,
           &cm
           );
    cmfdbi(&cm,                         /* 4th FDB - Confirmation */
           _CMCFM,                      /* fcode */
           "",                          /* hlpmsg */
           "",                          /* default */
           "",                          /* addtl string data */
           0,                           /* addtl numeric data 1 */
           0,                           /* addtl numeric data 2 */
           NULL,
           NULL,
           NULL
           );
  again:
    cmfdbi(&sf,                         /* 2nd FDB - file to send */
           _CMIFI,                      /* fcode */
           "",                          /* hlpmsg */
           "",                          /* default */
           "",                          /* addtl string data */
           /* 0 = parse files, 1 = parse files or dirs, 2 = skip symlinks */
           nolinks | x_recurse,         /* addtl numeric data 1 */
           0,                           /* dirflg 0 means "not dirs only" */
           xxstring,
           NULL,
#ifdef COMMENT
           mput ? &cm : &fl
#else
	   &fl
#endif /* COMMENT */
           );
    while (1) {                         /* Parse zero or more switches */
        x = cmfdb(&sw);                 /* Parse something */
        debug(F101,"ftp put cmfdb A","",x);
        debug(F101,"ftp put fcode A","",cmresult.fcode);
        if (x < 0)                      /* Error */
          goto xputx;                   /* or reparse needed */
        if (cmresult.fcode != _CMKEY)   /* Break out of loop if not a switch */
          break;
        c = cmgbrk();                   /* Get break character */
        getval = (c == ':' || c == '='); /* to see how they ended the switch */
        if (getval && !(cmresult.kflags & CM_ARG)) {
            printf("?This switch does not take arguments\n");
            x = -9;
            goto xputx;
        }
        if (!getval && (cmgkwflgs() & CM_ARG)) {
            printf("?This switch requires an argument\n");
            x = -9;
            goto xputx;
        }
        n = cmresult.nresult;           /* Numeric result = switch value */
        debug(F101,"ftp put switch","",n);
        switch (n) {                    /* Process the switch */
          case SND_AFT:                 /* Send /AFTER:date-time */
          case SND_BEF:                 /* Send /BEFORE:date-time */
          case SND_NAF:                 /* Send /NOT-AFTER:date-time */
          case SND_NBE:                 /* Send /NOT-BEFORE:date-time */
            if (!getval) break;
            if ((x = cmdate("File date-time","",&s,0,xxstring)) < 0) {
                if (x == -3) {
                    printf("?Date-time required\n");
                    x = -9;
                }
                goto xputx;
            }
            pv[n].ival = 1;
            makestr(&(pv[n].sval),s);
            break;
          case SND_ASN:                 /* /AS-NAME: */
            debug(F101,"ftp put /as-name getval","",getval);
            if (!getval) break;
            if ((x = cmfld("Name to send under","",&s,NULL)) < 0) {
                if (x == -3) {
                    printf("?name required\n");
                    x = -9;
                }
                goto xputx;
            }
            makestr(&(pv[n].sval),brstrip(s));
            debug(F110,"ftp put /as-name 1",pv[n].sval,0);
            if (pv[n].sval) pv[n].ival = 1;
            break;
#ifdef PUTARRAY
          case SND_ARR:                 /* /ARRAY */
            if (!getval) break;
            ap = NULL;
            if ((x = cmfld("Array name (a single letter will do)",
                           "",
                           &s,
                           NULL
                           )) < 0) {
                if (x == -3)
		  break;
		else
		  return(x);
            }
            if ((x = arraybounds(s,&(range[0]),&(range[1]))) < 0) {
                printf("?Bad array: %s\n",s);
                return(-9);
            }
            if (!(ap = a_ptr[x])) {
                printf("?No such array: %s\n",s);
                return(-9);
            }
            pv[n].ival = 1;
            pv[SND_CMD].ival = 0;       /* Undo any conflicting ones... */
            pv[SND_RES].ival = 0;
            pv[SND_FIL].ival = 0;
            arrayx = x;
            break;
#endif /* PUTARRAY */
          case SND_BIN:                 /* /BINARY */
          case SND_TXT:                 /* /TEXT or /ASCII */
          case SND_TEN:                 /* /TENEX */
            pv[SND_BIN].ival = 0;
            pv[SND_TXT].ival = 0;
            pv[SND_TEN].ival = 0;
            pv[n].ival = 1;
            break;
#ifdef PUTPIPE
          case SND_CMD:                 /* These take no args */
            if (nopush) {
                printf("?Sorry, system command access is disabled\n");
                x = -9;
                goto xputx;
            }
#ifdef PIPESEND
            else if (sndfilter) {
                printf("?Sorry, no PUT /COMMAND when SEND FILTER selected\n");
                x = -9;
                goto xputx;
            }
#endif /* PIPESEND */
            sw.hlpmsg = "Command, or switch"; /* Change help message */
            pv[n].ival = 1;             /* Just set the flag */
            pv[SND_ARR].ival = 0;
            break;
#endif /* PUTPIPE */
#ifdef CKSYMLINK
          case SND_LNK:
            nolinks = 0;
            goto again;			/* Because CMIFI params changed... */
          case SND_NLK:
            nolinks = 2;
            goto again;
#endif /* CKSYMLINK */
#ifdef FTP_RESTART
          case SND_RES:                 /* /RECOVER (resend) */
            pv[SND_ARR].ival = 0;       /* fall thru on purpose... */
#endif /* FTP_RESTART */
          case SND_NOB:
          case SND_DEL:                 /* /DELETE */
          case SND_SHH:                 /* /QUIET */
          case SND_UPD:                 /* /UPDATE */
          case SND_SIM:                 /* /UPDATE */
          case SND_USN:                 /* /UNIQUE */
            pv[n].ival = 1;             /* Just set the flag */
            break;
          case SND_REC:                 /* /RECURSIVE */
            recursive = 2;              /* Must be set before cmifi() */
            x_recurse = 1;
            goto again;			/* Because CMIFI params changed... */
            break;
#ifdef UNIXOROSK
          case SND_DOT:                 /* /DOTFILES */
            matchdot = 1;
            break;
          case SND_NOD:                 /* /NODOTFILES */
            matchdot = 0;
            break;
#endif /* UNIXOROSK */
          case SND_ERR:                 /* /ERROR-ACTION */
            if ((x = cmkey(qorp,2,"","",xxstring)) < 0)
              goto xputx;
            pv[n].ival = x;
            break;
          case SND_EXC:                 /* Excludes */
            if (!getval) break;
            if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
                if (x == -3) {
                    printf("?Pattern required\n");
                    x = -9;
                }
                goto xputx;
            }
            if (s) if (!*s) s = NULL;
            makestr(&(pv[n].sval),s);
            if (pv[n].sval)
              pv[n].ival = 1;
            break;
          case SND_PRM:                 /* /PERMISSIONS */
            if (!getval)
              x = 1;
            else if ((x = cmkey(onoff,2,"","on",xxstring)) < 0)
              goto xputx;
            pv[SND_PRM].ival = x;
            break;
#ifdef PIPESEND
          case SND_FLT:                 /* /FILTER */
            debug(F101,"ftp put /filter getval","",getval);
            if (!getval) break;
            if ((x = cmfld("Filter program to send through","",&s,NULL)) < 0) {
                if (x == -3)
                  s = "";
                else
                  goto xputx;
            }
            if (*s) s = brstrip(s);
            y = strlen(s);
            for (x = 0; x < y; x++) {   /* Make sure they included "\v(...)" */
                if (s[x] != '\\') continue;
                if (s[x+1] == 'v') break;
            }
            if (x == y) {
                printf(
                "?Filter must contain a replacement variable for filename.\n"
                       );
                x = -9;
                goto xputx;
            }
            if (s) if (!*s) s = NULL;
            makestr(&(pv[n].sval),s);
            if (pv[n].sval)
              pv[n].ival = 1;
            break;
#endif /* PIPESEND */
          case SND_NAM:                 /* /FILENAMES */
            if (!getval) break;
            if ((x = cmkey(fntab,nfntab,"","automatic",xxstring)) < 0)
              goto xputx;
            debug(F101,"ftp put /filenames","",x);
            pv[n].ival = x;
            break;
          case SND_SMA:                 /* Smaller / larger than */
          case SND_LAR: {
	      CK_OFF_T y;
	      if (!getval) break;
	      if ((x = cmnumw("Size in bytes","0",10,&y,xxstring)) < 0)
		goto xputx;
	      pv[n].wval = y;
	      break;
	  }
          case SND_FIL:                 /* Name of file containing filenames */
            if (!getval) break;
            if ((x = cmifi("Name of file containing list of filenames",
                               "",&s,&y,xxstring)) < 0) {
                if (x == -3) {
                    printf("?Filename required\n");
                    x = -9;
                }
                goto xputx;
            } else if (y && iswild(s)) {
                printf("?Wildcards not allowed\n");
                x = -9;
                goto xputx;
            }
            if (s) if (!*s) s = NULL;
            makestr(&(pv[n].sval),s);
            if (pv[n].sval) {
                pv[n].ival = 1;
                pv[SND_ARR].ival = 0;
            } else {
                pv[n].ival = 0;
            }
            mput = 0;
            break;
          case SND_MOV:                 /* MOVE after */
          case SND_REN:                 /* RENAME after */
          case SND_SRN: {               /* SERVER-RENAME after */
              char * m = "";
              switch (n) {
                case SND_MOV:
                  m = "device and/or directory for source file after sending";
                  break;
                case SND_REN:
                  m = "new name for source file after sending";
                  break;
                case SND_SRN:
                  m = "new name for destination file after sending";
                  break;
              }
              if (!getval) break;
              if ((x = cmfld(m, "", &s, n == SND_MOV ? xxstring : NULL)) < 0) {
                  if (x == -3) {
                      printf("%s\n", n == SND_MOV ?
                             "?Destination required" :
                             "?New name required"
                             );
                      x = -9;
                  }
                  goto xputx;
              }
              if (s) if (!*s) s = NULL;
              makestr(&(pv[n].sval),s ? brstrip(s) : NULL);
              pv[n].ival = (pv[n].sval) ? 1 : 0;
              break;
          }
          case SND_STA:                 /* Starting position (= PSEND) */
            if (!getval) break;
            if ((x = cmnum("0-based position","0",10,&y,xxstring)) < 0)
              goto xputx;
            pv[n].ival = y;
            break;
          case SND_TYP:                 /* /TYPE */
            if (!getval) break;
            if ((x = cmkey(txtbin,3,"","all",xxstring)) < 0)
              goto xputx;
            pv[n].ival = (x == 2) ? -1 : x;
            break;
#ifndef NOCSETS
          case SND_CSL:                 /* Local character set */
          case SND_CSR:                 /* Remote (server) charset */
            if ((x = cmkey(fcstab,nfilc,"","",xxstring)) < 0) {
		return((x == -3) ? -2 : x);
            }
	    if (n == SND_CSL)
              x_csl = x;
            else
              x_csr = x;
            x_xla = 1;                  /* Overrides global OFF setting */
            break;
          case SND_XPA:                 /* Transparent */
            x_xla = 0;
            x_csr = -1;
            x_csl = -1;
            break;
#endif /* NOCSETS */
        }
    }
#ifdef PIPESEND
    if (pv[SND_RES].ival > 0) { /* /RECOVER */
        if (sndfilter || pv[SND_FLT].ival > 0) {
            printf("?Sorry, no /RECOVER or /START if SEND FILTER selected\n");
            x = -9;
            goto xputx;
        }
	if (sfttab[0] > 0 && sfttab[SFT_REST] == 0)
	  printf("WARNING: Server says it doesn't support REST.\n");
    }
#endif /* PIPESEND */
    cmarg = "";
    cmarg2 = asnambuf;
    line[0] = NUL;
    s = line;
    wild = 0;
    switch (cmresult.fcode) {           /* How did we get out of switch loop */
      case _CMIFI:                      /* Input filename */
        if (pv[SND_FIL].ival > 0) {
            printf("?You may not give a PUT filespec and a /LISTFILE\n");
            x = -9;
            goto xputx;
        }
        ckstrncpy(line,cmresult.sresult,LINBUFSIZ); /* Name */
        if (pv[SND_ARR].ival > 0)
          ckstrncpy(asnambuf,line,CKMAXPATH);
        else
          wild = cmresult.nresult;      /* Wild flag */
        debug(F111,"ftp put wild",line,wild);
        if (!wild && !recursive && !mput)
          nolinks = 0;
        break;
      case _CMFLD:                      /* Field */
        /* Only allowed with /COMMAND and /ARRAY */
        if (pv[SND_FIL].ival > 0) {
            printf("?You may not give a PUT filespec and a /LISTFILE\n");
            x = -9;
            goto xputx;
        }
	/* For MPUT it's OK to have filespecs that don't match any files */
	if (mput)
	  break;
        if (pv[SND_CMD].ival < 1 && pv[SND_ARR].ival < 1) {
#ifdef CKROOT
            if (ckrooterr)
              printf("?Off limits: %s\n",cmresult.sresult);
            else
#endif /* CKROOT */
              printf("?%s - \"%s\"\n",
                   iswild(cmresult.sresult) ?
                   "No files match" : "File not found",
                   cmresult.sresult
                   );
            x = -9;
            goto xputx;
        }
        ckstrncpy(line,cmresult.sresult,LINBUFSIZ);
        if (pv[SND_ARR].ival > 0)
          ckstrncpy(asnambuf,line,CKMAXPATH);
        break;
      case _CMCFM:                      /* Confirmation */
        confirmed = 1;
        break;
      default:
        printf("?Unexpected function code: %d\n",cmresult.fcode);
        x = -9;
        goto xputx;
    }
    debug(F110,"ftp put string",s,0);
    debug(F101,"ftp put confirmed","",confirmed);
    /* Save and change protocol and transfer mode */
    /* Global values are restored in main parse loop */
    g_displa = fdispla;
    if (ftp_dis > -1)
      fdispla = ftp_dis;
    g_skipbup = skipbup;
    if (pv[SND_NOB].ival > -1) {        /* /NOBACKUP (skip backup file) */
        g_skipbup = skipbup;
        skipbup = 1;
    }
    if (pv[SND_TYP].ival > -1) {        /* /TYPE */
        xfiletype = pv[SND_TYP].ival;
        if (xfiletype == 2)
          xfiletype = -1;
    }
    if (pv[SND_BIN].ival > 0) {         /* /BINARY really means binary... */
        forcetype = 1;                  /* So skip file scan */
        ftp_typ = FTT_BIN;              /* Set binary */
    } else if (pv[SND_TXT].ival > 0) {  /* Similarly for /TEXT... */
        forcetype = 1;
        ftp_typ = FTT_ASC;
    } else if (pv[SND_TEN].ival > 0) {  /* and /TENEX*/
        forcetype = 1;
        ftp_typ = FTT_TEN;
    } else if (ftp_cmdlin && ftp_xfermode == XMODE_M) {
        forcetype = 1;
        ftp_typ = binary;
        g_ftp_typ = binary;
    }
#ifdef PIPESEND
    if (pv[SND_CMD].ival > 0) {         /* /COMMAND - strip any braces */
        debug(F110,"PUT /COMMAND before stripping",s,0);
        s = brstrip(s);
        debug(F110,"PUT /COMMAND after stripping",s,0);
        if (!*s) {
            printf("?Sorry, a command to send from is required\n");
            x = -9;
            goto xputx;
        }
        cmarg = s;
    }
#endif /* PIPESEND */
/* Set up /MOVE and /RENAME */
    if (pv[SND_DEL].ival > 0 &&
        (pv[SND_MOV].ival > 0 || pv[SND_REN].ival > 0)) {
        printf("?Sorry, /DELETE conflicts with /MOVE or /RENAME\n");
        x = -9;
        goto xputx;
    }
#ifdef CK_TMPDIR
    if (pv[SND_MOV].ival > 0) {
        int len;
        char * p = pv[SND_MOV].sval;
        len = strlen(p);
        if (!isdir(p)) {                /* Check directory */
#ifdef CK_MKDIR
            char * s = NULL;
            s = (char *)malloc(len + 4);
            if (s) {
                strcpy(s,p);            /* safe */
#ifdef datageneral
                if (s[len-1] != ':') { s[len++] = ':'; s[len] = NUL; }
#else
                if (s[len-1] != '/') { s[len++] = '/'; s[len] = NUL; }
#endif /* datageneral */
                s[len++] = 'X';
                s[len] = NUL;
#ifdef NOMKDIR
                x = -1;
#else
                x = zmkdir(s);
#endif /* NOMKDIR */
                free(s);
                if (x < 0) {
                    printf("?Can't create \"%s\"\n",p);
                    x = -9;
                    goto xputx;
                }
            }
#else
            printf("?Directory \"%s\" not found\n",p);
            x = -9;
            goto xputx;
#endif /* CK_MKDIR */
        }
        makestr(&snd_move,p);
    }
#endif /* CK_TMPDIR */
    if (pv[SND_REN].ival > 0) {         /* /RENAME */
        char * p = pv[SND_REN].sval;
        if (!p) p = "";
        if (!*p) {
            printf("?New name required for /RENAME\n");
            x = -9;
            goto xputx;
        }
        p = brstrip(p);
#ifndef NOSPL
    /* If name given is wild, rename string must contain variables */
        if (wild) {
            char * s = tmpbuf;
            x = TMPBUFSIZ;
            zzstring(p,&s,&x);
            if (!strcmp(tmpbuf,p)) {
                printf(
    "?/RENAME for file group must contain variables such as \\v(filename)\n"
                       );
                x = -9;
                goto xputx;
            }
        }
#endif /* NOSPL */
        makestr(&snd_rename,p);
        debug(F110,"FTP snd_rename",snd_rename,0);
    }
    if (pv[SND_SRN].ival > 0) {         /* /SERVER-RENAME */
        char * p = pv[SND_SRN].sval;
        if (!p) p = "";
        if (!*p) {
            printf("?New name required for /SERVER-RENAME\n");
            x = -9;
            goto xputx;
        }
        p = brstrip(p);
#ifndef NOSPL
        if (wild) {
            char * s = tmpbuf;
            x = TMPBUFSIZ;
            zzstring(p,&s,&x);
            if (!strcmp(tmpbuf,p)) {
                printf(
"?/SERVER-RENAME for file group must contain variables such as \\v(filename)\n"
                       );
                x = -9;
                goto xputx;
            }
        }
#endif /* NOSPL */
        makestr(&srv_renam,p);
        debug(F110,"ftp put srv_renam",srv_renam,0);
    }
    if (!confirmed) {                   /* CR not typed yet, get more fields */
        char * lp;
        if (mput) {                     /* MPUT or MMOVE */
            nfils = 0;                  /* We already have the first one */
#ifndef NOMSEND
	    if (cmresult.fcode == _CMIFI) {
		/* First filespec is valid */
		msfiles[nfils++] = line;    /* Store pointer */
		lp = line + (int)strlen(line) + 1; /* Point past it */
		debug(F111,"ftp put mput",msfiles[nfils-1],nfils-1);
	    } else {
		/* First filespec matches no files */
		debug(F110,"ftp put mput skipping first filespec",
		      cmresult.sresult,
		      0
		      );
		lp = line;
	    }
	    /* Parse a filespec, a "field", or confirmation */
	    cmfdbi(&sf,			/* 1st FDB - file to send */
		   _CMIFI,		/* fcode */
		   "",			/* hlpmsg */
		   "",			/* default */
		   "",			/* addtl string data */
		   nolinks | x_recurse,	/* addtl numeric data 1 */
		   0,			/* dirflg 0 means "not dirs only" */
		   xxstring,
		   NULL,
		   &fl
		   );
	    cmfdbi(&fl,			/* 2nd FDB - local filespec */
		   _CMFLD,		/* fcode */
		   "",			/* hlpmsg */
		   "",			/* default */
		   "",			/* addtl string data */
		   0,			/* addtl numeric data 1 */
		   0,			/* addtl numeric data 2 */
		   xxstring,
		   NULL,
		   &cm
		   );
	    cmfdbi(&cm,			/* 3rd FDB - Confirmation */
		   _CMCFM,		/* fcode */
		   "",
		   "",
		   "",
		   0,
		   0,
		   NULL,
		   NULL,
		   NULL
		   );
            while (!confirmed) {	/* Get more filenames */
		x = cmfdb(&sf);		/* Parse something */
		debug(F101,"ftp put cmfdb B","",x);
		debug(F101,"ftp put fcode B","",cmresult.fcode);
		if (x < 0)		/* Error */
		  goto xputx;		/* or reparse needed */
		switch (cmresult.fcode) {
		  case _CMCFM:		/* End of command */
		    confirmed++;
		    if (nfils < 1) {
			debug(F100,"ftp put mput no files match","",0);
			printf("?No files match MPUT list\n");
			x = -9;
			goto xputx;
		    }
		    break;
		  case _CMFLD:		/* No match */
		    debug(F110,"ftp put mput skipping",cmresult.sresult,0);
		    continue;
		  case _CMIFI:		/* Good match */
		    s = cmresult.sresult;
		    msfiles[nfils++] = lp; /* Got one, count, point to it, */
		    p = lp;		   /* remember pointer, */
		    while ((*lp++ = *s++)) /* and copy it into buffer */
		      if (lp > (line + LINBUFSIZ)) { /* Avoid memory leak */
			  printf("?MPUT list too long\n");
			  line[0] = NUL;
			  x = -9;
			  goto xputx;
		      }
		    debug(F111,"ftp put mput adding",msfiles[nfils-1],nfils-1);
		    if (nfils == 1)	/* Take care of \v(filespec) */
		      fspec[0] = NUL;
#ifdef ZFNQFP
		    zfnqfp(p,TMPBUFSIZ,tmpbuf);
		    p = tmpbuf;
#endif /* ZFNQFP */
		    if (((int)strlen(fspec) + (int)strlen(p) + 1) < fspeclen) {
			strcat(fspec,p);    /* safe */
			strcat(fspec," ");  /* safe */
		    } else {
#ifdef COMMENT
			printf("WARNING - \\v(filespec) buffer overflow\n");
#else
			debug(F101,"doxput filespec buffer overflow","",0);
#endif /* COMMENT */
		    }
		}
	    }
#endif /* NOMSEND */
        } else {                        /* Regular PUT */
            nfils = -1;
            if ((x = cmtxt(wild ?
"\nOptional as-name template containing replacement variables \
like \\v(filename)" :
                           "Optional name to send it with",
                           "",&p,NULL)) < 0)
              goto xputx;
            if (p) if (!*p) p = NULL;
            p = brstrip(p);
            if (p && *p) {
                makestr(&(pv[SND_ASN].sval),p);
                if (pv[SND_ASN].sval)
                  pv[SND_ASN].ival = 1;
                debug(F110,"ftp put /as-name 2",pv[SND_ASN].sval,0);
            }
        }
    }
    /* Set cmarg2 from as-name, however we got it. */
    CHECKCONN();
    if (pv[SND_ASN].ival > 0 && pv[SND_ASN].sval && !asnambuf[0]) {
        char * p;
        p = brstrip(pv[SND_ASN].sval);
        ckstrncpy(asnambuf,p,CKMAXPATH+1);
    }
    debug(F110,"ftp put asnambuf",asnambuf,0);
    if (pv[SND_FIL].ival > 0) {
        if (confirmed) {
            if (zopeni(ZMFILE,pv[SND_FIL].sval) < 1) {
                debug(F110,"ftp put can't open",pv[SND_FIL].sval,0);
                printf("?Failure to open %s\n",pv[SND_FIL].sval);
                x = -9;
                goto xputx;
            }
            makestr(&filefile,pv[SND_FIL].sval); /* Open, remember name */
            debug(F110,"ftp PUT /LISTFILE opened",filefile,0);
            wild = 1;
        }
    }
    if (confirmed && !line[0] && !filefile) {
#ifndef NOMSEND
        if (filehead) {                 /* OK if we have a SEND-LIST */
            nfils = filesinlist;
            sndsrc = nfils;             /* Like MSEND */
            addlist = 1;                /* But using a different list... */
            filenext = filehead;
            goto doput;
        }
#endif /* NOMSEND */
        printf("?Filename required but not given\n");
        x = -9;
        goto xputx;
    }
#ifndef NOMSEND
    addlist = 0;                        /* Don't use SEND-LIST. */
#endif /* NOMSEND */
    if (mput) {                         /* MPUT (rather than PUT) */
#ifndef NOMSEND
        cmlist = msfiles;               /* List of filespecs */
        sndsrc = nfils;                 /* rather filespec and as-name */
#endif /* NOMSEND */
        pipesend = 0;
    } else if (filefile) {              /* File contains list of filenames */
        s = "";
        cmarg = "";
        line[0] = NUL;
        nfils = 1;
        sndsrc = 1;
    } else if (pv[SND_ARR].ival < 1 && pv[SND_CMD].ival < 1) {
        /* Not MSEND, MMOVE, /LIST, or /ARRAY */
        nfils = sndsrc = -1;
        if (!wild) {
            if (zchki(s) < 0) {
                printf("?Read access denied - \"%s\"\n", s);
                x = -9;
                goto xputx;
            }
        }
        if (s != line)                  /* We might already have done this. */
          ckstrncpy(line,s,LINBUFSIZ);  /* Copy of string just parsed. */
#ifdef DEBUG
        else
          debug(F110,"doxput line=s",line,0);
#endif /* DEBUG */
        cmarg = line;                   /* File to send */
    }
#ifndef NOMSEND
    zfnqfp(cmarg,fspeclen,fspec);       /* Get full name */
#endif /* NOMSEND */
    if (!mput) {                        /* For all but MPUT... */
#ifdef PIPESEND
        if (pv[SND_CMD].ival > 0)       /* /COMMAND sets pipesend flag */
          pipesend = 1;
        debug(F101,"ftp put /COMMAND pipesend","",pipesend);
        if (pipesend && filefile) {
            printf("?Invalid switch combination\n");
            x = -9;
            goto xputx;
        }
#endif /* PIPESEND */
#ifndef NOSPL
    /* If as-name given and filespec is wild, as-name must contain variables */
        if ((wild || mput) && asnambuf[0]) {
            char * s = tmpbuf;
            x = TMPBUFSIZ;
            zzstring(asnambuf,&s,&x);
            if (!strcmp(tmpbuf,asnambuf)) {
                printf(
    "?As-name for file group must contain variables such as \\v(filename)\n"
                       );
                x = -9;
                goto xputx;
            }
        }
#endif /* NOSPL */
    }
  doput:
    if (pv[SND_SHH].ival > 0) {         /* SEND /QUIET... */
        fdispla = 0;
        debug(F101,"ftp put display","",fdispla);
    } else {
        displa = 1;
        if (ftp_deb)
	  fdispla = XYFD_B;
    }
#ifdef PUTARRAY                         /* SEND /ARRAY... */
    if (pv[SND_ARR].ival > 0) {
        if (!ap) { x = -2; goto xputx; } /* (shouldn't happen) */
        if (range[0] == -1)             /* If low end of range not specified */
          range[0] = 1;                 /* default to 1 */
        if (range[1] == -1)             /* If high not specified */
          range[1] = a_dim[arrayx];     /* default to size of array */
        if ((range[0] < 0) ||           /* Check range */
            (range[0] > a_dim[arrayx]) ||
            (range[1] < range[0]) ||
            (range[1] > a_dim[arrayx])) {
            printf("?Bad array range - [%d:%d]\n",range[0],range[1]);
            x = -9;
            goto xputx;
        }
        sndarray = ap;                  /* Array pointer */
        sndxin = arrayx;                /* Array index */
        sndxlo = range[0];              /* Array range */
        sndxhi = range[1];
        sndxnam[7] = (char)((sndxin == 1) ? 64 : sndxin + ARRAYBASE);
        if (!asnambuf[0])
          ckstrncpy(asnambuf,sndxnam,CKMAXPATH);
        cmarg = "";
    }
#endif /* PUTARRAY */
    moving = 0;
    if (pv[SND_ARR].ival < 1) {         /* File selection & disposition... */
        if (pv[SND_DEL].ival > 0)       /* /DELETE was specified */
          moving = 1;
        if (pv[SND_AFT].ival > 0)       /* Copy SEND criteria */
          ckstrncpy(sndafter,pv[SND_AFT].sval,19);
        if (pv[SND_BEF].ival > 0)
          ckstrncpy(sndbefore,pv[SND_BEF].sval,19);
        if (pv[SND_NAF].ival > 0)
          ckstrncpy(sndnafter,pv[SND_NAF].sval,19);
        if (pv[SND_NBE].ival > 0)
          ckstrncpy(sndnbefore,pv[SND_NBE].sval,19);
        if (pv[SND_EXC].ival > 0)
          makelist(pv[SND_EXC].sval,sndexcept,NSNDEXCEPT);
        if (pv[SND_SMA].ival > -1)
          sndsmaller = pv[SND_SMA].wval;
        if (pv[SND_LAR].ival > -1)
          sndlarger = pv[SND_LAR].wval;
        if (pv[SND_NAM].ival > -1)
          x_cnv = pv[SND_NAM].ival;
        if (pv[SND_USN].ival > -1)
          x_usn = pv[SND_USN].ival;
        if (pv[SND_ERR].ival > -1)
          puterror = pv[SND_ERR].ival;
#ifdef DOUPDATE
        if (pv[SND_UPD].ival > 0) {
            if (x_usn) {
                printf("?Conflicting switches: /UPDATE /UNIQUE\n");
                x = -9;
                goto xputx;
            }
            putflags |= PUT_UPD;
	    ftp_dates |= 2;
        }
#ifdef COMMENT
	/* This works but it's useless, maybe dangerous */
        if (pv[SND_DIF].ival > 0) {
            if (x_usn) {
                printf("?Conflicting switches: /DATES-DIFFER /UNIQUE\n");
                x = -9;
                goto xputx;
            }
            putflags |= PUT_DIF;
	    ftp_dates |= 2;
        }
#endif /* COMMENT */
#endif /* DOUPDATE */
        if (pv[SND_SIM].ival > 0)
          putflags |= PUT_SIM;
        if (pv[SND_PRM].ival > -1) {
#ifdef UNIX
            if (x_usn) {
                printf("?Conflicting switches: /PERMISSIONS /UNIQUE\n");
                x = -9;
                goto xputx;
            }
            x_prm = pv[SND_PRM].ival;
#else /* UNIX */
            printf("?/PERMISSIONS switch is not supported\n");
#endif /* UNIX */
        }
#ifdef FTP_RESTART
        if (pv[SND_RES].ival > 0) {
	    if (!sizeok) {
		printf("?PUT /RESTART can't be used because SIZE disabled.\n");
                x = -9;
                goto xputx;
	    }
            if (x_usn || putflags) {
                printf("?Conflicting switches: /RECOVER %s\n",
                       x_usn && putflags ? "/UNIQUE /UPDATE" :
                       (x_usn ? "/UNIQUE" : "/UPDATE")
                       );
                x = -9;
                goto xputx;
            }
#ifndef NOCSETS
            if (x_xla &&
                (x_csl == FC_UCS2 ||
                 x_csl == FC_UTF8 ||
                 x_csr == FC_UCS2 ||
                 x_csr == FC_UTF8)) {
                printf("?/RECOVER can not be used with Unicode translation\n");
                x = -9;
                goto xputx;
            }
#endif /* NOCSETS */
            putflags = PUT_RES;
        }
#endif /* FTP_RESTART */
    }
    debug(F101,"ftp PUT restart","",putflags & PUT_RES);
    debug(F101,"ftp PUT update","",putflags & PUT_UPD);
#ifdef PIPESEND
    if (pv[SND_FLT].ival > 0) {         /* Have SEND FILTER? */
        if (!pv[SND_FLT].sval) {
            sndfilter = NULL;
        } else {
            sndfilter = (char *) malloc((int) strlen(pv[SND_FLT].sval) + 1);
            if (sndfilter) strcpy(sndfilter,pv[SND_FLT].sval); /* safe */
        }
        debug(F110,"ftp put /FILTER", sndfilter, 0);
    }
    if (sndfilter || pipesend)          /* No /UPDATE or /RESTART */
      if (putflags)                     /* with pipes or filters */
        putflags = 0;
#endif /* PIPESEND */
    tfc = (CK_OFF_T)0;			/* Initialize stats and counters */
    filcnt = 0;
    pktnum = 0;
    spackets = 0L;
    if (wild)                           /* (is this necessary?) */
      cx = FTP_MPU;
    t0 = gmstimer();                    /* Record starting time */
    done = 0;                           /* Loop control */
    cancelgroup = 0;
    cdlevel = 0;
    cdsimlvl = 0;
    while (!done && !cancelgroup) {     /* Loop for all files */
                                        /* or until canceled. */
#ifdef FTP_PROXY
        /*
           If we are using a proxy, we don't use the local file list;
           instead we use the list on the remote machine which we want
           sent to someone else, and we use remglob() to get the names.
           But in that case we shouldn't even be executing this routine;
           see ftp_mput().
        */
#endif /* FTP_PROXY */
        cancelfile = 0;
        x = gnfile();                   /* Get next file from list(s) */
        if (x == 0)                     /* (see gnfile() comments...) */
          x = gnferror;
        debug(F111,"FTP PUT gnfile",filnam,x);
        debug(F111,"FTP PUT binary",filnam,binary);
        switch (x) {
          case 1:                       /* File to send */
            s2 = asnambuf;
#ifndef NOSPL
            if (asnambuf[0]) {          /* As-name */
                int n; char *p;         /* to be evaluated... */
                n = TMPBUFSIZ;
                p = tmpbuf;
                zzstring(asnambuf,&p,&n);
                s2 = tmpbuf;
                debug(F110,"ftp put asname",s2,0);
            }
#endif /* NOSPL */
            rc = putfile(cx,            /* Function (PUT, APPEND) */
                    filnam, s2,         /* Name to send, as-name */
                    forcetype, moving,  /* Parameters from switches... */
                    snd_move, snd_rename, srv_renam,
                    x_cnv, x_usn, xfiletype, x_prm,
#ifndef NOCSETS
                    x_csl, (!x_xla ? -1 : x_csr),
#else
                    -1, -1,
#endif /* NOCSETS */
                    putflags
                    );
            debug(F111,"ftp put putfile rc",filnam,rc);
            debug(F111,"ftp put putfile cancelfile",filnam,cancelfile);
            debug(F111,"ftp put putfile cancelgroup",filnam,cancelgroup);
            if (rc > -1) {
                good++;
                status = 1;
            }
            if (cancelfile)
              continue;
            if (rc < 0) {
                ftp_fai++;
                if (puterror) {
                    status = 0;
                    printf("?Fatal upload error: %s\n",filnam);
                    done++;
                }
            }
            continue;
          case 0:                       /* No more files, done */
            done++;
            continue;
          case -1:
            printf("?%s: file not found - \"%s\"\n",
                   puterror ? "Fatal" : "Warning",
                   filnam
                   );
            if (puterror) {
                status = 0;
                done++;
                break;
            }
            continue;
          case -2:
            if (puterror) {
                printf("?Fatal: file not found - \"%s\"\n", filnam);
                status = 0;
                done++;
                break;
            }
            continue;                   /* Not readable, keep going */
          case -3:
            if (puterror) {
                printf("?Fatal: Read access denied - \"%s\"\n", filnam);
                status = 0;
                done++;
                break;
            }
            printf("?Warning access denied - \"%s\"\n", filnam);
            continue;
#ifdef COMMENT
          case -4:                      /* Canceled */
            done++;
            break;
#endif /* COMMENT */
          case -5:
            printf("?Too many files match\n");
            done++;
            break;
          case -6:
            if (good < 1)
              printf("?No files selected\n");
            done++;
            break;
          default:
            printf("?getnextfile() - unknown failure\n");
            done++;
        }
    }
    if (cdlevel > 0) {
        while (cdlevel--) {
            if (cdsimlvl) {
                cdsimlvl--;
            } else if (!doftpcdup())
              break;
        }
    }
    if (status > 0) {
        if (cancelgroup)
          status = 0;
        else if (cancelfile && good < 1)
          status = 0;
    }
    success = status;
    x = success;
  xputx:
    if (x > -1) {
#ifdef GFTIMER
        t1 = gmstimer();                /* End time */
        sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
        if (!sec) sec = 0.001;
        fptsecs = sec;
#else
        sec = (t1 - t0) / 1000;
        if (!sec) sec = 1;
#endif /* GFTIMER */
        tfcps = (long) (tfc / sec);
        tsecs = (int)sec;
        lastxfer = W_FTP|W_SEND;
        xferstat = success;
        if (dpyactive)
	  ftscreen(status > 0 ? SCR_TC : SCR_CW, 0, (CK_OFF_T)0, "");
    }
    for (i = 0; i <= SND_MAX; i++) {    /* Free malloc'd memory */
        if (pv[i].sval)
          free(pv[i].sval);
    }
    ftreset();                          /* Undo switch effects */
    dpyactive = 0;
    return(x);
}
static char ** mgetlist = NULL;         /* For MGET */
static int mgetn = 0, mgetx = 0;
static char xtmpbuf[4096];
/*
  c m d l i n g e t
  Get files specified by -g command-line option.
  File list is set up in cmlist[] by ckuusy.c; nfils is length of list.
*/
int
cmdlinget(stay) int stay; {
    int i, x, rc = 0, done = 0, good = 0, status = 0, append = 0;
    int lcs = -1, rcs = -1, xlate = 0;
    int first = 1;
    int mget = 1;
    int nc;
    char * s, * s2, * s3;
    ULONG t0, t1;                       /* Times for stats */
#ifdef GFTIMER
    CKFLOAT sec;
#else
    int sec = 0;
#endif /* GFTIMER */
    if (quiet) {                        /* -q really means quiet */
        displa = 0;
        fdispla = 0;
    } else {
        displa = 1;
        fdispla = XYFD_B;
    }
    testing = 0;
    dpyactive = 0;
    out2screen = 0;
    what = W_FTP|W_RECV;
    mgetmethod = 0;
    mgetforced = 0;
    havetype = 0;
    havesize = (CK_OFF_T)-1;
    makestr(&havemdtm,NULL);
    if (ftp_fnc < 0)
      ftp_fnc = fncact;
#ifndef NOSPL
    cmd_quoting = 0;
#endif /* NOSPL */
    debug(F101,"ftp cmdlinget nfils","",nfils);
    if (ftp_cnv == CNV_AUTO) {          /* Name conversion is auto */
        if (alike) {                    /* If server & client are alike */
            nc = 0;                     /* no conversion */
        } else {                        /* If they are different */
            if (servertype == SYS_UNIX || servertype == SYS_WIN32)
              nc = -1;                  /* only minimal conversions needed */
            else                        /* otherwise */
              nc = 1;                   /* full conversion */
        }
    } else                              /* Not auto - do what user said */
      nc = ftp_cnv;
    if (nfils < 1)
      doexit(BAD_EXIT,-1);
    t0 = gmstimer();                    /* Starting time for this batch */
#ifndef NOCSETS
    if (xlate) {                        /* SET FTP CHARACTER-SET-TRANSLATION */
        lcs = ftp_csl;                  /* Local charset */
        if (lcs < 0) lcs = fcharset;
        if (lcs < 0) xlate = 0;
    }
    if (xlate) {                        /* Still ON? */
        rcs = ftp_csx;                  /* Remote (Server) charset */
        if (rcs < 0) rcs = ftp_csr;
        if (rcs < 0) xlate = 0;
    }
#endif /* NOCSETS */
    /*
      If we have only one file and it is a directory, then we ask for a
      listing of its contents, rather than retrieving the directory file
      itself.  This is what (e.g.) Netscape does.
    */
    if (nfils == 1) {
        if (doftpcwd((char *)cmlist[mgetx],-1)) {
            /* If we can CD to it, it must be a directory */
            if (recursive) {
                cmlist[mgetx] = "*";
            } else {
                status =
                  (recvrequest("LIST","-","","wb",0,0,NULL,xlate,lcs,rcs)==0);
                done = 1;
            }
        }
    }
/*
  The following is to work around UNIX servers which, when given a command
  like "NLST path/blah" (not wild) returns the basename without the path.
*/
    if (!done && servertype == SYS_UNIX && nfils == 1) {
        mget = iswild(cmlist[mgetx]);
    }
    if (!mget && !done) {               /* Invoked by command-line FTP URL */
        if (ftp_deb)
          printf("DOING GET...\n");
        done++;
        cancelfile = 0;                 /* This file not canceled yet */
        s = cmlist[mgetx];
        rc = 0;                         /* Initial return code */
	fsize = (CK_OFF_T)-1;
	if (sizeok) {
	    x = ftpcmd("SIZE",s,lcs,rcs,ftp_vbm); /* Get remote file's size */
	    if (x == REPLY_COMPLETE)
	      fsize = ckatofs(&ftp_reply_str[4]);
	}
        ckstrncpy(filnam,s,CKMAXPATH);  /* For \v(filename) */
        debug(F111,"ftp cmdlinget filnam",filnam,fsize);
        nzrtol(s,tmpbuf,nc,0,CKMAXPATH); /* Strip path and maybe convert */
        s2 = tmpbuf;
        /* If local file already exists, take collision action */
        if (zchki(s2) > -1) {
            switch (ftp_fnc) {
              case XYFX_A:              /* Append */
                append = 1;
                break;
              case XYFX_R:              /* Rename */
              case XYFX_B: {            /* Backup */
                  char * p = NULL;
                  int x = -1;
                  znewn(s2,&p);         /* Make unique name */
                  debug(F110,"ftp cmdlinget znewn",p,0);
                  if (ftp_fnc == XYFX_B) { /* Backup existing file */
                      x = zrename(s2,p);
                      debug(F111,"ftp cmdlinget backup zrename",p,x);
                  } else {              /* Rename incoming file */
                      x = ckstrncpy(tmpbuf,p,CKMAXPATH+1);
                      s2 = tmpbuf;
                      debug(F111,"ftp cmdlinget rename incoming",p,x);
                  }
                  if (x < 0) {
                      printf("?Backup/Rename failed\n");
                      return(success = 0);
                  }
                  break;
              }
              case XYFX_D:              /* Discard */
                ftscreen(SCR_FN,'F',(CK_OFF_T)0,s);
                ftscreen(SCR_ST,ST_SKIP,(CK_OFF_T)SKP_NAM,s);
                tlog(F100," refused: name","",0);
                debug(F110,"ftp cmdlinget skip name",s2,0);
                goto xclget;
              case XYFX_X:              /* Overwrite */
              case XYFX_U:              /* Update (already handled above) */
	      case XYFX_M:		/* ditto */
                break;
            }
        }
        rc = getfile(s,                 /* Remote name */
                     s2,                /* Local name */
                     0,                 /* Recover/Restart */
                     append,            /* Append */
                     NULL,              /* Pipename */
                     0,                 /* Translate charsets */
                     -1,                /* File charset (none) */
                     -1                 /* Server charset (none) */
                     );
        debug(F111,"ftp cmdlinget rc",s,rc);
        debug(F111,"ftp cmdlinget cancelfile",s,cancelfile);
        debug(F111,"ftp cmdlinget cancelgroup",s,cancelgroup);
        if (rc < 0 && haveurl && s[0] == '/') /* URL failed - try again */
            rc = getfile(&s[1],         /* Remote name without leading '/' */
                         s2,            /* Local name */
                         0,             /* Recover/Restart */
                         append,        /* Append */
                         NULL,          /* Pipename */
                         0,             /* Translate charsets */
                         -1,            /* File charset (none) */
                         -1             /* Server charset (none) */
                         );
        if (rc > -1) {
            good++;
            status = 1;
        }
        if (cancelfile)
          goto xclget;
        if (rc < 0) {
            ftp_fai++;
#ifdef FTP_TIMEOUT
	    if (ftp_timed_out)
	      status = 0;
#endif	/* FTP_TIMEOUT */
            if (geterror) {
                status = 0;
                done++;
            }
        }
    }
    if (ftp_deb && !done)
      printf("DOING MGET...\n");
    while (!done && !cancelgroup) {
        cancelfile = 0;                 /* This file not canceled yet */
        s = (char *)remote_files(first,(CHAR *)cmlist[mgetx],NULL,0);
        if (!s) s = "";
        if (!*s) {
            first = 1;
            mgetx++;
            if (mgetx < nfils)
              s = (char *)remote_files(first,(CHAR *)cmlist[mgetx],NULL,0);
            else
              s = NULL;
            debug(F111,"ftp cmdlinget remote_files B",s,0);
            if (!s) {
                done = 1;
                break;
            }
        }
        /*
          The semantics of NLST are ill-defined.  Suppose we have just sent
          NLST /path/[a-z]*.  Most servers send back names like /path/foo,
          /path/bar, etc.  But some send back only foo and bar, and subsequent
          RETR commands based on the pathless names are not going to work.
        */
        if (servertype == SYS_UNIX && !ckstrchr(s,'/')) {
            if ((s3 = ckstrrchr(cmlist[mgetx],'/'))) {
                int len, left = 4096;
                char * tmp = xtmpbuf;
                len = s3 - cmlist[mgetx] + 1;
                ckstrncpy(tmp,cmlist[mgetx],left);
                tmp += len;
                left -= len;
                ckstrncpy(tmp,s,left);
                s = xtmpbuf;
		debug(F111,"ftp cmdlinget remote_files X",s,0);
            }
        }
        first = 0;                      /* Not first any more */
	debug(F111,"ftp cmdlinget havetype",s,havetype);
	if (havetype > 0 && havetype != FTYP_FILE) { /* Server says not file */
	    debug(F110,"ftp cmdlinget not-a-file",s,0);
	    continue;
	}
        rc = 0;                         /* Initial return code */
	if (havesize > (CK_OFF_T)-1) {	/* Already have file size? */
	    fsize = havesize;
	} else {			/* No - must ask server */
	    /*
	      Prior to sending the NLST command we necessarily put the
	      server into ASCII mode.  We must now put it back into the
	      the requested mode so the upcoming SIZE command returns
	      right kind of size; this is especially important for
	      GET /RECOVER; otherwise the server returns the "ASCII" size
	      of the file, rather than its true size.
	    */
	    changetype(ftp_typ,0);	/* Change to requested type */
	    fsize = (CK_OFF_T)-1;
	    if (sizeok) {
		x = ftpcmd("SIZE",s,lcs,rcs,ftp_vbm);
		if (x == REPLY_COMPLETE)
		  fsize = ckatofs(&ftp_reply_str[4]);
	    }
	}
        ckstrncpy(filnam,s,CKMAXPATH);  /* For \v(filename) */
        debug(F111,"ftp cmdlinget filnam",filnam,fsize);
        nzrtol(s,tmpbuf,nc,0,CKMAXPATH); /* Strip path and maybe convert */
        s2 = tmpbuf;
        /* If local file already exists, take collision action */
        if (zchki(s2) > -1) {
            switch (ftp_fnc) {
              case XYFX_A:              /* Append */
                append = 1;
                break;
              case XYFX_R:              /* Rename */
              case XYFX_B: {            /* Backup */
                  char * p = NULL;
                  int x = -1;
                  znewn(s2,&p);         /* Make unique name */
                  debug(F110,"ftp cmdlinget znewn",p,0);
                  if (ftp_fnc == XYFX_B) { /* Backup existing file */
                      x = zrename(s2,p);
                      debug(F111,"ftp cmdlinget backup zrename",p,x);
                  } else {              /* Rename incoming file */
                      x = ckstrncpy(tmpbuf,p,CKMAXPATH+1);
                      s2 = tmpbuf;
                      debug(F111,"ftp cmdlinget rename incoming",p,x);
                  }
                  if (x < 0) {
                      printf("?Backup/Rename failed\n");
                      return(success = 0);
                  }
                  break;
              }
              case XYFX_D:      /* Discard */
                ftscreen(SCR_FN,'F',(CK_OFF_T)0,s);
                ftscreen(SCR_ST,ST_SKIP,(CK_OFF_T)SKP_NAM,s);
                tlog(F100," refused: name","",0);
                debug(F110,"ftp cmdlinget skip name",s2,0);
                continue;
              case XYFX_X:              /* Overwrite */
              case XYFX_U:              /* Update (already handled above) */
              case XYFX_M:              /* ditto */
                break;
            }
        }
                                        /* ^^^ ADD CHARSET STUFF HERE ^^^ */
        rc = getfile(s,                 /* Remote name */
                     s2,                /* Local name */
                     0,                 /* Recover/Restart */
                     append,            /* Append */
                     NULL,              /* Pipename */
                     0,                 /* Translate charsets */
                     -1,                /* File charset (none) */
                     -1                 /* Server charset (none) */
                     );
        debug(F111,"ftp cmdlinget rc",s,rc);
        debug(F111,"ftp cmdlinget cancelfile",s,cancelfile);
        debug(F111,"ftp cmdlinget cancelgroup",s,cancelgroup);
        if (rc > -1) {
            good++;
            status = 1;
        }
        if (cancelfile)
          continue;
        if (rc < 0) {
            ftp_fai++;
#ifdef FTP_TIMEOUT
	    if (ftp_timed_out)
	      status = 0;
#endif	/* FTP_TIMEOUT */
            if (geterror) {
                status = 0;
                done++;
            }
        }
    }
  xclget:
    if (cancelgroup)
      mlsreset();
    if (status > 0) {
        if (cancelgroup)
          status = 0;
        else if (cancelfile && good < 1)
          status = 0;
    }
    success = status;
#ifdef GFTIMER
    t1 = gmstimer();                    /* End time */
    sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
    if (!sec) sec = 0.001;
    fptsecs = sec;
#else
    sec = (t1 - t0) / 1000;
    if (!sec) sec = 1;
#endif /* GFTIMER */
    tfcps = (long) (tfc / sec);
    tsecs = (int)sec;
    lastxfer = W_FTP|W_RECV;
    xferstat = success;
    if (dpyactive)
      ftscreen(status > 0 ? SCR_TC : SCR_CW, 0, (CK_OFF_T)0, "");
    if (!stay)
      doexit(success ? GOOD_EXIT : BAD_EXIT, -1);
    return(success);
}
/*  d o f t p g e t  --  Parse and execute GET, MGET, MDELETE, ...  */
/*
  Note: if we wanted to implement /AFTER:, /BEFORE:, etc, we could use
  zstrdat() to convert to UTC-based time_t.  But it doesn't make sense from
  the user-interface perspective, since the server's directory listings show
  its own local times and since we don't know what timezone it's in, there's
  no way to reconcile our local times with the server's.
*/
int
doftpget(cx,who) int cx, who; {         /* who == 1 for ftp, 0 for kermit */
    struct FDB fl, sw, cm;
    int i, n, rc, getval = 0, mget = 0, done = 0, pipesave = 0;
    int x_cnv = 0, x_prm = 0, restart = 0, status = 0, good = 0;
    int x_fnc = 0, first = 0, skipthis = 0, append = 0, selected = 0;
    int renaming = 0, mdel = 0, listfile = 0, updating = 0, getone = 0;
    int moving = 0, deleting = 0, toscreen = 0, haspath = 0;
    int gotsize = 0;
    int matchdot = 0;
    CK_OFF_T getlarger = (CK_OFF_T)-1;
    CK_OFF_T getsmaller = (CK_OFF_T)-1;
    char * msg, * s, * s2, * nam, * pipename = NULL, * pn = NULL;
    char * src = "", * local = "";
    char * pat = "";
    int x_csl = -1, x_csr = -1;         /* Local and remote charsets */
    int x_xla = 0;
    char c;                             /* Worker char */
    ULONG t0 = 0L, t1;                  /* Times for stats */
#ifdef GFTIMER
    CKFLOAT sec;
#else
    int sec = 0;
#endif /* GFTIMER */
    struct stringint pv[SND_MAX+1];    /* Temporary array for switch values */
    success = 0;                        /* Assume failure */
    forcetype = 0;                      /* No /TEXT or /BINARY given yet */
    restart = 0;                        /* No restart yet */
    out2screen = 0;			/* No TO-SCREEN switch given yet */
    mgetmethod = 0;			/* No NLST or MLSD switch yet */
    mgetforced = 0;
    g_displa = fdispla;
    if (ftp_dis > -1)
      fdispla = ftp_dis;
    x_cnv = ftp_cnv;                    /* Filename conversion */
    if (x_cnv == CNV_AUTO) {		/* Name conversion is auto */
        if (alike) {                    /* If server & client are alike */
            x_cnv = 0;			/* no conversion */
        } else {                        /* If they are different */
            if (servertype == SYS_UNIX || servertype == SYS_WIN32)
              x_cnv = -1;		/* only minimal conversions needed */
            else                        /* otherwise */
              x_cnv = 1;		/* full conversion */
        }
    } else                              /* Not auto - do what user said */
      x_cnv = ftp_cnv;
    x_prm = ftp_prm;                    /* Permissions */
    if (x_prm == SET_AUTO)              /* Permissions AUTO */
      x_prm = alike;
#ifndef NOCSETS
    x_csr = ftp_csr;                    /* Inherit global server charset */
    x_csl = ftp_csl;                    /* Inherit global local charset */
    if (x_csl < 0)                      /* If none, use current */
      x_csl = fcharset;                 /* file character-set. */
    x_xla = ftp_xla;                    /* Translation On/Off */
#endif /* NOCSETS */
    geterror = ftp_err;                 /* Inherit global error action. */
    asnambuf[0] = NUL;                  /* No as-name yet. */
    pipesave = pipesend;
    pipesend = 0;
    havetype = 0;
    havesize = (CK_OFF_T)-1;
    makestr(&havemdtm,NULL);
    if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
        ftp_typ = g_ftp_typ;
        /* g_ftp_typ = -1; */
    }
    for (i = 0; i <= SND_MAX; i++) {    /* Initialize switch values */
        pv[i].sval = NULL;              /* to null pointers */
        pv[i].ival = -1;                /* and -1 int values */
        pv[i].wval = (CK_OFF_T)-1;	/* and -1 wide values */
    }
    zclose(ZMFILE);                     /* In case it was left open */
    x_fnc = ftp_fnc > -1 ? ftp_fnc : fncact; /* Filename collision action */
    if (fp_nml) {                       /* Reset /NAMELIST */
        if (fp_nml != stdout)
          fclose(fp_nml);
        fp_nml = NULL;
    }
    makestr(&ftp_nml,NULL);
    /* Initialize list of remote filespecs */
    if (!mgetlist) {
        mgetlist = (char **)malloc(MGETMAX * sizeof(char *));
        if (!mgetlist) {
            printf("?Memory allocation failure - MGET list\n");
            return(-9);
        }
        for (i = 0; i < MGETMAX; i++)
          mgetlist[i] = NULL;
    }
    mgetn = 0;                          /* Number of mget arguments */
    mgetx = 0;                          /* Current arg */
    if (who == 0) {                     /* Called with unprefixed command */
        if (cx == XXGET || cx == XXREGET || cx == XXRETR)
          getone++;
        switch (cx) {
          case XXREGET: pv[SND_RES].ival = 1; break;
          case XXRETR:  pv[SND_DEL].ival = 1; break;
          case XXGET:
          case XXMGET:  mget++; break;
        }
    } else {                            /* FTP command */
        if (cx == FTP_GET || cx == FTP_RGE)
          getone++;
        switch (cx) {
          case FTP_DEL:                 /* (fall thru on purpose) */
          case FTP_MDE: mdel++;         /* (ditto) */
          case FTP_GET:                 /* (ditto) */
          case FTP_MGE: mget++; break;
          case FTP_RGE: pv[SND_RES].ival = 1; break;
        }
    }
    cmfdbi(&sw,                         /* First FDB - command switches */
           _CMKEY,                      /* fcode */
           "Remote filename;\n or switch", /* hlpmsg */
           "",                          /* default */
           "",                          /* addtl string data */
           mdel ? ndelswi : ngetswi,    /* addtl numeric data 1: tbl size */
           4,                           /* addtl numeric data 2: 4 = cmswi */
           xxstring,                    /* Processing function */
           mdel ? delswi : getswi,      /* Keyword table */
           &fl                          /* Pointer to next FDB */
           );
    cmfdbi(&fl,                         /* 2nd FDB - remote filename */
           _CMFLD,                      /* fcode */
           "",                          /* hlpmsg */
           "",                          /* default */
           "",                          /* addtl string data */
           0,                           /* addtl numeric data 1 */
           0,                           /* addtl numeric data 2 */
           xxstring,
           NULL,
           &cm
           );
    cmfdbi(&cm,                         /* 3rd FDB - Confirmation */
           _CMCFM,                      /* fcode */
           "",                          /* hlpmsg */
           "",                          /* default */
           "",                          /* addtl string data */
           0,                           /* addtl numeric data 1 */
           0,                           /* addtl numeric data 2 */
           NULL,
           NULL,
           NULL
           );
    while (1) {                         /* Parse 0 or more switches */
        x = cmfdb(&sw);                 /* Parse something */
        debug(F101,"ftp get cmfdb","",x);
        if (x < 0)                      /* Error */
          goto xgetx;                   /* or reparse needed */
        if (cmresult.fcode != _CMKEY)   /* Break out of loop if not a switch */
          break;
        c = cmgbrk();                   /* Get break character */
        getval = (c == ':' || c == '='); /* to see how they ended the switch */
        if (getval && !(cmresult.kflags & CM_ARG)) {
            printf("?This switch does not take arguments\n");
            x = -9;
            goto xgetx;
        }
        n = cmresult.nresult;           /* Numeric result = switch value */
        debug(F101,"ftp get switch","",n);
        if (!getval && (cmgkwflgs() & CM_ARG)) {
            printf("?This switch requires an argument\n");
            x = -9;
            goto xgetx;
        }
        switch (n) {                    /* Process the switch */
          case SND_ASN:                 /* /AS-NAME: */
            debug(F101,"ftp get /as-name getval","",getval);
            if (!getval) break;
            if ((x = cmfld("Name to store it under","",&s,NULL)) < 0) {
                if (x == -3) {
                    printf("?name required\n");
                    x = -9;
                }
                goto xgetx;
            }
            s = brstrip(s);
            if (!*s) s = NULL;
            makestr(&(pv[n].sval),s);
            pv[n].ival = 1;
            break;
          case SND_BIN:                 /* /BINARY */
          case SND_TXT:                 /* /TEXT or /ASCII */
          case SND_TEN:                 /* /TENEX */
            pv[SND_BIN].ival = 0;
            pv[SND_TXT].ival = 0;
            pv[SND_TEN].ival = 0;
            pv[n].ival = 1;
            break;
#ifdef PUTPIPE
          case SND_CMD:                 /* These take no args */
            if (nopush) {
                printf("?Sorry, system command access is disabled\n");
                x = -9;
                goto xgetx;
            }
#ifdef PIPESEND
            else if (rcvfilter) {
                printf("?Sorry, no PUT /COMMAND when SEND FILTER selected\n");
                x = -9;
                goto xgetx;
            }
#endif /* PIPESEND */
            sw.hlpmsg = "Command, or switch"; /* Change help message */
            pv[n].ival = 1;             /* Just set the flag */
            pv[SND_ARR].ival = 0;
            break;
#endif /* PUTPIPE */
          case SND_SHH:                 /* /QUIET */
          case SND_RES:                 /* /RECOVER (reget) */
          case SND_NOB:                 /* /NOBACKUPFILES */
          case SND_DEL:                 /* /DELETE */
          case SND_UPD:                 /* /UPDATE */
          case SND_USN:                 /* /UNIQUE */
          case SND_NOD:                 /* /NODOTFILES */
          case SND_REC:                 /* /RECOVER */
          case SND_MAI:                 /* /TO-SCREEN */
            pv[n].ival = 1;             /* Just set the flag */
            break;
          case SND_DIF:                 /* /DATES-DIFFER */
	    pv[SND_COL].ival = XYFX_M;	/* Now it's a collision option */
	    pv[n].ival = 1;
	    break;
          case SND_COL:                 /* /COLLISION: */
            if ((x = cmkey(ftpcolxtab,nftpcolx,"","",xxstring)) < 0)
              goto xgetx;
	    if (x == XYFX_M)
	      pv[SND_DIF].ival = 1;	/* (phase this out) */
	    pv[n].ival = x;		/* this should be sufficient */
            break;
          case SND_ERR:                 /* /ERROR-ACTION */
            if ((x = cmkey(qorp,2,"","",xxstring)) < 0)
              goto xgetx;
            pv[n].ival = x;
            break;
          case SND_EXC:                 /* Exception list */
            if (!getval) break;
            if ((x = cmfld("Pattern","",&s,xxstring)) < 0) {
                if (x == -3) {
                    printf("?Pattern required\n");
                    x = -9;
                }
                goto xgetx;
            }
            if (s) if (!*s) s = NULL;
            makestr(&(pv[n].sval),s);
            if (pv[n].sval)
              pv[n].ival = 1;
            break;
#ifdef PIPESEND
          case SND_FLT:
            debug(F101,"ftp get /filter getval","",getval);
            if (!getval) break;
            if ((x = cmfld("Filter program to send through","",&s,NULL)) < 0) {
                if (x == -3)
                  s = "";
                else
                  goto xgetx;
            }
            s = brstrip(s);
            if (pv[SND_MAI].ival < 1) {
                y = strlen(s);
                /* Make sure they included "\v(...)" */
                for (x = 0; x < y; x++) {
                    if (s[x] != '\\') continue;
                    if (s[x+1] == 'v') break;
                }
                if (x == y) {
                    printf(
                "?Filter must contain a replacement variable for filename.\n"
                           );
                    x = -9;
                    goto xgetx;
                }
            }
            if (*s) {
                pv[n].ival = 1;
                makestr(&(pv[n].sval),s);
            } else {
                pv[n].ival = 0;
                makestr(&(pv[n].sval),NULL);
            }
            break;
#endif /* PIPESEND */
          case SND_NAM:
            if (!getval) break;
            if ((x = cmkey(fntab,nfntab,"","automatic",xxstring)) < 0)
              goto xgetx;
            debug(F101,"ftp get /filenames","",x);
            pv[n].ival = x;
            break;
          case SND_SMA:                 /* Smaller / larger than */
          case SND_LAR: {
	      CK_OFF_T y;
	      if (!getval) break;
	      if ((x = cmnumw("Size in bytes","0",10,&y,xxstring)) < 0)
		goto xgetx;
	      pv[n].wval = y;
	      break;
	  }
          case SND_FIL:                 /* Name of file containing filnames */
            if (!getval) break;
            if ((x = cmifi("Name of file containing list of filenames",
                               "",&s,&y,xxstring)) < 0) {
                if (x == -3) {
                    printf("?Filename required\n");
                    x = -9;
                }
                goto xgetx;
            } else if (y && iswild(s)) {
                printf("?Wildcards not allowed BBB\n");
                x = -9;
                goto xgetx;
            }
            if (s) if (!*s) s = NULL;
            makestr(&(pv[n].sval),s);
            if (pv[n].sval)
              pv[n].ival = 1;
            break;
          case SND_MOV:                 /* MOVE after */
          case SND_REN:                 /* RENAME after */
          case SND_SRN: {               /* SERVER-RENAME */
              char * m = "";
              switch (n) {
                case SND_MOV:
                  m =
                   "Device and/or directory for incoming file after reception";
                  break;
                case SND_REN:
                  m = "New name for incoming file after reception";
                  break;
                case SND_SRN:
                  m = "New name for source file on server after reception";
                  break;
              }
              if (!getval) break;
              if ((x = cmfld(m, "", &s, n == SND_MOV ? xxstring : NULL)) < 0) {
                  if (x == -3) {
                      printf("%s\n", n == SND_MOV ?
                             "?Destination required" :
                             "?New name required"
                             );
                      x = -9;
                  }
                  goto xgetx;
              }
              makestr(&(pv[n].sval),*s ? brstrip(s) : NULL);
              pv[n].ival = (pv[n].sval) ? 1 : 0;
              break;
          }
#ifndef NOCSETS
          case SND_CSL:                 /* Local character set */
          case SND_CSR:                 /* Remote (server) charset */
            if ((x = cmkey(fcstab,nfilc,"","",xxstring)) < 0)
              return((x == -3) ? -2 : x);
            if (n == SND_CSL)
              x_csl = x;
            else
              x_csr = x;
            x_xla = 1;                  /* Overrides global OFF setting */
            break;
          case SND_XPA:                 /* Transparent */
            x_xla =  0;
            x_csr = -1;
            x_csl = -1;
            break;
#endif /* NOCSETS */
          case SND_NML:
            if ((x = cmofi("Local filename","-",&s,xxstring)) < 0)
              goto xgetx;
            makestr(&ftp_nml,s);
            break;
	  case SND_PAT:			/* /PATTERN: */
	    if (!getval) break;
	    if ((x = cmfld("Pattern","*", &s, xxstring)) < 0)
	      goto xgetx;
	    makestr(&(pv[n].sval),*s ? brstrip(s) : NULL);
	    pv[n].ival = (pv[n].sval) ? 1 : 0;
	    break;
	  case SND_NLS:			/* /NLST */
            pv[n].ival = 1;		/* Use NLST */
	    pv[SND_MLS].ival = 0;	/* Don't use MLSD */
	    break;
	  case SND_MLS:			/* /MLSD */
            pv[n].ival = 1;		/* Use MLSD */
	    pv[SND_NLS].ival = 0;	/* Don't use NLST */
	    break;
          default:                      /* /AFTER, /PERMISSIONS, etc... */
            printf("?Sorry, \"%s\" works only with [M]PUT\n",atmbuf);
            x = -9;
            goto xgetx;
        }
    }
    line[0] = NUL;
    cmarg = line;
    cmarg2 = asnambuf;
    s = line;
/*
  For GET, we want to parse an optional as-name, like with PUT.
  For MGET, we must parse a list of names, and then send NLST or MLSD
  commands for each name separately.
*/
    switch (cmresult.fcode) {           /* How did we get out of switch loop */
      case _CMFLD:                      /* Field */
        if (!getone) {
            s = brstrip(cmresult.sresult);
            makestr(&(mgetlist[mgetn++]),s);
            while ((x = cmfld("Remote filename","",&s,xxstring)) != -3) {
                if (x < 0)
                  goto xgetx;
                makestr(&(mgetlist[mgetn++]),brstrip(s));
                if (mgetn >= MGETMAX) {
                    printf("?Too many items in MGET list\n");
                    goto xgetx;
                }
            }
            if ((x = cmcfm()) < 0)
              goto xgetx;
        } else {
            s = brstrip(cmresult.sresult);
            ckstrncpy(line,s,LINBUFSIZ);
            if ((x = cmfld("Name to store it under","",&s,xxstring)) < 0)
              if (x != -3)
                goto xgetx;
            s = brstrip(s);
            ckstrncpy(asnambuf,s,CKMAXPATH+1);
            if ((x = cmcfm()) < 0)
              goto xgetx;
        }
        break;
      case _CMCFM:                      /* Confirmation */
        break;
      default:
        printf("?Unexpected function code: %d\n",cmresult.fcode);
        x = -9;
        goto xgetx;
    }
    if (pv[SND_REC].ival > 0)           /* /RECURSIVE */
      recursive = 2;
    if (pv[SND_BIN].ival > 0) {         /* /BINARY really means binary... */
        forcetype = 1;                  /* So skip the name-pattern match */
        ftp_typ = XYFT_B;               /* Set binary */
    } else if (pv[SND_TXT].ival > 0) {  /* Similarly for /TEXT... */
        forcetype = 1;
        ftp_typ = XYFT_T;
    } else if (pv[SND_TEN].ival > 0) {  /* and /TENEX*/
        forcetype = 1;
        ftp_typ = FTT_TEN;
    } else if (ftp_cmdlin && ftp_xfermode == XMODE_M) {
        forcetype = 1;
        ftp_typ = binary;
        g_ftp_typ = binary;
    }
    if (pv[SND_ASN].ival > 0 && pv[SND_ASN].sval && !asnambuf[0]) {
        char * p;
        p = brstrip(pv[SND_ASN].sval);  /* As-name */
        ckstrncpy(asnambuf,p,CKMAXPATH+1);
    }
    debug(F110,"ftp get asnambuf",asnambuf,0);
#ifdef PIPESEND
    if (pv[SND_CMD].ival > 0) {         /* /COMMAND - strip any braces */
        char * p;
        p = asnambuf;
        debug(F110,"GET /COMMAND before stripping",p,0);
        p = brstrip(p);
        debug(F110,"GET /COMMAND after stripping",p,0);
        if (!*p) {
            printf("?Sorry, a command to write to is required\n");
            x = -9;
            goto xgetx;
        }
        pipename = p;
        pipesend = 1;
    }
#endif /* PIPESEND */
/* Set up /MOVE and /RENAME */
#ifdef COMMENT
    /* Conflict exists only for PUT - removed 13 Mar 2006 - fdc */
    if (pv[SND_DEL].ival > 0 &&
        (pv[SND_MOV].ival > 0 || pv[SND_REN].ival > 0)) {
        printf("?Sorry, /DELETE conflicts with /MOVE or /RENAME\n");
        x = -9;
        goto xgetx;
    }
#endif	/* COMMENT */
#ifdef CK_TMPDIR
    if (pv[SND_MOV].ival > 0 && pv[SND_MOV].sval) {
        int len;
        char * p = pv[SND_MOV].sval;
        len = strlen(p);
        if (!isdir(p)) {                /* Check directory */
#ifdef CK_MKDIR
            char * s = NULL;
            s = (char *)malloc(len + 4);
            if (s) {
                strcpy(s,p);            /* safe */
#ifdef datageneral
                if (s[len-1] != ':') { s[len++] = ':'; s[len] = NUL; }
#else
                if (s[len-1] != '/') { s[len++] = '/'; s[len] = NUL; }
#endif /* datageneral */
                s[len++] = 'X';
                s[len] = NUL;
#ifdef NOMKDIR
                x = -1;
#else
                x = zmkdir(s);
#endif /* NOMKDIR */
                free(s);
                if (x < 0) {
                    printf("?Can't create \"%s\"\n",p);
                    x = -9;
                    goto xgetx;
                }
            }
#else
            printf("?Directory \"%s\" not found\n",p);
            x = -9;
            goto xgetx;
#endif /* CK_MKDIR */
        }
        makestr(&rcv_move,p);
        moving = 1;
    }
#endif /* CK_TMPDIR */
    if (pv[SND_REN].ival > 0) {         /* /RENAME */
        char * p = pv[SND_REN].sval;
        if (!p) p = "";
        if (!*p) {
            printf("?New name required for /RENAME\n");
            x = -9;
            goto xgetx;
        }
        p = brstrip(p);
#ifndef NOSPL
    /* If name given is wild, rename string must contain variables */
        if (mget && !getone) {
            char * s = tmpbuf;
            x = TMPBUFSIZ;
            zzstring(p,&s,&x);
            if (!strcmp(tmpbuf,p)) {
                printf(
    "?/RENAME for file group must contain variables such as \\v(filename)\n"
                       );
                x = -9;
                goto xgetx;
            }
        }
#endif /* NOSPL */
        renaming = 1;
        makestr(&rcv_rename,p);
        debug(F110,"FTP rcv_rename",rcv_rename,0);
    }
    if (!cmarg[0] && mgetn == 0 && getone && pv[SND_FIL].ival < 1) {
        printf("?Filename required but not given\n");
        x = -9;
        goto xgetx;
    } else if ((cmarg[0] || mgetn > 0) && pv[SND_FIL].ival > 0) {
        printf("?You can't give both /LISTFILE and a remote filename\n");
        x = -9;
        goto xgetx;
    }
    CHECKCONN();                        /* Check connection */
    if (pv[SND_COL].ival > -1)
      x_fnc = pv[SND_COL].ival;
#ifndef NOSPL
    /* If as-name given for MGET, as-name must contain variables */
    if (mget && !getone && asnambuf[0] && x_fnc != XYFX_A) {
        char * s = tmpbuf;
        x = TMPBUFSIZ;
        zzstring(asnambuf,&s,&x);
        if (!strcmp(tmpbuf,asnambuf)) {
            printf(
    "?As-name for MGET must contain variables such as \\v(filename)\n"
                   );
            x = -9;
            goto xgetx;
        }
    }
#endif /* NOSPL */
/* doget: */
    if (pv[SND_SHH].ival > 0 || ftp_nml) { /* GET /QUIET... */
        fdispla = 0;
    } else {
        displa = 1;
        if (mdel || ftp_deb)
	  fdispla = XYFD_B;
    }
    deleting = 0;
    if (pv[SND_DEL].ival > 0)           /* /DELETE was specified */
      deleting = 1;
    if (pv[SND_EXC].ival > 0)
      makelist(pv[SND_EXC].sval,rcvexcept,NSNDEXCEPT);
    if (pv[SND_SMA].wval > -1)
      getsmaller = pv[SND_SMA].wval;
    if (pv[SND_LAR].wval > -1)
      getlarger = pv[SND_LAR].wval;
    if (pv[SND_NAM].ival > -1)
      x_cnv = pv[SND_NAM].ival;
    if (pv[SND_ERR].ival > -1)
      geterror = pv[SND_ERR].ival;
    if (pv[SND_MAI].ival > -1)
      toscreen = 1;
    if (pv[SND_NLS].ival > 0) {		/* Force NLST or MLSD? */
	mgetmethod = SND_NLS;
	mgetforced = 1;
    } else if (pv[SND_MLS].ival > 0) {
	mgetmethod = SND_MLS;
	mgetforced = 1;
    }
#ifdef FTP_RESTART
    if (pv[SND_RES].ival > 0) {
        if (!ftp_typ) {
            printf("?Sorry, GET /RECOVER requires binary mode\n");
            x = -9;
            goto xgetx;
#ifdef COMMENT
        /* Not true - the fact that the initial REST fails does not mean */
        /* it will fail here.  */
        } else if (!okrestart) {
            printf("WARNING: Server might not support restart...\n");
#endif /* COMMENT */
        }
        restart = 1;
    }
#endif /* FTP_RESTART */
#ifdef PIPESEND
    if (pv[SND_FLT].ival > 0) {         /* Have SEND FILTER? */
        if (pipesend) {
            printf("?Switch conflict: /FILTER and /COMMAND\n");
            x = -9;
            goto xgetx;
        }
        makestr(&rcvfilter,pv[SND_FLT].sval);
        debug(F110,"ftp get /FILTER", rcvfilter, 0);
    }
    if (rcvfilter || pipesend) {        /* /RESTART */
#ifdef FTP_RESTART
        if (restart) {                  /* with pipes or filters */
            printf("?Switch conflict: /FILTER or /COMMAND and /RECOVER\n");
            x = -9;
            goto xgetx;
        }
#endif /* FTP_RESTART */
        if (pv[SND_UPD].ival > 0 || x_fnc == XYFX_M || x_fnc == XYFX_U) {
            printf(
		"?Switch conflict: /FILTER or /COMMAND and Date Checking\n");
            x = -9;
            goto xgetx;
        }
    }
#endif /* PIPESEND */
    tfc = (CK_OFF_T)0;			/* Initialize stats and counters */
    filcnt = 0;
    pktnum = 0;
    rpackets = 0L;
    if (pv[SND_FIL].ival > 0) {
        if (zopeni(ZMFILE,pv[SND_FIL].sval) < 1) {
            debug(F111,"ftp get can't open listfile",pv[SND_FIL].sval,errno);
            printf("?Failure to open listfile - \"%s\"\n",pv[SND_FIL].sval);
            x = -9;
            goto xgetx;
        }
        if (zsinl(ZMFILE,tmpbuf,CKMAXPATH) < 0) { /* Read a line */
            zclose(ZMFILE);                       /* Failed */
            debug(F110,"ftp get listfile EOF",pv[SND_FIL].sval,0);
            printf("?Empty listfile - \"%s\"\n",pv[SND_FIL].sval);
            x = -9;
            goto xgetx;
        }
        listfile = 1;
        debug(F110,"ftp get listfile first",tmpbuf,0);
        makestr(&(mgetlist[0]),tmpbuf);
    }
    t0 = gmstimer();                    /* Record starting time */
    updating = 0;			/* Checking dates? */
    if (pv[SND_UPD].ival > 0 || (!mdel && x_fnc == XYFX_U))
      updating = 1;
    if (pv[SND_DIF].ival > 0 || x_fnc == XYFX_M)
      updating = 2;
    if (updating)			/* These switches force FTP DATES ON */
      ftp_dates |= 2;
    what = mdel ? W_FTP|W_FT_DELE : W_RECV|W_FTP; /* What we're doing */
    cancelgroup = 0;                    /* Group not canceled yet */
    if (!(ftp_xfermode == XMODE_A && patterns && get_auto && !forcetype))
      changetype(ftp_typ,0);		/* Change to requested type */
    binary = ftp_typ;                   /* For file-transfer display */
    first = 1;                          /* For MGET list */
    done = 0;                           /* Loop control */
#ifdef CK_TMPDIR
    if (dldir && !f_tmpdir) {           /* If they have a download directory */
        if ((s = zgtdir())) {           /* Get current directory */
            if (zchdir(dldir)) {        /* Change to download directory */
                ckstrncpy(savdir,s,TMPDIRLEN);
                f_tmpdir = 1;           /* Remember that we did this */
            }
        }
    }
#endif /* CK_TMPDIR */
    if (ftp_nml) {                      /* /NAMELIST */
        debug(F110,"ftp GET ftp_nml",ftp_nml,0);
        if (ftp_nml[0] == '-' && ftp_nml[1] == 0)
          fp_nml = stdout;
        else
          fp_nml = fopen(ftp_nml, "wb");
        if (!fp_nml) {
            printf("?%s: %s\n",ftp_nml,ck_errstr());
            goto xgetx;
        }
    }
    while (!done && !cancelgroup) {     /* Loop for all files */
                                        /* or until canceled. */
#ifdef FTP_PROXY
        /* do something here if proxy */
#endif /* FTP_PROXY */
        rs_len = (CK_OFF_T)0;		/* REGET position */
        cancelfile = 0;                 /* This file not canceled yet */
        haspath = 0;                    /* Recalculate this each time thru */
        if (getone) {                   /* GET */
            char * p;
            s = line;
            src = line;                 /* Server name */
            done = 1;
            debug(F111,"ftp get file",s,0);
        } else if (mget) {              /* MGET */
            src = mgetlist[mgetx];
            debug(F111,"ftp mget remote_files A",src,first);
            s = (char *)remote_files(first,
				     (CHAR *)mgetlist[mgetx],
				     (CHAR *)pv[SND_PAT].sval,
				     0
				     );
            debug(F110,"ftp mget remote_files B",s,0);
            if (!s) s = "";
            if (!*s) {
                first = 1;
                if (listfile) {		/* Names from listfile */
                  again:
                    tmpbuf[0] = NUL;
                    while (!tmpbuf[0]) {
                        if (zsinl(ZMFILE,tmpbuf,CKMAXPATH) < 0) {
                            zclose(ZMFILE);
                            debug(F110,"ftp get listfile EOF",
                                  pv[SND_FIL].sval,0);
                            makestr(&(mgetlist[0]),NULL);
                            s = NULL;
                            done = 1;
                            break;
                        }
                    }
                    if (done)
                      continue;
                    makestr(&(mgetlist[0]),tmpbuf);
		    debug(F110,"ftp get listfile next",tmpbuf,0);
                    s = (char *)remote_files(first,
					     (CHAR *)mgetlist[0],
					     (CHAR *)pv[SND_PAT].sval,
					     0
					     );
		    debug(F110,"ftp mget remote_files C",s,0);
                    if (!s) {
                        ftscreen(SCR_FN,'F',(CK_OFF_T)0,s);
                        ftscreen(SCR_ST,ST_MSG,(CK_OFF_T)0,"File not found");
                        tlog(F110,"ftp get file not found:",s,0);
                        goto again;
                    }
                } else {		/* Names from command line */
                    mgetx++;
                    if (mgetx < mgetn)
		      s = (char *)remote_files(first,
					       (CHAR *)mgetlist[mgetx],
					       (CHAR *)pv[SND_PAT].sval,
					       0
					       );
                    else
		      s = NULL;
		    if (!s) mgetx++;
		    debug(F111,"ftp mget remote_files D",s,mgetx);
                }
                if (!s) {
		    if (!first || mgetx >= mgetn) {
			done = 1;
			break;
		    } else if (geterror) {
			status = 0;
			done = 1;
			break;
		    } else {
			continue;
		    }
                }
            }
        }
	debug(F111,"ftp mget remote_files E",s,0);
        /*
          The semantics of NLST are ill-defined.  Suppose we have just sent
          NLST /path/[a-z]*.  Most servers send back names like /path/foo,
          /path/bar, etc.  But some send back only foo and bar, and subsequent
          RETR commands based on the pathless names are not going to work.
        */
        if (servertype == SYS_UNIX && !ckstrchr(s,'/')) {
            char * s3;
            if ((s3 = ckstrrchr(mgetlist[mgetx],'/'))) {
                int len, left = 4096;
                char * tmp = xtmpbuf;
                len = s3 - mgetlist[mgetx] + 1;
                ckstrncpy(tmp,mgetlist[mgetx],left);
                tmp += len;
                left -= len;
                ckstrncpy(tmp,s,left);
                s = xtmpbuf;
		debug(F111,"ftp mget remote_files F",s,0);
            }
        }
        first = 0;
        skipthis = 0;                   /* File selection... */
        msg = "";
        nam = s;                        /* Filename (without path) */
        rc = 0;                         /* Initial return code */
        s2 = "";
        if (!getone && !skipthis) {     /* For MGET and MDELETE... */
            char c, * p = s;
            int srvpath = 0;
            int usrpath = 0;
            int i, k = 0;
	    debug(F111,"ftp mget havetype",s,havetype);
	    if (havetype > 0 && havetype != FTYP_FILE) {
		/* Server says it's not file... */
		debug(F110,"ftp mget not-a-file",s,0);
		continue;
	    }
/*
  Explanation: Some ftp servers (such as wu-ftpd) return a recursive list.
  But if the client did not ask for a recursive list, we have to ignore any
  server files that include a pathname that extends beyond any path that
  was included in the user's request.
  User's filespec is blah or path/blah (or other non-UNIX syntax).  We need to
  get the user's path segment.  Then, for each incoming file, if it begins
  with the same path segment, we must strip it (point past it).
*/
            src = mgetlist[mgetx];      /* In case it moved! */
	    if (src) {
		for (i = 0; src[i]; i++) { /* Find rightmost path separator */
		    if (ispathsep(src[i])) /* in user's pathname */
		      k = i + 1;
		}
	    } else {
		src = "";
	    }
            usrpath = k;                /* User path segment length */
            debug(F111,"ftp get usrpath",src,usrpath);
            p = s;                      /* Server filename */
            while ((c = *p++)) {        /* Look for path in server filename */
                if (ispathsep(c)) {
		    /* haspath++; */
                    nam = p;            /* Pathless name (for ckmatch) */
                    srvpath = p - s;    /* Server path segment length */
                }
            }
            debug(F111,"ftp get srvpath",s,srvpath);
	    if (usrpath == 0) {
/*
  Here we handle the case where the user said "mget foo" where foo is a
  directory name, and the server is sending back names like "foo/file1",
  "foo/file2", etc.  This is a nasty trick but it's necessary because the
  user can't compensate by typing "mget foo/" because then the server is
  likely to send back "foo//file1, foo//file2" etc, and we still won't
  get a match...
*/
		int srclen = 0, srvlen = 0;
		if (src) srclen = strlen(src);
		if (s) srvlen = strlen(s);
		if (src && (srvlen > srclen)) {
		    if (!strncmp(src,s,srclen) && ispathsep(s[srclen])) {
			char * tmpsrc = NULL;
			tmpsrc = (char *)malloc(srclen + 2);
			strncpy(tmpsrc,src,srclen);
			tmpsrc[srclen] = s[srclen];
			tmpsrc[srclen+1] = NUL;
			free(mgetlist[mgetx]);
			mgetlist[mgetx] = tmpsrc;
			tmpsrc = NULL;
			src = mgetlist[mgetx];
			usrpath = srclen+1;
		    }			      
		}
	    }
/*
  If as-name not given and server filename includes path that matches
  the pathname from the user's file specification, we must trim the common
  path prefix from the server's name when constructing the local name.
*/
            if (src &&			/* Wed Sep 25 17:27:48 2002 */
		!asnambuf[0] &&
		!recursive &&		/* Thu Sep 19 16:11:59 2002 */
		(srvpath > 0) &&
		!strncmp(src,s,usrpath)) {
                s2 = s + usrpath;       /* Local name skips past remote path */
            }
#ifdef COMMENT
	    /* This doesn't work if the path prefix contains wildcards! */
	    haspath = (srvpath > usrpath);
#else
	    {				/* Count path segments instead */
		int x1 = 0, x2 = 0;
		char *p;
		for (p = s; *p; p++)
		  if (ispathsep(*p)) x1++;
		for (p = src; *p; p++) {
		    if (ispathsep(*p)) x2++;
		}
		haspath = recursive ? x1 || x2 : x1 > x2;
		debug(F111,"ftp get server path segments",s,x1);
		debug(F111,"ftp get user   path segments",src,x2);
	    }
#endif /* COMMENT */
            debug(F111,"ftp get haspath",s+usrpath,haspath);
            if (haspath) {              /* Server file has path segments? */
                if (!recursive) {       /* [M]GET /RECURSIVE? */
/*
  We did not ask for a recursive listing, but the server is sending us one
  anyway (as wu-ftpd is wont to do).  We get here if the current filename
  includes a path segment beyond any path segment we asked for in our
  non-recursive [M]GET command.  We MUST skip this file.
*/
                    debug(F111,"ftp get skipping because of path",s,0);
                    continue;
                }
            }
        } else if (getone && !skipthis) { /* GET (not MGET) */
            char * p = nam;
	    while ((c = *p++)) {	/* Handle path in local name */
		if (ispathsep(c)) {
		    if (recursive) {	/* If recursive, keep it */
			haspath = 1;
			break;
		    } else {		/* Otherwise lose it. */
		      nam = p;
		    }
		}
            }
            s2 = nam;
        }
        if (!*nam)                      /* Name without path */
          nam = s;
        if (!skipthis && pv[SND_NOD].ival > 0) { /* /NODOTFILES */
            if (nam[0] == '.')
	      continue;
        }
        if (!skipthis && rcvexcept[0]) { /* /EXCEPT: list */
	    int xx;
            for (i = 0; i < NSNDEXCEPT; i++) {
                if (!rcvexcept[i]) {
                    break;
                }
		xx = ckmatch(rcvexcept[i], nam, servertype == SYS_UNIX, 1);
		debug(F111,"ftp mget /except match",rcvexcept[i],xx);
                if (xx) {
                    tlog(F100," refused: exception list","",0);
		    msg = "Refused: Exception List";
                    skipthis++;
                    break;
                }
            }
        }
        if (!skipthis && pv[SND_NOB].ival > 0) { /* /NOBACKUPFILES */
            if (ckmatch(
#ifdef CKREGEX
                        "*.~[0-9]*~"
#else
                        "*.~*~"
#endif /* CKREGEX */
                        ,nam,0,1) > 0)
              continue;
        }
        if (!x_xla) {                   /* If translation is off */
            x_csl = -2;                 /* unset the charsets */
            x_csr = -2;
        }
        ckstrncpy(filnam,s,CKMAXPATH);  /* For \v(filename) */
        if (!*s2)                       /* Local name */
          s2 = asnambuf;                /* As-name */
	if (!*s2)			/* Sat Nov 16 19:19:39 2002 */
	  s2 = recursive ? s : nam;	/* Fri Jan 10 13:15:19 2003 */
        debug(F110,"ftp get filnam  ",s,0);
        debug(F110,"ftp get asname A",s2,0);
        /* Receiving to real file */
        if (!pipesend &&
#ifdef PIPESEND
            !rcvfilter &&
#endif /* PIPESEND */
            !toscreen) {
#ifndef NOSPL
            /* Do this here so we can decide whether to skip */
            if (cmd_quoting && !skipthis && asnambuf[0]) {
                int n; char *p;
                n = TMPBUFSIZ;
                p = tmpbuf;
                zzstring(asnambuf,&p,&n);
                s2 = tmpbuf;
                debug(F111,"ftp get asname B",s2,updating);
            }
#endif /* NOSPL */
	    local = *s2 ? s2 : s;
	    if (!skipthis && x_fnc == XYFX_D) { /* File Collision = Discard */
		CK_OFF_T x;
		x = zchki(local);
		debug(F111,"ftp get DISCARD zchki",local,x);
		if (x > -1) {
		    skipthis++;
		    debug(F110,"ftp get skip name",local,0);
		    tlog(F100," refused: name","",0);
		    msg = "Refused: Name";
		}
	    }
#ifdef DOUPDATE
            if (!skipthis && updating) { /* If updating and not yet skipping */
                if (zchki(local) > -1) {
                    x = chkmodtime(local,s,0);
#ifdef DEBUG
		    if (deblog) {
			if (updating == 2)
			  debug(F111,"ftp get /dates-diff chkmodtime",local,x);
			else
			  debug(F111,"ftp get /update chkmodtime",local,x);
		    }
#endif /* DEBUG */
		    if ((updating == 1 && x > 0) ||  /* /UPDATE */
			(updating == 2 && x == 1)) { /* /DATES-DIFFER */
			skipthis++;
			tlog(F100," refused: date","",0);
			msg = "Refused: Date";
                        debug(F110,"ftp get skip date",local,0);
                    }
                }
            }
#endif /* DOUPDATE */
        }
        /* Initialize file size to -1 in case server doesn't understand */
        /* SIZE command, so xxscreen() will know we don't know the size */
        fsize = (CK_OFF_T)-1;
	/* Ask for size now only if we need it for selection */
	/* because if you're going thru a list 100,000 files to select */
	/* a small subset, 100,000 SIZE commands can take hours... */
	gotsize = 0;
        if (!mdel && !skipthis &&        /* Don't need size for DELE... */
	    (getsmaller >= (CK_OFF_T)0  || getlarger >= (CK_OFF_T)0)) {
	    if (havesize >= (CK_OFF_T)0) { /* Already have file size? */
		fsize = havesize;
		gotsize = 1;
	    } else {			/* No - must ask server */
		/*
		  Prior to sending the NLST command we necessarily put the
		  server into ASCII mode.  We must now put it back into the
		  the requested mode so the upcoming SIZE command returns
		  right kind of size; this is especially important for
		  GET /RECOVER; otherwise the server returns the "ASCII" size
		  of the file, rather than its true size.
		*/
		changetype(ftp_typ,0);	/* Change to requested type */
		fsize = (CK_OFF_T)-1;
		if (sizeok) {
		    x = ftpcmd("SIZE",s,x_csl,x_csr,ftp_vbm);
		    if (x == REPLY_COMPLETE) {
			fsize = ckatofs(&ftp_reply_str[4]);
			gotsize = 1;
		    }
		}
	    }
            if (gotsize) {
                if (getsmaller >= (CK_OFF_T)0 && fsize >= getsmaller)
                  skipthis++;
                if (getlarger >= (CK_OFF_T)0 && fsize <= getlarger)
                  skipthis++;
                if (skipthis) {
                    debug(F111,"ftp get skip size",s,fsize);
                    tlog(F100," refused: size","",0);
                    msg = "Refused: Size";
                }
#ifdef COMMENT
            } else if (getone) {
                /* SIZE can fail for many reasons.  Does the file exist? */
                x = ftpcmd("NLST",s,x_csl,x_csr,ftp_vbm);
                if (x != REPLY_COMPLETE) {
                    printf(">>> FILE NOT FOUND: %s\n",s);
                    break;
                }
#endif /* COMMENT */
            }
        }
        if (skipthis) {                 /* Skipping this file? */
            ftscreen(SCR_FN,'F',(CK_OFF_T)0,s);
            if (msg)
              ftscreen(SCR_ST,ST_ERR,(CK_OFF_T)0,msg);
            else
              ftscreen(SCR_ST,ST_SKIP,(CK_OFF_T)0,s);
            continue;
        }
        if (fp_nml) {                   /* /NAMELIST only - no transfer */
            fprintf(fp_nml,"%s\n",s);
            continue;
        }
        if (recursive && haspath && !pipesend
#ifdef PIPESEND
            && !rcvfilter
#endif /* PIPESEND */
            ) {
	    int x;
#ifdef NOMKDIR
	    x = -1;
#else
            x = zmkdir(s);		/* Try to make the directory */
#endif /* NOMKDIR */
            if (x < 0) {
                rc = -1;                /* Failure is fatal */
                if (geterror) {
                    status = 0;
                    ftscreen(SCR_EM,0,(CK_OFF_T)0,
			     "Directory creation failure");
                    break;
                }
            }
        }
        /* Not skipping */
	selected++;			/* Count this file as selected */
        pn = NULL;
	if (!gotsize && !mdel) {	/* Didn't get size yet */
	    if (havesize > (CK_OFF_T)-1) { /* Already have file size? */
		fsize = havesize;
		gotsize = 1;
	    } else {			/* No - must ask server */
		fsize = (CK_OFF_T)-1;
		if (sizeok) {
		    x = ftpcmd("SIZE",s,x_csl,x_csr,ftp_vbm);
		    if (x == REPLY_COMPLETE) {
			fsize = ckatofs(&ftp_reply_str[4]);
			gotsize = 1;
		    }
		}
	    }
	}
        if (mdel) {                     /* [M]DELETE */
            if (displa && !ftp_vbm)
              printf(" %s...",s);
            rc =
             (ftpcmd("DELE",s,x_csl,x_csr,ftp_vbm) == REPLY_COMPLETE) ? 1 : -1;
            if (rc > -1) {
                tlog(F110,"ftp mdelete",s,0);
                if (displa && !ftp_vbm)
                  printf("OK\n");
            } else {
                tlog(F110,"ftp mdelete failed:",s,0);
                if (displa)
                  printf("Failed\n");
            }
#ifndef NOSPL
#ifdef PIPESEND
        } else if (rcvfilter) {         /* [M]GET with filter */
            int n; char * p;
            n = CKMAXPATH;
            p = tmpbuf;                 /* Safe - no asname with filter */
            zzstring(rcvfilter,&p,&n);
            if (n > -1)
              pn = tmpbuf;
            debug(F111,"ftp get rcvfilter",pn,n);
#endif /* PIPESEND */
#endif /* NOSPL */
            if (toscreen) s2 = "-";
        } else if (pipesend) {          /* [M]GET /COMMAND */
            int n; char * p;
            n = CKMAXPATH;
            p = tmpbuf;                 /* Safe - no asname with filter */
            zzstring(pipename,&p,&n);
            if (n > -1)
              pn = tmpbuf;
            debug(F111,"ftp get pipename",pipename,n);
            if (toscreen) s2 = "-";
        } else {                        /* [M]GET with no pipes or filters */
            debug(F111,"ftp get s2 A",s2,x_cnv);
            if (toscreen) {
                s2 = "-";               /* (hokey convention for stdout) */
            } else if (!*s2) {          /* No asname? */
                if (x_cnv) {            /* If converting */
                    nzrtol(s,tmpbuf,x_cnv,1,CKMAXPATH); /* convert */
                    s2 = tmpbuf;
                    debug(F110,"ftp get nzrtol",s2,0);
                } else                  /* otherwise */
                  s2 = s;               /* use incoming file's name */
            }
            debug(F110,"ftp get s2 B",s2,0);
            /* If local file already exists, take collision action */
            if (!pipesend &&
#ifdef PIPESEND
                !rcvfilter &&
#endif /* PIPESEND */
                !toscreen) {
		CK_OFF_T x;
                x = zchki(s2);
                debug(F111,"ftp get zchki",s2,x);
                debug(F111,"ftp get x_fnc",s2,x_fnc);
                if (x > (CK_OFF_T)-1 && !restart) {
		    int x = -1;
		    char * newname = NULL;
                    switch (x_fnc) {
                      case XYFX_A:      /* Append */
                        append = 1;
                        break;
                      case XYFX_R:      /* Rename */
                      case XYFX_B:	/* Backup */
			znewn(s2,&newname); /* Make unique name */
			debug(F110,"ftp get znewn",newname,0);
			if (x_fnc == XYFX_B) { /* Backup existing file */
			    x = zrename(s2,newname);
			    debug(F111,"ftp get backup zrename",newname,x);
			} else {      /* Rename incoming file */
			    x = ckstrncpy(tmpbuf,newname,CKMAXPATH+1);
			    s2 = tmpbuf;
			    debug(F111,"ftp get rename incoming",newname,x);
			}
			if (x < 0) {
			    ftscreen(SCR_EM,0,(CK_OFF_T)0,
				     "Backup/Rename failed");
			    x = 0;
			    goto xgetx;
			}
			break;
                      case XYFX_D:      /* Discard (already handled above) */
                      case XYFX_U:      /* Update (ditto) */
                      case XYFX_M:      /* Update (ditto) */
                      case XYFX_X:      /* Overwrite */
                        break;
                    }
                }
            }
        }
        if (!mdel) {
#ifdef PIPESEND
            debug(F111,"ftp get pn",pn,rcvfilter ? 1 : 0);
#endif /* PIPESEND */
            if (pipesend && !toscreen)
              s2 = NULL;
#ifdef DEBUG
            if (deblog) {
                debug(F101,"ftp get x_xla","",x_xla);
                debug(F101,"ftp get x_csl","",x_csl);
                debug(F101,"ftp get x_csr","",x_csr);
                debug(F101,"ftp get append","",append);
            }
#endif /* DEBUG */
            rc = getfile(s,s2,restart,append,pn,x_xla,x_csl,x_csr);
#ifdef DEBUG
            if (deblog) {
                debug(F111,"ftp get rc",s,rc);
                debug(F111,"ftp get ftp_timed_out",s,ftp_timed_out);
                debug(F111,"ftp get cancelfile",s,cancelfile);
                debug(F111,"ftp get cancelgroup",s,cancelgroup);
                debug(F111,"ftp get renaming",s,renaming);
                debug(F111,"ftp get moving",s,moving);
            }
#endif /* DEBUG */
        }
        if (rc > -1) {
            good++;
            status = 1;
            if (!cancelfile) {
                if (deleting) {         /* GET /DELETE (source file) */
                    rc =
                      (ftpcmd("DELE",s,x_csl,x_csr,ftp_vbm) == REPLY_COMPLETE)
                        ? 1 : -1;
                    tlog(F110, (rc > -1) ?
                         " deleted" : " failed to delete", s, 0);
                }
		if (renaming && rcv_rename && !toscreen) {
                    char *p;            /* Rename downloaded file */
#ifndef NOSPL
                    char tmpbuf[CKMAXPATH+1];
                    int n;
                    n = CKMAXPATH;
                    p = tmpbuf;
                    debug(F111,"ftp get /rename",rcv_rename,0);
                    zzstring(rcv_rename,&p,&n);
                    debug(F111,"ftp get /rename",rcv_rename,0);
                    p = tmpbuf;
#else
                    p = rcv_rename;
#endif /* NOSPL */
                    rc = (zrename(s2,p) < 0) ? -1 : 1;
                    debug(F111,"doftpget /RENAME zrename",p,rc);
                    tlog(F110, (rc > -1) ?
                         " renamed to" :
                         " failed to rename to",
                         p,
                         0
                         );
                } else if (moving && rcv_move && !toscreen) {
                    char *p;            /* Move downloaded file */
#ifndef NOSPL
                    char tmpbuf[CKMAXPATH+1];
                    int n;
                    n = TMPBUFSIZ;
                    p = tmpbuf;
                    debug(F111,"ftp get /move-to",rcv_move,0);
                    zzstring(rcv_move,&p,&n);
                    p = tmpbuf;
#else
                    p = rcv_move;
#endif /* NOSPL */
                    debug(F111,"ftp get /move-to",p,0);
                    rc = (zrename(s2,p) < 0) ? -1 : 1;
                    debug(F111,"doftpget /MOVE zrename",p,rc);
                    tlog(F110, (rc > -1) ?
                         " moved to" : " failed to move to", p, 0);
                }
                if (pv[SND_SRN].ival > 0 && pv[SND_SRN].sval) {
                    char * s = pv[SND_SRN].sval;
                    char * srvrn = pv[SND_SRN].sval;
                    char tmpbuf[CKMAXPATH+1];
#ifndef NOSPL
                    int y;              /* Pass it thru the evaluator */
                    extern int cmd_quoting; /* for \v(filename) */
                    debug(F111,"ftp get srv_renam",s,1);
                    if (cmd_quoting) {
                        y = CKMAXPATH;
                        s = (char *)tmpbuf;
                        zzstring(srvrn,&s,&y);
                        s = (char *)tmpbuf;
                    }
#endif /* NOSPL */
                    debug(F111,"ftp get srv_renam",s,1);
                    if (s) if (*s) {
                        int x;
                        x = ftp_rename(s2,s);
                        debug(F111,"ftp get ftp_rename",s2,x);
                        tlog(F110, (x > 0) ?
                             " renamed source file to" :
                             " failed to rename source file to",
                             s,
                             0
                             );
                        if (x < 1)
			  return(-1);
                    }
                }
            }
        }
        if (cancelfile)
          continue;
        if (rc < 0) {
            ftp_fai++;
#ifdef FTP_TIMEOUT
	    debug(F101,"ftp get ftp_timed_out","",ftp_timed_out);
	    if (ftp_timed_out) {
		status = 0;
                ftscreen(SCR_EM,0,(CK_OFF_T)0,"GET timed out");
	    }
#endif	/* FTP_TIMEOUT */
            if (geterror) {
                status = 0;
                ftscreen(SCR_EM,0,(CK_OFF_T)0,"Fatal download error");
                done++;
            }
        }
    }
#ifdef DEBUG
    if (deblog) {
	debug(F101,"ftp get status","",status);
	debug(F101,"ftp get cancelgroup","",cancelgroup);
	debug(F101,"ftp get cancelfile","",cancelfile);
	debug(F101,"ftp get selected","",selected);
	debug(F101,"ftp get good","",good);
    }
#endif /* DEBUG */
    if (selected == 0) {		/* No files met selection criteria */
	status = 1;			/* which is a kind of success. */
    } else if (status > 0) {		/* Some files were selected */
        if (cancelgroup)		/* but MGET was canceled */
          status = 0;			/* so MGET failed */
        else if (cancelfile && good < 1) /* If file was canceled */
          status = 0;			/* MGET failed if it got no files */
    }
    success = status;
    x = success;
    debug(F101,"ftp get success","",success);
  xgetx:
    pipesend = pipesave;                /* Restore global pipe selection */
    if (fp_nml) {                       /* Close /NAMELIST */
        if (fp_nml != stdout)
          fclose(fp_nml);
        fp_nml = NULL;
    }
    if (
#ifdef COMMENT
	x > -1
#else
	success
#endif	/* COMMENT */
	) {				/* Download successful */
#ifdef GFTIMER
        t1 = gmstimer();                /* End time */
        sec = (CKFLOAT)((CKFLOAT)(t1 - t0) / 1000.0); /* Stats */
        if (!sec) sec = 0.001;
        fptsecs = sec;
#else
        sec = (t1 - t0) / 1000;
        if (!sec) sec = 1;
#endif /* GFTIMER */
        tfcps = (long) (tfc / sec);
        tsecs = (int)sec;
        lastxfer = W_FTP|W_RECV;
        xferstat = success;
    }
    if (dpyactive)
      ftscreen(success > 0 ? SCR_TC : SCR_CW, 0, (CK_OFF_T)0, "");
#ifdef CK_TMPDIR
    if (f_tmpdir) {                     /* If we changed to download dir */
        zchdir((char *) savdir);        /* Go back where we came from */
        f_tmpdir = 0;
    }
#endif /* CK_TMPDIR */
    for (i = 0; i <= SND_MAX; i++) {    /* Free malloc'd memory */
        if (pv[i].sval)
          free(pv[i].sval);
    }
    for (i = 0; i < mgetn; i++)         /* MGET list too */
      makestr(&(mgetlist[i]),NULL);
    if (cancelgroup)			/* Clear temp-file stack */
      mlsreset();
    ftreset();                          /* Undo switch effects */
    dpyactive = 0;
    return(x);
}
static struct keytab ftprmt[] = {
    { "cd",        XZCWD, 0 },
    { "cdup",      XZCDU, 0 },
    { "cwd",       XZCWD, CM_INV },
    { "delete",    XZDEL, 0 },
    { "directory", XZDIR, 0 },
    { "exit",      XZXIT, 0 },
    { "help",      XZHLP, 0 },
    { "login",     XZLGI, 0 },
    { "logout",    XZLGO, 0 },
    { "mkdir",     XZMKD, 0 },
    { "pwd",       XZPWD, 0 },
    { "rename",    XZREN, 0 },
    { "rmdir",     XZRMD, 0 },
    { "type",      XZTYP, 0 },
    { "", 0, 0 }
};
static int nftprmt = (sizeof(ftprmt) / sizeof(struct keytab)) - 1;
int
doftpsite() {				/* Send a SITE command */
    int reply;
    char * s;
    int lcs = -1, rcs = -1;
    int save_vbm = ftp_vbm;
#ifndef NOCSETS
    if (ftp_xla) {
        lcs = ftp_csl;
        if (lcs < 0) lcs = fcharset;
        rcs = ftp_csx;
        if (rcs < 0) rcs = ftp_csr;
    }
#endif /* NOCSETS */
    if ((x = cmtxt("Command", "", &s, xxstring)) < 0)
      return(x);
    CHECKCONN();
    ckstrncpy(line,s,LINBUFSIZ);
    if (testing) printf(" ftp site \"%s\"...\n",line);
    if (!ftp_vbm)
        ftp_vbm = !ckstrcmp("HELP",line,4,0);
    if ((reply = ftpcmd("SITE",line,lcs,rcs,ftp_vbm)) == REPLY_PRELIM) {
	do {
	    reply = getreply(0,lcs,rcs,ftp_vbm,0);
	} while (reply == REPLY_PRELIM);
    }
    ftp_vbm = save_vbm;
    return(success = (reply == REPLY_COMPLETE));
}
int
dosetftppsv() {				/* Passive mode */
    x = seton(&ftp_psv);
    if (x > 0) passivemode = ftp_psv;
    return(x);
}
/*  d o f t p r m t  --  Parse and execute REMOTE commands  */
int
doftprmt(cx,who) int cx, who; {         /* who == 1 for ftp, 0 for kermit */
    /* cx == 0 means REMOTE */
    /* cx != 0 is a XZxxx value */
    char * s;
    if (who != 0)
      return(0);
    if (cx == 0) {
        if ((x = cmkey(ftprmt,nftprmt,"","",xxstring)) < 0)
          return(x);
        cx = x;
    }
    switch (cx) {
      case XZCDU:                       /* CDUP */
        if ((x = cmcfm()) < 0) return(x);
        return(doftpcdup());
      case XZCWD:                       /* RCD */
        if ((x = cmtxt("Remote directory", "", &s, xxstring)) < 0)
          return(x);
        ckstrncpy(line,s,LINBUFSIZ);
	s = brstrip(line);
        return(doftpcwd(s,1));
      case XZPWD:                       /* RPWD */
        return(doftppwd());
      case XZDEL:                       /* RDEL */
        return(doftpget(FTP_MDE,1));
      case XZDIR:                       /* RDIR */
        return(doftpdir(FTP_DIR));
      case XZHLP:                       /* RHELP */
        return(doftpxhlp());
      case XZMKD:                       /* RMKDIR */
        return(doftpmkd());
      case XZREN:                       /* RRENAME */
        return(doftpren());
      case XZRMD:                       /* RRMDIR */
        return(doftprmd());
      case XZLGO:                       /* LOGOUT */
        return(doftpres());
      case XZXIT:                       /* EXIT */
        return(ftpbye());
    }
    printf("?Not usable with FTP - \"%s\"\n", atmbuf);
    return(-9);
}
int
doxftp() {                              /* Command parser for built-in FTP */
    int cx, n;
    struct FDB kw, fl;
    char * s;
    int usetls = 0;
    int lcs = -1, rcs = -1;
#ifndef NOCSETS
    if (ftp_xla) {
        lcs = ftp_csl;
        if (lcs < 0) lcs = fcharset;
        rcs = ftp_csx;
        if (rcs < 0) rcs = ftp_csr;
    }
#endif /* NOCSETS */
    if (inserver)                       /* FTP not allowed in IKSD. */
      return(-2);
    if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
        ftp_typ = g_ftp_typ;
        /* g_ftp_typ = -1; */
    }
#ifdef COMMENT
/*
  We'll set the collision action locally in doftpget() based on whether
  ftp_fnc was ever set to a value.  if not, we'll use the fncact value.
*/
    if (ftp_fnc < 0)                    /* Inherit global collision action */
      ftp_fnc = fncact;                 /* if none specified for FTP */
#endif /* COMMENT */
    /* Restore global verbose mode */
    if (ftp_deb)
      ftp_vbm = 1;
    else if (quiet)
      ftp_vbm = 0;
    else
      ftp_vbm = ftp_vbx;
    ftp_dates &= 1;			/* Undo any previous /UPDATE switch */
    dpyactive = 0;                      /* Reset global transfer-active flag */
    printlines = 0;                     /* Reset printlines */
    if (fp_nml) {                       /* Reset /NAMELIST */
        if (fp_nml != stdout)
          fclose(fp_nml);
        fp_nml = NULL;
    }
    makestr(&ftp_nml,NULL);
    cmfdbi(&kw,                         /* First FDB - commands */
           _CMKEY,                      /* fcode */
           "Hostname; or FTP command",  /* help */
           "",                          /* default */
           "",                          /* addtl string data */
           nftpcmd,                     /* addtl numeric data 1: tbl size */
           0,                           /* addtl numeric data 2: none */
           xxstring,                    /* Processing function */
           ftpcmdtab,                   /* Keyword table */
           &fl                          /* Pointer to next FDB */
           );
    cmfdbi(&fl,                         /* A host name or address */
           _CMFLD,                      /* fcode */
           "Hostname or address",       /* help */
           "",                          /* default */
           "",                          /* addtl string data */
           0,                           /* addtl numeric data 1 */
           0,                           /* addtl numeric data 2 */
           xxstring,
           NULL,
           NULL
           );
    x = cmfdb(&kw);                     /* Parse a hostname or a keyword */
    if (x == -3) {
        printf("?ftp what? \"help ftp\" for hints\n");
        return(-9);
    }
    if (x < 0)
      return(x);
    if (cmresult.fcode == _CMFLD) {     /* If hostname */
        return(openftp(cmresult.sresult,0)); /* go open the connection */
    } else {
        cx = cmresult.nresult;
    }
    switch (cx) {
      case FTP_ACC:                     /* ACCOUNT */
        if ((x = cmtxt("Remote account", "", &s, xxstring)) < 0)
          return(x);
        CHECKCONN();
        makestr(&ftp_acc,s);
        if (testing)
          printf(" ftp account: \"%s\"\n",ftp_acc);
        success = (ftpcmd("ACCT",ftp_acc,-1,-1,ftp_vbm) == REPLY_COMPLETE);
        return(success);
      case FTP_GUP:                     /* Go UP */
        if ((x = cmcfm()) < 0) return(x);
        CHECKCONN();
        if (testing) printf(" ftp cd: \"(up)\"\n");
        return(success = doftpcdup());
      case FTP_CWD:                     /* CD */
        if ((x = cmtxt("Remote directory", "", &s, xxstring)) < 0)
          return(x);
        CHECKCONN();
        ckstrncpy(line,s,LINBUFSIZ);
        if (testing)
          printf(" ftp cd: \"%s\"\n", line);
        return(success = doftpcwd(line,1));
      case FTP_CHM:                     /* CHMOD */
        if ((x = cmfld("Permissions or protection code","",&s,xxstring)) < 0)
          return(x);
        ckstrncpy(tmpbuf,s,TMPBUFSIZ);
        if ((x = cmtxt("Remote filename", "", &s, xxstring)) < 0)
          return(x);
        CHECKCONN();
        ckmakmsg(ftpcmdbuf,FTP_BUFSIZ,tmpbuf," ",s,NULL);
        if (testing)
          printf(" ftp chmod: %s\n",ftpcmdbuf);
        success =
          (ftpcmd("SITE CHMOD",ftpcmdbuf,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
        return(success);
      case FTP_CLS:                     /* CLOSE FTP connection */
        if ((y = cmcfm()) < 0)
          return(y);
        CHECKCONN();
        if (testing)
          printf(" ftp closing...\n");
        ftpclose();
        return(success = 1);
      case FTP_DIR:                     /* DIRECTORY of remote files */
      case FTP_VDI:
        return(doftpdir(cx));
      case FTP_GET:                     /* GET a remote file */
      case FTP_RGE:                     /* REGET */
      case FTP_MGE:                     /* MGET */
      case FTP_MDE:                     /* MDELETE */
        return(doftpget(cx,1));
      case FTP_IDL:                     /* IDLE */
        if ((x = cmnum("Number of seconds","-1",10,&z,xxstring)) < 0)
          return(x);
        if ((y = cmcfm()) < 0)
          return(y);
        CHECKCONN();
        if (z < 0)  {                   /* Display idle timeout */
            if (testing)
              printf(" ftp query idle timeout...\n");
            success = (ftpcmd("SITE IDLE",NULL,0,0,1) == REPLY_COMPLETE);
        } else {                        /* Set idle timeout */
            if (testing)
              printf(" ftp idle timeout set: %d...\n",z);
            success =
              (ftpcmd("SITE IDLE",ckitoa(z),0,0,1) == REPLY_COMPLETE);
        }
        return(success);
      case FTP_MKD:                     /* MKDIR */
        return(doftpmkd());
      case FTP_MOD:                     /* MODTIME */
        if ((x = cmtxt("Remote filename", "", &s, xxstring)) < 0)
          return(x);
        CHECKCONN();
        ckstrncpy(line,s,LINBUFSIZ);
        if (testing)
          printf(" ftp modtime \"%s\"...\n",line);
        success = 0;
        if (ftpcmd("MDTM",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE) {
	    success = 1;
	    mdtmok = 1;
	    if (!quiet) {
		int flag = 0;
		char c, * s;
		struct tm tmremote;
		bzero((char *)&tmremote, sizeof(struct tm));
		s = ftp_reply_str;
		while ((c = *s++)) {
		    if (c == SP) {
			flag++;
			break;
		    }
		}
		if (flag) {
		    if (sscanf(s, "%04d%02d%02d%02d%02d%02d",
			       &tmremote.tm_year,
			       &tmremote.tm_mon,
			       &tmremote.tm_mday,
			       &tmremote.tm_hour,
			       &tmremote.tm_min,
			       &tmremote.tm_sec
			       ) == 6) {
			printf(" %s %04d-%02d-%02d %02d:%02d:%02d GMT\n",
			       line,
			       tmremote.tm_year,
			       tmremote.tm_mon,
			       tmremote.tm_mday,
			       tmremote.tm_hour,
			       tmremote.tm_min,
			       tmremote.tm_sec
			       );
		    } else {
			success = 0;
		    }
		}
	    }
        }
        return(success);
      case FTP_OPN:                     /* OPEN connection */
#ifdef COMMENT
        x = cmfld("IP hostname or address","",&s,xxstring);
        if (x < 0) {
            success = 0;
            return(x);
        }
        ckstrncpy(line,s,LINBUFSIZ);
        s = line;
        return(openftp(s,0));
#else
        {                               /* OPEN connection */
            char name[TTNAMLEN+1], *p;
            extern int network;
            extern char ttname[];
            if (network)                /* If we have a current connection */
              ckstrncpy(name,ttname,LINBUFSIZ); /* get the host name */
            else
              *name = '\0';             /* as default host */
            for (p = name; *p; p++)     /* Remove ":service" from end. */
              if (*p == ':') { *p = '\0'; break; }
#ifndef USETLSTAB
            x = cmfld("IP hostname or address",name,&s,xxstring);
#else
            cmfdbi(&kw,                 /* First FDB - commands */
                   _CMKEY,              /* fcode */
                   "Hostname or switch", /* help */
                   "",                  /* default */
                   "",                  /* addtl string data */
                   ntlstab,             /* addtl numeric data 1: tbl size */
                   0,                   /* addtl numeric data 2: none */
                   xxstring,            /* Processing function */
                   tlstab,              /* Keyword table */
                   &fl                  /* Pointer to next FDB */
                   );
            cmfdbi(&fl,                 /* A host name or address */
                   _CMFLD,              /* fcode */
                   "Hostname or address", /* help */
                   "",                  /* default */
                   "",                  /* addtl string data */
                   0,                   /* addtl numeric data 1 */
                   0,                   /* addtl numeric data 2 */
                   xxstring,
                   NULL,
                   NULL
                   );
            for (n = 0;; n++) {
                x = cmfdb(&kw);         /* Parse a hostname or a keyword */
                if (x == -3) {
                  printf("?ftp open what? \"help ftp\" for hints\n");
                  return(-9);
                }
                if (x < 0)
                  break;
                if (cmresult.fcode == _CMFLD) { /* Hostname */
                    s = cmresult.sresult;
                    break;
                } else if (cmresult.nresult == OPN_TLS) {
                    usetls = 1;
                }
            }
#endif /* USETLSTAB */
            if (x < 0) {
                success = 0;
                return(x);
            }
            ckstrncpy(line,s,LINBUFSIZ);
            s = line;
            return(openftp(s,usetls));
        }
#endif /* COMMENT */
      case FTP_PUT:                     /* PUT */
      case FTP_MPU:                     /* MPUT */
      case FTP_APP:                     /* APPEND */
      case FTP_REP:			/* REPUT */
        return(doftpput(cx,1));
      case FTP_PWD:                     /* PWD */
        x = doftppwd();
        if (x > -1) success = x;
        return(x);
      case FTP_REN:                     /* RENAME */
        return(doftpren());
      case FTP_RES:                     /* RESET */
        return(doftpres());
      case FTP_HLP:                     /* (remote) HELP */
        return(doftpxhlp());
      case FTP_RMD:                     /* RMDIR */
        return(doftprmd());
      case FTP_STA:                     /* STATUS */
        if ((x = cmtxt("Command", "", &s, xxstring)) < 0)
          return(x);
        CHECKCONN();
        ckstrncpy(line,s,LINBUFSIZ);
        if (testing) printf(" ftp status \"%s\"...\n",line);
        success = (ftpcmd("STAT",line,lcs,rcs,1) == REPLY_COMPLETE);
        return(success);
      case FTP_SIT: {                   /* SITE */
	  return(doftpsite());
      }
      case FTP_SIZ:                     /* (ask for) SIZE */
        if ((x = cmtxt("Remote filename", "", &s, xxstring)) < 0)
          return(x);
        CHECKCONN();
        ckstrncpy(line,s,LINBUFSIZ);
        if (testing)
          printf(" ftp size \"%s\"...\n",line);
        success = (ftpcmd("SIZE",line,lcs,rcs,1) == REPLY_COMPLETE);
	if (success)
	  sizeok = 1;
        return(success);
      case FTP_SYS:                     /* Ask for server's SYSTEM type */
        if ((x = cmcfm()) < 0) return(x);
        CHECKCONN();
        if (testing)
          printf(" ftp system...\n");
        success = (ftpcmd("SYST",NULL,0,0,1) == REPLY_COMPLETE);
        return(success);
      case FTP_UMA:                     /* Set/query UMASK */
        if ((x = cmfld("Umask to set or nothing to query","",&s,xxstring)) < 0)
          if (x != -3)
            return(x);
        ckstrncpy(tmpbuf,s,TMPBUFSIZ);
        if ((x = cmcfm()) < 0) return(x);
        CHECKCONN();
        if (testing) {
            if (tmpbuf[0])
              printf(" ftp umask \"%s\"...\n",tmpbuf);
            else
              printf(" ftp query umask...\n");
        }
        success = ftp_umask(tmpbuf);
        return(success);
      case FTP_USR:
        return(doftpusr());
      case FTP_QUO:
        if ((x = cmtxt("FTP protocol command", "", &s, xxstring)) < 0)
          return(x);
        CHECKCONN();
        success = (ftpcmd(s,NULL,0,0,ftp_vbm) == REPLY_COMPLETE);
        return(success);
      case FTP_TYP:                     /* Type */
        if ((x = cmkey(ftptyp,nftptyp,"","",xxstring)) < 0)
          return(x);
        if ((y = cmcfm()) < 0) return(y);
        CHECKCONN();
        ftp_typ = x;
        g_ftp_typ = x;
        tenex = (ftp_typ == FTT_TEN);
        changetype(ftp_typ,ftp_vbm);
        return(1);
      case FTP_CHK:                     /* Check if remote file(s) exist(s) */
        if ((x = cmtxt("remote filename", "", &s, xxstring)) < 0)
          return(x);
        CHECKCONN();
        success = remote_files(1,(CHAR *)s,(CHAR *)s,0) ? 1 : 0;
        return(success);
      case FTP_FEA:                     /* RFC2389 */
        if ((y = cmcfm()) < 0)
          return(y);
        CHECKCONN();
	success = (ftpcmd("FEAT",NULL,0,0,1) == REPLY_COMPLETE);
	if (success) {
	    if (sfttab[0] > 0) {
		ftp_aut = sfttab[SFT_AUTH];
		sizeok  = sfttab[SFT_SIZE];
		mdtmok  = sfttab[SFT_MDTM];
		mlstok  = sfttab[SFT_MLST];
	    }
	}
	return(success);
      case FTP_OPT:                     /* RFC2389 */
        /* Perhaps this should be a keyword list... */
        if ((x = cmfld("FTP command","",&s,xxstring)) < 0)
          return(x);
        CHECKCONN();
        ckstrncpy(line,s,LINBUFSIZ);
        if ((x = cmtxt("Options for this command", "", &s, xxstring)) < 0)
          return(x);
        success = (ftpcmd("OPTS",line,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
        return(success);
      case FTP_ENA:			/* FTP ENABLE */
      case FTP_DIS:			/* FTP DISABLE */
        if ((x = cmkey(ftpenatab,nftpena,"","",xxstring)) < 0)
          return(x);
        if ((y = cmcfm()) < 0) return(y);
	switch (x) {
	  case ENA_AUTH:		/* OK to use autoauthentication */
	    ftp_aut = (cx == FTP_ENA) ? 1 : 0;
	    sfttab[SFT_AUTH] = ftp_aut;
	    break;
	  case ENA_FEAT:		/* OK to send FEAT command */
	    featok = (cx == FTP_ENA) ? 1 : 0;
	    break;
	  case ENA_MLST:		/* OK to use MLST/MLSD */
	    mlstok = (cx == FTP_ENA) ? 1 : 0;
	    sfttab[SFT_MLST] = mlstok;
	    break;
	  case ENA_MDTM:		/* OK to use MDTM */
	    mdtmok = (cx == FTP_ENA) ? 1 : 0;
	    sfttab[SFT_MDTM] = mdtmok;
	    break;
	  case ENA_SIZE:		/* OK to use SIZE */
	    sizeok = (cx == FTP_ENA) ? 1 : 0;
	    sfttab[SFT_SIZE] = sizeok;
	    break;
	}
	return(success = 1);
    }
    return(-2);
}
#ifndef NOSHOW
static char *
shopl(x) int x; {
    switch (x) {
      case FPL_CLR: return("clear");
      case FPL_PRV: return("private");
      case FPL_SAF: return("safe");
      case 0:  return("(not set)");
      default: return("(unknown)");
    }
}
int
shoftp(brief) int brief; {
    char * s = "?";
    int n, x;
    if (g_ftp_typ > -1) {               /* Restore TYPE if saved */
        ftp_typ = g_ftp_typ;
        /* g_ftp_typ = -1; */
    }
    printf("\n");
    printf("FTP connection:                 %s\n",connected ?
           ftp_host :
           "(none)"
           );
    n = 2;
    if (connected) {
        n++;
        printf("FTP server type:                %s\n",
               ftp_srvtyp[0] ? ftp_srvtyp : "(unknown)");
    }
    if (loggedin)
      printf("Logged in as:                   %s\n",
             strval(ftp_logname,"(unknown)"));
    else
      printf("Not logged in\n");
    n++;
    if (brief) return(0);
    printf("\nSET FTP values:\n\n");
    n += 3;
    printf(" ftp anonymous-password:        %s\n",
	   ftp_apw ? ftp_apw : "(default)"
	   );
    printf(" ftp auto-login:                %s\n",showoff(ftp_log));
    printf(" ftp auto-authentication:       %s\n",showoff(ftp_aut));
    switch (ftp_typ) {
      case FTT_ASC: s = "text"; break;
      case FTT_BIN: s = "binary"; break;
      case FTT_TEN: s = "tenex"; break;
    }
#ifdef FTP_TIMEOUT
    printf(" ftp timeout:                   %ld\n",ftp_timeout);
#endif	/* FTP_TIMEOUT */
    printf(" ftp type:                      %s\n",s);
    printf(" ftp get-filetype-switching:    %s\n",showoff(get_auto));
    printf(" ftp dates:                     %s\n",showoff(ftp_dates));
    printf(" ftp error-action:              %s\n",ftp_err ? "quit":"proceed");
    printf(" ftp filenames:                 %s\n",
           ftp_cnv == CNV_AUTO ? "auto" : (ftp_cnv ? "converted" : "literal")
           );
    printf(" ftp debug                      %s\n",showoff(ftp_deb));
    printf(" ftp passive-mode:              %s\n",showoff(ftp_psv));
    printf(" ftp permissions:               %s\n",showooa(ftp_prm));
    printf(" ftp verbose-mode:              %s\n",showoff(ftp_vbx));
    printf(" ftp send-port-commands:        %s\n",showoff(ftp_psv));
    printf(" ftp unique-server-names:       %s\n",showoff(ftp_usn));
#ifdef COMMENT
    /* See note in doxftp() */
    if (ftp_fnc < 0)
      ftp_fnc = fncact;
#endif /* COMMENT */
    printf(" ftp collision:                 %s\n",
	   fncnam[ftp_fnc > -1 ? ftp_fnc : fncact]);
    printf(" ftp server-time-offset:        %s\n",
	   fts_sto ? fts_sto : "(none)");
    n += 15;
#ifndef NOCSETS
    printf(" ftp character-set-translation: %s\n",showoff(ftp_xla));
    if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
    printf(" ftp server-character-set:      %s\n",fcsinfo[ftp_csr].keyword);
    if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
    printf(" file character-set:            %s\n",fcsinfo[fcharset].keyword);
    if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
#endif /* NOCSETS */
    x = ftp_dis;
    if (x < 0)
      x = fdispla;
    switch (x) {
      case XYFD_N: s = "none"; break;
      case XYFD_R: s = "serial"; break;
      case XYFD_C: s = "fullscreen"; break;
      case XYFD_S: s = "crt"; break;
      case XYFD_B: s = "brief"; break;
    }
    printf(" ftp display:                   %s\n",s);
    if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
    if (mlstok || featok || mdtmok || sizeok || ftp_aut) {
	printf(" enabled:                      ");
	if (ftp_aut) printf(" AUTH");
	if (featok)  printf(" FEAT");
	if (mdtmok)  printf(" MDTM");
	if (mlstok)  printf(" MLST");
	if (sizeok)  printf(" SIZE");
	printf("\n");
	if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
    }
    if (!mlstok || !featok || !mdtmok || !sizeok || !ftp_aut) {
	printf(" disabled:                     ");
	if (!ftp_aut) printf(" AUTH");
	if (!featok)  printf(" FEAT");
	if (!mdtmok)  printf(" MDTM");
	if (!mlstok)  printf(" MLST");
	if (!sizeok)  printf(" SIZE");
	printf("\n");
	if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
    }
    switch (ftpget) {
      case 0: s = "kermit"; break;
      case 1: s = "ftp"; break;
      case 2: s = "auto"; break;
      default: s = "?";
    }
    printf(" get-put-remote:                %s\n",s);
    if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
    printf("\n");
    if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
#ifdef FTP_SECURITY
    printf("Available security methods:    ");
#ifdef FTP_GSSAPI
    printf("GSSAPI ");
#endif /* FTP_GSSAPI */
#ifdef FTP_KRB4
    printf("Kerberos4 ");
#endif /* FTP_KRB4 */
#ifdef FTP_SRP
    printf("SRP ");
#endif /* FTP_SRP */
#ifdef FTP_SSL
    printf("SSL ");
#endif /* FTP_SSL */
    n++;
    printf("\n\n");
    if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
    printf(" ftp authtype:                  %s\n",strval(auth_type,NULL));
    if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
    printf(" ftp auto-encryption:           %s\n",showoff(ftp_cry));
    if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
    printf(" ftp credential-forwarding:     %s\n",showoff(ftp_cfw));
    if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
    printf(" ftp command-protection-level:  %s\n",shopl(ftp_cpl));
    if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
    printf(" ftp data-protection-level:     %s\n",shopl(ftp_dpl));
    if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
    printf(" ftp secure proxy:              %s\n",shopl(ssl_ftp_proxy));
    if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
#else
    printf("Available security methods:     (none)\n");
    if (++ n > cmd_rows-3) { if (!askmore()) return 0; else n = 0; }
#endif /* FTP_SECURITY */
    if (n <= cmd_rows - 3)
      printf("\n");
    return(0);
}
#endif /* NOSHOW */
#ifndef NOHELP
/* FTP HELP text strings */
static char * fhs_ftp[] = {
    "Syntax: FTP subcommand [ operands ]",
    "  Makes an FTP connection, or sends a command to the FTP server.",
    "  To see a list of available FTP subcommands, type \"ftp ?\".",
    "  and then use HELP FTP xxx to get help about subcommand xxx.",
    "  Also see HELP SET FTP, HELP SET GET-PUT-REMOTE, and HELP FIREWALL.",
    ""
};
static char * fhs_acc[] = {             /* ACCOUNT */
    "Syntax: FTP ACCOUNT text",
    "  Sends an account designator to an FTP server that needs one.",
    "  Most FTP servers do not use accounts; some use them for other",
    "  other purposes, such as disk-access passwords.",
    ""
};
static char * fhs_app[] = {             /* APPEND */
    "Syntax: FTP APPEND filname",
    "  Equivalent to [ FTP ] PUT /APPEND.  See HELP FTP PUT.",
    ""
};
static char * fhs_cls[] = {             /* BYE, CLOSE */
    "Syntax: [ FTP ] BYE",
    "  Logs out from the FTP server and closes the FTP connection.",
    "  Also see HELP SET GET-PUT-REMOTE.  Synonym: [ FTP ] CLOSE.",
    ""
};
static char * fhs_cwd[] = {             /* CD, CWD */
    "Syntax: [ FTP ] CD directory",
    "  Asks the FTP server to change to the given directory.",
    "  Also see HELP SET GET-PUT-REMOTE.  Synonyms: [ FTP ] CWD, RCD, RCWD.",
    ""
};
static char * fhs_gup[] = {             /* CDUP, UP */
    "Syntax: FTP CDUP",
    "  Asks the FTP server to change to the parent directory of its current",
    "  directory.  Also see HELP SET GET-PUT-REMOTE.  Synonym: FTP UP.",
    ""
};
static char * fhs_chm[] = {             /* CHMOD */
    "Syntax: FTP CHMOD filename permissions",
    "  Asks the FTP server to change the permissions, protection, or mode of",
    "  the given file.  The given permissions must be in the syntax of the",
    "  the server's file system, e.g. an octal number for UNIX.  Also see",
    "  FTP PUT /PERMISSIONS",
    ""
};
static char * fhs_mde[] = {             /* DELETE */
    "Syntax: FTP DELETE [ switches ] filespec",
    "  Asks the FTP server to delete the given file or files.",
    "  Synonym: MDELETE (Kermit makes no distinction between single and",
    "  multiple file deletion).  Optional switches:",
    " ",
    "  /ERROR-ACTION:{PROCEED,QUIT}",
    "  /EXCEPT:pattern",
    "  /FILENAMES:{AUTO,CONVERTED,LITERAL}",
    "  /LARGER-THAN:number",
#ifdef UNIXOROSK
    "  /NODOTFILES",
#endif /* UNIXOROSK */
    "  /QUIET",
#ifdef RECURSIVE
    "  /RECURSIVE (depends on server)",
    "  /SUBDIRECTORIES",
#endif /* RECURSIVE */
    "  /SMALLER-THAN:number",
    ""
};
static char * fhs_dir[] = {             /* DIRECTORY */
    "Syntax: FTP DIRECTORY [ filespec ]",
    "  Asks the server to send a directory listing of the files that match",
    "  the given filespec, or if none is given, all the files in its current",
    "  directory.  The filespec, including any wildcards, must be in the",
    "  syntax of the server's file system.  Also see HELP SET GET-PUT-REMOTE.",
    "  Synonym: RDIRECTORY.",
    ""
};
static char * fhs_vdi[] = {             /* VDIRECTORY */
    "Syntax: FTP VDIRECTORY [ filespec ]",
    "  Asks the server to send a directory listing of the files that match",
    "  the given filespec, or if none is given, all the files in its current",
    "  directory.  VDIRECTORY is needed for getting verbose directory",
    "  listings from certain FTP servers, such as on TOPS-20.  Try it if",
    "  FTP DIRECTORY lists only filenames without details.",
    ""
};
static char * fhs_fea[] = {             /* FEATURES */
    "Syntax: FTP FEATURES",
    "  Asks the FTP server to list its special features.  Most FTP servers",
    "  do not recognize this command.",
    ""
};
static char * fhs_mge[] = {             /* MGET */
    "Syntax: [ FTP ] MGET [ options ] filespec [ filespec [ filespec ... ] ]",
    "  Download a single file or multiple files.  Asks the FTP server to send",
    "  the given file or files.  Also see FTP GET.  Optional switches:",
    " ",
    "  /AS-NAME:text",
    "    Name under which to store incoming file.",
    "    Pattern required for for multiple files.",
    "  /BINARY",                        /* /IMAGE */
    "    Force binary mode.  Synonym: /IMAGE.",
    "  /COLLISION:{BACKUP,RENAME,UPDATE,DISCARD,APPEND,OVERWRITE}",
   "    What to do if an incoming file has the same name as an existing file.",
#ifdef PUTPIPE
    "  /COMMAND",
    "    Specifies that the as-name is a command to which the incoming file",
    "    is to be piped as standard input.",
#endif /* PUTPIPE */
#ifdef DOUPDATE
    "  /DATES-DIFFER",
    "    Download only those files whose modification date-times differ from",
    "    those of the corresponding local files, or that do not already",
    "    exist on the local computer.",
#endif /* DOUPDATE */
    "  /DELETE",
    "    Specifies that each file is to be deleted from the server after,",
    "    and only if, it is successfully downloaded.",
    "  /ERROR-ACTION:{PROCEED,QUIT}",
    "    When downloading a group of files, what to do upon failure to",
    "    transfer a file: quit or proceed to the next one.",
    "  /EXCEPT:pattern",
    "    Exception list: don't download any files that match this pattern.",
    "    See HELP WILDCARD for pattern syntax.",
    "  /FILENAMES:{AUTOMATIC,CONVERTED,LITERAL}",
    "    Whether to convert incoming filenames to local syntax.",
#ifdef PIPESEND
#ifndef NOSPL
    "  /FILTER:command",
    "    Pass incoming files through the given command.",
#endif /* NOSPL */
#endif /* PIPESEND */
    "  /LARGER-THAN:number",
    "    Only download files that are larger than the given number of bytes.",
    "  /LISTFILE:filename",
    "    Obtain the list of files to download from the given file.",
#ifndef NOCSETS
    "  /LOCAL-CHARACTER-SET:name",
    "    When downloading in text mode and character-set conversion is",
    "    desired, this specifies the target set.",
#endif /* NOCSETS */
    "  /MATCH:pattern",
    "    Specifies a pattern to be used to select filenames locally from the",
    "    server's list.",
    "  /MLSD",
    "    Forces sending of MLSD (rather than NLST) to get the file list.",
#ifdef CK_TMPDIR
    "  /MOVE-TO:directory",
    "    Each file that is downloaded is to be moved to the given local",
    "    directory immediately after, and only if, it has been received",
    "    successfully.",
#endif /* CK_TMPDIR */
    "  /NAMELIST:filename",
    "    Instead of downloading the files, stores the list of files that",
    "    would be downloaded in the given local file, one filename per line.",
    "  /NLST",
    "    Forces sending of NLST (rather than MLSD) to get the file list.",
    "  /NOBACKUPFILES",
    "    Don't download any files whose names end with .~<number>~.",
    "  /NODOTFILES",
    "    Don't download any files whose names begin with period (.).",
    "  /QUIET",
    "    Suppress the file-transfer display.",
#ifdef FTP_RESTART
    "  /RECOVER",                       /* /RESTART */
    "    Resume a download that was previously interrupted from the point of",
    "    failure.  Works only in binary mode.  Not supported by all servers.",
    "    Synonym: /RESTART.",
#endif /* FTP_RESTART */
#ifdef RECURSIVE
    "  /RECURSIVE",                     /* /SUBDIRECTORIES */
    "    Create subdirectories automatically if the server sends files",
    "    recursively and includes pathnames (most don't).",
#endif /* RECURSIVE */
    "  /RENAME-TO:text",
    "    Each file that is downloaded is to be renamed as indicated just,",
    "    after, and only if, it has arrived successfully.",
#ifndef NOCSETS
    "  /SERVER-CHARACTER-SET:name",
    "    When downloading in text mode and character-set conversion is desired"
,   "    this specifies the original file's character set on the server.",
#endif /* NOCSETS */
    "  /SERVER-RENAME:text",
    "    Each server source file is to be renamed on the server as indicated",
    "    immediately after, but only if, it has arrived successfully.",
    "  /SMALLER-THAN:number",
    "    Download only those files smaller than the given number of bytes.",
    "  /TEXT",                          /* /ASCII */
    "    Force text mode.  Synonym: /ASCII.",
    "  /TENEX",
    "    Force TENEX (TOPS-20) mode (see HELP SET FTP TYPE).",
#ifndef NOCSETS
    "  /TRANSPARENT",
    "    When downloading in text mode, do not convert chracter-sets.",
#endif /* NOCSETS */
    "  /TO-SCREEN",
    "    The downloaded file is to be displayed on the screen.",
#ifdef DOUPDATE
    "  /UPDATE",
    "    Equivalent to /COLLISION:UPDATE.  Download only those files that are",
    "    newer than than their local counterparts, or that do not exist on",
    "    the local computer.",
#endif /* DOUPDATE */
    ""
};
static char * fhs_hlp[] = {             /* HELP */
    "Syntax: FTP HELP [ command [ subcommand... ] ]",
    "  Asks the FTP server for help about the given command.  First use",
    "  FTP HELP by itself to get a list of commands, then use HELP FTP xxx",
    "  to get help for command \"xxx\".  Synonyms: REMOTE HELP, RHELP.",
    ""
};
static char * fhs_idl[] = {             /* IDLE */
    "Syntax: FTP IDLE [ number ]",
    "  If given without a number, this asks the FTP server to tell its",
    "  current idle-time limit.  If given with a number, it asks the server",
    "  to change its idle-time limit to the given number of seconds.",
    ""
};
static char * fhs_usr[] = {             /* USER, LOGIN */
    "Syntax: FTP USER username [ password [ account ] ]",
    "  Log in to the FTP server.  To be used when connected but not yet",
    "  logged in, e.g. when SET FTP AUTOLOGIN is OFF or autologin failed.",
    "  If you omit the password, and one is required by the server, you are",
    "  prompted for it.  If you omit the account, no account is sent.",
    "  Synonym: FTP LOGIN.",
    ""
};
static char * fhs_get[] = {             /* GET */
    "Syntax: [ FTP ] GET [ options ] filename [ as-name ]",
    "  Download a single file.  Asks the FTP server to send the given file.",
    "  The optional as-name is the name to store it under when it arrives;",
    "  if omitted, the file is stored with the name it arrived with, as",
    "  modified according to the FTP FILENAMES setting or /FILENAMES: switch",
    "  value.  Aside from the file list and as-name, syntax and options are",
    "  the same as for FTP MGET, which is used for downloading multiple files."
,   ""
};
static char * fhs_mkd[] = {             /* MKDIR */
    "Syntax: FTP MKDIR directory",
    "  Asks the FTP server to create a directory with the given name,",
    "  which must be in the syntax of the server's file system.  Synonyms:",
    "  REMOTE MKDIR, RMKDIR.",
    ""
};
static char * fhs_mod[] = {             /* MODTIME */
    "Syntax: FTP MODTIME filename",
    "  Asks the FTP server to send the modification time of the given file,",
    "  to be displayed on the screen.  The date-time format is all numeric:",
    "  yyyymmddhhmmssxxx... (where xxx... is 0 or more digits indicating",
    "  fractions of seconds).",
    ""
};
static char * fhs_mpu[] = {             /* MPUT */
    "Syntax: [ FTP ] MPUT [ switches ] filespec [ filespec [ filespec ... ] ]",
    "  Uploads files.  Sends the given file or files to the FTP server.",
    "  Also see FTP PUT.  Optional switches are:",
    " ",
    "  /AFTER:date-time",
    "    Uploads only those files newer than the given date-time.",
    "    HELP DATE for info about date-time formats.  Synonym: /SINCE.",
#ifdef PUTARRAY
    "  /ARRAY:array-designator",
    "    Tells Kermit to upload the contents of the given array, rather than",
    "    a file.",
#endif /* PUTARRAY */
    "  /AS-NAME:text",
    "    Name under which to send files.",
    "    Pattern required for for multiple files.",
    "  /BEFORE:date-time",
    "    Upload only those files older than the given date-time.",
    "  /BINARY",
    "    Force binary mode.  Synonym: /IMAGE.",
#ifdef PUTPIPE
    "  /COMMAND",
    "    Specifies that the filespec is a command whose standard output is",
    "    to be sent.",
#endif /* PUTPIPE */
#ifdef COMMENT
#ifdef DOUPDATE
    "  /DATES-DIFFER",
    "    Upload only those files whose modification date-times differ from",
    "    those on the server, or that don't exist on the server at all.",
#endif /* DOUPDATE */
#endif /* COMMENT */
    "  /DELETE",
    "    Specifies that each source file is to be deleted after, and only if,",
    "    it is successfully uploaded.",
    "  /DOTFILES",
    "    Include files whose names begin with period (.).",
    "  /ERROR-ACTION:{PROCEED,QUIT}",
    "    When uploading a group of files, what to do upon failure to",
    "    transfer a file: quit or proceed to the next one.",
    "  /EXCEPT:pattern",
    "    Exception list: don't upload any files that match this pattern.",
    "    See HELP WILDCARD for pattern syntax.",
    "  /FILENAMES:{AUTOMATIC,CONVERTED,LITERAL}",
    "    Whether to convert outbound filenames to common syntax.",
#ifdef PIPESEND
#ifndef NOSPL
    "  /FILTER:command",
    "    Pass outbound files through the given command.",
#endif /* NOSPL */
#endif /* PIPESEND */
#ifdef CKSYMLINK
    "  /FOLLOWINKS",
    "    Send files that are pointed to by symbolic links.",
    "  /NOFOLLOWINKS",
    "    Skip over symbolic links (default).",
#endif /* CKSYMLINK */
    "  /LARGER-THAN:number",
    "    Only upload files that are larger than the given number of bytes.",
    "  /LISTFILE:filename",
    "    Obtain the list of files to upload from the given file.",
#ifndef NOCSETS
    "  /LOCAL-CHARACTER-SET:name",
    "    When uploading in text mode and character-set conversion is",
    "    desired, this specifies the source-file character set.",
#endif /* NOCSETS */
#ifdef CK_TMPDIR
    "  /MOVE-TO:directory",
    "    Each source file that is uploaded is to be moved to the given local",
    "    directory when, and only if, the transfer is successful.",
#endif /* CK_TMPDIR */
    "  /NOBACKUPFILES",
    "    Don't upload any files whose names end with .~<number>~.",
#ifdef UNIXOROSK
    "  /NODOTFILES",
    "    Don't upload any files whose names begin with period (.).",
#endif /* UNIXOROSK */
    "  /NOT-AFTER:date-time",
    "    Upload only files that are not newer than the given date-time",
    "  /NOT-BEFORE:date-time",
    "    Upload only files that are not older than the given date-time",
#ifdef UNIX
    "  /PERMISSIONS",
    "    Ask the server to set the permissions of each file it receives",
    "    according to the source file's permissions.",
#endif /* UNIX */
    "  /QUIET",
    "    Suppress the file-transfer display.",
#ifdef FTP_RESTART
    "  /RECOVER",
    "    Resume an upload that was previously interrupted from the point of",
    "    failure.  Synonym: /RESTART.",
#endif /* FTP_RESTART */
#ifdef RECURSIVE
    "  /RECURSIVE",
    "    Send files from the given directory and all the directories beneath",
    "    it.  Synonym: /SUBDIRECTORIES.",
#endif /* RECURSIVE */
    "  /RENAME-TO:text",
    "    Each source file that is uploaded is to be renamed on the local",
    "    local computer as indicated when and only if, the transfer completes",
    "    successfully.",
#ifndef NOCSETS
    "  /SERVER-CHARACTER-SET:name",
    "    When uploading in text mode and character-set conversion is desired,",
    "    this specifies the character set to which the file should be",
    "    converted for storage on the server.",
#endif /* NOCSETS */
    "  /SERVER-RENAME:text",
    "    Each file that is uploaded is to be renamed as indicated on the",
    "    server after, and only if, if arrives successfully.",
    "  /SIMULATE",
    "    Show which files would be sent without actually sending them.",
    "  /SMALLER-THAN:number",
    "    Upload only those files smaller than the given number of bytes.",
    "  /TEXT",
    "    Force text mode.  Synonym: /ASCII.",
    "  /TENEX",
    "    Force TENEX (TOPS-20) mode (see HELP SET FTP TYPE).",
#ifndef NOCSETS
    "  /TRANSPARENT",
    "    When uploading in text mode, do not convert chracter-sets.",
#endif /* NOCSETS */
    "  /TYPE:{TEXT,BINARY}",
    "    Upload only files of the given type.",
#ifdef DOUPDATE
    "  /UPDATE",
    "    If a file of the same name exists on the server, upload only if",
    "    the local file is newer.",
#endif /* DOUPDATE */
    "  /UNIQUE-SERVER-NAMES",
    "    Ask the server to compute new names for any incoming file that has",
    "    the same name as an existing file.",
    ""
};
static char * fhs_opn[] = {             /* OPEN */
#ifdef CK_SSL
    "Syntax: FTP [ OPEN ] [ { /SSL, /TLS } ] hostname [ port ] [ switches ]",
    "  Opens a connection to the FTP server on the given host.  The default",
    "  TCP port is 21 (990 if SSL/TLS is used), but a different port number",
    "  can be supplied if necessary.  Optional switches are:",
#else /* CK_SSL */
    "Syntax: FTP [ OPEN ] hostname [ port ] [ switches ]",
    "  Opens a connection to the FTP server on the given host.  The default",
    "  TCP port is 21, but a different port number can be supplied if",
    "  necessary.  Optional switches are:",
#endif /* CK_SSL */
    " ",
    "  /ANONYMOUS",
    "    Logs you in anonymously.",
    "  /USER:text",
    "    Supplies the given text as your username.",
    "  /PASSWORD:text",
    "    Supplies the given text as your password.  If you include a username",
    "    but omit this switch and the server requires a password, you are",
    "    prompted for it.",
    "  /ACCOUNT:text",
    "    Supplies the given text as your account, if required by the server.",
    "  /ACTIVE",
    "    Forces an active (rather than passive) connection.",
    "  /PASSIVE",
    "    Forces a passive (rather than active) connection.",
    "  /NOINIT",
    "    Inhibits sending initial REST, STRU, and MODE commands, which are",
    "    well-known standard commands, but to which some servers react badly.",
    "  /NOLOGIN",
    "    Inhibits autologin for this connection only.",
    ""
};
static char * fhs_opt[] = {             /* OPTS, OPTIONS */
    "Syntax: FTP OPTIONS",
    "  Asks the FTP server to list its current options.  Advanced, new,",
    "  not supported by most FTP servers.",
    ""
};
static char * fhs_put[] = {             /* PUT, SEND */
    "Syntax: [ FTP ] PUT [ switches ] filespec [ as-name ]",
    "  Like FTP MPUT, but only one filespec is allowed, and if it is followed",
    "  by an additional field, this is interpreted as the name under which",
    "  to send the file or files.  See HELP FTP MPUT.",
    ""
};
static char * fhs_reput[] = {		/* REPUT, RESEND */
    "Syntax: [ FTP ] REPUT [ switches ] filespec [ as-name ]",
    "  Synonym for FTP PUT /RECOVER.  Recovers an interrupted binary-mode",
    "  upload from the point of failure if the FTP server supports recovery.",
    "  Synonym: [ FTP ] RESEND.  For details see HELP FTP MPUT.",
    ""
};
static char * fhs_pwd[] = {             /* PWD */
    "Syntax: FTP PWD",
    "  Asks the FTP server to reveal its current working directory.",
    "  Synonyms: REMOTE PWD, RPWD.",
    ""
};
static char * fhs_quo[] = {             /* QUOTE */
    "Syntax: FTP QUOTE text",
    "  Sends an FTP protocol command to the FTP server.  Use this command",
    "  for sending commands that Kermit might not support.",
    ""
};
static char * fhs_rge[] = {             /* REGET */
    "Syntax: FTP REGET",
    "  Synonym for FTP GET /RECOVER.",
    ""
};
static char * fhs_ren[] = {             /* RENAME */
    "Syntax: FTP RENAME name1 name1",
    "  Asks the FTP server to change the name of the file whose name is name1",
    "  and which resides in the FTP server's file system, to name2.  Works",
    "  only for single files; wildcards are not accepted.",
    ""
};
static char * fhs_res[] = {             /* RESET */
    "Syntax: FTP RESET",
    "  Asks the server to log out your session, terminating your access",
    "  rights, without closing the connection.",
    ""
};
static char * fhs_rmd[] = {             /* RMDIR */
    "Syntax: FTP RMDIR directory",
    "  Asks the FTP server to remove the directory whose name is given.",
    "  This usually requires the directory to be empty.  Synonyms: REMOTE",
    "  RMDIR, RRMDIR.",
    ""
};
static char * fhs_sit[] = {             /* SITE */
    "Syntax: FTP SITE text",
    "  Sends a site-specific command to the FTP server.",
    ""
};
static char * fhs_siz[] = {             /* SIZE */
    "Syntax: FTP SIZE filename",
    "  Asks the FTP server to send a numeric string representing the size",
    "  of the given file.",
    ""
};
static char * fhs_sta[] = {             /* STATUS */
    "Syntax: FTP STATUS [ filename ]",
    "  Asks the FTP server to report its status.  If a filename is given,",
    "  the FTP server should report details about the file.",
    ""
};
static char * fhs_sys[] = {             /* SYSTEM */
    "Syntax: FTP SYSTEM",
    "  Asks the FTP server to report its operating system type.",
    ""
};
static char * fhs_typ[] = {             /* TYPE */
    "Syntax: FTP TYPE { TEXT, BINARY, TENEX }",
    "  Puts the client and server in the indicated transfer mode.",
    "  ASCII is a synonym for TEXT.  TENEX is used only for uploading 8-bit",
    "  binary files to a 36-bit platforms such as TENEX or TOPS-20 and/or",
    "  downloading files from TENEX or TOPS-20 that have been uploaded in",
    "  TENEX mode.",
    ""
};
static char * fhs_uma[] = {             /* UMASK */
    "Syntax: FTP UMASK number",
    "  Asks the FTP server to set its file creation mode mask.  Applies",
    "  only (or mainly) to UNIX-based FTP servers.",
    ""
};
static char * fhs_chk[] = {             /* CHECK */
    "Syntax: FTP CHECK remote-filespec",
    "  Asks the FTP server if the given file or files exist.  If the",
    "  remote-filespec contains wildcards, this command fails if no server",
    "  files match, and succeeds if at least one file matches.  If the",
    "  remote-filespec does not contain wildcards, this command succeeds if",
    "  the given file exists and fails if it does not.",
    ""
};
static char * fhs_ena[] = {		/* ENABLE */
    "Syntax: FTP ENABLE { AUTH, FEAT, MDTM, MLST, SIZE }",
    "  Enables the use of the given FTP protocol command in case it has been",
    "  disabled (but this is no guarantee that the FTP server understands it)."
,
    "  Use SHOW FTP to see which of these commands is enabled and disabled.",
    "  Also see FTP DISABLE.",
    ""
};
static char * fhs_dis[] = {		/* DISABLE */
    "Syntax: FTP DISABLE { AUTH, FEAT, MDTM, MLST, SIZE }",
    "  Disables the use of the given FTP protocol command.",
    "  Also see FTP ENABLE.",
    ""
};
#endif /* NOHELP */
int
doftphlp() {
    int cx;
    if ((cx = cmkey(ftpcmdtab,nftpcmd,"","",xxstring)) < 0)
      if (cx != -3)
        return(cx);
    if ((x = cmcfm()) < 0)
      return(x);
#ifdef NOHELP
    printf("Sorry, no help available\n");
#else
    switch (cx) {
      case -3:
        return(hmsga(fhs_ftp));
      case FTP_ACC:                     /* ACCOUNT */
        return(hmsga(fhs_acc));
      case FTP_APP:                     /* APPEND */
        return(hmsga(fhs_app));
      case FTP_CLS:                     /* BYE, CLOSE */
        return(hmsga(fhs_cls));
      case FTP_CWD:                     /* CD, CWD */
        return(hmsga(fhs_cwd));
      case FTP_GUP:                     /* CDUP, UP */
        return(hmsga(fhs_gup));
      case FTP_CHM:                     /* CHMOD */
        return(hmsga(fhs_chm));
      case FTP_MDE:                     /* DELETE, MDELETE */
        return(hmsga(fhs_mde));
      case FTP_DIR:                     /* DIRECTORY */
        return(hmsga(fhs_dir));
      case FTP_VDI:                     /* VDIRECTORY */
        return(hmsga(fhs_vdi));
      case FTP_FEA:                     /* FEATURES */
        return(hmsga(fhs_fea));
      case FTP_GET:                     /* GET */
        return(hmsga(fhs_get));
      case FTP_HLP:                     /* HELP */
        return(hmsga(fhs_hlp));
      case FTP_IDL:                     /* IDLE */
        return(hmsga(fhs_idl));
      case FTP_USR:                     /* USER, LOGIN */
        return(hmsga(fhs_usr));
      case FTP_MGE:                     /* MGET */
        return(hmsga(fhs_mge));
      case FTP_MKD:                     /* MKDIR */
        return(hmsga(fhs_mkd));
      case FTP_MOD:                     /* MODTIME */
        return(hmsga(fhs_mod));
      case FTP_MPU:                     /* MPUT */
        return(hmsga(fhs_mpu));
      case FTP_OPN:                     /* OPEN */
        return(hmsga(fhs_opn));
      case FTP_OPT:                     /* OPTS, OPTIONS */
        return(hmsga(fhs_opt));
      case FTP_PUT:                     /* PUT, SEND */
        return(hmsga(fhs_put));
      case FTP_REP:                     /* REPUT, RESEND */
        return(hmsga(fhs_reput));
      case FTP_PWD:                     /* PWD */
        return(hmsga(fhs_pwd));
      case FTP_QUO:                     /* QUOTE */
        return(hmsga(fhs_quo));
      case FTP_RGE:                     /* REGET */
        return(hmsga(fhs_rge));
      case FTP_REN:                     /* RENAME */
        return(hmsga(fhs_ren));
      case FTP_RES:                     /* RESET */
        return(hmsga(fhs_res));
      case FTP_RMD:                     /* RMDIR */
        return(hmsga(fhs_rmd));
      case FTP_SIT:                     /* SITE */
        return(hmsga(fhs_sit));
      case FTP_SIZ:                     /* SIZE */
        return(hmsga(fhs_siz));
      case FTP_STA:                     /* STATUS */
        return(hmsga(fhs_sta));
      case FTP_SYS:                     /* SYSTEM */
        return(hmsga(fhs_sys));
      case FTP_TYP:                     /* TYPE */
        return(hmsga(fhs_typ));
      case FTP_UMA:                     /* UMASK */
        return(hmsga(fhs_uma));
      case FTP_CHK:                     /* CHECK */
        return(hmsga(fhs_chk));
      case FTP_ENA:
        return(hmsga(fhs_ena));
      case FTP_DIS:
        return(hmsga(fhs_dis));
      default:
        printf("Sorry, help available for this command.\n");
        break;
    }
#endif /* NOHELP */
    return(success = 0);
}
int
dosetftphlp() {
    int cx;
    if ((cx = cmkey(ftpset,nftpset,"","",xxstring)) < 0)
      if (cx != -3)
        return(cx);
    if (cx != -3)
      ckstrncpy(tmpbuf,atmbuf,TMPBUFSIZ);
    if ((x = cmcfm()) < 0)
      return(x);
#ifdef NOHELP
    printf("Sorry, no help available\n");
#else
    switch (cx) {
      case -3:
        printf("\nSyntax: SET FTP parameter value\n");
        printf("  Type \"help set ftp ?\" for a list of parameters.\n");
        printf("  Type \"help set ftp xxx\" for information about setting\n");
        printf("  parameter xxx.  Type \"show ftp\" for current values.\n\n");
        return(0);
      case FTS_BUG:
	printf("\nSyntax: SET FTP BUG <name> {ON, OFF}\n");
	printf(
	    "  Activates a workaround for the named bug in the FTP server.\n");
	printf("  Type SET FTP BUG ? for a list of names.\n");
	printf("  For each bug, the default is OFF\n\n");
	return(0);
#ifdef FTP_SECURITY
      case FTS_ATP:                     /* "authtype" */
        printf("\nSyntax: SET FTP AUTHTYPE list\n");
        printf("  Specifies an ordered list of authentication methods to be\n"
               );
        printf("  when FTP AUTOAUTHENTICATION is ON.  The default list is:\n");
        printf("  GSSAPI-KRB5, SRP, KERBEROS_V4, TLS, SSL.\n\n");
        return(0);
      case FTS_AUT:                     /* "autoauthentication" */
        printf("\nSyntax:SET FTP AUTOAUTHENTICATION { ON, OFF }\n");
        printf("  Tells whether authentication should be negotiated by the\n");
        printf("  FTP OPEN command.  Default is ON.\n\n");
        break;
      case FTS_CRY:                     /* "autoencryption" */
        printf("\nSET FTP AUTOENCRYPTION { ON, OFF }\n");
        printf("  Tells whether encryption (privacy) should be negotiated\n");
        printf("  by the FTP OPEN command.  Default is ON.\n\n");
        break;
#endif /* FTP_SECURITY */
      case FTS_LOG:                     /* "autologin" */
        printf("\nSET FTP AUTOLOGIN { ON, OFF }\n");
        printf("  Tells Kermit whether to try to log you in automatically\n");
        printf("  as part of the connection process.\n\n");
        break;
      case FTS_DIS:
        printf("\nSET FTP DISPLAY { BRIEF, FULLSCREEN, CRT, ... }\n");
	printf("  Chooses the file-transfer display style for FTP.\n");
        printf("  Like SET TRANSFER DISPLAY but applies only to FTP.\n\n");
        break;
#ifndef NOCSETS
      case FTS_XLA:                     /* "character-set-translation" */
        printf("\nSET FTP CHARACTER-SET-TRANSLATION { ON, OFF }\n");
        printf("  Whether to translate character sets when transferring\n");
        printf("  text files with FTP.  OFF by default.\n\n");
        break;
#endif /* NOCSETS */
      case FTS_FNC:                     /* "collision" */
        printf("\n");
        printf(
"Syntax: SET FTP COLLISION { BACKUP,RENAME,UPDATE,DISCARD,APPEND,OVERWRITE }\n"
               );
        printf("  Tells what do when an incoming file has the same name as\n");
        printf("  an existing file when downloading with FTP.\n\n");
        break;
#ifdef FTP_SECURITY
      case FTS_CPL:                     /* "command-protection-level" */
        printf("\n");
        printf(
"Syntax: SET FTP COMMAND-PROTECTION-LEVEL { CLEAR,CONFIDENTIAL,PRIVATE,SAFE }"
               );
        printf("\n");
        printf(
"  Tells what level of protection is applied to the FTP command channel.\n\n");
        break;
      case FTS_CFW:                     /* "credential-forwarding" */
        printf("\nSyntax: SET FTP CREDENTIAL-FORWARDING { ON, OFF }\n");
        printf("  Tells whether end-user credentials are to be forwarded\n");
        printf("  to the server if supported by the authentication method\n");
        printf("  (GSSAPI-KRB5 only).\n\n");
        break;
      case FTS_DPL:                     /* "data-protection-level" */
        printf("\n");
        printf(
"Syntax: SET FTP DATA-PROTECTION-LEVEL { CLEAR,CONFIDENTIAL,PRIVATE,SAFE }"
               );
        printf("\n");
        printf(
"  Tells what level of protection is applied to the FTP data channel.\n\n");
        break;
#endif /* FTP_SECURITY */
      case FTS_DBG:                     /* "debug" */
        printf("\nSyntax: SET FTP DEBUG { ON, OFF }\n");
        printf("  Whether to print FTP protocol messages.\n\n");
        return(0);
      case FTS_ERR:                     /* "error-action" */
        printf("\nSyntax: SET FTP ERROR-ACTION { QUIT, PROCEED }\n");
        printf("  What to do when an error occurs when transferring a group\n")
          ;
        printf("  of files: quit and fail, or proceed to the next file.\n\n");
        return(0);
      case FTS_CNV:                     /* "filenames" */
        printf("\nSyntax: SET FTP FILENAMES { AUTO, CONVERTED, LITERAL }\n");
        printf("  What to do with filenames: convert them, take and use them\n"
               );
        printf("  literally; or choose what to do automatically based on the\n"
               );
        printf("  OS type of the server.  The default is AUTO.\n\n");
        return(0);
      case FTS_PSV:                     /* "passive-mode" */
        printf("\nSyntax: SET FTP PASSIVE-MODE { ON, OFF }\n");
        printf("  Whether to use passive mode, which helps to get through\n");
        printf("  firewalls.  ON by default.\n\n");
        return(0);
      case FTS_PRM:                     /* "permissions" */
        printf("\nSyntax: SET FTP PERMISSIONS { AUTO, ON, OFF }\n");
        printf("  Whether to try to send file permissions when uploading.\n");
        printf("  OFF by default.  AUTO means only if client and server\n");
        printf("  have the same OS type.\n\n");
        return(0);
      case FTS_TST:                     /* "progress-messages" */
        printf("\nSyntax: SET FTP PROGRESS-MESSAGES { ON, OFF }\n");
        printf("  Whether Kermit should print locally-generated feedback\n");
        printf("  messages for each non-file-transfer command.");
        printf("  ON by default.\n\n");
        return(0);
      case FTS_SPC:                     /* "send-port-commands" */
        printf("\nSyntax: SET FTP SEND-PORT-COMMANDS { ON, OFF }\n");
        printf("  Whether Kermit should send a new PORT command for each");
        printf("  task.\n\n");
        return(0);
#ifndef NOCSETS
      case FTS_CSR:                     /* "server-character-set" */
        printf("\nSyntax: SET FTP SERVER-CHARACTER-SET name\n");
        printf("  The name of the character set used for text files on the\n");
        printf("  server.  Enter a name of '?' for a menu.\n\n");
        return(0);
#endif /* NOCSETS */
      case FTS_STO:			/* "server-time-offset */
	printf(
"\nSyntax: SET FTP SERVER-TIME-OFFSET +hh[:mm[:ss]] or -hh[:mm[:ss]]\n");
        printf(
"  Specifies an offset to apply to the server's file timestamps.\n");
        printf(
"  Use this to correct for misconfigured server time or timezone.\n");
        printf(
"  Format: must begin with + or - sign.  Hours must be given; minutes\n");
        printf(
"  and seconds are optional: +4 = +4:00 = +4:00:00 (add 4 hours).\n\n");
        return(0);
      case FTS_TYP:                     /* "type" */
        printf("\nSyntax: SET FTP TYPE { TEXT, BINARY, TENEX }\n");
        printf("  Establishes the default transfer mode.\n");
        printf("  TENEX is used for uploading 8-bit binary files to 36-bit\n");
        printf("  platforms such as TENEX and TOPS-20 and for downloading\n");
        printf("  them again.  ASCII is a synonym for TEXT.  Normally each\n");
        printf("  file's type is determined automatically from its contents\n"
	       );
        printf("  or its name; SET FTP TYPE does not prevent that, it only\n");
        printf("  tells which mode to use when the type can't be determined\n"
	       );
        printf("  automatically.  To completely disable automatic transfer-\n"
	       );
        printf("  mode switching and force either text or binary mode, give\n"
	       );
        printf("  the top-level command ASCII or BINARY, as in traditional\n");
        printf("  FTP clients.\n\n");
        return(0);
#ifdef FTP_TIMEOUT
      case FTS_TMO:
       printf("\nSyntax: SET FTP TIMEOUT number-of-seconds\n");
       printf("  Establishes a timeout for FTP transfers.\n");
       printf("  The timeout applies per network read or write on the data\n");
       printf("  connection, not to the whole transfer.  By default the\n");
       printf("  timeout value is 0, meaning no timeout.  Use a positive\n");
       printf("  number to escape gracefully from hung data connections or\n");
       printf("  directory listings.\n\n");
        return(0);
#endif	/* FTP_TIMEOUT */
#ifdef PATTERNS
      case FTS_GFT:
        printf("\nSyntax: SET FTP GET-FILETYPE-SWITCHING { ON, OFF }\n");
        printf("  Tells whether GET and MGET should automatically switch\n");
        printf("  the appropriate file type, TEXT, BINARY, or TENEX, by\n");
        printf("  matching the name of each incoming file with its list of\n");
        printf("  FILE TEXT-PATTERNS and FILE BINARY-PATTERNS.  ON by\n");
        printf("  default.  SHOW PATTERNS displays the current pattern\n");
        printf("  list.  HELP SET FILE to see how to change it.\n");
        break;
#endif /* PATTERNS */
      case FTS_USN:                     /* "unique-server-names" */
        printf("\nSyntax: SET FTP UNIQUE-SERVER-NAMES { ON, OFF }\n");
        printf("  Tells whether to ask the server to create unique names\n");
        printf("  for any uploaded file that has the same name as an\n");
        printf("  existing file.  Default is OFF.\n\n");
        return(0);
      case FTS_VBM:                     /* "verbose-mode" */
        printf("\nSyntax: SET FTP VERBOSE-MODE { ON, OFF }\n");
        printf("  Whether to display all responses from the FTP server.\n");
        printf("  OFF by default.\n\n");
        return(0);
      case FTS_DAT:
        printf("\nSyntax: SET FTP DATES { ON, OFF }\n");
        printf("  Whether to set date of incoming files from the file date\n");
        printf("  on the server.  ON by default.  Note: there is no way to\n")
          ;
        printf("  set the date on files uploaded to the server.  Also note\n");
	printf("  that not all servers support this feature.\n\n");
        return(0);
      case FTS_APW:
	printf("\nSyntax: SET FTP ANONYMOUS-PASSWORD [ text ]\n");
	printf("  Password to supply automatically on anonymous FTP\n");
	printf("  connections instead of the default user@host.\n");
	printf("  Omit optional text to restore default.\n\n");
	return(0);
      default:
        printf("Sorry, help not available for \"set ftp %s\"\n",tmpbuf);
    }
#endif /* NOHELP */
    return(0);
}
#ifndef L_SET
#define L_SET 0
#endif /* L_SET */
#ifndef L_INCR
#define L_INCR 1
#endif /* L_INCR */
#ifdef FTP_SRP
char srp_user[BUFSIZ];                  /* where is BUFSIZ defined? */
char *srp_pass;
char *srp_acct;
#endif /* FTP_SRP */
static int kerror;                      /* Needed for all auth types */
static struct   sockaddr_in hisctladdr;
static struct   sockaddr_in hisdataaddr;
static struct   sockaddr_in data_addr;
static int      data = -1;
static int      ptflag = 0;
static struct   sockaddr_in myctladdr;
#ifdef COMMENT
#ifndef OS2
UID_T getuid();
#endif /* OS2 */
#endif /* COMMENT */
static int cpend = 0;                   /* No pending replies */
#ifdef CK_SSL
extern SSL *ssl_ftp_con;
extern SSL_CTX *ssl_ftp_ctx;
extern SSL *ssl_ftp_data_con;
extern int ssl_ftp_active_flag;
extern int ssl_ftp_data_active_flag;
#endif /* CK_SSL */
/*  f t p c m d  --  Send a command to the FTP server  */
/*
  Call with:
    char * cmd: The command to send.
    char * arg: The argument (e.g. a filename).
    int lcs: The local character set index.
    int rcs: The remote (server) character set index.
    int vbm: Verbose mode:
      0 = force verbosity off
     >0 = force verbosity on
  If arg is given (not NULL or empty) and lcs != rcs and both are > -1,
  and neither lcs or rcs is UCS-2, the arg is translated from the local
  character set to the remote one before sending the result to the server.
   Returns:
    0 on failure with ftpcode = -1
    >= 0 on success (getreply() result) with ftpcode = 0.
*/
static char xcmdbuf[RFNBUFSIZ];
static int
ftpcmd(cmd,arg,lcs,rcs,vbm) char * cmd, * arg; int lcs, rcs, vbm; {
    char * s = NULL;
    int r = 0, x = 0, fc = 0, len = 0, cmdlen = 0, q = -1;
    sig_t oldintr;
    if (ftp_deb)                        /* DEBUG */
      vbm = 1;
    else if (quiet || dpyactive)        /* QUIET or File Transfer Active */
      vbm = 0;
    else if (vbm < 0)                   /* VERBOSE */
      vbm = ftp_vbm;
    cancelfile = 0;
    if (!cmd) cmd = "";
    if (!arg) arg = "";
    cmdlen = (int)strlen(cmd);
    len = cmdlen + (int)strlen(arg) + 1;
    if (ftp_deb /* && !dpyactive */ ) {
#ifdef FTP_PROXY
        if (ftp_prx) printf("%s ", ftp_host);
#endif /* FTP_PROXY */
        printf("---> ");
        if (!anonymous && strcmp("PASS",cmd) == 0)
          printf("PASS XXXX");
        else
          printf("%s %s",cmd,arg);
        printf("\n");
    }
    /* bzero(xcmdbuf,RFNBUFSIZ); */
    ckmakmsg(xcmdbuf,RFNBUFSIZ, cmd, *arg ? " " : "", arg, NULL);
#ifdef DEBUG
    if (deblog) {
        debug(F110,"ftpcmd cmd",cmd,0);
        debug(F110,"ftpcmd arg",arg,0);
        debug(F101,"ftpcmd lcs","",lcs);
        debug(F101,"ftpcmd rcs","",rcs);
    }
#endif /* DEBUG */
    if (csocket == -1) {
        perror("No control connection for command");
        ftpcode = -1;
        return(0);
    }
    havesigint = 0;
    oldintr = signal(SIGINT, cmdcancel);
#ifndef NOCSETS
    if (*arg &&                         /* If an arg was given */
        lcs > -1 &&                     /* and a local charset */
        rcs > -1 &&                     /* and a remote charset */
        lcs != rcs &&                   /* and the two are not the same */
        lcs != FC_UCS2 &&               /* and neither one is UCS-2 */
        rcs != FC_UCS2                  /* ... */
        ) {
        initxlate(lcs,rcs);             /* Translate arg from lcs to rcs */
        xgnbp = arg;                    /* Global pointer to input string */
        rfnptr = rfnbuf;                /* Global pointer to output buffer */
        while (1) {
            if ((c0 = xgnbyte(FC_UCS2,lcs,strgetc)) < 0) break;
            if (xpnbyte(c0,TC_UCS2,rcs,strputc) < 0) break;
        }
        /*
          We have to copy here instead of translating directly into
          xcmdbuf[] so strputc() can check length.  Alternatively we could
          write yet another xpnbyte() output function.
        */
        if ((int)strlen(rfnbuf) > (RFNBUFSIZ - (cmdlen+1))) {
            printf("?FTP command too long: %s + arg\n",cmd);
            ftpcode = -1;
            return(0);
        }
        x = ckstrncpy(&xcmdbuf[cmdlen+1], rfnbuf, RFNBUFSIZ - (cmdlen+1));
    }
#endif /* NOCSETS */
    s = xcmdbuf;                        /* Command to send to server */
#ifdef DEBUG
    if (deblog) {			/* Log it */
	if (!anonymous && !ckstrcmp(s,"PASS ",5,0)) {
	    /* But don't log passwords */
	    debug(F110,"FTP SENT ","PASS XXXX",0);
	} else {
	    debug(F110,"FTP SENT ",s,0);
	}
    }
#endif /* DEBUG */
#ifdef CK_ENCRYPTION
  again:
#endif /* CK_ENCRYPTION */
    if (scommand(s) == 0) {              /* Send it. */
      signal(SIGINT, oldintr);
      return(0);
    }
    cpend = 1;
    x = !strcmp(cmd,"QUIT");		/* Is it the QUIT command? */
    if (x)				/* In case we're interrupted */
      connected = 0;			/* while waiting for the reply... */
    fc = 0;				/* Function code for getreply() */
    if (!strncmp(cmd,"AUTH ",5)		/* Must parse AUTH reply */
#ifdef FTPHOST
	&& strncmp(cmd, "HOST ",5)
#endif /* FTPHOST */
	) {
	fc = GRF_AUTH;
    } else if (!ckstrcmp(cmd,"FEAT",-1,0)) { /* Must parse FEAT reply */
	fc = GRF_FEAT;			/* But FEAT not widely understood */
	if (!ftp_deb)			/* So suppress error messages */
	  vbm = 9;
    }
    r = getreply(x,			/* Expect connection to close */
		 lcs,rcs,		/* Charsets */
		 vbm,			/* Verbosity */
		 fc			/* Function code */
		 );
    if (q > -1)
      quiet = q;
#ifdef CK_ENCRYPTION
    if (ftpcode == 533 && ftp_cpl == FPL_PRV) {
        fprintf(stderr,
               "ENC command not supported at server; retrying under MIC...\n");
        ftp_cpl = FPL_SAF;
        goto again;
    }
#endif /* CK_ENCRYPTION */
#ifdef COMMENT
    if (cancelfile && oldintr != SIG_IGN)
      (*oldintr)(SIGINT);
#endif /* COMMENT */
    signal(SIGINT, oldintr);
    return(r);
}
static VOID
lostpeer() {
    debug(F100,"lostpeer","",0);
    if (connected) {
        if (csocket != -1) {
#ifdef CK_SSL
            if (ssl_ftp_active_flag) {
                SSL_shutdown(ssl_ftp_con);
                SSL_free(ssl_ftp_con);
                ssl_ftp_proxy = 0;
                ssl_ftp_active_flag = 0;
                ssl_ftp_con = NULL;
            }
#endif /* CK_SSL */
#ifdef TCPIPLIB
            socket_close(csocket);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
            shutdown(csocket, 1+1);
#endif /* USE_SHUTDOWN */
            close(csocket);
#endif /* TCPIPLIB */
            csocket = -1;
        }
        if (data != -1) {
#ifdef CK_SSL
            if (ssl_ftp_data_active_flag) {
                SSL_shutdown(ssl_ftp_data_con);
                SSL_free(ssl_ftp_data_con);
                ssl_ftp_data_active_flag = 0;
                ssl_ftp_data_con = NULL;
            }
#endif /* CK_SSL */
#ifdef TCPIPLIB
            socket_close(data);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
            shutdown(data, 1+1);
#endif /* USE_SHUTDOWN */
            close(data);
#endif /* TCPIPLIB */
            data = -1;
            globaldin = -1;
        }
        connected = 0;
        anonymous = 0;
        loggedin = 0;
        auth_type = NULL;
        ftp_cpl = ftp_dpl = FPL_CLR;
#ifdef CKLOGDIAL
        ftplogend();
#endif /* CKLOGDIAL */
#ifdef LOCUS
	if (autolocus)			/* Auotomatic locus switching... */
	  setlocus(1,1);		/* Switch locus to local. */
#endif /* LOCUS */
#ifdef OS2
        DialerSend(OPT_KERMIT_HANGUP, 0);
#endif /* OS2 */
    }
#ifdef FTP_PROXY
    pswitch(1);
    if (connected) {
        if (csocket != -1) {
#ifdef TCPIPLIB
            socket_close(csocket);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
            shutdown(csocket, 1+1);
#endif /* USE_SHUTDOWN */
            close(csocket);
#endif /* TCPIPLIB */
            csocket = -1;
        }
        connected = 0;
        anonymous = 0;
        loggedin = 0;
        auth_type = NULL;
        ftp_cpl = ftp_dpl = FPL_CLR;
    }
    proxflag = 0;
    pswitch(0);
#endif /* FTP_PROXY */
}
int
ftpisopen() {
    return(connected);
}
static int
ftpclose() {
    extern int quitting;
    if (!connected)
      return(0);
    ftp_xfermode = xfermode;
    if (!ftp_vbm && !quiet)
      printlines = 1;
    ftpcmd("QUIT",NULL,0,0,ftp_vbm);
    if (csocket) {
#ifdef CK_SSL
        if (ssl_ftp_active_flag) {
            SSL_shutdown(ssl_ftp_con);
            SSL_free(ssl_ftp_con);
            ssl_ftp_proxy = 0;
            ssl_ftp_active_flag = 0;
            ssl_ftp_con = NULL;
        }
#endif /* CK_SSL */
#ifdef TCPIPLIB
        socket_close(csocket);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
        shutdown(csocket, 1+1);
#endif /* USE_SHUTDOWN */
        close(csocket);
#endif /* TCPIPLIB */
    }
    csocket = -1;
    connected = 0;
    anonymous = 0;
    loggedin = 0;
    mdtmok = 1;
    sizeok = 1;
    featok = 1;
    stouarg = 1;
    typesent = 0;
    data = -1;
    globaldin = -1;
#ifdef FTP_PROXY
    if (!proxy)
      macnum = 0;
#endif /* FTP_PROXY */
    auth_type = NULL;
    ftp_dpl = FPL_CLR;
#ifdef CKLOGDIAL
    ftplogend();
#endif /* CKLOGDIAL */
#ifdef LOCUS
    /* Unprefixed file management commands are executed locally */
    if (autolocus && !ftp_cmdlin && !quitting) {
        setlocus(1,1);
    }
#endif /* LOCUS */
#ifdef OS2
    DialerSend(OPT_KERMIT_HANGUP, 0);
#endif /* OS2 */
    return(0);
}
int
ftpopen(remote, service, use_tls) char * remote, * service; int use_tls; {
    char * host;
    if (connected) {
        printf("?Already connected to %s, use FTP CLOSE first.\n", ftp_host);
        ftpcode = -1;
        return(0);
    }
#ifdef FTPHOST
    hostcmd = 0;
#endif /* FTPHOST */
    alike = 0;
    ftp_srvtyp[0] = NUL;
    if (!service) service = "";
    if (!*service) service = use_tls ? "ftps" : "ftp";
    if (!isdigit(service[0])) {
        struct servent *destsp;
        destsp = getservbyname(service, "tcp");
        if (!destsp) {
            if (!ckstrcmp(service,"ftp",-1,0)) {
                ftp_port = 21;
            } else if (!ckstrcmp(service,"ftps",-1,0)) {
                ftp_port = 990;
            } else {
                printf("?Bad port name - \"%s\"\n", service);
                ftpcode = -1;
                return(0);
            }
        } else {
            ftp_port = destsp->s_port;
            ftp_port = ntohs((unsigned short)ftp_port);	/* SMS 2007/02/15 */
        }
    } else
        ftp_port = atoi(service);
    if (ftp_port <= 0) {
        printf("?Bad port name - \"%s\"\n", service);
        ftpcode = -1;
        return(0);
    }
    host = ftp_hookup(remote, ftp_port, use_tls);
    if (host) {
        ckstrncpy(ftp_user_host,remote,MAX_DNS_NAMELEN);
        connected = 1;                  /* Set FTP defaults */
        ftp_cpl = ftp_dpl = FPL_CLR;
        curtype = FTT_ASC;              /* Server uses ASCII mode */
        form = FORM_N;
        mode = MODE_S;
        stru = STRU_F;
        strcpy(bytename, "8");
        bytesize = 8;
#ifdef FTP_SECURITY
        if (ftp_aut) {
            if (ftp_auth()) {
                if (ftp_cry 
#ifdef OS2
                     && ck_crypt_is_installed()
#endif /* OS2 */
                     ) {
                    if (!quiet)
                      printf("FTP Command channel is Private (encrypted)\n");
                    ftp_cpl = FPL_PRV;
                    if (setpbsz(DEFAULT_PBSZ) < 0) {
                        /* a failure here is most likely caused by a mixup */
                        /* in the session key used by client and server    */
			printf("?Protection buffer size negotiation failed\n");
                        return(0);
                    }
                    if (ftpcmd("PROT P",NULL,0,0,ftp_vbm) == REPLY_COMPLETE) {
                        if (!quiet)
                          printf("FTP Data channel is Private (encrypted)\n");
                        ftp_dpl = FPL_PRV;
                    } else
                      printf("?Unable to enable encryption on data channel\n");
                } else {
                    ftp_cpl = FPL_SAF;
                }
            }
            if (!connected)
	      goto fail;
        }
#endif /* FTP_SECURITY */
        if (ftp_log)			/* ^^^ */
          ftp_login(remote);
        if (!connected)
	  goto fail;
	ftp_xfermode = xfermode;
#ifdef CKLOGDIAL
        dologftp();
#endif /* CKLOGDIAL */
#ifdef OS2
        DialerSend(OPT_KERMIT_CONNECT, 0);
#endif /* OS2 */
        passivemode = ftp_psv;
        sendport = ftp_spc;
	mdtmok = 1;
	sizeok = 1;
	stouarg = 1;
	typesent = 0;
        if (ucbuf == NULL) {
            actualbuf = DEFAULT_PBSZ;
            while (actualbuf && (ucbuf = (CHAR *)malloc(actualbuf)) == NULL)
              actualbuf >>= 2;
        }
        if (!maxbuf)
          ucbufsiz = actualbuf - FUDGE_FACTOR;
	debug(F101,"ftpopen ucbufsiz","",ucbufsiz);
        return(1);
    }
  fail:
    printf("?Can't FTP connect to %s:%s\n",remote,service);
    ftpcode = -1;
    return(0);
}
#ifdef CK_SSL
int
ssl_auth() {
    int i;
    char* p;
    CONST SSL_METHOD *client_method;
    if (ssl_debug_flag) {
        fprintf(stderr,"SSL DEBUG ACTIVE\n");
        fflush(stderr);
        /* for the moment I want the output on screen */
    }
    if (ssl_ftp_data_con != NULL) {
        SSL_free(ssl_ftp_data_con);
        ssl_ftp_data_con = NULL;
    }
    if (ssl_ftp_con != NULL) {
        SSL_free(ssl_ftp_con);
        ssl_ftp_con=NULL;
    }
    if (ssl_ftp_ctx != NULL) {
        SSL_CTX_free(ssl_ftp_ctx);
        ssl_ftp_ctx = NULL;
    }
    /* The SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS 
     * was added to OpenSSL 0.9.6e and 0.9.7.  It does not exist in previous
     * versions
     */
#ifndef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
#define SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS 0L
#endif
/*
  Pick allowed SSL/TLS versions according to enabled bugs.
  Modified 5 Feb 2015 to default to TLS 1.0 if no bugs are enabled,
  instead of to SSL 3.0, which has the POODLE vulnerability.
  Modified 7 Sep 2022 to use the best version of TLS available (DG)
*/
    if (ftp_bug_use_ssl_v2) {
        /* allow SSL 2.0 or later */
        client_method = SSLv23_client_method();
#ifndef OPENSSL_NO_SSL3
    } else if (ftp_bug_use_ssl_v3) {
        /* allow SSL 3.0 ONLY - previous default */
        client_method = SSLv3_client_method();
#endif /* OPENSSL_NO_SSL3 */
    } else {
        /* default - use the best version of TLS we can */
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
        /* OpenSSL >= 1.1.0: Negotiate the best TLS version possible */
        client_method = TLS_client_method();
#else /* OPENSSL_VERSION_NUMBER < 0x10100000L */
#if OPENSSL_VERSION_NUMBER >= 0x10001000L
        /* OpenSSL >= 1.0.1: Use TLS 1.2 - not yet deprecated as of 2022-09-06 */
        client_method = TLSv1_2_client_method();
#else
        /* OpenSSL 0.9.8 and 1.0.0 can't handle anything newer than TSL 1.0 */
        client_method = TLSv1_client_method();
#endif /* OPENSSL_VERSION_NUMBER >= 0x10001000L */
#endif /* OPENSSL_VERSION_NUMBER >= 0x10100000L */
    }
    if (auth_type && !strcmp(auth_type,"TLS")) {
        ssl_ftp_ctx=SSL_CTX_new(client_method);
        if (!ssl_ftp_ctx)
          return(0);
        SSL_CTX_set_options(ssl_ftp_ctx,
                            SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA
                            );
    } else {
        ssl_ftp_ctx = SSL_CTX_new(client_method);
        if (!ssl_ftp_ctx)
          return(0);
        SSL_CTX_set_options(ssl_ftp_ctx,
                            (ftp_bug_use_ssl_v2 ? 0 : SSL_OP_NO_SSLv2)|
                            SSL_OP_SINGLE_DH_USE|SSL_OP_EPHEMERAL_RSA
                            );
    }
    SSL_CTX_set_default_passwd_cb(ssl_ftp_ctx,
                                  (pem_password_cb *)ssl_passwd_callback);
    SSL_CTX_set_info_callback(ssl_ftp_ctx,ssl_client_info_callback);
    SSL_CTX_set_session_cache_mode(ssl_ftp_ctx,SSL_SESS_CACHE_CLIENT);
#ifdef OS2
#ifdef NT
    /* The defaults in the SSL crypto library are not appropriate for OS/2 */
    {
        char path[CKMAXPATH];
        extern char exedir[];
        ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
        if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,path) == 0)  {
            debug(F110,"ftp ssl_auth unable to load path",path,0);
            if (ssl_debug_flag)
                printf("?Unable to load verify-dir: %s\r\n",path);
        }
        ckmakmsg(path,CKMAXPATH,
                 (char *)GetAppData(1),"kermit 95/certs",NULL,NULL);
        if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,path) == 0)  {
            debug(F110,"ftp ssl_auth unable to load path",path,0);
            if (ssl_debug_flag)
                printf("?Unable to load verify-dir: %s\r\n",path);
        }
        ckmakmsg(path,CKMAXPATH,
                 (char *)GetAppData(0),"kermit 95/certs",NULL,NULL);
        if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,path) == 0)  {
            debug(F110,"ftp ssl_auth unable to load path",path,0);
            if (ssl_debug_flag)
                printf("?Unable to load verify-dir: %s\r\n",path);
        }
        ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
        if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,path,NULL) == 0) {
            debug(F110,"ftp ssl_auth unable to load path",path,0);
            if (ssl_debug_flag)
                printf("?Unable to load verify-file: %s\r\n",path);
        }
        ckmakmsg(path,CKMAXPATH,(char *)GetAppData(1),
		 "kermit 95/ca_certs.pem",NULL,NULL);
        if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,path,NULL) == 0) {
            debug(F110,"ftp ssl_auth unable to load path",path,0);
            if (ssl_debug_flag)
                printf("?Unable to load verify-file: %s\r\n",path);
        }
        ckmakmsg(path,CKMAXPATH,(char *)GetAppData(0),
		 "kermit 95/ca_certs.pem",NULL,NULL);
        if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,path,NULL) == 0) {
            debug(F110,"ftp ssl_auth unable to load path",path,0);
            if (ssl_debug_flag)
                printf("?Unable to load verify-file: %s\r\n",path);
        }
    }
#else /* NT */
    /* The defaults in the SSL crypto library are not appropriate for OS/2 */
    {
        char path[CKMAXPATH];
        extern char exedir[];
        ckmakmsg(path,CKMAXPATH,exedir,"certs",NULL,NULL);
        if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,path) == 0)  {
            debug(F110,"ftp ssl_auth unable to load path",path,0);
            if (ssl_debug_flag)
                printf("?Unable to load verify-dir: %s\r\n",path);
        }
        ckmakmsg(path,CKMAXPATH,exedir,"ca_certs.pem",NULL,NULL);
        if (SSL_CTX_load_verify_locations(ssl_ftp_ctx,path,NULL) == 0) {
            debug(F110,"ftp ssl_auth unable to load path",path,0);
            if (ssl_debug_flag)
                printf("?Unable to load verify-file: %s\r\n",path);
        }
    }
#endif /* NT */
#else /* OS2 */
    SSL_CTX_set_default_verify_paths(ssl_ftp_ctx);
#endif /* OS2 */
    if (ssl_verify_file &&
        SSL_CTX_load_verify_locations(ssl_ftp_ctx,ssl_verify_file,NULL) == 0) {
        debug(F110,
              "ftp ssl auth unable to load ssl_verify_file",
              ssl_verify_file,
              0
              );
        if (ssl_debug_flag)
          printf("?Unable to load verify-file: %s\r\n",ssl_verify_file);
    }
    if (ssl_verify_dir &&
        SSL_CTX_load_verify_locations(ssl_ftp_ctx,NULL,ssl_verify_dir) == 0) {
        debug(F110,
              "ftp ssl auth unable to load ssl_verify_dir",
              ssl_verify_dir,
              0
              );
        if (ssl_debug_flag)
          printf("?Unable to load verify-dir: %s\r\n",ssl_verify_dir);
    }
    /* set up the new CRL Store */
    crl_store = (X509_STORE *)X509_STORE_new();
    if (crl_store) {
#ifdef OS2
        char path[CKMAXPATH];
        extern char exedir[];
        ckmakmsg(path,CKMAXPATH,exedir,"crls",NULL,NULL);
        if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
            debug(F110,"ftp ssl auth unable to load dir",path,0);
            if (ssl_debug_flag)
                printf("?Unable to load crl-dir: %s\r\n",path);
        }
#ifdef NT
        ckmakmsg(path,CKMAXPATH,
		 (char *)GetAppData(1),"kermit 95/crls",NULL,NULL);
        if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
            debug(F110,"ftp ssl auth unable to load dir",path,0);
            if (ssl_debug_flag)
                printf("?Unable to load crl-dir: %s\r\n",path);
        }
        ckmakmsg(path,CKMAXPATH,
		 (char *)GetAppData(0),"kermit 95/crls",NULL,NULL);
        if (X509_STORE_load_locations(crl_store,NULL,path) == 0) {
            debug(F110,"ftp ssl auth unable to load dir",path,0);
            if (ssl_debug_flag)
                printf("?Unable to load crl-dir: %s\r\n",path);
        }
#endif /* NT */
        
        ckmakmsg(path,CKMAXPATH,exedir,"ca_crls.pem",NULL,NULL);
        if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
            debug(F110,"ftp ssl auth unable to load file",path,0);
            if (ssl_debug_flag)
                printf("?Unable to load crl-file: %s\r\n",path);
        }
#ifdef NT
        ckmakmsg(path,CKMAXPATH,(char *)GetAppData(1),
		 "kermit 95/ca_crls.pem",NULL,NULL);
        if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
            debug(F110,"ftp ssl auth unable to load file",path,0);
            if (ssl_debug_flag)
                printf("?Unable to load crl-file: %s\r\n",path);
        }
        ckmakmsg(path,CKMAXPATH,(char *)GetAppData(0),
		 "kermit 95/ca_crls.pem",NULL,NULL);
        if (X509_STORE_load_locations(crl_store,path,NULL) == 0) {
            debug(F110,"ftp ssl auth unable to load file",path,0);
            if (ssl_debug_flag)
                printf("?Unable to load crl-file: %s\r\n",path);
        }
#endif /* NT */
#endif /* OS2 */
        if (ssl_crl_file || ssl_crl_dir) {
            if (ssl_crl_file &&
                X509_STORE_load_locations(crl_store,ssl_crl_file,NULL) == 0) {
                debug(F110,
                      "ftp ssl auth unable to load ssl_crl_file",
                      ssl_crl_file,
                      0
                      );
                if (ssl_debug_flag)
                  printf("?Unable to load crl-file: %s\r\n",ssl_crl_file);
            }
            if (ssl_crl_dir &&
                X509_STORE_load_locations(crl_store,NULL,ssl_crl_dir) == 0) {
                debug(F110,
                      "ftp ssl auth unable to load ssl_crl_dir",
                      ssl_crl_dir,
                      0
                      );
                if (ssl_debug_flag)
                  printf("?Unable to load crl-dir: %s\r\n",ssl_crl_dir);
            }
        } else {
            X509_STORE_set_default_paths(crl_store);
        }
    }
    SSL_CTX_set_verify(ssl_ftp_ctx,ssl_verify_flag,
                       ssl_client_verify_callback);
    ssl_verify_depth = -1;
    ssl_ftp_con=(SSL *)SSL_new(ssl_ftp_ctx);
    tls_load_certs(ssl_ftp_ctx,ssl_ftp_con,0);
    SSL_set_fd(ssl_ftp_con,csocket);
    SSL_set_verify(ssl_ftp_con,ssl_verify_flag,NULL);
    if (ssl_cipher_list) {
        SSL_set_cipher_list(ssl_ftp_con,ssl_cipher_list);
    } else {
        char * p;
        if (p = getenv("SSL_CIPHER")) {
            SSL_set_cipher_list(ssl_ftp_con,p);
        } else {
            SSL_set_cipher_list(ssl_ftp_con,DEFAULT_CIPHER_LIST);
        }
    }
    if (ssl_debug_flag) {
        fprintf(stderr,"=>START SSL/TLS connect on COMMAND\n");
        fflush(stderr);
    }
    if (SSL_connect(ssl_ftp_con) <= 0) {
        static char errbuf[1024];
        ckmakmsg(errbuf,1024,"ftp: SSL/TLS connect COMMAND error: ",
                 ERR_error_string(ERR_get_error(),NULL),NULL,NULL);
        fprintf(stderr,"%s\n", errbuf);
        fflush(stderr);
        ssl_ftp_active_flag=0;
        SSL_free(ssl_ftp_con);
        ssl_ftp_con = NULL;
    } else {
        ssl_ftp_active_flag = 1;
        if (!ssl_certsok_flag &&
	    (ssl_verify_flag & SSL_VERIFY_PEER) && /* JEA 2013-12-10 */
	    !tls_is_krb5(1)) {
            char *subject = ssl_get_subject_name(ssl_ftp_con);
            if (!subject) {
                if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
                    debug(F110,"ssl_auth","[SSL - FAILED]",0);
                    return(ssl_ftp_active_flag = 0);
                } else {
                    if (uq_ok("Warning: Server didn't provide a certificate\n",
                               "Continue? (Y/N)",3,NULL,0) <= 0) {
                        debug(F110, "ssl_auth","[SSL - FAILED]",0);
                        return(ssl_ftp_active_flag = 0);
                    }
                }
            } else if (ssl_check_server_name(ssl_ftp_con, ftp_user_host)) {
                debug(F110,"ssl_auth","[SSL - FAILED]",0);
                return(ssl_ftp_active_flag = 0);
            }
        }
        debug(F110,"ssl_auth","[SSL - OK]",0);
        ssl_display_connect_details(ssl_ftp_con,0,ssl_verbose_flag);
    }
    if (ssl_debug_flag) {
        fprintf(stderr,"=>DONE SSL/TLS connect on COMMAND\n");
        fflush(stderr);
    }
    return(ssl_ftp_active_flag);
}
#endif /* CK_SSL */
static sigtype
cmdcancel(sig) int sig; {
#ifdef OS2
    /* In Unix we "chain" to trap(), which prints this */
    printf("^C...\n");
#endif /* OS2 */
    debug(F100,"ftp cmdcancel caught SIGINT ","",0);
    fflush(stdout);
    secure_getc(0,1);			/* Initialize net input buffers */
    cancelfile++;
    cancelgroup++;
    mlsreset();
#ifndef OS2
#ifdef FTP_PROXY
    if (ptflag)                         /* proxy... */
      longjmp(ptcancel,1);
#endif /* FTP_PROXY */
    debug(F100,"ftp cmdcancel chain to trap()...","",0);
    trap(SIGINT);
    /* NOTREACHED */
    debug(F100,"ftp cmdcancel return from trap()...","",0);
#else
    debug(F100,"ftp cmdcancel PostCtrlCSem()...","",0);
    PostCtrlCSem();
#endif /* OS2 */
}
static int
#ifdef CK_ANSIC
scommand(char * s)                      /* Was secure_command() */
#else
scommand(s) char * s;
#endif /* CK_ANSIC */
{
    int length = 0, len2;
    char in[FTP_BUFSIZ], out[FTP_BUFSIZ];
#ifdef CK_SSL
    if (ssl_ftp_active_flag) {
        int error, rc;
        length = strlen(s) + 2;
        length = ckmakmsg(out,FTP_BUFSIZ,s,"\r\n",NULL,NULL);
        rc = SSL_write(ssl_ftp_con,out,length);
        error = SSL_get_error(ssl_ftp_con,rc);
        switch (error) {
          case SSL_ERROR_NONE:
            return(1);
          case SSL_ERROR_WANT_WRITE:
          case SSL_ERROR_WANT_READ:
          case SSL_ERROR_SYSCALL:
#ifdef NT
            {
                int gle = GetLastError();
            }
#endif /* NT */
          case SSL_ERROR_WANT_X509_LOOKUP:
          case SSL_ERROR_SSL:
          case SSL_ERROR_ZERO_RETURN:
          default:
            lostpeer();
        }
        return(0);
    }
#endif /* CK_SSL */
    if (auth_type && ftp_cpl != FPL_CLR) {
#ifdef FTP_SRP
        if (ck_srp_is_installed() && (strcmp(auth_type,"SRP") == 0))
          if ((length = srp_encode(ftp_cpl == FPL_PRV,
                                   (CHAR *)s,
                                   (CHAR *)out,
                                   strlen(s))) < 0) {
              fprintf(stderr, "SRP failed to encode message\n");
              return(0);
          }
#endif /* FTP_SRP */
#ifdef FTP_KRB4
        if (ck_krb4_is_installed() &&
            (strcmp(auth_type, "KERBEROS_V4") == 0)) {
            if (ftp_cpl == FPL_PRV) {
                length =
                  krb_mk_priv((CHAR *)s, (CHAR *)out,
                              strlen(s), ftp_sched,
#ifdef KRB524
                              ftp_cred.session,
#else /* KRB524 */
                              &ftp_cred.session,
#endif /* KRB524 */
                              &myctladdr, &hisctladdr);
            } else {
                length =
                  krb_mk_safe((CHAR *)s,
                              (CHAR *)out,
                              strlen(s),
#ifdef KRB524
                              ftp_cred.session,
#else /* KRB524 */
                              &ftp_cred.session,
#endif /* KRB524 */
                              &myctladdr, &hisctladdr);
            }
            if (length == -1) {
                fprintf(stderr, "krb_mk_%s failed for KERBEROS_V4\n",
                        ftp_cpl == FPL_PRV ? "priv" : "safe");
                return(0);
            }
        }
#endif /* FTP_KRB4 */
#ifdef FTP_GSSAPI
        /* Scommand (based on level) */
        if (ck_gssapi_is_installed() && (strcmp(auth_type, "GSSAPI") == 0)) {
            gss_buffer_desc in_buf, out_buf;
            OM_uint32 maj_stat, min_stat;
            int conf_state;
            in_buf.value = s;
            in_buf.length = strlen(s) + 1;
            maj_stat = gss_seal(&min_stat, gcontext,
                                (ftp_cpl==FPL_PRV), /* private */
                                GSS_C_QOP_DEFAULT,
                                &in_buf, &conf_state,
                                &out_buf);
            if (maj_stat != GSS_S_COMPLETE) { /* Generally need to deal */
                user_gss_error(maj_stat, min_stat,
                               (ftp_cpl==FPL_PRV)?
                               "gss_seal ENC didn't complete":
                               "gss_seal MIC didn't complete");
            } else if ((ftp_cpl == FPL_PRV) && !conf_state) {
                fprintf(stderr, "GSSAPI didn't encrypt message");
            } else {
                if (ftp_deb)
                  fprintf(stderr, "sealed (%s) %d bytes\n",
                          ftp_cpl==FPL_PRV?"ENC":"MIC",
                          out_buf.length);
                memcpy(out, out_buf.value,
                       length=out_buf.length);
                gss_release_buffer(&min_stat, &out_buf);
            }
        }
#endif /* FTP_GSSAPI */
        /* Other auth types go here ... */
        len2 = FTP_BUFSIZ;
        if ((kerror = radix_encode((CHAR *)out, (CHAR *)in,
                                   length, &len2, RADIX_ENCODE))
            ) {
            fprintf(stderr,"Couldn't base 64 encode command (%s)\n",
                    radix_error(kerror));
            return(0);
        }
        if (ftp_deb)
          fprintf(stderr, "scommand(%s)\nencoding %d bytes\n", s, length);
        len2 = ckmakmsg(out,
			FTP_BUFSIZ,
			ftp_cpl == FPL_PRV ? "ENC " : "MIC ",
                        in,
			"\r\n",
			NULL
			);
        send(csocket,(SENDARG2TYPE)out,len2,0);
    } else {
        char out[FTP_BUFSIZ];
        int len = ckmakmsg(out,FTP_BUFSIZ,s,"\r\n",NULL,NULL);
        send(csocket,(SENDARG2TYPE)out,len,0);
    }
    return(1);
}
static int
mygetc() {
    static char inbuf[4096];
    static int bp = 0, ep = 0;
    int rc;
    if (bp == ep) {
        bp = ep = 0;
#ifdef CK_SSL
        if (ssl_ftp_active_flag) {
            int error;
            rc = SSL_read(ssl_ftp_con,inbuf,4096);
            error = SSL_get_error(ssl_ftp_con,rc);
            switch (error) {
              case SSL_ERROR_NONE:
                break;
              case SSL_ERROR_WANT_WRITE:
              case SSL_ERROR_WANT_READ:
                return(0);
              case SSL_ERROR_SYSCALL:
                if (rc == 0) {          /* EOF */
                    break;
                } else {
#ifdef NT
                    int gle = GetLastError();
#endif /* NT */
                    break;
                }
              case SSL_ERROR_WANT_X509_LOOKUP:
              case SSL_ERROR_SSL:
              case SSL_ERROR_ZERO_RETURN:
              default:
                break;
            }
        } else
#endif /* CK_SSL */
          rc = recv(csocket,(char *)inbuf,4096,0);
        if (rc <= 0)
          return(EOF);
        ep = rc;
    }
    return(inbuf[bp++]);
}
/*  x l a t e c  --  Translate a character  */
/*
    Call with:
      fc    = Function code: 0 = translate, 1 = initialize.
      c     = Character (as int).
      incs  = Index of charset to translate from.
      outcs = Index of charset to translate to.
    Returns:
      0: OK
     -1: Error
*/
static int
xlatec(fc,c,incs,outcs) int fc, c, incs, outcs; {
#ifdef NOCSETS
    return(c);
#else
    static char buf[128];
    static int cx;
    int c0, c1;
    if (fc == 1) {                      /* Initialize */
        cx = 0;                         /* Catch-up buffer write index */
        xgnbp = buf;                    /* Catch-up buffer read pointer */
        buf[0] = NUL;                   /* Buffer is empty */
        return(0);
    }
    if (cx >= 127) {                    /* Catch-up buffer full */
        debug(F100,"xlatec overflow","",0); /* (shouldn't happen) */
        printf("?Translation buffer overflow\n");
        return(-1);
    }
    /* Add char to buffer. */
    /* The buffer won't grow unless incs is a multibyte set, e.g. UTF-8. */
    debug(F000,"xlatec buf",ckitoa(cx),c);
    buf[cx++] = c;
    buf[cx] = NUL;
    while ((c0 = xgnbyte(FC_UCS2,incs,strgetc)) > -1) {
        if (xpnbyte(c0,TC_UCS2,outcs,NULL) < 0)	/* (NULL was xprintc) */
          return(-1);
    }
    /* If we're caught up, reinitialize the buffer */
    return((cx == (xgnbp - buf)) ? xlatec(1,0,0,0) : 0);
#endif /* NOCSETS */
}
/*  p a r s e f e a t  */
/* Note: for convenience we align keyword values with table indices */
/* If you need to insert a new keyword, adjust the SFT_xxx definitions */
static struct keytab feattab[] = {
    { "$$$$", 0,        0 },		/* Dummy for sfttab[0] */
    { "AUTH", SFT_AUTH, 0 },
    { "LANG", SFT_LANG, 0 },
    { "MDTM", SFT_MDTM, 0 },
    { "MLST", SFT_MLST, 0 },
    { "PBSZ", SFT_PBSZ, 0 },
    { "PROT", SFT_PROT, 0 },
    { "REST", SFT_REST, 0 },
    { "SIZE", SFT_SIZE, 0 },
    { "TVFS", SFT_TVFS, 0 },
    { "UTF8", SFT_UTF8, 0 }
};
static int nfeattab = (sizeof(feattab) / sizeof(struct keytab));
#define FACT_CSET  1
#define FACT_CREA  2
#define FACT_LANG  3
#define FACT_MTYP  4
#define FACT_MDTM  5
#define FACT_PERM  6
#define FACT_SIZE  7
#define FACT_TYPE  8
#define FACT_UNIQ  9
static struct keytab facttab[] = {
    { "CHARSET",    FACT_CSET, 0 },
    { "CREATE",     FACT_CREA, 0 },
    { "LANG",       FACT_LANG, 0 },
    { "MEDIA-TYPE", FACT_MTYP, 0 },
    { "MODIFY",     FACT_MDTM, 0 },
    { "PERM",       FACT_PERM, 0 },
    { "SIZE",       FACT_SIZE, 0 },
    { "TYPE",       FACT_TYPE, 0 },
    { "UNIQUE",     FACT_UNIQ, 0 }
};
static int nfacttab = (sizeof(facttab) / sizeof(struct keytab));
static struct keytab ftyptab[] = {
    { "CDIR", FTYP_CDIR, 0 },
    { "DIR",  FTYP_DIR,  0 },
    { "FILE", FTYP_FILE, 0 },
    { "PDIR", FTYP_PDIR, 0 }
};
static int nftyptab = (sizeof(ftyptab) / sizeof(struct keytab));
static VOID
parsefeat(s) char * s; {		/* Parse a FEATURE response */
    char kwbuf[8];
    int i, x;
    if (!s) return;
    if (!*s) return;
    while (*s < '!')
      s++;
    for (i = 0; i < 4; i++) {
	if (s[i] < '!')
	  break;
	kwbuf[i] = s[i];
    }
    if (s[i] && s[i] != SP && s[i] != CK_CR && s[i] != LF)
      return;
    kwbuf[i] = NUL;
    /* xlookup requires a full (but case independent) match */
    i = xlookup(feattab,kwbuf,nfeattab,&x);
    debug(F111,"ftp parsefeat",s,i);
    if (i < 0 || i > 15)
      return;
    switch (i) {
      case SFT_MDTM:			/* Controlled by ENABLE/DISABLE */
	sfttab[i] = mdtmok;
	if (mdtmok) sfttab[0]++;
	break;
      case SFT_MLST:			/* ditto */
	sfttab[i] = mlstok;
	if (mlstok) sfttab[0]++;
	break;
      case SFT_SIZE:			/* ditto */
	sfttab[i] = sizeok;
	if (sizeok) sfttab[0]++;
	break;
      case SFT_AUTH:			/* ditto */
	sfttab[i] = ftp_aut;
	if (ftp_aut) sfttab[0]++;
	break;
      default:				/* Others */
	sfttab[0]++;
	sfttab[i]++;
    }
}
static char *
parsefacts(s) char * s; {		/* Parse MLS[DT] File Facts */
    char * p;
    int i, j, x;
    if (!s) return(NULL);
    if (!*s) return(NULL);
    /* Maybe we should make a copy of s so we can poke it... */
    while ((p = ckstrchr(s,'='))) {
	*p = NUL;			/* s points to fact */
	i = xlookup(facttab,s,nfacttab,&x); 
	debug(F111,"ftp parsefact fact",s,i);
	*p = '=';
	s = p+1;			/* Now s points to arg */
	p = ckstrchr(s,';');
        if (!p)
	  p = ckstrchr(s,SP);
	if (!p) {
	    debug(F110,"ftp parsefact end-of-val search fail",s,0);
	    break;
	}
	*p = NUL;
	debug(F110,"ftp parsefact valu",s,0);
	switch (i) {
	  case FACT_CSET:		/* Ignore these for now */
	  case FACT_CREA:
	  case FACT_LANG:
	  case FACT_PERM:
	  case FACT_MTYP:
	  case FACT_UNIQ:
	    break;
	  case FACT_MDTM:		/* Modtime */
	    makestr(&havemdtm,s);
	    debug(F110,"ftp parsefact mdtm",havemdtm,0);
	    break;
	  case FACT_SIZE:		/* Size */
	    havesize = ckatofs(s);
	    debug(F101,"ftp parsefact size","",havesize);
	    break;
	  case FACT_TYPE:		/* Type */
	    j = xlookup(ftyptab,s,nftyptab,NULL);
	    debug(F111,"ftp parsefact type",s,j);
	    havetype = (j < 1) ? 0 : j;
	    break;
	}
	*p = ';';
	s = p+1;			/* s points next fact or name */
    }
    while (*s == SP)			/* Skip past spaces. */
      s++;
    if (!*s)				/* Make sure we still have a name */
      s = NULL;
    debug(F110,"ftp parsefact name",s,0);
    return(s);
}
/*  g e t r e p l y  --  (to an FTP command sent to server)  */
/* vbm = 1 (verbose); 0 (quiet except for error messages); 9 (super quiet) */
static int
getreply(expecteof,lcs,rcs,vbm,fc) int expecteof, lcs, rcs, vbm, fc; {
    /* lcs, rcs, vbm parameters as in ftpcmd() */
    register int i, c, n;
    register int dig;
    register char *cp;
    int xlate = 0;
    int count = 0;
    int auth = 0;
    int originalcode = 0, continuation = 0;
    sig_t oldintr;
    int pflag = 0;
    char *pt = pasv;
    char ibuf[FTP_BUFSIZ], obuf[FTP_BUFSIZ]; /* (these are pretty big...) */
    int safe = 0;
    int xquiet = 0;
    auth = (fc == GRF_AUTH);
#ifndef NOCSETS
    debug(F101,"ftp getreply lcs","",lcs);
    debug(F101,"ftp getreply rcs","",rcs);
    if (lcs > -1 && rcs > -1 && lcs != rcs) {
        xlate = 1;
        initxlate(rcs,lcs);
        xlatec(1,0,rcs,lcs);
    }
#endif /* NOCSETS */
    debug(F101,"ftp getreply fc","",fc);
    if (quiet)
      xquiet = 1;
    if (vbm == 9) {
        xquiet = 1;
        vbm = 0;
    }
    if (ftp_deb)                        /* DEBUG */
      vbm = 1;
    else if (quiet || dpyactive)        /* QUIET or File Transfer Active */
      vbm = 0;
    else if (vbm < 0)                   /* VERBOSE */
      vbm = ftp_vbm;
    ibuf[0] = '\0';
    if (reply_parse)
      reply_ptr = reply_buf;
    havesigint = 0;
    oldintr = signal(SIGINT, cmdcancel);
    for (count = 0;; count++) {
        obuf[0] = '\0';
        dig = n = ftpcode = i = 0;
        cp = ftp_reply_str;
        while ((c = ibuf[0] ? ibuf[i++] : mygetc()) != '\n') {
            if (c == IAC) {             /* Handle telnet commands */
                switch (c = mygetc()) {
                  case WILL:
                  case WONT:
                    c = mygetc();
                    obuf[0] = IAC;
                    obuf[1] = DONT;
                    obuf[2] = c;
                    obuf[3] = NUL;
#ifdef CK_SSL
                    if (ssl_ftp_active_flag) {
                        int error, rc;
                        rc = SSL_write(ssl_ftp_con,obuf,3);
                        error = SSL_get_error(ssl_ftp_con,rc);
                        switch (error) {
                          case SSL_ERROR_NONE:
                            break;
                          case SSL_ERROR_WANT_WRITE:
                          case SSL_ERROR_WANT_READ:
                            return(0);
                          case SSL_ERROR_SYSCALL:
                            if (rc == 0) { /* EOF */
                                break;
                            } else {
#ifdef NT
                                int gle = GetLastError();
#endif /* NT */
                                break;
                            }
                          case SSL_ERROR_WANT_X509_LOOKUP:
                          case SSL_ERROR_SSL:
                          case SSL_ERROR_ZERO_RETURN:
                          default:
                            break;
                        }
                    } else
#endif /* CK_SSL */
                      send(csocket,(SENDARG2TYPE)obuf,3,0);
                    break;
                  case DO:
                  case DONT:
                    c = mygetc();
                    obuf[0] = IAC;
                    obuf[1] = WONT;
                    obuf[2] = c;
                    obuf[3] = NUL;
#ifdef CK_SSL
                    if (ssl_ftp_active_flag) {
                        int error, rc;
                        rc = SSL_write(ssl_ftp_con,obuf,3);
                        error = SSL_get_error(ssl_ftp_con,rc);
                        switch (error) {
                          case SSL_ERROR_NONE:
                            break;
                          case SSL_ERROR_WANT_WRITE:
                          case SSL_ERROR_WANT_READ:
                              signal(SIGINT,oldintr);
                              return(0);
                          case SSL_ERROR_SYSCALL:
                            if (rc == 0) { /* EOF */
                                break;
                            } else {
#ifdef NT
                                int gle = GetLastError();
#endif /* NT */
                                break;
                            }
                          case SSL_ERROR_WANT_X509_LOOKUP:
                          case SSL_ERROR_SSL:
                          case SSL_ERROR_ZERO_RETURN:
                          default:
                            break;
                        }
                    } else
#endif /* CK_SSL */
                      send(csocket,(SENDARG2TYPE)obuf,3,0);
                    break;
                  default:
                    break;
                }
                continue;
            }
            dig++;
            if (c == EOF) {
                if (expecteof) {
                    signal(SIGINT,oldintr);
                    ftpcode = 221;
                    debug(F101,"ftp getreply EOF","",ftpcode);
                    return(0);
                }
                lostpeer();
                if (!xquiet) {
                    if (ftp_deb)
                      printf("421 ");
                    printf(
                      "Service not available, connection closed by server\n");
                    fflush(stdout);
                }
                signal(SIGINT,oldintr);
                ftpcode = 421;
                debug(F101,"ftp getreply EOF","",ftpcode);
                return(4);
            }
            if (n == 0) {		/* First digit */
		n = c;			/* Save it */
	    }
            if (auth_type &&
#ifdef CK_SSL
                !ssl_ftp_active_flag &&
#endif /* CK_SSL */
                !ibuf[0] && (n == '6' || continuation)) {
                if (c != '\r' && dig > 4)
                  obuf[i++] = c;
            } else {
                if (auth_type &&
#ifdef CK_SSL
                    !ssl_ftp_active_flag &&
#endif /* CK_SSL */
                    !ibuf[0] && dig == 1 && vbm)
                  printf("Unauthenticated reply received from server:\n");
                if (reply_parse) {
                    *reply_ptr++ = c;
                    *reply_ptr = NUL;
                }
                if ((!dpyactive || ftp_deb) && /* Don't mess up xfer display */
                    ftp_cmdlin < 2) {
                    if ((c != '\r') &&
                        (ftp_deb || ((vbm || (!auth && n == '5')) &&
                        (dig > 4 || ( dig <= 4 && !isdigit(c) && ftpcode == 0
                        )))))
                    {
#ifdef FTP_PROXY
                        if (ftp_prx && (dig == 1 || (dig == 5 && vbm == 0)))
                          printf("%s:",ftp_host);
#endif /* FTP_PROXY */
                        if (!xquiet) {
#ifdef NOCSETS
                            printf("%c",c);
#else
                            if (xlate) {
                                xlatec(0,c,rcs,lcs);
                            } else {
                                printf("%c",c);
                            }
#endif /* NOCSETS */
                        }
                    }
                }
            }
            if (auth_type &&
#ifdef CK_SSL
                !ssl_ftp_active_flag &&
#endif /* CK_SSL */
                !ibuf[0] && n != '6')
              continue;
            if (dig < 4 && isdigit(c))
              ftpcode = ftpcode * 10 + (c - '0');
            if (!pflag && ftpcode == 227)
              pflag = 1;
            if (dig > 4 && pflag == 1 && isdigit(c))
              pflag = 2;
            if (pflag == 2) {
                if (c != '\r' && c != ')')
                  *pt++ = c;
                else {
                    *pt = '\0';
                    pflag = 3;
                }
            }
            if (dig == 4 && c == '-' && n != '6') {
                if (continuation)
                  ftpcode = 0;
                continuation++;
            }
            if (cp < &ftp_reply_str[FTP_BUFSIZ - 1]) {
                *cp++ = c;
                *cp = NUL;
            }
        }
        if (deblog ||
#ifdef COMMENT
/*
  Sometimes we need to print the server reply.  printlines is nonzero for any
  command where the results are sent back on the control connection rather
  than the data connection, e.g. STAT.  In the TOPS-20 case, each file line
  has ftpcode 213.  But if you do this with a UNIX server, it sends "213-Start
  STAT", <line with ftpcode == 0>, "213-End" or somesuch.  So when printlines
  is nonzero, we want the 213 lines from TOPS-20 and we DON'T want the 213
  lines from UNIX.  Further experimentation needed with other servers.  Of
  course RFC959 is mute as to the format of the server reply.
  'printlines' is also true for PWD and BYE.
*/
	    (printlines && ((ftpcode == 0) || (servertype == SYS_TOPS20)))
#else
/* No, we can't be that clever -- it breaks other things like RPWD... */
            (printlines &&
             (ftpcode != 631 && ftpcode != 632 && ftpcode != 633))
#endif /* COMMENT */
            ) {
            char * q = cp;
            char *r = ftp_reply_str;
            *q-- = NUL;                 /* NUL-terminate */
            while (*q < '!' && q > r)   /* Strip CR, etc */
              *q-- = NUL;
            if (!ftp_deb && printlines) { /* If printing */
                if (ftpcode != 0)       /* strip ftpcode if any */
                  r += 4;
#ifdef NOCSETS
                printf("%s\n",r);       /* and print */
#else
                if (!xlate) {
                    printf("%s\n",r);
                } else {		/* Translating */
                    xgnbp = r;		/* Set up strgetc() */
                    while ((c0 = xgnbyte(FC_UCS2,rcs,strgetc)) > -1) {
                        if (xpnbyte(c0,TC_UCS2,lcs,NULL) < 0) {	/* (xprintc) */
                            signal(SIGINT,oldintr);
                            return(-1);
                        }
                    }
                    printf("\n");
                }
#endif /* NOCSETS */
            }
        }
	debug(F110,"FTP RCVD ",ftp_reply_str,0);
	if (fc == GRF_FEAT) {		/* Parsing FEAT command response? */
	    if (count == 0 && n == '2') {
		int i;			/* (Re)-init server FEATure table */
		debug(F100,"ftp getreply clearing feature table","",0);
		for (i = 0; i < 16; i++)
		  sfttab[i] = 0;
	    } else {
		parsefeat((char *)ftp_reply_str);
	    }
	}
        if (auth_type &&
#ifdef CK_SSL
            !ssl_ftp_active_flag &&
#endif /* CK_SSL */
             !ibuf[0] && n != '6') {
            signal(SIGINT,oldintr);
            return(getreply(expecteof,lcs,rcs,vbm,auth));
        }
        ibuf[0] = obuf[i] = '\0';
        if (ftpcode && n == '6') {
            if (ftpcode != 631 && ftpcode != 632 && ftpcode != 633) {
                printf("Unknown reply: %d %s\n", ftpcode, obuf);
                n = '5';
            } else safe = (ftpcode == 631);
        }
        if (obuf[0]                     /* if there is a string to decode */
#ifdef CK_SSL
            && !ssl_ftp_active_flag     /* and not SSL/TLS */
#endif /* CK_SSL */
            ) {
            if (!auth_type) {
                printf("Cannot decode reply:\n%d %s\n", ftpcode, obuf);
                n = '5';
            }
#ifndef CK_ENCRYPTION
            else if (ftpcode == 632) {
                printf("Cannot decrypt %d reply: %s\n", ftpcode, obuf);
                n = '5';
            }
#endif /* CK_ENCRYPTION */
#ifdef NOCONFIDENTIAL
            else if (ftpcode == 633) {
                printf("Cannot decrypt %d reply: %s\n", ftpcode, obuf);
                n = '5';
            }
#endif /* NOCONFIDENTIAL */
            else {
                int len = FTP_BUFSIZ;
                if ((kerror = radix_encode((CHAR *)obuf,
                                           (CHAR *)ibuf,
                                           0,
                                           &len,
                                           RADIX_DECODE))
                    ) {
                    printf("Can't decode base 64 reply %d (%s)\n\"%s\"\n",
                           ftpcode, radix_error(kerror), obuf);
                    n = '5';
                }
#ifdef FTP_SRP
                else if (strcmp(auth_type, "SRP") == 0) {
                    int outlen;
                    outlen = srp_decode(!safe, (CHAR *)ibuf,
                                        (CHAR *) ibuf, len);
                    if (outlen < 0) {
                        printf("Warning: %d reply %s!\n",
                               ftpcode, safe ? "modified" : "garbled");
                        n = '5';
                    } else {
                        ckstrncpy(&ibuf[outlen], "\r\n",FTP_BUFSIZ-outlen);
                        if (ftp_deb)
                          printf("%c:", safe ? 'S' : 'P');
                        continue;
                    }
                }
#endif /* FTP_SRP */
#ifdef FTP_KRB4
                else if (strcmp(auth_type, "KERBEROS_V4") == 0) {
                    if (safe) {
                        kerror = krb_rd_safe((CHAR *)ibuf, len,
#ifdef KRB524
                                             ftp_cred.session,
#else /* KRB524 */
                                             &ftp_cred.session,
#endif /* KRB524 */
                                             &hisctladdr,
                                             &myctladdr,
                                             &ftp_msg_data
                                             );
                    } else {
                        kerror = krb_rd_priv((CHAR *)ibuf, len,
                                             ftp_sched,
#ifdef KRB524
                                             ftp_cred.session,
#else /* KRB524 */
                                             &ftp_cred.session,
#endif /* KRB524 */
                                             &hisctladdr,
                                             &myctladdr,
                                             &ftp_msg_data
                                             );
                    }
                    if (kerror != KSUCCESS) {
                        printf("%d reply %s! (krb_rd_%s: %s)\n", ftpcode,
                               safe ? "modified" : "garbled",
                               safe ? "safe" : "priv",
                               krb_get_err_text(kerror));
                        n = '5';
                    } else if (ftp_msg_data.app_length >= FTP_BUFSIZ - 3) {
                        kerror = KFAILURE;
                        n = '5';
                        printf("reply data too large for buffer\n");
                    } else {
                        if (ftp_deb)
                          printf("%c:", safe ? 'S' : 'P');
                        memcpy(ibuf,ftp_msg_data.app_data,
                               ftp_msg_data.app_length);
                        ckstrncpy(&ibuf[ftp_msg_data.app_length], "\r\n",
                                  FTP_BUFSIZ - ftp_msg_data.app_length);
                        continue;
                    }
                }
#endif /* FTP_KRB4 */
#ifdef FTP_GSSAPI
                else if (strcmp(auth_type, "GSSAPI") == 0) {
                    gss_buffer_desc xmit_buf, msg_buf;
                    OM_uint32 maj_stat, min_stat;
                    int conf_state;
                    xmit_buf.value = ibuf;
                    xmit_buf.length = len;
                    /* decrypt/verify the message */
                    conf_state = safe;
                    maj_stat = gss_unseal(&min_stat, gcontext,
                                          &xmit_buf, &msg_buf,
                                          &conf_state, NULL);
                    if (maj_stat != GSS_S_COMPLETE) {
                        user_gss_error(maj_stat, min_stat,
                                       "failed unsealing reply");
                        n = '5';
                    } else {
                        memcpy(ibuf, msg_buf.value, msg_buf.length);
                        ckstrncpy(&ibuf[msg_buf.length], "\r\n",
                                  FTP_BUFSIZ-msg_buf.length);
                        gss_release_buffer(&min_stat,&msg_buf);
                        if (ftp_deb)
                          printf("%c:", safe ? 'S' : 'P');
                        continue;
                    }
                }
#endif /* FTP_GSSAPI */
                /* Other auth types go here... */
            }
        } else if ((!dpyactive || ftp_deb) && ftp_cmdlin < 2 &&
                   !xquiet && (vbm || (!auth && (n == '4' || n == '5')))) {
#ifdef NOCSETS
            printf("%c",c);
#else
            if (xlate) {
                xlatec(0,c,rcs,lcs);
            } else {
                printf("%c",c);
            }
#endif /* NOCSETS */
            fflush (stdout);
        }
        if (continuation && ftpcode != originalcode) {
            if (originalcode == 0)
              originalcode = ftpcode;
            continue;
        }
        *cp = '\0';
        if (n != '1')
          cpend = 0;
        signal(SIGINT,oldintr);
        if (ftpcode == 421 || originalcode == 421) {
	    lostpeer();
	    if (!xquiet && !ftp_deb)
	      printf("%s\n",reply_buf);
        }
        if ((cancelfile != 0) &&
#ifndef ULTRIX3
            /* Ultrix 3.0 cc objects violently to this clause */
            (oldintr != cmdcancel) &&
#endif /* ULTRIX3 */
            (oldintr != SIG_IGN)) {
            if (oldintr)
              (*oldintr)(SIGINT);
        }
        if (reply_parse) {
            *reply_ptr = '\0';
            if ((reply_ptr = ckstrstr(reply_buf, reply_parse))) {
                reply_parse = reply_ptr + strlen(reply_parse);
                if ((reply_ptr = ckstrpbrk(reply_parse, " \r")))
                  *reply_ptr = '\0';
            } else
              reply_parse = reply_ptr;
        }
        while (*cp < '!' && cp > ftp_reply_str) /* Remove trailing junk */
          *cp-- = NUL;
        debug(F111,"ftp getreply",ftp_reply_str,n - '0');
        return(n - '0');
    } /* for (;;) */
}
#ifdef BSDSELECT
static int
#ifdef CK_ANSIC
empty(fd_set * mask, int sec)
#else
empty(mask, sec) fd_set * mask; int sec;
#endif /* CK_ANSIC */
{
    struct timeval t;
    t.tv_sec = (long) sec;
    t.tv_usec = 0L;
    debug(F100,"ftp empty calling select...","",0);
#ifdef INTSELECT
    x = select(32, (int *)mask, NULL, NULL, &t);
#else
    x = select(32, mask, (fd_set *) 0, (fd_set *) 0, &t);
#endif /* INTSELECT */
    debug(F101,"ftp empty select","",x);
    return(x);
}
#else /* BSDSELECT */
#ifdef IBMSELECT
static int
empty(mask, cnt, sec) int * mask, sec;
                      int   cnt;
{
    return(select(mask,cnt,0,0,sec*1000));
}
#endif /* IBMSELECT */
#endif /* BSDSELECT */
static sigtype
cancelsend(sig) int sig; {
    havesigint++;
    cancelgroup++;
    cancelfile = 0;
    printf(" Canceled...\n");
    secure_getc(0,1);			/* Initialize net input buffers */
    debug(F100,"ftp cancelsend caught SIGINT ","",0);
    fflush(stdout);
#ifndef OS2
    longjmp(sendcancel, 1);
#else
    PostCtrlCSem();
#endif /* OS2 */
}
static VOID
#ifdef CK_ANSIC
secure_error(char *fmt, ...)
#else
/* VARARGS1 */
secure_error(fmt, p1, p2, p3, p4, p5)
   char *fmt; int p1, p2, p3, p4, p5;
#endif /* CK_ANSIC */
{
#ifdef CK_ANSIC
    va_list ap;
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
#else
    fprintf(stderr, fmt, p1, p2, p3, p4, p5);
#endif
    fprintf(stderr, "\n");
}
/*
 * Internal form of settype; changes current type in use with server
 * without changing our notion of the type for data transfers.
 * Used to change to and from ascii for listings.
 */
static VOID
changetype(newtype, show) int newtype, show; {
    int rc;
    char * s;
    if ((newtype == curtype) && typesent++)
      return;
    switch (newtype) {
      case FTT_ASC:
        s = "A";
        break;
      case FTT_BIN:
        s = "I";
        break;
      case FTT_TEN:
        s = "L 8";
        break;
      default:
        s = "I";
        break;
    }
    rc = ftpcmd("TYPE",s,-1,-1,show);
    if (rc == REPLY_COMPLETE)
      curtype = newtype;
}
/* PUT a file.  Returns -1 on error, 0 on success, 1 if file skipped */
static VOID
#ifdef CK_ANSIC
doftpsend(void * threadinfo)
#else
doftpsend(threadinfo) VOID * threadinfo;
#endif
{
#ifdef NTSIG
    if (threadinfo) {                   /* Thread local storage... */
        TlsSetValue(TlsIndex,threadinfo);
        debug(F100, "doftpsend called with threadinfo block","", 0);
    } else debug(F100, "doftpsend - threadinfo is NULL", "", 0);
#endif /* NTSIG */
#ifdef CK_LOGIN
#ifdef IKSD
#ifdef NT
    if (inserver)
      setntcreds();
#endif /* NT */
#endif /* IKSD */
#endif /* CK_LOGIN */
    if (initconn()) {
#ifndef NOHTTP
        int y = -1;
        /* debug(F101,"doftpsend","tcp_http_proxy",tcp_http_proxy); */
       /*  If the connection failed and we are using an HTTP Proxy
        *  and the reason for the failure was an authentication
        *  error, then we need to give the user to ability to
        *  enter a username and password, just like a browser.
        *
        *  I tried to do all of this within the netopen() call
        *  but it is much too much work.
        */
        while (y != 0 && tcp_http_proxy != NULL ) {
            if (tcp_http_proxy_errno == 401 ||
                 tcp_http_proxy_errno == 407 ) {
                char uid[UIDBUFLEN];
                char pwd[PWDSIZ];
                struct txtbox tb[2];
                int ok;
                tb[0].t_buf = uid;
                tb[0].t_len = UIDBUFLEN;
                tb[0].t_lbl = "Proxy Userid: ";
                tb[0].t_dflt = NULL;
                tb[0].t_echo = 1;
                tb[1].t_buf = pwd;
                tb[1].t_len = 256;
                tb[1].t_lbl = "Proxy Passphrase: ";
                tb[1].t_dflt = NULL;
                tb[1].t_echo = 2;
                ok = uq_mtxt("Proxy Server Authentication Required\n",
                              NULL, 2, tb);
                if (ok && uid[0]) {
                    char * proxy_user, * proxy_pwd;
                    proxy_user = tcp_http_proxy_user;
                    proxy_pwd  = tcp_http_proxy_pwd;
                    tcp_http_proxy_user = uid;
                    tcp_http_proxy_pwd = pwd;
                    y = initconn();
                    debug(F101,"doftpsend","initconn",y);
                    memset(pwd,0,PWDSIZ);
                    tcp_http_proxy_user = proxy_user;
                    tcp_http_proxy_pwd = proxy_pwd;
                } else
                    break;
            } else
                break;
        }
        if ( y != 0 ) {
#endif /* NOHTTP */
            signal(SIGINT, ftpsnd.oldintr);
#ifdef SIGPIPE
            if (ftpsnd.oldintp)
              signal(SIGPIPE, ftpsnd.oldintp);
#endif /* SIGPIPE */
            ftpcode = -1;
            zclose(ZIFILE);
            ftpsndret = -1;
#ifdef NTSIG
            ckThreadEnd(threadinfo);
#endif /* NTSIG */
            return;
#ifndef NOHTTP
        }
#endif /* NOHTTP */
    }
    ftpsndret = 0;
#ifdef NTSIG
     ckThreadEnd(threadinfo);
#endif /* NTSIG */
}
static VOID
#ifdef CK_ANSIC
failftpsend(void * threadinfo)
#else
failftpsend(threadinfo) VOID * threadinfo;
#endif /* CK_ANSIC */
{
#ifdef NTSIG
    if (threadinfo) {                   /* Thread local storage... */
        TlsSetValue(TlsIndex,threadinfo);
        debug(F100, "docmdfile called with threadinfo block","", 0);
    } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
#endif /* NTSIG */
#ifdef CK_LOGIN
#ifdef IKSD
#ifdef NT
    if (inserver)
      setntcreds();
#endif /* NT */
#endif /* IKSD */
#endif /* CK_LOGIN */
    while (cpend) {
        ftpsnd.reply = getreply(0,ftpsnd.incs,ftpsnd.outcs,ftp_vbm,0);
        debug(F111,"ftp sendrequest getreply","null command",ftpsnd.reply);
    }
    if (data >= 0) {
#ifdef CK_SSL
        if (ssl_ftp_data_active_flag) {
            SSL_shutdown(ssl_ftp_data_con);
            SSL_free(ssl_ftp_data_con);
            ssl_ftp_data_active_flag = 0;
            ssl_ftp_data_con = NULL;
        }
#endif /* CK_SSL */
#ifdef TCPIPLIB
        socket_close(data);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
        shutdown(data, 1+1);
#endif /* USE_SHUTDOWN */
        close(data);
#endif /* TCPIPLIB */
        data = -1;
        globaldin = -1;
    }
    if (ftpsnd.oldintr)
        signal(SIGINT,ftpsnd.oldintr);
#ifdef SIGPIPE
    if (ftpsnd.oldintp)
        signal(SIGPIPE,ftpsnd.oldintp);
#endif /* SIGPIPE */
    ftpcode = -1;
#ifndef OS2
    /* TEST ME IN K95 */
    if (havesigint) {
	havesigint = 0;
	debug(F100,"ftp failftpsend chain to trap()...","",0);
	if (ftpsnd.oldintr != SIG_IGN)
	  (*ftpsnd.oldintr)(SIGINT);
	/* NOTREACHED (I hope!) */
	debug(F100,"ftp failftpsend return from trap()...","",0);
    }
#endif /* OS2 */
}
static VOID
#ifdef CK_ANSIC
failftpsend2(void * threadinfo)
#else
failftpsend2(threadinfo) VOID * threadinfo;
#endif /* CK_ANSIC */
{
#ifdef NTSIG
    if (threadinfo) {                   /* Thread local storage... */
        TlsSetValue(TlsIndex,threadinfo);
        debug(F100, "docmdfile called with threadinfo block","", 0);
    } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
#endif /* NTSIG */
#ifdef CK_LOGIN
#ifdef IKSD
#ifdef NT
    if (inserver)
      setntcreds();
#endif /* NT */
#endif /* IKSD */
#endif /* CK_LOGIN */
    debug(F101,"ftp sendrequest canceled","",ftpsnd.bytes);
    tfc += ffc;
#ifdef GFTIMER
    fpfsecs = gftimer();
#endif /* GFTIMER */
    zclose(ZIFILE);
#ifdef PIPESEND
    if (sndfilter)
      pipesend = 0;
#endif /* PIPESEND */
    signal(SIGINT, ftpsnd.oldintr);
#ifdef SIGPIPE
    if (ftpsnd.oldintp)
      signal(SIGPIPE, ftpsnd.oldintp);
#endif /* SIGPIPE */
    if (!cpend) {
        ftpcode = -1;
        ftpsndret = -1;
#ifdef NTSIG
        ckThreadEnd(threadinfo);
#endif /* NTSIG */
        return;
    }
    if (data >= 0) {
#ifdef CK_SSL
        if (ssl_ftp_data_active_flag) {
            SSL_shutdown(ssl_ftp_data_con);
            SSL_free(ssl_ftp_data_con);
            ssl_ftp_data_active_flag = 0;
            ssl_ftp_data_con = NULL;
        }
#endif /* CK_SSL */
#ifdef TCPIPLIB
        socket_close(data);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
        shutdown(data, 1+1);
#endif /* USE_SHUTDOWN */
        close(data);
#endif /* TCPIPLIB */
        data = -1;
        globaldin = -1;
    }
    if (dout) {
#ifdef TCPIPLIB
        socket_close(dout);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
        shutdown(dout, 1+1);
#endif /* USE_SHUTDOWN */
        close(dout);
#endif /* TCPIPLIB */
    }
    ftpsnd.reply = getreply(0,ftpsnd.incs,ftpsnd.outcs,ftp_vbm,0);
    ftpcode = -1;
    ftpsndret = -1;
#ifndef OS2
    /* TEST ME IN K95 */
    if (havesigint) {
	havesigint = 0;
	debug(F100,"ftp failftpsend2 chain to trap()...","",0);
	if (ftpsnd.oldintr != SIG_IGN)
	  (*ftpsnd.oldintr)(SIGINT);
	/* NOTREACHED (I hope!) */
	debug(F100,"ftp failftpsend2 return from trap()...","",0);
    }
#endif /* OS2 */
}
static VOID
#ifdef CK_ANSIC
doftpsend2(void * threadinfo)
#else
doftpsend2(threadinfo) VOID * threadinfo;
#endif
{
    register int c, d = 0;
    int n, t, x, notafile, unique = 0;
    char *buf, *bufp;
    
#ifdef NTSIG
    if (threadinfo) {                   /* Thread local storage... */
        TlsSetValue(TlsIndex,threadinfo);
        debug(F100, "doftpsend2 called with threadinfo block","", 0);
    } else debug(F100, "doftpsend2 - threadinfo is NULL", "", 0);
#endif /* NTSIG */
#ifdef CK_LOGIN
#ifdef IKSD
#ifdef NT
    if (inserver)
      setntcreds();
#endif /* NT */
#endif /* IKSD */
#endif /* CK_LOGIN */
    buf = ftpsndbuf;			/* (not on stack) */
    unique = strcmp(ftpsnd.cmd,"STOU") ? 0 : 1;
    notafile = sndarray || pipesend;
#ifdef FTP_RESTART
    if (ftpsnd.restart && ((curtype == FTT_BIN) || (alike > 0))) {
        char * p;
        changetype(FTT_BIN,0);          /* Change to binary */
        /* Ask for remote file's size */
        x = ftpcmd("SIZE",ftpsnd.remote,ftpsnd.incs,ftpsnd.outcs,ftp_vbm);
        if (x == REPLY_COMPLETE) {      /* Have ftpsnd.reply */
            p = &ftp_reply_str[4];      /* Parse it */
            while (isdigit(*p)) {
                sendstart = sendstart * 10 + (int)(*p - '0');
                p++;
            }
            if (*p && *p != CK_CR) {    /* Bad number */
                debug(F110,"doftpsend2 bad size",ftp_reply_str,0);
                sendstart = (CK_OFF_T)0;
            } else if (sendstart > fsize) { /* Remote file bigger than local */
                debug(F110,"doftpsend2 big size",ckfstoa(fsize),sendstart);
                sendstart = (CK_OFF_T)0;
            }
	    /* Local is newer */
            debug(F111,"doftpsend2 size",ftpsnd.remote,sendstart);
            if (chkmodtime(ftpsnd.local,ftpsnd.remote,0) == 2) {
                debug(F110,"doftpsend2 date mismatch",ftp_reply_str,0);
                sendstart = (CK_OFF_T)0; /* Send the whole file */
            }
        }
        changetype(ftp_typ,0);          /* Change back to appropriate type */
        if (sendstart > (CK_OFF_T)0) {	/* Still restarting? */
            if (sendstart == fsize) {   /* Same size - no need to send */
                debug(F111,"doftpsend2 /restart SKIP",
		      ckfstoa(fsize),sendstart);
                zclose(ZIFILE);
                ftpsndret = SKP_RES;
#ifdef NTSIG
                ckThreadEnd(threadinfo);
#endif /* NTSIG */
                return;
            }
            errno = 0;                  /* Restart needed, seek to the spot */
            if (zfseek((long)sendstart) < 0) {
                debug(F111,"doftpsend2 zfseek fails",
		      ftpsnd.local,sendstart);
                fprintf(stderr, "FSEEK: %s: %s\n", ftpsnd.local, ck_errstr());
                sendstart = 0;
                zclose(ZIFILE);
                ftpsndret = -1;
#ifdef NTSIG
                ckThreadEnd(threadinfo);
#endif /* NTSIG */
                return;
            }
#ifdef COMMENT
            debug(F111,"doftpsend2 zfseek ok",ftpsnd.local,sendstart);
            x = ftpcmd("REST",ckltoa(sendstart),-1,-1,ftp_vbm);
            if (x != REPLY_CONTINUE) {
                sendstart = 0;
                zclose(ZIFILE);
                ftpsndret = -1;
#ifdef NTSIG
                ckThreadEnd(threadinfo);
#endif /* NTSIG */
                return;
            } else {
                ftpsnd.cmd = "STOR";
            }
#else
            sendmode = SM_RESEND;
            ftpsnd.cmd = "APPE";
#endif /* COMMENT */
            /* sendstart = (CK_OFF_T)0; */
        }
    }
#endif /* FTP_RESTART */
    if (unique && !stouarg)		/* If we know STOU accepts no arg */
      ftpsnd.remote = NULL;		/* don't include one. */
    x = ftpcmd(ftpsnd.cmd, ftpsnd.remote, ftpsnd.incs, ftpsnd.outcs, ftp_vbm);
    debug(F111,"doftpsend2 ftpcode",ftpsnd.cmd,ftpcode);
    debug(F101,"doftpsend2 ftpcmd","",x);
    if (x != REPLY_PRELIM && unique) {
	/*
	  RFC959 says STOU does not take an argument.  But every FTP server
	  I've encountered but one accepts the arg and constructs the unique
	  name from it, which is better than making up a totally random name
	  for the file, which is what RFC959 calls for.  Especially because
	  there is no way for the client to find out the name chosen by the
	  server.  So we try STOU with the argument first, which works with
	  most servers, and if it fails we retry it without the arg, for
	  the benefit of the one picky server that is not "liberal in what
	  it accepts" UNLESS the first STOU got a 502 code ("not implemented")
	  which means STOU is not accepted, period.
	*/
	if ((x == 5) && stouarg && (ftpcode != 502)) {
	    x = ftpcmd(ftpsnd.cmd,NULL,ftpsnd.incs,ftpsnd.outcs,ftp_vbm); 
	    if (x == REPLY_PRELIM)	/* If accepted */
	      stouarg = 0;		/* flag no STOU arg for this server */
	}
    }
    if (x != REPLY_PRELIM) {
        signal(SIGINT, ftpsnd.oldintr);
#ifdef SIGPIPE
        if (ftpsnd.oldintp)
          signal(SIGPIPE, ftpsnd.oldintp);
#endif /* SIGPIPE */
	debug(F101,"doftpsend2 not REPLY_PRELIM","",x);
        zclose(ZIFILE);
#ifdef PIPESEND
        if (sndfilter)
          pipesend = 0;
#endif /* PIPESEND */
        ftpsndret = -1;
#ifdef NTSIG
        ckThreadEnd(threadinfo);
#endif /* NTSIG */
        return;
    }
    debug(F100,"doftpsend2 getting data connection...","",0);
    dout = dataconn(ftpsnd.lmode);             /* Get data connection */
    debug(F101,"doftpsend2 dataconn","",dout);
    if (dout == -1) {
        failftpsend2(threadinfo);
#ifdef NTSIG
        ckThreadEnd(threadinfo);
#endif /* NTSIG */
        return;
    }
    /* Initialize per-file stats */
    ffc = (CK_OFF_T)0;			/* Character counter */
    cps = oldcps = 0L;                  /* Thruput */
    n = 0;
#ifdef GFTIMER
    rftimer();                          /* reset f.p. timer */
#endif /* GFTIMER */
#ifdef SIGPIPE
    ftpsnd.oldintp = signal(SIGPIPE, SIG_IGN);
#endif /* SIGPIPE */
    debug(F101,"doftpsend2 curtype","",curtype);
    switch (curtype) {
      case FTT_BIN:                     /* Binary mode */
      case FTT_TEN:
        errno = d = 0;
#ifdef VMS
	/*
	  This is because VMS zxin() is C-Library fread() 
          but the file was opened with zopeni(), which is RMS.
	*/
	while (((c = zminchar()) > -1) && !cancelfile) {
	    ffc++;
	    if (zzout(dout,c) < 0)
	      break;
	}
#else  /* VMS */
        while ((n = zxin(ZIFILE,buf,FTP_BUFSIZ - 1)) > 0 && !cancelfile) {
            ftpsnd.bytes += n;
            ffc += n;
            debug(F111,"doftpsend2 zxin",ckltoa(n),ffc);
            ckhexdump("doftpsend2 zxin",buf,16);
#ifdef CK_SSL
            if (ssl_ftp_data_active_flag) {
                for (bufp = buf; n > 0; n -= d, bufp += d) {
                    if ((d = SSL_write(ssl_ftp_data_con, bufp, n)) <= 0)
                      break;
                    spackets++;
                    pktnum++;
                    if (fdispla != XYFD_B) {
                        spktl = d;
                        ftscreen(SCR_PT,'D',(CK_OFF_T)spackets,NULL);
                    }
                }
            } else {
#endif /* CK_SSL */
                for (bufp = buf; n > 0; n -= d, bufp += d) {
                    if (((d = secure_write(dout, (CHAR *)bufp, n)) <= 0)
                        || iscanceled())
                      break;
                    spackets++;
                    pktnum++;
                    if (fdispla != XYFD_B) {
                        spktl = d;
                        ftscreen(SCR_PT,'D',(CK_OFF_T)spackets,NULL);
                    }
                }
#ifdef CK_SSL
            }
#endif /* CK_SSL */
            if (d <= 0)
              break;
        }
#endif	/* VMS */
	debug(F111,"doftpsend2 XX zxin",ckltoa(n),ffc);
        if (n < 0)
          fprintf(stderr, "local: %s: %s\n", ftpsnd.local, ck_errstr());
        if (d < 0 || (d = secure_flush(dout)) < 0) {
            if (d == -1 && errno && errno != EPIPE)
              perror("netout");
            ftpsnd.bytes = -1;
        }
        break;
      case FTT_ASC:                     /* Text mode */
#ifndef NOCSETS
        if (ftpsnd.xlate) {             /* With translation */
            initxlate(ftpsnd.incs,ftpsnd.outcs);
            while (!cancelfile) {
                if ((c0 = xgnbyte(FC_UCS2,ftpsnd.incs,NULL)) < 0) break;
                if ((x = xpnbyte(c0,TC_UCS2,ftpsnd.outcs,xxout)) < 0) break;
            }
        } else {
#endif /* NOCSETS */
            /* Text mode, no translation */
            while (((c = zminchar()) > -1) && !cancelfile) {
                ffc++;
		if (xxout(c) < 0)
		  break;
            }
            d = 0;
#ifndef NOCSETS
        }
#endif /* NOCSETS */
        if (dout == -1 || (d = secure_flush(dout)) < 0) {
            if (d == -1 && errno && errno != EPIPE)
              perror("netout");
            ftpsnd.bytes = -1;
        }
        break;
    }
    tfc += ffc;                         /* Total file chars */
#ifdef GFTIMER
    fpfsecs = gftimer();
#endif /* GFTIMER */
    zclose(ZIFILE);                     /* Close input file */
#ifdef PIPESEND
    if (sndfilter)                      /* Undo this (it's per file) */
      pipesend = 0;
#endif /* PIPESEND */
#ifdef CK_SSL
        if (ssl_ftp_data_active_flag) {
            SSL_shutdown(ssl_ftp_data_con);
            SSL_free(ssl_ftp_data_con);
            ssl_ftp_data_active_flag = 0;
            ssl_ftp_data_con = NULL;
        }
#endif /* CK_SSL */
#ifdef TCPIPLIB
    socket_close(dout);                 /* Close data connection */
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
    shutdown(dout, 1+1);
#endif /* USE_SHUTDOWN */
    close(dout);
#endif /* TCPIPLIB */
    ftpsnd.reply = getreply(0,ftpsnd.incs,ftpsnd.outcs,ftp_vbm,0);
    signal(SIGINT, ftpsnd.oldintr);            /* Put back interrupts */
#ifdef SIGPIPE
    if (ftpsnd.oldintp)
      signal(SIGPIPE, ftpsnd.oldintp);
#endif /* SIGPIPE */
    if (ftpsnd.reply == REPLY_TRANSIENT || ftpsnd.reply == REPLY_ERROR) {
        debug(F101,"doftpsend2 ftpsnd.reply","",ftpsnd.reply);
        ftpsndret = -1;
#ifdef NTSIG
        ckThreadEnd(threadinfo);
#endif /* NTSIG */
        return;
    } else if (cancelfile) {
        debug(F101,"doftpsend2 canceled","",ftpsnd.bytes);
        ftpsndret = -1;
#ifdef NTSIG
        ckThreadEnd(threadinfo);
#endif /* NTSIG */
        return;
    }
    debug(F101,"doftpsend2 ok","",ftpsnd.bytes);
    ftpsndret = 0;
#ifdef NTSIG
     ckThreadEnd(threadinfo);
#endif /* NTSIG */
}
static int
sendrequest(cmd, local, remote, xlate, incs, outcs, restart)
    char *cmd, *local, *remote; int xlate, incs, outcs, restart;
{
    if (!remote) remote = "";           /* Check args */
    if (!*remote) remote = local;
    if (!local) local = "";
    if (!*local) return(-1);
    if (!cmd) cmd = "";
    if (!*cmd) cmd = "STOR";
    debug(F111,"ftp sendrequest restart",local,restart);
    nout = 0;                           /* Init output buffer count */
    ftpsnd.bytes = 0;                   /* File input byte count */
    dout = -1;
#ifdef FTP_PROXY
    if (proxy) {
        proxtrans(cmd, local, remote, !strcmp(cmd,"STOU"));
        return(0);
    }
#endif /* FTP_PROXY */
    changetype(ftp_typ,0);              /* Change type for this file */
    ftpsnd.oldintr = NULL;		/* Set up interrupt handler */
    ftpsnd.oldintp = NULL;
    ftpsnd.restart = restart;
    ftpsnd.xlate = xlate;
    ftpsnd.lmode = "wb";
#ifdef PIPESEND                         /* Use Kermit API for file i/o... */
    if (sndfilter) {
        char * p = NULL, * q;
#ifndef NOSPL
        int n = CKMAXPATH;
        if (cmd_quoting && (p = (char *) malloc(n + 1))) {
            q = p;
            debug(F110,"sendrequest pipesend filter",sndfilter,0);
            zzstring(sndfilter,&p,&n);
            debug(F111,"sendrequest pipename",q,n);
            if (n <= 0) {
                printf("?Sorry, send filter + filename too long, %d max.\n",
                       CKMAXPATH
                       );
                free(q);
                return(-1);
            }
            ckstrncpy(filnam,q,CKMAXPATH+1);
            free(q);
            local = filnam;
        }
#endif /* NOSPL */
    }
    if (sndfilter)                      /* If sending thru a filter */
      pipesend = 1;                     /* set this for open and i/o */
#endif /* PIPESEND */
    
#ifdef VMS
    debug(F101,"XXX before openi binary","",binary);
    debug(F101,"XXX before openi ftp_typ","",ftp_typ);
#endif	/* VMS */
    if (openi(local) == 0)		/* Try to open the input file */
      return(-1);
#ifdef VMS
    debug(F101,"XXX after openi binary","",binary);
    debug(F101,"XXX after openi ftp_typ","",ftp_typ);
    if (!forcetype) {
	if (binary != ftp_typ) {	/* VMS zopeni() sets binary */
	    debug(F101,"XXX changing type","",binary);
	    doftptyp(binary);
	    debug(F101,"XXX after doftptyp","",ftp_typ);
	    /* **** */
	    if (displa && fdispla) {	/* Update file type display */
		ftscreen(SCR_FN,'F',(CK_OFF_T)0,local);
	    }
	}
    }
#endif	/* VMS */
    ftpsndret = 0;
    ftpsnd.incs = incs;
    ftpsnd.outcs = outcs;
    ftpsnd.cmd = cmd;
    ftpsnd.local = local;
    ftpsnd.remote = remote;
    ftpsnd.oldintr = signal(SIGINT, cancelsend);
    havesigint = 0;
    if (cc_execute(ckjaddr(sendcancel), doftpsend, failftpsend) < 0)
      return(-1);
    if (ftpsndret < 0)
      return(-1);
    if (cc_execute(ckjaddr(sendcancel), doftpsend2, failftpsend2) < 0)
      return(-1);
    return(ftpsndret);
}
static sigtype
cancelrecv(sig) int sig; {
    havesigint++;
    cancelfile = 0;
    cancelgroup++;
    secure_getc(0,1);			/* Initialize net input buffers */
    printf(" Canceling...\n");
    debug(F100,"ftp cancelrecv caught SIGINT","",0);
    fflush(stdout);
    if (fp_nml) {
        if (fp_nml != stdout)
          fclose(fp_nml);
        fp_nml = NULL;
    }
#ifndef OS2
    longjmp(recvcancel, 1);
#else
    PostCtrlCSem();
#endif /* OS2 */
}
/* Argumentless front-end for secure_getc() */
static int
netgetc() {
    return(secure_getc(globaldin,0));
}
/* Returns -1 on failure, 0 on success, 1 if file skipped */
/*
  Sets ftpcode < 0 on failure if failure reason is not server reply code:
    -1: interrupted by user.
    -2: error opening or writing output file (reason in errno).
    -3: failure to make data connection.
    -4: network read error (reason in errno).
*/
struct xx_ftprecv {
    int reply;
    int fcs;
    int rcs;
    int recover;
    int xlate;
    int din;
    int is_retr;
    sig_t oldintr, oldintp;
    char * cmd;
    char * local;
    char * remote;
    char * lmode;
    char * pipename;
    int    tcrflag;
    CK_OFF_T localsize;
};
static struct xx_ftprecv ftprecv;
static int ftprecvret = 0;
static VOID
#ifdef CK_ANSIC
failftprecv(VOID * threadinfo)
#else
failftprecv(threadinfo) VOID * threadinfo;
#endif /* CK_ANSIC */
{
#ifdef NTSIG
    if (threadinfo) {                   /* Thread local storage... */
        TlsSetValue(TlsIndex,threadinfo);
        debug(F100, "docmdfile called with threadinfo block","", 0);
    } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
#endif /* NTSIG */
#ifdef CK_LOGIN
#ifdef IKSD
#ifdef NT
    if (inserver)
      setntcreds();
#endif /* NT */
#endif /* IKSD */
#endif /* CK_LOGIN */
    while (cpend) {
        ftprecv.reply = getreply(0,ftprecv.fcs,ftprecv.rcs,ftp_vbm,0);
    }
    if (data >= 0) {
#ifdef CK_SSL
        if (ssl_ftp_data_active_flag) {
            SSL_shutdown(ssl_ftp_data_con);
            SSL_free(ssl_ftp_data_con);
            ssl_ftp_data_active_flag = 0;
            ssl_ftp_data_con = NULL;
        }
#endif /* CK_SSL */
#ifdef TCPIPLIB
        socket_close(data);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
        shutdown(data, 1+1);
#endif /* USE_SHUTDOWN */
        close(data);
#endif /* TCPIPLIB */
        data = -1;
        globaldin = -1;
    }
    if (ftprecv.oldintr)
      signal(SIGINT, ftprecv.oldintr);
    ftpcode = -1;
    ftprecvret = -1;
#ifndef OS2
    /* TEST ME IN K95 */
    if (havesigint) {
	havesigint = 0;
	debug(F100,"ftp failftprecv chain to trap()...","",0);
	if (ftprecv.oldintr != SIG_IGN)
	  (*ftprecv.oldintr)(SIGINT);
	/* NOTREACHED (I hope!) */
	debug(F100,"ftp failftprecv return from trap()...","",0);
    }
#endif /* OS2 */
    return;
}
static VOID
#ifdef CK_ANSIC
doftprecv(VOID * threadinfo)
#else
doftprecv(threadinfo) VOID * threadinfo;
#endif /* CK_ANSIC */
{
#ifdef NTSIG
    if (threadinfo) {                   /* Thread local storage... */
        TlsSetValue(TlsIndex,threadinfo);
        debug(F100, "docmdfile called with threadinfo block","", 0);
    } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
#endif /* NTSIG */
#ifdef CK_LOGIN
#ifdef IKSD
#ifdef NT
    if (inserver)
      setntcreds();
#endif /* NT */
#endif /* IKSD */
#endif /* CK_LOGIN */
#ifndef COMMENT
    if (!out2screen && !ftprecv.pipename) {
	int x;
	char * local;
	local = ftprecv.local;
	x = zchko(local);
        if (x < 0) {
            if ((!dpyactive || ftp_deb))
              fprintf(stderr,
		      "Temporary file %s: %s\n", ftprecv.local, ck_errstr());
            signal(SIGINT, ftprecv.oldintr);
            ftpcode = -2;
            ftprecvret = -1;
#ifdef NTSIG
            ckThreadEnd(threadinfo);
#endif /* NTSIG */
            return;
        }
    }
#endif /* COMMENT */
    changetype((!ftprecv.is_retr) ? FTT_ASC : ftp_typ, 0);
    if (initconn()) {                   /* Initialize the data connection */
        signal(SIGINT, ftprecv.oldintr);
        ftpcode = -1;
        ftprecvret = -3;
#ifdef NTSIG
        ckThreadEnd(threadinfo);
#endif /* NTSIG */
        return;
    }
    secure_getc(0,1);			/* Initialize net input buffers */
    ftprecvret = 0;
#ifdef NTSIG
    ckThreadEnd(threadinfo);
#endif /* NTSIG */
}
static VOID
#ifdef CK_ANSIC
failftprecv2(VOID * threadinfo)
#else
failftprecv2(threadinfo) VOID * threadinfo;
#endif /* CK_ANSIC */
{
#ifdef NTSIG
    if (threadinfo) {                   /* Thread local storage... */
        TlsSetValue(TlsIndex,threadinfo);
        debug(F100, "docmdfile called with threadinfo block","", 0);
    } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
#endif /* NTSIG */
#ifdef CK_LOGIN
#ifdef IKSD
#ifdef NT
    if (inserver)
      setntcreds();
#endif /* NT */
#endif /* IKSD */
#endif /* CK_LOGIN */
    /* Cancel using RFC959 recommended IP,SYNC sequence  */
    debug(F100,"ftp recvrequest CANCEL","",0);
#ifdef GFTIMER
    fpfsecs = gftimer();
#endif /* GFTIMER */
#ifdef SIGPIPE
    if (ftprecv.oldintp)
      signal(SIGPIPE, ftprecv.oldintr);
#endif /* SIGPIPE */
    signal(SIGINT, SIG_IGN);
    if (!cpend) {
        ftpcode = -1;
        signal(SIGINT, ftprecv.oldintr);
        ftprecvret = -1;
#ifdef NTSIG
        ckThreadEnd(threadinfo);
#endif /* NTSIG */
        return;
    }
    cancel_remote(ftprecv.din);
#ifdef FTP_TIMEOUT
    if (ftp_timed_out && out2screen && !quiet)
      printf("\n?Timed out.\n");
#endif	/* FTP_TIMEOUT */
    if (ftpcode > -1)
      ftpcode = -1;
    if (data >= 0) {
#ifdef CK_SSL
        if (ssl_ftp_data_active_flag) {
            SSL_shutdown(ssl_ftp_data_con);
            SSL_free(ssl_ftp_data_con);
            ssl_ftp_data_active_flag = 0;
            ssl_ftp_data_con = NULL;
        }
#endif /* CK_SSL */
#ifdef TCPIPLIB
        socket_close(data);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
        shutdown(data, 1+1);
#endif /* USE_SHUTDOWN */
        close(data);
#endif /* TCPIPLIB */
        data = -1;
        globaldin = -1;
    }
    if (!out2screen) {
	int x = 0;
	debug(F111,"ftp failrecv2 zclose",ftprecv.local,keep);
	zclose(ZOFILE);
	switch (keep) {			/* which is... */
	  case SET_AUTO:		/* AUTO */
	    if (curtype == FTT_ASC)	/* Delete file if TYPE A. */
	      x = 1;
	    break;
	  case SET_OFF:			/* DISCARD */
	    x = 1;			/* Delete file, period. */
	    break;
	  default:			/* KEEP */
	    break;
	}
	if (x) {
	    x = zdelet(ftprecv.local);
	    debug(F111,"ftp failrecv2 delete incomplete",ftprecv.local,x);
	}
    }
    if (ftprecv.din) {
#ifdef TCPIPLIB
        socket_close(ftprecv.din);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
        shutdown(ftprecv.din, 1+1);
#endif /* USE_SHUTDOWN */
        close(ftprecv.din);
#endif /* TCPIPLIB */
    }
    signal(SIGINT, ftprecv.oldintr);
    ftprecvret = -1;
    if (havesigint) {
	havesigint = 0;
	debug(F100,"FTP failftprecv2 chain to trap()...","",0);
#ifdef OS2
        debug(F100,"FTP failftprecv2 PostCtrlCSem()...","",0);
        PostCtrlCSem();
#else /* OS2 */
	if (ftprecv.oldintr != SIG_IGN)
	  (*ftprecv.oldintr)(SIGINT);
	/* NOTREACHED (I hope!) */
	debug(F100,"ftp failftprecv2 return from trap()...","",0);
#endif /* OS2 */
    }
}
static VOID
#ifdef CK_ANSIC
doftprecv2(VOID * threadinfo)
#else
doftprecv2(threadinfo) VOID * threadinfo;
#endif /* CK_ANSIC */
{
    register int c, d;
    CK_OFF_T bytes = (CK_OFF_T)0;
    int bare_lfs = 0;
    int blksize = 0;
    ULONG start = 0L, stop;
    char * p;
    static char * rcvbuf = NULL;
    static int rcvbufsiz = 0;
#ifdef CK_URL
    char newname[CKMAXPATH+1];		/* For file dialog */
#endif /* CK_URL */
    extern int adl_ask;
#ifdef FTP_TIMEOUT
    ftp_timed_out = 0;
#endif	/* FTP_TIMEOUT */
    ftprecv.din = -1;
#ifdef NTSIG
    if (threadinfo) {                   /* Thread local storage... */
        TlsSetValue(TlsIndex,threadinfo);
        debug(F100, "docmdfile called with threadinfo block","", 0);
    } else debug(F100, "docmdfile - threadinfo is NULL", "", 0);
#endif /* NTSIG */
#ifdef CK_LOGIN
#ifdef IKSD
#ifdef NT
    if (inserver)
      setntcreds();
#endif /* NT */
#endif /* IKSD */
#endif /* CK_LOGIN */
    if (ftprecv.recover) {                      /* Initiate recovery */
        x = ftpcmd("REST",ckfstoa(ftprecv.localsize),-1,-1,ftp_vbm);
        debug(F111,"ftp reply","REST",x);
        if (x == REPLY_CONTINUE) {
            ftprecv.lmode = "ab";
            rs_len = ftprecv.localsize;
        } else {
            ftprecv.recover = 0;
        }
    }
    /* IMPORTANT: No FTP commands can come between REST and RETR! */
    debug(F111,"ftp recvrequest recover E",ftprecv.remote,ftprecv.recover);
    /* Send the command and get reply */
    debug(F110,"ftp recvrequest cmd",ftprecv.cmd,0);
    debug(F110,"ftp recvrequest remote",ftprecv.remote,0);
    if (ftpcmd(ftprecv.cmd,ftprecv.remote,ftprecv.fcs,ftprecv.rcs,ftp_vbm)
	!= REPLY_PRELIM) {
        signal(SIGINT, ftprecv.oldintr); /* Bad reply, fail. */
        ftprecvret = -1;		/* ftpcode is set by ftpcmd() */
#ifdef NTSIG
        ckThreadEnd(threadinfo);
#endif /* NTSIG */
        return;
    }
    ftprecv.din = dataconn("r");        /* Good reply, open data connection */
    globaldin = ftprecv.din;            /* Global copy of file descriptor */
    if (ftprecv.din == -1) {            /* Check for failure */
        ftpcode = -3;                   /* Code for no data connection */
        ftprecvret = -1;
#ifdef NTSIG
        ckThreadEnd(threadinfo);
#endif /* NTSIG */
        return;
    }
#ifdef CK_URL
    /* In K95 GUI put up a file box */
    if (haveurl && g_url.pth && adl_ask	) { /* Downloading from a URL */
	int x;
	char * preface =
"\r\nIncoming file from FTP server...\r\n\
Please confirm output file specification or supply an alternative:";
	x = uq_file(preface,		/* K95 GUI: Put up file box. */
		    NULL,
		    4,
		    NULL,
		    ftprecv.local ? ftprecv.local : ftprecv.remote,
		    newname,
		    CKMAXPATH+1
		    );
	if (x > 0) {
	    ftprecv.local = newname;	/* Substitute user's file name */
	    if (x == 2)			/* And append if user said to */
	      ftprecv.lmode = "ab";
	}
    }
#endif /* CK_URL */
    x = 1;                              /* Output file open OK? */
    if (ftprecv.pipename) {		/* Command */
        x = zxcmd(ZOFILE,ftprecv.pipename);
        debug(F111,"ftp recvrequest zxcmd",ftprecv.pipename,x);
    } else if (!out2screen) {           /* File */
        struct filinfo xx;
        xx.bs = 0; xx.cs = 0; xx.rl = 0; xx.org = 0; xx.cc = 0;
        xx.typ = 0; xx.os_specific = ""; xx.lblopts = 0;
	/* Append or New */
        xx.dsp = !strcmp(ftprecv.lmode,"ab") ? XYFZ_A : XYFZ_N;
        x = zopeno(ZOFILE,ftprecv.local,NULL,&xx);
        debug(F111,"ftp recvrequest zopeno",ftprecv.local,x);
    }
    if (x < 1) {                        /* Failure to open output file */
        if ((!dpyactive || ftp_deb))
          fprintf(stderr, "local(2): %s: %s\n", ftprecv.local, ck_errstr());
        ftprecvret = -1;
#ifdef NTSIG
        ckThreadEnd(threadinfo);
#endif /* NTSIG */
        return;
    }
    blksize = FTP_BUFSIZ;               /* Allocate input buffer */
    debug(F101,"ftp recvrequest blksize","",blksize);
    debug(F101,"ftp recvrequest rcvbufsiz","",rcvbufsiz);
    if (rcvbufsiz < blksize) {          /* if necessary */
        if (rcvbuf) {
            free(rcvbuf);
            rcvbuf = NULL;
        }
        rcvbuf = (char *)malloc((unsigned)blksize);
        if (!rcvbuf) {
	    debug(F100,"ftp get rcvbuf malloc failed","",0);
            ftpcode = -2;
#ifdef ENOMEM
            errno = ENOMEM;
#endif /* ENOMEM */
            if ((!dpyactive || ftp_deb))
              perror("malloc");
            rcvbufsiz = 0;
            ftprecvret = -1;
#ifdef NTSIG
            ckThreadEnd(threadinfo);
#endif /* NTSIG */
            return;
        }
	debug(F101,"ftp get rcvbuf malloc ok","",blksize);
        rcvbufsiz = blksize;
    }
    debug(F111,"ftp get rcvbufsiz",ftprecv.local,rcvbufsiz);
    ffc = (CK_OFF_T)0;			/* Character counter */
    cps = oldcps = 0L;                  /* Thruput */
    start = gmstimer();                 /* Start time (msecs) */
#ifdef GFTIMER
    rftimer();                          /* Start time (float) */
#endif /* GFTIMER */
    debug(F111,"ftp get type",ftprecv.local,curtype);
    debug(F101,"ftp recvrequest ftp_dpl","",ftp_dpl);
    switch (curtype) {
      case FTT_BIN:                     /* Binary mode */
      case FTT_TEN:                     /* TENEX mode */
        d = 0;
        while (1) {
            errno = 0;
            c = secure_read(ftprecv.din, rcvbuf, rcvbufsiz);
            if (cancelfile) {
                failftprecv2(threadinfo);
#ifdef NTSIG
                ckThreadEnd(threadinfo);
#endif /* NTSIG */
                return;
            }
            if (c < 1)
              break;
#ifdef printf                           /* (What if it isn't?) */
            if (out2screen && !ftprecv.pipename) {
                int i;
                for (i = 0; i < c; i++)
                  printf("%c",rcvbuf[i]);
            } else
#endif /* printf */
              {
                register int i;
                i = 0;
                errno = 0;
                while (i < c) {
                    if (zmchout(rcvbuf[i++]) < 0) {
                        d = i;
                        break;
                    }
                }
            }
            bytes += c;
            ffc += c;
        }
#ifdef FTP_TIMEOUT
	if (c == -3) {
            debug(F100,"ftp recvrequest timeout","",0); 
            bytes = (CK_OFF_T)-1;
	    ftp_timed_out = 1;
	    ftpcode = -3;
	} else
#endif	/* FTP_TIMEOUT */
        if (c < 0) {
            debug(F111,"ftp recvrequest errno",ckitoa(c),errno);
            if (c == -1 && errno != EPIPE)
              if ((!dpyactive || ftp_deb))
                perror("netin");
            bytes = (CK_OFF_T)-1;
            ftpcode = -4;
        }
        if (d < c) {
            ftpcode = -2;
            if ((!dpyactive || ftp_deb)) {
                char * p;
                p = ftprecv.local ? ftprecv.local : ftprecv.pipename;
                if (d < 0)
                  fprintf(stderr,
			  "local(3): %s: %s\n", ftprecv.local, ck_errstr());
                else
                  fprintf(stderr,
			  "%s: short write\n", ftprecv.local);
            }
        }
        break;
      case FTT_ASC:                     /* Text mode */
	debug(F101,"ftp recvrequest TYPE A xlate","",ftprecv.xlate);
#ifndef NOCSETS
        if (ftprecv.xlate) {
            int t;
#ifdef CK_ANSIC
            int (*fn)(char);
#else
            int (*fn)();
#endif /* CK_ANSIC */
            debug(F110,"ftp recvrequest (data)","initxlate",0);
            initxlate(ftprecv.rcs,ftprecv.fcs);         /* (From,To) */
            if (ftprecv.pipename) {
                fn = pipeout;
                debug(F110,"ftp recvrequest ASCII","pipeout",0);
            } else {
                fn = out2screen ? scrnout : putfil;
                debug(F110,"ftp recvrequest ASCII",
                      out2screen ? "scrnout" : "putfil",0);
            }
            while (1) {
		/* Get byte from net */
                c0 = xgnbyte(FC_UCS2,ftprecv.rcs,netgetc);
                if (cancelfile) {
                    failftprecv2(threadinfo);
#ifdef NTSIG
                    ckThreadEnd(threadinfo);
#endif /* NTSIG */
                    return;
                }
                if (c0 < 0)
                  break;
		/* Second byte from net */
                c1 = xgnbyte(FC_UCS2,ftprecv.rcs,netgetc);
                if (cancelfile) {
                    failftprecv2(threadinfo);
#ifdef NTSIG
                    ckThreadEnd(threadinfo);
#endif /* NTSIG */
                    return;
                }
                if (c1 < 0)
                  break;
#ifdef COMMENT
		/* K95: Check whether we need this */
		if (fileorder > 0)	/* Little Endian */
		  bytswap(&c0,&c1);	/* swap bytes*/
#endif /* COMMENT */
#ifdef OS2
                if ( out2screen &&            /* we're translating to UCS-2 */ 
                     !k95stdout && !inserver) /* for the real screen... */     
                {
                    union {
                        USHORT ucs2;
                        UCHAR  bytes[2];
                    } output;
                    output.bytes[0] = c1;
                    output.bytes[1] = c0;
                    VscrnWrtUCS2StrAtt(VCMD,
                                       &output.ucs2,
                                       1,
                                       wherey[VCMD],
                                       wherex[VCMD],
                                       &colorcmd
                                       );
                } else 
#endif /* OS2 */
                {
                    if ((x = xpnbyte(c0,TC_UCS2,ftprecv.fcs,fn)) < 0) break;
                    if ((x = xpnbyte(c1,TC_UCS2,ftprecv.fcs,fn)) < 0) break;
                }
            }
        } else {
#endif /* NOCSETS */
            while (1) {
                c = secure_getc(ftprecv.din,0);
                if (cancelfile
#ifdef FTP_TIMEOUT
		    || ftp_timed_out
#endif	/* FTP_TIMEOUT */
		    ) {
                    failftprecv2(threadinfo);
#ifdef NTSIG
                    ckThreadEnd(threadinfo);
#endif /* NTSIG */
                    return;
                }
                if (c < 0 || c == EOF)
                  break;
#ifdef UNIX
		/* Record format conversion for Unix */
		/* SKIP THIS FOR WINDOWS! */
                if (c == '\n')
                  bare_lfs++;
                while (c == '\r') {
                    bytes++;
                    if ((c = secure_getc(ftprecv.din,0)) != '\n' ||
			ftprecv.tcrflag) {
                        if (cancelfile) {
                            failftprecv2(threadinfo);
#ifdef NTSIG
                            ckThreadEnd(threadinfo);
#endif /* NTSIG */
                            return;
                        }
                        if (c < 0 || c == EOF)
                          goto break2;
                        if (c == '\0') {
                            bytes++;
                            goto contin2;
                        }
                    }
                }
                if (c < 0)
                  break;
#endif /* UNX */
                if (out2screen && !ftprecv.pipename)
#ifdef printf
                  printf("%c",(char)c);
#else
                  putchar((char)c);
#endif /* printf */
                else
                  if ((d = zmchout(c)) < 0)
                    break;
                bytes++;
                ffc++;
              contin2:
                ;
            }
          break2:
            if (bare_lfs && (!dpyactive || ftp_deb)) {
                printf("WARNING! %d bare linefeeds received in ASCII mode\n",
                       bare_lfs);
                printf("File might not have transferred correctly.\n");
            }
            if (ftprecv.din == -1) {
                bytes = (CK_OFF_T)-1;
            }
            if (c == -2)
              bytes = (CK_OFF_T)-1;
            break;
#ifndef NOCSETS
        }
#endif /* NOCSETS */
    }
    if (ftprecv.pipename || !out2screen) {
	zclose(ZOFILE);			/* Close the file */
	debug(F111,"doftprecv2 zclose ftpcode",ftprecv.local,ftpcode);
	if (ftpcode < 0) {		/* If download failed */
	    int x = 0;
	    switch (keep) {		/* which is... */
	      case SET_AUTO:		/* AUTO */
		if (curtype == FTT_ASC) /* Delete file if TYPE A. */
		  x = 1;
		break;
	      case SET_OFF:		/* DISCARD */
		x = 1;			/* Delete file, period. */
		break;
	      default:			/* KEEP */
		break;
	    }
	    if (x) {
		x = zdelet(ftprecv.local);
		debug(F111,"ftp get delete incomplete",ftprecv.local,x);
	    }
	}
    }
    signal(SIGINT, ftprecv.oldintr);
#ifdef SIGPIPE
    if (ftprecv.oldintp)
      signal(SIGPIPE, ftprecv.oldintp);
#endif /* SIGPIPE */
    stop = gmstimer();
#ifdef GFTIMER
    fpfsecs = gftimer();
#endif /* GFTIMER */
    tfc += ffc;
#ifdef TCPIPLIB
    socket_close(ftprecv.din);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
    shutdown(ftprecv.din, 1+1);
#endif /* USE_SHUTDOWN */
    close(ftprecv.din);
#endif /* TCPIPLIB */
    ftprecv.reply = getreply(0,ftprecv.fcs,ftprecv.rcs,ftp_vbm,0);
    ftprecvret = ((ftpcode < 0 || ftprecv.reply == REPLY_TRANSIENT || 
                   ftprecv.reply == REPLY_ERROR) ? -1 : 0);
#ifdef NTSIG
     ckThreadEnd(threadinfo);
#endif /* NTSIG */
}
static int
recvrequest(cmd, local, remote, lmode, printnames, recover, pipename,
            xlate, fcs, rcs)
    char *cmd, *local, *remote, *lmode, *pipename;
    int printnames, recover, xlate, fcs, rcs;
{
#ifdef NT
    struct _stat stbuf;
#else /* NT */
    struct stat stbuf;
#endif /* NT */
#ifdef DEBUG
    if (deblog) {
        debug(F111,"ftp recvrequest cmd",cmd,recover);
        debug(F110,"ftp recvrequest local ",local,0);
        debug(F111,"ftp recvrequest remote",remote,ftp_typ);
        debug(F110,"ftp recvrequest pipename ",pipename,0);
        debug(F101,"ftp recvrequest xlate","",xlate);
        debug(F101,"ftp recvrequest fcs","",fcs);
        debug(F101,"ftp recvrequest rcs","",rcs);
    }
#endif /* DEBUG */
    ftprecv.localsize = (CK_OFF_T)0;
    if (remfile) {                      /* See remcfm(), remtxt() */
        if (rempipe) {
            pipename = remdest;
        } else {
            local = remdest;
            if (remappd) lmode = "ab";
        }
    }
    out2screen = 0;
    if (!cmd) cmd = "";                 /* Core dump prevention */
    if (!remote) remote = "";
    if (!lmode) lmode = "";
    if (pipename) {                     /* No recovery for pipes. */
        recover = 0;
        if (!local)
          local = pipename;
    } else {
        if (!local)                     /* Output to screen? */
          local = "-";
        out2screen = !strcmp(local,"-");
    }
    debug(F101,"ftp recvrequest out2screen","",out2screen);
#ifdef OS2
    if ( ftp_xla && out2screen && !k95stdout && !inserver )
        fcs = FC_UCS2;
#endif /* OS2 */
    if (out2screen)                     /* No recovery to screen */
      recover = 0;
    if (!ftp_typ)                       /* No recovery in text mode */
      recover = 0;
    ftprecv.is_retr = (strcmp(cmd, "RETR") == 0);
    if (!ftprecv.is_retr)               /* No recovery except for RETRieve */
      recover = 0;
#ifdef COMMENT
    if (!out2screen && !pipename && ftprecv.is_retr) { /* To real file */
        if (recursive && ckstrchr(local,'/')) {
	    
        }
    }
#endif /* COMMENT */
    ftprecv.localsize = (CK_OFF_T)0;	/* Local file size */
    rs_len = (CK_OFF_T)0;		/* Recovery point */
    debug(F101,"ftp recvrequest recover","",recover);
    if (recover) {                      /* Recovering... */
        if (stat(local, &stbuf) < 0) {  /* Can't stat local file */
	    debug(F101,"ftp recvrequest recover stat failed","",errno);
            recover = 0;                /* So cancel recovery */
        } else {                        /* Have local file info */
            ftprecv.localsize = stbuf.st_size;  /* Get size */
	    /* Remote file smaller than local */
            if (fsize < ftprecv.localsize) {
		debug(F101,"ftp recvrequest recover remote smaller","",fsize);
                recover = 0;            /* Recovery can't work */
            } else if (fsize == ftprecv.localsize) { /* Sizes are equal */
                debug(F111,"ftp recvrequest recover equal size",
		      remote,ftprecv.localsize);
                return(1);
            }
#ifdef COMMENT
/*
  The problem here is that the original partial file never got its date
  set, either because FTP DATES was OFF, or because the partial file was
  downloaded by some other program that doesn't set local file dates, or
  because Kermit only sets the file's date when the download was complete
  and successful.  In all these cases, the local file has a later time
  than the remote.
*/
            if (recover) {              /* Remote is bigger */
                x = chkmodtime(local,remote,0); /* Check file dates */
                debug(F111,"ftp recvrequest chkmodtime",remote,x);
                if (x != 1)		/* Dates must be equal! */
                  recover = 0;          /* If not, get whole file */
            }
#endif /* COMMENT */
        }
        debug(F111,"ftp recvrequest recover",remote,recover);
    }
#ifdef FTP_PROXY
    if (proxy && ftprecv.is_retr)
      return(proxtrans(cmd, local ? local : remote, remote));
#endif /* FTP_PROXY */
    ftprecv.tcrflag = (feol != CK_CR) && ftprecv.is_retr;
    ftprecv.reply = 0;
    ftprecv.fcs = fcs;
    ftprecv.rcs = rcs;
    ftprecv.recover = recover;
    ftprecv.xlate = xlate;
    ftprecv.cmd = cmd;
    ftprecv.local = local;
    ftprecv.remote = remote;
    ftprecv.lmode = lmode;
    ftprecv.pipename = pipename;
    ftprecv.oldintp = NULL;
    ftpcode = 0;
    havesigint = 0;
    ftprecv.oldintr = signal(SIGINT, cancelrecv);
    if (cc_execute(ckjaddr(recvcancel), doftprecv, failftprecv) < 0)
      return -1;
#ifdef FTP_TIMEOUT
    debug(F111,"ftp recvrequest ftprecvret",remote,ftprecvret);
    debug(F111,"ftp recvrequest ftp_timed_out",remote,ftp_timed_out);
    if (ftp_timed_out)
      ftprecvret = -1;
#endif	/* FTP_TIMEOUT */
    if (ftprecvret < 0)
      return -1;
    if (cc_execute(ckjaddr(recvcancel), doftprecv2, failftprecv2) < 0)
      return -1;
    return ftprecvret;
}
/*
 * Need to start a listen on the data channel before we send the command,
 * otherwise the server's connect may fail.
 */
static int
initconn() {
    register char *p, *a;
    int result, tmpno = 0;
    int on = 1;
    GSOCKNAME_T len;
#ifndef NO_PASSIVE_MODE
    int a1,a2,a3,a4,p1,p2;
    if (passivemode) {
        data = socket(AF_INET, SOCK_STREAM, 0);
        globaldin = data;
        if (data < 0) {
            perror("ftp: socket");
            return(-1);
        }
        if (ftpcmd("PASV",NULL,0,0,ftp_vbm) != REPLY_COMPLETE) {
            printf("Passive mode refused\n");
            passivemode = 0;
            return(initconn());
        }
/*
  Now we have a string of comma-separated one-byte unsigned integer values,
  The first four are the an IP address.  The fifth is the MSB of the port
  number, the sixth is the LSB.  From that we can make a sockaddr_in.
*/
        if (sscanf(pasv,"%d,%d,%d,%d,%d,%d",&a1,&a2,&a3,&a4,&p1,&p2) != 6) {
            printf("Passive mode address scan failure\n");
            return(-1);
        };
#ifndef NOHTTP
        if (tcp_http_proxy) {
#ifdef OS2
            char * agent = "Kermit 95"; /* Default user agent */
#else
            char * agent = "C-Kermit";
#endif /* OS2 */
            register struct hostent *hp = 0;
            struct servent *destsp;
            char host[512], *p, *q;
#ifdef IP_TOS
#ifdef IPTOS_THROUGHPUT
            int tos;
#endif /* IPTOS_THROUGHPUT */
#endif /* IP_TOS */
            int s;
#ifdef DEBUG
            extern int debtim;
            int xdebtim;
            xdebtim = debtim;
            debtim = 1;
#endif /* DEBUG */
            ckmakxmsg(proxyhost,HTTPCPYL,ckuitoa(a1),".",ckuitoa(a2),
                      ".",ckuitoa(a3),".",ckuitoa(a4),":",ckuitoa((p1<<8)|p2),
                      NULL,NULL,NULL
                      );
            memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
            for (p = tcp_http_proxy, q=host; *p != '\0' && *p != ':'; p++, q++)
              *q = *p;
            *q = '\0';
            hisctladdr.sin_addr.s_addr = inet_addr(host);
            if (hisctladdr.sin_addr.s_addr != INADDR_NONE) /* 2010-03-29 */
	    {
                debug(F110,"initconn A",host,0);
                hisctladdr.sin_family = AF_INET;
            } else {
                debug(F110,"initconn B",host,0);
                hp = gethostbyname(host);
#ifdef HADDRLIST
                hp = ck_copyhostent(hp); /* make safe copy that won't change */
#endif /* HADDRLIST */
                if (hp == NULL) {
                    fprintf(stderr, "ftp: %s: Unknown host\n", host);
                    ftpcode = -1;
#ifdef DEBUG
                    debtim = xdebtim;
#endif /* DEBUG */
                    return(0);
                }
                hisctladdr.sin_family = hp->h_addrtype;
#ifdef HADDRLIST
                memcpy((char *)&hisctladdr.sin_addr, hp->h_addr_list[0],
                       sizeof(hisctladdr.sin_addr));
#else /* HADDRLIST */
                memcpy((char *)&hisctladdr.sin_addr, hp->h_addr,
                       sizeof(hisctladdr.sin_addr));
#endif /* HADDRLIST */
            }
            data = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
            debug(F101,"initconn socket","",data);
            if (data < 0) {
                perror("ftp: socket");
                ftpcode = -1;
#ifdef DEBUG
                debtim = xdebtim;
#endif /* DEBUG */
                return(0);
            }
            if (*p == ':')
              p++;
            else
              p = "http";
            destsp = getservbyname(p,"tcp");
            if (destsp)
              hisctladdr.sin_port = destsp->s_port;
            else if (p)
              hisctladdr.sin_port = htons(atoi(p));
            else
              hisctladdr.sin_port = htons(80);
            errno = 0;
#ifdef HADDRLIST
            debug(F100,"initconn HADDRLIST","",0);
            while
#else
            debug(F100,"initconn no HADDRLIST","",0);
            if
#endif /* HADDRLIST */
              (connect(data, (struct sockaddr *)&hisctladdr,
                       sizeof (hisctladdr)) < 0) {
                  debug(F101,"initconn connect failed","",errno);
#ifdef HADDRLIST
                  if (hp && hp->h_addr_list[1]) {
                      int oerrno = errno;
                      fprintf(stderr,
                              "ftp: connect to address %s: ",
                              inet_ntoa(hisctladdr.sin_addr)
                              );
                      errno = oerrno;
                      perror("ftphookup");
                      hp->h_addr_list++;
                      memcpy((char *)&hisctladdr.sin_addr,
                             hp->h_addr_list[0],
                             sizeof(hisctladdr.sin_addr));
                      fprintf(stdout, "Trying %s...\n",
                              inet_ntoa(hisctladdr.sin_addr));
#ifdef TCPIPLIB
                      socket_close(data);
#else /* TCPIPLIB */
                      close(data);
#endif /* TCPIPLIB */
                      data = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
                      if (data < 0) {
                          perror("ftp: socket");
                          ftpcode = -1;
#ifdef DEBUG
                          debtim = xdebtim;
#endif /* DEBUG */
                          return(0);
                      }
                      continue;
                  }
#endif /* HADDRLIST */
                  perror("ftp: connect");
                  ftpcode = -1;
                  goto bad;
              }
            if (http_connect(data,
                             tcp_http_proxy_agent ?
			       tcp_http_proxy_agent :
			         agent,
			     NULL,
                             tcp_http_proxy_user,
                             tcp_http_proxy_pwd,
                             0,
                             proxyhost
                             ) < 0) {
#ifdef TCPIPLIB
                socket_close(data);
#else /* TCPIPLIB */
                close(data);
#endif /* TCPIPLIB */
                perror("ftp: connect");
                ftpcode = -1;
                goto bad;
            }
        } else
#endif /* NOHTTP */
        {
            data_addr.sin_family = AF_INET;
            data_addr.sin_addr.s_addr = htonl((a1<<24)|(a2<<16)|(a3<<8)|a4);
            data_addr.sin_port = htons((p1<<8)|p2);
            if (connect(data,
                        (struct sockaddr *)&data_addr,
                        sizeof(data_addr)) < 0
                ) {
                perror("ftp: connect");
                return(-1);
            }
        }
        debug(F100,"initconn connect ok","",0);
#ifdef IP_TOS
#ifdef IPTOS_THROUGHPUT
        on = IPTOS_THROUGHPUT;
        if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
          perror("ftp: setsockopt TOS (ignored)");
#endif /* IPTOS_THROUGHPUT */
#endif /* IP_TOS */
        memcpy(&hisdataaddr,&data_addr,sizeof(struct sockaddr_in));
        return(0);
    }
#endif /* NO_PASSIVE_MODE */
  noport:
    memcpy(&data_addr,&myctladdr,sizeof(struct sockaddr_in));
    if (sendport)
      data_addr.sin_port = 0;   /* let system pick one */
    if (data != -1) {
#ifdef TCPIPLIB
        socket_close(data);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
        shutdown(data, 1+1);
#endif /* USE_SHUTDOWN */
        close(data);
#endif /* TCPIPLIB */
    }
    data = socket(AF_INET, SOCK_STREAM, 0);
    globaldin = data;
    if (data < 0) {
        perror("ftp: socket");
        if (tmpno)
          sendport = 1;
        return(-1);
    }
    if (!sendport) {
        if (setsockopt(data,
                       SOL_SOCKET,
                       SO_REUSEADDR,
                       (char *)&on,
                       sizeof (on)
                       ) < 0
            ) {
            perror("ftp: setsockopt (reuse address)");
            goto bad;
        }
    }
    if (bind(data, (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) {
        perror("ftp: bind");
        goto bad;
    }
    len = sizeof (data_addr);
    if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
        perror("ftp: getsockname");
        goto bad;
    }
    if (listen(data, 1) < 0) {
        perror("ftp: listen");
        goto bad;
    }
    if (sendport) {
        a = (char *)&data_addr.sin_addr;
        p = (char *)&data_addr.sin_port;
        ckmakxmsg(ftpcmdbuf,FTP_BUFSIZ,"PORT ",
                  UC(a[0]),",",UC(a[1]),",", UC(a[2]),",", UC(a[3]),",",
                  UC(p[0]),",", UC(p[1]));
        result = ftpcmd(ftpcmdbuf,NULL,0,0,ftp_vbm);
        if (result == REPLY_ERROR && sendport) {
            sendport = 0;
            tmpno = 1;
            goto noport;
        }
        return(result != REPLY_COMPLETE);
    }
    if (tmpno)
      sendport = 1;
#ifdef IP_TOS
#ifdef IPTOS_THROUGHPUT
    on = IPTOS_THROUGHPUT;
    if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
      perror("ftp: setsockopt TOS (ignored)");
#endif
#endif
    return(0);
  bad:
#ifdef TCPIPLIB
    socket_close(data);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
    shutdown(data, 1+1);
#endif /* USE_SHUTDOWN */
    close(data);
#endif /* TCPIPLIB */
    data = -1;
    globaldin = data;
    if (tmpno)
      sendport = 1;
    return(-1);
}
#ifdef CK_SSL
static int
ssl_dataconn() {
    if (ssl_ftp_data_con!=NULL) {       /* Do SSL */
        SSL_free(ssl_ftp_data_con);
        ssl_ftp_data_con=NULL;
    }
    ssl_ftp_data_con=(SSL *)SSL_new(ssl_ftp_ctx);
    SSL_set_fd(ssl_ftp_data_con,data);
    SSL_set_verify(ssl_ftp_data_con,ssl_verify_flag,NULL);
    SSL_copy_session_id(ssl_ftp_data_con,ssl_ftp_con);
    if (ssl_debug_flag) {
        fprintf(stderr,"=>START SSL connect on DATA\n");
        fflush(stderr);
    }
    if (SSL_connect(ssl_ftp_data_con) <= 0) {
        static char errbuf[1024];
        ckmakmsg(errbuf,1024,"ftp: SSL_connect DATA error: ",
                  ERR_error_string(ERR_get_error(),NULL),NULL,NULL);
        fprintf(stderr,"%s\n", errbuf);
        fflush(stderr);
#ifdef TCPIPLIB
        socket_close(data);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
        shutdown(data, 1+1);
#endif /* USE_SHUTDOWN */
        close(data);
#endif /* TCPIPLIB */
        data = -1;
        globaldin = data;
        return(-1);
    } else {
        ssl_ftp_data_active_flag=1;
        if (!ssl_certsok_flag &&
	    (ssl_verify_flag & SSL_VERIFY_PEER) && /* JEA 2013-12-10 */
	    !tls_is_krb5(2)) {
            char *subject = ssl_get_subject_name(ssl_ftp_data_con);
            if (!subject) {
                if (ssl_verify_flag & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
                    debug(F110,"dataconn","[SSL _- FAILED]",0);
                    ssl_ftp_data_active_flag = 0;
#ifdef TCPIPLIB
                    socket_close(data);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
                    shutdown(data, 1+1);
#endif /* USE_SHUTDOWN */
                    close(data);
#endif /* TCPIPLIB */
                    data = -1;
                    globaldin = data;
                    return(-1);
                } else {
                    if (!out2screen && displa && fdispla) {
                        ftscreen(SCR_TC,0,(CK_OFF_T)0,"Display canceled");
                        /* fdispla = XYFD_B; */
                    }
                    if (uq_ok(
          "Warning: Server didn't provide a certificate on data connection\n",
                               "Continue with file transfer? (Y/N)",
                              3,NULL,0) <= 0) {
                        debug(F110, "dataconn","[SSL - FAILED]",0);
                        ssl_ftp_data_active_flag = 0;
#ifdef TCPIPLIB
                        socket_close(data);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
                        shutdown(data, 1+1);
#endif /* USE_SHUTDOWN */
                        close(data);
#endif /* TCPIPLIB */
                        data = -1;
                        globaldin = data;
                        return(-1);
                    }
                }
            } else {
                if (!out2screen && displa && fdispla == XYFD_C) {
                    ftscreen(SCR_TC,0,(CK_OFF_T)0,"Display canceled");
                    /* fdispla = XYFD_B; */
                }
                if (ssl_check_server_name(ssl_ftp_data_con,ftp_user_host)) {
                    debug(F110,"dataconn","[SSL - FAILED]",0);
                    ssl_ftp_data_active_flag = 0;
#ifdef TCPIPLIB
                    socket_close(data);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
                    shutdown(data, 1+1);
#endif /* USE_SHUTDOWN */
                    close(data);
#endif /* TCPIPLIB */
                    data = -1;
                    globaldin = data;
                    return(-1);
                }
            }
        }
        debug(F110,"dataconn","[SSL - OK]",0);
#ifdef COMMENT
        /* This messes up the full screen file transfer display */
        ssl_display_connect_details(ssl_ftp_con,0,ssl_verbose_flag);
#endif /* COMMENT */
    }
    if (ssl_debug_flag) {
        fprintf(stderr,"=>DONE SSL connect on DATA\n");
        fflush(stderr);
    }
    return(data);
}
#endif /* CK_SSL */
static int
dataconn(lmode) char *lmode; {
    int s;
#ifdef IP_TOS
    int tos;
#endif /* IP_TOS */
#ifdef UCX50
    static u_int fromlen;
#else
    static SOCKOPT_T fromlen;
#endif /* UCX50 */
    fromlen = sizeof(hisdataaddr);
#ifndef NO_PASSIVE_MODE
    if (passivemode) {
#ifdef CK_SSL
        ssl_ftp_data_active_flag=0;
        if (ssl_ftp_active_flag &&
            (ssl_ftp_proxy || ftp_dpl == FPL_PRV))
          return(ssl_dataconn());
#endif /* CK_SSL */
        return(data);
    }
#endif /* NO_PASSIVE_MODE */
    s = accept(data, (struct sockaddr *) &hisdataaddr, &fromlen);
    if (s < 0) {
        perror("ftp: accept");
#ifdef TCPIPLIB
        socket_close(data);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
        shutdown(data, 1+1);
#endif /* USE_SHUTDOWN */
        close(data);
#endif /* TCPIPLIB */
        data = -1;
        globaldin = data;
        return(-1);
    }
#ifdef TCPIPLIB
    socket_close(data);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
    shutdown(data, 1+1);
#endif /* USE_SHUTDOWN */
    close(data);
#endif /* TCPIPLIB */
    data = s;
    globaldin = data;
#ifdef IP_TOS
#ifdef IPTOS_THROUGHPUT
    tos = IPTOS_THROUGHPUT;
    if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
      perror("ftp: setsockopt TOS (ignored)");
#endif /* IPTOS_THROUGHPUT */
#endif /* IP_TOS */
#ifdef CK_SSL
    ssl_ftp_data_active_flag=0;
    if (ssl_ftp_active_flag &&
        (ssl_ftp_proxy || ftp_dpl == FPL_PRV))
      return(ssl_dataconn());
#endif /* CK_SSL */
    return(data);
}
#ifdef FTP_PROXY
static sigtype
pscancel(sig) int sig; {
    cancelfile++;
}
static VOID
pswitch(flag) int flag; {
    extern int proxy;
    sig_t oldintr;
    static struct comvars {
        int connect;
        char name[MAXHOSTNAMELEN];
        struct sockaddr_in mctl;
        struct sockaddr_in hctl;
        FILE *in;
        FILE *out;
        int tpe;
        int curtpe;
        int cpnd;
        int sunqe;
        int runqe;
        int mcse;
        int ntflg;
        char nti[17];
        char nto[17];
        int mapflg;
        char mi[CKMAXPATH];
        char mo[CKMAXPATH];
        char *authtype;
        int clvl;
        int dlvl;
#ifdef FTP_KRB4
        des_cblock session;
        des_key_schedule ftp_sched;
#endif /* FTP_KRB4 */
#ifdef FTP_GSSAPI
        gss_ctx_id_t gcontext;
#endif /* GSSAPI */
    } proxstruct, tmpstruct;
    struct comvars *ip, *op;
    cancelfile = 0;
    oldintr = signal(SIGINT, pscancel);
    if (flag) {
        if (proxy)
          return;
        ip = &tmpstruct;
        op = &proxstruct;
        proxy++;
    } else {
        if (!proxy)
          return;
        ip = &proxstruct;
        op = &tmpstruct;
        proxy = 0;
    }
    ip->connect = connected;
    connected = op->connect;
    if (ftp_host) {
        strncpy(ip->name, ftp_host, MAXHOSTNAMELEN - 1);
        ip->name[MAXHOSTNAMELEN - 1] = '\0';
        ip->name[strlen(ip->name)] = '\0';
    } else
      ip->name[0] = 0;
    ftp_host = op->name;
    ip->hctl = hisctladdr;
    hisctladdr = op->hctl;
    ip->mctl = myctladdr;
    myctladdr = op->mctl;
    ip->in = csocket;
    csocket = op->in;
    ip->out = csocket;
    csocket = op->out;
    ip->tpe = ftp_typ;
    ftp_typ = op->tpe;
    ip->curtpe = curtype;
    curtype = op->curtpe;
    ip->cpnd = cpend;
    cpend = op->cpnd;
    ip->sunqe = ftp_usn;
    ftp_usn = op->sunqe;
    ip->mcse = mcase;
    mcase = op->mcse;
    ip->ntflg = ntflag;
    ntflag = op->ntflg;
    strncpy(ip->nti, ntin, 16);
    (ip->nti)[strlen(ip->nti)] = '\0';
    strcpy(ntin, op->nti);
    strncpy(ip->nto, ntout, 16);
    (ip->nto)[strlen(ip->nto)] = '\0';
    strcpy(ntout, op->nto);
    ip->mapflg = mapflag;
    mapflag = op->mapflg;
    strncpy(ip->mi, mapin, CKMAXPATH - 1);
    (ip->mi)[strlen(ip->mi)] = '\0';
    strcpy(mapin, op->mi);
    strncpy(ip->mo, mapout, CKMAXPATH - 1);
    (ip->mo)[strlen(ip->mo)] = '\0';
    strcpy(mapout, op->mo);
    ip->authtype = auth_type;
    auth_type = op->authtype;
    ip->clvl = ftp_cpl;
    ftp_cpl = op->clvl;
    ip->dlvl = ftp_dpl;
    ftp_dpl = op->dlvl;
    if (!ftp_cpl)
      ftp_cpl = FPL_CLR;
    if (!ftp_dpl)
      ftp_dpl = FPL_CLR;
#ifdef FTP_KRB4
    memcpy(ip->session, ftp_cred.session, sizeof(ftp_cred.session));
    memcpy(ftp_cred.session, op->session, sizeof(ftp_cred.session));
    memcpy(ip->schedule, ftp_sched, sizeof(ftp_sched));
    memcpy(ftp_sched, op->schedule, sizeof(ftp_sched));
#endif /* FTP_KRB4 */
#ifdef FTP_GSSAPI
    ip->gcontext = gcontext;
    gcontext = op->gcontext;
#endif /* GSSAPI */
    signal(SIGINT, oldintr);
    if (cancelfile) {
        cancelfile = 0;
        debug(F101,"pswitch cancelfile B","",cancelfile);
        (*oldintr)(SIGINT);
    }
}
static sigtype
cancelpt(sig) int sig; {
    printf("\n");
    fflush(stdout);
    ptabflg++;
    cancelfile = 0;
#ifndef OS2
    longjmp(ptcancel, 1);
#else
    PostCtrlCSem();
#endif /* OS2 */
}
void
proxtrans(cmd, local, remote, unique) char *cmd, *local, *remote; int unique; {
    sig_t oldintr;
    int secndflag = 0, prox_type, nfnd;
    char *cmd2;
#ifdef BSDSELECT
    fd_set mask;
#endif /* BSDSELECT */
    sigtype cancelpt();
    if (strcmp(cmd, "RETR"))
      cmd2 = "RETR";
    else
      cmd2 = unique ? "STOU" : "STOR";
    if ((prox_type = type) == 0) {
        if (servertype == SYS_UNIX && unix_proxy)
          prox_type = FTT_BIN;
        else
          prox_type = FTT_ASC;
    }
    if (curtype != prox_type)
      changetype(prox_type, 1);
    if (ftpcmd("PASV",NULL,0,0,ftp_vbm) != REPLY_COMPLETE) {
        printf("Proxy server does not support third party transfers.\n");
        return;
    }
    pswitch(0);
    if (!connected) {
        printf("No primary connection\n");
        pswitch(1);
        ftpcode = -1;
        return;
    }
    if (curtype != prox_type)
      changetype(prox_type, 1);
    if (ftpcmd("PORT",pasv,-1,-1,ftp_vbm) != REPLY_COMPLETE) {
        pswitch(1);
        return;
    }
    /* Replace with calls to cc_execute() */
    if (setjmp(ptcancel))
      goto cancel;
    oldintr = signal(SIGINT, cancelpt);
    if (ftpcmd(cmd,remote,-1,-1,ftp_vbm) != PRELIM) {
        signal(SIGINT, oldintr);
        pswitch(1);
        return;
    }
    sleep(2000);
    pswitch(1);
    secndflag++;
    if (ftpcmd(cmd2,local,-1,-1,ftp_vbm) != PRELIM)
      goto cancel;
    ptflag++;
    getreply(0,-1,-1,ftp_vbm,0);
    pswitch(0);
    getreply(0,-1,-1,ftp_vbm,0);
    signal(SIGINT, oldintr);
    pswitch(1);
    ptflag = 0;
    return;
  cancel:
    signal(SIGINT, SIG_IGN);
    ptflag = 0;
    if (strcmp(cmd, "RETR") && !proxy)
      pswitch(1);
    else if (!strcmp(cmd, "RETR") && proxy)
      pswitch(0);
    if (!cpend && !secndflag) {  /* only here if cmd = "STOR" (proxy=1) */
        if (ftpcmd(cmd2,local,-1,-1,ftp_vbm) != PRELIM) {
            pswitch(0);
            if (cpend)
              cancel_remote(0);
        }
        pswitch(1);
        if (ptabflg)
          ftpcode = -1;
        signal(SIGINT, oldintr);
        return;
    }
    if (cpend)
      cancel_remote(0);
    pswitch(!proxy);
    if (!cpend && !secndflag) {  /* only if cmd = "RETR" (proxy=1) */
        if (ftpcmd(cmd2,local,-1,-1,ftp_vbm) != PRELIM) {
            pswitch(0);
            if (cpend)
              cancel_remote(0);
            pswitch(1);
            if (ptabflg)
              ftpcode = -1;
            signal(SIGINT, oldintr);
            return;
        }
    }
    if (cpend)
      cancel_remote(0);
    pswitch(!proxy);
    if (cpend) {
#ifdef BSDSELECT
        FD_ZERO(&mask);
        FD_SET(csocket, &mask);
        if ((nfnd = empty(&mask, 10)) <= 0) {
            if (nfnd < 0) {
                perror("cancel");
            }
            if (ptabflg)
              ftpcode = -1;
            lostpeer();
        }
#else /* BSDSELECT */
#ifdef IBMSELECT
        if ((nfnd = empty(&csocket, 1, 10)) <= 0) {
            if (nfnd < 0) {
                perror("cancel");
            }
            if (ptabflg)
              ftpcode = -1;
            lostpeer();
        }
#endif /* IBMSELECT */
#endif /* BSDSELECT */
        getreply(0,-1,-1,ftp_vbm,0);
        getreply(0,-1,-1,ftp_vbm,0);
    }
    if (proxy)
      pswitch(0);
    pswitch(1);
    if (ptabflg)
      ftpcode = -1;
    signal(SIGINT, oldintr);
}
#endif /* FTP_PROXY */
#ifdef FTP_SECURITY
#ifdef FTP_GSSAPI
#ifdef COMMENT
/* ck_gss_mech_krb5 is not declared anywhere */
struct {
    CONST gss_OID_desc * CONST * mech_type;
    char *service_name;
} gss_trials[] = {
    { &ck_gss_mech_krb5, "ftp" },
    { &ck_gss_mech_krb5, "host" },
};
#else
/* This matches what is declared above */
struct {
    CONST gss_OID_desc * CONST * mech_type;
    char *service_name;
} gss_trials[] = {
    { &gss_mech_krb5, "ftp" },
    { &gss_mech_krb5, "host" },
};
#endif	/* COMMENT */
int n_gss_trials = sizeof(gss_trials)/sizeof(gss_trials[0]);
#endif /* FTP_GSSAPI */
static int
ftp_auth() {
    extern int setsafe();
    int j = 0, n;
#ifdef FTP_KRB4
    char *service, inst[INST_SZ];
    ULONG cksum;
    ULONG checksum = (ULONG) getpid();
    CHAR out_buf[FTP_BUFSIZ];
    int i;
#else /* FTP_KRB4 */
#ifdef FTP_GSSAPI
    CHAR out_buf[FTP_BUFSIZ];
    int i;
#endif /* FTP_GSSAPI */
#endif /* FTP_KRB4 */
    if (ssl_ftp_proxy)                  /* Do not allow AUTH over SSL proxy */
        return(0);
    if (auth_type)
      return(1);                        /* auth already succeeded */
    /* Try each auth type as specified by the end user */
    for (j = 0; j < 8 && ftp_auth_type[j] != 0; j++) {
#ifdef FTP_GSSAPI
        if (ftp_auth_type[j] == FTA_GK5 && ck_gssapi_is_installed()) {
            n = ftpcmd("AUTH GSSAPI",NULL,0,0,ftp_vbm);
            if (n == REPLY_CONTINUE) {
                OM_uint32 maj_stat, min_stat;
                gss_name_t target_name;
                gss_buffer_desc send_tok, recv_tok, *token_ptr;
                char stbuf[FTP_BUFSIZ];
                int comcode, trial;
                struct gss_channel_bindings_struct chan;
                char * realm = NULL;
                char tgt[256];
                chan.initiator_addrtype = GSS_C_AF_INET; /* OM_uint32  */
                chan.initiator_address.length = 4;
                chan.initiator_address.value = &myctladdr.sin_addr.s_addr;
                chan.acceptor_addrtype = GSS_C_AF_INET; /* OM_uint32 */
                chan.acceptor_address.length = 4;
                chan.acceptor_address.value = &hisctladdr.sin_addr.s_addr;
                chan.application_data.length = 0;
                chan.application_data.value = 0;
                if (!quiet)
                  printf("GSSAPI accepted as authentication type\n");
                realm = ck_krb5_realmofhost(ftp_user_host);
                if (realm) {
                    ckmakmsg(tgt,sizeof(tgt),"krbtgt/",realm,"@",realm);
                    debug(F110,"ftp_auth(GSSAPI) TGT",tgt,0);
                    if ( krb5_autoget &&
                         !((ck_krb5_tkt_isvalid(NULL,tgt) > 0) ||
                            (ck_krb5_is_tgt_valid() > 0)) )
                        ck_krb5_autoget_TGT(realm);
                }
                /* Blob from gss-client */
                for (trial = 0; trial < n_gss_trials; trial++) {
                    /* ftp@hostname first, the host@hostname */
                    /* the V5 GSSAPI binding canonicalizes this for us... */
                    ckmakmsg(stbuf,FTP_BUFSIZ,
                             gss_trials[trial].service_name,
                             "@",
                             ftp_user_host,
                             NULL
                             );
                    if (ftp_deb)
                      fprintf(stderr,
                              "Authenticating to <%s>...\n", stbuf);
                    send_tok.value = stbuf;
                    send_tok.length = strlen(stbuf);
                    maj_stat = gss_import_name(&min_stat, &send_tok,
                                               gss_nt_service_name,
                                               &target_name
                                               );
                    if (maj_stat != GSS_S_COMPLETE) {
                        user_gss_error(maj_stat, min_stat, "parsing name");
                        secure_error("name parsed <%s>\n", stbuf);
                        continue;
                    }
                    token_ptr = GSS_C_NO_BUFFER;
                    gcontext = GSS_C_NO_CONTEXT; /* structure copy */
                    do {
                        if (ftp_deb)
                          fprintf(stderr, "calling gss_init_sec_context\n");
                        maj_stat =
                          gss_init_sec_context(&min_stat,
                                               GSS_C_NO_CREDENTIAL,
                                               &gcontext,
                                               target_name,
                                               (gss_OID) *
                                                 gss_trials[trial].mech_type,
                                               GSS_C_MUTUAL_FLAG |
                                               GSS_C_REPLAY_FLAG |
                                               (ftp_cfw ?
                                                GSS_C_DELEG_FLAG : 0),
                                               0,
                                                /* channel bindings */
                                                (krb5_d_no_addresses ?
                                                  GSS_C_NO_CHANNEL_BINDINGS :
                                                  &chan),
                                                token_ptr,
                                               NULL,    /* ignore mech type */
                                               &send_tok,
                                               NULL,    /* ignore ret_flags */
                                               NULL
                                               );       /* ignore time_rec */
                        if (maj_stat != GSS_S_COMPLETE &&
                            maj_stat != GSS_S_CONTINUE_NEEDED) {
                            if (trial == n_gss_trials-1)
                              user_gss_error(maj_stat,
                                             min_stat,
                                             "initializing context"
                                             );
                            gss_release_name(&min_stat, &target_name);
                            /* maybe we missed on the service name */
                            goto outer_loop;
                        }
                        if (send_tok.length != 0) {
                            int len;
                            reply_parse = "ADAT="; /* for ftpcmd() later */
                            len = FTP_BUFSIZ;
                            kerror =
                              radix_encode(send_tok.value,
                                           out_buf,
                                           send_tok.length,
                                           &len,
                                           RADIX_ENCODE
                                           );
                            if (kerror)  {
                                fprintf(stderr,
                                        "Base 64 encoding failed: %s\n",
                                        radix_error(kerror)
                                        );
                                goto gss_complete_loop;
                            }
                            comcode = ftpcmd("ADAT",out_buf,-1,-1,0);
                            if (comcode != REPLY_COMPLETE
                                && comcode != REPLY_CONTINUE /* (335) */
                                ) {
                                if (trial == n_gss_trials-1) {
                                    fprintf(stderr, "GSSAPI ADAT failed\n");
                                    /* force out of loop */
                                    maj_stat = GSS_S_FAILURE;
                                }
                                /*
                                  Backoff to the v1 gssapi is still possible.
                                  Send a new AUTH command.  If that fails,
                                  terminate the loop.
                                */
                                if (ftpcmd("AUTH GSSAPI",NULL,0,0,ftp_vbm)
                                    != REPLY_CONTINUE) {
                                    fprintf(stderr,
                                "GSSAPI ADAT failed, AUTH restart failed\n");
                                    /* force out of loop */
                                    maj_stat = GSS_S_FAILURE;
                                }
                                goto outer_loop;
                            }
                            if (!reply_parse) {
                                fprintf(stderr,
                              "No authentication data received from server\n");
                                if (maj_stat == GSS_S_COMPLETE) {
                                    fprintf(stderr,
                                            "...but no more was needed\n");
                                    goto gss_complete_loop;
                                } else {
                                    user_gss_error(maj_stat,
                                                   min_stat,
                                                   "no reply, huh?"
                                                   );
                                    goto gss_complete_loop;
                                }
                            }
                            len = FTP_BUFSIZ;
                            kerror = radix_encode(reply_parse,out_buf,i,&len,
                                                  RADIX_DECODE);
                            if (kerror) {
                                fprintf(stderr,
                                        "Base 64 decoding failed: %s\n",
                                        radix_error(kerror));
                                goto gss_complete_loop;
                            }
                            /* everything worked */
                            token_ptr = &recv_tok;
                            recv_tok.value = out_buf;
                            recv_tok.length = len;
                            continue;
                            /* get out of loop clean */
                          gss_complete_loop:
                            trial = n_gss_trials-1;
                            gss_release_buffer(&min_stat, &send_tok);
                            gss_release_name(&min_stat, &target_name);
                            goto outer_loop;
                        }
                    } while (maj_stat == GSS_S_CONTINUE_NEEDED);
                  outer_loop:
                    if (maj_stat == GSS_S_COMPLETE)
                      break;
                }
                if (maj_stat == GSS_S_COMPLETE) {
                    printf("GSSAPI authentication succeeded\n");
                    reply_parse = NULL;
                    auth_type = "GSSAPI";
                    return(1);
                } else {
                    fprintf(stderr, "GSSAPI authentication failed\n");
                    reply_parse = NULL;
                }
            } else {
                if (ftp_deb)
                fprintf(stderr, "GSSAPI rejected as an authentication type\n");
                if (ftpcode == 500 || ftpcode == 502)
                    return(0);
            }
        }
#endif /* FTP_GSSAPI */
#ifdef FTP_SRP
        if (ftp_auth_type[j] == FTA_SRP && ck_srp_is_installed()) {
            if (srp_ftp_auth(ftp_user_host,NULL,NULL))
              return(1);
            else if (ftpcode == 500 || ftpcode == 502)
              return(0);
        }
#endif /* FTP_SRP */
#ifdef FTP_KRB4
        if (ftp_auth_type[j] == FTA_K4 && ck_krb4_is_installed()) {
            n = ftpcmd("AUTH KERBEROS_V4",NULL,0,0,ftp_vbm);
            if (n == REPLY_CONTINUE) {
                char tgt[4*REALM_SZ+1];
                int rc;
                if (!quiet)
                  printf("KERBEROS_V4 accepted as authentication type\n");
                ckstrncpy(inst, (char *) krb_get_phost(ftp_user_host),INST_SZ);
                ckstrncpy(ftp_realm,
                          (char *)ck_krb4_realmofhost(ftp_user_host),
                          REALM_SZ
                          );
                ckmakmsg(tgt,sizeof(tgt),"krbtgt.",ftp_realm,"@",ftp_realm);
                rc = ck_krb4_tkt_isvalid(tgt);
                if (rc <= 0 && krb4_autoget)
                  ck_krb4_autoget_TGT(ftp_realm);
                service = "ftp";
                kerror = krb_mk_req(&ftp_tkt,service,inst,ftp_realm,checksum);
                if (kerror == KDC_PR_UNKNOWN) {
                    service = "rcmd";
                    kerror = krb_mk_req(&ftp_tkt,
                                        service,
                                        inst,
                                        ftp_realm,
                                        checksum
                                        );
                }
                if (kerror)
                  fprintf(stderr, "Kerberos V4 krb_mk_req failed: %s\n",
                          krb_get_err_text(kerror));
                if (!kerror) {
                    kerror = krb_get_cred(service, inst, ftp_realm,&ftp_cred);
                    if (kerror)
                      fprintf(stderr, "Kerberos V4 krb_get_cred failed: %s\n",
                              krb_get_err_text(kerror));
                }
                if (!kerror) {
                    int rc;
                    rc = des_key_sched(ftp_cred.session, ftp_sched);
                    if (rc == -1) {
                       printf("?Invalid DES key specified in credentials\r\n");
                       debug(F110,"ftp_auth",
                             "invalid DES Key specified in credentials",0);
                    } else if ( rc == -2 ) {
                        printf("?Weak DES key specified in credentials\r\n");
                        debug(F110,"ftp_auth",
                              "weak DES Key specified in credentials",0);
                    } else if ( rc != 0 ) {
                        printf("?DES Key Schedule not set by credentials\r\n");
                        debug(F110,"ftp_auth",
                              "DES Key Schedule not set by credentials",0);
                    }
                    reply_parse = "ADAT=";
                    i = FTP_BUFSIZ;
                    kerror = radix_encode(ftp_tkt.dat, out_buf, ftp_tkt.length,
                                          &i, RADIX_ENCODE);
                    if (kerror) {
                        fprintf(stderr, "Base 64 encoding failed: %s\n",
                                radix_error(kerror));
                        goto krb4_err;
                    }
                    if (i > FTP_BUFSIZ - 6)
                      printf("?ADAT data too long\n");
                    if (ftpcmd("ADAT",out_buf,-1,-1,0) !=
                        REPLY_COMPLETE) {
                        fprintf(stderr, "Kerberos V4 authentication failed\n");
                        goto krb4_err;
                    }
                    if (!reply_parse) {
                        fprintf(stderr,
                             "No authentication data received from server\n");
                        goto krb4_err;
                    }
                    i = sizeof(out_buf);
                    kerror =
                      radix_encode(reply_parse, out_buf, 0, &i, RADIX_DECODE);
                    if (kerror) {
                        fprintf(stderr, "Base 64 decoding failed: %s\n",
                                radix_error(kerror));
                        goto krb4_err;
                    }
                    kerror = krb_rd_safe(out_buf, i,
#ifdef KRB524
                                         ftp_cred.session,
#else /* KRB524 */
                                         &ftp_cred.session,
#endif /* KRB524 */
                                         &hisctladdr,
                                         &myctladdr,
                                         &ftp_msg_data
                                         );
                    if (kerror) {
                        fprintf(stderr, "Kerberos V4 krb_rd_safe failed: %s\n",
                                krb_get_err_text(kerror));
                        goto krb4_err;
                    }
                    /* fetch the (modified) checksum */
                    memcpy(&cksum, ftp_msg_data.app_data, sizeof(cksum));
                    if (ntohl(cksum) == checksum + 1) {
                        if (ftp_vbm)
                          printf("Kerberos V4 authentication succeeded\n");
                        reply_parse = NULL;
                        auth_type = "KERBEROS_V4";
                        return(1);
                    } else
                      fprintf(stderr,
                              "Kerberos V4 mutual authentication failed\n");
                  krb4_err:
                    reply_parse = NULL;
                }
            } else {
                if (ftp_deb)
		  fprintf(stderr,
                      "KERBEROS_V4 rejected as an authentication type\n");
                if (ftpcode == 500 || ftpcode == 502)
                    return(0);
            }
        }
#endif /* FTP_KRB4 */
#ifdef CK_SSL
        if (ftp_auth_type[j] == FTA_TLS && ck_ssleay_is_installed()) {
#ifdef FTPHOST
            if (!hostcmd) {
                ftpcmd("HOST",ftp_user_host,0,0,0);
                hostcmd = 1;
            }
#endif /* FTPHOST */
            n = ftpcmd("AUTH TLS",NULL,0,0,ftp_vbm);
            if (n != REPLY_COMPLETE)
              n = ftpcmd("AUTH TLS-P",NULL,0,0,ftp_vbm);
            if (n == REPLY_COMPLETE) {
                if (!quiet)
                  printf("TLS accepted as authentication type\n");
                auth_type = "TLS";
                ssl_auth();
                if (ssl_ftp_active_flag ) {
                    ftp_dpl = FPL_CLR;
                    ftp_cpl = FPL_PRV;
                    return(1);
                } else {
                    fprintf(stderr,"TLS authentication failed\n");
                    auth_type = NULL;
#ifdef TCPIPLIB
                    socket_close(csocket);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
                    shutdown(csocket, 1+1);
#endif /* USE_SHUTDOWN */
                    close(csocket);
#endif /* TCPIPLIB */
                    csocket = -1;
                    if (ftp_hookup(ftp_user_host,ftp_port,0) == NULL)
                      return(0);
                }
            } else {
                if (ftp_deb)
		  fprintf(stderr,"TLS rejected as an authentication type\n");
                if (ftpcode == 500 || ftpcode == 502)
                    return(0);
            }
        }
        if (ftp_auth_type[j] == FTA_SSL && ck_ssleay_is_installed()) {
#ifdef FTPHOST
            if (!hostcmd) {
                ftpcmd("HOST",ftp_user_host,0,0,0);
                hostcmd = 1;
            }
#endif /* FTPHOST */
            n = ftpcmd("AUTH SSL",NULL,0,0,ftp_vbm);
            if (n == REPLY_CONTINUE || n == REPLY_COMPLETE) {
                if (!quiet)
                  printf("SSL accepted as authentication type\n");
                auth_type = "SSL";
                ssl_auth();
                if (ssl_ftp_active_flag) {
                    ftp_dpl = FPL_PRV;
                    ftp_cpl = FPL_PRV;
                    setprotbuf(1<<20);
                    return(1);
                } else {
                    fprintf(stderr,"SSL authentication failed\n");
                    auth_type = NULL;
#ifdef TCPIPLIB
                    socket_close(csocket);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
                    shutdown(csocket, 1+1);
#endif /* USE_SHUTDOWN */
                    close(csocket);
#endif /* TCPIPLIB */
                    csocket = -1;
                    if (ftp_hookup(ftp_user_host,ftp_port,0) == NULL)
                      return(0);
                }
	    } else {
                if (ftp_deb)
		  fprintf(stderr, "SSL rejected as an authentication type\n");
                if (ftpcode == 500 || ftpcode == 502)
		  return(0);
            }
        }
#endif /* CK_SSL */
        /* Other auth types go here ... */
    } /* for (j;;) */
    return(0);
}
#endif /* FTP_SECURITY */
static int
#ifdef CK_ANSIC
setprotbuf(unsigned int size)
#else
setprotbuf(size) unsigned int size;
#endif /* CK_ANSIC */
/* setprotbuf */ {
    if (ucbuf)
      free(ucbuf);
    ucbuf = NULL;
    ucbufsiz = 0;
    actualbuf = size;
    while ((ucbuf = (CHAR *)malloc(actualbuf)) == NULL) {
        if (actualbuf)
          actualbuf /= 2;
        else
          return(0);
    }
    ucbufsiz = actualbuf - FUDGE_FACTOR;
    debug(F101,"setprotbuf ucbufsiz","",ucbufsiz);
    if (ucbufsiz < 128) {
        printf("WARNING: tiny ucbufsiz: %d\n",ucbufsiz);
    } else if (ucbufsiz < 0) {
        printf("ERROR: ucbuf allocation failure\n");
        return(-1);
    }
    maxbuf = actualbuf;
    return(1);
}
static int
#ifdef CK_ANSIC
setpbsz(unsigned int size)
#else
setpbsz(size) unsigned int size;
#endif /* CK_ANSIC */
/* setpbsz */ {
    if (!setprotbuf(size)) {
        perror("?Error while trying to malloc PROT buffer:");
#ifdef FTP_SRP
        srp_reset();
#endif /* FTP_SRP */
        ftpclose();
        return(-1);
    }
    reply_parse = "PBSZ=";
    ckmakmsg(ftpcmdbuf,FTP_BUFSIZ,"PBSZ ",
#ifdef CK_SSL
             ssl_ftp_active_flag ? "0" :
#endif /* CK_SSL */
             ckuitoa(actualbuf),NULL,NULL);
    if (ftpcmd(ftpcmdbuf,NULL,0,0,0) != REPLY_COMPLETE) {
        if (connected) {
            printf("?Unable to negotiate PROT buffer size with FTP server\n");
            ftpclose();
        }
        return(-1);
    }
    if (reply_parse) {
        if ((maxbuf = (unsigned int) atol(reply_parse)) > actualbuf)
          maxbuf = actualbuf;
    } else
      maxbuf = actualbuf;
    ucbufsiz = maxbuf - FUDGE_FACTOR;
    debug(F101,"setpbsz ucbufsiz","",ucbufsiz);    
    reply_parse = NULL;
    return(0);
}
static VOID
cancel_remote(din) int din; {
    CHAR buf[FTP_BUFSIZ];
    int x, nfnd;
#ifdef BSDSELECT
    fd_set mask;
#endif /* BSDSELECT */
#ifdef IBMSELECT
    int fds[2], fdcnt = 0;
#endif /* IBMSELECT */
#ifdef DEBUG
    extern int debtim;
    int xdebtim;
    xdebtim = debtim;
    debtim = 1;
#endif /* DEBUG */
    debug(F100,"ftp cancel_remote entry","",0);
#ifdef CK_SSL
    if (ssl_ftp_active_flag) {
        /*
         * Send Telnet IP, Telnet DM but do so inline and within the
         * TLS channel
         */
        int count, error;
        buf[0] = IAC;
        buf[1] = TN_IP;
        buf[2] = IAC;
        buf[3] = TN_DM;
        buf[4] = NUL;
        count = SSL_write(ssl_ftp_con, buf, 4);
        debug(F111,"ftp cancel_remote","SSL_write(IAC IP IAC DM)",count);
        error = SSL_get_error(ssl_ftp_con,count);
        debug(F111,"ftp cancel_remote","SSL_get_error()",error);
        switch (error) {
          case SSL_ERROR_NONE:
            break;
          case SSL_ERROR_WANT_WRITE:
          case SSL_ERROR_WANT_READ:
          case SSL_ERROR_SYSCALL:
#ifdef NT
            {
                int gle = GetLastError();
            }
#endif /* NT */
          case SSL_ERROR_WANT_X509_LOOKUP:
          case SSL_ERROR_SSL:
          case SSL_ERROR_ZERO_RETURN:
          default:
            lostpeer();
            return;
        }
    } else
#endif /* CK_SSL */
    {
        /*
         * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
         * after urgent byte rather than before as is protocol now.
         */
        buf[0] = IAC;
        buf[1] = TN_IP;
        buf[2] = IAC;
        buf[3] = NUL;
        if ((x = send(csocket, (SENDARG2TYPE)buf, 3, MSG_OOB)) != 3)
          perror("cancel");
        debug(F101,"ftp cancel_remote send 1","",x);
        buf[0] = TN_DM;
        x = send(csocket,(SENDARG2TYPE)buf,1,0);
        debug(F101,"ftp cancel_remote send 2","",x);
    }
    x = scommand("ABOR");
    debug(F101,"ftp cancel_remote scommand","",x);
#ifdef BSDSELECT
    FD_ZERO(&mask);
    FD_SET(csocket, &mask);
    if (din) {
        FD_SET(din, &mask);
    }
    nfnd = empty(&mask, 10);
    debug(F101,"ftp cancel_remote empty","",nfnd);
    if ((nfnd) <= 0) {
        if (nfnd < 0) {
            perror("cancel");
        }
#ifdef FTP_PROXY
        if (ptabflg)
          ftpcode = -1;
#endif /* FTP_PROXY */
        lostpeer();
    }
    debug(F110,"ftp cancel_remote","D",0);
    if (din && FD_ISSET(din, &mask)) {
        /* Security: No threat associated with this read. */
        /* But you can't simply read the TLS data stream  */
#ifdef CK_SSL
        if (ssl_ftp_data_active_flag) {
            int count, error;
            while ((count = SSL_read(ssl_ftp_data_con, buf, FTP_BUFSIZ)) > 0)
                    /* LOOP */ ;
        } else
#endif /* CK_SSL */
        {
            while (recv(din, (SENDARG2TYPE)buf, FTP_BUFSIZ,0) > 0)
                /* LOOP */ ;
        }
    }
    debug(F110,"ftp cancel_remote","E",0);
#else /* BSDSELECT */
#ifdef IBMSELECT
    fds[0] = csocket;
    fdcnt++;
    if (din) {
        fds[1] = din;
        fdcnt++;
    }
    nfnd = empty(fds, fdcnt, 10);
    debug(F101,"ftp cancel_remote empty","",nfnd);
    if ((nfnd) <= 0) {
        if (nfnd < 0) {
            perror("cancel");
        }
#ifdef FTP_PROXY
        if (ptabflg)
          ftpcode = -1;
#endif /* FTP_PROXY */
        lostpeer();
    }
    debug(F110,"ftp cancel_remote","D",0);
    if (din && select(&din, 1,0,0,1) ) {
#ifdef CK_SSL
        if (ssl_ftp_data_active_flag) {
            int count, error;
            while ((count = SSL_read(ssl_ftp_data_con, buf, FTP_BUFSIZ)) > 0)
                    /* LOOP */ ;
        } else
#endif /* CK_SSL */
        {
            while (recv(din, (SENDARG2TYPE)buf, FTP_BUFSIZ,0) > 0)
                /* LOOP */ ;
        }
    }
    debug(F110,"ftp cancel_remote","E",0);
#else /* IBMSELECT */
    Some form of select is required.
#endif /* IBMSELECT */
#endif /* BSDSELECT */
    if (getreply(0,-1,-1,ftp_vbm,0) == REPLY_ERROR && ftpcode == 552) {
        debug(F110,"ftp cancel_remote","F",0);
        /* 552 needed for NIC style cancel */
        getreply(0,-1,-1,ftp_vbm,0);
        debug(F110,"ftp cancel_remote","G",0);
    }
    debug(F110,"ftp cancel_remote","H",0);
    getreply(0,-1,-1,ftp_vbm,0);
    debug(F110,"ftp cancel_remote","I",0);
#ifdef DEBUG
    debtim = xdebtim;
#endif /* DEBUG */
}
static int
fts_dpl(x) int x; {
    if (!auth_type
#ifdef OS2
         || !ck_crypt_is_installed()
#endif /* OS2 */
         ) {
        switch ( x ) {
          case FPL_PRV:
            printf("?Cannot set protection level to PRIVATE\n");
            return(0);
          case FPL_SAF:
            printf("?Cannot set protection level to SAFE\n");
            return(0);
        }
        ftp_dpl = x;
        return(1);
    }
#ifdef CK_SSL
    if (x == FPL_SAF &&
        (!strcmp(auth_type,"SSL") || !strcmp(auth_type,"TLS"))) {
        printf("Cannot set protection level to safe\n");
        return(0);
    }
#endif /* CK_SSL */
    /* Start with a PBSZ of 1 meg */
    if (x != FPL_CLR) {
        if (setpbsz(DEFAULT_PBSZ) < 0)
          return(0);
    }
    y = ftpcmd(x == FPL_CLR ? "PROT C" :
               (x == FPL_SAF ? "PROT S" : "PROT P"), NULL, 0, 0,ftp_vbm);
    if (y == REPLY_COMPLETE) {
        ftp_dpl = x;
        return(1);
    }
    return(0);
}
static int
fts_cpl(x) int x; {
    if (!auth_type 
#ifdef OS2
         || !ck_crypt_is_installed()
#endif /* OS2 */
         ) {
        switch ( x ) {
          case FPL_PRV:
            printf("?Cannot set protection level to PRIVATE\n");
            return(0);
          case FPL_SAF:
            printf("?Cannot set protection level to SAFE\n");
            return(0);
        }
        ftp_cpl = x;
        return(1);
    }
    if (x == FPL_CLR) {
        y = ftpcmd("CCC",NULL,0,0,ftp_vbm);
        if (y == REPLY_COMPLETE) {
            ftp_cpl = x;
            return(1);
        }
        return(0);
    }
    ftp_cpl = x;
    return(1);
}
#ifdef FTP_GSSAPI
static VOID
user_gss_error(maj_stat, min_stat, s)
    OM_uint32 maj_stat, min_stat;
    char *s;
{
    /* a lot of work just to report the error */
    OM_uint32 gmaj_stat, gmin_stat, msg_ctx;
    gss_buffer_desc msg;
    msg_ctx = 0;
    while (!msg_ctx) {
        gmaj_stat = gss_display_status(&gmin_stat, maj_stat,
                                       GSS_C_GSS_CODE,
                                       GSS_C_NULL_OID,
                                       &msg_ctx,
                                       &msg
                                       );
        if ((gmaj_stat == GSS_S_COMPLETE)||
            (gmaj_stat == GSS_S_CONTINUE_NEEDED)) {
            fprintf(stderr, "GSSAPI error major: %s\n",
                    (char*)msg.value);
            gss_release_buffer(&gmin_stat, &msg);
        }
        if (gmaj_stat != GSS_S_CONTINUE_NEEDED)
          break;
    }
    msg_ctx = 0;
    while (!msg_ctx) {
        gmaj_stat = gss_display_status(&gmin_stat, min_stat,
                                       GSS_C_MECH_CODE,
                                       GSS_C_NULL_OID,
                                       &msg_ctx,
                                       &msg
                                       );
        if ((gmaj_stat == GSS_S_COMPLETE)||
            (gmaj_stat == GSS_S_CONTINUE_NEEDED)) {
            fprintf(stderr, "GSSAPI error minor: %s\n", (char*)msg.value);
            gss_release_buffer(&gmin_stat, &msg);
        }
        if (gmaj_stat != GSS_S_CONTINUE_NEEDED)
          break;
    }
    fprintf(stderr, "GSSAPI error: %s\n", s);
}
#endif /* FTP_GSSAPI */
#ifndef NOMHHOST
#ifdef datageneral
#define NOMHHOST
#else
#ifdef HPUX5WINTCP
#define NOMHHOST
#endif /* HPUX5WINTCP */
#endif /* datageneral */
#endif /* NOMHHOST */
#ifdef INADDRX
static struct in_addr inaddrx;
#endif /* INADDRX */
static char *
ftp_hookup(host, port, tls) char * host; int port; int tls; {
    register struct hostent *hp = 0;
#ifdef IP_TOS
#ifdef IPTOS_THROUGHPUT
    int tos;
#endif /* IPTOS_THROUGHPUT */
#endif /* IP_TOS */
    int s;
    GSOCKNAME_T len;
    static char hostnamebuf[MAXHOSTNAMELEN];
    char hostname[MAXHOSTNAMELEN] /* , *p, *q */ ;
    int  cport;
#ifdef DEBUG
    extern int debtim;
    int xdebtim;
    xdebtim = debtim;
    debtim = 1;
#endif /* DEBUG */
    debug(F111,"ftp_hookup",host,port);
#ifndef NOHTTP
    if (tcp_http_proxy) {
        struct servent *destsp;
        char *p, *q;
        ckmakmsg(proxyhost,HTTPCPYL,host,":",ckuitoa(port),NULL);
        for (p = tcp_http_proxy, q = hostname;
             *p != '\0' && *p != ':';
             p++, q++
             )
          *q = *p;
        *q = '\0';
        if (*p == ':')
          p++;
        else
          p = "http";
        destsp = getservbyname(p,"tcp");
        if (destsp)
          cport = ntohs(destsp->s_port);
        else if (p) {
          cport = atoi(p);
        } else
          cport = 80;
    } else
#endif /* NOHTTP */
    {
        ckstrncpy(hostname,host,MAXHOSTNAMELEN);
        cport = port;
    }
    memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
    hisctladdr.sin_addr.s_addr = inet_addr(host);
    if (hisctladdr.sin_addr.s_addr != INADDR_NONE) /* 2010-03-29 */
    {
        debug(F110,"ftp hookup A",hostname,0);
        hisctladdr.sin_family = AF_INET;
        ckstrncpy(hostnamebuf, hostname, MAXHOSTNAMELEN);
    } else {
        debug(F110,"ftp hookup B",hostname,0);
        hp = gethostbyname(hostname);
#ifdef HADDRLIST
        hp = ck_copyhostent(hp);        /* make safe copy that won't change */
#endif /* HADDRLIST */
        if (hp == NULL) {
            fprintf(stderr, "ftp: %s: Unknown host\n", host);
            ftpcode = -1;
#ifdef DEBUG
            debtim = xdebtim;
#endif /* DEBUG */
            return((char *) 0);
        }
        hisctladdr.sin_family = hp->h_addrtype;
#ifdef HADDRLIST
        memcpy((char *)&hisctladdr.sin_addr, hp->h_addr_list[0],
               sizeof(hisctladdr.sin_addr));
#else /* HADDRLIST */
        memcpy((char *)&hisctladdr.sin_addr, hp->h_addr,
               sizeof(hisctladdr.sin_addr));
#endif /* HADDRLIST */
        ckstrncpy(hostnamebuf, hp->h_name, MAXHOSTNAMELEN);
    }
    debug(F110,"ftp hookup C",hostnamebuf,0);
    ftp_host = hostnamebuf;
    s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
    debug(F101,"ftp hookup socket","",s);
    if (s < 0) {
        perror("ftp: socket");
        ftpcode = -1;
#ifdef DEBUG
        debtim = xdebtim;
#endif /* DEBUG */
        return(0);
    }
    hisctladdr.sin_port = htons(cport);
    errno = 0;
#ifdef COMMENT
  printf("hisctladdr=%d\n",sizeof(hisctladdr));
  printf("hisctladdr.sin_addr=%d\n",sizeof(hisctladdr.sin_addr));
  printf("sockaddr_in=%d\n",sizeof(struct sockaddr_in));
  printf("hisctladdr.sin_addr.s_addr=%d\n",sizeof(hisctladdr.sin_addr.s_addr));
#endif	/* COMMENT */
#ifdef HADDRLIST
    debug(F100,"ftp hookup HADDRLIST","",0);
    while
#else
    debug(F100,"ftp hookup no HADDRLIST","",0);
    if
#endif /* HADDRLIST */
      (connect(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0) {
          debug(F101,"ftp hookup connect failed","",errno);
#ifdef HADDRLIST
          if (hp && hp->h_addr_list[1]) {
              int oerrno = errno;
              fprintf(stderr, "ftp: connect to address %s: ",
                      inet_ntoa(hisctladdr.sin_addr));
              errno = oerrno;
              perror((char *) 0);
              hp->h_addr_list++;
              memcpy((char *)&hisctladdr.sin_addr,
                     hp->h_addr_list[0],
                     sizeof(hisctladdr.sin_addr));
              fprintf(stdout, "Trying %s...\n",
                      inet_ntoa(hisctladdr.sin_addr));
#ifdef TCPIPLIB
              socket_close(s);
#else /* TCPIPLIB */
              close(s);
#endif /* TCPIPLIB */
              s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
              if (s < 0) {
                  perror("ftp: socket");
                  ftpcode = -1;
#ifdef DEBUG
                  debtim = xdebtim;
#endif /* DEBUG */
                  return(0);
              }
              continue;
          }
#endif /* HADDRLIST */
          perror("ftp: connect");
          ftpcode = -1;
          goto bad;
      }
    debug(F100,"ftp hookup connect ok","",0);
    len = sizeof (myctladdr);
    errno = 0;
    if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
        debug(F101,"ftp hookup getsockname failed","",errno);
        perror("ftp: getsockname");
        ftpcode = -1;
        goto bad;
    }
    debug(F100,"ftp hookup getsockname ok","",0);
#ifndef NOHTTP
    if (tcp_http_proxy) {
#ifdef OS2
        char * agent = "Kermit 95";     /* Default user agent */
#else
        char * agent = "C-Kermit";
#endif /* OS2 */
        if (http_connect(s,agent,NULL,
                         tcp_http_proxy_user,
                         tcp_http_proxy_pwd,
                         0,
                         proxyhost
                         ) < 0) {
            char * foo = NULL;
#ifdef TCPIPLIB
            socket_close(s);
#else /* TCPIPLIB */
            close(s);
#endif /* TCPIPLIB */
            while (foo == NULL && tcp_http_proxy != NULL ) {
                if (tcp_http_proxy_errno == 401 ||
                     tcp_http_proxy_errno == 407 ) {
                    char uid[UIDBUFLEN];
                    char pwd[PWDSIZ];
                    struct txtbox tb[2];
                    int ok;
                    tb[0].t_buf = uid;
                    tb[0].t_len = UIDBUFLEN;
                    tb[0].t_lbl = "Proxy Userid: ";
                    tb[0].t_dflt = NULL;
                    tb[0].t_echo = 1;
                    tb[1].t_buf = pwd;
                    tb[1].t_len = 256;
                    tb[1].t_lbl = "Proxy Passphrase: ";
                    tb[1].t_dflt = NULL;
                    tb[1].t_echo = 2;
                    ok = uq_mtxt("Proxy Server Authentication Required\n",
                                  NULL, 2, tb);
                    if (ok && uid[0]) {
                        char * proxy_user, * proxy_pwd;
                        proxy_user = tcp_http_proxy_user;
                        proxy_pwd  = tcp_http_proxy_pwd;
                        tcp_http_proxy_user = uid;
                        tcp_http_proxy_pwd = pwd;
                        foo = ftp_hookup(host, port, 0);
                        debug(F110,"ftp_hookup()",foo,0);
                        memset(pwd,0,PWDSIZ);
                        tcp_http_proxy_user = proxy_user;
                        tcp_http_proxy_pwd = proxy_pwd;
                    } else
                        break;
                } else
                    break;
            }
            if (foo != NULL)
              return(foo);
            perror("ftp: connect");
            ftpcode = -1;
            goto bad;
        }
        ckstrncpy(hostnamebuf, proxyhost, MAXHOSTNAMELEN);
    }
#endif /* NOHTTP */
    csocket = s;
#ifdef CK_SSL
    if (tls) {
        /* FTP over SSL
         * If the connection is over an SSL proxy then the
         * auth_type will be NULL.  However, I'm not sure
         * whether we should protect the data channel in
         * that case or not.
         */
        debug(F100,"ftp hookup use_tls","",0);
        if (!ssl_auth()) {
            debug(F100,"ftp hookup ssl_auth failed","",0);
            auth_type = NULL;
            ftpcode = -1;
            csocket = -1;
            goto bad;
        }
        ssl_ftp_proxy = 1;
    }
#endif /* CK_SSL */
#ifdef IP_TOS
#ifdef IPTOS_LOWDELAY
    tos = IPTOS_LOWDELAY;
    if (setsockopt(csocket, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
      perror("ftp: setsockopt TOS (ignored)");
#endif
#endif
    if (!quiet)
      printf("Connected to %s.\n", host);
    /* Read greeting from server */
    if (getreply(0,ftp_csl,ftp_csr,ftp_vbm,0) > 2) {
        debug(F100,"ftp hookup bad reply","",0);
#ifdef TCPIPLIB
        socket_close(csocket);
#else /* TCPIPLIB */
        close(csocket);
#endif /* TCPIPLIB */
        ftpcode = -1;
        goto bad;
    }
#ifdef SO_OOBINLINE
    {
        int on = 1;
        errno = 0;
        if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on,
		       sizeof(on)) < 0) {
            perror("ftp: setsockopt");
            debug(F101,"ftp hookup setsockopt failed","",errno);
        }
#ifdef DEBUG
        else
          debug(F100,"ftp hookup setsockopt ok","",0);
#endif /* DEBUG */
    }
#endif /* SO_OOBINLINE */
#ifdef DEBUG
    debtim = xdebtim;
#endif /* DEBUG */
    return(ftp_host);
  bad:
    debug(F100,"ftp hookup bad","",0);
#ifdef TCPIPLIB
    socket_close(s);
#else /* TCPIPLIB */
    close(s);
#endif /* TCPIPLIB */
#ifdef DEBUG
    debtim = xdebtim;
#endif /* DEBUG */
    csocket = -1;
    return((char *)0);
}
static VOID
ftp_init() {
    int i, n;
    /* The purpose of the initial REST 0 is not clear, but other FTP */
    /* clients do it.  In any case, failure of this command is not a */
    /* reliable indication that the server does not support Restart. */
    okrestart = 0;
    if (!noinit) {
        n = ftpcmd("REST 0",NULL,0,0,0);
        if (n == REPLY_COMPLETE)
          okrestart = 1;
#ifdef COMMENT
        else if (ftp_deb)
          printf("WARNING: Unable to restore file pointer.\n");
#endif /* COMMENT */
    }
    n = ftpcmd("SYST",NULL,0,0,0);      /* Get server system type */
    if (n == REPLY_COMPLETE) {
        register char *cp, c = NUL;
        cp = ckstrchr(ftp_reply_str+4,' '); /* Get first word of reply */
        if (cp == NULL)
          cp = ckstrchr(ftp_reply_str+4,'\r');
        if (cp) {
            if (cp[-1] == '.')
              cp--;
            c = *cp;                    /* Save this char */
            *cp = '\0';                 /* Replace it with NUL */
        }
        if (!quiet)
          printf("Remote system type is %s.\n",ftp_reply_str+4);
        ckstrncpy(ftp_srvtyp,ftp_reply_str+4,SRVNAMLEN);
        if (cp)                         /* Put back saved char */
          *cp = c;
    }
    alike = !ckstrcmp(ftp_srvtyp,myostype,-1,0);
    if (!ckstrcmp(ftp_srvtyp,"UNIX",-1,0)) servertype = SYS_UNIX;
    else if (!ckstrcmp(ftp_srvtyp,"WIN32",-1,0)) servertype = SYS_WIN32;
    else if (!ckstrcmp(ftp_srvtyp,"OS/2",-1,0)) servertype = SYS_WIN32;
    else if (!ckstrcmp(ftp_srvtyp,"VMS",-1,0)) servertype = SYS_VMS;
    else if (!ckstrcmp(ftp_srvtyp,"DOS",-1,0)) servertype = SYS_DOS;
    else if (!ckstrcmp(ftp_srvtyp,"TOPS20",-1,0)) servertype = SYS_TOPS20;
    else if (!ckstrcmp(ftp_srvtyp,"TOPS10",-1,0)) servertype = SYS_TOPS10;
#ifdef FTP_PROXY
    unix_proxy = 0;
    if (servertype == SYS_UNIX && proxy) unix_proxy = 1;
#endif /* FTP_PROXY */
    if (ftp_cmdlin && ftp_xfermode == XMODE_M)
      ftp_typ = binary;                 /* Type given on command line */
    else                                /* Otherwise set it automatically */
      ftp_typ = alike ? FTT_BIN : FTT_ASC;
    changetype(ftp_typ,0);              /* Change to this type */
    g_ftp_typ = ftp_typ;                /* Make it the global type */
    if (!quiet)
      printf("Default transfer mode is %s\n",
             ftp_typ ? "BINARY" : "TEXT (\"ASCII\")"
             );
    for (i = 0; i < 16; i++)		/* Init server FEATure table */
      sfttab[i] = 0;
    if (!noinit) {
        n = ftpcmd("MODE S",NULL,0,0,0); /* We always send in Stream mode */
#ifdef COMMENT
        if (n != REPLY_COMPLETE)
          printf("WARNING: Server does not accept MODE S(TREAM)\n");
#endif /* COMMENT */
        n = ftpcmd("STRU F",NULL,0,0,0); /* STRU File (not Record or Page) */
#ifdef COMMENT
        if (n != REPLY_COMPLETE)
          printf("WARNING: Server does not accept STRU F(ILE)\n");
#endif /* COMMENT */
	if (featok) {
	    n = ftpcmd("FEAT",NULL,0,0,0); /* Ask server about features */
	    if (n == REPLY_COMPLETE) {
		debug(F101,"ftp_init FEAT","",sfttab[0]);
		if (deblog || ftp_deb) {
		    int i;
		    for (i = 1; i < 16 && i < nfeattab; i++) {
			debug(F111,"ftp_init FEAT",feattab[i].kwd,sfttab[i]);
			if (ftp_deb)
			  printf("  Server %s %s\n",
				 sfttab[i] ? "supports" : "does not support",
				 feattab[i].kwd
				 );
		    }
		    /* Deal with disabled MLST opts here if necessary */
		    /* But why would it be? */
		}
	    }
	}
    }
}
static int
ftp_login(host) char * host; {          /* (also called from ckuusy.c) */
    static char ftppass[PASSBUFSIZ]="";
    char tmp[PASSBUFSIZ];
    char *user = NULL, *pass = NULL, *acct = NULL;
    int n, aflag = 0;
    extern char uidbuf[];
    extern char pwbuf[];
    extern int  pwflg, pwcrypt;
    debug(F111,"ftp_login",ftp_logname,ftp_log);
    if (!ckstrcmp(ftp_logname,"anonymous",-1,0))
      anonymous = 1;
    if (!ckstrcmp(ftp_logname,"ftp",-1,0))
      anonymous = 1;
#ifdef FTP_SRP
    if (auth_type && !strcmp(auth_type, "SRP")) {
        user = srp_user;
        pass = srp_pass;
        acct = srp_acct;
    } else
#endif /* FTP_SRP */
      if (anonymous) {
          user = "anonymous";
          if (ftp_tmp) {		/* They gave a password */
              pass = ftp_tmp;
          } else if (ftp_apw) {		/* SET FTP ANONYMOUS-PASSWORD */
	      pass = ftp_apw;
	  } else {			/* Supply user@host */
	      ckmakmsg(tmp,PASSBUFSIZ,whoami(),"@",myhost,NULL);
	      pass = tmp;
          }
	  debug(F110,"ftp anonymous",pass,0);
      } else {
#ifdef USE_RUSERPASS
          if (ruserpass(host, &user, &pass, &acct) < 0) {
              ftpcode = -1;
              return(0);
          }
#endif /* USE_RUSERPASS */
          if (ftp_logname) {
              user = ftp_logname;
              pass = ftp_tmp;
          } else if (uidbuf[0] && (ftp_tmp || pwbuf[0] && pwflg)) {
              user = uidbuf;
              if (ftp_tmp) {
                  pass = ftp_tmp;
              } else if (pwbuf[0] && pwflg) {
                  ckstrncpy(ftppass,pwbuf,PASSBUFSIZ);
#ifdef OS2
                  if ( pwcrypt )
                      ck_encrypt((char *)ftppass);
#endif /* OS2 */
                  pass = ftppass;
              }
          }
          acct = ftp_acc;
          while (user == NULL) {
              char *myname, prompt[PROMPTSIZ];
              int ok;
              myname = whoami();
              if (myname)
                ckmakxmsg(prompt,PROMPTSIZ," Name (",host,":",myname,"): ",
                          NULL,NULL,NULL,NULL,NULL,NULL,NULL);
              else
                ckmakmsg(prompt,PROMPTSIZ," Name (",host,"): ",NULL);
              tmp[0] = '\0';
              
              ok = uq_txt(NULL,prompt,1,NULL,tmp,PASSBUFSIZ,NULL,
                          DEFAULT_UQ_TIMEOUT);
              if (!ok || *tmp == '\0')
                user = myname;
              else
                user = brstrip(tmp);
          }
      }
    n = ftpcmd("USER",user,-1,-1,ftp_vbm);
    if (n == REPLY_COMPLETE) {
        /* determine if we need to send a dummy password */
        if (ftpcmd("PWD",NULL,0,0,0) != REPLY_COMPLETE)
          ftpcmd("PASS dummy",NULL,0,0,1);
    } else if (n == REPLY_CONTINUE) {
#ifdef CK_ENCRYPTION
        int oldftp_cpl;
#endif /* CK_ENCRYPTION */
        if (pass == NULL) {
            int ok;
            setint();
            ok = uq_txt(NULL," Password: ",2,NULL,ftppass,PASSBUFSIZ,NULL,
                        DEFAULT_UQ_TIMEOUT);
            if (ok)
                pass = brstrip(ftppass);
        }
#ifdef CK_ENCRYPTION
        oldftp_cpl = ftp_cpl;
        ftp_cpl = FPL_PRV;
#endif /* CK_ENCRYPTION */
        n = ftpcmd("PASS",pass,-1,-1,1);
        if (!anonymous && pass) {
            char * p = pass;
            while (*p++) *(p-1) = NUL;
            makestr(&ftp_tmp,NULL);
        }
#ifdef CK_ENCRYPTION
        /* level may have changed */
        if (ftp_cpl == FPL_PRV)
          ftp_cpl = oldftp_cpl;
#endif /* CK_ENCRYPTION */
    }
    if (n == REPLY_CONTINUE) {
        aflag++;
        if (acct == NULL) {
            static char ftpacct[80];
            int ok;
            setint();
            ok = uq_txt(NULL," Account: ",2,NULL,ftpacct,80,NULL,
                        DEFAULT_UQ_TIMEOUT);
            if (ok)
                acct = brstrip(ftpacct);
        }
        n = ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
    }
    if (n != REPLY_COMPLETE) {
        fprintf(stderr, "FTP login failed.\n");
        if (haveurl)
          doexit(BAD_EXIT,-1);
        return(0);
    }
    if (!aflag && acct != NULL) {
        ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
    }
    makestr(&ftp_logname,user);
    loggedin = 1;
#ifdef LOCUS
    /* Unprefixed file management commands go to server */
    if (autolocus && !ftp_cmdlin) {
	setlocus(0,1);
    }
#endif /* LOCUS */
    ftp_init();
    if (anonymous && !quiet) {
        printf(" Logged in as anonymous (%s)\n",pass);
        memset(pass, 0, strlen(pass));
    }
    if (ftp_rdir) {
        if (doftpcwd(ftp_rdir,-1) < 1)
          doexit(BAD_EXIT,-1);
    }
#ifdef FTP_PROXY
    if (proxy)
      return(1);
#endif /* FTP_PROXY */
    return(1);
}
static int
ftp_reset() {
    int rc;
#ifdef BSDSELECT
    int nfnd = 1;
    fd_set mask;
    FD_ZERO(&mask);
    while (nfnd > 0) {
        FD_SET(csocket, &mask);
        if ((nfnd = empty(&mask,0)) < 0) {
            perror("reset");
            ftpcode = -1;
            lostpeer();
            return(0);
        } else if (nfnd) {
            getreply(0,-1,-1,ftp_vbm,0);
        }
    }
#else /* BSDSELECT */
#ifdef IBMSELECT
    int nfnd = 1;
    while (nfnd > 0) {
        if ((nfnd = empty(&csocket,1,0)) < 0) {
            perror("reset");
            ftpcode = -1;
            lostpeer();
            return(0);
        } else if (nfnd) {
            getreply(0,-1,-1,ftp_vbm,0);
        }
    }
#endif /* IBMSELECT */
#endif /* BSDSELECT */
    rc = (ftpcmd("REIN",NULL,0,0,ftp_vbm) == REPLY_COMPLETE);
    if (rc > 0)
      loggedin = 0;
    return(rc);
}
static int
ftp_rename(from, to) char * from, * to; {
    int lcs = -1, rcs = -1;
#ifndef NOCSETS
    if (ftp_xla) {
        lcs = ftp_csl;
        if (lcs < 0) lcs = fcharset;
        rcs = ftp_csx;
        if (rcs < 0) rcs = ftp_csr;
    }
#endif /* NOCSETS */
    if (ftpcmd("RNFR",from,lcs,rcs,ftp_vbm) == REPLY_CONTINUE) {
        return(ftpcmd("RNTO",to,lcs,rcs,ftp_vbm) == REPLY_COMPLETE);
    }
    return(0);                          /* Failure */
}
static int
ftp_umask(mask) char * mask; {
    int rc;
    rc = (ftpcmd("SITE UMASK",mask,-1,-1,1) == REPLY_COMPLETE);
    return(rc);
}
static int
ftp_user(user,pass,acct) char * user, * pass, * acct; {
    int n = 0, aflag = 0;
    char pwd[PWDSIZ];
    if (!auth_type && ftp_aut) {
#ifdef FTP_SRP
        if (ck_srp_is_installed()) {
            if (srp_ftp_auth( NULL, user, pass)) {
                makestr(&pass,srp_pass);
            }
        }
#endif /* FTP_SRP */
    }
    n = ftpcmd("USER",user,-1,-1,ftp_vbm);
    if (n == REPLY_COMPLETE)
      n = ftpcmd("PASS dummy",NULL,0,0,1);
    else if (n == REPLY_CONTINUE) {
#ifdef CK_ENCRYPTION
        int oldftp_cpl;
#endif /* CK_ENCRYPTION */
        if (pass == NULL || !pass[0]) {
            int ok;
            pwd[0] = '\0';
            setint();
            ok = uq_txt(NULL," Password: ",2,NULL,pwd,PWDSIZ,NULL,
                        DEFAULT_UQ_TIMEOUT);
            if (ok)
                pass = brstrip(pwd);
        }
#ifdef CK_ENCRYPTION
        if ((oldftp_cpl = ftp_cpl) == PROT_S)
          ftp_cpl = PROT_P;
#endif /* CK_ENCRYPTION */
        n = ftpcmd("PASS",pass,-1,-1,1);
        memset(pass, 0, strlen(pass));
#ifdef CK_ENCRYPTION
        /* level may have changed */
        if (ftp_cpl == PROT_P)
          ftp_cpl = oldftp_cpl;
#endif /* CK_ENCRYPTION */
    }
    if (n == REPLY_CONTINUE) {
        if (acct == NULL || !acct[0]) {
            int ok;
            pwd[0] = '\0';
            setint();
            ok = uq_txt(NULL," Account: ",2,NULL,pwd,PWDSIZ,NULL,
                        DEFAULT_UQ_TIMEOUT);
            if (ok)
                acct = pwd;
        }
        n = ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
        aflag++;
    }
    if (n != REPLY_COMPLETE) {
        printf("Login failed.\n");
        return(0);
    }
    if (!aflag && acct != NULL && acct[0]) {
        n = ftpcmd("ACCT",acct,-1,-1,ftp_vbm);
    }
    if (n == REPLY_COMPLETE) {
        makestr(&ftp_logname,user);
        loggedin = 1;
        ftp_init();
        return(1);
    }
    return(0);
}
char *
ftp_authtype() {
    if (!connected)
      return("NULL");
    return(auth_type ? auth_type : "NULL");
}
char *
ftp_cpl_mode() {
    switch (ftp_cpl) {
      case FPL_CLR:
        return("clear");
      case FPL_SAF:
        return("safe");
      case FPL_PRV:
        return("private");
      case FPL_CON:
        return("confidential");
      default:
        return("(error)");
    }
}
char *
ftp_dpl_mode() {
    switch (ftp_dpl) {
      case FPL_CLR:
        return("clear");
      case FPL_SAF:
        return("safe");
      case FPL_PRV:
        return("private");
      case FPL_CON:
        return("confidential");
      default:
        return("(error)");
    }
}
/* remote_files() */
/*
   Returns next remote filename on success;
   NULL on error or no more files with global rfrc set to:
     -1: Bad argument
     -2: Server error response to NLST, e.g. file not found
     -3: No more files
     -9: Internal error
*/
#define FTPNAMBUFLEN CKMAXPATH+1024
/* Check: ckmaxfiles CKMAXOPEN */
#define MLSDEPTH 128			/* Stack of open temp files */
static int mlsdepth = 0;		/* Temp file stack depth */
static FILE * tmpfilptr[MLSDEPTH+1] = { NULL, NULL }; /* Temp file pointers */
static char * tmpfilnam[MLSDEPTH+1] = { NULL, NULL }; /* Temp file names */
static VOID
mlsreset() {				/* Reset MGET temp-file stack */
    int i;
    for (i = 0; i <= mlsdepth; i++) {
	if (tmpfilptr[i]) {
	    fclose(tmpfilptr[i]);
	    tmpfilptr[i] = NULL;
	    if (tmpfilnam[i]) {
#ifdef OS2
		unlink(tmpfilnam[i]);
#endif /* OS2 */
		free(tmpfilnam[i]);
	    }
	}
    }
    mlsdepth = 0;
}
static CHAR *
#ifdef CK_ANSIC
remote_files(int new_query, CHAR * arg, CHAR * pattern, int proxy_switch)
#else /* CK_ANSIC */
remote_files(new_query, arg, pattern, proxy_switch)
    int new_query;
    CHAR * arg;				/* That we send to the server */
    CHAR * pattern;			/* That we use locally */
    int proxy_switch;
#endif /* CK_ANSIC */
/* remote_files */ {
    static CHAR buf[FTPNAMBUFLEN];
    CHAR *cp, *whicharg;
    char * cdto = NULL;
    char * p;
    int i, x, forced = 0;
    int lcs = 0, rcs = 0, xlate = 0;
    debug(F101,"ftp remote_files new_query","",new_query);
    debug(F110,"ftp remote_files arg",arg,0);
    debug(F110,"ftp remote_files pattern",pattern,0);
    rfrc = -1;
    if (pattern)			/* Treat empty pattern same as NULL */
      if (!*pattern)
	pattern = NULL;
    if (arg)				/* Ditto for arg */
      if (!*arg)
	arg = NULL;
  again:
    if (new_query) {
        if (tmpfilptr[mlsdepth]) {
            fclose(tmpfilptr[mlsdepth]);
            tmpfilptr[mlsdepth] = NULL;
#ifdef OS2
            if (!ftp_deb && !deblog)
              unlink(tmpfilnam[mlsdepth]);
#endif /* OS2 */
        }
    }
    if (tmpfilptr[mlsdepth] == NULL) {
        extern char * tempdir;
        char * p;
        debug(F110,"ftp remote_files tempdir",tempdir,0);
        if (tempdir) {
            p = tempdir;
        } else {
#ifdef OS2
#ifdef NT
            p = getenv("K95TMP");
#else
            p = getenv("K2TMP");
#endif /* NT */
            if (!p)
#endif /* OS2 */
              p = getenv("CK_TMP");
            if (!p)
              p = getenv("TMPDIR");
            if (!p) p = getenv("TEMP");
            if (!p) p = getenv("TMP");
#ifdef OS2ORUNIX
            if (p) {
                int len = strlen(p);
                if (p[len-1] != '/'
#ifdef OS2
                    && p[len-1] != '\\'
#endif /* OS2 */
                     ) {
                    static char foo[CKMAXPATH];
                    ckstrncpy(foo,p,CKMAXPATH);
                    ckstrncat(foo,"/",CKMAXPATH);
                    p = foo;
                }
            } else
#else /* OS2ORUNIX */
            if (!p)
#endif /* OS2ORUNIX */
#ifdef UNIX                             /* Systems that have a standard */
                p = "/tmp/";            /* temporary directory... */
#else
#ifdef datageneral
            p = ":TMP:";
#else
            p = "";
#endif /* datageneral */
#endif /* UNIX */
        }
        debug(F110,"ftp remote_files p",p,0);
	/* Get temp file */
	if ((tmpfilnam[mlsdepth] = (char *)malloc(CKMAXPATH+1))) {
	    ckmakmsg((char *)tmpfilnam[mlsdepth],
		     CKMAXPATH+1,p,"ckXXXXXX",NULL,NULL);
	} else {
	    printf("?Malloc failure: remote_files()\n");
	    return(NULL);
	}
#ifdef NT
	{
	    char * tmpfil = mktemp((char *)tmpfilnam[mlsdepth]);
	    if ( tmpfil )
		ckstrncpy(tmpfilnam[mlsdepth],tmpfil,CKMAXPATH+1);
	}
#else /* NT */
#ifdef MKTEMP
#ifdef MKSTEMP
	x = mkstemp((char *)tmpfilnam[mlsdepth]);
	if (x > -1) close(x);		/* We just want the name. */
#else
        mktemp((char *)tmpfilnam[mlsdepth]);
#endif /* MKSTEMP */
        /* if no mktmpnam() the name will just be "ckXXXXXX"... */
#endif /* MKTEMP */
#endif /* NT */
	debug(F111,"ftp remote_files tmpfilnam[mlsdepth]",
	      tmpfilnam[mlsdepth],mlsdepth);
#ifdef FTP_PROXY
        if (proxy_switch) {
            pswitch(!proxy);
        }
#endif /* FTP_PROXY */
        debug(F101,"ftp remote_files ftp_xla","",ftp_xla);
        debug(F101,"ftp remote_files ftp_csl","",ftp_csl);
        debug(F101,"ftp remote_files ftp_csr","",ftp_csr);
#ifndef NOCSETS
        xlate = ftp_xla;                /* SET FTP CHARACTER-SET-TRANSLATION */
        if (xlate) {                    /* ON? */
            lcs = ftp_csl;              /* Local charset */
            if (lcs < 0) lcs = fcharset;
            if (lcs < 0) xlate = 0;
        }
        if (xlate) {                    /* Still ON? */
            rcs = ftp_csx;              /* Remote (Server) charset */
            if (rcs < 0) rcs = ftp_csr;
            if (rcs < 0) xlate = 0;
        }
#endif /* NOCSETS */
	forced = mgetforced;		/* MGET method forced? */
	if (!forced || !mgetmethod)	/* Not forced... */
	  mgetmethod = (sfttab[0] && sfttab[SFT_MLST]) ? /* so pick one */
	      SND_MLS :
	      SND_NLS; 
/*                                           
  User's Command:                 Result:
    mget /nlst                     NLST (NULL)
    mget /nlst foo                 NLST foo
    mget /nlst *.txt               NLST *.txt 
    mget /nlst /match:*.txt        NLST (NULL)
    mget /nlst /match:*.txt  foo   NLST foo   
    mget /mlsd                     MLSD (NULL)
    mget /mlsd foo                 MLSD foo
    mget /mlsd *.txt               MLSD (NULL)
    mget /mlsd /match:*.txt        MLSD (NULL)
    mget /mlsd /match:*.txt  foo   MLSD foo
*/
	x = -1;
	while (x < 0) {
	    if (pattern) {		/* Don't simplify this! */
		whicharg = arg;
	    } else if (mgetmethod == SND_MLS) {
		if (arg)
		  whicharg = iswild((char *)arg) ? NULL : arg;
		else
		  whicharg = NULL;
	    } else {
		whicharg = arg;
	    }
	    debug(F110,"ftp remote_files mgetmethod",
		  mgetmethod == SND_MLS ? "MLSD" : "NLST", 0);
	    debug(F110,"ftp remote_files whicharg",whicharg,0);
	    x = recvrequest((mgetmethod == SND_MLS) ? "MLSD" : "NLST",
			    (char *)tmpfilnam[mlsdepth],
			    (char *)whicharg,
			    "wb",
			    0,
			    0,
			    NULL,
			    xlate,
			    lcs,
			    rcs
			    );
	    if (x < 0) {		/* Chosen method wasn't accepted */
		if (forced) {
		    if (ftpcode > 500 && ftpcode < 505 && !quiet)
		      printf("?%s: Not supported by server\n",
			     mgetmethod == SND_MLS ? "MLSD" : "NLST"
			     );
		    rfrc = -2;		/* Fail */
		    return(NULL);
		}
		/* Not forced - if MLSD failed, try NLST */
		if (mgetmethod == SND_MLS) {  /* Server lied about MLST */
		    sfttab[SFT_MLST] = 0;     /* So disable it */
		    mlstok = 0;		      /* and */
		    mgetmethod = SND_NLS;     /* try NLST */
		    continue;
		}
		rfrc = -2;
		return(NULL);
	    }
	}
#ifdef FTP_PROXY
        if (proxy_switch) {
            pswitch(!proxy);
        }
#endif /* FTP_PROXY */
        tmpfilptr[mlsdepth] = fopen((char *)tmpfilnam[mlsdepth], "r");
#ifndef OS2
	if (tmpfilptr[mlsdepth]) {
	    if (!ftp_deb && !deblog)
	      unlink(tmpfilnam[mlsdepth]);
	}
#endif /* OS2 */
      notemp:
        if (!tmpfilptr[mlsdepth]) {
            debug(F110,"ftp remote_files open fail",tmpfilnam[mlsdepth],0);
            if ((!dpyactive || ftp_deb))
              printf("?Can't find list of remote files, oops\n");
            rfrc = -9;
            return(NULL);
        }
	if (ftp_deb)
	  printf("LISTFILE: %s\n",tmpfilnam[mlsdepth]);
    }
    buf[0] = NUL;
    buf[FTPNAMBUFLEN-1] = NUL;
    buf[FTPNAMBUFLEN-2] = NUL;
    /* We have to redo all this because the first time was only for */
    /* for getting the file list, now it's for getting each file */
    if (arg && mgetmethod == SND_MLS) {	/* MLSD */
	if (!pattern && iswild((char *)arg)) {
	    pattern = arg;		/* Wild arg is really a pattern */
	    if (pattern)
	      if (!*pattern)
		pattern = NULL;
	    arg = NULL;			/* and not an arg */
	}
	if (new_query) {		/* Initial query? */
	    cdto = (char *)arg;		/* (nonwild) arg given? */
	    if (cdto)
	      if (!*cdto)
		cdto = NULL;
	    if (cdto)			/* If so, then CD to it */
	      doftpcwd(cdto,0);
	}
    }
    new_query = 0;
    if (fgets((char *)buf, FTPNAMBUFLEN, tmpfilptr[mlsdepth]) == NULL) {
        fclose(tmpfilptr[mlsdepth]);
        tmpfilptr[mlsdepth] = NULL;
#ifdef OS2
        if (!ftp_deb && !deblog)
          unlink(tmpfilnam[mlsdepth]);
#endif /* OS2 */
        if (ftp_deb && !deblog) {
            printf("(Temporary file %s NOT deleted)\n",
		   (char *)tmpfilnam[mlsdepth]);
        }
	if (mlsdepth <= 0) {		/* EOF at depth 0 */
	    rfrc = -3;			/* means we're done */
	    return(NULL);
	}
	printf("POPPING(%d)...\n",mlsdepth-1); 
	if (tmpfilnam[mlsdepth]) free(tmpfilnam[mlsdepth]);
	mlsdepth--;
	doftpcdup();
	zchdir("..");			/* <-- Not portable */
	goto again;
    }
    if (buf[FTPNAMBUFLEN-1]) {
	printf("?BUFFER OVERFLOW -- FTP NLST or MLSD string longer than %d\n",
	       FTPNAMBUFLEN
	       );
	debug(F101,"remote_files buffer overrun","",FTPNAMBUFLEN);
	return(NULL);
    }
    /* debug(F110,"ftp remote_files buf 1",buf,0); */
    if ((cp = (CHAR *)ckstrchr((char *)buf,'\n')) != NULL)
      *cp = '\0';
    if ((cp = (CHAR *)ckstrchr((char *)buf,'\r')) != NULL)
      *cp = '\0';
    debug(F110,"ftp remote_files buf",buf,0);
    rfrc = 0;
    if (ftp_deb)
      printf("[%s]\n",(char *)buf);
    havesize = (CK_OFF_T)-1;		/* Initialize file facts... */
    havetype = 0;
    makestr(&havemdtm,NULL);
    p = (char *)buf;
    if (mgetmethod == SND_NLS) {	/* NLST... */
	if (pattern) {
	    if (!ckmatch((char *)pattern,p,(servertype == SYS_UNIX),1))
	      goto again;
	}
    } else {				/* MLSD... */
	p = parsefacts((char *)buf);
	switch (havetype) {
	  case FTYP_FILE:		/* File: Get it if it matches */
	    if (pattern) {
		if (!ckmatch((char *)pattern,p,(servertype == SYS_UNIX),1))
		  goto again;
	    }
	    break;
	  case FTYP_CDIR:		/* Current directory */
	  case FTYP_PDIR:		/* Parent directory */
	    goto again;			/* Skip */
	  case FTYP_DIR:		/* (Sub)Directory */
	    if (!recursive)		/* If not /RECURSIVE */
	      goto again;		/* Skip */
	    if (mlsdepth < MLSDEPTH) {
		char * p2 = NULL;
		mlsdepth++;
		printf("RECURSING [%s](%d)...\n",p,mlsdepth); 
		if (doftpcwd(p,0) > 0) {
		    int x;
		    if (!ckstrchr(p,'/')) {
			/* zmkdir() needs dirsep */
			if ((p2 = (char *)malloc((int)strlen(p) + 2))) {
			    strcpy(p2,p);	/* SAFE */
			    strcat(p2,"/");	/* SAFE */
			    p = p2;
			}
		    }
#ifdef NOMKDIR
		    x = -1;
#else
		    x = zmkdir(p);
#endif /* NOMKDIR */
		    if (x > -1) {
			zchdir(p);
			p = (char *)remote_files(1,arg,pattern,0);
			if (p2) free(p2);
		    } else {
			printf("?mkdir failed: [%s] Depth=%d\n",
			       p,
			       mlsdepth
			       );
			mlsreset();
			if (p2) free(p2);
			return(NULL);
		    }
		} else {
		    printf("?CWD failed: [%s] Depth=%d\n",p,mlsdepth);
		    mlsreset();
		    return(NULL);
		}
	    } else {
		printf("MAX DIRECTORY STACK DEPTH EXCEEDED: %d\n",
		       mlsdepth
		       );
		mlsreset();
		return(NULL);
	    }
	}
    }
#ifdef DEBUG
    if (deblog) {
	debug(F101,"remote_files havesize","",havesize);
	debug(F101,"remote_files havetype","",havetype);
	debug(F110,"remote_files havemdtm",havemdtm,0);	
	debug(F110,"remote_files name",p,0);	
    }
#endif /* DEBUG */
    return((CHAR *)p);
}
/* N O T  P O R T A B L E !!! */
#if (SIZEOF_SHORT == 4)
typedef unsigned short ftp_uint32;
typedef short ftp_int32;
#else
#if (SIZEOF_INT == 4)
typedef unsigned int ftp_uint32;
typedef int ftp_int32;
#else
#if (SIZEOF_LONG == 4)
typedef ULONG ftp_uint32;
typedef long ftp_int32;
#endif
#endif
#endif
/* Perhaps use these in general, certainly use them for GSSAPI */
#ifndef looping_write
#define ftp_int32 int
#define ftp_uint32 unsigned int
static int
looping_write(fd, buf, len)
    int fd;
    register CONST char *buf;
    int len;
{
    int cc;
    register int wrlen = len;
    do {
        cc = send(fd, (SENDARG2TYPE)buf, wrlen, 0);
        if (cc < 0) {
            if (errno == EINTR)
              continue;
            return(cc);
        } else {
            buf += cc;
            wrlen -= cc;
        }
    } while (wrlen > 0);
    return(len);
}
#endif
#ifndef looping_read
static int
looping_read(fd, buf, len)
    int fd;
    register char *buf;
    register int len;
{
    int cc, len2 = 0;
    do {
        cc = recv(fd, (char *)buf, len,0);
        if (cc < 0) {
            if (errno == EINTR)
              continue;
            return(cc);                 /* errno is already set */
        } else if (cc == 0) {
            return(len2);
        } else {
            buf += cc;
            len2 += cc;
            len -= cc;
        }
    } while (len > 0);
    return(len2);
}
#endif /* looping_read */
#define ERR -2
#ifdef COMMENT
static
secure_putbyte(fd, c) int fd; CHAR c; {
    int ret;
    ucbuf[nout++] = c;
    if (nout == (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR) {
        nout = 0;
        if (!ftpissecure())
          ret = send(fd, (SENDARG2TYPE)ucbuf,
                     (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR, 0);
        else
          ret = secure_putbuf(fd,
                              ucbuf,
                              (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR
                              );
        return(ret?ret:c);
    }
    return(c);
}
#endif /* COMMENT */
/* returns:
 *       0  on success
 *      -1  on error (errno set)
 *      -2  on security error
 */
static int
secure_flush(fd) int fd; {
    int rc = 0;
    int len = 0;
    if (nout > 0) {
        len = nout;
        if (!ftpissecure()) {
            rc = send(fd, (SENDARG2TYPE)ucbuf, nout, 0);
            nout = 0;
            goto xflush;
        } else {
            rc = secure_putbuf(fd, ucbuf, nout);
            if (rc)
              goto xflush;
        }
    }
    rc = (!ftpissecure()) ? 0 : secure_putbuf(fd, (CHAR *)"", nout = 0);
  xflush:
    if (rc > -1 && len > 0 && fdispla != XYFD_B) {
	spackets++;
        spktl = len;
        ftscreen(SCR_PT,'D',(CK_OFF_T)spackets,NULL);
    }
    return(rc);
}
#ifdef COMMENT                          /* (not used) */
/* returns:
 *      c>=0  on success
 *      -1    on error
 *      -2    on security error
 */
static int
#ifdef CK_ANSIC
secure_putc(char c, int fd)
#else
secure_putc(c, fd) char c; int fd;
#endif /* CK_ANSIC */
/* secure_putc */ {
    return(secure_putbyte(fd, (CHAR) c));
}
#endif /* COMMENT */
/* returns:
 *      nbyte on success
 *      -1  on error (errno set)
 *      -2  on security error
 */
static int
#ifdef CK_ANSIC
secure_write(int fd, CHAR * buf, unsigned int nbyte)
#else
secure_write(fd, buf, nbyte)
    int fd;
    CHAR * buf;
    unsigned int nbyte;
#endif /* CK_ANSIC */
{
    int ret;
#ifdef FTP_TIMEOUT    
    ftp_timed_out = 0;
    if (check_data_connection(fd,1) < 0) {
	ftp_timed_out = 1;
	return(-3);
    }
#endif	/* FTP_TIMEOUT */
    if (!ftpissecure()) {
        if (nout > 0) {
            if ((ret = send(fd, (SENDARG2TYPE)ucbuf, nout, 0)) < 0)
              return(ret);
            nout = 0;
        }
        return(send(fd,(SENDARG2TYPE)buf,nbyte,0));
    } else {
        int ucbuflen = (maxbuf ? maxbuf : actualbuf) - FUDGE_FACTOR;
        int bsent = 0;
        while (bsent < nbyte) {
            int b2cp = ((nbyte - bsent) > (ucbuflen - nout) ?
                        (ucbuflen - nout) : (nbyte - bsent));
#ifdef DEBUG
	    if (deblog) {
		debug(F101,"secure_write ucbuflen","",ucbuflen);
		debug(F101,"secure_write ucbufsiz","",ucbufsiz);
		debug(F101,"secure_write bsent","",bsent);
		debug(F101,"secure_write b2cp","",b2cp);
	    }
#endif /* DEBUG */
            memcpy(&ucbuf[nout],&buf[bsent],b2cp);
            nout += b2cp;
            bsent += b2cp;
            if (nout == ucbuflen) {
                nout = 0;
                ret = secure_putbuf(fd, ucbuf, ucbuflen);
                if (ret < 0)
                  return(ret);
            }
        }
        return(bsent);
    }
}
/* returns:
 *       0  on success
 *      -1  on error (errno set)
 *      -2  on security error
 */
static int
#ifdef CK_ANSIC
secure_putbuf(int fd, CHAR * buf, unsigned int nbyte)
#else
secure_putbuf(fd, buf, nbyte) int fd; CHAR * buf; unsigned int nbyte;
#endif /* CK_ANSIC */
{
    static char *outbuf = NULL;         /* output ciphertext */
#ifdef FTP_SECURITY
    static unsigned int bufsize = 0;    /* size of outbuf */
#endif /* FTP_SECURITY */
    ftp_int32 length   = 0;
    ftp_uint32 net_len = 0;
    /* Other auth types go here ... */
#ifdef CK_SSL
    if (ssl_ftp_data_active_flag) {
        int count, error;
        /* there is no need to send an empty buffer when using SSL/TLS */
        if ( nbyte == 0 )
	  return(0);
        count = SSL_write(ssl_ftp_data_con, buf, nbyte);
        error = SSL_get_error(ssl_ftp_data_con,count);
        switch (error) {
          case SSL_ERROR_NONE:
            return(0);
          case SSL_ERROR_WANT_WRITE:
          case SSL_ERROR_WANT_READ:
          case SSL_ERROR_SYSCALL:
#ifdef NT
            {
                int gle = GetLastError();
		if (gle == 0)
		  return(0);
		debug(F111,"secure_putbuf","SSL_ERROR_SYSCALL",gle);
            }
#endif /* NT */
          case SSL_ERROR_WANT_X509_LOOKUP:
          case SSL_ERROR_SSL:
          case SSL_ERROR_ZERO_RETURN:
          default:
            SSL_shutdown(ssl_ftp_data_con);
            SSL_free(ssl_ftp_data_con);
            ssl_ftp_data_active_flag = 0;
            ssl_ftp_data_con = NULL;
#ifdef TCPIPLIB
            socket_close(data);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
            shutdown(data, 1+1);
#endif /* USE_SHUTDOWN */
            close(data);
#endif /* TCPIPLIB */
            data = -1;
            globaldin = data;
            return(-1);
        }
        return(-1);
    }
#endif /* CK_SSL */
#ifdef FTP_SRP
    if (ck_srp_is_installed() && (strcmp(auth_type, "SRP") == 0)) {
        if (bufsize < nbyte + FUDGE_FACTOR) {
            if (outbuf?
                (outbuf = realloc(outbuf, (unsigned) (nbyte + FUDGE_FACTOR))):
                (outbuf = malloc((unsigned) (nbyte + FUDGE_FACTOR)))) {
                bufsize = nbyte + FUDGE_FACTOR;
            } else {
                bufsize = 0;
                secure_error("%s (in malloc of PROT buffer)", ck_errstr());
                return(ERR);
            }
        }
        if ((length =
             srp_encode(ftp_dpl == FPL_PRV,
                        (CHAR *) buf,
                        (CHAR *) outbuf,
                        nbyte
                        )
             ) < 0) {
            secure_error ("srp_encode failed");
            return ERR;
        }
    }
#endif /* FTP_SRP */
#ifdef FTP_KRB4
    if (ck_krb4_is_installed() && (strcmp(auth_type, "KERBEROS_V4") == 0)) {
        struct sockaddr_in myaddr, hisaddr;
        GSOCKNAME_T len;
        len = sizeof(myaddr);
        if (getsockname(fd, (struct sockaddr*)&myaddr, &len) < 0) {
            secure_error("secure_putbuf: getsockname failed");
            return(ERR);
        }
        len = sizeof(hisaddr);
        if (getpeername(fd, (struct sockaddr*)&hisaddr, &len) < 0) {
            secure_error("secure_putbuf: getpeername failed");
            return(ERR);
        }
        if (bufsize < nbyte + FUDGE_FACTOR) {
            if (outbuf ?
                (outbuf = realloc(outbuf, (unsigned) (nbyte + FUDGE_FACTOR))):
                 (outbuf = malloc((unsigned) (nbyte + FUDGE_FACTOR)))) {
                bufsize = nbyte + FUDGE_FACTOR;
            } else {
                bufsize = 0;
                secure_error("%s (in malloc of PROT buffer)", ck_errstr());
                return(ERR);
            }
        }
        if (ftp_dpl == FPL_PRV) {
            length = krb_mk_priv(buf, (CHAR *) outbuf, nbyte,
                                 ftp_sched,
#ifdef KRB524
                                 ftp_cred.session,
#else /* KRB524 */
                                 &ftp_cred.session,
#endif /* KRB524 */
                                 &myaddr,
                                 &hisaddr
                                 );
        } else {
            length = krb_mk_safe(buf, (CHAR *) outbuf, nbyte,
#ifdef KRB524
                                 ftp_cred.session,
#else /* KRB524 */
                                 &ftp_cred.session,
#endif /* KRB524 */
                                 &myaddr,
                                 &hisaddr
                                 );
        }
        if (length == -1) {
            secure_error("krb_mk_%s failed for KERBEROS_V4",
                         ftp_dpl == FPL_PRV ? "priv" : "safe");
            return(ERR);
        }
    }
#endif /* FTP_KRB4 */
#ifdef FTP_GSSAPI
    if (ck_gssapi_is_installed() && (strcmp(auth_type, "GSSAPI") == 0)) {
        gss_buffer_desc in_buf, out_buf;
        OM_uint32 maj_stat, min_stat;
        int conf_state;
        in_buf.value = buf;
        in_buf.length = nbyte;
        maj_stat = gss_seal(&min_stat, gcontext,
                            (ftp_dpl == FPL_PRV), /* confidential */
                            GSS_C_QOP_DEFAULT,
                            &in_buf,
                            &conf_state,
                            &out_buf
                            );
        if (maj_stat != GSS_S_COMPLETE) {
            /* generally need to deal */
            /* ie. should loop, but for now just fail */
            user_gss_error(maj_stat, min_stat,
                           ftp_dpl == FPL_PRV?
                           "GSSAPI seal failed":
                           "GSSAPI sign failed");
            return(ERR);
        }
        if (bufsize < out_buf.length) {
            if (outbuf ?
                (outbuf = realloc(outbuf, (unsigned) out_buf.length)):
                (outbuf = malloc((unsigned) out_buf.length))) {
                bufsize = out_buf.length;
            } else {
                bufsize = 0;
                secure_error("%s (in malloc of PROT buffer)",
                             ck_errstr());
                return(ERR);
            }
        }
        memcpy(outbuf, out_buf.value, length=out_buf.length);
        gss_release_buffer(&min_stat, &out_buf);
    }
#endif /* FTP_GSSAPI */
    net_len = htonl((ULONG) length);
    if (looping_write(fd, (char *)&net_len, 4) == -1)
      return(-1);
    if (looping_write(fd, outbuf, length) != length)
      return(-1);
    return(0);
}
/* fc = 0 means to get a byte; nonzero means to initialize buffer pointers */
static int
secure_getbyte(fd,fc) int fd,fc; {
    /* number of chars in ucbuf, pointer into ucbuf */
    static unsigned int nin = 0, bufp = 0;
    int kerror;
    ftp_uint32 length;
    if (fc) {
	nin = bufp = 0;
	ucbuf[0] = NUL;
	return(0);
    }
    if (nin == 0) {
        if (iscanceled())
          return(-9);
#ifdef FTP_TIMEOUT
	if (check_data_connection(fd,0) < 0)
	  return(-3);
#endif	/* FTP_TIMEOUT */
#ifdef CK_SSL
        if (ssl_ftp_data_active_flag) {
            int count, error;
            count = SSL_read(ssl_ftp_data_con, ucbuf, ucbufsiz);
            error = SSL_get_error(ssl_ftp_data_con,count);
#ifdef DEBUG
	    if (error != SSL_ERROR_NONE)
	      debug(F101,"ftp secure_getbyte error","",error);
	    if (count == 0)
	      debug(F101,"ftp secure_getbyte count","",count);
#endif	/* DEBUG */
            switch (error) {
              case SSL_ERROR_NONE:
		if (count > 0) {
		    nin = bufp = count;
		    rpackets++;
		    pktnum++;
		    if (fdispla != XYFD_B) {
			rpktl = count;
			ftscreen(SCR_PT,'D',(CK_OFF_T)rpackets,NULL);
		    }
		    break;
		}
              case SSL_ERROR_WANT_WRITE:
              case SSL_ERROR_WANT_READ:
              case SSL_ERROR_SYSCALL:
#ifdef NT
                {
                    int gle = GetLastError();
                }
#endif /* NT */
              case SSL_ERROR_WANT_X509_LOOKUP:
              case SSL_ERROR_SSL:
              case SSL_ERROR_ZERO_RETURN:
              default:
                nin = bufp = count = 0;
                SSL_shutdown(ssl_ftp_data_con);
                SSL_free(ssl_ftp_data_con);
                ssl_ftp_data_active_flag = 0;
                ssl_ftp_data_con = NULL;
#ifdef TCPIPLIB
                socket_close(data);
#else /* TCPIPLIB */
#ifdef USE_SHUTDOWN
                shutdown(data, 1+1);
#endif /* USE_SHUTDOWN */
                close(data);
#endif /* TCPIPLIB */
                data = -1;
                globaldin = data;
                break;
            }
        } else
#endif /* CK_SSL */
          {
              kerror = looping_read(fd, (char *)&length, sizeof(length));
              if (kerror != sizeof(length)) {
                  secure_error("Couldn't read PROT buffer length: %d/%s",
                               kerror,
                               kerror == -1 ? ck_errstr()
                               : "premature EOF"
                               );
                  return(ERR);
              }
              debug(F101,"secure_getbyte length","",length);
              debug(F101,"secure_getbyte ntohl(length)","",ntohl(length));
              length = (ULONG) ntohl(length);
              if (length > maxbuf) {
                  secure_error("Length (%d) of PROT buffer > PBSZ=%u",
                               length,
                               maxbuf
                               );
                  return(ERR);
              }
              if ((kerror = looping_read(fd,(char *)ucbuf,length)) != length) {
                  secure_error("Couldn't read %u byte PROT buffer: %s",
                               length,
                               kerror == -1 ? ck_errstr() : "premature EOF"
                               );
                  return(ERR);
              }
              /* Other auth types go here ... */
#ifdef FTP_SRP
              if (strcmp(auth_type, "SRP") == 0) {
                  if ((nin = bufp = srp_decode (ftp_dpl == FPL_PRV,
                                                (CHAR *) ucbuf,
                                                ucbuf,
                                                length
                                                )
                       ) == -1) {
                      secure_error ("srp_encode failed" );
                      return ERR;
                  }
              }
#endif /* FTP_SRP */
#ifdef FTP_KRB4
              if (strcmp(auth_type, "KERBEROS_V4") == 0) {
                  struct sockaddr_in myaddr, hisaddr;
                  GSOCKNAME_T len;
                  len = sizeof(myaddr);
                  if (getsockname(fd, (struct sockaddr*)&myaddr, &len) < 0) {
                      secure_error("secure_putbuf: getsockname failed");
                      return(ERR);
                  }
                  len = sizeof(hisaddr);
                  if (getpeername(fd, (struct sockaddr*)&hisaddr, &len) < 0) {
                      secure_error("secure_putbuf: getpeername failed");
                      return(ERR);
                  }
                  if (ftp_dpl) {
                      kerror = krb_rd_priv(ucbuf, length, ftp_sched,
#ifdef KRB524
                                           ftp_cred.session,
#else /* KRB524 */
                                           &ftp_cred.session,
#endif /* KRB524 */
                                           &hisaddr, &myaddr, &ftp_msg_data);
                  } else {
                      kerror = krb_rd_safe(ucbuf, length,
#ifdef KRB524
                                           ftp_cred.session,
#else /* KRB524 */
                                           &ftp_cred.session,
#endif /* KRB524 */
                                           &hisaddr, &myaddr, &ftp_msg_data);
                  }
                  if (kerror) {
                      secure_error("krb_rd_%s failed for KERBEROS_V4 (%s)",
                                   ftp_dpl == FPL_PRV ? "priv" : "safe",
                                   krb_get_err_text(kerror));
                      return(ERR);
                  }
                  memcpy(ucbuf,ftp_msg_data.app_data,ftp_msg_data.app_length);
                  nin = bufp = ftp_msg_data.app_length;
              }
#endif /* FTP_KRB4 */
#ifdef FTP_GSSAPI
              if (strcmp(auth_type, "GSSAPI") == 0) {
                  gss_buffer_desc xmit_buf, msg_buf;
                  OM_uint32 maj_stat, min_stat;
                  int conf_state;
                  xmit_buf.value = ucbuf;
                  xmit_buf.length = length;
                  conf_state = (ftp_dpl == FPL_PRV);
                  /* decrypt/verify the message */
                  maj_stat = gss_unseal(&min_stat, gcontext, &xmit_buf,
                                        &msg_buf, &conf_state, NULL);
                  if (maj_stat != GSS_S_COMPLETE) {
                      user_gss_error(maj_stat, min_stat,
                                     (ftp_dpl == FPL_PRV)?
                                     "failed unsealing ENC message":
                                     "failed unsealing MIC message");
                      return ERR;
                  }
                  memcpy(ucbuf, msg_buf.value, nin = bufp = msg_buf.length);
                  gss_release_buffer(&min_stat, &msg_buf);
              }
#endif /* FTP_GSSAPI */
              /* Other auth types go here ... */
              /* Update file transfer display */
              rpackets++;
              pktnum++;
              if (fdispla != XYFD_B) {
                  rpktl = nin;
                  ftscreen(SCR_PT,'D',(CK_OFF_T)rpackets,NULL);
              }
          }
    }
    if (nin == 0)
      return(EOF);
    else
      return(ucbuf[bufp - nin--]);
}
/* secure_getc(fd,fc)
 * Call with:
 *   fd = file descriptor for connection.
 *   fc = 0 to get a character, fc != 0 to initialize buffer pointers.
 * Returns:
 *   c>=0 on success (character value)
 *   -1   on EOF
 *   -2   on security error
 *   -3   on timeout (if built with FTP_TIMEOUT defined)
 */
static int
secure_getc(fd,fc) int fd,fc; {		/* file descriptor, function code */
    if (!ftpissecure()) {
        static unsigned int nin = 0, bufp = 0;
	if (fc) {
	    nin = bufp = 0;
	    ucbuf[0] = NUL;
	    return(0);
	}
        if (nin == 0) {
            if (iscanceled())
              return(-9);
#ifdef FTP_TIMEOUT
	    if (check_data_connection(fd,0) < 0) {
                debug(F100,"secure_getc TIMEOUT","",0);
                nin = bufp = 0;
		ftp_timed_out = 1;
		return(-3);
	    }		
#endif	/* FTP_TIMEOUT */
            nin = bufp = recv(fd,(char *)ucbuf,actualbuf,0);
            if ((nin == 0) || (nin == (unsigned int)-1)) {
                debug(F111,"secure_getc recv errno",ckitoa(nin),errno);
                debug(F101,"secure_getc returns EOF","",EOF);
                nin = bufp = 0;
                return(EOF);
            }
            debug(F101,"ftp secure_getc recv","",nin);
            ckhexdump("ftp secure_getc recv",ucbuf,16);
            rpackets++;
            pktnum++;
            if (fdispla != XYFD_B) {
                rpktl = nin;
                ftscreen(SCR_PT,'D',(CK_OFF_T)rpackets,NULL);
            }
        }
        return(ucbuf[bufp - nin--]);
    } else
      return(secure_getbyte(fd,fc));
}
/* returns:
 *     n>0  on success (n == # of bytes read)
 *       0  on EOF
 *      -1  on error (errno set), only for FPL_CLR
 *      -2  on security error
 */
static int
secure_read(fd, buf, nbyte) int fd; char *buf; int nbyte; {
    static int c = 0;
    int i;
    debug(F101,"secure_read bytes requested","",nbyte);
    if (c == EOF)
      return(c = 0);
    for (i = 0; nbyte > 0; nbyte--) {
        c = secure_getc(fd,0);
        switch (c) {
          case -9:                      /* Canceled from keyboard */
            debug(F101,"ftp secure_read interrupted","",c);
            return(0);
          case ERR:
            debug(F101,"ftp secure_read error","",c);
            return(c);
          case EOF:
            debug(F101,"ftp secure_read EOF","",c);
            if (!i)
              c = 0;
            return(i);
#ifdef FTP_TIMEOUT
          case -3:
            debug(F101,"ftp secure_read timeout","",c);
	    return(c);
#endif	/* FTP_TIMEOUT */
          default:
            buf[i++] = c;
        }
    }
    return(i);
}
#ifdef USE_RUSERPASS
/* BEGIN_RUSERPASS
 *
 * Copyright (c) 1985 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by the University of
 *      California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#ifndef lint
static char sccsid[] = "@(#)ruserpass.c 5.3 (Berkeley) 3/1/91";
#endif /* not lint */
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 64
#endif
char * renvlook();
static FILE * cfile;
#define DEFAULT 1
#define LOGIN   2
#define PASSWD  3
#define ACCOUNT 4
#define MACDEF  5
#define ID      10
#define MACH    11
static char tokval[100];
static struct toktab {
    char *tokstr;
    int tval;
} toktab[]= {
    "default",  DEFAULT,
    "login",    LOGIN,
    "password", PASSWD,
    "passwd",   PASSWD,
    "account",  ACCOUNT,
    "machine",  MACH,
    "macdef",   MACDEF,
    0,          0
};
static int
token() {
    char *cp;
    int c;
    struct toktab *t;
    if (feof(cfile))
      return(0);
    while ((c = getc(cfile)) != EOF &&
           (c == '\n' || c == '\t' || c == ' ' || c == ','))
      continue;
    if (c == EOF)
      return(0);
    cp = tokval;
    if (c == '"') {
        while ((c = getc(cfile)) != EOF && c != '"') {
            if (c == '\\')
              c = getc(cfile);
            *cp++ = c;
        }
    } else {
        *cp++ = c;
        while ((c = getc(cfile)) != EOF
               && c != '\n' && c != '\t' && c != ' ' && c != ',') {
            if (c == '\\')
              c = getc(cfile);
            *cp++ = c;
        }
    }
    *cp = 0;
    if (tokval[0] == 0)
      return(0);
    for (t = toktab; t->tokstr; t++)
      if (!strcmp(t->tokstr, tokval))
        return(t->tval);
    return(ID);
}
ruserpass(host, aname, apass, aacct)
    char *host, **aname, **apass, **aacct;
{
    char *hdir, buf[FTP_BUFSIZ], *tmp;
    char myname[MAXHOSTNAMELEN], *mydomain;
    int t, i, c, usedefault = 0;
#ifdef NT
    struct _stat stb;
#else /* NT */
    struct stat stb;
#endif /* NT */
    hdir = getenv("HOME");
    if (hdir == NULL)
        hdir = ".";
    ckmakmsg(buf,FTP_BUFSIZ,hdir,"/.netrc",NULL,NULL);
    cfile = fopen(buf, "r");
    if (cfile == NULL) {
        if (errno != ENOENT)
          perror(buf);
        return(0);
    }
    if (gethostname(myname, MAXHOSTNAMELEN) < 0)
      myname[0] = '\0';
    if ((mydomain = ckstrchr(myname, '.')) == NULL)
      mydomain = "";
  next:
    while ((t = token())) switch(t) {
      case DEFAULT:
        usedefault = 1;
        /* FALL THROUGH */
      case MACH:
        if (!usedefault) {
            if (token() != ID)
              continue;
            /*
             * Allow match either for user's input host name
             * or official hostname.  Also allow match of
             * incompletely-specified host in local domain.
             */
            if (ckstrcmp(host, tokval,-1,1) == 0)
              goto match;
            if (ckstrcmp(ftp_host, tokval,-1,0) == 0)
              goto match;
            if ((tmp = ckstrchr(ftp_host, '.')) != NULL &&
                ckstrcmp(tmp, mydomain,-1,1) == 0 &&
                ckstrcmp(ftp_host, tokval, tmp-ftp_host,0) == 0 &&
                tokval[tmp - ftp_host] == '\0')
              goto match;
            if ((tmp = ckstrchr(host, '.')) != NULL &&
                ckstrcmp(tmp, mydomain,-1,1) == 0 &&
                ckstrcmp(host, tokval, tmp - host, 0) == 0 &&
                tokval[tmp - host] == '\0')
              goto match;
            continue;
        }
      match:
        while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
          case LOGIN:
            if (token())
              if (*aname == 0) {
                  *aname = malloc((unsigned) strlen(tokval) + 1);
                  strcpy(*aname, tokval);      /* safe */
              } else {
                  if (strcmp(*aname, tokval))
                    goto next;
              }
            break;
          case PASSWD:
            if (strcmp(*aname, "anonymous") &&
                fstat(fileno(cfile), &stb) >= 0 &&
                (stb.st_mode & 077) != 0) {
                fprintf(stderr, "Error - .netrc file not correct mode.\n");
                fprintf(stderr, "Remove password or correct mode.\n");
                goto bad;
            }
            if (token() && *apass == 0) {
                *apass = malloc((unsigned) strlen(tokval) + 1);
                strcpy(*apass, tokval);          /* safe */
            }
            break;
          case ACCOUNT:
            if (fstat(fileno(cfile), &stb) >= 0
                && (stb.st_mode & 077) != 0) {
                fprintf(stderr, "Error - .netrc file not correct mode.\n");
                fprintf(stderr, "Remove account or correct mode.\n");
                goto bad;
            }
            if (token() && *aacct == 0) {
                *aacct = malloc((unsigned) strlen(tokval) + 1);
                strcpy(*aacct, tokval);          /* safe */
            }
            break;
          default:
            fprintf(stderr, "Unknown .netrc keyword %s\n", tokval);
            break;
        }
        goto done;
    }
  done:
    fclose(cfile);
    return(0);
  bad:
    fclose(cfile);
    return(-1);
}
#endif /* USE_RUSERPASS */
static char *radixN =
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static char pad = '=';
static int
radix_encode(inbuf, outbuf, inlen, outlen, decode)
    CHAR inbuf[], outbuf[];
    int inlen, *outlen, decode;
{
    int i, j, D = 0;
    char *p;
    CHAR c = NUL;
    if (decode) {
        for (i = 0, j = 0; inbuf[i] && inbuf[i] != pad; i++) {
            if ((p = ckstrchr(radixN, inbuf[i])) == NULL)
              return(1);
            D = p - radixN;
            switch (i&3) {
              case 0:
                outbuf[j] = D<<2;
                break;
              case 1:
                outbuf[j++] |= D>>4;
                outbuf[j] = (D&15)<<4;
                break;
              case 2:
                outbuf[j++] |= D>>2;
                outbuf[j] = (D&3)<<6;
                break;
              case 3:
                outbuf[j++] |= D;
            }
            if (j == *outlen)
              return(4);
        }
        switch (i&3) {
          case 1: return(3);
          case 2: if (D&15) return(3);
            if (strcmp((char *)&inbuf[i], "==")) return(2);
            break;
          case 3: if (D&3) return(3);
            if (strcmp((char *)&inbuf[i], "="))  return(2);
        }
        *outlen = j;
    } else {
        for (i = 0, j = 0; i < inlen; i++) {
            switch (i%3) {
              case 0:
                outbuf[j++] = radixN[inbuf[i]>>2];
                c = (inbuf[i]&3)<<4;
                break;
              case 1:
                outbuf[j++] = radixN[c|inbuf[i]>>4];
                c = (inbuf[i]&15)<<2;
                break;
              case 2:
                outbuf[j++] = radixN[c|inbuf[i]>>6];
                outbuf[j++] = radixN[inbuf[i]&63];
                c = 0;
            }
            if (j == *outlen)
              return(4);
        }
        if (i%3) outbuf[j++] = radixN[c];
        switch (i%3) {
          case 1: outbuf[j++] = pad;
          case 2: outbuf[j++] = pad;
        }
        outbuf[*outlen = j] = '\0';
    }
    return(0);
}
static char *
radix_error(e) int e;
{
    switch (e) {
      case 0:  return("Success");
      case 1:  return("Bad character in encoding");
      case 2:  return("Encoding not properly padded");
      case 3:  return("Decoded # of bits not a multiple of 8");
      case 4:  return("Output buffer too small");
      default: return("Unknown error");
    }
}
/* END_RUSERPASS */
#ifdef FTP_SRP
/*---------------------------------------------------------------------------+
 |                                                                           |
 |   Package: srpftp                                                         |
 |   Author: Eugene Jhong                                                    |
 |                                                                           |
 +---------------------------------------------------------------------------*/
/*
 * Copyright (c) 1997-1999  The Stanford SRP Authentication Project
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 *
 * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
 * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * In addition, the following conditions apply:
 *
 * 1. Any software that incorporates the SRP authentication technology
 *    must display the following acknowlegment:
 *    "This product uses the 'Secure Remote Password' cryptographic
 *     authentication system developed by Tom Wu (tjw@CS.Stanford.EDU)."
 *
 * 2. Any software that incorporates all or part of the SRP distribution
 *    itself must also display the following acknowledgment:
 *    "This product includes software developed by Tom Wu and Eugene
 *     Jhong for the SRP Distribution (http://srp.stanford.edu/srp/)."
 *
 * 3. Redistributions in source or binary form must retain an intact copy
 *    of this copyright notice and list of conditions.
 */
#define SRP_PROT_VERSION        1
#ifdef CK_ENCRYPTION
#define SRP_DEFAULT_CIPHER      CIPHER_ID_CAST5_CBC
#else
#define SRP_DEFAULT_CIPHER      CIPHER_ID_NONE
#endif /* CK_ENCRYPTION */
#define SRP_DEFAULT_HASH        HASH_ID_SHA
CHAR srp_pref_cipher = CIPHER_ID_DES3_ECB;
CHAR srp_pref_hash = HASH_ID_SHA;
static struct t_client *tc = NULL;
static CHAR *skey = NULL;
static krypto_context *incrypt = NULL;
static krypto_context *outcrypt = NULL;
typedef unsigned int srp_uint32;
/*--------------------------------------------------------------+
 | srp_selcipher: select cipher                                 |
 +--------------------------------------------------------------*/
static int
srp_selcipher (cname) char *cname; {
    cipher_desc *cd;
    if (!(cd = cipher_getdescbyname (cname))) {
        int i;
        CHAR *list = cipher_getlist ();
        fprintf (stderr, "ftp: supported ciphers:\n\n");
        for (i = 0; i < strlen (list); i++)
          fprintf (stderr, "    %s\n", (cipher_getdescbyid(list[i]))->name);
        fprintf (stderr, "\n");
        return -1;
    }
    srp_pref_cipher = cd->id;
    return 0;
}
/*--------------------------------------------------------------+
 | srp_selhash: select hash                                     |
 +--------------------------------------------------------------*/
static int
srp_selhash (hname) char *hname; {
    hash_desc *hd;
    if (!(hd = hash_getdescbyname (hname))) {
        int i;
        CHAR *list = hash_getlist ();
        fprintf (stderr, "ftp: supported hash functions:\n\n");
        for (i = 0; i < strlen (list); i++)
          fprintf (stderr, "    %s\n", (hash_getdescbyid(list[i]))->name);
        fprintf (stderr, "\n");
        return -1;
    }
    srp_pref_hash = hd->id;
    return 0;
}
/*--------------------------------------------------------------+
 | srp_userpass: get username and password                      |
 +--------------------------------------------------------------*/
static int
srp_userpass (host) char *host; {
    char tmp[BUFSIZ], prompt[PROMPTSIZ];
    char *user;
    user = NULL;
#ifdef USE_RUSERPASS
    ruserpass (host, &user, &srp_pass, &srp_acct);
#endif /* USE_RUSERPASS */
    while (user == NULL)     {
        char *myname;
        int ok;
        myname = whoami();
        if (!myname) myname = "";
        if (myname[0])
          ckmakxmsg(prompt,PROMPTSIZ," Name (",host,":",myname,"): ",
                    NULL,NULL,NULL,NULL,NULL,NULL,NULL);
        else
          ckmakmsg(prompt,PROMPTSIZ," Name (",host,"): ",NULL);
        tmp[0] = '\0';
        ok = uq_txt(NULL,prompt,1,NULL,tmp,BUFSIZ,NULL,
                    DEFAULT_UQ_TIMEOUT);
        if (!ok || *tmp == '\0')
          user = myname;
        else
          user = brstrip(tmp);
    }
    ckstrncpy (srp_user, user,BUFSIZ);
    return(0);
}
/*--------------------------------------------------------------+
 | srp_reset: reset srp information                             |
 +--------------------------------------------------------------*/
static int
srp_reset () {
    if (tc) { t_clientclose (tc); tc = NULL; }
    if (incrypt) { krypto_delete (incrypt); incrypt = NULL; }
    if (outcrypt) { krypto_delete (outcrypt); outcrypt = NULL; }
    return(0);
}
/*--------------------------------------------------------------+
 | srp_ftp_auth: perform srp authentication                         |
 +--------------------------------------------------------------*/
static int
srp_ftp_auth(host, user, pass)
    char *host;
    char *user;
    char *pass;
{
    struct t_num *wp;
    struct t_num N;
    struct t_num g;
    struct t_num s;
    struct t_num yp;
    CHAR buf[FTP_BUFSIZ];
    CHAR tmp[FTP_BUFSIZ];
    CHAR *bp, *cp;
    int n, e, clen, blen, len, i;
    CHAR cid = 0;
    CHAR hid = 0;
    srp_pass = srp_acct = 0;
    n = ftpcmd("AUTH SRP",NULL,0,0,ftp_vbm);
    if (n != REPLY_CONTINUE) {
        if (ftp_deb)
            fprintf(stderr, "SRP rejected as an authentication type\n");
        return(0);
    } else {                            /* Send protocol version */
        CHAR vers[4];
        memset (vers, 0, 4);
        vers[3] = SRP_PROT_VERSION;
        if (!quiet)
          printf ("SRP accepted as authentication type.\n");
        bp = tmp; blen = 0;
        srp_put (vers, &bp, 4, &blen);
        len = FTP_BUFSIZ;
        if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
          goto encode_error;
        reply_parse = "ADAT=";
        n = ftpcmd("ADAT",buf,-1,-1,0);
    }
    if (n == REPLY_CONTINUE) {          /* Get protocol version */
        bp = buf;
        if (!reply_parse)
          goto data_error;
        blen = FTP_BUFSIZ;
        if (e = radix_encode(reply_parse, bp, 0, &blen, RADIX_DECODE))
          goto decode_error;
        if (srp_get (&bp, &cp, &blen, &clen) != 4)
          goto data_error;
        if (host) {                     /* Get username/password if needed */
            srp_userpass (host);
        } else {
            ckstrncpy (srp_user, user, BUFSIZ);
            srp_pass = pass;
        }
        bp = tmp; blen = 0;             /* Send username */
        srp_put (srp_user, &bp, strlen (srp_user), &blen);
        len = FTP_BUFSIZ;
        if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
          goto encode_error;
        reply_parse = "ADAT=";
        n = ftpcmd("ADAT",buf,-1,-1,0);
    }
    if (n == REPLY_CONTINUE) {          /* Get N, g and s */
        bp = buf;
        if (!reply_parse)
          goto data_error;
        blen = FTP_BUFSIZ;
        if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
          goto decode_error;
        if (srp_get (&bp, &(N.data), &blen, &(N.len)) < 0)
          goto data_error;
        if (srp_get (&bp, &(g.data), &blen, &(g.len)) < 0)
          goto data_error;
        if (srp_get (&bp, &(s.data), &blen, &(s.len)) < 0)
          goto data_error;
        if ((tc = t_clientopen (srp_user, &N, &g, &s)) == NULL) {
            fprintf (stderr, "Unable to open SRP client structure.\n");
            goto bad;
        }
        wp = t_clientgenexp (tc);       /* Send wp */
        bp = tmp; blen = 0;
        srp_put (wp->data, &bp, wp->len, &blen);
        len = FTP_BUFSIZ;
        if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
          goto encode_error;
        reply_parse = "ADAT=";
        n = ftpcmd("ADAT",buf,-1,-1,0);
    }
    if (n == REPLY_CONTINUE) {          /* Get yp */
        bp = buf;
        if (!reply_parse)
          goto data_error;
        blen = FTP_BUFSIZ;
        if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
          goto decode_error;
        if (srp_get (&bp, &(yp.data), &blen, &(yp.len)) < 0)
          goto data_error;
        if (!srp_pass) {
            static char ftppass[PASSBUFSIZ];
            int ok;
            setint();
            ok = uq_txt(NULL," SRP Password: ",2,NULL,ftppass,PASSBUFSIZ,NULL,
                        DEFAULT_UQ_TIMEOUT);
            if (ok)
	      srp_pass = brstrip(ftppass);
        }
        t_clientpasswd (tc, srp_pass);
        memset (srp_pass, 0, strlen (srp_pass));
        skey = t_clientgetkey (tc, &yp); /* Send response */
        bp = tmp; blen = 0;
        srp_put (t_clientresponse (tc), &bp, 20, &blen);
        len = FTP_BUFSIZ;
        if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
          goto encode_error;
        reply_parse = "ADAT=";
        n = ftpcmd("ADAT",buf,-1,-1,0);
    }
    if (n == REPLY_CONTINUE) {          /* Get response */
        bp = buf;
        if (!reply_parse)
          goto data_error;
        blen = FTP_BUFSIZ;
        if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
          goto encode_error;
        if (srp_get (&bp, &cp, &blen, &clen) != 20)
          goto data_error;
        if (t_clientverify (tc, cp)) {
            fprintf (stderr, "WARNING: bad response to client challenge.\n");
            goto bad;
        }
        bp = tmp; blen = 0;             /* Send nothing */
        srp_put ("\0", &bp, 1, &blen);
        len = FTP_BUFSIZ;
        if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
          goto encode_error;
        reply_parse = "ADAT=";
        n = ftpcmd("ADAT",buf,-1,-1,0);
    }
    if (n == REPLY_CONTINUE) {          /* Get cipher & hash lists, seqnum */
        CHAR seqnum[4];
        CHAR *clist;
        CHAR *hlist;
        CHAR *p1;
        int clist_len, hlist_len;
        bp = buf;
        if (!reply_parse)
          goto data_error;
        blen = FTP_BUFSIZ;
        if (e = radix_encode (reply_parse, bp, 0, &blen, RADIX_DECODE))
          goto encode_error;
        if (srp_get (&bp, &clist, &blen, &clist_len) < 0)
          goto data_error;
        if (srp_get (&bp, &hlist, &blen, &hlist_len) < 0)
          goto data_error;
        if (srp_get (&bp, &cp, &blen, &clen) != 4)
          goto data_error;
        memcpy (seqnum, cp, 4);
        if (cipher_supported (clist, srp_pref_cipher)) /* Choose cipher */
          cid = srp_pref_cipher;
        if (!cid && cipher_supported (clist, SRP_DEFAULT_CIPHER))
          cid = SRP_DEFAULT_CIPHER;
        if (!cid) {
            CHAR *loclist = cipher_getlist ();
            for (i = 0; i < strlen (loclist); i++)
              if (cipher_supported (clist, loclist[i])) {
                  cid = loclist[i];
                  break;
              }
        }
        if (!cid) {
            fprintf (stderr, "Unable to agree on cipher.\n");
            goto bad;
        }
        /* Choose hash */
        if (srp_pref_hash && hash_supported (hlist, srp_pref_hash))
          hid = srp_pref_hash;
        if (!hid && hash_supported (hlist, SRP_DEFAULT_HASH))
          hid = SRP_DEFAULT_HASH;
        if (!hid) {
            CHAR *loclist = hash_getlist ();
            for (i = 0; i < strlen (loclist); i++)
              if (hash_supported (hlist, loclist[i])) {
                  hid = loclist[i];
                  break;
              }
        }
        if (!hid) {
            fprintf (stderr, "Unable to agree on hash.\n");
            goto bad;
        }
        /* Set incrypt */
        if (!(incrypt = krypto_new (cid, hid, skey, 20, NULL, 0, seqnum,
                                    KRYPTO_DECODE)))
          goto bad;
        /* Generate random number for outkey and outseqnum */
        t_random (seqnum, 4);
        /* Send cid, hid, outkey, outseqnum */
        bp = tmp; blen = 0;
        srp_put (&cid, &bp, 1, &blen);
        srp_put (&hid, &bp, 1, &blen);
        srp_put (seqnum, &bp, 4, &blen);
        len = FTP_BUFSIZ;
        if (e = radix_encode (tmp, buf, blen, &len, RADIX_ENCODE))
          goto encode_error;
        reply_parse = "ADAT=";
        n = ftpcmd("ADAT",buf,-1,-1,0);
        /* Set outcrypt */
        if (!(outcrypt = krypto_new (cid, hid, skey+20, 20, NULL, 0, seqnum,
                                     KRYPTO_ENCODE)))
          goto bad;
        t_clientclose (tc);
        tc = NULL;
    }
    if (n != REPLY_COMPLETE)
      goto bad;
    if (ftp_vbm) {
        if (ftp_deb)
          printf("\n");
        printf ("SRP authentication succeeded.\n");
        printf ("Using cipher %s and hash function %s.\n",
                (cipher_getdescbyid(cid))->name,
                (hash_getdescbyid(hid))->name
                );
    }
    reply_parse = NULL;
    auth_type = "SRP";
    return(1);
  encode_error:
    fprintf (stderr, "Base 64 encoding failed: %s.\n", radix_error (e));
    goto bad;
  decode_error:
    fprintf (stderr, "Base 64 decoding failed: %s.\n", radix_error (e));
    goto bad;
  data_error:
    fprintf (stderr, "Unable to unmarshal authentication data.\n");
    goto bad;
  bad:
    fprintf (stderr, "SRP authentication failed, trying regular login.\n");
    reply_parse = NULL;
    return(0);
}
/*--------------------------------------------------------------+
 | srp_put: put item to send buffer                             |
 +--------------------------------------------------------------*/
static int
srp_put (in, out, inlen, outlen)
    CHAR *in;
    CHAR **out;
    int inlen;
    int *outlen;
{
    srp_uint32 net_len;
    net_len = htonl (inlen);
    memcpy (*out, &net_len, 4);
    *out += 4; *outlen += 4;
    memcpy (*out, in, inlen);
    *out += inlen; *outlen += inlen;
    return(0);
}
/*--------------------------------------------------------------+
 | srp_get: get item from receive buffer                        |
 +--------------------------------------------------------------*/
static int
srp_get (in, out, inlen, outlen)
    CHAR **in;
    CHAR **out;
    int *inlen;
    int *outlen;
{
    srp_uint32 net_len;
    if (*inlen < 4) return -1;
    memcpy (&net_len, *in, 4); *inlen -= 4; *in += 4;
    *outlen = ntohl (net_len);
    if (*inlen < *outlen) return -1;
    *out = *in; *inlen -= *outlen; *in += *outlen;
    return *outlen;
}
/*--------------------------------------------------------------+
 | srp_encode: encode control message                           |
 +--------------------------------------------------------------*/
static int
srp_encode (private, in, out, len)
    int private;
    CHAR *in;
    CHAR *out;
    unsigned len;
{
    if (private)
      return krypto_msg_priv (outcrypt, in, out, len);
    else
      return krypto_msg_safe (outcrypt, in, out, len);
}
/*--------------------------------------------------------------+
 | srp_decode: decode control message                           |
 +--------------------------------------------------------------*/
static int
srp_decode (private, in, out, len)
    int private;
    CHAR *in;
    CHAR *out;
    unsigned len;
{
    if (private)
      return krypto_msg_priv (incrypt, in, out, len);
    else
      return krypto_msg_safe (incrypt, in, out, len);
}
#endif /* FTP_SRP */
#ifdef NOT_USED
/*
  The following code is from the Unix FTP client.  Be sure to
  make sure that the functionality is not lost.  Especially
  the Proxy stuff even though we have not yet implemented it.
*/
/* Send multiple files  */
static int
ftp_mput(argc, argv) int argc; char **argv; {
    register int i;
    sig_t oldintr;
    int ointer;
    char *tp;
    sigtype mcancel();
    if (argc < 2 && !another(&argc, &argv, "local-files")) {
        printf("usage: %s local-files\n", argv[0]);
        ftpcode = -1;
        return;
    }
    mname = argv[0];
    mflag = 1;
    oldintr = signal(SIGINT, mcancel);
    /* Replace with calls to cc_execute() */
    setjmp(jcancel);
#ifdef FTP_PROXY
    if (proxy) {
        char *cp, *tp2, tmpbuf[CKMAXPATH];
        while ((cp = remglob(argv,0)) != NULL) {
            if (*cp == 0) {
                mflag = 0;
                continue;
            }
            if (mflag && confirm(argv[0], cp)) {
                tp = cp;
                if (mcase) {
                    while (*tp && !islower(*tp)) {
                        tp++;
                    }
                    if (!*tp) {
                        tp = cp;
                        tp2 = tmpbuf;
                        while ((*tp2 = *tp) != 0) {
                            if (isupper(*tp2)) {
                                *tp2 = 'a' + *tp2 - 'A';
                            }
                            tp++;
                            tp2++;
                        }
                    }
                    tp = tmpbuf;
                }
                if (ntflag) {
                    tp = dotrans(tp);
                }
                if (mapflag) {
                    tp = domap(tp);
                }
                sendrequest((sunique) ? "STOU" : "STOR", cp, tp, 0, -1, -1, 0);
                if (!mflag && fromatty) {
                    ointer = interactive;
                    interactive = 1;
                    if (confirm("Continue with","mput")) {
                        mflag++;
                    }
                    interactive = ointer;
                }
            }
        }
        signal(SIGINT, oldintr);
        mflag = 0;
        return;
    }
#endif /* FTP_PROXY */
    for (i = 1; i < argc; i++) {
        register char **cpp, **gargs;
        if (mflag && confirm(argv[0], argv[i])) {
            tp = argv[i];
            sendrequest((ftp_usn) ? "STOU" : "STOR", argv[i], tp, 0,-1,-1, 0);
            if (!mflag && fromatty) {
                ointer = interactive;
                interactive = 1;
                if (confirm("Continue with","mput")) {
                    mflag++;
                }
                interactive = ointer;
            }
        }
        continue;
        gargs = ftpglob(argv[i]);
        if (globerr != NULL) {
            printf("%s\n", globerr);
            if (gargs) {
                blkfree(gargs);
                free((char *)gargs);
            }
            continue;
        }
        for (cpp = gargs; cpp && *cpp != NULL; cpp++) {
            if (mflag && confirm(argv[0], *cpp)) {
                tp = *cpp;
                sendrequest((sunique) ? "STOU":"STOR", *cpp, tp, 0, -1, -1, 0);
                if (!mflag && fromatty) {
                    ointer = interactive;
                    interactive = 1;
                    if (confirm("Continue with","mput")) {
                        mflag++;
                    }
                    interactive = ointer;
                }
            }
        }
        if (gargs != NULL) {
            blkfree(gargs);
            free((char *)gargs);
        }
    }
    signal(SIGINT, oldintr);
    mflag = 0;
}
/* Get multiple files */
static int
ftp_mget(argc, argv) int argc; char **argv; {
    int rc = -1;
    sig_t oldintr;
    int ointer;
    char *cp, *tp, *tp2, tmpbuf[CKMAXPATH];
    sigtype mcancel();
    if (argc < 2 && !another(&argc, &argv, "remote-files")) {
        printf("usage: %s remote-files\n", argv[0]);
        ftpcode = -1;
        return(-1);
    }
    mname = argv[0];
    mflag = 1;
    oldintr = signal(SIGINT,mcancel);
    /* Replace with calls to cc_execute() */
    setjmp(jcancel);
    while ((cp = remglob(argv,proxy)) != NULL) {
        if (*cp == '\0') {
            mflag = 0;
            continue;
        }
        if (mflag && confirm(argv[0], cp)) {
            tp = cp;
            if (mcase) {
                while (*tp && !islower(*tp)) {
                    tp++;
                }
                if (!*tp) {
                    tp = cp;
                    tp2 = tmpbuf;
                    while ((*tp2 = *tp) != 0) {
                        if (isupper(*tp2)) {
                            *tp2 = 'a' + *tp2 - 'A';
                        }
                        tp++;
                        tp2++;
                    }
                }
                tp = tmpbuf;
            }
            rc = (recvrequest("RETR", tp, cp, "wb",
                               tp != cp || !interactive) == 0,0,NULL,0,0,0);
            if (!mflag && fromatty) {
                ointer = interactive;
                interactive = 1;
                if (confirm("Continue with","mget")) {
                    mflag++;
                }
                interactive = ointer;
            }
        }
    }
    signal(SIGINT,oldintr);
    mflag = 0;
    return(rc);
}
/* Delete multiple files */
static int
mdelete(argc, argv) int argc; char **argv; {
    sig_t oldintr;
    int ointer;
    char *cp;
    sigtype mcancel();
    if (argc < 2 && !another(&argc, &argv, "remote-files")) {
        printf("usage: %s remote-files\n", argv[0]);
        ftpcode = -1;
        return(-1);
    }
    mname = argv[0];
    mflag = 1;
    oldintr = signal(SIGINT, mcancel);
    /* Replace with calls to cc_execute() */
    setjmp(jcancel);
    while ((cp = remglob(argv,0)) != NULL) {
        if (*cp == '\0') {
            mflag = 0;
            continue;
        }
        if (mflag && confirm(argv[0], cp)) {
            rc = (ftpcmd("DELE",cp,-1,-1,ftp_vbm) == REPLY_COMPLETE);
            if (!mflag && fromatty) {
                ointer = interactive;
                interactive = 1;
                if (confirm("Continue with", "mdelete")) {
                    mflag++;
                }
                interactive = ointer;
            }
        }
    }
    signal(SIGINT, oldintr);
    mflag = 0;
    return(rc);
}
/* Get a directory listing of multiple remote files */
static int
mls(argc, argv) int argc; char **argv; {
    sig_t oldintr;
    int ointer, i;
    char *cmd, mode[1], *dest;
    sigtype mcancel();
    int rc = -1;
    if (argc < 2 && !another(&argc, &argv, "remote-files"))
      goto usage;
    if (argc < 3 && !another(&argc, &argv, "local-file")) {
      usage:
        printf("usage: %s remote-files local-file\n", argv[0]);
        ftpcode = -1;
        return(-1);
    }
    dest = argv[argc - 1];
    argv[argc - 1] = NULL;
    if (strcmp(dest, "-") && *dest != '|')
      if (!globulize(&dest) ||
          !confirm("output to local-file:", dest)) {
          ftpcode = -1;
          return(-1);
      }
    cmd = argv[0][1] == 'l' ? "NLST" : "LIST";
    mname = argv[0];
    mflag = 1;
    oldintr = signal(SIGINT, mcancel);
    /* Replace with calls to cc_execute() */
    setjmp(jcancel);
    for (i = 1; mflag && i < argc-1; ++i) {
        *mode = (i == 1) ? 'w' : 'a';
        rc = recvrequest(cmd, dest, argv[i], mode, 0,0,NULL,0,0,0);
        if (!mflag && fromatty) {
            ointer = interactive;
            interactive = 1;
            if (confirm("Continue with", argv[0])) {
                mflag ++;
            }
            interactive = ointer;
        }
    }
    signal(SIGINT, oldintr);
    mflag = 0;
    return(rc);
}
static char *
remglob(argv,doswitch) char *argv[]; int doswitch; {
    char temp[16];
    static char buf[CKMAXPATH];
    static FILE *ftemp = NULL;
    static char **args;
    int oldhash;
    char *cp, *mode;
    if (!mflag) {
        if (!doglob) {
            args = NULL;
        } else {
            if (ftemp) {
                (void) fclose(ftemp);
                ftemp = NULL;
            }
        }
        return(NULL);
    }
    if (!doglob) {
        if (args == NULL)
          args = argv;
        if ((cp = *++args) == NULL)
          args = NULL;
        return(cp);
    }
    if (ftemp == NULL) {
        (void) strcpy(temp, _PATH_TMP);
#ifdef MKTEMP
#ifndef MKSTEMP
        (void) mktemp(temp);
#endif /* MKSTEMP */
#endif /* MKTEMP */
        verbose = 0;
        oldhash = hash, hash = 0;
#ifdef FTP_PROXY
        if (doswitch) {
            pswitch(!proxy);
        }
#endif /* FTP_PROXY */
        for (mode = "wb"; *++argv != NULL; mode = "ab")
          recvrequest ("NLST", temp, *argv, mode, 0);
#ifdef FTP_PROXY
        if (doswitch) {
            pswitch(!proxy);
        }
#endif /* FTP_PROXY */
        hash = oldhash;
        ftemp = fopen(temp, "r");
        unlink(temp);
        if (ftemp == NULL && (!dpyactive || ftp_deb)) {
            printf("Can't find list of remote files, oops\n");
            return(NULL);
        }
    }
    if (fgets(buf, CKMAXPATH, ftemp) == NULL) {
        fclose(ftemp), ftemp = NULL;
        return(NULL);
    }
    if ((cp = ckstrchr(buf,'\n')) != NULL)
      *cp = '\0';
    return(buf);
}
#endif /* NOT_USED */
#endif /* TCPSOCKET (top of file) */
#endif /* SYSFTP (top of file) */
#endif /* NOFTP (top of file) */
 
     |