about summary refs log tree commit diff
path: root/Src/glob.c
blob: 0d6d4d778542c869717c8154c7a298c91d5cbd86 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
/*
 * glob.c - filename generation
 *
 * This file is part of zsh, the Z shell.
 *
 * Copyright (c) 1992-1997 Paul Falstad
 * All rights reserved.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and to distribute modified versions of this software for any
 * purpose, provided that the above copyright notice and the following
 * two paragraphs appear in all copies of this software.
 *
 * In no event shall Paul Falstad or the Zsh Development Group be liable
 * to any party for direct, indirect, special, incidental, or consequential
 * damages arising out of the use of this software and its documentation,
 * even if Paul Falstad and the Zsh Development Group have been advised of
 * the possibility of such damage.
 *
 * Paul Falstad and the Zsh Development Group specifically disclaim any
 * warranties, including, but not limited to, the implied warranties of
 * merchantability and fitness for a particular purpose.  The software
 * provided hereunder is on an "as is" basis, and Paul Falstad and the
 * Zsh Development Group have no obligation to provide maintenance,
 * support, updates, enhancements, or modifications.
 *
 */

#include "zsh.mdh"
#include "glob.pro"

/* flag for CSHNULLGLOB */

typedef struct gmatch *Gmatch; 

struct gmatch {
    char *name;
    long size;
    long atime;
    long mtime;
    long ctime;
    long links;
    long _size;
    long _atime;
    long _mtime;
    long _ctime;
    long _links;
};

#define GS_NAME   1
#define GS_SIZE   2
#define GS_ATIME  4
#define GS_MTIME  8
#define GS_CTIME 16
#define GS_LINKS 32

#define GS_SHIFT  5
#define GS__SIZE  (GS_SIZE << GS_SHIFT)
#define GS__ATIME (GS_ATIME << GS_SHIFT)
#define GS__MTIME (GS_MTIME << GS_SHIFT)
#define GS__CTIME (GS_CTIME << GS_SHIFT)
#define GS__LINKS (GS_LINKS << GS_SHIFT)

#define GS_DESC  2048

#define GS_NORMAL (GS_SIZE | GS_ATIME | GS_MTIME | GS_CTIME | GS_LINKS)
#define GS_LINKED (GS_NORMAL << GS_SHIFT)

/**/
int badcshglob;
 
static int mode;		/* != 0 if we are parsing glob patterns */
static int scanning;		/* != 0 if we are scanning file paths   */
static int pathpos;		/* position in pathbuf                  */
static int matchsz;		/* size of matchbuf                     */
static int matchct;		/* number of matches found              */
static char *pathbuf;		/* pathname buffer                      */
static int pathbufsz;		/* size of pathbuf			*/
static int pathbufcwd;		/* where did we chdir()'ed		*/
static Gmatch matchbuf;		/* array of matches                     */
static Gmatch matchptr;		/* &matchbuf[matchct]                   */
static char *colonmod;		/* colon modifiers in qualifier list    */

typedef struct stat *Statptr;	 /* This makes the Ultrix compiler happy.  Go figure. */

/* modifier for unit conversions */

#define TT_DAYS 0
#define TT_HOURS 1
#define TT_MINS 2
#define TT_WEEKS 3
#define TT_MONTHS 4

#define TT_BYTES 0
#define TT_POSIX_BLOCKS 1
#define TT_KILOBYTES 2
#define TT_MEGABYTES 3

typedef int (*TestMatchFunc) _((struct stat *, long));

struct qual {
    struct qual *next;		/* Next qualifier, must match                */
    struct qual *or;		/* Alternative set of qualifiers to match    */
    TestMatchFunc func;		/* Function to call to test match            */
    long data;			/* Argument passed to function               */
    int sense;			/* Whether asserting or negating             */
    int amc;			/* Flag for which time to test (a, m, c)     */
    int range;			/* Whether to test <, > or = (as per signum) */
    int units;			/* Multiplier for time or size, respectively */
};

/* Qualifiers pertaining to current pattern */
static struct qual *quals;

/* Other state values for current pattern */
static int qualct, qualorct;
static int range, amc, units;
static int gf_nullglob, gf_markdirs, gf_noglobdots, gf_listtypes, gf_follow;
static int gf_sorts, gf_nsorts, gf_sortlist[11];

/* Prefix, suffix for doing zle trickery */

/**/
char *glob_pre, *glob_suf;

/* pathname component in filename patterns */

struct complist {
    Complist next;
    Comp comp;
    int closure;		/* 1 if this is a (foo/)# */
    int follow; 		/* 1 to go thru symlinks */
};
struct comp {
    Comp left, right, next, exclude;
    char *str;
    int stat, errsmax;
};

/* Type of Comp:  a closure with one or two #'s, the end of a *
 * pattern or path component, a piece of path to be added.    */
#define C_ONEHASH	1
#define C_TWOHASH	2
#define C_OPTIONAL	4
#define C_STAR		8    
#define C_CLOSURE	(C_ONEHASH|C_TWOHASH|C_OPTIONAL|C_STAR)
#define C_LAST		16
#define C_PATHADD	32
#define C_LCMATCHUC	64
#define C_IGNCASE	128

/* Test macros for the above */
#define CLOSUREP(c)	(c->stat & C_CLOSURE)
#define ONEHASHP(c)	(c->stat & (C_ONEHASH|C_STAR))
#define TWOHASHP(c)	(c->stat & C_TWOHASH)
#define OPTIONALP(c)	(c->stat & C_OPTIONAL)
#define STARP(c)	(c->stat & C_STAR)
#define LASTP(c)	(c->stat & C_LAST)
#define PATHADDP(c)	(c->stat & C_PATHADD)

/* Flags passed down to guts when compiling */
#define GF_PATHADD	1	/* file glob, adding path components */
#define GF_TOPLEV	2	/* outside (), so ~ ends main match */

/* Next character after one which may be a Meta (x is any char *) */
#define METANEXT(x)	(*(x) == Meta ? (x)+2 : (x)+1)
/*
 * Increment pointer which may be on a Meta (x is a pointer variable),
 * returning the incremented value (i.e. like pre-increment).
 */
#define METAINC(x)	((x) += (*(x) == Meta) ? 2 : 1)
/*
 * Return unmetafied char from string (x is any char *)
 */
#define UNMETA(x)	(*(x) == Meta ? (x)[1] ^ 32 : *(x))

static char *pptr;		/* current place in string being matched */
static Comp tail;
static int first;		/* are leading dots special? */
static int longest;		/* always match longest piece of path. */
static int inclosure;		/* see comment in doesmatch() */

/* Add a component to pathbuf: This keeps track of how    *
 * far we are into a file name, since each path component *
 * must be matched separately.                            */

/**/
static void
addpath(char *s)
{
    DPUTS(!pathbuf, "BUG: pathbuf not initialised");
    while (pathpos + (int) strlen(s) + 1 >= pathbufsz)
	pathbuf = realloc(pathbuf, pathbufsz *= 2);
    while ((pathbuf[pathpos++] = *s++));
    pathbuf[pathpos - 1] = '/';
    pathbuf[pathpos] = '\0';
}

/* stat the filename s appended to pathbuf.  l should be true for lstat,    *
 * false for stat.  If st is NULL, the file is only chechked for existance. *
 * s == "" is treated as s == ".".  This is necessary since on most systems *
 * foo/ can be used to reference a non-directory foo.  Returns nonzero if   *
 * the file does not exists.                                                */

/**/
static int
statfullpath(const char *s, struct stat *st, int l)
{
    char buf[PATH_MAX];

    DPUTS(strlen(s) + !*s + pathpos - pathbufcwd >= PATH_MAX,
	  "BUG: statfullpath(): pathname too long");
    strcpy(buf, pathbuf + pathbufcwd);
    strcpy(buf + pathpos - pathbufcwd, s);
    if (!*s) {
	buf[pathpos - pathbufcwd] = '.';
	buf[pathpos - pathbufcwd + 1] = '\0';
	l = 0;
    }
    unmetafy(buf, NULL);
    if (!st)
	return access(buf, F_OK) && (!l || readlink(buf, NULL, 0));
    return l ? lstat(buf, st) : stat(buf, st);
}

/* add a match to the list */

/**/
static void
insert(char *s, int checked)
{
    struct stat buf, buf2, *bp;
    char *news = s;
    int statted = 0;

    if (gf_listtypes || gf_markdirs) {
	/* Add the type marker to the end of the filename */
	mode_t mode;
	checked = statted = 1;
	if (statfullpath(s, &buf, 1))
	    return;
	mode = buf.st_mode;
	if (gf_follow) {
	    if (!S_ISLNK(mode) || statfullpath(s, &buf2, 0))
		memcpy(&buf2, &buf, sizeof(buf));
	    statted = 2;
	    mode = buf2.st_mode;
	}
	if (gf_listtypes || S_ISDIR(mode)) {
	    int ll = strlen(s);

	    news = (char *)ncalloc(ll + 2);
	    strcpy(news, s);
	    news[ll] = file_type(mode);
	    news[ll + 1] = '\0';
	}
    }
    if (qualct || qualorct) {
	/* Go through the qualifiers, rejecting the file if appropriate */
	struct qual *qo, *qn;

	if (!statted && statfullpath(s, &buf, 1))
	    return;
	statted = 1;
	qo = quals;
	for (qn = qo; qn && qn->func;) {
	    range = qn->range;
	    amc = qn->amc;
	    units = qn->units;
	    if ((qn->sense & 2) && statted != 2) {
		/* If (sense & 2), we're following links */
		if (!S_ISLNK(buf.st_mode) || statfullpath(s, &buf2, 0))
		    memcpy(&buf2, &buf, sizeof(buf));
		statted = 2;
	    }
	    bp = (qn->sense & 2) ? &buf2 : &buf;
	    /* Reject the file if the function returned zero *
	     * and the sense was positive (sense&1 == 0), or *
	     * vice versa.                                   */
	    if ((!((qn->func) (bp, qn->data)) ^ qn->sense) & 1) {
		/* Try next alternative, or return if there are no more */
		if (!(qo = qo->or))
		    return;
		qn = qo;
		continue;
	    }
	    qn = qn->next;
	}
    } else if (!checked) {
	if (statfullpath(s, NULL, 1))
	    return;
	statted = 1;
    }
    news = dyncat(pathbuf, news);
    if (colonmod) {
	/* Handle the remainder of the qualifer:  e.g. (:r:s/foo/bar/). */
	s = colonmod;
	modify(&news, &s);
    }
    if (!statted && (gf_sorts & GS_NORMAL)) {
	statfullpath(s, &buf, 1);
	statted = 1;
    }
    if (statted != 2 && (gf_sorts & GS_LINKED)) {
	if (statted) {
	    if (!S_ISLNK(buf.st_mode) || statfullpath(s, &buf2, 0))
		memcpy(&buf2, &buf, sizeof(buf));
	} else if (statfullpath(s, &buf2, 0))
	    statfullpath(s, &buf2, 1);
    }
    matchptr->name = news;
    matchptr->size = buf.st_size;
    matchptr->atime = buf.st_atime;
    matchptr->mtime = buf.st_mtime;
    matchptr->ctime = buf.st_ctime;
    matchptr->links = buf.st_nlink;
    matchptr->_size = buf2.st_size;
    matchptr->_atime = buf2.st_atime;
    matchptr->_mtime = buf2.st_mtime;
    matchptr->_ctime = buf2.st_ctime;
    matchptr->_links = buf2.st_nlink;
    matchptr++;

    if (++matchct == matchsz) {
	matchbuf = (Gmatch )realloc((char *)matchbuf,
				    sizeof(struct gmatch) * (matchsz *= 2));

	matchptr = matchbuf + matchct;
    }
}

/* Check to see if str is eligible for filename generation. */

/**/
int
haswilds(char *str)
{
    /* `[' and `]' are legal even if bad patterns are usually not. */
    if ((*str == Inbrack || *str == Outbrack) && !str[1])
	return 0;

    /* If % is immediately followed by ?, then that ? is     *
     * not treated as a wildcard.  This is so you don't have *
     * to escape job references such as %?foo.               */
    if (str[0] == '%' && str[1] == Quest)
	str[1] = '?';

    for (; *str; str++) {
	switch (*str) {
	    case Inpar:
	    case Bar:
	    case Star:
	    case Inbrack:
	    case Inang:
	    case Quest:
		return 1;
	    case Pound:
	    case Hat:
		if (isset(EXTENDEDGLOB))
		    return 1;
		break;
	}
    }
    return 0;
}

/* Flags to apply to current level of grouping */

static int addflags;

/*
 * Approximate matching.
 *
 * errsmax is used while parsing to store the current number
 * of errors allowed.  While executing it is usually set to -1,
 * but can be set to something else to fix a different maximum
 * no. of errors which acts as a limit on the local value:  see
 * scanner() for why this is necessary.
 *
 * errsfound is used while executing a pattern to record the
 * number of errors found so far.
 */

static int errsmax, errsfound;

/* Do the globbing:  scanner is called recursively *
 * with successive bits of the path until we've    *
 * tried all of it.                                */

