-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
138 lines (125 loc) · 4.39 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LENGTH 1024
// Function to highlight matching words
void highlight(char* line, char* search) {
char* match = strstr(line, search);
if (match != NULL) {
printf("%s", line);
}
}
int main(int argc, char* argv[]) {
// Define variables for options and default values
int showLineNumbers = 0; // -n
int stripWhitespace = 0; // -s
int excludeLines = 0; // -e
// print the version of the program.
if(argv[1][0] == '-' && argv[1][1] == 'v'){
printf("grep v1.0.0");
exit(0);
}
// Check if the correct number of arguments is provided
if (argc < 3) {
printf("Usage: %s <filename> <search_string> [-n] [-s] [-e]\n", argv[0]);
return 1;
}
// Variables to store filename and search string
char *filename = NULL;
char *search_text = NULL;
// Parse command line options
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
for (int j = 1; j < strlen(argv[i]); j++) {
if (argv[i][j] == 'n') {
showLineNumbers = 1;
} else if (argv[i][j] == 's') {
stripWhitespace = 1;
} else if (argv[i][j] == 'e') {
excludeLines = 1;
} else {
printf("Error: Unknown option '%c'\n", argv[i][j]);
return 1;
}
}
} else {
if (filename == NULL) {
filename = argv[i];
} else if (search_text == NULL) {
search_text = argv[i];
} else {
printf("Error: Unexpected argument '%s'\n", argv[i]);
return 1;
}
}
}
// Check if filename and search text are provided
if (filename == NULL || search_text == NULL) {
printf("Error: Both filename and search text are required.\n");
return 1;
}
// Open the file
FILE* file = fopen(filename, "r");
if (file == NULL) {
printf("Error: File '%s' not found.\n", filename);
return 1;
}
char line[MAX_LINE_LENGTH];
int line_no = 0;
// Read lines from the file
while (fgets(line, sizeof(line), file)) {
line_no++;
// Check if the search string is present
if (strstr(line, search_text) != NULL) {
int matchFound = 0;
// Check options for each line
if (!excludeLines) {
if (showLineNumbers) {
printf("%d : ", line_no);
}
if (stripWhitespace) {
// Strip leading and trailing spaces
char *stripped_line = line;
char *end = stripped_line + strlen(stripped_line) - 1;
while (end >= stripped_line && (*end == '\n' || *end == '\r' || *end == ' ' || *end == '\t')) {
*end-- = '\0';
}
while (*stripped_line && (*stripped_line == ' ' || *stripped_line == '\t')) {
++stripped_line;
}
printf("%s\n", stripped_line);
continue; // Skip to the next line
}else{
printf("%s", line);
}
matchFound = 1;
}
if (excludeLines && !matchFound) {
continue; // Skip to the next line
}
} else if (excludeLines) {
// Print lines that don't contain the search string
if (showLineNumbers) {
printf("%d : ", line_no);
}
if (stripWhitespace) {
// Strip leading and trailing spaces
char *stripped_line = line;
char *end = stripped_line + strlen(stripped_line) - 1;
while (end >= stripped_line && (*end == '\n' || *end == '\r' || *end == ' ' || *end == '\t')) {
*end-- = '\0';
}
while (*stripped_line && (*stripped_line == ' ' || *stripped_line == '\t')) {
++stripped_line;
}
printf("%s\n", stripped_line);
continue; // Skip to the next line
}else{
printf("%s", line);
}
}
}
// Close the file
fclose(file);
return 0;
}