Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

nftw, nftw64, nftwx, nftw64x - traverse file tree

&pagelevel(4)&pagelevel

Syntax

#include <ftw.h>

int nftw(const char *path,
       int (*fn) (const char *, const struct stat *, in , struct FTW *),
       int depth, int flags);

int nftw64(const char *path,
       int (*fn) (const char *, const struct stat64 *, in , struct FTW *),
       int depth, int flags);

CRTE111A30

int nftwx(const char *path,
       int (*fn) (const char *, const struct statx *, in , struct FTW *),
       int depth, int flags);

int nftw64x(const char *path,
       int (*fn) (const char *, const struct stat64x *, in , struct FTW *),
       int depth, int flags); (End)

Description

nftw() recursively searches the directory hierarchy that begins with path. nftw() works similarly to ftw(), but also processes the flags argument, which is formed by bitwise inclusive ORing of the following values:

FTW_CHDIR

The searched directory becomes the current working directory. If FTW_CHDIR is not set, the current working directory remains unchanged.

FTW_DEPTH

Before the directory itself, all subdirectories are traversed. If FTW_DEPTH is not set, the directory is traversed first.

FTW_MOUNT

Only directories in the same file system as path are traversed. If FTW_MOUNT is not set, mounted directories are also traversed.

FTW_PHYS

The directory hierarchy is physically traversed; nftw() does not follow any symbolic links, but reports the links instead.
If FTW_PHYS is not set, nftw() follows symbolic links. nftw() does not report the same file twice.

For every file or directory found, nftw() calls the user-defined function fn with the following four arguments:

  1. Pathname of the object.

  2. Pointer to the stat buffer containing information on the object (obtained by calling the function stat()).

  3. Number of type integer, in which nftw() provides additional information.

    FTW_F

    The object is a file.

    FTW_D

    The object is a directory.

    FTW_DP

    The object is a directory; subdirectories have already been traversed (this situation can only occur if flags contains the value FTW_DEPTH).

    FTW_SLN

    The object is a symbolic link that points to a non-existent file (this situation can only occur if the flags does not contain the value FTW_PHYS).

    FTW_DNR

    The object is a directory which cannot be read. fn() is not called for any of the files in it or directories under it.

    FTW_NS

    stat() cannot process the object because the access permissions are not sufficient. The stat buffer passed to fn is undefined. If stat() fails for other reasons, nftw() will fail and return -1.

  4. Pointer to a struct FTW which contains the following elements:

    int base;

    int level;                

nftw() uses one file descriptor for each level in the file tree. The depth argument limits the number of file descriptors used. If depth is zero or negative, this has the same effect as the value 1. depth must not be greater than the number of file descriptors available at the specified time. If the nftw() function returns, it closes all file descriptors that it opened but none of the ones that were opened by fn.

nftw() descends the file tree from the highest hierarchy level onward until either the tree has been exhausted, an fn call returns a non-zero value, or an error is detected within nftw() (e.g. an I/O error).

There is no difference in functionality between nftw() and nftw64() except that nftw64() calls stat64() instead of stat() and passes a pointer to a stat64 structure to fn.

The functions with the suffix x behave like the functions of the same name without a suffix, except that they reference the corresponding stat() function and stat structure also with suffix x and therefore will work correctly after 01/19/2038 03:14:07 UTC.

Return val.

0

if the tree has been exhausted and fn () always returned the value 0.


Return value of the fn () function



if fn () returns a value != 0, nftw() stops traversing the file tree and returns the value that was returned by fn


-1

if nftw() detects an error other than EACCES. errno is set to indicate the error.

Errors

nftw(), nftw64(), nftwx() and nftw64x()will fail if: 

 

EACCES

Search permission is denied for any component of path, or
read permission is denied for path or
fn () returns the value -1 and does not reset.


ENAMETOOLONG



The length of the path argument is greater than {PATH_MAX} or a component of the pathname is longer than {NAME_MAX}.

The resolving a symbolic link led to an interim result whose length exceeds  {PATH_MAX}.


ENOENT

A component of the path prefix does not exist or path is an empty string.


ENOTDIR

A component of path is not a directory.


ELOOP

Too many symbolic links were encountered in resolving path.


EMFILE

{OPEN_MAX} file descriptors are already open.


ENFILE

Too many files are open.


errno can also be set if stat() or the function pointed to by fn() sets errno.

Hinweis

Since nftw() is recursive, it is possible for it to terminate with a memory error when applied to very deep file structures.

Siehe auch

ftw(), lstat(), opendir(), readdir(), stat(), ftw.h.