test-dir-iterator: use path argument directly
[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 /*
8  * usage:
9  * tool-test dir-iterator [--follow-symlinks] [--pedantic] directory_path
10  */
11 int cmd__dir_iterator(int argc, const char **argv)
12 {
13         struct dir_iterator *diter;
14         unsigned int flags = 0;
15         int iter_status;
16
17         for (++argv, --argc; *argv && starts_with(*argv, "--"); ++argv, --argc) {
18                 if (strcmp(*argv, "--follow-symlinks") == 0)
19                         flags |= DIR_ITERATOR_FOLLOW_SYMLINKS;
20                 else if (strcmp(*argv, "--pedantic") == 0)
21                         flags |= DIR_ITERATOR_PEDANTIC;
22                 else
23                         die("invalid option '%s'", *argv);
24         }
25
26         if (!*argv || argc != 1)
27                 die("dir-iterator needs exactly one non-option argument");
28
29         diter = dir_iterator_begin(*argv, flags);
30
31         if (!diter) {
32                 printf("dir_iterator_begin failure: %d\n", errno);
33                 exit(EXIT_FAILURE);
34         }
35
36         while ((iter_status = dir_iterator_advance(diter)) == ITER_OK) {
37                 if (S_ISDIR(diter->st.st_mode))
38                         printf("[d] ");
39                 else if (S_ISREG(diter->st.st_mode))
40                         printf("[f] ");
41                 else if (S_ISLNK(diter->st.st_mode))
42                         printf("[s] ");
43                 else
44                         printf("[?] ");
45
46                 printf("(%s) [%s] %s\n", diter->relative_path, diter->basename,
47                        diter->path.buf);
48         }
49
50         if (iter_status != ITER_DONE) {
51                 printf("dir_iterator_advance failure\n");
52                 return 1;
53         }
54
55         return 0;
56 }