diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.c | 45 |
1 files changed, 42 insertions, 3 deletions
| @@ -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 | |||
| 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 | } | ||
| 9 | 29 | ||
| 10 | int testfilter(const struct dirent *node) { | 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 | |||
| 11 | return 1; | 36 | return 1; |
| 12 | } | 37 | } |
| 13 | 38 | ||
| 14 | int main() { | 39 | int 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 |
