post
poster: psYchotic
description: Find and group hard links
language: C
[download]
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

struct LList {
    dev_t dev;
    ino_t inode;
    nlink_t nlink;
    char **names;
    unsigned int nnames;
    unsigned int max_nnames;
    struct LList *next;
};

static struct LList *list = NULL;

int dirsreglnkfilter(const struct dirent *dir) {
    switch (dir->d_type) {
        case DT_DIR:
            if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
                return 0;
        case DT_REG:
        case DT_LNK:
            return 1;
        default:
            return 0;
    }
}

char *dircat(char *a, char *b) {
    char *res = malloc((strlen(a) + strlen(b) + 2) * sizeof(char));
    strcpy(res, a);
    res[strlen(a)] = '/';
    strcpy(res + strlen(a) + 1, b);

    return res;
}

int dirslast(const struct dirent **a, const struct dirent **b) {
    if ((*a)->d_type == (*b)->d_type) {
        return 0;
    } else if ((*a)->d_type == DT_DIR) {
        return 1;
    } else if ((*b)->d_type == DT_DIR) {
        return -1;
    } else {
        return 0;
    }
}

void append_link(char *name, struct stat *sstat) {
    struct LList *found = list;
    struct LList *prev = NULL;

    while (found != NULL) {
        if (found->dev == sstat->st_dev &&
                found->inode == sstat->st_ino) {
            break;
        }
        prev = found;
        found = found->next;
    }

    if (found == NULL) {
        found = malloc(sizeof(struct LList));
        if (prev != NULL) {
            prev->next = found;
        } else {
            list = found;
        }
        found->max_nnames = 10;
        found->nnames = 0;
        found->names = malloc(10 * sizeof(char *));
        found->next = NULL;
        found->dev = sstat->st_dev;
        found->inode = sstat->st_ino;
    }

    if (found->nnames == found->max_nnames) {
        found->max_nnames *= 2;
        found->names = realloc(found->names, found->max_nnames * sizeof(char *));
    }

    found->names[found->nnames++] = strdup(name);
}

int handle_dir(char *name) {
    struct dirent **dirent;
    struct stat buf;
    char *cat;
    int count;

    count = scandir(name, &dirent, dirsreglnkfilter, dirslast);
    if (count == -1) {
        fprintf(stderr, "An error occurred while scanning directory '%s': %s\n", name, strerror(errno));
        return -1;
    } else {
        while (count--) {
            switch (dirent[count]->d_type) {
                case DT_LNK:
                case DT_REG:
                    cat = dircat(name, dirent[count]->d_name);
                    if (lstat(cat, &buf) == -1) {
                        fprintf(stderr, "lstat '%s': %s\n", cat, strerror(errno));
                    } else if (buf.st_nlink > 1) {
                        append_link(cat, &buf);
                    }
                    free(dirent[count]);
                    free(cat);
                    break;
                case DT_DIR:
                    cat = dircat(name, dirent[count]->d_name);
                    handle_dir(cat);
                    free(dirent[count]);
                    free(cat);
                    break;
            }
        }

        free(dirent);
    }

    return 0;
}

void print_hlinks() {
    struct LList *current = list;
    int i;

    while (current != NULL) {
        for (i = 0; i < current->nnames; i++) {
            printf("%s\n", current->names[i]);
        }
        printf("\n");
        current = current->next;
    }
}

void free_hlinks() {
    struct LList *prev, *current = list;
    int i;

    while (current != NULL) {
        prev = current;
        current = prev->next;
        for (i = 0; i < prev->nnames; i++) {
            free(prev->names[i]);
        }
        free(prev->names);
        free(prev);
    }

    list = NULL;
}

int main(int argc, char **argv) {
    if (argc != 2) {
        printf("Usage: %s <DIR>\n", argv[0]);
        return 0;
    }

    handle_dir(argv[1]);

    printf("Here are all the hard links, grouped together:\n");
    print_hlinks();
    free_hlinks();

    return 0;
}