Repository navigation
Expand file tree
/
Copy pathex26.c
More file actions
111 lines (91 loc) · 2.66 KB
/
Copy pathex26.c
File metadata and controls
111 lines (91 loc) · 2.66 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
#include "dbg.h"
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
typedef struct DirInfo {
char **files;
int num_files;
} DirInfo;
DirInfo *list_files_in_dir(char *dir_path) {
DirInfo *dir_info = malloc(sizeof(DirInfo));
dir_info->num_files = 0;
dir_info->files = malloc(1000);
DIR *d;
struct dirent *dir;
d = opendir(dir_path);
if (d) {
while ((dir = readdir(d)) != NULL) {
if (dir->d_type == DT_REG) {
dir_info->files[dir_info->num_files++] = dir->d_name;
}
}
}
return dir_info;
}
int find_needle_in_file(char *needle, char *file_name) {
size_t needle_length = strlen(needle);
char file_path[256];
snprintf(file_path, sizeof(file_path), "./.logfind/%s", file_name);
FILE *file = fopen(file_path, "r");
check(file != NULL, "Error opening file");
char file_char;
int index = -1;
while ((file_char = fgetc(file)) != EOF) {
index++;
if (file_char != needle[0]) {
// if the first character doesn't match, don't bother with rest
debug("No character match at index:%d, continue: %c", index, file_char);
continue;
}
debug("found a character match at index: %d with char: %c", index,
file_char);
int matches = 1;
char remaining[needle_length];
remaining[needle_length - 1] = '\0';
for (int i = 0; i < needle_length; i++) {
file_char = fgetc(file);
index++;
remaining[i] = file_char;
debug("rem: %s ;; index: %d ;; file_char: %c", remaining, index,
file_char);
if (file_char != needle[i + 1]) {
// no match, now undo all fgetc to put the stream back to the next
// character after mismatch
debug("no match after initial, undoing ;; rem: %s ;; index: %d",
remaining, index);
for (int j = i; j >= 0; j--) {
ungetc(remaining[j], file);
index--;
}
debug("breaking out to search again: %d", index);
break;
}
matches++;
debug("increment matches: index: %d", index);
}
if (matches != needle_length) {
debug("matches != needle_length, index: %d ;; matches: %d ;; "
"needle_length: %zu",
index, matches, needle_length);
continue;
}
return index;
}
return -1;
error:
return 0;
}
int main(int argc, char *argv[]) {
char *needle = argv[1];
DirInfo *dir_info = list_files_in_dir("./.logfind");
for (int i = 0; i < dir_info->num_files; i++) {
int result = find_needle_in_file(needle, dir_info->files[i]);
if (result >= 0) {
printf("File %s contains needle at index: %d\n", dir_info->files[i],
result);
}
}
return 0;
error:
return 1;
}