-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfind_files.c
81 lines (64 loc) · 2.36 KB
/
find_files.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
#include "find_files.h"
#include "elf.h"
static int FindCheckBitWidth(const char *Path, int BitWidth)
{
int FileWidth;
FileWidth=ELFFileGetBitWidth(Path);
if ( (BitWidth==32) && (FileWidth==64)) return(FALSE);
if ( (BitWidth==64) && (FileWidth==32)) return(FALSE);
return(TRUE);
}
void FindItems(const char *Path, const char *Inclusions, const char *Exclusions, int BitWidth, ListNode *Founds)
{
char *Tempstr=NULL;
struct stat FStat;
glob_t Glob;
int i;
const char *IgnoreDirs[]= {"windows", "Windows NT", "Internet Explorer", "Windows Media Player", NULL};
const char *ptr, *p_Basename;
Tempstr=CopyStr(Tempstr, Path);
Tempstr=SlashTerminateDirectoryPath(Tempstr);
Tempstr=CatStr(Tempstr, "*");
glob(Tempstr, 0, 0, &Glob);
for (i=0; i < Glob.gl_pathc; i++)
{
lstat(Glob.gl_pathv[i], &FStat);
p_Basename=GetBasename(Glob.gl_pathv[i]);
if (p_Basename)
{
if (S_ISLNK(FStat.st_mode)) /*Do nothing, don't follow links */ ;
else if (S_ISDIR(FStat.st_mode))
{
if (MatchTokenFromList(p_Basename, IgnoreDirs, 0) == -1) FindItems(Glob.gl_pathv[i], Inclusions, Exclusions, BitWidth, Founds);
}
else
{
Tempstr=CopyStr(Tempstr, p_Basename);
//must strlwr here, as InList will do so to Inclusions and Exclusions
strlwr(Tempstr);
if ( InList(Tempstr, Inclusions) && (! InList(Tempstr, Exclusions)) && FindCheckBitWidth(Glob.gl_pathv[i], BitWidth))
{
//can't use 'SetVar' here as multiple files might have the same basename, but different paths
ListAddNamedItem(Founds, p_Basename, CopyStr(NULL, Glob.gl_pathv[i]));
}
}
}
}
globfree(&Glob);
Destroy(Tempstr);
}
void FindFiles(const char *Path, const char *Inclusions, const char *Exclusions, ListNode *Founds)
{
return(FindItems(Path, Inclusions, Exclusions, 0, Founds));
}
char *FindSingleFile(char *RetStr, const char *Root, const char *File)
{
ListNode *Files, *Curr;
RetStr=CopyStr(RetStr, "");
Files=ListCreate();
FindFiles(Root, File, "", Files);
Curr=ListGetNext(Files);
if (Curr) RetStr=CopyStr(RetStr, Curr->Item);
ListDestroy(Files, Destroy);
return(RetStr);
}