/**/
static void
scanner(Complist q)
{
    Comp c;
    int closure;
    int pbcwdsav = pathbufcwd;
    int errssofar = errsfound;
    struct dirsav ds;
    char *str;

    ds.ino = ds.dev = 0;
    ds.dirname = NULL;
    ds.dirfd = ds.level = -1;
    if (!q)
	return;

    if ((closure = q->closure)) {
	/* (foo/)# - match zero or more dirs */
	if (q->closure == 2)	/* (foo/)## - match one or more dirs */
	    q->closure = 1;
	else
	    scanner(q->next);
    }
    c = q->comp;
    str = c->str;
    /* Now the actual matching for the current path section. */
    if (!(c->next || c->left) && !haswilds(str)
	&& (!((c->stat & (C_LCMATCHUC|C_IGNCASE)) || c->errsmax)
	    || !*str || !strcmp(".", str) || !strcmp("..", str))) {
	/*
	 * We always need to match . and .. explicitly, even if we're
	 * checking other strings for case-insensitive matches.
	 *
	 * It's a straight string to the end of the path section.
	 */
	int l = strlen(str);

	if (l + !l + pathpos - pathbufcwd >= PATH_MAX) {
	    int err;

	    if (l >= PATH_MAX)
		return;
	    err = lchdir(pathbuf + pathbufcwd, &ds, 0);
	    if (err == -1)
		return;
	    if (err) {
		zerr("current directory lost during glob", NULL, 0);
		return;
	    }
	    pathbufcwd = pathpos;
	}
	if (q->next) {
	    /* Not the last path section. Just add it to the path. */
	    int oppos = pathpos;

	    if (!errflag && !(q->closure && !strcmp(str, "."))) {
		addpath(str);
		if (!closure || statfullpath("", NULL, 1))
		    scanner((q->closure) ? q : q->next);
		pathbuf[pathpos = oppos] = '\0';
	    }
	} else
	    insert(str, 0);
    } else {
	/* Do pattern matching on current path section. */
	char *fn = pathbuf[pathbufcwd] ? unmeta(pathbuf + pathbufcwd) : ".";
	int dirs = !!q->next;
	DIR *lock = opendir(fn);
	char *subdirs = NULL;
	int subdirlen = 0;

	if (lock == NULL)
	    return;
	while ((fn = zreaddir(lock, 1)) && !errflag) {
	    /* prefix and suffix are zle trickery */
	    if (!dirs && !colonmod &&
		((glob_pre && !strpfx(glob_pre, fn))
		 || (glob_suf && !strsfx(glob_suf, fn))))
		continue;
	    errsfound = errssofar;
	    if (domatch(fn, c, gf_noglobdots)) {
		/* if this name matchs the pattern... */
		if (pbcwdsav == pathbufcwd &&
		    strlen(fn) + pathpos - pathbufcwd >= PATH_MAX) {
		    int err;

		    DPUTS(pathpos == pathbufcwd,
			  "BUG: filename longer than PATH_MAX");
		    err = lchdir(pathbuf + pathbufcwd, &ds, 0);
		    if (err == -1)
			break;
		    if (err) {
			zerr("current directory lost during glob", NULL, 0);
			break;
		    }
		    pathbufcwd = pathpos;
		}
		if (dirs) {
		    int l;

		    /*
		     * If not the last component in the path:
		     *
		     * If we made an approximation in the new path segment,
		     * and the pattern includes closures other than a
		     * trailing * (we only test if it is not a string),
		     * then it is possible we made too many errors.  For
		     * example, (ab)#(cb)# will match the directory abcb
		     * with one error if allowed to, even though it can
		     * match with none.  This will stop later parts of the
		     * path matching, so we need to check by reducing the
		     * maximum number of errors and seeing if the directory
		     * still matches.  Luckily, this is not a terribly
		     * common case, since complex patterns typically occur
		     * in the last part of the path which is not affected
		     * by this problem.
		     */
		    if (errsfound > errssofar && (c->next || c->left)) {
			errsmax = errsfound - 1;
			while (errsmax >= errssofar) {
			    errsfound = errssofar;
			    if (!domatch(fn, c, gf_noglobdots))
				break;
			    errsmax = errsfound - 1;
			}
			errsfound = errsmax + 1;
			errsmax = -1;
		    }
		    if (closure) {
			/* if matching multiple directories */
			struct stat buf;

			if (statfullpath(fn, &buf, !q->follow)) {
			    if (errno != ENOENT && errno != EINTR &&
				errno != ENOTDIR && !errflag) {
				zerr("%e: %s", fn, errno);
				errflag = 0;
			    }
			    continue;
			}
			if (!S_ISDIR(buf.st_mode))
			    continue;
		    }
		    l = strlen(fn) + 1;
		    subdirs = hrealloc(subdirs, subdirlen, subdirlen + l
				       + sizeof(int));
		    strcpy(subdirs + subdirlen, fn);
		    subdirlen += l;
		    /* store the count of errors made so far, too */
		    memcpy(subdirs + subdirlen, (char *)&errsfound,
			   sizeof(int));
		    subdirlen += sizeof(int);
		} else
		    /* if the last filename component, just add it */
		    insert(fn, 1);
	    }
	}
	closedir(lock);
	if (subdirs) {
	    int oppos = pathpos;

	    for (fn = subdirs; fn < subdirs+subdirlen; ) {
		addpath(fn);
		fn += strlen(fn) + 1;
		memcpy((char *)&errsfound, fn, sizeof(int));
		fn += sizeof(int);
		scanner((q->closure) ? q : q->next);  /* scan next level */
		pathbuf[pathpos = oppos] = '\0';
	    }
	    hrealloc(subdirs, subdirlen, 0);
	}
    }
    if (pbcwdsav < pathbufcwd) {
	if (restoredir(&ds))
	    zerr("current directory lost during glob", NULL, 0);
	zsfree(ds.dirname);
	if (ds.dirfd >= 0)
	    close(ds.dirfd);
	pathbufcwd = pbcwdsav;
    }
}

/* Parse a series of path components pointed to by pptr */

/**/
static Comp
compalloc(void)
{
    Comp c = (Comp) alloc(sizeof *c);
    c->stat |= addflags;
    c->errsmax = errsmax;
    return c;
}

/**/
static int
getglobflags(void)
{
    char *nptr;
    /* (#X): assumes we are still positioned on the initial '(' */
    pptr++;
    while (*++pptr && *pptr != Outpar) {
	switch (*pptr) {
	case 'a':
	    /* Approximate matching, max no. of errors follows */
	    errsmax = zstrtol(++pptr, &nptr, 10);
	    if (pptr == nptr)
		return 1;
	    pptr = nptr-1;
	    break;

	case 'l':
	    /* Lowercase in pattern matches lower or upper in target */
	    addflags |= C_LCMATCHUC;
	    break;

	case 'i':
	    /* Fully case insensitive */
	    addflags |= C_IGNCASE;
	    break;

	case 'I':
	    /* Restore case sensitivity */
	    addflags &= ~(C_LCMATCHUC|C_IGNCASE);
	    break;

	default:
	    return 1;
	}
    }
    if (*pptr != Outpar)
	return 1;
    pptr++;
    return 0;
}

/* enum used with ksh-like patterns, @(...) etc. */

enum { KF_NONE, KF_AT, KF_QUEST, KF_STAR, KF_PLUS, KF_NOT };

/* parse lowest level pattern */

/**/
static Comp
parsecomp(int gflag)
{
    int kshfunc;
    Comp c = compalloc(), c1, c2;
    char *cstr, *ls = NULL;

    /* In case of alternatives, code coming up is stored in tail. */
    c->next = tail;
    cstr = pptr;

    while (*pptr && (mode || *pptr != '/') && *pptr != Bar &&
	   (unset(EXTENDEDGLOB) || *pptr != Tilde ||
	    !pptr[1] || pptr[1] == Outpar || pptr[1] == Bar) &&
	   *pptr != Outpar) {
	/* Go through code until we find something separating alternatives,
	 * or path components if relevant.
	 */
	if (*pptr == Hat && isset(EXTENDEDGLOB)) {
	    /* negate remaining pattern */
	    Comp stail = tail;
	    tail = NULL;
	    c->str = dupstrpfx(cstr, pptr - cstr);
	    pptr++;

	    c1 = compalloc();
	    c1->stat |= C_STAR;

	    c2 = compalloc();
	    if (!(c2->exclude = parsecomp(gflag)))
		return NULL;
	    if (!*pptr || *pptr == '/')
		c2->stat |= C_LAST;
	    c2->left = c1;
	    c2->next = stail;
	    c->next = c2;
	    tail = stail;
	    return c;
	}

	/* Ksh-type globs */
	kshfunc = KF_NONE;
	if (isset(KSHGLOB) && *pptr && pptr[1] == Inpar) {
	    switch (*pptr) {
	    case '@':		/* just do paren as usual */
		kshfunc = KF_AT;
		break;

	    case Quest:
	    case '?':		/* matched optionally, treat as (...|) */
		kshfunc = KF_QUEST;
		break;

	    case Star:
	    case '*':		/* treat as (...)# */
		kshfunc = KF_STAR;
		break;

	    case '+':		/* treat as (...)## */
		kshfunc = KF_PLUS;
		break;

	    case '!':		/* treat as (*~...) */
		kshfunc = KF_NOT;
		break;
	    }
	    if (kshfunc != KF_NONE)
		pptr++;
	}

	if (*pptr == Inpar && pptr[1] == Pound) {
	    /* Found some globbing flags */
	    char *eptr = pptr;
	    if (kshfunc != KF_NONE)
		eptr--;
	    if (getglobflags())
		return NULL;
	    if (eptr == cstr) {
		/* if no string yet, carry on and get one. */
		c->stat = addflags;
		c->errsmax = errsmax;
		cstr = pptr;
		continue;
	    }
	    c->str = dupstrpfx(cstr, eptr - cstr);
	    /*
	     * The next bit simply handles the case where . or ..
	     * is followed by a set of flags, but we need to force
	     * them to be handled as a string.  Hardly worth it.
	     */
	    if (!*pptr || (!mode && *pptr == '/') || *pptr == Bar ||
		(isset(EXTENDEDGLOB) && *pptr == Tilde &&
		 pptr[1] && pptr[1] != Outpar && pptr[1] != Bar) ||
		*pptr == Outpar) {
		if (*pptr == '/' || !*pptr ||
		    ((*pptr == Bar ||
		      (isset(EXTENDEDGLOB) && *pptr == Tilde)) &&
		     (gflag & GF_TOPLEV)))
		    c->stat |= C_LAST;
		return c;
	    }
	    if (!(c->next = parsecomp(gflag)))
		return NULL;
	    return c;
	}
	if (*pptr == Inpar) {
	    /* Found a group (...) */
	    char *startp = pptr, *endp;
	    Comp stail = tail;
	    int dpnd = 0;

	    /* Need matching close parenthesis */
	    if (skipparens(Inpar, Outpar, &pptr)) {
		errflag = 1;
		return NULL;
	    }
	    if (kshfunc == KF_STAR)
		dpnd = 1;
	    else if (kshfunc == KF_PLUS)
		dpnd = 2;
	    else if (kshfunc == KF_QUEST)
		dpnd = 3;
	    if (*pptr == Pound && isset(EXTENDEDGLOB)) {
		/* Zero (or one) or more repetitions of group */
		pptr++;
		if(*pptr == Pound) {
		    pptr++;
		    if(dpnd == 0)
			dpnd = 2;
		    else if(dpnd == 3)
			dpnd = 1;
		} else
		    dpnd = 1;
	    }
	    /* Parse the remaining pattern following the group... */
	    if (!(c1 = parsecomp(gflag)))
		return NULL;
	    /* ...remembering what comes after it... */
	    tail = (dpnd || kshfunc == KF_NOT) ? NULL : c1;
	    /* ...before going back and parsing inside the group. */
	    endp = pptr;
	    pptr = startp;
	    c->str = dupstrpfx(cstr, (pptr - cstr) - (kshfunc != KF_NONE));
	    pptr++;
	    c2 = compalloc();
	    c->next = c2;
	    c2->next = (dpnd || kshfunc == KF_NOT) ?
		c1 : compalloc();
	    if (!(c2->left = parsecompsw(0)))
		return NULL;
	    if (kshfunc == KF_NOT) {
		/* we'd actually rather it didn't match.  Instead, match *
		 * a star and put the parsed pattern into exclude.       */
		Comp c3 = compalloc();
		c3->stat |= C_STAR;

		c2->exclude = c2->left;
		c2->left = c3;
	    }
	    /* Remember closures for group. */
	    if (dpnd)
		c2->stat |= (dpnd == 3) ? C_OPTIONAL
		    : (dpnd == 2) ? C_TWOHASH : C_ONEHASH;
	    pptr = endp;
	    tail = stail;
	    return c;
	}
	if (*pptr == Star && pptr[1] &&
	    (unset(EXTENDEDGLOB) || !(gflag & GF_TOPLEV) ||
	     pptr[1] != Tilde || !pptr[2] || pptr[2] == Bar ||
	     pptr[2] == Outpar) && (mode || pptr[1] != '/')) {
	    /* Star followed by other patterns is now treated as a special
	     * type of closure in doesmatch().
	     */
	    c->str = dupstrpfx(cstr, pptr - cstr);
	    pptr++;
	    c1 = compalloc();
	    c1->stat |= C_STAR;
	    if (!(c2 = parsecomp(gflag)))
		return NULL;
	    c1->next = c2;
	    c->next = c1;
	    return c;
	}
	if (*pptr == Pound && isset(EXTENDEDGLOB)) {
	    /* repeat whatever we've just had (ls) zero or more times */
	    if (!ls)
		return NULL;
	    c2 = compalloc();
	    c2->str = dupstrpfx(ls, pptr - ls);
	    pptr++;
	    if (*pptr == Pound) {
		/* need one or more matches: cheat by copying previous char */
		pptr++;
		c->next = c1 = compalloc();
		c1->str = c2->str;
	    } else
		c1 = c;
	    c1->next = c2;
	    c2->stat |= C_ONEHASH;
	    /* parse the rest of the pattern and return. */
	    c2->next = parsecomp(gflag);
	    if (!c2->next)
		return NULL;
	    c->str = dupstrpfx(cstr, ls - cstr);
	    return c;
	}
	ls = pptr;		/* whatever we just parsed */
	if (*pptr == Inang) {
	    /* Numeric glob */
	    int dshct;

	    dshct = (pptr[1] == Outang);
	    while (*++pptr && *pptr != Outang)
		if (*pptr == '-' && !dshct)
		    dshct = 1;
		else if (!idigit(*pptr))
		    break;
	    if (*pptr != Outang)
		return NULL;
	} else if (*pptr == Inbrack) {
	    /* Character set: brackets had better match */
	    if (pptr[1] == Outbrack)
		*++pptr = ']';
	    else if ((pptr[1] == Hat || pptr[1] == '^' || pptr[1] == '!') &&
		     pptr[2] == Outbrack)
		*(pptr += 2) = ']';
	    while (*++pptr && *pptr != Outbrack) {
		if (itok(*pptr)) {
		    /* POSIX classes: make sure it's a real one, *
		     * leave the Inbrack tokenised if so.        */
		    char *nptr;
		    if (*pptr == Inbrack && pptr[1] == ':'
			&& (nptr = strchr(pptr+2, ':')) && 
			*++nptr == Outbrack)
			pptr = nptr;
		    *pptr = ztokens[*pptr - Pound];
		}
	    }
	    if (*pptr != Outbrack)
		return NULL;
	} else if (itok(*pptr) && *pptr != Star && *pptr != Quest)
	    /* something that can be tokenised which isn't otherwise special */
	    *pptr = ztokens[*pptr - Pound];
	pptr++;
    }
    /* mark if last pattern component in path component or pattern */
    if (*pptr == '/' || !*pptr ||
	((*pptr == Bar ||
	 (isset(EXTENDEDGLOB) && *pptr == Tilde)) && (gflag & GF_TOPLEV)))
	c->stat |= C_LAST;
    c->str = dupstrpfx(cstr, pptr - cstr);
    return c;
}

