-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAST.c
74 lines (63 loc) · 1.92 KB
/
AST.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
#include "AST.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fnmatch.h>
#include <sys/stat.h>
#include <dirent.h>
extern int PRINTALLPATH;
extern int status;
int
checkMatch(char* itemFullPath, struct dirent *dir, AbstractSyntaxTree *condition)
{
int left;
switch(condition->type)
{
case OPERATOR_OR:
left = checkMatch(itemFullPath, dir, condition->left);
if (left) return 1;
return checkMatch(itemFullPath, dir, condition->right);
case OPERATOR_AND:
left = checkMatch(itemFullPath, dir, condition->left);
if (!left) return 0;
return checkMatch(itemFullPath, dir, condition->right);
case FILTER_NAME:
return checkName(dir->d_name, condition->condition);
case FILTER_TYPE:
return checkType(itemFullPath, condition->condition);
case FILTER_NEWER:
return checkNewer(itemFullPath, condition->condition);
case FILTER_PRINT:
default:
break;
}
return 0;
}
void
searchDirectory(char* currentDirName, AbstractSyntaxTree* treeCondition)
{
struct dirent *dir;
DIR *d;
d = opendir(currentDirName);
while ((dir = readdir(d)) != NULL) {
char *itemFullPath;
if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0)
continue;
itemFullPath = (char*) malloc(strlen(currentDirName) + strlen(dir->d_name) + 2);
sprintf(itemFullPath, "%s/%s", currentDirName, dir->d_name);
if (checkMatch(itemFullPath, dir, treeCondition))
printf("%s\n", itemFullPath);
if (dir->d_type == DT_DIR)
{
searchDirectory(itemFullPath, treeCondition);
}
free(itemFullPath);
if (dir->d_type == DT_UNKNOWN)
{
status = 1;
// TODO
}
}
closedir(d);
}