FTW(3POSIX)
π¦ ftw β the "walk the whole file tree bestie" page π
ok so this is the POSIX Programmer's Manual version of the page (that's the manual name, mwah). Before we even get into it, real talk from the prologue:
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.
like, translation: this is the POSIX flavor of ftw, and Linux might do its own lil thing. check the Linux page if you're on Linux, or it might not even exist there. π
β¨ NAME β¨
ftw β traverse (walk) a file tree πΆβοΈπ³
π SYNOPSIS π
#include <ftw.h>
int ftw(const char *path, int (*fn)(const char *,
const struct stat *ptr, int flag), int ndirs);
π DESCRIPTION π
okay so ftw() recursively descends the directory hierarchy rooted in path. like it's going DOWN, it's going all the way in, no skips. π³οΈ
For every single object in that hierarchy, ftw() shall call the function pointed to by fn, and it passes it three things:
- a pointer to a null-terminated character string with the name of the object π
- a pointer to a
statstructure with info about the object, filled in as ifstat()orlstat()had been called to retrieve it π - that integer
flagβ and here are the possible values, defined in the<ftw.h>header: FTW_Dβ for a directory. πFTW_DNRβ for a directory that cannot be read. π«FTW_Fβ for a non-directory file. πFTW_SLβ for a symbolic link (but see alsoFTW_NSbelow). πFTW_NSβ for an object other than a symbolic link on whichstat()could not successfully be executed. And here's the spicy bit: if the object is a symbolic link andstat()failed, it is unspecified whetherftw()passesFTW_SLorFTW_NSto the user-supplied function. like, nobody knows, it's giving mystery. π
If the integer is FTW_DNR, descendants of that directory shall not be processed. If the integer is FTW_NS, the stat structure contains undefined values. π An example of an object that would cause FTW_NS to be passed to the function pointed to by fn would be a file in a directory with read but without execute (search) permission.
ftw() shall visit a directory before visiting any of its descendants. parent first, always, respect the hierarchy bestie π
ftw() shall use at most one file descriptor for each level in the tree. ποΈ
The argument ndirs should be in the range [1,{OPEN_MAX}].
The tree traversal shall continue until either:
- the tree is exhausted,
- an invocation of
fnreturns a non-zero value, - or some error, other than
[EACCES], is detected withinftw().
The ndirs argument shall specify the maximum number of directory streams or file descriptors or both available for use by ftw() while traversing the tree. When ftw() returns it shall close any directory streams and file descriptors it uses β not counting any opened by the application-supplied fn function. π
βοΈ
The results are unspecified if the application-supplied fn function does not preserve the current working directory. like do NOT move the cwd on her, she will not recover. π
Also, heads up: ftw() need not be thread-safe. π§΅π
π₯ RETURN VALUE π₯
- If the tree is exhausted,
ftw()shall return0. β - If the function pointed to by
fnreturns a non-zero value,ftw()shall stop its tree traversal and return whatever value was returned by the function pointed to byfn. π - If
ftw()detects an error, it shall return-1and seterrnoto indicate the error. β
If ftw() encounters an error other than [EACCES] (see FTW_DNR and FTW_NS above), it shall return -1 and set errno to indicate the error. The external variable errno may contain any error value that is possible when a directory is opened or when one of the stat functions is executed on a directory or file.
π ERRORS π
The ftw() function shall fail if:
EACCESβ Search permission is denied for any component ofpathor read permission is denied forpath.ELOOPβ A loop exists in symbolic links encountered during resolution of thepathargument. πENAMETOOLONGβ The length of a component of a pathname is longer than{NAME_MAX}.ENOENTβ A component ofpathdoes not name an existing file orpathis an empty string.ENOTDIRβ A component ofpathnames an existing file that is neither a directory nor a symbolic link to a directory.EOVERFLOWβ A field in thestatstructure cannot be represented correctly in the current programming environment for one or more files found in the file hierarchy. π
The ftw() function may fail if:
EINVALβ The value of thendirsargument is invalid.ELOOPβ More than{SYMLOOP_MAX}symbolic links were encountered during resolution of thepathargument.ENAMETOOLONGβ 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}.
In addition, if the function pointed to by fn encounters system errors, errno may be set accordingly.
okay bestie, everything past this point is informative β the following sections are informative. π’
π EXAMPLES π
πΈ Walking a Directory Structure πΈ
The following example walks the current directory structure, calling the fn function for every directory entry, using at most 10 file descriptors:
#include <ftw.h>
...
if (ftw(".", fn, 10) != 0) {
perror("ftw"); exit(2);
}
π«Ά APPLICATION USAGE π«Ά
ftw() may allocate dynamic storage during its operation. So if ftw() is forcibly terminated β like by longjmp() or siglongjmp() being executed by the function pointed to by fn or an interrupt routine β ftw() does not have a chance to free that storage, so it remains permanently allocated. π« leaking, periodt.
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 fn return a non-zero value at its next invocation. that's the move. β¨
Also, real talk: applications should use the nftw() function instead of the obsolescent ftw() function. π like ftw() is the older sister, nftw() is the glow-up.
π§ RATIONALE π§
None. π (yes literally none, that's the whole section, she said nothing and slayed)
π FUTURE DIRECTIONS π
The ftw() function may be removed in a future version. β°οΈ get your nftw() in order bestie
π SEE ALSO π
fdopendir(), fstatat(), longjmp(), nftw(), siglongjmp()
The Base Definitions volume of POSIX.1β2017, <ftw.h>, <sys_stat.h>
β COPYRIGHT β
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. 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.opengroup.org/unix/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 .
and that's the whole thing bestie π this one came to you from IEEE/The Open Group, dated 2017, and the page itself goes by FTW(3POSIX). walk your trees responsibly, close your descriptors, and don't forget her π
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms