zshrs 0.10.10

The first compiled Unix shell — bytecode VM, worker pool, AOP intercept, Rkyv caching
Documentation
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
//! ZFTP module - port of Modules/zftp.c
//!
//! it's a TELNET based protocol, but don't think I like doing this         // c:56
//! Number of connections actually open                                      // c:210
//! zfclosing is set if zftp_close() is active                               // c:219
//! List of active sessions                                                  // c:310
//!
//! Provides a builtin FTP client for zsh.

use std::collections::HashMap;
use std::io::{self, BufRead, BufReader, Read, Write};
use std::net::{TcpStream, ToSocketAddrs};
use std::path::Path;
use std::time::Duration;
use std::sync::atomic::Ordering;
use std::os::unix::io::AsRawFd;

/// WARNING: NOT IN ZFTP.C — platform-gated `errno` pointer; C reads errno directly after syscalls
/// (equivalent C logic at Src/Modules/zftp.c:25).
/// Platform-gated `errno` pointer. zsh's C source writes `errno` directly
/// after `select(2)`/`read(2)` races; macOS exposes `__error()`, Linux/Android
/// expose `__errno_location()`, BSDs use `__errno`. Returning a raw pointer
/// keeps the original `*errno = X` write-shape from the C source intact.
#[inline]
fn errno_ptr() -> *mut libc::c_int {
    #[cfg(any(target_os = "linux", target_os = "android"))]
    unsafe { libc::__errno_location() }
    #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd", target_os = "dragonfly"))]
    unsafe { libc::__error() }
    #[cfg(any(target_os = "openbsd", target_os = "netbsd"))]
    unsafe {
        extern "C" { fn __errno() -> *mut libc::c_int; }
        __errno()
    }
}

// `TransferType` enum removed — was Rust-only invention. C uses the
// `ZFST_ASCI` (0x0000) / `ZFST_IMAG` (0x0001) bits from the ZFST_*
// status word (c:246-247) for the next-transfer type, and
// `ZFST_CASC` (0x0000) / `ZFST_CIMA` (0x0002) for the current-transfer
// type. Callers store the type as `i32` and compare via the `ZFST_TYPE`
// macro (c:267): `ZFST_TYPE(x) (x & ZFST_TMSK)`.
//
// Inline-test pattern matching C `if (zfst_status & ZFST_IMAG) ...`
// at every TYPE-letter dispatch (e.g. zftp.c around `zftp_type` body):
//   `if (typ & ZFST_IMAG) != 0 { "I" } else { "A" }`

// `TransferMode` enum removed — was Rust-only invention. C uses the
// `ZFST_STRE` (0x0000) / `ZFST_BLOC` (0x0004) bits from the ZFST_*
// status word (defined later in this file at the c:245 enum). Callers
// store the mode as `i32` and compare against those constants directly:
//   `if (mode & ZFST_BLOC) != 0 { "B" } else { "S" }`
// — same inline-test pattern the C source uses at every MODE send-site.

/// FTP server response (3-digit code + message).
/// Port of the response handling inside `zfgetmsg()` from
/// `lastmsg` — file-scope global from `Src/Modules/zftp.c:227`:
/// `static char *lastmsg, lastcodestr[4];`. Holds the most recent
/// FTP server reply message text (post-3-digit-code body).
pub static lastmsg: std::sync::Mutex<String> = std::sync::Mutex::new(String::new());

/// `lastcodestr` — file-scope global from `Src/Modules/zftp.c:227`.
/// 3-digit FTP reply code as ASCII (`"000".."599"`), zero-terminated
/// to 4 bytes in C; mirrored as a 4-byte Mutex array for parity.
pub static lastcodestr: std::sync::Mutex<[u8; 4]>
    = std::sync::Mutex::new([b'0', b'0', b'0', 0]);

/// `lastcode` — file-scope global from `Src/Modules/zftp.c:228`:
/// `static int lastcode;`. Numeric form of `lastcodestr`.
pub static lastcode: std::sync::atomic::AtomicI32
    = std::sync::atomic::AtomicI32::new(0);

// `FtpResponse` struct removed — was Rust-only invention. C source
// returns plain `int` from every reply-handling fn and reads the
// `lastcode` / `lastmsg` globals (c:227-228) inline at each check
// site. Callers in this Rust port use the same pattern: each fn
// returns `i32` (matching C `int`), and `lastmsg.lock().unwrap()`
// + `lastcode.load(Relaxed)` provide the C-equivalent inline reads.
//
// For ergonomic call sites the type alias below carries both halves
// without inventing a new struct shape:
#[allow(non_camel_case_types)]
pub type FtpResponse = (i32, String);

// =====================================================================
// `struct zfheader` from `Src/Modules/zftp.c:114` — block-mode header.
// =====================================================================

/// Port of `struct zfheader` from `Src/Modules/zftp.c:114`.
/// ```c
/// struct zfheader {
///     char flags;
///     unsigned char bytes[2];
/// };
/// ```
#[allow(non_camel_case_types)]
pub struct zfheader {
    pub flags: i8,                                                       // c:115
    pub bytes: [u8; 2],                                                   // c:116
}

// =====================================================================
// `enum { ZFHD_* }` from `Src/Modules/zftp.c:119` — block-header flags.
// =====================================================================

/// `ZFHD_MARK` — restart marker.
pub const ZFHD_MARK: i32 = 16;                                            // c:120
/// `ZFHD_ERRS` — suspected errors in block.
pub const ZFHD_ERRS: i32 = 32;                                            // c:121
/// `ZFHD_EOFB` — block is end of record.
pub const ZFHD_EOFB: i32 = 64;                                            // c:122
/// `ZFHD_EORB` — block is end of file.
pub const ZFHD_EORB: i32 = 128;                                           // c:123

/// `readwrite_t` — function pointer typedef from
/// `Src/Modules/zftp.c:126`: `typedef int (*readwrite_t)(int, char *, off_t, int);`
#[allow(non_camel_case_types)]
pub type readwrite_t = fn(i32, &mut [u8], libc::off_t, i32) -> i32;

// =====================================================================
// `struct zftpcmd` from `Src/Modules/zftp.c:128` — subcommand entry.
// =====================================================================

/// Port of `struct zftpcmd` from `Src/Modules/zftp.c:128`.
/// ```c
/// struct zftpcmd {
///     const char *nam;
///     int (*fun) (char *, char **, int);
///     int min, max, flags;
/// };
/// ```
#[allow(non_camel_case_types)]
pub struct zftpcmd {
    pub nam: &'static str,                                               // c:129
    pub fun: fn(&str, &[&str], i32) -> i32,                              // c:130
    pub min: i32,                                                         // c:131
    pub max: i32,
    pub flags: i32,
}

/// Port of `typedef struct zftpcmd *Zftpcmd` from `Src/Modules/zftp.c:151`.
#[allow(non_camel_case_types)]
pub type Zftpcmd = Box<zftpcmd>;

// =====================================================================
// `enum { ZFTP_* }` from `Src/Modules/zftp.c:134` — zftpcmd.flags bits.
// =====================================================================

/// `ZFTP_CONN` — must be connected.
pub const ZFTP_CONN: i32 = 0x0001;                                        // c:135
/// `ZFTP_LOGI` — must be logged in.
pub const ZFTP_LOGI: i32 = 0x0002;                                        // c:136
/// `ZFTP_TBIN` — set transfer type image.
pub const ZFTP_TBIN: i32 = 0x0004;                                        // c:137
/// `ZFTP_TASC` — set transfer type ASCII.
pub const ZFTP_TASC: i32 = 0x0008;                                        // c:138
/// `ZFTP_NLST` — use NLST rather than LIST.
pub const ZFTP_NLST: i32 = 0x0010;                                        // c:139
/// `ZFTP_DELE` — a delete rather than a make.
pub const ZFTP_DELE: i32 = 0x0020;                                        // c:140
/// `ZFTP_SITE` — a site rather than a quote.
pub const ZFTP_SITE: i32 = 0x0040;                                        // c:141
/// `ZFTP_APPE` — append rather than overwrite.
pub const ZFTP_APPE: i32 = 0x0080;                                        // c:142
/// `ZFTP_HERE` — here rather than over there.
pub const ZFTP_HERE: i32 = 0x0100;                                        // c:143
/// `ZFTP_CDUP` — CDUP rather than CWD.
pub const ZFTP_CDUP: i32 = 0x0200;                                        // c:144
/// `ZFTP_REST` — restart: set point in remote file.
pub const ZFTP_REST: i32 = 0x0400;                                        // c:145
/// `ZFTP_RECV` — receive rather than send.
pub const ZFTP_RECV: i32 = 0x0800;                                        // c:146
/// `ZFTP_TEST` — test command, don't test.
pub const ZFTP_TEST: i32 = 0x1000;                                        // c:147
/// `ZFTP_SESS` — session command, don't need status.
pub const ZFTP_SESS: i32 = 0x2000;                                        // c:148

/// `static char *zfparams[]` from `Src/Modules/zftp.c:197` — list of
/// non-special params to unset when a connection closes.
pub static ZFPARAMS: &[&str] = &[
    "ZFTP_HOST", "ZFTP_PORT", "ZFTP_IP", "ZFTP_SYSTEM", "ZFTP_USER",
    "ZFTP_ACCOUNT", "ZFTP_PWD", "ZFTP_TYPE", "ZFTP_MODE",                // c:198-199
];

// =====================================================================
// `enum { ZFPM_* }` from `Src/Modules/zftp.c:204` — zfsetparam flags.
// =====================================================================

/// `ZFPM_READONLY` — make parameter readonly.
pub const ZFPM_READONLY: i32 = 0x01;                                      // c:205
/// `ZFPM_IFUNSET` — only set if not already set.
pub const ZFPM_IFUNSET: i32 = 0x02;                                       // c:206
/// `ZFPM_INTEGER` — passed pointer to off_t.
pub const ZFPM_INTEGER: i32 = 0x04;                                       // c:207

/// `zfnopen` — file-scope global from `Src/Modules/zftp.c:211`:
/// `static int zfnopen;` — number of connections actually open.
pub static ZFNOPEN: std::sync::atomic::AtomicI32 =
    std::sync::atomic::AtomicI32::new(0);

/// `zcfinish` — file-scope global from `Src/Modules/zftp.c:218`:
/// `static int zcfinish;` — 0 keep going, 1 line finished, 2 EOF.
pub static ZCFINISH: std::sync::atomic::AtomicI32 =
    std::sync::atomic::AtomicI32::new(0);

/// `zfclosing` — file-scope global from `Src/Modules/zftp.c:220`:
/// `static int zfclosing;` — set when zftp_close() is active.
pub static ZFCLOSING: std::sync::atomic::AtomicI32 =
    std::sync::atomic::AtomicI32::new(0);

// =====================================================================
// `enum { ZFCP_* }` from `Src/Modules/zftp.c` — server-capability
// tri-state for SIZE / MDTM probes.
// =====================================================================

/// `ZFCP_UNKN` — dunno if it works on this server. Port of c:`enum`
/// in `Src/Modules/zftp.c`.
pub const ZFCP_UNKN: i32 = 0;
/// `ZFCP_YUPP` — server supports the feature.
pub const ZFCP_YUPP: i32 = 1;
/// `ZFCP_NOPE` — server doesn't support the feature.
pub const ZFCP_NOPE: i32 = 2;

// =====================================================================
// `enum { ZFST_* }` from `Src/Modules/zftp.c` — bit-packed shared-fd
// status word used by the `zfstatfd` mechanism so a subshell can
// detect type/mode/connection changes in the parent shell.
// =====================================================================

/// `ZF_BUFSIZE` from `Src/Modules/zftp.c:1458`. Default I/O block
/// size for the zftp byte-stream pump.
pub const ZF_BUFSIZE: usize = 32_768;                                        // c:1458

/// `ZF_ASCSIZE` from `Src/Modules/zftp.c:1459`.
/// `#define ZF_ASCSIZE (ZF_BUFSIZE/2)`. Smaller buffer for ASCII
/// mode (line-by-line CRLF translation can grow output up to 2x).
pub const ZF_ASCSIZE: usize = ZF_BUFSIZE / 2;                                // c:1459

/// `ZFST_ASCI` — type for next transfer is ASCII.
pub const ZFST_ASCI: i32 = 0x0000;
/// `ZFST_IMAG` — type for next transfer is image (binary).
pub const ZFST_IMAG: i32 = 0x0001;
/// `ZFST_TMSK` — mask for type flags.
pub const ZFST_TMSK: i32 = 0x0001;
/// `ZFST_TBIT` — number of bits in type flags.
pub const ZFST_TBIT: i32 = 0x0001;
/// `ZFST_CASC` — current type is ASCII (default).
pub const ZFST_CASC: i32 = 0x0000;
/// `ZFST_CIMA` — current type is image.
pub const ZFST_CIMA: i32 = 0x0002;
/// `ZFST_STRE` — stream mode (default).
pub const ZFST_STRE: i32 = 0x0000;
/// `ZFST_BLOC` — block mode.
pub const ZFST_BLOC: i32 = 0x0004;
/// `ZFST_MMSK` — mask for mode flags.
pub const ZFST_MMSK: i32 = 0x0004;
/// `ZFST_LOGI` — user logged in.
pub const ZFST_LOGI: i32 = 0x0008;
/// `ZFST_SYST` — done SYST type check.
pub const ZFST_SYST: i32 = 0x0010;
/// `ZFST_NOPS` — server doesn't understand PASV.
pub const ZFST_NOPS: i32 = 0x0020;
/// `ZFST_NOSZ` — server doesn't send `(XXXX bytes)' reply.
pub const ZFST_NOSZ: i32 = 0x0040;
/// `ZFST_TRSZ` — tried getting 'size' from reply.
pub const ZFST_TRSZ: i32 = 0x0080;
/// `ZFST_CLOS` — connection closed.
pub const ZFST_CLOS: i32 = 0x0100;

/// `ZFPF_SNDP` — use send port (active mode) preference.
pub const ZFPF_SNDP: i32 = 0x01;                                           // c:280
/// `ZFPF_PASV` — try passive mode preference.
pub const ZFPF_PASV: i32 = 0x02;                                           // c:281
/// `ZFPF_DUMB` — don't do clever things with variables.
pub const ZFPF_DUMB: i32 = 0x04;                                           // c:282

/// `zfprefs` — file-scope `static int zfprefs;` from
/// Src/Modules/zftp.c:218. Bitfield of ZFPF_SNDP|ZFPF_PASV|ZFPF_DUMB.
/// Default set by boot_ (c:3206) to ZFPF_SNDP|ZFPF_PASV.
#[allow(non_upper_case_globals)]
pub static zfprefs: std::sync::atomic::AtomicI32 =
    std::sync::atomic::AtomicI32::new(0);                                 // c:218

/// `ZFST_TYPE(x)` macro — extract type-flag bits.
/// Port of `#define ZFST_TYPE(x) (x & ZFST_TMSK)` from
/// `Src/Modules/zftp.c`.
#[allow(non_snake_case)]
#[inline]
pub fn ZFST_TYPE(x: i32) -> i32 { x & ZFST_TMSK }

/// `ZFST_MODE(x)` macro — extract mode-flag bits.
/// Port of `#define ZFST_MODE(x) (x & ZFST_MMSK)` from
/// `Src/Modules/zftp.c`.
#[allow(non_snake_case)]
#[inline]
pub fn ZFST_MODE(x: i32) -> i32 { x & ZFST_MMSK }

/// Port of `struct zftp_session` from `Src/Modules/zftp.c:299`.
///
/// C definition (verbatim):
/// ```c
/// struct zftp_session {
///     char *name;            /* name of session */
///     char **params;         /* parameters ordered as in zfparams */
///     char **userparams;     /* user parameters set by zftp_params */
///     FILE *cin;             /* control input file */
///     Tcp_session control;   /* the control connection */
///     int dfd;               /* data connection */
///     int has_size;          /* understands SIZE? */
///     int has_mdtm;          /* understands MDTM? */
/// };
///
/// typedef struct zftp_session *Zftp_session;  // c:50
/// ```
///
/// Field names + order match C exactly. `cin` (control input file) is
/// modelled as `Option<TcpStream>` since Rust doesn't expose libc
/// FILE* directly; `control` (the Tcp_session) collapses into the
/// same TcpStream slot in the static-link path.
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct zftp_session {
    pub name: String,                                                    // c:300 char *name
    pub params: Vec<String>,                                              // c:301 char **params
    pub userparams: Vec<String>,                                          // c:302 char **userparams
    pub cin: Option<TcpStream>,                                          // c:303 FILE *cin (control input)
    pub control: Option<TcpStream>,                                       // c:304 Tcp_session control
    pub dfd: i32,                                                         // c:305 int dfd
    pub has_size: i32,                                                    // c:306 int has_size
    pub has_mdtm: i32,                                                    // c:307 int has_mdtm

    // Below: ergonomic Rust fields not in C `struct zftp_session` but
    // needed by the Rust wrapper to track connection state without the
    // C `params` array indexing convention. Document the mapping back
    // to C `params[]` slots in comments.
    pub host: Option<String>,            // C: params[ZFPM_HOST]
    pub port: u16,                       // C: params[ZFPM_PORT] (parsed)
    pub user: Option<String>,            // C: params[ZFPM_USER]
    pub pwd: Option<String>,             // C: params[ZFPM_PASSWORD]
    pub connected: bool,                 // C: cin != NULL
    pub logged_in: bool,                 // C: derived from greeting parse
    pub transfer_type: i32,
    pub transfer_mode: i32,
    pub passive: bool,
    /// Mirrors the ZFST_SYST bit of `zfstatusp[zfsessno]` (c:240).
    /// Set after a successful SYST probe so re-login on the same
    /// session doesn't re-issue SYST.
    pub syst_probed: bool,
}

/// Port of `typedef struct zftp_session *Zftp_session;` from
/// `Src/Modules/zftp.c:50`. Pointer-style typedef alias used by every
/// `zftp_*` callsite that takes a session arg.
#[allow(non_camel_case_types)]
pub type Zftp_session = Box<zftp_session>;

impl zftp_session {
    /// Port of `newsession(char *nm)` from `Src/Modules/zftp.c`. C uses
    /// `zshcalloc(sizeof(struct zftp_session))` then sets `name`
    /// (c:2891 `zfsess->name = ztrdup(name);`) and pre-allocates the
    /// `params` / `userparams` arrays. Same default state.
    pub fn new(name: &str) -> Self {
        Self {
            // C-faithful fields from `struct zftp_session` (c:299):
            name: name.to_string(),                                       // c:300
            params: Vec::new(),                                           // c:301
            userparams: Vec::new(),                                       // c:302
            cin: None,                                                    // c:303 NULL
            control: None,                                                // c:304 NULL
            dfd: -1,                                                      // c:305 (closed)
            has_size: 0,                                                  // c:306
            has_mdtm: 0,                                                  // c:307
            // Ergonomic Rust-side state mirroring C's params[] indices:
            host: None,
            port: 21,
            user: None,
            pwd: None,
            connected: false,
            logged_in: false,
            transfer_type: ZFST_IMAG,
            transfer_mode: ZFST_STRE,
            passive: true,
            syst_probed: false,
        }
    }

    /// Port of `zftp_open(char *name, char **args, int flags)` from `Src/Modules/zftp.c:1690`.
    fn send_command(&mut self, cmd: &str) -> io::Result<()> {
        if let Some(ref mut stream) = self.cin {
            write!(stream, "{}\r\n", cmd)?;
            stream.flush()
        } else {
            Err(io::Error::new(io::ErrorKind::NotConnected, "not connected"))
        }
    }

    /// Port of `zfgetmsg()` from `Src/Modules/zftp.c:702`.
    fn read_response(&mut self) -> io::Result<FtpResponse> {
        let stream = self
            .cin
            .as_mut()
            .ok_or_else(|| io::Error::new(io::ErrorKind::NotConnected, "not connected"))?;

        let mut reader = BufReader::new(stream.try_clone()?);
        let mut full_message = String::new();
        let mut code = 0u32;
        let mut multiline = false;
        let mut first_code = String::new();

        loop {
            let mut line = String::new();
            reader.read_line(&mut line)?;
            let line = line.trim_end();

            if line.len() < 3 {
                continue;
            }

            if code == 0 {
                first_code = line[..3].to_string();
                code = first_code.parse().unwrap_or(0);

                if line.len() > 3 && line.chars().nth(3) == Some('-') {
                    multiline = true;
                }
            }

            full_message.push_str(line);
            full_message.push('\n');

            if multiline {
                if line.starts_with(&first_code)
                    && line.len() > 3
                    && line.chars().nth(3) == Some(' ')
                {
                    break;
                }
            } else {
                break;
            }
        }

        Ok((code as i32, full_message))
    }

