File: pathname.c

package info (click to toggle)
fdclone 3.01b-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 4,772 kB
  • ctags: 8,674
  • sloc: ansic: 100,552; makefile: 4,497; sh: 1,480; sed: 224
file content (3516 lines) | stat: -rw-r--r-- 70,306 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
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
/*
 *	pathname.c
 *
 *	path name management module
 */

#ifdef	FD
#include "fd.h"
#else
#include "headers.h"
#include "depend.h"
#include "printf.h"
#include "kctype.h"
#include "string.h"
#include "malloc.h"
#endif

#include "dirent.h"
#include "sysemu.h"
#include "pathname.h"

#ifdef	DEP_URLPATH
#include "url.h"
#endif

#if	MSDOS
#include <process.h>
# ifndef	FD
# include <dos.h>
# include <io.h>
# endif
#endif	/* !MSDOS */
#ifndef	NOUID
#include <pwd.h>
#include <grp.h>
#endif

#ifdef	FD
#define	FDSTR			"FD"
#endif
#if	MSDOS
#define	EXTWIDTH		4
#else
#define	EXTWIDTH		0
#endif
#define	getconstvar(s)		(getenvvar(s, sizeof(s) - 1))

#ifdef	FD
extern int stat2 __P_((CONST char *, struct stat *));
extern int _chdir2 __P_((CONST char *));
extern char *getenv2 __P_((CONST char *));
# ifdef	DEP_DOSLFN
extern char *shortname __P_((CONST char *, char *));
# endif
extern VOID demacroarg __P_((char **));
extern VOID lostcwd __P_((char *));
#else	/* !FD */
#define	_chdir2			Xchdir
# ifdef	NOSYMLINK
# define	stat2		Xstat
# else
static int NEAR stat2 __P_((CONST char *, struct stat *));
# endif
#define	getenv2			(char *)getenv
#endif	/* !FD */

#ifdef	DEBUG
extern char *_mtrace_file;
#endif
#ifdef	FD
extern char *progpath;
#endif

#if	defined (DEP_PATHTOP) || defined (BSPATHDELIM) || defined (FD)
static CONST char *NEAR topdelim __P_((CONST char *, int, int *, int *));
#endif
static CONST char *NEAR topdelim2 __P_((CONST char *, int *));
static char *NEAR getenvvar __P_((CONST char *, int));
static int NEAR setvar __P_((CONST char *, CONST char *, int));
static int NEAR isescape __P_((CONST char *, int, int, int, int));
#ifdef	_NOORIGGLOB
static char *NEAR cnvregexp __P_((CONST char *, int));
#else
static int NEAR _regexp_exec __P_((CONST reg_ex_t *, CONST char *));
#endif
static VOID NEAR addstrbuf __P_((strbuf_t *, CONST char *, int));
static VOID NEAR duplwild __P_((wild_t *, CONST wild_t *));
static VOID NEAR freewild __P_((wild_t *));
static int NEAR _evalwild __P_((int, char ***, wild_t *));
#ifndef	_NOUSEHASH
static int NEAR calchash __P_((CONST char *));
static VOID NEAR inithash __P_((VOID_A));
static hashlist *NEAR newhash __P_((CONST char *, char *, int, hashlist *));
static VOID NEAR freehash __P_((hashlist **));
static hashlist *NEAR findhash __P_((CONST char *, int));
static VOID NEAR rmhash __P_((CONST char *, int));
#endif
static int NEAR isexecute __P_((CONST char *, int, int));
#if	MSDOS
static int NEAR extaccess __P_((char *, CONST char *, int, int));
#endif
#if	!defined (FDSH) && !defined (_NOCOMPLETE)
static int NEAR completefile __P_((CONST char *, int, int, char ***,
		CONST char *, int, int));
static int NEAR completeexe __P_((CONST char *, int, int, char ***));
#endif	/* !FDSH && !_NOCOMPLETE */
static int NEAR addmeta __P_((char *, CONST char *, int));
static int NEAR skipvar __P_((CONST char **, int *, int *, int));
#ifdef	MINIMUMSHELL
static int NEAR skipvarvalue __P_((CONST char *, int *,
		CONST char *, int, int));
static char *NEAR evalshellparam __P_((int, int));
#else	/* !MINIMUMSHELL */
static int NEAR skipvarvalue __P_((CONST char *, int *,
		CONST char *, int, int, int));
static char *NEAR removeword __P_((CONST char *, CONST char *, int, int));
static char **NEAR removevar __P_((char **, CONST char *, int, int));
static char *NEAR evalshellparam __P_((int, int, CONST char *, int, int *));
#endif	/* !MINIMUMSHELL */
static int NEAR replacevar __P_((CONST char *, char **, int, int, int, int));
static char *NEAR insertarg __P_((char *, int, CONST char *, int, int));
static int NEAR evalvar __P_((char **, int, CONST char **, int));
static char *NEAR replacebackquote __P_((char *, int *, char *, int, int));
#ifdef	BASHSTYLE
static VOID replaceifs __P_((char *, int));
#endif
#ifndef	MINIMUMSHELL
static int NEAR evalhome __P_((char **, int, CONST char **));
#endif

CONST char nullstr[] = "";
CONST char rootpath[] = _SS_;
CONST char curpath[] = ".";
CONST char parentpath[] = "..";
char **argvar = NULL;
#ifndef	_NOUSEHASH
hashlist **hashtable = NULL;
#endif
char *(*getvarfunc)__P_((CONST char *, int)) = NULL;
int (*putvarfunc)__P_((char *, int)) = NULL;
int (*getretvalfunc)__P_((VOID_A)) = NULL;
p_id_t (*getpidfunc)__P_((VOID_A)) = NULL;
p_id_t (*getlastpidfunc)__P_((VOID_A)) = NULL;
char *(*getflagfunc)__P_((VOID_A)) = NULL;
int (*checkundeffunc)__P_((CONST char *, CONST char *, int)) = NULL;
VOID_T (*exitfunc)__P_((VOID_A)) = NULL;
char *(*backquotefunc)__P_((CONST char *)) = NULL;
#ifndef	MINIMUMSHELL
char *(*posixsubstfunc)__P_((CONST char *, int *)) = NULL;
#endif
#ifndef	PATHNOCASE
int pathignorecase = 0;
#endif
#ifndef	_NOVERSCMP
int versioncomp = 0;
#endif

static int skipdotfile = 0;
#if	!defined (USEREGCMP) && !defined (USEREGCOMP) && !defined (USERE_COMP)
static char *wildsymbol1 = "?";
static char *wildsymbol2 = "*";
#endif
#ifndef	NOUID
static uidtable *uidlist = NULL;
static int maxuid = 0;
static gidtable *gidlist = NULL;
static int maxgid = 0;
#endif	/* !NOUID */


#ifndef	FD
# ifndef	NOSYMLINK
static int NEAR stat2(path, stp)
CONST char *path;
struct stat *stp;
{
	int duperrno;

	if (Xstat(path, stp) < 0) {
		duperrno = errno;
		if (Xlstat(path, stp) < 0
		|| (stp -> st_mode & S_IFMT) != S_IFLNK) {
			errno = duperrno;
			return(-1);
		}
		stp -> st_mode &= ~S_IFMT;
	}

	return(0);
}
# endif	/* !NOSYMLINK */
#endif	/* !FD */

int getpathtop(s, drvp, typep)
CONST char *s;
int *drvp, *typep;
{
	int n, drv, type;

	n = drv = 0;
	type = PT_NONE;

	if (!s) /*EMPTY*/;
#ifdef	DEP_DOSPATH
	else if ((drv = _dospath(s))) {
		n = 2;
		type = PT_DOS;
	}
#endif
#ifdef	DOUBLESLASH
	else if ((n = isdslash(s))) type = PT_DSLASH;
#endif
#ifdef	DEP_URLPATH
	else if ((n = _urlpath(s, NULL, NULL))) type = PT_URL;
#endif

	if (drvp) *drvp = drv;
	if (typep) *typep = type;

	return(n);
}

#ifdef	DEP_DOSPATH
char *gendospath(path, drive, c)
char *path;
int drive, c;
{
	*(path++) = drive;
	*(path++) = ':';
	if (c) *(path++) = c;
	*path = '\0';

	return(path);
}
#endif	/* DEP_DOSPATH */

#if	defined (DEP_PATHTOP) || defined (BSPATHDELIM) || defined (FD)
static CONST char *NEAR topdelim(s, d, np, typep)
CONST char *s;
int d, *np, *typep;
{
	if (d) {
		*np = getpathtop(s, NULL, typep);
# ifdef	DEP_DOSPATH
		if (*typep == PT_DOS) return(++s);
# endif
	}
	else {
		*np = 0;
		*typep = PT_NONE;
	}

	return(NULL);
}
#endif	/* DEP_PATHTOP || BSPATHDELIM || defined FD */

static CONST char *NEAR topdelim2(s, np)
CONST char *s;
int *np;
{
	int type;

	*np = getpathtop(s, NULL, &type);
	s += *np;

	switch (type) {
		case PT_NONE:
		case PT_DOS:
			if (!*s) return(s);
			break;
		default:
			break;
	}
	if (*s == _SC_ && !s[1]) return(&(s[1]));

	return(NULL);
}

#if	defined (DEP_PATHTOP) || defined (BSPATHDELIM)
char *strdelim(s, d)
CONST char *s;
int d;
{
	CONST char *cp;
	int n, type;

	if ((cp = topdelim(s, d, &n, &type))) return((char *)cp);

	for (s += n; *s; s++) {
		if (*s == _SC_) return((char *)s);
# ifdef	BSPATHDELIM
		if (iskanji1(s, 0)) s++;
# endif
	}

	return(NULL);
}

char *strrdelim(s, d)
CONST char *s;
int d;
{
	CONST char *cp;
	int n, type;

	cp = topdelim(s, d, &n, &type);

	for (s += n; *s; s++) {
		if (*s == _SC_) cp = s;
# ifdef	BSPATHDELIM
		if (iskanji1(s, 0)) s++;
# endif
	}

	return((char *)cp);
}
#endif	/* DEP_PATHTOP || BSPATHDELIM */

#ifdef	FD
char *strrdelim2(s, d, eol)
CONST char *s;
int d;
CONST char *eol;
{
	CONST char *cp;
	int n, type;

	cp = topdelim(s, d, &n, &type);

# ifdef	BSPATHDELIM
	for (s += n; *s && s < eol; s++) {
		if (*s == _SC_) cp = s;
		if (iskanji1(s, 0)) s++;
	}
# else
	s += n;
	for (eol--; eol >= s; eol--) if (*eol == _SC_) return((char *)eol);
# endif

	return((char *)cp);
}
#endif	/* FD */

#ifndef	MINIMUMSHELL
int isdelim(s, ptr)
CONST char *s;
int ptr;
{
# ifdef	BSPATHDELIM
	int i;
# endif

	if (ptr < 0 || s[ptr] != _SC_) return(0);
# ifdef	BSPATHDELIM
	if (--ptr < 0) return(1);
	if (!ptr) return(!iskanji1(s, 0));

	for (i = 0; s[i] && i < ptr; i++) if (iskanji1(s, i)) i++;
	if (!s[i] || i > ptr) return(1);

	return(!iskanji1(s, i));
# else
	return(1);
# endif
}
#endif	/* !MINIMUMSHELL */

char *strcatdelim(s)
char *s;
{
	char *cp, *eol;
	int n;

	eol = &(s[MAXPATHLEN - 1]);
	if ((cp = (char *)topdelim2(s, &n))) return(cp);

	for (s += n; *s; s++) {
		if (*s == _SC_) {
			if (!cp) cp = s;
			continue;
		}
		cp = NULL;
#ifdef	BSPATHDELIM
		if (iskanji1(s, 0)) s++;
#endif
	}

	if (!cp) {
		cp = s;
		if (cp >= eol) return(cp);
		*cp = _SC_;
	}
	*(++cp) = '\0';

	return(cp);
}

char *strcatdelim2(buf, s1, s2)
char *buf;
CONST char *s1, *s2;
{
	CONST char *s;
	char *cp;
	int n, len;

	s = topdelim2(s1, &n);
	if (n > MAXPATHLEN - 1) {
		n = MAXPATHLEN - 1;
		Xstrncpy(buf, s1, n);
		return(&(buf[n]));
	}

	memcpy(buf, s1, n);
	if (s) {
		cp = &(buf[s - s1]);
		memcpy(&(buf[n]), &(s1[n]), cp - &(buf[n]));
	}
	else {
		cp = NULL;
		for (; s1[n]; n++) {
			if (n >= MAXPATHLEN - 1) {
				buf[n] = '\0';
				return(&(buf[n]));
			}
			buf[n] = s1[n];
			if (s1[n] == _SC_) {
				if (!cp) cp = &(buf[n]) + 1;
				continue;
			}
			cp = NULL;
#ifdef	BSPATHDELIM
			if (iskanji1(s1, n)) {
				if (!s1[++n]) break;
				buf[n] = s1[n];
			}
#endif
		}
		if (!cp) {
			cp = &(buf[n]);
			if (n >= MAXPATHLEN - 1) {
				*cp = '\0';
				return(cp);
			}
			*(cp++) = _SC_;
		}
	}
	if (s2) {
		len = MAXPATHLEN - 1 - (cp - buf);
		for (n = 0; s2[n] && n < len; n++) *(cp++) = s2[n];
	}
	*cp = '\0';

	return(cp);
}

