-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuzz_test_remove_duplicate.c
executable file
·71 lines (65 loc) · 1.72 KB
/
fuzz_test_remove_duplicate.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
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
void read_file_to_buffer(const char *filename, char **buffer) {
printf("read file: %s\n", filename);
struct stat path_stat;
stat(filename, &path_stat);
if (!S_ISREG(path_stat.st_mode)) {
return;
}
long length;
FILE *f = fopen(filename, "rb");
if (f) {
fseek(f, 0, SEEK_END);
length = ftell(f);
fseek(f, 0, SEEK_SET);
*buffer = malloc(length + 1);
if (*buffer) {
fread(*buffer, 1, length, f);
}
(*buffer)[length] = '\0';
fclose(f);
}
}
void save_current_error_to_dir(char *filename, char *content) {
FILE *f = fopen(filename, "w+");
fputs(content, f);
fclose(f);
}
int main(int argc, char const *argv[]) {
if (argc < 3) {
printf("Usage: fuzz_test_remove_duplicate dir log\n");
return 0;
}
const char *dir = argv[1];
const char *filename = argv[2];
char *current_error_file_content;
read_file_to_buffer(filename, ¤t_error_file_content);
DIR *dr = opendir(dir);
struct dirent *de;
int file_index = 0;
int found = 0;
while ((de = readdir(dr)) != NULL) {
char unique_error_file_name[1000];
printf("%s\n", de->d_name);
sprintf(unique_error_file_name, "%s/%s", dir, de->d_name);
char *unique_error_file_content = NULL;
read_file_to_buffer(unique_error_file_name, &unique_error_file_content);
if (unique_error_file_content != NULL) {
if (strcmp(current_error_file_content, unique_error_file_content) == 0) {
printf("error duplicated!\n");
exit(-1);
}
file_index++;
}
}
char save_filename[100];
sprintf(save_filename, "%s/%d", dir, file_index);
save_current_error_to_dir(save_filename, current_error_file_content);
return 0;
}