#include #include #include #include #include #include #include #define PATTERNS_FILE "patterns.txt" void* xmalloc(size_t size) { void* ptr = malloc(size); if (ptr == NULL) { fprintf(stderr, "Memory allocation failed\n"); exit(EXIT_FAILURE); } return ptr; } char** read_lines(const char* filename) { clock_t ctime = clock(), lastPrint = clock(); FILE* file = fopen(filename, "r"); if (file == NULL) { fprintf(stderr, "Error opening file %s: ", filename); perror(""); return nullptr; } int count = 0; { char* buf = xmalloc(10000); while (fgets(buf, 10000, file) != NULL) { if (buf[0] == '#') { continue; } count++; } free(buf); rewind(file); } // fprintf(stderr, "Counted %d lines from %s in %.3f ms\n", count, filename, (clock() - ctime) * 1000.0 / CLOCKS_PER_SEC); // count = count_lines(file); // printf("Counted %d lines from %s in %.3f ms\n", count, filename, (clock() - ctime) * 1000.0 / CLOCKS_PER_SEC); rewind(file); char** lines = xmalloc((count + 1) * sizeof(char*)); char* buf = xmalloc(10000); for (int i = 0; i < count; i++) { if (fgets(buf, 10000, file) == NULL) { fprintf(stderr, "Error reading line %d from %s\n", i + 1, filename); free(buf); return nullptr; } if (buf[0] == '#') { i--; continue; } buf[strcspn(buf, "\n")] = 0; lines[i] = strdup(buf); // double elapsed = (clock() - lastPrint) * 1000.0 / CLOCKS_PER_SEC; // if (elapsed > 50) // { // fprintf(stderr, "\rRead %d lines from %s in %.3f ms\r", i + 1, filename, (clock() - ctime) * 1000.0 / CLOCKS_PER_SEC); // lastPrint = clock(); // } } free(buf); lines[count] = nullptr; fclose(file); printf("Read %d definitions from %s in %.3f ms\n", count, filename, (clock() - ctime) * 1000.0 / CLOCKS_PER_SEC); return lines; } char** grow_array(char** array, size_t newSize) { printf("Growing array to %zu\n", newSize); char** newArray = realloc(array, sizeof(char*) * newSize); if (newArray == NULL) { fprintf(stderr, "Memory allocation failed\n"); exit(EXIT_FAILURE); } return newArray; } void check_dups(char* filename) { printf("# %s:\n", filename); char** lines = read_lines(filename); if (lines == NULL) { fprintf(stderr, "Error reading lines\n"); return; } for (int i = 0; lines[i] != NULL; i++) { for (int j = 0; lines[j] != NULL; j++) { // check if string contains if (lines[i] != lines[j] && strstr(lines[i], lines[j]) != NULL) { printf("Found duplicate: %d~%d: '%s' ~ '%s'\n", i, j, lines[i], lines[j]); } } } } int main(void) { printf("Hello, World!\n"); check_dups(PATTERNS_FILE); check_dups("allowed_ips.txt"); check_dups("allowed_patterns.txt"); return 0; }