aboutsummaryrefslogtreecommitdiffstats
path: root/freebsd.ports.find.c
blob: 309ea89f6951558b826b8b9914d00358aad1ddd7 (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
139
#define _POSIX_C_SOURCE 200809L
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <search.h>

#ifndef HASH_TABLE_SIZE
# define HASH_TABLE_SIZE 50000
#endif

#define msg(...)     fprintf(stderr, __VA_ARGS__)
#define msgstr(x)    fputs((x), stderr)

static void write_maps (const char* list_file) {
    FILE* fp = fopen (list_file, "r");
    if (fp == NULL) {
        msg ("Cannot open list file %s\n", list_file);
        perror ("fopen");
        exit (3);
    }

    char* line = NULL;
    size_t len = 0;
    unsigned n;

    for (n = 0; getline (&line, &len, fp) >= 0; n++) {
        strtok (line, "\n");

        ENTRY hfind = {
            .key = line,
            .data = NULL
        };
        ENTRY* hresult = hsearch (hfind, FIND);
        if (hresult == NULL) {
            msg ("Cannot find origin for %s\n", line);
            exit (4);
        }

        printf ("%-32s %s\n", line, hresult->data);
        line = NULL;
        len = 0;
    }

    if (!feof (fp)) {
        msgstr ("Fail to read the whole list file\n");
        perror ("getline");
        exit (6);
    }

    msg ("=> %u packages entries processed\n", n);
    fclose (fp);
}

static void read_entries (const char* index_file) {
    FILE* fp = fopen (index_file, "r");
    if (fp == NULL) {
        msg ("Cannot open index file %s\n", index_file);
        perror ("fopen");
        exit (3);
    }

    char* line = NULL;
    size_t len = 0;
    unsigned n;

    for (n = 0; getline (&line, &len, fp) >= 0; n++) {
        strtok (line, "\n");

        char* pkgname = strtok (line, "|");
        if (pkgname == NULL) {
            msgstr ("Malformed index file - no package name\n");
            exit (4);
        }

        char* pkgname_end = strrchr (line, '-');
        if (pkgname_end != NULL) {
            *pkgname_end = '\0';
        }

        char* fullpath = strtok (NULL, "|");
        if (fullpath == NULL) {
            msgstr ("Malformed index file - no path\n");
            exit (4);
        }

        bool dir_sp_got = false;
        for (char* p = fullpath + strlen (fullpath); p >= fullpath; p--) {
            if (*p == '/') {
                if (dir_sp_got) {
                    fullpath = p + 1;
                    break;
                } else {
                    dir_sp_got = true;
                }
            }
        }

        ENTRY hentry = {
            .key = pkgname,
            .data = fullpath
        };
        if (hsearch (hentry, ENTER) == NULL) {
            msgstr ("The hash table is full!\n");
            exit (5);
        }

        line = NULL;
        len = 0;
    }

    if (!feof (fp)) {
        msgstr ("Fail to read the whole index file\n");
        perror ("getline");
        exit (6);
    }

    msg ("=> %u ports entries read\n", n);
    fclose (fp);
}

int main (int argc, char* argv[]) {
    if (argc < 3) {
        msg ("Usage: %s list_file index_file\n", argv[0]);
        return 1;
    }

    if (!hcreate (HASH_TABLE_SIZE)) {
        msg ("Cannot create a hash table with size %d\n", HASH_TABLE_SIZE);
        perror ("hcreate");
        return 2;
    }

    read_entries (argv[2]);
    write_maps (argv[1]);

    return 0;
}