File: file.c

package info (click to toggle)
vile 9.6m-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,196 kB
  • ctags: 8,459
  • sloc: ansic: 90,876; lex: 9,514; sh: 3,223; cpp: 3,137; perl: 2,928; makefile: 785; awk: 276
file content (2884 lines) | stat: -rw-r--r-- 66,818 bytes parent folder | download
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
/*
 * The routines in this file
 * handle the reading and writing of
 * disk files. All of details about the
 * reading and writing of the disk are
 * in "fileio.c".
 *
 * $Header: /usr/build/vile/vile/RCS/file.c,v 1.415 2008/04/15 00:10:33 tom Exp $
 */

#include "estruct.h"
#include "edef.h"
#include "nefsms.h"

#if SYS_WINNT
#include <io.h>			/* for mktemp */
#include <direct.h>		/* for mkdir */
#define vl_mkdir(path,mode) mkdir(path)
#else
#define	vl_mkdir(path,mode) mkdir(path,mode)
#endif

#ifdef HAVE_UMASK
#define vl_umask(n) umask(n)
#else
#define vl_umask(n) n
#endif

#if CC_NEWDOSCC
#include <io.h>
#endif

#if CC_CSETPP
#define isFileMode(mode) (mode & S_IFREG) == S_IFREG
#else
#define isFileMode(mode) (mode & S_IFMT) == S_IFREG
#endif

#define NO_BNAME "NoName"

#if SYS_WINNT && DISP_NTWIN
    /*
     * Create a shadow copy of ffstatus for the purposes of distinguishing
     * a new file from an existing file.  A new file does not yet exist on
     * disk and the user may decide not to write back its contents before
     * exiting the editor.
     *
     * A shadow copy is used here to prevent breaking a _lot_ of existing
     * code that references the value of ffstatus.
     */
static FFType ffshadow;
#endif

static int
FIO2Status(int fio)
{
    int result;

    if (fio < FIOERR)		/* no error - all ok */
	result = TRUE;
    else if (fio == FIOERR)	/* a non-fatal error */
	result = FALSE;
    else if (fio == FIOABRT)	/* user killed it */
	result = SORTOFTRUE;
    else			/* something bad happened */
	result = ABORT;
    return result;
}

/*--------------------------------------------------------------------------*/

/* Returns the modification time iff the path corresponds to an existing file,
 * otherwise it returns zero.
 */
time_t
file_modified(char *path)
{
    struct stat statbuf;
    time_t the_time = 0;

    (void) file_stat(path, 0);
    if (file_stat(path, &statbuf) >= 0
	&& isFileMode(statbuf.st_mode)) {
#if SYS_VMS
	the_time = statbuf.st_ctime;	/* e.g., creation-time */
#else
	the_time = statbuf.st_mtime;
#endif
    }
    return the_time;
}

#ifdef MDCHK_MODTIME
static int
PromptFileChanged(BUFFER *bp, char *fname, const char *question, int iswrite)
{
    int status = SORTOFTRUE;
    char prompt[NLINE];
    const char *remind, *again;
    time_t curtime = 0;
    int modtimes_mismatch = FALSE;
    int fuids_mismatch = FALSE;
#ifdef CAN_CHECK_INO
    FUID curfuid;
#endif

    remind = again = "";

    if (isInternalName(bp->b_fname) || !bp->b_active)
	return SORTOFTRUE;

    if (b_val(bp, MDCHK_MODTIME)) {

	if (same_fname(fname, bp, FALSE)
	    && get_modtime(bp, &curtime)) {
	    time_t check_against;
	    if (iswrite) {
		check_against = bp->b_modtime;
		remind = "Reminder: ";
	    } else {
		if (bp->b_modtime_at_warn) {
		    check_against = bp->b_modtime_at_warn;
		    again = "again ";
		} else {
		    check_against = bp->b_modtime;
		}
	    }
	    modtimes_mismatch = (check_against != curtime);
	}
#ifdef CAN_CHECK_INO
	if (bp->b_fileuid.valid) {
	    int have_fuid;
	    FUID *check_against;
	    have_fuid = fileuid_get(bp->b_fname, &curfuid);
	    if (have_fuid) {
		if (iswrite) {
		    check_against = &bp->b_fileuid;
		    remind = "Reminder: ";
		} else {
		    if (bp->b_fileuid_at_warn.valid) {
			check_against =
			    &bp->b_fileuid_at_warn;
			again = "again ";
		    } else {
			check_against = &bp->b_fileuid;
		    }
		}
		fuids_mismatch = !fileuid_compare(check_against, &curfuid);
	    }
	}
#endif /* CAN_CHECK_INO */
    }

    if (modtimes_mismatch || fuids_mismatch) {
	(void) lsprintf(prompt,
			"%sFile for \"%s\" has changed %son disk.  %s",
			remind, bp->b_bname, again, question);
	if ((status = mlyesno(prompt)) != TRUE)
	    mlerase();
	/* avoid reprompts */
	if (modtimes_mismatch)
	    bp->b_modtime_at_warn = curtime;
#ifdef CAN_CHECK_INO
	if (fuids_mismatch)
	    bp->b_fileuid_at_warn = curfuid;
#endif
    }

    return status;
}

int
ask_shouldchange(BUFFER *bp)
{
    int status;
    status = PromptFileChanged(bp, bp->b_fname, "Continue", FALSE);
    return (status == TRUE || status == SORTOFTRUE);
}

int
check_file_changed(BUFFER *bp, char *fn)
{
    int status = TRUE;

    if (PromptFileChanged(bp, fn, "Reread", FALSE) == TRUE) {
#if OPT_LCKFILES
	/* release own lock before read the file again */
	if (global_g_val(GMDUSEFILELOCK)) {
	    if (!b_val(curbp, MDLOCKED) && !b_val(curbp, MDVIEW))
		release_lock(fn);
	}
#endif
	status = readin(fn, TRUE, bp, TRUE);
    }
    return status;
}

static int
inquire_file_changed(BUFFER *bp, char *fn)
{
    int status;
    if ((status = PromptFileChanged(bp, fn, "Write", TRUE)) != TRUE
	&& (status != SORTOFTRUE)) {
	mlforce("[Write aborted]");
	return FALSE;
    }
    return TRUE;
}

int
check_visible_files_changed(void)
{
    WINDOW *wp;

    for_each_visible_window(wp)
	(void) check_file_changed(wp->w_bufp, wp->w_bufp->b_fname);
    return TRUE;
}
#endif /*  MDCHK_MODTIME */

#ifdef MDCHK_MODTIME
int
get_modtime(BUFFER *bp, time_t * the_time)
{
    if (isInternalName(bp->b_fname))
	*the_time = 0;
    else
	*the_time = file_modified(bp->b_fname);

    return (*the_time != 0);
}

void
set_modtime(BUFFER *bp, char *fn)
{
    time_t current;

    if (same_fname(fn, bp, FALSE) && get_modtime(bp, &current)) {
	bp->b_modtime = current;
	bp->b_modtime_at_warn = 0;
    }
}
#endif /* MDCHK_MODTIME */

/*
 * Utility functions for preparing-for, and cleanup-from a pipe.  We have to
 * switch back to cooked I/O mode while the pipe is running, in case it is
 * interactive, e.g.,
 *	:w !more
 * as well as remember to repaint the screen, if we wrote a pipe rather than
 * reading it.
 */
static int must_clean_pipe;	/* copy of ffstatus (it is reset in ffclose) */
static int writing_to_pipe;	/* if writing, we disable 'working...' */

static void
CleanToPipe(int writing)
{
    must_clean_pipe = (ffstatus == file_is_pipe);
    writing_to_pipe = writing;

    if (must_clean_pipe) {
	if (writing) {
	    beginDisplay();
	}
#if SYS_UNIX || SYS_MSDOS
	term.clean(TRUE);
#else
#ifdef GMDW32PIPES
	kbd_erase_to_end(0);
	kbd_flush();
#endif
	term.kclose();
#endif
    }
}

static void
CleanAfterPipe(int Wrote)
{
    if (must_clean_pipe) {
#if SYS_UNIX || SYS_MSDOS
	term.unclean();		/* may clear the screen as a side-effect */
	term.kopen();
	term.flush();
	if (Wrote)
	    pressreturn();
	sgarbf = TRUE;
#else
	term.kopen();
#ifdef GMDW32PIPES
	if (global_g_val(GMDW32PIPES)) {
	    if (Wrote)
		pressreturn();
	    sgarbf = TRUE;
	}
#endif
#endif
	must_clean_pipe = FALSE;

	if (writing_to_pipe) {
	    writing_to_pipe = FALSE;
	    endofDisplay();
	}
    }
}

/*
 * On faster machines, a pipe-writer will tend to keep the pipe full. This
 * function is used by 'slowreadf()' to test if we've not done an update
 * recently even if this is the case.
 */
#if SYS_UNIX && OPT_SHELL
static int
slowtime(time_t * refp)
{
    int status = FALSE;

    if (ffstatus == file_is_pipe) {
	time_t temp = time((time_t *) 0);

	status = (!ffhasdata() || (temp != *refp));
	if (status)
	    *refp = temp;
    }
    return status;
}
#else
# if SYS_WINNT
#  define slowtime(refp) ((ffstatus == file_is_pipe) && !nowait_pipe_cmd && !ffhasdata())
# else
#  define slowtime(refp) ((ffstatus == file_is_pipe) && !ffhasdata())
# endif
#endif

int
no_such_file(const char *fname)
{
    mlwarn("[No such file \"%s\"]", fname);
    return FALSE;
}

#if OPT_VMS_PATH
static int
explicit_version(char *ver)
{
    if (*ver != EOS) {
	ver++;
	if (isDigit(*ver))
	    return TRUE;
    }
    return FALSE;
}
#endif /* OPT_VMS_PATH */

#if OPT_VMS_PATH
static char *
resolve_filename(BUFFER *bp)
{
    char temp[NFILEN];
#if SYS_VMS
    ch_fname(bp, fgetname(ffp, temp));
#else
    /*
     * The fgetname() should be just a shortcut; we should already have a
     * usable filename in bp->b_fname.
     */
    if (bp->b_fname == 0) {
	TRACE(("resolve_filename NULL\n"));
    } else if (is_vms_pathname(bp->b_fname, FALSE)
	       && strcspn(bp->b_fname, ":[];") != strlen(bp->b_fname)) {
	TRACE(("resolve_filename NONEED: %s\n", bp->b_fname));
    } else {
	char temp2[NFILEN];
	mklower(strcpy(temp2, bp->b_fname));
	if (realpath(temp2, temp)) {
	    (void) unix2vms_path(temp, temp);
	    TRACE(("resolve_filename %s -> %s\n", bp->b_fname, temp));
	    ch_fname(bp, temp);
	} else {
	    TRACE(("resolve_filename FAILED: %s\n", bp->b_fname));
	}
    }
#endif
    markWFMODE(bp);
    return bp->b_fname;
}
#endif

