-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
72 lines (51 loc) · 1.25 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "queue.h"
#include "grep.h"
int main(int argc, char** argv){
char flag;
queue files;
init_queue(&files);
int j = argc;
if(argv[1][1] == '-'){
FILE* fp = fopen("help.txt", "r");
char str[400];
while(fgets(str, 400, fp))
fputs(str, stdout);
fclose(fp);
return 0;
}
if(argv[1][0] == '-'){
flag = argv[1][1];
j -= 3;
int idx = 3;
if(flag == 'r'){
addfiles(argv[3], &files);
search_in_file(argv[2], files, flag, 2);
}
else{
while(j--){
push(&files, argv[idx++]);
}
if(idx == 4)
search_in_file(argv[2], files, flag, 1);
else
search_in_file(argv[2], files, flag, 2);
}
}
else{
flag = '0';
j -= 2;
int idx = 2;
while(j--){
push(&files, argv[idx++]);
}
if(idx == 3)
search_in_file(argv[1], files, flag, 1);
else
search_in_file(argv[1], files, flag, 2);
}
return 0;
}