/* Parse pattern possibly with different alternatives (|) */

/**/
static Comp
parsecompsw(int gflag)
{
    Comp c1, c2, c3, excl = NULL, stail = tail;
    int oaddflags = addflags;
    int oerrsmax = errsmax;
    char *sptr;

    /*
     * Check for a tilde in the expression.  We need to know this in 
     * advance so as to be able to treat the whole a~b expression by
     * backtracking:  see exclusion code in doesmatch().
     */
    if (isset(EXTENDEDGLOB)) {
	int pct = 0;
	for (sptr = pptr; *sptr; sptr++) {
	    if (*sptr == Inpar)
		pct++;
	    else if (*sptr == Outpar && --pct < 0)
		break;
	    else if (*sptr == Bar && !pct)
		break;
	    else if (*sptr == Tilde && !pct) {
		tail = NULL;
		break;
	    }
	}
    }

    c1 = parsecomp(gflag);
    if (!c1)
	return NULL;
    if (isset(EXTENDEDGLOB) && *pptr == Tilde) {
	/* Matching remainder of pattern excludes the pattern from matching */
	int oldmode = mode, olderrsmax = errsmax;

	mode = 1;
	errsmax = 0;
	pptr++;
	excl = parsecomp(gflag);
	mode = oldmode;
	errsmax = olderrsmax;
	if (!excl)
	    return NULL;
    }
    tail = stail;
    if (*pptr == Bar || excl) {
	/* found an alternative or something to exclude */
	c2 = compalloc();
	if (*pptr == Bar) {
	    /* get the next alternative after the | */
	    pptr++;
	    c3 = parsecompsw(gflag);
	    if (!c3)
		return NULL;
	} else
	    c3 = NULL;
	/* mark if end of pattern or path component */
	if (!*pptr || *pptr == '/')
	    c1->stat |= c2->stat = C_LAST;
	c2->str = dupstring("");
	c2->left = c1;
	c2->right = c3;
	if ((c2->exclude = excl))
	    c2->next = stail;
	if (gflag & GF_PATHADD)
	    c2->stat |= C_PATHADD;
	c1 = c2;
    }
    if (!(gflag & GF_TOPLEV)) {
	addflags = oaddflags;
	errsmax = oerrsmax;
    }
    return c1;
}

/* This function tokenizes a zsh glob pattern */

/**/
static Complist
parsecomplist(void)
{
    Comp c1;
    Complist p1;
    char *str;

    if (pptr[0] == Star && pptr[1] == Star &&
        (pptr[2] == '/' || (pptr[2] == Star && pptr[3] == '/'))) {
	/* Match any number of directories. */
	int follow;

	/* with three stars, follow symbolic links */
	follow = (pptr[2] == Star);
	pptr += (3 + follow);

	/* Now get the next path component if there is one. */
	p1 = (Complist) alloc(sizeof *p1);
	if ((p1->next = parsecomplist()) == NULL) {
	    errflag = 1;
	    return NULL;
	}
	p1->comp = compalloc();
	p1->comp->stat |= C_LAST;	/* end of path component  */
	p1->comp->str = dupstring("*");
	*p1->comp->str = Star;		/* match anything...      */
	p1->closure = 1;		/* ...zero or more times. */
	p1->follow = follow;
	return p1;
    }

    /* Parse repeated directories such as (dir/)# and (dir/)## */
    if (*(str = pptr) == Inpar && !skipparens(Inpar, Outpar, &str) &&
        *str == Pound && isset(EXTENDEDGLOB) && str[-2] == '/') {
	pptr++;
	if (!(c1 = parsecompsw(0)))
	    return NULL;
	if (pptr[0] == '/' && pptr[1] == Outpar && pptr[2] == Pound) {
	    int pdflag = 0;

	    pptr += 3;
	    if (*pptr == Pound) {
		pdflag = 1;
		pptr++;
	    }
	    p1 = (Complist) alloc(sizeof *p1);
	    p1->comp = c1;
	    p1->closure = 1 + pdflag;
	    p1->follow = 0;
	    p1->next = parsecomplist();
	    return (p1->comp) ? p1 : NULL;
	}
    } else {
	/* parse single path component */
	if (!(c1 = parsecompsw(GF_PATHADD|GF_TOPLEV)))
	    return NULL;
	/* then do the remaining path compoents */
	if (*pptr == '/' || !*pptr) {
	    int ef = *pptr == '/';

	    p1 = (Complist) alloc(sizeof *p1);
	    p1->comp = c1;
	    p1->closure = 0;
	    p1->next = ef ? (pptr++, parsecomplist()) : NULL;
	    return (ef && !p1->next) ? NULL : p1;
	}
    }
    errflag = 1;
    return NULL;
}

/* turn a string into a Complist struct:  this has path components */

/**/
static Complist
parsepat(char *str)
{
    mode = 0;			/* path components present */
    addflags = 0;
    errsmax = 0;
    pptr = str;
    tail = NULL;
    /*
     * Check for initial globbing flags, so that they don't form
     * a bogus path component.
     */
    if (*pptr == Inpar && pptr[1] == Pound && isset(EXTENDEDGLOB) &&
	getglobflags())
	return NULL;

    /* Now there is no (#X) in front, we can check the path. */
    if (!pathbuf)
	pathbuf = zalloc(pathbufsz = PATH_MAX);
    DPUTS(pathbufcwd, "BUG: glob changed directory");
    if (*pptr == '/') {		/* pattern has absolute path */
	pptr++;
	pathbuf[0] = '/';
	pathbuf[pathpos = 1] = '\0';
    } else			/* pattern is relative to pwd */
	pathbuf[pathpos = 0] = '\0';

    return parsecomplist();
}

/* get number after qualifier */

/**/
static long
qgetnum(char **s)
{
    long v = 0;

    if (!idigit(**s)) {
	zerr("number expected", NULL, 0);
	return 0;
    }
    while (idigit(**s))
	v = v * 10 + *(*s)++ - '0';
    return v;
}

/* get mode spec after qualifier */

/**/
static long
qgetmodespec(char **s)
{
    long yes = 0, no = 0, val, mask, t;
    char *p = *s, c, how, end;

    if ((c = *p) == '=' || c == Equals || c == '+' || c == '-' ||
	c == '?' || c == Quest || (c >= '0' && c <= '7')) {
	end = 0;
	c = 0;
    } else {
	end = (c == '<' ? '>' :
	       (c == '[' ? ']' :
		(c == '{' ? '}' :
		 (c == Inang ? Outang :
		  (c == Inbrack ? Outbrack :
		   (c == Inbrace ? Outbrace : c))))));
	p++;
    }
    do {
	mask = 0;
	while (((c = *p) == 'u' || c == 'g' || c == 'o' || c == 'a') && end) {
	    switch (c) {
	    case 'o': mask |= 01007; break;
	    case 'g': mask |= 02070; break;
	    case 'u': mask |= 04700; break;
	    case 'a': mask |= 07777; break;
	    }
	    p++;
	}
	how = ((c == '+' || c == '-') ? c : '=');
	if (c == '+' || c == '-' || c == '=' || c == Equals)
	    p++;
	val = 0;
	if (mask) {
	    while ((c = *p++) != ',' && c != end) {
		switch (c) {
		case 'x': val |= 00111; break;
		case 'w': val |= 00222; break;
		case 'r': val |= 00444; break;
		case 's': val |= 06000; break;
		case 't': val |= 01000; break;
		case '0': case '1': case '2': case '3':
		case '4': case '5': case '6': case '7':
		    t = ((long) c - '0');
		    val |= t | (t << 3) | (t << 6);
		    break;
		default:
		    zerr("invalid mode specification", NULL, 0);
		    return 0;
		}
	    }
	    if (how == '=' || how == '+') {
		yes |= val & mask;
		val = ~val;
	    }
	    if (how == '=' || how == '-')
		no |= val & mask;
	} else {
	    t = 07777;
	    while ((c = *p) == '?' || c == Quest ||
		   (c >= '0' && c <= '7')) {
		if (c == '?' || c == Quest) {
		    t = (t << 3) | 7;
		    val <<= 3;
		} else {
		    t <<= 3;
		    val = (val << 3) | ((long) c - '0');
		}
		p++;
	    }
	    if (end && c != end && c != ',') {
		zerr("invalid mode specification", NULL, 0);
		return 0;
	    }
	    if (how == '=') {
		yes = (yes & ~t) | val;
		no = (no & ~t) | (~val & ~t);
	    } else if (how == '+')
		yes |= val;
	    else
		no |= val;
	}
    } while (end && c != end);

    *s = p;
    return ((yes & 07777) | ((no & 07777) << 12));
}

static int
gmatchcmp(Gmatch a, Gmatch b)
{
    int i, *s;
    long r = 0L;

    for (i = gf_nsorts, s = gf_sortlist; i; i--, s++) {
	switch (*s & ~GS_DESC) {
	case GS_NAME:
	    r = notstrcmp(&a->name, &b->name);
	    break;
	case GS_SIZE:
	    r = b->size - a->size;
	    break;
	case GS_ATIME:
	    r = a->atime - b->atime;
	    break;
	case GS_MTIME:
	    r = a->mtime - b->mtime;
	    break;
	case GS_CTIME:
	    r = a->ctime - b->ctime;
	    break;
	case GS_LINKS:
	    r = b->links - a->links;
	    break;
	case GS__SIZE:
	    r = b->_size - a->_size;
	    break;
	case GS__ATIME:
	    r = a->_atime - b->_atime;
	    break;
	case GS__MTIME:
	    r = a->_mtime - b->_mtime;
	    break;
	case GS__CTIME:
	    r = a->_ctime - b->_ctime;
	    break;
	case GS__LINKS:
	    r = b->_links - a->_links;
	    break;
	}
	if (r)
	    return (int) ((*s & GS_DESC) ? -r : r);
    }
    return 0;
}

/* Main entry point to the globbing code for filename globbing. *
 * np points to a node in the list list which will be expanded  *
 * into a series of nodes.                                      */

