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
|
#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;
struct nodelist *scanner = nodelist_init(NULL), *holder = NULL, *start = scanner;
scanner->fullpath = realpath("./", NULL);
scanner->type = NODELIST_TYPE__FOLDER;
for(; scanner != NULL; scanner = scanner->next) {
if(chdir(scanner->fullpath) < 0) {
error(0, errno, "Couldn't change to dir \"%s\". Skipping scan of that folder", scanner->fullpath);
continue;
}
int n = scandir(scanner->fullpath, &nodes, foldersort, alphasort);
if(n >= 0) {
for(int i = 0; i < n; i++) {
holder = nodelist_init(NULL);
holder->fullpath = realpath(nodes[i]->d_name, NULL);
holder->type = NODELIST_TYPE__FOLDER;
nodelist_append(scanner, holder); // Yeah this is slow but idk rn
}
} else
error(-1, errno, "Couldn't open dir \"%s\"", scanner->fullpath);
}
for(struct nodelist *p = start; p != NULL; p = p->next) {
if(p->fullpath != NULL)
printf("%s\n", p->fullpath);
}
nodelist_delete(start);
return 0;
}
|