| 1 | #include <stdio.h> |
| 2 | #include <stddef.h> |
| 3 | #include "coroutine.h" |
| 4 | #include "generator.h" |
| 5 | #include "asleep.h" |
| 6 | #include "task.h" |
| 7 | |
| 8 | #include <dirent.h> |
| 9 | #include <string.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <time.h> |
| 12 | |
| 13 | void *yield_files(void *param){ |
| 14 | bool domore = true; |
| 15 | char *path = param; |
| 16 | DIR *d; |
| 17 | struct dirent *dir; |
| 18 | int pathlen = strlen(path); |
| 19 | d = opendir(path); |
| 20 | if (d) { |
| 21 | while (domore && (dir = readdir(d)) != NULL) { |
| 22 | int blklen = pathlen + 1 + strlen(dir->d_name) + 1; |
| 23 | char *r = malloc(blklen); |
| 24 | snprintf(r, blklen, "%s/%s", path, dir->d_name); |
| 25 | domore = Generator_Yield(r); |
| 26 | if (domore && dir->d_type == DT_DIR) { |
| 27 | if (strcmp(dir->d_name, ".") != 0 && strcmp(dir->d_name, "..") != 0) { |
| 28 | r = malloc(blklen); |
| 29 | snprintf(r, blklen, "%s/%s", path, dir->d_name); |
| 30 | domore = yield_files(r); |
| 31 | free(r); |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | closedir(d); |
| 36 | } |
| 37 | |
| 38 | return (void *)domore; |
| 39 | } |
| 40 | |
| 41 | void *gentest(void *param){ |
| 42 | Generator gen; |
| 43 | Generator_ctor(&gen, yield_files, (char *)param); |
| 44 | int count = 0; |
| 45 | while(Generator_Next(&gen, ¶m)){ |
| 46 | printf("%d) %s\n", count, (char *)param); |
| 47 | free(param); |
| 48 | if (++count>16000) break; |
| 49 | } |
| 50 | Generator_dtor(&gen); |
| 51 | |
| 52 | return param; |
| 53 | } |
| 54 | |
| 55 | int main(int argc, char *argv[]) { |
| 56 | (void)argc; |
| 57 | (void)argv; |
| 58 | |
| 59 | Coroutine_Run(gentest, ".."); |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |