From 36dc7e18b66bb374d4c67a7f526c088636eaf9a2 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Thu, 2 Jan 2025 23:59:40 -0600 Subject: Filter out . and .. folders from scan results --- src/main.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) (limited to 'src/main.c') 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 @@ #include #include -#include +#include +#include +#include #include +#include + +#include + +#include + +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) + // 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; @@ -20,9 +46,22 @@ int main() { error(1, errno, "scandir broke"); for(int i = 0; i < nnodes; i++) { - printf("%s\n", nodes[i]->d_name); + 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; } \ No newline at end of file -- cgit v1.2.3