ftw(3) Library Functions Manual ftw(3) ftw, nftw - C (libc, -lc) #include int nftw(const char *dirpath, typeof(int (const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)) *fn, int nopenfd, int flags); [[deprecated]] int ftw(const char *dirpath, typeof(int (const char *fpath, const struct stat *sb, int typeflag)) *fn, int nopenfd); glibc (. feature_test_macros(7)): nftw(): _XOPEN_SOURCE >= 500 nftw() walks through the directory tree that is located under the directory dirpath, and calls fn() once for each entry in the tree. By default, directories are handled before the files and subdirectories they contain (preorder traversal). , nopenfd nftw() . , nftw() , . nftw() . For each entry found in the tree, nftw() calls fn() with four arguments: fpath, sb, typeflag, and ftwbuf. fpath is the pathname of the entry, and is expressed either as a pathname relative to the calling process's current working directory at the time of the call to nftw(), if dirpath was expressed as a relative pathname, or as an absolute pathname, if dirpath was expressed as an absolute pathname. sb is a pointer to the stat structure returned by a call to stat(2) for fpath. typeflag, fn(), , : FTW_F fpath FTW_D fpath FTW_DNR fpath , FTW_DP fpath , flags FTW_DEPTH ( FTW_DEPTH flags, typeflag FTW_D). fpath. FTW_NS stat(2) fpath, . , , , fpath , , stat(2). , sb, . FTW_SL fpath flags FTW_PHYS. FTW_SLN fpath is a symbolic link pointing to a nonexistent file. (This occurs only if FTW_PHYS is not set.) In this case the sb argument passed to fn() contains information returned by performing lstat(2) on the "dangling" symbolic link. (But see BUGS.) (ftwbuf), nftw() fn(), FTW: struct FTW { int base; int level; }; base -- (.. ) , fpath. level -- fpath (dirpath 0). To stop the tree walk, fn() returns a nonzero value; this value will become the return value of nftw(). As long as fn() returns 0, nftw() will continue either until it has traversed the entire tree, in which case it will return zero, or until it encounters an error (such as a malloc(3) failure), in which case it will return -1. nftw() , fn(). , fn(). longjmp(3) . flags nftw() 0 : FTW_ACTIONRETVAL ( glibc 2.3.3) , glibc, , nftw() fn() . fn() : FTW_CONTINUE nftw() . FTW_SKIP_SIBLINGS If fn() returns this value, then siblings of the current entry will be skipped, and processing continues in the parent. FTW_SKIP_SUBTREE If fn() is called with an entry that is a directory (typeflag is FTW_D), this return value will prevent objects within that directory from being passed as arguments to fn(). nftw() continues processing with the next sibling of the directory. FTW_STOP nftw() FTW_STOP. Other return values could be associated with new actions in the future; fn() should not return values other than those listed above. FTW_ACTIONRETVAL , _GNU_SOURCE. FTW_CHDIR , chdir(2) . , - , fpath ( , fpath fn). FTW_DEPTH If set, do a post-order traversal, that is, call fn() for the directory itself after handling the contents of the directory and its subdirectories. (By default, each directory is handled before its contents.) FTW_MOUNT , (.. ). FTW_PHYS , (, ). , , . FTW_PHYS , FTW_DEPTH, fn() , . ftw() ftw() nftw(). : o ftw() flags. , nftw() flags . o fn() . o , typeflag fn() : FTW_F, FTW_D, FTW_DNR, FTW_NS () FTW_SL. 0 -1 . If fn() returns nonzero, then the tree walk is terminated and the value returned by fn() is returned as the result of ftw() or nftw(). nftw() FTW_ACTIONRETVAL, fn() FTW_STOP, nftw(). attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |nftw() | | MT-Safe cwd | +----------------------------+----------------------------------------------------------+--------------------------+ |ftw() | | MT-Safe | +----------------------------+----------------------------------------------------------+--------------------------+ In some implementations (e.g., glibc), ftw() will never use FTW_SL; on other systems FTW_SL occurs only for symbolic links that do not point to an existing file; and again on other systems ftw() will use FTW_SL for each symbolic link. If fpath is a symbolic link and stat(2) failed, POSIX.1-2008 states that it is undefined whether FTW_NS or FTW_SL is passed in typeflag. For predictable results, use nftw(). POSIX.1-2008. ftw() POSIX.1-2001, SVr4, SUSv1. POSIX.1-2008 marks it as obsolete. nftw() glibc 2.1. POSIX.1-2001, SUSv1. FTW_SL POSIX.1-2001, SUSv1. POSIX.1-2008 , , fn . According to POSIX.1-2008, when the typeflag argument passed to fn() contains FTW_SLN, the buffer pointed to by sb should contain information about the dangling symbolic link (obtained by calling lstat(2) on the link). Early glibc versions correctly followed the POSIX specification on this point. However, as a result of a regression introduced in glibc 2.4, the contents of the buffer pointed to by sb were undefined when FTW_SLN is passed in typeflag. (More precisely, the contents of the buffer were left unchanged in this case.) This regression was eventually fixed in glibc 2.30, so that the glibc implementation (once more) follows the POSIX specification. , , . . , flags nftw(). #define _XOPEN_SOURCE 500 #include #include #include #include #include static int display_info(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf) { printf("%-3s %2d ", (tflag == FTW_D) ? "d" : (tflag == FTW_DNR) ? "dnr" : (tflag == FTW_DP) ? "dp" : (tflag == FTW_F) ? "f" : (tflag == FTW_NS) ? "ns" : (tflag == FTW_SL) ? "sl" : (tflag == FTW_SLN) ? "sln" : "???", ftwbuf->level); if (tflag == FTW_NS) printf("-------"); else printf("%7jd", (intmax_t) sb->st_size); printf(" %-40s %d %s\n", 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); } stat(2), fts(3), readdir(3) () Azamat Hackimov , Dmitry Bolkhovskikh , Yuri Kozlov , Kirill Rekhov ; GNU (GNU General Public License - GPL, 3 ) , - . - , , () () () <>. Linux 6.15 17 2025 . ftw(3)