int strcatpath(path, cp, name)
char *path, *cp;
CONST char *name;
{
	int len;

	len = strlen(name);
	if (len + (cp - path) >= MAXPATHLEN) {
		errno = ENAMETOOLONG;
		return(-1);
	}
	Xstrncpy(cp, name, len);

	return(len);
}

#ifndef	PATHNOCASE
int strpathcmp2(s1, s2)
CONST char *s1, *s2;
{
	if (pathignorecase) return(Xstrcasecmp(s1, s2));

	return(strcmp(s1, s2));
}

int strnpathcmp2(s1, s2, n)
CONST char *s1, *s2;
int n;
{
	if (pathignorecase) return(Xstrncasecmp(s1, s2, n));

	return(strncmp(s1, s2, n));
}
#endif	/* !PATHNOCASE */

#ifndef	_NOVERSCMP
int strverscmp2(s1, s2)
CONST char *s1, *s2;
{
	if (versioncomp) return(Xstrverscmp(s1, s2, pathignorecase));

	return(strpathcmp2(s1, s2));
}
#endif	/* !_NOVERSCMP */

#ifdef	FD
char *underpath(path, dir, len)
CONST char *path, *dir;
int len;
{
	char *cp;

	if (len < 0) len = strlen(dir);
	if ((cp = strrdelim2(dir, 1, &(dir[len]))) && !cp[1]) len = cp - dir;
	if (len > 0 && strnpathcmp(path, dir, len)) return(NULL);
	if (path[len] && path[len] != _SC_) return(NULL);

	return((char *)&(path[len]));
}
#endif	/* FD */

static char *NEAR getenvvar(ident, len)
CONST char *ident;
int len;
{
#if	(!defined (FD) && !defined (FDSH)) || !defined (DEP_ORIGSHELL)
	char *cp, *env;
#endif

	if (getvarfunc) return((*getvarfunc)(ident, len));
#if	(defined (FD) || defined (FDSH)) && defined (DEP_ORIGSHELL)
	return(NULL);
#else
	if (len < 0) len = strlen(ident);
	cp = Xstrndup(ident, len);
	env = getenv2(cp);
	Xfree(cp);

	return(env);
#endif
}

static int NEAR setvar(ident, value, len)
CONST char *ident, *value;
int len;
{
	char *cp;
	int ret, vlen;

	vlen = strlen(value);
	cp = Xmalloc(len + 1 + vlen + 1);
	Xstrncpy(cp, ident, len);
	cp[len] = '=';
	Xstrncpy(&(cp[len + 1]), value, vlen);

#if	defined (FD) || defined (FDSH)
	ret = (putvarfunc) ? (*putvarfunc)(cp, len) : -1;
#else
	ret = (putvarfunc) ? (*putvarfunc)(cp, len) : putenv(cp);
#endif
	if (ret < 0) Xfree(cp);

	return(ret);
}

int isidentchar(c)
int c;
{
	return(c == '_' || Xisalpha(c));
}

int isidentchar2(c)
int c;
{
	return(c == '_' || Xisalnum(c));
}

int isdotdir(s)
CONST char *s;
{
	if (s[0] != '.') /*EMPTY*/;
	else if (!s[1]) return(2);
	else if (s[1] != '.') /*EMPTY*/;
	else if (!s[2]) return(1);

	return(0);
}

char *isrootdir(s)
CONST char *s;
{
#ifdef	DEP_DOSPATH
	if (_dospath(s)) s += 2;
#endif
	if (*s != _SC_) return(NULL);

	return((char *)s);
}

#ifndef	MINIMUMSHELL
int isrootpath(s)
CONST char *s;
{
	return((s[0] == _SC_ && !s[1]) ? 1 : 0);
}

VOID copyrootpath(s)
char *s;
{
	*(s++) = _SC_;
	*s = '\0';
}
#endif	/* !MINIMUMSHELL */

VOID copycurpath(s)
char *s;
{
	*(s++) = '.';
	*s = '\0';
}

#ifdef	DOUBLESLASH
int isdslash(s)
CONST char *s;
{
# if	MSDOS || defined (CYGWIN)
	char *cp;

	if (s[0] != _SC_ || s[1] != _SC_ || !s[2] || s[2] == _SC_) return(0);
	if ((cp = strdelim(&(s[2]), 0))) return(cp - s);

	return(strlen(s));
# else
	return((s[0] == _SC_ && s[1] == _SC_) ? 2 : 0);
# endif
}
#endif	/* DOUBLESLASH */

char *getbasename(s)
CONST char *s;
{
	char *cp;

	if ((cp = strrdelim(s, 1))) return(&(cp[1]));

	return((char *)s);
}

char *getshellname(s, loginp, restrictedp)
CONST char *s;
int *loginp, *restrictedp;
{
	if (loginp) *loginp = 0;
	if (restrictedp) *restrictedp = 0;
	if (*s == '-') {
		s++;
		if (loginp) *loginp = 1;
	}
#ifdef	PATHNOCASE
	if (Xtolower(*s) == 'r')
#else
	if (*s == 'r')
#endif
	{
		s++;
		if (restrictedp) *restrictedp = 1;
	}

	return((char *)s);
}

static int NEAR isescape(s, ptr, quote, len, flags)
CONST char *s;
int ptr, quote, len, flags;
{
#ifdef	FAKEESCAPE
	return(0);
#else	/* !FAKEESCAPE */
	if (s[ptr] != PESCAPE || quote == '\'') return(0);

	if (flags & EA_EOLESCAPE) /*EMPTY*/;
	else if (len >= 0) {
		if (ptr + 1 >= len) return(0);
	}
	else {
		if (!s[ptr + 1]) return(0);
	}

# ifdef	BSPATHDELIM
	if (quote == '"') {
		if (!Xstrchr(DQ_METACHAR, s[ptr + 1])) return(0);
	}
	else {	/* if (!quote || quote == '`') */
		if (!Xstrchr(METACHAR, s[ptr + 1])) return(0);
	}
# endif

	return(1);
#endif	/* !FAKEESCAPE */
}

#ifdef	_NOORIGGLOB
static char *NEAR cnvregexp(s, len)
CONST char *s;
int len;
{
	char *re;
	ALLOC_T size;
	int i, j, paren;

	if (!*s) {
		s = "*";
		len = 1;
	}
	if (len < 0) len = strlen(s);
	re = c_realloc(NULL, 0, &size);
	paren = j = 0;
	re[j++] = '^';
	for (i = 0; i < len; i++) {
		re = c_realloc(re, j + 2, &size);

		if (paren) {
			if (s[i] == ']') paren = 0;
			re[j++] = s[i];
			continue;
		}
		else if (isescape(s, i, '\0', len, 0)) {
			re[j++] = s[i++];
			re[j++] = s[i];
		}
		else switch (s[i]) {
			case '?':
				re[j++] = '.';
				break;
			case '*':
				re[j++] = '.';
				re[j++] = '*';
				break;
			case '[':
				paren = 1;
				re[j++] = s[i];
				break;
			case '^':
			case '$':
			case '.':
				re[j++] = '\\';
				re[j++] = s[i];
				break;
			default:
				re[j++] = s[i];
				break;
		}
	}
	re[j++] = '$';
	re[j++] = '\0';

	return(re);
}
#endif	/* _NOORIGGLOB */

#ifdef	USERE_COMP
extern char *re_comp __P_((CONST char *));
extern int re_exec __P_((CONST char *));
# ifdef	PATHNOCASE
extern int re_ignore_case;
# endif

reg_ex_t *regexp_init(s, len)
CONST char *s;
int len;
{
	char *new;

# ifdef	PATHNOCASE
	re_ignore_case = 1;
# endif
	skipdotfile = (*s == '*' || *s == '?' || *s == '[');
	new = cnvregexp(s, len);
# ifdef	DEBUG
	_mtrace_file = "re_comp(start)";
	re_comp(new);
	if (_mtrace_file) _mtrace_file = NULL;
	else {
		_mtrace_file = "re_comp(end)";
		malloc(0);	/* dummy alloc */
	}
# else
	re_comp(new);
# endif
	Xfree(new);

	return((reg_ex_t *)1);
}

/*ARGSUSED*/
int regexp_exec(re, s, fname)
CONST reg_ex_t *re;
CONST char *s;
int fname;
{
	if (fname && skipdotfile && *s == '.') return(0);

	return(re_exec(s) > 0);
}

/*ARGSUSED*/
VOID regexp_free(re)
reg_ex_t *re;
{
	return;
}

#else	/* !USERE_COMP */
# ifdef	USEREGCOMP

reg_ex_t *regexp_init(s, len)
CONST char *s;
int len;
{
	reg_ex_t *re;
	char *new;
	int n;

	skipdotfile = (*s == '*' || *s == '?' || *s == '[');
	s = new = cnvregexp(s, len);
	re = (reg_ex_t *)Xmalloc(sizeof(reg_ex_t));
# ifdef	PATHNOCASE
	n = regcomp(re, s, REG_EXTENDED | REG_ICASE);
# else
	n = regcomp(re, s, REG_EXTENDED | (pathignorecase) ? REG_ICASE : 0);
# endif
	if (n) {
		Xfree(re);
		re = NULL;
	}
	Xfree(new);

	return(re);
}

int regexp_exec(re, s, fname)
CONST reg_ex_t *re;
CONST char *s;
int fname;
{
	if (!re || (fname && skipdotfile && *s == '.')) return(0);

	return(!regexec(re, s, 0, NULL, 0));
}

VOID regexp_free(re)
reg_ex_t *re;
{
	if (re) {
		regfree(re);
		Xfree(re);
	}
}

# else	/* !USEREGCOMP */
#  ifdef	USEREGCMP
extern char *regcmp __P_((CONST char *, int));
extern char *regex __P_((CONST char *, CONST char *, ...));

reg_ex_t *regexp_init(s, len)
CONST char *s;
int len;
{
	reg_ex_t *re;
	char *new;

	skipdotfile = (*s == '*' || *s == '?' || *s == '[');
	new = cnvregexp(s, len);
	re = regcmp(new, 0);
	Xfree(new);

	return(re);
}

int regexp_exec(re, s, fname)
CONST reg_ex_t *re;
CONST char *s;
int fname;
{
	if (!re || (fname && skipdotfile && *s == '.')) return(0);

	return(regex(re, s) ? 1 : 0);
}

VOID regexp_free(re)
reg_ex_t *re;
{
	Xfree(re);
}
#  else	/* !USEREGCMP */

