NFTW(3)                    POSIX Programmer's Manual                   NFTW(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.


NNAAMMEE
       nftw -- walk a file tree

SSYYNNOOPPSSIISS
       #include <ftw.h>

       int nftw(const char *_p_a_t_h, int (*_f_n)(const char *,
           const struct stat *, int, struct FTW *), int _f_d___l_i_m_i_t, int _f_l_a_g_s);

DDEESSCCRRIIPPTTIIOONN
       The _n_f_t_w() function shall recursively descend the directory hierarchy
       rooted in _p_a_t_h.  The _n_f_t_w() function has a similar effect to _f_t_w()
       except that it takes an additional argument _f_l_a_g_s, which is a bitwise-
       inclusive OR of zero or more of the following flags:

       FTW_CHDIR   If set, _n_f_t_w() shall change the current working directory
                   to each directory as it reports files in that directory. If
                   clear, _n_f_t_w() shall not change the current working
                   directory.

       FTW_DEPTH   If set, _n_f_t_w() shall report all files in a directory before
                   reporting the directory itself. If clear, _n_f_t_w() shall
                   report any directory before reporting the files in that
                   directory.

       FTW_MOUNT   If set, _n_f_t_w() shall only report files in the same file
                   system as _p_a_t_h.  If clear, _n_f_t_w() shall report all files
                   encountered during the walk.

       FTW_PHYS    If set, _n_f_t_w() shall perform a physical walk and shall not
                   follow symbolic links.

       If FTW_PHYS is clear and FTW_DEPTH is set, _n_f_t_w() shall follow links
       instead of reporting them, but shall not report any directory that
       would be a descendant of itself. If FTW_PHYS is clear and FTW_DEPTH is
       clear, _n_f_t_w() shall follow links instead of reporting them, but shall
       not report the contents of any directory that would be a descendant of
       itself.

       At each file it encounters, _n_f_t_w() shall call the user-supplied
       function _f_n with four arguments:

        *  The first argument is the pathname of the object.

        *  The second argument is a pointer to the ssttaatt buffer containing
           information on the object, filled in as if _f_s_t_a_t_a_t(), _s_t_a_t(), or
           _l_s_t_a_t() had been called to retrieve the information.

        *  The third argument is an integer giving additional information. Its
           value is one of the following:

           FTW_D     The object is a directory.

           FTW_DNR   The object is a directory that cannot be read. The _f_n
                     function shall not be called for any of its descendants.

           FTW_DP    The object is a directory and subdirectories have been
                     visited. (This condition shall only occur if the
                     FTW_DEPTH flag is included in _f_l_a_g_s.)

           FTW_F     The object is a non-directory file.

           FTW_NS    The _s_t_a_t() function failed on the object because of lack
                     of appropriate permission. The ssttaatt buffer passed to _f_n
                     is undefined. Failure of _s_t_a_t() for any other reason is
                     considered an error and _n_f_t_w() shall return -1.

           FTW_SL    The object is a symbolic link. (This condition shall only
                     occur if the FTW_PHYS flag is included in _f_l_a_g_s.)

           FTW_SLN   The object is a symbolic link that does not name an
                     existing file.  (This condition shall only occur if the
                     FTW_PHYS flag is not included in _f_l_a_g_s.)

        *  The fourth argument is a pointer to an FFTTWW structure.  The value of
           bbaassee is the offset of the object's filename in the pathname passed
           as the first argument to _f_n.  The value of lleevveell indicates depth
           relative to the root of the walk, where the root level is 0.

       The results are unspecified if the application-supplied _f_n function
       does not preserve the current working directory.

       The argument _f_d___l_i_m_i_t sets the maximum number of file descriptors that
       shall be used by _n_f_t_w() while traversing the file tree. At most one
       file descriptor shall be used for each directory level.

       The _n_f_t_w() function need not be thread-safe.

RREETTUURRNN VVAALLUUEE
       The _n_f_t_w() function shall continue until the first of the following
       conditions occurs:

        *  An invocation of _f_n shall return a non-zero value, in which case
           _n_f_t_w() shall return that value.

        *  The _n_f_t_w() function detects an error other than [[EEAACCCCEESS]] (see
           FTW_DNR and FTW_NS above), in which case _n_f_t_w() shall return -1 and
           set _e_r_r_n_o to indicate the error.

        *  The tree is exhausted, in which case _n_f_t_w() shall return 0.