/**/
void
glob(LinkList list, LinkNode np)
{
    struct qual *qo, *qn, *ql;
    LinkNode node = prevnode(np);
    char *str;				/* the pattern                   */
    int sl;				/* length of the pattern         */
    Complist q;				/* pattern after parsing         */
    char *ostr = (char *)getdata(np);	/* the pattern before the parser */
					/* chops it up                   */
    int first = 0, last = -1;		/* index of first/last match to  */
				        /* return */
    MUSTUSEHEAP("glob");
    if (unset(GLOBOPT) || !haswilds(ostr)) {
	untokenize(ostr);
	return;
    }
    str = dupstring(ostr);
    sl = strlen(str);
    uremnode(list, np);

    /* Initialise state variables for current file pattern */
    qo = qn = quals = ql = NULL;
    qualct = qualorct = 0;
    colonmod = NULL;
    gf_nullglob = isset(NULLGLOB);
    gf_markdirs = isset(MARKDIRS);
    gf_listtypes = gf_follow = 0;
    gf_noglobdots = unset(GLOBDOTS);
    gf_sorts = gf_nsorts = 0;

    /* Check for qualifiers */
    if (isset(BAREGLOBQUAL) && str[sl - 1] == Outpar) {
	char *s;

	/* Check these are really qualifiers, not a set of *
	 * alternatives or exclusions                      */
	for (s = str + sl - 2; *s != Inpar; s--)
	    if (*s == Bar || *s == Outpar ||
		(isset(EXTENDEDGLOB) && *s == Tilde))
		break;
	if (*s == Inpar && (!isset(EXTENDEDGLOB) || s[1] != Pound)) {
	    /* Real qualifiers found. */
	    int sense = 0;	/* bit 0 for match (0)/don't match (1)   */
				/* bit 1 for follow links (2), don't (0) */
	    long data = 0;	/* Any numerical argument required       */
	    int (*func) _((Statptr, long));

	    str[sl-1] = 0;
	    *s++ = 0;
	    while (*s && !colonmod) {
		func = (int (*) _((Statptr, long)))0;
		if (idigit(*s)) {
		    /* Store numeric argument for qualifier */
		    func = qualflags;
		    data = 0;
		    while (idigit(*s))
			data = data * 010 + (*s++ - '0');
		} else if (*s == ',') {
		    /* A comma separates alternative sets of qualifiers */
		    s++;
		    sense = 0;
		    if (qualct) {
			qn = (struct qual *)hcalloc(sizeof *qn);
			qo->or = qn;
			qo = qn;
			qualorct++;
			qualct = 0;
			ql = NULL;
		    }
		} else
		    switch (*s++) {
		    case ':':
			/* Remaining arguments are history-type     *
			 * colon substitutions, handled separately. */
			colonmod = s - 1;
			untokenize(colonmod);
			break;
		    case Hat:
		    case '^':
			/* Toggle sense:  go from positive to *
			 * negative match and vice versa.     */
			sense ^= 1;
			break;
		    case '-':
			/* Toggle matching of symbolic links */
			sense ^= 2;
			break;
		    case '@':
			/* Match symbolic links */
			func = qualislnk;
			break;
		    case Equals:
		    case '=':
			/* Match sockets */
			func = qualissock;
			break;
		    case 'p':
			/* Match named pipes */
			func = qualisfifo;
			break;
		    case '/':
			/* Match directories */
			func = qualisdir;
			break;
		    case '.':
			/* Match regular files */
			func = qualisreg;
			break;
		    case '%':
			/* Match special files: block, *
			 * character or any device     */
			if (*s == 'b')
			    s++, func = qualisblk;
			else if (*s == 'c')
			    s++, func = qualischr;
			else
			    func = qualisdev;
			break;
		    case Star:
			/* Match executable plain files */
			func = qualiscom;
			break;
		    case 'R':
			/* Match world-readable files */
			func = qualflags;
			data = 0004;
			break;
		    case 'W':
			/* Match world-writeable files */
			func = qualflags;
			data = 0002;
			break;
		    case 'X':
			/* Match world-executable files */
			func = qualflags;
			data = 0001;
			break;
		    case 'A':
			func = qualflags;
			data = 0040;
			break;
		    case 'I':
			func = qualflags;
			data = 0020;
			break;
		    case 'E':
			func = qualflags;
			data = 0010;
			break;
		    case 'r':
			/* Match files readable by current process */
			func = qualflags;
			data = 0400;
			break;
		    case 'w':
			/* Match files writeable by current process */
			func = qualflags;
			data = 0200;
			break;
		    case 'x':
			/* Match files executable by current process */
			func = qualflags;
			data = 0100;
			break;
		    case 's':
			/* Match setuid files */
			func = qualflags;
			data = 04000;
			break;
		    case 'S':
			/* Match setgid files */
			func = qualflags;
			data = 02000;
			break;
		    case 't':
			func = qualflags;
			data = 01000;
			break;
		    case 'd':
			/* Match device files by device number  *
			 * (as given by stat's st_dev element). */
			func = qualdev;
			data = qgetnum(&s);
			break;
		    case 'l':
			/* Match files with the given no. of hard links */
			func = qualnlink;
			amc = -1;
			goto getrange;
		    case 'U':
			/* Match files owned by effective user ID */
			func = qualuid;
			data = geteuid();
			break;
		    case 'G':
			/* Match files owned by effective group ID */
			func = qualgid;
			data = getegid();
			break;
		    case 'u':
			/* Match files owned by given user id */
			func = qualuid;
			/* either the actual uid... */
			if (idigit(*s))
			    data = qgetnum(&s);
			else {
			    /* ... or a user name */
			    char sav, *tt;

			    /* Find matching delimiters */
			    tt = get_strarg(s);
			    if (!*tt) {
				zerr("missing end of name",
				     NULL, 0);
				data = 0;
			    } else {
#ifdef HAVE_GETPWNAM
				struct passwd *pw;
				sav = *tt;
				*tt = '\0';

				if ((pw = getpwnam(s + 1)))
				    data = pw->pw_uid;
				else {
				    zerr("unknown user", NULL, 0);
				    data = 0;
				}
				*tt = sav;
#else /* !HAVE_GETPWNAM */
				sav = *tt;
				zerr("unknown user", NULL, 0);
				data = 0;
#endif /* !HAVE_GETPWNAM */
				if (sav)
				    s = tt + 1;
				else
				    s = tt;
			    }
			}
			break;
		    case 'g':
			/* Given gid or group id... works like `u' */
			func = qualgid;
			/* either the actual gid... */
			if (idigit(*s))
			    data = qgetnum(&s);
			else {
			    /* ...or a delimited group name. */
			    char sav, *tt;

			    tt = get_strarg(s);
			    if (!*tt) {
				zerr("missing end of name",
				     NULL, 0);
				data = 0;
			    } else {
#ifdef HAVE_GETGRNAM
				struct group *gr;
				sav = *tt;
				*tt = '\0';

				if ((gr = getgrnam(s + 1)))
				    data = gr->gr_gid;
				else {
				    zerr("unknown group", NULL, 0);
				    data = 0;
				}
				*tt = sav;
#else /* !HAVE_GETGRNAM */
				sav = *tt;
				zerr("unknown group", NULL, 0);
				data = 0;
#endif /* !HAVE_GETGRNAM */
				if (sav)
				    s = tt + 1;
				else
				    s = tt;
			    }
			}
			break;
		    case 'f':
			/* Match modes with chmod-spec. */
			func = qualmodeflags;
			data = qgetmodespec(&s);
			break;
		    case 'M':
			/* Mark directories with a / */
			if ((gf_markdirs = !(sense & 1)))
			    gf_follow = sense & 2;
			break;
		    case 'T':
			/* Mark types in a `ls -F' type fashion */
			if ((gf_listtypes = !(sense & 1)))
			    gf_follow = sense & 2;
			break;
		    case 'N':
			/* Nullglob:  remove unmatched patterns. */
			gf_nullglob = !(sense & 1);
			break;
		    case 'D':
			/* Glob dots: match leading dots implicitly */
			gf_noglobdots = sense & 1;
			break;
		    case 'a':
			/* Access time in given range */
			amc = 0;
			func = qualtime;
			goto getrange;
		    case 'm':
			/* Modification time in given range */
			amc = 1;
			func = qualtime;
			goto getrange;
		    case 'c':
			/* Inode creation time in given range */
			amc = 2;
			func = qualtime;
			goto getrange;
		    case 'L':
			/* File size (Length) in given range */
			func = qualsize;
			amc = -1;
			/* Get size multiplier */
			units = TT_BYTES;
			if (*s == 'p' || *s == 'P')
			    units = TT_POSIX_BLOCKS, ++s;
			else if (*s == 'k' || *s == 'K')
			    units = TT_KILOBYTES, ++s;
			else if (*s == 'm' || *s == 'M')
			    units = TT_MEGABYTES, ++s;
		      getrange:
			/* Get time multiplier */
			if (amc >= 0) {
			    units = TT_DAYS;
			    if (*s == 'h')
				units = TT_HOURS, ++s;
			    else if (*s == 'm')
				units = TT_MINS, ++s;
			    else if (*s == 'w')
				units = TT_WEEKS, ++s;
			    else if (*s == 'M')
				units = TT_MONTHS, ++s;
			}
			/* See if it's greater than, equal to, or less than */
			if ((range = *s == '+' ? 1 : *s == '-' ? -1 : 0))
			    ++s;
			data = qgetnum(&s);
			break;

		    case 'o':
		    case 'O':
			{
			    int t;

			    switch (*s) {
			    case 'n': t = GS_NAME; break;
			    case 'L': t = GS_SIZE; break;
			    case 'l': t = GS_LINKS; break;
			    case 'a': t = GS_ATIME; break;
			    case 'm': t = GS_MTIME; break;
			    case 'c': t = GS_CTIME; break;
			    default:
				zerr("unknown sort specifier", NULL, 0);
				return;
			    }
			    if ((sense & 2) && t != GS_NAME)
				t <<= GS_SHIFT;
			    if (gf_sorts & t) {
				zerr("doubled sort specifier", NULL, 0);
				return;
			    }
			    gf_sorts |= t;
			    gf_sortlist[gf_nsorts++] = t |
				(((sense & 1) ^ (s[-1] == 'O')) ? GS_DESC : 0);
			    s++;
			    break;
			}
		    case '[':
		    case Inbrack:
			{
			    char *os = --s;
			    struct value v;

			    v.isarr = SCANPM_WANTVALS;
			    v.pm = NULL;
			    v.b = -1;
			    v.inv = 0;
			    if (getindex(&s, &v) || s == os) {
				zerr("invalid subscript", NULL, 0);
				return;
			    }
			    first = v.a;
			    last = v.b;
			    break;
			}
		    default:
			zerr("unknown file attribute", NULL, 0);
			return;
		    }
		if (func) {
		    /* Requested test is performed by function func */
		    if (!qn)
			qn = (struct qual *)hcalloc(sizeof *qn);
		    if (ql)
			ql->next = qn;
		    ql = qn;
		    if (!quals)
			quals = qo = qn;
		    qn->func = func;
		    qn->sense = sense;
		    qn->data = data;
		    qn->range = range;
		    qn->units = units;
		    qn->amc = amc;
		    qn = NULL;
		    qualct++;
		}
		if (errflag)
		    return;
	    }
	}
    }
    q = parsepat(str);
    if (!q || errflag) {	/* if parsing failed */
	if (unset(BADPATTERN)) {
	    untokenize(ostr);
	    insertlinknode(list, node, ostr);
	    return;
	}
	errflag = 0;
	zerr("bad pattern: %s", ostr, 0);
	return;
    }
    if (!gf_nsorts) {
	gf_sortlist[0] = gf_sorts = GS_NAME;
	gf_nsorts = 1;
    }
    /* Initialise receptacle for matched files, *
     * expanded by insert() where necessary.    */
    matchptr = matchbuf = (Gmatch)zalloc((matchsz = 16) *
					 sizeof(struct gmatch));
    matchct = 0;
    errsfound = 0;
    errsmax = -1;

    /* The actual processing takes place here: matches go into  *
     * matchbuf.  This is the only top-level call to scanner(). */
    scanning = 1;
    scanner(q);
    scanning = 0;

    /* Deal with failures to match depending on options */
    if (matchct)
	badcshglob |= 2;	/* at least one cmd. line expansion O.K. */
    else if (!gf_nullglob) {
	if (isset(CSHNULLGLOB)) {
	    badcshglob |= 1;	/* at least one cmd. line expansion failed */
	} else if (isset(NOMATCH)) {
	    zerr("no matches found: %s", ostr, 0);
	    free(matchbuf);
	    return;
	} else {
	    /* treat as an ordinary string */
	    untokenize(matchptr->name = dupstring(ostr));
	    matchptr++;
	    matchct = 1;
	}
    }
    /* Sort arguments in to lexical (and possibly numeric) order. *
     * This is reversed to facilitate insertion into the list.    */
    qsort((void *) & matchbuf[0], matchct, sizeof(struct gmatch),
	       (int (*) _((const void *, const void *)))gmatchcmp);

    if (first < 0)
	first += matchct;
    if (last < 0)
	last += matchct;
    if (first < 0)
	first = 0;
    if (last >= matchct)
	last = matchct - 1;
    if (first <= last) {
	matchptr = matchbuf + matchct - 1 - last;
	last -= first;
	while (last-- >= 0) {		/* insert matches in the arg list */
	    insertlinknode(list, node, matchptr->name);
	    matchptr++;
	}
    }
    free(matchbuf);
}

/* Return the order of two strings, taking into account *
 * possible numeric order if NUMERICGLOBSORT is set.    *
 * The comparison here is reversed.                     */