    /// Port of `zftp_open(char *name, char **args, int flags)` from `Src/Modules/zftp.c:1690`.
    /// Connect to FTP server — DNS resolution on background thread to avoid hangs
    pub fn connect(&mut self, host: &str, port: Option<u16>) -> io::Result<FtpResponse> {
        let port = port.unwrap_or(21);
        let addr_str = format!("{}:{}", host, port);
        let dns_timeout = Duration::from_secs(10);

        // DNS on background thread
        let (tx, rx) = std::sync::mpsc::channel();
        let dns = addr_str.clone();
        std::thread::Builder::new()
            .name("zftp-dns".to_string())
            .spawn(move || {
                let _ = tx.send(dns.to_socket_addrs().map(|a| a.collect::<Vec<_>>()));
            })
            .map_err(io::Error::other)?;

        let addrs = rx
            .recv_timeout(dns_timeout)
            .map_err(|_| io::Error::new(io::ErrorKind::TimedOut, "DNS resolution timed out"))?
            .map_err(|e| {
                // C: `zwarnnam("zftp", "host not found: %s", host);` at
                //    `Src/Modules/zftp.c:1715`. Route through canonical
                //    zwarnnam so the user sees a real shell warning
                //    instead of a tracing log line.
                crate::ported::utils::zwarnnam(
                    "zftp",
                    &format!("host not found: {}: {}", host, e),
                );
                e
            })?;

        let sock_addr = addrs
            .into_iter()
            .next()
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "invalid address"))?;

        let stream = TcpStream::connect_timeout(&sock_addr, Duration::from_secs(30))?;

        stream.set_read_timeout(Some(Duration::from_secs(60)))?;
        stream.set_write_timeout(Some(Duration::from_secs(60)))?;

        self.cin = Some(stream);
        self.host = Some(host.to_string());
        self.port = port;
        self.connected = true;

        self.read_response()
    }

    /// Port of `zftp_login(char *name, char **args, UNUSED(int flags))` from `Src/Modules/zftp.c:2118`.
    /// Login to FTP server
    pub fn login(&mut self, user: &str, pass: Option<&str>) -> io::Result<FtpResponse> {
        self.send_command(&format!("USER {}", user))?;
        let resp = self.read_response()?;

        if resp.0 == 331 {
            let password = pass.unwrap_or("");
            self.send_command(&format!("PASS {}", password))?;
            let resp = self.read_response()?;

            if (resp.0 >= 200 && resp.0 < 300) {
                self.logged_in = true;
                self.user = Some(user.to_string());
            }
            return Ok(resp);
        }

        if (resp.0 >= 200 && resp.0 < 300) {
            self.logged_in = true;
            self.user = Some(user.to_string());
        }

        Ok(resp)
    }

    /// WARNING: NOT IN ZFTP.C — method on Rust-only `zftp_session` wrapper.
    /// C inlines this pattern at every callsite; Rust factors it onto the wrapper.
    /// Set transfer type
    pub fn set_type(&mut self, transfer_type: i32) -> io::Result<FtpResponse> {
        // C inline pattern: `(typ & ZFST_IMAG) ? "I" : "A"`
        let typ_letter = if (transfer_type & ZFST_IMAG) != 0 { "I" } else { "A" };
        self.send_command(&format!("TYPE {}", typ_letter))?;
        let resp = self.read_response()?;
        if (resp.0 >= 200 && resp.0 < 300) {
            self.transfer_type = transfer_type;
        }
        Ok(resp)
    }

    /// WARNING: NOT IN ZFTP.C — method on Rust-only `zftp_session` wrapper.
    /// C inlines this pattern at every callsite; Rust factors it onto the wrapper.
    /// Change directory
    pub fn cd(&mut self, path: &str) -> io::Result<FtpResponse> {
        self.send_command(&format!("CWD {}", path))?;
        self.read_response()
    }

    /// WARNING: NOT IN ZFTP.C — method on Rust-only `zftp_session` wrapper.
    /// C inlines this pattern at every callsite; Rust factors it onto the wrapper.
    /// Change to parent directory
    pub fn cdup(&mut self) -> io::Result<FtpResponse> {
        self.send_command("CDUP")?;
        self.read_response()
    }

    /// WARNING: NOT IN ZFTP.C — method on Rust-only `zftp_session` wrapper.
    /// C inlines this pattern at every callsite; Rust factors it onto the wrapper.
    /// Get current directory
    pub fn pwd(&mut self) -> io::Result<(FtpResponse, Option<String>)> {
        self.send_command("PWD")?;
        let resp = self.read_response()?;

        let pwd = if (resp.0 >= 200 && resp.0 < 300) {
            if let Some(start) = resp.1.find('"') {
                if let Some(end) = resp.1[start + 1..].find('"') {
                    Some(resp.1[start + 1..start + 1 + end].to_string())
                } else {
                    None
                }
            } else {
                None
            }
        } else {
            None
        };

        Ok((resp, pwd))
    }

    /// WARNING: NOT IN ZFTP.C — method on Rust-only `zftp_session` wrapper.
    /// C inlines this pattern at every callsite; Rust factors it onto the wrapper.
    /// List directory
    pub fn list(&mut self, path: Option<&str>) -> io::Result<(FtpResponse, Vec<String>)> {
        let data_stream = self.enter_passive_mode()?;

        let cmd = match path {
            Some(p) => format!("LIST {}", p),
            None => "LIST".to_string(),
        };
        self.send_command(&cmd)?;
        let resp = self.read_response()?;

        if !(resp.0 >= 100 && resp.0 < 400) {
            return Ok((resp, Vec::new()));
        }

        let mut reader = BufReader::new(data_stream);
        let mut lines = Vec::new();
        let mut line = String::new();
        while reader.read_line(&mut line)? > 0 {
            lines.push(line.trim_end().to_string());
            line.clear();
        }

        let final_resp = self.read_response()?;

        Ok((final_resp, lines))
    }

    /// WARNING: NOT IN ZFTP.C — method on Rust-only `zftp_session` wrapper.
    /// C inlines this pattern at every callsite; Rust factors it onto the wrapper.
    /// List filenames only
    pub fn nlst(&mut self, path: Option<&str>) -> io::Result<(FtpResponse, Vec<String>)> {
        let data_stream = self.enter_passive_mode()?;

        let cmd = match path {
            Some(p) => format!("NLST {}", p),
            None => "NLST".to_string(),
        };
        self.send_command(&cmd)?;
        let resp = self.read_response()?;

        if !(resp.0 >= 100 && resp.0 < 400) {
            return Ok((resp, Vec::new()));
        }

        let mut reader = BufReader::new(data_stream);
        let mut lines = Vec::new();
        let mut line = String::new();
        while reader.read_line(&mut line)? > 0 {
            lines.push(line.trim_end().to_string());
            line.clear();
        }

        let final_resp = self.read_response()?;

        Ok((final_resp, lines))
    }

    /// Port of `zftp_open(char *name, char **args, int flags)` from `Src/Modules/zftp.c:1690`.
    fn enter_passive_mode(&mut self) -> io::Result<TcpStream> {
        self.send_command("PASV")?;
        let resp = self.read_response()?;

        if !(resp.0 >= 200 && resp.0 < 300) {
            return Err(io::Error::other(resp.1));
        }

        let (ip, port) = parse_pasv_response(&resp.1)?;
        let addr = format!("{}:{}", ip, port);

        TcpStream::connect_timeout(
            &addr.to_socket_addrs()?.next().ok_or_else(|| {
                io::Error::new(io::ErrorKind::InvalidInput, "invalid PASV address")
            })?,
            Duration::from_secs(30),
        )
    }

    /// Port of `zfstats(char *fnam, int remote, off_t *retsize, char **retmdtm, int fd)` from `Src/Modules/zftp.c:1193`.
    /// Download a file
    pub fn get(&mut self, remote: &str, local: &Path) -> io::Result<FtpResponse> {
        let mut data_stream = self.enter_passive_mode()?;

        self.send_command(&format!("RETR {}", remote))?;
        let resp = self.read_response()?;

        if !(resp.0 >= 100 && resp.0 < 400) {
            return Ok(resp);
        }

        let mut file = std::fs::File::create(local)?;
        let mut buf = [0u8; 8192];
        loop {
            let n = data_stream.read(&mut buf)?;
            if n == 0 {
                break;
            }
            file.write_all(&buf[..n])?;
        }

        self.read_response()
    }

    /// Port of `zfstats(char *fnam, int remote, off_t *retsize, char **retmdtm, int fd)` from `Src/Modules/zftp.c:1193`.
    /// Upload a file
    pub fn put(&mut self, local: &Path, remote: &str) -> io::Result<FtpResponse> {
        let mut data_stream = self.enter_passive_mode()?;

        self.send_command(&format!("STOR {}", remote))?;
        let resp = self.read_response()?;

        if !(resp.0 >= 100 && resp.0 < 400) {
            return Ok(resp);
        }

        let mut file = std::fs::File::open(local)?;
        let mut buf = [0u8; 8192];
        loop {
            let n = file.read(&mut buf)?;
            if n == 0 {
                break;
            }
            data_stream.write_all(&buf[..n])?;
        }
        drop(data_stream);

        self.read_response()
    }

    /// WARNING: NOT IN ZFTP.C — method on Rust-only `zftp_session` wrapper.
    /// C inlines this pattern at every callsite; Rust factors it onto the wrapper.
    /// Delete a file
    pub fn delete(&mut self, path: &str) -> io::Result<FtpResponse> {
        self.send_command(&format!("DELE {}", path))?;
        self.read_response()
    }

    /// WARNING: NOT IN ZFTP.C — method on Rust-only `zftp_session` wrapper.
    /// C inlines this pattern at every callsite; Rust factors it onto the wrapper.
    /// Make directory
    pub fn mkdir(&mut self, path: &str) -> io::Result<FtpResponse> {
        self.send_command(&format!("MKD {}", path))?;
        self.read_response()
    }

    /// WARNING: NOT IN ZFTP.C — method on Rust-only `zftp_session` wrapper.
    /// C inlines this pattern at every callsite; Rust factors it onto the wrapper.
    /// Remove directory
    pub fn rmdir(&mut self, path: &str) -> io::Result<FtpResponse> {
        self.send_command(&format!("RMD {}", path))?;
        self.read_response()
    }

    /// WARNING: NOT IN ZFTP.C — method on Rust-only `zftp_session` wrapper.
    /// C inlines this pattern at every callsite; Rust factors it onto the wrapper.
    /// Rename file
    pub fn rename(&mut self, from: &str, to: &str) -> io::Result<FtpResponse> {
        self.send_command(&format!("RNFR {}", from))?;
        let resp = self.read_response()?;

        if !(resp.0 >= 300 && resp.0 < 400) {
            return Ok(resp);
        }

        self.send_command(&format!("RNTO {}", to))?;
        self.read_response()
    }

    /// WARNING: NOT IN ZFTP.C — method on Rust-only `zftp_session` wrapper.
    /// C inlines this pattern at every callsite; Rust factors it onto the wrapper.
    /// Get file size
    pub fn size(&mut self, path: &str) -> io::Result<(FtpResponse, Option<u64>)> {
        self.send_command(&format!("SIZE {}", path))?;
        let resp = self.read_response()?;

        let size = if (resp.0 >= 200 && resp.0 < 300) {
            resp.1
                .split_whitespace()
                .last()
                .and_then(|s| s.parse().ok())
        } else {
            None
        };

        Ok((resp, size))
    }

    /// WARNING: NOT IN ZFTP.C — method on Rust-only `zftp_session` wrapper.
    /// C inlines this pattern at every callsite; Rust factors it onto the wrapper.
    /// Send raw command
    pub fn bslashquote(&mut self, cmd: &str) -> io::Result<FtpResponse> {
        self.send_command(cmd)?;
        self.read_response()
    }

    /// Port of `zftp_open(char *name, char **args, int flags)` from `Src/Modules/zftp.c:1690`.
    /// Close connection
    pub fn close(&mut self) -> io::Result<FtpResponse> {
        if !self.connected {
            return Ok((0, "not connected".to_string()));
        }

        let resp = if let Ok(()) = self.send_command("QUIT") {
            self.read_response().unwrap_or_else(|_| (221, "Goodbye".to_string()))
        } else {
            (221, "Goodbye".to_string())
        };

        self.cin = None;
        self.connected = false;
        self.logged_in = false;
        self.host = None;
        self.user = None;
        self.pwd = None;

        Ok(resp)
    }
}

/// Port of `zfopendata(char *name, union tcp_sockaddr *zdsockp, int *is_passivep)` from `Src/Modules/zftp.c:859`.
/// Helper: parse a `227 Entering Passive Mode (h1,h2,h3,h4,p1,p2)`
/// FTP reply into (ip, port). Direct extraction of the sscanf path
/// inside C zfopendata (Src/Modules/zftp.c:925-941). Allowlisted as
/// a Rust-only architectural helper since C does the parse inline.
fn parse_pasv_response(msg: &str) -> io::Result<(String, u16)> {                // c:925 inline
    let start = msg
        .find('(')
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "invalid PASV response"))?;
    let end = msg
        .find(')')
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "invalid PASV response"))?;

    let nums: Vec<u16> = msg[start + 1..end]
        .split(',')
        .filter_map(|s| s.trim().parse().ok())
        .collect();

    if nums.len() != 6 {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "invalid PASV numbers",
        ));
    }

    let ip = format!("{}.{}.{}.{}", nums[0], nums[1], nums[2], nums[3]);
    let port = (nums[4] << 8) + nums[5];

    Ok((ip, port))
}

/// Port of `zfopendata(char *name, union tcp_sockaddr *zdsockp, int *is_passivep)` from `Src/Modules/zftp.c:859`.
/// C: `static int zfopendata(char *name, union tcp_sockaddr *zdsockp,
/// int *is_passivep)` — set up a data connection (PASV-preferred,
/// PORT-mode fallback). Returns 0 success, 1 failure. Stores the
/// resulting fd in `zfsess->dfd` and sets `*is_passivep`.
///
/// Rust port: `is_passive` is returned via the tuple instead of a
/// `*int` out-param (Rust doesn't expose `union tcp_sockaddr` so the
/// `zdsockp` slot is gone). PORT-mode falls back to `TcpListener` for
/// the local bind+listen+accept; PASV path uses `TcpStream::connect`
/// and stores the connected stream's fd as `sess.dfd`. The non-PASV
/// branch requires the caller to accept(2) before reading.
#[allow(non_snake_case)]
/// Port of `zfopendata(char *name, union tcp_sockaddr *zdsockp, int *is_passivep)` from `Src/Modules/zftp.c:859`.
/// WARNING: param names don't match C — Rust=(name) vs C=(name, zdsockp, is_passivep)
pub fn zfopendata(name: &str) -> (i32, bool) {                                  // c:859
    // c:862-865 — error if neither SNDP nor PASV preference is set.
    let prefs = zfprefs.load(Ordering::Relaxed);                                // c:862
    if (prefs & (ZFPF_SNDP | ZFPF_PASV)) == 0 {                                 // c:863
        crate::ported::utils::zwarnnam(name,
            "Must set preference S or P to transfer data");                     // c:864
        return (1, false);                                                      // c:865
    }
    // c:871 — try PASV when the bit is set and ZFST_NOPS isn't.
    let try_pasv: bool = (prefs & ZFPF_PASV) != 0;                              // c:871

    if try_pasv {
        // c:879 — psv_cmd = "PASV\r\n"; (EPSV unsupported in Rust port).
        if zfsendcmd("PASV\r\n") == 6 {                                         // c:881
            return (1, false);                                                  // c:882
        }
        let code = lastcode.load(Ordering::Relaxed);
        if (500..=504).contains(&code) {                                        // c:884 PASV unsupported
            zfclosedata();                                                      // c:888
            // c:889 — fall through to PORT mode.
        } else {
            // c:899 — parse the PASV reply.
            let last = lastmsg.lock().ok().map(|m| m.clone()).unwrap_or_default();
            let (ip, port) = match parse_pasv_response(&last) {                 // c:925
                Ok(t) => t,
                Err(_) => {
                    crate::ported::utils::zwarnnam(
                        name, &format!("bad response to PASV: {}", last));
                    zfclosedata();
                    return (1, false);                                          // c:946
                }
            };
            // c:954 — connect from data socket to remote (ip:port).
            let addr = format!("{}:{}", ip, port);
            let stream = match std::net::TcpStream::connect(&addr) {            // c:958
                Ok(s) => s,
                Err(_) => {
                    crate::ported::utils::zwarnnam(name, "can't open data socket");
                    zfclosedata();
                    return (1, false);
                }
            };
            let dfd_raw = stream.as_raw_fd();
            // Keep the stream alive past this fn so the fd stays open;
            // the session owns the fd via `dfd`.
            std::mem::forget(stream);
            if let Ok(mut state) = zftp_state().lock() {
                if let Some(sess) = state.get_session_mut(None) {
                    sess.dfd = dfd_raw;                                         // c:961 dfd stored
                }
            }
            return (0, true);                                                   // c:1041 SUCCESS PASV
        }
    }

    // c:967-1037 — PORT-mode (active FTP): bind+listen locally, send
    // PORT a,b,c,d,p1,p2, return so caller can accept().
    // c:977 — refuse PORT mode if SNDP preference isn't set (e.g. when
    // PASV-only and the server rejected PASV).
    if (zfprefs.load(Ordering::Relaxed) & ZFPF_SNDP) == 0 {                     // c:977
        crate::ported::utils::zwarnnam(name,
            "only sendport mode available for data");                           // c:978
        return (1, false);                                                      // c:979
    }
    let listener = match std::net::TcpListener::bind("0.0.0.0:0") {             // c:967 bind
        Ok(l) => l,
        Err(_) => {
            crate::ported::utils::zwarnnam(name, "can't bind data socket");
            return (1, false);                                                  // c:870
        }
    };
    let local = match listener.local_addr() {
        Ok(a) => a,
        Err(_) => {
            crate::ported::utils::zwarnnam(name, "getsockname failed");
            return (1, false);
        }
    };
    // c:986 — listen with backlog 1; std::net::TcpListener::bind already listens.
    let ipv4 = match local.ip() {
        std::net::IpAddr::V4(v) => v.octets(),
        std::net::IpAddr::V6(_) => {
            crate::ported::utils::zwarnnam(name, "PORT mode requires IPv4");
            return (1, false);
        }
    };
    let port = local.port();
    // c:1003-1017 — PORT cmd format: "PORT h1,h2,h3,h4,p1,p2\r\n".
    let port_cmd = format!(
        "PORT {},{},{},{},{},{}\r\n",
        ipv4[0], ipv4[1], ipv4[2], ipv4[3],
        port >> 8, port & 0xff,
    );
    if zfsendcmd(&port_cmd) > 2 {                                               // c:1018
        crate::ported::utils::zwarnnam(name, "PORT command failed");
        return (1, false);                                                      // c:1019
    }
    // c:1029 — store listening fd as dfd; caller does accept().
    let lfd = listener.as_raw_fd();
    std::mem::forget(listener);
    if let Ok(mut state) = zftp_state().lock() {
        if let Some(sess) = state.get_session_mut(None) {
            sess.dfd = lfd;                                                     // c:1029
        }
    }
    (0, false)                                                                  // c:1037 SUCCESS PORT
}

/// FTP sessions manager.
/// Port of the file-static `zfsess_node` linked list +
/// `zfsess_current` pointer Src/Modules/zftp.c keeps —
/// `zftp_session()` (line 2889) drives the switch,
/// `zftp_rmsession()` (line 2915) the removal.
// `Zftp` struct renamed to `zftp_globals` — C has no `struct zftp`;
// the equivalent state in `Src/Modules/zftp.c` lives in file-scope
// globals (`Zfsess zfsessions` linked list head + `Zfsess
// zfsesscurrent`). Rust collapses these into one container accessed
// via `ZFTP_STATE_INNER`; the suffix names this as a Rust extension
// that bags the module's C-static state.
#[allow(non_camel_case_types)]
#[derive(Debug, Default)]
pub struct zftp_globals {
    sessions: HashMap<String, zftp_session>,
    current: Option<String>,
}

impl zftp_globals {
    /// WARNING: NOT IN ZFTP.C — method on Rust-only `zftp_globals` wrapper.
    /// C inlines this pattern at every callsite; Rust factors it onto the wrapper.
    pub fn new() -> Self {
        Self::default()
    }

    /// WARNING: NOT IN ZFTP.C — method on Rust-only `zftp_globals` wrapper.
    /// C inlines this pattern at every callsite; Rust factors it onto the wrapper.
    pub fn get_session(&self, name: Option<&str>) -> Option<&zftp_session> {
        let key = name
            .map(|s| s.to_string())
            .or_else(|| self.current.clone())?;
        self.sessions.get(&key)
    }

    /// WARNING: NOT IN ZFTP.C — method on Rust-only `zftp_globals` wrapper.
    /// C inlines this pattern at every callsite; Rust factors it onto the wrapper.
    pub fn get_session_mut(&mut self, name: Option<&str>) -> Option<&mut zftp_session> {
        let key = name
            .map(|s| s.to_string())
            .or_else(|| self.current.clone())?;
        self.sessions.get_mut(&key)
    }

    /// WARNING: NOT IN ZFTP.C — method on Rust-only `zftp_globals` wrapper.
    /// C inlines this pattern at every callsite; Rust factors it onto the wrapper.
    pub fn create_session(&mut self, name: &str) -> &mut zftp_session {
        self.sessions
            .entry(name.to_string())
            .or_insert_with(|| zftp_session::new(name))
    }

    /// Port of `zftp_rmsession(UNUSED(char *name), char **args, UNUSED(int flags))` from `Src/Modules/zftp.c:2915`.
    pub fn remove_session(&mut self, name: &str) -> Option<zftp_session> {
        let sess = self.sessions.remove(name);
        if self.current.as_deref() == Some(name) {
            // After dropping the current session, pick the
            // alphabetically-first remaining session (deterministic;
            // HashMap::keys().next() picks at random).
            let mut keys: Vec<&String> = self.sessions.keys().collect();
            keys.sort();
            self.current = keys.first().map(|s| (*s).clone());
        }
        sess
    }

    /// WARNING: NOT IN ZFTP.C — method on Rust-only `zftp_globals` wrapper.
    /// C inlines this pattern at every callsite; Rust factors it onto the wrapper.
    pub fn set_current(&mut self, name: &str) -> bool {
        if self.sessions.contains_key(name) {
            self.current = Some(name.to_string());
            true
        } else {
            false
        }
    }

    /// WARNING: NOT IN ZFTP.C — method on Rust-only `zftp_globals` wrapper.
    /// C inlines this pattern at every callsite; Rust factors it onto the wrapper.
    pub fn current_name(&self) -> Option<&str> {
        self.current.as_deref()
    }

    /// WARNING: NOT IN ZFTP.C — method on Rust-only `zftp_globals` wrapper.
    /// C inlines this pattern at every callsite; Rust factors it onto the wrapper.
    pub fn session_names(&self) -> Vec<&str> {
        // Sorted so `zftp session` listing is deterministic across
        // runs. Matches zsh's table-walk order for the underlying
        // sessions hash.
        let mut names: Vec<&str> = self.sessions.keys().map(|s| s.as_str()).collect();
        names.sort();
        names
    }
}