reg_ex_t *regexp_init(s, len)
CONST char *s;
int len;
{
	reg_ex_t *re;
	char *cp, *paren;
	ALLOC_T size;
	int i, j, n, pc, plen, metachar, quote;

	skipdotfile = 0;
	if (len < 0) len = strlen(s);
	re = NULL;
	paren = NULL;
	n = plen = size = 0;
	quote = '\0';
	re = b_realloc(re, n, reg_ex_t);
	re[n] = NULL;
	for (i = 0; i < len; i++) {
		cp = NULL;
		metachar = 0;
		pc = parsechar(&(s[i]), len - i, '\0', 0, &quote, NULL);
		if (pc == PC_OPQUOTE || pc == PC_CLQUOTE) continue;
		else if (pc == PC_ESCAPE) {
			metachar = 1;
			i++;
		}

		if (paren) {
			paren = c_realloc(paren, plen + 1, &size);

			if (quote || metachar) {
#ifdef	BASHSTYLE
	/* bash treats a character quoted by \ in "[]" as a character itself */
				paren[plen++] = PESCAPE;
				if (iswchar(s, i)) {
					paren[plen++] = s[i++];
					paren[plen++] = s[i];
				}
# ifndef	PATHNOCASE
				else if (!pathignorecase) paren[plen++] = s[i];
# endif
				paren[plen++] = Xtoupper(s[i]);
#endif	/* BASHSTYLE */
			}
			else if (s[i] == ']') {
				if (!plen) {
					Xfree(paren);
					regexp_free(re);
					return(NULL);
				}
#ifndef	BASHSTYLE
	/* bash treats "[a-]]" as "[a-]" + "]" */
				if (paren[plen - 1] == '-') {
					paren[plen++] = s[i];
					continue;
				}
#endif
				paren[plen] = '\0';
				cp = Xrealloc(paren, plen + 1);
				paren = NULL;
			}
#ifdef	BASHSTYLE
	/* bash treats "[^abc]" as "[!abc]" */
			else if (!plen && (s[i] == '!' || s[i] == '^'))
				paren[plen++] = '\0';
#else
			else if (!plen && s[i] == '!') paren[plen++] = '\0';
#endif
			else if (s[i] == '-') {
				if (i + 1 >= len) j = '\0';
#ifndef	PATHNOCASE
				else if (!pathignorecase) j = s[i + 1];
#endif
				else j = Xtoupper(s[i + 1]);

#ifdef	BASHSTYLE
	/* bash does not allow "[b-a]" */
				if (plen && j && j != ']'
				&& j < paren[plen - 1]) {
					Xfree(paren);
					regexp_free(re);
					return(NULL);
				}
#else
	/* Bourne shell ignores "[b-a]" */
				if (plen && j && j < paren[plen - 1]) {
					do {
						if (i + 1 >= len) break;
						i++;
					} while (s[i + 1] != ']');
				}
	/* Bourne shell does not allow "[-a]" */
				else if ((!plen && j != '-')) {
					Xfree(paren);
					regexp_free(re);
					return(NULL);
				}
#endif
				else paren[plen++] = s[i];
			}
			else if (iswchar(s, i)) {
				paren[plen++] = s[i++];
				paren[plen++] = s[i];
			}
#ifndef	PATHNOCASE
			else if (!pathignorecase) paren[plen++] = s[i];
#endif
			else paren[plen++] = Xtoupper(s[i]);
		}
		else if (!quote && !metachar) switch (s[i]) {
			case '?':
				cp = wildsymbol1;
				if (!n) skipdotfile++;
				break;
			case '*':
				cp = wildsymbol2;
				if (!n) skipdotfile++;
				break;
			case '[':
				paren = c_realloc(NULL, 0, &size);
				plen = 0;
				if (!n) skipdotfile++;
				break;
			default:
				break;
		}

		if (paren) continue;
		if (!cp) {
			cp = Xmalloc(2 + 1);
			j = 0;
			if (iswchar(s, i)) {
				cp[j++] = s[i++];
				cp[j++] = s[i];
			}
#ifndef	PATHNOCASE
			else if (!pathignorecase) cp[j++] = s[i];
#endif
			else cp[j++] = Xtoupper(s[i]);
			cp[j] = '\0';
		}
		re[n++] = cp;
		re = b_realloc(re, n, reg_ex_t);
		re[n] = NULL;
	}
	if (paren) {
		Xfree(paren);
		if (plen) {
			regexp_free(re);
			return(NULL);
		}
		cp = Xstrdup("[");
		re[n++] = cp;
		re = b_realloc(re, n, reg_ex_t);
		re[n] = NULL;
	}

	return((reg_ex_t *)Xrealloc(re, (n + 1) * sizeof(reg_ex_t)));
}

static int NEAR _regexp_exec(re, s)
CONST reg_ex_t *re;
CONST char *s;
{
	int i, n1, n2, c1, c2, beg, rev;

	for (n1 = n2 = 0; re[n1] && s[n2]; n1++, n2++) {
		c1 = (u_char)(s[i = n2]);
		if (iswchar(s, n2)) c1 = (c1 << 8) + (u_char)(s[++n2]);
#ifndef	PATHNOCASE
		else if (!pathignorecase) /*EMPTY*/;
#endif
		else c1 = Xtoupper(c1);

		if (re[n1] == wildsymbol1) continue;
		if (re[n1] == wildsymbol2) {
			if (_regexp_exec(&(re[n1 + 1]), &(s[i]))) return(1);
			n1--;
			continue;
		}

		i = rev = 0;
		if (!re[n1][i]) {
			rev = 1;
			i++;
		}
		beg = -1;
		c2 = '\0';
		for (; re[n1][i]; i++) {
#ifdef	BASHSTYLE
	/* bash treats a character quoted by \ in "[]" as a character itself */
			if (re[n1][i] == PESCAPE && re[n1][i + 1]) i++;
	/* bash treats "[a-]]" as "[a-]" + "]" */
			else if (re[n1][i] == '-' && re[n1][i + 1] && c2)
#else
			if (re[n1][i] == '-' && c2)
#endif
			{
				beg = c2;
				i++;
			}
			c2 = (u_char)(re[n1][i]);
			if (iswchar(re[n1], i))
				c2 = (c2 << 8) + (u_char)(re[n1][++i]);
			if (beg >= 0) {
				if (beg && c1 >= beg && c1 <= c2) break;
				beg = -1;
			}
			else if (c1 == c2) break;
		}
		if (rev) {
			if (re[n1][i]) return(0);
		}
		else {
			if (!re[n1][i]) return(0);
		}
	}
	while (re[n1] == wildsymbol2) n1++;
	if (re[n1] || s[n2]) return(0);

	return(1);
}

int regexp_exec(re, s, fname)
CONST reg_ex_t *re;
CONST char *s;
int fname;
{
	if (!re || (fname && skipdotfile && *s == '.')) return(0);

	return(_regexp_exec(re, s));
}

VOID regexp_free(re)
reg_ex_t *re;
{
	int i;

	if (re) {
		for (i = 0; re[i]; i++)
			if (re[i] != wildsymbol1 && re[i] != wildsymbol2)
				Xfree(re[i]);
		Xfree(re);
	}
}
#  endif	/* !USEREGCMP */
# endif		/* !USEREGCOMP */
#endif		/* !USERE_COMP */

static VOID NEAR addstrbuf(sp, s, len)
strbuf_t *sp;
CONST char *s;
int len;
{
	sp -> s = c_realloc(sp -> s, sp -> len + len, &(sp -> size));
	memcpy(&(sp -> s[sp -> len]), s, len);
	sp -> len += len;
	sp -> s[sp -> len] = '\0';
}

static VOID NEAR duplwild(dst, src)
wild_t *dst;
CONST wild_t *src;
{
	memcpy((char *)dst, (char *)src, sizeof(wild_t));
	dst -> fixed.s = Xmalloc(src -> fixed.size);
	memcpy(dst -> fixed.s, src -> fixed.s, src -> fixed.len + 1);
	dst -> path.s = Xmalloc(src -> path.size);
	memcpy(dst -> path.s, src -> path.s, src -> path.len + 1);

#ifndef	NODIRLOOP
	if (src -> ino) {
		dst -> ino = (devino_t *)Xmalloc(src -> nino
			* sizeof(devino_t));
		memcpy((char *)(dst -> ino), (char *)(src -> ino),
			src -> nino * sizeof(devino_t));
	}
#endif	/* !NODIRLOOP */
}

static VOID NEAR freewild(wp)
wild_t *wp;
{
	Xfree(wp -> fixed.s);
	Xfree(wp -> path.s);
#ifndef	NODIRLOOP
	Xfree(wp -> ino);
#endif
}

static int NEAR _evalwild(argc, argvp, wp)
int argc;
char ***argvp;
wild_t *wp;
{
#if	defined (DOUBLESLASH) || defined (DEP_URLPATH)
	int prefix;
#endif
	DIR *dirp;
	struct dirent *dp;
	struct stat st;
	reg_ex_t *re;
	wild_t dupl;
	ALLOC_T flen, plen;
	CONST char *cp;
	char *new;
	int i, n, w, pc, quote, isdir;

	if (!*(wp -> s)) {
		if (!(wp -> fixed.len)) return(argc);
		*argvp = (char **)Xrealloc(*argvp,
			(argc + 2) * sizeof(char *));
		(*argvp)[argc++] = wp -> fixed.s;
		wp -> fixed.s = NULL;
		return(argc);
	}

	flen = wp -> fixed.len;
	plen = wp -> path.len;
	quote = wp -> quote;

	if (wp -> fixed.len) addstrbuf(&(wp -> path), rootpath, 1);

#if	defined (DOUBLESLASH) || defined (DEP_URLPATH)
	if (wp -> path.len) prefix = 0;
# ifdef	DOUBLESLASH
	else if ((prefix = isdslash(wp -> s))) {
		addstrbuf(&(wp -> fixed), wp -> s, prefix);
		addstrbuf(&(wp -> path), wp -> s, prefix);
		wp -> s += prefix;
		wp -> type = WT_DOUBLESLASH;
	}
# endif
# ifdef	DEP_URLPATH
	else if ((prefix = isurl(wp -> s, 0))) {
		addstrbuf(&(wp -> fixed), wp -> s, prefix);
		addstrbuf(&(wp -> path), wp -> s, prefix);
		wp -> s += prefix;
		wp -> type = WT_URLPATH;
	}
# endif
#endif	/* DOUBLESLASH || DEP_URLPATH */

	for (i = w = 0; wp -> s[i] && wp -> s[i] != _SC_; i++) {
		pc = parsechar(&(wp -> s[i]), -1,
			'\0', 0, &(wp -> quote), NULL);
		if (pc == PC_OPQUOTE || pc == PC_CLQUOTE) {
			if (!(wp -> flags & EA_STRIPQ))
				addstrbuf(&(wp -> fixed), &(wp -> s[i]), 1);
			continue;
		}
		else if (pc == PC_WCHAR) {
			addstrbuf(&(wp -> fixed), &(wp -> s[i]), 1);
			addstrbuf(&(wp -> path), &(wp -> s[i]), 1);
			i++;
		}
		else if (pc == PC_ESCAPE) {
			if (wp -> flags & EA_KEEPESCAPE)
				addstrbuf(&(wp -> fixed), &(wp -> s[i]), 1);
			if (wp -> s[i + 1] == _SC_) continue;

			if (wp -> quote == '\''
			|| (wp -> quote == '"'
			&& !Xstrchr(DQ_METACHAR, wp -> s[i + 1]))) {
				if (!(wp -> flags & EA_KEEPESCAPE))
					addstrbuf(&(wp -> fixed),
						&(wp -> s[i]), 1);
				addstrbuf(&(wp -> path), &(wp -> s[i]), 1);
			}
			i++;
		}
		else if (pc == PC_NORMAL && Xstrchr("?*[", wp -> s[i])) {
			if (w >= 0 && wp -> s[i] == '*') w++;
			else w = -1;
		}

		addstrbuf(&(wp -> fixed), &(wp -> s[i]), 1);
		addstrbuf(&(wp -> path), &(wp -> s[i]), 1);
	}

	if (!(wp -> s[i])) isdir = 0;
	else {
		isdir = 1;
		addstrbuf(&(wp -> fixed), rootpath, 1);
	}

	if (!w) {
		if (wp -> path.len <= plen) w++;
#if	defined (DOUBLESLASH) || defined (DEP_URLPATH)
		else if (prefix || wp -> type == WT_URLPATH) w++;
#endif
		else if (stat2(wp -> path.s, &st) < 0) return(argc);

		wp -> s += i;
		if (isdir) {
			if (!w && (st.st_mode & S_IFMT) != S_IFDIR)
				return(argc);
			(wp -> s)++;
		}

#ifndef	NODIRLOOP
		if (!w) {
			wp -> ino = (devino_t *)Xrealloc(wp -> ino,
				(wp -> nino + 1) * sizeof(devino_t));
			wp -> ino[wp -> nino].dev = st.st_dev;
			wp -> ino[(wp -> nino)++].ino = st.st_ino;
		}
#endif

		return(_evalwild(argc, argvp, wp));
	}

	if (w != 2 || !isdir || strcmp(&(wp -> path.s[plen]), "**")) w = -1;
	wp -> fixed.len = flen;
	wp -> path.len = plen;
	wp -> fixed.s[flen] = wp -> path.s[plen] = '\0';

	if (w > 0) {
		duplwild(&dupl, wp);
		dupl.s += i + 1;
		argc = _evalwild(argc, argvp, &dupl);
		freewild(&dupl);
		re = NULL;
	}
	else {
		new = Xmalloc(i + 2);
		n = 0;
		if (quote) new[n++] = quote;
		memcpy(&(new[n]), wp -> s, i);
		n += i;
		if (wp -> quote) new[n++] = wp -> quote;
		re = regexp_init(new, n);
		Xfree(new);
		if (!re) return(argc);
		wp -> s += i + 1;
	}

	if (wp -> path.len) cp = wp -> path.s;
	else if (wp -> fixed.len) cp = rootpath;
	else cp = curpath;

	if (!(dirp = Xopendir(cp))) {
		regexp_free(re);
		return(argc);
	}
	if (wp -> path.len || wp -> fixed.len)
		addstrbuf(&(wp -> path), rootpath, 1);

	while ((dp = Xreaddir(dirp))) {
		if (isdotdir(dp -> d_name)) continue;

		duplwild(&dupl, wp);
		n = strlen(dp -> d_name);
		addstrbuf(&(dupl.fixed), dp -> d_name, n);
		addstrbuf(&(dupl.path), dp -> d_name, n);

		if (isdir) {
			if (re) n = regexp_exec(re, dp -> d_name, 1);
			else n = (*(dp -> d_name) == '.') ? 0 : 1;

			if (!n || Xstat(dupl.path.s, &st) < 0
			|| (st.st_mode & S_IFMT) != S_IFDIR) {
				freewild(&dupl);
				continue;
			}

#ifndef	NODIRLOOP
			if (!re) {
				for (n = 0; n < dupl.nino; n++)
					if (dupl.ino[n].dev == st.st_dev
					&& dupl.ino[n].ino == st.st_ino)
						break;
				if (n < dupl.nino) {
					freewild(&dupl);
					continue;
				}
			}

			dupl.ino = (devino_t *)Xrealloc(dupl.ino,
				(dupl.nino + 1) * sizeof(devino_t));
			dupl.ino[dupl.nino].dev = st.st_dev;
			dupl.ino[(dupl.nino)++].ino = st.st_ino;
#endif

			addstrbuf(&(dupl.fixed), rootpath, 1);
			argc = _evalwild(argc, argvp, &dupl);
		}
		else if (regexp_exec(re, dp -> d_name, 1)) {
			*argvp = (char **)Xrealloc(*argvp,
				(argc + 2) * sizeof(char *));
			(*argvp)[argc++] = dupl.fixed.s;
			dupl.fixed.s = NULL;
		}

		freewild(&dupl);
	}
	VOID_C Xclosedir(dirp);
	regexp_free(re);

	return(argc);
}