/*
 * Returns true if the given filename is the same as that of the referenced
 * buffer.  The 'lengthen' parameter controls whether we assume the filename is
 * already in canonical form, since that may be an expensive operation to do in
 * a loop.
 */
int
same_fname(const char *fname, BUFFER *bp, int lengthen)
{
    int status = FALSE;
    char temp[NFILEN];

    if (fname == 0
	|| bp->b_fname == 0
	|| isInternalName(fname)
	|| isInternalName(bp->b_fname)) {
	return (status);
    }

    TRACE(("same_fname(\n\tfname=%s,\n\tbname=%s,\n\tlengthen=%d)\n",
	   fname, bp->b_bname, lengthen));

    if (lengthen)
	fname = lengthen_path(vl_strncpy(temp, fname, sizeof(temp)));

#if OPT_VMS_PATH
    /* ignore version numbers in this comparison unless both are given */
    if (is_vms_pathname(fname, FALSE)) {
	char *bname = bp->b_fname;
	char *s = version_of(bname);
	char *t = version_of((char *) fname);

	if ((!explicit_version(s)
	     || !explicit_version(t))
	    && ((s - bname) == (t - fname))) {
	    status = !strnicmp(fname, bname, (size_t) (s - bname));
	    TRACE(("=>%d\n", status));
	    return (status);
	}
    }
#endif

    status = !strcmp(fname, bp->b_fname);
    TRACE(("=>%d\n", status));
    return (status);
}

/* support for "unique-buffers" mode -- file uids. */

int
fileuid_get(const char *fname, FUID * fuid)
{
#ifdef CAN_CHECK_INO
    struct stat sb;

    TRACE(("fileuid_get(%s) discarding cache\n", fname));
    (void) file_stat(fname, 0);
    memset(fuid, 0, sizeof(*fuid));
    if (file_stat(fname, &sb) == 0) {
	fuid->ino = sb.st_ino;
	fuid->dev = sb.st_dev;
	fuid->valid = TRUE;
	return TRUE;
    }
#endif
    return FALSE;
}

void
fileuid_set(BUFFER *bp, FUID * fuid)
{
#ifdef CAN_CHECK_INO
    bp->b_fileuid = *fuid;	/* struct copy */
#endif
}

void
fileuid_invalidate(BUFFER *bp)
{
#ifdef CAN_CHECK_INO
    bp->b_fileuid.ino = 0;
    bp->b_fileuid.dev = 0;
    bp->b_fileuid.valid = FALSE;
    bp->b_fileuid_at_warn = bp->b_fileuid;
#endif
}

void
fileuid_set_if_valid(BUFFER *bp, const char *fname)
{
    FUID fuid;

    if (!same_fname(fname, bp, FALSE))
	return;

    if (fileuid_get(fname, &fuid))
	fileuid_set(bp, &fuid);
    else
	fileuid_invalidate(bp);
}

int
fileuid_compare(FUID * fuid1, FUID * fuid2)
{
#ifdef CAN_CHECK_INO
    return (fuid1->valid && fuid2->valid &&
	    fuid1->ino == fuid2->ino &&
	    fuid1->dev == fuid2->dev);
#else
    return (FALSE);		/* Doesn't really matter */
#endif
}

int
fileuid_same(BUFFER *bp, FUID * fuid)
{
#ifdef CAN_CHECK_INO
    return fileuid_compare(&bp->b_fileuid, fuid);
#else
    return (FALSE);		/* Doesn't really matter */
#endif
}

/*
 * Set the buffer-name based on the filename
 */
static void
set_buffer_name(BUFFER *bp)
{
    char bname[NBUFN];

    bp->b_bname[0] = EOS;	/* ...so 'unqname()' doesn't find me */
    makename(bname, bp->b_fname);
    unqname(bname);
    set_bname(bp, bname);
    updatelistbuffers();
    markWFMODE(bp);
#if OPT_TITLE
    set_editor_title();
#endif
}

static int
cannot_reread(void)
{
    mlforce("[Cannot reread]");
    return FALSE;
}

/*
 * Zap the given buffer without trying to close its window, by renaming it
 * to the unnamed-buffer and resetting its local modes.  Keeping the window
 * sidesteps the issue of whether it was the only buffer, while allowing us
 * to free the associated memory.
 *
 * FIXME: this may also have a majormode associated with it, should remove it.
 */
static void
reset_to_unnamed(BUFFER *bp)
{
    WINDOW *wp;
    int n;

    for (n = 0; n < NUM_B_VALUES; ++n) {
	if (is_local_b_val(bp, n)) {
	    make_global_b_val(bp, n);
	}
    }
    bclear(bp);
    set_bname(bp, UNNAMED_BufName);
    ch_fname(bp, "");

    for_each_window(wp) {
	if (wp->w_bufp == bp) {
	    wp->w_flag |= (WFHARD | WFMODE);
	}
    }
}

/*
 * Read a file into the current
 * buffer. This is really easy; all you do it
 * find the name of the file, and call the standard
 * "read a file into the current buffer" code.
 */
/* ARGSUSED */
int
fileread(int f GCC_UNUSED, int n GCC_UNUSED)
{
    int status;
    char fname[NFILEN];
    W_VALUES *save_wvals;

    TRACE((T_CALLED "fileread(%d,%d)\n", f, n));

    if (more_named_cmd()) {
	if ((status = mlreply_file("Replace with file: ", (TBUFF **) 0,
				   FILEC_REREAD, fname)) != TRUE)
	    returnCode(status);
    } else if (b_is_temporary(curbp)) {
	returnCode(cannot_reread());
    } else if (!(global_g_val(GMDWARNREREAD) || curbp->b_fname[0] == EOS)
	       || ((status = mlyesno("Reread current buffer")) == TRUE)) {
	(void) vl_strncpy(fname, curbp->b_fname, sizeof(fname));
	/* Check if we are rereading an unnamed-buffer if it is not
	 * associated with a file.
	 */
	if (is_internalname(curbp->b_fname)) {
	    if (eql_bname(curbp, STDIN_BufName)
		|| eql_bname(curbp, UNNAMED_BufName)) {
		status = bclear(curbp);
		if (status == TRUE)
		    mlerase();
		curwp->w_flag |= (WFMODE | WFHARD);
		returnCode(status);
	    }
	    returnCode(cannot_reread());
	}
    } else {
	returnCode(FALSE);
    }

#if OPT_LCKFILES
    /* release own lock before read the replaced file */
    if (global_g_val(GMDUSEFILELOCK)) {
	if (!b_val(curbp, MDLOCKED) && !b_val(curbp, MDVIEW))
	    release_lock(curbp->b_fname);
    }
#endif

    /* we want no errors or complaints, so mark it unchanged */
    b_clr_changed(curbp);

    save_wvals = save_window_modes(curbp);
    status = readin(fname, TRUE, curbp, TRUE);
    if (status == ABORT) {
	reset_to_unnamed(curbp);
    } else {
	set_buffer_name(curbp);
#if OPT_FINDERR
	/*
	 * Doing ":e!" on [Output] will reset the buffer name to match the
	 * command.  Even if it was not [Output], it is still useful to make
	 * the error-buffer match the last-read shell/pipe output.
	 */
	if (isShellOrPipe(curbp->b_fname))
	    set_febuff(curbp->b_bname);
#endif
    }
    restore_window_modes(curbp, save_wvals);

    returnCode(status);
}

/*
 * Select a file for editing.
 * Look around to see if you can find the
 * file in another buffer; if you can find it
 * just switch to the buffer. If you cannot find
 * the file, create a new buffer, read in the
 * text, and switch to the new buffer.
 * This is ": e"
 */

void
set_last_file_edited(const char *f)
{
    tb_scopy(&lastfileedited, f);
}

static int
bp2swbuffer(BUFFER *bp, int ask_rerun, int lockfl)
{
    int s;

    if ((s = (bp != 0)) != FALSE) {
	if (bp->b_active) {
	    if (ask_rerun) {
		switch (mlyesno("Old command output -- rerun")) {
		case TRUE:
		    bp->b_active = FALSE;
		    break;
		case ABORT:
		    s = FALSE;
		    /* FALLTHRU */
		default:
		    mlerase();
		    break;
		}
	    } else {
		mlwrite("[Old buffer]");
	    }
	}
	if (s == TRUE)
	    s = swbuffer_lfl(bp, lockfl, FALSE);
	if (s == TRUE)
	    curwp->w_flag |= WFMODE | WFHARD;
    }

    return s;
}

int
set_files_to_edit(const char *prompt, int appflag)
{
    int status;
    BUFFER *bp, *bp_next;

    char fname[NFILEN];
    char *actual;
    BUFFER *firstbp = 0;

    TRACE((T_CALLED "set_files_to_edit(%s, %d)\n", NONNULL(prompt), appflag));

    if ((status = mlreply_file(prompt, &lastfileedited,
			       FILEC_READ | FILEC_EXPAND,
			       fname)) == TRUE) {
	while ((actual = filec_expand()) != 0) {
	    if ((bp = getfile2bp(actual, !clexec, FALSE)) == 0) {
		status = FALSE;
		break;
	    }
	    bp->b_flag |= BFARGS;	/* treat this as an argument */
	    if (firstbp == 0) {
		firstbp = bp;
		if (!appflag) {
		    for (bp = bheadp; bp; bp = bp_next) {
			bp_next = bp->b_bufp;
			if (bp != firstbp
			    && !b_is_temporary(bp))
			    kill_that_buffer(bp);
		    }
		}
	    }
	}
	if (find_bp(firstbp) != 0) {
	    status = bp2swbuffer(firstbp, FALSE, TRUE);
	    if (status == ABORT)
		reset_to_unnamed(firstbp);
	}
    }
    returnCode(status);
}

/* ARGSUSED */
int
filefind(int f GCC_UNUSED, int n GCC_UNUSED)
{
    return set_files_to_edit("Find file: ", TRUE);
}

/* ARGSUSED */
int
viewfile(int f GCC_UNUSED, int n GCC_UNUSED)
{				/* visit a file in VIEW mode */
    char fname[NFILEN];		/* file user wishes to find */
    int s;			/* status return */
    char *actual;
    static TBUFF *last;

    if ((s = mlreply_file("View file: ", &last, FILEC_READ | FILEC_EXPAND,
			  fname)) == TRUE) {
	while ((actual = filec_expand()) != 0) {
	    if ((s = getfile(actual, FALSE)) != TRUE)
		break;
	    /* if we succeed, put it in view mode */
	    make_local_b_val(curwp->w_bufp, MDVIEW);
	    set_b_val(curwp->w_bufp, MDVIEW, TRUE);
	    markWFMODE(curwp->w_bufp);
	}
    }
    return s;
}

/* utility routine for no. of lines read */
static void
readlinesmsg(int n, int s, const char *f, int rdo)
{
    char fname[NFILEN], *short_f;
    const char *m;

    /* if "f" is a pipe cmd, it can be arbitrarily long */
    vl_strncpy(fname, f, sizeof(fname));
    fname[sizeof(fname) - 1] = '\0';

    short_f = shorten_path(fname, TRUE);
    switch (s) {
    case FIOBAD:
	m = "Incomplete line, ";
	break;
    case FIOERR:
	m = "I/O Error, ";
	break;
    case FIOMEM:
	m = "Not enough memory, ";
	break;
    case FIOABRT:
	m = "Aborted, ";
	break;
    default:
	m = "";
	break;
    }
    if (!global_b_val(MDTERSE))
	mlwrite("[%sRead %d line%s from \"%s\"%s]", m,
		n, PLURAL(n), short_f, rdo ? "  (read-only)" : "");
    else
	mlforce("[%s%d lines]", m, n);
}

static void
writelinesmsg(char *fn, int nline, B_COUNT nchar)
{
#define WRITE_FILE_FMT "[%s %s line%s %s char%s to \"%s\"]"

    if (!global_b_val(MDTERSE)) {
	const char *action;
	char *aname, tmp[NFILEN], strlines[128], strchars[128];
	int outlen, lines_len, chars_len;
	if ((aname = is_appendname(fn)) != 0) {
	    fn = aname;
	    action = "Appended";
	} else {
	    action = "Wrote";
	}
	sprintf(strlines, "%d", nline);
	sprintf(strchars, "%lu", nchar);
	lines_len = (int) strlen(strlines);
	chars_len = (int) strlen(strchars);
	outlen = (term.cols - 1) -
	    (
		(sizeof(WRITE_FILE_FMT) - 13) +
		strlen(action) +
		lines_len +
		chars_len +
		strlen(PLURAL(nline)) +
		strlen(PLURAL(nchar))
	    );
	mlforce(WRITE_FILE_FMT,
		action,
		strlines,
		PLURAL(nline),
		strchars,
		PLURAL(nchar),
		path_trunc(fn, outlen, tmp, sizeof(tmp)));
    } else {
	mlforce("[%d lines]", nline);
    }
#undef WRITE_FILE_FMT
}

static void
set_rdonly_modes(BUFFER *bp, int always)
{
    /* set view mode for read-only files */
    if ((global_g_val(GMDRONLYVIEW))) {
	make_local_b_val(bp, MDVIEW);
	set_b_val(bp, MDVIEW, TRUE);
    }
    /* set read-only mode for read-only files */
    if (always || global_g_val(GMDRONLYRONLY)) {
	make_local_b_val(bp, MDREADONLY);
	set_b_val(bp, MDREADONLY, TRUE);
    }
}

#if COMPLETE_FILES || COMPLETE_DIRS
static void
ff_read_directory(BUFFER *bp, char *fname)
{
    int count = fill_directory_buffer(bp, fname, 0);

    mlwrite("[Read %d line%s]", count, PLURAL(count));
    b_set_flags(bp, BFDIRS);
    set_rdonly_modes(bp, TRUE);
}

/*
 * If we are inserting text from a directory list, make a buffer if necessary,
 * and redirect ffgetline() to read from that buffer.
 */
static int
ff_load_directory(char *fname)
{
    if (ffstatus == file_is_internal) {
	char temp[NFILEN];
	BUFFER *bp = find_b_file(lengthen_path(vl_strncpy(temp, fname,
							  sizeof(temp))));

	if (bp == 0)
	    bp = getfile2bp(temp, FALSE, FALSE);

	if (bp == 0)
	    return FIOERR;

	ffbuffer = bp;
	ff_read_directory(bp, temp);
	ffcursor = lforw(buf_head(bp));
	bp->b_active = TRUE;
    }
    return FIOSUC;
}
#endif

/*
 * Insert file "fname" into the kill register.  Called by insert file command.
 * Return the final status of the read as true/false/abort.
 */
static int
kifile(char *fname)
{
    int s;
    int nline;
    size_t i;
    size_t nbytes;

    TRACE((T_CALLED "kifile(%s)\n", NONNULL(fname)));

    ksetup();
    if ((s = ffropen(fname)) == FIOERR) {	/* Hard file open */
	goto out;
    } else if (s == FIOFNF) {	/* File not found */
	returnCode(no_such_file(fname));
#if COMPLETE_FILES || COMPLETE_DIRS
    } else if ((s = ff_load_directory(fname)) == FIOERR) {
	goto out;
#endif
    } else {

	nline = 0;
#if OPT_ENCRYPT
	if ((s = vl_resetkey(curbp, fname)) == TRUE)
#endif
	{
	    mlwrite("[Reading...]");
	    CleanToPipe(FALSE);
	    while ((s = ffgetline(&nbytes)) <= FIOSUC) {
		for (i = 0; i < nbytes; ++i)
		    if (!kinsert(fflinebuf[i]))
			returnCode(FIOMEM);
		if ((s == FIOSUC) && !kinsert('\n')) {
		    s = FIOMEM;
		    goto out;
		}
		++nline;
		if (s < FIOSUC)
		    break;
	    }
	    CleanAfterPipe(FALSE);
	}
	kdone();
	(void) ffclose();	/* Ignore errors.       */
	readlinesmsg(nline, s, fname, FALSE);
    }

  out:
    returnCode(FIO2Status(s));
}

/*
 * Insert a file into the current buffer.  This is really easy; all you do is
 * find the name of the file, and call the standard "insert a file into the
 * current buffer" code.
 */
/* ARGSUSED */
int
insfile(int f GCC_UNUSED, int n GCC_UNUSED)
{
    int status;
    char fname[NFILEN];
    static TBUFF *last;

    TRACE((T_CALLED "insfile(%d, %d)\n", f, n));

    if (!calledbefore) {
	if ((status = mlreply_file("Insert file: ", &last,
				   FILEC_READ | FILEC_PROMPT, fname)) != TRUE)
	    returnCode(status);
    }

    if (ukb == 0)
	status = ifile(fname, TRUE, (FILE *) 0);
    else
	status = kifile(fname);

    if (status == ABORT) {
	TRACE(("undo insert to recover memory\n"));
	undo(FALSE, 1);
    }
    returnCode(status);
}

BUFFER *
getfile2bp(const char *fname,	/* file name to find */
	   int ok_to_ask,
	   int cmdline)
{
    BUFFER *bp = cmdline ? NULL : find_b_name(fname);
    int s;
    char bname[NBUFN];		/* buffer name to put file */
    char nfname[NFILEN];	/* canonical form of 'fname' */
#ifdef CAN_CHECK_INO
    int have_fuid = FALSE;
    FUID fuid;
#endif

    /* user may have renamed buffer to look like filename */
    if (bp == NULL
	|| (strlen(fname) > (size_t) NBUFN - 1)) {

	/* It's not already here by that buffer name.
	 * Try to find it assuming we're given the file name.
	 */
	(void) lengthen_path(vl_strncpy(nfname, fname, sizeof(nfname)));
	if (is_internalname(nfname)) {
	    mlforce("[Buffer not found]");
	    return 0;
	}
#ifdef CAN_CHECK_INO
	if (global_g_val(GMDUNIQ_BUFS)) {
	    have_fuid = fileuid_get(nfname, &fuid);
	    if (have_fuid) {
		for_each_buffer(bp) {
		    /* is the same unique file */
		    if (fileuid_same(bp, &fuid))
			return bp;
		}
	    }
	}
#endif
	for_each_buffer(bp) {
	    /* is it here by that filename? */
	    if (same_fname(nfname, bp, FALSE)) {
		return bp;
	    }
	}

	/* it's not here */
	makename(bname, nfname);	/* New buffer name.     */
	/* make sure the buffer name doesn't exist */
	while ((bp = find_b_name(bname)) != NULL) {
	    if (!b_is_changed(bp) && is_empty_buf(bp) &&
		!ffexists(bp->b_fname)) {
		/* empty, unmodified, and non-existent --
		   then it's okay to re-use this buffer */
		bp->b_active = FALSE;
		ch_fname(bp, nfname);
		return bp;
	    }
	    /* make a new name if it conflicts */
	    unqname(bname);
	    if (!ok_to_ask || !global_g_val(GMDWARNRENAME))
		continue;
	    hst_glue(' ');
	    s = mlreply("Will use buffer name: ", bname, sizeof(bname));
	    if (s == ABORT)
		return 0;
	    if (s == FALSE || bname[0] == EOS)
		makename(bname, fname);
	}
	/* okay, we've got a unique name -- create it */
	if (bp == NULL && (bp = bfind(bname, 0)) == NULL) {
	    mlwarn("[Cannot create buffer]");
	    return 0;
	}
	/* switch and read it in. */
	ch_fname(bp, nfname);
    }
    return bp;
}

int
no_file_found(void)
{
    mlforce("[No file found]");
    return FALSE;
}

int
no_file_name(const char *fname)
{
    if (is_internalname(fname)) {
	mlwarn("[No filename]");
	return TRUE;
    }
    return FALSE;
}

int
getfile(char *fname,		/* file name to find */
	int lockfl)		/* check the file for locks? */
{
    BUFFER *bp;

    if (isEmpty(fname))
	return no_file_found();

    /* if there are no path delimiters in the name, then the user
       is likely asking for an existing buffer -- try for that
       first */
    if ((bp = find_b_file(fname)) == 0
	&& ((strlen(fname) > (size_t) NBUFN - 1)	/* too big to be a bname */
	    ||maybe_pathname(fname)	/* looks a lot like a filename */
	    ||(bp = find_b_name(fname)) == NULL)) {
	/* oh well.  canonicalize the name, and try again */
	bp = getfile2bp(fname, !clexec, FALSE);
	if (!bp)
	    return FALSE;
    }
    return bp2swbuffer(bp, isShellOrPipe(bp->b_fname), lockfl);
}

#if OPT_DOSFILES
static int
check_percent_crlf(BUFFER *bp, int doslines, int unixlines)
{
    double total = (doslines + unixlines) * b_val(bp, VAL_PERCENT_CRLF);
    double value = (doslines * 100);
    return (total > 0) && (value >= total);
}

/*
 * Scan a buffer to see if it contains more lines terminated by CR-LF than by
 * LF alone.  If so, set the DOS-mode to true, otherwise false.
 *
 * If the given buffer is one that we're sourcing (e.g., with ":so"), or
 * specified in initialization, we require that _all_ of the lines end with
 * ^M's before deciding that it is DOS-style.  That is to protect us from
 * accidentally trimming the ^M's from a :map command.
 */
static void
apply_dosmode(BUFFER *bp, int doslines, int unixlines)
{
    int result;

    if (bp->b_flag & BFEXEC) {
	if (doslines && !unixlines) {
	    result = TRUE;
	} else {
	    result = FALSE;
	}
    } else {
	result = check_percent_crlf(bp, doslines, unixlines);
    }
    TRACE2(("apply_dosmode %d dos:%d unix:%d\n", result, doslines, unixlines));

    /*
     * Make 'dos' and 'recordseparator' modes local, since this
     * transformation should apply only to the specified file.
     */
    set_record_sep(bp, result ? RS_CRLF : RS_LF);
}

static void
strip_if_dosmode(BUFFER *bp)
{
    if (b_val(bp, MDDOS)) {	/* if it _is_ a dos file, strip 'em */
	LINE *lp;
	int flag = FALSE;

	TRACE(("stripping CR's for dosmode\n"));
	if (b_val(bp, MDUNDO_DOS_TRIM))
	    mayneedundo();

	for_each_line(lp, bp) {
	    int len = llength(lp);
	    if (len > 0) {
		int chr = lgetc(lp, len - 1);
		if ((chr == '\r')
		    || (len == 1 && chr == '\032')) {
		    if (b_val(bp, MDUNDO_DOS_TRIM)) {
			copy_for_undo(lp);
		    }
		    llength(lp)--;
		    flag = TRUE;
		}
	    }
	}

	/*
	 * Force the screen to repaint if we changed anything.  This
	 * operation is not undoable, otherwise we would call chg_buff().
	 */
	if (flag) {
	    if (b_val(bp, MDUNDO_DOS_TRIM))
		chg_buff(bp, WFHARD);
	    else
		set_winflags(TRUE, WFHARD);
	    b_clr_counted(bp);
	}
    }
}

static void
guess_dosmode(BUFFER *bp)
{
    int doslines = 0, unixlines = 0;
    LINE *lp;

    /* first count 'em */
    for_each_line(lp, bp) {
	if (llength(lp) > 0 &&
	    lgetc(lp, llength(lp) - 1) == '\r') {
	    doslines++;
	} else {
	    unixlines++;
	}
    }
    /* then strip 'em */
    apply_dosmode(bp, doslines, unixlines);
    strip_if_dosmode(bp);
    TRACE(("guess_dosmode %d\n", b_val(bp, MDDOS)));
}

/*
 * Forces the current buffer to be either 'dos' or 'unix' format.
 */
void
explicit_dosmode(BUFFER *bp, RECORD_SEP record_sep)
{
    TRACE(("explicit_dosmode(%s)\n",
	   choice_to_name(&fsm_recordsep_blist, record_sep)));

    set_record_sep(bp, record_sep);
}

/*
 * Recompute the buffer size, redisplay the [Settings] buffer.
 */
static int
modified_record_sep(RECORD_SEP record_sep)
{
    TRACE((T_CALLED "modified_record_sep(%s)\n",
	   choice_to_name(&fsm_recordsep_blist, record_sep)));

    explicit_dosmode(curbp, record_sep);
    guess_dosmode(curbp);
    explicit_dosmode(curbp, record_sep);	/* ignore the guess - only want to strip CR's */

    b_clr_counted(curbp);
    bsizes(curbp);

    returnCode(TRUE);
}

/*
 * Forces the current buffer to be in DOS-mode, stripping any trailing CR's.
 */
/*ARGSUSED*/
int
set_rs_crlf(int f GCC_UNUSED, int n GCC_UNUSED)
{
    return modified_record_sep(RS_CRLF);
}

/* as above, but forces unix-mode instead */
/*ARGSUSED*/
int
set_rs_lf(int f GCC_UNUSED, int n GCC_UNUSED)
{
    return modified_record_sep(RS_LF);
}

/* as above, but forces macintosh-mode instead */
/*ARGSUSED*/
int
set_rs_cr(int f GCC_UNUSED, int n GCC_UNUSED)
{
    return modified_record_sep(RS_CR);
}
#endif /* OPT_DOSFILES */

#if OPT_LCKFILES
static void
grab_lck_file(BUFFER *bp, char *fname)
{
    /* Write the lock */
    if (global_g_val(GMDUSEFILELOCK) &&
	!isShellOrPipe(fname) &&
	!b_val(bp, MDVIEW)) {
	char locker[100];
	char *locked_by;

	if (!set_lock(fname, locker, sizeof(locker))) {
	    /* we didn't get it */
	    if ((locked_by = strmalloc(locker)) != 0) {
		make_local_b_val(bp, MDVIEW);
		set_b_val(bp, MDVIEW, TRUE);
		make_local_b_val(bp, MDLOCKED);
		set_b_val(bp, MDLOCKED, TRUE);
		make_local_b_val(bp, VAL_LOCKER);
		set_b_val_ptr(bp, VAL_LOCKER, locked_by);
		markWFMODE(bp);
	    }
	}
    }
}
#endif

/*
 * Set the buffer's window traits to sane values after reading the buffer from
 * a file.  We have to do this, since we may copy the buffer traits back to the
 * window when making a buffer visible after source'ing it from a file.
 */
static void
init_b_traits(BUFFER *bp)
{
    bp->b_dot.l = lforw(buf_head(bp));
    bp->b_dot.o = 0;
    bp->b_wline = bp->b_dot;
    bp->b_lastdot = bp->b_dot;
#if WINMARK
    bp->b_mark = bp->b_dot;
#endif
#if OPT_MOUSE
    bp->b_wtraits.insmode = FALSE;
#endif
}

/*
 * Analyze the file to determine its format
 */
static RECORD_SEP
guess_recordseparator(BUFFER *bp, UCHAR * buffer, B_COUNT length, L_NUM * lines)
{
    B_COUNT count_lf = 0;
    B_COUNT count_cr = 0;
    B_COUNT count_dos = 0;	/* CRLF's */
    B_COUNT count_unix = 0;	/* LF's w/o preceding CR */
    B_COUNT n;
    RECORD_SEP result;

    *lines = 0;
    for (n = 0; n < length; ++n) {
	switch (buffer[n]) {
	case '\r':
	    ++count_cr;
	    break;
	case '\n':
	    ++count_lf;
	    if (n != 0) {
		if (buffer[n - 1] == '\r')
		    ++count_dos;
		else
		    ++count_unix;
	    }
	    break;
	}
    }

    TRACE(("guess_recordseparator assume %s CR:%ld, LF:%ld, CRLF:%ld\n",
	   global_b_val(MDDOS) ? "dos" : "unix",
	   (long) (count_cr - count_dos), (long) count_unix, (long) count_dos));

    if (count_lf != 0) {
	result = RS_LF;
	if (global_b_val(MDDOS)) {
	    if ((bp->b_flag & BFEXEC) != 0) {
		result = (count_dos && !count_unix) ? RS_CRLF : RS_LF;
	    } else if (check_percent_crlf(bp, count_dos, count_unix)) {
		result = RS_CRLF;
	    }
	}
	if (buffer[length - 1] != '\n')
	    ++count_lf;
	*lines = count_lf;
    } else if (count_cr != 0) {
	result = RS_CR;
	if (buffer[length - 1] != '\r')
	    ++count_cr;
	*lines = count_cr;
    } else {
	*lines = 1;		/* we still need a line, to allocate data */
	result = RS_DEFAULT;
    }

    TRACE(("...line count = %ld, format=%s\n", (long) *lines,
	   choice_to_name(&fsm_recordsep_blist, result)));
    return result;
}

/*
 * Return the index in buffer[] past the end of the record we're pointing to
 * with 'offset'.
 */
static B_COUNT
next_recordseparator(UCHAR * buffer, B_COUNT length, RECORD_SEP rscode,
		     B_COUNT offset)
{
    B_COUNT result = offset;
    B_COUNT n;
    int done = FALSE;

    for (n = offset; !done && (n < length); ++n) {
	switch (buffer[n]) {
	case '\r':
	    if (rscode == RS_CR) {
		result = n + 1;
		done = TRUE;
	    }
	    break;
	case '\n':
	    if (rscode == RS_CRLF || rscode == RS_LF) {
		result = n + 1;
		done = TRUE;
	    }
	    break;
	}
    }
    if (!done)
	result = length;
    return result;
}

/*
 * If reading from an external file, read the whole file into memory at once
 * and split it into lines.  That is potentially much faster than allocating
 * each line separately.
 */
static int
quickreadf(BUFFER *bp, int *nlinep)
{
    B_COUNT length;
    B_COUNT request;
    B_COUNT offset;
    LINE *lp;
    L_NUM lineno = 0;
    L_NUM nlines;
    RECORD_SEP rscode;
    UCHAR *buffer;
    int rc;

    TRACE((T_CALLED "quickreadf(buffer=%s, file=%s)\n", bp->b_bname, bp->b_fname));

    beginDisplay();
    if (ffsize(&request) < 0) {
	mlwarn("[Can't size file]");
	rc = FIOERR;
    } else if (request == 0) {
	/* avoid malloc(0) problems down below; let slowreadf() do the work */
	rc = FIONUL;
    } else if ((buffer = castalloc(UCHAR, request)) == NULL) {
	rc = FIOMEM;
    }
#if OPT_ENCRYPT
    else if ((rc = vl_resetkey(curbp, (const char *) buffer)) != TRUE) {
	free(buffer);
    }
#endif
    else if (ffread((char *) buffer, request, &length) < 0
#if !SYS_VMS
	/*
	 * For most systems, the advertised size of the file will match the
	 * number of chars that we can read from it.  However, VMS's structured
	 * filetypes such as VFC or VAR/CR will return fewer since we are not
	 * using the structure information.
	 */
	     || (length != request)
#endif
	) {
	free(buffer);
	mlerror("reading");
	rc = FIOERR;
    } else {
#if OPT_ENCRYPT
	if (b_val(bp, MDCRYPT)
	    && bp->b_cryptkey[0]) {	/* decrypt the file */
	    vl_setup_encrypt(bp->b_cryptkey);
	    vl_encrypt_blok((char *) buffer, (UINT) length);
	}
#endif
#if OPT_MULTIBYTE
	decode_bom(bp, buffer, &length);
	deduce_charset(bp, buffer, &length, TRUE);
#endif

	/*
	 * Analyze the file to determine its format:
	 */
	rscode = guess_recordseparator(bp, buffer, length, &nlines);

	/*
	 * Modify readin()'s setting for newline mode if needed:
	 */
	if (buffer[length - 1] != (UCHAR) (rscode == RS_CR ? '\r' : '\n')) {
	    set_b_val(bp, MDNEWLINE, FALSE);
	}

	/* allocate all of the line structs we'll need */
	if ((bp->b_LINEs = typeallocn(LINE, nlines)) == NULL) {
	    free(buffer);
	    ffrewind();
	    rc = FIOMEM;
	} else {
	    bp->b_ltext = buffer;
	    bp->b_ltext_end = bp->b_ltext + length;
	    bp->b_bytecount = length;
	    bp->b_linecount = nlines;
	    bp->b_LINEs_end = bp->b_LINEs + nlines;

	    /* loop through the buffer again, creating
	       line data structure for each line */
	    for (lp = bp->b_LINEs, offset = 0; lp != bp->b_LINEs_end; ++lp) {
		B_COUNT next = next_recordseparator(buffer, length, rscode, offset);
		if (next == offset)
		    break;
#if !SMALLER
		lp->l_number = ++lineno;
#endif
		llength(lp) = next - offset - 1;
		if (!b_val(bp, MDNEWLINE) && next == length)
		    llength(lp) += 1;
		lp->l_size = llength(lp) + 1;
		lvalue(lp) = (char *) (buffer + offset);
		set_lforw(lp, lp + 1);
		if (lp != bp->b_LINEs)
		    set_lback(lp, lp - 1);
		lsetclear(lp);
		lp->l_nxtundo = 0;
#if OPT_LINE_ATTRS
		lp->l_attrs = NULL;
#endif
		decode_charset(bp, lp);
		offset = next;
	    }
	    lp--;		/* point at last line again */

	    /* connect the end of the list */
	    set_lforw(lp, buf_head(bp));
	    set_lback(buf_head(bp), lp);

	    /* connect the front of the list */
	    set_lback(bp->b_LINEs, buf_head(bp));
	    set_lforw(buf_head(bp), bp->b_LINEs);

	    init_b_traits(bp);

	    *nlinep = nlines;

	    set_record_sep(bp, rscode);
	    strip_if_dosmode(bp);

	    rc = FIOSUC;
	}
    }
    endofDisplay();
    returnCode(rc);
}

/*
 * Read file "fname" into a buffer, blowing away any text found there.  Returns
 * the final status of the read.
 *
 * fname  - name of file to read
 * lockfl - check for file locks?
 * bp     - read into this buffer
 * mflg   - print messages?
 */
/* ARGSUSED */
int
readin(char *fname, int lockfl, BUFFER *bp, int mflg)
{
    WINDOW *wp;
    int s;
    int nline;
#if OPT_ENCRYPT
    int local_crypt = valid_buffer(bp)
    && is_local_val(bp->b_values.bv, MDCRYPT);
#endif

    TRACE((T_CALLED "readin(fname=%s, lockfl=%d, bp=%p, mflg=%d)\n",
	   NONNULL(fname), lockfl, bp, mflg));

#if SYS_WINNT && DISP_NTWIN
    ffshadow = file_is_closed;	/* an assumption */
#endif

    if (bp == 0)		/* doesn't hurt to check */
	returnCode(FALSE);

    if (*fname == EOS) {
	TRACE(("...called with NULL fname\n"));
	returnCode(TRUE);	/* we do not want to do that */
    }

    if ((s = bclear(bp)) != TRUE)	/* Might be old.    */
	returnCode(s);

#if OPT_ENCRYPT
    /* bclear() gets rid of local flags */
    if (local_crypt) {
	make_local_b_val(bp, MDCRYPT);
	set_b_val(bp, MDCRYPT, TRUE);
    }
#endif

    b_clr_flags(bp, BFINVS | BFCHG);
    ch_fname(bp, fname);
    fname = bp->b_fname;	/* this may have been b_fname! */
#if OPT_DOSFILES
    make_local_b_val(bp, MDDOS);
    /* assume that if our OS wants it, that the buffer will have CRLF
     * lines.  this may change when the file is read, based on actual
     * line counts, below.  otherwise, if there's an error, or the
     * file doesn't exist, we will keep this default.
     */
    set_b_val(bp, MDDOS, CRLF_LINES);
#endif
    make_local_b_val(bp, MDNEWLINE);
    set_b_val(bp, MDNEWLINE, TRUE);	/* assume we've got it */

    if ((s = ffropen(fname)) == FIOERR) {	/* Hard file error */
	/* do nothing -- error has been reported,
	   and it will appear as empty buffer */
	/*EMPTY */ ;
    } else if (s == FIOFNF) {	/* File not found */
#if SYS_WINNT && DISP_NTWIN
	ffshadow = file_is_new;
#endif
	if (mflg)
	    mlwrite("[New file]");
#if COMPLETE_FILES || COMPLETE_DIRS
    } else if (ffstatus == file_is_internal) {
	ff_read_directory(bp, fname);
#endif
    } else {

	if (mflg) {
#define READING_FILE_FMT "[Reading %s]"

	    int outlen;
	    char tmp[NFILEN];

	    outlen = (term.cols - 1) -
		(sizeof(READING_FILE_FMT) - 3);
	    mlforce(READING_FILE_FMT,
		    path_trunc(fname, outlen, tmp, sizeof(tmp)));
#undef READING_FILE_FMT
	}
#if OPT_VMS_PATH
	if (!isInternalName(bp->b_fname))
	    fname = resolve_filename(bp);
#endif
	/* start reading */
	nline = 0;
#if OPT_WORKING
	max_working = cur_working = old_working = 0;
#endif

	if (ffstatus == file_is_pipe
	    || global_g_val(GVAL_READER_POLICY) == RP_SLOW) {
	    s = slowreadf(bp, &nline);
	} else {
	    s = quickreadf(bp, &nline);
	    if (s == FIONUL
		|| (s == FIOMEM
		    && global_g_val(GVAL_READER_POLICY) == RP_BOTH))
		s = slowreadf(bp, &nline);
	}

#if OPT_WORKING
	cur_working = 0;
#endif
	if (s == FIOERR) {
	    /*EMPTY */ ;
	} else {

	    if (s == FIOBAD)	/* last line is incomplete */
		set_b_val(bp, MDNEWLINE, FALSE);
	    b_clr_changed(bp);
#if OPT_FINDERR
	    if (ffstatus == file_is_pipe)
		set_febuff(bp->b_bname);
#endif
	    (void) ffclose();	/* Ignore errors.       */
	    if (mflg)
		readlinesmsg(nline, s, fname, ffronly(fname));

	    if (ffronly(fname)) {
		set_rdonly_modes(bp, isShellOrPipe(fname));
	    }

	    bp->b_active = TRUE;
	    bp->b_lines_on_disk = bp->b_linecount;
	}

    }

    /*
     * Set the majormode if the file's suffix matches.
     */
    if (s < FIOERR) {
	infer_majormode(bp);
    }

    for_each_window(wp) {
	if (wp->w_bufp == bp) {
	    init_window(wp, bp);
	}
    }
    if (s < FIOERR)
	imply_alt(fname, FALSE, lockfl);
    updatelistbuffers();

#if OPT_LCKFILES
    if (lockfl && s != FIOERR)
	grab_lck_file(bp, fname);
#endif
#if OPT_MODELINE
    /* do this before the $read-hook, so one can set a majormode in the
     * modeline, causing the file to be colored.
     */
    do_modelines(bp);
#endif
#if OPT_HOOKS
    if (s <= FIOEOF && (bp == curbp))
	run_readhook();
#endif
#ifdef GMDCD_ON_OPEN
    if (global_g_val(GMDCD_ON_OPEN) > 0)
	set_directory_from_file(bp);
#endif
    b_match_attrs_dirty(bp);
    returnCode(FIO2Status(s));
}

/*
 * Read the file into a given buffer, setting dot to the first line.
 */
int
bp2readin(BUFFER *bp, int lockfl)
{
    int status = readin(bp->b_fname, lockfl, bp, TRUE);

    if (status == TRUE) {
	bp->b_active = TRUE;

#if SYS_WINNT && DISP_NTWIN
	if (status &&
	    (!(isShellOrPipe(bp->b_fname) ||
	       ffshadow == file_is_new ||
	       b_is_registered(bp)))) {
	    /* save file path to windows registry for later recall */

	    store_recent_file_or_folder(bp->b_fname, TRUE);
	    b_set_registered(bp);
	}
#endif
    }

    return status;
}

/*
 * Read a file slowly, e.g., a line at a time, for instance from a pipe.
 */
int
slowreadf(BUFFER *bp, int *nlinep)
{
    int s;
    size_t len;
#if OPT_DOSFILES
    int doslines = 0, unixlines = 0;
#endif
#if SYS_UNIX || SYS_MSDOS || SYS_OS2 || SYS_WINNT	/* i.e., we can read from a pipe */
    USHORT flag = 0;
    int done_update = FALSE;
#endif
#if SYS_UNIX && OPT_SHELL
    time_t last_updated = time((time_t *) 0);
#endif

    TRACE((T_CALLED "slowreadf(buffer=%s, file=%s)\n", bp->b_bname, bp->b_fname));

#if OPT_ENCRYPT
    if ((s = vl_resetkey(curbp, curbp->b_fname)) != TRUE)
	returnCode(s);
#endif
    b_set_counted(bp);		/* make 'addline()' do the counting */
    b_set_reading(bp);
    make_local_b_val(bp, MDDOS);	/* keep it local, if not */
    bp->b_lines_on_disk = 0;
    while ((s = ffgetline(&len)) <= FIOSUC) {
#if OPT_MULTIBYTE
	/*
	 * ffgetline() does the checks for UTF-16, etc., since it is in the
	 * right place to account for alignment bytes.  Copy the attributes
	 * from its buffer to ours so the decode_charset() call will work.
	 */
	if (bp->b_lines_on_disk == 0) {
	    COPY_B_VAL(bp, btempp, VAL_BYTEORDER_MARK);
	    COPY_B_VAL(bp, btempp, VAL_FILE_ENCODING);
	}
	bp->implied_BOM = btempp->implied_BOM;
#endif
#if OPT_DOSFILES
	/*
	 * Strip CR's if we are reading in DOS-mode.  Otherwise,
	 * keep any CR's that we read.
	 */
	if (global_b_val(MDDOS)) {
	    if (len != 0 && fflinebuf[len - 1] == '\r') {
		doslines++;
	    } else {
		unixlines++;
	    }
	}
#endif
	if (addline(bp, fflinebuf, (int) len) != TRUE) {
	    s = FIOMEM;
	    break;
	}
#if SYS_UNIX || SYS_MSDOS || SYS_OS2 || SYS_WINNT
	else {
	    decode_charset(bp, lback(buf_head(bp)));
	    bp->b_lines_on_disk += 1;
	    /* reading from a pipe, and internal? */
	    if (slowtime(&last_updated)) {
		WINDOW *wp;

		flag |= (WFEDIT | WFFORCE);

		if (!done_update || bp->b_nwnd > 1)
		    flag |= WFHARD;
		for_each_window(wp) {
		    if (wp->w_bufp == bp) {
			wp->w_line.l =
			    lforw(buf_head(bp));
			wp->w_dot.l =
			    lback(buf_head(bp));
			wp->w_dot.o = 0;
			wp->w_flag |= flag;
			wp->w_force = -1;
		    }
		}

		/* track changes in dosfile as lines arrive */
#if OPT_DOSFILES
		if (global_b_val(MDDOS))
		    apply_dosmode(bp, doslines, unixlines);
#endif
		curwp->w_flag |= WFMODE | WFKILLS;
		if (!update(TRUE)) {
		    s = FIOERR;
		    break;
		}
#if DISP_X11
		/* to pick up intrc if it's been hit */
		x_move_events();
#endif
		done_update = TRUE;
		flag = 0;
	    } else {
		flag |= WFHARD;
	    }

	}
#endif
	++(*nlinep);
	if (s == FIOBAD) {
	    set_b_val(bp, MDNEWLINE, FALSE);
	    break;
	}
    }
#if OPT_MULTIBYTE
    /*
     * Look for UTF-8 encoding when we have the entire buffer, since only a
     * small part of it may be distinct from ASCII.
     */
    if (b_is_enc_AUTO(bp)) {
	LINE *lp;
	int check, found = SORTOFTRUE;

	TRACE(("...try looking for UTF-8\n"));
	for_each_line(lp, bp) {
	    if (llength(lp) > 0) {
		check = check_utf8((UCHAR *) lvalue(lp), llength(lp));
		if (check == FALSE) {
		    found = FALSE;
		} else if (check == TRUE) {
		    found = TRUE;
		}
	    }
	}
	if (found == TRUE) {
	    found_utf8(bp);
	}
    }
#endif
#if OPT_DOSFILES
    if (global_b_val(MDDOS)) {
	apply_dosmode(bp, doslines, unixlines);
	strip_if_dosmode(bp);
    }
#endif
    init_b_traits(bp);
    b_clr_reading(bp);
    returnCode(s);
}

/*
 * Take a (null-terminated) file name, and from it fabricate a buffer name.
 * This routine knows about the syntax of file names on the target system.  I
 * suppose that this information could be put in a better place than a line of
 * code.
 */
void
makename(char *bname, const char *fname)
{
    char *fcp;
    char *bcp;
    int j;
    char temp[NFILEN];

    TRACE(("makename(%s)\n", fname));

    *bname = EOS;
    fcp = skip_string(vl_strncpy(temp, fname, sizeof(temp)));
#if OPT_VMS_PATH
    if (is_vms_pathname(temp, TRUE)) {
	vms2unix_path(temp, temp);
	(void) strncpy0(bname, pathleaf(temp), NBUFN);
	strip_version(bname);
    } else if (is_vms_pathname(temp, FALSE)) {
	for (; fcp > temp && !strchr(":]", fcp[-1]); fcp--) {
	    ;
	}
	(void) strncpy0(bname, fcp, NBUFN);
	strip_version(bname);
	(void) mklower(bname);
    } else
#endif
    {
	/* trim trailing whitespace */
	while (fcp != temp && (isBlank(fcp[-1])
#if SYS_UNIX || OPT_MSDOS_PATH	/* trim trailing slashes as well */
			       || is_slashc(fcp[-1])
#endif
	       )) {
	    *(--fcp) = EOS;
	}
	fcp = skip_space_tab(temp);

#if SYS_UNIX || SYS_MSDOS || SYS_VMS || SYS_OS2 || SYS_WINNT
	bcp = bname;
	if (isShellOrPipe(fcp)) {
	    /* ...it's a shell command; bname is first word */
	    *bcp++ = SCRTCH_LEFT[0];
	    *bcp++ = SHPIPE_LEFT[0];
	    do {
		++fcp;
	    } while (isSpace(*fcp));

	    (void) vl_strncpy(bcp, fcp, NBUFN - 2);

	    for (j = 4; (j < NBUFN) && isPrint(*bcp); j++) {
		bcp++;
	    }
	    (void) strcpy(bcp, SCRTCH_RIGHT);

	} else {

	    (void) vl_strncpy(bcp, pathleaf(fcp), NBUFN);

#if SYS_UNIX
	    /* UNIX filenames can have any characters (other than EOS!).
	     * Refuse (rightly) to deal with leading/trailing blanks, but allow
	     * embedded blanks.  For this special case, ensure that the buffer
	     * name has no blanks, otherwise it is difficult to reference from
	     * commands.
	     */
	    for (j = 0; j < NBUFN; j++) {
		if (*bcp == EOS)
		    break;
		if (isSpace(*bcp))
		    *bcp = '-';
		bcp++;
	    }
#endif
	}

#else /* !(SYS_UNIX||SYS_VMS||SYS_MSDOS) */

	bcp = skip_string(fcp);
	{
	    char *cp2 = bname;
	    vl_strncpy(bname, bcp, NBUFN);
	    cp2 = strchr(bname, ':');
	    if (cp2)
		*cp2 = EOS;
	}
#endif
    }
    if (*bname == EOS)
	(void) strcpy(bname, NO_BNAME);
    TRACE((" -> '%s'\n", bname));
}

/* generate a unique name for a buffer */
void
unqname(char *name)
{
    size_t j;
    char newname[NBUFN * 2];
    char suffixbuf[NBUFN];
    int suffixlen;
    int adjust;
    int i = 0;
    size_t k;

    j = strlen(name);
    if (j == 0)
	j = strlen(strcpy(name, NO_BNAME));

    /* check to see if this name is in use */
    vl_strncpy(newname, name, NBUFN);
    adjust = is_scratchname(newname);
    strcpy(suffixbuf, "-");
    while (find_b_name(newname) != NULL) {
	/* from "foo" create "foo-1" or "foo-a1b5" */
	/* from "thisisamuchlongernam" create
	   "thisisamuchlongern-1" or
	   "thisisamuchlong-a1b5" */
	/* that is, put suffix at end if it fits, or else
	   overwrite some of the name to make it fit */
	/* the suffix is in "base 36" */
	suffixlen = format_int(suffixbuf + 1, ++i, 36) + 1;
	k = NBUFN - 1 - suffixlen;
	if (j < k)
	    k = j;
	if (adjust) {
	    strcpy(&newname[k - 1], suffixbuf);
	    strcat(newname, SCRTCH_RIGHT);
	} else
	    strcpy(&newname[k], suffixbuf);
    }
    strncpy0(name, newname, NBUFN);
    TRACE(("unqname ->%s\n", name));
}

/*
 * Ask for a file name, and write the
 * contents of the current buffer to that file.
 */
int
filewrite(int f, int n)
{
    int s;
    char fname[NFILEN];
    int forced = (f && n == SPECIAL_BANG_ARG);

    if (more_named_cmd()) {
	if ((s = mlreply_file("Write to file: ", (TBUFF **) 0,
			      forced ? FILEC_WRITE2
			      : FILEC_WRITE, fname)) != TRUE)
	    return s;
    } else
	(void) vl_strncpy(fname, curbp->b_fname, sizeof(fname));

    if ((s = writeout(fname, curbp, forced, TRUE)) == TRUE)
	unchg_buff(curbp, 0);
    return s;
}

/*
 * Save the contents of the current
 * buffer in its associatd file.
 * Error if there is no remembered file
 * name for the buffer.
 */
/* ARGSUSED */
int
filesave(int f, int n)
{
    int s;
    int forced = (f && n == SPECIAL_BANG_ARG);	/* then it was :w! */

    if (no_file_name(curbp->b_fname))
	return FALSE;

    if ((s = writeout(curbp->b_fname, curbp, forced, TRUE)) == TRUE)
	unchg_buff(curbp, 0);
    return s;
}

static void
setup_file_region(BUFFER *bp, REGION * rp)
{
    (void) bsizes(bp);		/* make sure we have current count */
    /* starting at the beginning of the buffer */
    rp->r_orig.l = lforw(buf_head(bp));
    rp->r_orig.o = 0;
    rp->r_size = bp->b_bytecount;
    rp->r_end = bp->b_line;
}

#if OPT_MULTIBYTE
static int
write_encoded_text(BUFFER *bp, const char *buf, int nbuf, const char *ending)
{
    int rc;

    if (b_val(bp, VAL_FILE_ENCODING) > enc_UTF8) {
	int len = encode_charset(bp, buf, nbuf, ending);
	rc = ffputline(bp->encode_utf_buf, len, 0);
    } else {
	rc = ffputline(buf, nbuf, ending);
    }
    return rc;
}
#define FFPUTLINE(bp,buf,len,end) write_encoded_text(bp,buf,len,end)
#else
#define FFPUTLINE(bp,buf,len,end) ffputline(buf,len,end)
#endif