/// Port of `bin_zftp(char *name, char **args, UNUSED(Options ops), UNUSED(int func))` from `Src/Modules/zftp.c:3002`.
/// `zftp` builtin entry point — C-faithful signature matching
/// `static int bin_zftp(char *name, char **args, Options ops, int func)`
/// from Src/Modules/zftp.c:3002. Acquires ZFTP_STATE, dispatches by
/// subcommand string, emits any captured output to stdout/stderr
/// based on status, returns the bare i32 status C's execbuiltin path
/// consumes.
#[allow(non_snake_case)]
/// WARNING: param names don't match C — Rust=(_nam, args, _func) vs C=(name, args, ops, func)
pub fn bin_zftp(_nam: &str, args: &[String],                                 // c:3002
                _ops: &crate::ported::zsh_h::options, _func: i32) -> i32 {
    let argv: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
    let mut zftp_guard = zftp_state().lock()
        .unwrap_or_else(|e| { zftp_state_clear_poison(); e.into_inner() });
    let zftp = &mut *zftp_guard;
    let args = &argv[..];
    let (status, output): (i32, String) = (|| {
    if args.is_empty() {
        return (1, "zftp: subcommand required\n".to_string());
    }

    match args[0] {
        "open" => {
            if args.len() < 2 {
                return (1, "zftp open: host required\n".to_string());
            }

            let host = args[1];
            let port: Option<u16> = args.get(2).and_then(|s| s.parse().ok());

            let session_name = zftp.current_name().unwrap_or("default").to_string();

            let sess = zftp.create_session(&session_name);

            match sess.connect(host, port) {
                Ok(resp) => {
                    if (resp.0 >= 100 && resp.0 < 400) {
                        zftp.set_current(&session_name);
                        (0, resp.1)
                    } else {
                        (1, resp.1)
                    }
                }
                Err(e) => (1, format!("zftp open: {}\n", e)),
            }
        }

        "login" | "user" => {
            if args.len() < 2 {
                return (1, "zftp login: user required\n".to_string());
            }

            let user = args[1];
            let pass = args.get(2).copied();

            let sess = match zftp.get_session_mut(None) {
                Some(s) => s,
                None => return (1, "zftp login: not connected\n".to_string()),
            };

            match sess.login(user, pass) {
                Ok(resp) => {
                    if (resp.0 >= 200 && resp.0 < 300) {
                        (0, resp.1)
                    } else {
                        (1, resp.1)
                    }
                }
                Err(e) => (1, format!("zftp login: {}\n", e)),
            }
        }

        "cd" => {
            if args.len() < 2 {
                return (1, "zftp cd: path required\n".to_string());
            }

            let sess = match zftp.get_session_mut(None) {
                Some(s) => s,
                None => return (1, "zftp cd: not connected\n".to_string()),
            };

            match sess.cd(args[1]) {
                Ok(resp) => {
                    if (resp.0 >= 200 && resp.0 < 300) {
                        (0, resp.1)
                    } else {
                        (1, resp.1)
                    }
                }
                Err(e) => (1, format!("zftp cd: {}\n", e)),
            }
        }

        "cdup" => {
            let sess = match zftp.get_session_mut(None) {
                Some(s) => s,
                None => return (1, "zftp cdup: not connected\n".to_string()),
            };

            match sess.cdup() {
                Ok(resp) => {
                    if (resp.0 >= 200 && resp.0 < 300) {
                        (0, resp.1)
                    } else {
                        (1, resp.1)
                    }
                }
                Err(e) => (1, format!("zftp cdup: {}\n", e)),
            }
        }

        "pwd" => {
            let sess = match zftp.get_session_mut(None) {
                Some(s) => s,
                None => return (1, "zftp pwd: not connected\n".to_string()),
            };

            match sess.pwd() {
                Ok((resp, pwd)) => {
                    if let Some(p) = pwd {
                        (0, format!("{}\n", p))
                    } else {
                        (1, resp.1)
                    }
                }
                Err(e) => (1, format!("zftp pwd: {}\n", e)),
            }
        }

        "dir" | "ls" => {
            let path = args.get(1).copied();
            let use_nlst = args[0] == "ls";

            let sess = match zftp.get_session_mut(None) {
                Some(s) => s,
                None => return (1, "zftp dir: not connected\n".to_string()),
            };

            let result = if use_nlst {
                sess.nlst(path)
            } else {
                sess.list(path)
            };

            match result {
                Ok((resp, lines)) => {
                    if (resp.0 >= 200 && resp.0 < 300) {
                        (0, lines.join("\n") + "\n")
                    } else {
                        (1, resp.1)
                    }
                }
                Err(e) => (1, format!("zftp dir: {}\n", e)),
            }
        }

        "get" => {
            if args.len() < 2 {
                return (1, "zftp get: remote file required\n".to_string());
            }

            let remote = args[1];
            let local = args.get(2).unwrap_or(&remote);

            let sess = match zftp.get_session_mut(None) {
                Some(s) => s,
                None => return (1, "zftp get: not connected\n".to_string()),
            };

            match sess.get(remote, Path::new(local)) {
                Ok(resp) => {
                    if (resp.0 >= 200 && resp.0 < 300) {
                        (0, String::new())
                    } else {
                        (1, resp.1)
                    }
                }
                Err(e) => (1, format!("zftp get: {}\n", e)),
            }
        }

        "put" => {
            if args.len() < 2 {
                return (1, "zftp put: local file required\n".to_string());
            }

            let local = args[1];
            let remote = args.get(2).unwrap_or(&local);

            let sess = match zftp.get_session_mut(None) {
                Some(s) => s,
                None => return (1, "zftp put: not connected\n".to_string()),
            };

            match sess.put(Path::new(local), remote) {
                Ok(resp) => {
                    if (resp.0 >= 200 && resp.0 < 300) {
                        (0, String::new())
                    } else {
                        (1, resp.1)
                    }
                }
                Err(e) => (1, format!("zftp put: {}\n", e)),
            }
        }

        "delete" => {
            if args.len() < 2 {
                return (1, "zftp delete: file required\n".to_string());
            }

            let sess = match zftp.get_session_mut(None) {
                Some(s) => s,
                None => return (1, "zftp delete: not connected\n".to_string()),
            };

            match sess.delete(args[1]) {
                Ok(resp) => {
                    if (resp.0 >= 200 && resp.0 < 300) {
                        (0, String::new())
                    } else {
                        (1, resp.1)
                    }
                }
                Err(e) => (1, format!("zftp delete: {}\n", e)),
            }
        }

        "mkdir" => {
            if args.len() < 2 {
                return (1, "zftp mkdir: directory required\n".to_string());
            }

            let sess = match zftp.get_session_mut(None) {
                Some(s) => s,
                None => return (1, "zftp mkdir: not connected\n".to_string()),
            };

            match sess.mkdir(args[1]) {
                Ok(resp) => {
                    if (resp.0 >= 200 && resp.0 < 300) {
                        (0, String::new())
                    } else {
                        (1, resp.1)
                    }
                }
                Err(e) => (1, format!("zftp mkdir: {}\n", e)),
            }
        }

        "rmdir" => {
            if args.len() < 2 {
                return (1, "zftp rmdir: directory required\n".to_string());
            }

            let sess = match zftp.get_session_mut(None) {
                Some(s) => s,
                None => return (1, "zftp rmdir: not connected\n".to_string()),
            };

            match sess.rmdir(args[1]) {
                Ok(resp) => {
                    if (resp.0 >= 200 && resp.0 < 300) {
                        (0, String::new())
                    } else {
                        (1, resp.1)
                    }
                }
                Err(e) => (1, format!("zftp rmdir: {}\n", e)),
            }
        }

        "rename" => {
            if args.len() < 3 {
                return (1, "zftp rename: from and to required\n".to_string());
            }

            let sess = match zftp.get_session_mut(None) {
                Some(s) => s,
                None => return (1, "zftp rename: not connected\n".to_string()),
            };

            match sess.rename(args[1], args[2]) {
                Ok(resp) => {
                    if (resp.0 >= 200 && resp.0 < 300) {
                        (0, String::new())
                    } else {
                        (1, resp.1)
                    }
                }
                Err(e) => (1, format!("zftp rename: {}\n", e)),
            }
        }

        "type" | "ascii" | "binary" => {
            let transfer_type = match args[0] {
                "ascii" => ZFST_ASCI,
                "binary" => ZFST_IMAG,
                "type" => {
                    if args.len() < 2 {
                        let sess = match zftp.get_session(None) {
                            Some(s) => s,
                            None => return (1, "zftp type: not connected\n".to_string()),
                        };
                        return (
                            0,
                            format!(
                                "{}\n",
                                if sess.transfer_type == ZFST_ASCI {
                                    "ascii"
                                } else {
                                    "binary"
                                }
                            ),
                        );
                    }
                    match args[1].to_lowercase().as_str() {
                        "a" | "ascii" => ZFST_ASCI,
                        "i" | "binary" | "image" => ZFST_IMAG,
                        _ => return (1, format!("zftp type: unknown type {}\n", args[1])),
                    }
                }
                _ => unreachable!(),
            };

            let sess = match zftp.get_session_mut(None) {
                Some(s) => s,
                None => return (1, "zftp type: not connected\n".to_string()),
            };

            match sess.set_type(transfer_type) {
                Ok(resp) => {
                    if (resp.0 >= 200 && resp.0 < 300) {
                        (0, String::new())
                    } else {
                        (1, resp.1)
                    }
                }
                Err(e) => (1, format!("zftp type: {}\n", e)),
            }
        }

        "bslashquote" => {
            if args.len() < 2 {
                return (1, "zftp bslashquote: command required\n".to_string());
            }

            let cmd = args[1..].join(" ");

            let sess = match zftp.get_session_mut(None) {
                Some(s) => s,
                None => return (1, "zftp bslashquote: not connected\n".to_string()),
            };

            match sess.bslashquote(&cmd) {
                Ok(resp) => (if (resp.0 >= 100 && resp.0 < 400) { 0 } else { 1 }, resp.1),
                Err(e) => (1, format!("zftp bslashquote: {}\n", e)),
            }
        }

        "close" | "quit" => {
            let sess = match zftp.get_session_mut(None) {
                Some(s) => s,
                None => return (0, String::new()),
            };

            match sess.close() {
                Ok(_) => (0, String::new()),
                Err(e) => (1, format!("zftp close: {}\n", e)),
            }
        }

        "session" => {
            if args.len() < 2 {
                let names = zftp.session_names();
                let current = zftp.current_name();
                let mut out = String::new();
                for name in names {
                    let marker = if Some(name) == current { "* " } else { "  " };
                    out.push_str(&format!("{}{}\n", marker, name));
                }
                return (0, out);
            }

            let name = args[1];
            if zftp.sessions.contains_key(name) {
                zftp.set_current(name);
            } else {
                zftp.create_session(name);
                zftp.set_current(name);
            }
            (0, String::new())
        }

        "rmsession" => {
            if args.len() < 2 {
                return (1, "zftp rmsession: session name required\n".to_string());
            }

            if zftp.remove_session(args[1]).is_some() {
                (0, String::new())
            } else {
                (
                    1,
                    format!("zftp rmsession: session {} not found\n", args[1]),
                )
            }
        }

        "test" => {
            // c:158 zftpcmdtab entry — inline check (zftp_test re-acquires
            // the lock; doing it here avoids cross-fn deadlock).
            let sess = zftp.get_session(None);
            if sess.map(|s| s.connected).unwrap_or(false) {
                (0, String::new())
            } else {
                (1, String::new())
            }
        }

        _ => (1, format!("zftp: unknown subcommand {}\n", args[0])),
    }
    })();
    drop(zftp_guard);
    if !output.is_empty() {
        if status == 0 { print!("{}", output); } else { eprint!("{}", output); }
    }
    status
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_transfer_type() {
        // Inline-test pattern matching C: `(typ & ZFST_IMAG) ? "I" : "A"`
        let ascii_letter = if (ZFST_ASCI & ZFST_IMAG) != 0 { "I" } else { "A" };
        let image_letter = if (ZFST_IMAG & ZFST_IMAG) != 0 { "I" } else { "A" };
        assert_eq!(ascii_letter, "A");
        assert_eq!(image_letter, "I");
    }

    #[test]
    fn test_transfer_mode() {
        // Inline-test pattern matching C: `(mode & ZFST_BLOC) ? "B" : "S"`
        let stream_letter = if (ZFST_STRE & ZFST_BLOC) != 0 { "B" } else { "S" };
        let block_letter  = if (ZFST_BLOC & ZFST_BLOC) != 0 { "B" } else { "S" };
        assert_eq!(stream_letter, "S");
        assert_eq!(block_letter, "B");
    }

    /// FTP reply-code class predicates per RFC 959. C tests these
    /// inline at every reply-check call site (e.g. `if (lastcode < 400)`).
    fn is_positive(c: i32) -> bool { c >= 100 && c < 400 }
    fn is_positive_completion(c: i32) -> bool { c >= 200 && c < 300 }
    fn is_positive_intermediate(c: i32) -> bool { c >= 300 && c < 400 }
    fn is_negative(c: i32) -> bool { c >= 400 }

    #[test]
    fn test_ftp_response_positive() {
        let resp: FtpResponse = (200, "OK".to_string());
        assert!(is_positive(resp.0));
        assert!(is_positive_completion(resp.0));
        assert!(!is_negative(resp.0));
    }

    #[test]
    fn test_ftp_response_intermediate() {
        let resp: FtpResponse = (331, "Password required".to_string());
        assert!(is_positive(resp.0));
        assert!(is_positive_intermediate(resp.0));
        assert!(!is_positive_completion(resp.0));
    }

    #[test]
    fn test_ftp_response_negative() {
        let resp: FtpResponse = (550, "File not found".to_string());
        assert!(is_negative(resp.0));
        assert!(!is_positive(resp.0));
    }

    #[test]
    fn test_ftp_session_new() {
        let sess = zftp_session::new("test");
        assert_eq!(sess.name, "test");
        assert!(!sess.connected);
        assert!(!sess.logged_in);
    }

    #[test]
    fn test_parse_pasv_response() {
        let msg = "227 Entering Passive Mode (192,168,1,1,4,1)";
        let (ip, port) = parse_pasv_response(msg).unwrap();
        assert_eq!(ip, "192.168.1.1");
        assert_eq!(port, 1025);
    }

    #[test]
    fn test_parse_pasv_response_invalid() {
        let msg = "invalid";
        assert!(parse_pasv_response(msg).is_err());
    }

    #[test]
    fn test_zftp_new() {
        let zftp = zftp_globals::new();
        assert!(zftp.session_names().is_empty());
    }

    #[test]
    fn test_zftp_create_session() {
        let mut zftp = zftp_globals::new();
        zftp.create_session("test");
        assert!(zftp.sessions.contains_key("test"));
    }

    #[test]
    fn test_zftp_remove_session() {
        let mut zftp = zftp_globals::new();
        zftp.create_session("test");
        assert!(zftp.remove_session("test").is_some());
        assert!(zftp.remove_session("test").is_none());
    }

    #[test]
    fn test_zftp_set_current() {
        let mut zftp = zftp_globals::new();
        zftp.create_session("test");
        assert!(zftp.set_current("test"));
        assert!(!zftp.set_current("nonexistent"));
    }

    #[test]
    fn test_builtin_zftp_no_args() {
        let mut zftp = zftp_globals::new();
        let status = bin_zftp("zftp", &[].iter().map(|s: &&str| s.to_string()).collect::<Vec<_>>(), &crate::ported::zsh_h::options { ind: [0u8; crate::ported::zsh_h::MAX_OPS], args: Vec::new(), argscount: 0, argsalloc: 0 }, 0);
        assert_eq!(status, 1);
    }

    /// Port of `zftp_open(char *name, char **args, int flags)` from `Src/Modules/zftp.c:1690`.
    #[test]
    fn test_builtin_zftp_session() {
        // Reset global state for test isolation.
        zftp_cleanup();
        let status = bin_zftp("zftp", &["session", "test"].iter().map(|s: &&str| s.to_string()).collect::<Vec<_>>(), &crate::ported::zsh_h::options { ind: [0u8; crate::ported::zsh_h::MAX_OPS], args: Vec::new(), argscount: 0, argsalloc: 0 }, 0);
        assert_eq!(status, 0);
        assert!(zftp_state().lock().unwrap().sessions.contains_key("test"));
        zftp_cleanup();
    }

    #[test]
    fn test_builtin_zftp_test_not_connected() {
        let mut zftp = zftp_globals::new();
        let status = bin_zftp("zftp", &["test"].iter().map(|s: &&str| s.to_string()).collect::<Vec<_>>(), &crate::ported::zsh_h::options { ind: [0u8; crate::ported::zsh_h::MAX_OPS], args: Vec::new(), argscount: 0, argsalloc: 0 }, 0);
        assert_eq!(status, 1);
    }
}

// ===========================================================
// Methods moved verbatim from src/ported/exec.rs because their
// C counterpart's source file maps 1:1 to this Rust module.
// Phase: module-shims
// ===========================================================

// BEGIN moved-from-exec-rs
// (impl ShellExecutor block moved to src/exec_shims.rs — see file marker)

// END moved-from-exec-rs

// =====================================================================
// static struct features module_features                            c:3163 (zftp.c)
// =====================================================================

use crate::ported::zsh_h::module;

// `bintab` — port of `static struct builtin bintab[]` (zftp.c).


// `module_features` — port of `static struct features module_features`
// from zftp.c:3163.



/// Port of `setup_(UNUSED(Module m))` from `Src/Modules/zftp.c:3174`.
#[allow(unused_variables)]
pub fn setup_(m: *const module) -> i32 {                                    // c:3174
    // C body c:3176-3177 — `return 0`. Faithful empty-body port.
    0
}

/// Port of `features_(UNUSED(Module m), UNUSED(char ***features))` from `Src/Modules/zftp.c:3181`.
pub fn features_(m: *const module, features: &mut Vec<String>) -> i32 {
    *features = featuresarray(m, module_features());
    0
}

/// Port of `enables_(UNUSED(Module m), UNUSED(int **enables))` from `Src/Modules/zftp.c:3189`.
pub fn enables_(m: *const module, enables: &mut Option<Vec<i32>>) -> i32 {
    handlefeatures(m, module_features(), enables)
}

/// Port of `boot_(UNUSED(Module m))` from `Src/Modules/zftp.c:3196`.
#[allow(unused_variables)]
pub fn boot_(m: *const module) -> i32 {                                     // c:3196
    // C body c:3198-3214:
    //   off_t tmout_def = 60;
    //   zfsetparam("ZFTP_VERBOSE", "450", ZFPM_IFUNSET);
    //   zfsetparam("ZFTP_TMOUT", &tmout_def, ZFPM_IFUNSET|ZFPM_INTEGER);
    //   zfsetparam("ZFTP_PREFS", "PS", ZFPM_IFUNSET);
    //   zfprefs = ZFPF_SNDP|ZFPF_PASV;
    //   zfsessions = znewlinklist(); newsession("default");
    //   addhookfunc("exit", zftpexithook);
    zfsetparam("ZFTP_VERBOSE", "450", ZFPM_IFUNSET);                         // c:3203
    zfsetparam("ZFTP_TMOUT", "60", ZFPM_IFUNSET | ZFPM_INTEGER);             // c:3219
    zfsetparam("ZFTP_PREFS", "PS", ZFPM_IFUNSET);                            // c:3219
    zfprefs.store(ZFPF_SNDP | ZFPF_PASV,                                     // c:3219
                  std::sync::atomic::Ordering::Relaxed);
    let _default = newsession("default");                                    // c:3219
    // c:3219 — addhookfunc("exit", zftpexithook). Hook-table substrate
    // not ported; exit-hook firing is process-end so the leak window
    // is bounded to module-unload-then-exit which is the normal flow.
    0
}

/// Port of `cleanup_(UNUSED(Module m))` from `Src/Modules/zftp.c:3219`.
pub fn cleanup_(m: *const module) -> i32 {                                  // c:3219
    // c:3228 — deletehookfunc("exit", zftpexithook). The Rust hook
    // table (utils::HOOKS) isn't yet wired to dispatch through
    // deletehookfunc; static-link skip until the hook-fn substrate
    // lands. The exit hook is one-shot anyway (process is going away).
    // c:3228 — zftp_cleanup(): close every live session.
    zftp_cleanup();                                                          // c:3228
    setfeatureenables(m, module_features(), None) // c:3228
}

/// Port of `finish_(UNUSED(Module m))` from `Src/Modules/zftp.c:3228`.
#[allow(unused_variables)]
pub fn finish_(m: *const module) -> i32 {                                   // c:3228
    // C body c:3230-3231 — `return 0`. Faithful empty-body port; the
    //                      cleanup of zfsessions happens in cleanup_.
    0
}

// === auto-generated stubs ===
// Direct ports of static helpers from Src/Modules/zftp.c not
// yet covered above. zshrs links modules statically; live
// state owned by the module's typed struct. Name-parity shims.

/// Port of `freesession(Zftp_session sptr)` from `Src/Modules/zftp.c:2874`.
/// C: `static void freesession(Zftp_session sptr)` — release `sptr`'s
/// name + params + userparams + the struct itself.
#[allow(non_snake_case)]
pub fn freesession(sptr: &mut zftp_session) {                                 // c:2874
    // c:2874 — zsfree(sptr->name);
    sptr.name.clear();
    // c:2878-2881 — walk zfparams + sptr->params freeing each param value.
    sptr.params.clear();
    // c:2882-2883 — if (sptr->userparams) freearray(sptr->userparams);
    sptr.userparams.clear();
    // c:2884 — zfree(sptr, sizeof(struct zftp_session)); the caller's
    // owning Box::drop releases the struct memory.
}

/// Port of `newsession(char *nm)` from `Src/Modules/zftp.c:2803`.
/// C: `static Zftp_session newsession(char *nm)`.
#[allow(non_snake_case)]
pub fn newsession(nm: &str) -> Box<zftp_session> {
    Box::new(zftp_session::new(nm))
}

/// Port of `savesession()` from `Src/Modules/zftp.c:2832`.
/// C: `static void savesession(void)` — copy each ZFTP_* shell param
/// into zfsess->params so session-switching preserves the values.
#[allow(non_snake_case)]
pub fn savesession() {                                                        // c:2832
    // c:2832 — char **ps, **pd, *val; (Rust uses indexing over slices)
    let val: String;
    let _ = val;

    if let Ok(mut state) = zftp_state().lock() {
        let sess = match state.get_session_mut(None) {
            Some(s) => s,
            None => return,
        };
        // c:2836-2845 — for each zfparams[i], copy the current shell param.
        sess.params.clear();
        for ps in ZFPARAMS {                                                  // c:2836
            // c:2840 — `val = getsparam(*ps);`. paramtab is bucket-2-
            //          consolidated now; read directly. Was a fake env
            //          read which never picked up shell-internal params.
            let val = crate::ported::params::getsparam(ps).unwrap_or_default();
            // c:2856 / c:2843 — *pd = ztrdup(val) or NULL.
            sess.params.push(val);
        }
        // c:2856 — *pd = NULL; (terminator) — Rust Vec is self-terminating.
    }
}

/// Port of `switchsession(char *nm)` from `Src/Modules/zftp.c:2856`.
/// C: `static void switchsession(char *nm)`.
#[allow(non_snake_case)]
pub fn switchsession(nm: &str) {
    if let Ok(mut state) = zftp_state().lock() {
        // C: walks zfsessions list for matching `nm`; if missing,
        // creates one. Static-link path: register-or-create on the zftp_globals wrapper.
        let _ = state.create_session(nm);
        state.set_current(nm);
    }
}

/// Port of `zfalarm(int tmout)` from `Src/Modules/zftp.c:384`.
/// C: `static void zfalarm(int tmout)` — set up alarm + SIGALRM handler.
#[allow(non_snake_case)]
pub fn zfalarm(tmout: i32) {                                                  // c:384
    ZFDRRRRING.store(0, std::sync::atomic::Ordering::Relaxed);                // c:384
    // c:387-392 — fire alarm even when tmout is 0 so a pending non-zero
    // main-shell alarm doesn't bleed into the FTP code path.
    if ZFALARMED.load(std::sync::atomic::Ordering::Relaxed) != 0 {            // c:393
        unsafe { libc::alarm(tmout as u32); }                                 // c:394
        return;                                                               // c:395
    }
    // c:397 — signal(SIGALRM, zfhandler);
    unsafe {
        libc::signal(libc::SIGALRM, zfhandler as libc::sighandler_t);
    }
    // c:398 — oalremain = alarm(tmout);
    let oalremain = unsafe { libc::alarm(tmout as u32) };
    OALREMAIN.store(oalremain, std::sync::atomic::Ordering::Relaxed);
    if oalremain != 0 {                                                       // c:399
        // c:400 — oaltime = zmonotime(NULL);
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs() as i64)
            .unwrap_or(0);
        OALTIME.store(now, std::sync::atomic::Ordering::Relaxed);
    }
    ZFALARMED.store(1, std::sync::atomic::Ordering::Relaxed);                 // c:405
}

/// Port of `zfargstring(char *cmd, char **args)` from `Src/Modules/zftp.c:546`.
/// C: `char *zfargstring(char *cmd, char **args)` — joins cmd + args.
#[allow(non_snake_case)]
pub fn zfargstring(cmd: &str, args: &[&str]) -> String {
    // c:546-570 — zhalloc + sprintf joining cmd + space-sep args.
    let mut s = cmd.to_string();
    for a in args {
        s.push(' ');
        s.push_str(a);
    }
    s
}

/// Port of `zfclose(int leaveparams)` from `Src/Modules/zftp.c:2711`.
/// C: `void zfclose(int leaveparams)` — close the control connection
/// (and optionally tear down the ZFTP_* params), run the zftp_chpwd
/// hook, reset zfclosing+zfdrrrring tidy-up flags.
#[allow(non_snake_case)]
pub fn zfclose(leaveparams: i32) {                                            // c:2711
    // c:2715-2716 — early-return when no live control connection.
    let alive = zftp_state().lock().ok()
        .and_then(|s| s.get_session(None).map(|sess| sess.control.is_some()))
        .unwrap_or(false);
    if !alive {                                                               // c:2715
        return;                                                               // c:2716
    }
    // c:2718 — zfclosing = 1.
    ZFCLOSING.store(1, Ordering::Relaxed);                                    // c:2718
    // c:2719-2725 — send QUIT before teardown unless server already EOF'd.
    if ZCFINISH.load(Ordering::Relaxed) != 2 {                                // c:2719
        let _ = zfsendcmd("QUIT\r\n");                                        // c:2724
    }
    // c:2727-2737 — drop cin + control TcpStream + decrement zfnopen.
    if let Ok(mut state) = zftp_state().lock() {
        if let Some(sess) = state.get_session_mut(None) {
            sess.cin = None;                                                  // c:2735 fclose(cin)
            sess.control = None;                                              // c:2745 tcp_close
            sess.dfd = -1;
            sess.connected = false;
            sess.logged_in = false;
        }
    }
    ZFNOPEN.fetch_sub(1, Ordering::Relaxed);                                  // c:2743

    // c:2749-2759 — zfstatusp ZFST_CLOS + close zfstatfd when last open
    // session goes away. zfstatfd substrate not ported — skipped.

    // c:2761-2774 — !leaveparams: unset ZFTP_* params + zftp_chpwd hook.
    if leaveparams == 0 {                                                     // c:2761
        for n in ["ZFTP_HOST", "ZFTP_PORT", "ZFTP_IP", "ZFTP_SYSTEM",         // c:2763 zfparams[]
                  "ZFTP_USER", "ZFTP_ACCOUNT", "ZFTP_PWD",
                  "ZFTP_TYPE", "ZFTP_MODE"] {
            zfunsetparam(n);                                                  // c:2764
        }
        // c:2767-2773 — zftp_chpwd shfunc dispatch.
        if crate::ported::utils::getshfunc("zftp_chpwd").is_some() {          // c:2767
            let osc = crate::ported::builtin::SFCONTEXT.load(Ordering::Relaxed);
            crate::ported::builtin::SFCONTEXT.store(
                crate::ported::zsh_h::SFC_HOOK, Ordering::Relaxed);            // c:2770
            // c:2771 doshfunc dispatch — VM-level CallFunction lives
            // inside fusevm; static caller probes via getshfunc.
            let _ = crate::ported::builtin::LASTVAL.load(Ordering::Relaxed);
            crate::ported::builtin::SFCONTEXT.store(osc, Ordering::Relaxed);   // c:2772
        }
    }
    // c:2777 — zfclosing = zfdrrrring = 0.
    ZFCLOSING.store(0, Ordering::Relaxed);                                    // c:2777
    ZFDRRRRING.store(0, Ordering::Relaxed);                                   // c:2777
}

/// Port of `zfclosedata()` from `Src/Modules/zftp.c:1043`.
/// C: `static void zfclosedata(void)` — early-return when no dfd is
/// live, otherwise close(dfd) + dfd = -1.
#[allow(non_snake_case)]
pub fn zfclosedata() {                                                    // c:1043
    if let Ok(mut state) = zftp_state().lock() {
        if let Some(sess) = state.get_session_mut(None) {
            if sess.dfd == -1 {                                           // c:1045
                return;                                                   // c:1046
            }
            unsafe { libc::close(sess.dfd); }                             // c:1047 close(dfd)
            sess.dfd = -1;                                                // c:1048
        }
    }
}

/// Port of `zfendtrans()` from `Src/Modules/zftp.c:1295`.
/// C: `static void zfendtrans(void)` — unsets the ZFTP_* transfer params.
#[allow(non_snake_case)]
pub fn zfendtrans() {                                                         // c:1295
    zfunsetparam("ZFTP_SIZE");                                                // c:1295
    zfunsetparam("ZFTP_FILE");                                                // c:1298
    zfunsetparam("ZFTP_TRANSFER");                                            // c:1299
    zfunsetparam("ZFTP_COUNT");                                               // c:1300
}

/// Port of `zfgetcwd()` from `Src/Modules/zftp.c:2358`.
/// C: `static int zfgetcwd(void)` — sends PWD, parses reply.
#[allow(non_snake_case)]
pub fn zfgetcwd() -> i32 {
    // c:2358 — short-circuit when ZFPF_DUMB is set (don't fiddle with
    // variables in dumb mode).
    if (zfprefs.load(std::sync::atomic::Ordering::Relaxed) & ZFPF_DUMB) != 0 { // c:2364
        return 1;                                                              // c:2365
    }
    if zfsendcmd("PWD\r\n") > 2 {                                              // c:2366
        zfunsetparam("ZFTP_PWD");                                              // c:2367
        return 1;                                                              // c:2368
    }
    if lastcode.load(std::sync::atomic::Ordering::Relaxed) >= 200 { 0 } else { 1 }
}

/// Port of `zfgetdata(char *name, char *rest, char *cmd, int getsize)` from `Src/Modules/zftp.c:1065`.
/// C: `static int zfgetdata(char *name, char *rest, char *cmd, int getsize)` —
/// open the data connection (PASV or PORT mode via zfopendata),
/// optionally send REST, send the transfer command, and (PORT mode)
/// accept(2) on the listening socket. Returns 0 success, 1 failure.
#[allow(non_snake_case)]
pub fn zfgetdata(name: &str, rest: &str, cmd: &str, getsize: i32) -> i32 {    // c:1065
    // c:1065-1069 — locals at fn top.
    let is_passive: bool;                                                     // c:1068

    // c:1071-1072 — zfopendata: full PASV/PORT setup; sets sess.dfd.
    let (rc, ip) = zfopendata(name);                                          // c:1071
    if rc != 0 {                                                              // c:1071
        return 1;                                                             // c:1072
    }
    is_passive = ip;

    // c:1084-1087 — REST command for resume.
    if !rest.is_empty() && zfsendcmd(rest) > 3 {
        zfclosedata();
        return 1;
    }

    // c:1089-1092 — send the transfer command (RETR / STOR / etc.).
    if zfsendcmd(cmd) > 2 {                                                   // c:1089
        zfclosedata();                                                        // c:1090
        return 1;                                                             // c:1091
    }

    // c:1093-1116 — parse "Opening data connection for file (N bytes)"
    // hint to populate ZFTP_SIZE without a separate SIZE request.
    if getsize != 0 || cmd.starts_with("RETR") {
        let cur_last = lastmsg.lock().ok().map(|m| m.clone()).unwrap_or_default();
        if let Some(byte_idx) = cur_last.find("bytes") {                      // c:1101
            // Walk backward to find the start of the digit run.
            let prefix = &cur_last[..byte_idx];
            let trimmed: String = prefix
                .chars()
                .rev()
                .skip_while(|c| !c.is_ascii_digit())
                .take_while(|c| c.is_ascii_digit())
                .collect::<String>()
                .chars()
                .rev()
                .collect();
            if !trimmed.is_empty() && getsize != 0 {
                zfsetparam("ZFTP_SIZE", &trimmed, ZFPM_READONLY | ZFPM_INTEGER); // c:1112
            }
        }
    }

    // c:1118-1143 — PORT-mode accept handling. PASV: dfd is already
    // the data fd, just zfmovefd it. PORT: accept() on the listening
    // fd to obtain the real data fd, then close the listener.
    let dfd_raw: i32;
    if !is_passive {                                                          // c:1118
        // c:1124-1128 — accept the connection from the server.
        let mut sa: libc::sockaddr_storage = unsafe { std::mem::zeroed() };
        let mut sl: libc::socklen_t = std::mem::size_of::<libc::sockaddr_storage>() as _;
        let listen_fd = zftp_state().lock().ok()
            .and_then(|s| s.get_session(None).map(|x| x.dfd))
            .unwrap_or(-1);
        let newfd = unsafe {
            libc::accept(listen_fd, &mut sa as *mut _ as *mut _, &mut sl)
        };
        if newfd < 0 {                                                        // c:1129
            crate::ported::utils::zwarnnam(name,
                &format!("unable to accept data: {}", std::io::Error::last_os_error()));
            zfclosedata();                                                    // c:1131
            return 1;
        }
        // c:1133 — close the original listening fd, install the accepted one.
        unsafe { libc::close(listen_fd); }
        dfd_raw = zfmovefd(newfd);
    } else {                                                                  // c:1136
        // c:1139-1142 — zfmovefd(zfsess->dfd) for PASV.
        let cur_dfd = zftp_state().lock().ok()
            .and_then(|s| s.get_session(None).map(|x| x.dfd))
            .unwrap_or(-1);
        dfd_raw = zfmovefd(cur_dfd);
    }
    if let Ok(mut state) = zftp_state().lock() {
        if let Some(sess) = state.get_session_mut(None) {
            sess.dfd = dfd_raw;                                               // c:1142
        }
    }

    // c:1156-1163 — SO_LINGER 120s.
    let li = libc::linger { l_onoff: 1, l_linger: 120 };
    unsafe {
        libc::setsockopt(dfd_raw, libc::SOL_SOCKET, libc::SO_LINGER,
                         &li as *const _ as *const libc::c_void,
                         std::mem::size_of::<libc::linger>() as libc::socklen_t);
    }
    // c:1167-1170 — IP_TOS = IPTOS_THROUGHPUT.
    let tos: libc::c_int = 0x08;                                              // IPTOS_THROUGHPUT
    unsafe {
        libc::setsockopt(dfd_raw, libc::IPPROTO_IP, libc::IP_TOS,
                         &tos as *const _ as *const libc::c_void,
                         std::mem::size_of::<libc::c_int>() as libc::socklen_t);
    }
    // c:1174 — fcntl(dfd, F_SETFD, FD_CLOEXEC).
    unsafe { libc::fcntl(dfd_raw, libc::F_SETFD, libc::FD_CLOEXEC); }

    0                                                                         // c:1177
}

/// Port of `zfgetinfo(char *prompt, int noecho)` from `Src/Modules/zftp.c:1999`.
/// C: `static char * zfgetinfo(char *prompt, int noecho)` — prompt
/// the tty (echoing or with ECHO masked off for passwords) and read
/// one line of input.
#[allow(non_snake_case)]
pub fn zfgetinfo(prompt: &str, noecho: i32) -> Option<String> {              // c:1999
    // c:2001-2006 — locals.
    let mut resettty: i32 = 0;                                                // c:2001
    let mut instr = String::new();                                            // c:2005 char instr[256]
    let len: usize = 0;                                                       // c:2006 (unused in Rust path)
    let _ = len;

    let saved_termios: Option<libc::termios>;

    // c:2013 — if (isatty(0)) prompt + tty setup.
    if unsafe { libc::isatty(0) } != 0 {                                      // c:2013
        if noecho != 0 {                                                      // c:2014
            // c:2024-2032 — copy current termios, clear ECHO, install.
            let mut ti: libc::termios = unsafe { std::mem::zeroed() };
            if unsafe { libc::tcgetattr(0, &mut ti) } == 0 {
                saved_termios = Some(ti);
                ti.c_lflag &= !libc::ECHO;                                    // c:2028
                unsafe { libc::tcsetattr(0, libc::TCSANOW, &ti); }            // c:2032
                resettty = 1;                                                 // c:2033
            } else {
                saved_termios = None;
            }
        } else {
            saved_termios = None;
        }
        // c:2035-2037 — fflush(stdin) + write prompt to stderr.
        eprint!("{}", prompt);                                                // c:2036
        let _ = std::io::stderr().flush();                                    // c:2037
    } else {
        saved_termios = None;
    }

    // c:2040-2043 — fgets(instr, 256, stdin); strip trailing \n.
    let stdin = std::io::stdin();
    let mut handle = stdin.lock();
    match handle.read_line(&mut instr) {                                      // c:2040
        Ok(0) => instr.clear(),                                               // c:2041 NULL → empty
        Ok(_) => {                                                            // c:2042-2043 strip \n
            if instr.ends_with('\n') {
                instr.pop();
            }
        }
        Err(_) => instr.clear(),
    }

    // c:2045 — strret = dupstring(instr); (just keep instr as the result)
    let strret = instr.clone();

    // c:2047-2052 — restore termios if we modified it.
    if resettty != 0 {                                                        // c:2047
        println!();                                                           // c:2049 '\n' didn't echo
        let _ = std::io::stdout().flush();                                    // c:2050
        if let Some(ti) = saved_termios {                                     // c:2051
            unsafe { libc::tcsetattr(0, libc::TCSANOW, &ti); }
        }
    }

    Some(strret)                                                              // c:2054
}

/// Port of `zfgetline(char *ln, int lnsize, int tmout)` from `Src/Modules/zftp.c:571`.
/// C: `int zfgetline(char *ln, int lnsize, int tmout)` — read a single
/// CRLF-terminated line from the control connection, handling TELNET
/// IAC command escapes and SIGALRM-driven timeout.
#[allow(non_snake_case)]
pub fn zfgetline(ln: &mut [u8], lnsize: i32, tmout: i32) -> i32 {             // c:571
    // c:573-575 — locals at function top (Rule 5).
    let mut ch: i32;                                                          // c:573 int ch
    let mut added: i32 = 0;                                                   // c:573 added
    // c:575 — char *pcur = ln, cmdbuf[3];
    let mut pcur: usize = 0;                                                  // pointer index into ln
    let mut cmdbuf: [u8; 3] = [0; 3];

    ZCFINISH.store(0, std::sync::atomic::Ordering::Relaxed);                  // c:577 zcfinish = 0
    let lnsize = lnsize - 1;                                                  // c:579 leave room for null
    if !ln.is_empty() {
        ln[0] = 0;                                                            // c:581 ln[0] = '\0'
    }

    // c:583-587 — setjmp guard via ZFDRRRRING flag.
    if ZFDRRRRING.load(std::sync::atomic::Ordering::Relaxed) != 0 {           // c:583
        unsafe { libc::alarm(0); }                                            // c:584
        crate::ported::utils::zwarnnam("zftp", "timeout getting response");   // c:585
        return 6;                                                             // c:586
    }
    zfalarm(tmout);                                                           // c:588

    // c:597-678 — for (;;) read loop with TELNET IAC handling.
    let mut state = match zftp_state().lock() {
        Ok(s) => s,
        Err(_) => return 6,
    };
    let sess = match state.get_session_mut(None) {
        Some(s) => s,
        None => return 6,
    };
    let stream = match sess.cin.as_mut() {
        Some(s) => s,
        None => return 6,
    };
    let mut byte = [0u8; 1];

    'main: loop {                                                             // c:597 for (;;)
        // c:598 — ch = fgetc(zfsess->cin);
        ch = match stream.read(&mut byte) {
            Ok(0) => -1,                                                      // EOF
            Ok(_) => byte[0] as i32,
            Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,// c:602 EINTR retry
            Err(_) => -1,
        };

        match ch {
            -1 => {                                                           // c:601 EOF
                ZCFINISH.store(2, std::sync::atomic::Ordering::Relaxed);      // c:606
            }
            0x0d => {                                                         // c:609 '\r'
                ch = match stream.read(&mut byte) {                           // c:611
                    Ok(0) => -1,
                    Ok(_) => byte[0] as i32,
                    Err(_) => -1,
                };
                if ch == -1 {                                                 // c:612 EOF
                    ZCFINISH.store(2, std::sync::atomic::Ordering::Relaxed);  // c:613
                } else if ch == 0x0a {                                        // c:616 '\n'
                    ZCFINISH.store(1, std::sync::atomic::Ordering::Relaxed);  // c:617
                } else if ch == 0x00 {                                        // c:620 '\0'
                    ch = 0x0d;                                                // c:621
                } else {
                    ch = 0x0d;                                                // c:625
                }
            }
            0x0a => {                                                         // c:628 '\n' (unexpected)
                ZCFINISH.store(1, std::sync::atomic::Ordering::Relaxed);      // c:630
            }
            255 => {                                                          // c:633 IAC
                ch = match stream.read(&mut byte) {                           // c:638
                    Ok(0) => -1,
                    Ok(_) => byte[0] as i32,
                    Err(_) => -1,
                };
                match ch {
                    251 | 252 => {                                            // c:640-641 WILL/WONT
                        ch = match stream.read(&mut byte) {                   // c:642
                            Ok(0) => -1,
                            Ok(_) => byte[0] as i32,
                            Err(_) => -1,
                        };
                        cmdbuf[0] = 255;                                      // c:644 IAC
                        cmdbuf[1] = 254;                                      // c:645 DONT
                        cmdbuf[2] = ch as u8;                                 // c:646
                        // c:647 — write_loop(zfsess->control->fd, cmdbuf, 3);
                        if let Some(ctrl) = sess.control.as_mut() {
                            let _ = ctrl.write_all(&cmdbuf);
                        }
                        continue 'main;                                       // c:648
                    }
                    253 | 254 => {                                            // c:650-651 DO/DONT
                        ch = match stream.read(&mut byte) {                   // c:652
                            Ok(0) => -1,
                            Ok(_) => byte[0] as i32,
                            Err(_) => -1,
                        };
                        cmdbuf[0] = 255;                                      // c:654 IAC
                        cmdbuf[1] = 252;                                      // c:655 WONT
                        cmdbuf[2] = ch as u8;                                 // c:656
                        if let Some(ctrl) = sess.control.as_mut() {
                            let _ = ctrl.write_all(&cmdbuf);
                        }
                        continue 'main;                                       // c:658
                    }
                    -1 => {                                                   // c:660 EOF
                        ZCFINISH.store(2, std::sync::atomic::Ordering::Relaxed); // c:662
                    }
                    _ => {}                                                   // c:665 default
                }
            }
            _ => {}
        }

        // c:671-672 — if (zcfinish) break;
        if ZCFINISH.load(std::sync::atomic::Ordering::Relaxed) != 0 {
            break;
        }
        // c:673-676 — if (added < lnsize) { *pcur++ = ch; added++; }
        if added < lnsize && pcur < ln.len() {
            ln[pcur] = ch as u8;
            pcur += 1;
            added += 1;
        }
        // c:677 — junk if no room, keep reading.
    }

    unsafe { libc::alarm(0); }                                                // c:680
    if pcur < ln.len() {
        ln[pcur] = 0;                                                         // c:702 *pcur = '\0'
    }
    // c:702 — return (zcfinish & 2);
    ZCFINISH.load(std::sync::atomic::Ordering::Relaxed) & 2
}

/// Port of `zfgetmsg()` from `Src/Modules/zftp.c:702`.
/// C: `static int zfgetmsg(void)` — read a complete FTP server reply
/// (possibly multi-line), parse the 3-digit code, update lastcode +
/// lastcodestr + lastmsg + ZFTP_REPLY, return the first-digit status
/// (1/2/3/4/5) or 6 on error/disconnect.
#[allow(non_snake_case)]
pub fn zfgetmsg() -> i32 {                                                    // c:702
    // c:702-705 — char line[256], *ptr, *verbose;
    //             int stopit, printing = 0, tmout;
    let mut line = [0u8; 256];
    let mut printing: i32 = 0;
    let stopit_initial: bool;
    let tmout: i32;

    // c:707-708 — if (!zfsess->control) return 6;
    {
        let state = match zftp_state().lock() {
            Ok(s) => s,
            Err(_) => return 6,
        };
        let sess = match state.get_session(None) {
            Some(s) => s,
            None => return 6,
        };
        if sess.control.is_none() {
            return 6;                                                         // c:708
        }
    }

    // c:709-710 — zsfree(lastmsg); lastmsg = NULL;
    if let Ok(mut m) = lastmsg.lock() {
        m.clear();
    }

    // c:712 — tmout = getiparam("ZFTP_TMOUT");
    // c:712 — `tmout = getiparam("ZFTP_TMOUT");`. Read paramtab, not OS env.
    tmout = crate::ported::params::getiparam("ZFTP_TMOUT") as i32;

    // c:714 — zfgetline(line, 256, tmout);
    zfgetline(&mut line, 256, tmout);
    // c:715 — ptr = line; (use string slice + offset index instead)
    let mut ptr_off: usize = 0;
    let line_str = std::str::from_utf8(&line)
        .unwrap_or("")
        .trim_end_matches('\0')
        .to_string();

    // c:716 — if (zfdrrrring || !idigit(ptr[0..3])) — timeout or not FTP.
    let is_digit = |b: u8| b.is_ascii_digit();
    let timeout_or_bad = ZFDRRRRING.load(std::sync::atomic::Ordering::Relaxed) != 0
        || line.len() < 3
        || !is_digit(line[0])
        || !is_digit(line[1])
        || !is_digit(line[2]);
    if timeout_or_bad {                                                       // c:716
        ZCFINISH.store(2, std::sync::atomic::Ordering::Relaxed);              // c:718
        if ZFCLOSING.load(std::sync::atomic::Ordering::Relaxed) == 0 {        // c:719
            zfclose(0);                                                       // c:720
        }
        if let Ok(mut m) = lastmsg.lock() { m.clear(); }                      // c:721
        if let Ok(mut cs) = lastcodestr.lock() {                              // c:722
            cs.copy_from_slice(b"000\0");
        }
        zfsetparam("ZFTP_REPLY", "", ZFPM_READONLY);                          // c:723
        return 6;                                                             // c:724
    }

    // c:726-729 — extract first 3 bytes into lastcodestr, parse to int.
    let code_str: String = std::str::from_utf8(&line[..3]).unwrap_or("0").to_string();
    if let Ok(mut cs) = lastcodestr.lock() {
        cs[0] = line[0]; cs[1] = line[1]; cs[2] = line[2]; cs[3] = 0;
    }
    let code: i32 = code_str.parse().unwrap_or(0);
    lastcode.store(code, std::sync::atomic::Ordering::Relaxed);
    ptr_off += 3;
    // c:730 — zfsetparam("ZFTP_CODE", lastcodestr, ZFPM_READONLY);
    zfsetparam("ZFTP_CODE", &code_str, ZFPM_READONLY);
    // c:731 — stopit = (*ptr++ != '-');
    stopit_initial = line.get(ptr_off).copied() != Some(b'-');
    ptr_off += 1;
    let mut stopit = stopit_initial;

    // c:733-744 — verbose check + initial-line printing.
    let verbose = crate::ported::params::getsparam("ZFTP_VERBOSE").unwrap_or_default();  // c:734
    if verbose.contains(line[0] as char) {                                    // c:736
        printing = 1;                                                         // c:738
        eprint!("{}", line_str);                                              // c:739
    } else if verbose.contains('0') && !stopit {                              // c:740
        printing = 2;                                                         // c:742
        eprint!("{}", &line_str[ptr_off..]);                                  // c:743
    }
    if printing != 0 {                                                        // c:746
        eprintln!();                                                          // c:747
    }

    // c:749-775 — multi-line continuation loop.
    while ZCFINISH.load(std::sync::atomic::Ordering::Relaxed) != 2 && !stopit {
        line.fill(0);                                                         // reset
        ptr_off = 0;
        zfgetline(&mut line, 256, tmout);                                     // c:750
        if ZFDRRRRING.load(std::sync::atomic::Ordering::Relaxed) != 0 {       // c:752
            line[0] = 0;                                                      // c:753
            break;                                                            // c:754
        }
        // c:757-764 — code-prefix check.
        if &line[..3] == &code_str.as_bytes()[..3] {                          // c:757
            if line[3] == b' ' {                                              // c:758
                stopit = true;                                                // c:759
                ptr_off = 4;                                                  // c:760
            } else if line[3] == b'-' {                                       // c:761
                ptr_off = 4;                                                  // c:762
            }
        } else if &line[..4] == b"    " {                                     // c:763
            ptr_off = 4;                                                      // c:764
        }

        // c:766-774 — print intermediate line per `printing` mode.
        let cont_line = std::str::from_utf8(&line)
            .unwrap_or("")
            .trim_end_matches('\0');
        if printing == 2 {                                                    // c:766
            if !stopit {                                                      // c:767
                eprintln!("{}", &cont_line[ptr_off..]);                       // c:768-769
            }
        } else if printing != 0 {                                             // c:771
            eprintln!("{}", cont_line);                                       // c:772-773
        }
    }

    // c:777-778 — fflush(stderr);
    if printing != 0 {
        let _ = std::io::stderr().flush();
    }

    // c:781 — lastmsg = ztrdup(ptr);  (the trailing portion of last line)
    let last_msg_str: String = std::str::from_utf8(&line)
        .unwrap_or("")
        .trim_end_matches('\0')
        .chars()
        .skip(ptr_off)
        .collect();
    if let Ok(mut m) = lastmsg.lock() {
        *m = last_msg_str.clone();
    }
    // c:785 — zfsetparam("ZFTP_REPLY", ztrdup(line), ZFPM_READONLY);
    let whole_line = std::str::from_utf8(&line)
        .unwrap_or("")
        .trim_end_matches('\0');
    zfsetparam("ZFTP_REPLY", whole_line, ZFPM_READONLY);

    // c:791-797 — EOF or 421: close + warn.
    let zcfin = ZCFINISH.load(std::sync::atomic::Ordering::Relaxed);
    let cur_code = lastcode.load(std::sync::atomic::Ordering::Relaxed);
    if (zcfin == 2 || cur_code == 421)
        && ZFCLOSING.load(std::sync::atomic::Ordering::Relaxed) == 0 {
        ZCFINISH.store(2, std::sync::atomic::Ordering::Relaxed);              // c:792
        zfclose(0);                                                           // c:793
        crate::ported::utils::zwarnnam("zftp",                                // c:795
            "remote server has closed connection");
        return 6;                                                             // c:796
    }
    // c:798-801 — 530 not-logged-in.
    if cur_code == 530 {                                                      // c:798
        return 6;                                                             // c:800
    }
    // c:807-810 — 120 wait-and-retry.
    if cur_code == 120 {                                                      // c:807
        crate::ported::utils::zwarnnam("zftp",                                // c:808
            &format!("delay expected, waiting: {}", last_msg_str));
        return zfgetmsg();                                                    // c:809
    }
    // c:813 — return lastcodestr[0] - '0';
    (code_str.as_bytes()[0] - b'0') as i32
}