int cmppath(vp1, vp2)
CONST VOID_P vp1;
CONST VOID_P vp2;
{
	return(strverscmp2(*((char **)vp1), *((char **)vp2)));
}

char **evalwild(s, flags)
CONST char *s;
int flags;
{
	wild_t w;
	char **argv;
	int argc;

	argv = (char **)Xmalloc(1 * sizeof(char *));
	w.s = s;
	w.fixed.s = c_realloc(NULL, 0, &(w.fixed.size));
	w.path.s = c_realloc(NULL, 0, &(w.path.size));
	w.fixed.len = w.path.len = (ALLOC_T)0;
	w.quote = '\0';
#ifndef	NODIRLOOP
	w.nino = 0;
	w.ino = NULL;
#endif
#if	defined (DOUBLESLASH) || defined (DEP_URLPATH)
	w.type = WT_NORMAL;
#endif
	w.flags = flags;

	argc = _evalwild(0, &argv, &w);
	freewild(&w);

	if (!argc) {
		Xfree(argv);
		return(NULL);
	}

	argv[argc] = NULL;
	if (argc > 1) qsort(argv, argc, sizeof(char *), cmppath);

	return(argv);
}

#ifndef	_NOUSEHASH
static int NEAR calchash(s)
CONST char *s;
{
	u_int n;
	int i;

	for (i = n = 0; s[i]; i++) n = n * 12345 + (u_char)(s[i]);

	return(n % MAXHASH);
}

static VOID NEAR inithash(VOID_A)
{
	int i;

	if (hashtable) return;
	hashtable = (hashlist **)Xmalloc(MAXHASH * sizeof(hashlist *));
	for (i = 0; i < MAXHASH; i++) hashtable[i] = NULL;
}

static hashlist *NEAR newhash(com, path, cost, next)
CONST char *com;
char *path;
int cost;
hashlist *next;
{
	hashlist *new;

	new = (hashlist *)Xmalloc(sizeof(hashlist));
	new -> command = Xstrdup(com);
	new -> path = path;
	new -> hits = 0;
	new -> cost = cost;
	new -> next = next;

	return(new);
}

static VOID NEAR freehash(htable)
hashlist **htable;
{
	hashlist *hp, *next;
	int i;

	if (!htable) {
		if (!hashtable) return;
		htable = hashtable;
	}
	for (i = 0; i < MAXHASH; i++) {
		next = NULL;
		for (hp = htable[i]; hp; hp = next) {
			next = hp -> next;
			Xfree(hp -> command);
			Xfree(hp -> path);
			Xfree(hp);
		}
		htable[i] = NULL;
	}
	Xfree(htable);
	if (htable == hashtable) hashtable = NULL;
}

hashlist **duplhash(htable)
hashlist **htable;
{
	CONST hashlist *hp;
	hashlist **nextp, **new;
	int i;

	if (!htable) return(NULL);
	new = (hashlist **)Xmalloc(MAXHASH * sizeof(hashlist *));

	for (i = 0; i < MAXHASH; i++) {
		*(nextp = &(new[i])) = NULL;
		for (hp = htable[i]; hp; hp = hp -> next) {
			*nextp = newhash(hp -> command,
				Xstrdup(hp -> path), hp -> cost, NULL);
			(*nextp) -> type = hp -> type;
			(*nextp) -> hits = hp -> hits;
			nextp = &((*nextp) -> next);
		}
	}

	return(new);
}

static hashlist *NEAR findhash(com, n)
CONST char *com;
int n;
{
	hashlist *hp;

	for (hp = hashtable[n]; hp; hp = hp -> next)
		if (!strpathcmp(com, hp -> command)) return(hp);

	return(NULL);
}

static VOID NEAR rmhash(com, n)
CONST char *com;
int n;
{
	hashlist *hp, *prev;

	prev = NULL;
	for (hp = hashtable[n]; hp; hp = hp -> next) {
		if (!strpathcmp(com, hp -> command)) {
			if (!prev) hashtable[n] = hp -> next;
			else prev -> next = hp -> next;
			Xfree(hp -> command);
			Xfree(hp -> path);
			Xfree(hp);
			return;
		}
		prev = hp;
	}
}
#endif	/* !_NOUSEHASH */

static int NEAR isexecute(path, dirok, exe)
CONST char *path;
int dirok, exe;
{
	struct stat st;
	int d;

	if (stat2(path, &st) < 0) return(-1);
	d = ((st.st_mode & S_IFMT) == S_IFDIR);
	if (!exe) return(d);
	if (d) return(dirok ? d : -1);

	return(Xaccess(path, X_OK));
}

#if	MSDOS
static int NEAR extaccess(path, ext, len, exe)
char *path;
CONST char *ext;
int len, exe;
{
	if (ext) {
		if (isexecute(path, 0, exe) >= 0) {
			if (!(strpathcmp(ext, EXTCOM))) return(0);
			else if (!(strpathcmp(ext, EXTEXE))) return(CM_EXE);
			else if (!(strpathcmp(ext, EXTBAT))) return(CM_BATCH);
		}
	}
	else {
		path[len++] = '.';
		Xstrcpy(&(path[len]), EXTCOM);
		if (isexecute(path, 0, exe) >= 0) return(CM_ADDEXT);
		Xstrcpy(&(path[len]), EXTEXE);
		if (isexecute(path, 0, exe) >= 0) return(CM_ADDEXT | CM_EXE);
		Xstrcpy(&(path[len]), EXTBAT);
		if (isexecute(path, 0, exe) >= 0) return(CM_ADDEXT | CM_BATCH);
	}

	return(-1);
}
#endif	/* MSDOS */

int searchhash(hpp, com, search)
hashlist **hpp;
CONST char *com, *search;
{
#ifndef	_NOUSEHASH
	hashlist *hp;
	int n, recalc, duperrno;
#endif
#if	MSDOS
	char *ext;
#endif
	CONST char *cp, *next;
	char *tmp, *path;
	int len, dlen, cost, size, ret;

#ifndef	_NOUSEHASH
	if (!hpp || (!com && !search)) {
		duperrno = errno;
		if (!com && !search) freehash(hpp);
		if (!hpp && hashtable) for (n = 0; n < MAXHASH; n++) {
			for (hp = hashtable[n]; hp; hp = hp -> next)
				if (hp -> type & CM_RECALC)
					hp -> type |= CM_REHASH;
		}
		errno = duperrno;
		return(CM_NOTFOUND);
	}
#endif	/* !_NOUSEHASH */

#if	MSDOS
	if ((ext = Xstrrchr(com, '.')) && strdelim(++ext, 0)) ext = NULL;
#endif
	if (strdelim(com, 1)) {
#if	MSDOS
		len = strlen(com);
		path = Xmalloc(len + EXTWIDTH + 1);
		Xstrcpy(path, com);
		ret = extaccess(path, ext, len, 0);
		Xfree(path);
		if (ret >= 0) return(ret | CM_FULLPATH);
#else
		if (isexecute(com, 0, 0) >= 0) return(CM_FULLPATH);
#endif
		return(CM_NOTFOUND | CM_FULLPATH);
	}

#ifndef	_NOUSEHASH
	if (!hashtable) inithash();
	n = calchash(com);
	if ((*hpp = findhash(com, n))) {
		if (!((*hpp) -> type & CM_REHASH)
		&& isexecute((*hpp) -> path, 0, 1) >= 0)
			return((*hpp) -> type);
		rmhash(com, n);
	}
#endif

#ifdef	CWDINPATH
	len = strlen(com);
	path = Xmalloc(len + 2 + EXTWIDTH + 1);
	path[0] = '.';
	path[1] = _SC_;
	Xstrcpy(&(path[2]), com);
# if	MSDOS
	if ((ret = extaccess(path, ext, len + 2, 1)) < 0) Xfree(path);
# else
	if ((ret = isexecute(path, 0, 0)) < 0) Xfree(path);
# endif
	else {
# ifdef	_NOUSEHASH
		*hpp = (hashlist *)path;
# else
		*hpp = newhash(com, path, 0, hashtable[n]);
		hashtable[n] = *hpp;
		(*hpp) -> type = (ret |= (CM_HASH | CM_RECALC));
# endif
		return(ret);
	}
#endif	/* CWDINPATH */

#ifndef	_NOUSEHASH
	recalc = 0;
#endif
	if ((next = (search) ? search : getconstvar(ENVPATH))) {
		len = strlen(com);
		size = ret = 0;
		path = NULL;
		cost = 1;
		for (cp = next; cp; cp = next) {
			next = cp;
#ifdef	DEP_DOSPATH
			if (_dospath(cp)) next += 2;
#endif
#ifndef	_NOUSEHASH
			if (*next != _SC_) recalc = CM_RECALC;
#endif
			next = Xstrchr(next, PATHDELIM);
			dlen = (next) ? (next++) - cp : strlen(cp);
			if (!dlen) tmp = NULL;
			else {
				tmp = _evalpath(cp, &(cp[dlen]), 0);
				dlen = strlen(tmp);
			}
			if (dlen + len + 1 + EXTWIDTH + 1 > size) {
				size = dlen + len + 1 + EXTWIDTH + 1;
				path = Xrealloc(path, size);
			}
			if (tmp) {
				Xstrncpy(path, tmp, dlen);
				Xfree(tmp);
				dlen = strcatdelim(path) - path;
			}
			Xstrncpy(&(path[dlen]), com, len);
			dlen += len;
#if	MSDOS
			if ((ret = extaccess(path, ext, dlen, 1)) >= 0) break;
#else
			if (isexecute(path, 0, 1) >= 0) break;
#endif
			cost++;
		}
		if (cp) {
#ifdef	_NOUSEHASH
			*hpp = (hashlist *)path;
#else
			*hpp = newhash(com, path, cost, hashtable[n]);
			hashtable[n] = *hpp;
			(*hpp) -> type = (ret |= (CM_HASH | recalc));
#endif
			return(ret);
		}
		Xfree(path);
	}

	return(CM_NOTFOUND);
}

#ifdef	FD
char *searchexecpath(path, search)
CONST char *path, *search;
{
	hashlist *hp;
	int type;

	if ((type = searchhash(&hp, path, search)) & CM_NOTFOUND) return(NULL);
	if (type & CM_FULLPATH) return(Xstrdup(path));
# ifdef	_NOUSEHASH
	return((char *)hp);
# else
	return(Xstrdup(hp -> path));
# endif
}
#endif	/* FD */

#if	!defined (FDSH) && !defined (_NOCOMPLETE)
int addcompletion(s, cp, argc, argvp)
CONST char *s;
char *cp;
int argc;
char ***argvp;
{
	int n;

	if (!s) {
		if (!cp) return(argc);
		s = cp;
	}

	for (n = 0; n < argc; n++)
		if (!strpathcmp(s, (*argvp)[n])) break;

	if (n < argc) Xfree(cp);
	else {
		*argvp = (char **)Xrealloc(*argvp, ++argc * sizeof(char *));
		(*argvp)[n] = (cp) ? cp : Xstrdup(s);
	}

	return(argc);
}

# ifndef	NOUID
#  ifndef	NOGETPWENT
int completeuser(name, len, argc, argvp, home)
CONST char *name;
int len, argc;
char ***argvp;
int home;
{
	struct passwd *pwd;
	char *new;
	ALLOC_T size;

	len = strlen(name);
#   ifdef	DEBUG
	_mtrace_file = "setpwent(start)";
	setpwent();
	if (_mtrace_file) _mtrace_file = NULL;
	else {
		_mtrace_file = "setpwent(end)";
		malloc(0);	/* dummy alloc */
	}
	for (;;) {
		_mtrace_file = "getpwent(start)";
		pwd = getpwent();
		if (_mtrace_file) _mtrace_file = NULL;
		else {
			_mtrace_file = "getpwent(end)";
			malloc(0);	/* dummy alloc */
		}
		if (!pwd) break;
		if (strnpathcmp(name, pwd -> pw_name, len)) continue;
		if (!home) new = Xstrdup(pwd -> pw_name);
		else {
			size = strlen(pwd -> pw_name);
			new = Xmalloc(size + 2 + 1);
			new[0] = '~';
			VOID_C strcatdelim2(&(new[1]), pwd -> pw_name, NULL);
		}
		argc = addcompletion(NULL, new, argc, argvp);
	}
	_mtrace_file = "endpwent(start)";
	endpwent();
	if (_mtrace_file) _mtrace_file = NULL;
	else {
		_mtrace_file = "endpwent(end)";
		malloc(0);	/* dummy alloc */
	}
#   else	/* !DEBUG */
	setpwent();
	while ((pwd = getpwent())) {
		if (strnpathcmp(name, pwd -> pw_name, len)) continue;
		if (!home) new = Xstrdup(pwd -> pw_name);
		else {
			size = strlen(pwd -> pw_name);
			new = Xmalloc(size + 2 + 1);
			new[0] = '~';
			VOID_C strcatdelim2(&(new[1]), pwd -> pw_name, NULL);
		}
		argc = addcompletion(NULL, new, argc, argvp);
	}
	endpwent();
#   endif	/* !DEBUG */

	return(argc);
}
#  endif	/* !NOGETPWENT */

#  ifndef	NOGETGRENT
int completegroup(name, len, argc, argvp)
CONST char *name;
int len, argc;
char ***argvp;
{
	struct group *grp;

	len = strlen(name);
#   ifdef	DEBUG
	_mtrace_file = "setgrent(start)";
	setgrent();
	if (_mtrace_file) _mtrace_file = NULL;
	else {
		_mtrace_file = "setgrent(end)";
		malloc(0);	/* dummy alloc */
	}
	for (;;) {
		_mtrace_file = "getgrent(start)";
		grp = getgrent();
		if (_mtrace_file) _mtrace_file = NULL;
		else {
			_mtrace_file = "getgrent(end)";
			malloc(0);	/* dummy alloc */
		}
		if (!grp) break;
		if (strnpathcmp(name, grp -> gr_name, len)) continue;
		argc = addcompletion(grp -> gr_name, NULL, argc, argvp);
	}
	_mtrace_file = "endgrent(start)";
	endgrent();
	if (_mtrace_file) _mtrace_file = NULL;
	else {
		_mtrace_file = "endgrent(end)";
		malloc(0);	/* dummy alloc */
	}
#   else	/* !DEBUG */
	setgrent();
	while ((grp = getgrent())) {
		if (strnpathcmp(name, grp -> gr_name, len)) continue;
		argc = addcompletion(grp -> gr_name, NULL, argc, argvp);
	}
	endgrent();
#   endif	/* !DEBUG */

	return(argc);
}
#  endif	/* !NOGETGRENT */
# endif	/* !NOUID */

static int NEAR completefile(file, len, argc, argvp, dir, dlen, exe)
CONST char *file;
int len, argc;
char ***argvp;
CONST char *dir;
int dlen, exe;
{
	DIR *dirp;
	struct dirent *dp;
	char *cp, *new, path[MAXPATHLEN];
	int d, size, dirok;

	if (dlen >= MAXPATHLEN - 2) return(argc);
	Xstrncpy(path, dir, dlen);
	if (!(dirp = Xopendir(path))) return(argc);
	cp = strcatdelim(path);

	dirok = (exe <= 1) ? 1 : 0;
	while ((dp = Xreaddir(dirp))) {
		if ((!len && isdotdir(dp -> d_name))
		|| strnpathcmp(file, dp -> d_name, len))
			continue;
		size = strcatpath(path, cp, dp -> d_name);
		if (size < 0) continue;

		if (isdotdir(dp -> d_name)) d = 1;
		else if ((d = isexecute(path, dirok, exe)) < 0) continue;

		new = Xmalloc(size + 1 + 1);
		Xstrncpy(new, dp -> d_name, size);
		if (d) new[size++] = _SC_;
		new[size] = '\0';
		argc = addcompletion(NULL, new, argc, argvp);
	}
	VOID_C Xclosedir(dirp);

	return(argc);
}

static int NEAR completeexe(file, len, argc, argvp)
CONST char *file;
int len, argc;
char ***argvp;
{
	char *cp, *tmp, *next;
	int dlen;

# ifdef	CWDINPATH
	argc = completefile(file, len, argc, argvp, curpath, 1, 2);
# endif
	if (!(next = getconstvar(ENVPATH))) return(argc);
	for (cp = next; cp; cp = next) {
# ifdef	DEP_DOSPATH
		if (_dospath(cp)) next = Xstrchr(&(cp[2]), PATHDELIM);
		else
# endif
		next = Xstrchr(cp, PATHDELIM);
		dlen = (next) ? (next++) - cp : strlen(cp);
		tmp = _evalpath(cp, &(cp[dlen]), 0);
		dlen = strlen(tmp);
		argc = completefile(file, len, argc, argvp, tmp, dlen, 2);
		Xfree(tmp);
	}

	return(argc);
}

int completepath(path, len, argc, argvp, exe)
CONST char *path;
int len, argc;
char ***argvp;
int exe;
{
# ifdef	DEP_DOSPATH
	char cwd[4];
# endif
# if	defined (DOUBLESLASH) || defined (DEP_URLPATH)
	int n;
# endif
	CONST char *dir, *file;
	int dlen;

	dlen = 0;
# ifdef	DEP_DOSPATH
	if (_dospath(path)) dlen = 2;
# endif
	dir = &(path[dlen]);

	if ((file = strrdelim(dir, 0))) {
		if (file == dir) {
			dlen++;
			file++;
		}
# ifdef	DOUBLESLASH
		else if ((n = isdslash(path)) && file < &(path[n])) {
			if (path[n - 1] != _SC_) return(argc);
			dlen = n;
			file = &(path[n]);
			if (*file == _SC_) file++;
		}
# endif
# ifdef	DEP_URLPATH
		else if ((n = _urlpath(path, NULL, NULL)) && file < &(path[n]))
			return(argc);
# endif
		else dlen += file++ - dir;
		return(completefile(file, strlen(file), argc, argvp,
			path, dlen, exe));
	}
# if	!defined (NOUID) && !defined (NOGETPWENT)
	else if (*path == '~')
		return(completeuser(&(path[1]), len - 1, argc, argvp, 1));
# endif
# ifdef	DEP_DOSPATH
	else if (dir != path) {
		len -= dlen;
		dlen = gendospath(cwd, *path, '.') - cwd;
		return(completefile(dir, len, argc, argvp, cwd, dlen, exe));
	}
# endif
	else if (exe) return(completeexe(path, len, argc, argvp));

	return(completefile(path, len, argc, argvp, curpath, 1, exe));
}

char *findcommon(argc, argv)
int argc;
char *CONST *argv;
{
	char *common;
	int i, n;

	if (!argv || !argv[0]) return(NULL);
	common = Xstrdup(argv[0]);

	for (n = 1; n < argc; n++) {
		for (i = 0; common[i]; i++) if (common[i] != argv[n][i]) break;
		common[i] = '\0';
	}

	if (!common[0]) {
		Xfree(common);
		return(NULL);
	}

	return(common);
}
#endif	/* !FDSH && !_NOCOMPLETE */

static int NEAR addmeta(s1, s2, flags)
char *s1;
CONST char *s2;
int flags;
{
	int i, j, c;

	if (!s2) return(0);
	if (flags & EA_INQUOTE) flags |= EA_NOEVALQ;
	for (i = j = 0; s2[i]; i++, j++) {
		c = '\0';
		if (s2[i] == '\'' && !(flags & EA_NOEVALQ)) c = PESCAPE;
		else if (s2[i] == '"' && !(flags & EA_NOEVALDQ)) c = PESCAPE;
#ifndef	FAKEESCAPE
		else if (s2[i] == PESCAPE && !(flags & EA_STRIPESCAPE))
			c = PESCAPE;
#endif
		else if (iswchar(s2, i)) c = s2[i++];

		if (c) {
			if (s1) s1[j] = c;
			j++;
		}
		if (s1) s1[j] = s2[i];
	}

	return(j);
}

char *catvar(argv, delim)
char *CONST *argv;
int delim;
{
	char *cp;
	int i, len;

	if (!argv) return(NULL);
	for (i = len = 0; argv[i]; i++) len += strlen(argv[i]);
	if (i < 1) return(Xstrdup(nullstr));
	len += (delim) ? i - 1 : 0;
	cp = Xmalloc(len + 1);
	len = Xstrcpy(cp, argv[0]) - cp;
	for (i = 1; argv[i]; i++) {
		if (delim) cp[len++] = delim;
		len = Xstrcpy(&(cp[len]), argv[i]) - cp;
	}

	return(cp);
}

int countvar(var)
char *CONST *var;
{
	int i;

	if (!var) return(0);
	for (i = 0; var[i]; i++) /*EMPTY*/;

	return(i);
}

VOID freevar(var)
char **var;
{
	int i, duperrno;

	duperrno = errno;
	if (var) {
		for (i = 0; var[i]; i++) Xfree(var[i]);
		Xfree(var);
	}
	errno = duperrno;
}

char **duplvar(var, margin)
char *CONST *var;
int margin;
{
	char **dupl;
	int i, n;

	if (margin < 0) {
		if (!var) return(NULL);
		margin = 0;
	}
	n = countvar(var);
	dupl = (char **)Xmalloc((n + margin + 1) * sizeof(char *));
	for (i = 0; i < n; i++) dupl[i] = Xstrdup(var[i]);
	dupl[i] = NULL;

	return(dupl);
}

int parsechar(s, len, spc, flags, qp, pqp)
CONST char *s;
int len, spc, flags, *qp, *pqp;
{
	if (*s == *qp) {
#ifdef	FD
		if (flags & EA_FINDMETA)
			return((*qp == '\'') ? PC_EXMETA : PC_META);
#endif
		if (!pqp) *qp = '\0';
		else {
			*qp = *pqp;
			*pqp = '\0';
		}
		return(PC_CLQUOTE);
	}
	else if (iswchar(s, 0)) return(PC_WCHAR);
	else if (*qp == '\'') return(PC_SQUOTE);
#ifdef	FD
	else if ((flags & EA_FINDMETA) && Xstrchr(DQ_METACHAR, *s))
		return(PC_META);
	else if ((flags & EA_FINDDELIM) && Xstrchr(CMDLINE_DELIM, *s))
		return(PC_DELIM);
#endif
	else if (isescape(s, 0, *qp, len, flags)) return(PC_ESCAPE);
	else if (*qp == '`') return(PC_BQUOTE);
#ifdef	NESTINGQUOTE
	else if ((flags & EA_BACKQ) && *s == '`') {
		if (pqp && *qp) *pqp = *qp;
		*qp = *s;
		return(PC_OPQUOTE);
	}
#endif
	else if (spc && *s == spc) return(*s);
	else if (*qp) return(PC_DQUOTE);
	else if (!(flags & EA_NOEVALQ) && *s == '\'') {
#ifdef	FD
		if (flags & EA_FINDMETA) return(PC_META);
#endif
		*qp = *s;
		return(PC_OPQUOTE);
	}
	else if (!(flags & EA_NOEVALDQ) && *s == '"') {
		*qp = *s;
		return(PC_OPQUOTE);
	}
#ifndef	NESTINGQUOTE
	else if ((flags & EA_BACKQ) && *s == '`') {
		if (pqp && *qp) *pqp = *qp;
		*qp = *s;
		return(PC_OPQUOTE);
	}
#endif
#ifdef	FD
	else if ((flags & EA_FINDMETA) && Xstrchr(METACHAR, *s))
		return(PC_META);
#endif

	return(PC_NORMAL);
}

static int NEAR skipvar(bufp, eolp, ptrp, flags)
CONST char **bufp;
int *eolp, *ptrp, flags;
{
	int i, mode;

	mode = '\0';
	*bufp += *ptrp;
	*ptrp = 0;

	if (isidentchar((*bufp)[*ptrp])) {
		while ((*bufp)[++(*ptrp)])
			if (!isidentchar2((*bufp)[*ptrp])) break;
		if (eolp) *eolp = *ptrp;
		return(mode);
	}

#ifndef	MINIMUMSHELL
	if ((*bufp)[*ptrp] == '(') {
		if ((*bufp)[++(*ptrp)] != '(') {
			if (skipvarvalue(*bufp, ptrp, ")", flags, 0, 1) < 0)
				return(-1);
		}
		else {
			(*ptrp)++;
			if (skipvarvalue(*bufp, ptrp, "))", flags, 0, 1) < 0)
				return(-1);
		}
		if (eolp) *eolp = *ptrp;
		return('(');
	}
#endif

	if ((*bufp)[*ptrp] != '{') {
		if (!(flags & EA_INQUOTE) || (*bufp)[*ptrp] != '"') (*ptrp)++;
		if (eolp) *eolp = *ptrp;
		return(mode);
	}

	(*bufp)++;
#ifndef	MINIMUMSHELL
	if ((*bufp)[*ptrp] == '#' && (*bufp)[*ptrp + 1] != '}') {
		(*bufp)++;
		mode = 'n';
	}
#endif
	if (isidentchar((*bufp)[*ptrp])) {
		while ((*bufp)[++(*ptrp)])
			if (!isidentchar2((*bufp)[*ptrp])) break;
	}
	else (*ptrp)++;

	if (eolp) *eolp = *ptrp;
	if ((i = (*bufp)[(*ptrp)++]) == '}') return(mode);
#ifndef	MINIMUMSHELL
	if (mode) return(-1);
#endif

	if (i == ':') {
		mode = (*bufp)[(*ptrp)++];
		if (!Xstrchr("-=?+", mode)) return(-1);
		mode |= 0x80;
	}
	else if (Xstrchr("-=?+", i)) mode = i;
#ifndef	MINIMUMSHELL
	else if (i != RMSUFFIX && i != '#') return(-1);
	else if ((*bufp)[*ptrp] != i) mode = i;
	else {
		(*ptrp)++;
		mode = i | 0x80;
	}
#endif	/* !MINIMUMSHELL */

#ifdef	BASHSTYLE
	/* bash treats any meta character in ${} as just a character */
# ifdef	MINIMUMSHELL
	if (skipvarvalue(*bufp, ptrp, "}", flags, 0) < 0) return(-1);
# else
	if (skipvarvalue(*bufp, ptrp, "}", flags, 0, 0) < 0) return(-1);
# endif
#else	/* !BASHSTYLE */
# ifdef	MINIMUMSHELL
	if (skipvarvalue(*bufp, ptrp, "}", flags, 1) < 0) return(-1);
# else
	i = (flags & EA_INQUOTE) ? 0 : 1;
	if (skipvarvalue(*bufp, ptrp, "}", flags, i, 0) < 0) return(-1);
# endif
#endif	/* !BASHSTYLE */

	return(mode);
}

#ifdef	MINIMUMSHELL
static int NEAR skipvarvalue(s, ptrp, next, flags, nonl)
CONST char *s;
int *ptrp;
CONST char *next;
int flags, nonl;
#else
static int NEAR skipvarvalue(s, ptrp, next, flags, nonl, nest)
CONST char *s;
int *ptrp;
CONST char *next;
int flags, nonl, nest;
#endif
{
#ifdef	NESTINGQUOTE
	int pq;
#endif
	CONST char *cp;
	int pc, q, f, len;

#ifdef	NESTINGQUOTE
	pq = '\0';
#endif
	q = '\0';
	len = strlen(next);
	while (s[*ptrp]) {
		f = flags;
		if (q == '"') f |= EA_INQUOTE;
#ifdef	NESTINGQUOTE
		pc = parsechar(&(s[*ptrp]), -1, '$', EA_BACKQ, &q, &pq);
#else
		pc = parsechar(&(s[*ptrp]), -1, '$', EA_BACKQ, &q, NULL);
#endif
		if (pc == PC_WCHAR || pc == PC_ESCAPE) (*ptrp)++;
		else if (pc == '$') {
			if (!s[++(*ptrp)]) return(0);
			cp = s;
			if (skipvar(&cp, NULL, ptrp, f) < 0) return(-1);
			*ptrp += (cp - s);
			continue;
		}
		else if (pc != PC_NORMAL) /*EMPTY*/;
		else if (nonl && s[*ptrp] == '\n') return(-1);
#ifndef	MINIMUMSHELL
		else if (nest && s[*ptrp] == '(') {
			(*ptrp)++;
			if (skipvarvalue(s, ptrp, ")", f, nonl, nest) < 0)
				return(-1);
			continue;
		}
#endif
		else if (!strncmp(&(s[*ptrp]), next, len)) {
			*ptrp += len;
			return(0);
		}

		(*ptrp)++;
	}

	return(-1);
}

#ifndef	MINIMUMSHELL
static char *NEAR removeword(s, pattern, plen, mode)
CONST char *s, *pattern;
int plen, mode;
{
	reg_ex_t *re;
	CONST char *cp;
	char *ret, *tmp, *new;
	int c, n, len;

	if (!s || !*s) return(NULL);
	tmp = Xstrndup(pattern, plen);
	new = evalarg(tmp, EA_BACKQ);
	Xfree(tmp);
	re = regexp_init(new, -1);
	Xfree(new);
	if (!re) return(NULL);
	ret = NULL;
	len = strlen(s);
	n = -1;
	if ((mode & ~0x80) != '#') {
		if (mode & 0x80) for (cp = s; cp < &(s[len]); cp++) {
			if (regexp_exec(re, cp, 0)) {
				n = cp - s;
				break;
			}
		}
		else for (cp = &(s[len - 1]); cp >= s; cp--) {
			if (regexp_exec(re, cp, 0)) {
				n = cp - s;
				break;
			}
		}
		if (n >= 0) ret = Xstrndup(s, n);
	}
	else {
		new = Xstrdup(s);
		if (mode & 0x80)
		for (tmp = &(new[len]); tmp >= new; tmp--) {
			*tmp = '\0';
			if (regexp_exec(re, new, 0)) {
				n = tmp - new;
				break;
			}
		}
		else for (tmp = &(new[1]); tmp <= &(new[len]); tmp++) {
			c = *tmp;
			*tmp = '\0';
			if (regexp_exec(re, new, 0)) {
				n = tmp - new;
				break;
			}
			*tmp = c;
		}
		if (n >= 0) ret = Xstrdup(&(s[n]));
		Xfree(new);
	}
	regexp_free(re);

	return(ret);
}

static char **NEAR removevar(var, pattern, plen, mode)
char **var;
CONST char *pattern;
int plen, mode;
{
	char *cp, **new;
	int i, n;

	n = countvar(var);
	new = (char **)Xmalloc((n + 1) * sizeof(char *));
	for (i = 0; i < n; i++) {
		if ((cp = removeword(var[i], pattern, plen, mode)))
			new[i] = cp;
		else new[i] = Xstrdup(var[i]);
	}
	new[i] = NULL;

	return(new);
}
#endif	/* !MINIMUMSHELL */

#ifdef	MINIMUMSHELL
static char *NEAR evalshellparam(c, flags)
int c, flags;
#else
static char *NEAR evalshellparam(c, flags, pattern, plen, modep)
int c, flags;
CONST char *pattern;
int plen, *modep;
#endif
{
#ifndef	MINIMUMSHELL
	char **new;
#endif
	char *cp, **arglist, tmp[MAXLONGWIDTH + 1];
	p_id_t pid;
	int i, j, sp;

	cp = NULL;
#ifndef	MINIMUMSHELL
	new = NULL;
#endif
	switch (c) {
		case '@':
			if (!(arglist = argvar)) break;
#ifndef	MINIMUMSHELL
			if (modep) {
				new = removevar(arglist,
					pattern, plen, *modep);
				arglist = new;
				*modep = '\0';
			}
#endif
			sp = ' ';
			if (!(flags & EA_INQUOTE)) {
				cp = catvar(&(arglist[1]), sp);
				break;
			}
#ifdef	BASHSTYLE
	/* bash uses IFS instead of a space as a separator */
			if ((cp = getconstvar(ENVIFS)) && *cp)
				sp = *cp;
#endif

			for (i = j = 0; arglist[i + 1]; i++)
				j += addmeta(NULL, arglist[i + 1], flags);
			if (i <= 0) cp = Xstrdup(nullstr);
			else {
				j += (i - 1) * 3;
				cp = Xmalloc(j + 1);
				j = addmeta(cp, arglist[1], flags);
				for (i = 2; arglist[i]; i++) {
					cp[j++] = '"';
					cp[j++] = sp;
					cp[j++] = '"';
					j += addmeta(&(cp[j]), arglist[i],
						flags);
				}
				cp[j] = '\0';
			}
			break;
		case '*':
			if (!(arglist = argvar)) break;
#ifndef	MINIMUMSHELL
			if (modep) {
				new = removevar(arglist,
					pattern, plen, *modep);
				arglist = new;
				*modep = '\0';
			}
#endif
			sp = ' ';
#ifdef	BASHSTYLE
	/* bash uses IFS instead of a space as a separator */
			if ((flags & EA_INQUOTE) && (cp = getconstvar(ENVIFS)))
				sp = (*cp) ? *cp : '\0';
#endif
			cp = catvar(&(arglist[1]), sp);
			break;
		case '#':
			if (!argvar) break;
			VOID_C Xsnprintf(tmp, sizeof(tmp),
				"%d", countvar(argvar + 1));
			cp = tmp;
			break;
		case '?':
			VOID_C Xsnprintf(tmp, sizeof(tmp),
				"%d",
				(getretvalfunc) ? (*getretvalfunc)() : 0);
			cp = tmp;
			break;
		case '$':
			if (!getpidfunc) pid = getpid();
			else pid = (*getpidfunc)();
			VOID_C Xsnprintf(tmp, sizeof(tmp), "%id", pid);
			cp = tmp;
			break;
		case '!':
			if (getlastpidfunc
			&& (pid = (*getlastpidfunc)()) >= 0) {
				VOID_C Xsnprintf(tmp, sizeof(tmp), "%id", pid);
				cp = tmp;
			}
			break;
		case '-':
			if (getflagfunc) cp = (*getflagfunc)();
			break;
		default:
			break;
	}
	if (cp == tmp) cp = Xstrdup(tmp);
#ifndef	MINIMUMSHELL
	freevar(new);
#endif

	return(cp);
}

static int NEAR replacevar(arg, cpp, s, len, vlen, mode)
CONST char *arg;
char **cpp;
int s, len, vlen, mode;
{
	char *val;
	int i;

	if (!mode) return(0);
	if (mode == '+') {
		if (!*cpp) return(0);
	}
	else if (*cpp) return(0);
	else if (mode == '=' && !isidentchar(*arg)) return(-1);

	val = Xstrndup(&(arg[s]), vlen);
	*cpp = evalarg(val,
		(mode == '=' || mode == '?')
		? EA_STRIPQ | EA_BACKQ : EA_BACKQ);
	Xfree(val);
	if (!*cpp) return(-1);

	if (mode == '=') {
#ifdef	FD
		demacroarg(cpp);
#endif
		if (setvar(arg, *cpp, len) < 0) {
			Xfree(*cpp);
			*cpp = NULL;
			return(-1);
		}
#ifdef	BASHSTYLE
	/* bash does not evaluates a quoted string in substitution itself */
		Xfree(*cpp);
		val = Xstrndup(&(arg[s]), vlen);
		*cpp = evalarg(val, EA_BACKQ);
		Xfree(val);
		if (!*cpp) return(-1);
# ifdef	FD
		demacroarg(cpp);
# endif
#endif
	}
	else if (mode == '?') {
#ifdef	FD
		demacroarg(cpp);
#endif
		for (i = 0; i < len; i++) Xfputc(arg[i], Xstderr);
		VOID_C Xfprintf(Xstderr,
			": %k",
			(vlen > 0) ? *cpp : "parameter null or not set");
		VOID_C fputnl(Xstderr);
		Xfree(*cpp);
		*cpp = NULL;
		if (exitfunc) (*exitfunc)();
		return(-2);
	}

	return(mode);
}

static char *NEAR insertarg(buf, ptr, arg, olen, nlen)
char *buf;
int ptr;
CONST char *arg;
int olen, nlen;
{
	if (nlen <= olen) return(buf);
	buf = Xrealloc(buf, ptr + nlen + (int)strlen(arg) - olen + 1);

	return(buf);
}

