]>
git.saurik.com Git - apple/libc.git/blob - tests/fts_find.c
12 // Can ASan fts by uncommenting below
13 //#include "../gen/fts.c"
16 #define fts_find_main main
18 #include <darwintest.h>
21 int fts_find_main(int argc
, char *argv
[]);
24 stat_str(struct stat
*st
)
26 static char charbuf
[256];
27 snprintf(charbuf
, sizeof(charbuf
), "dev: %d, mode: %x, nlink: %d, ino: %lld, "
28 "owner: %d/%d, rdev: %d, mtime: %ld, ctime: %ld, btime: %ld, "
29 "size: %lld, blocks: %lld, blksize: %d, flags: %d, gen: %d",
30 st
->st_dev
, st
->st_mode
, st
->st_nlink
, st
->st_ino
, st
->st_uid
,
31 st
->st_gid
, st
->st_rdev
, st
->st_mtimespec
.tv_sec
,
32 st
->st_ctimespec
.tv_sec
, st
->st_birthtimespec
.tv_sec
, st
->st_size
,
33 st
->st_blocks
, st
->st_blksize
, st
->st_flags
, st
->st_gen
);
38 fts_find_main(int argc
, char *argv
[])
43 bool print_children
= false;
44 int fts_options
= FTS_COMFOLLOW
| FTS_XDEV
;
47 while ((ch
= getopt(argc
, argv
, "lpcdsS")) != -1){
50 fts_options
|= FTS_LOGICAL
;
53 fts_options
|= FTS_PHYSICAL
;
56 print_children
= true;
59 fts_options
|= FTS_NOCHDIR
;
62 fts_options
|= FTS_NOSTAT
;
65 fts_options
|= FTS_NOSTAT_TYPE
;
68 fprintf(stderr
, "Usage: %s (-l|-p) [-c] [-d] [-s|-S] <path> ...\n", argv
[0]);
73 if ((fts_options
& (FTS_LOGICAL
|FTS_PHYSICAL
)) == 0){
74 fprintf(stderr
, "Usage: %s (-l|-p) [-c] [-s|-S] <path> ...\n", argv
[0]);
81 char **args
= alloca((size_t)(argc
+ 1)*sizeof(char*));
82 for (int i
= 0; i
< argc
; i
++){
86 fts
= fts_open_b(args
, fts_options
, ^(const FTSENT
**a
, const FTSENT
**b
){
87 return strcmp((*a
)->fts_name
, (*b
)->fts_name
);
89 if (!fts
) err(EX_DATAERR
, "fts_open_b");
91 while ((ftse
= fts_read(fts
)) != NULL
) {
93 if (!print_children
|| (ftse
->fts_info
& FTS_D
)){
94 printf("%s (%s): 0x%x\n", ftse
->fts_path
, ftse
->fts_name
, ftse
->fts_info
);
95 if (!(fts_options
& (FTS_NOSTAT
|FTS_NOSTAT_TYPE
))) printf("\t\t%s\n", stat_str(ftse
->fts_statp
));
99 FTSENT
*child
= fts_children(fts
, 0);
102 if (child
->fts_info
& FTS_F
){
103 printf("\t%s (%s): 0x%x\n", child
->fts_path
, child
->fts_name
, child
->fts_info
);
104 if (!(fts_options
& (FTS_NOSTAT
|FTS_NOSTAT_TYPE
))) printf("\t\t%s\n", stat_str(child
->fts_statp
));
107 child
= child
->fts_link
;
112 (void)fts_close(fts
);
117 T_DECL(fts_find
, "A find(1) example in fts"){
119 char *fts_argv
[] = {"fts_find", "-lc", "/System", NULL
};
120 if (fts_find_main(fts_argc
, fts_argv
) == 0){
121 T_PASS("fts_find() completed successfully");
123 T_FAIL("fts_find() exited with error");