/// Port of `zfhandler(int sig)` from `Src/Modules/zftp.c:366`.
/// C: `static void zfhandler(int sig)` — SIGALRM handler. Sets the
/// `zfdrrrring` flag so the next zfread/zfgetline returns -1 and exits
/// its setjmp-protected critical section.
#[allow(non_snake_case)]
pub extern "C" fn zfhandler(sig: i32) {                                       // c:366
    if sig == libc::SIGALRM {                                                 // c:368
        ZFDRRRRING.store(1, std::sync::atomic::Ordering::Relaxed);            // c:369
        // c:370-374 — errno = ETIMEDOUT (or EIO).
        unsafe {
            *errno_ptr() = libc::ETIMEDOUT;
        }
        // c:375 — longjmp(zfalrmbuf, 1). Rust port doesn't use setjmp;
        // the ZFDRRRRING flag is the timeout signal each blocking
        // read/write polls.
    }
    // c:377 DPUTS — unreachable in static-link path.
}

/// Port of `zfmovefd(int fd)` from `Src/Modules/zftp.c:472`.
/// C: `static int zfmovefd(int fd)` — moves fd above SHTTY.
#[allow(non_snake_case)]
pub fn zfmovefd(fd: i32) -> i32 {
    // c:472-490 — fcntl(F_DUPFD) past 10. Static-link: pass through.
    fd
}

/// Port of `zfpipe()` from `Src/Modules/zftp.c:412`.
/// C: `static void zfpipe(void)` — ignore SIGPIPE so write() returns
/// EPIPE instead of killing the shell.
#[allow(non_snake_case)]
pub fn zfpipe() {                                                             // c:412
    // c:412 — signal(SIGPIPE, SIG_IGN);
    unsafe {
        libc::signal(libc::SIGPIPE, libc::SIG_IGN);
    }
}

/// Port of `zfread(int fd, char *bf, off_t sz, int tmout)` from `Src/Modules/zftp.c:1307`.
/// C: `static int zfread(int fd, char *bf, off_t sz, int tmout)` — read
/// up to `sz` bytes from fd; with `tmout > 0` install a SIGALRM-driven
/// timeout that aborts the read.
#[allow(non_snake_case)]
pub fn zfread(fd: i32, bf: &mut [u8], sz: libc::off_t, tmout: i32) -> i32 {   // c:1307
    let ret: isize;                                                           // c:1307 int ret

    // c:1311-1312 — no timeout: plain read.
    if tmout == 0 {
        let n = unsafe {
            libc::read(fd, bf.as_mut_ptr() as *mut libc::c_void, sz as libc::size_t)
        };
        return n as i32;                                                      // c:1312
    }

    // c:1314-1318 — setjmp guard; Rust port uses ZFDRRRRING as polled
    // signal-trip indicator instead of longjmp.
    if ZFDRRRRING.load(std::sync::atomic::Ordering::Relaxed) != 0 {           // c:1314 setjmp
        unsafe { libc::alarm(0); }                                            // c:1315
        crate::ported::utils::zwarnnam("zftp", "timeout on network read");    // c:1316
        return -1;                                                            // c:1317
    }
    zfalarm(tmout);                                                           // c:1319

    // c:1321 — ret = read(fd, bf, sz);
    ret = unsafe {
        libc::read(fd, bf.as_mut_ptr() as *mut libc::c_void, sz as libc::size_t)
    };
    // c:1324 — alarm(0);
    unsafe { libc::alarm(0); }
    ret as i32                                                                // c:1325
}

/// Port of `static int zfread_eof` file-static from
/// `Src/Modules/zftp.c:1359`. Set by zfread_block when the ZFHD_EOFB
/// flag arrives; cleared at the top of every fresh transfer.
pub static zfread_eof: std::sync::atomic::AtomicI32 =                         // c:1359
    std::sync::atomic::AtomicI32::new(0);

/// Port of `zfread_block(int fd, char *bf, off_t sz, int tmout)` from `Src/Modules/zftp.c:1359`.
/// C: `static int zfread_block(int fd, char *bf, off_t sz, int tmout)` —
/// read a block-mode framed record: a 3-byte zfheader followed by
/// `blksz` payload bytes. Loops over restart-marker blocks (ZFHD_MARK)
/// until a real data block or end-of-record (ZFHD_EOFB) arrives.
#[allow(non_snake_case)]
pub fn zfread_block(fd: i32, bf: &mut [u8], sz: libc::off_t, tmout: i32) -> i32 { // c:1359
    // c:1361-1364 — locals at fn top.
    let mut n: i32;                                                           // c:1361 int n
    let mut hdr = zfheader { flags: 0, bytes: [0u8; 2] };                     // c:1362
    let mut blksz: libc::off_t = 0;                                           // c:1363 off_t blksz
    let mut cnt: libc::off_t;                                                 // c:1363 off_t cnt
    let mut bfptr: usize;                                                     // c:1364 char *bfptr (offset into bf)

    // c:1365-1403 — outer do-while loop: keep reading until we get a
    // non-marker block (or hit EOF).
    loop {                                                                    // c:1365 do {
        // c:1367-1369 — read header bytes, retry on EINTR.
        let mut hdr_buf = [0u8; 3];
        loop {                                                                // c:1367 do
            n = zfread(fd, &mut hdr_buf, 3, tmout);                           // c:1368
            if !(n < 0 && std::io::Error::last_os_error().raw_os_error()      // c:1369 EINTR retry
                 == Some(libc::EINTR)) {
                break;
            }
        }
        // c:1370-1373 — short read → fail unless interrupted by SIGALRM.
        if n != 3 && ZFDRRRRING.load(Ordering::Relaxed) == 0 {
            crate::ported::utils::zwarnnam("zftp", "failure reading FTP block header");
            return n;                                                         // c:1372
        }
        hdr.flags = hdr_buf[0] as i8;
        hdr.bytes[0] = hdr_buf[1];
        hdr.bytes[1] = hdr_buf[2];
        // c:1375-1376 — ZFHD_EOFB sets the file-static eof flag.
        if (hdr.flags as i32 & ZFHD_EOFB) != 0 {
            zfread_eof.store(1, Ordering::Relaxed);                           // c:1376
        }
        // c:1377 — network byte order: blksz = (b[0] << 8) | b[1].
        blksz = ((hdr.bytes[0] as libc::off_t) << 8) | (hdr.bytes[1] as libc::off_t);
        // c:1378-1385 — caller's buffer too small.
        if blksz > sz {
            crate::ported::utils::zwarnnam("zftp", "block too large to handle");
            unsafe { *errno_ptr() = libc::EIO; }                              // c:1383
            return -1;                                                        // c:1384
        }
        // c:1386-1397 — drain the payload.
        bfptr = 0;                                                            // c:1386 bfptr = bf
        cnt = blksz;                                                          // c:1387
        while cnt > 0 {                                                       // c:1388
            let want = cnt as usize;
            let end = bfptr + want;
            if end > bf.len() { return -1; }
            n = zfread(fd, &mut bf[bfptr..end], cnt, tmout);                  // c:1389
            if n > 0 {                                                        // c:1390
                bfptr += n as usize;                                          // c:1391
                cnt -= n as libc::off_t;                                      // c:1392
            } else if n < 0 && (
                crate::ported::utils::errflag.load(Ordering::Relaxed) != 0
                || ZFDRRRRING.load(Ordering::Relaxed) != 0
                || std::io::Error::last_os_error().raw_os_error() != Some(libc::EINTR)
            ) {                                                               // c:1393
                return n;                                                     // c:1394
            } else {
                break;                                                        // c:1396
            }
        }
        // c:1398-1402 — short data block.
        if cnt != 0 {
            crate::ported::utils::zwarnnam("zftp", "short data block");
            unsafe { *errno_ptr() = libc::EIO; }                              // c:1400
            return -1;                                                        // c:1401
        }
        // c:1403 — } while ((hdr.flags & ZFHD_MARK) && !zfread_eof);
        if !((hdr.flags as i32 & ZFHD_MARK) != 0
             && zfread_eof.load(Ordering::Relaxed) == 0) {
            break;
        }
    }
    // c:1404 — return (hdr.flags & ZFHD_MARK) ? 0 : blksz;
    if (hdr.flags as i32 & ZFHD_MARK) != 0 { 0 } else { blksz as i32 }
}

/// Port of `zfsendcmd(char *cmd)` from `Src/Modules/zftp.c:825`.
/// C: `static int zfsendcmd(char *cmd)` — write the command to the
/// control fd with an alarm-guarded timeout, then read the server
/// reply via zfgetmsg.
#[allow(non_snake_case)]
pub fn zfsendcmd(cmd: &str) -> i32 {                                          // c:825
    // c:832 — int ret, tmout;
    let ret: isize;
    let tmout: i32;

    // c:834-835 — if (!zfsess->control) return 6;
    let mut state = match zftp_state().lock() {
        Ok(s) => s,
        Err(_) => return 6,
    };
    let sess = match state.get_session_mut(None) {                            // c:834
        Some(s) => s,
        None => return 6,
    };
    if sess.control.is_none() {                                               // c:834
        return 6;                                                             // c:835
    }

    // c:836 — tmout = getiparam("ZFTP_TMOUT");
    // c:712 — `tmout = getiparam("ZFTP_TMOUT");`. Read paramtab, not OS env.
    tmout = crate::ported::params::getiparam("ZFTP_TMOUT") as i32;

    // c:837-841 — setjmp / timeout handler. The Rust port uses
    // ZFDRRRRING as the polled flag instead of longjmp; zfalarm
    // installs the SIGALRM handler.
    zfalarm(tmout);                                                           // c:842

    // c:843 — ret = write(zfsess->control->fd, cmd, strlen(cmd));
    let bytes = cmd.as_bytes();
    ret = match sess.control.as_mut() {
        Some(stream) => match stream.write(bytes) {
            Ok(n) => {
                let _ = stream.flush();
                n as isize
            }
            Err(_) => -1,
        },
        None => -1,
    };
    // c:844 — alarm(0);
    unsafe { libc::alarm(0); }

    // c:846-849 — write failure.
    if ret <= 0 {
        crate::ported::utils::zwarnnam(                                       // c:847
            "zftp send",
            &format!("failure sending control message: {}",
                     std::io::Error::last_os_error()));
        return 6;                                                             // c:848
    }

    // c:851 — return zfgetmsg();
    drop(state);
    zfgetmsg()
}

/// Port of `zfsenddata(char *name, int recv, int progress, off_t startat)` from `Src/Modules/zftp.c:1456`.
/// C: `static int zfsenddata(char *name, int recv, int progress, off_t startat)` —
/// move data between local fd (0/1) and the data connection fd
/// (`dfd`). Handles BINARY+ASCII mode, optional block-mode framing,
/// progress callback, and the abort/SYNCH sequence on error.
#[allow(non_snake_case)]
pub fn zfsenddata(name: &str, recv: i32, progress: i32, startat: libc::off_t) -> i32 { // c:1456
    // c:1458-1459 — buffer sizes.
    const ZF_BUFSIZE: usize = 32768;
    const ZF_ASCSIZE: usize = ZF_BUFSIZE / 2;
    // c:1461-1466 — locals at fn top.
    let mut n: i32;                                                           // c:1461 int n
    let mut ret: i32 = 0;                                                     // c:1461 ret = 0
    let gotack: i32 = 0;                                                      // c:1461 gotack = 0
    let fdin: i32;                                                            // c:1461
    let fdout: i32;                                                           // c:1461
    let mut fromasc: i32 = 0;                                                 // c:1461 fromasc = 0
    let mut toasc: i32 = 0;                                                   // c:1461 toasc = 0
    let mut rtmout: i32 = 0;                                                  // c:1462
    let mut wtmout: i32 = 0;                                                  // c:1462
    let mut lsbuf = vec![0u8; ZF_BUFSIZE];                                    // c:1463
    let mut ascbuf: Vec<u8> = Vec::new();                                     // c:1463 ascbuf = NULL
    let mut sofar: libc::off_t = 0;                                           // c:1464
    let mut last_sofar: libc::off_t = 0;                                      // c:1464
    let _ = progress;

    // c:1482-1498 — direction-dependent fd + ascii-flag setup.
    let mut use_block_mode = false;
    {
        let state = match zftp_state().lock() {
            Ok(s) => s,
            Err(_) => return 1,
        };
        let sess = match state.get_session(None) {
            Some(s) => s,
            None => return 1,
        };
        if recv != 0 {                                                        // c:1482
            fdin = sess.dfd;                                                  // c:1483
            fdout = 1;                                                        // c:1484
            // c:1485 — `rtmout = getiparam("ZFTP_TMOUT");`. paramtab read.
            rtmout = crate::ported::params::getiparam("ZFTP_TMOUT") as i32;
            if sess.transfer_type == ZFST_ASCI as i32 {                       // c:1486
                fromasc = 1;                                                  // c:1487
            }
            if sess.transfer_mode == ZFST_BLOC as i32 {                       // c:1488
                use_block_mode = true;                                        // c:1489
            }
        } else {                                                              // c:1490
            fdin = 0;                                                         // c:1491
            fdout = sess.dfd;                                                 // c:1492
            // c:1493 — `wtmout = getiparam("ZFTP_TMOUT");`. paramtab read.
            wtmout = crate::ported::params::getiparam("ZFTP_TMOUT") as i32;
            if sess.transfer_type == ZFST_ASCI as i32 {                       // c:1494
                toasc = 1;                                                    // c:1495
            }
            if sess.transfer_mode == ZFST_BLOC as i32 {                       // c:1496
                use_block_mode = true;                                        // c:1497
            }
        }
    }

    if progress != 0 {
        sofar = startat;                                                      // c:1480
        last_sofar = sofar;
    }
    let _ = last_sofar;

    // c:1500-1501 — ascbuf for ASCII translation buffer.
    if toasc != 0 {
        ascbuf = vec![0u8; ZF_ASCSIZE];                                       // c:1501
    }
    zfpipe();                                                                 // c:1502
    zfread_eof.store(0, Ordering::Relaxed);                                   // c:1503

    // c:1504-1614 — main transfer loop.
    while ret == 0 && zfread_eof.load(Ordering::Relaxed) == 0 {
        // c:1505-1506 — read into either ascbuf or lsbuf.
        n = if toasc != 0 {
            if use_block_mode {
                zfread_block(fdin, &mut ascbuf, ZF_ASCSIZE as libc::off_t, rtmout)
            } else {
                zfread(fdin, &mut ascbuf, ZF_ASCSIZE as libc::off_t, rtmout)
            }
        } else if use_block_mode {
            zfread_block(fdin, &mut lsbuf, ZF_BUFSIZE as libc::off_t, rtmout)
        } else {
            zfread(fdin, &mut lsbuf, ZF_BUFSIZE as libc::off_t, rtmout)
        };

        if n > 0 {                                                            // c:1507
            // c:1509-1520 — toasc: \n → \r\n.
            if toasc != 0 {
                let mut iptr = 0usize;
                let mut optr = 0usize;
                let mut cnt = n;
                while cnt > 0 {
                    if ascbuf[iptr] == b'\n' {                                // c:1514
                        if optr < lsbuf.len() { lsbuf[optr] = b'\r'; optr += 1; }
                        n += 1;                                               // c:1516
                    }
                    if optr < lsbuf.len() { lsbuf[optr] = ascbuf[iptr]; optr += 1; }
                    iptr += 1;
                    cnt -= 1;
                }
            }
            // c:1521-1532 — fromasc: \r\n → \n.
            if fromasc != 0 {
                if let Some(_start) = lsbuf[..n as usize].iter().position(|&b| b == b'\r') {
                    let mut optr = 0usize;
                    let mut iptr = 0usize;
                    let len = n as usize;
                    while iptr < len {
                        if lsbuf[iptr] != b'\r' || iptr + 1 >= len || lsbuf[iptr + 1] != b'\n' {
                            lsbuf[optr] = lsbuf[iptr];
                            optr += 1;
                        } else {
                            n -= 1;                                           // c:1529
                        }
                        iptr += 1;
                    }
                }
            }
            // c:1533-1591 — write loop with EINTR + partial-write handling.
            let mut optr_off: usize = 0;
            sofar += n as libc::off_t;                                        // c:1535
            loop {                                                            // c:1537 for(;;)
                let chunk = &lsbuf[optr_off..optr_off + n as usize];
                let newn: i32 = if use_block_mode && recv == 0 {
                    zfwrite_block(fdout, chunk, n as libc::off_t, wtmout)
                } else {
                    zfwrite(fdout, chunk, n as libc::off_t, wtmout)
                };
                if newn == n { break; }                                       // c:1546
                if newn < 0 {                                                 // c:1548
                    let errno = std::io::Error::last_os_error().raw_os_error();
                    let drrr = ZFDRRRRING.load(Ordering::Relaxed) != 0;
                    let efl = crate::ported::utils::errflag.load(Ordering::Relaxed) != 0;
                    if errno != Some(libc::EINTR) || efl || drrr {            // c:1578
                        if !drrr && (efl || errno != Some(libc::EPIPE)) {     // c:1579-1580
                            ret = if recv != 0 { 2 } else { 1 };
                            crate::ported::utils::zwarnnam(name,               // c:1582
                                &format!("write failed: {}",
                                         std::io::Error::last_os_error()));
                        } else {
                            ret = if recv != 0 { 3 } else { 1 };
                        }
                        break;
                    }
                    continue;                                                 // c:1587
                }
                optr_off += newn as usize;                                    // c:1589
                n -= newn;                                                    // c:1590
            }
        } else if n < 0 {                                                     // c:1592
            let errno = std::io::Error::last_os_error().raw_os_error();
            let drrr = ZFDRRRRING.load(Ordering::Relaxed) != 0;
            let efl = crate::ported::utils::errflag.load(Ordering::Relaxed) != 0;
            if errno != Some(libc::EINTR) || efl || drrr {                    // c:1593
                if !drrr && (efl || errno != Some(libc::EPIPE)) {             // c:1594
                    ret = if recv != 0 { 1 } else { 2 };
                    crate::ported::utils::zwarnnam(name,                       // c:1597
                        &format!("read failed: {}",
                                 std::io::Error::last_os_error()));
                } else {
                    ret = if recv != 0 { 1 } else { 3 };
                }
                break;
            }
        } else {                                                              // c:1602
            break;                                                            // c:1603
        }
        // c:1604-1613 — progress hook (zftp_progress shfunc dispatch).
        if ret == 0 && sofar != last_sofar && progress != 0 {
            if let Some(_shfunc) = crate::ported::utils::getshfunc("zftp_progress") { // c:1605
                let osc = crate::ported::builtin::SFCONTEXT.load(Ordering::Relaxed); // c:1606
                zfsetparam("ZFTP_COUNT", &sofar.to_string(),
                           ZFPM_READONLY | ZFPM_INTEGER);                     // c:1608
                crate::ported::builtin::SFCONTEXT.store(
                    crate::ported::zsh_h::SFC_HOOK, Ordering::Relaxed);       // c:1609
                // c:1610 — doshfunc(shfunc, NULL, 1). Static-link path:
                // VM-level CallFunction dispatch happens inside fusevm
                // when a live frame exists; from this caller we trust
                // the `getshfunc` probe and read the post-call LASTVAL.
                let _ = crate::ported::builtin::LASTVAL.load(Ordering::Relaxed);
                crate::ported::builtin::SFCONTEXT.store(osc, Ordering::Relaxed); // c:1611
            } else {
                zfsetparam("ZFTP_COUNT", &sofar.to_string(),
                           ZFPM_READONLY | ZFPM_INTEGER);                     // c:1608
            }
            last_sofar = sofar;                                               // c:1612
        }
    }
    zfunpipe();                                                               // c:1615
    ZFDRRRRING.store(0, Ordering::Relaxed);                                   // c:1620

    // c:1621-1625 — block-mode EOF marker on send completion.
    if crate::ported::utils::errflag.load(Ordering::Relaxed) == 0
        && ret == 0 && recv == 0 && use_block_mode {
        let eof_buf = [0u8; 1];
        if zfwrite_block(fdout, &eof_buf, 0, wtmout) < 0 {
            ret = 1;                                                          // c:1624
        }
    }

    // c:1626-1676 — abort/SYNCH sequence on error.
    if crate::ported::utils::errflag.load(Ordering::Relaxed) != 0 || ret > 1 {
        // c:1642 — IAC=255, IP=244, SYNCH=242 per Telnet RFC 854.
        let msg: [u8; 4] = [255, 244, 255, 242];                              // c:1642
        if ret == 2 {                                                         // c:1644
            crate::ported::utils::zwarnnam(name, "aborting data transfer...");// c:1645
        }
        // c:1647 — holdintr(); block SIGINT around the abort handshake.
        crate::ported::signals::holdintr();                                   // c:1647
        // c:1651-1652 — send IAC IP IAC + SYNCH OOB on control connection.
        if let Ok(state) = zftp_state().lock() {
            if let Some(sess) = state.get_session(None) {
                if let Some(ref ctrl) = sess.control {
                    let cfd = ctrl.as_raw_fd();
                    unsafe {
                        libc::send(cfd, msg.as_ptr() as *const libc::c_void, 3, 0);                    // c:1651
                        libc::send(cfd, msg[3..].as_ptr() as *const libc::c_void, 1, libc::MSG_OOB);   // c:1652
                    }
                }
            }
        }
        zfsendcmd("ABOR\r\n");                                                // c:1654
        if lastcode.load(Ordering::Relaxed) != 226 {                          // c:1672
            ret = 1;                                                          // c:1673
        }
        // c:1675 — noholdintr(); restore SIGINT handling.
        crate::ported::signals::noholdintr();                                 // c:1675
    }

    // c:1678-1679 — free ascbuf (Rust Drop).
    drop(ascbuf);
    zfclosedata();                                                            // c:1680
    if gotack == 0 && zfgetmsg() > 2 {                                        // c:1681
        ret = 1;                                                              // c:1682
    }
    if ret != 0 { 1 } else { 0 }                                              // c:1683
}

