test-dir-iterator: use path argument directly
[git/git.git] / t / helper / test-dir-iterator.c
CommitLineData
150791ad
DF
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
fa1da7d2
MT
7/*
8 * usage:
9 * tool-test dir-iterator [--follow-symlinks] [--pedantic] directory_path
10 */
150791ad
DF
11int cmd__dir_iterator(int argc, const char **argv)
12{
150791ad 13 struct dir_iterator *diter;
fa1da7d2
MT
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 }
150791ad 25
fa1da7d2
MT
26 if (!*argv || argc != 1)
27 die("dir-iterator needs exactly one non-option argument");
150791ad 28
7df3bd46 29 diter = dir_iterator_begin(*argv, flags);
150791ad 30
3012397e
MT
31 if (!diter) {
32 printf("dir_iterator_begin failure: %d\n", errno);
33 exit(EXIT_FAILURE);
34 }
35
fa1da7d2 36 while ((iter_status = dir_iterator_advance(diter)) == ITER_OK) {
150791ad
DF
37 if (S_ISDIR(diter->st.st_mode))
38 printf("[d] ");
39 else if (S_ISREG(diter->st.st_mode))
40 printf("[f] ");
fa1da7d2
MT
41 else if (S_ISLNK(diter->st.st_mode))
42 printf("[s] ");
150791ad
DF
43 else
44 printf("[?] ");
45
46 printf("(%s) [%s] %s\n", diter->relative_path, diter->basename,
47 diter->path.buf);
48 }
49
fa1da7d2
MT
50 if (iter_status != ITER_DONE) {
51 printf("dir_iterator_advance failure\n");
52 return 1;
53 }
54
150791ad
DF
55 return 0;
56}