#include "shared.h" #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) 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; }