/**/
static int
notstrcmp(char **a, char **b)
{
    char *c = *b, *d = *a;
    int cmp;

#ifdef HAVE_STRCOLL
    cmp = strcoll(c, d);
#endif
    for (; *c == *d && *c; c++, d++);
#ifndef HAVE_STRCOLL
    cmp = (int)STOUC(*c) - (int)STOUC(*d);
#endif
    if (isset(NUMERICGLOBSORT) && (idigit(*c) || idigit(*d))) {
	for (; c > *b && idigit(c[-1]); c--, d--);
	if (idigit(*c) && idigit(*d)) {
	    while (*c == '0')
		c++;
	    while (*d == '0')
		d++;
	    for (; idigit(*c) && *c == *d; c++, d++);
	    if (idigit(*c) || idigit(*d)) {
		cmp = (int)STOUC(*c) - (int)STOUC(*d);
		while (idigit(*c) && idigit(*d))
		    c++, d++;
		if (idigit(*c) && !idigit(*d))
		    return 1;
		if (idigit(*d) && !idigit(*c))
		    return -1;
	    }
	}
    }
    return cmp;
}

/* Return the trailing character for marking file types */

/**/
char
file_type(mode_t filemode)
{
    if(S_ISBLK(filemode))
	return '#';
    else if(S_ISCHR(filemode))
	return '%';
    else if(S_ISDIR(filemode))
	return '/';
    else if(S_ISFIFO(filemode))
	return '|';
    else if(S_ISLNK(filemode))
	return '@';
    else if(S_ISREG(filemode))
	return (filemode & S_IXUGO) ? '*' : ' ';
    else if(S_ISSOCK(filemode))
	return '=';
    else
	return '?';
}

/* check to see if str is eligible for brace expansion */

/**/
int
hasbraces(char *str)
{
    char *lbr, *mbr, *comma;

    if (isset(BRACECCL)) {
	/* In this case, any properly formed brace expression  *
	 * will match and expand to the characters in between. */
	int bc;

	for (bc = 0; *str; ++str)
	    if (*str == Inbrace) {
		if (!bc && str[1] == Outbrace)
		    *str++ = '{', *str = '}';
		else
		    bc++;
	    } else if (*str == Outbrace) {
		if (!bc)
		    *str = '}';
		else if (!--bc)
		    return 1;
	    }
	return 0;
    }
    /* Otherwise we need to look for... */
    lbr = mbr = comma = NULL;
    for (;;) {
	switch (*str++) {
	case Inbrace:
	    if (!lbr) {
		lbr = str - 1;
		while (idigit(*str))
		    str++;
		if (*str == '.' && str[1] == '.') {
		    str++;
		    while (idigit(*++str));
		    if (*str == Outbrace &&
			(idigit(lbr[1]) || idigit(str[-1])))
			return 1;
		}
	    } else {
		char *s = --str;

		if (skipparens(Inbrace, Outbrace, &str)) {
		    *lbr = *s = '{';
		    if (comma)
			str = comma;
		    if (mbr && mbr < str)
			str = mbr;
		    lbr = mbr = comma = NULL;
		} else if (!mbr)
		    mbr = s;
	    }
	    break;
	case Outbrace:
	    if (!lbr)
		str[-1] = '}';
	    else if (comma)
		return 1;
	    else {
		*lbr = '{';
		str[-1] = '}';
		if (mbr)
		    str = mbr;
		mbr = lbr = NULL;
	    }
	    break;
	case Comma:
	    if (!lbr)
		str[-1] = ',';
	    else if (!comma)
		comma = str - 1;
	    break;
	case '\0':
	    if (lbr)
		*lbr = '{';
	    if (!mbr && !comma)
		return 0;
	    if (comma)
		str = comma;
	    if (mbr && mbr < str)
		str = mbr;
	    lbr = mbr = comma = NULL;
	    break;
	}
    }
}

/* expand stuff like >>*.c */

/**/
int
xpandredir(struct redir *fn, LinkList tab)
{
    LinkList fake;
    char *nam;
    struct redir *ff;
    int ret = 0;

    /* Stick the name in a list... */
    fake = newlinklist();
    addlinknode(fake, fn->name);
    /* ...which undergoes all the usual shell expansions */
    prefork(fake, isset(MULTIOS) ? 0 : PF_SINGLE);
    /* Globbing is only done for multios. */
    if (!errflag && isset(MULTIOS))
	globlist(fake);
    if (errflag)
	return 0;
    if (nonempty(fake) && !nextnode(firstnode(fake))) {
	/* Just one match, the usual case. */
	char *s = peekfirst(fake);
	fn->name = s;
	untokenize(s);
	if (fn->type == MERGEIN || fn->type == MERGEOUT) {
	    if (s[0] == '-' && !s[1])
		fn->type = CLOSE;
	    else if (s[0] == 'p' && !s[1]) 
		fn->fd2 = (fn->type == MERGEOUT) ? coprocout : coprocin;
	    else {
		while (idigit(*s))
		    s++;
		if (!*s && s > fn->name)
		    fn->fd2 = zstrtol(fn->name, NULL, 10);
		else if (fn->type == MERGEIN)
		    zerr("file number expected", NULL, 0);
		else
		    fn->type = ERRWRITE;
	    }
	}
    } else if (fn->type == MERGEIN)
	zerr("file number expected", NULL, 0);
    else {
	if (fn->type == MERGEOUT)
	    fn->type = ERRWRITE;
	while ((nam = (char *)ugetnode(fake))) {
	    /* Loop over matches, duplicating the *
	     * redirection for each file found.   */
	    ff = (struct redir *)alloc(sizeof *ff);
	    *ff = *fn;
	    ff->name = nam;
	    addlinknode(tab, ff);
	    ret = 1;
	}
    }
    return ret;
}

/* concatenate s1 and s2 in dynamically allocated buffer */

/**/
char *
dyncat(char *s1, char *s2)
{
    /* This version always uses space from the current heap. */
    char *ptr;
    int l1 = strlen(s1);

    ptr = (char *)ncalloc(l1 + strlen(s2) + 1);
    strcpy(ptr, s1);
    strcpy(ptr + l1, s2);
    return ptr;
}

/* concatenate s1, s2, and s3 in dynamically allocated buffer */

/**/
char *
tricat(char const *s1, char const *s2, char const *s3)
{
    /* This version always uses permanently-allocated space. */
    char *ptr;

    ptr = (char *)zalloc(strlen(s1) + strlen(s2) + strlen(s3) + 1);
    strcpy(ptr, s1);
    strcat(ptr, s2);
    strcat(ptr, s3);
    return ptr;
}

/* brace expansion */

/**/
void
xpandbraces(LinkList list, LinkNode *np)
{
    LinkNode node = (*np), last = prevnode(node);
    char *str = (char *)getdata(node), *str3 = str, *str2;
    int prev, bc, comma, dotdot;

    for (; *str != Inbrace; str++);
    /* First, match up braces and see what we have. */
    for (str2 = str, bc = comma = dotdot = 0; *str2; ++str2)
	if (*str2 == Inbrace)
	    ++bc;
	else if (*str2 == Outbrace) {
	    if (--bc == 0)
		break;
	} else if (bc == 1) {
	    if (*str2 == Comma)
		++comma;	/* we have {foo,bar} */
	    else if (*str2 == '.' && str2[1] == '.')
		dotdot++;	/* we have {num1..num2} */
	}
    DPUTS(bc, "BUG: unmatched brace in xpandbraces()");
    if (!comma && dotdot) {
	/* Expand range like 0..10 numerically: comma or recursive
	   brace expansion take precedence. */
	char *dots, *p;
	LinkNode olast = last;
	/* Get the first number of the range */
	int rstart = zstrtol(str+1,&dots,10), rend = 0, err = 0, rev = 0;
	int wid1 = (dots - str) - 1, wid2 = (str2 - dots) - 2;
	int strp = str - str3;
      
	if (dots == str + 1 || *dots != '.' || dots[1] != '.')
	    err++;
	else {
	    /* Get the last number of the range */
	    rend = zstrtol(dots+2,&p,10);
	    if (p == dots+2 || p != str2)
		err++;
	}
	if (!err) {
	    /* If either no. begins with a zero, pad the output with   *
	     * zeroes. Otherwise, choose a min width to suppress them. */
	    int minw = (str[1] == '0') ? wid1 : (dots[2] == '0' ) ? wid2 :
		(wid2 > wid1) ? wid1 : wid2;
	    if (rstart > rend) {
		/* Handle decreasing ranges correctly. */
		int rt = rend;
		rend = rstart;
		rstart = rt;
		rev = 1;
	    }
	    uremnode(list, node);
	    for (; rend >= rstart; rend--) {
		/* Node added in at end, so do highest first */
		p = dupstring(str3);
		sprintf(p + strp, "%0*d", minw, rend);
		strcat(p + strp, str2 + 1);
		insertlinknode(list, last, p);
		if (rev)	/* decreasing:  add in reverse order. */
		    last = nextnode(last);
	    }
	    *np = nextnode(olast);
	    return;
	}
    }
    if (!comma && isset(BRACECCL)) {	/* {a-mnop} */
	/* Here we expand each character to a separate node,      *
	 * but also ranges of characters like a-m.  ccl is a      *
	 * set of flags saying whether each character is present; *
	 * the final list is in lexical order.                    */
	char ccl[256], *p;
	unsigned char c1, c2, lastch;
	unsigned int len, pl;

	uremnode(list, node);
	memset(ccl, 0, sizeof(ccl) / sizeof(ccl[0]));
	for (p = str + 1, lastch = 0; p < str2;) {
	    if (itok(c1 = *p++))
		c1 = ztokens[c1 - STOUC(Pound)];
	    if ((char) c1 == Meta)
		c1 = 32 ^ *p++;
	    if (itok(c2 = *p))
		c2 = ztokens[c2 - STOUC(Pound)];
	    if ((char) c2 == Meta)
		c2 = 32 ^ p[1];
	    if (c1 == '-' && lastch && p < str2 && (int)lastch <= (int)c2) {
		while ((int)lastch < (int)c2)
		    ccl[lastch++] = 1;
		lastch = 0;
	    } else
		ccl[lastch = c1] = 1;
	}
	pl = str - str3;
	len = pl + strlen(++str2) + 2;
	for (p = ccl + 255; p-- > ccl;)
	    if (*p) {
		c1 = p - ccl;
		if (imeta(c1)) {
		    str = ncalloc(len + 1);
		    str[pl] = Meta;
		    str[pl+1] = c1 ^ 32;
		    strcpy(str + pl + 2, str2);
		} else {
		    str = ncalloc(len);
		    str[pl] = c1;
		    strcpy(str + pl + 1, str2);
		}
		memcpy(str, str3, pl);
		insertlinknode(list, last, str);
	    }
	*np = nextnode(last);
	return;
    }
    prev = str++ - str3;
    str2++;
    uremnode(list, node);
    node = last;
    /* Finally, normal comma expansion               *
     * str1{foo,bar}str2 -> str1foostr2 str1barstr2. *
     * Any number of intervening commas is allowed.  */
    for (;;) {
	char *zz, *str4;
	int cnt;

	for (str4 = str, cnt = 0; cnt || (*str != Comma && *str !=
					  Outbrace); str++) {
	    if (*str == Inbrace)
		cnt++;
	    else if (*str == Outbrace)
		cnt--;
	    DPUTS(!*str, "BUG: illegal brace expansion");
	}
	/* Concatenate the string before the braces (str3), the section *
	 * just found (str4) and the text after the braces (str2)       */
	zz = (char *)ncalloc(prev + (str - str4) + strlen(str2) + 1);
	ztrncpy(zz, str3, prev);
	strncat(zz, str4, str - str4);
	strcat(zz, str2);
	/* and add this text to the argument list. */
	insertlinknode(list, node, zz);
	incnode(node);
	if (*str != Outbrace)
	    str++;
	else
	    break;
    }
    *np = nextnode(last);
}

/* check to see if a matches b (b is not a filename pattern) */

/**/
int
matchpat(char *a, char *b)
{
    Comp c = parsereg(b);

    if (!c) {
	zerr("bad pattern: %s", b, 0);
	return 0;
    }
    return domatch(a, c, 0);
}

/* do the ${foo%%bar}, ${foo#bar} stuff */
/* please do not laugh at this code. */

struct repldata {
    int b, e;			/* beginning and end of chunk to replace */
};
typedef struct repldata *Repldata;

/* 
 * List of bits of matches to concatenate with replacement string.
 * The data is a struct repldata.  It is not used in cases like
 * ${...//#foo/bar} even though SUB_GLOBAL is set, since the match
 * is anchored.  It goes on the heap.
 */

static LinkList repllist;

/* Having found a match in getmatch, decide what part of string
 * to return.  The matched part starts b characters into string s
 * and finishes e characters in: 0 <= b <= e <= strlen(s)
 * (yes, empty matches should work).
 * fl is a set of the SUB_* matches defined in zsh.h from SUB_MATCH onwards;
 * the lower parts are ignored.
 * replstr is the replacement string for a substitution
 */

