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
|
#include "shared.h"
#include <errno.h>
#include <error.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <regex.h>
#include <string.h>
regex_t* initfilterregex() {
static regex_t *reg = NULL;
if(reg != NULL)
return reg;
reg = xcalloc(1, sizeof(*reg));
if(regcomp(reg, "^(.{1,2})$", REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0)
return NULL;
return reg;
}
int testfilter(const struct dirent *node) {
regex_t *reg = initfilterregex();
if(regexec(reg, node->d_name, 0, NULL, 0) != (REG_NOMATCH | REG_NOERROR) && S_ISDIR(DTTOIF(node->d_type)))
return 0;
return 1;
}
int main() {
// Alright, going to start simple. First: scanning for files. I want to do this quickly and in one motion
// No reason to do things in O(n2) time if I can do it in O(n)
int nnodes = -1;
struct dirent **nodes = NULL;
if((nnodes = scandir(".", &nodes, testfilter, alphasort)) < 0)
error(1, errno, "scandir broke");
for(int i = 0; i < nnodes; i++) {
printf("\"%s\"", nodes[i]->d_name);
#if defined _DIRENT_HAVE_D_TYPE
int mode = DTTOIF(nodes[i]->d_type);
printf(", Type: %s%s%s",
S_ISREG(mode) ? "Regular file" : "",\
S_ISDIR(mode) ? "Directory" : "",\
(!S_ISREG(mode) && !S_ISDIR(mode)) ? "Unknown" : "");
#endif
printf("\n");
}
// Ok so scandir is annoying and doesn't create a linked list, but rather an array of dirent objects. This means that if I want
// to iterate over the list myself and it'll be a pain in the ass to do so. That, or I can implement a linked list, which would
// also be a pain in the ass. I'm starting to remember why I finished vxgg the way I did
return 0;
}
|