FTW: The File Tree Walker
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
     char path[1000], *home;

     int myfunc(const char *, const struct stat *, int);
     home = getenv("HOME");
     sprintf(path, "%s/sysprog", home);
     ftw(path, myfunc, 7);

int myfunc(const char *path, const struct stat *sptr, int type)
 /* Types: FTW_D = directory, FTW_F = normal file, FTW_DNR = non-traversable
           directory.  Do a man ftw for the full scoop!!!!*/
      if ((strstr(path,"demo") && strstr(path,"sig")) || strstr(path,"sub"))
         printf("Name = %s\n", path);
         if (type == FTW_DNR) printf("Directory %s cannot be traversed.\n",
      return 0;
/***************************  Output Below  *******************************/

Name = /mnt/diska/staff/jwp2286/sysprog/demos/forksig2.c
Name = /mnt/diska/staff/jwp2286/sysprog/demos/forksig3.c
Name = /mnt/diska/staff/jwp2286/sysprog/demos/forksignal.c
Name = /mnt/diska/staff/jwp2286/sysprog/demos/forksignal.demo
Name = /mnt/diska/staff/jwp2286/sysprog/demos/sigalarm.demo
Name = /mnt/diska/staff/jwp2286/sysprog/demos/sigblock.demo
Name = /mnt/diska/staff/jwp2286/sysprog/demos/sigchld.c
Name = /mnt/diska/staff/jwp2286/sysprog/demos/sigchld.demo
Name = /mnt/diska/staff/jwp2286/sysprog/demos/sigchld2.demo
Name = /mnt/diska/staff/jwp2286/sysprog/demos/sigmalloc.demo
Name = /mnt/diska/staff/jwp2286/sysprog/demos/signal1.demo
Name = /mnt/diska/staff/jwp2286/sysprog/demos/signals.demo
Name = /mnt/diska/staff/jwp2286/sysprog/demos/sigpending.demo
Name = /mnt/diska/staff/jwp2286/sysprog/demos/sigproblem.demo
Name = /mnt/diska/staff/jwp2286/sysprog/demos/sigq.demo
Name = /mnt/diska/staff/jwp2286/sysprog/demos/sigraise.demo
Name = /mnt/diska/staff/jwp2286/sysprog/demos/sigsegv.demo
Name = /mnt/diska/staff/jwp2286/sysprog/demos/sigsets.demo
Name = /mnt/diska/staff/jwp2286/sysprog/stevens/unpv22e/config.sub
Name = /mnt/diska/staff/jwp2286/sysprog/stevens/unpv22e/lib/tv_sub.c

     ftw - walk a file tree

     #include <ftw.h>

     int ftw (path, fn, depth)
     char *path;
     int (*fn) ( );
     int depth;

     The ftw subroutine    recursively descends the directory hierarchy rooted
     in path.  For each    object in the hierarchy, ftw calls fn, passing it a
     pointer to a null-terminated character string containing the name of the
     object, a pointer to a stat structure containing information about the
     object, and an integer.  For further information, see stat(2).  Possible
     values of the integer, defined in the <ftw.h> header file, are FTW_F for
     a file, FTW_D for a directory, FTW_DNR for a directory that cannot be
     read, and FTW_NS for an object for which stat could not successfully be
     executed.  If the integer is FTW_DNR, descendants of that directory will
     not be processed.  If the integer is FTW_NS, the the contents of the
     stat structure will be undefined. An example of an object that would
     cause FTW_NS to be passed to fn would be a    file in a directory with read
     but without execute (search) permission.

     The ftw subroutine    visits a directory before visiting any of its descen-

     The tree traversal continues until the tree is exhausted, an invocation
     of fn returns a nonzero value, or some error is detected within ftw
     (such as an I/O error).  If the tree is exhausted, ftw returns zero.  If
     fn    returns a nonzero value, ftw stops its tree traversal and returns
     whatever value was returned by fn.  If ftw    detects an error, it returns
     -1, and sets the error type in errno.

     The ftw subroutine    uses one file descriptor for each level in the tree.
     The depth argument    limits the number of file descriptors so used.  If
     depth is zero or negative,    the effect is the same as if it were 1.  The
     depth must    not be greater than the number of file descriptors currently
     available for use.  The ftw subroutine will run more quickly if depth is
     at least as large as the number of levels in the tree.


     Because ftw is recursive, it is possible for it to    terminate with a
     memory fault when applied to very deep file structures.
     It could be made to run faster and use less storage on deep structures
     at the cost of considerable complexity.
     The ftw subroutine    uses malloc(3) to allocate dynamic storage during its
     operation.  If ftw    is forcibly terminated, such as by longjmp being exe-
     cuted by fn or an interrupt routine, ftw will not have a chance to   free
     that storage, so it will remain permanently allocated.  A safe way to
     handle interrupts is to store the fact that an interrupt has occurred,
     and arrange to have fn return a nonzero value at its next invocation.

   Diagnostics  /* i.e. errno settings (JP)  */

     [EACCES]       Search permission is denied on a component of path or
                    read permission is denied for path.

     [ENAMETOOLONG] The length of the path string exceeds {PATH_MAX}, or a
                    pathname component is longer than {NAME_MAX}.

     [ENOENT]       The path argument points to the name of a file which does
                    not exist, or to an empty string and the environment
                    defined is POSIX or SYSTEM_FIVE.

     [ENOTDIR]      A component of path    is not a directory.

     [ENOMEM]       Not enough memory was available to complete the file tree

   See Also
     stat(2), malloc(3)