-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrepcmd.c
More file actions
335 lines (215 loc) · 6.63 KB
/
Copy pathgrepcmd.c
File metadata and controls
335 lines (215 loc) · 6.63 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
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
/*
author: Siddharth C kulkarni
purpose: imitate the grep cmd in linux
following functionality!!
following are the command line arguments!:
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
*/
// we want to use the non standard functions like getline() , so we use #define _GNU_SOURCE
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include <sys/types.h> //If you only need the OS types, say for the prototypes of your functions, then just #include <sys/types.h>.
#include <sys/stat.h> //this header defines the structure of the data returned by the functions fstat(), lstat(), and stat().
#include <unistd.h> // if you need the function definitions, then you #include <unistd.h> or any of the other system headers, as needed.
#include <dirent.h> /*DIR data type representing directory stream It also defines the structure dirent which includes the following members:
ino_t d_ino file serial number
char d_name[] name of entry
*/
//#include<regex.h>
int findString(char str[100], char search[20]){
int count1 = 0, count2 = 0, i, j, flag;
while (str[count1] != '\0')
count1++;
while (search[count2] != '\0')
count2++;
for (i = 0; i <= count1 - count2; i++)
{
for (j = i; j < i + count2; j++)
{
flag = 1;
if (str[j] != search[j - i])
{
flag = 0;
break;
}
}
if (flag == 1)
break;
}
return flag;
}
int findStringi(char str[100], char search[20]){
int count1 = 0, count2 = 0, i, j, flag;
while (str[count1] != '\0')
count1++;
while (search[count2] != '\0')
count2++;
for (i = 0; i <= count1 - count2; i++)
{
for (j = i; j < i + count2; j++)
{
flag = 1;
if (tolower(str[j]) != tolower(search[j - i]))
{
flag = 0;
break;
}
}
if (flag == 1)
break;
}
return flag;
}
void handleFile(char path[],char par,char pattern[]){
if(findString(path,".txt")||findString(path,".c")||findString(path,".cpp")||findString(path,".html")||findString(path,".py")){
FILE *f=fopen(path,"r");
if(f!=NULL){
char * line = NULL;
int len = 0;
int read;
int line_count=0;
int match_count=0;
f = fopen(path, "r");
switch(par){
case 'c'://This prints only a count of the lines that match a pattern
{
int start=1;
int toprint=0;
//printf("file name: %s\n",path );
while ((read = getline(&line, &len, f)) != -1) {
line_count+=1;
if(findString(line,pattern)){
if(start==1){
printf("file name: %s\n",path );
start=0;toprint=1;}
match_count+=1;
}
}if(toprint){
printf("%d\n",match_count);toprint=0;}
}
break;
case 'h'://Display the matched lines, but do not display the filenames.
while ((read = getline(&line, &len, f)) != -1) {
line_count+=1;
if(findString(line,pattern)){
printf("%d.%s",line_count, line);
}
}
break;
case 'i'://Ignores, case for matching
{int start=1;
while ((read = getline(&line, &len, f)) != -1) {
line_count+=1;
if(findStringi(line,pattern)){
if(start==1){
printf("file name: %s\n",path );
start=0;}
printf("%d.%s",line_count, line);
}
}}
break;
case 'n'://Displays line with number
{ int start=1;
while ((read = getline(&line, &len, f)) != -1) {
line_count+=1;
if(findString(line,pattern)){
if(start==1){
printf("file name: %s\n",path );
start=0;}
printf("%d.%s",line_count, line);
}
}}
break;
case 'v'://This prints out all the lines that do not match the pattern
{int start=1;
while ((read = getline(&line, &len, f)) != -1) {
line_count+=1;
if(!findString(line,pattern)){
if(start==1){
printf("file name: %s\n",path );
start=0;}
printf("%d.%s",line_count, line);
}
}}
break;
case 'l':
{ int start=1;
while ((read = getline(&line, &len, f)) != -1) {
line_count+=1;
if(findString(line,pattern)){
if(start==1){
printf("file name: %s\n",path );
start=0;break;}
}
}}
break;
default:
printf("argv[1] not valid\n");
break;
}
fclose(f);
if (line)
free(line);
}else{
printf("can not open file!! or file does not exist \n");
}
}else{return;}}
void traverseDir(char *dir, int depth,char par,char pattern[])
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
int spaces = depth*4;// for listing the file, we have to show what is depth of directory 0 if same directory
if((dp = opendir(dir)) == NULL) {
// fprintf(stderr,"cannot open directory: %s\n", dir);
handleFile(dir,par,pattern);
return;
}
chdir(dir);
while((entry = readdir(dp)) != NULL) {
lstat(entry->d_name,&statbuf);
/*
lstat() - execute (search) permission is required on all of the directories in path that lead to the file.
stat() stats the file pointed to by path and fills in buf.
lstat() is identical to stat(), except that if path is a symbolic link, then the link itself is stat-ed, not the file that it refers to.
*/
if(S_ISDIR(statbuf.st_mode)) {// This function return information about a file( 1 if it is directory, i.e. recursion case), in the buffer pointed to by statbuf.
/* Found a directory, but ignore . and .. (current directory and parent dir*/
if(strcmp(".",entry->d_name) == 0 ||
strcmp("..",entry->d_name) == 0)
continue;
printf("%*s%s/\n",spaces,"",entry->d_name);
/* Recurse at a new indent level */
traverseDir(entry->d_name,depth+1,par,pattern);
}
else //printf("\n%*s%s\n",spaces,"",entry->d_name); base case if it is not directory!!
handleFile(entry->d_name,par,pattern);
}
chdir("..");
closedir(dp);
}
int main(int argc, char* argv[])
{
if(argc!=4){
printf("invalid command line arguments!!!\n");
exit(1);
}
char par=argv[1][1];
if(!(par=='l' ||par=='n'||par=='c'||par=='h'||par=='i'||par=='v')){
printf("invalid argv[1]\n");
exit(1);
}
char *topdir;
topdir=argv[3];
// printf("Directory scan of %s\n",topdir);
traverseDir(topdir,0,par,argv[2]);
// printf("done.\n");
return 0;
}