1 contributor
65 lines1.6 KB
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#define DEMO_STACK_SIZE (8192*sizeof(void *))
14
15void *yield_files(void *param){
16 bool domore = true;
17 char *path = param;
18 DIR *d;
19 struct dirent *dir;
20 int pathlen = strlen(path);
21 d = opendir(path);
22 if (d) {
23 while (domore && (dir = readdir(d)) != NULL) {
24 int blklen = pathlen + 1 + strlen(dir->d_name) + 1;
25 char *r = malloc(blklen);
26 snprintf(r, blklen, "%s/%s", path, dir->d_name);
27 domore = Generator_Yield(r);
28 if (domore && dir->d_type == DT_DIR) {
29 if (strcmp(dir->d_name, ".") != 0 && strcmp(dir->d_name, "..") != 0) {
30 r = malloc(blklen);
31 snprintf(r, blklen, "%s/%s", path, dir->d_name);
32 domore = yield_files(r);
33 free(r);
34 }
35 }
36 }
37 closedir(d);
38 }
39
40 return (void *)domore;
41}
42
43void *gentest(void *param){
44 Generator gen;
45 Generator_ctor(&gen, DEMO_STACK_SIZE, yield_files, (char *)param);
46 int count = 0;
47 while(Generator_Next(&gen, &param)){
48 printf("%d) %s\n", count, (char *)param);
49 free(param);
50 if (++count>16000) break;
51 }
52 Generator_dtor(&gen);
53
54 return param;
55}
56
57int main(int argc, char *argv[]) {
58 (void)argc;
59 (void)argv;
60
61 Coroutine_Run(DEMO_STACK_SIZE, gentest, "..", NULL);
62
63 return 0;
64}
65