-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathglobbing.c
More file actions
119 lines (86 loc) · 2.23 KB
/
globbing.c
File metadata and controls
119 lines (86 loc) · 2.23 KB
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
/*
glob_t is a glob structure type defined in glob.h
gl_pathV is a list of all matched pathnames
gl_pathc is the total count of paths mathched
if GLOB_NOCHECK is used the structure returns
wildcard if no matches are found and
path count is set to 1
three state parser for seperating normal keywords from wildcards
wildcards in this parser are assumed to start with '*' and '['
and ending with a space
Example *.c *.mp* or *
or [a-z].c or [1-9].*
*/
char * glob_pattern(char * wildcard){
char * gfilename;
size_t cnt,length;
glob_t glob_results;
char **p;
glob(wildcard,GLOB_NOCHECK,0,&glob_results);
for(p=glob_results.gl_pathv,cnt=glob_results.gl_pathc;cnt;p++,cnt--)
length += strlen(*p)+ 1;
gfilename=(char *) calloc(length,sizeof(char));
for(p=glob_results.gl_pathv,cnt=glob_results.gl_pathc;cnt;p++,cnt--)
{
strcat(gfilename,*p);
if(cnt > 1)
strcat(gfilename," ");
}
globfree(&glob_results);
return gfilename;
}
int glob_parser(char input[]){
char * cp;
char c;
char word[1024];
int i=0,j=0;
char * results;
char wild[20];
enum states { START, IN_WORD, IN_WILD } state = START;
for (cp = input; *cp != '\0'; cp++) {
c = (unsigned char) *cp;
switch (state) {
case START:
if(c=='*' || c=='['){
state=IN_WILD;
wild[j]=c;
j++;
}
else{
state=IN_WORD;
word[i]=c;
i++;
}
continue;
case IN_WORD:
if(c =='*' || c=='['){
state=IN_WILD;
wild[j]=c;
j++;
}
else{
word[i]=c;
i++;
}
continue;
case IN_WILD:
if(c==' '){
state=START;
word[i]=c;
}
else{
wild[j]=c;
j++;
}
}
}
word[i]='\0';
wild[j]='\0';
//removing the trailing newline from array
strtok(wild,"\n");
results=glob_pattern(wild);
strcat(word," ");
strcat(word,results);
parse(word,1024);
return 0;
}