summary refs log tree commit diff
path: root/dedup.c
blob: 36878c46e4e2eeb2b216c5359c25caa748854497 (plain) (blame)
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 <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>

#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;
}