/**/
static char *
get_match_ret(char *s, int b, int e, int fl, char *replstr)
{
    char buf[80], *r, *p, *rr;
    int ll = 0, l = strlen(s), bl = 0, t = 0, i;

    if (replstr) {
	if ((fl & SUB_GLOBAL) && repllist) {
	    /* We are replacing the chunk, just add this to the list */
	    Repldata rd = (Repldata) zhalloc(sizeof(*rd));
	    rd->b = b;
	    rd->e = e;
	    addlinknode(repllist, rd);
	    return s;
	}
	ll += strlen(replstr);
    }
    if (fl & SUB_MATCH)			/* matched portion */
	ll += 1 + (e - b);
    if (fl & SUB_REST)		/* unmatched portion */
	ll += 1 + (l - (e - b));
    if (fl & SUB_BIND) {
	/* position of start of matched portion */
	sprintf(buf, "%d ", b + 1);
	ll += (bl = strlen(buf));
    }
    if (fl & SUB_EIND) {
	/* position of end of matched portion */
	sprintf(buf + bl, "%d ", e + 1);
	ll += (bl = strlen(buf));
    }
    if (fl & SUB_LEN) {
	/* length of matched portion */
	sprintf(buf + bl, "%d ", e - b);
	ll += (bl = strlen(buf));
    }
    if (bl)
	buf[bl - 1] = '\0';

    rr = r = (char *)ncalloc(ll);

    if (fl & SUB_MATCH) {
	/* copy matched portion to new buffer */
	for (i = b, p = s + b; i < e; i++)
	    *rr++ = *p++;
	t = 1;
    }
    if (fl & SUB_REST) {
	/* Copy unmatched portion to buffer.  If both portions *
	 * requested, put a space in between (why?)            */
	if (t)
	    *rr++ = ' ';
	/* there may be unmatched bits at both beginning and end of string */
	for (i = 0, p = s; i < b; i++)
	    *rr++ = *p++;
	if (replstr)
	    for (p = replstr; *p; )
		*rr++ = *p++;
	for (i = e, p = s + e; i < l; i++)
	    *rr++ = *p++;
	t = 1;
    }
    *rr = '\0';
    if (bl) {
	/* if there was a buffer (with a numeric result), add it; *
	 * if there was other stuff too, stick in a space first.  */
	if (t)
	    *rr++ = ' ';
	strcpy(rr, buf);
    }
    return r;
}

/*
 * Run the pattern so that we always get the longest possible match.
 * This eliminates a loop where we gradually shorten the target string
 * to find same.  We also need to check pptr (the point successfully
 * reached along the target string) explicitly.
 *
 * For this to work, we need the full hairy closure code, so
 * set inclosure.
 */

/**/
static int
dolongestmatch(char *str, Comp c, int fist)
{
    int ret;
    longest = 1;
    inclosure++;
    ret = domatch(str, c, fist);
    inclosure--;
    longest = 0;
    return ret;
}

/*
 * This is called from paramsubst to get the match for ${foo#bar} etc.
 * fl is a set of the SUB_* flags defined in zsh.h
 * *sp points to the string we have to modify. The n'th match will be
 * returned in *sp. ncalloc is used to get memory for the result string.
 * replstr is the replacement string from a ${.../orig/repl}, in
 * which case pat is the original.
 *
 * n is now ignored unless we are looking for a substring, in
 * which case the n'th match from the start is counted such that
 * there is no more than one match from each position.
 */

/**/
int
getmatch(char **sp, char *pat, int fl, int n, char *replstr)
{
    Comp c;

    MUSTUSEHEAP("getmatch");	/* presumably covered by prefork() test */
    c = parsereg(pat);
    if (!c) {
 	zerr("bad pattern: %s", pat, 0);
 	return 1;
    }
    return igetmatch(sp, c, fl, n, replstr);
}

/**/
void
getmatcharr(char ***ap, char *pat, int fl, int n, char *replstr)
{
    char **arr = *ap, **pp;
    Comp c;

    MUSTUSEHEAP("getmatch");	/* presumably covered by prefork() test */

    c = parsereg(pat);
    if (!c) {
	zerr("bad pattern: %s", pat, 0);
	return;
    }
    *ap = pp = ncalloc(sizeof(char *) * (arrlen(arr) + 1));
    while ((*pp = *arr++))
	if (igetmatch(pp, c, fl, n, replstr))
	    pp++;
}

/**/
static int
igetmatch(char **sp, Comp c, int fl, int n, char *replstr)
{
    char *s = *sp, *t, *start, sav;
    int i, l = strlen(*sp), matched;

    repllist = NULL;

    if (fl & SUB_ALL) {
	i = domatch(s, c, 0);
	*sp = get_match_ret(*sp, 0, i ? l : 0, fl, i ? replstr : 0);
	if (! **sp && (((fl & SUB_MATCH) && !i) || ((fl & SUB_REST) && i)))
	    return 0;
	return 1;
    }
    switch (fl & (SUB_END|SUB_LONG|SUB_SUBSTR)) {
    case 0:
    case SUB_LONG:
	/*
	 * Largest/smallest possible match at head of string.
	 * First get the longest match...
	 */
	if (dolongestmatch(s, c, 0)) {
	    char *mpos = pptr;
	    if (!(fl & SUB_LONG)) {
	      /*
	       * ... now we know whether it's worth looking for the
	       * shortest, which we do by brute force.
	       */
	      for (t = s; t < mpos; METAINC(t)) {
		sav = *t;
		*t = '\0';
		if (dolongestmatch(s, c, 0)) {
		  mpos = pptr;
		  *t = sav;
		  break;
		}
		*t = sav;
	      }
	    }
	    *sp = get_match_ret(*sp, 0, mpos-s, fl, replstr);
	    return 1;
	}
	break;

    case SUB_END:
	/* Smallest possible match at tail of string:  *
	 * move back down string until we get a match. *
	 * There's no optimization here.               */
	for (t = s + l; t >= s; t--) {
	    if (domatch(t, c, 0)) {
		*sp = get_match_ret(*sp, t - s, l, fl, replstr);
		return 1;
	    }
	    if (t > s+1 && t[-2] == Meta)
		t--;
	}
	break;

    case (SUB_END|SUB_LONG):
	/* Largest possible match at tail of string:       *
	 * move forward along string until we get a match. *
	 * Again there's no optimisation.                  */
	for (i = 0, t = s; i < l; i++, t++) {
	    if (domatch(t, c, 0)) {
		*sp = get_match_ret(*sp, i, l, fl, replstr);
		return 1;
	    }
	    if (*t == Meta)
		i++, t++;
	}
	break;

    case SUB_SUBSTR:
	/* Smallest at start, but matching substrings. */
	if (!(fl & SUB_GLOBAL) && domatch(s + l, c, 0) && !--n) {
	    *sp = get_match_ret(*sp, 0, 0, fl, replstr);
	    return 1;
	} /* fall through */
    case (SUB_SUBSTR|SUB_LONG):
	/* longest or smallest at start with substrings */
	start = s;
	if (fl & SUB_GLOBAL)
	    repllist = newlinklist();
	do {
	    /* loop over all matches for global substitution */
	    matched = 0;
	    for (t = start; t < s + l; t++) {
		/* Find the longest match from this position. */
		if (dolongestmatch(t, c, 0) && pptr > t) {
		    char *mpos = pptr;
		    while (!(fl & SUB_LONG) && pptr > t) {
			/* Now reduce to find the smallest match */
			char *p = (pptr > t + 1 && pptr[-2] == Meta) ?
			    pptr - 2 : pptr - 1;
			sav = *p;
			*p = '\0';
			if (!dolongestmatch(t, c, 0)) {
			    *p = sav;
			    break;
			}
			mpos = pptr;
			*p = sav;
		    }
		    if (!--n || (n <= 0 && (fl & SUB_GLOBAL)))
			*sp = get_match_ret(*sp, t-s, mpos-s, fl, replstr);
		    if (!(fl & SUB_GLOBAL)) {
			if (n) {
			    /*
			     * Looking for a later match: in this case,
			     * we can continue looking for matches from
			     * the next character, even if it overlaps
			     * with what we just found.
			     */
			    continue;
			} else
			    return 1;
		    }
		    /*
		     * For a global match, we need to skip the stuff
		     * which is already marked for replacement.
		     */
		    matched = 1;
		    start = mpos;
		    break;
		}
		if (*t == Meta)
		    t++;
	    }
	} while (matched);
	/*
	 * check if we can match a blank string, if so do it
	 * at the start.  Goodness knows if this is a good idea
	 * with global substitution, so it doesn't happen.
	 */
	if ((fl & (SUB_LONG|SUB_GLOBAL)) == SUB_LONG &&
	    domatch(s + l, c, 0) && !--n) {
	    *sp = get_match_ret(*sp, 0, 0, fl, replstr);
	    return 1;
	}
	break;

    case (SUB_END|SUB_SUBSTR):
	/* Shortest at end with substrings */
	if (domatch(s + l, c, 0) && !--n) {
	    *sp = get_match_ret(*sp, l, l, fl, replstr);
	    return 1;
	} /* fall through */
    case (SUB_END|SUB_LONG|SUB_SUBSTR):
	/* Longest/shortest at end, matching substrings.       */
	for (t = s + l - 1; t >= s; t--) {
	    if (t > s && t[-1] == Meta)
		t--;
	    if (dolongestmatch(t, c, 0) && pptr > t && !--n) {
		/* Found the longest match */
		char *mpos = pptr;
		while (!(fl & SUB_LONG) && pptr > t) {
		    /* Look for the shortest match */
		    char *p = (pptr > t+1 && pptr[-2] == Meta) ?
			pptr-2 : pptr-1;
		    sav = *p;
		    *p = '\0';
		    if (!dolongestmatch(t, c, 0) || pptr == t) {
			*p = sav;
			break;
		    }
		    *p = sav;
		    mpos = pptr;
		}
		*sp = get_match_ret(*sp, t-s, mpos-s, fl, replstr);
		return 1;
	    }
	}
	if ((fl & SUB_LONG) && domatch(s + l, c, 0) && !--n) {
	    *sp = get_match_ret(*sp, l, l, fl, replstr);
	    return 1;
	}
	break;
    }

    if (repllist && nonempty(repllist)) {
	/* Put all the bits of a global search and replace together. */
	LinkNode nd;
	Repldata rd;
	int rlen;
	int lleft = 0;		/* size of returned string */

	i = 0;			/* start of last chunk we got from *sp */
	rlen = strlen(replstr);
	for (nd = firstnode(repllist); nd; incnode(nd)) {
	    rd = (Repldata) getdata(nd);
	    lleft += rd->b - i; /* previous chunk of *sp */
	    lleft += rlen;	/* the replaced bit */
	    i = rd->e;		/* start of next chunk of *sp */
	}
	lleft += l - i;	/* final chunk from *sp */
	start = t = zhalloc(lleft+1);
	i = 0;
	for (nd = firstnode(repllist); nd; incnode(nd)) {
	    rd = (Repldata) getdata(nd);
	    memcpy(t, s + i, rd->b - i);
	    t += rd->b - i;
	    memcpy(t, replstr, rlen);
	    t += rlen;
	    i = rd->e;
	}
	memcpy(t, s + i, l - i);
	start[lleft] = '\0';
	*sp = start;
	return 1;
    }

    /* munge the whole string: no match, so no replstr */
    *sp = get_match_ret(*sp, 0, 0, fl, 0);
    return 1;
}

/* The main entry point for matching a string str against  *
 * a compiled pattern c.  `fist' indicates whether leading *
 * dots are special.                                       */

/**/
int
domatch(char *str, Comp c, int fist)
{
    int ret;
    pptr = str;
    first = fist;
    /*
     * If scanning paths, keep the error count over the whole
     * filename
     */
    if (!scanning) {
	errsfound = 0;
	errsmax = -1;
    }
    if (*pptr == Nularg)
	pptr++;
    PERMALLOC {
	ret = doesmatch(c);
    } LASTALLOC;
    return ret;
}

#define untok(C)  (itok(C) ? ztokens[(C) - Pound] : (C))

/* See if pattern has a matching exclusion (~...) part */

/**/
static int
excluded(Comp c, char *eptr)
{
    char *saves = pptr;
    int savei = first, savel = longest, savee = errsfound, ret;

    first = 0;
    /*
     * Even if we've been told always to match the longest string,
     * i.e. not anchored to the end, we want to match the full string
     * we've already matched when we're trying to exclude it.
     * Likewise, approximate matching here is treated separately.
     */
    longest = 0;
    errsfound = 0;
    if (PATHADDP(c) && pathpos) {
	VARARR(char, buf, pathpos + strlen(eptr) + 1);

	strcpy(buf, pathbuf);
	strcpy(buf + pathpos, eptr);
	pptr = buf;
	ret = doesmatch(c->exclude);
    } else {
	pptr = eptr;
	ret = doesmatch(c->exclude);
    }
    if (*pptr)
	ret = 0;

    pptr = saves;
    first = savei;
    longest = savel;
    errsfound = savee;

    return ret;
}

/*
 * Structure for storing instances of a pattern in a closured.
 */
struct gclose {
    char *start;		/* Start of this instance */
    char *end;			/* End of this instance */
    int errsfound;		/* Errors found up to start of instance */
};
typedef struct gclose *Gclose;

/* Add a list of matches that fit the closure.  trystring is a string of
 * the same length as the target string; a non-zero in that string
 * indicates that we have already tried to match the patterns following
 * the closure (c->next) at that point and failed.  This means that not
 * only should we not bother using the corresponding match, we should
 * also not bother going any further, since the first time we got to
 * that position (when it was marked), we must already have failed on
 * and backtracked over any further closure matches beyond that point.
 * If we are using approximation, the number in the string is incremented
 * by the current error count; if we come back to that point with a
 * lower error count, it is worth trying from that point again, but
 * we must now mark that point in trystring with the new error.
 */

