dir-iterator: refactor state machine model
[git] / t / helper / test-dir-iterator.c
1 #include "test-tool.h"
2 #include "git-compat-util.h"
3 #include "strbuf.h"
4 #include "iterator.h"
5 #include "dir-iterator.h"
6
7 /* Argument is a directory path to iterate over */
8 int cmd__dir_iterator(int argc, const char **argv)
9 {
10         struct strbuf path = STRBUF_INIT;
11         struct dir_iterator *diter;
12
13         if (argc < 2)
14                 die("BUG: test-dir-iterator needs one argument");
15
16         strbuf_add(&path, argv[1], strlen(argv[1]));
17
18         diter = dir_iterator_begin(path.buf);
19
20         if (!diter) {
21                 printf("dir_iterator_begin failure: %d\n", errno);
22                 exit(EXIT_FAILURE);
23         }
24
25         while (dir_iterator_advance(diter) == ITER_OK) {
26                 if (S_ISDIR(diter->st.st_mode))
27                         printf("[d] ");
28                 else if (S_ISREG(diter->st.st_mode))
29                         printf("[f] ");
30                 else
31                         printf("[?] ");
32
33                 printf("(%s) [%s] %s\n", diter->relative_path, diter->basename,
34                        diter->path.buf);
35         }
36
37         return 0;
38 }