Traverse a directory tree in C using a function pointer.
The traversewfp()
function is the same for all examples below.
What changes between examples is the set of function pointers passed to traversewfp()
.
Usage:
make clean all run
Examples:
- rm-rf: equivalent to '
rm -rf
' - ls-r: similar to '
ls -R
' - find-type: similar to '
find . -type -f -o -type -d
' - find-regex: similar to '
find . -name "*html" -o -name "*txt"
' - count-files: similar to '
find . | wc -l
'
Set of function pointers (see source code examples for usage):
action_status_t on_opendir_err; /* What to do on opendir() error? */
action_status_t on_file_detect_err; /* What to do on dir/file detect error? */
action_t on_process_dir_ok; /* What to do after successfully processing the dir? */
action_t on_process_dir_err; /* What to do if processing the dir failed ? */
action_file_detection_status_t on_detect_file_ok; /* What to do when detected a file? */
action_args_status_t after_process_dir; /* What to do to a directory after processing all files in it? */
Additional function pointers (only needed when changing the directory tree, such as deleting files and/or subdirectories, see rm-rf-fp.c
for usage):
action_t rewind; /* Optionally, rewinddir(), only needed when applying unlink() and rmdir() */
action_t reset_flag; /* Optionally clear flag for rewind() */
action_t set_flag; /* Optionally set flag for rewind() */
action_flag_t get_flag; /* Optionally get flag for rewind() */
Note: Regex code influenced by https://rosettacode.org/wiki/Walk_a_directory/Recursively#Library:_POSIX