-
Notifications
You must be signed in to change notification settings - Fork 0
/
myglob.c
429 lines (373 loc) · 12.9 KB
/
myglob.c
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
//--------------------------------------------------------------------------------
// Module to do recursive directory file matching under windows.
//
// Tries to do pattern matching to produce similar results as Unix, but using
// the Windows _findfirst to do all the pattern matching.
//
// Also handles recursive directories - "**" path component expands into
// any levels of subdirectores (ie c:\**\*.c matches ALL .c files on drive c:)
//
// Matthias Wandel Nov 5 2000 - March 2009
//
// Version 1.24
// Copyright (C) May 2017 thomas694 (@GH 0CFD61744DA1A21C)
// added support for multiple ref patterns
// Version 1.25
// Copyright (C) Jun 2017 thomas694
// added unicode support
//
// This file is part of finddupe.
//
// finddupe is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//--------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <errno.h>
#include <ctype.h>
#include <io.h>
#include <sys/stat.h>
#define WIN32_LEAN_AND_MEAN // To keep windows.h bloat down.
#include <windows.h>
#define TRUE 1
#define FALSE 0
//#define DEBUGGING
#define REF_CODE
#ifdef REF_CODE
extern TCHAR* * PathData;
extern int PathAllocated;
extern int PathUnique;
extern int ReferenceFiles;
#endif
typedef struct {
TCHAR * Name;
int attrib;
}FileEntry;
#ifdef DEBUGGING
//--------------------------------------------------------------------------------
// Dummy function to show operation.
//--------------------------------------------------------------------------------
void ShowName(const TCHAR * FileName)
{
_tprintf(TEXT(" %s\n"), FileName);
}
#endif
//--------------------------------------------------------------------------------
// Simple path splicing (assumes no '\' in either part)
//--------------------------------------------------------------------------------
static int CatPath(TCHAR * dest, const TCHAR * p1, const TCHAR * p2)
{
int l;
l = _tcslen(p1);
if (!l){
_tcscpy(dest, p2);
}else{
if (l+_tcslen(p2) > _MAX_PATH-2){
//fprintf(stderr,"\n\n\nPath too long: \n %s + %s\n",p1,p2);
return 0;
}
#ifdef UNICODE
wmemcpy(dest, p1, l+1);
#else
memcpy(dest, p1, l+1);
#endif
if (dest[l-1] != '\\' && dest[l-1] != ':'){
dest[l++] = '\\';
}
_tcscpy(dest+l, p2);
}
return 1;
}
//--------------------------------------------------------------------------------
// Qsort compare function
//--------------------------------------------------------------------------------
int CompareFunc(const void * f1, const void * f2)
{
return _tcscmp(((FileEntry *)f1)->Name,((FileEntry *)f2)->Name);
}
//--------------------------------------------------------------------------------
// Check if directory is a reparse point
//--------------------------------------------------------------------------------
int IsReparsePoint(TCHAR * DirName)
{
HANDLE FileHandle;
BY_HANDLE_FILE_INFORMATION FileInfo;
FileHandle = CreateFile(DirName,
0, // dwDesiredAccess
FILE_SHARE_READ, // dwShareMode
NULL, // Security attirbutes
OPEN_EXISTING, // dwCreationDisposition
FILE_FLAG_BACKUP_SEMANTICS | // dwFlagsAndAttributes. Need this to do dirs.
FILE_FLAG_OPEN_REPARSE_POINT, // Need this flag to open the reparse point instead of following it.
NULL); // hTemplateFile. Ignored for existing.
if (FileHandle == (void *)-1){
return FALSE;
}
if (!GetFileInformationByHandle(FileHandle, &FileInfo)){
return FALSE;
}
// Directory node is in: FileInfo.nFileIndexHigh, FileInfo.nFileIndexLow
if (FileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT){
return TRUE;
}else{
return FALSE;
}
}
//--------------------------------------------------------------------------------
// Decide how a particular pattern should be handled, and call function for each.
//--------------------------------------------------------------------------------
static void Recurse(const TCHAR * Pattern, int FollowReparse, void (*FileFuncParm)(const TCHAR * FileName))
{
TCHAR BasePattern[_MAX_PATH];
TCHAR MatchPattern[_MAX_PATH];
TCHAR PatCopy[_MAX_PATH*2];
#ifdef REF_CODE
TCHAR * refpath;
#endif
int a;
int MatchDirs;
int BaseEnd, PatternEnd;
int SawPat;
int StarStarAt;
_tcscpy(PatCopy, Pattern);
#ifdef DEBUGGING
_tprintf(TEXT("\nCalled with '%s'\n"), Pattern);
#endif
DoExtraLevel:
MatchDirs = TRUE;
BaseEnd = 0;
PatternEnd = 0;
SawPat = FALSE;
StarStarAt = -1;
// Split the path into base path and pattern to match against using findfirst.
for (a=0;;a++){
if (PatCopy[a] == '*' || PatCopy[a] == '?'){
SawPat = TRUE;
}
if (PatCopy[a] == '*' && PatCopy[a+1] == '*'){
if (a == 0 || PatCopy[a-1] == '\\' || PatCopy[a-1] == ':'){
if (PatCopy[a+2] == '\\' || PatCopy[a+2] == '\0'){
// x\**\y ---> x\y x\*\**\y
StarStarAt = a;
if (PatCopy[a+2]){
#ifdef UNICODE
wmemcpy(PatCopy+a, PatCopy+a+3, _tcslen(PatCopy)-a-1);
#else
memcpy(PatCopy+a, PatCopy+a+3, _tcslen(PatCopy)-a-1);
#endif
}else{
PatCopy[a+1] = '\0';
}
}
}
}
if (PatCopy[a] == '\\' || (PatCopy[a] == ':' && PatCopy[a+1] != '\\')){
PatternEnd = a;
if (SawPat) break; // Findfirst can only match one level of wildcard at a time.
BaseEnd = a+1;
}
if (PatCopy[a] == '\0'){
PatternEnd = a;
MatchDirs = FALSE;
break;
}
}
_tcsncpy(BasePattern, PatCopy, BaseEnd);
BasePattern[BaseEnd] = 0;
_tcsncpy(MatchPattern, PatCopy, PatternEnd);
MatchPattern[PatternEnd] = 0;
#ifdef DEBUGGING
_tprintf(TEXT("Base:%s Pattern:%s dirs:%d\n"), BasePattern, MatchPattern, MatchDirs);
#endif
#ifdef REF_CODE
if (MatchDirs == 0 && ReferenceFiles) {
if (PathUnique >= PathAllocated) {
// Array is full. Make it bigger
PathAllocated = PathAllocated + PathAllocated/2;
PathData = realloc(PathData, sizeof(TCHAR*) * PathAllocated);
if (PathData == NULL){
_ftprintf(stderr, TEXT("Malloc failure"));
exit(EXIT_FAILURE);
}
};
refpath = _tcsdup(BasePattern);
PathData[PathUnique] = refpath;
PathUnique += 1;
}
#endif
{
FileEntry * FileList = NULL;
int NumAllocated = 0;
int NumHave = 0;
struct _tfinddata_t finddata;
intptr_t find_handle;
find_handle = _tfindfirst(MatchPattern, &finddata);
for (;;){
if (find_handle == -1) break;
// Eliminate the obvious patterns.
#ifdef UNICODE
if (!wmemcmp(finddata.name, L".", 2)) goto next_file;
if (!wmemcmp(finddata.name, L"..", 3)) goto next_file;
#else
if (!memcmp(finddata.name, ".",2)) goto next_file;
if (!memcmp(finddata.name, "..",3)) goto next_file;
#endif
if (finddata.attrib & _A_SUBDIR){
if (!MatchDirs) goto next_file;
}else{
if (MatchDirs) goto next_file;
}
// Add it to the list.
if (NumAllocated <= NumHave){
NumAllocated = NumAllocated+10+NumAllocated/2;
FileList = realloc(FileList, NumAllocated * sizeof(FileEntry));
if (FileList == NULL) goto nomem;
}
a = _tcslen(finddata.name);
FileList[NumHave].Name = malloc((a+1)*sizeof(TCHAR));
if (FileList[NumHave].Name == NULL){
nomem:
_tprintf(TEXT("malloc failure\n"));
exit(-1);
}
#ifdef UNICODE
wmemcpy(FileList[NumHave].Name, finddata.name, a+1);
#else
memcpy(FileList[NumHave].Name, finddata.name, a+1);
#endif
FileList[NumHave].attrib = finddata.attrib;
NumHave++;
next_file:
if (_tfindnext(find_handle, &finddata) != 0) break;
}
_findclose(find_handle);
// Sort the list...
qsort(FileList, NumHave, sizeof(FileEntry), CompareFunc);
// Use the list.
for (a=0;a<NumHave;a++){
TCHAR CombinedName[_MAX_PATH*2];
if (FileList[a].attrib & _A_SUBDIR){
if (CatPath(CombinedName, BasePattern, FileList[a].Name)){
if (FollowReparse || !IsReparsePoint(CombinedName)){
_tcscat(CombinedName, PatCopy+PatternEnd);
Recurse(CombinedName, FollowReparse, FileFuncParm);
}
}
}else{
if (CatPath(CombinedName, BasePattern, FileList[a].Name)){
FileFuncParm(CombinedName);
}
}
free(FileList[a].Name);
}
free(FileList);
}
if(StarStarAt >= 0){
_tcscpy(MatchPattern, PatCopy+StarStarAt);
PatCopy[StarStarAt] = 0;
_tcscpy(PatCopy+StarStarAt, TEXT("*\\**\\"));
_tcscat(PatCopy, MatchPattern);
#ifdef DEBUGGING
_tprintf(TEXT("Recurse with '%s'\n"), PatCopy);
#endif
// As this function context is no longer needed, we can just goto back
// to the top of it to avoid adding another context on the stack.
goto DoExtraLevel;
}
}
//--------------------------------------------------------------------------------
// Do quick precheck - if no wildcards, and it names a directory, do whole dir.
//--------------------------------------------------------------------------------
int MyGlob(const TCHAR * Pattern, int FollowReparse, void (*FileFuncParm)(const TCHAR * FileName))
{
int a;
TCHAR PathCopy[_MAX_PATH];
_tcsncpy(PathCopy, Pattern, _MAX_PATH-1);
a = _tcslen(PathCopy);
if (a && PathCopy[a-1] == '\\'){ // Endsi with backslash
if (!(a == 3 && PathCopy[1] == ':')){
// and its not something like c:\, then delete the trailing backslash
PathCopy[a-1] = '\0';
}
}
for (a=0;;a++){
if (PathCopy[a] == '*' || PathCopy[a] == '?') break; // Contains wildcards
if (PathCopy[a] == '\0') break;
}
if (PathCopy[a] == '\0'){
// No wildcards were specified. Do a whole tree, or file.
struct _stat FileStat;
if (_tstat(PathCopy, &FileStat) != 0){
// There is no file or directory by that name.
return -1;
_tprintf(TEXT("Stat failed\n"));
}
if (FileStat.st_mode & 040000){
if (CatPath(PathCopy, PathCopy, TEXT("**"))) {
Recurse(PathCopy, FollowReparse, FileFuncParm);
}
}else{
FileFuncParm(PathCopy);
}
}else{
// A wildcard was specified.
Recurse(PathCopy, FollowReparse, FileFuncParm);
}
return 0;
}
#ifdef DEBUGGING
//--------------------------------------------------------------------------------
// The main program.
// debug: -ref "C:\(abc)\**\orig\**" "C:\(abc)"
// debug: "C:\(abc)"
//--------------------------------------------------------------------------------
int _tmain (int argc, TCHAR **argv)
{
int argn;
TCHAR * arg;
#ifdef REF_CODE
PathUnique = 0;
PathAllocated = 64;
PathData = malloc(sizeof(TCHAR*)*PathAllocated);
if (PathData == NULL){
_ftprintf(stderr, TEXT("Malloc failure"));
exit(EXIT_FAILURE);
}
#endif
for (argn=1;argn<argc;argn++){
MyGlob(argv[argn], 1, ShowName);
}
return EXIT_SUCCESS;
}
#endif
/*
non-recursive test cases:
e:\make*\*
\make*\*
e:*\*.c
\*\*.c
\*
c:*.c
c:\*
..\*.c
recursive test cases:
**
**\*.c
c:\**\*.c
c:**\*.c
.\**
..\**
c:\
*/