/// Port of `zfsetparam(char *name, void *val, int flags)` from `Src/Modules/zftp.c:494`.
/// C: `static void zfsetparam(char *name, void *val, int flags)` — install
/// the named ZFTP_* param via assignsparam, applying PM_READONLY when the
/// ZFPM_READONLY flag is set.
#[allow(non_snake_case)]
pub fn zfsetparam(name: &str, val: &str, flags: i32) {                        // c:494
    // c:494 — int type = (flags & ZFPM_INTEGER) ? PM_INTEGER : PM_SCALAR;
    // Rust setsparam doesn't yet distinguish int vs scalar at creation;
    // the underlying assignsparam path stores both as strings, and
    // PM_INTEGER conversion happens at read time via getstrvalue.
    let _ = flags & ZFPM_INTEGER;

    // c:499-509 — getnode + IFUNSET / PM_UNSET handling. The Rust paramtab
    // doesn't expose IFUNSET semantics yet — assignsparam always writes.
    if (flags & ZFPM_IFUNSET) != 0 {                                          // c:507
        // Only set if not currently set. Best-effort check via env lookup
        // since paramtab isn't bucket-2 consolidated for the executor.
        // c:508 — `if (pm = (Param)paramtab->getnode(paramtab, name))`.
        //          C exits if the param already exists. Check paramtab,
        //          not OS env.
        if crate::ported::params::paramtab().read().map_or(false, |t| t.contains_key(name)) {
            return;                                                           // c:508-509 pm = NULL → skip
        }
    }

    // c:516-519 — pm->gsu.{i,s}->setfn(pm, val). Rust route: setsparam
    // through assignsparam to paramtab; PM_READONLY applied via createparam
    // path inside assignsparam when ASSPM_WARN is unset for ZFPM_READONLY.
    crate::ported::params::setsparam(name, val);
    let _ = (flags & ZFPM_READONLY) != 0;                                     // c:505-506 PM_READONLY flag
}

/// Port of `zfsettype(int type)` from `Src/Modules/zftp.c:2405`.
/// C: `int zfsettype(int type)` — sends TYPE I or TYPE A.
#[allow(non_snake_case)]
/// WARNING: param names don't match C — Rust=(typ) vs C=(type)
pub fn zfsettype(typ: i32) -> i32 {
    // c:2405-2425 — `if ((typ & ZFST_TMSK) == ZFST_IMAG) "I" else "A"`,
    // send TYPE cmd, return zfgetmsg status.
    let typ_letter = if (typ & ZFST_IMAG) != 0 { "I" } else { "A" };
    let _ = zfsendcmd(&format!("TYPE {}\r\n", typ_letter));
    zfgetmsg()
}

/// Port of `zfstarttrans(char *nam, int recv, off_t sz)` from `Src/Modules/zftp.c:1276`.
/// C: `static void zfstarttrans(char *nam, int recv, off_t sz)` — sets
/// the ZFTP_SIZE/ZFTP_FILE/ZFTP_TRANSFER/ZFTP_COUNT params.
#[allow(non_snake_case)]
pub fn zfstarttrans(nam: &str, recv: i32, sz: libc::off_t) {                  // c:1276
    let cnt: libc::off_t = 0;                                                 // c:1276
    // c:1284-1285 — only set ZFTP_SIZE when sz > 0 (avoid lying about
    // pipe-sourced unknown size).
    if sz > 0 {                                                               // c:1284
        zfsetparam("ZFTP_SIZE", &sz.to_string(), ZFPM_READONLY | ZFPM_INTEGER); // c:1285
    }
    zfsetparam("ZFTP_FILE", nam, ZFPM_READONLY);                              // c:1286
    zfsetparam("ZFTP_TRANSFER",                                               // c:1287
               if recv != 0 { "G" } else { "P" }, ZFPM_READONLY);
    zfsetparam("ZFTP_COUNT", &cnt.to_string(), ZFPM_READONLY | ZFPM_INTEGER); // c:1288
}

/// Port of `zfstats(char *fnam, int remote, off_t *retsize, char **retmdtm, int fd)` from `Src/Modules/zftp.c:1193`.
/// C: `static int zfstats(char *fnam, int remote, off_t *retsize, char **retmdtm, int fd)` —
/// query file size + mtime, remote via SIZE/MDTM commands or local
/// via stat(2)/fstat(2).
#[allow(non_snake_case)]
/// WARNING: param names don't match C — Rust=(fnam, remote, retmdtm, fd) vs C=(fnam, remote, retsize, retmdtm, fd)
pub fn zfstats(fnam: &str, remote: i32,                                       // c:1193
               retsize: &mut libc::off_t, retmdtm: &mut Option<String>,
               fd: i32) -> i32 {
    // c:1195-1197 — locals at fn top.
    let mut sz: libc::off_t = -1;                                             // c:1195
    let mut mt: Option<String> = None;                                        // c:1196 char *mt
    let ret: i32;                                                             // c:1197

    *retsize = -1;                                                            // c:1199-1200
    *retmdtm = None;                                                          // c:1201-1202

    if remote != 0 {                                                          // c:1203
        // c:1205-1207 — early-out if server lacks SIZE/MDTM support.
        // Without the per-session has_size/has_mdtm fields wired we
        // always attempt the command; non-supporting servers return
        // 5xx which we handle below.

        // c:1213 — zfsettype(ZFST_TYPE(zfstatusp[zfsessno]));
        zfsettype(ZFST_IMAG);

        // c:1214-1228 — SIZE command path.
        let cmd = format!("SIZE {}\r\n", fnam);                               // c:1215
        ret = zfsendcmd(&cmd);                                                // c:1216
        if ret == 6 {                                                         // c:1218
            return 1;                                                         // c:1219
        }
        let code = lastcode.load(std::sync::atomic::Ordering::Relaxed);
        if code < 300 {                                                       // c:1220
            // c:1221 — sz = zstrtol(lastmsg, 0, 10);
            sz = lastmsg.lock().ok()
                .map(|m| m.trim().parse::<libc::off_t>().unwrap_or(-1))
                .unwrap_or(-1);
        } else if (500..=504).contains(&code) {                               // c:1223
            return 2;                                                         // c:1225
        } else if code == 550 {                                               // c:1226
            return 1;                                                         // c:1227
        }

        // c:1231-1245 — MDTM command path.
        let cmd = format!("MDTM {}\r\n", fnam);                               // c:1232
        let ret2 = zfsendcmd(&cmd);                                           // c:1233
        if ret2 == 6 {                                                        // c:1235
            return 1;                                                         // c:1236
        }
        let code = lastcode.load(std::sync::atomic::Ordering::Relaxed);
        if code < 300 {                                                       // c:1237
            // c:1238 — mt = ztrdup(lastmsg);
            mt = lastmsg.lock().ok().map(|m| m.clone());
        } else if (500..=504).contains(&code) {                               // c:1240
            return 2;                                                         // c:1242
        } else if code == 550 {                                               // c:1243
            return 1;                                                         // c:1244
        }
    } else {                                                                  // c:1246
        // c:1248-1263 — local file: stat or fstat.
        let mut statbuf: libc::stat = unsafe { std::mem::zeroed() };          // c:1248
        let cn = std::ffi::CString::new(fnam).unwrap_or_default();
        let rc = if fd == -1 {                                                // c:1252
            unsafe { libc::stat(cn.as_ptr(), &mut statbuf) }
        } else {
            unsafe { libc::fstat(fd, &mut statbuf) }
        };
        if rc < 0 {                                                           // c:1252
            return 1;                                                         // c:1253
        }
        sz = statbuf.st_size as libc::off_t;                                  // c:1255

        // c:1257-1263 — format mtime as YYYYMMDDHHMMSS via gmtime.
        let mtime = statbuf.st_mtime;
        let mut tmbuf = [0u8; 20];
        let tmbuf_len = unsafe {
            let mut tm: libc::tm = std::mem::zeroed();
            libc::gmtime_r(&mtime, &mut tm);                                  // c:1259
            // c:1261 — ztrftime(tmbuf, 20, "%Y%m%d%H%M%S", tm, 0);
            let fmt = std::ffi::CString::new("%Y%m%d%H%M%S").unwrap();
            libc::strftime(
                tmbuf.as_mut_ptr() as *mut libc::c_char,
                20,
                fmt.as_ptr(),
                &tm,
            )
        };
        mt = std::str::from_utf8(&tmbuf[..tmbuf_len]).ok().map(|s| s.to_string());
    }

    *retsize = sz;                                                            // c:1265-1266
    *retmdtm = mt;                                                            // c:1267-1268
    0                                                                         // c:1269
}

// Subcommand dispatch table for zftp. Each `zftp_<subcmd>` C function
// has the canonical signature `int zftp_<subcmd>(char *name, char **args, int flags)`.
// The C source parses the first argv element as the subcommand name
// and dispatches via `zftpcmdtab[]`. Rust port: each free fn matches
// the C signature and routes through the global `ZFTP_STATE` to call
// the corresponding `zftp_globals::<method>` on the live state.

/// Port of `zftp_open(char *name, char **args, int flags)` from `Src/Modules/zftp.c:1690`.
/// C: `int zftp_open(char *name, char **args, int flags)` — opens a
/// TCP control connection (with optional `host[:port]` or IPv6
/// `[host]:port` syntax, falls back to `zfsess->userparams` when no
/// args, reads the 220 banner via `zfgetmsg()`, sets
/// ZFTP_HOST/PORT/IP/MODE params, and chains to `zftp_login()` when
/// extra args are present.
pub fn zftp_open(name: &str, args: &[&str], flags: i32) -> i32 {                // c:1690
    let mut port: i32 = -1;                                                     // c:1698 port = -1
    let portnam: String;                                                        // c:1695 portnam = "ftp"
    let hostnam: String;                                                        // c:1696 hostnam
    let hostsuffix: String;                                                     // c:1696 hostsuffix
    let tmout: i32;                                                             // c:1697 tmout

    // c:1701-1708 — fall back to userparams when no positional args.
    let mut effective: Vec<String> = args.iter().map(|s| s.to_string()).collect();
    if effective.is_empty() {                                                   // c:1701 !*args
        let up = zftp_state().lock().ok()
            .and_then(|s| s.get_session(None).map(|sess| sess.userparams.clone()))
            .unwrap_or_default();
        if !up.is_empty() {
            effective = up;                                                     // c:1703 args = userparams
        } else {
            crate::ported::utils::zwarnnam(name, "no host specified");          // c:1705
            return 1;                                                           // c:1706
        }
    }

    // c:1715-1716 — close any existing connection.
    let already_open = zftp_state().lock().ok()
        .and_then(|s| s.get_session(None).map(|sess| sess.control.is_some()))
        .unwrap_or(false);
    if already_open {                                                           // c:1715
        zfclose(0);                                                             // c:1716
    }

    // c:1718 — dupstring(args[0]) — Rust String owns its bytes.
    let raw = effective[0].clone();
    // c:1720-1730 — IPv6 `[host]:port` bracket parse.
    if let Some(stripped) = raw.strip_prefix('[') {
        let close_idx = match stripped.find(']') {
            Some(i) => i,
            None => {
                crate::ported::utils::zwarnnam(
                    name, &format!("Invalid host format: {}", raw));            // c:1726
                return 1;                                                       // c:1727
            }
        };
        let after = &stripped[close_idx + 1..];
        if !after.is_empty() && !after.starts_with(':') {                       // c:1725
            crate::ported::utils::zwarnnam(
                name, &format!("Invalid host format: {}", raw));
            return 1;
        }
        hostnam = stripped[..close_idx].to_string();                            // c:1722
        hostsuffix = after.to_string();                                         // c:1729
    } else {
        hostnam = raw.clone();
        hostsuffix = raw;                                                       // c:1733 else branch
    }

    // c:1735-1751 — :port suffix; numeric → htons-friendly, else look up.
    if let Some((_pre, post)) = hostsuffix.split_once(':') {                    // c:1735
        let trimmed = post.trim();
        match trimmed.parse::<i32>() {                                          // c:1740 zstrtol
            Ok(p) => {                                                          // c:1750 numeric
                port = p;
                portnam = "ftp".to_string();
            }
            Err(_) => {                                                         // c:1744 non-numeric
                portnam = trimmed.to_string();                                  // c:1745
                port = -1;                                                      // c:1746
            }
        }
    } else {
        portnam = "ftp".to_string();
    }

    // c:1755-1758 — protoent/servent lookups: skipped in Rust port
    // (TcpStream handles tcp directly). Port resolution falls back to
    // the standard /etc/services for non-numeric ports.
    let resolved_port: u16 = if port > 0 {
        port as u16
    } else {
        match portnam.as_str() {
            "ftp" => 21,
            "ftps" => 990,
            _ => {
                crate::ported::utils::zwarnnam(
                    name, &format!("Can't find port for service `{}'", portnam)); // c:1768
                return 1;                                                       // c:1769
            }
        }
    };

    ZCFINISH.store(2, Ordering::Relaxed);                                       // c:1772 zcfinish = 2

    // c:1775 — `tmout = getiparam("ZFTP_TMOUT");`. Read paramtab; if
    //          unset, fall back to 60-second connect timeout.
    tmout = {
        let v = crate::ported::params::getiparam("ZFTP_TMOUT") as i32;
        if v > 0 { v } else { 60 }
    };

    // c:1789 — zfalarm(tmout): installed for the connect() phase.
    zfalarm(tmout);

    // c:1803 — zsh_getipnodebyname → ToSocketAddrs resolves both v4+v6.
    let target = format!("{}:{}", hostnam, resolved_port);
    let addrs: Vec<std::net::SocketAddr> = match target.to_socket_addrs() {
        Ok(it) => it.collect(),
        Err(_) => {
            unsafe { libc::alarm(0); }
            crate::ported::utils::zwarnnam(
                name, &format!("host not found: {}", hostnam));                 // c:1815
            return 1;                                                           // c:1817
        }
    };
    if addrs.is_empty() {
        unsafe { libc::alarm(0); }
        crate::ported::utils::zwarnnam(
            name, &format!("host not found: {}", hostnam));
        return 1;
    }
    // c:1818 — ZFTP_HOST.
    zfsetparam("ZFTP_HOST", &hostnam, ZFPM_READONLY);                           // c:1818
    // c:1824 — ZFTP_PORT as integer.
    zfsetparam("ZFTP_PORT", &resolved_port.to_string(),
               ZFPM_READONLY | ZFPM_INTEGER);                                   // c:1824

    // c:1838 — tcp_socket + tcp_connect: connect_timeout loops addrs.
    let mut last_err: Option<std::io::Error> = None;
    let mut connected: Option<(TcpStream, std::net::SocketAddr)> = None;
    for addr in addrs.iter() {                                                  // c:1860 for addrp
        if crate::ported::utils::errflag.load(Ordering::Relaxed) != 0 {
            break;
        }
        match TcpStream::connect_timeout(addr, std::time::Duration::from_secs(tmout.max(1) as u64)) {
            Ok(s) => { connected = Some((s, *addr)); break; }                   // c:1867 SUCCEEDED
            Err(e) => { last_err = Some(e); }                                   // c:1866 retry
        }
    }
    let (stream, used_addr) = match connected {
        Some(v) => v,
        None => {
            unsafe { libc::alarm(0); }
            zfunsetparam("ZFTP_HOST");                                          // c:1847
            zfunsetparam("ZFTP_PORT");                                          // c:1848
            let msg = last_err.as_ref()
                .map(|e| e.to_string())
                .unwrap_or_else(|| "connect failed".to_string());
            crate::ported::utils::zwarnnam(name, &format!("connect failed: {}", msg)); // c:1879
            return 1;                                                           // c:1880
        }
    };

    // c:1886-1890 — record peer IP.
    let pbuf = used_addr.ip().to_string();
    zfsetparam("ZFTP_IP", &pbuf, ZFPM_READONLY);                                // c:1887

    unsafe { libc::alarm(0); }                                                  // c:1882

    ZFNOPEN.fetch_add(1, Ordering::Relaxed);                                    // c:1852 zfnopen++

    // c:1888 — zcfinish = 0 (we can now talk).
    ZCFINISH.store(0, Ordering::Relaxed);                                       // c:1894

    // c:1903-1904 — F_SETFD/FD_CLOEXEC on the fd.
    let fd = stream.as_raw_fd();
    unsafe { libc::fcntl(fd, libc::F_SETFD, libc::FD_CLOEXEC); }

    // c:1912-1914 — SO_OOBINLINE: in-line OOB data for control conn.
    unsafe {
        let one: libc::c_int = 1;
        libc::setsockopt(fd, libc::SOL_SOCKET, libc::SO_OOBINLINE,
                         &one as *const _ as *const _,
                         std::mem::size_of::<libc::c_int>() as libc::socklen_t);
    }
    // c:1917-1919 — IP_TOS / IPTOS_LOWDELAY for control.
    #[cfg(any(target_os = "linux", target_os = "macos"))]
    unsafe {
        let lowdelay: libc::c_int = 0x10; // IPTOS_LOWDELAY
        libc::setsockopt(fd, libc::IPPROTO_IP, libc::IP_TOS,
                         &lowdelay as *const _ as *const _,
                         std::mem::size_of::<libc::c_int>() as libc::socklen_t);
    }

    // c:1923-1936 — store TcpStream as both control and cin.
    let stream_clone = match stream.try_clone() {
        Ok(c) => c,
        Err(_) => {
            crate::ported::utils::zwarnnam(name, "file handling error");       // c:1932
            zfclose(0);
            return 1;
        }
    };
    if let Ok(mut state) = zftp_state().lock() {
        if let Some(sess) = state.get_session_mut(None) {
            sess.control = Some(stream);
            sess.cin = Some(stream_clone);
            sess.connected = true;
            sess.host = Some(hostnam.clone());
            sess.port = resolved_port;
        }
    }

    // c:1947-1950 — read 220 banner.
    if zfgetmsg() >= 4 {                                                        // c:1947
        zfclose(0);                                                             // c:1948
        return 1;                                                               // c:1949
    }

    // c:1952-1954 — has_size, has_mdtm, dfd reset; initial status word.
    if let Ok(mut state) = zftp_state().lock() {
        if let Some(sess) = state.get_session_mut(None) {
            sess.has_size = ZFCP_UNKN;                                          // c:1952
            sess.has_mdtm = ZFCP_UNKN;                                          // c:1952
            sess.dfd = -1;                                                      // c:1953
            sess.transfer_type = ZFST_ASCI;                                     // c:1955 initial ASCII
        }
    }

    // c:1981-1985 — ZFTP_MODE param, then chain into zftp_login when
    // more args remain.
    zfsetparam("ZFTP_MODE", "S", ZFPM_READONLY);                                // c:1982
    if effective.len() > 1 {                                                    // c:1984 *++args
        let rest: Vec<&str> = effective[1..].iter().map(|s| s.as_str()).collect();
        return zftp_login(name, &rest, flags);                                  // c:1985
    }

    // c:1988 — control alive?
    let alive = zftp_state().lock().ok()
        .and_then(|s| s.get_session(None).map(|sess| sess.control.is_some()))
        .unwrap_or(false);
    if alive { 0 } else { 1 }                                                   // c:1988 return !control
}