static int NEAR evalvar(bufp, ptr, argp, flags)
char **bufp;
int ptr;
CONST char **argp;
int flags;
{
#ifndef	MINIMUMSHELL
	char tmp[MAXLONGWIDTH + 1];
#endif
	CONST char *arg, *top;
	char *cp, *new;
	int i, c, n, len, vlen, s, nul, mode;

	new = NULL;
	arg = *argp;
	top = &(arg[1]);
	n = 0;
	if ((mode = skipvar(&top, &s, &n, flags)) < 0) return(-1);

	*argp = &(top[n - 1]);
	if (!(len = s)) {
		(*bufp)[ptr++] = '$';
		return(ptr);
	}

	nul = (mode & 0x80);
	if (n > 0 && top[n - 1] == '}') n--;
	if (!mode) vlen = 0;
	else {
		s++;
		if (nul) s++;
		vlen = n - s;
	}

#ifdef	MINIMUMSHELL
	mode &= ~0x80;
#else
	if (Xstrchr("-=?+", mode & ~0x80)) mode &= ~0x80;
	else if (mode == '(') {
		if (!posixsubstfunc) return(-1);
		for (;;) {
			if ((new = (*posixsubstfunc)(top, &n))) break;
			if (n < 0 || !top[n]) return(-1);
			if (skipvarvalue(top, &n, ")", flags, 0, 1) < 0)
				return(-1);
			*argp = top + n - 1;
		}
		mode = '\0';
	}
	else if (mode == 'n') {
		if (len == 1 && (*top == '*' || *top == '@')) {
			top--;
			mode = '\0';
		}
	}
	else nul = -1;
#endif	/* !MINIMUMSHELL */

	c = '\0';
	if ((cp = new)) /*EMPTY*/;
	else if (isidentchar(*top)) cp = getenvvar(top, len);
	else if (Xisdigit(*top)) {
		if (len > 1) return(-1);
		if (!argvar) {
			(*bufp)[ptr++] = '$';
			(*bufp)[ptr++] = *top;
			return(ptr);
		}
		i = *top - '0';
		if (i < countvar(argvar)) cp = argvar[i];
	}
	else if (len == 1) {
		c = *top;
#ifdef	MINIMUMSHELL
		cp = evalshellparam(c, flags);
#else
		cp = evalshellparam(c, flags,
			&(top[s]), vlen, (nul < 0) ? &mode : NULL);
#endif
		if (cp) new = cp;
		else {
			(*bufp)[ptr++] = '$';
			(*bufp)[ptr++] = *top;
			return(ptr);
		}
	}
	else return(-1);

	if (!mode && checkundeffunc && (*checkundeffunc)(cp, top, len) < 0)
		return(-1);
	if (cp && (nul == 0x80) && !*cp) cp = NULL;

#ifndef	MINIMUMSHELL
	if (mode == 'n') {
		VOID_C Xsnprintf(tmp, sizeof(tmp),
			"%d", (cp) ? strlen(cp) : 0);
		cp = tmp;
	}
	else if (nul == -1) {
		new = removeword(cp, top + s, vlen, mode);
		if (new) cp = new;
	}
	else
#endif	/* !MINIMUMSHELL */
	if ((mode = replacevar(top, &cp, s, len, vlen, mode)) < 0) {
		Xfree(new);
		return(mode);
	}
	else if (mode) {
		Xfree(new);
		new = cp;
	}

	if (!mode && (c != '@' || !(flags & EA_INQUOTE))) {
		vlen = addmeta(NULL, cp, flags);
		*bufp = insertarg(*bufp, ptr, arg, *argp - arg + 1, vlen);
		addmeta(&((*bufp)[ptr]), cp, flags);
	}
	else if (!cp) vlen = 0;
	else if (c == '@' && !*cp && (flags & EA_INQUOTE)
	&& ptr > 0 && (*bufp)[ptr - 1] == '"' && *(*argp + 1) == '"') {
		vlen = 0;
		ptr--;
		(*argp)++;
	}
	else {
		vlen = strlen(cp);
		*bufp = insertarg(*bufp, ptr, arg, *argp - arg + 1, vlen);
		Xstrncpy(&((*bufp)[ptr]), cp, vlen);
	}

	ptr += vlen;
	Xfree(new);

	return(ptr);
}

#ifndef	NOUID
VOID getlogininfo(homep, shellp)
CONST char **homep, **shellp;
{
	struct passwd *pwd;

	if (homep) *homep = NULL;
	if (shellp) *shellp = NULL;
# ifdef	DEBUG
	_mtrace_file = "getpwuid(start)";
	pwd = getpwuid(getuid());
	if (_mtrace_file) _mtrace_file = NULL;
	else {
		_mtrace_file = "getpwuid(end)";
		malloc(0);	/* dummy alloc */
	}
# else
	pwd = getpwuid(getuid());
# endif
	if (!pwd) return;
	if (homep && pwd -> pw_dir && *(pwd -> pw_dir)) *homep = pwd -> pw_dir;
	if (shellp && pwd -> pw_shell && *(pwd -> pw_shell))
		*shellp = pwd -> pw_shell;
}

uidtable *finduid(uid, name)
u_id_t uid;
CONST char *name;
{
	struct passwd *pwd;
	int i;

	if (name) {
		for (i = 0; i < maxuid; i++)
			if (!strpathcmp(name, uidlist[i].name))
				return(&(uidlist[i]));
# ifdef	DEBUG
		_mtrace_file = "getpwnam(start)";
		pwd = getpwnam(name);
		if (_mtrace_file) _mtrace_file = NULL;
		else {
			_mtrace_file = "getpwnam(end)";
			malloc(0);	/* dummy alloc */
		}
# else
		pwd = getpwnam(name);
# endif
	}
	else {
		for (i = 0; i < maxuid; i++)
			if (uid == uidlist[i].uid) return(&(uidlist[i]));
# ifdef	DEBUG
		_mtrace_file = "getpwuid(start)";
		pwd = getpwuid((uid_t)uid);
		if (_mtrace_file) _mtrace_file = NULL;
		else {
			_mtrace_file = "getpwuid(end)";
			malloc(0);	/* dummy alloc */
		}
# else
		pwd = getpwuid((uid_t)uid);
# endif
	}

	if (!pwd) return(NULL);
	uidlist = b_realloc(uidlist, maxuid, uidtable);
	uidlist[maxuid].uid = convuid(pwd -> pw_uid);
	uidlist[maxuid].name = Xstrdup(pwd -> pw_name);
	uidlist[maxuid].home = Xstrdup(pwd -> pw_dir);

	return(&(uidlist[maxuid++]));
}

gidtable *findgid(gid, name)
g_id_t gid;
CONST char *name;
{
	struct group *grp;
	int i;

	if (name) {
		for (i = 0; i < maxgid; i++)
			if (!strpathcmp(name, gidlist[i].name))
				return(&(gidlist[i]));
# ifdef	DEBUG
		_mtrace_file = "getgrnam(start)";
		grp = getgrnam(name);
		if (_mtrace_file) _mtrace_file = NULL;
		else {
			_mtrace_file = "getgrnam(end)";
			malloc(0);	/* dummy alloc */
		}
# else
		grp = getgrnam(name);
# endif
	}
	else {
		for (i = 0; i < maxgid; i++)
			if (gid == gidlist[i].gid) return(&(gidlist[i]));
# ifdef	DEBUG
		_mtrace_file = "getgrgid(start)";
		grp = getgrgid((gid_t)gid);
		if (_mtrace_file) _mtrace_file = NULL;
		else {
			_mtrace_file = "getgrgid(end)";
			malloc(0);	/* dummy alloc */
		}
# else
		grp = getgrgid((gid_t)gid);
# endif
	}

	if (!grp) return(NULL);
	gidlist = b_realloc(gidlist, maxgid, gidtable);
	gidlist[maxgid].gid = convgid(grp -> gr_gid);
	gidlist[maxgid].name = Xstrdup(grp -> gr_name);
# ifndef	USEGETGROUPS
	gidlist[maxgid].gr_mem = duplvar(grp -> gr_mem, -1);
# endif
	gidlist[maxgid].ismem = 0;

	return(&(gidlist[maxgid++]));
}

int isgroupmember(gid)
g_id_t gid;
{
# ifdef	USEGETGROUPS
	gid_t *gidset;
	int n;
# else
	uidtable *up;
# endif
	gidtable *gp;
	int i;

	if (!(gp = findgid(gid, NULL))) return(0);
	if (!(gp -> ismem)) {
		gp -> ismem++;
# ifdef	USEGETGROUPS
		if ((n = getgroups(0, NULL)) > 0) {
			gidset = (gid_t *)Xmalloc(n * sizeof(*gidset));
			n = getgroups(n, gidset);
			for (i = 0; i < n; i++) {
				if (gidset[i] != (gid_t)(gp -> gid)) continue;
				gp -> ismem++;
				break;
			}
			Xfree(gidset);
		}
# else	/* !USEGETGROUPS */
		if (gp -> gr_mem && (up = finduid(geteuid(), NULL)))
		for (i = 0; gp -> gr_mem[i]; i++) {
			if (!strpathcmp(up -> name, gp -> gr_mem[i])) {
				gp -> ismem++;
				break;
			}
		}
		freevar(gp -> gr_mem);
		gp -> gr_mem = NULL;
# endif	/* !USEGETGROUPS */
	}

	return(gp -> ismem - 1);
}

# ifdef	DEBUG
VOID freeidlist(VOID_A)
{
	int i;

	if (uidlist) {
		for (i = 0; i < maxuid; i++) {
			Xfree(uidlist[i].name);
			Xfree(uidlist[i].home);
		}
		Xfree(uidlist);
	}
	if (gidlist) {
		for (i = 0; i < maxgid; i++) {
			Xfree(gidlist[i].name);
#  ifndef	USEGETGROUPS
			freevar(gidlist[i].gr_mem);
#  endif
		}
		Xfree(gidlist);
	}
	uidlist = NULL;
	gidlist = NULL;
	maxuid = maxgid = 0;
}
# endif
#endif	/* !NOUID */

static char *NEAR replacebackquote(buf, ptrp, bbuf, rest, flags)
char *buf;
int *ptrp;
char *bbuf;
int rest, flags;
{
	char *tmp;
	int len, size;

	stripquote(bbuf, EA_BACKQ);
	if (!(tmp = (*backquotefunc)(bbuf))) return(buf);
	len = addmeta(NULL, tmp, flags);
	size = *ptrp + len + rest + 1;
	buf = Xrealloc(buf, size);
	addmeta(&(buf[*ptrp]), tmp, flags);
	*ptrp += len;
	Xfree(tmp);

	return(buf);
}

#ifndef	MINIMUMSHELL
CONST char *gethomedir(VOID_A)
{
# ifndef	NOUID
#  ifdef	FD
	uidtable *up;
#  endif
# endif	/* !NOUID */
	CONST char *cp;

	if (!(cp = getconstvar(ENVHOME))) {
# ifndef	NOUID
#  ifdef	FD
		if ((up = finduid(getuid(), NULL))) cp = up -> home;
#  else
		getlogininfo(&cp, NULL);
#  endif
# endif	/* !NOUID */
	}

	return(cp);
}
#endif	/* !MINIMUMSHELL */

CONST char *getrealpath(path, resolved, cwd)
CONST char *path;
char *resolved, *cwd;
{
#ifdef	DEP_PSEUDOPATH
	int drv;
#endif
	CONST char *err;
	char buf[MAXPATHLEN];

	if (cwd) /*EMPTY*/;
	else if (!Xgetwd(buf)) return(NULL);
	else cwd = buf;

#ifdef	DEP_PSEUDOPATH
	if ((drv = preparedrv(cwd, NULL, NULL)) < 0) return(cwd);
#endif
	if (_chdir2(path) < 0) err = path;
	else if (!Xgetwd(resolved)) err = NULL;
	else if (_chdir2(cwd) < 0) {
#ifdef	FD
		lostcwd(NULL);
		err = resolved;
#else
		err = cwd;
#endif
	}
	else err = resolved;
#ifdef	DEP_PSEUDOPATH
	shutdrv(drv);
#endif

	return(err);
}

#ifdef	BASHSTYLE
static VOID replaceifs(s, len)
char *s;
int len;
{
	CONST char *ifs;
	int i;

	if (!(ifs = getconstvar(ENVIFS))) return;
	for (i = 0; i < len; i++) {
		if (Xstrchr(ifs, s[i]) && !Xstrchr(IFS_SET, s[i])) s[i] = ' ';
		else if (iswchar(s, i)) i++;
	}
}
#endif	/* BASHSTYLE */

#ifndef	MINIMUMSHELL
static int NEAR evalhome(bufp, ptr, argp)
char **bufp;
int ptr;
CONST char **argp;
{
# ifndef	NOUID
#  ifdef	FD
	uidtable *up;
#  else
	struct passwd *pwd;
#  endif
	char *tmp;
# endif	/* !NOUID */
	CONST char *cp, *top;
	int len, vlen;

	top = &((*argp)[1]);

	len = ((cp = strdelim(top, 0))) ? (cp - top) : strlen(top);
	if (!len) cp = gethomedir();
# ifdef	FD
	else if (len == strsize(FDSTR) && !strnpathcmp(top, FDSTR, len))
		cp = progpath;
# endif
	else {
# ifdef	NOUID
		cp = NULL;
# else	/* !NOUID */
		tmp = Xstrndup(top, len);
#  ifdef	FD
		up = finduid(0, tmp);
		cp = (up) ? up -> home : NULL;
#  else	/* !FD */
#   ifdef	DEBUG
		_mtrace_file = "getpwnam(start)";
		pwd = getpwnam(tmp);
		if (_mtrace_file) _mtrace_file = NULL;
		else {
			_mtrace_file = "getpwnam(end)";
			malloc(0);	/* dummy alloc */
		}
#   else
		pwd = getpwnam(tmp);
#   endif
		cp = (pwd) ? pwd -> pw_dir : NULL;
#  endif	/* !FD */
		Xfree(tmp);
# endif	/* !NOUID */
	}

	if (!cp) {
		vlen = len = 0;
		(*bufp)[ptr++] = '~';
	}
	else {
		top = cp;
		vlen = strlen(top);
		*bufp = insertarg(*bufp, ptr, *argp, len + 1, vlen);
	}
	Xstrncpy(&((*bufp)[ptr]), top, vlen);
	ptr += vlen;
	*argp += len;

	return(ptr);
}
#endif	/* !MINIMUMSHELL */

