forked from Matthias-Wandel/jhead
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyglob.c
302 lines (255 loc) · 8.41 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
//--------------------------------------------------------------------------------
// 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 hadles 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
//--------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <io.h>
#include "jhead.h"
#define TRUE 1
#define FALSE 0
//#define DEBUGGING
typedef struct {
char * Name;
int attrib;
}FileEntry;
#ifdef DEBUGGING
//--------------------------------------------------------------------------------
// Dummy function to show operation.
//--------------------------------------------------------------------------------
void ShowName(const char * FileName)
{
printf(" %s\n",FileName);
}
#endif
//--------------------------------------------------------------------------------
// Simple path splicing (assumes no '\' in either part)
//--------------------------------------------------------------------------------
static void SplicePath(char * dest, const char * p1, const char * p2)
{
int l;
l = strlen(p1);
if (!l){
strcpy(dest, p2);
}else{
if (l+strlen(p2) > _MAX_PATH-2){
fprintf(stderr,"Path too long\n");
exit(-1);
}
memcpy(dest, p1, l+1);
if (dest[l-1] != '\\' && dest[l-1] != ':'){
dest[l++] = '\\';
}
strcpy(dest+l, p2);
}
}
//--------------------------------------------------------------------------------
// Qsort compare function
//--------------------------------------------------------------------------------
int CompareFunc(const void * f1, const void * f2)
{
return strcmp(((FileEntry *)f1)->Name,((FileEntry *)f2)->Name);
}
//--------------------------------------------------------------------------------
// Decide how a particular pattern should be handled, and call function for each.
//--------------------------------------------------------------------------------
void MyGlob(const char * Pattern , void (*FileFuncParm)(const char * FileName))
{
char BasePattern[_MAX_PATH];
char MatchPattern[_MAX_PATH];
char PatCopy[_MAX_PATH*2+1];
int a;
int MatchFiles, MatchDirs;
int BaseEnd, PatternEnd;
int SawPat;
int RecurseAt;
strcpy(PatCopy, Pattern);
#ifdef DEBUGGING
printf("Called with '%s'\n",Pattern);
#endif
DoRecursion:
MatchFiles = FALSE;
MatchDirs = TRUE;
BaseEnd = 0;
PatternEnd = 0;
SawPat = FALSE;
RecurseAt = -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
RecurseAt = a;
if (PatCopy[a+2]){
memcpy(PatCopy+a, PatCopy+a+3, strlen(PatCopy)-a-1);
}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;
MatchFiles = TRUE;
MatchDirs = FALSE;
break;
}
}
if (!SawPat){
// No pattern. This should refer to a file.
FileFuncParm(PatCopy);
return;
}
strncpy(BasePattern, PatCopy, BaseEnd);
BasePattern[BaseEnd] = 0;
strncpy(MatchPattern, PatCopy, PatternEnd);
MatchPattern[PatternEnd] = 0;
#ifdef DEBUGGING
printf("Base:%s Pattern:%s Files:%d dirs:%d\n",BasePattern, MatchPattern, MatchFiles, MatchDirs);
#endif
{
FileEntry * FileList = NULL;
int NumAllocated = 0;
int NumHave = 0;
struct _finddata_t finddata;
long find_handle;
find_handle = _findfirst(MatchPattern, &finddata);
for (;;){
if (find_handle == -1) break;
// Eliminate the obvious patterns.
if (!memcmp(finddata.name, ".",2)) goto next_file;
if (!memcmp(finddata.name, "..",3)) goto next_file;
if (finddata.attrib & _A_SUBDIR){
if (!MatchDirs) goto next_file;
}else{
if (!MatchFiles) 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 = strlen(finddata.name);
FileList[NumHave].Name = malloc(a+1);
if (FileList[NumHave].Name == NULL){
nomem:
printf("malloc failure\n");
exit(-1);
}
memcpy(FileList[NumHave].Name, finddata.name, a+1);
FileList[NumHave].attrib = finddata.attrib;
NumHave++;
next_file:
if (_findnext(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++){
char CombinedName[_MAX_PATH*2+1];
if (FileList[a].attrib & _A_SUBDIR){
if (MatchDirs){
// Need more directories.
SplicePath(CombinedName, BasePattern, FileList[a].Name);
strncat(CombinedName, PatCopy+PatternEnd, _MAX_PATH*2-strlen(CombinedName));
MyGlob(CombinedName,FileFuncParm);
}
}else{
if (MatchFiles){
// We need files at this level.
SplicePath(CombinedName, BasePattern, FileList[a].Name);
FileFuncParm(CombinedName);
}
}
free(FileList[a].Name);
}
free(FileList);
}
if(RecurseAt >= 0){
strcpy(MatchPattern, PatCopy+RecurseAt);
PatCopy[RecurseAt] = 0;
strncpy(PatCopy+RecurseAt, "*\\**\\", _MAX_PATH*2-RecurseAt);
strncat(PatCopy, MatchPattern, _MAX_PATH*2-strlen(PatCopy));
#ifdef DEBUGGING
printf("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 DoRecursion;
}
}
//--------------------------------------------------------------------------------
// Flip slashes to native OS representation (for Windows)
//--------------------------------------------------------------------------------
void SlashToNative(char * Path)
{
int a;
for (a=0;Path[a];a++){
if (Path[a] == '/') Path[a] = SLASH;
}
}
#ifdef DEBUGGING
//--------------------------------------------------------------------------------
// The main program.
//--------------------------------------------------------------------------------
int main (int argc, char **argv)
{
int argn;
char * arg;
for (argn=1;argn<argc;argn++){
arg = argv[argn];
if (arg[0] != '-') break; // Filenames from here on.
if (!strcmp(arg,"-r")){
printf("do recursive\n");
}else{
fprintf(stderr, "Argument '%s' not understood\n",arg);
}
}
if (argn == argc){
fprintf(stderr,"Error: Must supply a file name\n");
}
for (;argn<argc;argn++){
MyGlob(argv[argn], 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
.\**
..\**
*/