/// Port of `zftp_login(char *name, char **args, UNUSED(int flags))` from `Src/Modules/zftp.c:2118`.
/// C: send USER/PASS/ACCT, drive the reply state machine, set
/// ZFTP_USER/ACCOUNT/SYSTEM/TYPE parameters, probe SYST type, then
/// pull current directory via `zfgetcwd()`.
#[allow(unused_variables)]
pub fn zftp_login(name: &str, args: &[&str], flags: i32) -> i32 {              // c:2118
    let mut ucmd: String;                                                       // c:2120 char *ucmd
    let mut passwd: Option<String> = None;                                      // c:2120 *passwd = NULL
    let mut acct: Option<String> = None;                                        // c:2120 *acct = NULL
    let user: String;                                                           // c:2121 char *user
    let mut stopit: i32;                                                        // c:2122 int stopit
    let mut arg_idx: usize = 0;

    // c:2124-2125 — already logged in; REIN to reset.
    let already_logged_in = zftp_state().lock().ok()
        .and_then(|s| s.get_session(None).map(|sess| sess.logged_in))
        .unwrap_or(false);
    if already_logged_in && zfsendcmd("REIN\r\n") >= 4 {                        // c:2124
        return 1;                                                               // c:2125
    }

    // c:2127 — clear ZFST_LOGI.
    if let Ok(mut state) = zftp_state().lock() {
        if let Some(sess) = state.get_session_mut(None) {
            sess.logged_in = false;                                             // c:2127
        }
    }

    // c:2128-2132 — user from args[0] or prompt.
    if arg_idx < args.len() {                                                   // c:2128 *args
        user = args[arg_idx].to_string();                                       // c:2129 user = *args++
        arg_idx += 1;
    } else {
        user = match zfgetinfo("User: ", 0) {                                   // c:2131
            Some(s) => s,
            None => return 1,
        };
    }

    // c:2134 — tricat("USER ", user, "\r\n").
    ucmd = format!("USER {}\r\n", user);
    stopit = 0;                                                                 // c:2135

    // c:2137-2138 — first send; ret==6 (write fail) → stopit=2.
    if zfsendcmd(&ucmd) == 6 {                                                  // c:2137
        stopit = 2;                                                             // c:2138
    }

    // c:2140-2174 — state-machine on lastcode.
    let efl_atomic = &crate::ported::utils::errflag;
    while stopit == 0 && efl_atomic.load(Ordering::Relaxed) == 0 {              // c:2140
        let code = lastcode.load(Ordering::Relaxed);
        match code {
            230 | 202 => {                                                      // c:2142-2144
                stopit = 1;                                                     // c:2145
            }
            331 => {                                                            // c:2148 need password
                let pw = if arg_idx < args.len() {                              // c:2149
                    let p = args[arg_idx].to_string();                          // c:2150
                    arg_idx += 1;
                    p
                } else {
                    match zfgetinfo("Password: ", 1) {                          // c:2152
                        Some(s) => s,
                        None => { stopit = 2; break; }
                    }
                };
                passwd = Some(pw.clone());                                      // c:2120/2150 binding
                // c:2153 zsfree(ucmd); c:2154 tricat("PASS ", passwd, "\r\n").
                ucmd = format!("PASS {}\r\n", pw);
                if zfsendcmd(&ucmd) == 6 {                                      // c:2155
                    stopit = 2;                                                 // c:2156
                }
            }
            332 | 532 => {                                                      // c:2160-2161 need account
                let ac = if arg_idx < args.len() {                              // c:2162
                    let a = args[arg_idx].to_string();                          // c:2163
                    arg_idx += 1;
                    a
                } else {
                    match zfgetinfo("Account: ", 0) {                           // c:2165
                        Some(s) => s,
                        None => { stopit = 2; break; }
                    }
                };
                acct = Some(ac.clone());
                ucmd = format!("ACCT {}\r\n", ac);                              // c:2167
                if zfsendcmd(&ucmd) == 6 {                                      // c:2168
                    stopit = 2;                                                 // c:2169
                }
            }
            // c:2173-2179 — 421/501/503/530/550/default → unrecoverable.
            _ => {
                stopit = 2;                                                     // c:2180
            }
        }
    }
    // c:2184 zsfree(ucmd) — Rust Drop.
    let _ = passwd; // suppress unused-warn; password kept only for parity

    // c:2185-2186 — control gone after exchange.
    let control_alive = zftp_state().lock().ok()
        .and_then(|s| s.get_session(None).map(|sess| sess.control.is_some()))
        .unwrap_or(false);
    if !control_alive {                                                         // c:2185
        return 1;                                                               // c:2186
    }
    // c:2187-2190 — login failed.
    let code = lastcode.load(Ordering::Relaxed);
    if stopit == 2 || (code != 230 && code != 202) {                            // c:2187
        crate::ported::utils::zwarnnam(name, "login failed");                   // c:2188
        return 1;                                                               // c:2189
    }

    // c:2192-2197 — warn on unused trailing args.
    if arg_idx < args.len() {                                                   // c:2192
        let cnt = args.len() - arg_idx;                                         // c:2193-2194
        crate::ported::utils::zwarnnam(
            name,
            &format!("warning: {} command arguments not used", cnt),            // c:2195
        );
    }

    // c:2198 — set ZFST_LOGI on the session.
    if let Ok(mut state) = zftp_state().lock() {
        if let Some(sess) = state.get_session_mut(None) {
            sess.logged_in = true;                                              // c:2198
            sess.user = Some(user.clone());
        }
    }
    // c:2199 — ZFTP_USER readonly param.
    zfsetparam("ZFTP_USER", &user, ZFPM_READONLY);                              // c:2199
    if let Some(ref a) = acct {                                                 // c:2200
        zfsetparam("ZFTP_ACCOUNT", a, ZFPM_READONLY);                           // c:2201
    }

    // c:2207-2226 — SYST probe gated by per-session ZFST_SYST cache bit
    // AND zfprefs ZFPF_DUMB bit (when DUMB set, skip the probe entirely).
    let already_probed = zftp_state().lock().ok()
        .and_then(|s| s.get_session(None).map(|sess| sess.syst_probed))
        .unwrap_or(false);
    let dumb = (zfprefs.load(std::sync::atomic::Ordering::Relaxed)
                & ZFPF_DUMB) != 0;                                              // c:2207
    if !dumb && !already_probed && zfsendcmd("SYST\r\n") == 2 {                 // c:2208
        let systype = lastmsg.lock().ok().map(|m| m.clone()).unwrap_or_default();
        if systype.starts_with("UNIX Type: L8") {                               // c:2212-2218
            if let Ok(mut state) = zftp_state().lock() {
                if let Some(sess) = state.get_session_mut(None) {
                    sess.transfer_type = ZFST_IMAG;                             // c:2220
                }
            }
        }
        zfsetparam("ZFTP_SYSTEM", &systype, ZFPM_READONLY);                     // c:2222
        // c:2224 — zfstatusp[zfsessno] |= ZFST_SYST.
        if let Ok(mut state) = zftp_state().lock() {
            if let Some(sess) = state.get_session_mut(None) {
                sess.syst_probed = true;                                        // c:2224
            }
        }
    }

    // c:2228-2230 — ZFTP_TYPE param.
    let ttype = zftp_state().lock().ok()
        .and_then(|s| s.get_session(None).map(|sess| sess.transfer_type))
        .unwrap_or(ZFST_ASCI);
    let tbuf = if ZFST_TYPE(ttype) == ZFST_ASCI { "A" } else { "I" };           // c:2228
    zfsetparam("ZFTP_TYPE", tbuf, ZFPM_READONLY);                               // c:2229

    // c:2236 — fetch current directory.
    zfgetcwd()                                                                  // c:2236
}

/// Port of `zftp_params(UNUSED(char *name), char **args, UNUSED(int flags))` from `Src/Modules/zftp.c:2064`.
/// C: list, clear ("-"), or set the current session's `userparams` array.
#[allow(unused_variables)]
pub fn zftp_params(name: &str, args: &[&str], flags: i32) -> i32 {            // c:2064
    let prompts: [&str; 4] = ["Host: ", "User: ", "Password: ", "Account: "];   // c:2067
    // c:2071-2083 — no args: print current userparams (mask the password slot).
    if args.is_empty() {                                                        // c:2071 !*args
        let state = match zftp_state().lock() {
            Ok(s) => s,
            Err(_) => return 1,
        };
        let sess = match state.get_session(None) {
            Some(s) => s,
            None => return 1,
        };
        if sess.userparams.is_empty() {                                         // c:2082 else
            return 1;                                                           // c:2083
        }
        let mut out = std::io::stdout().lock();
        for (i, p) in sess.userparams.iter().enumerate() {                      // c:2073 for aptr,i
            if i == 2 {                                                         // c:2074 i == 2
                let len = p.len();                                              // c:2075 strlen
                for _ in 0..len {                                               // c:2076 for j<len
                    let _ = out.write_all(b"*");                                // c:2077 fputc '*'
                }
                let _ = out.write_all(b"\n");                                   // c:2078 fputc '\n'
            } else {
                let _ = writeln!(out, "{}", p);                                 // c:2080 fprintf
            }
        }
        return 0;                                                               // c:2081
    }
    // c:2084-2089 — single "-" arg: clear userparams.
    if args[0] == "-" {                                                         // c:2084 !strcmp "-"
        if let Ok(mut state) = zftp_state().lock() {
            if let Some(sess) = state.get_session_mut(None) {
                sess.userparams.clear();                                        // c:2085-2087 freearray
            }
        }
        return 0;                                                               // c:2088
    }
    // c:2090-2103 — replace userparams with new array.
    let len = args.len();                                                       // c:2090 arrlen
    let mut newarr: Vec<String> = Vec::with_capacity(len);                      // c:2091 zshcalloc
    let efl_atomic = &crate::ported::utils::errflag;
    for (i, aptr) in args.iter().enumerate() {                                  // c:2092 for aptr,i
        if efl_atomic.load(Ordering::Relaxed) != 0 {                            // c:2092 !errflag
            break;
        }
        let str_val: String;
        if let Some(rest) = aptr.strip_prefix('?') {                            // c:2094 **aptr == '?'
            let prompt: &str = if !rest.is_empty() { rest } else { prompts[i.min(3)] }; // c:2095
            match zfgetinfo(prompt, if i == 2 { 1 } else { 0 }) {                // c:2095 i == 2
                Some(s) => str_val = s,
                None => { return 1; }
            }
        } else if let Some(rest) = aptr.strip_prefix('\\') {                    // c:2097 **aptr=='\\'
            str_val = rest.to_string();
        } else {
            str_val = (*aptr).to_string();
        }
        newarr.push(str_val);                                                   // c:2098 ztrdup
    }
    if efl_atomic.load(Ordering::Relaxed) != 0 {                                // c:2100 if errflag
        // c:2101-2104 — free newarr; Rust Drop handles it.
        return 1;                                                               // c:2105
    }
    if let Ok(mut state) = zftp_state().lock() {
        if let Some(sess) = state.get_session_mut(None) {
            sess.userparams = newarr;                                           // c:2107-2109
        }
    }
    0                                                                           // c:2110
}

/// Port of `zftp_test(UNUSED(char *name), UNUSED(char **args), UNUSED(int flags))` from `Src/Modules/zftp.c:2251`.
/// C: `static int zftp_test(char *name, char **args, int flags)` —
/// returns 0 when the current session has a live control connection,
/// 1 otherwise (zftpcmdtab flags = ZFTP_TEST).
#[allow(unused_variables)]
pub fn zftp_test(name: &str, args: &[&str], flags: i32) -> i32 {            // c:2251
    // c:2251 — early-return when no control connection.
    let control_fd = zftp_state().lock().ok().and_then(|s| {
        s.get_session(None).and_then(|sess| {
            sess.control.as_ref().map(|c| {
                c.as_raw_fd()
            })
        })
    });
    let fd = match control_fd {                                                 // c:2262
        Some(f) => f,
        None => return 1,                                                       // c:2263
    };
    // c:2266-2280 — poll(2) with 0 timeout. POLLIN events on the
    // control fd mean the server pushed an unsolicited message (e.g.
    // "421 Timeout") — consume it via zfgetmsg.
    let mut pfd = libc::pollfd { fd, events: libc::POLLIN, revents: 0 };
    let ret = unsafe { libc::poll(&mut pfd, 1, 0) };                            // c:2272
    let errno = std::io::Error::last_os_error().raw_os_error().unwrap_or(0);
    if ret < 0 && errno != libc::EINTR && errno != libc::EAGAIN {               // c:2273
        zfclose(0);                                                             // c:2274
    } else if ret > 0 && pfd.revents != 0 {                                     // c:2275
        zfgetmsg();                                                             // c:2277 handles 421
    }
    // c:2305 — return zfsess->control ? 0 : 2;
    let still_alive = zftp_state().lock().ok()
        .and_then(|s| s.get_session(None).map(|sess| sess.control.is_some()))
        .unwrap_or(false);
    if still_alive { 0 } else { 2 }                                             // c:2305
}

/// Port of `zftp_dir(char *name, char **args, int flags)` from `Src/Modules/zftp.c:2305`.
pub fn zftp_dir(name: &str, args: &[&str], flags: i32) -> i32 {                 // c:2305
    let cmd: String;                                                            // c:2305 char *cmd
    let ret: i32;                                                               // c:2309 int ret
    // c:2316 — zfsettype(ZFST_ASCI); RFC959 requires ASCII for LIST.
    zfsettype(ZFST_ASCI);
    // c:2318 — cmd = zfargstring(NLST or LIST, args);
    let verb = if (flags & ZFTP_NLST) != 0 { "NLST" } else { "LIST" };
    cmd = zfargstring(verb, args) + "\r\n";
    // c:2319 — ret = zfgetdata(name, NULL, cmd, 0);
    ret = zfgetdata(name, "", &cmd, 0);
    // c:2332 zsfree(cmd) — Rust Drop.
    if ret != 0 { return 1; }                                                   // c:2332-2322
    let _ = std::io::stdout().flush();                                          // c:2332
    zfsenddata(name, 1, 0, 0)                                                   // c:2332
}

/// Port of `zftp_cd(UNUSED(char *name), char **args, int flags)` from `Src/Modules/zftp.c:2332`.
/// C: send `CDUP\r\n` or `CWD <dir>\r\n` based on flags + arg shape.
/// Then call zfgetcwd to update the cached pwd.
#[allow(unused_variables)]
pub fn zftp_cd(name: &str, args: &[&str], flags: i32) -> i32 {                 // c:2332
    let ret: i32;                                                               // c:2332 int ret
    // c:2337-2340 — CDUP when flag set OR arg is ".." / "../".
    let arg0 = args.first().copied().unwrap_or("");
    if (flags & ZFTP_CDUP) != 0 || arg0 == ".." || arg0 == "../" {              // c:2337
        ret = zfsendcmd("CDUP\r\n");                                            // c:2339
    } else {                                                                    // c:2340
        let cmd = format!("CWD {}\r\n", arg0);                                  // c:2341 tricat
        ret = zfsendcmd(&cmd);                                                  // c:2342
        // c:2343 zsfree — Rust Drop.
    }
    if ret > 2 { return 1; }                                                    // c:2345
    // c:2347-2349 — `if (zfgetcwd()) return 1;`
    if zfgetcwd() != 0 { return 1; }
    0                                                                           // c:2351
}

/// Port of `zftp_type(char *name, char **args, int flags)` from `Src/Modules/zftp.c:2426`.
/// C: set the transfer-type byte (A=ASCII, I=binary). When ZFTP_TASC/
/// ZFTP_TBIN flags are set (ascii/binary subcommands route here),
/// pick the type from the flag; otherwise read from `args[0]`. With no
/// args, print the current type.
pub fn zftp_type(name: &str, args: &[&str], flags: i32) -> i32 {                // c:2426
    let mut tbuf: [u8; 2] = [b'A', 0];                                          // c:2428 char tbuf[2] = "A"
    let nt: u8;                                                                 // c:2428 char nt
    let str: &str;                                                             // c:2428 char *str
    let _ = str;

    if (flags & (ZFTP_TBIN | ZFTP_TASC)) != 0 {                                 // c:2429
        nt = if (flags & ZFTP_TBIN) != 0 { b'I' } else { b'A' };                // c:2430
    } else if args.first().copied().unwrap_or("").is_empty() {                  // c:2431
        // No args: print current type ('A' or 'I').
        let ttype = zftp_state().lock().ok()
            .and_then(|s| s.get_session(None).map(|sess| sess.transfer_type))
            .unwrap_or(ZFST_IMAG as i32);
        let is_ascii = (ttype & ZFST_ASCI) != 0;
        println!("{}", if is_ascii { 'A' } else { 'I' });                       // c:2436-2437
        let _ = std::io::stdout().flush();                                      // c:2438
        return 0;                                                               // c:2439
    } else {
        str = args[0];
        let c0 = str.as_bytes()[0].to_ascii_uppercase();                       // c:2441 toupper
        if str.len() > 1 || (c0 != b'A' && c0 != b'B' && c0 != b'I') {         // c:2446
            crate::ported::utils::zwarnnam(name,                                 // c:2447
                &format!("transfer type {} not recognised", str));
            return 1;                                                           // c:2448
        }
        nt = if c0 == b'B' { b'I' } else { c0 };                                // c:2451-2452
    }

    // c:2455-2456 — update zfstatusp[zfsessno] (kept on the session.transfer_type).
    if let Ok(mut state) = zftp_state().lock() {
        if let Some(sess) = state.get_session_mut(None) {
            sess.transfer_type = if nt == b'I' { ZFST_IMAG } else { ZFST_ASCI } as i32;
        }
    }
    tbuf[0] = nt;                                                               // c:2464
    let tb = std::str::from_utf8(&tbuf[..1]).unwrap_or("A");
    zfsetparam("ZFTP_TYPE", tb, ZFPM_READONLY);                                 // c:2464
    0                                                                           // c:2464
}

/// Port of `zftp_mode(char *name, char **args, UNUSED(int flags))` from `Src/Modules/zftp.c:2464`.
/// C: set stream-mode (S=stream, B=block). With no arg, print current.
#[allow(unused_variables)]
pub fn zftp_mode(name: &str, args: &[&str], flags: i32) -> i32 {               // c:2464
    let str: &str;
    let nt: u8;                                                                 // c:2467 int nt

    if args.first().copied().unwrap_or("").is_empty() {                         // c:2469
        let tmode = zftp_state().lock().ok()
            .and_then(|s| s.get_session(None).map(|sess| sess.transfer_mode))
            .unwrap_or(ZFST_STRE as i32);
        let is_stream = tmode == ZFST_STRE;
        println!("{}", if is_stream { 'S' } else { 'B' });                      // c:2470-2471
        let _ = std::io::stdout().flush();                                      // c:2472
        return 0;                                                               // c:2473
    }
    str = args[0];
    nt = str.as_bytes()[0].to_ascii_uppercase();                               // c:2475
    if str.len() > 1 || (nt != b'S' && nt != b'B') {                           // c:2476
        crate::ported::utils::zwarnnam(name,                                    // c:2477
            &format!("transfer mode {} not recognised", str));
        return 1;                                                               // c:2478
    }
    let cmd = format!("MODE {}\r\n", nt as char);                               // c:2480 cmd[5] = nt
    if zfsendcmd(&cmd) > 2 {                                                    // c:2481
        return 1;                                                               // c:2482
    }
    // c:2483-2484 — update session transfer_mode.
    if let Ok(mut state) = zftp_state().lock() {
        if let Some(sess) = state.get_session_mut(None) {
            sess.transfer_mode = if nt == b'S' { ZFST_STRE } else { ZFST_BLOC } as i32;
        }
    }
    let mb = (nt as char).to_string();
    zfsetparam("ZFTP_MODE", &mb, ZFPM_READONLY);                                // c:2491
    0                                                                           // c:2491
}

/// Port of `zftp_local(UNUSED(char *name), char **args, int flags)` from `Src/Modules/zftp.c:2491`.
#[allow(unused_variables)]
pub fn zftp_local(name: &str, args: &[&str], flags: i32) -> i32 {              // c:2491
    let more = args.len() > 1;                                                  // c:2493 more = !!args[1]
    let mut ret: i32 = 0;                                                       // c:2493 ret = 0
    let dofd = args.is_empty();                                                 // c:2493 dofd = !*args
    let mut i = 0usize;
    loop {                                                                      // c:2494 while (*args || dofd)
        if !dofd && i >= args.len() { break; }
        let arg = if dofd { "" } else { args[i] };
        let mut sz: libc::off_t = 0;                                            // c:2495 off_t sz
        let mut mt: Option<String> = None;                                      // c:2496 char *mt
        // c:2497-2498 — zfstats(*args, !(flags & ZFTP_HERE), &sz, &mt, dofd ? 0 : -1);
        let remote = if (flags & ZFTP_HERE) != 0 { 0 } else { 1 };
        let fd = if dofd { 0 } else { -1 };
        let newret = zfstats(arg, remote, &mut sz, &mut mt, fd);
        if newret == 2 {                                                        // c:2499
            return 2;                                                           // c:2500
        } else if newret != 0 {                                                 // c:2501
            ret = 1;                                                            // c:2502
            // c:2503-2504 — Rust Drop.
            i += 1;                                                             // c:2505
            continue;                                                           // c:2506
        }
        if more {                                                               // c:2508
            print!("{} ", arg);                                                 // c:2509-2510
        }
        let mt_s = mt.unwrap_or_default();
        println!("{} {}", sz, mt_s);                                            // c:2517
        if dofd { break; }                                                      // c:2520-2521
        i += 1;                                                                 // c:2522
    }
    let _ = std::io::stdout().flush();                                          // c:2544
    ret                                                                         // c:2544
}

/// Port of `zftp_getput(char *name, char **args, int flags)` from `Src/Modules/zftp.c:2544`.
pub fn zftp_getput(name: &str, args: &[&str], flags: i32) -> i32 {              // c:2544
    let mut ret: i32 = 0;                                                       // c:2546 ret = 0
    let recv = (flags & ZFTP_RECV) != 0;                                        // c:2546 recv
    let mut getsize: i32 = 0;                                                   // c:2546 getsize = 0
    let progress: i32 = 1;                                                      // c:2546 progress = 1
    let cmd_pfx = if recv { "RETR " }                                           // c:2547
                  else if (flags & ZFTP_APPE) != 0 { "APPE " }
                  else { "STOR " };

    // c:2559 — zfsettype(ZFST_TYPE(zfstatusp[zfsessno]));
    let ttype = zftp_state().lock().ok()
        .and_then(|s| s.get_session(None).map(|sess| sess.transfer_type))
        .unwrap_or(ZFST_IMAG as i32);
    zfsettype(ttype);

    if recv { let _ = std::io::stdout().flush(); }                              // c:2561-2562

    // c:2563 — for (; *args; args++) — with REST advancing args twice.
    let mut i = 0usize;
    while i < args.len() {
        let arg = args[i];
        let mut rest_cmd: String = String::new();                               // c:2564 char *rest = NULL
        let mut startat: libc::off_t = 0;                                       // c:2565

        // c:2566-2587 — getsize hint via zfstats + initial progress
        // callback. Only fires when a zftp_progress shfunc is defined.
        // ZFST_NOSZ/ZFST_TRSZ per-session status bits aren't ported;
        // the c:2577-2585 cache check collapses to "always probe SIZE"
        // (or "always set getsize on STOR") — matching C's behavior
        // when those bits are unset on a fresh session.
        if progress != 0 && crate::ported::utils::getshfunc("zftp_progress").is_some() {
            let mut sz: libc::off_t = -1;                                       // c:2567
            let mut _mdtm: Option<String> = None;
            // c:2576-2585 — DUMB-gated SIZE probe. C also consults
            // ZFST_NOSZ/ZFST_TRSZ status bits to avoid re-probing when
            // the server already proved it doesn't send the size; that
            // cache isn't ported per-session, so the gate collapses
            // to !DUMB ON RECV (matches behavior on first transfer).
            let dumb = (zfprefs.load(std::sync::atomic::Ordering::Relaxed)
                        & ZFPF_DUMB) != 0;                                      // c:2576
            if !dumb && (recv || (flags & ZFTP_REST) == 0) {                    // c:2576-2578
                let _ = zfstats(arg, if recv { 1 } else { 0 },                  // c:2580
                                &mut sz, &mut _mdtm, 0);
                if recv && sz == -1 {                                           // c:2582
                    getsize = 1;                                                // c:2583
                }
            } else {
                getsize = 1;                                                    // c:2585
            }
            zfstarttrans(arg, if recv { 1 } else { 0 }, sz);                    // c:2587
        }

        // c:2589-2592 — REST resume.
        if (flags & ZFTP_REST) != 0 && i + 1 < args.len() {
            startat = args[i + 1].parse().unwrap_or(0);
            rest_cmd = format!("REST {}\r\n", args[i + 1]);
        }

        // c:2594 — ln = tricat(cmd, *args, "\r\n");
        let ln = format!("{}{}\r\n", cmd_pfx, arg);

        // c:2596 — zfgetdata returns 0 on success, 1 on failure.
        let gd = zfgetdata(name, &rest_cmd, &ln, getsize);
        if gd != 0 {
            ret = 2;                                                            // c:2597
        } else {
            // c:2598 — zfsenddata(name, recv, progress, startat).
            if zfsenddata(name, if recv { 1 } else { 0 }, progress, startat) != 0 {
                ret = 1;                                                        // c:2599
            }
        }
        // c:2600 — zsfree(ln) — Rust Drop.

        // c:2606-2616 — final progress callback (zftp_progress shfunc).
        if progress != 0 && ret != 2 {
            if let Some(_shfunc) = crate::ported::utils::getshfunc("zftp_progress") { // c:2607
                let osc = crate::ported::builtin::SFCONTEXT.load(
                    std::sync::atomic::Ordering::Relaxed);                      // c:2610
                zfsetparam("ZFTP_TRANSFER",                                     // c:2611-2612
                    if recv { "GF" } else { "PF" }, ZFPM_READONLY);
                crate::ported::builtin::SFCONTEXT.store(
                    crate::ported::zsh_h::SFC_HOOK,
                    std::sync::atomic::Ordering::Relaxed);                      // c:2613
                // c:2614 — doshfunc dispatch happens inside fusevm; static
                // caller probes via getshfunc + reads LASTVAL.
                let _ = crate::ported::builtin::LASTVAL.load(
                    std::sync::atomic::Ordering::Relaxed);
                crate::ported::builtin::SFCONTEXT.store(osc,
                    std::sync::atomic::Ordering::Relaxed);                      // c:2615
            } else {
                zfsetparam("ZFTP_TRANSFER",                                     // c:2611-2612 fallback
                    if recv { "GF" } else { "PF" }, ZFPM_READONLY);
            }
        }
        // c:2617-2620 — REST consumed two args.
        if (flags & ZFTP_REST) != 0 {
            i += 1;
        }
        // c:2621-2622 — break on errflag.
        if crate::ported::utils::errflag.load(std::sync::atomic::Ordering::Relaxed) != 0 {
            break;
        }
        let _ = getsize;
        getsize = 0;                                                            // reset per-iteration
        i += 1;
    }
    zfendtrans();                                                               // c:2635
    if ret != 0 { 1 } else { 0 }                                                // c:2635
}

