-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
260 lines (241 loc) · 8.28 KB
/
main.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
/*
File Name:
Author Name: Apurva Vijay Deshpande
*/
//Problem : GREP COMMAND IMPLEMENTATIPN
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include"grep.h"
int cflag = 0, hflag = 0, iflag = 0, lflag = 0, nflag = 0, vflag = 0, fflag = 0, wflag = 0, oflag = 0, dflag = 0, eflag = 0, xflag = 0;
int main(int argc, char*argv[]){
int opt;
int i_f = 0, i_p = 0, len_f = 0, len_p = 0;
char* patterns[10];
char* filenames[10];
if(strcmp(argv[2], "--help") == 0){
print_usage();
}
else if (argc < 3){
printf("GREP: Improper number of arguments\nType grep--help for usage instructions\n");
return 1;
}
else{
while((opt = getopt(argc, argv, "dchilnvwox?d:e"))!= -1){
switch(opt){
case 'd':
dflag = 1;
break;
case 'c':
cflag = 1;
break;
case 'h':
hflag = 1;
break;
case 'i':
iflag = 1;
break;
case 'l':
lflag = 1;
break;
case 'n':
nflag = 1;
break;
case 'v':
vflag = 1;
break;
case 'w':
wflag = 1;
break;
case 'o':
oflag = 1;
break;
case 'e':
eflag = 1;
break;
case 'x':
xflag = 1;
break;
case '?':
printf("Unknown Command: grep\nType grep--help for usage instructions\n\n");
cflag++;
break;
default:
printf("Unknown option\nType grep--help for usage instructions\n ");
}
}
//Extract Files and Patterns from Command Line Arguments
for(; optind < argc; optind++){
char* argument = argv[optind];
if(isFile(argument)){
filenames[i_f] = argument;
i_f++;
}
else{
patterns[i_p] = argument;
i_p++;
}
}
//Length of Pattern and File arrays
len_f = i_f;
len_p = i_p;
//-d = default grep command
if(dflag != 0){
i_p = i_f = 0;
printf("\n-d : Default Grep Command\n");
while(i_p < len_p){
i_f = 0;
while(i_f < len_f){
printf("\nOccurrence of '%s' in file: %s -\n", patterns[i_p], filenames[i_f]);
d_grep(filenames[i_f], patterns[i_p]);
i_f++;
}
i_p++;
printf("\n");
}
}
// -i = Ignore Case for Matching
if(iflag != 0){
i_p = i_f = 0;
printf("\n-i: Ignore Case for Matching\n");
while(i_p < len_p){
i_f = 0;
while(i_f < len_f){
printf("\nOccurrence of '%s'(case insensitive) in file: %s -\n", patterns[i_p], filenames[i_f]);
i_grep(filenames[i_f], patterns[i_p]);
i_f++;
}
i_p++;
}
}
//-c = Count lines that Match Pattern
if(cflag != 0){
i_p = i_f = 0;
printf("\nCount lines that Match Pattern\n");
while(i_f < len_f){
i_p = 0;
while(i_p < len_p){
c_grep(filenames[i_f], patterns[i_p]);
i_p++;
}
i_f++;
}
}
//-h = Display the matched lines, but do not display the filenames.
if(hflag != 0){
i_p = i_f = 0;
printf("\nDisplay the matched lines, but do not display the filenames.\n");
while(i_p < len_p){
i_f = 0;
printf("\nLines in %s having '%s' are- \n", filenames[i_f], patterns[i_p]);
while(i_f < len_f){
h_grep(filenames[i_f], patterns[i_p]);
i_f++;
}
i_p++;
}
}
//-l = Display the matched lines, but do not display the filenames.
if(lflag != 0){
i_p = i_f = 0;
printf("\nDisplay the matched lines, but do not display the filenames.\n");
while(i_p < len_p){
i_f = 0;
while(i_f < len_f){
printf("\nMatched Lines in %s having occurrence of '%s' are:\n", filenames[i_f], patterns[i_p]);
if(l_grep(filenames[i_f], patterns[i_p]))
printf("%s\n", filenames[i_f]);
i_f++;
}
i_p++;
}
}
// -v = prints out all the lines that do not matches the pattern
if(vflag != 0){
i_p = i_f = 0;
printf("This prints out all the lines that do not matches the pattern\n\n");
while(i_p < len_p){
i_f = 0;
while(i_f < len_f){
printf("\nLines in %s which do not have occurrence of '%s' are- \n", filenames[i_f], patterns[i_p]);
v_grep(filenames[i_f], patterns[i_p]);
i_f++;
}
i_p++;
}
}
// -n = Match whole/ exact word
if(wflag != 0){
i_p = i_f = 0;
printf("\nMatch whole word\n");
while(i_p < len_p){
i_f = 0;
while(i_f < len_f){
printf("\n Lines in %s having occurrence of exact word: '%s' are-\n", filenames[i_f], patterns[i_p]);
w_grep(filenames[i_f], patterns[i_p]);
i_f++;
}
i_p++;
}
}
// -n = Display the matched lines and their line numbers.
if(nflag != 0){
i_p = i_f = 0;
printf("\nDisplay the matched lines with their line numbers.\n");
while(i_p < len_p){
i_f = 0;
while(i_f < len_f){
printf("\nLines in %s having occurrence of exact word: '%s' are:\n", filenames[i_f], patterns[i_p]);
n_grep(filenames[i_f], patterns[i_p]);
i_f++;
}
i_p++;
}
}
// -o = Print only the matched parts of a matching line, with each such part on a separate output line.
if(oflag != 0){
i_p = i_f = 0;
printf("\nPrint only the matched parts of a matching line, with each such part on a separate output line.\n");
while(i_p < len_p){
i_f = 0;
while(i_f < len_f){
printf("\nMatched parts of %s having '%s' are: \n", filenames[i_f], patterns[i_p]);
o_grep(filenames[i_f], patterns[i_p]);
i_f++;
}
i_p++;
}
}
// -x : Print Exact location of Pattern found in the file(s)
if(xflag != 0){
i_p = i_f = 0;
printf("\nPrint Exact location of Pattern found in the file(s)\n");
while(i_p < len_p){
i_f = 0;
while(i_f < len_f){
printf("\nPrint Exact location of '%s' found in %s are:\n", filenames[i_f], patterns[i_p]);
x_grep(filenames[i_f], patterns[i_p]);
i_f++;
}
i_p++;
}
}
/*
//-e = Evaluate Regular Expression
if(eflag != 0){
i_p = i_f = 0;
printf("\nEvaluate Regular Expression\n\n");
while(i_p < len_p){
i_f = 0;
while(i_f < len_f){
check_regex(filenames[i_f], patterns[i_p]);
i_f++;
}
i_p++;
}
}
*/
return 0;
}
}