summaryrefslogtreecommitdiff
path: root/src/search.c
blob: bb2cc0e09348cb5091c171eb2e99504f2bfcf317 (plain)
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
#define _GNU_SOURCE

#include "search.h"
#include "ll.h"

// Scan a directory
// Sort inputs into files and folders
// Store the full path for each file/folder
// Search new folders

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <error.h>

#include <stdio.h>

int nodesort(const struct dirent *node, int (*S_IS_CALLBACK)(mode_t)) {
    // Ignore . and .. folders
    if(strcmp(node->d_name, ".") == 0 || strcmp(node->d_name, "..") == 0)
        return 0;

    // Stat the file
    struct stat sb;
    if(stat(node->d_name, &sb) < 0) {
        error(0, errno, "Couldn't stat file \"%s\"", node->d_name);
        return 0;
    }

    // Check if the mode is correct
    if((*S_IS_CALLBACK)(sb.st_mode))
        return 1;

    // God I love function pointers

    // Not what is being checked for, exclude it
    return 0;
}

int S_ISDIR_WRAPPER(mode_t mode) {
    return S_ISDIR(mode);
}

int S_ISREG_WRAPPER(mode_t mode) {
    return S_ISREG(mode);
}

int foldersort(const struct dirent *node) {
    return nodesort(node, S_ISDIR_WRAPPER);
}

int filesort(const struct dirent *node) {
    return nodesort(node, S_ISREG_WRAPPER);
}


int main(void) {
    /*
    struct dirent **nodes;
    int n;

    char *actualpath = NULL;
    n = scandir("./", &nodes, foldersort, alphasort);
    if(n >= 0) {
        int cnt;
        for(cnt = 0; cnt < n; ++cnt) {
            actualpath = realpath(nodes[cnt]->d_name, actualpath);
            puts(actualpath);
            free(actualpath);
        }

    } else
        perror("Couldn't open the directory");
    //*/

    struct dirent **nodes = NULL;
    int n = 0;
    
    struct nodelist *scanner = nodelist_init(NULL), *holder = nodelist_init(NULL), *hstart = holder;
    if(scanner == NULL)
        error(-1, errno, "shit brokey");
    scanner->fullpath = realpath("./", NULL);
    scanner->type = NODELIST_TYPE__FOLDER;

    n = scandir(scanner->fullpath, &nodes, foldersort, alphasort);
    if(n > 0) {
        for(int i = 0; i < n; i++) {
            holder->fullpath = realpath(nodes[i]->d_name, NULL);
            holder->type = NODELIST_TYPE__FOLDER;
            holder->next = nodelist_init(NULL);
            holder = holder->next;
        }
    } else
        error(-1, 0, "Couldn't open folder");

    nodelist_append(scanner, hstart);

    printf("Scanned paths:\n");
    for(struct nodelist *p = scanner; p->next != NULL; p = p->next) // Because of how holder is populated, checking p->next for being null makes sure printf doesn't try to do anything dumb like printing a null string
        printf("%s\n", p->fullpath);

    return 0;
}