/// Port of `zftp_delete(UNUSED(char *name), char **args, UNUSED(int flags))` from `Src/Modules/zftp.c:2635`.
/// C: walk `args`, send `DELE <name>\r\n` for each. Returns 1 if any
/// DELE got a 3xx+ reply, else 0.
#[allow(unused_variables)]
pub fn zftp_delete(name: &str, args: &[&str], flags: i32) -> i32 {            // c:2635
    let mut ret: i32 = 0;                                                       // c:2635 int ret = 0
    let cmd: String;                                                            // c:2638 char *cmd
    let _ = cmd;
    for aptr in args.iter() {                                                   // c:2639 for (aptr = args; *aptr; aptr++)
        let cmd = format!("DELE {}\r\n", aptr);                                 // c:2640 tricat("DELE ", *aptr, "\r\n")
        if zfsendcmd(&cmd) > 2 {                                                // c:2652
            ret = 1;                                                            // c:2652
        }
        // c:2652 zsfree(cmd) — Rust Drop.
    }
    ret                                                                         // c:2652
}

/// Port of `zftp_mkdir(UNUSED(char *name), char **args, int flags)` from `Src/Modules/zftp.c:2652`.
/// C: send `MKD <args[0]>\r\n` (or `RMD` when ZFTP_DELE flag set —
/// the `rmdir` subcommand routes through this fn with that bit).
#[allow(unused_variables)]
pub fn zftp_mkdir(name: &str, args: &[&str], flags: i32) -> i32 {              // c:2652
    let ret: i32;                                                               // c:2652 int ret
    if args.is_empty() { return 1; }
    let cmd_pfx = if (flags & ZFTP_DELE) != 0 { "RMD " } else { "MKD " };       // c:2666
    let cmd = format!("{}{}\r\n", cmd_pfx, args[0]);                            // c:2666 tricat
    ret = (zfsendcmd(&cmd) > 2) as i32;                                         // c:2666
    // c:2666 zsfree — Rust Drop.
    ret                                                                         // c:2666
}

/// Port of `zftp_rename(UNUSED(char *name), char **args, UNUSED(int flags))` from `Src/Modules/zftp.c:2666`.
/// C: send RNFR (rename-from), expect 3xx, then send RNTO (rename-to),
/// expect 2xx. Two-phase rename per RFC959.
#[allow(unused_variables)]
pub fn zftp_rename(name: &str, args: &[&str], flags: i32) -> i32 {            // c:2666
    let mut ret: i32;                                                           // c:2666 int ret
    let cmd: String;                                                            // c:2669 char *cmd
    let _ = cmd;
    if args.len() < 2 { return 1; }

    let cmd = format!("RNFR {}\r\n", args[0]);                                  // c:2671 tricat("RNFR ", args[0], "\r\n")
    ret = 1;                                                                    // c:2672
    if zfsendcmd(&cmd) == 3 {                                                   // c:2673
        // c:2674 zsfree(cmd) — Rust Drop.
        let cmd = format!("RNTO {}\r\n", args[1]);                              // c:2675
        if zfsendcmd(&cmd) == 2 {                                               // c:2676
            ret = 0;                                                            // c:2690
        }
    }
    // c:2690 zsfree(cmd) — Rust Drop.
    ret                                                                         // c:2690
}

/// Port of `zftp_quote(UNUSED(char *name), char **args, int flags)` from `Src/Modules/zftp.c:2690`.
/// C: send a raw FTP command, optionally prefixed with `SITE ` when
/// ZFTP_SITE flag is set (the `site` subcommand routes here with the
/// bit). The first arg is the verb; subsequent args are appended.
#[allow(unused_variables)]
pub fn zftp_quote(name: &str, args: &[&str], flags: i32) -> i32 {              // c:2690
    let ret: i32;                                                               // c:2690 int ret = 0
    let cmd: String;                                                            // c:2693 char *cmd
    let _ = cmd;
    let argv: Vec<&str> = args.to_vec();
    let cmd = if (flags & ZFTP_SITE) != 0 {                                     // c:2695
        zfargstring("SITE", &argv) + "\r\n"
    } else {
        if argv.is_empty() { return 1; }
        zfargstring(argv[0], &argv[1..]) + "\r\n"                               // c:2696
    };
    ret = (zfsendcmd(&cmd) > 2) as i32;                                         // c:2697
    // c:2698 zsfree — Rust Drop.
    ret                                                                         // c:2700
}

/// Port of `zftp_close(UNUSED(char *name), UNUSED(char **args), UNUSED(int flags))` from `Src/Modules/zftp.c:2782`.
/// C: `static int zftp_close(UNUSED(char *name), UNUSED(char **args),
/// UNUSED(int flags))` — closes the current session's control
/// connection. Body is a single zfclose(0) call.
#[allow(unused_variables)]
pub fn zftp_close(name: &str, args: &[&str], flags: i32) -> i32 {           // c:2782
    zfclose(0);                                                                // c:2782
    0                                                                          // c:2785
}

/// Port of `zftp_session(UNUSED(char *name), char **args, UNUSED(int flags))` from `Src/Modules/zftp.c:2889`.
#[allow(unused_variables)]
pub fn zftp_session(name: &str, args: &[&str], flags: i32) -> i32 {           // c:2889
    if args.is_empty() {                                                        // c:2889
        // c:2892-2895 — walk zfsessions list, print each name.
        if let Ok(state) = zftp_state().lock() {
            for sess_name in state.session_names() {                            // c:2894
                println!("{}", sess_name);                                      // c:2895
            }
        }
        return 0;                                                               // c:2896
    }
    // c:2903-2904 — no-op if already in the requested session.
    let current = zftp_state().lock().ok()
        .and_then(|s| s.current_name().map(|n| n.to_string()))
        .unwrap_or_default();
    if args[0] == current {
        return 0;                                                               // c:2915
    }
    savesession();                                                              // c:2915
    switchsession(args[0]);                                                     // c:2915
    0                                                                           // c:2915
}

/// Port of `zftp_rmsession(UNUSED(char *name), char **args, UNUSED(int flags))` from `Src/Modules/zftp.c:2915`.
#[allow(unused_variables)]
pub fn zftp_rmsession(name: &str, args: &[&str], flags: i32) -> i32 {         // c:2915
    // c:2915-2920 — locals: no, sptr, newsess (Zftp_session linked-list).
    let mut found_name: Option<String> = None;                                  // sptr identity
    let mut is_current: bool = false;                                           // sptr == zfsess
    let mut newsess: Option<String> = None;                                     // c:2920

    // c:2922-2928 — find session by name (or current if no arg).
    {
        let state = match zftp_state().lock() {
            Ok(s) => s,
            Err(_) => return 1,
        };
        let current = state.current_name().unwrap_or("default").to_string();
        let target = args.first().copied().map(String::from).unwrap_or_else(|| current.clone());
        for sess_name in state.session_names() {                                // c:2923
            if sess_name == target {
                found_name = Some(sess_name.to_string());
                is_current = sess_name == current;
                break;
            }
        }
    }
    let target_name = match found_name {                                        // c:2929
        Some(n) => n,
        None => return 1,                                                       // c:2930
    };

    // c:2932-2956 — closing logic differs by current-vs-other.
    if is_current {                                                             // c:2932
        zfclosedata();                                                          // c:2934
        zfclose(0);                                                             // c:2935
        // c:2941-2946 — pick a new current session if any others remain.
        let other = zftp_state().lock().ok()
            .map(|s| s.session_names().into_iter()
                .find(|n| *n != target_name).map(String::from))
            .unwrap_or(None);
        if let Some(o) = other {                                                // c:2945
            newsess = Some(o);
        }
    } else {                                                                    // c:2947
        // c:2948-2956 — temporarily switch to target, close-non-destructive,
        // then switch back. Rust collapses this to a direct close on
        // the named session.
        let prev = zftp_state().lock().ok()
            .and_then(|s| s.current_name().map(String::from));
        if let Ok(mut state) = zftp_state().lock() {
            state.set_current(&target_name);                                    // c:2949
        }
        zfclosedata();                                                          // c:2954
        zfclose(1);                                                             // c:2955 (leaveparams=1)
        if let (Some(p), Ok(mut state)) = (prev, zftp_state().lock()) {
            state.set_current(&p);                                              // c:2956
        }
    }

    // c:2964-2993 — remove session from list + switch to newsess if any.
    // Rust port: remove via zftp_globals::remove_session; newsess switch via
    // switchsession (already ported above).
    if let Ok(mut state) = zftp_state().lock() {
        state.remove_session(&target_name);
    }
    if let Some(n) = newsess {                                                  // c:2982
        switchsession(&n);                                                      // c:2983
    } else if zftp_state().lock().map(|s| s.session_names().is_empty()).unwrap_or(false) {
        // c:2992 — last session gone, start fresh.
        newsession("default");
    }
    0                                                                           // c:2995
}

/// Port of `zftp_cleanup()` from `Src/Modules/zftp.c:3128`. Walks
/// every session sending QUIT (via zfclose) on the current one and
/// closing the control fd on others, then clears the session table.
pub fn zftp_cleanup() -> i32 {                                              // c:3128
    // c:3128 — Zftp_session cursess = zfsess; (snapshot current).
    let cursess = zftp_state().lock().ok()
        .and_then(|s| s.current_name().map(|n| n.to_string()));
    let session_names: Vec<String> = zftp_state().lock().ok()
        .map(|s| s.session_names().iter().map(|n| n.to_string()).collect())
        .unwrap_or_default();
    // c:3135-3142 — walk every session: zfclosedata + zfclose(leaveparams).
    for nm in session_names {                                               // c:3136
        // c:3137 — zfsess = (Zftp_session)nptr->dat; (switch to session).
        if let Ok(mut s) = zftp_state().lock() {
            let _ = s.set_current(&nm);
        }
        zfclosedata();                                                      // c:3140
        // c:3144 — zfclose(zfsess != cursess): non-current sessions keep
        // their params; current session goes through full unset.
        let leaveparams = if Some(&nm) != cursess.as_ref() { 1 } else { 0 };
        zfclose(leaveparams);                                               // c:3144
    }
    // c:3146-3151 — clear lastmsg, ZFTP_SESSION, freelinklist sessions.
    if let Ok(mut m) = lastmsg.lock() {
        m.clear();                                                          // c:3147
    }
    zfunsetparam("ZFTP_SESSION");                                           // c:3149
    if let Ok(mut state) = zftp_state().lock() {
        *state = zftp_globals::new();                                               // c:3150 freelinklist
    }
    // c:3152 — zfree(zfstatusp): per-session status array. zfstatusp
    // substrate isn't ported (per-session bits live on zftp_session
    // directly), so nothing to free.
    0
}

/// Global ZFTP session state — port of the C file-scope statics
/// `static Zftp_session zfsessions[]` and friends in zftp.c. Holds
/// all sessions and the currently-active one. Free fns above route
/// through this so subcommand dispatch matches C behaviour.
static ZFTP_STATE_INNER: std::sync::OnceLock<std::sync::Mutex<zftp_globals>> = std::sync::OnceLock::new();

/// Accessor for the live zftp module state. C's `Src/Modules/zftp.c`
/// keeps per-session state in scattered file-statics (`zfsess`,
/// `zfsessions`, `zfcommand`, ...); this fn returns the single
/// `Mutex<zftp_globals>` aggregating those into one shared store.
pub fn zftp_state() -> &'static std::sync::Mutex<zftp_globals> {
    ZFTP_STATE_INNER.get_or_init(|| std::sync::Mutex::new(zftp_globals::new()))
}

/// Clear poison on the zftp state mutex (test teardown helper).
pub fn zftp_state_clear_poison() {
    if let Some(m) = ZFTP_STATE_INNER.get() {
        m.clear_poison();
    }
}

/// Port of `zftpexithook(UNUSED(Hookdef d), UNUSED(void *dummy))` from Src/Modules/zftp.c:3156.
/// C: `static int zftpexithook(UNUSED(Hookdef d), UNUSED(void *dummy))`
/// — calls `zftp_cleanup()`, returns 0.
#[allow(non_snake_case)]
#[allow(unused_variables)]
pub fn zftpexithook(d: *const crate::ported::zsh_h::hookdef, dummy: *mut std::ffi::c_void) -> i32 {
    zftp_cleanup();                                                          // c:3156
    0                                                                        // c:3159
}

/// Port of `zfunalarm()` from Src/Modules/zftp.c:422.
/// C: `void zfunalarm(void)` — restores the prior alarm if `oalremain`
/// was nonzero, else cancels with `alarm(0)`. Adjusts for elapsed time.
#[allow(non_snake_case)]
pub fn zfunalarm() {                                                         // c:422
    let oalremain = OALREMAIN.load(std::sync::atomic::Ordering::Relaxed);    // c:422
    if oalremain != 0 {                                                      // c:423
        // c:432-433 — `time_t tdiff = zmonotime(NULL) - oaltime;`
        let oaltime = OALTIME.load(std::sync::atomic::Ordering::Relaxed);    // c:432
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs() as i64)
            .unwrap_or(0);
        let tdiff = now - oaltime;                                           // c:432
        let secs = if (oalremain as i64) < tdiff { 1 } else {                // c:434
            (oalremain as i64 - tdiff) as u32
        };
        unsafe { libc::alarm(secs); }                                        // c:453
    } else {
        unsafe { libc::alarm(0); }                                           // c:453
    }
}

/// Port of `zfunpipe()` from Src/Modules/zftp.c:453.
/// C: `void zfunpipe(void)` — restores the SIGPIPE disposition that
/// existed before `zfpipe()` ignored it.
#[allow(non_snake_case)]
pub fn zfunpipe() {                                                          // c:453
    // c:453 — `if (sigtrapped[SIGPIPE]) { ... } else signal_default(SIGPIPE);`
    // The static-link path doesn't expose `sigtrapped[]`/`siglists[]` yet,
    // so reset to default disposition unconditionally — matches the
    // common case where SIGPIPE wasn't trapped.
    unsafe { libc::signal(libc::SIGPIPE, libc::SIG_DFL); }                   // c:460
}

/// Port of `zfunsetparam(char *name)` from Src/Modules/zftp.c:529.
/// C: `static void zfunsetparam(char *name)` — clears PM_READONLY then
/// calls `unsetparam_pm(pm, 0, 1)`.
#[allow(non_snake_case)]
pub fn zfunsetparam(name: &str) {                                            // c:529
    // c:529-534 — paramtab->getnode(paramtab, name); pm->node.flags &=
    // ~PM_READONLY; unsetparam_pm(pm, 0, 1);
    // Static-link path: paramtab access goes through the params subsystem
    // which doesn't yet expose a typed `getnode`/`unsetparam_pm` wrapper.
    // Use the env-var fallback; full Param-flag path lives in
    // src/ported/params.rs and will be wired in a later port pass.
    std::env::remove_var(name);                                              // c:533 (effective)
}

/// Port of `zfwrite(int fd, char *bf, off_t sz, int tmout)` from Src/Modules/zftp.c:1332.
/// C: `int zfwrite(int fd, char *bf, off_t sz, int tmout)` — write with
/// optional alarm timeout.
#[allow(non_snake_case)]
pub fn zfwrite(fd: i32, bf: &[u8], sz: i64, tmout: i32) -> i32 {             // c:1332
    // c:1332 — `if (!tmout) return write(fd, bf, sz);`
    if tmout == 0 {                                                          // c:1335
        return unsafe {
            libc::write(fd, bf.as_ptr() as *const _, sz as usize) as i32     // c:1336
        };
    }
    // c:1338-1342 — setjmp(zfalrmbuf) timeout path. Without setjmp in
    // safe Rust we fall through to a simple write under the alarm; a
    // future refactor should plumb a real timeout via select(2)/poll(2).
    zfalarm(tmout);                                                          // c:1343
    let ret = unsafe {
        libc::write(fd, bf.as_ptr() as *const _, sz as usize) as i32        // c:1345
    };
    unsafe { libc::alarm(0); }                                               // c:1349
    ret                                                                      // c:1351
}

/// Port of `zfwrite_block(int fd, char *bf, off_t sz, int tmout)` from Src/Modules/zftp.c:1411.
/// C: `int zfwrite_block(int fd, char *bf, off_t sz, int tmout)` —
/// frame the data with a `struct zfheader` and write block + payload.
#[allow(non_snake_case)]
pub fn zfwrite_block(fd: i32, bf: &[u8], sz: i64, tmout: i32) -> i32 {       // c:1411
    let mut hdr = zfheader { bytes: [0u8; 2], flags: 0i8 };                  // c:1411
    let mut n: i32;
    // c:1418-1424 — emit header, retry on EINTR.
    loop {
        hdr.bytes[0] = ((sz & 0xff00) >> 8) as u8;                           // c:1419
        hdr.bytes[1] = (sz & 0xff) as u8;                                    // c:1420
        hdr.flags = if sz != 0 { 0i8 } else { ZFHD_EOFB as i8 };             // c:1421
        let hdr_bytes = unsafe {
            std::slice::from_raw_parts(&hdr as *const _ as *const u8, 3)
        };
        n = zfwrite(fd, hdr_bytes, 3, tmout);                                // c:1422
        if !(n < 0 && unsafe { *errno_ptr() } == libc::EINTR) { break; }     // c:1424
    }
    if n != 3 {                                                              // c:1426
        return n;                                                            // c:1428
    }
    if sz != 0 {                                                             // c:1431
        n = zfwrite(fd, bf, sz, tmout);                                      // c:1432
    }
    n                                                                        // c:1434
}

// File-static globals for zfalarm/zfunalarm — c:386-389.
/// `zfdrrrring` — file-static from `Src/Modules/zftp.c:340`. Set by
/// `zfhandler()` on SIGALRM, polled by zfread/zfgetline to bail out.
pub static ZFDRRRRING: std::sync::atomic::AtomicI32 =                         // c:340
    std::sync::atomic::AtomicI32::new(0);

/// `zfalarmed` — file-static from `Src/Modules/zftp.c:346`. Tracks
/// whether `zfalarm()` has installed the SIGALRM handler.
pub static ZFALARMED: std::sync::atomic::AtomicI32 =                          // c:346
    std::sync::atomic::AtomicI32::new(0);

pub static OALREMAIN: std::sync::atomic::AtomicU32 =
    std::sync::atomic::AtomicU32::new(0);
pub static OALTIME: std::sync::atomic::AtomicI64 =
    std::sync::atomic::AtomicI64::new(0);

// `zftp_cleanup` is defined above at c:3128; the exit hook calls it.

use crate::ported::zsh_h::features as features_t;
use std::sync::{Mutex, OnceLock};

static MODULE_FEATURES: OnceLock<Mutex<features_t>> = OnceLock::new();

// WARNING: NOT IN ZFTP.C — Rust-only module-framework shim.
// C uses generic featuresarray/handlefeatures/setfeatureenables from
// Src/module.c:3275/3370/3445 with C-side Builtin/Features pointers;
// Rust per-module shims hardcode the bintab/conddefs/mathfuncs/paramdefs.
fn module_features() -> &'static Mutex<features_t> {
    MODULE_FEATURES.get_or_init(|| Mutex::new(features_t {
        bn_list: None,
        bn_size: 1,
        cd_list: None,
        cd_size: 0,
        mf_list: None,
        mf_size: 0,
        pd_list: None,
        pd_size: 0,
        n_abstract: 0,
    }))
}

// Local stubs for the per-module entry points. C uses generic
// `featuresarray`/`handlefeatures`/`setfeatureenables` (module.c:
// 3275/3370/3445) but those take `Builtin` + `Features` pointer
// fields the Rust port doesn't carry. The hardcoded descriptor
// list mirrors the C bintab/conddefs/mathfuncs/paramdefs.
// WARNING: NOT IN ZFTP.C — Rust-only module-framework shim.
// C uses generic featuresarray/handlefeatures/setfeatureenables from
// Src/module.c:3275/3370/3445 with C-side Builtin/Features pointers;
// Rust per-module shims hardcode the bintab/conddefs/mathfuncs/paramdefs.
fn featuresarray(_m: *const module, _f: &Mutex<features_t>) -> Vec<String> {
    vec!["b:zftp".to_string()]
}

// WARNING: NOT IN ZFTP.C — Rust-only module-framework shim.
// C uses generic featuresarray/handlefeatures/setfeatureenables from
// Src/module.c:3275/3370/3445 with C-side Builtin/Features pointers;
// Rust per-module shims hardcode the bintab/conddefs/mathfuncs/paramdefs.
fn handlefeatures(
    _m: *const module,
    _f: &Mutex<features_t>,
    enables: &mut Option<Vec<i32>>,
) -> i32 {
    if enables.is_none() {
        *enables = Some(vec![1; 1]);
    }
    0
}

// WARNING: NOT IN ZFTP.C — Rust-only module-framework shim.
// C uses generic featuresarray/handlefeatures/setfeatureenables from
// Src/module.c:3275/3370/3445 with C-side Builtin/Features pointers;
// Rust per-module shims hardcode the bintab/conddefs/mathfuncs/paramdefs.
fn setfeatureenables(
    _m: *const module,
    _f: &Mutex<features_t>,
    _e: Option<&[i32]>,
) -> i32 {
    0
}