efs / efs.el

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
;; -*-Emacs-Lisp-*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; File:         efs.el
;; Release:      $efs release: 1.19 $
;; Version:      #Revision: 1.61 $
;; RCS:          
;; Description:  Transparent FTP support for the original GNU Emacs
;;               from FSF and XEmacs
;; Authors:      Andy Norman <ange@hplb.hpl.hp.com>,
;;               Sandy Rutherford <sandy@ibm550.sissa.it>
;; Created:      Thu Oct 12 14:00:05 1989 (as ange-ftp)
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; The following restrictions apply to all of the files in the efs
;;; distribution.
;;; 
;;; Copyright (C) 1993  Andy Norman / Sandy Rutherford
;;;
;;; Authors:
;;;          Andy Norman (ange@hplb.hpl.hp.com)
;;;          Sandy Rutherford (sandy@ibm550.sissa.it)
;;;          
;;;          The authors of some of the sub-files of efs are different
;;;          from the above.  We are very grateful to people who have
;;;          contributed code to efs.
;;;
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 1, or (at your option)
;;; any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; A copy of the GNU General Public License can be obtained from this
;;; program's authors (send electronic mail to ange@hplb.hpl.hp.com) or
;;; from the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
;;; MA 02139, USA.

;;; Description:
;;;
;;; This package attempts to make accessing files and directories on
;;; remote computers from within GNU Emacs as simple and transparent
;;; as possible.  Currently all remote files are accessed using FTP.
;;; The goal is to make the entire internet accessible as a virtual
;;; file system.

;;; -----------------------------------------------------------
;;; Technical information on this package:
;;; -----------------------------------------------------------

;;; efs hooks onto the following functions using the
;;; file-name-handler-alist.  Depending on which version of emacs you
;;; are using, not all of these functions may access this alist. In
;;; this case, efs overloads the definitions of these functions with
;;; versions that do access the file-name-handler-alist. These
;;; overloads are done in efs's version-specific files.
;;;
;;; abbreviate-file-name
;;; backup-buffer
;;; copy-file
;;; create-file-buffer
;;; delete-directory
;;; delete-file
;;; directory-file-name
;;; directory-files
;;; file-attributes
;;; file-directory-p
;;; file-exists-p
;;; file-local-copy
;;; file-modes
;;; file-name-all-completions
;;; file-name-as-directory
;;; file-name-completion
;;; file-name-directory
;;; file-name-nondirectory
;;; file-name-sans-versions
;;; file-newer-than-file-p
;;; file-readable-p
;;; file-executable-p
;;; file-accessible-directory-p
;;; file-symlink-p
;;; file-writable-p
;;; get-file-buffer
;;; insert-directory
;;; insert-file-contents
;;; insert-file-contents-literally
;;; list-directory
;;; make-directory-internal
;;; rename-file
;;; set-file-modes
;;; set-visited-file-modtime
;;; substitute-in-file-name
;;; verify-visited-file-modtime
;;; write-region
;;; 
;;; The following functions are overloaded in efs.el, because they cannot
;;; be handled via the file-name-handler-alist.
;;;
;;; expand-file-name
;;; load
;;; read-file-name-internal (Emacs 18, only)
;;; require
;;; 
;;; The following dired functions are handled by hooking them into the
;;; the file-name-handler-alist. This is done in efs-dired.el.
;;; 
;;; efs-dired-compress-file
;;; eds-dired-print-file
;;; efs-dired-make-compressed-filename
;;; efs-compress-file
;;; efs-dired-print-file
;;; efs-dired-create-directory
;;; efs-dired-recursive-delete-directory
;;; efs-dired-uncache
;;; efs-dired-call-process
;;; 
;;; In efs-dired.el, the following dired finctions are overloaded.
;;;
;;; dired-collect-file-versions
;;; dired-find-file
;;; dired-flag-backup-files
;;; dired-get-filename
;;; dired-insert-headerline
;;; dired-move-to-end-of-filename
;;; dired-move-to-filename
;;; dired-run-shell-command
;;;
;;; efs makes use of the following hooks
;;;
;;; diff-load-hook
;;; dired-before-readin-hook
;;; find-file-hooks
;;; dired-grep-load-hook

;;; LISPDIR ENTRY for the Elisp Archive:
;;; 
;;;    LCD Archive Entry:
;;;    efs|Andy Norman and Sandy Rutherford
;;;    |ange@hplb.hpl.hp.com and sandy@ibm550.sissa.it
;;;    |transparent FTP Support for GNU Emacs
;;;    |$Date$|$efs release: 1.19 $|

;;; Host and listing type notation:
;;;
;;; The functions efs-host-type and efs-listing-type, and the
;;; variable efs-dired-host-type follow the following conventions
;;; for remote host types.
;;;
;;; nil = local host type, whatever that is (probably unix).
;;;       Think nil as in "not a remote host". This value is used by
;;;       efs-dired-host-type for local buffers.
;;;       (efs-host-type nil) => nil
;;;
;;; 'type = a remote host of TYPE type.
;;;
;;; 'type:list = a remote host using listing type 'type:list.
;;;              This is currently used for Unix dl (descriptive
;;;              listings), when efs-dired-host-type is set to
;;;              'unix:dl, and to support the myriad of DOS FTP
;;;              servers.

;;; Supported host and listing types:
;;;
;;; unknown, unix, dumb-unix, bsd-unix, sysV-unix, next-unix,
;;; super-dumb-unix, dumb-apollo-unix,
;;; apollo-unix, unix:dl, dos-distinct, ka9q, dos, dos:ftp, dos:novell,
;;; dos:ncsa, dos:winsock, vos, hell, dos:microsoft, super-dumb-unix
;;; vms, cms, mts, mvs, mvs:tcp mvs:nih tops-20, mpe, ti-twenex,
;;; ti-explorer, os2, vos,
;;; vms:full, guardian, ms-unix (This is the Microsoft NT Windows server
;;; in unix mode.), plan9, unix:unknown, nos-ve (actually NOS/VE).

;;; Host and listing type hierarchy:
;;;
;;; unknown: unix, dumb-unix, sysV-unix, bsd-unix, next-unix, apollo-unix,
;;;          ka9q, dos-distinct, unix:dl, hell, 
;;;          super-dumb-unix, dumb-apollo-unix
;;; unix:    sysV-unix, bsd-unix, next-unix, apollo-unix, unix:dl
;;; dos:     dos:ftp, dos:novell, dos:ncsa, dos:microsoft, dos:winsock
;;; dumb-unix:
;;; bsd-unix:
;;; sysV-unix:
;;; next-unix:
;;; apollo-unix:
;;; dumb-apollo-unix:
;;; unix:dl:
;;; unix:unknown: unix:dl, unix
;;; super-dumb-unix:
;;; dos-distinct:
;;; dos:ftp:
;;; dos:novell:
;;; dos:microsoft
;;; ka9q:
;;; vms: vms:full
;;; cms:
;;; mts:
;;; mvs: mvs:tcp, mvs:nih
;;; mvs:tcp:
;;; mvs:nih:
;;; tops-20:
;;; ti-twenex:
;;; ti-explorer:
;;; os2:
;;; vos:
;;; vms:full:
;;; dos:ncsa:
;;; dos:winsock:
;;; vos:
;;; hell:
;;; guardian:
;;; ms-unix:
;;; plan9:
;;; nos-ve:
;;; coke:
;;; 


;;;; ================================================================
;;;; >0
;;;; Table of Contents for efs.el
;;;; ================================================================
;;
;;   Each section of efs.el is labelled by >#, where # is the number of
;;   the section.
;;
;;    1. Provisions, requirements, and autoloads.
;;    2. Variable definitions.
;;    3. Utilities.
;;    4. Hosts, users, accounts, and passwords.
;;    5. FTP client process and server responses.
;;    6. Sending commands to the FTP server.
;;    7. Parsing and storing remote file system data.
;;    8. Redefinitions of standard GNU Emacs functions.
;;    9. Multiple host type support.
;;   10. Attaching onto the appropriate emacs version.


;;;; ================================================================
;;;; >1
;;;; General provisions, requirements, and autoloads.
;;;; Host type, and local emacs type dependent loads, and autoloads
;;;; are in the last two sections of this file.
;;;; ================================================================

;;;; ----------------------------------------------------------------
;;;; Provide the package (Do this now to avoid an infinite loop)
;;;; ----------------------------------------------------------------

(provide 'efs)

;;;; ----------------------------------------------------------------
;;;; Our requirements.
;;;; ----------------------------------------------------------------

(require 'backquote)
(require 'custom)
;; When bootstapping XEmacs, comint is not available
(condition-case nil
    (require 'comint)
  (error 
   (defun comint-mode ()
     (fundamental-mode))
   (defun comint-output-filter (process string)
     (with-current-buffer (process-buffer process)
       (goto-char (point-max))
       (insert string)))))
(require 'efs-defun)
(require 'efs-netrc)
(require 'efs-cu)
(require 'efs-ovwrt)
;; Do this last, as it installs efs into the file-name-handler-alist.
(require 'efs-fnh)

(autoload 'efs-report-bug "efs-report" "Submit a bug report for efs." t)
(autoload 'efs-gwp-start "efs-gwp" ; For interactive gateways.
	  "Login to the gateway machine and fire up an FTP client.")
(autoload 'efs-kerberos-login "efs-kerberos")
(autoload 'efs-insert-directory "efs-dired" "Insert a directory listing.")
(autoload 'efs-set-mdtm-of "efs-cp-p")
(autoload 'diff-latest-backup-file "diff")
(autoload 'read-passwd "passwd" "Read a password from the minibuffer." t)


;;;; ============================================================
;;;; >2
;;;;           Variable Definitions
;;;; **** The user configuration variables are in  ****
;;;; **** the second subsection of this section.   ****
;;;; ============================================================

;;;; ------------------------------------------------------------
;;;; Constant Definitions
;;;; ------------------------------------------------------------

(defconst efs-version
  (concat (substring "$efs release: 1.19 $" 14 -2)
	  "/"
	  (substring "#Revision: 1.57 $" 11 -2)))

(defconst efs-time-zero 1970) ; we count time from midnight, Jan 1, 1970 GMT.

(defconst efs-dumb-host-types
  '(dumb-unix super-dumb-unix vms cms mts ti-twenex ti-explorer dos mvs
	      tops-20 mpe ka9q dos-distinct os2 vos hell guardian
	      netware cms-knet nos-ve coke dumb-apollo-unix)
  "List of host types that can't take UNIX ls-style listing options.")
;; dos-distinct only ignores ls switches; it doesn't barf.
;; Still treat it as dumb.

(defconst efs-unix-host-types
  '(unix sysV-unix bsd-unix next-unix apollo-unix dumb-unix
	 dumb-apollo-unix super-dumb-unix)
  "List of unix host types.")

(defconst efs-version-host-types '(vms tops-20 ti-twenex ti-explorer)
  "List of host-types which associated a version number to all files.
This is not the same as associating version numbers to only backup files.")
;; Note that on these systems, 
;;  (file-name-sans-versions EXISTING-FILE) does not exist as a file.

(defconst efs-single-extension-host-types
  '(vms tops-20 ti-twenex ti-explorer cms mvs dos ka9q dos-distinct hell
	netware ms-unix plan9 cms-knet nos-ve)
  "List of host types which allow at most one extension on a file name.
Extensions are deliminated by \".\". In addition, these host-types must
allow \"-\" in file names, because it will be used to add additional extensions
to indicate compressed files.")

(defconst efs-idle-host-types
  (append '(coke unknown) efs-unix-host-types))
;; List of host types for which it is possible that the SITE IDLE command
;; is supported.

(defconst efs-listing-types
  '(unix:dl unix:unknown
    dos:novell dos:ftp dos:ncsa dos:microsoft dos:stcp dos:winsock
    mvs:nih mvs:tcp mvs:tcp
    vms:full)
  "List of supported listing types")

(defconst efs-nlist-listing-types
  '(vms:full))
;; Listing types which give a long useless listing when asked for a
;; LIST. For these, use an NLST instead. This can only be done
;; when there is some way to distinguish directories from
;; plain files in an NLST.

(defconst efs-opaque-gateways '(remsh interactive))
;; List of gateway types for which we need to do explicit file handling on
;; the gateway machine.

;;;; ------------------------------------------------------------------
;;;; User customization variables. Please read through these carefully.
;;;; ------------------------------------------------------------------

(defgroup efs nil
  "Transparent ftp access."
  :group 'files)

(defgroup efs-behavior nil
  "User-visible aspects of EFS."
  :group 'efs)

(defgroup efs-auto-save nil
  "EFS interaction with auto-save."
  :group 'efs)

(defgroup efs-gateways nil
  "Using EFS via an ftp gateway."
  :group 'efs)

(defgroup efs-programs nil
  "External programs used by EFS."
  :group 'efs)

(defgroup efs-parameters nil
  "Behind-the-scenes parameters of EFS."
  :group 'efs)

(defgroup efs-hooks nil
  "Hooks for EFS."
  :group 'efs)

;;;>>>>  If you are not fully connected to the internet,        <<<< 
;;;>>>>  and need to use a gateway (no matter how transparent)  <<<<
;;;>>>>  you will need to set some of the following variables.  <<<<
;;;>>>>  Read the documentation carefully.                      <<<<

(defcustom efs-local-host-regexp ".*"
  "Regexp to match names of local hosts.
These are hosts to which it is possible to obtain a direct internet
connection.  Even if the host is accessible by a very transparent FTP gateway,
it does not qualify as a local host.  The test to determine if machine A is
local to your machine is if it is possible to ftp from A _back_ to your
local machine.  Also, open-network-stream must be able to reach the host 
in question."
  :type 'regexp
  :group 'efs-gateways)

(defcustom efs-ftp-local-host-regexp ".*"
  "Regexp to match the names of hosts reachable by a direct ftp connection.
This regexp should match the names of hosts which can be reached using ftp,
without requiring any explicit connection to a gateway. If you have a smart
ftp client which is able to transparently go through a gateway, this will
differ from `efs-local-host-regexp'."
  :type 'regexp
  :group 'efs-gateways)

(defcustom efs-gateway-host nil
  "If non-nil, this must be the name of your ftp gateway machine.
If your net world is divided into two domains according to
`efs-local-ftp-host-regexp', set this variable to the name of the
gateway machine."
  :type '(choice string
		 (const nil))
  :group 'efs-gateways)

(defcustom efs-gateway-type nil
  "Specifies which type of gateway you wish efs to use.
This should be a list, the first element of which is a symbol denoting the
gateway type, and following elements give data on how to use the gateway.

The following possibilities are supported:

  '(local FTP-PROGRAM FTP-PROGRAM-ARGS)
  This means that your local host is itself the gateway.  However,
  you need to run a special FTP client to access outside hosts.
  FTP-PROGRAM should be the name of this FTP client,  and FTP-PROGRAM-ARGS
  is a list of arguments to pass to it \(probably set this to the value of
  efs-ftp-program-args <V>\).  Note that if your gateway is of this type,
  then you would set efs-gateway-host to nil.

  '(proxy FTP-PROGRAM FTP-PROGRAM-ARGS)
  This indicates that your gateway works by first FTP'ing to it, and
  then giving a  USER command of the form \"USER <username>@<host>\".
  FTP-PROGRAM is the FTP program to use to connect to the gateway; this
  is most likely \"ftp\".  FTP-PROGRAM-ARGS is a list of arguments to 
  pass to it.  You likely want this to be set to the value of
  efs-ftp-program-args <V>.  If the connection to the gateway FTP server
  is to be on a port different from 21, set efs-gateway-host to 
  \"<host>#<port>\".

  '(sidewinder FTP-PROGRAM FTP-PROGRAM-ARGS)
  This is for sidewinder proxies. We first FTP to the proxy, and give
  a USER command of the form \"USER <username>@<host>\". This should
  now connect to <host>. Now we send another USER command \"USER <user>\".

  '(raptor FTP-PROGRAM FTP-PROGRAM-ARGS USER)
  This is for the gateway called raptor by Eagle.  After connecting to the
  the gateway, the command \"user <user>@host USER\" is issued to login
  as <user> on <host>, where USER is an authentication username for the 
  gateway.  After issuing the password for the remote host, efs will
  send the password for USER on efs-gateway-host <V> as an account command.

 '(interlock FTP-PROGRAM FTP-PROGRAM-ARGS)
  This is for the interlock gateway.  The exact login sequence is to
  connect to the gateway specified by efs-gateway-host <V>, send the
  gateway password with a PASS command, send the command
  \"user <user>@<host>\" to connect to remote host <host> as user <user>,
  and finally to send the password for <user> on <host> with a second
  PASS command.

  '(kerberos FTP-PROGRAM FTP-PROGRAM-ARGS KINIT-PROGRAM KINIT-PROGRAM-ARGS)
  This is for the kerberos gateway where you need to run a program (kinit) to
  obtain a ticket for gateway authroization first.  FTP-PROGRAM should be
  the name of the FTP client that you use to connect to the gateway.  This
  may likely be \"iftp\".  FTP-PROGRAM-ARGS are the arguments that you need
  to pass to FTP-PROGRAM.  This is probably the value of
  efs-ftp-program-args <V>.  KINIT-PROGRAM is the name of the program to
  run in order to obtain a ticket.  This is probably \"kinit\".
  KINIT-PROGRAM-ARGS is a list og strings indicating any arguments that you
  need to pass to KINIT-PROGRAM.  Most likely this is nil.

  '(remsh GATEWAY-PROGRAM GATEWAY-PROGRAM-ARGS FTP-PROGRAM FTP-PROGRAM-ARGS)
  This indicates that you wish to run FTP on your gateway using a remote shell.
  GATEWAY-PROGRAM is the name of the program to use to start a remote shell.
  It is assumed that it is not necessary to provide a password to start
  this remote shell.  Likely values are \"remsh\" or \"rsh\".
  GATEWAY-PROGRAM-ARGS is a list of arguments to pass to GATEWAY-PROGRAM.
  FTP-PROGRAM is the name of the FTP program on the gateway.  A likely setting
  of this is \"ftp\".  FTP-PROGRAM-ARGS is a list of arguments to pass to 
  FTP-PROGRAM.  Most likely these should be set to the value of
  efs-ftp-program-args <V>.

  '(interactive GATEWAY-PROGRAM GATEWAY-PROGRAM-ARGS FTP-PROGRAM 
      FTP-PROGRAM-ARGS)
  This indicates that you need to start an interactive login on your gatway,
  using rlogin, telnet, or something similar.  GATEWAY-PROGRAM is the name
  of the program to use to log in to the gateway, and GATEWAY-PROGRAM-ARGS
  is a list of arguments to pass to it.  FTP-PROGRAM is the name of the FTP
  program on the gateway.  A likely setting for this variable would be
  \"exec ftp\".  FTP-PROGRAM-ARGS is a list of arguments to pass
  to FTP-PROGRAM.  You probably want to set these to the same value as
  efs-ftp-program-args <V>.  If you are using this option, read the
  documentation at the top of efs-gwp.el, and see 
  efs-gwp-setup-term-command <V>."
  :type '(choice (const nil)
                 (list :tag "Local"
                       :value (local "" nil)
                       (const :format "" local)
					   (file :tag "FTP Program")
					   (repeat :tag "FTP Program Args" string))
                 (list :tag "Proxy"
                       :value (proxy "" nil)
                       (const :format "" proxy)
					   (file :tag "FTP Program")
					   (repeat :tag "FTP Program Args" string))
				 (list :tag "Sidewinder"
					   :value (sidewinder "" nil)
					   (const :format "" sidewinder)
					   (file :tag "FTP Program")
					   (repeat :tag "FTP Program Args" string))
                 (list :tag "Raptor"
                       :value (raptor "" nil "")
                       (const :format "" raptor)
					   (file :tag "FTP Program")
					   (repeat :tag "FTP Program Args" string)
					   (string :tag "User"))
                 (list :tag "Interlock"
                       :value (interlock "" nil)
                       (const :format "" interlock)
					   (file :tag "FTP Program")
					   (repeat :tag "FTP Program Args" string))
                 (list :tag "Kerberos"
                       :value (kerberos "" nil "" nil)
                       (const :format "" kerberos)
					   (file :tag "FTP Program")
					   (repeat :tag "FTP Program Args" string)
					   (file :tag "KINIT Program")
					   (repeat :tag "KINIT Program Args" string))
                 (list :tag "Remsh"
                       :value (remsh "" nil "" nil)
                       (const :format "" remsh)
					   (file :tag "Gateway Program")
					   (repeat :tag "Gateway Program Args" string)
					   (file :tag "FTP Program")
					   (repeat :tag "FTP Program Args" string))
                 (list :tag "Interactive"
                       :value (interactive "" nil "" nil)
                       (const :format "" interactive)
					   (file :tag "Gateway Program")
					   (repeat :tag "Gateway Program Args" string)
					   (file :tag "FTP Program")
					   (repeat :tag "FTP Program Args" string)))
  :group 'efs-gateways)

(defcustom efs-gateway-hash-mark-size nil
  "*Value of `efs-hash-mark-size' for FTP clients on `efs-gateway-host'.
See the documentation of these variables for more information."
  :type '(choice integer
		 (const nil))
  :group 'efs-gateways)

(defcustom efs-gateway-incoming-binary-hm-size nil
  "*Value of `efs-incoming-binary-hm-size' for `efs-gateway-host'.
See documentation of these variables for more information."
  :type '(choice integer
		 (const nil))
  :group 'efs-gateways)

(defcustom efs-gateway-tmp-name-template "/tmp/efs"
  "Template used to create temporary files when ftp-ing through a gateway.
This should be the name of the file on the gateway, and not necessarily
the name on the local host."
  :type 'string
  :group 'efs-gateways)

(defcustom efs-gateway-mounted-dirs-alist nil
  "An alist of directories cross-mounted between the gateway and local host.
Each entry is of the form \( DIR1 . DIR2 \), where DIR1 is the name of the
directory on the local host, and DIR2 is its name on the remote host. Both
DIR1 and DIR2 must be specified in directory syntax, i.e. end in a slash.
Note that we will assume that subdirs of DIR1 and DIR2 are also accessible
on both machines."
  :type '(repeat (cons string string))
  :group 'efs-gateways)

(defcustom efs-gateway-ftp-prompt-regexp "^\\(ftp\\|Ftp\\|FTP\\)> *"
  "*Regular expression to match the prompt of the gateway FTP client."
  :type 'regexp
  :group 'efs-gateways)

;;; End of gateway config variables.

(defcustom efs-tmp-name-template
  (concat (if (fboundp 'temp-directory) (temp-directory) "/tmp") "/efs")
  "Template used to create temporary files.
If you are worried about security, make this a directory in some
bomb-proof cave somewhere. efs does clean up its temp files, but
they do live for short periods of time."
  :type 'string
  :group 'efs-parameters)

(defcustom efs-generate-anonymous-password t
  "*If t, use a password of `user@host' when logging in as the anonymous user.
`host' is generated by the function `efs-system-fqdn'. If `system name' returns
a fully qualified domain name, `efs-system-fqdn' will return this. Otherwise,
it will attempt to use nslookup to obtain a fully qualified domain name. If
this is unsuccessful, the returned value will be the same as `system-name',
whether this is a fully qualified domain name or not.

If a string then use that as the password.

If nil then prompt the user for a password.

Beware that some operating systems, such as MVS, restrict substantially
the password length. The login will fail with a weird error message
if you exceed it."
  :type '(choice (const t)
		 string
		 (const nil))
  :group 'efs-behavior)

(defcustom efs-high-security-hosts nil
  "*Indicates host user pairs for which passwords should not be cached.
If non-nil, should be a regexp matching user@host constructions for which
efs should not store passwords in its internal cache."
  :type '(repeat (cons string string))
  :group 'efs-parameters)

;; The following regexps are tested in the following order:
;; efs-binary-file-host-regexp, efs-36-bit-binary-file-name-regexp,
;; efs-binary-file-name-regexp, efs-text-file-name-regexp.
;; File names which match nothing are transferred in 'image mode.

;; If we're not careful, we're going to blow the regexp stack here.
;; Probably should move to a list of regexps. Slower, but safer.
;; This is not a problem in Emacs 19.
(defcustom efs-binary-file-name-regexp
  (concat "\\." ; the dot
	  ;; extensions
	  "\\([zZ]\\|t?gz\\|lzh\\|arc\\|zip\\|zoo\\|ta[rz]\\|dvi\\|sit\\|"
	  "ps\\|elc\\|gif\\|Z-part-..\\|tpz\\|exe\\|[jm]pg\\|TZ[a-z]?\\|lib\\)"
	  "\\(~\\|~[0-9]+~\\)?$" ; backups
	  "\\|"
	  ;; UPPER CASE LAND
	  "\\."
	  "\\(ARC\\|ELC\\|TAGS\\|EXE\\|ZIP\\|DVI\|ZOO\\|GIF\\|T?GZ\\|"
	  "[JM]PG\\)"
	  "\\([.#;][0-9]+\\)?$" ; versions
	  )
  "*Files whose names  match this regexp will be considered to be binary.
By binary here, we mean 8-bit binary files (the usual unix binary files).
If nil, no files will be considered to be binary."
  :type '(repeat regexp)
  :group 'efs-parameters)

(defcustom efs-binary-file-host-regexp nil
  "*All files on hosts matching this regexp are treated as 8-bit binary.
Setting this to nil, inhibits this feature."
  :type '(choice (const nil)
		 regexp)
  :group 'efs-parameters)

(defcustom efs-36-bit-binary-file-name-regexp nil
  "*Files whose names match this regexp will be considered to PDP 10 binaries.
These are 36-bit word-aligned binary files. This is really only relevant for
files on PDP 10's, and similar machines. If nil, no files will be considered
to be PDP 10 binaries."
  :type '(choice (const nil)
		 regexp)
  :group 'efs-parameters)


(defcustom efs-text-file-name-regexp ".*"
  "*Files whose names match this regexp will be considered to be text files."
  :type '(choice (const nil)
		 regexp)
  :group 'efs-parameters)

(defcustom efs-prompt-for-transfer-type nil
  "*If non-nil, efs will prompt for the transfer type for each file transfer.
The command efs-prompt-for-transfer-type can be used to toggle its value."
  :type 'boolean
  :group 'efs-behavior)

(defcustom efs-treat-crlf-as-nl nil
  "*Controls how file systems using CRLF as end of line are treated.
If non-nil, such file systems will be considered equivalent to those which use
LF as end of line.  This is particularly relevant to transfers between DOS
systems and UNIX.  Setting this to be non-nil will cause all file transfers
between DOS and UNIX systems to use be image or binary transfers."
  :type 'boolean
  :group 'efs-parameters)

(defcustom efs-send-hash t
  "*If non-nil, send the HASH command to the FTP client."
  :type 'boolean
  :group 'efs-parameters)

(defcustom efs-hash-mark-size nil
  "*Default size, in bytes, between hash-marks when transferring a file.
If this is nil then efs will attempt to assign a value based on the
output of the HASH command. Also, if this variable is incorrectly set,
then efs will try to correct it based on the size of the last file
transferred, and the number hashes outputed by the client during the
transfer.

The variable `efs-gateway-hash-mark-size' defines the corresponding value
for the FTP client on the gateway, if you are using a gateway. 

Some client-server combinations do not correctly compute the number of hash
marks for incoming binary transfers. In this case, a separate variable
`efs-incoming-binary-hm-size' can be used to set a default value of the
hash mark size for incoming binary transfers."
  :type '(choice integer
		 (const nil))
  :group 'efs-parameters)

(defcustom efs-incoming-binary-hm-size nil
  "*Default hash mark size for incoming binary transfers.
If this is nil, incoming binary transfers will use `efs-hash-mark-size' as 
the default. See the documentation of this variable for more details."
  :type '(choice integer
		 (const nil))
  :group 'efs-parameters)

(defcustom efs-verbose t
  "*If non-NIL then be chatty about interaction with the FTP process.
If 0 do not give % transferred reports for asynchronous commands and status
reports for commands verifying file modtimes, but report on everything else."
  :type 'boolean
  :group 'efs-behavior)

(defcustom efs-message-interval 0
  "*Defines the minimum time in seconds between status messages.
A new status message is not displayed if one has already been given
within this period of time."
  :type 'integer
  :group 'efs-behavior)

(defcustom efs-max-ftp-buffer-size 3000
  "*Maximum size in characters of FTP process buffer, before it is trimmed.
The buffer is trimmed to approximately half this size. Setting this to nil
inhibits trimming of FTP process buffers."
  :type 'integer
  :group 'efs-parameters)

(defcustom efs-ls-cache-max 5
  "*Maximum number of directory listings to be cached in efs-ls-cache."
  :type 'integer
  :group 'efs-parameters)

(defcustom efs-mode-line-format " ftp(%d)"
  "Format string used to determine how FTP activity is shown on the mode line.
It is passed to format, with second argument the number of active FTP
sessions as an integer."
  :type 'string
  :group 'efs-behavior)

(defcustom efs-show-host-type-in-dired t
  "If non-nil, show the system type on the mode line of remote dired buffers."
  :type 'boolean
  :group 'efs-behavior)

(defcustom efs-ftp-activity-function nil
  "Function called to indicate FTP activity. 
It must have exactly one argument, the number of active FTP sessions as an
integer."
  :type '(choice function
		 (const nil))
  :group 'efs-hooks)

(defcustom efs-ftp-program-name "ftp"
  "Name of FTP program to run."
  :type 'string
  :group 'efs-programs)

(defcustom efs-ftp-program-args '("-i" "-n" "-g" "-v")
  "*A list of arguments passed to the FTP program when started."
  :type '(repeat string)
  :group 'efs-programs)

(defcustom efs-ftp-prompt-regexp "^\\(ftp\\|Ftp\\|FTP\\)> *"
  "*Regular expression to match the prompt of your FTP client."
  :type 'regexp
  :group 'efs-programs)

;; This works around a bug in the NT ftp client, really.

(defcustom efs-ftp-flush-command (and (eq system-type 'windows-nt) "help help")
  "If non-NIL, get the FTP client to flush output by sending command."
  :type '(choice string
		 (const nil))
  :group 'efs-programs)

(defcustom efs-nslookup-program
  (and (not (eq system-type 'windows-nt)) "nslookup")
  "*If non-NIL then a string naming the nslookup program."
  :type '(choice string
		 (const nil))
  :group 'efs-programs)

(defcustom efs-nslookup-on-connect nil
  "*If non-NIL then use nslookup to resolve the host name before connecting."
  :type 'boolean
  :group 'efs-programs)

(defcustom efs-nslookup-threshold 5000
  "How many iterations efs waits on the nslookup program.
Applies when nslookup is used to compute a fully qualified domain name
for the local host, in the case when `system-name' does not return one.
If you set this to nil, efs will wait an arbitrary amount of time to get
output."
  :type 'integer
  :group 'efs-programs)

(defcustom efs-remote-shell-file-name
  (if (memq system-type '(hpux usg-unix-v)) ; hope that's right
      "remsh"
    "rsh")
  "Remote shell used by efs."
  :type 'string
  :group 'efs-programs)

(defcustom efs-use-remote-shell-internally t
  "Set to non-nil if efs is to use remote shell commands internally.
If non-nil, efs will attempt to use remote shell commands to implement
certain commands the ftp server lacks such as chmod."
  :type 'boolean
  :group 'efs-programs)

(defcustom efs-remote-shell-takes-user
  (null (null (memq system-type '(aix aix-v3 hpux silicon-graphics-unix
				      berkeley-unix))))
  ;; Complete? Doubt it.
  "Set to non-nil if your remote shell command takes \"-l USER\"."
  :type 'boolean
  :group 'efs-programs)

(defcustom efs-umask-command
  (and (not (eq system-type 'windows-nt)) "umask")
  "Shell command to print out umask."
  :type '(choice string
		 (const nil))
  :group 'efs-programs)

(defcustom efs-make-backup-files efs-unix-host-types
  "*A list of operating systems for which efs will make Emacs backup files.
The backup files are made on the remote host.

For example:
'\(unix sysV-unix bsd-unix apollo-unix dumb-unix\) makes sense, but
'\(unix vms\) would be silly, since vms makes its own backups."
  :type '(repeat symbol)
  :group 'efs-behavior)

;; Is this variable really useful? We should try to figure a way to
;; do local copies on a remote machine that doesn't take forever.
(defcustom efs-backup-by-copying nil
  "*Version of `backup by copying' for remote files.
If non-nil, remote files will be backed up by copying, instead of by renaming.
Note the copying will be done by moving the file through the local host -- a 
very time consuming operation."
  :type 'boolean
  :group 'efs-behavior)

;;; Auto-save variables. Relevant for auto-save.el

(defcustom efs-auto-save 0
  "*If 1, allows efs files to be auto-saved.
If 0, suppresses auto-saving of efs files.
Don't use any other value."
  :type '(choice (const 0)
		 (const 1))
  :group 'efs-auto-save)

(defcustom efs-auto-save-remotely nil
  "*Determines where remote files are auto-saved.

If nil, auto-saves for remote files will be written in `auto-save-directory'
or `auto-save-directory-fallback' if this isn't defined.

If non-nil, causes the auto-save file for an efs file to be written in
the remote directory containing the file, rather than in a local directory.
For remote files, this overrides a non-nil `auto-save-directory'. Local files
are unaffected. If you want to use this feature, you probably only want to 
set this true in a few buffers, rather than globally.  You might want to give
each buffer its own value using `make-variable-buffer-local'. It is usually
a good idea to auto-save remote files locally, because it is not only faster,
but provides protection against a connection going down.

See also variable `efs-auto-save'."
  :type 'boolean
  :group 'efs-auto-save)

(defcustom efs-short-circuit (not (string-equal (expand-file-name "//") "//"))
  "*Defines whether expand-file-name and substitute-in-file-name short-circuit
\"//\" and \"/~\"."
  :type 'boolean
  :group 'efs-parameters)

(defcustom efs-short-circuit-to-remote-root nil
  "*Defines whether \"//\" short-circuits to the remote or local root."
  :type 'boolean
  :group 'efs-parameters)

;; Can we somehow grok this from system type?  No.
(defcustom efs-local-apollo-unix
  (eq 0 (string-match "//" (or (user-home-directory) (getenv "SHELL") "")))
  "*Defines whether the local machine is an apollo running Domain.
This variable has nothing to do with efs, and should be basic to all 
of emacs."
  :type 'boolean
  :group 'efs-parameters)

(defcustom efs-root-umask nil
  "*umask to use for root logins."
  :type '(choice integer
		 (const nil))
  :group 'efs-parameters)

(defcustom efs-anonymous-umask nil
  "*umask to use for anonymous logins."
  :type '(choice integer
		 (const nil))
  :group 'efs-parameters)

(defcustom efs-umask nil
  "*umask to use for efs sessions.
If this is nil, then the setting of umask on the local host is used."
  :type '(choice integer
		 (const nil))
  :group 'efs-parameters)

;; Eliminate these variables when Sun gets around to getting its FTP server
;; out of the stone age.
(defcustom efs-ding-on-umask-failure t
  "*Ring the bell if the umask command fails on a unix host. Many servers don't
support this command, so if you get a lot of annoying failures, set this
to nil."
  :type 'boolean
  :group 'efs-behavior)

(defcustom efs-ding-on-chmod-failure t
  "*Ring the bell if the chmod command fails on a unix host. Some servers don't
support this command, so if you get a lot of annoying failures, set this
to nil."
  :type 'boolean
  :group 'efs-behavior)

;; Please let us know if you can contribute more entries to this guessing game.
(defcustom efs-nlist-cmd
  (cond
   ;; Covers Ultrix, SunOS, and NeXT.
   ((eq system-type 'berkeley-unix)
    "ls")
   ((memq system-type '(hpux aix-v3 silicon-graphics-unix))
    "nlist")
   ;; Blind guess
   ("ls"))
  "*FTP client command for getting a brief listing (NLST) from the FTP server. 
We try to guess this based on the local system-type, but obviously if you
are using a gateway, you'll have to set it yourself."
  :type 'string
  :group 'efs-programs)

(defcustom efs-compute-remote-buffer-file-truename nil
  "*If non-nil, `buffer-file-truename' will be computed for remote buffers.
In emacs 19, each buffer has a local variable, `buffer-file-truename',
which is used to ensure that symbolic links will not confuse emacs into
visiting the same file with two buffers. This variable is computed by
chasing all symbolic links in `buffer-file-name', both at the level of the
file and at the level of all parent directories. Since this operation can be
very time-consuming over FTP, this variable can be used to inhibit it."
  :type 'boolean
  :group 'efs-parameters)

(defcustom efs-buffer-name-case nil
  "*Selects the case used for buffer names of case-insensitive file names.
Case-insensitive file names are files on hosts whose host type is in
`efs-case-insensitive-host-types'.

If this is 'up upper case is used, if it is 'down lower case is used. 
If this has any other value, the case is inherited from the name used 
to access the file."
  :type '(choice (const nil)
		 (const up)
		 (const down))
  :group 'efs-behavior)

(defcustom efs-fancy-buffer-names "%s@%s"
  "Format used to compute names of buffers attached to remote files.

If this is nil, buffer names are computed in the usual way.

If it is a string, then the it is passed to format with second and third
arguments the host name and file name.

Otherwise, it is assumed to be function taking three arguments, the host name,
the user name, and the truncated file name.  It should returns the name to
be used for the buffer."
  :type '(choice string
		 function)
  :group 'efs-behavior)

(defcustom efs-verify-anonymous-modtime nil
  "*Determines if efs checks modtimes for remote files on anonymous logins.
If non-nil, efs runs `verify-visited-file-modtime' for remote files on 
anonymous ftp logins. Since verify-visited-file-modtime slows things down,
and most people aren't editing files on anonymous ftp logins, this is nil
by default."
  :type 'boolean
  :group 'efs-parameters)

(defcustom efs-verify-modtime-host-regexp ".*"
  "*Regexp to match host names for which efs checks file modtimes.
If non-nil, efs will run `verify-visited-file-modtime' for remote
files on hosts matching this regexp. If nil, verify-visited-file-modtime
is supressed for all remote hosts. This is tested before
`efs-verify-anonymous-modtime'."
  :type 'regexp
  :group 'efs-parameters)

(defcustom efs-maximize-idle nil
  "*If non-nil, efs will attempt to maximize the idle time out period.
At some idle moment in the connection after login, efs will attempt to
set the idle time out period to the maximum amount allowed by the server.
It applies only to non-anonymous logins on unix hosts."
  :type 'boolean
  :group 'efs-parameters)

(defcustom efs-expire-ftp-buffers t
  "*If non-nil ftp buffers will be expired.
The buffers will be killed either after `efs-ftp-buffer-expire-time' has
elapsed with no activity, or the remote FTP server has timed out."
  :type 'boolean
  :group 'efs-parameters)

(defcustom efs-ftp-buffer-expire-time nil
  "*If non-nil, the time after which ftp buffers will be expired.
If nil, ftp buffers will be expired only when the remote server has timed out.
If an integer, ftp buffers will be expired either when the remote server
has timed out, or when this many seconds on inactivity has elapsed."
  :type '(choice (const nil)
		 integer)
  :group 'efs-parameters)

;; If you need to increase this variable much, it is likely that
;; the true problem is timing errors between the efs process filter
;; and the FTP server. This could either be caused by the server
;; not following RFC959 response codes, or a bug in efs. In either
;; case please report the problem to us. If it's a bug, we'll fix it.
;; If the server is at fault we may try to do something. Our rule
;; of thumb is that we will support non-RFC959 behaviour, as long as
;; it doesn't risk breaking efs for servers which behave properly.

(defcustom efs-retry-time 5
  "*Number of seconds to wait before retrying if data doesn't arrive.
The FTP command isn't retried, rather efs just takes a second look
for the data file. This might need to be increased for very slow FTP
clients."
  :type 'integer
  :group 'efs-parameters)

(defcustom efs-pty-check-threshold 1000
  "*How long efs waits before deciding that it doesn't have a pty.
Specifically it is the number of iterations through `accept-process-output'
that `efs-pty-p' waits before deciding that the pty is really a pipe.
Set this to nil to inhibit checking for pty's. If efs seems to be
mistaking some pty's for pipes, try increasing this number."
  :type 'integer
  :group 'efs-parameters)

(defcustom efs-pty-check-retry-time 5
  "*Number of seconds that efs waits before retrying a pty check.
This can be lengthened, if your FTP client is slow to start."
  :type 'integer
  :group 'efs-parameters)

(defcustom efs-suppress-abort-recursive-edit-and-then nil
  "*If non-nil, `efs-abort-recursive-edit-and-then' will not run its function.
This means that when a recursive edit is in progress, automatic popping of the
FTP process buffer, and automatic popping of the bug report buffer will not
work. `efs-abort-recursive-edit-and-then' works by forking a \"sleep 0\"
process. On some unix implementations the forked process might be of the same
size as the original GNU Emacs process. Forking such a large process just to
do a \"sleep 0\" is probably not good."
  :type 'boolean
  :group 'efs-parameters)

(defcustom efs-ftp-buffer-format "*ftp %s@%s*"
  "Format to construct the name of FTP process buffers.
This string is fed to `format' with second and third arguments the user
name and host name."
  :type 'string
  :group 'efs-behavior)

;; This does not affect the process name of the FTP client process.
;; That is always *ftp USER@HOST*

(defcustom efs-debug-ftp-connection nil
  "*If non-nil, the user will be permitted to debug the FTP connection.
This means that typing a C-g to the FTP process filter will give the user
the option to type commands at the FTP connection.  Normally, the connection
is killed first.  Note that doing this may result in the FTP process filter
getting out of synch with the FTP client, so using this feature routinely
isn't recommended."
  :type 'boolean
  :group 'efs-behavior)

(defcustom efs-use-passive-mode nil
  "*If non-nil, the ftp client will specify passive mode for all transfers."
  :type 'boolean
  :group 'efs-parameters)

;;; Hooks and crooks.

(defcustom efs-ftp-startup-hook nil
  "Hook to run immediately after starting the FTP client.
This hook is run before the FTP OPEN command is sent."
  :type 'hook
  :group 'efs-hooks)

(defcustom efs-ftp-startup-function-alist nil
  "Association list of functions to running after FTP login.
This should be an alist of the form '\(\(REGEXP . FUNCTION\) ...\), where
REGEXP is a regular expression matched against the name of the remote host,
and FUNCTION is a function of two arguments, HOST and USER. REGEXP is
compared to the host name with `case-fold-search' bound to t. Only the first
match in the alist is run."
  :type '(repeat (cons regexp function))
  :group 'efs-hooks)

(defcustom efs-load-hook nil
  "Hook to run immediately after loading efs.el.
You can use it to alter definitions in efs.el, but why would you want 
to do such a thing?"
  :type 'hook
  :group 'efs-hooks)

;;;; -----------------------------------------------------------
;;;; Regexps for parsing FTP server responses.
;;;; -----------------------------------------------------------
;;;
;;;  If you have to tune these variables, please let us know, so that
;;;  we can get them right in the next release.

(defvar efs-multi-msgs
  ;; RFC959 compliant codes
  "^[1-5][0-5][0-7]-")
;; Regexp to match the start of an FTP server multiline reply.

(defvar efs-skip-msgs
  ;; RFC959 compliant codes
  (concat
   "^110 \\|" ; Restart marker reply.
   "^125 \\|" ; Data connection already open; transfer starting.
   "^150 ")) ; File status OK; about to open connection.
;; Regexp to match an FTP server response which we wish to ignore.

(defvar efs-cmd-ok-msgs
  ;; RFC959 compliant
  "^200 \\|^227 ")
;; Regexp to match the server command OK response.
;; Because PORT commands return this we usually ignore it. However, it is
;; a valid response for TYPE, SITE, and a few other commands (cf. RFC 959).
;; If we are explicitly sending a PORT, or one of these other commands, 
;; then we don't want to ignore this response code.  Also use this to match
;; the return code for PASV, as some clients burp these things out at odd
;; times.

(defvar efs-pending-msgs
  ;; RFC959 compliant
  "^350 ") ; Requested file action, pending further information.
;; Regexp to match the \"requested file action, pending further information\"
;; message. These are usually ignored, except if we are using RNFR to test for
;; file existence.

(defvar efs-cmd-ok-cmds
  (concat
   "^quote port \\|^type \\|^quote site \\|^chmod \\|^quote noop\\|"
   "^quote pasv\\|^passive"))
;; Regexp to match commands for which efs-cmd-ok-msgs is a valid server
;; response for success.

(defvar efs-passwd-cmds
  "^quote pass \\|^quote acct \\|^quote site gpass ")
;; Regexp to match commands for sending passwords.
;; All text following (match-end 0) will be replaced by "Turtle Power!"

(defvar efs-bytes-received-msgs
  ;; Strictly a client response
  "^[0-9]+ bytes ")
;; Regexp to match the reply from the FTP client that it has finished
;; receiving data.

(defvar efs-server-confused-msgs
  ;; ka9q uses this to indicate an incorrectly set transfer mode, and
  ;; then does send a second completion code for the command. This does
  ;; *not* conform to RFC959.
  "^100 Warning: type is ")
;; Regexp to match non-standard response from the FTP server. This can 
;; sometimes be the result of an incorrectly set transfer mode. In this case
;; we do not rely on the server to tell us when the data transfer is complete,
;; but check with the client.

(defvar efs-good-msgs
  (concat
   ;; RFC959 compliant codes
   "^2[01345][0-7] \\|" ; 2yz = positive completion reply
   "^22[02-7] \\|"      ; 221 = successful logout
			; (Sometimes get this with a timeout,
			; so treat as fatal.)
   "^3[0-5][0-7] \\|"    ; 3yz = positive intermediate reply
   ;; passive
   "^[Pp]assive \\|"
   ;; client codes
   "^[Hh]ash mark "))
;; Response to indicate that the requested action was successfully completed.

(defvar efs-failed-msgs
  (concat
   ;; RFC959 compliant codes
   "^120 \\|"       ; Service ready in nnn minutes.
   "^450 \\|"       ; File action not taken; file is unavailable, or busy.
   "^452 \\|"       ; Insufficient storage space on system.
   "^5[0-5][0-7] \\|" ; Permanent negative reply codes.
   ;; When clients tell us that	a file doesn't exist, or can't access.
   "^\\(local: +\\)?/[^ ]* +"
   "\\([Nn]o such file or directory\\|[Nn]ot a plain file\\|"
   "The file access permissions do not allow \\|Is a directory\\b\\)"))
;; Regexp to match responses for failed commands. However, the ftp connection
;; is assumed to be good.

(defvar efs-fatal-msgs
  (concat
   ;; RFC959 codes
   "^221 \\|" ; Service closing control connection.
   "^421 \\|" ; Service not available.
   "^425 \\|" ; Can't open data connection.
   "^426 \\|" ; Connection closed, transfer aborted.
   "^451 \\|" ; Requested action aborted, local error in processing.
   ;; RFC959 non-compliant codes
   "^552 Maximum Idle Time Exceded\\.$\\|" ; Hellsoft server uses this to
					   ; indicate a timeout. 552 is
					   ; supposed to be used for exceeded
					   ; storage allocation. Note that
					   ; they also misspelled the error
					   ; message.
   ;; client problems
   "^ftp: \\|^Not connected\\|^rcmd: \\|^No control connection\\|"
   "^unknown host\\|: unknown host$\\|^lost connection\\|"
   "^[Ss]egmentation fault\\|"
   ;; Make sure that the "local: " isn't just a message about a file.
   "^local: [^/]\\|"
   ;; Gateways
   "^iftp: cannot authenticate to server\\b"
   ))
;; Regexp to match responses that something has gone drastically wrong with
;; either the client, server, or connection. We kill the ftp process, and start
;; anew.

(defvar efs-unknown-response-msgs
  "^[0-9][0-9][0-9] ")
;; Regexp to match server response codes that we don't understand. This
;; is tested after all the other regexp, so it can match everything.

(defvar efs-pasv-msgs
  ;; According to RFC959.
  "^227 .*(\\([0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+\\))$")
;; Matches the output of a PASV. (match-beginning 1) and (match-end 1)
;; must bracket the IP address and port.

(defvar efs-syst-msgs "^215 \\|^210 ")
;; 215 is RFC959. Plan 9 FTP server returns a 210. 210 is not assigned in
;; RFC 959.
;; The plan 9 people tell me that they fixed this. -- sr 18/4/94
;; Matches the output of a SYST.

(defvar efs-mdtm-msgs
  (concat
   "^213 [0-9][0-9][0-9][0-9][0-9][0-9][0-9]"
   "[0-9][0-9][0-9][0-9][0-9][0-9][0-9]$"))
;; Regexp to match the output of a quote mdtm command.

(defvar efs-idle-msgs
  "^200 [^0-9]+ \\([0-9]+\\)[^0-9]* max \\([0-9]+\\)")
;; Regexp to match the output of a SITE IDLE command.
;; Match 1 should refer to the current idle time, and match 2 the maximum 
;; idle time.

(defvar efs-write-protect-msgs "^532 ") ; RFC959
;; Regexp to match a server ressponse to indicate that a STOR failed
;; because of insufficient write privileges.

(defvar efs-hash-mark-msgs
  "[hH]ash mark [^0-9]*\\([0-9]+\\)")
;; Regexp matching the FTP client's output upon doing a HASH command.

(defvar efs-xfer-size-msgs
  (concat
   ;; UN*X
   "^150 .* connection for .* (\\([0-9]+\\) bytes)\\|"
   ;; Wollongong VMS server.
   "^125 .* transfer started for .* (\\([0-9]+\\) bytes)\\|"
   ;; TOPS-20 server
   "^150 .* retrieve of .* ([0-9]+ pages?, \\([0-9]+\\) 7-bit bytes)"))
;; Regular expression used to determine the number of bytes
;; in a FTP transfer. The first (match-beginning #) which is non-nil is assumed
;; to give the size.

(defvar efs-expand-dir-msgs "^550 \\([^: ]+\\):")
;; Regexp to match the error response from a "get ~sandy".
;; By parsing the error, we can get a quick expansion of ~sandy
;; According to RFC 959, should be a 550.

(defvar efs-gateway-fatal-msgs
  "No route to host\\|Connection closed\\|No such host\\|Login incorrect")
;; Regular expression matching messages from the rlogin / telnet process that
;; indicates that logging in to the gateway machine has gone wrong.

(defvar efs-too-many-users-msgs
  ;; The test for "two many" is because some people can't spell.
  ;; I allow for up to two adjectives before "users".
  (concat
   "\\b[Tt][wo]o many\\( +[^ \n]+\\)?\\( +[^ \n]+\\)? +users\\b\\|"
   "\\btry back later\\b"))
;; Regular expresion to match what servers output when there are too many
;; anonymous logins.  It is assumed that this is part of a 530 or 530- response
;; to USER or PASS.

;;;; -------------------------------------------------------------
;;;; Buffer local FTP process variables
;;;; -------------------------------------------------------------

;;; Variables buffer local to the process buffers are
;;; named with the prefix efs-process-

(defvar efs-process-q nil)
;; List of functions to be performed asynch.
(make-variable-buffer-local 'efs-process-q)

(defvar efs-process-cmd-waiting nil)
;; Set to t if a process has a synchronous cmd waiting to execute.
;; In this case, it will allow the synch. cmd to run before returning to
;;  the cmd queue.
(make-variable-buffer-local 'efs-process-cmd-waiting)

(defvar efs-process-server-confused nil)
(make-variable-buffer-local 'efs-process-server-confused)

(defvar efs-process-cmd nil)
;; The command currently being executed, as a string.
(make-variable-buffer-local 'efs-process-cmd)

(defvar efs-process-xfer-size 0)
(make-variable-buffer-local 'efs-process-xfer-size)

(defvar efs-process-umask nil)
;; nil if the umask hash not been set
;; an integer (the umask) if the umask has been set
(make-variable-buffer-local 'efs-process-umask)

(defvar efs-process-idle-time nil)
;; If non-nil, the idle time of the server in seconds.
(make-variable-buffer-local 'efs-process-idle-time)

(defvar efs-process-busy nil)
(make-variable-buffer-local 'efs-process-busy)

(defvar efs-process-result-line "")
(make-variable-buffer-local 'efs-process-result-line)

(defvar efs-process-result nil)
(make-variable-buffer-local 'efs-process-result)

(defvar efs-process-result-cont-lines "")
(make-variable-buffer-local 'efs-process-result-cont-lines)

(defvar efs-process-msg "")
(make-variable-buffer-local 'efs-process-msg)

(defvar efs-process-nowait nil)
(make-variable-buffer-local 'efs-process-nowait)

(defvar efs-process-string "")
(make-variable-buffer-local 'efs-process-string)

(defvar efs-process-continue nil)
(make-variable-buffer-local 'efs-process-continue)

(defvar efs-process-hash-mark-count 0)
(make-variable-buffer-local 'efs-process-hash-mark-count)

(defvar efs-process-hash-mark-unit nil)
(make-variable-buffer-local 'efs-process-hash-mark-unit)

;; History of hash-mark counts and times to compute transfer rate
(defvar efs-process-hash-mark-history (list (list 0 (current-time))))
(make-variable-buffer-local 'efs-process-hash-mark-history)

(defvar efs-process-last-percent -1)
(make-variable-buffer-local 'efs-process-last-percent)

(defvar efs-process-host nil)
(make-variable-buffer-local 'efs-process-host)

(defvar efs-process-user nil)
(make-variable-buffer-local 'efs-process-user)

(defvar efs-process-host-type nil)
;; Holds the host-type as a string, for showing it on the mode line.
(make-variable-buffer-local 'efs-process-host-type)

(defvar efs-process-xfer-type nil)
;; Set to one of 'ascii, 'ebcdic, 'image, 'tenex, or nil to indicate
;; the current setting of the transfer type for the connection. nil means
;; that we don't know.
(make-variable-buffer-local 'efs-process-xfer-type)

(defvar efs-process-client-altered-xfer-type nil)
;; Sometimes clients alter the xfer type, such as doing
;; an ls it is changed to ascii. If we are using quoted commands
;; to do xfers the client doesn't get a chance to set it back.
(make-variable-buffer-local 'efs-process-client-altered-xfer-type)

(defvar efs-process-prompt-regexp nil)
;; local value of prompt of FTP client.
(make-variable-buffer-local 'efs-process-prompt-regexp)

(defvar efs-process-cmd-counter 0)
;; Counts FTP commands, mod 16.
(make-variable-buffer-local 'efs-process-cmd-counter)

;;;; ------------------------------------------------------------
;;;; General Internal Variables.
;;;; ------------------------------------------------------------

;;; For the byte compiler
;;
;;  These variables are usually unbound.  We are just notifying the
;;  byte compiler that we know what we are doing.

(defvar bv-length) ; getting file versions.
(defvar default-file-name-handler-alist) ; for file-name-handler-alist
(defvar efs-completion-dir) ; for file name completion predicates
(defvar dired-directory) ; for default actions in interactive specs
(defvar dired-local-variables-file) ; for inhibiting child look ups
(defvar dired-in-query) ; don't clobber dired queries with stat messages
(defvar after-load-alist) ; in case we're in emacs 18.
(defvar comint-last-input-start)
(defvar comint-last-input-end)
(defvar explicit-shell-file-name)

;;; fluid vars

(defvar efs-allow-child-lookup t)
;; let-bind to nil, if want to inhibit child lookups.

(defvar efs-nested-cmd nil)
;; let-bound to t, when a cmd is executed by a cont or pre-cont.
;; Such cmds will never end by looking at the next item in the queue,
;; if they are run synchronously, but rely on their calling function
;; to do this.

;;; polling ftp buffers

(defvar efs-ftp-buffer-poll-time 300
  "Period, in seconds, which efs will poll ftp buffers for activity.
Used for expiring \(killing\) inactive ftp buffers.")

(defconst efs-ftp-buffer-alist nil)
;; alist of ftp buffers, and the total number of seconds that they
;; have been idle.

;;; load extensions

(defvar efs-load-lisp-extensions '(".elc" ".el" "")
  "List of extensions to try when loading lisp files.")

;;; mode-line

(defvar efs-mode-line-string "")
;; Stores the string that efs displays on the mode line.

;;; data & temporary buffers

(defvar efs-data-buffer-name " *ftp data*")
;; Buffer name to hold directory listing data received from ftp process.

(defvar efs-data-buffer-name-2 " *ftp data-2*")
;; A second buffer name in which to hold directory listings.
;; Used for listings which are made during another directory listing.

;;; process names

(defvar efs-ctime-process-name-format "*efs ctime %s*")
;; Passed to format with second arg the host name.

;;; For temporary files.

;; This is a list of symbols.
(defconst efs-tmp-name-files ())
;; Here is where these symbols live:
(defconst efs-tmp-name-obarray (make-vector 7 0))
;; We put our version of the emacs PID here:
(defvar efs-pid nil)

;;; For abort-recursive-edit

(defvar efs-abort-recursive-edit-data nil)
(defvar efs-abort-recursive-edit-delay 5)
;; Number of seconds after which efs-abort-recursive-edit-and-then
;; will decide not to runs its sentinel. The assumption is that something
;; went wrong.

;;; hashtables (Use defconst's to clobber any user silliness.)

(defconst efs-files-hashtable (efs-make-hashtable 97))
;; Hash table for storing directories and their respective files.

(defconst efs-expand-dir-hashtable (efs-make-hashtable))
;; Hash table of tilde expansions for remote directories.

(defconst efs-ls-converter-hashtable (efs-make-hashtable 37))
;; Hashtable for storing functions to convert listings from one
;; format to another.  Keys are the required switches, and the values
;; are alist of the form ((SWITCHES . CONVERTER)...) where is SWITCHES
;; are the listing switches for the original listing, and CONVERTER is a
;; function of one-variable, the listing-type, to do the conversion
;; on data in the current buffer. SWITCHES is either a string, or nil.
;; nil means that the listing can be converted from cache in
;; efs-files-hashtable, a string from cache in efs-ls-cache.  For the latter,
;; listings with no switches (dumb listings), represent SWITCHES as a string
;; consisting only of the ASCII null character.

;;; cache variables (Use defconst's to clobber any user sillines.)

(defconst efs-ls-cache nil
  "List of results from efs-ls.
Each entry is a list of four elements, the file listed, the switches used
\(nil if none\), the listing string, and whether this string has already been
parsed.")

(defvar efs-ls-uncache nil)
;; let-bind this to t, if you want to be sure that efs-ls will replace any
;; cache entries.

;; This is a cache to see if the user has changed
;; completion-ignored-extensions.
(defconst efs-completion-ignored-extensions completion-ignored-extensions
  "This variable is internal to efs. Do not set.
See completion-ignored-extensions, instead.")

;; We cache the regexp we use for completion-ignored-extensions. This
;; saves building a string every time we do completion. String construction
;; is costly in emacs.
(defconst efs-completion-ignored-pattern
  (mapconcat (function
	      (lambda (s) (if (stringp s)
			      (concat (regexp-quote s) "$")
			    "/"))) ; / never in filename
	     efs-completion-ignored-extensions
	     "\\|")
  "This variable is internal to efs. Do not set.
See completion-ignored-extensions, instead.")

(defvar efs-system-fqdn nil
  "Cached value of the local systems' fully qualified domain name.")

;;; The file-type-alist

;; efs-file-type-alist is an alist indexed by host-type
;; which stores data on how files are structured on the given
;; host-type. Each entry is a list of three elements. The first is the
;; definition of a `byte', the second the native character representation,
;; and the third, the file structure.
;;
;; Meanings of the symbols:
;; ------------------------
;; The byte symbols:
;; 8-bit     = bytes of 8-bits
;; 36-bit-wa = 36-bit word aligned. Precisely, the addressing unit is that
;;             of a PDP-10 using the "<440700,,0> byte pointer".
;;
;; The native character set symbols:
;; 8-ascii = 8-bit NVT-ASCII
;; 7-ascii = 7-bit ascii as on a PDP-10
;; ebcdic  = EBCDIC as on an IBM mainframe
;; lispm   = the native character set on a lispm (Symbolics and LMI)
;; mts     = native character representation in the Michigan Terminal System
;;           (which runs on IBM and Amdal mainframes), similar to ebcdic
;;
;; The file structure symbols:
;;
;; file-nl    = data is stored as a contiguous sequence of data bytes
;;              with EOL denoted by <NL>.
;; file-crlf  = data is stored as a contiguous sequence of data bytes
;;              with EOL denoted by <CR-LF>
;; record     = data is stored as a sequence of records
;; file-lispm = data as stored on a lispm. i.e. a sequence of bits
;;              with EOL denoted by character code 138 (?)
;;
;; If we've messed anything up here, please let us know.

(defvar efs-file-type-alist
  '((unix . (8-bit 8-ascii file-nl))
    (sysV-unix . (8-bit 8-ascii file-nl))
    (bsd-unix . (8-bit 8-ascii file-nl))
    (apollo-unix . (8-bit 8-ascii file-nl))
    (dumb-apollo-unix . (8-bit 8-ascii file-nl))
    (dumb-unix . (8-bit 8-ascii file-nl))
    (super-dumb-unix . (8-bit 8-ascii file-nl))
    (guardian . (8-bit ascii file-nl))
    (plan9 . (8-bit 8-ascii file-nl))
    (dos . (8-bit 8-ascii file-crlf))
    (ms-unix . (8-bit 8-ascii file-crlf))
    (netware . (8-bit 8-ascii file-crlf))
    (os2 . (8-bit 8-ascii file-crlf))
    (tops-20 . (36-bit-wa 7-ascii file-crlf))
    (mpe . (8-bit 8-ascii record))
    (mvs . (8-bit ebcdic record))
    (cms . (8-bit ebcdic record))
    (cms-knet . (8-bit ebcdic record))
    (mts . (8-bit mts record)) ; mts seems to have its own char rep.
			       ; Seems to be close to ebcdic, but not the same.
    (dos-distinct . (8-bit 8-ascii file-crlf))
    (ka9q . (8-bit 8-ascii file-crlf))
    (vms . (8-bit 8-ascii record)) ; The mysteries of VMS's RMS.
    (hell . (8-bit 8-ascii file-crlf))
    (vos . (8-bit 8-ascii record))
    (ti-explorer . (8-bit lispm file-lispm)) ; lispms use a file structure, but
					     ; use an out of range char to
					     ; indicate EOL.
    (ti-twenex . (8-bit lispm file-lispm))
    (nos-ve . (8-bit 8-ascii record))
    (coke . (8-bit 8-ascii file-nl)) ; only support 8-bit beverages
    (nil . (8-bit 8-ascii file-nl)))) ; the local host

;;; Status messages

(defvar efs-last-message-time -86400) ; yesterday
;; The time of the last efs status message. c.f. efs-message-interval

;;; For handling dir listings

;; This MUST match all the way to to the start of the filename.
;; This version corresponds to what dired now uses (sandy, 14.1.93)
(defvar efs-month-and-time-regexp
  (concat
   " \\([0-9]+\\) +" ; file size
   "\\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|June?\\|July?\\|Aug\\|Sep\\|Oct"
					; June and July are for HP-UX 9.0
   "\\|Nov\\|Dec\\) \\([ 0-3][0-9]\\)\\("
   " [012][0-9]:[0-6][0-9] \\|"  ; time
   "  [12][90][0-9][0-9] \\|"    ; year on IRIX, NeXT, SunOS, ULTRIX, Apollo
				 ; HP-UX, A/UX
   " [12][90][0-9][0-9]  \\)"    ; year on AIX
   ))

(defvar efs-month-alist
  '(("Jan" . 1) ("Feb". 2) ("Mar" . 3) ("Apr" . 4) ("May" . 5) ("Jun" . 6)
    ("June" . 6) ("Jul" . 7) ("July" . 7) ("Aug" . 8) ("Sep" . 9) ("Oct" . 10)
    ("Nov" . 11) ("Dec" . 12)))

;; Matches the file modes, link number, and owner string.
;; The +/- is for extended file access permissions.
(defvar efs-modes-links-owner-regexp
  (concat
   "\\([^ ][-r][-w][^ ][-r][-w][^ ][-r][-w][^ ]\\)[-+]? *\\([0-9]+\\)"
   " +\\([^ ]+\\) "))
  
;;;; ---------------------------------------------------------------
;;;; efs-dired variables
;;;; ---------------------------------------------------------------

;; These variables must be here, instead of in efs-dired.el, because
;; the efs-HOST-TYPE.el files need to add to it.
(defvar efs-dired-re-exe-alist nil
  "Association list of regexps which match file lines of executable files.")

(defvar efs-dired-re-dir-alist nil
  "Association list of regexps which match file lines of subdirectories.")

(defvar efs-dired-host-type nil
  "Host type of a dired buffer. \(buffer local\)")
(make-variable-buffer-local 'efs-dired-host-type)

(defvar efs-dired-listing-type nil
  "Listing type of a dired buffer. \(buffer local\)")
(make-variable-buffer-local 'efs-dired-listing-type)

(defvar efs-dired-listing-type-string nil)
(make-variable-buffer-local 'efs-dired-listing-type-string)

;;;; -------------------------------------------------------------
;;;; New error symbols.
;;;; -------------------------------------------------------------

(put 'ftp-error 'error-conditions '(ftp-error file-error error))
;; (put 'ftp-error 'error-message "FTP error")


;;;; =============================================================
;;;; >3
;;;; Utilities
;;;; =============================================================

;;; -------------------------------------------------------------------
;;; General Macros (Make sure that macros are defined before they're
;;;                 used, for the byte compiler.
;;; -------------------------------------------------------------------

(defmacro efs-kbd-quit-protect (proc &rest body)
  ;; When an efs function controlling an FTP connection gets a kbd-quit
  ;; this tries to make sure that everything unwinds consistently.
  (let ((temp (make-symbol "continue")))
    (list 'let
	  (list '(quit-flag nil)
		'(inhibit-quit nil)
		(list temp t))
	  (list
	   'while temp
	   (list 'setq temp nil)
	   (list
	    'condition-case nil
	    (cons 'progn
		  body)
	    (list 'quit
		  (list 'setq temp
			(list 'efs-kbd-quit-protect-cover-quit proc))))))))

(defun efs-kbd-quit-protect-cover-quit (proc)
  ;; This function exists to keep the macro expansion of the
  ;; efs-kbd-quit-protect down to a reasonable size.
  (let ((pop-up-windows t)
	(buff (get-buffer (process-buffer proc)))
	res)
    (if (save-window-excursion
	  (if buff
	      (progn
		(pop-to-buffer buff)
		(goto-char (point-max))
		(recenter (- (window-height)
			     2))))
	  (setq res (efs-kill-ftp-buffer-with-prompt proc buff)))
	(progn
	  (if (eq res 0)
	      (if (eq (selected-window)
		      (minibuffer-window))
		  (efs-abort-recursive-edit-and-then
		   (function
		    (lambda (buff)
		      (if (get-buffer buff)
			  (display-buffer buff))))
		   buff)
		(if (get-buffer buff)
		    (display-buffer buff))
		(signal 'quit nil))
	    (if (eq (selected-window) (minibuffer-window))
		(abort-recursive-edit)
	      (signal (quote quit) nil)))
	  nil)
      (sit-for 0)
      (message "Waiting on %s..." (or (car (efs-parse-proc-name proc))
				      "a whim"))
      t)))

(put 'efs-kbd-quit-protect 'lisp-indent-hook 1)

(defmacro efs-save-buffer-excursion (&rest forms)
  "Execute FORMS, restoring the current buffer afterwards.
Unlike, save-excursion, this does not restore the point."
  (let ((temp (make-symbol "saved-buff")))
    (list 'let
	  (list (list temp '(current-buffer)))
	  (list 'unwind-protect
		(cons 'progn forms)
		(list 'condition-case nil
		      (list 'set-buffer temp)
		      '(error nil))))))

(put 'efs-save-buffer-excursion 'lisp-indent-hook 0)

(defmacro efs-unquote-dollars (string)
  ;; Unquote $$'s to $'s in STRING.
  (` (let ((string (, string))
	   (start 0)
	   new)
       (while (string-match "\\$\\$" string start)
	 (setq new (concat new (substring
				string start (1+ (match-beginning 0))))
	       start (match-end 0)))
       (if new
	   (concat new (substring string start))
	 string))))

(defmacro efs-get-file-part (path)
  ;; Given PATH, return the file part used for looking up the file's entry
  ;; in a hashtable.
  ;; This need not be the same thing as file-name-nondirectory.
  (` (let ((file (file-name-nondirectory (, path))))
       (if (string-equal file "")
	   "."
	 file))))

(defmacro efs-ftp-path-macro (path)
  ;; Just a macro version of efs-ftp-path, for speed critical
  ;; situations. Could use (inline ...) instead, but not everybody
  ;; uses the V19 byte-compiler. Also, doesn't call efs-save-match-data,
  ;; but assumes that the calling function does it.
  (`
   (let ((path (, path)))
     (or (string-equal path efs-ftp-path-arg)
	 (setq efs-ftp-path-res
	       (and (string-match efs-path-regexp path)
		    (let ((host (substring path (match-beginning 2)
					   (match-end 2)))
			  (user (and (match-beginning 1)
				     (substring path (match-beginning 1)
						(1- (match-end 1)))))
			  (rpath (substring path (1+ (match-end 2)))))
		      (list (if (string-equal host "")
				(setq host (system-name))
			      host)
			    (or user (efs-get-user host))
			    rpath)))
	       ;; Set this last, in case efs-get-user calls this function,
	       ;; which would modify an earlier setting.
	       efs-ftp-path-arg path))
     efs-ftp-path-res)))

(defmacro efs-canonize-switches (switches)
  ;; Converts a switches string, into a lexographically ordered string,
  ;; omitting - and spaces.  Should we remove duplicate characters too?
  (` (if (, switches)
	 (mapconcat
	  'char-to-string
	  (sort (delq ?- (delq ?\  (mapcar 'identity (, switches)))) '<) "")
       ;; For the purpose of interning in a hashtable, represent the nil
       ;; switches, as a string consisting of the ascii null character.
       (char-to-string 0))))

(defmacro efs-canonize-file-name (fn)
  ;; Canonizes the case of file names.
  (` (let ((parsed (efs-ftp-path (, fn))))
       (if parsed
	   (let ((host (car parsed)))
	     (if (memq (efs-host-type host) efs-case-insensitive-host-types)
		 (downcase (, fn))
	       (format efs-path-format-string (nth 1 parsed) (downcase host)
		       (nth 2 parsed))))
	 (, fn)))))

(defmacro efs-get-files-hashtable-entry (fn)
  (` (efs-get-hash-entry (efs-canonize-file-name (, fn)) efs-files-hashtable)))

;;;; ------------------------------------------------------------
;;;; Utility Functions
;;;; ------------------------------------------------------------

(defun efs-kill-ftp-buffer-with-prompt (proc buffer)
  ;; Does a 3-way prompt to kill a ftp PROC and BUFFER.
  ;; Returns t if buffer was killed, 0 if only process, nil otherwise.
  (let ((inhibit-quit t)
	(cursor-in-echo-area t)
	char)
    (message
     (if efs-debug-ftp-connection
	 "Kill ftp process and buffer (y[es], n[o], c[lose], d[ebug] ) "
       "Kill ftp process and buffer? (y or n, c to only close process) "))
    (setq char (read-char))
    (prog1
	(cond
	 ((memq char '(?y ?Y ?\ ))
	  (set-process-sentinel proc nil)
	  (condition-case nil
	      (kill-buffer buffer)
	    (error nil))
	  t)
	 ((memq char '(?c ?C))
	  (set-process-sentinel proc nil)
	  (condition-case nil
	      (save-excursion
		(set-buffer buffer)
		(setq efs-process-busy nil
		      efs-process-q nil)
		(delete-process proc))
	    (error nil))
	  0)
	 ((memq char '(?n ?N))
	  (message "")
	  nil)
	 ((and efs-debug-ftp-connection
	       (memq char '(?d ?D)))
	  (condition-case nil
	      (save-excursion
		(set-buffer buffer)
		(setq efs-process-busy nil
		      efs-process-q nil))
	    (error nil))
	  0)
	 (t
	  (message
	   (if efs-debug-ftp-connection
	       "Type one of y, n, c or d."
	     "Type one of y, n or c."))
	  (ding)
	  (sit-for 1)
	  (setq quit-flag nil)
	  (efs-kill-ftp-buffer-with-prompt proc buffer))))))

(defun efs-barf-if-not-directory (directory)
  ;; Signal an error if DIRECTORY is not one.
  (or (file-directory-p directory)
      (signal 'file-error
	      (list "Opening directory"
		    (if (file-exists-p directory)
			"not a directory"
		      "no such file or directory")
		    directory))))

(defun efs-call-cont (cont &rest args)
  "Call the function specified by CONT.
CONT can be either a function or a list of a function and some args.
The first parameters passed to the function will be ARGS.  The remaining
args will be taken from CONT if a list was passed."
  (if cont
      (let ((efs-nested-cmd t)) ; let-bound so that conts don't pop any queues
	(efs-save-buffer-excursion
	  (if (and (listp cont)
		   (not (eq (car cont) 'lambda)))
	      (apply (car cont) (append args (cdr cont)))
	    (apply cont args))))))

(defun efs-replace-path-component (fullpath path)
  "For FULLPATH matching efs-path-regexp replace the path component with PATH."
  (efs-save-match-data
    (if (string-match efs-path-root-regexp fullpath)
	(concat (substring fullpath 0 (match-end 0)) path)
      path)))

(defun efs-abort-recursive-edit-and-then (fun &rest args)
  ;; Does an abort-recursive-edit, and runs fun _after_ emacs returns to
  ;; top level.
  (if (get-process "efs-abort-recursive-edit")
      ;; Don't queue these things. Clean them out.
      (delete-process "efs-abort-recursive-edit"))
  (or efs-suppress-abort-recursive-edit-and-then
      (progn
	(setq efs-abort-recursive-edit-data (cons (nth 1 (current-time))
						  (cons fun args)))
	(condition-case nil
	    (set-process-sentinel
	     (let ((default-directory exec-directory)
		   (process-connection-type nil))
	       (start-process "efs-abort-recursive-edit" nil "sleep" "0"))
	     (function
	      (lambda (proc string)
		(let ((data efs-abort-recursive-edit-data))
		  (setq efs-abort-recursive-edit-data)
		  (if (and data
			   (integerp (car data))
			   (<= (- (nth 1 (current-time)) (car data))
			       efs-abort-recursive-edit-delay))
		      (apply (nth 1 data) (nthcdr 2 data)))))))
	  (error nil))))
  (abort-recursive-edit))

(defun efs-occur-in-string (char string)
  ;; Return the number of occurrences of CHAR in STRING.
  (efs-save-match-data
    (let ((regexp (regexp-quote (char-to-string char)))
	  (count 0)
	  (start 0))
      (while (string-match regexp string start)
	(setq start (match-end 0)
	      count (1+ count)))
      count)))

(defun efs-parse-proc-name (proc)
  ;; Parses the name of process to return a list \(host user\).
  (efs-save-match-data
    (let ((name (process-name proc)))
      (and name
	   (string-match "^\\*ftp \\([^@]*\\)@\\([^*]+\\)\\*$" name)
	   (list (substring name (match-beginning 2) (match-end 2))
		 (substring name (match-beginning 1) (match-end 1)))))))

;;;; ------------------------------------------------------------
;;;; Of Geography, connectivity, and the internet... Gateways.
;;;; ------------------------------------------------------------

(defun efs-use-gateway-p (host &optional opaque-p)
;; Returns whether to access this host via a gateway.
;; Returns the gateway type as a symbol.  See efs-gateway-type <V>.
;; If optional OPAQUE-P is non-nil, only returns non-nil if the gateway
;; type is in the list efs-opaque-gateways <V>.
  (and efs-gateway-type
       host  ;local host is nil
       (efs-save-match-data
	 (and (not (string-match efs-ftp-local-host-regexp host))
	      (let ((type (car efs-gateway-type)))
		(if opaque-p
		    (and (memq type efs-opaque-gateways) type)
		  type))))))

(defun efs-local-to-gateway-filename (filename &optional reverse)
  ;; Converts a FILENAME on the local host to its name on the gateway,
  ;; using efs-gateway-mounted-dirs-alist. If REVERSE is non-nil, does just
  ;; that. If the there is no corresponding name because non of its parent
  ;; directories are mounted, returns nil.
  (if efs-gateway-mounted-dirs-alist
      (let ((len (length filename))
	    (alist efs-gateway-mounted-dirs-alist)
	    result elt elt-len)
	(if reverse
	    (while (setq elt (car alist))
	      (if (and (>= len (setq elt-len (length (cdr elt))))
		       (string-equal (cdr elt) (substring filename 0 elt-len)))
		  (setq result (concat (car elt)
				       (substring filename elt-len))
			alist nil)
		(setq alist (cdr alist))))
	  (while (setq elt (car alist))
	    (if (and (>= len (setq elt-len (length (car elt))))
		     (string-equal (car elt) (substring filename 0 elt-len)))
		(setq result (concat (cdr elt)
				     (substring filename elt-len))
		      alist nil)
	      (setq alist (cdr alist)))))
	result)))

;;; ------------------------------------------------------------
;;; Enhanced message support.
;;; ------------------------------------------------------------

(defun efs-message (fmt &rest args)
  "Output the given message, truncating to the size of the minibuffer window."
  (let ((msg (apply (function format) fmt args))
	(max (window-width (minibuffer-window))))
    (if (>= (length msg) max)
	(setq msg (concat "> " (substring msg (- 3 max)))))
    (message "%s" msg)))

(defun efs-message-p ()
  ;; Returns t, if efs is allowed to display a status message.
  (not
   (or (and (boundp 'dired-in-query) dired-in-query)
       (boundp 'search-message)
       cursor-in-echo-area
       (and (/= efs-message-interval 0)
	    (let ((diff (- efs-last-message-time
			   (setq efs-last-message-time
				 (nth 1 (current-time))))))
	      (and
	       (> diff (- efs-message-interval))
	       (< diff 0))))))) ; in case the clock wraps.

(efs-define-fun efs-relativize-filename (file &optional dir new)
  "Abbreviate the given filename relative to DIR .
If DIR is nil, use the value of `default-directory' for the currently selected
window. If the optional parameter NEW is given and the 
non-directory parts match, only return the directory part of the file."
  (let* ((dir (or dir (save-excursion
			(set-buffer (window-buffer (selected-window)))
			default-directory)))
	 (dlen (length dir))
	 (result file))
    (and (> (length file) dlen)
	 (string-equal (substring file 0 dlen) dir)
	 (setq result (substring file dlen)))
    (and new
	 (string-equal (file-name-nondirectory result)
		       (file-name-nondirectory new))
	 (or (setq result (file-name-directory result))
	     (setq result "./")))
    (abbreviate-file-name result)))

;;; ------------------------------------------------------------
;;; Temporary file location and deletion...
;;; ------------------------------------------------------------

(defun efs-get-pid ()
  ;; Half-hearted attempt to get the current process's id.
  (setq efs-pid (substring (make-temp-name "") 1)))

(defun efs-make-tmp-name (host1 host2)
  ;; Returns the name of a new temp file, for moving data between HOST1
  ;; and HOST2. This temp file must be directly accessible to the
  ;; FTP client connected to HOST1. Using nil for either HOST1 or
  ;; HOST2 means the local host. The return value is actually a list
  ;; whose car is the name of the temp file wrto to the local host
  ;; and whose cdr is the name of the temp file wrto to the host
  ;; on which the client connected to HOST1 is running. If the gateway
  ;; is only accessible by FTP, then the car of this may be in efs extended
  ;; file name syntax.
  (let ((pid (or efs-pid (efs-get-pid)))
	(start ?a)
	file entry template rem-template template-len)
    ;; Compute the templates.
    (if (null (and host1 (efs-use-gateway-p host1 t)))
	;; file must be local
	(if (null (and host2 (efs-use-gateway-p host2 t)))
	    (setq template efs-tmp-name-template)
	  (setq template (or (efs-local-to-gateway-filename
			      efs-gateway-tmp-name-template t)
			     efs-tmp-name-template)))
      ;; file must be on the gateway -- make sure that the gateway
      ;; configuration is sensible.
      (efs-save-match-data
	(or (string-match efs-ftp-local-host-regexp efs-gateway-host)
	    (error "Gateway %s must be directly ftp accessible."
		   efs-gateway-host)))
      (setq rem-template efs-gateway-tmp-name-template
	    template (or (efs-local-to-gateway-filename
			  efs-gateway-tmp-name-template t)
			 (format efs-path-format-string
				 (efs-get-user efs-gateway-host)
				 efs-gateway-host
				 efs-gateway-tmp-name-template))
	    template-len (length template)))
    ;; Compute a new file name.
    (while (let (efs-verbose)
	     (setq file (format "%s%c%s" template start pid)
		   entry (intern file efs-tmp-name-obarray))
	     (or (memq entry efs-tmp-name-files)
		 (file-exists-p file)))
      (if (> (setq start (1+ start)) ?z)
	  (progn
	    (setq template (concat template "X"))
	    (setq start ?a))))
    (setq efs-tmp-name-files
	  (cons entry efs-tmp-name-files))
    (if rem-template
	(cons file (concat rem-template (substring file template-len)))
      (cons file file))))

(defun efs-del-tmp-name (temp)
  ;; Deletes file TEMP, a string.
  (setq efs-tmp-name-files
	(delq (intern temp efs-tmp-name-obarray)
	      efs-tmp-name-files))
  (condition-case ()
      (let (efs-verbose)
	(delete-file temp))
    (error nil)))


;;;; ==============================================================
;;;; >4
;;;; Hosts, Users, Accounts, and Passwords
;;;; ==============================================================
;;;
;;; A lot of the support for this type of thing is in efs-netrc.el.

;;;; ------------------------------------------------------------
;;;; Password support.
;;;; ------------------------------------------------------------

(defun efs-lookup-passwd (host user)
  ;; Look up the password for HOST and USER.
  (let ((ent (efs-get-host-user-property host user 'passwd)))
    (and ent (efs-code-string ent))))

(defun efs-system-fqdn ()
  "Returns a fully qualified domain name for the current host, if possible."
  (or efs-system-fqdn
      (setq efs-system-fqdn
	    (let ((sys (system-name)))
	      (if (string-match "\\." sys)
		  sys
		(if efs-nslookup-program
		    (let ((proc (let ((default-directory exec-directory)
				      (process-connection-type nil))
				  (start-process " *nslookup*" " *nslookup*"
						 efs-nslookup-program sys)))
			  (res sys)
			  (n 0))
		      (process-kill-without-query proc)
		      (save-excursion
			(set-buffer (process-buffer proc))
			(let ((quit-flag nil)
			      (inhibit-quit nil))
			  (if efs-nslookup-threshold
			      (progn
				(while (and (memq (process-status proc)
						  '(run open))
					    (< n efs-nslookup-threshold))
				  (accept-process-output)
				  (setq n (1+ n)))
				(if (>= n efs-nslookup-threshold)
				    (progn
				      (with-output-to-temp-buffer "*Help*"
					(princ (format "\
efs is unable to determine a fully qualified domain name
for the local host to send as an anonymous ftp password.

The function `system-name' is not returning a fully qualified
domain name. An attempt to obtain a fully qualified domain name
with `efs-nslookup-program' (currently set to \"%s\") has
elicited no response from that program. Consider setting 
`efs-generate-anonymous-password' to an email address for anonymous
ftp passwords.

For more information see the documentation (use C-h v) for the
variables `efs-nslookup-program' and `efs-nslookup-threshold'."
						       efs-nslookup-program)))
				      (error "No response from %s"
					     efs-nslookup-program))))
			    (while (memq (process-status proc) '(run open))
			      (accept-process-output proc)))
			  (goto-char (point-min))
			  (if (re-search-forward
			       (format "^Name: *\\(%s\\.[^ \n\t]+\\)"
				       sys) nil t)
			      (setq res (buffer-substring
					 (match-beginning 1)
					 (match-end 1)))
			    (kill-buffer (current-buffer)))))
		      res)
		  sys))))))

(defun efs-passwd-unique-list (alist)
  ;; Preserving the relative order of ALIST, remove all entries with duplicate 
  ;; cars.
  (let (result)
    (while alist
      (or (assoc (car alist) result)
	  (setq result (cons (car alist) result)))
      (setq alist (cdr alist)))
    (nreverse result)))

(defun efs-get-passwd-list (user host)
  ;; Returns an alist of the form '((pass host user) ...).
  ;; The order is essentially arbitrary, except that entries with user
  ;; equal to USER will appear first. Followed by entries with host equal to
  ;; HOST. Also, there will be no entries with duplicate values of pass.
  (efs-parse-netrc)
  (let* ((user-template (concat "/" user))
	 (ulen (length user-template))
	 (hlen (length host))
	 primaries secondaries tertiaries)
    (efs-save-match-data
      (efs-map-hashtable
       (function
	(lambda (key passwd)
	  (cond ((null passwd) nil)
		((and (> (length key) ulen)
		      (string-equal user-template
				    (substring key (- ulen))))
		 (setq primaries (cons (list (efs-code-string passwd)
					     (substring key 0 (- ulen))
					     (substring user-template 1))
				       primaries)))
		((and (> (length key) hlen)
		      (string-equal host (substring key 0 hlen))
		      (memq (aref key hlen) '(?/ ?.)))
		 (if (string-match "/" key hlen)
		     (setq secondaries
			   (cons (list (efs-code-string passwd)
				       (substring key 0 (match-beginning 0))
				       (substring key (match-end 0)))
				 secondaries))))
		((string-match "/" key)
		 (setq tertiaries
		       (cons (list (efs-code-string passwd)
				   (substring key 0 (match-beginning 0))
				   (substring key (match-end 0)))
			     tertiaries))))))
       efs-host-user-hashtable 'passwd))
    (efs-passwd-unique-list (nconc primaries secondaries tertiaries))))
    
(defun efs-get-passwd (host user)
  "Given a HOST and USER, return the FTP password, prompting if it was not
previously set."
  (efs-parse-netrc)

  ;; look up password in the hash table first; user might have overriden the
  ;; defaults.
  (cond ((efs-lookup-passwd host user))
	
	;; see if default user and password set from the .netrc file.
	((and (stringp efs-default-user)
	      efs-default-password
	      (string-equal user efs-default-user))
	 (copy-sequence efs-default-password))
	
	;; anonymous ftp password is handled specially since there is an
	;; unwritten rule about how that is used on the Internet.
	((and (efs-anonymous-p user)
	      efs-generate-anonymous-password)
	 (if (stringp efs-generate-anonymous-password)
	     (copy-sequence efs-generate-anonymous-password)
	   (concat (user-login-name) "@" (efs-system-fqdn))))
	
	;; see if same user has logged in to other hosts; if so then prompt
	;; with the password that was used there.
	(t
	 (let (others defaults passwd)
	   (unwind-protect
	       (progn
		 (setq others (efs-get-passwd-list user host)
		       defaults (mapcar
				 (function
				  (lambda (x)
				    (cons
				     (format
				      "Passwd for %s@%s (same as %s@%s): "
				      user host (nth 2 x) (nth 1 x))
				     (car x))))
				 others))
		 (setq passwd
		       (read-passwd
			(or defaults
			    (format "Password for %s@%s: " user host)))))
	     (while others
	       (fillarray (car (car others)) 0)
	       (setq others (cdr others))))
	   (or (null passwd)
	       (and efs-high-security-hosts
		    (efs-save-match-data
		      (string-match efs-high-security-hosts
				    (format "%s@%s" user host))))
	       (efs-set-passwd host user passwd))
	   passwd))))

;;;; ------------------------------------------------------------
;;;; Account support
;;;; ------------------------------------------------------------

(defun efs-get-account (host user &optional minidisk really)
  "Given a HOST, USER, and optional MINIDISK return the FTP account password.
If the optional REALLY argument is given, prompts the user if it can't find
one."
  (efs-parse-netrc)
  (let ((account (if minidisk
		     (efs-get-hash-entry
		      (concat (downcase host) "/" user "/" minidisk)
		      efs-minidisk-hashtable
		      (memq (efs-host-type host)
			    efs-case-insensitive-host-types))
		   (efs-get-host-user-property host user 'account))))
    (if account
	(efs-code-string account)
      ;; Do we really want to send the default-account passwd for all
      ;; minidisks?
      (if (and (stringp efs-default-user)
	       (string-equal user efs-default-user)
	       efs-default-account)
	  efs-default-account
	(and really
	     (let ((acct
		    (read-passwd
		     (if minidisk
			 (format
			  "Write access password for minidisk %s on %s@%s: "
			  minidisk user host)
		       (format
			"Account password for %s@%s: " user host)))))
	       (or (and efs-high-security-hosts
			(efs-save-match-data
			  efs-high-security-hosts
			  (format "%s@%s" user host)))
		   (efs-set-account host user minidisk acct))
	       acct))))))

;;;; -------------------------------------------------------------
;;;; Special classes of users.
;;;; -------------------------------------------------------------

(defun efs-anonymous-p (user)
  ;; Returns t if USER should be treated as an anonymous FTP login.
  (let ((user (downcase user)))
    (or (string-equal user "anonymous") (string-equal user "ftp"))))


;;;; =============================================================
;;;; >5
;;;; FTP client process, and server responses
;;;; =============================================================

;;;; ---------------------------------------------------------
;;;; Support for asynch process queues.
;;;; ---------------------------------------------------------

(defun efs-add-to-queue (host user item)
  "To the end of the command queue for HOST and USER, adds ITEM.
Does nothing if there is no process buffer for HOST and USER."
  (let ((buff (efs-ftp-process-buffer host user)))
    (if (get-buffer buff)
	(save-excursion
	  (set-buffer buff)
	  (setq efs-process-q
		(nconc efs-process-q (list item)))))))

;;;; -------------------------------------------------------
;;;; Error recovery for the process filter.
;;;; -------------------------------------------------------

;;; Could make this better, but it's such an unlikely error to hit.
(defun efs-process-scream-and-yell (line)
  (let* ((buff (buffer-name (current-buffer)))
	 (host (and (string-match "@\\(.*\\)\\*$" buff)
		    (substring buff (match-beginning 1) (match-end 1)))))
    (with-output-to-temp-buffer "*Help*"
      (princ
       (concat
	"efs is unable to identify the following reply code
from the ftp server " host ":\n\n" line "

Please send a bug report to ange@hplb.hpl.hp.com.
In your report include a transcript of your\n"
buff " buffer."))))
  (error "Unable to identify server code."))

(defun efs-error (host user msg)
  "Signal \'ftp-error for the FTP connection for HOST and USER.
The error gives the string MSG as text. The process buffer for the FTP
is popped up in another window."
  (let ((cur (selected-window))
	(pop-up-windows t)
	(buff (get-buffer (efs-ftp-process-buffer host user))))
    (if buff
	(progn
	  (pop-to-buffer buff)
	  (goto-char (point-max))
	  (select-window cur))))
  (signal 'ftp-error (list (format "FTP Error: %s" msg))))

;;;; --------------------------------------------------------------------
;;;; Process filter and supporting functions for handling FTP codes.
;;;; --------------------------------------------------------------------

(defun efs-process-handle-line (line proc)
  ;; Look at the given LINE from the ftp process PROC and try to catagorize it.
  (cond ((string-match efs-xfer-size-msgs line)
	 (let ((n 1))
	   ;; this loop will bomb with an args out of range error at 10
	   (while (not (match-beginning n))
	     (setq n (1+ n)))
	   (setq efs-process-xfer-size
		 (ash (string-to-int (substring line
						(match-beginning n)
						(match-end n)))
		    -10))))
	
	((string-match efs-multi-msgs line)
	 (setq efs-process-result-cont-lines
	       (concat efs-process-result-cont-lines line "\n")))
	
	((string-match efs-skip-msgs line))

	((string-match efs-cmd-ok-msgs line)
	 (if (string-match efs-cmd-ok-cmds efs-process-cmd)
	     (setq efs-process-busy nil
		   efs-process-result nil
		   efs-process-result-line line)))

	((string-match efs-pending-msgs line)
	 (if (string-match "^quote rnfr " efs-process-cmd)
	     (setq efs-process-busy nil
		   efs-process-result nil
		   efs-process-result-line line)))
	
	((string-match efs-bytes-received-msgs line)
	 (if efs-process-server-confused
	     (setq efs-process-busy nil
		   efs-process-result nil
		   efs-process-result-line line)))
	
	((string-match efs-server-confused-msgs line)
	 (setq efs-process-server-confused t))

	((string-match efs-good-msgs line)
	 (setq efs-process-busy nil
	       efs-process-result nil
	       efs-process-result-line line))

	((string-match efs-fatal-msgs line)
	 (set-process-sentinel proc nil)
	 (delete-process proc)
	 (setq efs-process-busy nil
	       efs-process-result 'fatal
	       efs-process-result-line line))
	
	((string-match efs-failed-msgs line)
	 (setq efs-process-busy nil
	       efs-process-result 'failed
	       efs-process-result-line line))
	
	((string-match efs-unknown-response-msgs line)
	 (setq efs-process-busy nil
	       efs-process-result 'weird
	       efs-process-result-line line)
	 (efs-process-scream-and-yell line))))

(efs-define-fun efs-process-log-string (proc str)
  ;; For a given PROCESS, log the given STRING at the end of its
  ;; associated buffer.
  (let ((buff (get-buffer (process-buffer proc))))
    (if buff
	(efs-save-buffer-excursion
	  (set-buffer buff)
	  (comint-output-filter proc str)))))

(defun efs-process-filter (proc str)
  ;; Build up a complete line of output from the ftp PROCESS and pass it
  ;; on to efs-process-handle-line to deal with.
  (let ((inhibit-quit t)
	(buffer (get-buffer (process-buffer proc)))
	(efs-default-directory default-directory))

    ;; see if the buffer is still around... it could have been deleted.
    (if buffer
	(efs-save-buffer-excursion
	  (set-buffer (process-buffer proc))
	  (efs-save-match-data

	    ;; handle hash mark printing
	    (if efs-process-busy
		(setq str (efs-process-handle-hash str)
		      efs-process-string (concat efs-process-string str)))
	    (efs-process-log-string proc str)
	    (while (and efs-process-busy
			(string-match "\n" efs-process-string))
	      (let ((line (substring efs-process-string
				     0
				     (match-beginning 0))))
		(setq efs-process-string (substring
					  efs-process-string
					  (match-end 0)))
		;; If we are in synch with the client, we should
		;; never get prompts in the wrong place. Just to be safe,
		;; chew them off.
		(while (string-match efs-process-prompt-regexp line)
		  (setq line (substring line (match-end 0))))
		(efs-process-handle-line line proc)))
	    
	    ;; has the ftp client finished?  if so then do some clean-up
	    ;; actions.
	    (if (not efs-process-busy)
		(progn
		  (efs-correct-hash-mark-size)
		  ;; reset process-kill-without-query
		  (process-kill-without-query proc)
		  ;; issue the "done" message since we've finished.
		  (if (and efs-process-msg
			   (efs-message-p)
			   (null efs-process-result))
		      (progn

			(efs-message "%s...done" efs-process-msg)
			(setq efs-process-msg nil)))
		  
		  (if (and efs-process-nowait
			   (null efs-process-cmd-waiting))
		      
		      (progn
			;; Is there a continuation we should be calling?
			;; If so, we'd better call it, making sure we
			;; only call it once.
			(if efs-process-continue
			    (let ((cont efs-process-continue))
			      (setq efs-process-continue nil)
			      (efs-call-cont
			       cont
			       efs-process-result
			       efs-process-result-line
			       efs-process-result-cont-lines)))
			;; If the cmd was run asynch, run the next
			;; cmd from the queue. For synch cmds, this
			;; is done by efs-send-cmd. For asynch
			;; cmds we don't care about
			;; efs-nested-cmd, since nothing is
			;; waiting for the cmd to complete. If
			;; efs-process-cmd-waiting is t, exit
			;; to let this command run.
			(if (and efs-process-q
				 ;; Be careful to check efs-process-busy
				 ;; again, because the cont may have started
				 ;; some new ftp action.
				 ;; wheels within wheels...
				 (null efs-process-busy))
			    (let ((next (car efs-process-q)))
			      (setq efs-process-q
				      (cdr efs-process-q))
			      (apply 'efs-send-cmd
				     efs-process-host
				     efs-process-user
				     next))))
		    
		    (if efs-process-continue
			(let ((cont efs-process-continue))
			  (setq efs-process-continue nil)
			  (efs-call-cont
			   cont
			   efs-process-result
			   efs-process-result-line
			   efs-process-result-cont-lines))))
		  
		  ;; Update the mode line
		  ;; We can't test nowait to see if we changed the
		  ;; modeline in the first place, because conts
		  ;; may be running now, which will confuse the issue.
		  ;; The logic is simpler if we update the modeline
		  ;; before the cont, but then the user sees the
		  ;; modeline track the cont execution. It's dizzying.
		  (if (and (or efs-mode-line-format
			       efs-ftp-activity-function)
			   (null efs-process-busy))
		      (efs-update-mode-line)))))

	  ;; Trim buffer, if required.
	  (and efs-max-ftp-buffer-size
	       (zerop efs-process-cmd-counter)
	       (> (point-max) efs-max-ftp-buffer-size)
	       (= (point-min) 1) ; who knows, the user may have narrowed.
	       (null (get-buffer-window (current-buffer)))
	       (save-excursion
		 (goto-char (/ efs-max-ftp-buffer-size 2))
		 (forward-line 1)
		 (delete-region (point-min) (point))))))))

;;;; ------------------------------------------------------------------
;;;; Functions for counting hashes and reporting on bytes transferred.
;;;; ------------------------------------------------------------------

(defun efs-set-xfer-size (host user bytes)
  ;; Set the size of the next FTP transfer in bytes.
  (let ((proc (efs-get-process host user)))
    (if proc
	(let ((buf (process-buffer proc)))
	  (if buf
	      (save-excursion
		(set-buffer buf)
		(setq efs-process-xfer-size (ash bytes -10))))))))

(defun efs-guess-incoming-bin-hm-size ()
  ;; Guess at the hash mark size for incoming binary transfers by taking
  ;; the average value for such transfers to other hosts.
  (let ((total 0)
	(n 0))
    (efs-map-hashtable
     (function
      (lambda (host hm-size)
	(if hm-size (setq total (+ total hm-size)
			  n (1+ n)))))
     efs-host-hashtable
     'incoming-bin-hm-size)
    (and (> n 0) (/ total n))))

(defun efs-set-hash-mark-unit (host user &optional incoming)
  ;; Sets the value of efs-process-hash-mark-unit according to the xfer-type.
  ;; efs-hash-mark-unit is the number of bytes represented by a hash mark,
  ;; in units of 16. If INCOMING is non-nil, the xfer will be a GET.
  (if efs-send-hash
      (let ((buff (efs-ftp-process-buffer host user))
	    (gate-p (efs-use-gateway-p host t)))
	(if buff
	    (save-excursion
	      (set-buffer buff)
	      (setq efs-process-hash-mark-unit
		    (ash (or
			  (and incoming (eq efs-process-xfer-type 'image)
			       (or (efs-get-host-property
				    host 'incoming-bin-hm-size)
				   (if gate-p
				       efs-gateway-incoming-binary-hm-size
				     efs-incoming-binary-hm-size)
				   (let ((guess
					  (efs-guess-incoming-bin-hm-size)))
				     (and guess
					  (efs-set-host-property
					   host 'incoming-bin-hm-size
					   guess)))))
			  (if gate-p
			      efs-gateway-hash-mark-size
			    efs-hash-mark-size)
			  1024) ; make sure that we have some integer
			 -4)))))))

(defun efs-correct-hash-mark-size ()
  ;; Corrects the value of efs-{ascii,binary}-hash-mark-size.
  ;; Must be run in the process buffer.
  (and efs-send-hash
       efs-process-hash-mark-unit
       (> efs-process-xfer-size 0)
       (< efs-process-xfer-size 524288) ; 2^19, prevent overflows
       (> efs-process-hash-mark-count 0)
       (or (> efs-process-last-percent 100)
	   (< (ash (* efs-process-hash-mark-unit
		      (1+ efs-process-hash-mark-count )) -6)
	      efs-process-xfer-size))
       (let ((val (ash (/ (ash efs-process-xfer-size 6)
			  efs-process-hash-mark-count) 4)))
	 (if (and (eq efs-process-xfer-type 'image)
		  (>= (length efs-process-cmd) 4)
		  (string-equal (downcase (substring efs-process-cmd 0 4))
				"get "))
	     (efs-set-host-property efs-process-host 'incoming-bin-hm-size val)
	   (set (if (efs-use-gateway-p efs-process-host t)
		    'efs-gateway-hash-mark-size
		  'efs-hash-mark-size)
		val)))))

(defun efs-process-handle-hash (str)
  ;; Remove hash marks from STRING and display count so far.
  (if (string-match "^#+$" str)
      (progn
	(setq efs-process-hash-mark-count
	      (+ efs-process-hash-mark-count
		 (- (match-end 0) (match-beginning 0))))
	(and
	 efs-process-msg
	 efs-process-hash-mark-unit
	 (not (and efs-process-nowait
		   (or (eq efs-verbose 0)
		       (eq (selected-window) (minibuffer-window)))))
	 (efs-message-p)
	 (let* ((big (> efs-process-hash-mark-count 65536)) ; 2^16
		(kbytes (if big
			    (* efs-process-hash-mark-unit
			       (ash efs-process-hash-mark-count -6))
			  (ash (* efs-process-hash-mark-unit
				  efs-process-hash-mark-count)
			       -6)))
		;; dkbytes is the number of kbytes since previous call
		(oldhashcnt (car (car efs-process-hash-mark-history)))
		(oldtime (nth 1 (car efs-process-hash-mark-history)))
		(diffcount (- efs-process-hash-mark-count oldhashcnt))
		(dkbytes (if big
			     (* efs-process-hash-mark-unit
				(ash diffcount -6))
			   (ash (* efs-process-hash-mark-unit
				   diffcount)
				-6)))
		(curtime (current-time))
		dtime rate)

	   (setq efs-process-hash-mark-history
		 (append efs-process-hash-mark-history (list (list efs-process-hash-mark-count curtime))))
	   (if (> (length efs-process-hash-mark-history) 20)
	       (setq efs-process-hash-mark-history (cdr efs-process-hash-mark-history)))

	   ;; We'll assume there aren't 18 hours between hash updates
	   (setq dtime (nth 1 (efs-time-minus curtime oldtime)))
	   (setq dtime (- (+ dtime (/ (nth 2 curtime) 1000000.0))
			  (/ (nth 2 oldtime) 1000000.0)))
	   (setq rate (round (* 1000.0 (/ (float dkbytes) dtime))))
	   (if (< rate 1000)
	       (setq rate (format "(%d B/s)" rate))
	     (setq rate (number-to-string (round (/ rate 100.0))))
	     (setq rate (format "(%s.%s KB/s)"
				(substring rate 0 -1)
				(substring rate -1 nil))))

	   (if (zerop efs-process-xfer-size)
	       (or (zerop kbytes)
		   (efs-message "%s...%dk %s" efs-process-msg kbytes rate))
	     (let ((percent (if big
				(/ (* 100 (ash kbytes -7))
				   (ash efs-process-xfer-size -7))
			      (/ (* 100 kbytes) efs-process-xfer-size))))
	       ;; Don't display %'s betwwen 100 and 110
	       (and (> percent 100) (< percent 110) (setq percent 100))
	       ;; cut out the redisplay of identical %-age messages.
	       (or (eq percent efs-process-last-percent)
		   (progn
		     (setq efs-process-last-percent percent)
		     (efs-message "%s...%d%% %s" efs-process-msg percent rate)))))))
	(concat (substring str 0 (match-beginning 0))
		(and (/= (length str) (match-end 0))
		     (substring str (1+ (match-end 0))))))
    str))

;;;; ------------------------------------------------------------------
;;;; Keeping track of the number of active background connections.
;;;; ------------------------------------------------------------------

(defun efs-ftp-processes-active ()
  ;; Return the number of FTP processes busy.
  (save-excursion
    (length
     (delq nil
	   (mapcar
	    (function
	     (lambda (buff)
	       (set-buffer buff)
	       (and (boundp 'efs-process-busy)
		    efs-process-busy)))
	    (buffer-list))))))

(defun efs-update-mode-line ()
  ;; Updates the mode with FTP activity, and runs `efs-ftp-activity-function'.
  (let ((num (efs-ftp-processes-active)))
    (if efs-mode-line-format
	(progn
	  (if (zerop num)
	      (setq efs-mode-line-string "")
	    (setq efs-mode-line-string (format efs-mode-line-format num)))
	  ;; fake emacs into re-calculating all the mode lines.
	  (save-excursion (set-buffer (other-buffer)))
	  (set-buffer-modified-p (buffer-modified-p))))
    (if efs-ftp-activity-function
	(funcall efs-ftp-activity-function num))))

;;;###autoload
(defun efs-display-ftp-activity ()
  "Displays the number of active background ftp sessions in the modeline.
Uses the variable `efs-mode-line-format' to determine how this will be
displayed."
  (interactive)
  (or (memq 'efs-mode-line-string global-mode-string)
      (if global-mode-string
	  (nconc global-mode-string '(efs-mode-line-string))
	(setq global-mode-string '("" efs-mode-line-string)))))

;;;; -------------------------------------------------------------------
;;;; Expiring inactive ftp buffers.
;;;; -------------------------------------------------------------------

(defun efs-start-polling ()
  ;; Start polling FTP buffers, to look for idle ones.
  (or (null efs-expire-ftp-buffers)
      (let ((proc (get-process "efs poll")))
	(or (and proc (eq (process-status proc) 'run))))
      (let ((default-directory exec-directory)
	    (process-connection-type nil)
	    new-proc)
	(condition-case nil
	    (delete-process "efs poll")
	  (error nil))
	(setq new-proc (start-process
			"efs poll" nil
			(concat exec-directory "wakeup")
			(int-to-string efs-ftp-buffer-poll-time)))
	(set-process-filter new-proc (function efs-expire-ftp-buffers-filter))
	(process-kill-without-query new-proc))))

(defun efs-connection-visited-p (host user)
  ;; Returns t if there are any buffers visiting files on HOST and USER.
  (save-excursion
    (let ((list (buffer-list))
	  (case-fold (memq (efs-host-type host)
			   efs-case-insensitive-host-types))
	  (visited nil)
	  parsed)
      (setq host (downcase host))
      (if case-fold (setq user (downcase user)))
      (while list
	(set-buffer (car list))
	(if (or (and buffer-file-name
		     (setq parsed (efs-ftp-path buffer-file-name))
		     (string-equal host (downcase (car parsed)))
		     (string-equal user (if case-fold
					    (downcase (nth 1 parsed))
					  (nth 1 parsed))))
		(and (boundp 'dired-directory)
		     (stringp dired-directory)
		     efs-dired-host-type
		     (setq parsed (efs-ftp-path dired-directory))
		     (string-equal host (downcase (car parsed)))
		     (string-equal user (if case-fold
					    (downcase (nth 1 parsed))
					  (nth 1 parsed)))))
	    (setq visited t
		  list nil)
	  (setq list (cdr list))))
      visited)))

(defun efs-expire-ftp-buffers-filter (proc string)
  ;; Check all ftp buffers, and kill them if they have been inactive
  ;; for the minimum of efs-ftp-buffer-expire-time and their local
  ;; time out time.
  (if efs-expire-ftp-buffers
      (let ((list (buffer-list))
	    new-alist)
	(save-excursion
	  (while list
	    (set-buffer (car list))
	    (if (eq major-mode 'efs-mode)
		(let* ((proc (get-buffer-process (current-buffer)))
		       (proc-p (and proc (memq (process-status proc)
					       '(run open)))))
		  (if (or efs-ftp-buffer-expire-time
			  efs-process-idle-time
			  (null proc-p))
		      (let ((elt (assq (car list) efs-ftp-buffer-alist))
			    (wind-p (get-buffer-window (car list))))
			(if (or (null elt) (buffer-modified-p)
				efs-process-busy wind-p)
			    (progn
			      (setq new-alist (cons (cons (car list) 0)
						    new-alist))
			      (or wind-p (set-buffer-modified-p nil)))
			  (let ((idle (+ (cdr elt)
					 efs-ftp-buffer-poll-time)))
			    (if (and proc-p
				     (< idle
					(if efs-ftp-buffer-expire-time
					    (if efs-process-idle-time
						(min efs-ftp-buffer-expire-time
						     efs-process-idle-time)
					      efs-ftp-buffer-expire-time)
					  efs-process-idle-time)))
				(progn
				  (setq new-alist (cons (cons (car list) idle)
							new-alist))
				  (set-buffer-modified-p nil))
			      ;; If there are still buffers for host & user,
			      ;; don't wipe the cache.
			      (and proc
				   (efs-connection-visited-p
				    efs-process-host efs-process-user)
				   (set-process-sentinel proc nil))
			      (kill-buffer (car list)))))))))
	    (setq list (cdr list))))
	(setq efs-ftp-buffer-alist new-alist))
    (condition-case nil
	(delete-process "efs poll")
      (error nil))))

;;;; -------------------------------------------------------------------
;;;; When the FTP client process dies...
;;;; -------------------------------------------------------------------

(defun efs-process-sentinel (proc str)
  ;; When ftp process changes state, nuke all file-entries in cache.
  (let ((buff (process-buffer proc)))
    ;; If the client dies, make sure that efs doesn't think that
    ;; there is a running process.
    (save-excursion
      (condition-case nil
	  (progn
	    (set-buffer buff)
	    (setq efs-process-busy nil))
	(error nil)))
    (let ((parsed (efs-parse-proc-name proc)))
      (if parsed
	  (progn
	    (apply 'efs-wipe-file-entries parsed)
	    (apply 'efs-wipe-from-ls-cache parsed))))
    (if (or efs-mode-line-format efs-ftp-activity-function)
	(efs-update-mode-line))))

(defun efs-kill-ftp-process (buffer)
  "Kill an FTP connection and its associated process buffer.
If the BUFFER's visited file name or default-directory is an efs remote
file name, it is the connection for that file name that is killed."
  (interactive "bKill FTP process associated with buffer: ")
  (or buffer (setq buffer (current-buffer)))
  (save-excursion
    (set-buffer buffer)
    (if (eq major-mode 'efs-mode)
	(kill-buffer buffer)
      (let ((file (or (buffer-file-name) default-directory)))
	(if file
	    (let ((parsed (efs-ftp-path (expand-file-name file))))
	      (if parsed
		  (let ((host (nth 0 parsed))
			(user (nth 1 parsed)))
		    (kill-buffer
		     (efs-ftp-process-buffer host user))))))))))

(defun efs-close-ftp-process (buffer)
  "Close an FTP connection.
This kills the FTP client process, but unlike `efs-kill-ftp-process' this
neither kills the process buffer, nor deletes cached data for the connection."
  (interactive "bClose FTP process associated with buffer: ")
  (or buffer (setq buffer (current-buffer)))
  (save-excursion
    (set-buffer buffer)
    (if (eq major-mode 'efs-mode)
	(let ((process (get-buffer-process buffer)))
	  (if process
	      (progn
		(set-process-sentinel process nil)
		(setq efs-process-busy nil
		      efs-process-q nil)
		(if (or efs-mode-line-format efs-ftp-activity-function)
		    (efs-update-mode-line))
		(delete-process process))))
      (let ((file (or (buffer-file-name) default-directory)))
	(if file
	    (let ((parsed (efs-ftp-path (expand-file-name file))))
	      (if parsed
		  (let ((process (get-process
				  (format "*ftp %s@%s*"
					  (nth 1 parsed) (car parsed)))))
		    (if process
			(progn
			  (set-buffer (process-buffer process))
			  (set-process-sentinel process nil)
			  (setq efs-process-busy nil
				efs-process-q nil)
			  (if (or efs-mode-line-format
				  efs-ftp-activity-function)
			      (efs-update-mode-line))
			  (delete-process process)))))))))))

(defun efs-ping-ftp-connection (buffer)
  "Ping a connection by sending a NOOP command.
Useful for waking up a possible expired connection."
  (interactive "bPing FTP connection associated with buffer: ")
  (or buffer (setq buffer (current-buffer)))
  (efs-save-buffer-excursion
    (set-buffer buffer)
    (let (file host user parsed)
      (if (or (and (eq major-mode 'efs-mode)
		   (setq host efs-process-host
			 user efs-process-user))
	      (and (setq file (or (buffer-file-name) default-directory))
		   (setq parsed (efs-ftp-path file))
		   (setq host (car parsed)
			 user (nth 1 parsed))))
	  (or (car
	       (efs-send-cmd
		host user '(quote noop)
		(format "Pinging connection %s@%s" user host)))
	      (message "Connection %s@%s is alive." user host))))))

(defun efs-display-ftp-process-buffer (buffer)
  "Displays the FTP process buffer associated with the current buffer."
  (interactive "bDisplay FTP buffer associated with buffer: ")
  (if (null buffer) (setq buffer (current-buffer)))
  (let ((file (or (buffer-file-name) default-directory))
	parsed proc-buffer)
    (if (and file (setq parsed (efs-ftp-path file))
	     (setq proc-buffer (get-buffer (efs-ftp-process-buffer
					    (car parsed)
					    (nth 1 parsed)))))
	(display-buffer proc-buffer)
      (error "Buffer %s not associated with an FTP process" buffer))))

;;;; -------------------------------------------------------------------
;;;; Starting the FTP client process
;;;; -------------------------------------------------------------------

(defun efs-ftp-process-buffer (host user)
  "Return name of the process buffer for ftp process for HOST and USER."
  ;; Host names on the internet are case-insensitive.
  (format efs-ftp-buffer-format user (downcase host)))

(defun efs-pty-check (proc threshold)
  ;; Checks to see if PROC is a pty. Beware, it clobbers the process
  ;; filter, so run this before you set the filter.
  ;; THRESHOLD is an integer to tell it how long to wait for output.
  (sit-for 0)				; Update the display before doing any waiting.
  (let ((efs-pipe-p t)
	(n 0))
    (set-process-filter proc (function (lambda (proc string)
					 (setq efs-pipe-p nil))))

    (if efs-ftp-flush-command
	(process-send-string proc (concat efs-ftp-flush-command "\n")))

    (while (and (< n threshold) efs-pipe-p)
      (accept-process-output)
      (setq n (1+ n)))
    (if efs-pipe-p
	(progn
	  (sit-for 0)			; update display
	  ;; Use a sleep-for as I don't want pty-checking to depend
	  ;; on pending input.
	  (sleep-for efs-pty-check-retry-time)))
    (accept-process-output)
    (if (and efs-pipe-p
	     (or noninteractive
		 (progn
		   ;; in case the user typed something during the wait.
		   (discard-input)
		   (y-or-n-p
		    (format "%s seems not a pty. Kill? " proc)))))
	(progn
	  (kill-buffer (process-buffer proc))
	  (if (eq (selected-window) (minibuffer-window))
	      (abort-recursive-edit)
	    (signal 'quit nil)))
      ;; Need to send a \n to make sure, because sometimes we get the startup
      ;; prompt from a pipe.
      (sit-for 0)
      (process-send-string proc "\n")
      (if efs-ftp-flush-command
	  (process-send-string proc (concat efs-ftp-flush-command "\n")))
      (setq efs-pipe-p t
	    n 0)
      (while (and (< n threshold) efs-pipe-p)
	(accept-process-output)
	(setq n (1+ n)))
      (if efs-pipe-p
	  (progn
	    (sit-for 0)
	    (sleep-for efs-pty-check-retry-time)))
      (accept-process-output)
      (if (and efs-pipe-p
	       (or noninteractive
		   (progn
		     ;; in case the user typed something during the wait.
		     (discard-input)
		     (y-or-n-p
		      (format "%s seems not a pty. Kill? " proc)))))
	  (progn
	    (kill-buffer (process-buffer proc))
	    (if (eq (selected-window) (minibuffer-window))
		(abort-recursive-edit)
	      (signal 'quit nil)))))))

(defun efs-start-process (host user name)
  "Spawn a new ftp process ready to connect to machine HOST as USER.
If HOST is only ftp-able through a gateway machine then spawn a shell
on the gateway machine to do the ftp instead. NAME is the name of the
process."
  (let* ((use-gateway (efs-use-gateway-p host))
	 (buffer (get-buffer-create (efs-ftp-process-buffer host user)))
	 (process-connection-type t)
	 (opaque-p (memq use-gateway efs-opaque-gateways))
	 proc)
    (save-excursion
      (set-buffer buffer)
      (efs-mode host user (if opaque-p
			      efs-gateway-ftp-prompt-regexp
			    efs-ftp-prompt-regexp)))
    (cond
     ((null use-gateway)
      (message "Opening FTP connection to %s..." host)
      (setq proc (apply 'start-process name buffer efs-ftp-program-name
			efs-ftp-program-args)))
     ((eq use-gateway 'interactive)
      (setq proc (efs-gwp-start host user name)))
     ((eq use-gateway 'remsh)
      (message "Opening FTP connection to %s via %s..." host efs-gateway-host)
      (setq proc (apply 'start-process name buffer (nth 1 efs-gateway-type)
			(append (list efs-gateway-host)
				(nth 2 efs-gateway-type)
				(list (nth 3 efs-gateway-type))
				(nth 4 efs-gateway-type)))))
     ((memq use-gateway '(proxy sidewinder raptor interlock kerberos))
      (message "Opening FTP connection to %s via %s..." host efs-gateway-host)
      (setq proc (apply 'start-process name buffer (nth 1 efs-gateway-type)
			(nth 2 efs-gateway-type))))
     ((eq use-gateway 'local)
      (message "Opening FTP connection to %s..." host)
      (setq proc (apply 'start-process name buffer (nth 1 efs-gateway-type)
			(nth 2 efs-gateway-type))))
     ((error "Never heard of gateway type %s" use-gateway)))
    (process-kill-without-query proc)
    (if opaque-p
	(accept-process-output proc)
      (if efs-pty-check-threshold
	  (efs-pty-check proc efs-pty-check-threshold)
	(accept-process-output proc)))
    (set-process-sentinel proc (function efs-process-sentinel))
    (set-process-filter proc (function efs-process-filter))
    (efs-start-polling)
    (save-excursion
      (set-buffer buffer)
      (goto-char (point-max))
      (set-marker (process-mark proc) (point)))
    proc))

(defun efs-get-process-internal (host user)
  ;; Get's the first process for HOST and USER.  If HOST runs a
  ;; a case insignificant OS, then case is not considered in USER.
  (let ((list (process-list))
	(case-fold (memq (efs-host-type host)
			 efs-case-insensitive-host-types))
	(len (+ (length host) (length user) 7))
	fmt name found)
    (setq host (downcase host))
    (if case-fold (setq user (downcase user)))
    (while (and (not found) list)
      (setq name (process-name (car list)))
      (if (and (= (length name) len)
	       (string-equal (substring name 0 5) "*ftp ")
	       (string-equal
		(if case-fold (downcase (substring name 5)) (substring name 5))
		(or fmt (setq fmt (format "%s@%s*" user host))))
	       (memq (process-status (car list)) '(run open)))
	  (setq found (car list))
	(setq list (cdr list))))
    found))

;; efs-guess-host-type calls this
;; function recursively. The (if (and proc... avoids an infinite
;; loop. We should make sure that this won't hang things if the
;; connection goes wrong.

(defun efs-get-process (host user)
  "Return the process object for the FTP process for HOST and USER.
Create a new process if needed."

  (let ((proc (efs-get-process-internal host user)))
    (if (and proc (memq (process-status proc) '(run open)))
	proc
      
      ;; Make sure that the process isn't around in some strange state.

      (setq host (downcase host))
      (let ((name (concat "*ftp " user "@" host "*")))
	(if proc (condition-case nil (delete-process proc) (error nil)))
	
	;; grab a suitable process.
	(setq proc (efs-start-process host user name))
	
	(efs-save-match-data
	  (efs-save-buffer-excursion
	    (set-buffer (process-buffer proc))
	    
	    ;; Run any user-specified hooks.
	    (run-hooks 'efs-ftp-startup-hook)
	    
	    ;; login to FTP server.
	    (efs-login host user proc)

	    ;; Beware, the process may have died if the login went bad.
	    (if (memq (process-status proc) '(run open))

		(progn
		  ;; Tell client to send back hash-marks as progress.  It isn't
		  ;; usually fatal if this command fails.
		  (efs-guess-hash-mark-size proc)

		  (if efs-use-passive-mode
		      (efs-passive-mode host user))
		  
		  ;; Run any user startup functions
		  (let ((alist efs-ftp-startup-function-alist)
			(case-fold-search t))
		    (while alist
		      (if (string-match (car (car alist)) host)
			  (progn
			    (funcall (cdr (car alist)) host user)
			    (setq alist nil))
			(setq alist (cdr alist)))))
		  
		  ;; Guess at the host type.
		  (efs-guess-host-type host user)
		  
		  ;; Check the idle time.
		  (efs-check-idle host user)

		  proc)

	      ;; Hopefully a recursive retry worked.
	      (or (efs-get-process-internal host user)
		  (error "No FTP process for %s@%s" user host)))))))))

(defun efs-guess-hash-mark-size (proc)
  ;; Doesn't run efs-save-match-data. You must do that yourself.
  (if efs-send-hash
      (save-excursion
	(set-buffer (process-buffer proc))
	(let ((line (nth 1 (efs-raw-send-cmd proc "hash")))
	      (gate-p (efs-use-gateway-p efs-process-host t)))
	  ;; Don't guess if the hash-mark-size is already set.
	  (or (if gate-p efs-gateway-hash-mark-size efs-hash-mark-size)
	      (if (string-match efs-hash-mark-msgs line)
		  (let ((size (substring line (match-beginning 1)
					 (match-end 1))))
		    (if (string-match "^[0-9]+$" size)
			(set (if gate-p
				 'efs-gateway-hash-mark-size
			       'efs-hash-mark-size)
			     (string-to-int size))))))))))

(defun efs-passive-mode (host user)
  ;; put ftp into passive mode
  (efs-send-cmd host user '(passive)))

;;;; ------------------------------------------------------------
;;;; Simple FTP process shell support.
;;;; ------------------------------------------------------------

(defun efs-mode (host user prompt)
  "Major mode for interacting with an FTP process.
The user interface for sending commands to the FTP process is `comint-mode'.
For more information see the documentation for `comint-mode'.  This command
is not intended for interactive use.
Takes arguments: HOST USER PROMPT

Runs efs-mode-hook if it is not nil.

Key map:
\\{comint-mode-map}"
  (let ((proc (get-buffer-process (current-buffer))))
    ;; Running comint-mode will kill-all-local-variables.
    (comint-mode)
    ;; All these variables are buffer local.
    (setq major-mode 'efs-mode
	  mode-name "efs"
	  default-directory (file-name-directory efs-tmp-name-template)
	  comint-prompt-regexp prompt
	  efs-process-host host
	  efs-process-user user
	  efs-process-prompt-regexp prompt)
    (set (make-local-variable 'paragraph-start) comint-prompt-regexp)
    ;; Old versions of comint don't have this.  It does no harm for
    ;; the newer ones.
    (set (make-local-variable 'comint-last-input-start) (make-marker))
    (set (make-local-variable 'comint-last-input-end) (make-marker))
    (goto-char (point-max))
    ;; in case there is a running process
    (if proc (set-marker (process-mark proc) (point)))
    (run-hooks 'efs-mode-hook)))


;;;; =============================================================
;;;; >6
;;;; Sending commands to the FTP server.
;;;; =============================================================

;;;; -------------------------------------------------------------
;;;; General purpose functions for sending commands.
;;;; -------------------------------------------------------------

(defun efs-raw-send-cmd (proc cmd &optional msg pre-cont cont nowait)
;; Low-level routine to send the given ftp CMD to the ftp PROCESS.
;; MSG is an optional message to output before and after the command.
;; If PRE-CONT is non-nil, it is called immediately after execution
;; of the command starts, but without waiting for it to finish.
;; If CONT is non-NIL then it is either a function or a list of function and
;; some arguments.  The function will be called when the ftp command has 
;; completed.
;; If CONT is NIL then this routine will return \( RESULT . LINE \) where
;; RESULT is whether the command was successful, and LINE is the line from
;; the FTP process that caused the command to complete.
;; If NOWAIT is nil then we will wait for the command to complete before 
;; returning. If NOWAIT is 0, then we will wait until the command starts,
;; executing before returning. NOWAIT of 1 is like 0, except that the modeline
;; will indicate an asynch FTP command.
;; If NOWAIT has any other value, then we will simply queue the
;; command. In all cases, CONT will still be called

  (if (memq (process-status proc) '(run open))
      (efs-save-buffer-excursion
	(set-buffer (process-buffer proc))
	
	(if efs-process-busy
	    ;; This function will always wait on a busy process.
	    ;; Queueing is done by efs-send-cmd.
	    (let ((efs-process-cmd-waiting t))
	      (efs-kbd-quit-protect proc
		(while efs-process-busy
		  (accept-process-output)))))

	(setq efs-process-string ""
	      efs-process-result-line ""
	      efs-process-result-cont-lines ""
	      efs-process-busy t
	      efs-process-msg (and efs-verbose msg)
	      efs-process-continue cont
	      efs-process-server-confused nil
	      efs-process-nowait nowait
	      efs-process-hash-mark-count 0
	      efs-process-hash-mark-history (list (list 0 (current-time)))
	      efs-process-last-percent -1
	      efs-process-xfer-size 0
	      efs-process-cmd-counter (% (1+ efs-process-cmd-counter) 16))
	(process-kill-without-query proc t)
	(and efs-process-msg
	     (efs-message-p)
	     (efs-message "%s..." efs-process-msg))
	(goto-char (point-max))
	(move-marker comint-last-input-start (point))
	(move-marker comint-last-input-end (point))
	;; don't insert the password into the buffer on the USER command.
	(efs-save-match-data
	  (if (string-match efs-passwd-cmds cmd)
	      (insert (setq efs-process-cmd
			    (substring cmd 0 (match-end 0)))
		      " Turtle Power!\n")
	    (setq efs-process-cmd cmd)
	    (insert cmd "\n")))
	(process-send-string proc (concat cmd "\n"))
	(set-marker (process-mark proc) (point))
	;; Update the mode-line
	(if (and (or efs-mode-line-format efs-ftp-activity-function)
		 (memq nowait '(t 1)))
	    (efs-update-mode-line))
	(if pre-cont
	    (let ((efs-nested-cmd t))
	      (save-excursion
		(apply (car pre-cont) (cdr pre-cont)))))
	(prog1
	    (if nowait 
		nil
	      ;; hang around for command to complete
	      ;; Some clients die after the command is sent, if the server
	      ;; times out. Don't wait on dead processes.
	      (efs-kbd-quit-protect proc
		(while (and efs-process-busy
			    ;; Need to recheck nowait, since it may get reset
			    ;; in a cont.
			    (null efs-process-nowait)
			    (memq (process-status proc) '(run open)))
		  (accept-process-output proc)))
	      
	      ;; cont is called by the process filter
	      (if cont
		  ;; Return nil if a cont was called.
		  ;; Can't return process-result
		  ;; and process-line since executing
		  ;; the cont may have changed
		  ;; the state of the process buffer.
		  nil
		(list efs-process-result
		      efs-process-result-line
		      efs-process-result-cont-lines)))
	  
	  ;; If the process died, the filter would have never got the chance
	  ;; to call the cont. Try to jump start things.
	  
	  (if (and (not (memq (process-status proc) '(run open)))
		   (string-equal efs-process-result-line "")
		   cont
		   (equal cont efs-process-continue))
	      (progn
		(setq efs-process-continue nil
		      efs-process-busy nil)
		;; The process may be in some strange state. Get rid of it.
		(condition-case nil (delete-process proc) (error nil))
		(efs-call-cont cont 'fatal "" "")))))
    
    (error "FTP process %s has died." (process-name proc))))

(efs-defun efs-quote-string nil (string &optional not-space)
  "Quote any characters in STRING that may confuse the ftp process.
If NOT-SPACE is non-nil, then blank characters are not quoted, because
it is assumed that the string will be surrounded by \"'s."
  (apply (function concat)
	 (mapcar (function
		   (lambda (char)
		     (if (or (< char ?\ )
			     (and (null not-space) (= char ?\ ))
			     (> char ?\~)
		             (= char ?\")
			     (= char ?\\))
			 (vector ?\\ char)
		       (vector char))))
		 string)))

(efs-defun efs-fix-path nil (path &optional reverse)
  "Convert PATH from a unix format to a non-unix format.
If optional REVERSE, convert in the opposite direction."
  (identity path))

(efs-defun efs-fix-dir-path nil (dir-path)
  "Convert DIR-PATH from unix format to a non-unix format for a dir listing"
  ;; The default def runs for dos-distinct, ka9q, and all the unix's.
  ;; To be more careful about distinguishing dirs from plain files,
  ;; we append a ".".
  (let ((len (length dir-path)))
    (if (and (not (zerop len)) (= (aref dir-path (1- len)) ?/))
	(concat dir-path ".")
      dir-path)))

(defun efs-send-cmd (host user cmd
			       &optional msg pre-cont cont nowait noretry)
  "Find an ftp process connected to HOST logged in as USER and send it CMD.
MSG is an optional status message to be output before and after issuing the
command.

See the documentation for efs-raw-send-cmd for a description of CONT, PRE-CONT
and NOWAIT. Normally, if the command fails it is retried. If NORETRY is
non-nil, this is not done."
  ;; Handles conversion to remote pathname syntax and remote ls option
  ;; capability. Also, sends umask if nec.

  (let ((proc (efs-get-process host user)))
    
    (if (and
	 (eq nowait t)
	 (save-excursion
	   (set-buffer (process-buffer proc))
	   (or efs-process-busy
	       efs-process-cmd-waiting)))
	
	(progn
	  (efs-add-to-queue
	   host user
	   ;; Not nec. to store host and user, because the queue is for
	   ;; a specific host user pair anyway. Because the queue is always
	   ;; examined when efs-process-busy
	   ;; is nil, it should be impossible to get into a loop
	   ;; where we keep re-queueing over and over. To be on the safe
	   ;; side, store nowait as 1.
	   (list cmd msg pre-cont cont 1 noretry))
	  nil)
      
      ;; Send a command.

      (let (cmd-string afsc-result afsc-line afsc-cont-lines)

	(let ((efs-nested-cmd t)
	      (cmd0 (car cmd))
	      (cmd1 (nth 1 cmd))
	      (cmd2 (nth 2 cmd))
	      (cmd3 (nth 3 cmd)))
	  
	  (cond
	   
	   ((eq cmd0 'quote)
	    ;; QUOTEd commands
	    (cond
	     
	     ((eq cmd1 'site)
	      ;; SITE commands
	      (cond
	       ((memq cmd2 '(umask idle dos exec nfs group gpass))
		;; For UMASK cmd3 = value of umask
		;; For IDLE cmd3 = idle setting, or nil if we're querying.
		;; For DOS and NFS cmd3 is nil.
		;; For EXEC cmd3 is the command to be exec'ed -- a string.
		(if cmd3 (setq cmd3 (concat " " cmd3)))
		(setq cmd-string (concat "quote site " (symbol-name cmd2)
					 cmd3)))
	       ((eq cmd2 'chmod)
		(let* ((host-type (efs-host-type host user))
		       (cmd4 (efs-quote-string
			      host-type (efs-fix-path host-type (nth 4 cmd)))))
		  (setq cmd-string (concat "quote site chmod " cmd3 " "
					   cmd4))))
	       (t (error "efs: Don't know how to send %s %s %s %s"
			 cmd0 cmd1 cmd2 cmd3))))
	     
	     ((memq cmd1 '(pwd xpwd syst pasv noop))
	      (setq cmd-string (concat "quote " (symbol-name cmd1))))
	     
	     ;; PORT command (cmd2 is IP + port address)
	     ((eq cmd1 'port)
	      (setq cmd-string (concat "quote port " cmd2)))

	     ((memq cmd1 '(appe retr))
	      (let ((host-type (efs-host-type host user)))
		;; Set an xfer type
		(if cmd3 (efs-set-xfer-type host user cmd3 t))
		(setq cmd2 (efs-quote-string host-type
					     (efs-fix-path host-type cmd2))
		      cmd-string (concat "quote " (symbol-name cmd1) " "
					 cmd2))))
	     
	     ((eq cmd1 'stor)
	      (let ((host-type (efs-host-type host user)))
		(if (memq host-type efs-unix-host-types)
		    (efs-set-umask host user))
		;; Set an xfer type
		(if cmd3 (efs-set-xfer-type host user cmd3 t))
		(setq cmd2 (efs-quote-string host-type
					     (efs-fix-path host-type cmd2))
		      cmd-string (concat "quote stor " cmd2))))
	     
	     ((memq cmd1 '(size mdtm rnfr))
	      (let ((host-type (efs-host-type host user)))
		(setq cmd2 (efs-quote-string host-type
					     (efs-fix-path host-type cmd2))
		      cmd-string (concat "quote "
					 (symbol-name cmd1) " " cmd2))))

	     ((memq cmd1 '(pass user))
	      (setq cmd-string (concat "quote " (symbol-name cmd1) " " cmd2)))
	     
	     (t
	      (error "efs: Don't know how to send %s %s %s %s"
		     cmd0 cmd1 cmd2 cmd3))))
	   
	   ;; TYPE command
	   ((eq cmd0 'type)
	    (setq cmd-string (concat "type " (symbol-name cmd1))))
     
	   ;; CWD command
	   ((eq cmd0 'cwd)
	    (let ((host-type (efs-host-type host user)))
	      (setq cmd1 (efs-quote-string host-type
					   (efs-fix-path host-type cmd1))
		    cmd-string (concat "quote " (symbol-name cmd0) " " cmd1))))

	   ;; DIR command
	   ;; cmd == 'dir "remote-path" "local-path" "ls-switches"
	   ((memq cmd0 '(dir nlist))
	    (let ((host-type (efs-host-type host user))
		  (listing-type (efs-listing-type host user)))
	      (setq cmd1 (efs-fix-dir-path host-type cmd1))
	      (cond
	       ((memq listing-type efs-nlist-listing-types)
		(setq cmd-string (concat efs-nlist-cmd " "
					 (efs-quote-string host-type cmd1)
					 " " cmd2)))
	       ((or (memq host-type efs-dumb-host-types)
		    (null cmd3))
		(setq cmd-string (format "%s %s %s"
					 (if (eq cmd0 'nlist)
					     efs-nlist-cmd
					   "dir")
					 (efs-quote-string host-type cmd1)
					 cmd2)))
	       ((setq cmd-string
		      (format "%s \"%s%s%s\" %s"
			      (if (eq cmd0 'nlist)
				  efs-nlist-cmd
				"ls")
			      cmd3
			      (if (string-equal cmd1 "") "" " ")
			      (efs-quote-string host-type cmd1 t)
			      ;; cmd2 is a temp file, not nec. to quote.
			      cmd2))))))
	   
	   ;; First argument is the remote pathname
	   ((memq cmd0 '(delete mkdir rmdir cd))
	    (let ((host-type (efs-host-type host user)))
	      (setq cmd1 (efs-quote-string host-type
					   (efs-fix-path host-type cmd1))
		    cmd-string (concat (symbol-name cmd0) " " cmd1))))
	   
	   ;; GET command
	   ((eq cmd0 'get)
	    (let ((host-type (efs-host-type host user)))
	      (if cmd3 (efs-set-xfer-type host user cmd3))
	      (efs-set-hash-mark-unit host user t)
	      (setq cmd1 (efs-quote-string host-type
					   (efs-fix-path host-type cmd1))
		    cmd-string (concat "get " cmd1 " " cmd2))))
	   
	   ;; PUT command
	   ((eq cmd0 'put)
	    (let ((host-type (efs-host-type host user)))
	      (if (memq host-type efs-unix-host-types)
		  (efs-set-umask host user))
	      (if cmd3 (efs-set-xfer-type host user cmd3))
	      (efs-set-hash-mark-unit host user)
	      (setq cmd2 (efs-quote-string host-type
					   (efs-fix-path host-type cmd2))
		    cmd-string (concat "put " cmd1 " " cmd2))))

	   ;; APPEND command
	   ((eq cmd0 'append)
	    (let ((host-type (efs-host-type host user)))
	      (if cmd3 (efs-set-xfer-type host user cmd3))
	      (efs-set-hash-mark-unit host user)
	      (setq cmd2 (efs-quote-string host-type
					   (efs-fix-path host-type cmd2))
		    cmd-string (concat "append " cmd1 " " cmd2))))
	   
	   ;; CHMOD command
	   ((eq cmd0 'chmod)
	    (let ((host-type (efs-host-type host user)))
	      (setq cmd2 (efs-quote-string host-type
					   (efs-fix-path host-type cmd2))
		    cmd-string (concat "chmod " cmd1 " " cmd2))))
	   
	   ;; Both arguments are remote pathnames
	   ((eq cmd0 'rename)
	    (let ((host-type (efs-host-type host user)))
	      (setq cmd1 (efs-quote-string host-type
					   (efs-fix-path host-type cmd1))
		    cmd2 (efs-quote-string host-type
					   (efs-fix-path host-type cmd2))
		    cmd-string (concat "rename " cmd1 " " cmd2))))
	   
	   ;; passive command
	   ((eq cmd0 'passive)
	    (setq cmd-string "passive"))
	
	   (t
	    (error "efs: Don't know how to send %s %s %s %s"
		   cmd0 cmd1 cmd2 cmd3))))
	  
	;; Actually send the resulting command.
	;; Why do we use this complicated binding of afsc-{result,line},
	;; rather then use the fact that efs-raw-send-cmd returns?
	;; Because efs-raw-send-cmd returns the result of the first
	;; attempt only. efs-send-cmd should return the result of
	;; the retry, if one was necessary.
	;; Maybe it would be better if efs-raw-send-cmd returned
	;; the result of cont, if nowait was nil? Or maybe still return
	;; \(result  line \)? As long as nowait is nil, it should
	;; return something useful.

	;; Beware, if some of the above FTP commands had to restart
	;; the process, PROC won't be set to the right process object.
	(setq proc (efs-get-process host user))
	
	(efs-raw-send-cmd
	 proc
	 cmd-string
	 msg
	 pre-cont
	 (efs-cont (result line cont-lines) (host user proc cmd msg pre-cont
						  cont nowait noretry)
	   (cond ((and (null noretry) (eq result 'fatal))
		  (let ((retry
			 (efs-send-cmd
			  host user cmd msg pre-cont cont
			  (if (eq nowait t) 1 nowait) t)))
		    (or cont nowait
			(setq afsc-result (car retry)
			      afsc-line (nth 1 retry)
			      afsc-cont-lines (nth 2 retry)))))
		 ((and (eq result 'failed)
		       (or (memq (car cmd) '(append rename put))
			   (and (eq (car cmd) 'quote)
				(eq (nth 1 cmd) 'stor)))
		       (efs-save-match-data
			 (string-match efs-write-protect-msgs line)))
		  (let ((retry (efs-write-recover
				(efs-host-type host)
				line cont-lines host user cmd msg pre-cont
				cont nowait noretry)))
		    (or cont nowait
			(setq afsc-result (car retry)
			      afsc-line (nth 1 retry)
			      afsc-cont-lines (nth 2 retry)))))
		 
		 (t (if cont
			(efs-call-cont cont result line cont-lines)
		      (or nowait
			  (setq afsc-result result
				afsc-line line
				afsc-cont-lines cont-lines))))))
	 nowait)
	
	(prog1
	    (if (or nowait cont)
		nil
	      (list afsc-result afsc-line afsc-cont-lines))
	  
	  ;; Check the queue
	  (or nowait
	      efs-nested-cmd
	      (let ((buff (efs-ftp-process-buffer host user)))
		(if (get-buffer buff)
		    (save-excursion
		      (set-buffer buff)
		      (if efs-process-q
			  (let ((next (car efs-process-q)))
			    (setq efs-process-q (cdr efs-process-q))
			    (apply 'efs-send-cmd host user next))))))))))))

(efs-defun efs-write-recover nil
  (line cont-lines host user cmd msg pre-cont cont nowait noretry)
  "Called when a write command fails with `efs-write-protect-msgs'.
Should return \(result  line cont-lines\), like `efs-raw-send-cmd'."
  ;; This default version doesn't do anything.
  (if cont
      (progn
	(efs-call-cont cont 'failed line cont-lines)
	nil)
    (if nowait nil (list 'failed line cont-lines))))

;;;; ---------------------------------------------------------------------
;;;; The login sequence. (The follows RFC959 rather tightly. If a server
;;;;                      can't even get the login codes right, it is
;;;;                      pretty much scrap metal.)
;;;; ---------------------------------------------------------------------

;;;###autoload
(defun efs-nslookup-host (host)
  "Attempt to resolve the given HOSTNAME using nslookup if possible."
  (interactive "sHost: ")
  (if efs-nslookup-program
      (let* ((default-directory exec-directory)
	     (default-major-mode 'fundamental-mode)
	     (process-connection-type nil)
	     (proc (start-process " *nslookup*" " *nslookup*"
				  efs-nslookup-program host))
	     (res host))
	(process-kill-without-query proc)
	(save-excursion
	  (set-buffer (process-buffer proc))
	  (let ((quit-flag nil)
		(inhibit-quit nil))
	    (while (memq (process-status proc) '(run open))
	      (accept-process-output proc)))
	  (goto-char (point-min))
	  (if (re-search-forward
	       "Name:.*\nAddress\\(es\\)?: *\\([.0-9]+\\)$" nil t)
	      (setq res (buffer-substring (match-beginning 2)
					  (match-end 2))))
	  (kill-buffer (current-buffer)))
	(if (interactive-p)
	    (message "%s: %s" host res))
	res)
    (if (interactive-p)
	(message
	 "No nslookup program. See the variable efs-nslookup-program."))
    host))

(defun efs-login (host user proc)
  "Connect to the FTP-server on HOST as USER.
PROC is the process to the FTP-client. Doesn't call efs-save-match-data.
You must do that yourself."
  (let ((gate (efs-use-gateway-p host)))
    (if (eq gate 'kerberos)
	(progn
	  (setq proc (efs-kerberos-login host user proc))
	  (efs-login-send-user host user proc gate))
      (let ((to (if (memq gate '(proxy sidewinder raptor))
		    efs-gateway-host
		  host))
	    port cmd result)
	(if (string-match "#" to)
	    (setq port (substring to (match-end 0))
		  to (substring to 0 (match-beginning 0))))
	(and efs-nslookup-on-connect
	     (string-match "[^0-9.]" to)
	     (setq to (efs-nslookup-host to)))
	(setq cmd (concat "open " to))
	(if port (setq cmd (concat cmd " " port)))
	
	;; Send OPEN command.
	(setq result (efs-raw-send-cmd proc cmd nil))
	
	(and (eq gate 'interlock) (string-match "^331 " (nth 1 result))
	     (setq result (efs-login-send-pass
			   efs-gateway-host
			   (efs-get-user efs-gateway-host) proc)))
	
	;; Analyze result of OPEN.
	(if (car result)
	    (progn
	      (condition-case nil (delete-process proc) (error nil))
	      (efs-error host user (concat "OPEN request failed: "
					   (nth 1 result))))
	  (efs-login-send-user host user proc gate))))))

(defun efs-login-send-user (host user proc &optional gate retry)
  "Send user command to HOST and USER. PROC is the ftp client process.
Optional argument GATE specifies which type of gateway is being used.
RETRY argument specifies to try twice if we get a 421 response."
  (let ((cmd (cond
	      ((memq gate '(proxy sidewinder interlock))
	       (format "quote USER \"%s\"@%s" user
		       (if (and efs-nslookup-on-connect
				(string-match "[^0-9.]" host))
			   (efs-nslookup-host host)
			 host)))
	      ((eq gate 'raptor)
	       (format "quote USER \"%s\"@%s %s" user
		       (if (and efs-nslookup-on-connect
				(string-match "[^0-9.]" host))
			   (efs-nslookup-host host)
			 host)
		       (nth 3 efs-gateway-type)))
	      ((eq gate 'kerberos)
	       (let ((to host)
		     port)
		 (if (string-match "#" host)
		     (progn
		       (setq to (substring host 0 (match-beginning 0))
			     port (substring host (match-end 0)))
		       (and efs-nslookup-on-connect
			    (string-match "[^0-9.]" to)
			    (efs-nslookup-host to))
		       (setq to (concat to "@" port))))
		 (format "quote user \"%s\"@%s" user to)))
	      (t
	       (format "quote user \"%s\"" user))))
	(msg (format "Logging in as user %s%s..." user
		     (if (memq gate '(proxy sidewinder raptor kerberos))
			 (concat "@" host) "")))  
	result code)	 
	
    ;; Send the message by hand so that we can report on the size
    ;; of the MOTD.
    (message msg)
    
    ;; Send USER command.
    (setq result (efs-raw-send-cmd proc cmd nil))

	(if (eq gate 'sidewinder)
		(setq result (efs-raw-send-cmd proc (format "quote user \"%s\"" user))))
    
    ;; Analyze result of USER (this follows RFC959 strictly)
    (if (< (length (nth 1 result)) 4)
	(progn
	  (condition-case nil (delete-process proc) (error nil))
	  (efs-error host user
		     (concat "USER request failed: " (nth 1 result))))

      (setq code (substring (nth 1 result) 0 4))
      (cond
       
       ((string-equal "331 " code)
	;; Need password
	(setq result (efs-login-send-pass host user proc gate)))
     
       ((string-equal "332 " code)
	;; Need an account, but no password
	(setq result (efs-login-send-acct host user proc gate)))
     
       ((null (car result))
	;; logged in proceed
	nil)

       ((and (or (string-equal "530 " code) (string-equal "421 " code))
	     (efs-anonymous-p user)
	     (or (string-match efs-too-many-users-msgs (nth 1 result))
		 (string-match efs-too-many-users-msgs (nth 2 result))))
	(if (save-window-excursion
	      (condition-case nil
		  (display-buffer (process-buffer proc))
		(error nil))
	      (y-or-n-p (format
			 "Too many users for %s@%s. Try again? "
			 user host)))
	    (progn
	      ;; Set result to nil if we are doing a retry, so done
	      ;; message only gets sent once.
	      (setq result nil)
	      (if (string-equal code "530 ")
		  (efs-login-send-user host user proc gate t)
		(efs-get-process host user)))
	  (signal 'quit nil)))
       
       ((and retry (string-equal code "421 "))
	(setq result nil)
	(efs-get-process host user))
       
       (t  ; bombed
	(condition-case nil (delete-process proc) (error nil))
	;; Wrong username?
	(efs-set-user host nil)
	(efs-error host user
		   (concat "USER request failed: " (nth 1 result)))))
      (and (null (car result))
	   (stringp (nth 2 result))
	   (message "%sdone%s" msg
		    (let ((n (efs-occur-in-string ?\n (nth 2 result))))
		      (if (> n 1)
			  (format "; MOTD of %d lines" n)
			"")))))))

(defun efs-login-send-pass (host user proc &optional gate)
  "Sends password to HOST and USER. PROC is the ftp client process.
Doesn't call efs-save-match data. You must do that yourself."
  ;; Note that efs-get-password always returns something.
  ;; It prompts the user if necessary. Even if the returned password is
  ;; \"\", send it, because we wouldn't be running this function
  ;; if the server wasn't insisting on a password.
  (let* ((pass "")
	 (qpass "")
	 (cmd "")
	 (result (unwind-protect
		     (progn
		       (condition-case nil
			   (setq pass (efs-get-passwd host user))
			 (quit (condition-case nil
				   (kill-buffer (process-buffer proc))
				 (error nil))
			       (signal 'quit nil)))
		       (setq cmd (concat
				  "quote pass "
				  (setq qpass (efs-quote-string nil pass t))))
		       (efs-raw-send-cmd proc cmd))
		   (fillarray pass 0)
		   (fillarray qpass 0)
		   (fillarray cmd 0)))
	 (code (and (>= (length (nth 1 result)) 4)
		    (substring (nth 1 result) 0 4))))
    (or code (setq code ""))
    ;; Analyze the result.
    (cond
     ((string-equal code "332 ")
      ;; require an account passwd
      (setq result (efs-login-send-acct host user proc gate)))
     ((null (car result))
      ;; logged in proceed
      nil)
     ((or (string-equal code "530 ") (string-equal code "421 "))
      ;; Give the user another chance
      (condition-case nil
	  (if (efs-anonymous-p user)
	      (if (or (string-match efs-too-many-users-msgs (nth 1 result))
		      (string-match efs-too-many-users-msgs (nth 2 result)))
		  (if (save-window-excursion
			(condition-case nil
			    (display-buffer (process-buffer proc))
			  (error nil))
			(y-or-n-p (format
				   "Too many users for %s@%s. Try again? "
				   user host)))
		      (progn
			;; Return nil if we are doing a retry, so done
			;; message only gets sent once.
			(setq result nil)
			(if (string-equal code "530 ")
			    (efs-login-send-user host user proc gate)
			  (efs-get-process host user)))
		    (signal 'quit nil))
		(unwind-protect
		    (efs-set-passwd
		     host user
		     (save-window-excursion
		       (condition-case nil
			   (display-buffer (process-buffer proc))
			 (error nil))
		       (setq pass
			     (read-passwd
			      (format
			       "Password for %s@%s failed. Try again: "
			       user host)))))
		  (fillarray pass 0))
		(setq result nil)
		(efs-login-send-user host user proc gate))
	    (unwind-protect
		(efs-set-passwd
		 host user
		 (setq pass
		       (read-passwd
			(format "Password for %s@%s failed. Try again: "
				user host))))
	      (fillarray pass 0))
	    (setq result nil)
	    (efs-login-send-user host user proc gate))
	(quit (condition-case nil (delete-process proc) (error nil))
	      (efs-set-user host nil)
	      (efs-set-passwd host user nil)
	      (signal 'quit nil))
	(error (condition-case nil (delete-process proc) (error nil))
	       (efs-set-user host nil)
	       (efs-set-passwd host user nil)
	       (efs-error host user "PASS request failed."))))
     (t ; bombed for unexplained reasons
      (condition-case nil (delete-process proc) (error nil))
      (efs-error host user (concat "PASS request failed: " (nth 1 result)))))
    result))

(defun efs-login-send-acct (host user proc &optional gate)
  "Sends account password to HOST and USER. PROC is the ftp client process.
Doesn't call efs-save-match data. You must do that yourself."
  (let* ((acct "")
	 (qacct "")
	 (cmd "")
	 (result (unwind-protect
		     (progn
		       ;; The raptor gateway requires us to send a gateway
		       ;; authentication password for account.  What if the
		       ;; remote server wants one too?
		       (setq acct (if (eq gate 'raptor)
				      (efs-get-account
				       efs-gateway-host
				       (nth 3 efs-gateway-type) nil t)
				    (efs-get-account host user nil t))
			     qacct (efs-quote-string nil acct t)
			     cmd (concat "quote acct " qacct))
		       (efs-raw-send-cmd proc cmd))
		   (fillarray acct 0)
		   (fillarray qacct 0)
		   (fillarray cmd 0))))
    ;; Analyze the result
    (cond
     ((null (car result))
      ;; logged in proceed
      nil)
     ((eq (car result) 'failed)
      ;; Give the user another chance
      (condition-case nil
	  (progn
	    (unwind-protect
		(progn
		  (setq acct (read-passwd
			      (format
			       "Account password for %s@%s failed. Try again: "
			       user host)))
		  (or (and efs-high-security-hosts
			   (string-match efs-high-security-hosts
					 (format "%s@%s" user host)))
		      (efs-set-account host user nil acct)))
	      (fillarray acct 0))
	    (setq result (efs-login-send-user host user proc gate)))
	(quit (condition-case nil (delete-process proc) (error nil)))
	(error (condition-case nil (delete-process proc) (error nil))
	       (efs-error host user "ACCT request failed."))))
     (t ; bombed for unexplained reasons
      (condition-case nil (delete-process proc) (error nil))
      (efs-error host user (concat "ACCT request failed: " (nth 1 result)))))
    result))

;;;; ----------------------------------------------------------------------
;;;; Changing working directory.
;;;; ----------------------------------------------------------------------

(defun efs-raw-send-cd (host user dir &optional no-error)
  ;; If NO-ERROR, doesn't barf, but just returns success (t) or failure (nil).
  ;; This does not use efs-send-cmd.
  ;; Also DIR must be in the syntax of the remote host-type.
  (let* ((cmd (concat "cd " dir))
	 cd-result cd-line)
    (efs-raw-send-cmd
     (efs-get-process host user)
     cmd nil nil
     (efs-cont (result line cont-lines) (cmd)
       (if (eq result 'fatal)
	   (efs-raw-send-cmd
	    (efs-get-process host user)
	    cmd nil nil
	    (function (lambda (result line cont-lines)
			(setq cd-result result
			      cd-line line))))
	 (setq cd-result result
	       cd-line line))))
    (if no-error
	(null cd-result)
      (if cd-result
	  (efs-error host user (concat "CD failed: " cd-line))))))

;;;; --------------------------------------------------------------
;;;; Getting a PWD.
;;;; --------------------------------------------------------------

(defun efs-unquote-quotes (string)
  ;; Unquote \"\"'s in STRING to \".
  (let ((start 0)
	new)
    (while (string-match "\"\"" string start)
      (setq new (concat new (substring
			     string start (1+ (match-beginning 0))))
	    start (match-end 0)))
    (if new
	(concat new (substring string start))
      string)))

(efs-defun efs-send-pwd nil (host user &optional xpwd)
  "Attempts to get the current working directory for the given HOST/USER pair.
Returns \( DIR . LINE \) where DIR is either the directory or NIL if not found,
and LINE is the relevant success or fail line from the FTP-server. If the
optional arg XPWD is given, uses this server command instead of PWD."
  (let* ((result (efs-send-cmd host user
			       (list 'quote (if xpwd 'xpwd 'pwd))
			       "Getting pwd"))
	 (line (nth 1 result))
	 dir)
    (or (car result)
	(efs-save-match-data
	  (if (string-match "\"\\(.*\\)\"[^\"]*$" line)
	      (setq dir (efs-unquote-quotes (substring line (match-beginning 1)
						       (match-end 1))))
	    (if (string-match " \\([^ ]+\\) " line) ; stone-age servers!
		(setq dir (substring line
				     (match-beginning 1)
				     (match-end 1)))))))
    (cons dir line)))

(efs-defun efs-send-pwd super-dumb-unix (host user &optional xpwd)
  ;; Guess at the pwd for a unix host that doesn't support pwd.
  (if (efs-anonymous-p user)
      ;; guess
      (cons "/" "")
    ;; Who knows?
    (message "Can't obtain pwd for %s" host)
    (ding)
    (sleep-for 2)
    (message "All file names must be specified as full paths.")
    (cons nil "")))

;;;; --------------------------------------------------------
;;;; Getting the SIZE of a remote file.
;;;; --------------------------------------------------------

(defun efs-send-size (host user file)
  "For HOST and USER, get the size of FILE in bytes.
This returns a list \( SIZE . LINE \), where SIZE is the file size in bytes,
or nil if this couldn't be determined, and LINE is the output line of the 
FTP server."
  (efs-save-match-data
    (let ((result (efs-send-cmd host user (list 'quote 'size file))))
      (setcar result
	      (and (null (car result))
		   (string-match "^213 +\\([0-9]+\\)$" (nth 1 result))
		   (string-to-int
		    (substring
		     (cdr result)
		     (match-beginning 1) (match-end 1)))))
      result)))

;;;; ------------------------------------------------------------
;;;; umask support
;;;; ------------------------------------------------------------

(defun efs-umask (user)
  "Returns the umask that efs will use for USER.
If USER is root or anonymous, then the values of efs-root-umask
and efs-anonymous-umask, respectively, take precedence, to be followed
by the value of efs-umask, and if this is nil, it returns your current
umask on the local machine. Returns nil if this can't be determined."
  (or
   (and (string-equal user "root") efs-root-umask)
   (and (efs-anonymous-p user)
	efs-anonymous-umask)
   efs-umask
   (and
    efs-umask-command
    (let* ((shell (or (and (boundp 'explicit-shell-file-name)
			   explicit-shell-file-name)
		      (getenv "ESHELL")
		      (getenv "SHELL")
		      "/bin/sh"))
	   (default-major-mode 'fundamental-mode)
	   (default-directory exec-directory)
	   (buff (get-buffer-create " *efs-umask-data*")))
      (unwind-protect
	  (save-excursion
	    (set-buffer buff)
	    (call-process shell nil buff nil "-c" efs-umask-command)
	    (goto-char (point-min))
	    (if (re-search-forward "[0-7]?[0-7]?[0-7]" nil t)
		(string-to-int (buffer-substring (match-beginning 0)
						 (match-end 0)))))
	(kill-buffer buff))))))

(defun efs-send-umask (host user mask)
  "Sets the umask on HOST for USER to MASK.
Returns t for success, nil for failure."
  (interactive
   (let* ((path (or buffer-file-name
		    (and (eq major-mode 'dired-mode)
			 dired-directory)))
	  (parsed (and path (efs-ftp-path path)))
	  (default-host (car parsed))
	  (default-user (nth 1 parsed))
	  (default-mask (efs-umask default-user)))
     (list
      (read-string "Host: " default-host)
      (read-string "User: " default-user)
      (read-string "Umask: " (int-to-string default-mask)))))
  (let (int-mask)
    (if (integerp mask)
	(setq int-mask mask
	      mask (int-to-string mask))
      (setq int-mask (string-to-int mask)))
    (or (string-match "^ *[0-7]?[0-7]?[0-7] *$" mask)
	(error "Invalid umask %s" mask))
    (efs-send-cmd host user
		  (list 'quote 'site 'umask mask)
		  (concat "Setting umask to " mask)
		  (list
		   (function
		    (lambda (int-mask)
		      (let ((buff (efs-ftp-process-buffer host user)))
			(if (get-buffer buff)
			    (save-excursion
			      (set-buffer buff)
			      (setq efs-process-umask int-mask))))))
		   int-mask)
		  (efs-cont (result line cont-lines) (host user mask)
		    (if result
			(let ((buff (efs-ftp-process-buffer host user)))
			  (efs-set-host-property host 'umask-failed t)
			  (if (get-buffer buff)
			      (save-excursion
				(set-buffer buff)
				(setq efs-process-umask nil)))
			  (message
			   "Unable to set umask to %s on %s" mask host)
			  (if efs-ding-on-umask-failure
			      (progn
				(ding)
				(sit-for 1))))))
		  0))) ; Do this NOWAIT = 0

(defun efs-set-umask (host user)
  "Sets the umask for HOST and USER, if it has not already been set."
  (save-excursion
    (set-buffer (process-buffer (efs-get-process host user)))
    (if (or efs-process-umask (efs-get-host-property host 'umask-failed))
	nil
      (let ((umask (efs-umask user)))
	(and umask
	     (progn
	       (efs-send-umask host user umask)
	       t))))))			; Tell the caller that we did something.
	
(defun efs-modes-from-umask (umask)
  ;; Given the 3 digit octal integer umask, returns the decimal integer
  ;; according to chmod that a file would be written with.
  ;; Assumes only ordinary files, so ignores x bits.
  (let* ((others (% umask 10))
	 (umask (/ umask 10))
	 (group (% umask 10))
	 (umask (/ umask 10))
	 (owner (% umask 10))
	 (factor 1))
    (apply '+
	   (mapcar
	    (function
	     (lambda (x)
	       (prog1
		   (* factor (- 6 (- x (% x 2))))
		 (setq factor (* factor 8)))))
	    (list others group owner)))))

;;;; ------------------------------------------------------------
;;;; Idle time manipulation.
;;;; ------------------------------------------------------------

(defun efs-check-idle (host user)
  ;; We just toss it in the queue to run whenever there's time.
  ;; Just fail quietly if this doesn't work.
  (if (and (or efs-maximize-idle efs-expire-ftp-buffers)
	   (memq (efs-host-type host) efs-idle-host-types)
	   (null (efs-get-host-property host 'idle-failed)))
      (let ((buffname (efs-ftp-process-buffer host user)))
	(efs-add-to-queue
	 host user
	 (list '(quote site idle)
	       nil nil
	       (efs-cont (result line cont-lines) (host user buffname)
		 (efs-save-match-data
		   (if (and (null result)
			    (string-match efs-idle-msgs line))
		       (let ((max (substring line (match-beginning 2)
					     (match-end 2))))
			 (if (get-buffer buffname)
			     (save-excursion
			       (set-buffer buffname)
			       (setq efs-process-idle-time
				     (string-to-int
				      (substring line (match-beginning 1)
						 (match-end 1))))))
			 (if (and efs-maximize-idle
				  (not (efs-anonymous-p user)))
			     (efs-add-to-queue
			      host user
			      (list
			       (list 'quote 'site 'idle max)
			       nil nil
			       (efs-cont (result line cont-lines) (buffname
								   max)
				 (and (null result)
				      (get-buffer buffname)
				      (save-excursion
					(set-buffer buffname)
					(setq efs-process-idle-time
					      (string-to-int max)))))
			       0))))
		     (efs-set-host-property host 'idle-failed t))))
	       0 nil))))) ; Using NOWAIT = 0 inhibits mode line toggling.


;;;; ------------------------------------------------------------
;;;; Sending the SYST command for system type.
;;;; ------------------------------------------------------------

(defun efs-get-syst (host user)
  "Use SYST to get the remote system type.
Returns the system type as a string if this succeeds, otherwise nil."
  (let* ((result (efs-send-cmd host user '(quote syst)))
	 (line (nth 1 result)))
    (efs-save-match-data
      (and (null (car result))
	   (string-match efs-syst-msgs line)
	   (substring line (match-end 0))))))
  
;;;; ------------------------------------------------------------
;;;; File transfer representation type support
;;;; ------------------------------------------------------------

;;; Legal representation types are: image, ascii, ebcdic, tenex

(efs-defun efs-file-type nil (path)
  ;; Returns the file type for PATH, the full efs path, with filename FILE.
  ;; The return value is one of 'text, '8-binary, or '36-binary.
  (let ((parsed (efs-ftp-path path)))
    (efs-save-match-data
      (cond
       ;; There is no special significance to temp names, but we assume that
       ;; they exist on an 8-bit byte machine.
       ((or (null path)
	    (let ((temp (intern-soft path efs-tmp-name-obarray)))
	      (and temp (memq temp efs-tmp-name-files))))
	'8-binary)
       ((and (null parsed) (file-exists-p path))
	(efs-local-file-type path))
       ;; test special hosts
       ((and parsed
	     efs-binary-file-host-regexp
	     (let ((case-fold-search t))
	       (string-match efs-binary-file-host-regexp (car parsed))))
	'8-binary)
       (t
	;; Test file names
	(let ((file (efs-internal-file-name-nondirectory
		     (or (nth 2 parsed) path))))
	  (cond
	   ;; test for PDP-10 binaries
	   ((and efs-36-bit-binary-file-name-regexp
		 (string-match efs-36-bit-binary-file-name-regexp file))
	    '36-binary)
	   ((and efs-binary-file-name-regexp
		 (string-match efs-binary-file-name-regexp file))
	    '8-binary)
	   ((and efs-text-file-name-regexp
		 (string-match efs-text-file-name-regexp file))
	    'text)
	   ;; by default
	   (t
	    '8-binary))))))))

(efs-define-fun efs-local-file-type (file)
  ;; Looks at the beginning (magic-cookie) of a local file to determine
  ;; if it is a text file or not.  If it's not a text file, it doesn't care
  ;; about what type of binary file, so this doesn't really look for a magic
  ;; cookie.
  ;; Doesn't call efs-save-match-data.  The caller should do so.
  (save-excursion
    (set-buffer (get-buffer-create efs-data-buffer-name))
    (erase-buffer)
    (insert-file-contents file nil 0 16)
    (if (looking-at "[ -~\n\r\C-L]*\\'")
	'text
      '8-binary)))

(defun efs-rationalize-file-type (f-type t-type)
  ;; When the original and new names for a file indicate
  ;; different file types, this function applies an ad hoc heuristic
  ;; to return a single file type.
  (cond
   ((eq f-type t-type)
    f-type)
   ((memq '36-binary (list f-type t-type))
    '36-binary)
   ((memq '8-binary (list f-type t-type))
    '8-binary)
   (t
    'text)))

(defun efs-prompt-for-transfer-type (arg)
  "Toggles value of efs-prompt-for-transfer-type.
With prefix arg, turns prompting on if arg is positive, otherwise turns
prompting off."
  (interactive "P")
  (if (if arg
	  (> (prefix-numeric-value arg) 0)
	(null efs-prompt-for-transfer-type))
      ;; turn prompting on
      (prog1
	  (setq efs-prompt-for-transfer-type t)
	(message "Prompting for FTP transfer TYPE is on."))
    (prog1
	(setq efs-prompt-for-transfer-type nil)
      (message "Prompting for FTP transfer TYPE is off."))))

(defun efs-read-xfer-type (path)
  ;; Prompt for the transfer type to use for PATH
  (let ((type
	 (completing-read
	  (format "FTP transfer TYPE for %s: " (efs-relativize-filename path))
	  '(("binary") ("image") ("ascii") ("ebcdic") ("tenex"))
	  nil t)))
    (if (string-equal type "binary")
	'image
      (intern type))))

(defun efs-xfer-type (f-host-type f-path t-host-type t-path
				  &optional via-local)
  ;; Returns the transfer type for transferring a file.
  ;; F-HOST-TYPE = the host type of the machine on which the file is from.
  ;; F-PATH = path, in full efs-syntax, of the original file
  ;; T-HOST-TYPE = host-type of the machine to which the file is being
  ;;               transferred.
  ;; VIA-LOCAL = non-nil of the file is being moved through the local, or
  ;;             a gateway machine.
  ;; Set F-PATH or T-PATH to nil, to indicate that the file is being
  ;; transferred from/to a temporary file, whose name has no significance.
  (let (temp)
    (and f-path
	 (setq temp (intern-soft f-path efs-tmp-name-obarray))
	 (memq temp efs-tmp-name-files)
	 (setq f-path nil))
    (and t-path
	 (setq temp (intern-soft t-path efs-tmp-name-obarray))
	 (memq temp efs-tmp-name-files)
	 (setq t-path nil)))
  (if (or (null (or f-host-type t-host-type)) (null (or f-path t-path)))
      'image ; local copy?
    (if efs-prompt-for-transfer-type
	(efs-read-xfer-type (if f-path f-path t-path))
      (let ((f-fs (cdr (assq f-host-type efs-file-type-alist)))
	    (t-fs (cdr (assq t-host-type efs-file-type-alist))))
	(if (and f-fs t-fs
		 (if efs-treat-crlf-as-nl
		     (and (eq (car f-fs) (car t-fs))
			  (eq (nth 1 f-fs) (nth 1 t-fs))
			  (let ((f2-fs (nth 2 f-fs))
				(t2-fs (nth 2 t-fs)))
			    (or (eq f2-fs t2-fs)
				(and (memq f2-fs '(file-crlf file-nl))
				     (memq t2-fs '(file-crlf file-nl))))))
		   (equal f-fs t-fs)))
	    'image
	  (let ((type (cond
		       ((and f-path t-path)
			(efs-rationalize-file-type
			 (efs-file-type t-host-type t-path)
			 (efs-file-type f-host-type f-path)))
		       (f-path
			(efs-file-type f-host-type f-path))
		       (t-path
			(efs-file-type t-host-type t-path)))))
	    (cond
	     ((eq type '36-binary)
	      'image)
	     ((eq type '8-binary)
	      (if (or (eq (car f-fs) '36-bit-wa)
		      (eq (car t-fs) '36-bit-wa))
		  'tenex
		'image))
	     (t ; handles 'text
	      (if (and t-fs f-fs (eq (nth 1 f-fs) 'ebcdic)
		       (eq (nth 1 t-fs) 'ebcdic) (null via-local))
		  'ebcdic
		'ascii)))))))))

(defun efs-set-xfer-type (host user type &optional clientless)
  ;; Sets the xfer type for HOST and USER to TYPE.
  ;; If the connection is already using the required type, does nothing.
  ;; If clientless is non-nil, we are using a quoted xfer command, and
  ;; need to check if the client has changed things.
  (save-excursion
    (let ((buff (process-buffer (efs-get-process host user))))
      (set-buffer buff)
      (or (if (and clientless efs-process-client-altered-xfer-type)
	      (or (eq type efs-process-client-altered-xfer-type)
		  (setq efs-process-client-altered-xfer-type nil))
	    ;; We are sending a non-clientless command, so the client
	    ;; gets back in synch.
	    (setq efs-process-client-altered-xfer-type nil)
	    (and efs-process-xfer-type
		 (eq type efs-process-xfer-type)))
	  (let ((otype efs-process-xfer-type))
	    ;; Set this now in anticipation that the TYPE command will work,
	    ;; in case other commands, such as efs-set-hash-mark-unit want to
	    ;; grok this before the TYPE command completes.
	    (setq efs-process-xfer-type type)
	    (efs-send-cmd
	     host user (list 'type type)
	     nil nil
	     (efs-cont (result line cont-lines) (host user type otype buff)
	       (if result
		   (unwind-protect
		       (efs-error host user (format "TYPE %s failed: %s"
						    (upcase (symbol-name type))
						    line))
		     (if (get-buffer buff)
			 (save-excursion
			   (set-buffer buff)
			   (setq efs-process-xfer-type otype))))))
	     0)))))) ; always send type commands NOWAIT = 0


;;;; ------------------------------------------------------------
;;;; Obtaining DIR listings.
;;;; ------------------------------------------------------------

(defun efs-ls-guess-switches ()
  ;; Tries to determine what would be the most useful switches
  ;; to use for a DIR listing.
  (if (and (boundp 'dired-listing-switches)
	   (stringp dired-listing-switches)
	   (efs-parsable-switches-p dired-listing-switches t))
      dired-listing-switches
    "-al"))

(efs-defun efs-ls-dumb-check nil (line host file path lsargs msg noparse
				       noerror nowait cont)
  nil)

(efs-defun efs-ls-dumb-check unknown (line host file path lsargs
			       msg noparse noerror nowait cont)
  ;; Checks to see if the host type might be dumb unix. If so, returns the 
  ;; listing otherwise nil.
  (and
   lsargs
   (or
    (string-match
     ;; Some CMU servers return a 530 here.  550 is correct.
     (concat "^5[35]0 \\(The file \\)?"
	     (regexp-quote (concat lsargs " " path)))
     ;; 550 is for a non-accessible file -- RFC959
     line)
    (string-match
     (regexp-quote "^501 Too many arguments")
     line))
   (progn
     (if (eq (efs-host-type host) 'apollo-unix)
	 (efs-add-host 'dumb-apollo-unix host)
       (efs-add-host 'dumb-unix host))
     ;; try again
     (if nowait
	 t ; return t if asynch
	   ; This is because dumb-check can't run asynch.
	   ; This means that we can't recognize dumb hosts asynch.
	   ; Shouldn't be a problem.
       (efs-ls file nil
	       (if (eq msg t)
		   (format "Relisting %s" (efs-relativize-filename file))
		 msg)
	       noparse noerror nowait cont)))))

;; With no-error nil, this function returns:
;; an error if file is not an efs-path
;;                      (This should never happen.) 
;; an error if either the listing is unreadable or there is an ftp error.
;; the listing (a string), if everything works.
;; 
;; With no-error t, it returns:
;; an error if not an efs-path
;; error if listing is unreable (most likely caused by a slow connection)
;; nil if ftp error (this is because although asking to list a nonexistent
;;                   directory on a remote unix machine usually (except
;;                   maybe for dumb hosts) returns an ls error, but no
;;                   ftp error, if the same is done on a VMS machine,
;;                   an ftp error is returned. Need to trap the error
;;                   so we can go on and try to list the parent.)
;; the listing, if everything works.

(defun efs-ls (file lsargs msg &optional noparse noerror nowait cont nlist)
  "Return the output of a `DIR' or `ls' command done over ftp.
FILE is the full name of the remote file, LSARGS is any args to pass to the
`ls' command. MSG is a message to be displayed while listing, if MSG is given
as t, a suitable message will be computed. If nil, no message will be
displayed. If NOPARSE is non-nil, then the listing will not be parsed and
stored in internal cache. Otherwise, the listing will be parsed, if LSARGS
allow it. If NOERROR is non-nil, then we return nil if the listing fails,
rather than signal an error. If NOWAIT is non-nil, we do the listing
asynchronously, returning nil. If CONT is non-nil it is called with first
argument the listing string."
  ;; If lsargs are nil, this forces a one-time only dumb listing using dir.
  (setq file (efs-expand-file-name file))
  (let ((parsed (efs-ftp-path file)))
    (if parsed
	(let* ((host (nth 0 parsed))
	       (user (nth 1 parsed))
	       (path (nth 2 parsed))
	       (host-type (efs-host-type host user))
	       (listing-type (efs-listing-type host user))
	       (parse (cond
		       ((null noparse)
			(efs-parsable-switches-p lsargs t))
		       ((eq noparse 'parse)
			t)
		       (t nil)))
	       (switches lsargs)
	       cache)
	  
	  (if (memq host-type efs-dumb-host-types)
	      (setq lsargs nil))
	  (if (and (null efs-ls-uncache)
		   (setq cache
			 (or (efs-get-from-ls-cache file switches)
			     (and switches
				  (efs-convert-from-ls-cache
				   file switches host-type listing-type)))))
	      ;; The listing is in the mail, errr... cache.
	      (let (listing)
		(if (stringp cache)
		    (setq listing cache)
		  (setq listing (car cache))
		  (if (and parse (null (nth 1 cache)))
		      (save-excursion
			(set-buffer
			 (let ((default-major-mode 'fundamental-mode))
			   (get-buffer-create
			    efs-data-buffer-name)))
			(erase-buffer)
			(insert listing)
			(goto-char (point-min))
			(efs-set-files
			 file
			 (efs-parse-listing listing-type
					    host user path
					    file lsargs))
			;; Note that we have parsed it now.
			(setcar (cdr cache) t))))
		(if cont (efs-call-cont cont listing))
		listing)
	    
	    (if cache
		(efs-del-from-ls-cache file nil nil))
	    ;; Need to get the listing via FTP.
	    (let* ((temp (efs-make-tmp-name host nil))
		   (temp-file (car temp))
		   listing-result)
	      (efs-send-cmd
	       host user
	       (list 'cwd path)
	       (if (eq msg t)
		   (format "CWD'ing to %s" (efs-relativize-filename file))
		 msg)
	       nil
	       (efs-cont (result line cont-lines)
		   (host-type listing-type host user temp-file path
			      switches file lsargs noparse parse noerror
			      msg nowait cont)
		 (if result
		     (efs-error host user (format "CWD %s failed: " line))
		   (efs-send-cmd
		    host user
		    (list (if nlist 'nlist 'dir) "" (cdr temp) lsargs)
		    (if (eq msg t)
			(format "Listing %s" (efs-relativize-filename file))
		      msg)
		    nil
		    (efs-cont (result line cont-lines)
			(host-type listing-type host user temp-file path
				   switches file lsargs noparse parse noerror
				   msg nowait cont)
		      ;; The client flipped to ascii, remember this.
		      (let ((buff (get-buffer
				   (efs-ftp-process-buffer host user))))
			(if buff
			    (efs-save-buffer-excursion
			      (set-buffer buff)
			      (setq efs-process-client-altered-xfer-type
				    'ascii))))
		      (unwind-protect
			  (if result
			      (or (setq listing-result
					(efs-ls-dumb-check
					 (and (or (eq host-type 'unknown)
						  (eq listing-type 'unix:unknown))
					      'unknown)
					 line host file path lsargs msg
					 noparse noerror nowait cont))
				  ;; If dumb-check returns non-nil
				  ;; then it would have handled any error recovery
				  ;; and conts. listing-result would only be set to
				  ;; t if nowait was non-nil. Therefore, the final
				  ;; return for efs-ls could never be t, even if I
				  ;; set listing-result to t here.
				  (if noerror
				      (if cont
					  (efs-call-cont cont nil))
				    (efs-error host user
					       (concat "DIR failed: "
						       line))))
		       
			    ;; listing worked
			    (if (efs-ftp-path temp-file)
				(efs-add-file-entry (efs-host-type efs-gateway-host)
						    temp-file nil nil nil))
			    (save-excursion
			      ;; A hack to get around a jka-compr problem.
			      ;; Do we still need it?
			      (let ((default-major-mode 'fundamental-mode)
				    efs-verbose jka-compr-enabled)
				(set-buffer (get-buffer-create
					     efs-data-buffer-name))
				(erase-buffer)
				(if (or (file-readable-p temp-file)
					(sleep-for efs-retry-time)
					(file-readable-p temp-file))
				    (insert-file-contents temp-file)
				  (efs-error host user
					     (format
					      "list data file %s not readable"
					      temp-file))))
			      (if parse
				  (progn
				    (efs-set-files
				     file
				     (efs-parse-listing listing-type host user path
							file lsargs))
				    ;; Parsing may update the host type.
				    (and lsargs (memq (efs-host-type host)
						      efs-dumb-host-types)
					 (setq lsargs nil))))
			      (let ((listing (buffer-string)))
				(efs-add-to-ls-cache file lsargs listing parse)
				(if (and (null lsargs) switches)
				    ;; Try to convert
				    (let ((conv (efs-get-ls-converter switches)))
				      (and conv
					   (setq conv (assoc
						       (char-to-string 0)
						       conv))
					   (funcall (cdr conv) listing-type nil)
					   (setq listing (buffer-string)))))
				(or nowait (setq listing-result listing))
				;; Call the ls cont, with first arg the
				;; listing string.
				(if cont
				    (efs-call-cont cont listing)))))
			(efs-del-tmp-name temp-file)))
		    nowait)))
	       nowait)
	      (and (null nowait) listing-result))))
      (error "Attempt to get a remote listing for the local file %s" file))))


;;;; ===============================================================
;;;; >7
;;;; Parsing and storing remote file system data.
;;;; ===============================================================

;;; The directory listing parsers do some host type guessing.
;;; Most of the host type guessing is done when the PWD output
;;; is parsed. A bit is done when the error codes for DIR are
;;; analyzed.

;;;; -----------------------------------------------------------
;;;; Caching directory listings.
;;;; -----------------------------------------------------------

;;;  Aside from storing files data in a hashtable, a limited number
;;;  of listings are stored in complete form in `efs-ls-cache'.

(defun efs-del-from-ls-cache (file &optional parent-p dir-p)
  ;; Deletes from the ls cache the listing for FILE.
  ;; With optional PARENT-P, deletes any entry for the parent
  ;; directory of FILE too.
  ;; If DIR-P is non-nil, then the directory listing of FILE is to be deleted.
  (if dir-p
      (setq file (file-name-as-directory file))
    (setq file (directory-file-name file)))
  (setq file (efs-canonize-file-name file))
  (if parent-p
      (setq parent-p (file-name-directory
		      (if dir-p
			  (directory-file-name file)
			file))))
  (setq efs-ls-cache
	(delq nil
	      (mapcar
	       (if parent-p
		   (function
		    (lambda (x)
		      (let ((f-ent (car x)))
			(and (not (string-equal file f-ent))
			     (not (string-equal parent-p f-ent)) 
			     x))))
		 (function
		  (lambda (x)
		    (and (not (string-equal file (car x)))
			 x))))
	       efs-ls-cache))))

(defun efs-wipe-from-ls-cache (host user)
  ;; Remove from efs-ls-cache all listings for HOST and USER.
  (let ((host (downcase host))
	(case-insens (memq (efs-host-type host)
			   efs-case-insensitive-host-types)))
    (if case-insens (setq user (downcase user)))
    (setq efs-ls-cache
	(delq nil
	      (mapcar
	       (function
		(lambda (x)
		  (let ((parsed (efs-ftp-path (car x))))
		    (and (not
			  (and (string-equal (car parsed) host)
			       (string-equal (if case-insens
						 (downcase (nth 1 parsed))
					       (nth 1 parsed))
					     user)))
			 x))))
	       efs-ls-cache)))))

(defun efs-get-from-ls-cache (file switches)
  ;; Returns the value in `ls-cache' for FILE and SWITCHES.
  ;; Returns a list consisting of the listing string, and whether its
  ;; already been parsed. This list is eq to the nthcdr 2 of the actual
  ;; cache entry, so you can setcar it.
  ;; For dumb listings, SWITCHES will be nil.
  (let ((list efs-ls-cache)
	(switches (efs-canonize-switches switches))
	(file (efs-canonize-file-name file)))
    (catch 'done
      (while list
	(if (and (string-equal file (car (car list)))
		 (string-equal switches (nth 1 (car list))))
	    (throw 'done (nthcdr 2 (car list)))
	  (setq list (cdr list)))))))

(defun efs-add-to-ls-cache (file switches listing parsed)
  ;; Only call after efs-get-from-cache returns nil, to avoid duplicate
  ;; entries. PARSED should be t, if the listing has already been parsed.
  (and (> efs-ls-cache-max 0)
       (let ((switches (efs-canonize-switches switches))
	     (file (efs-canonize-file-name file)))
	 (if (= efs-ls-cache-max 1)
	     (setq efs-ls-cache
		   (list (list file switches listing parsed)))
	   (if (>= (length efs-ls-cache) efs-ls-cache-max)
	       (setcdr (nthcdr (- efs-ls-cache-max 2) efs-ls-cache) nil))
	   (setq efs-ls-cache (cons (list file switches listing parsed)
				    efs-ls-cache))))))

;;;; --------------------------------------------------------------
;;;; Converting listings from cache.
;;;; --------------------------------------------------------------

(defun efs-get-ls-converter (to-switches)
  ;; Returns converter alist for TO-SWITCHES
  (efs-get-hash-entry (efs-canonize-switches to-switches)
		      efs-ls-converter-hashtable))

(defun efs-add-ls-converter (to-switches  from-switches converter)
  ;; Adds an entry to `efs-ls-converter-hashtable'.
  ;; If from-switches is t, the converter converts from internal files
  ;; hashtable.
  (let* ((to-switches (efs-canonize-switches to-switches))
	 (ent (efs-get-hash-entry to-switches efs-ls-converter-hashtable))
	 (add (cons (or (eq from-switches t)
			(efs-canonize-switches from-switches))
		    converter)))
    (if ent
	(or (member add ent)
	    (nconc ent (list add)))
      (efs-put-hash-entry to-switches (list add) efs-ls-converter-hashtable))))

(defun efs-convert-from-ls-cache (file switches host-type listing-type)
  ;; Returns a listing by converting the switches from a cached listing.
  (let ((clist (efs-get-ls-converter switches))
	(dir-p (eq ?/ (aref file (1- (length file)))))
	elt listing result regexp alist)
    (while file ; this loop will iterate at most twice.
      (setq alist clist)
      (while alist
	(setq elt (car alist))
	(if (eq (car elt) t)
	    (if (and dir-p (setq result (funcall (cdr elt) host-type
						 (let ((efs-ls-uncache t))
						   (efs-get-files file))
						 regexp)))
		(setq alist nil
		      file nil)
	      (setq alist (cdr alist)))
	  (if (and (setq listing
			 (efs-get-from-ls-cache file (car elt)))
		   (save-excursion
		     (set-buffer
		      (let ((default-major-mode 'fundamental-mode))
			(get-buffer-create efs-data-buffer-name)))
		     (erase-buffer)
		     (insert (car listing))
		     (and (funcall (cdr elt) listing-type regexp)
			  (setq result (buffer-string)))))
	      (setq alist nil
		    file nil)
	    (setq alist (cdr alist)))))
      ;; Look for wildcards.
      (if (and file (null dir-p) (null regexp))
	  (setq regexp (efs-shell-regexp-to-regexp
			(file-name-nondirectory file))
		file (file-name-directory file)
		dir-p t)
	(setq file nil)))
    result))

;;; Define some converters

(defun efs-unix-t-converter-sort-pred (elt1 elt2)
  (let* ((data1 (car elt1))
	 (data2 (car elt2))
	 (year1 (car data1))
	 (year2 (car data2))
	 (month1 (nth 1 data1))
	 (month2 (nth 1 data2))
	 (day1 (nth 2 data1))
	 (day2 (nth 2 data2))
	 (hour1 (nth 3 data1))
	 (hour2 (nth 3 data2))
	 (minutes1 (nth 4 data1))
	 (minutes2 (nth 4 data2)))
    (if year1
	(and year2
	     (or (> year1 year2)
		 (and (= year1 year2)
		      (or (> month1 month2)
			  (and (= month1 month2)
			       (> day1 day2))))))
      (if year2
	  t
	(or (> month1 month2)
	    (and (= month1 month2)
		 (or (> day1 day2)
		     (and (= day1 day2)
			  (or (> hour1 hour2)
			      (and (= hour1 hour2)
				   (> minutes1 minutes2)))))))))))

(defun efs-unix-t-converter (&optional regexp reverse)
  (if regexp
      nil
    (goto-char (point-min))
    (efs-save-match-data
      (if (re-search-forward efs-month-and-time-regexp nil t)
	  (let ((current-month (cdr (assoc (substring
					    (current-time-string) 4 7)
					   efs-month-alist)))
		list-start start end list year month day hour minutes)
	    (beginning-of-line)
	    (setq list-start (point))
	    (while (progn
		     (setq start (point))
		     (forward-line 1)
		     (setq end (point))
		     (goto-char start)
		     (re-search-forward efs-month-and-time-regexp end t))
	      ;; Need to measure wrto the current month
	      ;; There is a bug here if because of time-zone shifts, the
	      ;; local machine and the remote one are on different months.
	      (setq month (% (+ (- 11 current-month)
				(cdr (assoc
				      (buffer-substring (match-beginning 2)
							(match-end 2))
				      efs-month-alist))) 12)
		    day (string-to-int
			 (buffer-substring (match-beginning 3) (match-end 3)))
		    year (buffer-substring (match-beginning 4) (match-end 4)))
	      (if (string-match ":" year)
		  (setq hour (string-to-int (substring year 0
						       (match-beginning 0)))
			minutes (string-to-int (substring year (match-end 0)))
			year nil)
		(setq hour nil
		      minutes nil
		      year (string-to-int year)))
	      (setq list (cons
			  (cons
			   (list year month day hour minutes)
			   (buffer-substring start end))
			  list))
	      (goto-char end))
	    (setq list
		  (mapcar 'cdr
			  (sort list 'efs-unix-t-converter-sort-pred)))
	    (if reverse (setq list (nreverse list)))
	    (delete-region list-start (point))
	    (apply 'insert list)
	    t)))))

(efs-defun efs-t-converter nil (&optional regexp reverse)
  ;; Converts listing without the t-switch, to ones with it.
  nil) ; by default assume that we cannot work.

(efs-fset 'efs-t-converter 'unix 'efs-unix-t-converter)
(efs-fset 'efs-t-converter 'sysV-unix 'efs-unix-t-converter)
(efs-fset 'efs-t-converter 'apollo-unix 'efs-unix-t-converter)
(efs-fset 'efs-t-converter 'bsd-unix 'efs-unix-t-converter)
(efs-fset 'efs-t-converter 'dumb-unix 'efs-unix-t-converter)
(efs-fset 'efs-t-converter 'dumb-apollo-unix 'efs-unix-t-converter)
(efs-fset 'efs-t-converter 'super-dumb-unix 'efs-unix-t-converter)

(defun efs-rt-converter (listing-type &optional regexp)
  ;; Reverse time sorting
  (efs-t-converter listing-type regexp t))

(defun efs-unix-alpha-converter (&optional regexp reverse)
  (if regexp
      nil
    (goto-char (point-min))
    (efs-save-match-data
      (if (re-search-forward efs-month-and-time-regexp nil t)
	  (let (list list-start end start next)
	    (beginning-of-line)
	    (setq list-start (point))
	    (while (progn
		     (setq start (point))
		     (end-of-line)
		     (setq end (point)
			   next (1+ end))
		     (goto-char start)
		     (re-search-forward efs-month-and-time-regexp end t))
	      ;; Need to measure wrto the current month
	      ;; There is a bug here if because of time-zone shifts, the
	      ;; local machine and the remote one are on different months.
	      (setq list
		    (cons
		     (cons (buffer-substring (point) end)
			   (buffer-substring start next))
		     list))
	      (goto-char next))
	    (delete-region list-start (point))
	    (apply 'insert
		   (mapcar 'cdr
			   (sort list (if reverse
					  (function
					   (lambda (x y)
					     (string< (car y) (car x))))
					(function
					 (lambda (x y)
					   (string< (car x) (car y))))))))
	    t)))))

(efs-defun efs-alpha-converter nil (&optional regexp reverse)
  ;; Converts listing to lexigraphical order.
  nil) ; by default assume that we cannot work.

(efs-fset 'efs-alpha-converter 'unix 'efs-unix-alpha-converter)
(efs-fset 'efs-alpha-converter 'sysV-unix 'efs-unix-alpha-converter)
(efs-fset 'efs-alpha-converter 'apollo-unix 'efs-unix-alpha-converter)
(efs-fset 'efs-alpha-converter 'bsd-unix 'efs-unix-alpha-converter)
(efs-fset 'efs-alpha-converter 'dumb-unix 'efs-unix-alpha-converter)
(efs-fset 'efs-alpha-converter 'dumb-apollo-unix 'efs-unix-alpha-converter)
(efs-fset 'efs-alpha-converter 'super-dumb-unix 'efs-unix-alpha-converter)

(defun efs-ralpha-converter (listing-type &optional regexp)
  ;; Reverse alphabetic
  (efs-alpha-converter listing-type regexp t))

(defun efs-unix-S-converter (&optional regexp reverse)
  (if regexp
      nil
    (goto-char (point-min))
    (efs-save-match-data
      (if (re-search-forward efs-month-and-time-regexp nil t)
	  (let (list list-start start next)
	    (beginning-of-line)
	    (setq list-start (point))
	    (while (progn
		     (setq start (point))
		     (forward-line 1)
		     (setq next (point))
		     (goto-char start)
		     (re-search-forward efs-month-and-time-regexp next t))
	      ;; Need to measure wrto the current month
	      ;; There is a bug here if because of time-zone shifts, the
	      ;; local machine and the remote one are on different months.
	      (setq list
		    (cons
		     (cons (string-to-int
			    (buffer-substring (match-beginning 1)
					      (match-end 1)))
			   (buffer-substring start next))
		     list))
	      (goto-char next))
	    (delete-region list-start (point))
	    (apply 'insert
		   (mapcar 'cdr
			   (sort list (if reverse
					  (function
					   (lambda (x y)
					     (< (car x) (car y))))
					(function
					 (lambda (x y)
					   (> (car x) (car y))))))))
	    t)))))

(efs-defun efs-S-converter nil (&optional regexp reverse)
  ;; Converts listing without the S-switch, to ones with it.
  nil) ; by default assume that we cannot work.

(efs-fset 'efs-S-converter 'unix 'efs-unix-S-converter)
(efs-fset 'efs-S-converter 'sysV-unix 'efs-unix-S-converter)
(efs-fset 'efs-S-converter 'apollo-unix 'efs-unix-S-converter)
(efs-fset 'efs-S-converter 'bsd-unix 'efs-unix-S-converter)
(efs-fset 'efs-S-converter 'dumb-unix 'efs-unix-S-converter)
(efs-fset 'efs-S-converter 'dumb-apollo-unix 'efs-unix-S-converter)
(efs-fset 'efs-S-converter 'super-dumb-unix 'efs-unix-S-converter)

(defun efs-rS-converter (listing-type &optional regexp)
  ;; Reverse S switch.
  (efs-S-converter listing-type regexp t))

(defun efs-unix-X-converter (&optional regexp reverse)
  (if regexp
      nil
    (goto-char (point-min))
    (efs-save-match-data
      (if (re-search-forward efs-month-and-time-regexp nil t)
	  (let (next list list-start fnstart eol start end link-p)
	    (beginning-of-line)
	    (setq list-start (point))
	    (while (progn
		     (setq start (point))
		     (skip-chars-forward "0-9 ")
		     (setq link-p (= (char-after (point)) ?l))
		     (end-of-line)
		     (setq eol (point)
			   next (1+ eol))
		     (goto-char start)
		     (re-search-forward efs-month-and-time-regexp eol t))
	      ;; Need to measure wrto the current month
	      ;; There is a bug here if because of time-zone shifts, the
	      ;; local machine and the remote one are on different months.
	      (setq fnstart (point))
	      (or (and link-p (search-forward " -> " eol t)
		       (goto-char (match-beginning 0)))
		  (goto-char eol))
	      (setq end (point))
	      (skip-chars-backward "^." fnstart)
	      (setq list
		    (cons
		     (cons
		      (if (= (point) fnstart)
			  ""
			(buffer-substring (point) end))
		      (buffer-substring start next))
		     list))
	      (goto-char next))
	    (delete-region list-start (point))
	    (apply 'insert
		   (mapcar 'cdr
			   (sort list (if reverse
					  (function
					   (lambda (x y)
					     (string< (car y) (car x))))
					(function
					 (lambda (x y)
					   (string< (car x) (car y))))))))
	    t)))))

(efs-defun efs-X-converter nil (&optional regexp reverse)
  ;; Sort on file name extension.  By default do nothing
  nil)

(defun efs-rX-converter (listing-type &optional regexp)
  (efs-X-converter listing-type regexp t))

(efs-fset 'efs-X-converter 'unix 'efs-unix-X-converter)
(efs-fset 'efs-X-converter 'sysV-unix 'efs-unix-X-converter)
(efs-fset 'efs-X-converter 'apollo-unix 'efs-unix-X-converter)
(efs-fset 'efs-X-converter 'bsd-unix 'efs-unix-X-converter)
(efs-fset 'efs-X-converter 'dumb-unix 'efs-unix-X-converter)
(efs-fset 'efs-X-converter 'dumb-apollo-unix 'efs-unix-X-converter)
(efs-fset 'efs-X-converter 'super-dumb-unix 'efs-unix-X-converter)

;;; Brief listings

;;; The following functions do a heap better at packing than
;;; the usual ls listing.  A variable column width is used.
(defun efs-column-widths (columns list &optional across)
  ;; Returns the column widths for breaking LIST into
  ;; COLUMNS number of columns.
  (cond
   ((null list)
    nil)
   ((= columns 1)
    (list (apply 'max (mapcar 'length list))))
   ((let* ((len (length list))
	   (col-length (/ len columns))
	   (remainder (% len columns))
	   (i 0)
	   (j 0)
	   (max-width 0)
	   widths padding)
      (if (zerop remainder)
	  (setq padding 0)
	(setq col-length (1+ col-length)
	      padding (- columns remainder)))
      (setq list (nconc (copy-sequence list) (make-list padding nil)))
      (setcdr (nthcdr (1- (+ len padding)) list) list)
      (while (< i columns)
	(while (< j col-length)
	  (setq max-width (max max-width (length (car list)))
		list (if across (nthcdr columns list) (cdr list))
		j (1+ j)))
	(setq widths (cons (+ max-width 2) widths)
	      max-width 0
	      j 0
	      i (1+ i))
	(if across (setq list (cdr list))))
      (setcar widths (- (car widths) 2))
      (nreverse widths)))))
  
(defun efs-calculate-columns (list &optional across)
  ;; Returns a list of integers which are the column widths that best pack
  ;; LIST, a list of strings, onto the screen.
  (and list
       (let* ((width (1- (window-width)))
	      (columns (max 1 (/ width
				 (+ 2 (apply 'max (mapcar 'length list))))))
	      col-list last-col-list)
	 (while (<= (apply '+ (setq col-list
				    (efs-column-widths columns list across)))
		    width)
	   (setq columns (1+ columns)
		 last-col-list col-list))
	 (or last-col-list col-list))))

(defun efs-format-columns-of-files (files &optional across)
  ;; Returns the number of lines used.
  ;; If ACROSS is non-nil, sorts across rather than down the buffer, like
  ;; ls -x
  ;; A beefed up version of the function in dired. Thanks Sebastian.
  (and files
       (let* ((columns (efs-calculate-columns files across))
	      (ncols (length columns))
	      (ncols1 (1- ncols))
	      (nfiles (length files))
	      (nrows (+ (/ nfiles ncols)
			(if (zerop (% nfiles ncols)) 0 1)))
	      (space-left (- (window-width) (apply '+ columns) 1))
	      (stretch (/ space-left ncols1))
	      (float-stretch (if (zerop ncols1) 0 (% space-left ncols1)))
	      (i 0)
	      (j 0)
	      (result "")
	      file padding)
	 (setq files (nconc (copy-sequence files) ; fill up with empty fns
			    (make-list (- (* ncols nrows) nfiles) "")))
	 (setcdr (nthcdr (1- (length files)) files) files) ; make circular
	 (while (< j nrows)
	   (while (< i ncols)
	     (setq result (concat result (setq file (car files))))
	     (setq padding (- (nth i columns) (length file)))
	     (or (= i ncols1)
		 (progn
		   (setq padding (+ padding stretch))
		   (if (< i float-stretch) (setq padding (1+ padding)))))
	     (setq result (concat result (make-string padding ?\ )))
	     (setq files (if across (cdr files) (nthcdr nrows files))
		   i (1+ i)))
	   (setq result (concat result "\n"))
	   (setq i 0
		 j (1+ j))
	   (or across (setq files (cdr files))))
	 result)))

(defun efs-brief-converter (host-type file-table F a A p x C &optional regexp)
  ;; Builds a brief directory listing for file cache, with
  ;; possible switches F, a, A, p, x.
  (efs-save-match-data
   (let (list ent modes)
     (efs-map-hashtable
      (function
       (lambda (key val)
	 (if (and
	      (efs-really-file-p host-type key val)
	      (or a
		  (and A (not (or (string-equal "." key)
				  (string-equal ".." key))))
		  (/= (string-to-char key) ?.))
	      (or (null regexp)
		  (string-match regexp key)))
	     (setq ent (car val)
		   modes (nth 3 val)
		   list (cons
			 (cond ((null (or F p))
				key)
			       ((eq t ent)
			      (concat key "/"))
			       ((cond
				 ((null F)
				  key)
				 ((stringp ent)
				  (concat key "@"))
				 ((null modes)
				  key)
				 ((eq (string-to-char modes) ?s)
				  ;; a socket
				  (concat key "="))
				 ((or
				   (memq (elt modes 3) '(?x ?s ?t))
				   (memq (elt modes 6) '(?x ?s ?t))
				   (memq (elt modes 9) '(?x ?s ?t)))
				  (concat key "*"))
				 (t
				  key))))
			 list)))))
      file-table)
     (setq list (sort list 'string<))
     (if (or C x)
	 (efs-format-columns-of-files list x)
       (concat (mapconcat 'identity list "\n") "\n")))))

;;; Store converters.

;; The cheaters.
(efs-add-ls-converter "-al" nil (function
				 (lambda (listing-type &optional regexp)
				   (null regexp))))
(efs-add-ls-converter "-Al" nil (function
				 (lambda (listing-type &optional regexp)
				   (null regexp))))
(efs-add-ls-converter "-alF" nil (function
				  (lambda (listing-type &optional regexp)
				    (null regexp))))
(efs-add-ls-converter "-AlF" nil (function
				  (lambda (listing-type &optional regexp)
				    (null regexp))))

(efs-add-ls-converter "-alt" "-al" 'efs-t-converter)
(efs-add-ls-converter "-Alt" "-Al" 'efs-t-converter)
(efs-add-ls-converter "-lt" "-l" 'efs-t-converter)
(efs-add-ls-converter "-altF" "-alF" 'efs-t-converter)
(efs-add-ls-converter "-AltF" "-AlF" 'efs-t-converter)
(efs-add-ls-converter "-ltF" "-lF" 'efs-t-converter)
(efs-add-ls-converter "-alt" nil 'efs-t-converter)
(efs-add-ls-converter "-altF" nil 'efs-t-converter) 
(efs-add-ls-converter "-Alt" nil 'efs-t-converter)  ; cheating a bit
(efs-add-ls-converter "-AltF" nil 'efs-t-converter) ; cheating a bit

(efs-add-ls-converter "-altr" "-al" 'efs-rt-converter)
(efs-add-ls-converter "-Altr" "-Al" 'efs-rt-converter)
(efs-add-ls-converter "-ltr" "-l" 'efs-rt-converter)
(efs-add-ls-converter "-altFr" "-alF" 'efs-rt-converter)
(efs-add-ls-converter "-AltFr" "-AlF" 'efs-rt-converter)
(efs-add-ls-converter "-ltFr" "-lF" 'efs-rt-converter)
(efs-add-ls-converter "-altr" nil 'efs-rt-converter)
(efs-add-ls-converter "-Altr" nil 'efs-rt-converter)

(efs-add-ls-converter "-alr" "-alt" 'efs-alpha-converter)
(efs-add-ls-converter "-Alr" "-Alt" 'efs-alpha-converter)
(efs-add-ls-converter "-lr" "-lt" 'efs-alpha-converter)
(efs-add-ls-converter "-alFr" "-alFt" 'efs-alpha-converter)
(efs-add-ls-converter "-AlFr" "-AlFt" 'efs-alpha-converter)
(efs-add-ls-converter "-lFr" "-lFt" 'efs-alpha-converter)

(efs-add-ls-converter "-al" "-alt" 'efs-alpha-converter)
(efs-add-ls-converter "-Al" "-Alt" 'efs-alpha-converter)
(efs-add-ls-converter "-l" "-lt" 'efs-alpha-converter)
(efs-add-ls-converter "-alF" "-alFt" 'efs-alpha-converter)
(efs-add-ls-converter "-AlF" "-AlFt" 'efs-alpha-converter)
(efs-add-ls-converter "-lF" "-lFt" 'efs-alpha-converter)
(efs-add-ls-converter nil "-alt" 'efs-alpha-converter)

(efs-add-ls-converter "-alr" "-al" 'efs-ralpha-converter)
(efs-add-ls-converter "-Alr" "-Al" 'efs-ralpha-converter)
(efs-add-ls-converter "-lr" "-l" 'efs-ralpha-converter)
(efs-add-ls-converter "-alFr" "-alF" 'efs-ralpha-converter)
(efs-add-ls-converter "-lAFr" "-lAF" 'efs-ralpha-converter)
(efs-add-ls-converter "-lFr" "-lF" 'efs-ralpha-converter)
(efs-add-ls-converter "-alr" nil 'efs-ralpha-converter)

(efs-add-ls-converter "-alr" "-alt" 'efs-ralpha-converter)
(efs-add-ls-converter "-Alr" "-Alt" 'efs-ralpha-converter)
(efs-add-ls-converter "-lr" "-lt" 'efs-ralpha-converter)
(efs-add-ls-converter "-alFr" "-alFt" 'efs-ralpha-converter)
(efs-add-ls-converter "-lAFr" "-lAFt" 'efs-ralpha-converter)
(efs-add-ls-converter "-lFr" "-lFt" 'efs-ralpha-converter)

(efs-add-ls-converter "-alS" "-al" 'efs-S-converter)
(efs-add-ls-converter "-AlS" "-Al" 'efs-S-converter)
(efs-add-ls-converter "-lS" "-l" 'efs-S-converter)
(efs-add-ls-converter "-alSF" "-alF" 'efs-S-converter)
(efs-add-ls-converter "-AlSF" "-AlF" 'efs-S-converter)
(efs-add-ls-converter "-lSF" "-lF" 'efs-S-converter)
(efs-add-ls-converter "-alS" nil 'efs-S-converter)

(efs-add-ls-converter "-alSr" "-al" 'efs-rS-converter)
(efs-add-ls-converter "-AlSr" "-Al" 'efs-rS-converter)
(efs-add-ls-converter "-lSr" "-l" 'efs-rS-converter)
(efs-add-ls-converter "-alSFr" "-alF" 'efs-rS-converter)
(efs-add-ls-converter "-AlSFr" "-AlF" 'efs-rS-converter)
(efs-add-ls-converter "-lSFr" "-lF" 'efs-rS-converter)
(efs-add-ls-converter "-alSr" nil 'efs-rS-converter)

(efs-add-ls-converter "-alS" "-alt" 'efs-S-converter)
(efs-add-ls-converter "-AlS" "-Alt" 'efs-S-converter)
(efs-add-ls-converter "-lS" "-lt" 'efs-S-converter)
(efs-add-ls-converter "-alSF" "-alFt" 'efs-S-converter)
(efs-add-ls-converter "-AlSF" "-AlFt" 'efs-S-converter)
(efs-add-ls-converter "-lSF" "-lFt" 'efs-S-converter)

(efs-add-ls-converter "-alSr" "-alt" 'efs-rS-converter)
(efs-add-ls-converter "-AlSr" "-Alt" 'efs-rS-converter)
(efs-add-ls-converter "-lSr" "-lt" 'efs-rS-converter)
(efs-add-ls-converter "-alSFr" "-alFt" 'efs-rS-converter)
(efs-add-ls-converter "-AlSFr" "-AlFt" 'efs-rS-converter)
(efs-add-ls-converter "-lSFr" "-lFt" 'efs-rS-converter)

(efs-add-ls-converter "-AlX" nil 'efs-X-converter)
(efs-add-ls-converter "-alX" nil 'efs-X-converter)
(efs-add-ls-converter "-AlXr" nil 'efs-rX-converter)
(efs-add-ls-converter "-alXr" nil 'efs-rX-converter)

(efs-add-ls-converter "-alX" "-al" 'efs-X-converter)
(efs-add-ls-converter "-AlX" "-Al" 'efs-X-converter)
(efs-add-ls-converter "-lX" "-l" 'efs-X-converter)
(efs-add-ls-converter "-alXF" "-alF" 'efs-X-converter)
(efs-add-ls-converter "-AlXF" "-AlF" 'efs-X-converter)
(efs-add-ls-converter "-lXF" "-lF" 'efs-X-converter)

(efs-add-ls-converter "-alXr" "-al" 'efs-rX-converter)
(efs-add-ls-converter "-AlXr" "-Al" 'efs-rX-converter)
(efs-add-ls-converter "-lXr" "-l" 'efs-rX-converter)
(efs-add-ls-converter "-alXFr" "-alF" 'efs-rX-converter)
(efs-add-ls-converter "-AlXFr" "-AlF" 'efs-rX-converter)
(efs-add-ls-converter "-lXFr" "-lF" 'efs-rX-converter)

;;; Converters for efs-files-hashtable

(efs-add-ls-converter
 "" t (function
       (lambda (host-type files &optional regexp)
	 (efs-brief-converter host-type files
			      nil nil nil nil nil nil regexp))))
(efs-add-ls-converter
 "-C" t (function
	 (lambda (host-type files &optional regexp)
	   (efs-brief-converter host-type files
				nil nil nil nil nil t regexp))))
(efs-add-ls-converter
 "-F" t (function
	 (lambda (host-type files &optional regexp)
	   (efs-brief-converter host-type files
				t nil nil nil nil nil regexp))))
(efs-add-ls-converter
 "-p" t (function
	 (lambda (host-type files &optional regexp)
	   (efs-brief-converter host-type files
				nil nil nil t nil nil regexp))))
(efs-add-ls-converter
 "-CF" t (function
	  (lambda (host-type files &optional regexp)
	     (efs-brief-converter host-type files
				  t nil nil nil nil t regexp))))
(efs-add-ls-converter
 "-Cp" t (function
	  (lambda (host-type files &optional regexp)
	    (efs-brief-converter host-type files nil nil nil t nil t regexp))))
(efs-add-ls-converter
 "-x" t (function
	 (lambda (host-type files &optional regexp)
	   (efs-brief-converter host-type files
				nil nil nil nil t nil regexp))))
(efs-add-ls-converter
 "-xF" t (function
	  (lambda (host-type files &optional regexp)
	    (efs-brief-converter host-type files t nil nil nil t nil regexp))))
(efs-add-ls-converter
 "-xp" t (function
	  (lambda (host-type files &optional regexp)
	    (efs-brief-converter host-type files nil nil nil t t nil regexp))))
(efs-add-ls-converter
 "-Ca" t (function
	  (lambda (host-type files &optional regexp)
	    (efs-brief-converter host-type files nil t nil nil nil t regexp))))
(efs-add-ls-converter
 "-CFa" t (function
	   (lambda (host-type files &optional regexp)
	     (efs-brief-converter host-type files t t nil nil nil t regexp))))
(efs-add-ls-converter
 "-Cpa" t (function
	   (lambda (host-type files &optional regexp)
	     (efs-brief-converter host-type files nil t nil t nil t regexp))))
(efs-add-ls-converter
 "-xa" t (function
	  (lambda (host-type files &optional regexp)
	    (efs-brief-converter host-type files nil t nil nil t nil regexp))))
(efs-add-ls-converter
 "-xFa" t (function
	   (lambda (host-type files &optional regexp)
	     (efs-brief-converter host-type files t t nil nil t nil regexp))))
(efs-add-ls-converter
 "-xpa" t (function
	   (lambda (host-type files &optional regexp)
	     (efs-brief-converter host-type files nil t nil t t nil regexp))))
(efs-add-ls-converter
 "-CA" t (function
	  (lambda (host-type files &optional regexp)
	    (efs-brief-converter host-type files nil nil t nil nil t regexp))))
(efs-add-ls-converter
 "-CFA" t (function
	   (lambda (host-type files &optional regexp)
	     (efs-brief-converter host-type files t nil t nil nil t regexp))))
(efs-add-ls-converter
 "-CpA" t (function
	   (lambda (host-type files &optional regexp)
	     (efs-brief-converter host-type files nil nil t t nil t regexp))))
(efs-add-ls-converter
 "-xA" t (function
	  (lambda (host-type files &optional regexp)
	    (efs-brief-converter host-type files nil nil t nil t nil regexp))))
(efs-add-ls-converter
 "-xFA" t (function
	   (lambda (host-type files &optional regexp)
	     (efs-brief-converter host-type files t nil t nil t nil regexp))))
(efs-add-ls-converter
 "-xpA" t (function
	   (lambda (host-type files &optional regexp)
	     (efs-brief-converter host-type files nil nil t t t nil regexp))))

;;;; ------------------------------------------------------------
;;;; Directory Listing Parsers
;;;; ------------------------------------------------------------

(defconst efs-unix:dl-listing-regexp
  "^[^ \n\t]+\n? +\\([0-9]+\\|-\\|=\\) ")

;; Note to progammers:
;; Below are a series of macros and functions used for parsing unix
;; file listings. They are intended only to be used together, so be careful
;; about using them out of context.

(defmacro efs-ls-parse-file-line ()
  ;; Extract the filename, size, and permission string from the current
  ;; line of a dired-like listing. Assumes that the point is at
  ;; the beginning of the line, leaves it just before the size entry.
  ;; Returns a list (name size perm-string nlinks owner).
  ;; If there is no file on the line, returns nil.
  (` (let ((eol (save-excursion (end-of-line) (point)))
	   name size modes nlinks owner)
       (skip-chars-forward " 0-9" eol)
       (and
	(looking-at efs-modes-links-owner-regexp)
	(setq modes (buffer-substring (match-beginning 1)
				      (match-end 1))
	      nlinks (string-to-int (buffer-substring (match-beginning 2)
						      (match-end 2)))
	      owner (buffer-substring (match-beginning 3) (match-end 3)))
	(re-search-forward efs-month-and-time-regexp eol t)
	(setq name (buffer-substring (point) eol)
	      size (string-to-int (buffer-substring (match-beginning 1)
						    (match-end 1))))
	(list name size modes nlinks owner)))))
	      
(defun efs-relist-symlink (host user symlink path switches)
  ;; Does a re-list of a single symlink in efs-data-buffer-name-2,
  ;; HOST = remote host
  ;; USER = remote username
  ;; SYMLINK = symbolic link name as a remote fullpath
  ;; PATH = efs full path syntax for the dir. being listed
  ;; SWITCHES = ls switches to use for the re-list
  ;; Returns (symlink-name symlink-target), as given by the listing. Returns
  ;; nil if the listing fails.
  ;; Does NOT correct for any symlink marking.
  (let* ((temp (efs-make-tmp-name host nil))
	 (temp-file (car temp))
	 (default-major-mode 'fundamental-mode)
	 spot)
    (unwind-protect
	(and
	 (prog1
	     (null
	      (car
	       (efs-send-cmd host user
			     (list 'dir symlink (cdr temp) switches)
			     (format "Listing %s"
				     (efs-relativize-filename
				      (efs-replace-path-component
				       path symlink))))))
	   ;; Put the old message back.
	   (if (and efs-verbose
		    (not (and (boundp 'dired-in-query) dired-in-query)))
	       (message "Listing %s..."
			(efs-relativize-filename path))))
	 (save-excursion
	   (if (efs-ftp-path temp-file)
	       (efs-add-file-entry (efs-host-type efs-gateway-host)
				   temp-file nil nil nil))
	   (set-buffer (get-buffer-create efs-data-buffer-name-2))
	   (erase-buffer)
	   (if (or (file-readable-p temp-file)
		   (sleep-for efs-retry-time)
		   (file-readable-p temp-file))
	       (let (efs-verbose)
		 (insert-file-contents temp-file))
	     (efs-error host user
			(format
			 "list data file %s not readable" temp-file)))
	   (skip-chars-forward " 0-9")
	   (and
	    (eq (char-after (point)) ?l)
	    (re-search-forward efs-month-and-time-regexp nil t)
	    (setq spot (point))
	    (re-search-forward " -> " nil t)
	    (progn
	      (end-of-line)
	      (list
	       ;; We might get the full path in the listing.
	       (file-name-nondirectory
		(buffer-substring spot (match-beginning 0)))
	       (buffer-substring (match-end 0) (point)))))))
      (efs-del-tmp-name temp-file))))

(defun efs-ls-sysV-p (host user dir linkname path)
  ;; Returns t if the symlink is listed in sysV style. i.e. The
  ;; symlink name is marked with an @.
  ;; HOST = remote host name
  ;; USER = remote user name
  ;; DIR = directory being listed as a remote full path.
  ;; LINKNAME = relative name of symbolic link as derived from an ls -..F...
  ;;            this is assumed to end with an @
  ;; PATH = efs full path synatx for the directory
  (let ((link (car (efs-relist-symlink
		    host user
		    (concat dir (substring linkname 0 -1))
		    path "-lFd" ))))
    (and link (string-equal link linkname))))

(defun efs-ls-next-p (host user dir linkname target path)
  ;; Returns t is the symlink is marked in the NeXT style.
  ;; i.e. The symlink destination is marked with an @.
  ;; This assumes that the host-type has already been identified
  ;; as NOT sysV-unix, and that target ends in an "@".
  ;; HOST = remote host name
  ;; USER = remote user name
  ;; DIR = remote directory being listed, as a remore full path
  ;; LINKNAME = relative name of symbolic link
  ;;            Since we've eliminated sysV, it won't be marked with an @
  ;; TARGET = target of symbolic link, as derived from an ls -..F..
  ;; PATH = directory being listed in full efs path syntax.
  (let ((no-F-target (nth 1 (efs-relist-symlink
			     host user
			     (concat dir linkname)
			     path "-ld"))))
    (and no-F-target
	 (string-equal (concat no-F-target "@") target))))

;; This deals with the F switch. Should also do something about
;; unquoting names obtained with the SysV b switch and the GNU Q
;; switch. See Sebastian's dired-get-filename.

(defun efs-ls-parser (host-type host user dir path switches)
  ;; Meant to be called by efs-parse-listing.
  ;; Assumes that point is at the beginning of the first file line.
  ;; Assumes that SWITCHES has already been bound to nil for a dumb host.
  ;; HOST-TYPE is the remote host-type
  ;; HOST is the remote host name
  ;; USER is the remote user name
  ;; DIR is the remote directory as a full path
  ;; PATH is the directory in full efs syntax, and directory syntax.
  ;; SWITCHES is the ls listing switches
  (let ((tbl (efs-make-hashtable))
	(used-F (and switches (string-match "F" switches)))
	(old-tbl (efs-get-files-hashtable-entry path))
	file-type symlink directory file size modes nlinks owner)
    (while (setq file (efs-ls-parse-file-line))
      (setq size (nth 1 file)
	    modes (nth 2 file)
	    nlinks (nth 3 file)
	    owner (nth 4 file)
	    file (car file)
	    file-type (string-to-char modes)
	    directory (eq file-type ?d))
      (if (eq file-type ?l)
	  (if (string-match " -> " file)
	      (setq symlink (substring file (match-end 0))
		    file (substring file 0 (match-beginning 0)))
	    ;; Shouldn't happen
	    (setq symlink ""))
	(setq symlink nil))
      (if used-F
	  ;; The F-switch jungle
	  (let ((socket (eq file-type ?s))
		(fifo (eq file-type ?p))
		(executable
		 (and (not symlink) ; x bits don't mean a thing for symlinks
		      (or (memq (elt modes 3) '(?x ?s ?t))
			  (memq (elt modes 6) '(?x ?s ?t))
			  (memq (elt modes 9) '(?x ?s ?t))))))
	    ;; Deal with marking of directories, executables, and sockets.
	    (if (or (and executable (string-match "*$" file))
		    (and socket (string-match "=$" file))
		    (and fifo (string-match "|$" file)))
		(setq file (substring file 0 -1))
	      ;; Do the symlink dance.
	      (if symlink
		  (let ((fat-p (string-match "@$" file))
			(sat-p (string-match "@$" symlink)))
		    (cond
		     ;; Those that mark the file
		     ((and (memq host-type '(sysV-unix apollo-unix)) fat-p)
		      (setq file (substring file 0 -1)))
		     ;; Those that mark nothing
		     ((memq host-type '(bsd-unix dumb-unix)))
		     ;; Those that mark the target
		     ((and (eq host-type 'next-unix) sat-p)
		      (setq symlink (substring symlink 0 -1)))
		     ;; We don't know
		     ((eq host-type 'unix)
		      (if fat-p
			  (cond
			   ((efs-ls-sysV-p host user dir
					   file path)
			    (setq host-type 'sysV-unix
				  file (substring file 0 -1))
			    (efs-add-host 'sysV-unix host)
			    (efs-add-listing-type 'sysV-unix host user))
			   ((and sat-p
				 (efs-ls-next-p host user dir file symlink
						path))
			    (setq host-type 'next-unix
				  symlink (substring symlink 0 -1))
			    (efs-add-host 'next-unix host)
			    (efs-add-listing-type 'next-unix host user))
			   (t
			    (setq host-type 'bsd-unix)
			    (efs-add-host 'bsd-unix host)
			    (efs-add-listing-type 'bsd-unix host user)))
			(if (and sat-p
				 (efs-ls-next-p host user dir file
						symlink path))
			    (progn
			      (setq host-type 'next-unix
				    symlink (substring symlink 0 -1))
			      (efs-add-host 'next-unix host)
			      (efs-add-listing-type 'next-unix host user))
			  (setq host-type 'bsd-unix)
			  (efs-add-host 'bsd-unix host)
			  (efs-add-listing-type 'bsd-unix host user)))))
		    ;; Look out for marking of symlink
		    ;; If we really wanted to, at this point we
		    ;; could distinguish aix from hp-ux, ultrix, irix and a/ux,
		    ;; allowing us to skip the re-list in the future, for the
		    ;; later 4 host types. Another version...
		    (if (string-match "[=|*]$" symlink)
			(let ((relist (efs-relist-symlink
				       host user (concat dir file)
				       path "-dl")))
			  (if relist (setq symlink (nth 1 relist))))))))))
      ;; Strip / off the end unconditionally.  It's not a valid file character
      ;; anyway.
      (if (string-match "/$" file) (setq file (substring file 0 -1)))
      (let ((mdtm (and old-tbl (nth 5 (efs-get-hash-entry file old-tbl)))))
	(if mdtm
	    (efs-put-hash-entry file (list (or symlink directory) size owner
					   modes nlinks mdtm) tbl)
	  (efs-put-hash-entry file (list (or symlink directory) size owner
					 modes nlinks) tbl)))
      (forward-line 1))
    (efs-put-hash-entry "." '(t) tbl)
    (efs-put-hash-entry ".." '(t) tbl)
    tbl))

(efs-defun efs-parse-listing nil (host user dir path &optional switches)
  ;; Parse the a listing which is assumed to be from some type of unix host.
  ;; Note that efs-key will be bound to the actual host type.
  ;; HOST = remote host name
  ;; USER = remote user name
  ;; DIR = directory as a remote full path
  ;; PATH = directory in full efs path syntax
  ;; SWITCHES = ls switches used for the listing
  (efs-save-match-data
    (cond
     ;; look for total line
     ((looking-at "^total [0-9]+$")
      (forward-line 1)
      ;; Beware of machines that put a blank line after the totals line.
      (skip-chars-forward " \t\n")
      (efs-ls-parser efs-key host user dir path switches))
     ;; look for errors
     ((looking-at "[^\n]+\\( not found\\|: Not a directory\\)\n\\'")
      ;; It's an ls error message.
      nil)
     ((eobp) ; i.e. zerop buffer-size
      nil) ; assume an ls error message
     ;; look for listings without total lines
     ((re-search-forward efs-month-and-time-regexp nil t)
      (beginning-of-line)
      (efs-ls-parser efs-key host user dir path switches))
     (t nil))))

(efs-defun efs-parse-listing unix:unknown
  (host user dir path &optional switches)
  ;; Parse the a listing which is assumed to be from some type of unix host,
  ;; possibly one doing a dl listing.
  ;; HOST = remote host name
  ;; USER = remote user name
  ;; DIR = directory as a remote full path
  ;; PATH = directory in full efs path syntax
  ;; SWITCHES = ls switches used for the listing
 (efs-save-match-data
   (cond
    ;; look for total line
    ((looking-at "^total [0-9]+$")
     (forward-line 1)
     ;; Beware of machines that put a blank line after the totals line.
     (skip-chars-forward " \t\n")
     ;; This will make the listing-type track the host-type.
     (efs-add-listing-type nil host user)
     (efs-ls-parser 'unix host user dir path switches))
    ;; look for errors
    ((looking-at "[^\n]+\\( not found\\|: Not a directory\\)\n\\'")
     ;; It's an ls error message.
     nil)
    ((eobp) ; i.e. zerop buffer-size
     nil) ; assume an ls error message
    ;; look for listings without total lines
    ((and (re-search-forward efs-month-and-time-regexp nil t)
	  (progn
	    (beginning-of-line)
	    (looking-at efs-modes-links-owner-regexp)))
     (efs-add-listing-type nil host user)
     (efs-ls-parser 'unix host user dir path switches))
    ;; look for dumb listings
    ((re-search-forward
      (concat (regexp-quote switches)
	      " not found\\|\\(^ls: +illegal option -- \\)")
      (save-excursion (end-of-line) (point)) t)
     (if (eq (efs-host-type host) 'apollo-unix)
	 (progn
	   (efs-add-host 'dumb-apollo-unix host)
	   (efs-add-listing-type 'dumb-apollo-unix host user))
       (efs-add-host 'dumb-unix host)
       (efs-add-listing-type 'dumb-unix host user))
     (if (match-beginning 1)
	 ;; Need to try to list again.
	 (let ((efs-ls-uncache t))
	   (efs-ls
	    path nil (format "Relisting %s" (efs-relativize-filename path)) t)
	   (goto-char (point-min))
	   (efs-parse-listing nil host user dir path switches))
       (if (re-search-forward "^total [0-9]+$" nil t)
	   (progn
	     (beginning-of-line)
	     (delete-region (point-min) (point))
	     (forward-line 1)
	     (efs-ls-parser 'dumb-unix host user dir path switches)))))
    ;; Look for dl listings.
    ((re-search-forward  efs-unix:dl-listing-regexp nil t)
     (efs-add-host 'unix host)
     (efs-add-listing-type 'unix:dl host user)
     (efs-parse-listing 'unix:dl host user dir path switches))
    ;; don't know, return nil
    (t nil))))

(defun efs-ls-parse-1-liner (filename buffer &optional symlink)
  ;; Parse a 1-line listing for FILENAME in BUFFER, and update
  ;; the cached info for FILENAME.
  ;; Optional SYMLINK arg gives the expected target of a symlink.
  ;; Since one-line listings are usually used to update info for
  ;; newly created files, we usually know what sort of a file to expect.
  ;; Actually trying to parse out the symlink target could be impossible
  ;; for some types of switches.
  (efs-save-buffer-excursion
    (set-buffer buffer)
    (goto-char (point-min))
    (skip-chars-forward " 0-9")
    (efs-save-match-data
      (let (modes nlinks owner size)
      (and
       (looking-at efs-modes-links-owner-regexp)
       (setq modes (buffer-substring (match-beginning 1) (match-end 1))
	     nlinks (string-to-int (buffer-substring (match-beginning 2)
						     (match-end 2)))
	     owner (buffer-substring (match-beginning 3) (match-end 3)))
       (re-search-forward efs-month-and-time-regexp nil t)
       (setq size (string-to-int (buffer-substring (match-beginning 1)
						   (match-end 1))))
       (let* ((filename (directory-file-name filename))
	      (files (efs-get-files-hashtable-entry
		      (file-name-directory filename))))
	 (if files
	     (let* ((key (efs-get-file-part filename))
		    (ignore-case (memq (efs-host-type
					(car (efs-ftp-path filename)))
				       efs-case-insensitive-host-types))
		    (ent (efs-get-hash-entry key files ignore-case))
		    (mdtm (nth 5 ent))
		    type)
	       (if (= (string-to-char modes) ?l)
		   (setq type
			 (cond
			  ((stringp symlink)
			   symlink)
			  ((stringp (car ent))
			   (car ent))
			  (t ; something weird happened.
			   "")))
		 (if (= (string-to-char modes) ?d)
		     (setq type t)))
	       (efs-put-hash-entry
		key (list type size owner modes nlinks mdtm)
		files ignore-case)))))))))

(efs-defun efs-update-file-info nil (file buffer &optional symlink)
  "For FILE, update cache information from a single file listing in BUFFER."
  ;; By default, this does nothing.
  nil)

(efs-defun efs-update-file-info unix (file buffer &optional symlink)
  (efs-ls-parse-1-liner file buffer))
(efs-defun efs-update-file-info sysV-unix (file buffer &optional symlink)
  (efs-ls-parse-1-liner file buffer))
(efs-defun efs-update-file-info bsd-unix (file buffer &optional symlink)
  (efs-ls-parse-1-liner file buffer))
(efs-defun efs-update-file-info next-unix (file buffer &optional symlink)
  (efs-ls-parse-1-liner file buffer))
(efs-defun efs-update-file-info apollo-unix (file buffer &optional symlink)
  (efs-ls-parse-1-liner file buffer))
(efs-defun efs-update-file-info dumb-unix (file buffer &optional symlink)
  (efs-ls-parse-1-liner file buffer))
(efs-defun efs-update-file-info dumb-apollo-unix
  (file buffer &optional symlink)
  (efs-ls-parse-1-liner file buffer))
(efs-defun efs-update-file-info super-dumb-unix (file buffer &optional symlink)
  (efs-ls-parse-1-liner file buffer))

;;;; ----------------------------------------------------------------
;;;; The 'unknown listing parser. This does some host-type guessing.
;;;; ----------------------------------------------------------------

;;; Regexps for host and listing type guessing from the listing syntax.

(defconst efs-ka9q-listing-regexp
  (concat
   "^\\([0-9,.]+\\|No\\) files\\. [0-9,.]+ bytes free\\. "
   "Disk size [0-9,]+ bytes\\.$"))
;; This version of the regexp is really for hosts which allow some switches,
;; but not ours. Rather than determine which switches we could be using
;; we just assume that it's dumb.
(defconst efs-dumb-unix-listing-regexp
  (concat
   "^[Uu]sage: +ls +-[a-zA-Z0-9]+[ \n]\\|"
   "^Only ls flags accepted are\\|"
   ;; Unitree server
   "^Error getting stats for \"-[a-zA-Z0-9]+\""))

(defconst efs-dos-distinct-date-and-time-regexp
  (concat
   " \\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct"
   "\\|Nov\\|Dec\\) [ 0-3][0-9],[12][90][0-9][0-9]  "
   "[ 12][0-9]:[0-5][0-9]  "))

(defconst efs-dos:microsoft-file-line-regexp
  ;; matches all the way to the first char of the filename.
  (concat
   "[01][0-9]-[0-3][0-9]-[0-9][0-9] +[012][0-9]:[0-5][0-9][AP]M +"
   "\\(<DIR>\\|[0-9]+\\) +"))

;; Regexp to match the output from the hellsoft ftp server to an
;; ls -al. Unfortunately, this looks a lot like some unix ls error
;; messages.
(defconst efs-hell-listing-regexp
  (concat
   "ls: file or directory not found\n\\'\\|"
   "[-d]\\[[-A-Z][-A-Z][-A-Z][-A-Z][-A-Z][-A-Z][-A-Z]\\]"))

(efs-defun efs-parse-listing unknown
  (host user dir path &optional switches)
  "Parse the current buffer which is assumed to contain a dir listing.
Return a hashtable as the result. If the listing is not really a
directory listing, then return nil.

HOST is the remote host's name.
USER is the remote user name.
DIR is the directory as a full remote path.
PATH is the directory in full efs path synatx.
SWITCHES are the switches passed to ls. If SWITCHES is nil, then a
dumb \(with dir\) listing has been done."
  (efs-save-match-data
    (cond
     
     ;; look for total line
     ((looking-at "^total [0-9]+$")
      (efs-add-host 'unix host)
      (forward-line 1)
      ;; Beware of machines that put a blank line after the totals line.
      (skip-chars-forward " \t\n")
      (efs-ls-parser 'unix host user dir path switches))

     ;; Look for hellsoft. Need to do this before looking
     ;; for ls errors, since the hellsoft output looks a lot like an ls error.
     ((looking-at efs-hell-listing-regexp)
      (if (null (car (efs-send-cmd host user '(quote site dos))))
	  (let* ((key (concat host "/" user "/~"))
		 (tilde (efs-get-hash-entry
			 key efs-expand-dir-hashtable)))
	    (efs-add-host 'hell host)
	    ;; downcase the expansion of ~
	    (if (and tilde (string-match "^[^a-z]+$" tilde))
		(efs-put-hash-entry key (downcase tilde)
				    efs-expand-dir-hashtable))
	    ;; Downcase dir, in case its got some upper case stuff in it.
	    (setq dir (downcase dir)
		  path (efs-replace-path-component path dir))
	    (let ((efs-ls-uncache t))
	      ;; This will force the data buffer to be re-filled
	      (efs-ls path nil (format "Relisting %s"
				       (efs-relativize-filename path))
		      t))
	    (efs-parse-listing 'hell host user dir path))
	;; Don't know, give unix a try.
	(efs-add-host 'unix host)
	nil))
     
     ;; look for ls errors
     ((looking-at "[^\n]+\\( not found\\|: Not a directory\\)\n\\'")
      ;; It's an ls error message.
      (efs-add-host 'unix host)
      nil)
     
     ((eobp) ; i.e. (zerop (buffer-size))
      ;; This could be one of:
      ;; (1) An Ultrix ls error message
      ;; (2) A listing with the A switch of an empty directory
      ;;     on a machine which doesn't give a total line.
      ;; (3) The result of an attempt at an nlist. (This would mean a
      ;;     dumb host.)
      ;; (4) The twilight zone.
      (cond
       ((save-excursion
	  (set-buffer (process-buffer
		       (efs-get-process host user)))
	  (save-excursion
	    (goto-char (point-max))
	    (and
	     ;; The dir ftp output starts with a 200 cmd.
	     (re-search-backward "^150 " nil t)
	     ;; We never do an nlist (it's a short listing).
	     ;; If the machine thinks that we did, it's dumb.
	     (looking-at "[^\n]+ NLST "))))
	;; It's dumb-unix or ka9q. Anything else?
	;; This will re-fill the data buffer with a dumb listing.
	(let ((efs-ls-uncache t))
	  (efs-ls path nil (format "Relisting %s"
				   (efs-relativize-filename path))
		  t))
	(cond
	 ;; check for dumb-unix
	 ((re-search-forward efs-month-and-time-regexp nil t)
	  (efs-add-host 'dumb-unix host)
	  (beginning-of-line)
	  (efs-parse-listing 'dumb-unix host user dir path))
	 ;; check for ka9q
	 ((save-excursion
	    (goto-char (point-max))
	    (forward-line -1)
	    (looking-at efs-ka9q-listing-regexp))
	  (efs-add-host 'ka9q host)
	  (efs-parse-listing 'ka9q host user dir path))
	 (t ; Don't know, try unix.
	  (efs-add-host 'unix host)
	  nil)))
       ;; check for Novell Netware
       ((null (car (efs-send-cmd host user '(quote site nfs))))
	(efs-add-host 'netware host)
	(let ((efs-ls-uncache t))
	  (efs-ls path nil (format "Relisting %s"
				   (efs-relativize-filename path))
		  t))
	(efs-parse-listing 'netware host user dir path))
       (t
	;; Assume (1), an Ultrix error message.
	(efs-add-host 'unix host)
	nil)))
     
     ;; unix without a total line
     ((re-search-forward efs-month-and-time-regexp nil t)
      (efs-add-host 'unix host)
      (beginning-of-line)
      (efs-ls-parser 'unix host user dir path switches))
     
     ;; Now we look for host-types, or listing-types which are auto-rec
     ;; by the listing parser, because it's not possible to pick them out
     ;; from a pwd.
     
     ;; check for dumb-unix
     ;; (Guessing of dumb-unix hosts which return an ftp error message is
     ;; done in efs-ls.)
     ((re-search-forward efs-dumb-unix-listing-regexp nil t)
      (efs-add-host 'dumb-unix host)
      ;; This will force the data buffer to be re-filled
      (let ((efs-ls-uncache t))
	(efs-ls path nil (format "Relisting %s"
				 (efs-relativize-filename path))
		t))
      (efs-parse-listing 'dumb-unix host user dir path))

     ;; check for Microsoft NT's ftp server
     ((re-search-forward efs-dos:microsoft-file-line-regexp nil t)
      (efs-add-host 'dos host)
      (efs-parse-listing 'dos host user dir path))
     
     ;; check for Distinct's DOS ftp server
     ((re-search-forward efs-dos-distinct-date-and-time-regexp nil t)
      (efs-add-host 'dos-distinct host)
      (efs-parse-listing 'dos-distinct host user dir path))

     ;; check for KA9Q pseudo-unix (LINUX?)
     ((save-excursion
	(goto-char (point-max))
	(forward-line -1)
	(looking-at efs-ka9q-listing-regexp))
      (efs-add-host 'ka9q host)
      ;; This will re-fill the data buffer.
      ;; Need to do this because ka9q is a dumb host.
      (let ((efs-ls-uncache t))
	(efs-ls path nil (format "Relisting %s"
				 (efs-relativize-filename path))
		t))
      (efs-parse-listing 'ka9q host user dir path))
     
     ;; Check for a unix descriptive (dl) listing
     ;; Do this last, because it's hard to guess.
     ((re-search-forward  efs-unix:dl-listing-regexp nil t)
      (efs-add-host 'unix host)
      (efs-add-listing-type 'unix:dl host user)
      (efs-parse-listing 'unix:dl host user dir path switches))

     ;; Don't know what's going on. Return nil, and assume unix.
     (t
      (efs-add-host 'unix host)
      nil))))

;;;; ------------------------------------------------------------
;;;; Directory information hashtable.
;;;; ------------------------------------------------------------

(efs-defun efs-really-file-p nil (file ent)
  ;; efs-files-hashtable sometimes contains fictitious entries, when
  ;; some OS's allow a file to be accessed by another name. For example,
  ;; in VMS the highest version of a file may be accessed by omitting the
  ;; the file version number. This function should return t if the
  ;; filename FILE is really a file. ENT is the hash entry of the file.
  t)

(efs-defun efs-add-file-entry nil (path type size owner
					&optional modes nlinks mdtm)
  ;; Add a new file entry for PATH
  ;; TYPE is nil for a plain file, t for a directory, and a string
  ;;   (the target of the link) for a symlink.
  ;; SIZE is the size of the file in bytes.
  ;; OWNER is the owner of the file, as a string.
  ;; MODES is the file modes, as a string.  In Unix, this will be 10 cars.
  ;; NLINKS is the number of links for the file.
  ;; MDTM is the last modtime obtained for the file.  This is for
  ;;   short-term cache only, as emacs often has sequences of functions
  ;;   doing modtime lookup.  If you really want to be sure of the modtime,
  ;;   use efs-get-file-mdtm, which asks the remote server.
  
  (and (eq type t)
       (setq path (directory-file-name path)))
  (let ((files (efs-get-files-hashtable-entry (file-name-directory path))))
    (if files
	(efs-put-hash-entry
	 (efs-get-file-part path)
	 (cond (mdtm
		(list type size owner modes nlinks
		      mdtm))
	       (nlinks
		(list type size owner modes nlinks))
	       (modes (list type size owner modes))
	       (t (list type size owner)))
	 files
	 (memq efs-key efs-case-insensitive-host-types)))
    (efs-del-from-ls-cache path t nil)))

(efs-defun efs-delete-file-entry nil (path &optional dir-p)
  "Delete the file entry for PATH, if its directory info exists."
  (if dir-p
      (progn 
	(setq path (file-name-as-directory path))
	(efs-del-hash-entry (efs-canonize-file-name path)
			    efs-files-hashtable)
	;; Note that file-name-as-directory followed by
	;; (substring path 0 -1) 
	;; serves to canonicalize directory file names to their unix form.
	;; i.e. in VMS, FOO.DIR -> FOO/ -> FOO
	;; PATH is supposed to be s fully expanded efs-style path.
	(setq path (substring path 0 -1))))
  (let ((files (efs-get-files-hashtable-entry (file-name-directory path))))
    (if files
	(efs-del-hash-entry
	 (efs-get-file-part path)
	 files
	 (memq (efs-host-type (car (efs-ftp-path path)))
	       efs-case-insensitive-host-types))))
  (efs-del-from-ls-cache path t nil)
  (if dir-p (efs-del-from-ls-cache path nil t)))

(defun efs-set-files (directory files)
  "For DIRECTORY, set or change the associated FILES hashtable."
  (if files
      (efs-put-hash-entry
       (efs-canonize-file-name (file-name-as-directory directory))
       files efs-files-hashtable)))

(defun efs-parsable-switches-p (switches &optional full-dir)
  ;; Returns non-nil if SWITCHES would give an ls listing suitable for parsing
  ;; If FULL-DIR is non-nil, the switches must be suitable for parsing a full
  ;; ditectory.
  (or (null switches)
      (efs-save-match-data
	(and (string-match "[aA]" switches)
	     ;; g is not good enough, need l or o for owner.
	     (string-match "[lo]" switches)
	     ;; L shows link target, rather than link. We need both.
	     (not (string-match "[RfL]" switches))
	     (not (and full-dir (string-match "d" switches)))))))

(defun efs-get-files (directory &optional no-error)
  "For DIRECTORY, return a hashtable of file entries.
This will give an error or return nil, depending on the value of
NO-ERROR, if a listing for DIRECTORY cannot be obtained."
  (let ((directory (file-name-as-directory directory)))
    (or (efs-get-files-hashtable-entry directory)
	(and (efs-ls directory (efs-ls-guess-switches) t 'parse no-error)
	     (efs-get-files-hashtable-entry directory)))))

(efs-defun efs-allow-child-lookup nil (host user dir file)
  ;; Returns non-nil if in directory DIR,  FILE could possibly be a subdir
  ;; according to its file-name syntax, and therefore a child listing should
  ;; be attempted. Note that DIR is in directory syntax.
  ;; i.e. /foo/bar/, not /foo/bar.
  ;; Deal with dired. Anything else?
  (not (and (boundp 'dired-local-variables-file)
	    (stringp dired-local-variables-file)
	    (string-equal dired-local-variables-file file))))

(defmacro efs-ancestral-check (host-type path ignore-case)
  ;; Checks to see if something in a path's ancient parentage
  ;; would make it impossible for the path to exist in the directory
  ;; tree. In this case it returns nil. Otherwise returns t (there
  ;; is essentially no information returned in this case, the file
  ;; may exist or not).
  ;; This macro should make working with RCS more efficient.
  ;; It also helps with FTP servers that go into fits if we ask to
  ;; list a non-existent dir.
  ;; Yes, I know that the function mapped over the hashtable can
  ;; be written more cleanly with a concat, but this is faster.
  ;; concat's cause a lot of consing. So do regexp-quote's, but we can't
  ;; avoid it.
  ;; Probably doesn't make much sense for this to be an efs-defun, since
  ;; the host-type dependence is very mild.
  (`
   (let ((path (, path)) ; expand once
	 (ignore-case (, ignore-case))
	 str)
     ;; eliminate flat file systems -- should have a constant for this
     (or (memq (, host-type) '(mts cms mvs cms-knet))
	 (efs-save-match-data
	   (catch 'foo
	     (efs-map-hashtable
	      (function
	       (lambda (key val)
		 (and (eq (string-match (regexp-quote key) path) 0)
		      (setq str (substring path (match-end 0)))
		      (string-match "^[^/]+" str)
		      (not (efs-hash-entry-exists-p
			    (substring str 0 (match-end 0))
			    val ignore-case))
		      (throw 'foo nil))))
	      efs-files-hashtable)
	     t))))))

(defun efs-file-entry-p (path)
  ;; Return whether there is a file entry for PATH.
  ;; Under no circumstances does this cause FTP activity.
  (let* ((path (directory-file-name (efs-canonize-file-name path)))
	 (dir (file-name-directory path))
	 (file (efs-get-file-part path))
	 (tbl (efs-get-files-hashtable-entry dir)))
    (and tbl (efs-hash-entry-exists-p
	      file tbl
	      (memq (efs-host-type (car (efs-ftp-path dir)))
		    efs-case-insensitive-host-types)) t)))

(defun efs-get-file-entry (path)
  "Return the given file entry for PATH.
This is a list of the form \(type size owner modes nlinks modtm\), 
where type is nil for a normal file, t for a directory, and a string for a
symlink, size is the size of the file in bytes, if known, and modes are 
the permission modes of the file as a string. modtm is short-term the
cache of the file modtime.  It is not used by `verify-visited-file-modtime'.
If the file isn't in the hashtable, this returns nil."
  (let* ((path (directory-file-name (efs-canonize-file-name path)))
	 (dir (file-name-directory path))
	 (file (efs-get-file-part path))
	 (parsed (efs-ftp-path dir))
	 (host (car parsed))
	 (host-type (efs-host-type host))
	 (ent (efs-get-files-hashtable-entry dir))
	 (ignore-case (memq host-type efs-case-insensitive-host-types)))
    (if ent
	(efs-get-hash-entry file ent ignore-case)
      (let ((user (nth 1 parsed))
	    (r-dir (nth 2 parsed)))
	(and (efs-ancestral-check host-type path ignore-case)
	     (or (and efs-allow-child-lookup
		      (efs-allow-child-lookup host-type
					      host user r-dir file)
		      (setq ent (efs-get-files path t))
		      (efs-get-hash-entry "." ent))
		 ;; i.e. it's a directory by child lookup
		 (efs-get-hash-entry
		  file (efs-get-files dir) ignore-case)))))))

(defun efs-wipe-file-entries (host user)
  "Remove cache data for all files on HOST and USER.
This replaces the file entry information hashtable with one that
doesn't have any entries for the given HOST, USER pair."
  (let ((new-tbl (efs-make-hashtable (length efs-files-hashtable)))
	(host (downcase host))
	(case-fold (memq (efs-host-type host)
			 efs-case-insensitive-host-types)))
    (if case-fold (setq user (downcase user)))
    (efs-map-hashtable
     (function
      (lambda (key val)
	(let ((parsed (efs-ftp-path key)))
	  (if parsed
	      (let ((h (nth 0 parsed))
		    (u (nth 1 parsed)))
		(or (and (string-equal host (downcase h))
			 (string-equal user (if case-fold (downcase u) u)))
		    (efs-put-hash-entry key val new-tbl)))))))
     efs-files-hashtable)
    (setq efs-files-hashtable new-tbl)))


;;;; ============================================================
;;;; >8
;;;; Redefinitions of standard GNU Emacs functions.
;;;; ============================================================

;;;; ------------------------------------------------------------
;;;; expand-file-name and friends...
;;;; ------------------------------------------------------------

;; New filename expansion code for efs.
;; The overall structure is based around the following internal
;; functions and macros. Since these are internal, they do NOT
;; call efs-save-match-data. This is done by their calling
;; function.
;; 
;; efs-expand-tilde
;;   - expands all ~ constructs, both local and remote.
;; efs-short-circuit-file-name
;;   - short-circuits //'s and /~'s, for both local and remote paths.
;; efs-de-dot-file-name
;;   - canonizes /../ and /./'s in both local and remote paths.
;;
;; The following two functions overload existing emacs functions.
;; They are the entry points to this filename expansion code, and as such
;; call efs-save-match-data.
;; 
;; efs-expand-file-name
;; efs-substitute-in-file-name

;;; utility macros

(defmacro efs-short-circuit-file-name (filename)
  ;; Short-circuits //'s and /~'s in filenames.
  ;; Returns a list consisting of the local path,
  ;; host-type, host, user. For local hosts,
  ;; host-type, host, and user are all nil.
  (`
   (let ((start 0)
	 (string (, filename))
	 backskip regexp lbackskip
	 lregexp parsed host-type host user)

     (if efs-local-apollo-unix
	 (setq lregexp ".//+"
	       lbackskip 2)
       (setq lregexp "//+"
	     lbackskip 1))
     
     ;; Short circuit /user@mach: roots. It is important to do this
     ;; now to avoid unnecessary ftp connections.
     
     (if efs-short-circuit
	 (while (string-match efs-path-root-short-circuit-regexp string start)
	   (setq start (1+ (match-beginning 0)))))
     (or (zerop start) (setq string (substring string start)
			     start 0))
     
     ;; identify remote root
     
     (if (setq parsed (efs-ftp-path-macro string))
	 (if (memq (setq string (nth 2 parsed)
		       host-type
		       (efs-host-type (setq host (car parsed))
				      (setq user (nth 1 parsed))))
		   '(apollo-unix dumb-apollo-unix))
	     (setq regexp ".//+"
		   backskip 2)
	   (setq regexp "//+"
		 backskip 1))
       (setq regexp lregexp
	     backskip lbackskip))
     
     ;; Now short-circuit in an apollo and efs sensitive way.
     
     (if efs-short-circuit
	 (while (cond ((string-match regexp string start)
		       (setq start (- (match-end 0) backskip)))
		      ((string-match  "/~" string start)
		       (setq start (1- (match-end 0)))))
       
	   (and host-type
		(null efs-short-circuit-to-remote-root)
		(setq host-type nil
		      regexp lregexp
		      backskip lbackskip))))
     (or (zerop start) (setq string (substring string start)))
     (list string host-type (and host-type host) (and host-type user)))))

(defmacro efs-expand-tilde (tilde host-type host user)
  ;; Expands a TILDE (~ or ~sandy type construction)
  ;; Takes as an arg a filename (not directory name!)
  ;; and returns a filename. HOST-TYPE is the type of remote host.
  ;; nil is the type of the local host.
  (`
   (if (, host-type) ; nil host-type is the local machine
       (let* ((host (downcase (, host)))
	      (host-type (, host-type))
	      (ignore-case (memq host-type
				 efs-case-insensitive-host-types))
	      (tilde (, tilde))
	      (user (, user))
	      (key (concat host "/" user "/" tilde))
	      (res (efs-get-hash-entry
		    key efs-expand-dir-hashtable ignore-case)))
	 (or res
	     ;; for real accounts on unix systems, use the get trick
	     (and (not (efs-anonymous-p user))
		  (memq host-type efs-unix-host-types)
		  (let ((line (nth 1 (efs-send-cmd
				      host user
				      (list 'get tilde "/dev/null")
				      (format "expanding %s" tilde)))))
		    (setq res
			  (and (string-match efs-expand-dir-msgs line)
			       (substring line 
					  (match-beginning 1)
					  (match-end 1))))
		    (if res
			(progn
			  (setq res (efs-internal-directory-file-name res))
			  (efs-put-hash-entry
			   key res efs-expand-dir-hashtable ignore-case)
			  res))))
	     (progn
	       (setq res
		     (if (string-equal tilde "~")
			 (car (efs-send-pwd
			       host-type host user))
		       (let* ((home-key (concat host "/" user "/~"))
			      (home (efs-get-hash-entry
				     home-key efs-expand-dir-hashtable
				     ignore-case))
			      pwd-result)
			 (if home
			     (setq home
				   (efs-fix-path
				    host-type
				    (efs-internal-file-name-as-directory
				     host-type home)))
			   (if (setq home
				     (car
				      (setq pwd-result
					    (efs-send-pwd
					     host-type
					     host user))))
			       (efs-put-hash-entry
				home-key
				(efs-internal-directory-file-name
				 (efs-fix-path host-type home 'reverse))
				efs-expand-dir-hashtable ignore-case)
			     (efs-error host user
					     (concat "PWD failed: "
						     (cdr pwd-result)))))
			 (unwind-protect
			     (and (efs-raw-send-cd host user
					       (efs-fix-path
						host-type tilde) t)
				  (car
				   (efs-send-pwd
				    host-type host user)))
			   (efs-raw-send-cd host user home)))))
	       (if res
		   (progn
		     (setq res (efs-internal-directory-file-name
				(efs-fix-path host-type res 'reverse)))
		     (efs-put-hash-entry
		      key res efs-expand-dir-hashtable ignore-case)
		     res)))
	     (if (string-equal tilde "~")
		 (error "Cannot get home directory on %s" host)
	       (error "User %s is not known on %s" (substring tilde 1) host))))
     ;; local machine
     (if (boundp 'efs-real-expand-file-name)
	 (efs-real-expand-file-name (, tilde))
       (let ((inhibit-file-name-handlers
	     (cons 'efs-file-handler-function
		   (and (eq inhibit-file-name-operation 'expand-file-name)
			inhibit-file-name-handlers)))
	    (inhibit-file-name-operation 'expand-file-name))
	 (expand-file-name (, tilde)))))))

(defmacro efs-de-dot-file-name (string)
  ;; Takes a string as arguments, and removes /../'s and /./'s.
  (`
   (let ((string (, string))
	 (start 0)
	 new make-dir)
     ;; to make the regexp's simpler, canonicalize to directory name.
     (if (setq make-dir (string-match "/\\.\\.?$" string))
	 (setq string (concat string "/")))
     (while (string-match "/\\./" string start)
       (setq new (concat new
			 (substring string
				    start (match-beginning 0)))
	     start (1- (match-end 0))))
     
     (if new (setq string (concat new (substring string start))))
     
     (while (string-match "/[^/]+/\\.\\./" string)
       ;; Is there a way to avoid all this concating and copying?
       (setq string (concat (substring string 0 (1+ (match-beginning 0)))
			    (substring string (match-end 0)))))
     
     ;; Do /../ and //../ special cases. They should expand to
     ;; / and //, respectively.
     (if (string-match "^\\(/+\\)\\.\\./" string)
	 (setq string (concat (substring string 0 (match-end 1))
			      (substring string (match-end 0)))))
     
     (if (and make-dir
	      (not (string-match "^/+$" string)))
	 (substring string 0 -1)
       string))))

(defun efs-substitute-in-file-name (string)
  "Documented as original."
  ;; Because of the complicated interaction between short-circuiting
  ;; and environment variable substitution, this can't call the macro
  ;; efs-short-circuit-file-name.
  (efs-save-match-data
    (let ((start 0)
	  var new root backskip regexp lbackskip
	  lregexp parsed fudge-host-type rstart error)
     
      (if efs-local-apollo-unix
	  (setq lregexp ".//+"
		lbackskip 2)
	(setq lregexp "//+"
	      lbackskip 1))
      
      ;; Subst. existing env variables
      (while (string-match "\\$" string start)
	(setq new (concat new (substring string start (match-beginning 0)))
	      start (match-end 0))
	(cond ((eq (string-match "\\$" string start) start)
	       (setq start (1+ start)
		     new (concat new "$$")))
	      ((eq (string-match "{" string start) start)
	       (if (and (string-match "}" string start)
			(setq var (getenv
				   (substring string (1+ start)
					      (1- (match-end 0))))))
		   (setq start (match-end 0)
			 new (concat new var))
		 (setq new (concat new "$"))))
	      ((eq (string-match "[a-zA-Z0-9]+" string start) start)
	       (if (setq var (getenv
			      (substring string start (match-end 0))))
		   (setq start (match-end 0)
			 new (concat new var))
		 (setq new (concat new "$"))))
	      ((setq new (concat new "$")))))
      (if new (setq string (concat new (substring string start))
		    start 0))

      ;; Short circuit /user@mach: roots. It is important to do this
      ;; now to avoid unnecessary ftp connections.
      
      (if efs-short-circuit
	  (while (string-match efs-path-root-short-circuit-regexp
			       string start)
	    (setq start (1+ (match-beginning 0)))))
      (or (zerop start) (setq string (substring string start)
			      start 0))

      ;; Look for invalid environment variables in the root. If one is found,
      ;; we set the host-type to 'unix. Since we can't login in to determine
      ;; it. There is a good chance that we will bomb later with an error,
      ;; but the day may yet be saved if the root is short-circuited off.

      (if (string-match efs-path-root-regexp string)
	  (progn
	    (setq root (substring string 0 (match-end 0))
		  start (match-end 0))
	    (if (string-match "[^$]\\(\\$\\$\\)*\\$[^$]" root)
		(progn
		  (setq rstart (1- (match-end 0))
			fudge-host-type t)
		  (cond
		   ((eq (elt root rstart) ?{)
		    (setq
		     error
		     (if (string-match "}" root rstart)
			 (concat
			  "Subsituting non-existent environment variable "
			  (substring root (1+ rstart) (match-beginning 0)))
		       "Missing \"}\" in environment-variable substitution")))
		   ((eq (string-match "[A-Za-z0-9]+" root rstart) rstart)
		    (setq
		     error
		     (concat
		      "Subsituting non-existent environment variable "
		      (substring root rstart (match-beginning 0)))))
		   (t
		    (setq
		     error
		     "Bad format environment-variable substitution")))))
	    (setq root (efs-unquote-dollars root)
		  parsed (efs-ftp-path root))

	    (if (and (not fudge-host-type)
		     ;; This may trigger an FTP connection
		     (memq (efs-host-type (car parsed) (nth 1 parsed))
			   '(apollo-unix dumb-apollo-unix)))
		(setq regexp ".//+"
		      backskip 2)
	      (setq regexp "//+"
		    backskip 1)))
	;; no root, we're local
	(setq regexp lregexp
	      backskip lbackskip))
     
      ;; Now short-circuit in an apollo and efs sensitive way.
      
      (if efs-short-circuit
	  (while (cond ((string-match regexp string start)
			(setq start (- (match-end 0) backskip)))
		       ((string-match  "/~" string start)
			(setq start (1- (match-end 0)))))
	    
	    (and root
		 (null efs-short-circuit-to-remote-root)
		 (setq root nil
		       regexp lregexp
		       backskip lbackskip))))
      
      ;; If we still have a bad root, barf.
      (if (and root error) (error error))

      ;; look for non-existent evironment variables in the path
      
      (if (string-match
	   "\\([^$]\\|^\\)\\(\\$\\$\\)*\\$\\([^$]\\|$\\)" string start)
	  (progn
	    (setq start (match-beginning 3))
	    (cond
	     ((eq (length string) start)
	      (error "Empty string is an invalid environment variable"))
	     ((eq (elt string start) ?{)
	      (if (string-match "}" string start)
		  (error
		   "Subsituting non-existent environment variable %s"
		   (substring string (1+ start) (match-end 0)))
		(error
		 "Missing \"}\" in environment-variable substitution")))
	     ((eq (string-match "[A-Za-z0-9]+" string start) start)
	      (error
	       "Subsituting non-existent environment variable %s"
	       (substring string start (match-end 0))))
	     (t
	      (error
	       "Bad format environment-variable substitution")))))

      (if root
	  (concat root
		  (efs-unquote-dollars
		   (if (zerop start)
		       string
		     (substring string start))))
	(efs-unquote-dollars
	 (if (zerop start)
	     string
	   (substring string start)))))))
	      
(defun efs-expand-file-name (name &optional default)
  "Documented as original."
  (let (s-c-res path host user host-type)
    (efs-save-match-data
      (or (file-name-absolute-p name)
	  (setq name (concat
		      (file-name-as-directory
		       (or default default-directory))
		      name)))
      (setq s-c-res (efs-short-circuit-file-name name)
	    path (car s-c-res)
	    host-type (nth 1 s-c-res)
	    host (nth 2 s-c-res)
	    user (nth 3 s-c-res))
      (cond ((string-match "^~[^/]*" path)
	     (let ((start (match-end 0)))
	       (setq path (concat
			   (efs-expand-tilde
			    (substring path 0 start)
			    host-type host user)
			   (substring path start)))))
	    ((and host-type (not (file-name-absolute-p path)))
	     ;; We expand the empty string to a directory.
	     ;; This can be more efficient for filename
	     ;; completion. It's also consistent with non-unix.
	     (let ((tilde (efs-expand-tilde
			   "~" host-type host user)))
	       (if (string-equal tilde "/")
		   (setq path (concat "/" path))
		 (setq path (concat tilde "/" path))))))
      
      (setq  path (efs-de-dot-file-name path))
      (if host-type
	  (format efs-path-format-string user host path)
	path))))

;;;; ------------------------------------------------------------
;;;; Other functions for manipulating file names.
;;;; ------------------------------------------------------------

(defun efs-internal-file-name-extension (filename)
  ;; Returns the extension for file name FN.
  (save-match-data
    (let ((file (file-name-sans-versions (file-name-nondirectory filename))))
      (if (string-match "\\.[^.]*\\'" file)
	  (substring file (match-beginning 0))
	""))))

(defun efs-file-name-as-directory (name)
  ;; version of file-name-as-directory for remote files.
  ;; Usually just appends a / if there isn't one already.
  ;; For some systems, it may also remove .DIR like extensions.
  (let* ((parsed (efs-ftp-path name))
	 (file (nth 2 parsed)))
    (if (string-equal file "")
	name
      (efs-internal-file-name-as-directory
       (efs-host-type (car parsed) (nth 1 parsed)) name))))

(efs-defun efs-internal-file-name-as-directory nil (name)
  ;; By default, simply adds a trailing /, if there isn't one.
  ;; Note that for expanded filenames, it pays to call this rather
  ;; than efs-file-name-as-directory.
  (cond
   ((string-equal "" name)
    "./")
   ((char-equal (aref name (- (length name) 1)) ?/)
    name)
   (t
    (concat name "/"))))
 	
(defun efs-file-name-directory (name)
  ;; file-name-directory for remote files. Takes care not to
  ;; turn /user@host: into /.
  (let ((path (nth 2 (efs-ftp-path name)))
	file-name-handler-alist)
    (if (or (string-equal path "")
	    (and (= (string-to-char path) ?~)
		 (not
		  (efs-save-match-data
		    (string-match "/" path 1)))))
	name
      (if (efs-save-match-data
	    (not (string-match "/" path)))
	  (efs-replace-path-component name "")
	(file-name-directory name)))))

(defun efs-file-name-nondirectory (name)
  ;; Computes file-name-nondirectory for remote files.
  ;; For expanded filenames, can just call efs-internal-file-name-nondirectory.
  (let ((file (nth 2 (efs-ftp-path name))))
    (if (or (string-equal file "")
	    (and (= (string-to-char file) ?~)
		 (not
		  (efs-save-match-data
		    (string-match "/" file 1)))))
	""
      (if (efs-save-match-data
	    (not (string-match "/" file)))
	  file
	(efs-internal-file-name-nondirectory name)))))

(defun efs-internal-file-name-nondirectory (name)
  ;; Version of file-name-nondirectory, without the efs-file-handler-function.
  ;; Useful to call this, if we have already decomposed the filename.
  (let (file-name-handler-alist)
    (file-name-nondirectory name)))

(defun efs-directory-file-name (dir)
  ;; Computes directory-file-name for remote files.
  ;; Needs to be careful not to turn /foo@bar:/ into /foo@bar:
  (let ((parsed (efs-ftp-path dir)))
    (if (string-equal "/" (nth 2 parsed))
	dir
      (efs-internal-directory-file-name dir))))

(defun efs-internal-directory-file-name (dir)
  ;; Call this if you want to apply directory-file-name to the remote
  ;; part of a efs-style path. Don't call for non-efs-style paths,
  ;; as this short-circuits the file-name-handler-alist completely.
  (let (file-name-handler-alist)
    (directory-file-name dir)))

(efs-defun efs-remote-directory-file-name nil (dir)
  "Returns the file name on the remote system of directory DIR.
If the remote system is not unix, this may not be the same as the file name
of the directory in efs's internal cache."
  (directory-file-name dir))

(defun efs-file-name-sans-versions (filename &optional keep-backup-versions)
  ;; Version of file-name-sans-versions for remote files.
  (or (file-name-absolute-p filename)
      (setq filename (expand-file-name filename)))
  (let ((parsed (efs-ftp-path filename)))
    (efs-internal-file-name-sans-versions
     (efs-host-type (car parsed) (nth 1 parsed))
     filename keep-backup-versions)))

(efs-defun efs-internal-file-name-sans-versions nil
  (filename &optional keep-backup-versions)
  (let (file-name-handler-alist)
    (file-name-sans-versions filename keep-backup-versions)))

(defun efs-diff-latest-backup-file (fn)
  ;; Version of diff latest backup file for remote files.
  ;; Accomodates non-unix.
  ;; Returns the latest backup for fn, according to the numbering
  ;; of the backups. Does not check file-newer-than-file-p.
  (let ((parsed (efs-ftp-path fn)))
    (efs-internal-diff-latest-backup-file
     (efs-host-type (car parsed) (nth 1 parsed)) fn)))

(efs-defun efs-internal-diff-latest-backup-file nil (fn)
  ;; Default behaviour is the behaviour in diff.el
  (let (file-name-handler-alist)
    (diff-latest-backup-file fn)))

(defun efs-unhandled-file-name-directory (filename)
  ;; Calculate a default unhandled directory for an efs buffer.
  ;; This is used to compute directories in which to execute
  ;; processes. This is relevant to V19 only. Doesn't do any harm for
  ;; older versions though. It would be nice if this wasn't such a
  ;; kludge.
  (file-name-directory efs-tmp-name-template))

(defun efs-file-truename (filename)
  ;; Calculates a remote file's truename, if this isn't inhibited.
  (let ((filename (expand-file-name filename)))
    (if (and efs-compute-remote-buffer-file-truename
	     (memq (efs-host-type (car (efs-ftp-path filename)))
		   efs-unix-host-types))
	(efs-internal-file-truename filename)
      filename)))

(defun efs-internal-file-truename (filename)
  ;; Internal function so that we don't keep checking
  ;; efs-compute-remote-buffer-file-truename, etc, as we recurse.
  (let ((dir (efs-file-name-directory filename))
	target dirfile)
    ;; Get the truename of the directory.
    (setq dirfile (efs-directory-file-name dir))
    ;; If these are equal, we have the (or a) root directory.
    (or (string= dir dirfile)
	(setq dir (efs-file-name-as-directory
		   (efs-internal-file-truename dirfile))))
    (if (equal ".." (efs-file-name-nondirectory filename))
	(efs-directory-file-name (efs-file-name-directory
				  (efs-directory-file-name dir)))
      (if (equal "." (efs-file-name-nondirectory filename))
	  (efs-directory-file-name dir)
	;; Put it back on the file name.
	(setq filename (concat dir (efs-file-name-nondirectory filename)))
	;; Is the file name the name of a link?
	(setq target (efs-file-symlink-p filename))
	(if target
	    ;; Yes => chase that link, then start all over
	    ;; since the link may point to a directory name that uses links.
	    ;; We can't safely use expand-file-name here
	    ;; since target might look like foo/../bar where foo
	    ;; is itself a link.  Instead, we handle . and .. above.
	    (if (file-name-absolute-p target)
		(efs-internal-file-truename target)
	      (efs-internal-file-truename (concat dir target)))
	  ;; No, we are done!
	  filename)))))


;;;; ----------------------------------------------------------------
;;;; I/O functions
;;;; ----------------------------------------------------------------

(efs-define-fun efs-set-buffer-file-name (filename)
  ;; Sets the buffer local variables for filename appropriately.
  ;; A special function because XEmacs and FSF do this differently.
  ;; This default behaviour is the lowest common denominator.
  (setq buffer-file-name filename))

(defun efs-write-region (start end filename &optional append visit &rest args)
  ;; write-region for remote files.
  ;; This version accepts the V19 interpretation for the arg VISIT.
  ;; However, making use of this within V18 may cause errors to crop up.
  ;; ARGS should catch the MULE coding-system argument.
  (if (stringp visit) (setq visit (expand-file-name visit)))
  (setq filename (expand-file-name filename))
  (let ((parsed (efs-ftp-path filename))
	;; Make sure that the after-write-region-hook isn't called inside
	;; the file-handler-alist
	(after-write-region-hook nil))
    (if parsed
	(let* ((host (car parsed))
	       (user (nth 1 parsed))
	       (host-type (efs-host-type host user))
	       (temp (car (efs-make-tmp-name nil host)))
	       (type (efs-xfer-type nil nil host-type filename))
	       (abbr (and (or (stringp visit) (eq t visit) (null visit))
			  (efs-relativize-filename
			   (if (stringp visit) visit filename))))
	       (buffer (current-buffer))
	       (b-file-name buffer-file-name)
	       (mod-p (buffer-modified-p)))
	  (unwind-protect
	      (progn
		(condition-case err
		    (progn
		      (unwind-protect
			  (let ((executing-macro t))
			    ;; let-bind executing-macro to inhibit messaging.
			    ;; Setting VISIT to 'quiet is more elegant.
			    ;; But in Emacs 18, doing it this way allows
			    ;; us to modify the visited file modtime, so
			    ;; that undo's show the buffer modified.
			    (let ((inhibit-file-name-handlers
				   (cons 'efs-file-handler-function
					 (and (eq inhibit-file-name-operation
						  'expand-file-name)
					      inhibit-file-name-handlers)))
				  (inhibit-file-name-operation 'write-region))
			      (apply 'write-region start end
				     temp nil visit args)))
			;; buffer-modified-p is now correctly set
			(setq buffer-file-name b-file-name)
			;; File modtime is bogus, so clear.
			(clear-visited-file-modtime))
		      (efs-copy-file-internal
		       temp nil filename parsed (if append 'append t)
		       nil (and abbr (format "Writing %s" abbr))
		       ;; cont
		       (efs-cont (result line cont-lines) (filename buffer
								    visit)
			 (if result
			     (signal 'ftp-error
				     (list "Opening output file"
					   (format "FTP Error: \"%s\"" line)
					   filename)))
			 ;; The new file entry will be added by
			 ;; efs-copy-file-internal.
			 (cond
			  ((eq visit t)
			   ;; This will run asynch.
			   (efs-save-buffer-excursion
			     (set-buffer buffer)
			     (efs-set-buffer-file-name filename)
			     (efs-set-visited-file-modtime)))
			  ((stringp visit)
			   (efs-save-buffer-excursion
			     (set-buffer buffer)
			     (efs-set-buffer-file-name visit)
			     (set-visited-file-modtime)))))
		       nil type))
		  (error
		   ;; restore buffer-modified-p
		   (let (file-name-handler-alist)
		     (set-buffer-modified-p mod-p))
		   (signal (car err) (cdr err))))
		(if (or (eq visit t)
			(and (stringp visit)
			     (efs-ftp-path visit)))
		    (efs-set-buffer-mode)))
	    (efs-del-tmp-name temp))
	  (and abbr (efs-message "Wrote %s" abbr)))
      (if (and (stringp visit) (efs-ftp-path visit))
	  (progn
	    (let ((inhibit-file-name-handlers
		   (cons 'efs-file-handler-function
			 (and (eq inhibit-file-name-operation
				  'expand-file-name)
			      inhibit-file-name-handlers)))
		  (inhibit-file-name-operation 'write-region))
	      (apply 'write-region start end filename append visit args))
	    (efs-set-buffer-file-name visit)
	    (efs-set-visited-file-modtime)
	    (efs-set-buffer-mode))
	(error "efs-write-region called for a local file")))))

(defun efs-insert-file-contents (filename &rest args)
  ;; Inserts file contents for remote files.
  ;; The additional ARGS covers V19 BEG and END. Should also handle the
  ;; CODING-SYSTEM arg for mule. Hope the two don't trip over each other.
  (apply 'efs-insert-file-contents-general 'insert-file-contents filename args))

(defun efs-insert-file-contents-literally (filename &rest args)
  ;; Inserts file contents for remote files.
  ;; The additional ARGS covers V19 BEG and END. Should also handle the
  ;; CODING-SYSTEM arg for mule. Hope the two don't trip over each other.
  (apply 'efs-insert-file-contents-general 'insert-file-contents-literally
	 filename args))

(defun efs-insert-file-contents-general
         (handled-func filename &optional visit &rest args)  
  (barf-if-buffer-read-only)
  (unwind-protect
      (let* ((filename (expand-file-name filename))
	     (parsed (efs-ftp-path filename))
	     (host (car parsed))
	     (host-type (efs-host-type host))
	     (user (nth 1 parsed))
	     (path (nth 2 parsed))
	     (buffer (current-buffer)))
	
	(if (or (file-exists-p filename)
		(let* ((res (and
			     (not (efs-get-host-property host 'rnfr-failed))
			     (efs-send-cmd
			      host user (list 'quote 'rnfr path))))
		       (line (nth 1 res)))
		  ;; RNFR returns a 550 if the file doesn't exist.
		  (if (and line (>= (length line) 4)
			   (string-equal "550 " (substring line 0 4)))
		      nil
		    (if (car res) (efs-set-host-property host 'rnfr-failed t))
		    (efs-del-from-ls-cache filename t nil)
		    (efs-del-hash-entry
		     (efs-canonize-file-name (file-name-directory filename))
		     efs-files-hashtable)
		    (file-exists-p filename))))
	    
	    (let ((temp (concat
			 (car (efs-make-tmp-name nil host))
			 (efs-internal-file-name-extension filename)))
		  (type (efs-xfer-type host-type filename nil nil))
		  (abbr (efs-relativize-filename filename))
		  (i-f-c-size 0))
	      
	      (unwind-protect
		  (efs-copy-file-internal
		   filename parsed temp nil t nil
		   (format "Retrieving %s" abbr)
		   (efs-cont (result line cont-lines) (filename visit buffer
								host-type
								temp handled-func args)
		     (if result
			 (signal 'ftp-error
				 (list "Opening input file"
				       (format "FTP Error: \"%s\""
					       line)
				       filename))
		       (if (eq host-type 'coke)
			   (efs-coke-insert-beverage-contents buffer filename
							      line)
			 (efs-save-buffer-excursion
			   (set-buffer buffer)
			   (if (or (file-readable-p temp)
				   (sleep-for efs-retry-time)
				   ;; Wait for file to hopefully appear.
				   (file-readable-p temp))
			       
			       (setq i-f-c-size
				     (nth 1 (apply handled-func
						 temp visit args)))
			     (signal 'ftp-error
				     (list
				      "Opening input file:"
				      (format
				       "FTP Error: %s not arrived or readable"
				       filename))))
			   ;; This is done asynch
			   (if visit
			       (let ((buffer-file-name filename))
				 (efs-set-visited-file-modtime)))))))
		   nil type)
		(efs-del-tmp-name temp))
	      ;; Return (FILENAME SIZE)
	      (list filename i-f-c-size))
	  (signal 'file-error (list "Opening input file" filename))))
    ;; Set buffer-file-name at the very last, so if anything bombs, we're
    ;; not visiting.
    (if visit
	(efs-set-buffer-file-name filename))))

(defun efs-revert-buffer (arg noconfirm)
  "Revert this buffer from a remote file using ftp."
  (let ((opoint (point)))
    (cond ((null buffer-file-name)
	   (error "Buffer does not seem to be associated with any file"))
	  ((or noconfirm
	       (yes-or-no-p (format "Revert buffer from file %s? "
				    buffer-file-name)))
	   (let ((buffer-read-only nil))
	     ;; Set buffer-file-name to nil
	     ;; so that we don't try to lock the file.
	     (let ((buffer-file-name nil))
	       (unlock-buffer)
	       (erase-buffer))
	     (insert-file-contents buffer-file-name t))
	   (goto-char (min opoint (point-max)))
	   (after-find-file nil)
	   t))))

(defun efs-recover-file (file)
  ;; Version of recover file for remote files, and remote autosave files too.
  (if (auto-save-file-name-p file) (error "%s is an auto-save file" file))
  (let* ((file-name (let ((buffer-file-name file)) (make-auto-save-file-name)))
	 (file-name-parsed (efs-ftp-path file-name))
	 (file-parsed (efs-ftp-path file))
	 (efs-ls-uncache t))
    (cond ((not (file-newer-than-file-p file-name file))
	   (error "Auto-save file %s not current" file-name))
	  ((save-window-excursion
	     (or (eq system-type 'vax-vms)
		 (progn
		   (with-output-to-temp-buffer "*Directory*"
		     (buffer-disable-undo standard-output)
		     (if file-parsed
			 (progn
			   (princ (format "On the host %s:\n"
					  (car file-parsed)))
			   (princ
			    (let ((default-directory exec-directory))
			      (efs-ls file (if (file-symlink-p file)
					       "-lL" "-l")
				      t t))))
		       (princ "On the local host:\n")
		       (let ((default-directory exec-directory))
			 (call-process "ls" nil standard-output nil
				       (if (file-symlink-p file) "-lL" "-l")
				       file)))
		     (princ "\nAUTO SAVE FILE on the ")
		     (if file-name-parsed
			 (progn
			   (princ (format "host %s:\n"
					  (car file-name-parsed)))
			   (princ
			    (efs-ls file-name
				    (if (file-symlink-p file-name) "-lL" "-l")
				    t t)))
		       (princ "local host:\n")
		       (let ((default-directory exec-directory))
			 (call-process "ls" nil standard-output nil
				       "-l" file-name)))
		     (princ "\nFile modification times are given in ")
		     (princ "the local time of each host.\n"))
		   (save-excursion
		     (set-buffer "*Directory*")
		     (goto-char (point-min))
		     (while (not (eobp))
		       (end-of-line)
		       (if (> (current-column) (window-width))
			   (progn
			     (skip-chars-backward " \t")
			     (skip-chars-backward "^ \t\n")
			     (if (> (current-column) 12)
				 (progn
				   (delete-horizontal-space)
				   (insert "\n           ")))))
		       (forward-line 1))
		     (set-buffer-modified-p nil)
		     (goto-char (point-min)))))
	     (yes-or-no-p (format "Recover using this auto save file? ")))
	   (switch-to-buffer (find-file-noselect file t))
	   (let ((buffer-read-only nil))
	     (erase-buffer)
	     (insert-file-contents file-name nil))
	   (after-find-file nil))
	  (t (error "Recover-file cancelled."))))
  ;; This is no longer done in V19. However, I like the caution for
  ;; remote files, where file-newer-than-file-p may lie.
  (setq buffer-auto-save-file-name nil)
  (message "Auto-save off in this buffer till you do M-x auto-save-mode."))

;;;; ------------------------------------------------------------------
;;;; Attributes of files.
;;;; ------------------------------------------------------------------

(defun efs-file-symlink-p (file)
  ;; Version of file-symlink-p for remote files.
  ;; Call efs-expand-file-name rather than the normal
  ;; expand-file-name to stop loops when using a package that
  ;; redefines both file-symlink-p and expand-file-name.
  ;; Do not use efs-get-file-entry, because a child-lookup won't do.
  (let* ((file (efs-expand-file-name file))
	 (ignore-case (memq (efs-host-type (car (efs-ftp-path file)))
			    efs-case-insensitive-host-types))
	 (file-type (car (efs-get-hash-entry
			  (efs-get-file-part file)
			  (efs-get-files (file-name-directory file))
			  ignore-case))))
    (and (stringp file-type)
	 (if (file-name-absolute-p file-type)
	     (efs-replace-path-component file file-type)
	   file-type))))

(defun efs-file-exists-p (path)
  ;; file-exists-p for remote file. Uses the cache if possible.
  (let* ((path (expand-file-name path))
	 (parsed (efs-ftp-path path)))
    (efs-internal-file-exists-p (efs-host-type (car parsed) (nth 1 parsed))
				path)))

(efs-defun efs-internal-file-exists-p nil (path)
  (and (efs-get-file-entry path) t))

(defun efs-file-directory-p (file)
  (let* ((file (expand-file-name file))
	 (parsed (efs-ftp-path file)))
    (efs-internal-file-directory-p (efs-host-type (car parsed) (nth 1 parsed))
				   file)))

(efs-defun efs-internal-file-directory-p nil (path)
  ;; Version of file-directory-p for remote files.
  (let ((parsed (efs-ftp-path path)))
    (or (string-equal (nth 2 parsed) "/")  ; root is always a directory
	(let ((file-ent (car (efs-get-file-entry
			      (efs-internal-file-name-as-directory
			       (efs-host-type (car parsed) (nth 1 parsed))
			       path)))))
	  ;; We do a file-name-as-directory on path here because some
	  ;; machines (VMS) use a .DIR to indicate the filename associated
	  ;; with a directory. This needs to be canonicalized.
	  (if (stringp file-ent)
	      (efs-internal-file-directory-p
	       nil
	       (efs-chase-symlinks
		;; efs-internal-directory-file-name
		;; only loses for paths where the remote file
		;; is /. This has been eliminated.
		(efs-internal-directory-file-name path)))
	    file-ent)))))

(defun efs-file-attributes (file)
  ;; Returns file-file-attributes for a remote file.
  ;; For the file modtime does not return efs's cached value, as that
  ;; corresponds to buffer-file-modtime (i.e. the modtime of the file
  ;; the last time the buffer was vsisted or saved). Caching modtimes
  ;; does not make much sense, as they are usually used to determine
  ;; if a cache is stale. The modtime if a remote file can be obtained with
  ;; efs-get-file-mdtm. This is _not_ returned for the 5th entry here,
  ;; because it requires an FTP transaction, and a priori we don't know
  ;; if the caller actually cares about this info. Having file-attributes
  ;; return such a long list of info is not well suited to remote files,
  ;; as some of this info may be costly to obtain.
  (let* ((file (expand-file-name file))
	 (ent (efs-get-file-entry file)))
    (if ent
	(let* ((parsed (efs-ftp-path file))
	       (host (nth 0 parsed))
	       (user (nth 1 parsed))
	       (path (nth 2 parsed))
	       (type (car ent))
	       (size (or (nth 1 ent) -1))
	       (owner (nth 2 ent))
	       (modes (nth 3 ent))
	       ;; Hack to give remote files a "unique" "inode number".
	       ;; It's actually the sum of the characters in its name.
	       ;; It's not even really unique.
	       (inode (apply '+
			     (nconc (mapcar 'identity host)
				    (mapcar 'identity user)
				    (mapcar 'identity
					    (efs-internal-directory-file-name
					     path)))))
	       (nlinks (or (nth 4 ent) -1))) ; return -1 if we don't know
	  (list
	   (if (and (stringp type) (file-name-absolute-p type))
	       (efs-replace-path-component file type)
	     type)		        ;0 file type
	   nlinks	        	;1 link count
	   (if owner                    ;2 uid
	       ;; Not really a unique integer,
	       ;; just a half-hearted attempt
	       (apply '+ (mapcar 'identity owner))
	     -1)
	   -1	                  	;3 gid
	   '(0 0)	        	;4 atime
	   '(0 0)	        	;5 mtime
	   '(0 0)	         	;6 ctime
	   size 		        ;7 size
	   (or modes                    ;8 mode
	       (concat
		(cond ((stringp type) "l")
		      (type "d")
		      (t "-"))
		"?????????"))
	   nil    		        ;9 gid weird (Who knows if the gid
					;             would be changed?)
	   inode                        ;10 inode
	   -1	         	        ;11 device number [v19 only]
	   )))))

(defun efs-file-writable-p (file)
  ;; file-writable-p for remote files.
  ;; Does not attempt to open the file, but just looks at the cached file
  ;; modes.
  (let* ((file (expand-file-name file))
	 (ent (efs-get-file-entry file)))
    (if (and ent (or (not (stringp (car ent)))
		     (setq file (efs-chase-symlinks file)
			   ent (efs-get-file-entry file))))
	(let* ((owner (nth 2 ent))
	       (modes (nth 3 ent))
	       (parsed (efs-ftp-path file))
	       (host-type (efs-host-type (car parsed)))
	       (user (nth 1 parsed)))
	  (if (memq host-type efs-unix-host-types)
	      (setq host-type 'unix))
	  (efs-internal-file-writable-p host-type user owner modes))
      (let ((dir (file-name-directory file)))
	(and
	 (not (string-equal dir file))
	 (file-directory-p dir)
	 (file-writable-p dir))))))

(efs-defun efs-internal-file-writable-p nil (user owner modes)
  ;; By default, we'll just guess yes.
  t)

(efs-defun efs-internal-file-writable-p unix (user owner modes)
  (if (and modes
	   (not (string-equal user "root")))
      (null
       (null
	(if (string-equal user owner)
	    (memq ?w (list (aref modes 2) (aref modes 5)
			   (aref modes 8)))
	  (memq ?w (list (aref modes 5) (aref modes 8))))))
    t)) ; guess

(defun efs-file-readable-p (file)
  ;; Version of file-readable-p that works for remote files.
  ;; Works by checking efs's cache of the file modes.
  (let* ((file (expand-file-name file))
	 (ent (efs-get-file-entry file)))
    (and ent
	 (or (not (stringp (car ent)))
	     (setq ent (efs-get-file-entry (efs-chase-symlinks file))))
	 ;; file exists
	 (let* ((parsed (efs-ftp-path file))
		(owner (nth 2 ent))
		(modes (nth 3 ent))
		(host-type (efs-host-type (car parsed)))
		(user (nth 1 parsed)))
	   (if (memq host-type efs-unix-host-types)
	       (setq host-type 'unix))
	   (efs-internal-file-readable-p host-type user owner modes)))))

(efs-defun efs-internal-file-readable-p nil (user owner modes)
  ;; Guess t by default
  t)

(efs-defun efs-internal-file-readable-p unix (user owner modes)
  (if (and modes
	   (not (string-equal user "root")))
      (null
       (null
	(if (string-equal user owner)
	    (memq ?r (list (aref modes 1) (aref modes 4)
			   (aref modes 7)))
	  (memq ?r (list (aref modes 4) (aref modes 7))))))
    t)) ; guess

(defun efs-file-executable-p (file)
  ;; Version of file-executable-p for remote files.
  (let ((ent (efs-get-file-entry file)))
    (and ent
	 (or (not (stringp (car ent)))
	     (setq ent (efs-get-file-entry (efs-chase-symlinks file))))
	 ;; file exists
	 (let* ((parsed (efs-ftp-path file))
		(owner (nth 2 ent))
		(modes (nth 3 ent))
		(host-type (efs-host-type (car parsed)))
		(user (nth 1 parsed)))
	   (if (memq host-type efs-unix-host-types)
	       (setq host-type 'unix))
	   (efs-internal-file-executable-p host-type user owner modes)))))

(efs-defun efs-internal-file-executable-p nil (user owner modes)
  ;; Guess t by default
  t)

(efs-defun efs-internal-file-executable-p unix (user owner modes)
  (if (and modes
	   (not (string-equal user "root")))
      (null
       (null
	(if (string-equal user owner)
	    (memq ?x (list (aref modes 3) (aref modes 6)
			   (aref modes 9)))
	  (memq ?x (list (aref modes 6) (aref modes 9))))))
    t)) ; guess

(defun efs-file-accessible-directory-p (dir)
  ;; Version of file-accessible-directory-p for remote directories.
  (let ((file (directory-file-name dir)))
    (and (efs-file-directory-p file) (efs-file-executable-p file))))

;;;; --------------------------------------------------------------
;;;; Listing directories.
;;;; --------------------------------------------------------------

(defun efs-shell-regexp-to-regexp (regexp)
  ;; Converts a shell regexp to an emacs regexp.
  ;; Probably full of bugs. Tries to follow csh globbing.
  (let ((curly 0)
	backslash)
    (concat "^"
	    (mapconcat
	     (function
	      (lambda (char)
		(cond
		 (backslash
		  (setq backslash nil)
		  (regexp-quote (char-to-string char)))
		 ((and (> curly 0) (eq char ?,))
		  "\\|")
		 ((memq char '(?[ ?]))
		  (char-to-string char))
		 ((eq char ??)
		  ".")
		 ((eq char ?\\)
		  (setq backslash t)
		  "")
		 ((eq char ?*)
		  ".*")
		 ((eq char ?{)
		  (setq curly (1+ curly))
		  "\\(")
		 ((and (eq char ?}) (> curly 0))
		  (setq curly (1- curly))
		  "\\)")
		 (t (regexp-quote (char-to-string char))))))
	     regexp nil)
	    "$")))


;;; Getting directory listings.

(defun efs-directory-files (directory &optional full match nosort &rest ignored-for-now)
  ;; Returns directory-files for remote directories.
  ;; NOSORT is a V19 arg.
  (let* ((directory (expand-file-name directory))
	 (parsed (efs-ftp-path directory))
	 (directory (efs-internal-file-name-as-directory
		     (efs-host-type (car parsed) (nth 1 parsed)) directory))
	 files)
    (efs-barf-if-not-directory directory)
    (setq files (efs-hash-table-keys (efs-get-files directory) nosort))
    (cond
     ((null (or full match))
      files)
     (match ; this is slow case
      (let (res f)
	(efs-save-match-data
	  (while files
	    (setq f (if full (concat directory (car files)) (car files))
		  files (cdr files))
	    (if (string-match match f)
		(setq res (nconc res (list f))))))
	res))
     (full
      (mapcar (function
	       (lambda (fn)
		 (concat directory fn)))
	      files)))))

(defun efs-list-directory (dirname &optional verbose)
  ;; Version of list-directory for remote directories.
  ;; If verbose is nil, it gets its information from efs's
  ;; internal cache.
  (let* ((dirname (expand-file-name (or dirname default-directory)))
	 header)
    (if (file-directory-p dirname)
	(setq dirname (file-name-as-directory dirname)))
    (setq header dirname)
    (with-output-to-temp-buffer "*Directory*"
      (buffer-disable-undo standard-output)
      (princ "Directory ")
      (princ header)
      (terpri)
      (princ
       (efs-ls dirname (if verbose
			   list-directory-verbose-switches
			 list-directory-brief-switches)
	       t)))))

;;;; -------------------------------------------------------------------
;;;; Manipulating buffers.
;;;; -------------------------------------------------------------------

(defun efs-get-file-buffer (file)
  ;; Version of get-file-buffer for remote files. Needs to fuss over things
  ;; like OS's which are case-insens. for file names.
  (let ((file (efs-canonize-file-name (expand-file-name file)))
	(buff-list (buffer-list))
	buff-name)
    (catch 'match
      (while buff-list
	(and (setq buff-name (buffer-file-name (car buff-list)))
	     (= (length buff-name) (length file)) ; efficiency hack
	     (string-equal (efs-canonize-file-name buff-name) file)
	     (throw 'match (car buff-list)))
	(setq buff-list (cdr buff-list))))))

(defun efs-create-file-buffer (filename)
  ;; Version of create-file-buffer for remote file names.
  (let* ((parsed (efs-ftp-path (expand-file-name filename)))
	 (file (nth 2 parsed))
	 (host (car parsed))
	 (host-type (efs-host-type host))
	 (buff (cond
		((null efs-fancy-buffer-names)
		 (if (string-equal file "/")
		     "/"
		   (efs-internal-file-name-nondirectory
		    (efs-internal-directory-file-name file))))
		((stringp efs-fancy-buffer-names)
		 (format efs-fancy-buffer-names
			 (if (string-equal file "/")
			     "/"
			   (efs-internal-file-name-nondirectory
			    (efs-internal-directory-file-name file)))
			 (substring host 0 (string-match "\\." host 1))))
		(t ; efs-fancy-buffer-names had better be a function
		 (funcall efs-fancy-buffer-names host
			  (nth 1 parsed) file)))))
    (if (memq host-type efs-case-insensitive-host-types)
	(cond ((eq efs-buffer-name-case 'down)
	       (setq buff (downcase buff)))
	      ((eq efs-buffer-name-case 'up)
	       (setq buff (upcase buff)))))
    (get-buffer-create (generate-new-buffer-name buff))))
    
(defun efs-set-buffer-mode ()
  "Set correct modes for the current buffer if it is visiting a remote file."
  (if (and (stringp buffer-file-name)
	   (efs-ftp-path buffer-file-name))
      (progn
	(auto-save-mode efs-auto-save)
	(set (make-local-variable 'revert-buffer-function)
	     'efs-revert-buffer)
	(set (make-local-variable 'default-directory-function)
	     'efs-default-dir-function))))

;;;; ---------------------------------------------------------
;;;; Functions for doing backups.
;;;; ---------------------------------------------------------

(defun efs-backup-buffer ()
  ;; Version of backup-buffer for buffers visiting remote files.
  (if (and efs-make-backup-files
	   (not backup-inhibited))
      (let* ((parsed (efs-ftp-path buffer-file-name))
	     (host (car parsed))
	     (host-type (efs-host-type (car parsed))))
	(if (or (not (listp efs-make-backup-files))
		(memq host-type efs-make-backup-files))
	    (efs-internal-backup-buffer
	     host host-type (nth 1 parsed) (nth 2 parsed))))))

(defun efs-internal-backup-buffer (host host-type user remote-path)
  ;; This is almost a copy of the function in files.el, modified
  ;; to check to see if the backup file exists, before deleting it.
  ;; It also supports efs-backup-by-copying, and tries to do the
  ;; right thing about backup-by-copying-when-mismatch. Only called
  ;; for remote files.
  ;; Set the umask now, so that `setmodes' knows about it.
  (efs-set-umask host user)
  (let ((ent (efs-get-file-entry (expand-file-name buffer-file-name)))
	;; Never do version-control if the remote operating system is doing it.
	(version-control (if (memq host-type efs-version-host-types)
			     'never
			   version-control))
	modstring)
    (and make-backup-files
	 (not buffer-backed-up)
	 ent ; i.e. file-exists-p
	 (not (eq t (car ent)))
	 (or (null (setq modstring (nth 3 ent)))
	     (not (memq host-type efs-unix-host-types))
	     (memq (aref modstring 0) '(?- ?l)))
	 (or (< (length remote-path) 5)
	     (not (string-equal "/tmp/" (substring remote-path 0 5))))
	 (condition-case ()
	     (let* ((backup-info (find-backup-file-name buffer-file-name))
		    (backupname (car backup-info))
		    (targets (cdr backup-info))
		    (links (nth 4 ent))
		    setmodes)
	       (condition-case ()
		   (if (or file-precious-flag
			   (stringp (car ent)) ; symlinkp
			   efs-backup-by-copying
			   (and backup-by-copying-when-linked
				links (> links 1))
			   (and backup-by-copying-when-mismatch
				(not
				 (if (memq
				      host-type
				      efs-case-insensitive-host-types)
				     (string-equal
				      (downcase user) (downcase (nth 2 ent)))
				   (string-equal user (nth 2 ent))))))
		       (copy-file buffer-file-name backupname t t)
		     (condition-case ()
			 (if (file-exists-p backupname)
			     (delete-file backupname))
		       (file-error nil))
		     (rename-file buffer-file-name backupname t)
		     (setq setmodes (file-modes backupname)))
		 (file-error
		  ;; If trouble writing the backup, write it in ~.
		  (setq backupname (expand-file-name "~/%backup%~"))
		  (message
		   "Cannot write backup file; backing up in ~/%%backup%%~")
		  (sleep-for 1)
		  (copy-file buffer-file-name backupname t t)))
	       (setq buffer-backed-up t)
	       ;; Starting with 19.26, trim-versions-without-asking
	       ;; has been renamed to delete-old-verions.
	       (if (and targets
			(or (if (boundp 'trim-versions-without-asking)
				trim-versions-without-asking
			      (and
			       (boundp 'delete-old-versions)
			       delete-old-versions))
			    (y-or-n-p (format
				       "Delete excess backup versions of %s? "
				       buffer-file-name))))
		   (while targets
		     (condition-case ()
			 (delete-file (car targets))
		       (file-error nil))
		     (setq targets (cdr targets))))
	       ;; If the file was already written with the right modes,
	       ;; don't return set-modes.
	       (and setmodes
		    (null
		     (let ((buff (get-buffer
				  (efs-ftp-process-buffer host user))))
		       (and buff
			    (save-excursion
			      (set-buffer buff)
			      (and (integerp efs-process-umask)
				   (= (efs-modes-from-umask efs-process-umask)
				      setmodes))))))
		    setmodes))
	   (file-error nil)))))

;;;; ------------------------------------------------------------
;;;; Redefinition for Emacs file mode support
;;;; ------------------------------------------------------------

(defmacro efs-build-mode-string-element (int suid-p sticky-p)
  ;; INT is between 0 and 7.
  ;; If SUID-P is non-nil, we are building the 3-char string for either
  ;; the owner or group, and the s[ug]id bit is set.
  ;; If STICKY-P is non-nil, we are building the string for other perms,
  ;; and the sticky bit is set.
  ;; It doesn't make sense for both SUID-P and STICKY-P be non-nil!
  (` (let* ((int (, int))
	    (suid-p (, suid-p))
	    (sticky-p (, sticky-p))
	    (read-bit (if (memq int '(4 5 6 7)) "r" "-"))
	    (write-bit (if (memq int '(2 3 6 7)) "w" "-"))
	    (x-bit (if (memq int '(1 3 5 7))
		       (cond (suid-p "s") (sticky-p "t") ("x"))
		     (cond (suid-p "S") (sticky-p "T") ("-")))))
       (concat read-bit write-bit x-bit))))
       
(defun efs-mode-string (int)
  ;; Takes an octal integer between 0 and 7777, and returns the 9 character
  ;; mode string.
  (let* ((other-int (% int 10))
	 (int (/ int 10))
	 (group-int (% int 10))
	 (int (/ int 10))
	 (owner-int (% int 10))
	 (int (/ int 10))
	 (suid (memq int '(4 5 6 7)))
	 (sgid (memq int '(2 3 6 7)))
	 (sticky (memq int '(1 3 5 7))))
    (concat (efs-build-mode-string-element owner-int suid nil)
	    (efs-build-mode-string-element group-int sgid nil)
	    (efs-build-mode-string-element other-int nil sticky))))
  
(defun efs-shell-call-process (command dir &optional in-background)
  ;; Runs shell process on remote hosts.
  (if (not efs-use-remote-shell-internally)
      -1
    (let* ((parsed (efs-ftp-path dir))
	   (host (car parsed))
	   (user (nth 1 parsed))
	   (rdir (nth 2 parsed))
	   (file-name-handler-alist nil))
      (or (string-equal (efs-internal-directory-file-name dir)
			(efs-expand-tilde "~" (efs-host-type host) host user))
	  (string-match "^cd " command)
	  (setq command (concat "cd " rdir "; " command)))
      (setq command
	    (format  "%s %s%s \"%s\""	; remsh -l USER does not work well
					; on a hp-ux machine I tried
		     efs-remote-shell-file-name host
		     (if efs-remote-shell-takes-user
			 (concat " -l " user)
		       "")
		     command))
      (message "Doing shell command on %s..." host)
      ;; do it
      (let ((process-connection-type	; don't waste pty's
	     (null (null in-background))))
	(setq default-directory (file-name-directory efs-tmp-name-template))
	(if in-background
	    (progn
	      (setq mode-line-process '(": %s"))
	      (start-process "Shell" (current-buffer)
			     shell-file-name "-c" command))
	  (call-process shell-file-name nil t nil "-c" command))))))

(defun efs-set-file-modes (file mode)
  ;; set-file-modes for remote files.
  ;; For remote files, if mode is nil, does nothing.
  ;; This is because efs-file-modes returns nil if the modes
  ;; of a remote file couldn't be determined, even if the file exists.
  (and mode
       (let* ((file (expand-file-name file))
	      (parsed (efs-ftp-path file))
	      (host (car parsed))
	      (user (nth 1 parsed))
	      (r-file (nth 2 parsed))
	      ;; convert to octal, and keep only 12 lowest order bits.
	      (omode (format "%o" (- mode (lsh (lsh mode -12) 12)))))
	 (if (or (efs-get-host-property host 'chmod-failed)
		 (null (memq (efs-host-type host user) efs-unix-host-types)))
	     (message "Unable to set file modes for %s to %s." file omode)
	   (efs-send-cmd
	    host user
	    (list 'quote 'site 'chmod omode r-file)
	    nil nil
	    (efs-cont (result line cont-lines) (host file r-file omode)
	      (if result
		  (let ((exit-code
			 (efs-shell-call-process
			  (concat "chmod " omode " " (file-name-nondirectory file))
			  (file-name-directory file))))
		    (if (not (equal 0 exit-code))
			(progn
			  (efs-set-host-property host 'chmod-failed t)
			  (message "CHMOD %s failed for %s on %s." omode r-file host)
			  (if efs-ding-on-chmod-failure
			      (progn (ding) (sit-for 1))))))
		(let ((ent (efs-get-file-entry file)))
		  (if ent
		      (let* ((type
			      (cond
			       ((null (car ent)) "-")
			       ((eq (car ent) t) "d")
			       ((stringp (car ent)) "s")
			       (t
				(error
				 "Weird error in efs-set-file-modes"))))
			     (mode-string (concat
					   type
					   (efs-mode-string
					    (string-to-int omode))))
			     (tail (nthcdr 3 ent)))
			(if (consp tail)
			    (setcar tail mode-string)
			  (efs-add-file-entry nil file (car ent) (nth 1 ent)
					      (nth 2 ent) mode-string)))))))
	    0)))) ; It should be safe to do this NOWAIT = 0
  ;; set-file-modes returns nil
  nil)

(defmacro efs-parse-mode-element (modes)
  ;; Parses MODES, a string of three chars, and returns an integer
  ;; between 0 and 7 according to how unix file modes are represented
  ;; for chmod.
  (` (if (= (length (, modes)) 3)
	 (let ((list (mapcar
		      (function (lambda (char)
				  (if (memq char '( ?- ?S ?T)) 0 1)))
		      (, modes))))
	   ;; Convert to octal
	   (+ (* (car list) 4) (* (nth 1 list) 2) (nth 2 list)))
       (error "Can't parse modes %s" (, modes)))))

(defun efs-parse-mode-string (string)
  ;; Parse a 9-character mode string, and return what it represents
  ;; as a decimal integer.
  (let ((owner (efs-parse-mode-element (substring string 0 3)))
	(group (efs-parse-mode-element (substring string 3 6)))
	(other (efs-parse-mode-element (substring string 6 9)))
	(owner-x (elt string 2))
	(group-x (elt string 5))
	(other-x (elt string 8)))
    (+ (* (+ (if (memq owner-x '(?s ?S)) 4 0)
	     (if (memq group-x '(?s ?S)) 2 0)
	     (if (memq other-x '(?t ?T)) 1 0))
	  512)
       (* owner 64)
       (* group 8)
       other)))

(defun efs-file-modes (file)
  ;; Version of file-modes for remote files.
  ;; Returns nil if the file modes can't be determined, either because
  ;; the file doesn't exist, or for any other reason.
  (let* ((file (expand-file-name file))
	 (parsed (efs-ftp-path file)))
    (and (memq (efs-host-type (car parsed)) efs-unix-host-types)
	 ;; Someday we should cache mode strings for non-unix, but they
	 ;; won't be in unix format. Also, CHMOD doesn't work for non-unix
	 ;; hosts, so returning this info to emacs is a waste.
	 (let* ((ent (efs-get-file-entry file))
		(modes (nth 3 ent)))
	   (and modes
		(efs-parse-mode-string (substring modes 1)))))))

;;;; ------------------------------------------------------------
;;;; Redefinition of Emacs file modtime support.
;;;; ------------------------------------------------------------

(defun efs-day-number (year month day)
  ;; Returns the day number within year of date. Taken from calendar.el,
  ;; by Edward Reingold. Thanks.
  ;; An explanation of the calculation can be found in PascAlgorithms by
  ;; Edward and Ruth Reingold, Scott-Foresman/Little, Brown, 1988.
  (let ((day-of-year (+ day (* 31 (1- month)))))
    (if (> month 2)
	(progn
	  (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
	  (if (zerop (% year 4))
	      (setq day-of-year (1+ day-of-year)))))
    day-of-year))

(defun efs-days-elapsed (year month day)
  ;; Number of days elapsed since Jan 1, `efs-time-zero'
  (+ (efs-day-number year month day)         ; days this year
     (* 365 (- year efs-time-zero))          ; days in prior years
     (- (/ (max (1- year) efs-time-zero) 4)
	(/ efs-time-zero 4))                 ; leap years
     -1 ))                                   ; don't count today

;; 2^16 = 65536
;; Use this to avoid overflows

(defun efs-seconds-elapsed (year month day hours minutes seconds)
  ;; Computes the seconds elapsed from `efs-time-zero', in emacs'
  ;; format of a list of two integers, the first the higher 16-bits,
  ;; the second the lower 16-bits.
  (let* ((days (efs-days-elapsed year month day))
	 ;; compute hours
	 (hours (+ (* 24 days) hours))
	 (high (lsh hours -16))
	 (low (- hours (lsh high 16)))
	 ;; compute minutes
	 (low (+ (* low 60) minutes))
	 (carry (lsh low -16))
	 (high (+ (* high 60) carry))
	 (low (- low (lsh carry 16)))
	 ;; compute seconds
	 (low (+ (* low 60) seconds))
	 (carry (lsh low -16))
	 (high (+ (* high 60) carry))
	 (low (- low (lsh carry 16))))
    (list high low)))

(defun efs-parse-mdtime (string)
  ;; Parse a string, which is assumed to be the result of an ftp MDTM command.
  (efs-save-match-data
    (if (string-match efs-mdtm-msgs string)
	(efs-seconds-elapsed
	 (string-to-int (substring string 4 8))
	 (string-to-int (substring string 8 10))
	 (string-to-int (substring string 10 12))
	 (string-to-int (substring string 12 14))
	 (string-to-int (substring string 14 16))
	 (string-to-int (substring string 16 18))))))

(defun efs-parse-ctime (string)
  ;; Parse STRING which is assumed to be the result of a query over port 37.
  ;; Returns the number of seconds since the turn of the century, as a
  ;; list of two 16-bit integers.
  (and (= (length string) 4)
       (list (+ (lsh (aref string 0) 8) (aref string 1))
	     (+ (lsh (aref string 2) 8) (aref string 3)))))

(defun efs-time-minus (time1 time2)
  ;; Subtract 32-bit integers, represented as two 16-bit integers.
  (let ((high (- (car time1) (car time2)))
	(low (- (nth 1 time1) (nth 1 time2))))
    (cond
     ((and (< high 0) (> low 0))
      (setq high (1+ high)
	    low (- low 65536)))
     ((and (> high 0) (< low 0))
      (setq high (1- high)
	    low (+ 65536 low))))
    (list high low)))

(defun efs-time-greater (time1 time2)
  ;; Compare two 32-bit integers, each represented as a list of two 16-bit
  ;; integers.
  (or (> (car time1) (car time2))
      (and (= (car time1) (car time2))
	   (> (nth 1 time1) (nth 1 time2)))))

(defun efs-century-time (host &optional nowait cont)
  ;; Treat nil as the local host.
  ;; Returns the # of seconds since the turn of the century, according
  ;; to the system clock on host.
  ;; CONT is called with first arg HOST and second the # of seconds.
  (or host (setq host (system-name)))
  (efs-set-host-property host 'last-ctime nil)
  (efs-set-host-property host 'ctime-cont cont)
  (let ((name (format efs-ctime-process-name-format host))
	proc)
    (condition-case nil (delete-process name) (error nil))
    (if (and
	 (or (efs-save-match-data (string-match efs-local-host-regexp host))
	     (string-equal host (system-name)))
	 (setq proc (condition-case nil
			(open-network-stream name nil host 37)
		      (error nil))))
	(progn
	  (set (intern name) "")
	  (set-process-filter
	   proc
	   (function
	    (lambda (proc string)
	      (let ((name (process-name proc))
		    result)
		(set (intern name) (concat (symbol-value (intern name))
					   string))
		(setq result (efs-parse-ctime
			      (symbol-value (intern name))))
		(if result
		    (let* ((host (substring name 11 -1))
			   (cont (efs-get-host-property host 'ctime-cont)))
		      (efs-set-host-property host 'last-ctime result)
		      (condition-case nil (delete-process proc) (error nil))
		      (if cont
			  (progn
			    (efs-set-host-property host 'ctime-cont nil)
			    (efs-call-cont cont host result)))))))))
	  (set-process-sentinel
	   proc
	   (function
	    (lambda (proc state)
	      (let* ((name (process-name proc))
		     (host (substring name 11 -1))
		     (cont (efs-get-host-property host 'ctime-cont)))
		(makunbound (intern name))
		(or (efs-get-host-property host 'last-ctime)
		    (if cont
			(progn
			  (efs-set-host-property host 'ctime-cont nil)
			  (efs-call-cont cont host 'failed))))))))
	  (if nowait
	      nil
	    (let ((quit-flag nil)
		  (inhibit-quit nil))
	      (while (memq (process-status proc) '(run open))
		(accept-process-output)))
	    (accept-process-output)
	    (or (efs-get-host-property host 'last-ctime)
		'failed)))
      (if cont
	  (progn
	    (efs-set-host-property host 'ctime-cont nil)
	    (efs-call-cont cont host 'failed)))
      (if nowait nil 'failed))))

(defun efs-clock-difference (host &optional nowait)
  ;; clock difference with the local host
  (let ((result (efs-get-host-property host 'clock-diff)))
    (or
     result
     (progn
       (efs-century-time
	host nowait
	(efs-cont (host result) (nowait)
	  (if (eq result 'failed)
	      (efs-set-host-property host 'clock-diff 'failed)
	    (efs-century-time
	     nil nowait
	     (efs-cont (lhost lresult) (host result)
	       (if (eq lresult 'failed)
		   (efs-set-host-property host 'clock-diff 'failed)
		 (efs-set-host-property host 'clock-diff
					(efs-time-minus result lresult))))))))
       (and (null nowait)
	    (or (efs-get-host-property host 'clock-diff)
		'failed))))))

(defun efs-get-file-mdtm (host user file path)
  "For HOST and USER, return FILE's last modification time.
PATH is the file name in full efs syntax.
Returns a list of two six-digit integers which represent the 16 high order
bits, and 16 low order bits of the number of elapsed seconds since
`efs-time-zero'"
  (and (null (efs-get-host-property host 'mdtm-failed))
       (let ((result (efs-send-cmd host user (list 'quote 'mdtm file)
				   (and (eq efs-verbose t)
					"Getting modtime")))
	     parsed)
	 (if (and (null (car result))
		  (setq parsed (efs-parse-mdtime (nth 1 result))))
	     (let ((ent (efs-get-file-entry path)))
	       (if ent
		   (setcdr ent (list (nth 1 ent) (nth 2 ent)
				     (nth 3 ent) (nth 4 ent)
				     parsed)))
	       parsed)
	   (efs-save-match-data
	     ;; The 550 error is for a nonexistent file.  Actually implies
	     ;; that MDTM works.
	     (if (string-match "^550 " (nth 1 result))
		 '(0 0)
	       (efs-set-host-property host 'mdtm-failed t)
	       nil))))))

(efs-define-fun efs-set-emacs-bvf-mdtm (buffer mdtm)
  ;; Sets cached value for the buffer visited file modtime.
  (if (get-buffer buffer)
      (save-excursion
	(set-buffer buffer)
	(let (file-name-handler-alist)
	  (set-visited-file-modtime mdtm)))))

;; (defun efs-set-visited-file-modtime (&optional time)
;;   ;; For remote files sets the modtime for a buffer to be that of the
;;   ;; visited file. With arg TIME sets the modtime to TIME. TIME must be a list
;;   ;; of two 16-bit integers.
;;   ;; The function set-visited-file-modtime is for emacs-19. It doesn't
;;   ;; exist in emacs 18. If you're running efs, it will work in emacs 18 for
;;   ;; remote files only.
;;   (if time
;;       (efs-set-emacs-bvf-mdtm (current-buffer) time)
;;     (let* ((path buffer-file-name)
;; 	   (parsed (efs-ftp-path path))
;; 	   (host (car parsed))
;; 	   (user (nth 1 parsed))
;; 	   (file (nth 2 parsed))
;; 	   (buffer (current-buffer)))
;;       (if (efs-save-match-data
;; 	    (and efs-verify-modtime-host-regexp
;; 		 (string-match efs-verify-modtime-host-regexp host)
;; 		 (or efs-verify-anonymous-modtime
;; 		     (not (efs-anonymous-p user)))
;; 		 (not (efs-get-host-property host 'mdtm-failed))))
;; 	  (efs-send-cmd
;; 	   host user (list 'quote 'mdtm file)
;; 	   nil nil
;; 	   (efs-cont (result line cont-lines) (host user path buffer)
;; 	     (let (modtime)
;; 	       (if (and (null result)
;; 			(setq modtime (efs-parse-mdtime line)))
;; 		   (let ((ent (efs-get-file-entry path)))
;; 		     (if ent
;; 			 (setcdr ent (list (nth 1 ent) (nth 2 ent)
;; 					   (nth 3 ent) (nth 4 ent)
;; 					   modtime)))
;; 		     (setq buffer (and (setq buffer (get-buffer buffer))
;; 				       (buffer-name buffer)))
;; 		     ;; Beware that since this is happening asynch, the buffer
;; 		     ;; may have disappeared.
;; 		     (and buffer (efs-set-emacs-bvf-mdtm buffer modtime)))
;; 		 (efs-save-match-data
;; 		   (or (string-match "^550 " line)
;; 		       (efs-set-host-property host 'mdtm-failed t)))
;; 		 (efs-set-emacs-bvf-mdtm buffer 0)))) ; store dummy values
;; 	   0) ; Always do this NOWAIT = 0
;; 	(efs-set-emacs-bvf-mdtm buffer 0))
;;       nil) ; return NIL
;;     ))

(defvar efs-set-modtimes-synchronously nil
  "*Whether efs uses a synchronous FTP command to set the visited file modtime.
Setting this variable to non-nil means that efs will set visited file modtimes
synchronously.

Asynchronous setting of visited file modtimes leaves a very small
window where Emacs may fail to detect a super session.  However, it gives
faster user access to newly visited files.")


(defun efs-set-visited-file-modtime (&optional time)
  ;; For remote files sets the modtime for a buffer to be that of the
  ;; visited file. With arg TIME sets the modtime to TIME. TIME must be a list
  ;; of two 16-bit integers.
  ;; The function set-visited-file-modtime is for emacs-19. It doesn't
  ;; exist in emacs 18. If you're running efs, it will work in emacs 18 for
  ;; remote files only.
  (if time
      (efs-set-emacs-bvf-mdtm (current-buffer) time)
    (let* ((path buffer-file-name)
	   (parsed (efs-ftp-path path))
	   (host (car parsed))
	   (user (nth 1 parsed))
	   (file (nth 2 parsed))
	   (buffer (current-buffer)))
      (if (efs-save-match-data
	    (and efs-verify-modtime-host-regexp
		 (string-match efs-verify-modtime-host-regexp host)
		 (or efs-verify-anonymous-modtime
		     (not (efs-anonymous-p user)))
		 (not (efs-get-host-property host 'mdtm-failed))))
	  (progn
	    (or efs-set-modtimes-synchronously (clear-visited-file-modtime))
	    (efs-send-cmd
	     host user (list 'quote 'mdtm file)
	     nil nil
	     (efs-cont (result line cont-lines) (host user path buffer)
	       (let (modtime)
		 (if (and (null result)
			  (setq modtime (efs-parse-mdtime line)))
		     (let ((ent (efs-get-file-entry path)))
		       (if ent
			   (setcdr ent (list (nth 1 ent) (nth 2 ent)
					     (nth 3 ent) (nth 4 ent)
					     modtime)))
		       (setq buffer (and (setq buffer (get-buffer buffer))
					 (buffer-name buffer)))
		       ;; Beware that since might be happening asynch,
		       ;; the buffer may have disappeared.
		       (and buffer (efs-set-emacs-bvf-mdtm buffer modtime)))
		   (efs-save-match-data
		     (or (string-match "^550 " line)
			 (efs-set-host-property host 'mdtm-failed t)))
		   (efs-set-emacs-bvf-mdtm buffer '(0 0))))) ; store dummy values
	     (and (null efs-set-modtimes-synchronously) 0)))
	(efs-set-emacs-bvf-mdtm buffer '(0 0)))
      nil))) ; return NIL

(defun efs-file-newer-than-file-p (file1 file2)
  ;; Version of file-newer-than-file-p for remote files.
  (let* ((file1 (expand-file-name file1))
	 (file2 (expand-file-name file2))
	 (parsed1 (efs-ftp-path file1))
	 (parsed2 (efs-ftp-path file2))
	 (host1 (car parsed1))
	 (host2 (car parsed2))
	 (user1 (nth 1 parsed1))
	 (user2 (nth 1 parsed2)))
    (cond
     ;; If the first file doedn't exist, or is remote but
     ;; we're not supposed to check modtimes on it, return nil.
     ((or (null (file-exists-p file1))
	  (and parsed1
	       (or
		(null efs-verify-modtime-host-regexp)
		(efs-get-host-property host1 'mdtm-failed)
		(not (string-match efs-verify-modtime-host-regexp host1))
		(and (null efs-verify-anonymous-modtime)
		     (efs-anonymous-p user1)))))
      nil)
     ;; If the same is true for the second file, return t.
     ((or (null (file-exists-p file2))
	  (and parsed2
	       (or
		(null efs-verify-modtime-host-regexp)
		(efs-get-host-property host2 'mdtm-failed)
		(not (string-match efs-verify-modtime-host-regexp host2))
		(and (null efs-verify-anonymous-modtime)
		     (efs-anonymous-p user2)))))
      t)
     ;; Calculate modtimes. If we get here, any remote files should
     ;; have a file entry.
     (t
      (let (mod1 mod2 shift1 shift2)
	(if parsed1
	    (let ((ent (efs-get-file-entry file1)))
	      (setq mod1 (nth 5 ent)
		    shift1 (efs-clock-difference host1))
	      (or mod1
		  (setq mod1 (efs-get-file-mdtm
			      host1 user1 (nth 2 parsed1) file1))))
	  (setq mod1 (nth 5 (file-attributes file1))))
	(if parsed2
	    (let ((ent (efs-get-file-entry file2)))
	      (setq mod2 (nth 5 ent)
		    shift2 (efs-clock-difference host2))
	      (or mod2
		  (setq mod2 (efs-get-file-mdtm
				host2 user2 (nth 2 parsed2) file2))))
	  (setq mod2 (nth 5 (file-attributes file2))))
	;; If we can't compute clock shifts, we act as if we don't
	;; even know the modtime. Should we have more faith in ntp?
	(cond
	 ((or (null mod1) (eq shift1 'failed))
	  nil)
	 ((or (null mod2) (eq shift2 'failed))
	  t)
	 ;; We get to compute something!
	 (t
	  (efs-time-greater
	   (if shift1 (efs-time-minus mod1 shift1) mod1)
	   (if shift2 (efs-time-minus mod2 shift2) mod2)))))))))

(defun efs-verify-visited-file-modtime (buff)
  ;; Verifies the modtime for buffers visiting remote files.
  ;; Won't get called for buffer not visiting any file.
  (let ((buff (get-buffer buff)))
    (null 
     (and buff ; return t if no buffer?  Need to beware of multi-threading.
	  (buffer-file-name buff) ; t if no file
	  (let ((mdtm (save-excursion
			(set-buffer buff)
			(visited-file-modtime))))
	    (and
	     (not (eq mdtm 0))
	     (not (equal mdtm '(0 0)))
	     efs-verify-modtime-host-regexp
	     (let* ((path (buffer-file-name buff))
		    (parsed (efs-ftp-path path))
		    (host (car parsed))
		    (user (nth 1 parsed))
		    nmdtm)
	       (and
		(null (efs-get-host-property host 'mdtm-failed))
		(efs-save-match-data
		  (string-match
		   efs-verify-modtime-host-regexp host))
		(or  efs-verify-anonymous-modtime
		     (not (efs-anonymous-p user)))
		(setq nmdtm (efs-get-file-mdtm host user (nth 2 parsed) path))
		(progn
		  (or (equal nmdtm '(0 0))
		      (file-exists-p path) ; Make sure that there is an entry.
		      (null
		       (efs-get-files
			(file-name-directory
			 (efs-internal-directory-file-name path))))
		      (efs-add-file-entry
		       (efs-host-type host) path nil nil nil nil nil nmdtm))
		  (null (and (eq (cdr mdtm) (nth 1 nmdtm))
			     (eq (car mdtm) (car nmdtm)))))))))))))

;;;; -----------------------------------------------------------
;;;; Redefinition of Emacs file name completion
;;;; -----------------------------------------------------------

(defmacro efs-set-completion-ignored-pattern ()
  ;; Set regexp efs-completion-ignored-pattern
  ;; to use for filename completion.
  (`
   (or (equal efs-completion-ignored-extensions
	      completion-ignored-extensions)
       (setq efs-completion-ignored-extensions
	     completion-ignored-extensions
	     efs-completion-ignored-pattern
	     (mapconcat (function
			 (lambda (s) (if (stringp s)
					 (concat (regexp-quote s) "$")
				       "/"))) ; / never in filename
			efs-completion-ignored-extensions
			"\\|")))))

(defun efs-file-entry-active-p (sym)
  ;; If the file entry is a symlink, returns whether the file pointed to
  ;; exists.
  ;; Note that DIR is dynamically bound.
  (let ((file-type (car (get sym 'val))))
    (or (not (stringp file-type))
	(file-exists-p (efs-chase-symlinks
			(expand-file-name file-type efs-completion-dir))))))

(defun efs-file-entry-not-ignored-p (sym)
  ;; If the file entry is not a directory (nor a symlink pointing to a
  ;; directory) returns whether the file (or file pointed to by the symlink)
  ;; is ignored by completion-ignored-extensions.
  (let ((file-type (car (get sym 'val)))
	(symname (symbol-name sym)))
    (if (stringp file-type)
	;; Maybe file-truename would be better here, but it is very costly
	;; to chase symlinks at every level over FTP.
	(let ((file (efs-chase-symlinks (expand-file-name
					 file-type efs-completion-dir))))
	  (or (file-directory-p file)
	      (and (file-exists-p file)
		   (not (string-match efs-completion-ignored-pattern
				      symname)))))
      (or file-type ; is a directory name
	  (not (string-match efs-completion-ignored-pattern symname))))))

(defun efs-file-name-all-completions (file dir)
  ;; Does file-name-all-completions in remote directories.
  (efs-barf-if-not-directory dir)
  (let* ((efs-completion-dir (file-name-as-directory (expand-file-name dir)))
	 (completion-ignore-case
	  (memq (efs-host-type (car (efs-ftp-path efs-completion-dir)))
		efs-case-insensitive-host-types))
	 (tbl (efs-get-files efs-completion-dir))
	 (completions
	  (all-completions file tbl
			   (function efs-file-entry-active-p))))
    ;; see whether each matching file is a directory or not...
    (mapcar
     ;; Since the entries in completions will match the case
     ;; of the entries in tbl, don't need to case-fold
     ;; in efs-get-hash-entry below.
     (function
      (lambda (file)
	(let ((ent (car (efs-get-hash-entry file tbl))))
	  (if (or (eq ent t)
		  (and (stringp ent)
		       (file-directory-p (efs-chase-symlinks
					  (expand-file-name
					   ent efs-completion-dir)))))
	      (concat file "/")
	    file))))
     completions)))

(defun efs-file-name-completion (file dir)
  ;; Does file name expansion in remote directories.
  (efs-barf-if-not-directory dir)
  (if (equal file "")
      ""
    (let* ((efs-completion-dir (file-name-as-directory (expand-file-name dir)))
	   (completion-ignore-case
	    (memq (efs-host-type (car (efs-ftp-path efs-completion-dir)))
		  efs-case-insensitive-host-types))
	   (tbl (efs-get-files efs-completion-dir)))
      (efs-set-completion-ignored-pattern)
      (efs-save-match-data
	(or (efs-file-name-completion-1
	     file tbl efs-completion-dir
	     (function efs-file-entry-not-ignored-p))
	    (efs-file-name-completion-1
	     file tbl efs-completion-dir
	     (function efs-file-entry-active-p)))))))

(defun efs-file-name-completion-1 (file tbl dir predicate)
  ;; Internal subroutine for efs-file-name-completion.  Do not call this.
  (let ((bestmatch (try-completion file tbl predicate)))
    (if bestmatch
	(if (eq bestmatch t)
	    (if (file-directory-p (expand-file-name file dir))
		(concat file "/")
	      t)
	  (if (and (eq (try-completion bestmatch tbl predicate) t)
		   (file-directory-p
		    (expand-file-name bestmatch dir)))
	      (concat bestmatch "/")
	    bestmatch)))))

;;;; ----------------------------------------------------------
;;;; Functions for loading lisp.
;;;; ----------------------------------------------------------

;;; jka-load provided ideas here. Thanks, Jay.

(defun efs-load-openp (str suffixes)
  ;; Given STR, searches load-path and efs-load-lisp-extensions
  ;; for the name of a file to load. Returns the full path, or nil
  ;; if none found.
  (let ((path-list (if (file-name-absolute-p str) t load-path))
	root result)
    ;; If there is no load-path, at least try the default directory.
    (or path-list
	(setq path-list (list default-directory)))
    (while (and path-list (null result))
      (if (eq path-list t)
	  (setq path-list nil
		root str)
	(setq root (expand-file-name str (car path-list))
	      path-list (cdr path-list))
	(or (file-name-absolute-p root)
	    (setq root (expand-file-name root default-directory))))
      (let ((suff-list suffixes))
	(while (and suff-list (null result))
	  (let ((try (concat root (car suff-list))))
	    (if (or (not (file-readable-p try))
		    (file-directory-p try))
		(setq suff-list (cdr suff-list))
	      (setq result try))))))
    result))

(defun efs-load (file &optional noerror nomessage nosuffix)
  "Documented as original."
  (let ((filename (efs-load-openp
		   file
		   (if nosuffix '("") efs-load-lisp-extensions))))
    (if (not filename)
	(and (null noerror) (signal 'file-error
				    (list "Cannot open load file %s" file)))
      (let ((parsed (efs-ftp-path filename))
	    (after-load (and (boundp 'after-load-alist)
			     (assoc file after-load-alist))))
	(if parsed
	    (let ((temp (car (efs-make-tmp-name nil (car parsed)))))
	      (unwind-protect
		  (progn
		    (efs-copy-file-internal
		     filename parsed temp nil t nil
		     (format "Getting %s" filename))
		    (or (file-readable-p temp)
			(signal-error
			 'file-error
			 (list "efs-load: temp file %s is unreadable" temp)))
		    (or nomessage
			(message "Loading %s..." file))
		    ;; temp is an absolute filename, so load path
		    ;; won't be searched.
		    (let (after-load-alist)
		      (efs-real-load temp t t t))
		    (or nomessage
			(message "Loading %s...done" file))
		    (if after-load (mapcar 'eval (cdr after-load)))
		    t) ; return t if everything worked
		(efs-del-tmp-name temp)))
	  (prog2
	   (or nomessage
	       (message "Loading %s..." file))
	   (let (after-load-alist)
	     (or (efs-real-load filename noerror t t)
		 (setq after-load nil)))
	   (or nomessage
	       (message "Loading %s...done" file))
	   (if after-load (mapcar 'eval (cdr after-load)))))))))

(defun efs-require (feature &optional filename)
  "Documented as original."
  (if (eq feature 'ange-ftp) (efs-require-scream-and-yell))
  (if (featurep feature)
      feature
    (or filename (setq filename (symbol-name feature)))
    (let ((fullpath (efs-load-openp filename
				    efs-load-lisp-extensions)))
      (if (not fullpath)
	  (signal 'file-error (list "Cannot open load file: %s" filename))
	(let ((parsed (efs-ftp-path fullpath)))
	  (if parsed
	      (let ((temp (car (efs-make-tmp-name nil (car parsed)))))
		(unwind-protect
		    (progn
		      (efs-copy-file-internal
		       fullpath parsed temp nil t nil
		       (format "Getting %s" fullpath))
		      (or (file-readable-p temp)
			  (signal 'file-error
				  (list "efs-require: temp file %s is unreadable" temp)))
		      (efs-real-require feature temp))
		  (efs-del-tmp-name temp)))
	    (efs-real-require feature fullpath)))))))

(defun efs-require-scream-and-yell ()
  ;; Complain if something attempts to load ange-ftp.
  (with-output-to-temp-buffer "*Help*"
    (princ
     "Something tried to load ange-ftp.
EFS AND ANGE-FTP DO NOT WORK TOGETHER.

If the culprit package does need to access ange-ftp internal functions,
then it should be adequate to simply remove the \(require 'ange-ftp\)
line and let efs handle remote file access.  Otherwise, it will need to
be ported to efs.  This may already have been done, and you can find out
by sending an enquiry to efs-help@hplb.hpl.hp.com.

Signalling an error with backtrace will allow you to determine which
package was requiring ange-ftp.\n"))
  (select-window (get-buffer-window "*Help*"))
  (enlarge-window (- (count-lines (point-min) (point-max))
		     (window-height) -1))
  (if (y-or-n-p "Signal error with backtrace? ")
      (let ((stack-trace-on-error t))
	(error "Attempt to require ange-ftp"))))

;;;; -----------------------------------------------------------
;;;; Redefinition of Emacs functions for reading file names.
;;;; -----------------------------------------------------------

(defun efs-unexpand-parsed-filename (host user path)
  ;; Replaces the home directory in path with "~". Returns the unexpanded
  ;; full-path.
  (let* ((path-len (length path))
	 (def-user (efs-get-user host))
	 (host-type (efs-host-type host user))
	 (ignore-case (memq host-type efs-case-insensitive-host-types)))
    (if (> path-len 1)
      (let* ((home (efs-expand-tilde "~" host-type host user))
	     (home-len (length home)))
	(if (and (> path-len home-len)
		 (if ignore-case (string-equal (downcase home)
					       (downcase
						(substring path
							   0 home-len)))
		   (string-equal home (substring path 0 home-len)))
		 (= (aref path home-len) ?/))
	    (setq path (concat "~" (substring path home-len))))))
    (if (if ignore-case (string-equal (downcase user)
				      (downcase def-user))
	  (string-equal user def-user))
	(format efs-path-format-without-user host path)
      (format efs-path-format-string user host path))))

(efs-define-fun efs-abbreviate-file-name (filename &optional ignored-for-now)
  ;; Version of abbreviate-file-name for remote files.
  (efs-save-match-data
    (let ((tail directory-abbrev-alist))
      (while tail
	(if (string-match (car (car tail)) filename)
	    (setq filename
		  (concat (cdr (car tail))
			  (substring filename (match-end 0)))))
	(setq tail (cdr tail)))
      (apply 'efs-unexpand-parsed-filename (efs-ftp-path filename)))))

(defun efs-default-dir-function ()
  (let ((parsed (efs-ftp-path default-directory))
	(dd default-directory))
    (if parsed
	(efs-save-match-data
	  (let ((tail directory-abbrev-alist))
	    (while tail
	      (if (string-match (car (car tail)) dd)
		  (setq dd (concat (cdr (car tail))
				   (substring dd (match-end 0)))
			parsed nil))
	      (setq tail (cdr tail)))
	    (apply 'efs-unexpand-parsed-filename
		   (or parsed (efs-ftp-path dd)))))
      default-directory)))

(defun efs-re-read-dir (&optional dir)
  "Forces a re-read of the directory DIR.
If DIR is omitted then it defaults to the directory part of the contents
of the current buffer. This is so this function can be caled from the
minibuffer."
  (interactive)
  (if dir
      (setq dir (expand-file-name dir))
    (setq dir (file-name-directory (expand-file-name (buffer-string)))))
  (let ((parsed (efs-ftp-path dir)))
    (if parsed
	(let ((efs-ls-uncache t))
	  (efs-del-hash-entry (efs-canonize-file-name dir)
			      efs-files-hashtable)
	  (efs-get-files dir t)))))

;;;; ---------------------------------------------------------------
;;;; Creation and deletion of files and directories.
;;;; ---------------------------------------------------------------

(defun efs-delete-file (file)
  ;; Deletes remote files.
  (let* ((file (expand-file-name file))
	 (parsed (efs-ftp-path file))
	 (host (car parsed))
	 (user (nth 1 parsed))
	 (host-type (efs-host-type host user))
	 (path (nth 2 parsed))
	 (abbr (efs-relativize-filename file))
	 (result (efs-send-cmd host user (list 'delete path)
			       (format "Deleting %s" abbr))))
    (if (car result)
	(signal 'ftp-error
		(list "Removing old name"
		      (format "FTP Error: \"%s\"" (nth 1 result))
		      file)))
    (efs-delete-file-entry host-type file)))

(defun efs-make-directory-internal (dir)
  ;; version of make-directory-internal for remote directories.
  (if (file-exists-p dir)
      (error "Cannot make directory %s: file already exists" dir)
    (let* ((parsed (efs-ftp-path dir))
	   (host (nth 0 parsed))
	   (user (nth 1 parsed))
	   (host-type (efs-host-type host user))
	   ;; Some ftp's on unix machines (at least on Suns)
	   ;; insist that mkdir take a filename, and not a
	   ;; directory-name name as an arg. Argh!! This is a bug.
	   ;; Non-unix machines will probably always insist
	   ;; that mkdir takes a directory-name as an arg
	   ;; (as the ftp man page says it should).
	   (path (if (or (memq host-type efs-unix-host-types)
			 (memq host-type '(os2 dos)))
		     (efs-internal-directory-file-name (nth 2 parsed))
		   (efs-internal-file-name-as-directory
		    host-type (nth 2 parsed))))
	   (abbr (efs-relativize-filename dir))
	   (result (efs-send-cmd host user
				 (list 'mkdir path)
				 (format "Making directory %s"
					 abbr))))
      (if (car result)
	  (efs-error host user
		     (format "Could not make directory %s: %s" dir
			     (nth 1 result))))
      (efs-add-file-entry host-type dir t nil user))))

;; V19 calls this function delete-directory. It used to be called
;; remove-directory.

(defun efs-delete-directory (dir)
  ;; Version of delete-directory for remote directories.
  (if (file-directory-p dir)
      (let* ((parsed (efs-ftp-path dir))
	     (host (nth 0 parsed))
	     (user (nth 1 parsed))
	     (host-type (efs-host-type host user))
	     ;; Some ftp's on unix machines (at least on Suns)
	     ;; insist that rmdir take a filename, and not a
	     ;; directory-name name as an arg. Argh!! This is a bug.
	     ;; Non-unix machines will probably always insist
	     ;; that rmdir takes a directory-name as an arg
	     ;; (as the ftp man page says it should).
	     (path 
	      (if (or (memq host-type efs-unix-host-types)
		      (memq host-type '(os2 dos)))
		  (efs-internal-directory-file-name (nth 2 parsed))
		(efs-internal-file-name-as-directory
		 host-type (nth 2 parsed))))
	     (abbr (efs-relativize-filename dir))
	     (result (efs-send-cmd host user
				   (list 'rmdir path)
				   (format "Deleting directory %s" abbr))))
	(if (car result)
	    (efs-error host user
		       (format "Could not delete directory %s: %s"
			       dir (nth 1 result))))
	(efs-delete-file-entry host-type dir t))
    (error "Not a directory: %s" dir)))

(defun efs-file-local-copy (file)
  ;; internal function for diff.el (dired 6.3 or later)
  ;; Makes a temp file containing the contents of file.
  ;; returns the name of the tmp file created, or nil if none is.
  ;; This function should have optional cont and nowait args.
  (let* ((file (expand-file-name file))
	 (tmp (car (efs-make-tmp-name nil  (car (efs-ftp-path file))))))
    (efs-copy-file-internal file (efs-ftp-path file)
			    tmp nil t nil (format "Getting %s" file))
    tmp))

(defun efs-diff/grep-del-temp-file (temp)
  ;; internal function for diff.el and grep.el
  ;; if TEMP is non-nil, deletes the temp file TEMP.
  ;; if TEMP is nil, does nothing.
  (and temp
       (efs-del-tmp-name temp)))
	    
;;;; ------------------------------------------------------------
;;;; File copying support...
;;;; ------------------------------------------------------------

;;;  - totally re-written 6/24/92.
;;;  - re-written again 9/3/93
;;;  - and again 14/4/93
;;;  - and again 17/8/93

(if (not (get 'file-already-exists 'error-conditions))
    (let ((conds (get 'file-error 'error-conditions)))
      (put 'file-already-exists (cons 'file-already-exists conds))))

(defun efs-barf-or-query-if-file-exists (absname querystring interactive)
  (if (file-exists-p absname)
      (if (not interactive)
	  (signal 'file-already-exists
		  (list (format "File %s already exists." absname)))
	(if (not (yes-or-no-p (format "File %s already exists; %s anyway? "
				      absname querystring)))
	    (signal 'file-already-exists
		    (list (format "File %s already exists." absname)))))))

(defun efs-concatenate-files (file1 file2)
  ;; Concatenates file1 to file2. Both must be local files.
  ;; Needed because the efs version of copy-file understands
  ;; ok-if-already-exists = 'append
  (or (file-readable-p file1)
      (signal 'file-error
	      (list (format "Input file %s not readable." file1))))
  (or (file-writable-p file2)
      (signal 'file-error
	      (list (format "Output file %s not writable." file2))))
  (let ((default-directory exec-directory))
    (call-process "sh" nil nil nil "-c" (format "cat %s >> %s" file1 file2))))

(defun efs-copy-add-file-entry (newname host-type user size append)
  ;; Add an entry in `efs-files-hashtable' for a file newly created via a copy.
  (if (eq size -1) (setq size nil))
  (if append
      (let ((ent (efs-get-file-entry newname)))
	(if (and ent (null (car ent)))
	    (if (and size (numberp (nth 1 ent)))
		(setcar (cdr ent) (+ size (nth 1 ent)))
	      (setcar (cdr ent) nil))
	  ;; If the ent is a symlink or directory, don't overwrite that entry.
	  (if (null ent)
	      (efs-add-file-entry host-type newname nil nil nil))))
    (efs-add-file-entry host-type newname nil size user)))
  
(defun efs-copy-remote-to-remote (f-host-type f-host f-user f-path filename
					      t-host-type t-host t-user
					      t-path newname append msg cont
					      nowait xfer-type)
;; Use a 3rd data connection to copy from F-HOST for F-USER to T-HOST
;; for T-USER.
  (if (efs-get-host-property t-host 'pasv-failed)
      ;; PASV didn't work before, don't try again.
      (if cont (efs-call-cont cont 'failed "" ""))
    (or xfer-type
	(setq xfer-type (efs-xfer-type f-host-type filename
				       t-host-type newname)))
    (efs-send-cmd
     t-host t-user '(quote pasv) nil nil
     (efs-cont (pasv-result pasv-line pasv-cont-lines)
	 (cont nowait f-host-type f-host f-user f-path filename
	       t-host-type t-host t-user t-path newname xfer-type msg append)
       (efs-save-match-data
	 (if (or pasv-result
		 (not (string-match efs-pasv-msgs pasv-line)))
	     (progn
	       (efs-set-host-property t-host 'pasv-failed t)
	       (if cont
		   (efs-call-cont
		    cont (or pasv-result 'failed) pasv-line pasv-cont-lines)))
	   (let ((address (substring pasv-line (match-beginning 1)
				     (match-end 1))))
	     (efs-send-cmd
	      f-host f-user
	      (list 'quote 'port address) nil nil
	      (efs-cont (port-result port-line port-cont-lines)
		  (cont f-host f-user f-host-type f-path filename
			xfer-type msg)
		(if port-result
		    (if cont
			(efs-call-cont
			 cont port-result port-line port-cont-lines)
		      (efs-error f-host f-user
				 (format "PORT failed for %s: %s"
					 filename port-line)))
		  (progn
		    (efs-send-cmd
		     f-host f-user
		     (list 'quote 'retr f-path xfer-type)
		     msg nil
		     (efs-cont (retr-result retr-line retr-cont-lines)
			 (cont f-host f-user f-path)
		       (and retr-result
			    (null cont)
			    (efs-error
			     f-host f-user
			     (format "RETR failed for %s: %s"
				     f-path retr-line)))
		       (if cont (efs-call-cont
				 cont retr-result retr-line retr-cont-lines)))
		     1)
		    (efs-send-cmd
		     t-host t-user
		     (list 'quote (if append 'appe 'stor) t-path xfer-type)
		     nil nil
		     (efs-cont (stor-result stor-line stor-cont-lines)
			 (t-host t-user t-path t-host-type newname filename
				 append)
		       (if stor-result
			   (efs-error
			    t-host t-user (format "%s failed for %s: %s"
						  (if append "APPE" "STOR")
						  t-path stor-line))
			 (efs-copy-add-file-entry
			  newname t-host-type t-user
			  (nth 1 (efs-get-file-entry filename)) append)))
		     (if (eq nowait t) 1 nowait)))))
	      1) ; can't ever wait on this command.
	     ))))
     nowait)))

(defun efs-copy-on-remote (host user host-type filename newname filename-parsed
				newname-parsed keep-date append-p msg cont
				nowait xfer-type)
  ;; Uses site exec to copy the file on a remote host
  (let ((exec-cp (efs-get-host-property host 'exec-cp)))
    (if (or append-p
	    (not (memq host-type efs-unix-host-types))
	    (efs-get-host-property host 'exec-failed)
	    (eq exec-cp 'failed))
	(efs-copy-via-temp filename filename-parsed newname newname-parsed
			   append-p keep-date msg cont nowait xfer-type)
      (if (eq exec-cp 'works)
	  (efs-send-cmd
	   host user
	   (list 'quote 'site 'exec
		 (format "cp %s%s %s" (if keep-date "-p " "")
			 (nth 2 filename-parsed) (nth 2 newname-parsed)))
	   msg nil
	   (efs-cont (result line cont-lines) (host user filename newname
						    host-type filename-parsed
						    newname-parsed
						    keep-date append-p msg cont
						    xfer-type nowait)
	     (if result
		 (progn
		   (efs-set-host-property host 'exec-failed t)
		   (efs-copy-via-temp filename filename-parsed newname
				      newname-parsed append-p keep-date
				      nil cont nowait xfer-type))
	       (efs-save-match-data
		 (if (string-match "\n200-\\([^\n]*\\)" cont-lines)
		     (let ((err (substring cont-lines (match-beginning 1)
					   (match-end 1))))
		       (if cont
			   (efs-call-cont cont 'failed err cont-lines)
			 (efs-error host user err)))
		   (efs-copy-add-file-entry
		    newname host-type user
		    (nth 7 (efs-file-attributes filename)) nil)
		   (if cont (efs-call-cont cont nil line cont-lines))))))
	   nowait)
	(message "Checking for cp executable on %s..." host)
	(efs-send-cmd
	 host user (list 'quote 'site 'exec "cp / /") nil nil
	 (efs-cont (result line cont-lines) (host user filename newname
						  host-type filename-parsed
						  newname-parsed
						  keep-date append-p msg cont
						  xfer-type nowait)
	   (efs-save-match-data
	     (if (string-match "\n200-" cont-lines)
		 (efs-set-host-property host 'exec-cp 'works)
	       (efs-set-host-property host 'exec-cp 'failed)))
	   (efs-copy-on-remote host user host-type filename newname
			       filename-parsed newname-parsed keep-date
			       append-p msg cont nowait xfer-type))
	 nowait)))))

(defun efs-copy-via-temp (filename filename-parsed newname newname-parsed
				   append keep-date msg cont nowait xfer-type)
  ;; Copies from FILENAME to NEWNAME via a temp file.
  (let* ((temp (car (if (efs-use-gateway-p (car filename-parsed) t)
			(efs-make-tmp-name (car filename-parsed)
					   (car newname-parsed))
		      (efs-make-tmp-name (car newname-parsed)
					 (car filename-parsed)))))
	 (temp-parsed (efs-ftp-path temp)))
    (or xfer-type (setq xfer-type
			(efs-xfer-type
			 (efs-host-type (car filename-parsed)) filename
			 (efs-host-type (car newname-parsed)) newname
			 t)))
    (efs-copy-file-internal
     filename filename-parsed temp temp-parsed t nil (if (eq 0 msg) 2 msg)
     (efs-cont (result line cont-lines) (newname newname-parsed temp
						 temp-parsed append msg cont
						 nowait xfer-type)
       (if result
	   (progn
	     (efs-del-tmp-name temp)
	     (if cont
		 (efs-call-cont cont result line cont-lines)
	       (signal 'ftp-error
		       (list "Opening input file"
			     (format "FTP Error: \"%s\" " line) filename))))
	 (efs-copy-file-internal
	  temp temp-parsed newname newname-parsed (if append 'append t) nil
	  (if (eq msg 0) 1 msg)
	  (efs-cont (result line cont-lines) (temp newname cont)
	    (efs-del-tmp-name temp)
	    (if cont
		(efs-call-cont cont result line cont-lines)
	      (if result
		  (signal 'ftp-error
			  (list "Opening output file"
				(format "FTP Error: \"%s\" " line) newname)))))
	  nowait xfer-type)))
     nowait xfer-type)))

(defun efs-copy-file-internal (filename filename-parsed newname newname-parsed
					ok-if-already-exists keep-date
					&optional msg cont nowait xfer-type)
  ;; Internal function for copying a file from FILENAME to NEWNAME.
  ;; FILENAME-PARSED and NEWNAME-PARSED are the lists obtained by parsing
  ;; FILENAME and NEWNAME with efs-ftp-path.
  ;; If OK-IF-ALREADY-EXISTS is nil, then existing files will not be
  ;; overwritten.
  ;; If it is a number, then the user will be prompted about overwriting.
  ;; If it eq 'append, then an existing file will be appended to.
  ;; If it has anyother value, then existing files will be silently
  ;; overwritten.
  ;; If KEEP-DATE is t then we will attempt to reatin the date of the
  ;; original copy of the file. If this is a string, the modtime of the
  ;; NEWNAME will be set to this date. Must be in touch -t format.
  ;; If MSG is nil, then the copying will be done silently.
  ;; If it is a string, then that will be the massage displayed while copying.
  ;; If it is 0, then a suitable default message will be computed.
  ;; If it is 1, then a suitable default will be computed, assuming
  ;; that FILENAME is a temporary file, whose name is not suitable to use
  ;; in a status message.
  ;; If it is 2, then a suitable default will be used, assuming that
  ;; NEWNAME is a temporary file.
  ;; CONT is a continuation to call after completing the copy.
  ;; The first two args are RESULT and LINE, the result symbol and status
  ;; line of the FTP command. If more than one ftp command has been used,
  ;; then these values for the last FTP command are given.
  ;; NOWAIT can be either nil, 0, 1, t. See `efs-send-cmd' for an explanation.
  ;; XFER-TYPE is the transfer type to use for transferring the files.
  ;; If this is nil, than a suitable transfer type is computed.
  ;; Does not call expand-file-name. Do that yourself.

  ;; check to see if we can overwrite
  (if (or (not ok-if-already-exists)
	  (numberp ok-if-already-exists))
      (efs-barf-or-query-if-file-exists
       newname "copy to it" (numberp ok-if-already-exists)))
  (if (null (or filename-parsed newname-parsed))
      ;; local to local copy
      (progn
	(if (eq ok-if-already-exists 'append)
	    (efs-concatenate-files filename newname)
	  (copy-file filename newname ok-if-already-exists keep-date))
	(if cont
	    (efs-call-cont cont nil "Copied locally" "")))
    (let* ((f-host (car filename-parsed))
	   (f-user (nth 1 filename-parsed))
	   (f-path (nth 2 filename-parsed))
	   (f-host-type (efs-host-type f-host f-user))
	   (f-gate-p (efs-use-gateway-p f-host t))
	   (t-host (car newname-parsed))
	   (t-user (nth 1 newname-parsed))
	   (t-path (nth 2 newname-parsed))
	   (t-host-type (efs-host-type t-host t-user))
	   (t-gate-p (efs-use-gateway-p t-host t))
	   (append-p (eq ok-if-already-exists 'append))
	   gatename)

      (if (and (eq keep-date t) (null newname-parsed))
	  ;; f-host must be remote now.
	  (setq keep-date filename))
	  
      (cond
       
       ;; Check to see if we can do a PUT
       ((or
	 (and (null f-host)
	      (or (null t-gate-p)
		  (setq gatename (efs-local-to-gateway-filename filename))))
	 (and t-gate-p
	      f-host
	      (string-equal (downcase f-host) (downcase efs-gateway-host))
	      (if (memq f-host-type efs-case-insensitive-host-types)
		  (string-equal (downcase f-user)
				(downcase (efs-get-user efs-gateway-host)))
		(string-equal f-user (efs-get-user efs-gateway-host)))))
	(or f-host (let (file-name-handler-alist)
		     (if (file-exists-p filename)
			 (cond
			  ((file-directory-p filename)
			   (signal 'file-error
				   (list "Non-regular file"
					 "is a directory" filename)))
			  ((not (file-readable-p filename))
			   (signal 'file-error
				   (list "Opening input file"
					 "permission denied" filename))))
		       (signal 'file-error
			       (list "Opening input file"
				     "no such file or directory" filename)))))
	(or xfer-type
	    (setq xfer-type
		  (efs-xfer-type f-host-type filename t-host-type newname)))
	(let ((size (and (or (null f-host-type)
			     (efs-file-entry-p filename))
			 (nth 7 (file-attributes filename)))))
	  ;; -1 is a bogus size for remote files
	  (if (eq size -1) (setq size nil))
	  (efs-send-cmd
	   t-host t-user
	   (list (if append-p 'append 'put)
		 (if f-host
		     f-path
		   (or gatename filename))
		 t-path
		 xfer-type)
	   (cond ((eq msg 2)
		  (concat (if append-p "Appending " "Putting ")
			  (efs-relativize-filename filename)))
		 ((eq msg 1)
		  (concat (if append-p "Appending " "Putting ")
			  (efs-relativize-filename newname)))
		 ((eq msg 0)
		  (concat (if append-p "Appending " "Copying ")
			  (efs-relativize-filename filename)
			  " to "
			  (efs-relativize-filename
			   newname (file-name-directory filename) filename)))
		 (t msg))
	   (and size (list 'efs-set-xfer-size t-host t-user size))
	   (efs-cont (result line cont-lines) (newname t-host-type t-user size
						       append-p cont)
	     (if result
		 (if cont
		     (efs-call-cont cont result line cont-lines)
		   (signal 'ftp-error
			   (list "Opening output file"
				 (format "FTP Error: \"%s\" " line) newname)))
	       ;; add file entry
	       (efs-copy-add-file-entry newname t-host-type t-user
					size append-p)
	       (if cont
		   (efs-call-cont cont result line cont-lines))))
	   nowait)))
       
       ;; Check to see if we can do a GET
       ((and
	 ;; I think that giving the append arg, will cause this function
	 ;; to make a temp file, recursively call itself, and append the temp
	 ;; file to the local file. Hope it works out...
	 (null append-p)
	 (or
	  (and (null t-host)
	       (or (null f-gate-p)
		   (setq gatename (efs-local-to-gateway-filename newname))))
	  (and f-gate-p
	       t-host
	       (string-equal (downcase t-host) (downcase efs-gateway-host))
	       (if (memq t-host-type efs-case-insensitive-host-types)
		   (string-equal (downcase t-user)
				 (downcase (efs-get-user efs-gateway-host)))
		 (string-equal t-user (efs-get-user efs-gateway-host))))))
	(or t-host (let (file-name-handler-alist)
		     (cond ((not (file-writable-p newname))
			    (signal 'file-error
				    (list "Opening output file"
					  "permission denied" newname)))
			   ((file-directory-p newname)
			    (signal 'file-error
				    (list "Opening output file"
					  "is a directory" newname))))))
	(or xfer-type
	    (setq xfer-type
		  (efs-xfer-type f-host-type filename t-host-type newname)))
	(let ((size (and (or (null f-host-type)
			     (efs-file-entry-p filename))
			 (nth 7 (file-attributes filename)))))
	  ;; -1 is a bogus size for remote files.
	  (if (eq size -1) (setq size nil))
	  (efs-send-cmd
	   f-host f-user
	   (list 'get
		 f-path
		 (if t-host
		     t-path
		   (or gatename newname))
		 xfer-type)
	   (cond ((eq msg 0)
		  (concat "Copying "
			  (efs-relativize-filename filename)
			  " to "
			  (efs-relativize-filename
			   newname (file-name-directory filename) filename)))
		 ((eq msg 2)
		  (concat "Getting " (efs-relativize-filename filename)))
		 ((eq msg 1)
		  (concat "Getting " (efs-relativize-filename newname)))
		 (t msg))
	   ;; If the server emits a efs-xfer-size-msgs, it will over-ride this.
	   ;; With no xfer msg, this is will do the job.
	   (and size (list 'efs-set-xfer-size f-host f-user size))
	   (efs-cont (result line cont-lines) (filename newname size
							t-host-type t-user
							cont keep-date)
	     (if result
		 (if cont
		     (efs-call-cont cont result line cont-lines)
		   (signal 'ftp-error
			   (list "Opening input file"
				 (format "FTP Error: \"%s\" " line) filename)))
	       ;; Add a new file entry, if relevant.
	       (if t-host-type
		   ;; t-host will be equal to efs-gateway-host, if t-host-type
		   ;; is non-nil.
		   (efs-copy-add-file-entry newname t-host-type
					    t-user size nil))
	       (if (and (null t-host-type) (stringp keep-date))
		   (efs-set-mdtm-of
		    filename newname
		    (and cont
			 (efs-cont (result1 line1 cont-lines1) (result
								line cont-lines
								cont)
			   (efs-call-cont cont result line cont-lines))))
		 (if cont
		     (efs-call-cont cont result line cont-lines)))))
	   nowait)))

       ;; Can we do a EXEC cp?
       ((and t-host f-host
	     (string-equal (downcase t-host) (downcase f-host))
	     (if (memq t-host-type efs-case-insensitive-host-types)
		 (string-equal (downcase t-user) (downcase f-user))
	       (string-equal t-user f-user)))
	(efs-copy-on-remote
	 t-host t-user t-host-type filename newname filename-parsed
	 newname-parsed keep-date append-p
	 (cond ((eq msg 0)
		(concat "Copying "
			(efs-relativize-filename filename)
			" to "
			(efs-relativize-filename
			 newname (file-name-directory filename) filename)))
	       ((eq msg 1)
		(concat "Copying " (efs-relativize-filename newname)))
	       ((eq msg 2)
		(concat "Copying " (efs-relativize-filename filename)))
	       (t msg))
	 cont nowait xfer-type))

       ;; Try for a copy with PASV
       ((and t-host f-host
	     (not (and (string-equal (downcase t-host) (downcase f-host))
		       (if (memq t-host-type efs-case-insensitive-host-types)
			   (string-equal (downcase t-user) (downcase f-user))
			 (string-equal t-user f-user))))
	     (or
	      (and efs-gateway-host
		   ;; The gateway should be able to talk to anything.
		   (let ((gh (downcase efs-gateway-host)))
		     (or (string-equal (downcase t-host) gh)
			 (string-equal (downcase f-host) gh))))
	      (efs-save-match-data
		(eq (null (string-match efs-local-host-regexp t-host))
		    (null (string-match efs-local-host-regexp f-host))))))
	(efs-copy-remote-to-remote
	 f-host-type f-host f-user f-path filename
	 t-host-type t-host t-user t-path newname
	 append-p
	 (cond ((eq msg 0)
		(concat "Copying "
			(efs-relativize-filename filename)
			" to "
			(efs-relativize-filename
			 newname (file-name-directory filename) filename)))
	       ((eq msg 1)
		(concat "Copying " (efs-relativize-filename newname)))
	       ((eq msg 2)
		(concat "Copying " (efs-relativize-filename filename)))
	       (t msg))
	 (efs-cont (result line cont-lines)
	     (filename filename-parsed newname newname-parsed
		       append-p keep-date msg cont nowait xfer-type)
	   (if result
	       ;; PASV didn't work. Do things the old-fashioned
	       ;; way.
	       (efs-copy-via-temp
		filename filename-parsed newname newname-parsed
		append-p keep-date msg cont nowait xfer-type)
	     (if cont
		 (efs-call-cont cont result line cont-lines))))
	 nowait xfer-type))
       
       ;; Can't do anything direct. Divide and conquer.
       (t
	(efs-copy-via-temp filename filename-parsed newname newname-parsed
			   append-p keep-date msg cont nowait xfer-type))))))

(defun efs-copy-file (filename newname &optional ok-if-already-exists
			       keep-date nowait)
  ;; Version of copy file for remote files. Actually, will also work
  ;; for local files too, since efs-copy-file-internal can copy anything.
  ;; If called interactively, copies asynchronously.
  (setq filename (expand-file-name filename)
	newname (expand-file-name newname))
  (if (eq ok-if-already-exists 'append)
      (setq ok-if-already-exists t))
  (efs-copy-file-internal filename (efs-ftp-path filename)
			  newname (efs-ftp-path newname)
			  ok-if-already-exists keep-date 0 nil nowait))

;;;; ------------------------------------------------------------
;;;; File renaming support.
;;;; ------------------------------------------------------------

(defun efs-rename-get-file-list (dir ent)
  ;; From hashtable ENT for DIR returns a list of all files except "."
  ;; and "..".
  (let (list)
    (efs-map-hashtable
     (function
      (lambda (key val)
	(or (string-equal "." key) (string-equal ".." key)
	    (setq list
		  (cons (expand-file-name key dir) list)))))
     ent)
    list))

(defun efs-rename-get-files (dir cont nowait)
  ;; Obtains a list of files in directory DIR (except . and ..), and applies
  ;; CONT to the list. Doesn't return anything useful.
  (let* ((dir (file-name-as-directory dir))
	 (ent (efs-get-files-hashtable-entry dir)))
    (if ent
	(efs-call-cont cont (efs-rename-get-file-list dir ent))
      (efs-ls
       dir (efs-ls-guess-switches) t nil t nowait
       (efs-cont (listing) (dir cont)
	 (efs-call-cont
	  cont (and listing
		    (efs-rename-get-file-list
		     dir (efs-get-files-hashtable-entry dir)))))))))

(defun efs-rename-get-local-file-tree (dir)
  ;; Returns a list of the full directory tree under DIR, for DIR on the
  ;; local host.  The list is in tree order.
  (let ((res (list dir)))
    (mapcar
     (function
      (lambda (file)
	(if (file-directory-p file)
	    (nconc res (delq nil (mapcar
				  (function
				   (lambda (f)
				     (and (not (string-equal "." f))
					  (not (string-equal ".." f))
					  (expand-file-name f file))))
				  (directory-files file)))))))
     res)
    res))

(defun efs-rename-get-remote-file-tree (next curr total cont nowait)
  ;; Builds a hierarchy of files.
  ;; NEXT is the next level so far.
  ;; CURR are unprocessed files in the current level.
  ;; TOTAL is the processed files so far.
  ;; CONT is a cont. function called on the total list after all files
  ;;      are processed.
  ;; NOWAIT non-nil means run asynch.
  (or curr (setq curr next
		 next nil))
  (if curr
      (let ((file (car curr)))
	(setq curr (cdr curr)
	      total (cons file total))
	(if (file-directory-p file)
	    (efs-rename-get-files
	     file
	     (efs-cont (list) (next curr total cont nowait)
	       (efs-rename-get-remote-file-tree (nconc next list)
					   curr total cont nowait))
	     nowait)
	  (efs-rename-get-remote-file-tree next curr total cont nowait)))
    (efs-call-cont cont (nreverse total))))

(defun efs-rename-make-targets (files from-dir-len to-dir host user host-type
				      cont nowait)
  ;; Make targets (copy a file or make a subdir) on local or host
  ;; for the files in list. Afterwhich, call CONT.
  (if files
      (let* ((from (car files))
	     (files (cdr files))
	     (to (concat to-dir (substring from from-dir-len))))
	(if (file-directory-p from)
	    (if host-type
		(let ((dir (nth 2 (efs-ftp-path to))))
		  (or (memq host-type efs-unix-host-types)
		      (memq host-type '(dos os2))
		      (setq dir (efs-internal-file-name-as-directory nil dir)))
		  (efs-send-cmd
		   host user (list 'mkdir dir)
		   (format "Making directory %s" (efs-relativize-filename to))
		   nil
		   (efs-cont (res line cont-lines) (to files from-dir-len
						       to-dir host user
						       host-type cont nowait)
		     (if res
			 (if cont
			     (efs-call-cont cont res line cont-lines)
			   (signal 'ftp-error
				   (list "Making directory"
					 (format "FTP Error: \"%s\"" line)
					 to)))
		       (efs-rename-make-targets
			files from-dir-len to-dir host user
			host-type cont nowait)))
		   nowait))
	      (condition-case nil
		  (make-directory-internal to)
		(error (efs-call-cont
			cont 'failed (format "Failed to mkdir %s" to) "")))
	      (efs-rename-make-targets
	       files from-dir-len to-dir host user host-type cont nowait))
	  (efs-copy-file-internal
	   from (efs-ftp-path from) to (and host-type (efs-ftp-path to)) nil t
	   (format "Renaming %s to %s" (efs-relativize-filename from)
		   (efs-relativize-filename to))
	   (efs-cont (res line cont-lines) (from to files from-dir-len to-dir
						 host user host-type cont
						 nowait)
	     (if res
		 (if cont
		     (efs-call-cont cont res line cont-lines)
		   (signal 'ftp-error
			   (list "Renaming"
				 (format "FTP Error: \"%s\"" line) from to)))
	       (efs-rename-make-targets
		files from-dir-len to-dir host user host-type cont nowait)))
	   nowait)))
    (if cont (efs-call-cont cont nil "" ""))))

(defun efs-rename-delete-on-local (files)
  ;; Delete the files FILES, and then run CONT.
  ;; FILES are assumed to be in inverse tree order.
  (message "Deleting files...")
  (mapcar
   (function
    (lambda (f)
      (condition-case nil
	  (if (file-directory-p f)
	      (delete-directory f)
	    (delete-file f))
	(file-error nil)))) ; don't complain if the file is already gone.
   files)
  (message "Deleting files...done"))

(defun efs-rename-delete-on-remote (files host user host-type cont nowait)
  ;; Deletes the list FILES on a remote host.  When done calls CONT.
  ;; FILES is assumed to be in reverse tree order.
  (if files
      (let* ((f (car files))
	     (rf (nth 2 (efs-ftp-path f))))
	(progn
	  (setq files (cdr files))
	  (if (file-directory-p f)
	      (let ((rf (if (memq host-type (append efs-unix-host-types
						    '(dos os2)))
			    (efs-internal-directory-file-name f)
			  (efs-internal-file-name-as-directory nil f))))
		
		(efs-send-cmd
		 host user (list 'rmdir rf)
		 (concat "Deleting directory " (efs-relativize-filename f))
		 nil
		 (efs-cont (res line cont-lines) (f files host user host-type
						    cont nowait)
		   (if (and res
			    (efs-save-match-data
			      (not (string-match "^550 " line))))
		       (if cont
			   (efs-call-cont cont res line cont-lines)
			 (signal 'ftp-error
				 (list "Deleting directory"
				       (format "FTP Error: \"%s\"" line)
				       f)))
		     (efs-rename-delete-on-remote
		      files host user host-type cont nowait)))
		 nowait))
	    (efs-send-cmd
	     host user (list 'delete rf)
	     (concat "Deleting " rf)
	     nil
	     (efs-cont (res line cont-lines) (f files host user host-type
						cont nowait)
	       (if (and res
			(efs-save-match-data
			  (not (string-match "^550 " line))))
		   (if cont
		       (efs-call-cont cont res line cont-lines)
		     (signal 'ftp-error
			     (list "Deleting"
				   (format "FTP Error: \"%s\"" line)
				   f)))
		 (efs-rename-delete-on-remote
		  files host user host-type cont nowait)))
	     nowait))))
    (if cont (efs-call-cont cont nil "" ""))))

(defun efs-rename-on-remote (host user old-path new-path old-file new-file
				  msg nowait cont)
  ;; Run a rename command on the remote server.
  ;; OLD-PATH and NEW-PATH are in full efs syntax.
  ;; OLD-FILE and NEW-FILE are the remote full pathnames, not in efs syntax.
  (efs-send-cmd
   host user (list 'rename old-file new-file) msg nil
   (efs-cont (result line cont-lines) (cont old-path new-path host)
     (if result
	 (progn
	   (or (and (>= (length line) 4)
		    (string-equal "550 " (substring line 0 4)))
	       (efs-set-host-property host 'rnfr-failed t))
	   (if cont
	       (efs-call-cont cont result line cont-lines)
	     (signal 'ftp-error
		     (list "Renaming"
			   (format "FTP Error: \"%s\"" line)
			   old-path new-path))))
       (let ((entry (efs-get-file-entry old-path))
	     (host-type (efs-host-type host))
	     ;; If no file entry, do extra work on the hashtable,
	     ;; rather than force a listing.
	     (dir-p (or (not (efs-file-entry-p old-path))
			(file-directory-p old-path))))
	 (apply 'efs-add-file-entry host-type new-path
		(eq (car entry) t) (cdr entry))
	 (efs-delete-file-entry host-type old-path)
	 (if dir-p
	     (let* ((old (efs-canonize-file-name
			  (file-name-as-directory old-path)))
		    (new (efs-canonize-file-name
			  (file-name-as-directory new-path)))
		    (old-len (length old))
		    (new-tbl (efs-make-hashtable
			      (length efs-files-hashtable))))
	       (efs-map-hashtable
		(function
		 (lambda (key val)
		   (if (and (>= (length key) old-len)
			    (string-equal (substring key 0 old-len)
					  old))
		       (efs-put-hash-entry
			(concat new (substring key old-len)) val new-tbl)
		     (efs-put-hash-entry key val new-tbl))))
		efs-files-hashtable)
	       (setq efs-files-hashtable new-tbl)))
	 (if cont (efs-call-cont cont result line cont-lines)))))
   nowait))

(defun efs-rename-local-to-remote (filename newname newname-parsed
					    msg cont nowait)
  ;; Renames a file from the local host to a remote host.
  (if (file-directory-p filename)
      (let* ((files (efs-rename-get-local-file-tree filename))
	     (to-dir (directory-file-name newname))
	     (filename (directory-file-name filename))
	     (len (length filename))
	     (t-parsed (efs-ftp-path to-dir))
	     (host (car t-parsed))
	     (user (nth 1 t-parsed))
	     (host-type (efs-host-type host)))
	;; MSG is never passed here, instead messages are constructed
	;; internally.  I don't know how to use a single message
	;; in a function which sends so many FTP commands.
	(efs-rename-make-targets
	 files len to-dir host user host-type
	 (efs-cont (result line cont-lines) (files filename newname cont)
	   (if result
	       (if cont
		   (efs-call-cont cont result line cont-lines)
		 (signal 'ftp-error
			 (list "Renaming" (format "FTP Error: \"%s\"" line)
			       filename newname)))
	     (efs-rename-delete-on-local (nreverse files))
	     (if cont (efs-call-cont cont result line cont-lines))))
	 nowait))
    (efs-copy-file-internal
     filename nil newname newname-parsed t t msg
     (efs-cont (result line cont-lines) (filename cont)
       (if result
	   (if cont
	       (efs-call-cont cont result line cont-lines)
	     (signal 'ftp-error
		     (list "Renaming"
			   (format "FTP Error: \"%s\"" line)
			   filename newname)))
	 (condition-case nil
	     (delete-file filename)
	   (error nil))
	 (if cont (efs-call-cont cont result line cont-lines))))
     nowait)))

(defun efs-rename-from-remote (filename filename-parsed newname newname-parsed
					msg cont nowait)
  (let ((f-host (car filename-parsed))
	(f-user (nth 1 filename-parsed))
	(fast-nowait (if (eq nowait t) 1 nowait)))
    (if (file-directory-p filename)
	(let* ((t-host (car newname-parsed))
	       (t-user (nth 1 newname-parsed))
	       (t-host-type (and t-host (efs-host-type t-host)))
	       (f-host-type (efs-host-type f-host)))
	  (efs-rename-get-remote-file-tree
	   nil (list filename) nil
	   (efs-cont (list) (filename filename-parsed newname t-host t-user
				      t-host-type f-host f-user f-host-type
				      cont fast-nowait)
	     (efs-rename-make-targets
	      list (length filename) newname t-host t-user t-host-type
	      (efs-cont (res line cont-lines) (filename newname f-host f-user
							f-host-type list cont
							fast-nowait)
		(if res
		    (if cont
			(efs-call-cont cont res line cont-lines)
		      (signal 'ftp-error
			      (list "Renaming"
				    (format "FTP Error: \"%s\"" line)
				    filename newname)))
		  (efs-rename-delete-on-remote
		   (nreverse list) f-host f-user f-host-type cont
		   fast-nowait)))
	      fast-nowait)) nowait))
      ;; Do things the simple way.
      (let ((f-path (nth 2 filename-parsed))
	    (f-abbr (efs-relativize-filename filename)))
	(efs-copy-file-internal
	 filename filename-parsed newname newname-parsed t t msg
	 (efs-cont (result line cont-lines) (filename newname f-host f-user
						      f-path f-abbr
						      cont fast-nowait)
	   (if result
	       (if cont
		   (efs-call-cont cont result line cont-lines)
		 (signal 'ftp-error
			 (list "Renaming"
			       (format "FTP Error: \"%s\"" line)
			       filename newname)))
	     (efs-send-cmd
	      f-host f-user (list 'delete f-path)
	      (format "Removing %s" f-abbr) nil
	      (efs-cont (result line cont-lines) (filename f-host cont)
		(if result
		    (if cont
			(efs-call-cont cont result line cont-lines)
		      (signal 'ftp-error
			      (list "Renaming"
				    (format "Failed to remove %s"
					    filename)
				    "FTP Error: \"%s\"" line)))
		  (efs-delete-file-entry (efs-host-type f-host)
					 filename)
		  (if cont
		      (efs-call-cont cont result line cont-lines))))
	      fast-nowait))) nowait)))))

(defun efs-rename-file-internal (filename newname ok-if-already-exists
					  &optional msg cont nowait)
  ;; Internal version of rename-file for remote files.
  ;; Takes CONT and NOWAIT args.
  (let ((filename (expand-file-name filename))
	(newname (expand-file-name newname)))
    (let ((f-parsed (efs-ftp-path filename))
	  (t-parsed (efs-ftp-path newname)))
      (if (null (or f-parsed t-parsed))
	  (progn
	    ;; local rename
	    (rename-file filename newname ok-if-already-exists)
	    (if cont
		(efs-call-cont cont nil "Renamed locally" "")))

	;; check to see if we can overwrite
	(if (or (not ok-if-already-exists)
		(numberp ok-if-already-exists))
	    (efs-barf-or-query-if-file-exists
	     newname "rename to it" (numberp ok-if-already-exists)))

	(let ((f-abbr (efs-relativize-filename filename))
	      (t-abbr (efs-relativize-filename newname
					       (file-name-directory filename)
					       filename)))
	  (or msg (setq msg (format "Renaming %s to %s" f-abbr t-abbr)))
	  (if f-parsed
	      (let* ((f-host (car f-parsed))
		     (f-user (nth 1 f-parsed))
		     (f-path (nth 2 f-parsed))
		     (f-host-type (efs-host-type f-host)))
		(if (and t-parsed
			 (string-equal (downcase f-host)
				       (downcase (car t-parsed)))
			 (not (efs-get-host-property f-host 'rnfr-failed))
			 (if (memq f-host-type efs-case-insensitive-host-types)
			     (string-equal (downcase f-user)
					   (downcase (nth 1 t-parsed)))
			   (string-equal f-user (nth 1 t-parsed))))
		    ;; Can run a RENAME command on the server.
		    (efs-rename-on-remote
		     f-host f-user filename newname f-path (nth 2 t-parsed)
		     msg nowait
		     (efs-cont (result line cont-lines) (f-host
							 filename
							 newname
							 ok-if-already-exists
							 msg cont nowait)
		       (if result
			   (progn
			     (efs-set-host-property f-host 'rnfr-failed t)
			     (efs-rename-file-internal
			      filename newname ok-if-already-exists msg cont
			      (if (eq nowait t) 1 nowait)))
			 (if cont
			     (efs-call-cont cont result line cont-lines)))))
		  ;; remote to remote
		  (efs-rename-from-remote filename f-parsed newname t-parsed
					  msg cont nowait)))
	    ;; local to remote
	    (efs-rename-local-to-remote 
	     filename newname t-parsed msg cont nowait)))))))

(defun efs-rename-file (filename newname &optional ok-if-already-exists nowait)
  ;; Does file renaming for remote files.
  (efs-rename-file-internal filename newname ok-if-already-exists
			    nil nil nowait))

;;;; ------------------------------------------------------------
;;;; Making symbolic and hard links.
;;;; ------------------------------------------------------------

;;;  These functions require that the remote FTP server understand
;;;  SITE EXEC and that ln is in its the ftp-exec path.

(defun efs-try-ln (host user cont nowait)
  ;; Do some preemptive testing to see if exec ln works
  (if (efs-get-host-property host 'exec-failed)
      (signal 'ftp-error (list "Unable to exec ln on host" host)))
  (let ((exec-ln (efs-get-host-property host 'exec-ln)))
    (cond
     ((eq exec-ln 'failed)
      (signal 'ftp-error (list "ln is not in FTP exec path on host" host)))
     ((eq exec-ln 'works)
      (efs-call-cont cont))
     (t
      (message "Checking for ln executable on %s..." host)
      (efs-send-cmd
       host user '(quote site exec "ln / /")
       nil nil
       (efs-cont (result line cont-lines) (host user cont)
	 (if result
	     (progn
	       (efs-set-host-property host 'exec-failed t)
	       (efs-error host user (format "exec: %s" line)))
	   ;; Look for an error message
	   (if (efs-save-match-data
		 (string-match "\n200-" cont-lines))
	       (progn
		 (efs-set-host-property host 'exec-ln 'works)
		 (efs-call-cont cont))
	     (efs-set-host-property host 'exec-ln 'failed)
	     (efs-error host user
			(format "ln not in FTP exec path on host %s" host)))))
       nowait)))))

(defun efs-make-symbolic-link-internal
  (target linkname &optional ok-if-already-exists cont nowait)
  ;; Makes remote symbolic links. Assumes that linkname is already expanded.
  (let* ((parsed (efs-ftp-path linkname))
	 (host (car parsed))
	 (user (nth 1 parsed))
	 (linkpath (nth 2 parsed))
	 (abbr (efs-relativize-filename linkname
					(file-name-directory target) target))
	 (tparsed (efs-ftp-path target))
	 (com-target target)
	 cmd-string)
    (if (null (file-directory-p
	       (file-name-directory linkname)))
	(if cont
	    (efs-call-cont cont 'failed
			   (format "no such file or directory, %s" linkname)
			   "")
	  (signal 'file-error (list "no such file or directory" linkname)))
      (if (or (not ok-if-already-exists)
	      (numberp ok-if-already-exists))
	  (efs-barf-or-query-if-file-exists
	   linkname "make symbolic link" (numberp ok-if-already-exists)))
      ;; Do this after above, so that hopefully the host type is sorted out
      ;; by now.
      (let ((host-type (efs-host-type host)))
	(if (or (not (memq host-type efs-unix-host-types))
		(memq host-type efs-dumb-host-types)
		(efs-get-host-property host 'exec-failed))
	    (error "Unable to make symbolic links on %s." host)))
      ;; Be careful not to spoil relative links, or symlinks to other
      ;; machines, which maybe symlink-fix.el can sort out.
      (if (and tparsed
	       (string-equal (downcase (car tparsed)) (downcase host))
	       (string-equal (nth 1 tparsed) user))
	  (setq com-target (nth 2 tparsed)))
      ;; symlinks only work for unix, so don't need to
      ;; convert pathnames. What about VOS?
      (setq cmd-string (concat "ln -sf "  com-target " " linkpath))
      (efs-try-ln
       host user
       (efs-cont () (host user cmd-string target linkname com-target
			  abbr cont nowait)
	 (efs-send-cmd
	  host user (list 'quote 'site 'exec cmd-string)
	  (format "Symlinking %s to %s" target abbr)
	  nil
	  (efs-cont (result line cont-lines) (host user com-target linkname
						   cont)
	    (if result
		(progn
		  (efs-set-host-property host 'exec-failed t)
		  (efs-error host user (format "exec: %s" line)))
	      (efs-save-match-data
		(if (string-match "\n200-\\([^\n]*\\)" cont-lines)
		    (let ((err (substring cont-lines (match-beginning 1)
					  (match-end 1))))
		      (if cont
			  (efs-call-cont cont 'failed err cont-lines)
			(efs-error host user err)))
		  (efs-add-file-entry nil linkname com-target nil user)
		  (if cont (efs-call-cont cont nil line cont-lines))))))
	  nowait))
       nowait))))

(defun efs-make-symbolic-link (target linkname &optional ok-if-already-exists)
  ;; efs version of make-symbolic-link
  (let* ((linkname (expand-file-name linkname))
	 (parsed (efs-ftp-path linkname)))
    (if parsed
	(efs-make-symbolic-link-internal target linkname ok-if-already-exists)
      ;; Handler will match on either target or linkname. We are only
      ;; interested in the linkname.
      (let ((file-name-handler-alist (efs-file-name-handler-alist-sans-fn
				      'efs-file-handler-function)))
	(make-symbolic-link target linkname ok-if-already-exists)))))

(defun efs-add-name-to-file-internal
  (file newname &optional ok-if-already-exists cont nowait)
  ;; Makes remote symbolic links. Assumes that linkname is already expanded.
  (let* ((parsed (efs-ftp-path file))
	 (host (car parsed))
	 (user (nth 1 parsed))
	 (path (nth 2 parsed))
	 (nparsed (efs-ftp-path newname))
	 (nhost (car nparsed))
	 (nuser (nth 1 nparsed))
	 (npath (nth 2 nparsed))
	 (abbr (efs-relativize-filename newname
					(file-name-directory file)))
	 (ent (efs-get-file-entry file))
	 cmd-string)
    (or (and (string-equal (downcase host) (downcase nhost))
	     (string-equal user nuser))
	(error "Cannot create hard links between different host user pairs."))
    (if (or (null ent) (stringp (car ent))
	    (not (file-directory-p
		  (file-name-directory newname))))
	(if cont
	    (efs-call-cont cont 'failed
			   (format "no such file or directory, %s %s"
				   file newname) "")
	  (signal 'file-error
		  (list "no such file or directory"
			file newname)))
      (if (or (not ok-if-already-exists)
	      (numberp ok-if-already-exists))
	  (efs-barf-or-query-if-file-exists
	   newname "make hard link" (numberp ok-if-already-exists)))
      ;; Do this last, so that hopefully the host type is known.
      (let ((host-type (efs-host-type host)))
	(if (or (not (memq host-type efs-unix-host-types))
		(memq host-type efs-dumb-host-types)
		(efs-get-host-property host 'exec-failed))
	    (error "Unable to make hard links on %s." host)))
      (setq cmd-string (concat "ln -f "  path " " npath))
      (efs-try-ln
       host user
       (efs-cont () (host user cmd-string file newname abbr cont nowait)
	 (efs-send-cmd
	  host user (list 'quote 'site 'exec cmd-string)
	  (format "Adding to %s name %s" file abbr)
	  nil
	  (efs-cont (result line cont-lines) (host user file newname cont)
	    (if result
		(progn
		  (efs-set-host-property host 'exec-failed t)
		  (efs-error host user (format "exec: %s" line)))
	      (efs-save-match-data
		(if (string-match "\n200-\\([^\n]*\\)" cont-lines)
		    (let ((err (substring cont-lines (match-beginning 1)
					  (match-end 1))))
		      (if cont
			  (efs-call-cont cont 'failed err cont-lines)
			(efs-error host user err)))
		  (let ((ent (efs-get-file-entry file)))
		    (if ent
			(let ((nlinks (nthcdr 4 ent))
			      new-nlinks)
			  (and (integerp (car nlinks))
			       (setq new-nlinks (1+ (car nlinks)))
			       (setcar nlinks new-nlinks))
			  (apply 'efs-add-file-entry nil newname ent)
			  (if cont (efs-call-cont cont nil line cont-lines)))
		      (let ((tbl (efs-get-files-hashtable-entry
				  (file-name-directory
				   (directory-file-name newname)))))
			(if tbl
			    (efs-ls
			     newname
			     (concat (efs-ls-guess-switches) "d") t t nil
			     nowait
			     (efs-cont (listing) (newname cont line cont-lines)
			       (efs-update-file-info
				newname efs-data-buffer-name)
			       (if cont
				   (efs-call-cont cont nil line cont-lines))))
			  (if cont
			      (efs-call-cont cont nil line cont-lines))))))))))
	  nowait))
       nowait))))

(defun efs-add-name-to-file (file newname &optional ok-if-already-exists)
  ;; efs version of add-name-to-file
  (efs-add-name-to-file-internal file newname ok-if-already-exists))


;;;; ==============================================================
;;;; >9
;;;; Multiple Host Type Support.
;;;; The initial host type guessing is done in the PWD code below.
;;;; If necessary, further guessing is done in the listing parser.
;;;; ==============================================================


;;;; --------------------------------------------------------------
;;;; Functions for setting and retrieving host types.
;;;; --------------------------------------------------------------

(defun efs-add-host (type host)
  "Sets the TYPE of the remote host HOST.
The host type is read with completion so this can be used to obtain a
list of supported host types. HOST must be a string, giving the name of
the host, exactly as given in file names. Setting the host type with
this function is preferable to setting the efs-TYPE-host-regexp, as look up
will be faster. Returns TYPE."
  ;; Since internet host names are always case-insensitive, we will cache
  ;; them in lower case.
  (interactive
   (list
    (intern
     (completing-read "Host type: "
		      (mapcar
		       (function (lambda (elt)
				   (list (symbol-name (car elt)))))
		       efs-host-type-alist)
		      nil t))
    (read-string "Host: "
		 (let ((name (or (buffer-file-name)
				 (and (eq major-mode 'dired-mode)
				      dired-directory))))
		   (and name (car (efs-ftp-path name)))))))
  (setq host (downcase host))
  (efs-set-host-property host 'host-type type)
  (prog1
      (setq efs-host-cache host
	    efs-host-type-cache type)
    (efs-set-process-host-type host)))

(defun efs-set-process-host-type (host &optional user)
  ;; Sets the value of efs-process-host-type so that it is shown
  ;; on the mode-line.
  (let ((buff-list (buffer-list)))
    (save-excursion
      (while buff-list
	(set-buffer (car buff-list))
	(if (equal efs-process-host host)
	    (setq efs-process-host-type (concat " " (symbol-name
						     (efs-host-type host))))
	  (and efs-show-host-type-in-dired
	       (eq major-mode 'dired-mode)
	       efs-dired-host-type
	       (string-equal (downcase
			      (car (efs-ftp-path default-directory)))
			     (downcase host))
	       (if user
		   (setq efs-dired-listing-type-string
			 (concat
			  " "
			  (symbol-name (efs-listing-type host user))))
		 (or efs-dired-listing-type-string
		     (setq efs-dired-listing-type-string
			   (concat " " (symbol-name (efs-host-type host))))))))
	(setq buff-list (cdr buff-list))))))

;;;; ----------------------------------------------------------------
;;;; Functions for setting and retrieving listings types.
;;;; ----------------------------------------------------------------

;;;  listing types??
;;;  These are distinguished from host types, in case some OS's have two
;;;  breeds of listings. e.g. Unix descriptive listings.
;;;  We also use this to support the myriad of DOS ftp servers.


(defun efs-listing-type (host user)
  "Returns the type of listing used on HOST by USER.
If there is no entry for a specialized listing, returns the host type."
  (or
   (efs-get-host-user-property host user 'listing-type)
   (efs-host-type host user)))

(defun efs-add-listing-type (type host user)
  "Interactively adds the specialized listing type TYPE for HOST and USER
to the listing type cache."
  (interactive
   (let ((name (or (buffer-file-name)
		   (and (eq major-mode 'dired-mode)
			dired-directory))))
     (list
      (intern
       (completing-read "Listing type: "
			(mapcar
			 (function (lambda (elt)
				     (list (symbol-name elt))))
			 efs-listing-types)
			nil t))
      (read-string "Host: "
		   (and name (car (efs-ftp-path name))))
      (read-string "User: "
		   (and name (nth 1 (efs-ftp-path name)))))))
  (efs-set-host-user-property host user 'listing-type type)
  (efs-set-process-host-type host user))

;;;; --------------------------------------------------------------
;;;; Auotomagic bug reporting for unrecognized host types.
;;;; --------------------------------------------------------------

(defun efs-scream-and-yell-1 (host user)
  ;; Internal for efs-scream-and-yell.
  (with-output-to-temp-buffer "*Help*"
    (princ
     (format
      "efs is unable to identify the remote host type of %s.

Please report this as a bug. It would be very helpful
if your bug report contained at least the PWD command
within the *ftp %s@%s* buffer.
If you know them, also send the operating system 
and ftp server types of the remote host." host user host)))
  (if (y-or-n-p "Would you like to submit a bug report now? ")
      (efs-report-bug host user
		      "Bug occurred during efs-guess-host-type." t)))

(defun efs-scream-and-yell (host user)
  ;; Advertises that something has gone wrong in identifying the host type.
  (if (eq (selected-window) (minibuffer-window))
      (efs-abort-recursive-edit-and-then 'efs-scream-and-yell-1 host user)
    (efs-scream-and-yell-1 host user)
    (error "Unable to identify remote host type")))

;;;; --------------------------------------------------------
;;;; Guess at the host type using PWD syntax.
;;;; --------------------------------------------------------

;; host-type path templates. These should match a pwd performed
;; as the first command after connecting. They should be as tight
;; as possible

(defconst efs-unix-path-template "^/")
(defconst efs-apollo-unix-path-template "^//")
(defconst efs-cms-path-template
  (concat
   "^[-A-Z0-9$*][-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?"
   "[-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?\\.[0-9][0-9][0-9A-Z]$\\|"
   ;; For the SFS version of CMS
   "^[-A-Z0-9]+:[-A-Z0-9$*]+\\.$"))
   
(defconst efs-mvs-path-template "^'?[A-Z][0-9][0-9]?[0-9]?[0-9]?[0-9]?\\.'?")

(defconst efs-guardian-path-template
  (concat
   "^\\("
   "\\\\[A-Z0-9][A-Z0-9]?[A-Z0-9]?[A-Z0-9]?[A-Z0-9]?[A-Z0-9]?[A-Z0-9]?\\."
   "\\)?"
   "\\$[A-Z0-9][A-Z0-9]?[A-Z0-9]?[A-Z0-9]?[A-Z0-9]?[A-Z0-9]?[A-Z0-9]?\\."
   "[A-Z][A-Z0-9]?[A-Z0-9]?[A-Z0-9]?[A-Z0-9]?[A-Z0-9]?[A-Z0-9]?[A-Z0-9]?$"))
;; guardian and cms are very close to overlapping (they don't). Be careful.
(defconst efs-vms-path-template
  "^[-A-Z0-9_$]+:\\[[-A-Z0-9_$]+\\(\\.[-A-Z0-9_$]+\\)*\\]$")
(defconst efs-mts-path-template
  "^[A-Z0-9._][A-Z0-9._][A-Z0-9._][A-Z0-9._]:$")
(defconst efs-ms-unix-path-template "^[A-Za-z0-9]:/")

;; Following two are for TI lisp machines. Note that lisp machines
;; do not have a default directory, but only a default pathname against
;; which relative pathnames are merged (Jamie tells me).
(defconst efs-ti-explorer-pwd-line-template
  (let* ((excluded-chars ":;<>.#\n\r\t\\/a-z ")
	 (token  (concat "[^" excluded-chars "]+")))
    (concat "^250 "
	    token ": "					; host name
	    token "\\(\\." token "\\)*; "		; directory
	    "\\(\\*.\\*\\|\\*\\)#\\(\\*\\|>\\)"	; name, ext, version
	    "$")))	; "*.*#*" or "*.*#>" or "*#*" or "*#>" or "#*" ...
(defconst efs-ti-twenex-path-template
  (let* ((excluded-chars ":;<>.#\n\r\t\\/a-z ")
	 (token  (concat "[^" excluded-chars "]+")))
    (concat "^"
	    token ":"					; host name
	    "<\\(" token "\\)?\\(\\." token "\\)*>"	; directory
	    "\\(\\*.\\*\\|\\*\\)"			; name and extension
	    "$")))

(defconst efs-tops-20-path-template
  "^[-A-Z0-9_$]+:<[-A-Z0-9_$]\\(.[-A-Z0-9_$]+\\)*>$")
(defconst efs-pc-path-template
  "^[a-zA-Z0-9]:\\\\\\([-_+=a-zA-Z0-9.]+\\\\\\)*[-_+=a-zA-Z0-9.]*$")
(defconst efs-mpe-path-template
  (let ((token (concat  "[A-Z][A-Z0-9]?[A-Z0-9]?[A-Z0-9]?[A-Z0-9]?"
			"[A-Z0-9]?[A-Z0-9]?[A-Z0-9]?")))
    (concat
     ;; optional session name
     "^\\(" token "\\)?,"
     ;; username
     token "."
     ;; account
     token ","
     ;; group
     token "$")))
(defconst efs-vos-path-template
  (let ((token "[][@\\^`{}|~\"$+,---./:_a-zA-Z0-9]+"))
    (concat
     "%" token           ; host
     "#" token           ; disk
     "\\(>" token "\\)+" ; directories
     )))
(defconst efs-netware-path-template "^[-A-Z0-9_][-A-Z0-9_/]*:/")
;; Sometimes netware doesn't return a device to a PWD. Then it will be
;; recognized by the listing parser.

(defconst efs-nos-ve-path-template "^:[A-Z0-9]")
;; Matches the path for NOS/VE

(defconst efs-mvs-pwd-line-template
  ;; Not sure how the PWD parser will do with empty strings, so treate
  ;; this as a line regexp.
  "^257 \\([Nn]o prefix defined\\|\"\" is working directory\\)")
(defconst efs-cms-pwd-line-template
  "^450 No current working directory defined$")
(defconst efs-tops-20-pwd-line-template
  "^500 I never heard of the \\(XPWD\\|PWD\\) command\\. Try HELP\\.$")
(defconst efs-dos:ftp-pwd-line-template
  "^250 Current working directory is +")
(defconst efs-coke-pwd-line-template "^257 Current balance \\$[0-9]")

(defconst efs-super-dumb-unix-tilde-regexp
  "^550 /.*: No such file or directory\\.?$")
(defconst efs-cms-knet-tilde-regexp
  "^501 Invalid CMS fileid: ~$")


;; It might be nice to message users about the host type identified,
;; but there is so much other messaging going on, it would not be
;; seen. No point in slowing things down just so users can read
;; a host type message.

(defun efs-guess-host-type (host user)
  "Guess the host type of HOST.
Does a PWD and examines the directory syntax. The PWD is then cached for use
in file name expansion."
  (let ((host-type (efs-host-type host))
	(key (concat host "/" user "/~"))
	syst)
    (efs-save-match-data
      (if (eq host-type 'unknown)
	  ;; Note that efs-host-type returns unknown as the default.
	  ;; Since we don't yet know the host-type, we use the default
	  ;; version of efs-send-pwd. We compensate if necessary
	  ;; by looking at the entire line of output.
	  (let* ((result (efs-send-pwd nil host user))
		 (dir (car result))
		 (line (cdr result)))
	    (cond
	     
	     ;; First sift through process lines to see if we recognize
	     ;; any pwd errors, or full line messages.
	     
	     ;; CMS
	     ((string-match efs-cms-pwd-line-template line)
	      (setq host-type (efs-add-host 'cms host)
		    dir (concat "/" (if (> (length user) 8)
					(substring user 0 8)
				      user)
				".191"))
	      (message
	       "Unable to determine a \"home\" CMS minidisk.  Assuming %s"
	       dir)
	      (sit-for 1))
	     
	     ;; TOPS-20
	     ((string-match efs-tops-20-pwd-line-template line)
	      (setq host-type (efs-add-host 'tops-20 host)
		    dir (car (efs-send-pwd 'tops-20 host user))))
	     
	     ;; TI-EXPLORER lisp machine. pwd works here, but the output
	     ;; needs to be specially parsed since spaces separate
	     ;; hostnames from dirs from filenames.
	     ((string-match efs-ti-explorer-pwd-line-template line)
	      (setq host-type (efs-add-host 'ti-explorer host)
		    dir (substring line 4)))

	     ;; FTP Software's DOS Server
	     ((string-match efs-dos:ftp-pwd-line-template line)
	      (setq host-type (efs-add-host 'dos host)
		    dir (substring line (match-end 0)))
	      (efs-add-listing-type 'dos:ftp host user))

	     ;; MVS
	     ((string-match efs-mvs-pwd-line-template line)
	      (setq host-type (efs-add-host 'mvs host)
		    dir "")) ; "" will convert to /, which is always
			     ; the mvs home dir.

	     ;; COKE
	     ((string-match efs-coke-pwd-line-template line)
	      (setq host-type (efs-add-host 'coke host)
		    dir "/"))
	     
	     ;; Try to get tilde.
	     ((null dir)
	      (let ((tilde (nth 1 (efs-send-cmd
				   host user (list 'get "~" "/dev/null")))))
		(cond
		 ;; super dumb unix
		 ((string-match efs-super-dumb-unix-tilde-regexp tilde)
		  (setq dir (car (efs-send-pwd 'super-dumb-unix host user))
			host-type (efs-add-host 'super-dumb-unix host)))

		 ;; Try for cms-knet
		 ((string-match efs-cms-knet-tilde-regexp tilde)
		  (setq dir (car (efs-send-pwd 'cms-knet host user))
			host-type (efs-add-host 'cms-knet host)))
		 
		 ;; We don't know. Scream and yell.
		 (efs-scream-and-yell host user))))
	     
	     ;; Now look at dir to determine host type
	     
	     ;; try for UN*X-y type stuff
	     ((string-match efs-unix-path-template dir)
	      (if
		  ;; Check for apollo, so we know not to short-circuit //.
		  (string-match efs-apollo-unix-path-template dir)
		  (progn
		    (setq host-type (efs-add-host 'apollo-unix host))
		    (efs-add-listing-type 'unix:unknown host user))
		;; could be ka9q, dos-distinct, plus any of the unix breeds,
		;; except apollo.
		(if (setq syst (efs-get-syst host user))
		    (let ((case-fold-search t))
		      (cond
		       ((string-match "\\bNet[wW]are\\b" syst)
			(setq host-type (efs-add-host 'netware host)))
		       ((string-match "^Plan 9" syst)
			(setq host-type (efs-add-host 'plan9 host)))
		       ((string-match "^UNIX" syst)
			(setq host-type (efs-add-host 'unix host))
			(efs-add-listing-type 'unix:unknown host user)))))))
	     
	     ;; try for VMS
	     ((string-match efs-vms-path-template dir)
	      (setq host-type (efs-add-host 'vms host)))
	     
	     ;; try for MTS
	     ((string-match efs-mts-path-template dir)
	      (setq host-type (efs-add-host 'mts host)))
	     
	     ;; try for CMS
	     ((string-match efs-cms-path-template dir)
	      (setq host-type (efs-add-host 'cms host)))

	     ;; try for Tandem's guardian OS
	     ((string-match efs-guardian-path-template dir)
	      (setq host-type (efs-add-host 'guardian host)))
	     
	     ;; Try for TOPS-20. pwd doesn't usually work for tops-20
	     ;; But who knows???
	     ((string-match efs-tops-20-path-template dir)
	      (setq host-type (efs-add-host 'tops-20 host)))
	     
	     ;; Try for DOS or OS/2.
	     ((string-match efs-pc-path-template dir)
	      (let ((syst (efs-get-syst host user))
		    (case-fold-search t))
		(if (and syst (string-match "^OS/2 " syst))
		    (setq host-type (efs-add-host 'os2 host))
		  (setq host-type (efs-add-host 'dos host)))))
	     
	     ;; try for TI-TWENEX lisp machine
	     ((string-match efs-ti-twenex-path-template dir)
	      (setq host-type (efs-add-host 'ti-twenex host)))

	     ;; try for MPE
	     ((string-match efs-mpe-path-template dir)
	      (setq host-type (efs-add-host 'mpe host)))

	     ;; try for VOS
	     ((string-match efs-vos-path-template dir)
	      (setq host-type (efs-add-host 'vos host)))

	     ;; try for the microsoft server in unix mode
	     ((string-match efs-ms-unix-path-template dir)
	      (setq host-type (efs-add-host 'ms-unix host)))

	     ;; Netware?
	     ((string-match efs-netware-path-template dir)
	      (setq host-type (efs-add-host 'netware host)))

	     ;; Try for MVS
	     ((string-match efs-mvs-path-template dir)
	      (if (string-match "^'.+'$" dir)
		  ;; broken MVS PWD quoting
		  (setq dir (substring dir 1 -1)))
	      (setq host-type (efs-add-host 'mvs host)))

	     ;; Try for NOS/VE
	     ((string-match efs-nos-ve-path-template dir)
	      (setq host-type (efs-add-host 'nos-ve host)))
	     
	     ;; We don't know. Scream and yell.
	     (t
	      (efs-scream-and-yell host user)))
	    
	    ;; Now that we have done a pwd, might as well put it in
	    ;; the expand-dir hashtable.
	    (if dir
		(efs-put-hash-entry
		 key
		 (efs-internal-directory-file-name
		  (efs-fix-path host-type dir 'reverse))
		 efs-expand-dir-hashtable
		 (memq host-type efs-case-insensitive-host-types))))

	;; host-type has been identified by regexp, set the mode-line.
	(efs-set-process-host-type host user)
	
	;; Some special cases, where we need to store the cwd on login.
	(if (not (efs-hash-entry-exists-p
		  key efs-expand-dir-hashtable))
	    (cond
	     ;; CMS: We will be doing cd's, so we'd better make sure that
	     ;; we know where home is.
	     ((eq host-type 'cms)
	      (let* ((res (efs-send-pwd 'cms host user))
		     (dir (car res))
		     (line (cdr res)))
		(if (and dir (not (string-match
				   efs-cms-pwd-line-template line)))
		    (setq dir (concat "/" dir))
		  (setq dir (concat "/" (if (> (length user) 8)
					    (substring user 0 8)
					  user)
				    ".191"))
		  (message
		   "Unable to determine a \"home\" CMS minidisk. Assuming %s"
		   dir))
		(efs-put-hash-entry
		 key dir efs-expand-dir-hashtable
		 (memq 'cms efs-case-insensitive-host-types))))
	     ;; MVS: pwd doesn't work in the root directory, so we stuff this
	     ;; into the hashtable manually.
	     ((eq host-type 'mvs)
	      (efs-put-hash-entry key "/" efs-expand-dir-hashtable))
	     ))))))


;;;; -----------------------------------------------------------
;;;; efs-autoloads
;;;; These provide the entry points for the non-unix packages.
;;;; -----------------------------------------------------------

(efs-autoload 'efs-fix-path vms "efs-vms")
(efs-autoload 'efs-fix-path mts "efs-mts")
(efs-autoload 'efs-fix-path cms "efs-cms")
(efs-autoload 'efs-fix-path ti-explorer "efs-ti-explorer")
(efs-autoload 'efs-fix-path ti-twenex "efs-ti-twenex")
(efs-autoload 'efs-fix-path dos "efs-pc")
(efs-autoload 'efs-fix-path mvs "efs-mvs")
(efs-autoload 'efs-fix-path tops-20 "efs-tops-20")
(efs-autoload 'efs-fix-path mpe "efs-mpe")
(efs-autoload 'efs-fix-path os2 "efs-pc")
(efs-autoload 'efs-fix-path vos "efs-vos")
(efs-autoload 'efs-fix-path ms-unix "efs-ms-unix")
(efs-autoload 'efs-fix-path netware "efs-netware")
(efs-autoload 'efs-fix-path cms-knet "efs-cms-knet")
(efs-autoload 'efs-fix-path guardian "efs-guardian")
(efs-autoload 'efs-fix-path nos-ve "efs-nos-ve")

(efs-autoload 'efs-fix-dir-path vms "efs-vms")
(efs-autoload 'efs-fix-dir-path mts "efs-mts")
(efs-autoload 'efs-fix-dir-path cms "efs-cms")
(efs-autoload 'efs-fix-dir-path ti-explorer "efs-ti-explorer")
(efs-autoload 'efs-fix-dir-path ti-twenex "efs-ti-twenex")
(efs-autoload 'efs-fix-dir-path dos "efs-pc")
(efs-autoload 'efs-fix-dir-path mvs "efs-mvs")
(efs-autoload 'efs-fix-dir-path tops-20 "efs-tops-20")
(efs-autoload 'efs-fix-dir-path mpe "efs-mpe")
(efs-autoload 'efs-fix-dir-path os2 "efs-pc")
(efs-autoload 'efs-fix-dir-path vos "efs-vos")
(efs-autoload 'efs-fix-dir-path hell "efs-hell")
(efs-autoload 'efs-fix-dir-path ms-unix "efs-ms-unix")
(efs-autoload 'efs-fix-dir-path netware "efs-netware")
(efs-autoload 'efs-fix-dir-path plan9 "efs-plan9")
(efs-autoload 'efs-fix-dir-path cms-knet "efs-cms-knet")
(efs-autoload 'efs-fix-dir-path guardian "efs-guardian")
(efs-autoload 'efs-fix-dir-path nos-ve "efs-nos-ve")
(efs-autoload 'efs-fix-dir-path coke "efs-coke")

;; A few need to autoload a pwd function
(efs-autoload 'efs-send-pwd tops-20 "efs-tops-20")
(efs-autoload 'efs-send-pwd cms-knet "efs-cms-knet")
(efs-autoload 'efs-send-pwd ti-explorer "efs-ti-explorer")
(efs-autoload 'efs-send-pwd hell "efs-hell")
(efs-autoload 'efs-send-pwd mvs "efs-mvs")
(efs-autoload 'efs-send-pwd coke "efs-coke")

;; A few packages are loaded by the listing parser.
(efs-autoload 'efs-parse-listing ka9q "efs-ka9q")
(efs-autoload 'efs-parse-listing unix:dl "efs-dl")
(efs-autoload 'efs-parse-listing dos-distinct "efs-dos-distinct")
(efs-autoload 'efs-parse-listing hell "efs-hell")
(efs-autoload 'efs-parse-listing netware "efs-netware")

;; Packages that need to autoload for child-lookup
(efs-autoload 'efs-allow-child-lookup plan9 "efs-plan9")
(efs-autoload 'efs-allow-child-lookup coke "efs-coke")

;; Packages that need to autoload for file-exists-p and file-directory-p
(efs-autoload 'efs-internal-file-exists-p guardian "efs-guardian")
(efs-autoload 'efs-internal-file-directory-p guardian "efs-guardian")



;;;; ============================================================
;;;; >10
;;;; Attaching onto the appropriate Emacs version
;;;; ============================================================

;;;; -------------------------------------------------------------------
;;;; Connect to various hooks.
;;;; -------------------------------------------------------------------

(or (memq 'efs-set-buffer-mode find-file-hooks)
    (setq find-file-hooks
	  (cons 'efs-set-buffer-mode find-file-hooks)))

;;; We are using our own dired.el, so this doesn't depend on Emacs flavour.

(if (featurep 'dired)
    (require 'efs-dired)
  (add-hook 'dired-load-hook (function
			      (lambda ()
				(require 'efs-dired)))))

;;;; ------------------------------------------------------------
;;;; Add to minor-mode-alist.
;;;; ------------------------------------------------------------

(or (assq 'efs-process-host-type minor-mode-alist)
    (if (assq 'dired-sort-mode minor-mode-alist)
	(let ((our-list
	       (nconc
		(delq nil
		      (list (assq 'dired-sort-mode minor-mode-alist)
			    (assq 'dired-subdir-omit minor-mode-alist)
			    (assq 'dired-marker-stack minor-mode-alist)))
		(list '(efs-process-host-type efs-process-host-type)
		      '(efs-dired-listing-type
			efs-dired-listing-type-string))))
	      (old-list (delq
			 (assq 'efs-process-host-type minor-mode-alist)
			 (delq
			  (assq 'efs-dired-listing-type minor-mode-alist)
			  minor-mode-alist))))
	  (setq minor-mode-alist nil)
	  (while old-list
	    (or (assq (car (car old-list)) our-list)
		(setq minor-mode-alist (nconc minor-mode-alist
					      (list (car old-list)))))
	    (setq old-list (cdr old-list)))
	  (setq minor-mode-alist (nconc our-list minor-mode-alist)))
      (setq minor-mode-alist
	    (nconc
	     (list '(efs-process-host-type efs-process-host-type)
		   '(efs-dired-listing-type efs-dired-listing-type-string))
	     minor-mode-alist))))

;;;; ------------------------------------------------------------
;;;; File name handlers
;;;; ------------------------------------------------------------

;;;###autoload
(defun efs-file-handler-function (operation &rest args)
  "Function to call special file handlers for remote files."
  (let ((handler (and (if (boundp 'allow-remote-paths)
			  allow-remote-paths
			t)
		      (get operation 'efs))))
    (if handler
	(apply handler args)
      (let ((inhibit-file-name-handlers
	     (cons 'efs-file-handler-function
		   (and (eq inhibit-file-name-operation operation)
			inhibit-file-name-handlers)))
	    (inhibit-file-name-operation operation))
	(apply operation args)))))

(defun efs-sifn-handler-function (operation &rest args)
  ;; Handler function for substitute-in-file-name
  (if (and (if (boundp 'allow-remote-paths)
			  allow-remote-paths
			t)
	   (eq operation 'substitute-in-file-name))
      (apply 'efs-substitute-in-file-name args)
    (let ((inhibit-file-name-handlers
	   (cons 'efs-sifn-handler-function
		 (and (eq operation inhibit-file-name-operation)
		      inhibit-file-name-handlers)))
	  (inhibit-file-name-operation operation))
      (apply operation args))))

;; Yes, this is what it looks like.  I'm defining the handler to run our
;; version whenever there is an environment variable.

(defvar efs-path-sifn-regexp "\\(^\\|[^$]\\)\\(\\$\\$\\)*\\$[{a-zA-Z0-9]"
  "Regexp to match environment variables in file names.")

(or (assoc efs-path-sifn-regexp file-name-handler-alist)
    (nconc file-name-handler-alist
	   (list
	    (cons efs-path-sifn-regexp
		  'efs-sifn-handler-function))))

;;;; ------------------------------------------------------------
;;;; Necessary overloads.
;;;; ------------------------------------------------------------

;;;  The following functions are overloaded, instead of extended via
;;;  the file-name-handler-alist. For various reasons, the
;;;  file-name-handler-alist doesn't work for them. It would be nice if
;;;  this list could be shortened in the future.

;; Loading lisp files. The problem with using the file-name-handler-alist
;; here is that we don't know what is to be handled, until after searching
;; the load-path. The solution is to change the C code for Fload.
;; A patch to do this has been written by Jay Adams <jka@ece.cmu.edu>.
(efs-overwrite-fn "efs" 'load)
(efs-overwrite-fn "efs" 'require)

;;;; ------------------------------------------------------------
;;;; Install the file handlers for efs-file-handler-function.
;;;; ------------------------------------------------------------

;; I/O
(put 'insert-file-contents 'efs 'efs-insert-file-contents)
(put 'insert-file-contents-literally 'efs 'efs-insert-file-contents-literally)
(put 'write-region 'efs 'efs-write-region)
(put 'directory-files 'efs 'efs-directory-files)
(put 'list-directory 'efs 'efs-list-directory)
(put 'insert-directory 'efs 'efs-insert-directory)
(put 'recover-file 'efs 'efs-recover-file)
;; file properties
(put 'file-directory-p 'efs 'efs-file-directory-p)
(put 'file-writable-p 'efs 'efs-file-writable-p)
(put 'file-readable-p 'efs 'efs-file-readable-p)
(put 'file-executable-p 'efs 'efs-file-executable-p)
(put 'file-symlink-p 'efs 'efs-file-symlink-p)
(put 'file-attributes 'efs 'efs-file-attributes)
(put 'file-exists-p 'efs 'efs-file-exists-p)
(put 'file-accessible-directory-p 'efs 'efs-file-accessible-directory-p)
;; manipulating file names
(put 'expand-file-name 'efs 'efs-expand-file-name)
(put 'file-name-directory 'efs 'efs-file-name-directory)
(put 'file-name-nondirectory 'efs 'efs-file-name-nondirectory)
(put 'file-name-as-directory 'efs 'efs-file-name-as-directory)
(put 'directory-file-name 'efs 'efs-directory-file-name)
(put 'abbreviate-file-name 'efs 'efs-abbreviate-file-name)
(put 'file-name-sans-versions 'efs 'efs-file-name-sans-versions)
(put 'unhandled-file-name-directory 'efs 'efs-unhandled-file-name-directory)
(put 'diff-latest-backup-file 'efs 'efs-diff-latest-backup-file)
(put 'file-truename 'efs 'efs-file-truename)
;; modtimes
(put 'verify-visited-file-modtime 'efs 'efs-verify-visited-file-modtime)
(put 'file-newer-than-file-p 'efs 'efs-file-newer-than-file-p)
(put 'set-visited-file-modtime 'efs 'efs-set-visited-file-modtime)
;; file modes
(put 'set-file-modes 'efs 'efs-set-file-modes)
(put 'file-modes 'efs 'efs-file-modes)
;; buffers
(put 'backup-buffer 'efs 'efs-backup-buffer)
(put 'get-file-buffer 'efs 'efs-get-file-buffer)
(put 'create-file-buffer 'efs 'efs-create-file-buffer)
;; creating and removing files
(put 'delete-file 'efs 'efs-delete-file)
(put 'copy-file 'efs 'efs-copy-file)
(put 'rename-file 'efs 'efs-rename-file)
(put 'file-local-copy 'efs 'efs-file-local-copy)
(put 'make-directory-internal 'efs 'efs-make-directory-internal)
(put 'delete-directory 'efs 'efs-delete-directory)
(put 'add-name-to-file 'efs 'efs-add-name-to-file)
(put 'make-symbolic-link 'efs 'efs-make-symbolic-link)
;; file name completion
(put 'file-name-completion 'efs 'efs-file-name-completion)
(put 'file-name-all-completions 'efs 'efs-file-name-all-completions)

;;;; ------------------------------------------------------------
;;;; Finally run any load-hooks.
;;;; ------------------------------------------------------------

(run-hooks 'efs-load-hook)

;;; end of efs.el
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.