ftw(3) Library Functions Manual ftw(3) ftw, nftw - LIBRARY Standard C library (libc, -lc) #include int nftw(const char *dirpath, int (*fn)(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf), int nopenfd, int flags); [[deprecated]] int ftw(const char *dirpath, int (*fn)(const char *fpath, const struct stat *sb, int typeflag), int nopenfd); glibc (. feature_test_macros(7)): nftw(): _XOPEN_SOURCE >= 500 nftw() , dirpath, fn(). , ( ). , nopenfd nftw() . , nftw() , . nftw() . nftw() fn() : fpath, sb, fpath ftwbuf. fpath , nftw() ( dirpath ), ( dirpath ). sb stat, stat(2) 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). fn() ; nftw(). fn() 0, nftw() ( ), (, - malloc(3)). -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 . FTW_SKIP_SUBTREE fn() , (typeflag FTW_D), fn() . nftw() . FTW_STOP nftw() FTW_STOP. ; fn() , . FTW_ACTIONRETVAL , _GNU_SOURCE. FTW_CHDIR , chdir(2) . , - , fpath ( , fpath fn). FTW_DEPTH , , .. fn() ( ). 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 . fn() , , fn(), ftw() 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 ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . ftw(3)