/**/
static void
addclosures(Comp c, LinkList closlist, int *pdone, unsigned char *trystring)
{
    Gclose gcnode;
    char *opptr = pptr;
    unsigned char *tpos;

    while (*pptr) {
	if (STARP(c)) {
	    if (*(tpos = trystring + ((pptr+1) - opptr))) {
		if ((unsigned)(errsfound+1) >= *tpos)
		    break;
		*tpos = (unsigned)(errsfound+1);
	    }
	    gcnode = (Gclose)zalloc(sizeof(struct gclose));
	    gcnode->start = pptr;
	    gcnode->end = METAINC(pptr);
	    gcnode->errsfound = errsfound;
	} else {
	    char *saves = pptr;
	    int savee = errsfound;
	    if (OPTIONALP(c) && *pdone >= 1)
		return;
	    if (!matchonce(c) || saves == pptr ||
		(*(tpos = trystring + (pptr-opptr)) &&
		 (unsigned)(errsfound+1) >= *tpos)) {
		pptr = saves;
		break;
	    }
	    if (*tpos)
		*tpos = (unsigned)(errsfound+1);
	    gcnode = (Gclose)zalloc(sizeof(struct gclose));
	    gcnode->start = saves;
	    gcnode->end = pptr;
	    gcnode->errsfound = savee;
	}
	pushnode(closlist, gcnode);
	(*pdone)++;
    }
}

/*
 * Match characters with case-insensitivity.  Note charmatch(x,y) !=
 * charmatch(y,x): the first argument comes from the target string, the
 * second from the pattern.  This used to be a macro, and in principle
 * could be turned back into one.
 */

/**/
static int
charmatch(Comp c, char *x, char *y)
{
    /*
     * This takes care of matching two characters which may be a
     * two byte sequence, Meta followed by something else.
     * Here we bypass tulower() and tuupper() for speed.
     */
    int xi = (STOUC(UNMETA(x)) & 0xff), yi = (STOUC(UNMETA(y)) & 0xff);
    return xi == yi ||
	(((c->stat & C_IGNCASE) ?
	  ((isupper(xi) ? tolower(xi) : xi) ==
	   (isupper(yi) ? tolower(yi) : yi)) :
	  (c->stat & C_LCMATCHUC) ?
	  (islower(yi) && toupper(yi) == xi) : 0));
}

/* see if current string in pptr matches c */

/**/
static int
doesmatch(Comp c)
{
    if (CLOSUREP(c)) {
	int done, retflag = 0;
	char *saves, *opptr;
	unsigned char *trystring, *tpos;
	int savee;
	LinkList closlist;
	Gclose gcnode;

	if (first && *pptr == '.')
	    return 0;

	if (!inclosure && !c->left && !c->errsmax) {
	    /* We are not inside another closure, and the current
	     * pattern is a simple string.  We handle this very common
	     * case specially: otherwise, matches like *foo* are
	     * extremely slow.  Here, instead of backtracking, we track
	     * forward until we get a match.  At top level, we are bound
	     * to get there eventually, so this is OK.
	     */
	    char looka;

	    if (STARP(c) && c->next &&
		!c->next->left && (looka = *c->next->str) &&
		!itok(looka)) {
		/* Another simple optimisation for a very common case:
		 * we are processing a * and there is
		 * an ordinary character match (which may not be a Metafied
		 * character, just to make it easier) next.  We look ahead
		 * for that character, taking care of Meta bytes.
		 */
		while (*pptr) {
		    for (; *pptr; pptr++) {
			if (*pptr == Meta)
			    pptr++;
			else if (charmatch(c, pptr, &looka))
			    break;
		    }
		    if (!*(saves = pptr))
			break;
		    savee = errsfound;
		    if (doesmatch(c->next))
			return 1;
		    pptr = saves+1;
		    errsfound = savee;
		}
	    } else {
		/* Standard track-forward code */
		for (done = 0; ; done++) {
		    saves = pptr;
		    savee = errsfound;
		    if ((done || ONEHASHP(c) || OPTIONALP(c)) &&
			((!c->next && (!LASTP(c) || !*pptr || longest)) ||
			 (c->next && doesmatch(c->next))))
			return 1;
		    if (done && OPTIONALP(c))
			return 0;
		    pptr = saves;
		    errsfound = savee;
		    first = 0;
		    if (STARP(c)) {
			if (!*pptr)
			    return 0;
			pptr++;
		    } else if (!matchonce(c) || pptr == saves)
			return 0;
		}
	    }
	    return 0;
	}
	/* The full, gory backtracking code is now necessary. */
	inclosure++;
	closlist = newlinklist();
	trystring = (unsigned char *)zcalloc(strlen(pptr)+1);
	opptr = pptr;

	/* Start by making a list where each match is as long
	 * as possible.  We later have to take account of the
	 * fact that earlier matches may be too long.
	 */
	done = 0;
	addclosures(c, closlist, &done, trystring);
	for (;;) {
	    if (TWOHASHP(c) && !done)
		break;
	    saves = pptr;
	    /* do we really want this LASTP here?? */
	    if ((!c->next && (!LASTP(c) || !*pptr || longest)) ||
		(c->next && doesmatch(c->next))) {
		retflag = 1;
		break;
	    }
	    trystring[saves-opptr] = (unsigned)(errsfound + 1);
	    /*
	     * If we failed, the first thing to try is whether we can
	     * shorten the match using the last pattern in the closure.
	     */
	    gcnode = firstnode(closlist) ? peekfirst(closlist) : NULL;
	    if (gcnode && --gcnode->end > gcnode->start
		&& (gcnode->end[-1] != Meta ||
		    --gcnode->end > gcnode->start)) {
		char savec = *gcnode->end;
		*gcnode->end = '\0';
		pptr = gcnode->start;
		errsfound = gcnode->errsfound;
		if (matchonce(c) && pptr != gcnode->start
		    && (!*(tpos = trystring + (pptr-opptr)) ||
			*tpos > (unsigned)(errsfound+1))) {
		    if (*tpos)
			*tpos = (unsigned)(errsfound+1);
		    *gcnode->end = savec;
		    gcnode->end = pptr;
		    /* Try again to construct a list based on
		     * this new position
		     */
		    addclosures(c, closlist, &done, tpos);
		    continue;
		}
		*gcnode->end = savec;
	    }
	    /* We've now exhausted the possibilities with that match,
	     * backtrack to the previous.
	     */
	    if ((gcnode = (Gclose)getlinknode(closlist))) {
		pptr = gcnode->start;
		errsfound = gcnode->errsfound;
		zfree(gcnode, sizeof(struct gclose));
		done--;
	    } else
		break;
	}
	freelinklist(closlist, free);
	zfree(trystring, strlen(opptr)+1);
	inclosure--;

	return retflag;
    } else
	return matchonce(c);
}

/**/
static int
posix_range(char **patptr, int ch)
{
    /* Match POSIX ranges, which correspond to ctype macros,  *
     * e.g. [:alpha:] -> isalpha.  It just doesn't seem worth * 
     * the palaver of creating a hash table for this.         */
    char *start = *patptr;
    int len;

    /* we made sure in parsecomp() there was a ':' to search for */
    *patptr = strchr(start, ':');
    len = (*patptr)++ - start;

    if (!strncmp(start, "alpha", len))
	return isalpha(ch);
    if (!strncmp(start, "alnum", len))
	return isalnum(ch);
    if (!strncmp(start, "blank", len))
	return ch == ' ' || ch == '\t';
    if (!strncmp(start, "cntrl", len))
	return iscntrl(ch);
    if (!strncmp(start, "digit", len))
	return isdigit(ch);
    if (!strncmp(start, "graph", len))
	return isgraph(ch);
    if (!strncmp(start, "lower", len))
	return islower(ch);
    if (!strncmp(start, "print", len))
	return isprint(ch);
    if (!strncmp(start, "punct", len))
	return ispunct(ch);
    if (!strncmp(start, "space", len))
	return isspace(ch);
    if (!strncmp(start, "upper", len))
	return isupper(ch);
    if (!strncmp(start, "xdigit", len))
	return isxdigit(ch);
    return 0;
}

/**/
static void
rangematch(char **patptr, int ch, int rchar)
{
    /* Check for a character in a [...] or [^...].  The [ *
     * and optional ^ have already been skipped.          */

    char *pat = *patptr;
#ifdef HAVE_STRCOLL
    char l_buf[2], r_buf[2], ch_buf[2];

    ch_buf[0] = ch;
    l_buf[1] = r_buf[1] = ch_buf[1] = '\0';
#endif

#define PAT(X) (pat[X] == Meta ? pat[(X)+1] ^ 32 : untok(pat[X]))
#define PPAT(X) (pat[(X)-1] == Meta ? pat[X] ^ 32 : untok(pat[X]))

    for (pat++; *pat != Outbrack && *pat; METAINC(pat)) {
	if (*pat == Inbrack) {
	    /* Inbrack can only occur inside a range if we found [:...:]. */
	    pat += 2;
	    if (posix_range(&pat, ch))
		break;
	} else if (*pat == '-' && pat[-1] != rchar &&
		   pat[1] != Outbrack) {
#ifdef HAVE_STRCOLL
	    l_buf[0] = PPAT(-1);
	    r_buf[0] = PAT(1);
	    if (strcoll(l_buf, ch_buf) <= 0 &&
		strcoll(ch_buf, r_buf) <= 0)
#else
		if (PPAT(-1) <= ch && PAT(1) >= ch)
#endif
		    break;
	} else if (ch == PAT(0))
	    break;
    }

    *patptr = pat;
}

/*
 * matchonce() is the core of the pattern matching, handling individual
 * strings and instances of a pattern in a closure.
 *
 * Note on approximate matching:  The rule is supposed to be
 *   (1) Take the longest possible match without approximation.
 *   (2) At any failure, make the single approximation that results
 *       in the longest match for the remaining part (which may
 *       include further approximations).
 *   (3) If we match the same distance, take the one with fewer
 *       approximations.
 * If this is wrong, I haven't yet discovered a counterexample.  Email
 * lines are now open.
 *                   pws 1999/02/23
 */