EERRRROORRSS
       The _n_f_t_w() function shall fail if:

       EEAACCCCEESS Search permission is denied for any component of _p_a_t_h or read
              permission is denied for _p_a_t_h, or _f_n returns -1 and does not
              reset _e_r_r_n_o.

       EELLOOOOPP  A loop exists in symbolic links encountered during resolution of
              the _p_a_t_h argument.

       EENNAAMMEETTOOOOLLOONNGG
              The length of a component of a pathname is longer than
              {NAME_MAX}.

       EENNOOEENNTT A component of _p_a_t_h does not name an existing file or _p_a_t_h is an
              empty string.

       EENNOOTTDDIIRR
              A component of _p_a_t_h names an existing file that is neither a
              directory nor a symbolic link to a directory.

       EEOOVVEERRFFLLOOWW
              A field in the ssttaatt structure cannot be represented correctly in
              the current programming environment for one or more files found
              in the file hierarchy.

       The _n_f_t_w() function may fail if:

       EELLOOOOPP  More than {SYMLOOP_MAX} symbolic links were encountered during
              resolution of the _p_a_t_h argument.

       EEMMFFIILLEE All file descriptors available to the process are currently
              open.

       EENNAAMMEETTOOOOLLOONNGG
              The length of a pathname exceeds {PATH_MAX}, or pathname
              resolution of a symbolic link produced an intermediate result
              with a length that exceeds {PATH_MAX}.

       EENNFFIILLEE Too many files are currently open in the system.

       In addition, _e_r_r_n_o may be set if the function pointed to by _f_n causes
       _e_r_r_n_o to be set.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
       The following program traverses the directory tree under the path named
       in its first command-line argument, or under the current directory if
       no argument is supplied. It displays various information about each
       file. The second command-line argument can be used to specify
       characters that control the value assigned to the _f_l_a_g_s argument when
       calling _n_f_t_w().

           ##iinncclluuddee <<ffttww..hh>>
           ##iinncclluuddee <<ssttddiioo..hh>>
           ##iinncclluuddee <<ssttddlliibb..hh>>
           ##iinncclluuddee <<ssttrriinngg..hh>>
           ##iinncclluuddee <<ssttddiinntt..hh>>

           static int
           display_info(const char *fpath, const struct stat *sb,
               int tflag, struct FTW *ftwbuf)
           {
               printf("%-3s %2d %7jd   %-40s %d %s\n",
                   (tflag == FTW_D) ?   "d"   : (tflag == FTW_DNR) ? "dnr" :
                   (tflag == FTW_DP) ?  "dp"  : (tflag == FTW_F) ?
                       (S_ISBLK(sb->st_mode) ? "f b" :
                        S_ISCHR(sb->st_mode) ? "f c" :
                        S_ISFIFO(sb->st_mode) ? "f p" :
                        S_ISREG(sb->st_mode) ? "f r" :
                        S_ISSOCK(sb->st_mode) ? "f s" : "f ?") :
                   (tflag == FTW_NS) ?  "ns"  : (tflag == FTW_SL) ?  "sl" :
                   (tflag == FTW_SLN) ? "sln" : "?",
                   ftwbuf->level, (intmax_t) sb->st_size,
                   fpath, ftwbuf->base, fpath + ftwbuf->base);
               return 0;           /* To tell nftw() to continue */
           }

           int
           main(int argc, char *argv[])
           {
               int flags = 0;

               if (argc > 2 && strchr(argv[2], 'd') != NULL)
                   flags |= FTW_DEPTH;
               if (argc > 2 && strchr(argv[2], 'p') != NULL)
                   flags |= FTW_PHYS;

               if (nftw((argc < 2) ? "." : argv[1], display_info, 20, flags) == -1)
               {
                   perror("nftw");
                   exit(EXIT_FAILURE);
               }

               exit(EXIT_SUCCESS);
           }

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       The _n_f_t_w() function may allocate dynamic storage during its operation.
       If _n_f_t_w() is forcibly terminated, such as by _l_o_n_g_j_m_p() or _s_i_g_l_o_n_g_j_m_p()
       being executed by the function pointed to by _f_n or an interrupt
       routine, _n_f_t_w() does not have a chance to free that storage, so it
       remains permanently allocated. A safe way to handle interrupts is to
       store the fact that an interrupt has occurred, and arrange to have the
       function pointed to by _f_n return a non-zero value at its next
       invocation.

RRAATTIIOONNAALLEE
       None.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _f_d_o_p_e_n_d_i_r(), _f_s_t_a_t_a_t(), _r_e_a_d_d_i_r()

       The Base Definitions volume of POSIX.1-2008, <<ffttww..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
       -- Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, Copyright (C) 2013 by the Institute of
       Electrical and Electronics Engineers, Inc and The Open Group.  (This is
       POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
       event of any discrepancy between this version and the original IEEE and
       The Open Group Standard, the original IEEE and The Open Group Standard
       is the referee document. The original Standard can be obtained online
       at http://www.unix.org/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2013                              NFTW(3)
