summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
author@syxhe <https://t.me/syxhe>2025-01-02 23:59:40 -0600
committer@syxhe <https://t.me/syxhe>2025-01-03 00:01:14 -0600
commit36dc7e18b66bb374d4c67a7f526c088636eaf9a2 (patch)
treefc6554c9e2b40d30958321459cab4fde4d0116ca /src/main.c
parent03c5fce0220d3e5d02d320f925a3b9401a397729 (diff)
Filter out . and .. folders from scan results
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c45
1 files changed, 42 insertions, 3 deletions
diff --git a/src/main.c b/src/main.c
index bf110c2..4ecf545 100644
--- a/src/main.c
+++ b/src/main.c
@@ -4,15 +4,41 @@
4#include <error.h> 4#include <error.h>
5#include <stdio.h> 5#include <stdio.h>
6 6
7#include <fcntl.h> 7#include <sys/types.h>
8#include <sys/stat.h>
9#include <unistd.h>
8#include <dirent.h> 10#include <dirent.h>
11#include <fcntl.h>
12
13#include <regex.h>
14
15#include <string.h>
16
17regex_t* initfilterregex() {
18 static regex_t *reg = NULL;
19
20 if(reg != NULL)
21 return reg;
22
23 reg = xcalloc(1, sizeof(*reg));
24 if(regcomp(reg, "^(.{1,2})$", REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0)
25 return NULL;
26
27 return reg;
28}
9 29
10int testfilter(const struct dirent *node) { 30int testfilter(const struct dirent *node) {
31 regex_t *reg = initfilterregex();
32
33 if(regexec(reg, node->d_name, 0, NULL, 0) != (REG_NOMATCH | REG_NOERROR) && S_ISDIR(DTTOIF(node->d_type)))
34 return 0;
35
11 return 1; 36 return 1;
12} 37}
13 38
14int main() { 39int main() {
15 // 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) 40 // Alright, going to start simple. First: scanning for files. I want to do this quickly and in one motion
41 // No reason to do things in O(n2) time if I can do it in O(n)
16 42
17 int nnodes = -1; 43 int nnodes = -1;
18 struct dirent **nodes = NULL; 44 struct dirent **nodes = NULL;
@@ -20,9 +46,22 @@ int main() {
20 error(1, errno, "scandir broke"); 46 error(1, errno, "scandir broke");
21 47
22 for(int i = 0; i < nnodes; i++) { 48 for(int i = 0; i < nnodes; i++) {
23 printf("%s\n", nodes[i]->d_name); 49 printf("\"%s\"", nodes[i]->d_name);
50 #if defined _DIRENT_HAVE_D_TYPE
51 int mode = DTTOIF(nodes[i]->d_type);
52 printf(", Type: %s%s%s",
53 S_ISREG(mode) ? "Regular file" : "",\
54 S_ISDIR(mode) ? "Directory" : "",\
55 (!S_ISREG(mode) && !S_ISDIR(mode)) ? "Unknown" : "");
56
57 #endif
24 58
59 printf("\n");
25 } 60 }
26 61
62 // 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
63 // 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
64 // also be a pain in the ass. I'm starting to remember why I finished vxgg the way I did
65
27 return 0; 66 return 0;
28} \ No newline at end of file 67} \ No newline at end of file