-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
184 lines (140 loc) · 4.32 KB
/
main.c
File metadata and controls
184 lines (140 loc) · 4.32 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
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <regex.h>
#include <stdlib.h>
void check_if_file_exists(const char *path, struct stat *st);
void is_file_valid(const char *path, struct stat *st);
FILE *read_file(const char *path);
void is_file_empty(struct stat *st);
int determine_case(int flag_i);
void compile_regex(regex_t *pattern, const char *regex, int regex_flags);
int print_matches(FILE *file, regex_t *pattern, int flag_a, int flag_n);
void check_matches_flag(int flag_m, int matches_count, const char *regex);
void check_debugging_flag(int flag_i, int flag_n, int flag_a, int flag_m, const char *path, int flag_d, const char *regex);
int main(int argc, char *argv[]) {
int flag_i = 0, flag_n = 0, flag_a = 0, flag_m = 0, flag_d = 0;
const char *regex = NULL;
const char *path = NULL;
struct stat st;
regex_t pattern;
int i = 1;
for (; i < argc; i++) {
if ((argv[i][0] == '-') && (argv[i] != argv[argc - 2])) {
if (argv[i][1] == '\0') {
break;
}
for (int j = 1; argv[i][j] != '\0'; j++) {
switch (argv[i][j]) {
case 'i': flag_i = 1; break;
case 'n': flag_n = 1; break;
case 'a': flag_a = 1; break;
case 'm': flag_m = 1; break;
case 'd': flag_d = 1; break;
default:
fprintf(stderr, "Unknown option: %c\n", argv[i][j]);
return 1;
}
}
} else {
break;
}
}
if (i < argc - 1) {
regex = argv[i++];
}
if (i < argc) {
path = argv[i];
}
if (!path || !regex) {
fprintf(stderr, "Usage: %s [-i] [-n] [-a] [-m] [-d] <regex> <path-to-log>\n", argv[0]);
return 1;
}
check_if_file_exists(path, &st);
is_file_valid(path, &st);
FILE *file = read_file(path);
is_file_empty(&st);
int regex_flags = determine_case(flag_i);
compile_regex(&pattern, regex, regex_flags);
int matches_count = print_matches(file, &pattern, flag_a, flag_n);
check_matches_flag(flag_m, matches_count, regex);
check_debugging_flag(flag_i, flag_n, flag_a, flag_m, path, flag_d, regex);
return 0;
}
void check_if_file_exists(const char *path, struct stat *st) {
if (stat(path, st) != 0) {
perror("stat");
fprintf(stderr, "Error: cannot access file: %s\n", path);
exit(EXIT_FAILURE);
}
}
void is_file_valid(const char *path, struct stat *st) {
if (!S_ISREG(st -> st_mode)) {
fprintf(stderr, "Error: path is not a regular file: %s\n", path);
exit(EXIT_FAILURE);
}
}
FILE *read_file(const char *path) {
return fopen(path, "r");
}
void is_file_empty(struct stat *st) {
if (st->st_size == 0) {
printf("The file provided is empty.\n");
exit(EXIT_FAILURE);
}
}
int determine_case(int flag_i) {
int regex_flags = REG_EXTENDED;
if (flag_i) {
regex_flags |= REG_ICASE;
}
return regex_flags;
}
void compile_regex(regex_t *pattern, const char *regex, int regex_flags) {
int ret = regcomp(pattern, regex, regex_flags);
if (ret) {
char errbuf[128];
regerror(ret, pattern, errbuf, sizeof(errbuf));
fprintf(stderr, "Could not compile regex: %s\n", errbuf);
exit(EXIT_FAILURE);
}
}
int print_matches(FILE *file, regex_t *pattern, int flag_a, int flag_n) {
char line[1024];
int matches_count = 0;
int line_number = 0;
regmatch_t match[1];
while (fgets(line, sizeof(line), file) != NULL ) {
line_number++;
if (regexec(pattern, line, 1, match, 0) == 0) {
if (flag_a) {
if (flag_n) printf("%d:", line_number);
fwrite(line, 1, match[0].rm_so, stdout);
printf("\x1b[31m");
fwrite(line + match[0].rm_so, 1, match[0].rm_eo - match[0].rm_so, stdout);
printf("\x1b[0m");
printf("%s", line + match[0].rm_eo);
} else {
if (flag_n) printf("%d:", line_number);
printf("%s", line);
}
matches_count++;
}
}
fclose(file);
regfree(pattern);
return matches_count;
}
void check_matches_flag(int flag_m, int matches_count, const char *regex) {
if (flag_m) {
printf("\nA total of %d matches were found for the \"%s\" pattern.\n", matches_count, regex);
}
}
void check_debugging_flag(int flag_i, int flag_n, int flag_a, int flag_m, const char *path, int flag_d, const char *regex) {
if (flag_d) {
printf("\nDebugging info:\n");
printf("Flags: -i=%d -n=%d -a=%d -m=%d -d=%d\n", flag_i, flag_n, flag_a, flag_m, flag_d);
printf("Regex: %s\n", regex);
printf("Path: %s\n", path);
}
}