static int
actually_write(REGION * rp, char *fn, int msgf, BUFFER *bp, int forced, int encoded)
{
    int s;
    LINE *lp;
    int nline;
    B_COUNT nchar;
    const char *ending = get_record_sep(bp);
    int len_rs = len_record_sep(bp);
    C_NUM offset = rp->r_orig.o;

    /* this is adequate as long as we cannot write parts of lines */
    int whole_file = ((rp->r_orig.l == lforw(buf_head(bp)))
		      && (rp->r_end.l == buf_head(bp)));

    TRACE((T_CALLED " actually_write\n"));
#if OPT_HOOKS
    if (run_a_hook(&writehook)) {
	/*
	 * The write-hook may have modified the buffer.  Assume
	 * the worst, and reconstruct the region.
	 */
	(void) bsizes(bp);
	if (whole_file
	    || line_no(bp, rp->r_orig.l) > bp->b_linecount
	    || line_no(bp, rp->r_end.l) > bp->b_linecount) {
	    setup_file_region(bp, rp);
	} else {
	    DOT = rp->r_orig;
	    MK = rp->r_end;
	    (void) getregion(rp);
	}
	offset = rp->r_orig.o;
    }
#endif

#if OPT_ENCRYPT
    if ((s = vl_resetkey(curbp, fn)) != TRUE)
	returnCode(s);
#endif

    /* open writes error message, if needed */
    if ((s = ffwopen(fn, forced)) != FIOSUC)
	returnCode(FALSE);

    /* disable "working..." while we are writing - not reading - a pipe, since
     * it may be a quasi-interactive process that we don't want to modify its
     * display.
     */
    if (isShellOrPipe(fn)) {
	beginDisplay();
    }

    /* tell us we're writing */
    if (msgf == TRUE)
	mlwrite("[Writing...]");

    CleanToPipe(TRUE);

    lp = rp->r_orig.l;
    nline = 0;			/* Number of lines     */
    nchar = 0;			/* Number of chars     */

#if OPT_MULTIBYTE
    if ((s = write_bom(bp)) != FIOSUC) {
	goto out;
    }
#endif

    /* first (maybe partial) line and succeeding whole lines */
    while ((rp->r_size + offset) >= line_length(lp)) {
	C_NUM len = llength(lp) - offset;
	char *text = lvalue(lp) + offset;

	/* If this is the last line (and no fragment will be written
	 * after the line), allow 'newline' mode to suppress the
	 * trailing newline.
	 */
	if (rp->r_size <= line_length(lp)) {
	    rp->r_size = 0;
	    if (!b_val(bp, MDNEWLINE))
		ending = "";
	} else {
	    rp->r_size -= line_length(lp);
	}
#if OPT_SELECTIONS
	if (encoded) {
	    TBUFF *temp;
	    if ((temp = encode_attributes(lp, bp, rp)) != 0) {
		text = tb_values(temp);
		len = (int) tb_length(temp);
	    }
	    if ((s = FFPUTLINE(bp, text, len, ending)) != FIOSUC) {
		tb_free(&temp);
		goto out;
	    }
	    tb_free(&temp);
	} else
#endif
	if ((s = FFPUTLINE(bp, text, len, ending)) != FIOSUC)
	    goto out;

	++nline;
	nchar += line_length(lp);
	offset = 0;
	lp = lforw(lp);
    }

    /* last line (fragment) */
    if (rp->r_size > 0) {
	if ((s = FFPUTLINE(bp, lvalue(lp), rp->r_size, NULL)) != FIOSUC)
	    goto out;
	nchar += rp->r_size;
	++nline;		/* it _looks_ like a line */
    }

  out:
    if (s == FIOSUC) {		/* No write error.      */
#if OPT_VMS_PATH
	if (same_fname(fn, bp, FALSE))
	    fn = resolve_filename(bp);
#endif
	s = ffclose();
	if (s == FIOSUC && msgf) {	/* No close error.      */
	    writelinesmsg(fn, nline, nchar);
	}
    } else {			/* Ignore close error   */
	(void) ffclose();	/* if a write error.    */
    }
    if (whole_file) {
	bp->b_linecount =
	    bp->b_lines_on_disk = nline;
    }

    CleanAfterPipe(TRUE);

    if (isShellOrPipe(fn)) {
	endofDisplay();
    }

    if (s != FIOSUC)		/* Some sort of error.      */
	returnCode(FALSE);

#ifdef MDCHK_MODTIME
    set_modtime(bp, fn);
#endif
    fileuid_set_if_valid(bp, fn);
    /*
     * If we've written the unnamed-buffer, rename it according to the file.
     * FIXME: maybe we should do this to all internal-names?
     */
    if (!i_am_dead
	&& whole_file
	&& eql_bname(bp, UNNAMED_BufName)
	&& !isShellOrPipe(fn)
	&& find_b_file(fn) == 0) {
	ch_fname(bp, fn);
	set_buffer_name(bp);
    }

    imply_alt(fn, whole_file, FALSE);
    returnCode(TRUE);
}

static int
file_protection(char *fn)
{
    int result = -1;
    struct stat sb;

    if (!isShellOrPipe(fn)) {
	if (file_stat(fn, &sb) == 0) {
	    if (isFileMode(sb.st_mode))
		result = sb.st_mode & 0777;
	}
    }
    return result;
}

static int
writereg(REGION * rp,
	 const char *given_fn,
	 int msgf,
	 BUFFER *bp,
	 int forced,
	 int encoded)
{
    int status;
    char fname[NFILEN], *fn;
    int protection = -1;

    if (no_file_name(given_fn)) {
	status = FALSE;
    } else if (!forced && b_val(bp, MDREADONLY)
	       && (bp->b_fname == 0 || !strcmp(given_fn, bp->b_fname))) {
	mlwarn("[Buffer mode is \"readonly\"]");
	status = FALSE;
    } else {
	if (isShellOrPipe(given_fn)
	    && bp->b_fname != 0
	    && !strcmp(given_fn, bp->b_fname)
	    && mlyesno("Are you sure (this was a pipe-read)") != TRUE) {
	    mlwrite("File not written");
	    status = FALSE;
	} else {
	    fn = lengthen_path(vl_strncpy(fname, given_fn, sizeof(fname)));
	    if (same_fname(fn, bp, FALSE) && b_val(bp, MDVIEW)) {
		mlwarn("[Can't write-back from view mode]");
		status = FALSE;
	    } else {
#if defined(MDCHK_MODTIME)
		if (!inquire_file_changed(bp, fn))
		    status = FALSE;
		else
#endif
		{
#if SYS_WINNT
		    ULONG prev_access_mask = 0;
		    int write_acl_added = FALSE;
#endif
		    if (forced) {
#if SYS_WINNT
			/*
			 * If running cygwin on an NTFS partition, cygwin's
			 * chmod utility adds ACLs to files to simulate
			 * unix file permissions.  In the case of readonly
			 * files (e.g., $ chmod a=r fn), the win32 posix
			 * interfaces don't work well, if at all.  So make
			 * the current file object a bit more Unix API
			 * friendly, if necessary.
			 */

			write_acl_added = w32_add_write_acl(fn,
							    &prev_access_mask);
#endif
			if (!ffaccess(fn, FL_WRITEABLE)
			    && (protection = file_protection(fn)) >= 0) {
			    chmod(SL_TO_BSL(fn), protection | 0600);
			}
		    }
		    status = actually_write(rp, fn, msgf, bp, forced, encoded);
		    if (protection > 0)
			chmod(SL_TO_BSL(fn), protection);
#if SYS_WINNT
		    if (write_acl_added)
			(void) w32_remove_write_acl(fn, prev_access_mask);
#endif
		}
	    }
	}
    }

    return status;
}

/*
 * Write a whole file
 */
int
writeout(const char *fn, BUFFER *bp, int forced, int msgf)
{
    REGION region;
    int status;

    setup_file_region(bp, &region);

    status = writereg(&region, fn, msgf, bp, forced, FALSE);

#if SYS_WINNT && DISP_NTWIN
    if (status && (!(isShellOrPipe(fn) || b_is_registered(bp)))) {
	/* save file path to windows registry for later recall */

	store_recent_file_or_folder(fn, TRUE);
	b_set_registered(bp);
    }
#endif

    return (status);
}

/*
 * Write the currently-selected region (i.e., the range of lines from DOT to
 * MK, inclusive).
 */
static int
prompt_and_write_region(int encoded)
{
    REGION region;
    int status;
    char fname[NFILEN];

    if (end_named_cmd()) {
	if (mlyesno("Okay to write [possible] partial range") != TRUE) {
	    mlwrite("Range not written");
	    return FALSE;
	}
	(void) vl_strncpy(fname, curbp->b_fname, sizeof(fname));
    } else {
	if ((status = mlreply_file("Write region to file: ",
				   (TBUFF **) 0, FILEC_WRITE | FILEC_PROMPT,
				   fname)) != TRUE)
	    return status;
    }
    if ((status = getregion(&region)) == TRUE)
	status = writereg(&region, fname, TRUE, curbp, FALSE, encoded);
    return status;
}

int
writeregion(void)
{
    return prompt_and_write_region(FALSE);
}

#if OPT_SELECTIONS
int
write_enc_region(void)
{
    return prompt_and_write_region(TRUE);
}

#endif

/*
 * This function writes the kill register to a file
 * Uses the file management routines in the
 * "fileio.c" package. The number of lines written is
 * displayed.
 */
int
kwrite(char *fn, int msgf)
{
    KILL *kp;			/* pointer into kill register */
    int nline;
    B_COUNT nchar;
    int s;
    int c;
    int i;
    char *sp;			/* pointer into string to insert */

    /* make sure there is something to put */
    if (kbs[ukb].kbufh == NULL) {
	if (msgf)
	    mlforce("Nothing to write");
	return FALSE;		/* not an error, just nothing */
    }
#if OPT_ENCRYPT
    if ((s = vl_resetkey(curbp, fn)) != TRUE)
	return s;
#endif
    if ((s = ffwopen(fn, FALSE)) != FIOSUC) {	/* Open writes message. */
	return FALSE;
    }
    /* tell us we're writing */
    if (msgf == TRUE)
	mlwrite("[Writing...]");
    nline = 0;			/* Number of lines.     */
    nchar = 0;

    kp = kbs[ukb].kbufh;
    while (kp != NULL) {
	i = KbSize(ukb, kp);
	sp = (char *) kp->d_chunk;
	nchar += i;
	while (i--) {
	    if ((c = *sp++) == '\n')
		nline++;
	    if ((s = ffputc(c)) != FIOSUC)
		break;
	}
	kp = kp->d_next;
    }
    if (s == FIOSUC) {		/* No write error.      */
	s = ffclose();
	if (s == FIOSUC && msgf) {	/* No close error.      */
	    writelinesmsg(fn, nline, nchar);
	}
    } else {			/* Ignore close error   */
	(void) ffclose();	/* if a write error.    */
    }
    if (s != FIOSUC)		/* Some sort of error.      */
	return FALSE;
    return TRUE;
}

/*
 * The command allows the user
 * to modify the file name associated with
 * the current buffer. It is like the "f" command
 * in UNIX "ed". The operation is simple; just zap
 * the name in the BUFFER structure, and mark the windows
 * as needing an update. You can type a blank line at the
 * prompt if you wish.
 */
/* ARGSUSED */
int
vl_filename(int f GCC_UNUSED, int n GCC_UNUSED)
{
    int s;
    char fname[NFILEN];

    if (end_named_cmd()) {
	return showcpos(FALSE, 1);
    }

    if ((s = mlreply_file("Name: ", (TBUFF **) 0, FILEC_UNKNOWN, fname))
	== ABORT)
	return s;
    if (s == FALSE)
	return s;

#if OPT_LCKFILES
    if (global_g_val(GMDUSEFILELOCK)) {
	if (!b_val(curbp, MDLOCKED) && !b_val(curbp, MDVIEW))
	    release_lock(curbp->b_fname);
	ch_fname(curbp, fname);
	make_global_b_val(curbp, MDLOCKED);
	make_global_b_val(curbp, VAL_LOCKER);
	grab_lck_file(curbp, fname);
    } else
#endif
	ch_fname(curbp, fname);

#if SYS_WINNT && DISP_NTWIN
    /* remember new filename if later written out to disk */
    b_clr_registered(curbp);
#endif

    curwp->w_flag |= WFMODE;
    updatelistbuffers();
    return TRUE;
}