char *evalarg(arg, flags)
char *arg;
int flags;
{
#ifdef	DEP_DOSLFN
	char path[MAXPATHLEN], alias[MAXPATHLEN];
#endif
#ifndef	MINIMUMSHELL
	int q2, prev;
#endif
#ifdef	NESTINGQUOTE
	int pq;
#endif
#ifdef	BASHSTYLE
	int top;
#endif
	CONST char *cp;
	char *buf, *bbuf;
	int i, j, pc, q, f;

#ifdef	DEP_DOSLFN
	if (*arg == '"' && (i = strlen(arg)) > 2 && arg[i - 1] == '"') {
		Xstrncpy(path, &(arg[1]), i - 2);
		if (shortname(path, alias) == alias) {
			if (flags & (EA_STRIPQ | EA_STRIPQLATER))
				return(Xstrdup(alias));
			i = strlen(alias);
			buf = Xmalloc(i + 2 + 1);
			buf[0] = '"';
			memcpy(&(buf[1]), alias, i);
			buf[++i] = '"';
			buf[++i] = '\0';
			return(buf);
		}
	}
#endif

	i = strlen(arg) + 1;
	buf = Xmalloc(i);
	if (!backquotefunc) flags &= ~EA_BACKQ;
	bbuf = (flags & EA_BACKQ) ? Xmalloc(i) : NULL;
#ifndef	MINIMUMSHELL
	q2 = prev =
#endif
#ifdef	NESTINGQUOTE
	pq =
#endif
	q = '\0';
	i = j = 0;
	cp = arg;

	while (*cp) {
#ifdef	BASHSTYLE
		top = i;
#endif
#ifdef	NESTINGQUOTE
		pc = parsechar(cp, -1, '$', flags, &q, &pq);
#else
		pc = parsechar(cp, -1, '$', flags, &q, NULL);
#endif
		if (pc == PC_CLQUOTE) {
			if (*cp == '`') {
				bbuf[j] = '\0';
				buf = replacebackquote(buf, &i,
					bbuf, strlen(&(cp[1])), flags);
#ifdef	BASHSTYLE
	/* bash replaces the IFS character to a space */
				if (flags & EA_EVALIFS)
					replaceifs(&(buf[top]), i - top);
#endif
				j = 0;
			}
			else if (!(flags & EA_STRIPQ)) buf[i++] = *cp;
		}
		else if (pc == PC_WCHAR) {
			if (q == '`') {
				bbuf[j++] = *cp++;
				bbuf[j++] = *cp;
			}
			else {
				buf[i++] = *cp++;
				buf[i++] = *cp;
			}
		}
		else if (pc == PC_BQUOTE) {
			bbuf[j++] = *cp;
#ifndef	MINIMUMSHELL
			parsechar(cp, -1, '\0', flags, &q2, NULL);
#endif
		}
		else if (pc == PC_SQUOTE || pc == PC_DQUOTE) buf[i++] = *cp;
		else if (pc == '$') {
			f = flags;
			if (q == '"') f |= EA_INQUOTE;
			if (!cp[1]) buf[i++] = *cp;
#ifdef	FAKEESCAPE
# ifdef	MINIMUMSHELL
			else if (cp[1] == '$') cp++;
# else
			else if (cp[1] == '$' || cp[1] == '~') cp++;
# endif
#endif	/* FAKEESCAPE */
			else if ((i = evalvar(&buf, i, &cp, f)) < 0) {
				Xfree(bbuf);
				Xfree(buf);
				if (i < -1) *arg = '\0';
				return(NULL);
			}
#ifdef	BASHSTYLE
	/* bash replaces the IFS character to a space */
			else if (flags & EA_EVALIFS)
				replaceifs(&(buf[top]), i - top);
#endif
		}
		else if (pc == PC_ESCAPE) {
			cp++;
			if (flags & EA_KEEPESCAPE) pc = PC_NORMAL;
			else if (*cp == '$') /*EMPTY*/;
			else if ((flags & EA_BACKQ) && *cp == '`') /*EMPTY*/;
			else if ((flags & EA_STRIPQ)
			&& (*cp == '\'' || *cp == '"'))
				/*EMPTY*/;
			else if ((flags & EA_STRIPESCAPE) && *cp == PESCAPE)
				/*EMPTY*/;
			else pc = PC_NORMAL;

			if (q == '`') {
				if (*cp == '$') /*EMPTY*/;
#ifndef	MINIMUMSHELL
				else if (q2 == '\'' && *cp == PESCAPE)
					/*EMPTY*/;
#endif
				else bbuf[j++] = PESCAPE;
				bbuf[j++] = *cp;
			}
			else {
				if (pc != PC_ESCAPE) buf[i++] = PESCAPE;
				buf[i++] = *cp;
			}
		}
		else if (pc == PC_OPQUOTE) {
			if (*cp == '`') {
				j = 0;
#ifndef	MINIMUMSHELL
				q2 = '\0';
#endif
			}
			else if (!(flags & EA_STRIPQ)) buf[i++] = *cp;
		}
		else if (pc != PC_NORMAL) /*EMPTY*/;
#ifndef	MINIMUMSHELL
		else if (*cp == '~' && (!prev || prev == ':' || prev == '='))
			i = evalhome(&buf, i, &cp);
#endif
		else buf[i++] = *cp;

#ifdef	MINIMUMSHELL
		cp++;
#else
		prev = *(cp++);
#endif
	}
#ifndef	BASHSTYLE
	/* bash does not allow unclosed quote */
	if ((flags & EA_BACKQ) && q == '`') {
		bbuf[j] = '\0';
		buf = replacebackquote(buf, &i, bbuf, 0, flags);
	}
#endif
	Xfree(bbuf);
	buf[i] = '\0';

	if (flags & EA_STRIPQLATER) stripquote(buf, EA_STRIPQ);

	return(buf);
}

int evalifs(argc, argvp, ifs)
int argc;
char ***argvp;
CONST char *ifs;
{
	char *cp;
	int i, j, n, pc, quote;

	for (n = 0; n < argc; n++) {
		for (i = 0, quote = '\0'; (*argvp)[n][i]; i++) {
			pc = parsechar(&((*argvp)[n][i]), -1,
				'\0', 0, &quote, NULL);
			if (pc == PC_WCHAR || pc == PC_ESCAPE) i++;
			else if (pc != PC_NORMAL) /*EMPTY*/;
			else if (Xstrchr(ifs, (*argvp)[n][i])) {
				for (j = i + 1; (*argvp)[n][j]; j++)
					if (!Xstrchr(ifs, (*argvp)[n][j]))
						break;
				if (!i) {
					for (i = 0; (*argvp)[n][i + j]; i++)
						(*argvp)[n][i] =
							(*argvp)[n][i + j];
					(*argvp)[n][i] = '\0';
					i = -1;
					continue;
				}
				(*argvp)[n][i] = '\0';
				if (!(*argvp)[n][j]) break;
				cp = Xstrdup(&((*argvp)[n][j]));
				*argvp = (char **)Xrealloc(*argvp,
					(argc + 2) * sizeof(char *));
				memmove((char *)(&((*argvp)[n + 2])),
					(char *)(&((*argvp)[n + 1])),
					(argc++ - n) * sizeof(char *));
				(*argvp)[n + 1] = cp;
				break;
			}
		}
		if (!i) {
			Xfree((*argvp)[n--]);
			memmove((char *)(&((*argvp)[n + 1])),
				(char *)(&((*argvp)[n + 2])),
				(argc-- - n) * sizeof(char *));
		}
	}

	return(argc);
}

int evalglob(argc, argvp, flags)
int argc;
char ***argvp;
int flags;
{
	char **wild;
	int i, n;

	for (n = 0; n < argc; n++) {
		if (!(wild = evalwild((*argvp)[n], flags))) {
			stripquote((*argvp)[n], flags);
			continue;
		}

		i = countvar(wild);
		if (i > 1) {
			*argvp = (char **)Xrealloc(*argvp,
				(argc + i) * sizeof(char *));
			memmove((char *)(&((*argvp)[n + i])),
				(char *)(&((*argvp)[n + 1])),
				(argc - n) * sizeof(char *));
			argc += i - 1;
		}
		Xfree((*argvp)[n]);
		memmove((char *)(&((*argvp)[n])),
			(char *)wild, i * sizeof(char *));
		Xfree(wild);
		n += i - 1;
	}

	return(argc);
}

int stripquote(arg, flags)
char *arg;
int flags;
{
	int i, j, pc, quote, stripped;

	stripped = 0;
	if (!arg) return(stripped);
	for (i = j = 0, quote = '\0'; arg[i]; i++) {
		pc = parsechar(&(arg[i]), -1, '\0', 0, &quote, NULL);
		if (pc == PC_OPQUOTE || pc == PC_CLQUOTE) {
			stripped++;
			if (flags & EA_STRIPQ) continue;
		}
		else if (pc == PC_WCHAR) arg[j++] = arg[i++];
		else if (pc == PC_ESCAPE) {
			i++;
			if (flags & EA_KEEPESCAPE) pc = PC_NORMAL;
			else if (!quote && !(flags & EA_BACKQ)) /*EMPTY*/;
			else if (!Xstrchr(DQ_METACHAR, arg[i])) pc = PC_NORMAL;

			if (pc != PC_ESCAPE) arg[j++] = PESCAPE;
			else stripped++;
		}

		arg[j++] = arg[i];
	}
	arg[j] = '\0';

	return(stripped);
}

char *_evalpath(path, eol, flags)
CONST char *path, *eol;
int flags;
{
#ifdef	DEP_DOSLFN
	char alias[MAXPATHLEN];
	int top, len;
#endif
#ifdef	DOUBLESLASH
	int ds;
#endif
#ifdef	DEP_URLPATH
	int url;
#endif
	char *cp, *tmp;
	int i, j, c, pc, size, quote;

	if (eol) i = eol - path;
	else i = strlen(path);
	cp = Xstrndup(path, i);

	tmp = evalarg(cp, EA_NOEVALQ | EA_NOEVALDQ | EA_KEEPESCAPE);
	if (!tmp) {
		*cp = '\0';
		return(cp);
	}
	Xfree(cp);
	cp = tmp;

#ifdef	DEP_DOSLFN
	top = -1;
#endif
#ifdef	DOUBLESLASH
	ds = isdslash(cp);
#endif
#ifdef	DEP_URLPATH
	url = isurl(cp, 0);
#endif
	size = strlen(cp) + 1;
	tmp = Xmalloc(size);
	quote = '\0';

	for (i = j = c = 0; cp[i]; c = cp[i++]) {
		pc = parsechar(&(cp[i]), -1, '\0', 0, &quote, NULL);
		if (pc == PC_CLQUOTE) {
#ifdef	DEP_DOSLFN
			if ((flags & EA_NOEVALQ) && top >= 0 && ++top < j) {
				tmp[j] = '\0';
				if (shortname(&(tmp[top]), alias) == alias) {
					len = strlen(alias);
					size += top + len - j;
					j = top + len;
					tmp = Xrealloc(tmp, size);
					Xstrncpy(&(tmp[top]), alias, len);
				}
			}
			top = -1;
#endif
			if (!(flags & EA_NOEVALQ)) continue;
		}
		else if (pc == PC_WCHAR) tmp[j++] = cp[i++];
		else if (pc == PC_ESCAPE) {
			i++;
			if ((flags & EA_KEEPESCAPE)
			|| (quote && !Xstrchr(DQ_METACHAR, cp[i])))
				tmp[j++] = PESCAPE;
		}
		else if (pc == PC_OPQUOTE) {
#ifdef	DEP_DOSLFN
			if (cp[i] == '"') top = j;
#endif
			if (!(flags & EA_NOEVALQ)) continue;
		}
		else if (pc != PC_NORMAL) /*EMPTY*/;
		else if (flags & EA_NOUNIQDELIM) /*EMPTY*/;
#ifdef	DOUBLESLASH
		else if (ds && i == 1) /*EMPTY*/;
#endif
#ifdef	DEP_URLPATH
		else if (url && i < url) /*EMPTY*/;
#endif
		else if (cp[i] == _SC_ && c == _SC_) continue;

		tmp[j++] = cp[i];
	}
	tmp[j] = '\0';
	Xfree(cp);

	return(tmp);
}

char *evalpath(path, flags)
char *path;
int flags;
{
	char *cp;

	if (!path || !*path) return(path);
	for (cp = path; Xisblank(*cp); cp++) /*EMPTY*/;
	cp = _evalpath(cp, NULL, flags);
	Xfree(path);

	return(cp);
}