diff options
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 55 |
1 files changed, 0 insertions, 55 deletions
| @@ -4,64 +4,9 @@ | |||
| 4 | #include <error.h> | 4 | #include <error.h> |
| 5 | #include <stdio.h> | 5 | #include <stdio.h> |
| 6 | 6 | ||
| 7 | #include <sys/types.h> | ||
| 8 | #include <sys/stat.h> | ||
| 9 | #include <unistd.h> | ||
| 10 | #include <dirent.h> | ||
| 11 | #include <fcntl.h> | ||
| 12 | |||
| 13 | #include <regex.h> | ||
| 14 | |||
| 15 | #include <string.h> | ||
| 16 | |||
| 17 | regex_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 | } | ||
| 29 | |||
| 30 | int 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 | |||
| 36 | return 1; | ||
| 37 | } | ||
| 38 | |||
| 39 | int main() { | 7 | int main() { |
| 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) | ||
| 42 | |||
| 43 | int nnodes = -1; | ||
| 44 | struct dirent **nodes = NULL; | ||
| 45 | if((nnodes = scandir(".", &nodes, testfilter, alphasort)) < 0) | ||
| 46 | error(1, errno, "scandir broke"); | ||
| 47 | |||
| 48 | for(int i = 0; i < nnodes; i++) { | ||
| 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 | ||
| 58 | 8 | ||
| 59 | printf("\n"); | ||
| 60 | } | ||
| 61 | 9 | ||
| 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 | 10 | ||
| 66 | return 0; | 11 | return 0; |
| 67 | } \ No newline at end of file | 12 | } \ No newline at end of file |