/*
 * Insert file "fname" into the current buffer, called by insert file command.
 * Return the final status of the read.
 */
int
ifile(char *fname, int belowthisline, FILE *haveffp)
{
    LINE *prevp;
    LINE *newlp;
    LINE *nextp;
    int status;
    size_t nbytes;
    int nline;

    TRACE((T_CALLED "ifile(fname=%s, belowthisline=%d, haveffp=%p)\n",
	   NONNULL(fname), belowthisline, haveffp));

    b_clr_invisible(curbp);	/* we are not temporary */
    if (haveffp == 0) {
	if ((status = ffropen(fname)) == FIOERR)	/* Hard file open */
	    goto out;
	else if (status == FIOFNF)	/* File not found */
	    returnCode(no_such_file(fname));
#if COMPLETE_FILES || COMPLETE_DIRS
	else if ((status = ff_load_directory(fname)) == FIOERR)
	    goto out;
	if (b_is_directory(curbp)) {
	    /* special case: contents are already added */
	    goto out;
	}
#endif
#if OPT_ENCRYPT
	if ((status = vl_resetkey(curbp, fname)) != TRUE)
	    returnCode(status);
#endif
	mlwrite("[Inserting...]");
	CleanToPipe(FALSE);

    } else {			/* we already have the file pointer */
	ffp = haveffp;
    }
    prevp = DOT.l;
    DOT.o = 0;
    MK = DOT;

    nline = 0;
    nextp = 0;
    while ((status = ffgetline(&nbytes)) <= FIOSUC) {
#if OPT_DOSFILES
	if (b_val(curbp, MDDOS)
	    && (nbytes != 0)
	    && fflinebuf[nbytes - 1] == '\r')
	    nbytes--;
#endif
	if (!belowthisline) {
	    nextp = prevp;
	    prevp = lback(prevp);
	}

	beginDisplay();
	if (add_line_at(curbp, prevp, fflinebuf, (int) nbytes) != TRUE) {
	    status = FIOMEM;
	    newlp = 0;
	} else {
	    newlp = lforw(prevp);
	    if ((tag_for_undo(newlp)) == ABORT) {
		status = FIOMEM;
		/*
		 * De-link the line added for which we cannot undo.
		 * If we don't do this, undoworker() won't find a match
		 * against the buffer, and will be deadlocked.
		 */
		lremove(curbp, prevp);
	    }
	}
	endofDisplay();

	if (status == FIOMEM)
	    break;

	prevp = belowthisline ? newlp : nextp;
	++nline;
	if (status < FIOSUC)
	    break;
    }
    if (!haveffp) {
	CleanAfterPipe(FALSE);
	(void) ffclose();	/* Ignore errors.       */
	readlinesmsg(nline, status, fname, FALSE);
    }

    /* mark the window for changes.  could this be moved up to
     * where we actually insert a line? */
    if (nline)
	chg_buff(curbp, WFHARD);

  out:
    /* copy window parameters back to the buffer structure */
    copy_traits(&(curbp->b_wtraits), &(curwp->w_traits));

    if (status < FIOERR)
	imply_alt(fname, FALSE, FALSE);

#if COMPLETE_FILES || COMPLETE_DIRS
    if (b_is_directory(curbp)) {
	DOT.l = lback(DOT.l);
    } else
#endif
	/* advance to the next line */
	DOT.l = lforw(DOT.l);

    returnCode(FIO2Status(status));
}

/* try really hard to create a private subdirectory in some tmp
 * space to save the modified buffers in.  start with $TMPDIR, and
 * then the rest of the table.  if all that fails, try and create a subdir
 * of the current directory.
 */
static int
create_save_dir(char *dirnam)
{
    int result = FALSE;
#if defined(HAVE_MKDIR) && !SYS_MSDOS && !SYS_OS2
    static const char *tbl[] =
    {
	0,			/* reserved for $TMPDIR */
#if SYS_UNIX
	"/var/tmp",
	"/usr/tmp",
	"/tmp",
#endif
	"."
    };

    char *np;
    unsigned n;

    if ((np = getenv("TMPDIR")) != 0 &&
	(int) strlen(np) < 32 &&
	is_directory(np)) {
	tbl[0] = np;
	n = 0;
    } else {
	n = 1;
    }

    for (; n < TABLESIZE(tbl); n++) {
	if (is_directory(tbl[n])) {
	    int omask = vl_umask(0077);
	    (void) pathcat(dirnam, tbl[n], "vileDXXXXXX");

	    /* on failure, keep going */
#if defined(HAVE_MKSTEMP) && defined(HAVE_MKDTEMP)
	    result = (vl_mkdtemp(dirnam) != 0);
#else
	    result = (vl_mkdir(mktemp(dirnam), 0700) == 0);
#endif
	    (void) vl_umask(omask);
	    if (result)
		break;
	}
    }
#endif /* no mkdir, or dos, or os/2 */
    return result;
}

#if SYS_UNIX
static const char *mailcmds[] =
{
    "/usr/lib/sendmail",
    "/sbin/sendmail",
    "/usr/sbin/sendmail",
    "/bin/mail",
    0
};
#endif

/* called on hangups, interrupts, and quits */
/* This code is definitely not production quality, or probably very
	robust, or probably very secure.  I whipped it up to save
	myself while debugging...		pgf */
/* on the other hand, it has limped along for well over six years now :-/ */

SIGT
imdying(int ACTUAL_SIG_ARGS)
{
    static char dirnam[NSTRING] = "";
    static int wrote = 0;
    char filnam[NFILEN];
    BUFFER *bp;
    int bad_karma = FALSE;
#if SYS_UNIX
    char cmd[(NFILEN * 2) + 250];
#endif
    static int created = FALSE;
    char temp[NFILEN];
    char my_buffer[NSTRING];

#if OPT_WORKING && defined(SIGALRM)
    setup_handler(SIGALRM, SIG_IGN);
#endif

    if (i_am_dead++)		/* prevent recursive faults */
	_exit(signo);

    /*
     * Buffered I/O would do things that we can't rely upon in a signal
     * handler.  Force unbuffered I/O in ffopen/.../ffclose.
     */
    beginDisplay();
    fflinebuf = my_buffer;
    fflinelen = 0;
    ffp = 0;
    ffd = -1;

    /* write all modified buffers to the temp directory */
    set_global_g_val(GMDIMPLYBUFF, FALSE);	/* avoid side-effects! */
    for_each_buffer(bp) {
	if (!b_is_temporary(bp) &&
	    bp->b_active == TRUE &&
	    b_is_changed(bp)) {
	    if (!created) {
		created = create_save_dir(dirnam);
	    }
	    if (created) {
		/* then the path is dir+file */
		(void) pathcat(filnam, dirnam, bp->b_bname);
	    } else {
		/* no dir: the path is V+file */
		(void) vl_strncpy(filnam,
				  strcat(strcpy(temp, "V"), bp->b_bname),
				  sizeof(filnam));
	    }
	    set_b_val(bp, MDVIEW, FALSE);
	    if (writeout(filnam, bp, TRUE, FALSE) != TRUE) {
		bad_karma = TRUE;
		continue;
	    }
	    wrote++;
	}
    }
#if SYS_UNIX			/* try to send mail */
    /*
     * If VILE_ERROR_ABORT is defined, send mail even if there are no pieces
     * to pick up.  It makes it simpler to find the core file, if any was
     * forced, and ensures that we can distinguish an abnormal exit from
     * accidentally typing 'Q'.
     */
#ifndef VILE_ERROR_ABORT
    /* if we wrote, or tried to */
    if (wrote || bad_karma)
#endif
    {
	const char **mailcmdp;
	struct stat sb;
	char *np;
	/* choose the first mail sender we can find.
	   it used to be you could rely on /bin/mail being
	   a simple mailer, but no more.  and sendmail has
	   been moving around too. */
	for (mailcmdp = mailcmds; *mailcmdp != 0; mailcmdp++) {
	    if (file_stat(*mailcmdp, &sb) == 0)
		break;
	}
	if (*mailcmdp &&
	    ((np = getenv("LOGNAME")) != 0 ||
	     (np = getenv("USER")) != 0)) {
	    char *cp;
	    cp = lsprintf(cmd, "( %s %s; %s; %s; %s %d;",
			  "echo To:", np,
			  ((wrote || bad_karma)
			   ? "echo Subject: vile died, files saved"
			   : "echo Subject: vile died, no files saved"),
			  "echo",
			  "echo vile died due to signal", signo);

#ifdef HAVE_GETHOSTNAME
	    {
		char hostname[128];
		if (gethostname(hostname, sizeof(hostname)) < 0)
		    (void) strcpy(hostname, "unknown");
		hostname[sizeof(hostname) - 1] = EOS;
		cp = lsprintf(cp, "%s %s;",
			      "echo on host", hostname);
	    }
#endif
	    cp = lsprintf(cp, "echo current directory:;pwd;");
	    if (wrote) {
		cp = lsprintf(cp,
			      "echo the following files were saved in directory %s;",
			      dirnam);

		cp = lsprintf(cp, "%s%s%s;",
#ifdef HAVE_MKDIR
		/* reverse sort so '.' comes last, in case it
		 * terminates the mail message early */
			      "ls -a ", dirnam, " | sort -r"
#else
			      "ls ", dirnam, "/V*"
#endif
		    );
	    }

	    if (bad_karma)
		cp = lsprintf(cp, "%s %s",
			      "echo errors:  check in current",
			      "directory too.;");

	    cp = lsprintf(cp, ") | %s %s", *mailcmdp, np);

	    (void) system(cmd);
	}
    }
    term.cursorvis(TRUE);	/* ( this might work ;-) */
    if (signo != SIGHUP && signo != SIGINT) {
	term.clean(FALSE);
#ifdef VILE_ERROR_ABORT
	ExitProgram(signo);
#else
	abort();
#endif
    }
#else
    if (wrote) {
	fprintf(stderr, "vile died: Files saved in directory %s\r\n",
		dirnam);
    }
    if (bad_karma) {
	fprintf(stderr, "problems encountered during save.  sorry\r\n");
    }
#endif

    tidy_exit(BADEXIT);
    /* NOTREACHED */
    SIGRET;
}

void
markWFMODE(BUFFER *bp)
{
    WINDOW *wp;			/* scan for windows that need updating */
    for_each_visible_window(wp) {
	if (wp->w_bufp == bp)
	    wp->w_flag |= WFMODE;
    }
}