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
|
/* $Id: RTPathGlob.cpp $ */
/** @file
* IPRT - RTPathGlob
*/
/*
* Copyright (C) 2006-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include "internal/iprt.h"
#include <iprt/path.h>
#include <iprt/asm.h>
#include <iprt/assert.h>
#include <iprt/buildconfig.h>
#include <iprt/ctype.h>
#include <iprt/dir.h>
#include <iprt/env.h>
#include <iprt/err.h>
#include <iprt/mem.h>
#include <iprt/string.h>
#include <iprt/uni.h>
#if defined(RT_OS_WINDOWS)
# include <iprt/utf16.h>
# include <iprt/win/windows.h>
# include "../../r3/win/internal-r3-win.h"
#elif defined(RT_OS_OS2)
# define INCL_BASE
# include <os2.h>
# undef RT_MAX /* collision */
#endif
/*********************************************************************************************************************************
* Defined Constants And Macros *
*********************************************************************************************************************************/
/** Maximum number of results. */
#define RTPATHGLOB_MAX_RESULTS _32K
/** Maximum number of zero-or-more wildcards in a pattern.
* This limits stack usage and recursion depth, as well as execution time. */
#define RTPATHMATCH_MAX_ZERO_OR_MORE 24
/** Maximum number of variable items. */
#define RTPATHMATCH_MAX_VAR_ITEMS _4K
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
/**
* Matching operation.
*/
typedef enum RTPATHMATCHOP
{
RTPATHMATCHOP_INVALID = 0,
/** EOS: Returns a match if at end of string. */
RTPATHMATCHOP_RETURN_MATCH_IF_AT_END,
/** Asterisk: Returns a match (trailing asterisk). */
RTPATHMATCHOP_RETURN_MATCH,
/** Asterisk: Returns a match (just asterisk), unless it's '.' or '..'. */
RTPATHMATCHOP_RETURN_MATCH_EXCEPT_DOT_AND_DOTDOT,
/** Plain text: Case sensitive string compare. */
RTPATHMATCHOP_STRCMP,
/** Plain text: Case insensitive string compare. */
RTPATHMATCHOP_STRICMP,
/** Question marks: Skips exactly one code point. */
RTPATHMATCHOP_SKIP_ONE_CODEPOINT,
/** Question marks: Skips exactly RTPATHMATCHCORE::cch code points. */
RTPATHMATCHOP_SKIP_MULTIPLE_CODEPOINTS,
/** Char set: Requires the next codepoint to be in the ASCII-7 set defined by
* RTPATHMATCHCORE::pch & RTPATHMATCHCORE::cch. No ranges. */
RTPATHMATCHOP_CODEPOINT_IN_SET_ASCII7,
/** Char set: Requires the next codepoint to not be in the ASCII-7 set defined
* by RTPATHMATCHCORE::pch & RTPATHMATCHCORE::cch. No ranges. */
RTPATHMATCHOP_CODEPOINT_NOT_IN_SET_ASCII7,
/** Char set: Requires the next codepoint to be in the extended set defined by
* RTPATHMATCHCORE::pch & RTPATHMATCHCORE::cch. Ranges, UTF-8. */
RTPATHMATCHOP_CODEPOINT_IN_SET_EXTENDED,
/** Char set: Requires the next codepoint to not be in the extended set defined
* by RTPATHMATCHCORE::pch & RTPATHMATCHCORE::cch. Ranges, UTF-8. */
RTPATHMATCHOP_CODEPOINT_NOT_IN_SET_EXTENDED,
/** Variable: Case sensitive variable value compare, RTPATHMATCHCORE::uOp2 is
* the variable table index. */
RTPATHMATCHOP_VARIABLE_VALUE_CMP,
/** Variable: Case insensitive variable value compare, RTPATHMATCHCORE::uOp2 is
* the variable table index. */
RTPATHMATCHOP_VARIABLE_VALUE_ICMP,
/** Asterisk: Match zero or more code points, there must be at least
* RTPATHMATCHCORE::cch code points after it. */
RTPATHMATCHOP_ZERO_OR_MORE,
/** Asterisk: Match zero or more code points, there must be at least
* RTPATHMATCHCORE::cch code points after it, unless it's '.' or '..'. */
RTPATHMATCHOP_ZERO_OR_MORE_EXCEPT_DOT_AND_DOTDOT,
/** End of valid operations. */
RTPATHMATCHOP_END
} RTPATHMATCHOP;
/**
* Matching instruction.
*/
typedef struct RTPATHMATCHCORE
{
/** The action to take. */
RTPATHMATCHOP enmOpCode;
/** Generic value operand. */
uint16_t uOp2;
/** Generic length operand. */
uint16_t cch;
/** Generic string pointer operand. */
const char *pch;
} RTPATHMATCHCORE;
/** Pointer to a matching instruction. */
typedef RTPATHMATCHCORE *PRTPATHMATCHCORE;
/** Pointer to a const matching instruction. */
typedef RTPATHMATCHCORE const *PCRTPATHMATCHCORE;
/**
* Path matching instruction allocator.
*/
typedef struct RTPATHMATCHALLOC
{
/** Allocated array of instructions. */
PRTPATHMATCHCORE paInstructions;
/** Index of the next free entry in paScratch. */
uint32_t iNext;
/** Number of instructions allocated. */
uint32_t cAllocated;
} RTPATHMATCHALLOC;
/** Pointer to a matching instruction allocator. */
typedef RTPATHMATCHALLOC *PRTPATHMATCHALLOC;
/**
* Path matching cache, mainly intended for variables like the PATH.
*/
typedef struct RTPATHMATCHCACHE
{
/** @todo optimize later. */
uint32_t iNothingYet;
} RTPATHMATCHCACHE;
/** Pointer to a path matching cache. */
typedef RTPATHMATCHCACHE *PRTPATHMATCHCACHE;
/** Parsed path entry.*/
typedef struct RTPATHGLOBPPE
{
/** Normal: Index into RTPATHGLOB::MatchInstrAlloc.paInstructions. */
uint32_t iMatchProg : 16;
/** Set if this is a normal entry which is matched using iMatchProg. */
uint32_t fNormal : 1;
/** !fNormal: Plain name that can be dealt with using without
* enumerating the whole directory, unless of course the file system is case
* sensitive and the globbing isn't (that needs figuring out on a per
* directory basis). */
uint32_t fPlain : 1;
/** !fNormal: Match zero or more subdirectories. */
uint32_t fStarStar : 1;
/** !fNormal: The whole component is a variable expansion. */
uint32_t fExpVariable : 1;
/** Filter: Set if it only matches directories. */
uint32_t fDir : 1;
/** Set if it's the final component. */
uint32_t fFinal : 1;
/** Unused bits. */
uint32_t fReserved : 2+8;
} RTPATHGLOBPPE;
typedef struct RTPATHGLOB
{
/** Path buffer. */
char szPath[RTPATH_MAX];
/** Temporary buffers. */
union
{
/** File system object info structure. */
RTFSOBJINFO ObjInfo;
/** Directory entry buffer. */
RTDIRENTRY DirEntry;
/** Padding the buffer to an unreasonably large size. */
uint8_t abPadding[RTPATH_MAX + sizeof(RTDIRENTRY)];
} u;
/** Where to insert the next one.*/
PRTPATHGLOBENTRY *ppNext;
/** The head pointer. */
PRTPATHGLOBENTRY pHead;
/** Result count. */
uint32_t cResults;
/** Counts path overflows. */
uint32_t cPathOverflows;
/** The input flags. */
uint32_t fFlags;
/** Matching instruction allocator. */
RTPATHMATCHALLOC MatchInstrAlloc;
/** Matching state. */
RTPATHMATCHCACHE MatchCache;
/** The pattern string. */
const char *pszPattern;
/** The parsed path. */
PRTPATHPARSED pParsed;
/** The component to start with. */
uint16_t iFirstComp;
/** The corresponding path offset (previous components already present). */
uint16_t offFirstPath;
/** Path component information we need. */
RTPATHGLOBPPE aComps[1];
} RTPATHGLOB;
typedef RTPATHGLOB *PRTPATHGLOB;
/**
* Matching variable lookup table.
* Currently so small we don't bother sorting it and doing binary lookups.
*/
typedef struct RTPATHMATCHVAR
{
/** The variable name. */
const char *pszName;
/** The variable name length. */
uint16_t cchName;
/** Only available as the verify first component. */
bool fFirstOnly;
/**
* Queries a given variable value.
*
* @returns IPRT status code.
* @retval VERR_BUFFER_OVERFLOW
* @retval VERR_TRY_AGAIN if the caller should skip this value item and try the
* next one instead (e.g. env var not present).
* @retval VINF_EOF when retrieving the last one, if possible.
* @retval VERR_EOF when @a iItem is past the item space.
*
* @param iItem The variable value item to retrieve. (A variable may
* have more than one value, e.g. 'BothProgramFile' on a
* 64-bit system or 'Path'.)
* @param pszBuf Where to return the value.
* @param cbBuf The buffer size.
* @param pcchValue Where to return the length of the return string.
* @param pCache Pointer to the path matching cache. May speed up
* enumerating PATH items and similar.
*/
DECLCALLBACKMEMBER(int, pfnQuery)(uint32_t iItem, char *pszBuf, size_t cbBuf, size_t *pcchValue, PRTPATHMATCHCACHE pCache);
/**
* Matching method, optional.
*
* @returns IPRT status code.
* @retval VINF_SUCCESS on match.
* @retval VERR_MISMATCH on mismatch.
*
* @param pszMatch String to match with (not terminated).
* @param cchMatch The length of what we match with.
* @param fIgnoreCase Whether to ignore case or not when comparing.
* @param pcchMatched Where to return the length of the match (value length).
*/
DECLCALLBACKMEMBER(int, pfnMatch)(const char *pchMatch, size_t cchMatch, bool fIgnoreCase, size_t *pcchMatched);
} RTPATHMATCHVAR;
/*********************************************************************************************************************************
* Internal Functions *
*********************************************************************************************************************************/
static int rtPathGlobExecRecursiveStarStar(PRTPATHGLOB pGlob, size_t offPath, uint32_t iStarStarComp, size_t offStarStarPath);
static int rtPathGlobExecRecursiveVarExp(PRTPATHGLOB pGlob, size_t offPath, uint32_t iComp);
static int rtPathGlobExecRecursivePlainText(PRTPATHGLOB pGlob, size_t offPath, uint32_t iComp);
static int rtPathGlobExecRecursiveGeneric(PRTPATHGLOB pGlob, size_t offPath, uint32_t iComp);
/**
* Implements the two variable access functions for a simple one value variable.
*/
#define RTPATHMATCHVAR_SIMPLE(a_Name, a_GetStrExpr) \
static DECLCALLBACK(int) RT_CONCAT(rtPathVarQuery_,a_Name)(uint32_t iItem, char *pszBuf, size_t cbBuf, size_t *pcchValue, \
PRTPATHMATCHCACHE pCache) \
{ \
if (iItem == 0) \
{ \
const char *pszValue = a_GetStrExpr; \
size_t cchValue = strlen(pszValue); \
if (cchValue + 1 <= cbBuf) \
{ \
memcpy(pszBuf, pszValue, cchValue + 1); \
*pcchValue = cchValue; \
return VINF_EOF; \
} \
return VERR_BUFFER_OVERFLOW; \
} \
NOREF(pCache);\
return VERR_EOF; \
} \
static DECLCALLBACK(int) RT_CONCAT(rtPathVarMatch_,a_Name)(const char *pchMatch, size_t cchMatch, bool fIgnoreCase, \
size_t *pcchMatched) \
{ \
const char *pszValue = a_GetStrExpr; \
size_t cchValue = strlen(pszValue); \
if ( cchValue >= cchMatch \
&& ( !fIgnoreCase \
? memcmp(pszValue, pchMatch, cchValue) == 0 \
: RTStrNICmp(pszValue, pchMatch, cchValue) == 0) ) \
{ \
*pcchMatched = cchValue; \
return VINF_SUCCESS; \
} \
return VERR_MISMATCH; \
} \
typedef int RT_CONCAT(DummyColonType_,a_Name)
/**
* Implements mapping a glob variable to an environment variable.
*/
#define RTPATHMATCHVAR_SIMPLE_ENVVAR(a_Name, a_pszEnvVar, a_cbMaxValue) \
static DECLCALLBACK(int) RT_CONCAT(rtPathVarQuery_,a_Name)(uint32_t iItem, char *pszBuf, size_t cbBuf, size_t *pcchValue, \
PRTPATHMATCHCACHE pCache) \
{ \
if (iItem == 0) \
{ \
int rc = RTEnvGetEx(RTENV_DEFAULT, a_pszEnvVar, pszBuf, cbBuf, pcchValue); \
if (RT_SUCCESS(rc)) \
return VINF_EOF; \
if (rc != VERR_ENV_VAR_NOT_FOUND) \
return rc; \
} \
NOREF(pCache);\
return VERR_EOF; \
} \
static DECLCALLBACK(int) RT_CONCAT(rtPathVarMatch_,a_Name)(const char *pchMatch, size_t cchMatch, bool fIgnoreCase, \
size_t *pcchMatched) \
{ \
char szValue[a_cbMaxValue]; \
size_t cchValue; \
int rc = RTEnvGetEx(RTENV_DEFAULT, a_pszEnvVar, szValue, sizeof(szValue), &cchValue); \
if ( RT_SUCCESS(rc) \
&& cchValue >= cchMatch \
&& ( !fIgnoreCase \
? memcmp(szValue, pchMatch, cchValue) == 0 \
: RTStrNICmp(szValue, pchMatch, cchValue) == 0) ) \
{ \
*pcchMatched = cchValue; \
return VINF_SUCCESS; \
} \
return VERR_MISMATCH; \
} \
typedef int RT_CONCAT(DummyColonType_,a_Name)
/**
* Implements mapping a glob variable to multiple environment variable values.
*
* @param a_Name The variable name.
* @param a_apszVarNames Assumes to be a global variable that RT_ELEMENTS
* works correctly on.
* @param a_cbMaxValue The max expected value size.
*/
#define RTPATHMATCHVAR_MULTIPLE_ENVVARS(a_Name, a_apszVarNames, a_cbMaxValue) \
static DECLCALLBACK(int) RT_CONCAT(rtPathVarQuery_,a_Name)(uint32_t iItem, char *pszBuf, size_t cbBuf, size_t *pcchValue, \
PRTPATHMATCHCACHE pCache) \
{ \
if (iItem < RT_ELEMENTS(a_apszVarNames)) \
{ \
int rc = RTEnvGetEx(RTENV_DEFAULT, a_apszVarNames[iItem], pszBuf, cbBuf, pcchValue); \
if (RT_SUCCESS(rc)) \
return iItem + 1 == RT_ELEMENTS(a_apszVarNames) ? VINF_EOF : VINF_SUCCESS; \
if (rc == VERR_ENV_VAR_NOT_FOUND) \
rc = VERR_TRY_AGAIN; \
return rc; \
} \
NOREF(pCache);\
return VERR_EOF; \
} \
static DECLCALLBACK(int) RT_CONCAT(rtPathVarMatch_,a_Name)(const char *pchMatch, size_t cchMatch, bool fIgnoreCase, \
size_t *pcchMatched) \
{ \
for (uint32_t iItem = 0; iItem < RT_ELEMENTS(a_apszVarNames); iItem++) \
{ \
char szValue[a_cbMaxValue]; \
size_t cchValue; \
int rc = RTEnvGetEx(RTENV_DEFAULT, a_apszVarNames[iItem], szValue, sizeof(szValue), &cchValue);\
if ( RT_SUCCESS(rc) \
&& cchValue >= cchMatch \
&& ( !fIgnoreCase \
? memcmp(szValue, pchMatch, cchValue) == 0 \
: RTStrNICmp(szValue, pchMatch, cchValue) == 0) ) \
{ \
*pcchMatched = cchValue; \
return VINF_SUCCESS; \
} \
} \
return VERR_MISMATCH; \
} \
typedef int RT_CONCAT(DummyColonType_,a_Name)
RTPATHMATCHVAR_SIMPLE(Arch, RTBldCfgTargetArch());
RTPATHMATCHVAR_SIMPLE(Bits, RT_XSTR(ARCH_BITS));
#ifdef RT_OS_WINDOWS
RTPATHMATCHVAR_SIMPLE_ENVVAR(WinAppData, "AppData", RTPATH_MAX);
RTPATHMATCHVAR_SIMPLE_ENVVAR(WinProgramData, "ProgramData", RTPATH_MAX);
RTPATHMATCHVAR_SIMPLE_ENVVAR(WinProgramFiles, "ProgramFiles", RTPATH_MAX);
RTPATHMATCHVAR_SIMPLE_ENVVAR(WinCommonProgramFiles, "CommonProgramFiles", RTPATH_MAX);
# if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
RTPATHMATCHVAR_SIMPLE_ENVVAR(WinOtherProgramFiles, "ProgramFiles(x86)", RTPATH_MAX);
RTPATHMATCHVAR_SIMPLE_ENVVAR(WinOtherCommonProgramFiles, "CommonProgramFiles(x86)", RTPATH_MAX);
# else
# error "Port ME!"
# endif
static const char * const a_apszWinProgramFilesVars[] =
{
"ProgramFiles",
# ifdef RT_ARCH_AMD64
"ProgramFiles(x86)",
# endif
};
RTPATHMATCHVAR_MULTIPLE_ENVVARS(WinAllProgramFiles, a_apszWinProgramFilesVars, RTPATH_MAX);
static const char * const a_apszWinCommonProgramFilesVars[] =
{
"CommonProgramFiles",
# ifdef RT_ARCH_AMD64
"CommonProgramFiles(x86)",
# endif
};
RTPATHMATCHVAR_MULTIPLE_ENVVARS(WinAllCommonProgramFiles, a_apszWinCommonProgramFilesVars, RTPATH_MAX);
#endif
/**
* @interface_method_impl{RTPATHMATCHVAR,pfnQuery, Enumerates the PATH}
*/
static DECLCALLBACK(int) rtPathVarQuery_Path(uint32_t iItem, char *pszBuf, size_t cbBuf, size_t *pcchValue,
PRTPATHMATCHCACHE pCache)
{
RT_NOREF_PV(pCache);
/*
* Query the PATH value.
*/
/** @todo cache this in pCache with iItem and offset. */
char *pszPathFree = NULL;
char *pszPath = pszBuf;
size_t cchActual;
const char *pszVarNm = "PATH";
int rc = RTEnvGetEx(RTENV_DEFAULT, pszVarNm, pszPath, cbBuf, &cchActual);
#ifdef RT_OS_WINDOWS
if (rc == VERR_ENV_VAR_NOT_FOUND)
rc = RTEnvGetEx(RTENV_DEFAULT, pszVarNm = "Path", pszPath, cbBuf, &cchActual);
#endif
if (rc == VERR_BUFFER_OVERFLOW)
{
for (uint32_t iTry = 0; iTry < 10; iTry++)
{
size_t cbPathBuf = RT_ALIGN_Z(cchActual + 1 + 64 * iTry, 64);
pszPathFree = (char *)RTMemTmpAlloc(cbPathBuf);
rc = RTEnvGetEx(RTENV_DEFAULT, pszVarNm, pszPathFree, cbPathBuf, &cchActual);
if (RT_SUCCESS(rc))
break;
RTMemTmpFree(pszPathFree);
AssertReturn(cchActual >= cbPathBuf, VERR_INTERNAL_ERROR_3);
}
pszPath = pszPathFree;
}
/*
* Spool forward to the given PATH item.
*/
rc = VERR_EOF;
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
const char chSep = ';';
#else
const char chSep = ':';
#endif
while (*pszPath != '\0')
{
char *pchSep = strchr(pszPath, chSep);
/* We ignore empty strings, which is probably not entirely correct,
but works better on DOS based system with many entries added
without checking whether there is a trailing separator or not.
Thus, the current directory is only searched if a '.' is present
in the PATH. */
if (pchSep == pszPath)
pszPath++;
else if (iItem > 0)
{
/* If we didn't find a separator, the item doesn't exists. Quit. */
if (!pchSep)
break;
pszPath = pchSep + 1;
iItem--;
}
else
{
/* We've reached the item we wanted. */
size_t cchComp = pchSep ? pchSep - pszPath : strlen(pszPath);
if (cchComp < cbBuf)
{
if (pszBuf != pszPath)
memmove(pszBuf, pszPath, cchComp);
pszBuf[cchComp] = '\0';
rc = pchSep ? VINF_SUCCESS : VINF_EOF;
}
else
rc = VERR_BUFFER_OVERFLOW;
*pcchValue = cchComp;
break;
}
}
if (pszPathFree)
RTMemTmpFree(pszPathFree);
return rc;
}
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
/**
* @interface_method_impl{RTPATHMATCHVAR,pfnQuery,
* The system drive letter + colon.}.
*/
static DECLCALLBACK(int) rtPathVarQuery_DosSystemDrive(uint32_t iItem, char *pszBuf, size_t cbBuf, size_t *pcchValue,
PRTPATHMATCHCACHE pCache)
{
RT_NOREF_PV(pCache);
if (iItem == 0)
{
AssertReturn(cbBuf >= 3, VERR_BUFFER_OVERFLOW);
# ifdef RT_OS_WINDOWS
/* Since this is used at the start of a pattern, we assume
we've got more than enough buffer space. */
AssertReturn(g_pfnGetSystemWindowsDirectoryW, VERR_SYMBOL_NOT_FOUND);
PRTUTF16 pwszTmp = (PRTUTF16)pszBuf;
UINT cch = g_pfnGetSystemWindowsDirectoryW(pwszTmp, (UINT)(cbBuf / sizeof(WCHAR)));
if (cch >= 2)
{
RTUTF16 wcDrive = pwszTmp[0];
if ( RT_C_IS_ALPHA(wcDrive)
&& pwszTmp[1] == ':')
{
pszBuf[0] = wcDrive;
pszBuf[1] = ':';
pszBuf[2] = '\0';
*pcchValue = 2;
return VINF_EOF;
}
}
# else
ULONG ulDrive = ~(ULONG)0;
APIRET rc = DosQuerySysInfo(QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, &ulDrive, sizeof(ulDrive));
ulDrive--; /* 1 = 'A' */
if ( rc == NO_ERROR
&& ulDrive <= (ULONG)'Z')
{
pszBuf[0] = (char)ulDrive + 'A';
pszBuf[1] = ':';
pszBuf[2] = '\0';
*pcchValue = 2;
return VINF_EOF;
}
# endif
return VERR_INTERNAL_ERROR_4;
}
return VERR_EOF;
}
#endif
#ifdef RT_OS_WINDOWS
/**
* @interface_method_impl{RTPATHMATCHVAR,pfnQuery,
* The system root directory (C:\Windows).}.
*/
static DECLCALLBACK(int) rtPathVarQuery_WinSystemRoot(uint32_t iItem, char *pszBuf, size_t cbBuf, size_t *pcchValue,
PRTPATHMATCHCACHE pCache)
{
RT_NOREF_PV(pCache);
if (iItem == 0)
{
Assert(pszBuf); Assert(cbBuf);
AssertReturn(g_pfnGetSystemWindowsDirectoryW, VERR_SYMBOL_NOT_FOUND);
RTUTF16 wszSystemRoot[MAX_PATH];
UINT cchSystemRoot = g_pfnGetSystemWindowsDirectoryW(wszSystemRoot, MAX_PATH);
if (cchSystemRoot > 0)
return RTUtf16ToUtf8Ex(wszSystemRoot, cchSystemRoot, &pszBuf, cbBuf, pcchValue);
return RTErrConvertFromWin32(GetLastError());
}
return VERR_EOF;
}
#endif
#undef RTPATHMATCHVAR_SIMPLE
#undef RTPATHMATCHVAR_SIMPLE_ENVVAR
#undef RTPATHMATCHVAR_DOUBLE_ENVVAR
/**
* Variables.
*/
static RTPATHMATCHVAR const g_aVariables[] =
{
{ RT_STR_TUPLE("Arch"), false, rtPathVarQuery_Arch, rtPathVarMatch_Arch },
{ RT_STR_TUPLE("Bits"), false, rtPathVarQuery_Bits, rtPathVarMatch_Bits },
{ RT_STR_TUPLE("Path"), true, rtPathVarQuery_Path, NULL },
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
{ RT_STR_TUPLE("SystemDrive"), true, rtPathVarQuery_DosSystemDrive, NULL },
#endif
#ifdef RT_OS_WINDOWS
{ RT_STR_TUPLE("SystemRoot"), true, rtPathVarQuery_WinSystemRoot, NULL },
{ RT_STR_TUPLE("AppData"), true, rtPathVarQuery_WinAppData, rtPathVarMatch_WinAppData },
{ RT_STR_TUPLE("ProgramData"), true, rtPathVarQuery_WinProgramData, rtPathVarMatch_WinProgramData },
{ RT_STR_TUPLE("ProgramFiles"), true, rtPathVarQuery_WinProgramFiles, rtPathVarMatch_WinProgramFiles },
{ RT_STR_TUPLE("OtherProgramFiles"), true, rtPathVarQuery_WinOtherProgramFiles, rtPathVarMatch_WinOtherProgramFiles },
{ RT_STR_TUPLE("AllProgramFiles"), true, rtPathVarQuery_WinAllProgramFiles, rtPathVarMatch_WinAllProgramFiles },
{ RT_STR_TUPLE("CommonProgramFiles"), true, rtPathVarQuery_WinCommonProgramFiles, rtPathVarMatch_WinCommonProgramFiles },
{ RT_STR_TUPLE("OtherCommonProgramFiles"), true, rtPathVarQuery_WinOtherCommonProgramFiles, rtPathVarMatch_WinOtherCommonProgramFiles },
{ RT_STR_TUPLE("AllCommonProgramFiles"), true, rtPathVarQuery_WinAllCommonProgramFiles, rtPathVarMatch_WinAllCommonProgramFiles },
#endif
};
/**
* Handles a complicated set.
*
* A complicated set is either using ranges, character classes or code points
* outside the ASCII-7 range.
*
* @returns VINF_SUCCESS or VERR_MISMATCH. May also return UTF-8 decoding
* errors as well as VERR_PATH_MATCH_FEATURE_NOT_IMPLEMENTED.
*
* @param ucInput The input code point to match with.
* @param pchSet The start of the set specification (after caret).
* @param cchSet The length of the set specification.
*/
static int rtPathMatchExecExtendedSet(RTUNICP ucInput, const char *pchSet, size_t cchSet)
{
while (cchSet > 0)
{
RTUNICP ucSet;
int rc = RTStrGetCpNEx(&pchSet, &cchSet, &ucSet);
AssertRCReturn(rc, rc);
/*
* Check for character class, collating symbol and equvalence class.
*/
if (ucSet == '[' && cchSet > 0)
{
char chNext = *pchSet;
if (chNext == ':')
{
#define CHECK_CHAR_CLASS(a_szClassNm, a_BoolTestExpr) \
if ( cchSet >= sizeof(a_szClassNm) \
&& memcmp(pchSet, a_szClassNm "]", sizeof(a_szClassNm)) == 0) \
{ \
if (a_BoolTestExpr) \
return VINF_SUCCESS; \
pchSet += sizeof(a_szClassNm); \
cchSet -= sizeof(a_szClassNm); \
continue; \
} do { } while (0)
CHECK_CHAR_CLASS(":alpha:", RTUniCpIsAlphabetic(ucInput));
CHECK_CHAR_CLASS(":alnum:", RTUniCpIsAlphabetic(ucInput) || RTUniCpIsDecDigit(ucInput)); /** @todo figure what's correct here and fix uni.h */
CHECK_CHAR_CLASS(":blank:", ucInput == ' ' || ucInput == '\t');
CHECK_CHAR_CLASS(":cntrl:", ucInput < 31 || ucInput == 127);
CHECK_CHAR_CLASS(":digit:", RTUniCpIsDecDigit(ucInput));
CHECK_CHAR_CLASS(":lower:", RTUniCpIsLower(ucInput));
CHECK_CHAR_CLASS(":print:", RTUniCpIsAlphabetic(ucInput) || (RT_C_IS_PRINT(ucInput) && ucInput < 127)); /** @todo fixme*/
CHECK_CHAR_CLASS(":punct:", RT_C_IS_PRINT(ucInput) && ucInput < 127); /** @todo fixme*/
CHECK_CHAR_CLASS(":space:", RTUniCpIsSpace(ucInput));
CHECK_CHAR_CLASS(":upper:", RTUniCpIsUpper(ucInput));
CHECK_CHAR_CLASS(":xdigit:", RTUniCpIsHexDigit(ucInput));
AssertMsgFailedReturn(("Unknown or malformed char class: '%.*s'\n", cchSet + 1, pchSet - 1),
VERR_PATH_GLOB_UNKNOWN_CHAR_CLASS);
#undef CHECK_CHAR_CLASS
}
/** @todo implement collating symbol and equvalence class. */
else if (chNext == '=' || chNext == '.')
AssertFailedReturn(VERR_PATH_MATCH_FEATURE_NOT_IMPLEMENTED);
}
/*
* Check for range (leading or final dash does not constitute a range).
*/
if (cchSet > 1 && *pchSet == '-')
{
pchSet++; /* skip dash */
cchSet--;
RTUNICP ucSet2;
rc = RTStrGetCpNEx(&pchSet, &cchSet, &ucSet2);
AssertRCReturn(rc, rc);
Assert(ucSet < ucSet2);
if (ucInput >= ucSet && ucInput <= ucSet2)
return VINF_SUCCESS;
}
/*
* Single char comparison.
*/
else if (ucInput == ucSet)
return VINF_SUCCESS;
}
return VERR_MISMATCH;
}
/**
* Variable matching fallback using the query function.
*
* This must not be inlined as it consuming a lot of stack! Which is why it's
* placed a couple of functions away from the recursive rtPathExecMatch.
*
* @returns VINF_SUCCESS or VERR_MISMATCH.
* @param pchInput The current input position.
* @param cchInput The amount of input left..
* @param idxVar The variable table index.
* @param fIgnoreCase Whether to ignore case when comparing.
* @param pcchMatched Where to return how much we actually matched up.
* @param pCache Pointer to the path matching cache.
*/
DECL_NO_INLINE(static, int) rtPathMatchExecVariableFallback(const char *pchInput, size_t cchInput, uint16_t idxVar,
bool fIgnoreCase, size_t *pcchMatched, PRTPATHMATCHCACHE pCache)
{
for (uint32_t iItem = 0; iItem < RTPATHMATCH_MAX_VAR_ITEMS; iItem++)
{
char szValue[RTPATH_MAX];
size_t cchValue;
int rc = g_aVariables[idxVar].pfnQuery(iItem, szValue, sizeof(szValue), &cchValue, pCache);
if (RT_SUCCESS(rc))
{
if (cchValue <= cchInput)
{
if ( !fIgnoreCase
? memcmp(pchInput, szValue, cchValue) == 0
: RTStrNICmp(pchInput, szValue, cchValue) == 0)
{
*pcchMatched = cchValue;
return VINF_SUCCESS;
}
}
if (rc == VINF_EOF)
return VERR_MISMATCH;
}
else if (rc == VERR_EOF)
return VERR_MISMATCH;
else
Assert(rc == VERR_BUFFER_OVERFLOW || rc == VERR_TRY_AGAIN);
}
AssertFailed();
return VERR_MISMATCH;
}
/**
* Variable matching worker.
*
* @returns VINF_SUCCESS or VERR_MISMATCH.
* @param pchInput The current input position.
* @param cchInput The amount of input left..
* @param idxVar The variable table index.
* @param fIgnoreCase Whether to ignore case when comparing.
* @param pcchMatched Where to return how much we actually matched up.
* @param pCache Pointer to the path matching cache.
*/
static int rtPathMatchExecVariable(const char *pchInput, size_t cchInput, uint16_t idxVar,
bool fIgnoreCase, size_t *pcchMatched, PRTPATHMATCHCACHE pCache)
{
Assert(idxVar < RT_ELEMENTS(g_aVariables));
if (g_aVariables[idxVar].pfnMatch)
return g_aVariables[idxVar].pfnMatch(pchInput, cchInput, fIgnoreCase, pcchMatched);
return rtPathMatchExecVariableFallback(pchInput, cchInput, idxVar, fIgnoreCase, pcchMatched, pCache);
}
/**
* Variable matching worker.
*
* @returns VINF_SUCCESS or VERR_MISMATCH.
* @param pchInput The current input position.
* @param cchInput The amount of input left..
* @param pProg The first matching program instruction.
* @param pCache Pointer to the path matching cache.
*/
static int rtPathMatchExec(const char *pchInput, size_t cchInput, PCRTPATHMATCHCORE pProg, PRTPATHMATCHCACHE pCache)
{
for (;;)
{
switch (pProg->enmOpCode)
{
case RTPATHMATCHOP_RETURN_MATCH_IF_AT_END:
return cchInput == 0 ? VINF_SUCCESS : VERR_MISMATCH;
case RTPATHMATCHOP_RETURN_MATCH:
return VINF_SUCCESS;
case RTPATHMATCHOP_RETURN_MATCH_EXCEPT_DOT_AND_DOTDOT:
if ( cchInput > 2
|| cchInput < 1
|| pchInput[0] != '.'
|| (cchInput == 2 && pchInput[1] != '.') )
return VINF_SUCCESS;
return VERR_MISMATCH;
case RTPATHMATCHOP_STRCMP:
if (pProg->cch > cchInput)
return VERR_MISMATCH;
if (memcmp(pchInput, pProg->pch, pProg->cch) != 0)
return VERR_MISMATCH;
cchInput -= pProg->cch;
pchInput += pProg->cch;
break;
case RTPATHMATCHOP_STRICMP:
if (pProg->cch > cchInput)
return VERR_MISMATCH;
if (RTStrNICmp(pchInput, pProg->pch, pProg->cch) != 0)
return VERR_MISMATCH;
cchInput -= pProg->cch;
pchInput += pProg->cch;
break;
case RTPATHMATCHOP_SKIP_ONE_CODEPOINT:
{
if (cchInput == 0)
return VERR_MISMATCH;
RTUNICP ucInputIgnore;
int rc = RTStrGetCpNEx(&pchInput, &cchInput, &ucInputIgnore);
AssertRCReturn(rc, rc);
break;
}
case RTPATHMATCHOP_SKIP_MULTIPLE_CODEPOINTS:
{
uint16_t cCpsLeft = pProg->cch;
Assert(cCpsLeft > 1);
if (cCpsLeft > cchInput)
return VERR_MISMATCH;
while (cCpsLeft-- > 0)
{
RTUNICP ucInputIgnore;
int rc = RTStrGetCpNEx(&pchInput, &cchInput, &ucInputIgnore);
if (RT_FAILURE(rc))
return rc == VERR_END_OF_STRING ? VERR_MISMATCH : rc;
}
break;
}
case RTPATHMATCHOP_CODEPOINT_IN_SET_ASCII7:
{
if (cchInput == 0)
return VERR_MISMATCH;
RTUNICP ucInput;
int rc = RTStrGetCpNEx(&pchInput, &cchInput, &ucInput);
AssertRCReturn(rc, rc);
if (ucInput >= 0x80)
return VERR_MISMATCH;
if (memchr(pProg->pch, (char)ucInput, pProg->cch) == NULL)
return VERR_MISMATCH;
break;
}
case RTPATHMATCHOP_CODEPOINT_NOT_IN_SET_ASCII7:
{
if (cchInput == 0)
return VERR_MISMATCH;
RTUNICP ucInput;
int rc = RTStrGetCpNEx(&pchInput, &cchInput, &ucInput);
AssertRCReturn(rc, rc);
if (ucInput >= 0x80)
break;
if (memchr(pProg->pch, (char)ucInput, pProg->cch) != NULL)
return VERR_MISMATCH;
break;
}
case RTPATHMATCHOP_CODEPOINT_IN_SET_EXTENDED:
{
if (cchInput == 0)
return VERR_MISMATCH;
RTUNICP ucInput;
int rc = RTStrGetCpNEx(&pchInput, &cchInput, &ucInput);
AssertRCReturn(rc, rc);
rc = rtPathMatchExecExtendedSet(ucInput, pProg->pch, pProg->cch);
if (rc == VINF_SUCCESS)
break;
return rc;
}
case RTPATHMATCHOP_CODEPOINT_NOT_IN_SET_EXTENDED:
{
if (cchInput == 0)
return VERR_MISMATCH;
RTUNICP ucInput;
int rc = RTStrGetCpNEx(&pchInput, &cchInput, &ucInput);
AssertRCReturn(rc, rc);
rc = rtPathMatchExecExtendedSet(ucInput, pProg->pch, pProg->cch);
if (rc == VERR_MISMATCH)
break;
if (rc == VINF_SUCCESS)
rc = VERR_MISMATCH;
return rc;
}
case RTPATHMATCHOP_VARIABLE_VALUE_CMP:
case RTPATHMATCHOP_VARIABLE_VALUE_ICMP:
{
size_t cchMatched = 0;
int rc = rtPathMatchExecVariable(pchInput, cchInput, pProg->uOp2,
pProg->enmOpCode == RTPATHMATCHOP_VARIABLE_VALUE_ICMP, &cchMatched, pCache);
if (rc == VINF_SUCCESS)
{
pchInput += cchMatched;
cchInput -= cchMatched;
break;
}
return rc;
}
/*
* This is the expensive one. It always completes the program.
*/
case RTPATHMATCHOP_ZERO_OR_MORE:
{
if (cchInput < pProg->cch)
return VERR_MISMATCH;
size_t cchMatched = cchInput - pProg->cch;
do
{
int rc = rtPathMatchExec(&pchInput[cchMatched], cchInput - cchMatched, pProg + 1, pCache);
if (RT_SUCCESS(rc))
return rc;
} while (cchMatched-- > 0);
return VERR_MISMATCH;
}
/*
* Variant of the above that doesn't match '.' and '..' entries.
*/
case RTPATHMATCHOP_ZERO_OR_MORE_EXCEPT_DOT_AND_DOTDOT:
{
if (cchInput < pProg->cch)
return VERR_MISMATCH;
if ( cchInput <= 2
&& cchInput > 0
&& pchInput[0] == '.'
&& (cchInput == 1 || pchInput[1] == '.') )
return VERR_MISMATCH;
size_t cchMatched = cchInput - pProg->cch;
do
{
int rc = rtPathMatchExec(&pchInput[cchMatched], cchInput - cchMatched, pProg + 1, pCache);
if (RT_SUCCESS(rc))
return rc;
} while (cchMatched-- > 0);
return VERR_MISMATCH;
}
default:
AssertMsgFailedReturn(("enmOpCode=%d\n", pProg->enmOpCode), VERR_INTERNAL_ERROR_3);
}
pProg++;
}
}
/**
* Compiles a path matching program.
*
* @returns IPRT status code.
* @param pchPattern The pattern to compile.
* @param cchPattern The length of the pattern.
* @param fIgnoreCase Whether to ignore case or not when doing the
* actual matching later on.
* @param pAllocator Pointer to the instruction allocator & result
* array. The compiled "program" starts at
* PRTPATHMATCHALLOC::paInstructions[PRTPATHMATCHALLOC::iNext]
* (input iNext value).
*
* @todo Expose this matching code and also use it for RTDirOpenFiltered
*/
static int rtPathMatchCompile(const char *pchPattern, size_t cchPattern, bool fIgnoreCase, PRTPATHMATCHALLOC pAllocator)
{
/** @todo PORTME: big endian. */
static const uint8_t s_bmMetaChars[256/8] =
{
0x00, 0x00, 0x00, 0x00, /* 0 thru 31 */
0x10, 0x04, 0x00, 0x80, /* 32 thru 63 */
0x00, 0x00, 0x00, 0x08, /* 64 thru 95 */
0x00, 0x00, 0x00, 0x00, /* 96 thru 127 */
/* UTF-8 multibyte: */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
Assert(ASMBitTest(s_bmMetaChars, '$')); AssertCompile('$' == 0x24 /*36*/);
Assert(ASMBitTest(s_bmMetaChars, '*')); AssertCompile('*' == 0x2a /*42*/);
Assert(ASMBitTest(s_bmMetaChars, '?')); AssertCompile('?' == 0x3f /*63*/);
Assert(ASMBitTest(s_bmMetaChars, '[')); AssertCompile('[' == 0x5b /*91*/);
/*
* For checking for the first instruction.
*/
uint16_t const iFirst = pAllocator->iNext;
/*
* This is for tracking zero-or-more instructions and for calculating
* the minimum amount of input required for it to be considered.
*/
uint16_t aiZeroOrMore[RTPATHMATCH_MAX_ZERO_OR_MORE];
uint8_t cZeroOrMore = 0;
size_t offInput = 0;
/*
* Loop thru the pattern and translate it into string matching instructions.
*/
for (;;)
{
/*
* Allocate the next instruction.
*/
if (pAllocator->iNext >= pAllocator->cAllocated)
{
uint32_t cNew = pAllocator->cAllocated ? pAllocator->cAllocated * 2 : 2;
void *pvNew = RTMemRealloc(pAllocator->paInstructions, cNew * sizeof(pAllocator->paInstructions[0]));
AssertReturn(pvNew, VERR_NO_MEMORY);
pAllocator->paInstructions = (PRTPATHMATCHCORE)pvNew;
pAllocator->cAllocated = cNew;
}
PRTPATHMATCHCORE pInstr = &pAllocator->paInstructions[pAllocator->iNext++];
pInstr->pch = pchPattern;
pInstr->cch = 0;
pInstr->uOp2 = 0;
/*
* Special case: End of pattern.
*/
if (!cchPattern)
{
pInstr->enmOpCode = RTPATHMATCHOP_RETURN_MATCH_IF_AT_END;
break;
}
/*
* Parse the next bit of the pattern.
*/
char ch = *pchPattern;
if (ASMBitTest(s_bmMetaChars, (uint8_t)ch))
{
/*
* Zero or more characters wildcard.
*/
if (ch == '*')
{
/* Skip extra asterisks. */
do
{
cchPattern--;
pchPattern++;
} while (cchPattern > 0 && *pchPattern == '*');
/* There is a special optimization for trailing '*'. */
pInstr->cch = 1;
if (cchPattern == 0)
{
pInstr->enmOpCode = iFirst + 1U == pAllocator->iNext
? RTPATHMATCHOP_RETURN_MATCH_EXCEPT_DOT_AND_DOTDOT : RTPATHMATCHOP_RETURN_MATCH;
break;
}
pInstr->enmOpCode = iFirst + 1U == pAllocator->iNext
? RTPATHMATCHOP_ZERO_OR_MORE_EXCEPT_DOT_AND_DOTDOT : RTPATHMATCHOP_ZERO_OR_MORE;
pInstr->uOp2 = (uint16_t)offInput;
AssertReturn(cZeroOrMore < RT_ELEMENTS(aiZeroOrMore), VERR_OUT_OF_RANGE);
aiZeroOrMore[cZeroOrMore] = (uint16_t)(pInstr - pAllocator->paInstructions);
/* cchInput unchanged, zero-or-more matches. */
continue;
}
/*
* Single character wildcard.
*/
if (ch == '?')
{
/* Count them if more. */
uint16_t cchQms = 1;
while (cchQms < cchPattern && pchPattern[cchQms] == '?')
cchQms++;
pInstr->cch = cchQms;
pInstr->enmOpCode = cchQms == 1 ? RTPATHMATCHOP_SKIP_ONE_CODEPOINT : RTPATHMATCHOP_SKIP_MULTIPLE_CODEPOINTS;
cchPattern -= cchQms;
pchPattern += cchQms;
offInput += cchQms;
continue;
}
/*
* Character in set.
*
* Note that we skip the first char in the set as that is the only place
* ']' can be placed if one desires to explicitly include it in the set.
* To make life a bit more interesting, [:class:] is allowed inside the
* set, so we have to do the counting game to find the end.
*/
if (ch == '[')
{
if ( cchPattern > 2
&& (const char *)memchr(pchPattern + 2, ']', cchPattern) != NULL)
{
/* Check for not-in. */
bool fInverted = false;
size_t offStart = 1;
if (pchPattern[offStart] == '^')
{
fInverted = true;
offStart++;
}
/* Special case for ']' as the first char, it doesn't indicate closing then. */
size_t off = offStart;
if (pchPattern[off] == ']')
off++;
bool fExtended = false;
while (off < cchPattern)
{
ch = pchPattern[off++];
if (ch == '[')
{
if (off < cchPattern)
{
char chOpen = pchPattern[off];
if ( chOpen == ':'
|| chOpen == '='
|| chOpen == '.')
{
off++;
const char *pchFound = (const char *)memchr(&pchPattern[off], ']', cchPattern - off);
if ( pchFound
&& pchFound[-1] == chOpen)
{
fExtended = true;
off = pchFound - pchPattern + 1;
}
else
AssertFailed();
}
}
}
/* Check for closing. */
else if (ch == ']')
break;
/* Check for range expression, promote to extended if this happens. */
else if ( ch == '-'
&& off != offStart + 1
&& off < cchPattern
&& pchPattern[off] != ']')
fExtended = true;
/* UTF-8 multibyte chars forces us to use the extended version too. */
else if ((uint8_t)ch >= 0x80)
fExtended = true;
}
if (ch == ']')
{
pInstr->pch = &pchPattern[offStart];
pInstr->cch = (uint16_t)(off - offStart - 1);
if (!fExtended)
pInstr->enmOpCode = !fInverted
? RTPATHMATCHOP_CODEPOINT_IN_SET_ASCII7 : RTPATHMATCHOP_CODEPOINT_NOT_IN_SET_ASCII7;
else
pInstr->enmOpCode = !fInverted
? RTPATHMATCHOP_CODEPOINT_IN_SET_EXTENDED
: RTPATHMATCHOP_CODEPOINT_NOT_IN_SET_EXTENDED;
pchPattern += off;
cchPattern -= off;
offInput += 1;
continue;
}
/* else: invalid, treat it as */
AssertFailed();
}
}
/*
* Variable matching.
*/
else if (ch == '$')
{
const char *pchFound;
if ( cchPattern > 3
&& pchPattern[1] == '{'
&& (pchFound = (const char *)memchr(pchPattern + 2, '}', cchPattern)) != NULL
&& pchFound != &pchPattern[2])
{
/* skip to the variable name. */
pchPattern += 2;
cchPattern -= 2;
size_t cchVarNm = pchFound - pchPattern;
/* Look it up. */
uint32_t iVar;
for (iVar = 0; iVar < RT_ELEMENTS(g_aVariables); iVar++)
if ( g_aVariables[iVar].cchName == cchVarNm
&& memcmp(g_aVariables[iVar].pszName, pchPattern, cchVarNm) == 0)
break;
if (iVar < RT_ELEMENTS(g_aVariables))
{
pInstr->uOp2 = (uint16_t)iVar;
pInstr->enmOpCode = !fIgnoreCase ? RTPATHMATCHOP_VARIABLE_VALUE_CMP : RTPATHMATCHOP_VARIABLE_VALUE_ICMP;
pInstr->pch = pchPattern; /* not necessary */
pInstr->cch = (uint16_t)cchPattern; /* ditto */
pchPattern += cchVarNm + 1;
cchPattern -= cchVarNm + 1;
AssertMsgReturn(!g_aVariables[iVar].fFirstOnly || iFirst + 1U == pAllocator->iNext,
("Glob variable '%s' should be first\n", g_aVariables[iVar].pszName),
VERR_PATH_MATCH_VARIABLE_MUST_BE_FIRST);
/* cchInput unchanged, value can be empty. */
continue;
}
AssertMsgFailedReturn(("Unknown path matching variable '%.*s'\n", cchVarNm, pchPattern),
VERR_PATH_MATCH_UNKNOWN_VARIABLE);
}
}
else
AssertFailedReturn(VERR_INTERNAL_ERROR_2); /* broken bitmap / compiler codeset */
}
/*
* Plain text. Look for the next meta char.
*/
uint32_t cchPlain = 1;
while (cchPlain < cchPattern)
{
ch = pchPattern[cchPlain];
if (!ASMBitTest(s_bmMetaChars, (uint8_t)ch))
{ /* probable */ }
else if ( ch == '?'
|| ch == '*')
break;
else if (ch == '$')
{
const char *pchFound;
if ( cchPattern > cchPlain + 3
&& pchPattern[cchPlain + 1] == '{'
&& (pchFound = (const char *)memchr(&pchPattern[cchPlain + 2], '}', cchPattern - cchPlain - 2)) != NULL
&& pchFound != &pchPattern[cchPlain + 2])
break;
}
else if (ch == '[')
{
/* We don't put a lot of effort into getting this 100% right here,
no point it complicating things for malformed expressions. */
if ( cchPattern > cchPlain + 2
&& memchr(&pchPattern[cchPlain + 2], ']', cchPattern - cchPlain - 1) != NULL)
break;
}
else
AssertFailedReturn(VERR_INTERNAL_ERROR_2); /* broken bitmap / compiler codeset */
cchPlain++;
}
pInstr->enmOpCode = !fIgnoreCase ? RTPATHMATCHOP_STRCMP : RTPATHMATCHOP_STRICMP;
pInstr->cch = cchPlain;
Assert(pInstr->pch == pchPattern);
Assert(pInstr->uOp2 == 0);
pchPattern += cchPlain;
cchPattern -= cchPlain;
offInput += cchPlain;
}
/*
* Optimize zero-or-more matching.
*/
while (cZeroOrMore-- > 0)
{
PRTPATHMATCHCORE pInstr = &pAllocator->paInstructions[aiZeroOrMore[cZeroOrMore]];
pInstr->uOp2 = (uint16_t)(offInput - pInstr->uOp2);
}
/** @todo It's possible to use offInput to inject a instruction for checking
* minimum input length at the start of the program. Not sure it's
* worth it though, unless it's long a complicated expression... */
return VINF_SUCCESS;
}
/**
* Parses the glob pattern.
*
* This compiles filename matching programs for each component and determins the
* optimal search strategy for them.
*
* @returns IPRT status code.
* @param pGlob The glob instance data.
* @param pszPattern The pattern to parse.
* @param pParsed The RTPathParse output for the pattern.
* @param fFlags The glob flags (same as pGlob->fFlags).
*/
static int rtPathGlobParse(PRTPATHGLOB pGlob, const char *pszPattern, PRTPATHPARSED pParsed, uint32_t fFlags)
{
AssertReturn(pParsed->cComps > 0, VERR_INVALID_PARAMETER); /* shouldn't happen */
uint32_t iComp = 0;
/*
* If we've got a rootspec, mark it as plain. On platforms with
* drive letter and/or UNC we don't allow wildcards or such in
* the drive letter spec or UNC server name. (At least not yet.)
*/
if (RTPATH_PROP_HAS_ROOT_SPEC(pParsed->fProps))
{
AssertReturn(pParsed->aComps[0].cch < sizeof(pGlob->szPath) - 1, VERR_FILENAME_TOO_LONG);
memcpy(pGlob->szPath, &pszPattern[pParsed->aComps[0].off], pParsed->aComps[0].cch);
pGlob->offFirstPath = pParsed->aComps[0].cch;
pGlob->iFirstComp = iComp = 1;
}
else
{
const char * const pszComp = &pszPattern[pParsed->aComps[0].off];
/*
* The tilde is only applicable to the first component, expand it
* immediately.
*/
if ( *pszComp == '~'
&& !(fFlags & RTPATHGLOB_F_NO_TILDE))
{
if (pParsed->aComps[0].cch == 1)
{
int rc = RTPathUserHome(pGlob->szPath, sizeof(pGlob->szPath) - 1);
AssertRCReturn(rc, rc);
}
else
AssertMsgFailedReturn(("'%.*s' is not supported yet\n", pszComp, pParsed->aComps[0].cch),
VERR_PATH_MATCH_FEATURE_NOT_IMPLEMENTED);
pGlob->offFirstPath = (uint32_t)RTPathEnsureTrailingSeparator(pGlob->szPath, sizeof(pGlob->szPath));
pGlob->iFirstComp = iComp = 1;
}
}
/*
* Process the other components.
*/
bool fStarStar = false;
for (; iComp < pParsed->cComps; iComp++)
{
const char *pszComp = &pszPattern[pParsed->aComps[iComp].off];
uint16_t cchComp = pParsed->aComps[iComp].cch;
Assert(pGlob->aComps[iComp].fNormal == false);
pGlob->aComps[iComp].fDir = iComp + 1 < pParsed->cComps || (fFlags & RTPATHGLOB_F_ONLY_DIRS);
if ( cchComp != 2
|| pszComp[0] != '*'
|| pszComp[1] != '*'
|| (fFlags & RTPATHGLOB_F_NO_STARSTAR) )
{
/* Compile the pattern. */
uint16_t const iMatchProg = pGlob->MatchInstrAlloc.iNext;
pGlob->aComps[iComp].iMatchProg = iMatchProg;
int rc = rtPathMatchCompile(pszComp, cchComp, RT_BOOL(fFlags & RTPATHGLOB_F_IGNORE_CASE),
&pGlob->MatchInstrAlloc);
if (RT_FAILURE(rc))
return rc;
/* Check for plain text as well as full variable matching (not applicable after '**'). */
uint16_t const cInstructions = pGlob->MatchInstrAlloc.iNext - iMatchProg;
if ( cInstructions == 2
&& !fStarStar
&& pGlob->MatchInstrAlloc.paInstructions[iMatchProg + 1].enmOpCode == RTPATHMATCHOP_RETURN_MATCH_IF_AT_END)
{
if ( pGlob->MatchInstrAlloc.paInstructions[iMatchProg].enmOpCode == RTPATHMATCHOP_STRCMP
|| pGlob->MatchInstrAlloc.paInstructions[iMatchProg].enmOpCode == RTPATHMATCHOP_STRICMP)
pGlob->aComps[iComp].fPlain = true;
else if ( pGlob->MatchInstrAlloc.paInstructions[iMatchProg].enmOpCode == RTPATHMATCHOP_VARIABLE_VALUE_CMP
|| pGlob->MatchInstrAlloc.paInstructions[iMatchProg].enmOpCode == RTPATHMATCHOP_VARIABLE_VALUE_ICMP)
{
pGlob->aComps[iComp].fExpVariable = true;
AssertMsgReturn( iComp == 0
|| !g_aVariables[pGlob->MatchInstrAlloc.paInstructions[iMatchProg].uOp2].fFirstOnly,
("Glob variable '%.*s' can only be used as the path component.\n", cchComp, pszComp),
VERR_PATH_MATCH_VARIABLE_MUST_BE_FIRST);
}
else
pGlob->aComps[iComp].fNormal = true;
}
else
pGlob->aComps[iComp].fNormal = true;
}
else
{
/* Recursive "**" matching. */
pGlob->aComps[iComp].fNormal = false;
pGlob->aComps[iComp].fStarStar = true;
AssertReturn(!fStarStar, VERR_PATH_MATCH_FEATURE_NOT_IMPLEMENTED); /** @todo implement multiple '**' sequences in a pattern. */
fStarStar = true;
}
}
pGlob->aComps[pParsed->cComps - 1].fFinal = true;
return VINF_SUCCESS;
}
/**
* This is for skipping overly long directories entries.
*
* Since our directory entry buffer can hold filenames of RTPATH_MAX bytes, we
* can safely skip filenames that are longer. There are very few file systems
* that can actually store filenames longer than 255 bytes at time of coding
* (2015-09), and extremely few which can exceed 4096 (RTPATH_MAX) bytes.
*
* @returns IPRT status code.
* @param hDir The directory handle.
* @param cbNeeded The required entry size.
*/
DECL_NO_INLINE(static, int) rtPathGlobSkipDirEntry(RTDIR hDir, size_t cbNeeded)
{
int rc = VERR_BUFFER_OVERFLOW;
cbNeeded = RT_ALIGN_Z(cbNeeded, 16);
PRTDIRENTRY pDirEntry = (PRTDIRENTRY)RTMemTmpAlloc(cbNeeded);
if (pDirEntry)
{
rc = RTDirRead(hDir, pDirEntry, &cbNeeded);
RTMemTmpFree(pDirEntry);
}
return rc;
}
/**
* Adds a result.
*
* @returns IPRT status code.
* @retval VINF_CALLBACK_RETURN if we can stop searching.
*
* @param pGlob The glob instance data.
* @param cchPath The number of bytes to add from pGlob->szPath.
* @param uType The RTDIRENTRYTYPE value.
*/
DECL_NO_INLINE(static, int) rtPathGlobAddResult(PRTPATHGLOB pGlob, size_t cchPath, uint8_t uType)
{
if (pGlob->cResults < RTPATHGLOB_MAX_RESULTS)
{
PRTPATHGLOBENTRY pEntry = (PRTPATHGLOBENTRY)RTMemAlloc(RT_UOFFSETOF_DYN(RTPATHGLOBENTRY, szPath[cchPath + 1]));
if (pEntry)
{
pEntry->uType = uType;
pEntry->cchPath = (uint16_t)cchPath;
memcpy(pEntry->szPath, pGlob->szPath, cchPath);
pEntry->szPath[cchPath] = '\0';
pEntry->pNext = NULL;
*pGlob->ppNext = pEntry;
pGlob->ppNext = &pEntry->pNext;
pGlob->cResults++;
if (!(pGlob->fFlags & RTPATHGLOB_F_FIRST_ONLY))
return VINF_SUCCESS;
return VINF_CALLBACK_RETURN;
}
return VERR_NO_MEMORY;
}
return VERR_TOO_MUCH_DATA;
}
/**
* Adds a result, constructing the path from two string.
*
* @returns IPRT status code.
* @retval VINF_CALLBACK_RETURN if we can stop searching.
*
* @param pGlob The glob instance data.
* @param cchPath The number of bytes to add from pGlob->szPath.
* @param pchName The string (usual filename) to append to the szPath.
* @param cchName The length of the string to append.
* @param uType The RTDIRENTRYTYPE value.
*/
DECL_NO_INLINE(static, int) rtPathGlobAddResult2(PRTPATHGLOB pGlob, size_t cchPath, const char *pchName, size_t cchName,
uint8_t uType)
{
if (pGlob->cResults < RTPATHGLOB_MAX_RESULTS)
{
PRTPATHGLOBENTRY pEntry = (PRTPATHGLOBENTRY)RTMemAlloc(RT_UOFFSETOF_DYN(RTPATHGLOBENTRY, szPath[cchPath + cchName + 1]));
if (pEntry)
{
pEntry->uType = uType;
pEntry->cchPath = (uint16_t)(cchPath + cchName);
memcpy(pEntry->szPath, pGlob->szPath, cchPath);
memcpy(&pEntry->szPath[cchPath], pchName, cchName);
pEntry->szPath[cchPath + cchName] = '\0';
pEntry->pNext = NULL;
*pGlob->ppNext = pEntry;
pGlob->ppNext = &pEntry->pNext;
pGlob->cResults++;
if (!(pGlob->fFlags & RTPATHGLOB_F_FIRST_ONLY))
return VINF_SUCCESS;
return VINF_CALLBACK_RETURN;
}
return VERR_NO_MEMORY;
}
return VERR_TOO_MUCH_DATA;
}
/**
* Prepares a result, constructing the path from two string.
*
* The caller must call either rtPathGlobCommitResult or
* rtPathGlobRollbackResult to complete the operation.
*
* @returns IPRT status code.
* @retval VINF_CALLBACK_RETURN if we can stop searching.
*
* @param pGlob The glob instance data.
* @param cchPath The number of bytes to add from pGlob->szPath.
* @param pchName The string (usual filename) to append to the szPath.
* @param cchName The length of the string to append.
* @param uType The RTDIRENTRYTYPE value.
*/
DECL_NO_INLINE(static, int) rtPathGlobAlmostAddResult(PRTPATHGLOB pGlob, size_t cchPath, const char *pchName, size_t cchName,
uint8_t uType)
{
if (pGlob->cResults < RTPATHGLOB_MAX_RESULTS)
{
PRTPATHGLOBENTRY pEntry = (PRTPATHGLOBENTRY)RTMemAlloc(RT_UOFFSETOF_DYN(RTPATHGLOBENTRY, szPath[cchPath + cchName + 1]));
if (pEntry)
{
pEntry->uType = uType;
pEntry->cchPath = (uint16_t)(cchPath + cchName);
memcpy(pEntry->szPath, pGlob->szPath, cchPath);
memcpy(&pEntry->szPath[cchPath], pchName, cchName);
pEntry->szPath[cchPath + cchName] = '\0';
pEntry->pNext = NULL;
*pGlob->ppNext = pEntry;
/* Note! We don't update ppNext here, that is done in rtPathGlobCommitResult. */
if (!(pGlob->fFlags & RTPATHGLOB_F_FIRST_ONLY))
return VINF_SUCCESS;
return VINF_CALLBACK_RETURN;
}
return VERR_NO_MEMORY;
}
return VERR_TOO_MUCH_DATA;
}
/**
* Commits a pending result from rtPathGlobAlmostAddResult.
*
* @param pGlob The glob instance data.
* @param uType The RTDIRENTRYTYPE value.
*/
static void rtPathGlobCommitResult(PRTPATHGLOB pGlob, uint8_t uType)
{
PRTPATHGLOBENTRY pEntry = *pGlob->ppNext;
AssertPtr(pEntry);
pEntry->uType = uType;
pGlob->ppNext = &pEntry->pNext;
pGlob->cResults++;
}
/**
* Rolls back a pending result from rtPathGlobAlmostAddResult.
*
* @param pGlob The glob instance data.
*/
static void rtPathGlobRollbackResult(PRTPATHGLOB pGlob)
{
PRTPATHGLOBENTRY pEntry = *pGlob->ppNext;
AssertPtr(pEntry);
RTMemFree(pEntry);
*pGlob->ppNext = NULL;
}
/**
* Whether to call rtPathGlobExecRecursiveVarExp for the next component.
*
* @returns true / false.
* @param pGlob The glob instance data.
* @param offPath The next path offset/length.
* @param iComp The next component.
*/
DECLINLINE(bool) rtPathGlobExecIsExpVar(PRTPATHGLOB pGlob, size_t offPath, uint32_t iComp)
{
return pGlob->aComps[iComp].fExpVariable
&& ( !(pGlob->fFlags & RTPATHGLOB_F_IGNORE_CASE)
|| (offPath ? !RTFsIsCaseSensitive(pGlob->szPath) : !RTFsIsCaseSensitive(".")) );
}
/**
* Whether to call rtPathGlobExecRecursivePlainText for the next component.
*
* @returns true / false.
* @param pGlob The glob instance data.
* @param offPath The next path offset/length.
* @param iComp The next component.
*/
DECLINLINE(bool) rtPathGlobExecIsPlainText(PRTPATHGLOB pGlob, size_t offPath, uint32_t iComp)
{
return pGlob->aComps[iComp].fPlain
&& ( !(pGlob->fFlags & RTPATHGLOB_F_IGNORE_CASE)
|| (offPath ? !RTFsIsCaseSensitive(pGlob->szPath) : !RTFsIsCaseSensitive(".")) );
}
/**
* Helper for rtPathGlobExecRecursiveVarExp and rtPathGlobExecRecursivePlainText
* that compares a file mode mask with dir/no-dir wishes of the caller.
*
* @returns true if match, false if not.
* @param pGlob The glob instance data.
* @param fMode The file mode (only the type is used).
*/
DECLINLINE(bool) rtPathGlobExecIsMatchFinalWithFileMode(PRTPATHGLOB pGlob, RTFMODE fMode)
{
if (!(pGlob->fFlags & (RTPATHGLOB_F_NO_DIRS | RTPATHGLOB_F_ONLY_DIRS)))
return true;
return RT_BOOL(pGlob->fFlags & RTPATHGLOB_F_ONLY_DIRS) == RTFS_IS_DIRECTORY(fMode);
}
/**
* Recursive globbing - star-star mode.
*
* @returns IPRT status code.
* @retval VINF_CALLBACK_RETURN is used to implement RTPATHGLOB_F_FIRST_ONLY.
*
* @param pGlob The glob instance data.
* @param offPath The current path offset/length.
* @param iStarStarComp The star-star component index.
* @param offStarStarPath The offset of the star-star component in the
* pattern path.
*/
DECL_NO_INLINE(static, int) rtPathGlobExecRecursiveStarStar(PRTPATHGLOB pGlob, size_t offPath, uint32_t iStarStarComp,
size_t offStarStarPath)
{
/** @todo implement multi subdir matching. */
RT_NOREF_PV(pGlob);
RT_NOREF_PV(offPath);
RT_NOREF_PV(iStarStarComp);
RT_NOREF_PV(offStarStarPath);
return VERR_PATH_MATCH_FEATURE_NOT_IMPLEMENTED;
}
/**
* Recursive globbing - variable expansion optimization.
*
* @returns IPRT status code.
* @retval VINF_CALLBACK_RETURN is used to implement RTPATHGLOB_F_FIRST_ONLY.
*
* @param pGlob The glob instance data.
* @param offPath The current path offset/length.
* @param iComp The current component.
*/
DECL_NO_INLINE(static, int) rtPathGlobExecRecursiveVarExp(PRTPATHGLOB pGlob, size_t offPath, uint32_t iComp)
{
Assert(iComp < pGlob->pParsed->cComps);
Assert(pGlob->szPath[offPath] == '\0');
Assert(pGlob->aComps[iComp].fExpVariable);
Assert(!pGlob->aComps[iComp].fPlain);
Assert(!pGlob->aComps[iComp].fStarStar);
Assert(rtPathGlobExecIsExpVar(pGlob, offPath, iComp));
/*
* Fish the variable index out of the first matching instruction.
*/
Assert( pGlob->MatchInstrAlloc.paInstructions[pGlob->aComps[iComp].iMatchProg].enmOpCode
== RTPATHMATCHOP_VARIABLE_VALUE_CMP
|| pGlob->MatchInstrAlloc.paInstructions[pGlob->aComps[iComp].iMatchProg].enmOpCode
== RTPATHMATCHOP_VARIABLE_VALUE_ICMP);
uint16_t const iVar = pGlob->MatchInstrAlloc.paInstructions[pGlob->aComps[iComp].iMatchProg].uOp2;
/*
* Enumerate all the variable, giving them the plain text treatment.
*/
for (uint32_t iItem = 0; iItem < RTPATHMATCH_MAX_VAR_ITEMS; iItem++)
{
size_t cch;
int rcVar = g_aVariables[iVar].pfnQuery(iItem, &pGlob->szPath[offPath], sizeof(pGlob->szPath) - offPath, &cch,
&pGlob->MatchCache);
if (RT_SUCCESS(rcVar))
{
Assert(pGlob->szPath[offPath + cch] == '\0');
int rc = RTPathQueryInfoEx(pGlob->szPath, &pGlob->u.ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
if (RT_SUCCESS(rc))
{
if (pGlob->aComps[iComp].fFinal)
{
if (rtPathGlobExecIsMatchFinalWithFileMode(pGlob, pGlob->u.ObjInfo.Attr.fMode))
{
rc = rtPathGlobAddResult(pGlob, cch,
(pGlob->u.ObjInfo.Attr.fMode & RTFS_TYPE_MASK)
>> RTFS_TYPE_DIRENTRYTYPE_SHIFT);
if (rc != VINF_SUCCESS)
return rc;
}
}
else if (RTFS_IS_DIRECTORY(pGlob->u.ObjInfo.Attr.fMode))
{
Assert(pGlob->aComps[iComp].fDir);
cch = RTPathEnsureTrailingSeparator(pGlob->szPath, sizeof(pGlob->szPath));
if (cch > 0)
{
if (rtPathGlobExecIsExpVar(pGlob, cch, iComp + 1))
rc = rtPathGlobExecRecursiveVarExp(pGlob, cch, iComp + 1);
else if (rtPathGlobExecIsPlainText(pGlob, cch, iComp + 1))
rc = rtPathGlobExecRecursivePlainText(pGlob, cch, iComp + 1);
else if (pGlob->aComps[pGlob->iFirstComp].fStarStar)
rc = rtPathGlobExecRecursiveStarStar(pGlob, cch, iComp + 1, cch);
else
rc = rtPathGlobExecRecursiveGeneric(pGlob, cch, iComp + 1);
if (rc != VINF_SUCCESS)
return rc;
}
else
pGlob->cPathOverflows++;
}
}
/* else: file doesn't exist or something else is wrong, ignore this. */
if (rcVar == VINF_EOF)
return VINF_SUCCESS;
}
else if (rcVar == VERR_EOF)
return VINF_SUCCESS;
else if (rcVar != VERR_TRY_AGAIN)
{
Assert(rcVar == VERR_BUFFER_OVERFLOW);
pGlob->cPathOverflows++;
}
}
AssertFailedReturn(VINF_SUCCESS); /* Too many items returned, probably buggy query method. */
}
/**
* Recursive globbing - plain text optimization.
*
* @returns IPRT status code.
* @retval VINF_CALLBACK_RETURN is used to implement RTPATHGLOB_F_FIRST_ONLY.
*
* @param pGlob The glob instance data.
* @param offPath The current path offset/length.
* @param iComp The current component.
*/
DECL_NO_INLINE(static, int) rtPathGlobExecRecursivePlainText(PRTPATHGLOB pGlob, size_t offPath, uint32_t iComp)
{
/*
* Instead of recursing, we loop thru adjacent plain text components.
*/
for (;;)
{
/*
* Preconditions.
*/
Assert(iComp < pGlob->pParsed->cComps);
Assert(pGlob->szPath[offPath] == '\0');
Assert(pGlob->aComps[iComp].fPlain);
Assert(!pGlob->aComps[iComp].fExpVariable);
Assert(!pGlob->aComps[iComp].fStarStar);
Assert(rtPathGlobExecIsPlainText(pGlob, offPath, iComp));
Assert(pGlob->MatchInstrAlloc.paInstructions[pGlob->aComps[iComp].iMatchProg].enmOpCode
== RTPATHMATCHOP_STRCMP
|| pGlob->MatchInstrAlloc.paInstructions[pGlob->aComps[iComp].iMatchProg].enmOpCode
== RTPATHMATCHOP_STRICMP);
/*
* Add the plain text component to the path.
*/
size_t const cch = pGlob->pParsed->aComps[iComp].cch;
if (cch + pGlob->aComps[iComp].fDir < sizeof(pGlob->szPath) - offPath)
{
memcpy(&pGlob->szPath[offPath], &pGlob->pszPattern[pGlob->pParsed->aComps[iComp].off], cch);
offPath += cch;
pGlob->szPath[offPath] = '\0';
/*
* Check if it exists.
*/
int rc = RTPathQueryInfoEx(pGlob->szPath, &pGlob->u.ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
if (RT_SUCCESS(rc))
{
if (pGlob->aComps[iComp].fFinal)
{
if (rtPathGlobExecIsMatchFinalWithFileMode(pGlob, pGlob->u.ObjInfo.Attr.fMode))
return rtPathGlobAddResult(pGlob, offPath,
(pGlob->u.ObjInfo.Attr.fMode & RTFS_TYPE_MASK)
>> RTFS_TYPE_DIRENTRYTYPE_SHIFT);
break;
}
if (RTFS_IS_DIRECTORY(pGlob->u.ObjInfo.Attr.fMode))
{
Assert(pGlob->aComps[iComp].fDir);
pGlob->szPath[offPath++] = RTPATH_SLASH;
pGlob->szPath[offPath] = '\0';
iComp++;
if (rtPathGlobExecIsExpVar(pGlob, offPath, iComp))
return rtPathGlobExecRecursiveVarExp(pGlob, offPath, iComp);
if (!rtPathGlobExecIsPlainText(pGlob, offPath, iComp))
return rtPathGlobExecRecursiveGeneric(pGlob, offPath, iComp);
if (pGlob->aComps[pGlob->iFirstComp].fStarStar)
return rtPathGlobExecRecursiveStarStar(pGlob, offPath, iComp, offPath);
/* Continue with the next plain text component. */
continue;
}
}
/* else: file doesn't exist or something else is wrong, ignore this. */
}
else
pGlob->cPathOverflows++;
break;
}
return VINF_SUCCESS;
}
/**
* Recursive globbing - generic.
*
* @returns IPRT status code.
* @retval VINF_CALLBACK_RETURN is used to implement RTPATHGLOB_F_FIRST_ONLY.
*
* @param pGlob The glob instance data.
* @param offPath The current path offset/length.
* @param iComp The current component.
*/
DECL_NO_INLINE(static, int) rtPathGlobExecRecursiveGeneric(PRTPATHGLOB pGlob, size_t offPath, uint32_t iComp)
{
/*
* Enumerate entire directory and match each entry.
*/
RTDIR hDir;
int rc = RTDirOpen(&hDir, offPath ? pGlob->szPath : ".");
if (RT_SUCCESS(rc))
{
for (;;)
{
size_t cch = sizeof(pGlob->u);
rc = RTDirRead(hDir, &pGlob->u.DirEntry, &cch);
if (RT_SUCCESS(rc))
{
if (pGlob->aComps[iComp].fFinal)
{
/*
* Final component: Check if it matches the current pattern.
*/
if ( !(pGlob->fFlags & (RTPATHGLOB_F_NO_DIRS | RTPATHGLOB_F_ONLY_DIRS))
|| RT_BOOL(pGlob->fFlags & RTPATHGLOB_F_ONLY_DIRS)
== (pGlob->u.DirEntry.enmType == RTDIRENTRYTYPE_DIRECTORY)
|| pGlob->u.DirEntry.enmType == RTDIRENTRYTYPE_UNKNOWN)
{
rc = rtPathMatchExec(pGlob->u.DirEntry.szName, pGlob->u.DirEntry.cbName,
&pGlob->MatchInstrAlloc.paInstructions[pGlob->aComps[iComp].iMatchProg],
&pGlob->MatchCache);
if (RT_SUCCESS(rc))
{
/* Construct the result. */
if ( pGlob->u.DirEntry.enmType != RTDIRENTRYTYPE_UNKNOWN
|| !(pGlob->fFlags & (RTPATHGLOB_F_NO_DIRS | RTPATHGLOB_F_ONLY_DIRS)) )
rc = rtPathGlobAddResult2(pGlob, offPath, pGlob->u.DirEntry.szName, pGlob->u.DirEntry.cbName,
(uint8_t)pGlob->u.DirEntry.enmType);
else
{
rc = rtPathGlobAlmostAddResult(pGlob, offPath,
pGlob->u.DirEntry.szName, pGlob->u.DirEntry.cbName,
(uint8_t)RTDIRENTRYTYPE_UNKNOWN);
if (RT_SUCCESS(rc))
{
RTDirQueryUnknownType((*pGlob->ppNext)->szPath, false /*fFollowSymlinks*/,
&pGlob->u.DirEntry.enmType);
if ( RT_BOOL(pGlob->fFlags & RTPATHGLOB_F_ONLY_DIRS)
== (pGlob->u.DirEntry.enmType == RTDIRENTRYTYPE_DIRECTORY))
rtPathGlobCommitResult(pGlob, (uint8_t)pGlob->u.DirEntry.enmType);
else
rtPathGlobRollbackResult(pGlob);
}
}
if (rc != VINF_SUCCESS)
break;
}
else
{
AssertMsgBreak(rc == VERR_MISMATCH, ("%Rrc\n", rc));
rc = VINF_SUCCESS;
}
}
}
/*
* Intermediate component: Directories only.
*/
else if ( pGlob->u.DirEntry.enmType == RTDIRENTRYTYPE_DIRECTORY
|| pGlob->u.DirEntry.enmType == RTDIRENTRYTYPE_UNKNOWN)
{
rc = rtPathMatchExec(pGlob->u.DirEntry.szName, pGlob->u.DirEntry.cbName,
&pGlob->MatchInstrAlloc.paInstructions[pGlob->aComps[iComp].iMatchProg],
&pGlob->MatchCache);
if (RT_SUCCESS(rc))
{
/* Recurse down into the alleged directory. */
cch = offPath + pGlob->u.DirEntry.cbName;
if (cch + 1 < sizeof(pGlob->szPath))
{
memcpy(&pGlob->szPath[offPath], pGlob->u.DirEntry.szName, pGlob->u.DirEntry.cbName);
pGlob->szPath[cch++] = RTPATH_SLASH;
pGlob->szPath[cch] = '\0';
if (rtPathGlobExecIsExpVar(pGlob, cch, iComp + 1))
rc = rtPathGlobExecRecursiveVarExp(pGlob, cch, iComp + 1);
else if (rtPathGlobExecIsPlainText(pGlob, cch, iComp + 1))
rc = rtPathGlobExecRecursivePlainText(pGlob, cch, iComp + 1);
else if (pGlob->aComps[pGlob->iFirstComp].fStarStar)
rc = rtPathGlobExecRecursiveStarStar(pGlob, cch, iComp + 1, cch);
else
rc = rtPathGlobExecRecursiveGeneric(pGlob, cch, iComp + 1);
if (rc != VINF_SUCCESS)
return rc;
}
else
pGlob->cPathOverflows++;
}
else
{
AssertMsgBreak(rc == VERR_MISMATCH, ("%Rrc\n", rc));
rc = VINF_SUCCESS;
}
}
}
/*
* RTDirRead failure.
*/
else
{
/* The end? */
if (rc == VERR_NO_MORE_FILES)
rc = VINF_SUCCESS;
/* Try skip the entry if we end up with an overflow (szPath can't hold it either then). */
else if (rc == VERR_BUFFER_OVERFLOW)
{
pGlob->cPathOverflows++;
rc = rtPathGlobSkipDirEntry(hDir, cch);
if (RT_SUCCESS(rc))
continue;
}
/* else: Any other error is unexpected and should be reported. */
break;
}
}
RTDirClose(hDir);
}
/* Directory doesn't exist or something else is wrong, ignore this. */
else
rc = VINF_SUCCESS;
return rc;
}
/**
* Executes a glob search.
*
* @returns IPRT status code.
* @param pGlob The glob instance data.
*/
static int rtPathGlobExec(PRTPATHGLOB pGlob)
{
Assert(pGlob->offFirstPath < sizeof(pGlob->szPath));
Assert(pGlob->szPath[pGlob->offFirstPath] == '\0');
int rc;
if (RT_LIKELY(pGlob->iFirstComp < pGlob->pParsed->cComps))
{
/*
* Call the appropriate function.
*/
if (rtPathGlobExecIsExpVar(pGlob, pGlob->offFirstPath, pGlob->iFirstComp))
rc = rtPathGlobExecRecursiveVarExp(pGlob, pGlob->offFirstPath, pGlob->iFirstComp);
else if (rtPathGlobExecIsPlainText(pGlob, pGlob->offFirstPath, pGlob->iFirstComp))
rc = rtPathGlobExecRecursivePlainText(pGlob, pGlob->offFirstPath, pGlob->iFirstComp);
else if (pGlob->aComps[pGlob->iFirstComp].fStarStar)
rc = rtPathGlobExecRecursiveStarStar(pGlob, pGlob->offFirstPath, pGlob->iFirstComp, pGlob->offFirstPath);
else
rc = rtPathGlobExecRecursiveGeneric(pGlob, pGlob->offFirstPath, pGlob->iFirstComp);
}
else
{
/*
* Special case where we only have a root component or tilde expansion.
*/
Assert(pGlob->offFirstPath > 0);
rc = RTPathQueryInfoEx(pGlob->szPath, &pGlob->u.ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
if ( RT_SUCCESS(rc)
&& rtPathGlobExecIsMatchFinalWithFileMode(pGlob, pGlob->u.ObjInfo.Attr.fMode))
rc = rtPathGlobAddResult(pGlob, pGlob->offFirstPath,
(pGlob->u.ObjInfo.Attr.fMode & RTFS_TYPE_MASK) >> RTFS_TYPE_DIRENTRYTYPE_SHIFT);
else
rc = VINF_SUCCESS;
}
/*
* Adjust the status code. Check for results, hide RTPATHGLOB_F_FIRST_ONLY
* status code, and add warning if necessary.
*/
if (pGlob->cResults > 0)
{
if (rc == VINF_CALLBACK_RETURN)
rc = VINF_SUCCESS;
if (rc == VINF_SUCCESS)
{
if (pGlob->cPathOverflows > 0)
rc = VINF_BUFFER_OVERFLOW;
}
}
else
rc = VERR_FILE_NOT_FOUND;
return rc;
}
RTDECL(int) RTPathGlob(const char *pszPattern, uint32_t fFlags, PPCRTPATHGLOBENTRY ppHead, uint32_t *pcResults)
{
/*
* Input validation.
*/
AssertPtrReturn(ppHead, VERR_INVALID_POINTER);
*ppHead = NULL;
if (pcResults)
{
AssertPtrReturn(pcResults, VERR_INVALID_POINTER);
*pcResults = 0;
}
AssertPtrReturn(pszPattern, VERR_INVALID_POINTER);
AssertReturn(!(fFlags & ~RTPATHGLOB_F_MASK), VERR_INVALID_FLAGS);
AssertReturn((fFlags & (RTPATHGLOB_F_NO_DIRS | RTPATHGLOB_F_ONLY_DIRS)) != (RTPATHGLOB_F_NO_DIRS | RTPATHGLOB_F_ONLY_DIRS),
VERR_INVALID_FLAGS);
/*
* Parse the path.
*/
size_t cbParsed = RT_UOFFSETOF(RTPATHPARSED, aComps[1]); /** @todo 16 after testing */
PRTPATHPARSED pParsed = (PRTPATHPARSED)RTMemTmpAlloc(cbParsed);
AssertReturn(pParsed, VERR_NO_MEMORY);
int rc = RTPathParse(pszPattern, pParsed, cbParsed, RTPATH_STR_F_STYLE_HOST);
if (rc == VERR_BUFFER_OVERFLOW)
{
cbParsed = RT_UOFFSETOF_DYN(RTPATHPARSED, aComps[pParsed->cComps + 1]);
RTMemTmpFree(pParsed);
pParsed = (PRTPATHPARSED)RTMemTmpAlloc(cbParsed);
AssertReturn(pParsed, VERR_NO_MEMORY);
rc = RTPathParse(pszPattern, pParsed, cbParsed, RTPATH_STR_F_STYLE_HOST);
}
if (RT_SUCCESS(rc))
{
/*
* Check dir slash vs. only/not dir flag.
*/
if ( !(fFlags & RTPATHGLOB_F_NO_DIRS)
|| ( !(pParsed->fProps & RTPATH_PROP_DIR_SLASH)
&& ( !(pParsed->fProps & (RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_UNC))
|| pParsed->cComps > 1) ) )
{
if (pParsed->fProps & RTPATH_PROP_DIR_SLASH)
fFlags |= RTPATHGLOB_F_ONLY_DIRS;
/*
* Allocate and initialize the glob state data structure.
*/
size_t cbGlob = RT_UOFFSETOF_DYN(RTPATHGLOB, aComps[pParsed->cComps + 1]);
PRTPATHGLOB pGlob = (PRTPATHGLOB)RTMemTmpAllocZ(cbGlob);
if (pGlob)
{
pGlob->pszPattern = pszPattern;
pGlob->fFlags = fFlags;
pGlob->pParsed = pParsed;
pGlob->ppNext = &pGlob->pHead;
rc = rtPathGlobParse(pGlob, pszPattern, pParsed, fFlags);
if (RT_SUCCESS(rc))
{
/*
* Execute the search.
*/
rc = rtPathGlobExec(pGlob);
if (RT_SUCCESS(rc))
{
*ppHead = pGlob->pHead;
if (pcResults)
*pcResults = pGlob->cResults;
}
else
RTPathGlobFree(pGlob->pHead);
}
RTMemTmpFree(pGlob->MatchInstrAlloc.paInstructions);
RTMemTmpFree(pGlob);
}
else
rc = VERR_NO_MEMORY;
}
else
rc = VERR_NOT_FOUND;
}
RTMemTmpFree(pParsed);
return rc;
}
RTDECL(void) RTPathGlobFree(PCRTPATHGLOBENTRY pHead)
{
PRTPATHGLOBENTRY pCur = (PRTPATHGLOBENTRY)pHead;
while (pCur)
{
PRTPATHGLOBENTRY pNext = pCur->pNext;
pCur->pNext = NULL;
RTMemFree(pCur);
pCur = pNext;
}
}
|