summaryrefslogtreecommitdiffstats
path: root/private/utils/ntbackup/src/qtc_srch.c
blob: 6accc9945580809e84b816e8a72eed3f2d133fe1 (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
/***************************************************
Copyright (C) Maynard, An Archive Company. 1991

        Name: QTC_SRCH.C

        Description:

        A lot of searching functions for the catalogs.

        $Log:   N:\logfiles\qtc_srch.c_v  $

   Rev 1.31.1.0   20 Jul 1994 19:36:38   STEVEN
now search next tape as well

   Rev 1.31   18 Feb 1994 17:01:52   MIKEP
one more unicode typo

   Rev 1.30   15 Feb 1994 11:15:24   MIKEP
fix unicode catalog search stuff

   Rev 1.29   24 Jan 1994 09:36:04   MIKEP
fix yet another unicode bug

   Rev 1.28   07 Jan 1994 14:23:22   mikep
fixes for unicode

   Rev 1.27   11 Dec 1993 11:49:12   MikeP
fix warnings from unicode compile

   Rev 1.26   06 Dec 1993 09:46:26   mikep
Very deep path support & unicode fixes

   Rev 1.25   28 Oct 1993 14:47:40   MIKEP
dll changes

   Rev 1.24   20 Jul 1993 20:18:06   MIKEP
fix unicode bug for steve.

   Rev 1.23   16 Jul 1993 11:45:46   MIKEP
Fix bug if searching for files & not searching subdirectories &
crossing tapes all at the same time. It would return files that
were in child subdirectories, when it shouldn't.

   Rev 1.22   22 Jun 1993 14:35:12   DON
Needed to check if there was an error before checking for QTC_NO_MORE in SearchFirstItem

   Rev 1.21   08 Jun 1993 19:05:32   DON
If we are calling QTC_SearchFirstItem and then searching for more matches
withing the same BSD, as is the case when restoring multiple Single
File Selections, ERROR will be set to QTC_NO_MORE and when we call
QTC_SearchFirstItem again to find the next file, ERROR has not be cleared!
So...If ERROR is QTC_NO_MORE just reset error and continue else FAILURE.

   Rev 1.20   11 May 1993 08:55:24   MIKEP
Enable unicode to compile.

   Rev 1.19   30 Apr 1993 08:36:24   MIKEP
Fix search if the user is searching the root and does not wish
to search subdirectories. It was broken and would search
everything in the set if the path was the root.

   Rev 1.18   23 Mar 1993 18:00:34   ChuckS
Added arg to QTC_OpenFile indicating if need to open for writes

   Rev 1.17   04 Mar 1993 17:28:54   ANDY
Added bug-fix for MIKEP

   Rev 1.16   18 Feb 1993 09:02:10   DON
Cleaned up compiler warnings

   Rev 1.15   09 Feb 1993 17:26:46   STEVEN
checkin for mikep

   Rev 1.14   26 Jan 1993 12:28:04   ANDY
if OS_NLM, don't want alloc_text pragma's either!

   Rev 1.13   25 Jan 1993 09:09:00   MIKEP
fix duplicate \system directory bug

   Rev 1.12   21 Jan 1993 16:20:58   MIKEP
fix undisplayed directories with duplicate parents

   Rev 1.11   20 Jan 1993 19:47:16   MIKEP
fix nt warnings

   Rev 1.10   04 Jan 1993 09:34:36   MIKEP
unicode changes

   Rev 1.8   22 Dec 1992 12:02:12   DAVEV
fix for loop problem indexing past end of unicode string

   Rev 1.7   14 Dec 1992 12:29:24   DAVEV
Enabled for Unicode compile

   Rev 1.6   20 Nov 1992 13:50:38   CHARLIE
JAGUAR: Move to SRM based QTC code

ENDEAVOR: Virtualized QTC_SRCH into multiple sections in anticipation of DOS.

   Rev 1.5   15 Nov 1992 16:05:18   MIKEP
fix warnings and change wcs.h to stdwcs.h

   Rev 1.4   06 Nov 1992 15:29:58   DON
Change debug.h to be_debug.h and zprintf to BE_Zprintf

   Rev 1.3   05 Nov 1992 08:55:20   STEVEN
fix typo

   Rev 1.2   29 Oct 1992 14:41:28   MIKEP
fix duplicate directory detection

   Rev 1.1   09 Oct 1992 11:53:56   MIKEP
unicode pass

   Rev 1.0   03 Sep 1992 16:56:06   STEVEN
Initial revision.


****************************************************/

//
// A query structure may be used for 1 set of GetFirst/GetNext commands
// at a time.  You may not use the same query structure to go through
// the directory tree and get all the files in those directories.
// But you can use as many different query structures as you wish,
// at the same time, intermingling the calls. You may reuse a query
// structure.
//

#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <io.h>
#include <string.h>
#include <time.h>
#include <share.h>
#include <malloc.h>

#include "stdtypes.h"
#include "stdmath.h"
#include "stdwcs.h"
#include "qtc.h"

#if !defined( OS_WIN32 ) && !defined( OS_NLM )
#pragma alloc_text( QTC_SRCH_SEG1, QTC_SearchFirstItem )
#pragma alloc_text( QTC_SRCH_SEG2, QTC_SearchNextItem )
#pragma alloc_text( QTC_SRCH_SEG3, QTC_FastSearchForFile )
#pragma alloc_text( QTC_SRCH_SEG4, QTC_FastSearchForDir )
#pragma alloc_text( QTC_SRCH_SEG5, QTC_GetNextItem )
#pragma alloc_text( QTC_SRCH_SEG6, QTC_FindStoppingOffset )
#endif

// unicode text macro
#ifndef UNALIGNED
#define UNALIGNED
#endif

#ifndef TEXT
#define TEXT( x )      x
#endif

// defines for data buffering scheme

#define BUFF_DIR         0
#define BUFF_FILE        1
#define BUFF_CHILD_DIR   2


static INT QTC_FindDirectoryPath( QTC_QUERY_PTR, QTC_RECORD_PTR, UINT32, INT );


QTC_BSET_PTR QTC_GetBsetForSrch( QTC_QUERY_PTR qtc )
{
   QTC_TAPE_PTR tape;
   QTC_BSET_PTR bset;
   QTC_BSET_PTR temp_bset;

   // Now find a bset that matches it

   tape = QTC_GetFirstTape( );

   temp_bset = NULL;
   bset = NULL;

   while ( tape != NULL ) {

      if ( ( tape->tape_fid == qtc->tape_fid ) &&
           ( tape->tape_seq_num == qtc->tape_seq_num ) ) {

          bset = QTC_GetFirstBset( tape );

          while ( bset != NULL ) {

             if ( bset->bset_num == qtc->bset_num ) {

                if ( ! ( bset->status & ( QTC_PARTIAL | QTC_IMAGE ) ) ) {
                   break;
                }
             }
             bset = QTC_GetNextBset( bset );
          }
          break;
      }
      else {

         if ( ( tape->tape_fid == qtc->tape_fid ) &&
              ( qtc->tape_seq_num == -1 ) ) {

             temp_bset = QTC_GetFirstBset( tape );

             while ( temp_bset != NULL ) {

                if ( temp_bset->bset_num == qtc->bset_num ) {

                   if ( ! ( temp_bset->status & ( QTC_PARTIAL | QTC_IMAGE ) ) ) {

                      if ( bset == NULL ) {
                         bset = temp_bset;
                      }

                      if ( bset->tape_seq_num > temp_bset->tape_seq_num ) {

                         bset = temp_bset;
                      }
                   }
                }
                temp_bset = QTC_GetNextBset( temp_bset );
             }
         }
      }

      tape = QTC_GetNextTape( tape );
   }

   return( bset );
}



INT QTC_SetFileCounts( QTC_QUERY_PTR qtc )
{
   INT index;
   UINT32 files = 0;
   UINT32 bytes_msw = 0;
   UINT32 bytes_lsw = 0;
   UINT32 UNALIGNED *i32;

   index = 0;

   while ( index < qtc->xtra_size ) {

      if ( qtc->xtra_bytes[ index ] == QTC_COMBO_COUNT ) {

         i32 = (UINT32 *)&qtc->xtra_bytes[ index + 1 ];
         files = *i32 >> 24;
         bytes_lsw = *i32 & 0x00FFFFFFL;

      }
      if ( qtc->xtra_bytes[ index ] == QTC_FILE_COUNT ) {

         i32 = (UINT32 *)&qtc->xtra_bytes[ index + 1 ];
         files = *i32;
      }
      if ( qtc->xtra_bytes[ index ] == QTC_BYTE_COUNT_LSW ) {

         i32 = (UINT32 *)&qtc->xtra_bytes[ index + 1 ];
         bytes_lsw = *i32;
      }
      if ( qtc->xtra_bytes[ index ] == QTC_BYTE_COUNT_MSW ) {

         i32 = (UINT32 *)&qtc->xtra_bytes[ index + 1 ];
         bytes_msw = *i32;
      }
      index += 5;
   }

   qtc->file_count = (INT)files;
   qtc->byte_count = U64_Init( bytes_lsw, bytes_msw );

   return( SUCCESS );
}

INT QTC_TestItemSize( QTC_QUERY_PTR qtc, INT bytes )
{
   CHAR_PTR item;

   bytes += 4;   // in case they ask for 0

   if ( qtc->size_of_item < bytes ) {
      item = malloc( bytes );
      memcpy( item, qtc->item, qtc->size_of_item );
      free( qtc->item );
      qtc->item = item;
      qtc->size_of_item = bytes;
   }

   return( SUCCESS );
}

INT QTC_TestPathSize( QTC_QUERY_PTR qtc, INT bytes )
{
   CHAR_PTR path;

   bytes += 4;

   if ( qtc->size_of_path < bytes ) {
      path = malloc( bytes );
      memcpy( path, qtc->path, qtc->size_of_path );
      free( qtc->path );
      qtc->path = path;
      qtc->size_of_path = bytes;
   }

   return( SUCCESS );
}

INT QTC_TestLastPathSize( QTC_QUERY_PTR qtc, INT bytes )
{
   CHAR_PTR path;

   bytes += 4;

   if ( qtc->size_of_last_path < bytes ) {
      path = malloc( bytes );
      memcpy( path, qtc->last_path, qtc->size_of_last_path );
      free( qtc->last_path );
      qtc->last_path = path;
      qtc->size_of_last_path = bytes;
   }

   return( SUCCESS );
}

/***************
  Get the path to search for in the query structure provided.
****************/

INT QTC_GetSearchPath( QTC_QUERY_PTR qtc, CHAR_PTR path )
{
   if ( qtc == NULL ) {
      return( QTC_FAILURE );
   }

   memcpy( path, qtc->search_path, qtc->search_path_size );

   return( QTC_SUCCESS );
}

/***************
  Get the path to search for in the query structure provided.
****************/

INT QTC_GetSearchPathLength( QTC_QUERY_PTR qtc )
{
   if ( qtc == NULL ) {
      return( 0 );
   }

   return( qtc->search_path_size );
}


/***************
  Set the path to search for in the query structure provided.
****************/

INT QTC_SetSearchPath( QTC_QUERY_PTR qtc, CHAR_PTR path, INT size )
{
   free( qtc->search_path );

   qtc->search_path = malloc( size );
   qtc->search_path_size = size;

   if ( qtc->search_path == NULL ) {
      qtc->error = TRUE;
      return( FAILURE );
   }

   memcpy( qtc->search_path, path, size );

   return( QTC_SUCCESS );
}


/***************
  Set the name to search for in the query structure provided.

  Fills in the unicode and ascii versions for use later if needed.

****************/

INT QTC_SetSearchName( QTC_QUERY_PTR qtc, CHAR_PTR name )
{
   int length;

   free( qtc->search_name );

   length = strlen( name ) + 1;

   qtc->search_name = malloc( length * sizeof (CHAR) );

   if ( qtc->search_name == NULL ) {
      qtc->error = TRUE;
      return( FAILURE );
   }

   memcpy( qtc->search_name, name, length * sizeof (CHAR) );

   return( SUCCESS );
}


/*******************

  The user would like to query the catalogs and he has asked us to init
  a query structure for him. So do it.

********************/

QTC_QUERY_PTR QTC_InitQuery(  )
{
   QTC_QUERY_PTR qtc;

   qtc = calloc( sizeof(QTC_QUERY), 1 );

   if ( qtc != NULL ) {

      qtc->search_path = NULL;

      qtc->path = NULL;
      qtc->item = NULL;
      qtc->last_path = NULL;

      qtc->size_of_path = 0;
      qtc->size_of_item = 0;
      qtc->size_of_last_path = 0;

      qtc->header = NULL;
      qtc->error = FALSE;           // no error yet
      qtc->file_open = FALSE;       // no data file open either

      QTC_TestItemSize( qtc, sizeof( CHAR ) );
      strcpy( qtc->item, TEXT( "" ) );

      QTC_SetSubdirs( qtc, TRUE );
      QTC_SetPreDate( qtc, 0 );
      QTC_SetPostDate( qtc, 0 );

      QTC_SetSearchName( qtc, TEXT("*.*") );
      QTC_SetSearchPath( qtc, TEXT(""), sizeof(CHAR) );
   }


   return( qtc );
}

/**************
   The user is done with this query structure.
   Terminate It.
**************/

INT QTC_CloseQuery(
QTC_QUERY_PTR qtc )  // I - query structure to dump
{

   if ( qtc->file_open ) {
      QTC_CloseFile( qtc->fh );
   }

   free( qtc->path );
   free( qtc->last_path );
   free( qtc->item );
   free( qtc->header );
   free( qtc->search_path );
   free( qtc->search_name );
   free( qtc );

   return( SUCCESS );
}


/********************

  The user has requested a search of the catalogs.  The search criteria are
  in the qtc structure.  Set up our own stuff for the search and then call
  QTC_GetNextSearchItem(), to find the first item.

*********************/

INT QTC_SearchFirstItem(
QTC_QUERY_PTR qtc )
{
   BOOLEAN found;
   QTC_RECORD record;

   if ( gb_QTC.inited != QTC_MAGIC_NUMBER ) {
      return( QTC_NO_INIT );
   }

   free( qtc->header );
   qtc->header = NULL;

   /*
     If we are calling this function and then searching for more matches
     withing the same BSD, as is the case when restoring multiple Single
     File Selections, error will be set to QTC_NO_MORE and when we call
     this function again to find the next file, error will not be cleared!

     So, if the error is QTC_NO_MORE we will just reset error and continue
     else FAILURE.
   */

   if ( qtc->error )
   {
      if ( qtc->error == QTC_NO_MORE )
      {
         qtc->error = FALSE;
      }
      else
      {
         return( QTC_FAILURE );
      }
   }

   if ( qtc->file_open ) {
      QTC_CloseFile( qtc->fh );
      qtc->file_open = FALSE;
   }

   qtc->bset = QTC_GetBsetForSrch( qtc );

   if ( qtc->bset == NULL ) {
      return( QTC_BSET_NOT_FOUND );
   }

   qtc->header = QTC_LoadHeader( qtc->bset );
   if ( qtc->header == NULL ) {
      return( QTC_NO_HEADER );
   }

   qtc->fh = QTC_OpenFile( qtc->bset->tape_fid,
                           (INT16)qtc->bset->tape_seq_num, FALSE, FALSE );

   if ( qtc->fh < 0 ) {
      return( QTC_OPEN_FAILED );
   }

   qtc->file_open = TRUE;

   qtc->fil_dir_offset = 0;

   // Here we need to find the starting and ending offsets to search.
   // For the root default to entire bset.

   if ( ! strlen( qtc->search_name ) ) {

      // they only wanted a directory

      found = FALSE;

      do {

         if ( QTC_FindDirRec( qtc, &record ) == SUCCESS ) {

            qtc->curr_mom_offset = qtc->header->dir_start + record.name_offset;
            found = TRUE;
            break;
         }

      } while ( QTC_MoveToNextTapeInFamily( qtc ) == SUCCESS );

      if ( ! found ) {
         return( QTC_NO_MORE );
      }

      QTC_TestPathSize( qtc, qtc->search_path_size );
      memcpy( qtc->path, qtc->search_path, qtc->search_path_size );
      qtc->path_size = qtc->search_path_size;

      QTC_TestItemSize( qtc, ( strlen( TEXT("") ) + 1 ) * sizeof(CHAR) );
      strcpy( qtc->item, TEXT("") );

      qtc->status = (UINT8)record.status;
      qtc->date = (INT16)record.date;
      qtc->time = (INT16)record.time;
      qtc->attrib = record.attribute;
      qtc->size = U64_Init( record.common.common.height, 0L );
      qtc->lba = record.lba;

      QTC_SetFileCounts( qtc );
      return( QTC_SUCCESS );
   }


   // Decide where to start and stop searching in this set. We can just
   // search the entire set if the path is the root and we can search
   // subdirectories.


   if ( ( qtc->search_path_size == sizeof(CHAR) ) && ( qtc->subdirs ) ) {

      qtc->search_start = qtc->header->fil_start;
      qtc->curr_mom_offset = qtc->header->dir_start;
      qtc->search_stop = qtc->header->fil_start + qtc->header->fil_size;
   }
   else {

      found = FALSE;

      do {

         if ( QTC_FindDirRec( qtc, &record ) == SUCCESS ) {

            qtc->curr_mom_offset = qtc->header->dir_start + record.name_offset;
            found = TRUE;
            break;
         }

      } while ( QTC_MoveToNextTapeInFamily( qtc ) == SUCCESS );

      if ( ! found ) {
         return( QTC_NO_MORE );
      }

      qtc->search_start = qtc->header->fil_start + record.common.common.file_start;

      // Find the first directory at a higher level than this one and
      // that's where to stop.

      if ( QTC_FindStoppingOffset( qtc, &record ) ) {
         return( QTC_READ_FAILED );
      }

   }

   // Initialize buffers as empty

   qtc->search_max = 0;
   qtc->search_index = 0;
   qtc->search_base = qtc->search_start;

   // If no '*' characters in search name then set requested search length

   if ( strchr( qtc->search_name, TEXT('*') ) ) {
      qtc->search_size = 0;
   }
   else {

      qtc->search_size = (UINT16)( sizeof( QTC_NAME ) +
                         (strlen( qtc->search_name ) * sizeof(CHAR) ) );
   }

   return( QTC_SearchNextItem( qtc ) );
}

/************************

  Find the next matching item to the search request set up in the qtc.

*************************/

INT QTC_SearchNextItem( QTC_QUERY_PTR qtc )
{
   QTC_NAME UNALIGNED * name;
   QTC_RECORD record;
   UINT32 offset;
   UINT32 msw_size = 0L;
   BYTE_PTR s;
   BOOLEAN found;
   INT result;
   INT size;
   INT i;
   INT Error;


   if ( qtc->error ) {
      return( QTC_FAILURE );
   }

   if ( ! strlen( qtc->search_name ) ) {

      // They only wanted a directory, so look for a duplicate.

      if ( QTC_FindNextDirRec( qtc, &record ) != SUCCESS ) {

            found = FALSE;

            while ( QTC_MoveToNextTapeInFamily( qtc ) == SUCCESS ) {

               if ( QTC_FindDirRec( qtc, &record ) == SUCCESS ) {

                  qtc->curr_mom_offset = qtc->header->dir_start + record.name_offset;
                  found = TRUE;
                  break;
               }

            } 

            if ( ! found ) {
               return( QTC_NO_MORE );
            }
      }

      qtc->curr_mom_offset = qtc->header->dir_start + record.name_offset;

      QTC_TestPathSize( qtc, qtc->search_path_size );
      memcpy( qtc->path, qtc->search_path, qtc->search_path_size );
      qtc->path_size = qtc->search_path_size;

      QTC_TestItemSize( qtc, ( strlen( TEXT("") ) + 1 ) * sizeof(CHAR) );
      strcpy( qtc->item, TEXT("") );

      qtc->status = (UINT8)record.status;
      qtc->date = (INT16)record.date;
      qtc->time = (INT16)record.time;
      qtc->attrib = record.attribute;
      qtc->size = U64_Init( record.common.common.height, 0L );
      qtc->lba = record.lba;

      QTC_SetFileCounts( qtc );
      return( QTC_SUCCESS );
   }

   // If duplicates exist in the directory, than we need to check for the
   // possible existence of another valid search area.

   if ( QTC_FastSearchForFile( qtc ) != SUCCESS ) {

      found = FALSE;

      // Loop over all the directories in all the tapes.

      while  ( ! found ) {

         // Look at all duplicate directories first.

         while ( QTC_FindNextDirRec( qtc, &record ) == SUCCESS ) {

            qtc->curr_mom_offset = qtc->header->dir_start + record.name_offset;

            qtc->search_start = qtc->header->fil_start + record.common.common.file_start;

            // Find the first directory at a higher level than this one and
            // that's where to stop.

            if ( QTC_FindStoppingOffset( qtc, &record ) ) {
               return( QTC_READ_FAILED );
            }

            // Initialize buffers as empty

            qtc->search_max = 0;
            qtc->search_index = 0;
            qtc->search_base = qtc->search_start;

            // Now look for the file in our new duplicate directory.

            if ( QTC_FastSearchForFile( qtc ) == SUCCESS ) {
               found = TRUE;
               break;
            }

         }

         if ( ! found ) {

            // Try another tape, same bset number.

            if ( QTC_MoveToNextTapeInFamily( qtc ) == SUCCESS ) {

               // Find the directory.

               if ( QTC_FindDirRec( qtc, &record ) == SUCCESS ) {

                  qtc->curr_mom_offset = qtc->header->dir_start + record.name_offset;

                  if ( ( qtc->search_path_size == sizeof(CHAR) ) && ( qtc->subdirs ) ) {

                     qtc->search_start = qtc->header->fil_start;
                     qtc->search_stop = qtc->header->fil_start + qtc->header->fil_size;
                  }
                  else {

                     qtc->search_start = qtc->header->fil_start + record.common.common.file_start;

                     // Find the first directory at a higher level than this one and
                     // that's where to stop.

                     if ( QTC_FindStoppingOffset( qtc, &record ) ) {
                        return( QTC_READ_FAILED );
                     }
                  }

                  // Initialize buffers as empty

                  qtc->search_max = 0;
                  qtc->search_index = 0;
                  qtc->search_base = qtc->search_start;

                  // Look for the file.

                  if ( QTC_FastSearchForFile( qtc ) == SUCCESS ) {
                     found = TRUE;
                  }

                  // Go back up to the top and look for a duplicate directory.
               }
            }
            else {

               // Every damn thing we've tried has failed.

               return( FAILURE );
            }
         }
      }
   }

   name = (QTC_NAME UNALIGNED *)qtc->buff1;

   offset = qtc->header->rec_start;
   offset += name->record * sizeof( QTC_RECORD );

   QTC_SeekFile( qtc->fh, offset );

   result = QTC_ReadFile( qtc->fh, (BYTE_PTR)&record, sizeof( QTC_RECORD ), &Error );

   if ( result != sizeof( QTC_RECORD ) ) {
      return( QTC_READ_FAILED );
   }

   QTC_TestPathSize( qtc, sizeof(CHAR) );
   qtc->path[ 0 ] = TEXT( '\0' );
   qtc->path_size = sizeof(CHAR);

   // Copy name

   s = qtc->buff1 + sizeof(  QTC_NAME );

   size = (INT)name->size - (INT)name->xtra_size - sizeof( QTC_NAME );

   QTC_TestItemSize( qtc, size );
   memcpy( qtc->item, s, size );
   qtc->item[ size / sizeof(CHAR) ] = TEXT( '\0' );

   // Item xtra bytes

   s += name->size - (INT)name->xtra_size - sizeof( QTC_NAME );
   memcpy( qtc->xtra_bytes, s, (INT)name->xtra_size );
   qtc->xtra_size = (INT8)name->xtra_size;

   for ( i = 0; i < qtc->xtra_size; i += 5 ) {
      if ( qtc->xtra_bytes[ i ] == QTC_XTRA_64BIT_SIZE ) {
         memcpy( &msw_size, &qtc->xtra_bytes[ i + 1 ] , 4 );
      }
   }

   // record data

   qtc->status = (UINT8)record.status;
   qtc->date = (INT16)record.date;
   qtc->time = (INT16)record.time;
   qtc->attrib = record.attribute;
   qtc->size = U64_Init( record.common.size, msw_size );
   qtc->lba = record.lba;

   // build path up

   if ( QTC_BuildWholePath( qtc, name->mom_offset ) ) {
      return( QTC_READ_FAILED );
   }

   QTC_SetFileCounts( qtc );

   return( QTC_SUCCESS );
}

/**********************

  The goal here is to fill in the qtc->search_stop field.

  The user has asked for a search and specified a path. We have already
  found the location in the catalogs where the path  "\DOS\GAMES\JF"
  starts and now we want to know where it stops.  By finding the end
  of this directory we can abort out of our search as soon as the valid
  area has been searched.  The QTC_NAME structure for the \JF entry is
  in qtc->buff1 and the record for \JF is in the record field.  Both of
  these are no longer needed so they can be trashed by this routine.

  If the user has requested we not search subdirectories then stop at
  the first directory we find.

  If we hit the end of the backup set before we reach the end of the
  \JF directory than the end of the bset is where we stop searching.

***********************/

INT QTC_FindStoppingOffset( QTC_QUERY_PTR qtc, QTC_RECORD_PTR record )
{
   UINT32 curr_try_offset;
   UINT32 dir_max;
   UINT32 offset;
   INT init = TRUE;
   INT result;
   INT Error;
   QTC_NAME UNALIGNED * name;


   // where is the end of valid data ?

   dir_max = qtc->header->dir_start + qtc->header->dir_size;

   // use buff1 to store the temp results in

   name = ( QTC_NAME *)qtc->buff1;

   // start looking immediately after the entry for starting at

   curr_try_offset = qtc->header->dir_start;
   curr_try_offset += record->name_offset + name->size;

   // Look for name with mom_offset < last_offset
   // This guy will fill in the name structure for us

   while ( ! QTC_FastSearchForDir( qtc,
                                   &curr_try_offset,
                                   dir_max,
                                   0,
                                   init ) ) {

      // Only init once

      if ( init ) {
         init = FALSE;
      }

      // See if we found a brother or higher relative to the
      // directory we started with. If we do then we are done.

      if ( ( name->mom_offset < record->name_offset ) ||
           ( ! qtc->subdirs ) ) {

         // Yo dude, success !

         offset = qtc->header->rec_start;
         offset += ( name->record * sizeof( QTC_RECORD ) );

         QTC_SeekFile( qtc->fh, offset );

         result = QTC_ReadFile( qtc->fh, (BYTE_PTR)record, sizeof( QTC_RECORD ), &Error );

         if ( result != sizeof(QTC_RECORD ) ) {
            return( QTC_READ_FAILED );
         }

         qtc->search_stop = qtc->header->fil_start + record->common.common.file_start;

         return( QTC_SUCCESS );
      }
   }

   // We hit the end of the bset, use it for the stopping location

   qtc->search_stop = qtc->header->fil_start + qtc->header->fil_size;

   return( QTC_SUCCESS );
}


/**********************

  Build the entire path starting with the offset passed in.  We have
  found a file during a search whose parent directory ends with the
  directory entry at 'offset'.  Fill in the qtc->path field with the
  complete path for this file by working backwards up the name table.
  Where each entry has the offset of its parent.  When the parent
  offset is zero, you can stop, you are at the root.

***********************/

INT QTC_BuildWholePath( QTC_QUERY_PTR qtc, UINT32 offset )
{
  INT i;
  INT result;
  INT bytes;
  INT Error;
  QTC_NAME UNALIGNED * name;
  CHAR_PTR dirname;
  BYTE buff1[ QTC_BUF_SIZE ];
  BYTE_PTR byte_ptr;

  name = (QTC_NAME UNALIGNED *)buff1;

  byte_ptr = (BYTE_PTR)name;
  byte_ptr += sizeof( QTC_NAME );

  dirname = (CHAR_PTR)byte_ptr;

  if ( offset == 0 ) {
     QTC_TestPathSize( qtc, sizeof( CHAR ) );
     qtc->path[ 0 ] = TEXT( '\0' );
     qtc->path_size = sizeof( CHAR );
  }
  else {

     qtc->path_size = 0;

     while ( offset ) {

        QTC_SeekFile( qtc->fh, qtc->header->dir_start + offset );

        result = QTC_ReadFile( qtc->fh, buff1, QTC_BUF_SIZE, &Error );

        if ( result < sizeof( QTC_NAME ) || result < (INT)name->size ) {
           return( QTC_READ_FAILED );
        }

        // Zero terminate name

        dirname[ (name->size - name->xtra_size - sizeof( QTC_NAME )) / sizeof(CHAR) ] = 0;

        offset = name->mom_offset;

        // BEFORE
        //   qtc->path[]         dirname[]
        //   01234567890         0123456789
        //   xx0xxx0             yyy0
        //
        // AFTER
        //   qtc->path[]
        //   yyy0xx0xxx0

        bytes =  qtc->path_size + strsize( dirname );

        QTC_TestPathSize( qtc, bytes );
        for ( i = qtc->path_size/sizeof(CHAR); i > 0; i-- ) {
           qtc->path[ i + strlen( dirname ) ] = qtc->path[ i - 1 ];
        }

        strcpy( qtc->path, dirname );
        qtc->path_size += strsize( dirname );
     }
  }


  return( SUCCESS );
}


/*******************

   The user is performing a catalog search call.  The search parameters are
   all set up and the only thing for us to do now is skim through the
   filenames looking for one that is a match.

********************/

INT QTC_FastSearchForFile( QTC_QUERY_PTR qtc )
{
  UINT bytes_left;
  INT index;
  QTC_NAME UNALIGNED *name;
  BYTE buff[ QTC_BUF_SIZE ];
  BYTE_PTR s;
  INT Error;


  name = ( QTC_NAME *)qtc->buff1;

  while ( TRUE ) {

     s = (BYTE_PTR)name;

     // copy the fixed size part of the structure

     if ( qtc->search_index + sizeof( QTC_NAME ) <= qtc->search_max ) {

        // fast copy, no buffer break

        memcpy( s, &(qtc->buff2[ qtc->search_index ]), sizeof( QTC_NAME ) );

        qtc->search_index += sizeof( QTC_NAME );
     }
     else {

        // Slow copy to handle buffer break

        memcpy( s,
                &(qtc->buff2[ qtc->search_index ]),
                qtc->search_max - qtc->search_index );


        s += qtc->search_max - qtc->search_index;

        bytes_left = sizeof(QTC_NAME) -
                     ( qtc->search_max - qtc->search_index );

        qtc->search_base += qtc->search_max;

        if ( qtc->search_base >= qtc->search_stop ) {
           return( QTC_NO_MORE );
        }

        qtc->search_max = (UINT16)min( (INT32)QTC_BUF_SIZE,
                                       qtc->search_stop - qtc->search_base );

        QTC_SeekFile( qtc->fh, qtc->search_base );

        qtc->search_max = (UINT16)QTC_ReadFile( qtc->fh, qtc->buff2, (INT)qtc->search_max, &Error );

        if ( qtc->search_max < (UINT16)bytes_left ) {
           return( QTC_NO_MORE );
        }

        memcpy( s,
                &(qtc->buff2[ 0 ]),
                bytes_left );

        qtc->search_index = (UINT16)bytes_left;

     }

     // See if this file is a match

     s = (BYTE_PTR)name;

     if ( ( qtc->search_size == 0 ) ||
          ( qtc->search_size == (UINT16)(name->size - name->xtra_size ) ) ) {

        // copy variable sized part of structure, the name which we learned
        // how long it was from the fixed part we just read in.

        s += sizeof( QTC_NAME );

        if ( (UINT16)( qtc->search_index +
               (INT)name->size - sizeof(QTC_NAME) ) <= qtc->search_max ) {

           // fast copy name

           memcpy( s,
                   &(qtc->buff2[ qtc->search_index ]),
                   (INT)name->size - sizeof( QTC_NAME ) );

           qtc->search_index += (UINT16)(name->size - sizeof( QTC_NAME ));
        }
        else {

           // Slow copy to handle buffer break

           memcpy( s,
                   &(qtc->buff2[ qtc->search_index ]),
                   qtc->search_max - qtc->search_index );

           s += qtc->search_max - qtc->search_index;

           bytes_left = (INT)name->size - sizeof(QTC_NAME) -
                        ( qtc->search_max - qtc->search_index );

           qtc->search_base += qtc->search_max;

           if ( qtc->search_base >= qtc->search_stop ) {
              return( QTC_NO_MORE );
           }

           qtc->search_max = (UINT16)min( (INT32)QTC_BUF_SIZE,
                                         qtc->search_stop - qtc->search_base );

           QTC_SeekFile( qtc->fh, qtc->search_base );

           qtc->search_max = (UINT16)QTC_ReadFile( qtc->fh, qtc->buff2, (INT)qtc->search_max, &Error );

           if ( qtc->search_max < (UINT16)bytes_left ) {
              return( QTC_NO_MORE );
           }

           memcpy( s,
                   &(qtc->buff2[ 0 ]),
                   bytes_left );

           qtc->search_index = (UINT16)bytes_left;

        }

        s = (BYTE_PTR)name;
        s += sizeof(QTC_NAME);     // point to file name again

        memcpy( buff, s, (INT)name->size - (INT)name->xtra_size - sizeof(QTC_NAME) );

        index = (INT)name->size - (INT)name->xtra_size - sizeof(QTC_NAME);
        buff[ index ] = 0;

        if ( sizeof(CHAR) != 1 ) {
           buff[ ++index ] = 0;
        }

        if ( ! QTC_TryToMatchFile( qtc, buff ) ) {
           return( QTC_SUCCESS );
        }
     }
     else {

        // skip over worthless data

        if ( (UINT16)( qtc->search_index +
               (INT)name->size - sizeof(QTC_NAME) ) <= qtc->search_max ) {

           qtc->search_index += (UINT16)(name->size - sizeof( QTC_NAME ));
        }
        else {

           bytes_left = (INT)name->size - sizeof(QTC_NAME) -
                        ( qtc->search_max - qtc->search_index );

           if ( (qtc->search_base + qtc->search_max) >= qtc->search_stop ) {
              return( QTC_NO_MORE );
           }

           qtc->search_base += qtc->search_max;

           qtc->search_max = (UINT16)min( (INT32)QTC_BUF_SIZE,
                                         qtc->search_stop - qtc->search_base );

           QTC_SeekFile( qtc->fh, qtc->search_base );

           qtc->search_max = (UINT16)QTC_ReadFile( qtc->fh, qtc->buff2, (INT)qtc->search_max, &Error );

           if ( qtc->search_max < (UINT16)bytes_left ) {
              return( QTC_NO_MORE );
           }

           qtc->search_index = (UINT16)bytes_left;
        }
     }
  }

  return( QTC_FAILURE );
}


/**********************

  While doing a getfirst/getnext command we ran into the end of media and
  we want to attempt to move on to the next tape and continue with the
  same bset.

***********************/

INT QTC_MoveToNextTapeInFamily( QTC_QUERY_PTR qtc )
{
   QTC_TAPE_PTR tape;
   QTC_BSET_PTR bset;
   QTC_TAPE_PTR best_tape = NULL;
   INT tape_seq_num = 0;

   // was user picky about sequence number ?

   if ( qtc->tape_seq_num != -1 ) {
      return( FAILURE );
   }

   free( qtc->header );
   qtc->header = NULL;

   // close the old file if it's open

   if ( qtc->file_open ) {
      QTC_CloseFile( qtc->fh );
      qtc->file_open = FALSE;
   }

   // Now find a bset that matches it

   tape = QTC_GetFirstTape( );

   while ( tape != NULL ) {

      if ( ( tape->tape_fid == qtc->tape_fid ) &&
           ( tape->tape_seq_num > (INT16)qtc->bset->tape_seq_num ) ) {

         // Find the lowest numbered tape in this family that's higher
         // number than the current tape we are using.

         if ( ( best_tape == NULL ) ||
              ( tape_seq_num > tape->tape_seq_num ) ) {

            tape_seq_num = tape->tape_seq_num;
            best_tape = tape;
         }
      }
      tape = QTC_GetNextTape( tape );
   }

   // We have found another member of the same tape family

   if ( best_tape == NULL ) {
      return( FAILURE );
   }

   // Now see if the desired set continues on to this tape.

   bset = QTC_GetFirstBset( best_tape );

   while ( bset != NULL ) {

      if ( qtc->bset->bset_num == bset->bset_num ) {
         break;
      }

      bset = QTC_GetNextBset( bset );
   }

   // No known continuation bset

   if ( bset == NULL ) {
      return( FAILURE );
   }

   qtc->header = QTC_LoadHeader( bset );

   if ( qtc->header == NULL ) {
      return( QTC_NO_HEADER );
   }

   // Open the file

   qtc->fh = QTC_OpenFile( bset->tape_fid, (INT16)bset->tape_seq_num, FALSE, FALSE );

   if ( qtc->fh < 0 ) {
      return( FAILURE );
   }

   qtc->bset = bset;

   qtc->file_open = TRUE;

   return( SUCCESS );
}

/**********************

 Get us all the entries for a bset

***********************/

INT QTC_GetFirstItem(
QTC_QUERY_PTR qtc )   // I - query structure to use
{

   if ( gb_QTC.inited != QTC_MAGIC_NUMBER ) {
      return( QTC_NO_INIT );
   }

   if ( qtc->file_open ) {
      QTC_CloseFile( qtc->fh );
      qtc->file_open = FALSE;
   }

   free( qtc->header );
   qtc->header = NULL;

   qtc->last_path_size = 0;

   // Now find a bset that matches it

   qtc->bset = QTC_GetBsetForSrch( qtc );

   if ( qtc->bset == NULL ) {
      // So sorry, never heard of that bset
      return( QTC_BSET_NOT_FOUND );
   }

   qtc->header = QTC_LoadHeader( qtc->bset );

   if ( qtc->header == NULL ) {
      return( QTC_NO_HEADER );
   }

   // Open data file for bset

   qtc->fh = QTC_OpenFile( qtc->bset->tape_fid, (INT16)qtc->bset->tape_seq_num, FALSE, FALSE );

   if ( qtc->fh < 0 ) {
      return( QTC_OPEN_FAILED );
   }

   qtc->file_open = TRUE;

   // Set up the record number to return next

   qtc->record_number = 0L;

   return( QTC_GetNextItem( qtc ) );

}

/**********************

   NAME :

   DESCRIPTION :

   RETURNS :

**********************/

INT QTC_GetNextItem(
QTC_QUERY_PTR qtc )    // I - query structure to use
{
   BYTE_PTR s;
   QTC_NAME UNALIGNED * name;
   QTC_RECORD record;
   INT limit;
   INT i, j;
   INT size;
   INT Error;
   INT BytesNeeded;
   UINT32 msw_size = 0L;

   name = (QTC_NAME UNALIGNED *)qtc->buff1;

   if ( ! qtc->file_open ) {
      return( QTC_NO_MORE );
   }

   do {

      if ( qtc->record_number * sizeof( QTC_RECORD ) < qtc->header->rec_size ) {

         QTC_SeekFile( qtc->fh, qtc->header->rec_start + ( qtc->record_number * sizeof( QTC_RECORD ) ) );

         if ( QTC_ReadFile( qtc->fh, (BYTE_PTR)&record, sizeof( QTC_RECORD ), &Error ) == sizeof( QTC_RECORD ) ) {

            if ( record.status & QTC_DIRECTORY ) {

               // read in a directory name item

               QTC_SeekFile( qtc->fh, qtc->header->dir_start + record.name_offset );

               limit = QTC_ReadFile( qtc->fh, qtc->buff2, QTC_BUF_SIZE, &Error );
               limit = (INT)min( (LONG)limit, (LONG)(qtc->header->dir_size - record.name_offset) );
               if ( ! QTC_GetNameFromBuff( qtc->buff2, name, limit ) ) {
                  break;
               }
            }
            else {

               // read in a file name item

               QTC_SeekFile( qtc->fh, qtc->header->fil_start + record.name_offset );

               limit = QTC_ReadFile( qtc->fh, qtc->buff2, QTC_BUF_SIZE, &Error );
               limit = (INT)min( (LONG)limit, (LONG)(qtc->header->fil_size - record.name_offset) );
               if ( ! QTC_GetNameFromBuff( qtc->buff2, name, limit ) ) {
                  break;
               }
            }
         }
      }

      if ( QTC_MoveToNextTapeInFamily( qtc ) != SUCCESS ) {
         return( QTC_NO_MORE );
      }

      // We've changed backup sets, so reset buffer.

      qtc->data_index = 0;
      qtc->data_max = 0;

   } while ( TRUE );

   // next time get the next record

   qtc->record_number++;

   // Fill in results for application layer

   // Item name

   s = (BYTE_PTR)name;
   s += sizeof( QTC_NAME );

   size = (INT)name->size - (INT)name->xtra_size - sizeof( QTC_NAME );

   QTC_TestItemSize( qtc, size + sizeof(CHAR) );
   memcpy( qtc->item, s, size );
   qtc->item[ size / sizeof(CHAR) ] = TEXT( '\0' );

   // Item xtra bytes

   s += name->size - (INT)name->xtra_size - sizeof( QTC_NAME );
   memcpy( qtc->xtra_bytes, s, (INT)name->xtra_size );
   qtc->xtra_size = (INT8)name->xtra_size;

   for ( i = 0; i < qtc->xtra_size; i += 5 ) {
      if ( qtc->xtra_bytes[ i ] == QTC_XTRA_64BIT_SIZE ) {
         memcpy( &msw_size, &qtc->xtra_bytes[ i + 1 ] , 4 );
      }
   }

   // Record data

   qtc->status = (UINT8)record.status;
   qtc->date = (INT16)record.date;
   qtc->time = (INT16)record.time;
   qtc->attrib = record.attribute;
   qtc->lba = record.lba;

   if ( qtc->status & QTC_FILE ) {
      qtc->size = U64_Init( record.common.size, msw_size );
   }
   else {

      qtc->size = U64_Init( record.common.common.height, 0L );

      QTC_TestPathSize( qtc, qtc->last_path_size );
      memcpy( qtc->path, qtc->last_path, qtc->last_path_size );

      i = 1;
      j = 0;

      while ( i < (INT)U64_Lsw( qtc->size ) ) {

         while ( qtc->path[ j++ ] );
         i++;
      }

      qtc->path_size = j * sizeof (CHAR);

      if ( U64_Lsw( qtc->size ) < 2 ) {
         QTC_TestPathSize( qtc, sizeof(CHAR) );
         qtc->path[ 0 ] = TEXT( '\0' );
         qtc->path_size = sizeof(CHAR);
      }

      QTC_TestLastPathSize( qtc, qtc->path_size );
      memcpy( qtc->last_path, qtc->path, qtc->path_size );

      BytesNeeded = strsize( qtc->item ) + (j * sizeof(CHAR));
      QTC_TestLastPathSize( qtc, BytesNeeded );
      strcpy( &qtc->last_path[ j ], qtc->item );

      qtc->last_path_size = BytesNeeded;

      QTC_SetFileCounts( qtc );
   }

   return( QTC_SUCCESS );
}

/**********************

 Get us all the directory entries for a bset

***********************/

INT QTC_GetFirstDir(
QTC_QUERY_PTR qtc )   // I - query structure to use
{

   if ( gb_QTC.inited != QTC_MAGIC_NUMBER ) {
      return( QTC_NO_INIT );
   }

   if ( qtc->file_open ) {
      QTC_CloseFile( qtc->fh );
      qtc->file_open = FALSE;
   }

   free( qtc->header );
   qtc->header = NULL;

   qtc->last_path_size = 0;

   // Now find a bset that matches it

   qtc->bset = QTC_GetBsetForSrch( qtc );

   if ( qtc->bset == NULL ) {
      // So sorry, never heard of that bset
      return( QTC_BSET_NOT_FOUND );
   }

   qtc->header = QTC_LoadHeader( qtc->bset );

   if ( qtc->header == NULL ) {
      return( QTC_NO_HEADER );
   }

   // Open data file for bset

   qtc->fh = QTC_OpenFile( qtc->bset->tape_fid, (INT16)qtc->bset->tape_seq_num, FALSE, FALSE );

   if ( qtc->fh < 0 ) {
      return( QTC_OPEN_FAILED );
   }

   qtc->file_open = TRUE;

   // Set up our buffer stuff

   qtc->dir_offset = qtc->header->dir_start;

   qtc->data_index = 0;
   qtc->data_max = 0;

   return( QTC_GetNextDir( qtc ) );

}

/**********************

   NAME :

   DESCRIPTION :

   RETURNS :

**********************/

INT QTC_GetNextDir(
QTC_QUERY_PTR qtc )    // I - query structure to use
{
   BYTE_PTR s;
   QTC_NAME UNALIGNED * name;
   QTC_RECORD record;
   INT i, j;
   INT size;
   INT BytesNeeded;
   UINT32 msw_size = 0L;


   if ( ! qtc->file_open ) {
      return( QTC_NO_MORE );
   }

   do {

      name = QTC_GetNextItemFromBuffer( qtc, &record, BUFF_DIR );

      if ( name ) {
         break;
      }

      if ( QTC_MoveToNextTapeInFamily( qtc ) != SUCCESS ) {
         return( QTC_NO_MORE );
      }

      // We've changed backup sets, so reset buffer.

      qtc->dir_offset = qtc->header->dir_start;
      qtc->data_index = 0;
      qtc->data_max = 0;

   } while ( TRUE );

   // Fill in results for application layer

   // Item name

   s = (BYTE_PTR)name;
   s += sizeof( QTC_NAME );

   size = (INT)name->size - (INT)name->xtra_size - sizeof( QTC_NAME );

   QTC_TestItemSize( qtc, size + sizeof(CHAR) );
   memcpy( qtc->item, s, size );
   qtc->item[ size / sizeof(CHAR) ] = TEXT( '\0' );

   // Item xtra bytes

   s += name->size - (INT)name->xtra_size - sizeof( QTC_NAME );
   memcpy( qtc->xtra_bytes, s, (INT)name->xtra_size );
   qtc->xtra_size = (INT8)name->xtra_size;

   for ( i = 0; i < qtc->xtra_size; i += 5 ) {
      if ( qtc->xtra_bytes[ i ] == QTC_XTRA_64BIT_SIZE ) {
         memcpy( &msw_size, &qtc->xtra_bytes[ i + 1 ] , 4 );
      }
   }

   // Record data

   qtc->status = (UINT8)record.status;
   qtc->date = (INT16)record.date;
   qtc->time = (INT16)record.time;
   qtc->attrib = record.attribute;
   qtc->size = U64_Init( record.common.common.height, msw_size );
   qtc->lba = record.lba;

   QTC_TestPathSize( qtc, qtc->last_path_size );
   memcpy( qtc->path, qtc->last_path, qtc->last_path_size );

   i = 1;
   j = 0;

   while ( i < (INT)U64_Lsw( qtc->size ) ) {

      while ( qtc->path[ j++ ] );
      i++;
   }

   qtc->path_size = j * sizeof (CHAR);

   if ( U64_Lsw( qtc->size ) < 2 ) {
      QTC_TestPathSize( qtc, sizeof(CHAR) );
      qtc->path[ 0 ] = TEXT( '\0' );
      qtc->path_size = sizeof(CHAR);
   }


   BytesNeeded = strsize( qtc->item ) + ( j * sizeof(CHAR) );
   QTC_TestLastPathSize( qtc, BytesNeeded );
   memcpy( qtc->last_path, qtc->path, qtc->path_size );
   strcpy( &qtc->last_path[ j ], qtc->item );

   qtc->last_path_size = BytesNeeded;

   QTC_SetFileCounts( qtc );
   return( QTC_SUCCESS );
}

/*************

  The data[] buffer contains info from a name file, with a name record
  starting at byte 0.  Limit is the size of good data in the buffer.
  name points to not only a name structure, but also has space for the
  name to be placed in memory after it.  This routine will fill in the
  name structure for you.

***************/


INT QTC_GetNameFromBuff(
BYTE_PTR data,              // I - source of data
QTC_NAME UNALIGNED *name,   // O - name structure to fill in
INT limit )                 // I - how much data is available
{
   if ( limit < sizeof( QTC_NAME ) ) {
      return( QTC_NO_MORE );
   }

   // Copy fixed size of structure

   memcpy( (BYTE *)name, data, sizeof( QTC_NAME ) );

   // Now get variable size of structure from fixed part

   if ( name->size > (INT16)limit ) {
      return( QTC_NO_MORE );
   }

   memcpy( (BYTE *)name, data, (INT)name->size );

   return( QTC_SUCCESS );
}


/**********************

   NAME :

   DESCRIPTION :

   RETURNS :

**********************/

QTC_NAME UNALIGNED * QTC_GetNextItemFromBuffer(
QTC_QUERY_PTR qtc,
QTC_RECORD_PTR record,
INT buff_type )
{
   BOOLEAN buffer_needs_refilling;
   QTC_NAME UNALIGNED *name;
   INT limit;
   INT32 data_left;
   INT Error;

   if ( qtc->data_max >= (INT)sizeof( QTC_NAME ) + qtc->data_index ) {

      name = (QTC_NAME UNALIGNED *)&qtc->buff2[ qtc->data_index ];

      if ( qtc->data_max - qtc->data_index < (INT)name->size ) {

         buffer_needs_refilling = TRUE;
      }

   }
   else {

      buffer_needs_refilling = TRUE;
   }

   if ( buffer_needs_refilling ) {

      buffer_needs_refilling = FALSE;

      name = (QTC_NAME UNALIGNED *)qtc->buff2;

      switch ( buff_type ) {

         case BUFF_DIR:

              qtc->dir_offset += qtc->data_index;

              QTC_SeekFile( qtc->fh, qtc->dir_offset );

              data_left = qtc->header->dir_start + qtc->header->dir_size;
              data_left -= qtc->dir_offset;
              break;

         case BUFF_FILE:
              qtc->fil_offset += qtc->data_index;

              QTC_SeekFile( qtc->fh, qtc->fil_offset );

              data_left = qtc->header->fil_start + qtc->header->fil_size;
              data_left -= qtc->fil_offset;
              break;

         case BUFF_CHILD_DIR:

              qtc->fil_dir_offset += qtc->data_index;

              QTC_SeekFile( qtc->fh, qtc->fil_dir_offset );

              data_left = qtc->header->dir_start + qtc->header->dir_size;
              data_left -= qtc->fil_dir_offset;
              break;

         default:
              return( NULL );

      }

      limit = (INT)min( (INT32)QTC_BUF_SIZE, data_left );

      qtc->data_max = QTC_ReadFile( qtc->fh, qtc->buff2, limit, &Error );

      if ( qtc->data_max == -1 ) {

         // Read error
         return( NULL );
      }

      qtc->data_index = 0;

      if ( qtc->data_max < sizeof( QTC_NAME ) ) {
         buffer_needs_refilling = TRUE;
      }
      else {

         if ( qtc->data_max - qtc->data_index < (INT)name->size ) {

            buffer_needs_refilling = TRUE;
         }
      }
   }

   if ( ! buffer_needs_refilling ) {

      // We have enough good data to return a solution.

      QTC_SeekFile( qtc->fh, qtc->header->rec_start + (name->record * sizeof( QTC_RECORD)) );

      if ( QTC_ReadFile( qtc->fh, (BYTE_PTR)record, sizeof( QTC_RECORD ), &Error ) != sizeof( QTC_RECORD ) ) {

         return( NULL );
      }

      qtc->data_index += (INT)name->size;    // bump our buffer index

   }
   else {
      return( NULL );
   }

   return( (QTC_NAME UNALIGNED *)name );
}

// if we do find a duplicate directory path after all the files in the
// current directory are returned, then we set everything up for the
// next call and return QTC_TRY_AGAIN.

INT QTC_GetFirstObj(
QTC_QUERY_PTR qtc )         // I - query structure to use
{
   QTC_RECORD record;
   QTC_TAPE_PTR tape;
   QTC_BSET_PTR temp_bset;

   if ( gb_QTC.inited != QTC_MAGIC_NUMBER ) {
      return( QTC_NO_INIT );
   }

   if ( qtc->file_open ) {
      QTC_CloseFile( qtc->fh );
      qtc->file_open = FALSE;
   }
   // Now find a bset that matches it

   tape = QTC_GetFirstTape( );

   temp_bset = NULL;
   qtc->bset = NULL;

   while ( tape != NULL ) {

      if ( ( tape->tape_fid == qtc->tape_fid ) &&
           ( tape->tape_seq_num == qtc->tape_seq_num ) ) {

          qtc->bset = QTC_GetFirstBset( tape );

          while ( qtc->bset != NULL ) {

             if ( qtc->bset->bset_num == qtc->bset_num ) {
                if ( ! ( qtc->bset->status & ( QTC_PARTIAL | QTC_IMAGE ) ) ) {
                   break;
                }
             }
             qtc->bset = QTC_GetNextBset( qtc->bset );
          }
          break;
      }
      else {

         if ( ( tape->tape_fid == qtc->tape_fid ) &&
              ( qtc->tape_seq_num == -1 ) ) {

             temp_bset = QTC_GetFirstBset( tape );

             while ( temp_bset != NULL ) {

                if ( temp_bset->bset_num == qtc->bset_num ) {

                   if ( ! ( temp_bset->status & ( QTC_PARTIAL | QTC_IMAGE ) ) ) {
                      if ( qtc->bset == NULL ) {
                         qtc->bset = temp_bset;
                      }

                      if ( qtc->bset->tape_seq_num > temp_bset->tape_seq_num ) {

                         qtc->bset = temp_bset;
                      }
                   }
                }
                temp_bset = QTC_GetNextBset( temp_bset );
             }
         }
      }

      tape = QTC_GetNextTape( tape );
   }

   if ( qtc->bset == NULL ) {
      return( QTC_BSET_NOT_FOUND );
   }

   qtc->header = QTC_LoadHeader( qtc->bset );
   if ( qtc->header == NULL ) {
      return( QTC_NO_HEADER );
   }

   qtc->fh = QTC_OpenFile( qtc->bset->tape_fid, (INT16)qtc->bset->tape_seq_num, FALSE, FALSE );

   if ( qtc->fh < 0 ) {
      return( QTC_OPEN_FAILED );
   }

   // Copy search path to reply path, it won't change

   QTC_TestPathSize( qtc, qtc->search_path_size );
   memcpy( qtc->path, qtc->search_path, qtc->search_path_size );
   qtc->path_size = qtc->search_path_size;

   qtc->file_open = TRUE;

   // Attempt to find the requested path on all the tapes this
   // bset is on.

   do {

      qtc->fil_dir_offset = 0;

      if ( QTC_FindDirRec( qtc, &record ) == SUCCESS ) {

         // We found the path, set up for FindNextObj.

         qtc->fil_offset = record.common.common.file_start + qtc->header->fil_start;
         qtc->curr_mom_offset = qtc->header->dir_start + record.name_offset;

         qtc->data_index = 0;
         qtc->data_max = 0;

         return( QTC_GetNextObj( qtc ) );
      }

   } while ( QTC_MoveToNextTapeInFamily( qtc ) == SUCCESS );

   // Requested path doesn't exist in catalogs.


   return( QTC_NO_MORE );
}


/**********************

   NAME :

   DESCRIPTION :

   RETURNS :

**********************/

INT QTC_GetNextObj(
QTC_QUERY_PTR qtc )      // I - query structure to use
{
  BOOLEAN done = FALSE;
  QTC_RECORD record;


  if ( ! qtc->file_open ) {
     return( QTC_NO_MORE );
  }

  while ( ! done ) {

     if ( QTC_TryToLocateFile( qtc ) == SUCCESS ) {
        QTC_SetFileCounts( qtc );
        return( SUCCESS );
     }

     if ( QTC_LookForChildDirs( qtc ) == SUCCESS ) {
        QTC_SetFileCounts( qtc );
        return( SUCCESS );
     }

     // Try for a duplicate directory.

     qtc->fil_dir_offset = 0;

     if ( QTC_FindNextDirRec( qtc, &record ) == SUCCESS ) {

        // We found a new duplicate path, go to top loop again.

        // Set up for FindNextObj.

        qtc->fil_offset = record.common.common.file_start + qtc->header->fil_start;
        qtc->curr_mom_offset = qtc->header->dir_start + record.name_offset;

        qtc->data_index = 0;
        qtc->data_max = 0;

        continue;
     }

     // Everything has failed on this tape, try for a new one.

     done = TRUE;

     while ( QTC_MoveToNextTapeInFamily( qtc ) == SUCCESS ) {

        qtc->fil_dir_offset = 0;

        if ( QTC_FindDirRec( qtc, &record ) == SUCCESS ) {

           // We found a new duplicate path, go to top loop again.

           // Set up for FindNextObj.

           qtc->fil_offset = record.common.common.file_start + qtc->header->fil_start;
           qtc->curr_mom_offset = qtc->header->dir_start + record.name_offset;

           qtc->data_index = 0;
           qtc->data_max = 0;

           done = FALSE;
           break;
        }
     }

  }


  return( QTC_NO_MORE );
}


/**********************

   NAME :

   DESCRIPTION :

   RETURNS :

**********************/

INT QTC_TryToLocateFile(
QTC_QUERY_PTR qtc )
{
   INT bytes;
   INT i;
   UINT32 msw_size = 0L;
   BYTE_PTR s;
   QTC_NAME UNALIGNED * name;
   QTC_RECORD record;


   do {

      if ( qtc->fil_dir_offset ) {
         return( FAILURE );
      }

      name = QTC_GetNextItemFromBuffer( qtc, &record, BUFF_FILE );

      // No more files

      if ( name == NULL ) {
         return( FAILURE );
      }

      // No more files in this directory

      if ( (qtc->header->dir_start + name->mom_offset) != qtc->curr_mom_offset ) {
         return( FAILURE );
      }

      s = (BYTE_PTR)name;
      s += sizeof( QTC_NAME );

      // Item name

      bytes = (INT)name->size - (INT)name->xtra_size - sizeof( QTC_NAME );

      QTC_TestItemSize( qtc, bytes + 1 );
      memcpy( qtc->item, s, bytes );
      qtc->item[ bytes/sizeof (CHAR) ] = TEXT( '\0' );

   } while ( QTC_TryToMatchFile( qtc, (BYTE_PTR)qtc->item ) );

   // Item xtra bytes

   s += name->size - (INT)name->xtra_size - sizeof( QTC_NAME );
   memcpy( qtc->xtra_bytes, s, (INT)name->xtra_size );
   qtc->xtra_size = (UINT8)name->xtra_size;

   for ( i = 0; i < qtc->xtra_size; i += 5 ) {
      if ( qtc->xtra_bytes[ i ] == QTC_XTRA_64BIT_SIZE ) {
         memcpy( &msw_size, &qtc->xtra_bytes[ i + 1 ] , 4 );
      }
   }

   // Record data

   qtc->status = (UINT8)record.status;
   qtc->date = (INT16)record.date;
   qtc->time = (INT16)record.time;
   qtc->attrib = record.attribute;
   qtc->size = U64_Init( record.common.size, msw_size );
   qtc->lba = record.lba;

   QTC_SetFileCounts( qtc );

   return( SUCCESS );
}


/**********************

   NAME :

   DESCRIPTION :

   Given the qtc->curr_mom_offset, look for any children directories, using
   qtc->fil_dir_offset as a base and stop at bset->dir_start + bset->dir_size;

********************/

INT QTC_LookForChildDirs(
QTC_QUERY_PTR qtc )
{
   QTC_RECORD record;
   QTC_NAME UNALIGNED *name;
   BYTE_PTR s;
   UINT32 msw_size = 0L;
   INT ret;
   INT i;
   INT bytes;
   INT Error;


   name = ( QTC_NAME UNALIGNED *)qtc->buff1;

   if ( qtc->fil_dir_offset == 0 ) {

      qtc->fil_dir_offset = qtc->curr_mom_offset;
      qtc->data_index = 0;
      qtc->data_max = 0;

      /** skip over parent **/

      QTC_SeekFile( qtc->fh, qtc->fil_dir_offset );

      ret = QTC_ReadFile( qtc->fh, (BYTE_PTR)name, sizeof( QTC_NAME), &Error );

      if ( ret != sizeof( QTC_NAME ) ) {
         return( FAILURE );
      }

      qtc->fil_dir_offset += name->size;
   }

   do {

      name = QTC_GetNextItemFromBuffer( qtc, &record, BUFF_CHILD_DIR );

      if ( name == NULL ) {
         return( FAILURE );
      }

      // Once we find an older parent's kids,
      // we will never hit any more of this guys.

      if ( qtc->header->dir_start + name->mom_offset < qtc->curr_mom_offset ) {
         return( FAILURE );
      }

      // Keep skipping over younger children.

   } while ( qtc->header->dir_start + name->mom_offset != qtc->curr_mom_offset );

   // Item name

   s = (BYTE_PTR)name;
   s += sizeof( QTC_NAME );
   bytes = (INT)name->size - (INT)name->xtra_size - sizeof( QTC_NAME );
   QTC_TestItemSize( qtc, bytes + 1 );
   memcpy( qtc->item, s, bytes );
   qtc->item[ bytes / sizeof (CHAR) ] = TEXT( '\0' );

   // Item xtra bytes

   s += name->size - (INT)name->xtra_size - sizeof( QTC_NAME );
   memcpy( qtc->xtra_bytes, s, (INT)name->xtra_size );
   qtc->xtra_size = (INT8)name->xtra_size;

   for ( i = 0; i < qtc->xtra_size; i += 5 ) {
      if ( qtc->xtra_bytes[ i ] == QTC_XTRA_64BIT_SIZE ) {
         memcpy( &msw_size, &qtc->xtra_bytes[ i + 1 ] , 4 );
      }
   }

   // Record data

   qtc->status = (UINT8)record.status;
   qtc->date = (INT16)record.date;
   qtc->time = (INT16)record.time;
   qtc->attrib = record.attribute;
   qtc->size = U64_Init( record.common.common.height, msw_size );
   qtc->lba = record.lba;
   QTC_SetFileCounts( qtc );

   return( SUCCESS );

}




/**************

  Given the path in qtc fill in the record structure for the last
  subdirectory in the path.
  Also fill in the dir_rec_offsets[] array;

  ex. \DOS\GAMES\TETRIS

  fills in the record structure for TETRIS.

****************/

INT QTC_FindDirRec(
QTC_QUERY_PTR qtc,          // I - use the path in this structure
QTC_RECORD_PTR record )     // O - the record for the deepest dir in path
{
   // start looking at the start of the directory data

   return( QTC_FindDirectoryPath( qtc, record, qtc->header->dir_start, TRUE ) );
}


/**********************

   NAME :

   DESCRIPTION :

   We have been asked to find yet another matching path. The success of
   this function call is dependent on there being duplicate directories
   in the catalog.  This DOES happen with the \SYSTEM directory on
   NOVELL servers if security stuff is backed up.

   qtc->curr_mom_offset is the current directory tail.

   RETURNS :

**********************/

INT QTC_FindNextDirRec(
QTC_QUERY_PTR qtc,          // I - use the path in this structure
QTC_RECORD_PTR record )     // O - the record for the deepest dir in path
{
   UINT32 curr_try_offset;     // current offset in the directory names
   QTC_NAME UNALIGNED * name;
   INT Error;

   name = (QTC_NAME UNALIGNED *)qtc->buff1;

   // Determine the offset we wish to find a child from. Use the mom
   // offset of the current directory at qtc->curr_mom_offset.

   QTC_SeekFile( qtc->fh, qtc->curr_mom_offset );

   if ( QTC_ReadFile( qtc->fh, (BYTE_PTR)name, sizeof( QTC_NAME ), &Error ) != sizeof( QTC_NAME ) ) {
      return( FAILURE );
   }

   // Determine the offset in the directory data to start looking at.
   // It should be the entry immediately after qtc->curr_mom_offset.

   curr_try_offset = qtc->curr_mom_offset + name->size;

   return( QTC_FindDirectoryPath( qtc, record, curr_try_offset, FALSE ) );
}



/**********************

   NAME :

   QTC_FindDirectoryPath

   DESCRIPTION :

   Called by QTC_FindDirRec() and QTC_FindNextDirRec() to find out if
   a path exists in the current backup set. It returns the record for
   the last directory entry in the path, if successful.

   RETURNS :

   SUCCESS/FAILURE

**********************/

static INT  QTC_FindDirectoryPath(
QTC_QUERY_PTR qtc,          // I - use the path in this structure
QTC_RECORD_PTR record,      // O - the record for the deepest dir in path
UINT32 curr_try_offset,
INT start_at_root )
{
   QTC_NAME UNALIGNED *name;
   INT current_level;
   INT final_level;
   INT new_level;
   INT i;
   INT Error;
   INT init = TRUE;
   CHAR *dir_name;
   BYTE *s;
   UINT32 dir_max;
   UINT32 offset;

   name = (QTC_NAME UNALIGNED *)qtc->buff1;

   // Stop looking at the end of the directory data

   dir_max = qtc->header->dir_start + qtc->header->dir_size;

   // Uuse buff1 to store the temp results in

   name = ( QTC_NAME *)qtc->buff1;

   s = (BYTE_PTR)name;
   s += sizeof( QTC_NAME );

   // Determine depth of this path

   final_level = 1;

   if ( qtc->search_path_size > sizeof(CHAR) ) {
      for ( i = 0; i < (INT)( qtc->search_path_size / sizeof(CHAR) ); i++ ) {
          if ( qtc->search_path[i] == TEXT('\0') ) {
             final_level++;
          }
      }
   }

   // example:
   // for \, final_level = 1
   // for \dos\games\tetris, final_level = 4


   // current_level is the maximum level for which a solution directory
   // is valid, we may come across a directory at level 6, that
   // is an exact match for the level 6 name we are looking for,
   // but if we are currently only working at level 2, then this
   // new entry has the wrong parent for us to use it.

   if ( start_at_root ) {
      current_level = 0;
   }
   else {
      current_level = final_level - 1;
   }

   do {

      // Look for name with mom_offset == last_offset

      if ( QTC_FastSearchForDir( qtc, &curr_try_offset,
                                 dir_max, 0, init ) ) {

         // A normal return, we ran out of namespace.
         return( FAILURE );
      }

      // Only init once

      if ( init ) {
         init = FALSE;
      }

      // See if the directory found matches the name and
      // height we are looking for.

      // Load the record for this directory. Notice that if this is the
      // solution directory, than we have loaded it here to return it.

      offset = qtc->header->rec_start;
      offset += ( name->record * sizeof( QTC_RECORD ) );
      QTC_SeekFile( qtc->fh, offset );

      if ( QTC_ReadFile( qtc->fh, (BYTE_PTR)record, sizeof( QTC_RECORD ), &Error ) != sizeof( QTC_RECORD ) ) {
         return( FAILURE );
      }

      new_level = (INT)record->common.common.height;

      if ( new_level <= current_level ) {

         // Get the name we are looking for at this level.

         dir_name = TEXT("");   // find the root first
         if ( new_level != 0 ) {
            dir_name = qtc->search_path;
            for ( i = 1; i < new_level; i++ ) {
               while ( *dir_name++ );
            }
         }

         // Initialize a pointer to this new entry's name.

         ((CHAR_PTR)s)[ ((INT)name->size - (INT)name->xtra_size - sizeof( QTC_NAME ))/ sizeof (CHAR) ] = TEXT ('\0');

         if ( ! stricmp( (CHAR_PTR)s, dir_name ) ) {

            // We matched at this level, move down one deeper.

            current_level = new_level + 1;
         }
         else {
            current_level = new_level;
         }

      }

      // We are done as soon as this we need to look one deeper than
      // what our goal was.

   } while ( current_level < final_level );

   return( SUCCESS );

}



/**********************

   NAME :

   DESCRIPTION :

  Starting at offset in the directory name file, this routine will look
  for an entry with size == requested_size.  Max is the ending offset
  for data to be searched, usually the end of this BSET. Init is TRUE
  if this is the first call.  The name structure will
  be filled in with the answer for you.  If requested_size == 0, it
  returns the first one found.

******************/

INT QTC_FastSearchForDir(
QTC_QUERY_PTR qtc,         // I - query structure to use
UINT32_PTR offset,         // I/O - file offset to start/finished at
UINT32 max,                // I - how far from offset can we look
INT  requested_size,     // I - size of requested name
INT  init )              // I - is this the first call
{
  UINT bytes_left;
  INT Error;
  QTC_NAME UNALIGNED * name;
  BYTE_PTR s;


  name = (QTC_NAME UNALIGNED *)qtc->buff1;

  if ( init ) {
     qtc->search_index = 0;
     qtc->search_max = 0;
     qtc->search_base = *offset;
     QTC_SeekFile( qtc->fh, *offset );
  }

  /** find dir name in dir names **/

  while ( TRUE ) {

     *offset = qtc->search_base + (UINT32)qtc->search_index;

     s = (BYTE_PTR)name;

     // copy structure

     if ( qtc->search_index + sizeof( QTC_NAME ) <= qtc->search_max ) {

        memcpy( s, &(qtc->buff2[ qtc->search_index ]), sizeof( QTC_NAME ) );
        qtc->search_index += sizeof( QTC_NAME );
     }
     else {

        // Slow copy to handle buffer break

        memcpy( s,
                &(qtc->buff2[ qtc->search_index ]),
                qtc->search_max - qtc->search_index );


        s += qtc->search_max - qtc->search_index;

        bytes_left = sizeof(QTC_NAME) - ( qtc->search_max - qtc->search_index );

        if ( (qtc->search_base + qtc->search_max) >= max ) {
           return( QTC_NO_MORE );
        }

        qtc->search_base += qtc->search_max;

        qtc->search_max = (UINT16)min( (INT32)QTC_BUF_SIZE, max - qtc->search_base );

        QTC_SeekFile( qtc->fh, qtc->search_base );

        qtc->search_max = (UINT16)QTC_ReadFile( qtc->fh, qtc->buff2, (INT)qtc->search_max, &Error );

        if ( qtc->search_max < (UINT16)bytes_left ) {
           return( QTC_NO_MORE );
        }

        memcpy( s, &(qtc->buff2[ 0 ]), bytes_left );

        qtc->search_index = (UINT16)bytes_left;

        s = (BYTE_PTR)name;
     }

     if ( ( requested_size == (INT)name->size - (INT)name->xtra_size ) ||
          ( requested_size == 0 ) ) {

        // Copy variable sized name part,
        // we are going to return this entry.

        s += sizeof( QTC_NAME );

        if ( ( qtc->search_index +
               (INT16)name->size - sizeof(QTC_NAME) ) <= qtc->search_max ) {

           memcpy( s,
                   &(qtc->buff2[ qtc->search_index ]),
                   (INT)name->size - sizeof( QTC_NAME ) );

           qtc->search_index += (UINT16)(name->size - sizeof( QTC_NAME ));
        }
        else {

           // Slow copy to handle buffer break

           memcpy( s,
                   &(qtc->buff2[ qtc->search_index ]),
                   (INT)name->size - sizeof(QTC_NAME) );

           s += qtc->search_max - qtc->search_index;

           bytes_left = (INT)name->size - sizeof(QTC_NAME) - ( qtc->search_max - qtc->search_index );

           qtc->search_base += qtc->search_max;

           if ( qtc->search_base >= max ) {
              return( QTC_NO_MORE );
           }

           qtc->search_max = (UINT16)min( (INT32)QTC_BUF_SIZE, max - qtc->search_base );

           QTC_SeekFile( qtc->fh, qtc->search_base );

           qtc->search_max = (UINT16)QTC_ReadFile( qtc->fh, qtc->buff2, (INT)qtc->search_max, &Error );

           if ( qtc->search_max < (UINT16)bytes_left ) {
              return( QTC_NO_MORE );
           }

           memcpy( s,
                   &(qtc->buff2[ 0 ]),
                   bytes_left );

           qtc->search_index = (UINT16)bytes_left;

        }

        return( QTC_SUCCESS );
     }
     else {

        // skip worthless data

        if ( (UINT16)( qtc->search_index + (INT)name->size - sizeof(QTC_NAME) ) <= qtc->search_max ) {

           qtc->search_index += (UINT16)(name->size - sizeof( QTC_NAME ));
        }
        else {

           bytes_left = (INT)name->size - sizeof(QTC_NAME) - ( qtc->search_max - qtc->search_index );

           qtc->search_base += qtc->search_max;

           if ( qtc->search_base >= max ) {
              return( QTC_NO_MORE );
           }

           qtc->search_max = (UINT16)min( (INT32)QTC_BUF_SIZE, max - qtc->search_base );

           QTC_SeekFile( qtc->fh, qtc->search_base );

           qtc->search_max = (UINT16)QTC_ReadFile( qtc->fh, qtc->buff2, (INT)qtc->search_max, &Error );

           if ( qtc->search_max < (UINT16)bytes_left ) {
              return( QTC_NO_MORE );
           }

           qtc->search_index = (UINT16)bytes_left;
        }

     }

  }

  return( QTC_FAILURE );
}


/*
   Here's how this works, to keep up the speed of searches, we created
   two copies of the name comparison code.  One if the bset is ascii,
   and the other if the bset is unicode.  We have two search name fields
   already filled in. A one time conversion, to last the entire search.
*/

INT QTC_TryToMatchFile(
QTC_QUERY_PTR qtc,
BYTE_PTR file_name )
{
  INT ret = -1;

#ifdef NO_UNICODE

  // The UI has no desire to support anything having to do with unicode.

  if ( ! ( qtc->bset->status & QTC_UNICODE ) ) {
     ret = QTC_CompNormalNames( (CHAR_PTR)qtc->search_name,
                                (CHAR_PTR)file_name );
  }

#else

  if ( gb_QTC.unicode ) {
     ret = QTC_CompUnicodeNames( (WCHAR_PTR)qtc->search_name,
                                 (WCHAR_PTR)file_name );
  }
  else {
     ret = QTC_CompAsciiNames( (ACHAR_PTR)qtc->search_name,
                               (ACHAR_PTR)file_name );
  }
#endif

  return( ret );
}

#if defined(NO_UNICODE)

INT QTC_CompNormalNames( CHAR_PTR srch_name, CHAR_PTR file_name )
{
  INT pos = 0;                  /* index for file_name */
  INT i;                        /* index for wild_name */
  INT ret_val = 0;
  CHAR_PTR p;
  CHAR save_char;
  INT ptrn_len;

  pos = 0;

  if ( strcmp( srch_name, "*.*" ) ) {

     for ( i = 0; (srch_name[i] != 0) && (ret_val == 0); i++ ) {

        switch( srch_name[i] ) {

        case '*':

             while ( srch_name[i+1] != 0 ) {

                if ( srch_name[i+1] == '?' ) {
                   if ( file_name[ ++pos ] == 0 ) {
                      break;
                   }

                }
                else {
                   if ( srch_name[i+1] != '*' ) {
                     break;
                   }
                }
                i++;
             }

             p = strpbrk( &srch_name[i+1], TEXT ("*?") );

             if ( p != NULL ) {
                save_char = *p;
                *p = 0;

                ptrn_len = strlen( &srch_name[i+1] );

                while ( file_name[pos] &&
                     strnicmp( &file_name[pos], &srch_name[i+1], ptrn_len ) ) {
                     pos++;
                }

                i += ptrn_len;

                *p = save_char;

                if ( file_name[pos] == 0 ) {
                     ret_val = -1;
                } else {
                     pos++;
                }
             }
             else {
                if ( srch_name[i+1] == 0 ) {
                   pos = strlen( file_name );
                   break;
                }
                else {

                   p = strchr( &file_name[pos], srch_name[i+1] );
                   if ( p != NULL ) {
                      pos += p - &file_name[pos];
                   }
                   else {
                      ret_val = -1;
                   }
                }
             }
             break;

        case '?' :
             if ( file_name[pos] != 0 ) {
                pos++;
             }
             break;

        default:
             if ( ( file_name[pos] == 0 ) ||
                  ( toupper(file_name[pos]) != toupper(srch_name[i]) ) ){
                ret_val = -1;
             }
             else {
                pos++;
             }
        }
     }

     if ( file_name[pos] != 0 ) {
          ret_val = -1;
     }
  }

  return( ret_val );
}

#else

INT QTC_CompAsciiNames( ACHAR_PTR srch_name, ACHAR_PTR file_name )
{
  INT pos = 0;                   /* index for file_name */
  INT i;                        /* index for wild_name */
  INT ret_val = 0;
  ACHAR_PTR p;
  ACHAR save_char;
  INT ptrn_len;

  pos = 0;

  if ( strcmpA( srch_name, "*.*" ) ) {

     for ( i = 0; (srch_name[i] != 0) && (ret_val == 0); i++ ) {

        switch( srch_name[i] ) {

        case '*':

             while ( srch_name[i+1] != 0 ) {

                if ( srch_name[i+1] == '?' ) {
                   if ( file_name[ ++pos ] == 0 ) {
                      break;
                   }

                }
                else {
                   if ( srch_name[i+1] != '*' ) {
                     break;
                   }
                }
                i++;
             }

             p = strpbrkA( &srch_name[i+1], "*?" );

             if ( p != NULL ) {
                save_char = *p;
                *p = 0;

                ptrn_len = strlenA( &srch_name[i+1] );

                while ( file_name[pos] &&
                     strnicmpA( &file_name[pos], &srch_name[i+1], ptrn_len ) ) {
                     pos++;
                }

                i += ptrn_len;

                *p = save_char;

                if ( file_name[pos] == 0 ) {
                     ret_val = -1;
                } else {
                     pos++;
                }
             }
             else {
                if ( srch_name[i+1] == 0 ) {
                   pos = strlenA( file_name );
                   break;
                }
                else {

                   p = strchrA( &file_name[pos], srch_name[i+1] );
                   if ( p != NULL ) {
                      pos += p - &file_name[pos];
                   }
                   else {
                      ret_val = -1;
                   }
                }
             }
             break;

        case '?' :
             if ( file_name[pos] != 0 ) {
                pos++;
             }
             break;

        default:
             if ( ( file_name[pos] == 0 ) ||
                  ( toupper(file_name[pos]) != toupper(srch_name[i]) ) ){
                ret_val = -1;
             }
             else {
                pos++;
             }
        }
     }

     if ( file_name[pos] != 0 ) {
          ret_val = -1;
     }
  }

  return( ret_val );
}



INT QTC_CompUnicodeNames( WCHAR_PTR srch_name, WCHAR_PTR file_name )
{
#ifdef UNICODE

  INT pos = 0;                   /* index for file_name */
  INT i;                        /* index for wild_name */
  INT ret_val = 0;
  WCHAR_PTR p;
  WCHAR save_char;
  WCHAR stardotstar[ 4 ];
  WCHAR starquestion[ 4 ];
  INT ptrn_len;

  strcpy( stardotstar, TEXT( "*.*" ) );

  strcpy( starquestion, TEXT( "*?" ) );

  pos = 0;

  if ( wcscmp( srch_name, stardotstar ) ) {

     for ( i = 0; (srch_name[i] != TEXT('\0')) && (ret_val == 0); i++ ) {

        switch ( srch_name[i] ) {

        case TEXT('*'):

             while ( srch_name[i+1] != TEXT('\0') ) {

                if ( srch_name[i+1] == TEXT('?') ) {
                   if ( file_name[ ++pos ] == TEXT('\0') ) {
                      break;
                   }

                }
                else {
                   if ( srch_name[i+1] != TEXT('*') ) {
                     break;
                   }
                }
                i++;
             }

             p = wcspbrk( &srch_name[i+1], starquestion );

             if ( p != NULL ) {
                save_char = *p;
                *p = TEXT('0');

                ptrn_len = wcslen( &srch_name[i+1] );

                while ( file_name[pos] &&
                     wcsnicmp( &file_name[pos], &srch_name[i+1], ptrn_len ) ) {
                     pos++;
                }

                i += ptrn_len;

                *p = save_char;

                if ( file_name[pos] == TEXT('\0') ) {
                     ret_val = -1;
                } else {
                     pos++;
                }
             }
             else {
                if ( srch_name[i+1] == TEXT('\0') ) {
                   pos = wcslen( file_name );
                   break;
                }
                else {

                   p = wcschr( &file_name[pos], srch_name[i+1] );
                   if ( p != NULL ) {
                      pos += p - &file_name[pos];
                   }
                   else {
                      ret_val = -1;
                   }
                }
             }
             break;

        case TEXT('?') :
             if ( file_name[pos] != TEXT('\0') ) {
                pos++;
             }
             break;

        default:
             if ( ( file_name[pos] == TEXT('\0') ) ||
                  ( toupper(file_name[pos]) != toupper(srch_name[i]) ) ){
                ret_val = -1;
             }
             else {
                pos++;
             }
        }
     }

     if ( file_name[pos] != TEXT( '\0' ) ) {
          ret_val = -1;
     }
  }
  return( ret_val );

#endif
  return( 0 );
}

#endif