/**/
static int
matchonce(Comp c)
{
    char *pat = c->str;
    for (;;) {
	/* loop until success or failure of pattern */
	if (!pat || !*pat) {
	    /* No current pattern (c->str). */
	    char *saves;
	    int savee, savei;

	    if (errflag)
		return 0;
	    /* Remember state in case we need to go back and   *
	     * check for exclusion of pattern or alternatives. */
	    saves = pptr;
	    savei = first;
	    savee = errsfound;
	    /* Loop over alternatives with exclusions: (foo~bar|...). *
	     * Exclusions apply to the pattern in c->left.            */
	    if (c->left || c->right) {
		int ret = 0, ret2 = 0;
		if (c->exclude) {
		    char *exclend = 0;

		    /* We may need to back up on things like `(*~foo)'
		     * if the `*' matched `foo' but needs to match `fo'.
		     * exclend marks the end of the shortened text.  We
		     * need to restore it to match the tail.
		     * We set `inclosure' because we need the more
		     * sophisticated code in doesmatch() for any nested
		     * closures.
		     */
		    inclosure++;

		    while (!exclend || exclend >= pptr) {
			char exclsav = 0;
			if (exclend) {
			     exclsav = *exclend;
			    *exclend = '\0';
			 }
			if ((ret = doesmatch(c->left))) {
			    if (exclend)
				*exclend = exclsav;
			    exclsav = *(exclend = pptr);
			    *exclend = '\0';
			    ret2 = !excluded(c, saves);
			}
			if (exclend)
			    *exclend = exclsav;

			if (!ret)
			    break;
			if ((ret = ret2 &&
			     ((!c->next && (!LASTP(c) || !*pptr || longest))
			      || (c->next && doesmatch(c->next)))) ||
			    (!c->next && LASTP(c)))
			    break;
			/* Back up if necessary: exclend gives the position
			 * of the end of the match we are excluding,
			 * so only try to match to there.
			 */
			exclend--;
			pptr = saves;
			errsfound = savee;
		    }
		    inclosure--;
		    if (ret)
			return 1;
		} else
		    ret = doesmatch(c->left);
		ret2 = 0;
		if (c->right && (!ret || inclosure)) {
		    /* If in a closure, we always want the longest match. */
		    char *newpptr = pptr;
		    int newerrsfound = errsfound;
		    pptr = saves;
		    first = savei;
		    errsfound = savee;
		    ret2 = doesmatch(c->right);
		    if (ret && (!ret2 || pptr < newpptr)) {
			pptr = newpptr;
			errsfound = newerrsfound;
		    }
		}
		if (!ret && !ret2) {
		    pptr = saves;
		    first = savei;
		    errsfound = savee;
		    break;
		}
	    }
	    if (CLOSUREP(c))
		return 1;
	    if (!c->next) {
		/*
		 * No more patterns left, but we may still be in the middle
		 * of a match, in which case alles in Ordnung...
		 */ 
		if (!LASTP(c))
		    return 1;
		/*
		 * ...else we're at the last pattern, so this is our last
		 * ditch attempt at an approximate match: try to omit the
		 * last few characters.
		 */
		for (; *pptr && errsfound < c->errsmax &&
			 (errsmax == -1 || errsfound < errsmax);
		     METAINC(pptr))
		    errsfound++;
		return !*pptr || longest;
	    }
	    /* optimisation when next pattern is not a closure */
	    if (!CLOSUREP(c->next)) {
		c = c->next;
		pat = c->str;
		continue;
	    }
	    return doesmatch(c->next);
	}
	/*
	 * Don't match leading dot if first is set
	 * (don't even try for an approximate match)
	 */
	if (first && *pptr == '.' && *pat != '.')
	    return 0;
	if (*pat == Star) {	/* final * is not expanded to ?#; returns success */
	    while (*pptr)
		pptr++;
	    return 1;
	}
	first = 0;		/* finished checking start of pattern */
	if (*pat == Quest) {
	    /* match exactly one character */
	    if (!*pptr)
		break;
	    METAINC(pptr);
	    pat++;
	    continue;
	}
	if (*pat == Inbrack) {
	    /* Match groups of characters */
	    char ch;
	    char *saves, *savep;

	    if (!*pptr)
		break;
	    saves = pptr;
	    savep = pat;
	    ch = UNMETA(pptr);
	    if (pat[1] == Hat || pat[1] == '^' || pat[1] == '!') {
		/* group is negated */
		*++pat = Hat;
		rangematch(&pat, ch, Hat);
		DPUTS(!*pat, "BUG: something is very wrong in doesmatch()");
		if (*pat != Outbrack) {
		    pptr = saves;
		    pat = savep;
		    break;
		}
		pat++;
		METAINC(pptr);
		continue;
	    } else {
		/* pattern is not negated (affirmed? asserted?) */
		rangematch(&pat, ch, Inbrack);
		DPUTS(!pat || !*pat, "BUG: something is very wrong in doesmatch()");
		if (*pat == Outbrack) {
		    pptr = saves;
		    pat = savep;
		    break;
		}
		for (METAINC(pptr); *pat != Outbrack; pat++);
		pat++;
		continue;
	    }
	}
	if (*pat == Inang) {
	    /* Numeric globbing. */
	    unsigned long t1, t2, t3;
	    char *ptr, *saves = pptr, *savep = pat;

	    if (!idigit(*pptr))
		break;
	    if (*++pat == Outang || 
		(*pat == '-' && pat[1] == Outang && ++pat)) {
		/* <> or <->:  any number matches */
		while (idigit(*++pptr));
		pat++;
	    } else {
		/* Flag that there is no upper limit */
		int not3 = 0;
		char *opptr = pptr;
		/*
		 * Form is <a-b>, where a or b are numbers or blank.
		 * t1 = number supplied:  must be positive, so use
		 * unsigned arithmetic.
		 */
		t1 = (unsigned long)zstrtol(pptr, &ptr, 10);
		pptr = ptr;
		/* t2 = lower limit */
		if (idigit(*pat))
		    t2 = (unsigned long)zstrtol(pat, &ptr, 10);
		else
		    t2 = 0, ptr = pat;
		if (*ptr != '-' || (not3 = (ptr[1] == Outang)))
				/* exact match or no upper limit */
		    t3 = t2, pat = ptr + not3;
		else		/* t3 = upper limit */
		    t3 = (unsigned long)zstrtol(ptr + 1, &pat, 10);
		DPUTS(*pat != Outang, "BUG: wrong internal range pattern");
		pat++;
		/*
		 * If the number found is too large for the pattern,
		 * try matching just the first part.  This way
		 * we always get the longest possible match.
		 */
		while (!not3 && t1 > t3 && pptr > opptr+1) {
		  pptr--;
		  t1 /= 10;
		}
		if (t1 < t2 || (!not3 && t1 > t3)) {
		    pptr = saves;
		    pat = savep;
		    break;
		}
	    }
	    continue;
	}
	/* itok(Meta) is zero */
	DPUTS(itok(*pat), "BUG: matching tokenized character");
	if (charmatch(c, pptr, pat)) {
	    /* just plain old characters (or maybe unplain new characters) */
	    METAINC(pptr);
	    METAINC(pat);
	    continue;
	}
	if (errsfound < c->errsmax &&
	    (errsmax == -1 || errsfound < errsmax)) {
	    /*
	     * We tried to match literal characters and failed.  Now it's
	     * time to match approximately.  Note this doesn't handle the
	     * case where we *didn't* try to match literal characters,
	     * including the case where we were already at the end of the
	     * pattern, because then we never get here.  In that case the
	     * pattern has to be matched exactly, but we could maybe
	     * advance up the target string before trying it, so we have to
	     * handle that case elsewhere.
	     */
	    char *saves = pptr, *savep = c->str;
	    char *maxpptr = pptr, *patnext = METANEXT(pat);
	    int savee, maxerrs = -1;

	    /* First try to advance up the pattern. */
	    c->str = patnext;
	    savee = ++errsfound;
	    if (matchonce(c)) {
		maxpptr = pptr;
		maxerrs = errsfound;
	    }
	    errsfound = savee;

	    if (*saves) {
		/*
		 * If we have characters on both strings, we have more
		 * choice.
		 *
		 * Try to edge up the target string.
		 */
		char *strnext = METANEXT(saves);
		pptr = strnext;
		c->str = pat;
		if (matchonce(c) &&
		    (maxerrs == -1 || pptr > maxpptr ||
		     (pptr == maxpptr && errsfound <= maxerrs))) {
		    maxpptr = pptr;
		    maxerrs = errsfound;
		}
		errsfound = savee;

		/*
		 * Try edging up both of them at once.
		 * Note this takes precedence in the case of equal
		 * length as we get further up the pattern.
		 */
		c->str = patnext;
		pptr = strnext;
		if (matchonce(c) &&
		    (maxerrs == -1 || pptr > maxpptr ||
		     (pptr == maxpptr && errsfound <= maxerrs))) {
		    maxpptr = pptr;
		    maxerrs = errsfound;
		}
		errsfound = savee;

		/*
		 * See if we can transpose: that counts as just one error.
		 *
		 * Note, however, `abanana' and `banana': transposing
		 * is wrong here, as it gets us two errors,
		 * [ab][a]nana vs [ba][]nana, instead of the correct
		 * [a]banana vs []banana, so we still need to try
		 * the other possibilities.
		 */
		if (strnext && patnext && !itok(*patnext) &&
		    charmatch(c, strnext, pat) &&
		    charmatch(c, saves, patnext)) {
		    c->str = patnext;
		    METAINC(c->str);

		    pptr = strnext;
		    METAINC(pptr);

		    if (matchonce(c) &&
			(maxerrs == -1 || pptr > maxpptr ||
			 (pptr == maxpptr && errsfound <= maxerrs))) {
			maxpptr = pptr;
			maxerrs = errsfound;
		    }
		}
	    }
	    /*
	     * We don't usually restore state on failure, but we need
	     * to fix up the Comp structure which we altered to
	     * look at the tail of the pattern.
	     */
	    c->str = savep;
	    /*
	     * If that failed, game over:  we don't want to break
	     * and try the other approximate test, because we just did
	     * that.
	     */
	    if (maxerrs == -1)
		return 0;
	    pptr = maxpptr;
	    errsfound = maxerrs;
	    return 1;
	}
	break;
    }
    if (*pptr && errsfound < c->errsmax &&
	(errsmax == -1 || errsfound < errsmax)) {
	/*
	 * The pattern failed, but we can try edging up the target string
	 * and rematching with an error.  Note we do this from wherever we
	 * got to in the pattern string c->str, not the start. hence the
	 * need to modify c->str.
	 *
	 * At this point, we don't have a literal character in the pattern
	 * (handled above), so we don't try any funny business on the
	 * pattern itself.
	 */
	int ret;
	char *savep = c->str;
	errsfound++;
	METAINC(pptr);
	c->str = pat;
	ret = matchonce(c);
	c->str = savep;
	return ret;
    }
    return 0;
}

/* turn a string into a Comp struct:  this doesn't treat / specially */

/**/
Comp
parsereg(char *str)
{
    remnulargs(str);
    mode = 1;			/* no path components */
    addflags = 0;
    errsmax = 0;
    pptr = str;
    tail = NULL;
    return parsecompsw(GF_TOPLEV);
}

/* blindly turn a string into a tokenised expression without lexing */

/**/
void
tokenize(char *s)
{
    char *t;
    int bslash = 0;

    for (; *s; s++) {
      cont:
	switch (*s) {
	case Bnull:
	case '\\':
	    if (bslash) {
		s[-1] = Bnull;
		break;
	    }
	    bslash = 1;
	    continue;
	case '<':
	    if (isset(SHGLOB))
		break;
	    if (bslash) {
		s[-1] = Bnull;
		break;
	    }
	    t = s;
	    while (idigit(*++s));
	    if (*s != '-')
		goto cont;
	    while (idigit(*++s));
	    if (*s != '>')
		goto cont;
	    *t = Inang;
	    *s = Outang;
	    break;
	case '^':
	case '#':
	case '~':
	    if (unset(EXTENDEDGLOB))
		break;
	case '(':
	case '|':
	case ')':
	    if (isset(SHGLOB))
		break;
	case '[':
	case ']':
	case '*':
	case '?':
	    for (t = ztokens; *t; t++)
		if (*t == *s) {
		    if (bslash)
			s[-1] = Bnull;
		    else
			*s = (t - ztokens) + Pound;
		    break;
		}
	}
	bslash = 0;
    }
}

/* remove unnecessary Nulargs */

/**/
void
remnulargs(char *s)
{
    int nl = *s;
    char *t = s;

    while (*s)
	if (INULL(*s))
	    chuck(s);
	else
	    s++;
    if (!*t && nl) {
	t[0] = Nularg;
	t[1] = '\0';
    }
}

/* qualifier functions:  mostly self-explanatory, see glob(). */

/* device number */

/**/
static int
qualdev(struct stat *buf, long dv)
{
    return buf->st_dev == dv;
}

/* number of hard links to file */

/**/
static int
qualnlink(struct stat *buf, long ct)
{
    return (range < 0 ? buf->st_nlink < ct :
	    range > 0 ? buf->st_nlink > ct :
	    buf->st_nlink == ct);
}

/* user ID */

/**/
static int
qualuid(struct stat *buf, long uid)
{
    return buf->st_uid == uid;
}

/* group ID */

/**/
static int
qualgid(struct stat *buf, long gid)
{
    return buf->st_gid == gid;
}

/* device special file? */

/**/
static int
qualisdev(struct stat *buf, long junk)
{
    return S_ISBLK(buf->st_mode) || S_ISCHR(buf->st_mode);
}

/* block special file? */

/**/
static int
qualisblk(struct stat *buf, long junk)
{
    return S_ISBLK(buf->st_mode);
}

/* character special file? */

/**/
static int
qualischr(struct stat *buf, long junk)
{
    return S_ISCHR(buf->st_mode);
}

/* directory? */

/**/
static int
qualisdir(struct stat *buf, long junk)
{
    return S_ISDIR(buf->st_mode);
}

/* FIFO? */

/**/
static int
qualisfifo(struct stat *buf, long junk)
{
    return S_ISFIFO(buf->st_mode);
}

/* symbolic link? */

/**/
static int
qualislnk(struct stat *buf, long junk)
{
    return S_ISLNK(buf->st_mode);
}

/* regular file? */

/**/
static int
qualisreg(struct stat *buf, long junk)
{
    return S_ISREG(buf->st_mode);
}

/* socket? */

/**/
static int
qualissock(struct stat *buf, long junk)
{
    return S_ISSOCK(buf->st_mode);
}

/* given flag is set in mode */

/**/
static int
qualflags(struct stat *buf, long mod)
{
    return mode_to_octal(buf->st_mode) & mod;
}

/* mode matches specification */

/**/
static int
qualmodeflags(struct stat *buf, long mod)
{
    long v = mode_to_octal(buf->st_mode), y = mod & 07777, n = mod >> 12;

    return ((v & y) == y && !(v & n));
}

/* regular executable file? */

/**/
static int
qualiscom(struct stat *buf, long mod)
{
    return S_ISREG(buf->st_mode) && (buf->st_mode & S_IXUGO);
}

/* size in required range? */

/**/
static int
qualsize(struct stat *buf, long size)
{
    unsigned long scaled = buf->st_size;

    switch (units) {
    case TT_POSIX_BLOCKS:
	scaled += 511l;
	scaled /= 512l;
	break;
    case TT_KILOBYTES:
	scaled += 1023l;
	scaled /= 1024l;
	break;
    case TT_MEGABYTES:
	scaled += 1048575l;
	scaled /= 1048576l;
	break;
    }

    return (range < 0 ? scaled < (unsigned long) size :
	    range > 0 ? scaled > (unsigned long) size :
	    scaled == (unsigned long) size);
}

/* time in required range? */

/**/
static int
qualtime(struct stat *buf, long days)
{
    time_t now, diff;

    time(&now);
    diff = now - (amc == 0 ? buf->st_atime : amc == 1 ? buf->st_mtime :
		  buf->st_ctime);
    /* handle multipliers indicating units */
    switch (units) {
    case TT_DAYS:
	diff /= 86400l;
	break;
    case TT_HOURS:
	diff /= 3600l;
	break;
    case TT_MINS:
	diff /= 60l;
	break;
    case TT_WEEKS:
	diff /= 604800l;
	break;
    case TT_MONTHS:
	diff /= 2592000l;
	break;
    }

    return (range < 0 ? diff < days :
	    range > 0 ? diff > days :
	    diff == days);
}