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 stat structure with info about the object, filled in as if stat() or lstat() 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 also FTW_NS below). πŸ”—
  • FTW_NS β€” for an object other than a symbolic link on which stat() could not successfully be executed. And here's the spicy bit: if the object is a symbolic link and stat() failed, it is unspecified whether ftw() passes FTW_SL or FTW_NS to 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:

  1. the tree is exhausted,
  2. an invocation of fn returns a non-zero value,
  3. or some error, other than [EACCES], is detected within ftw().

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 return 0. βœ…
  • If the function pointed to by fn returns a non-zero value, ftw() shall stop its tree traversal and return whatever value was returned by the function pointed to by fn. πŸ›‘
  • If ftw() detects an error, it shall return -1 and set errno to 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 of path or read permission is denied for path.
  • ELOOP β€” A loop exists in symbolic links encountered during resolution of the path argument. πŸŒ€
  • ENAMETOOLONG β€” The length of a component of a pathname is longer than {NAME_MAX}.
  • ENOENT β€” A component of path does not name an existing file or path is an empty string.
  • ENOTDIR β€” A component of path names an existing file that is neither a directory nor a symbolic link to a directory.
  • EOVERFLOW β€” A field in the stat structure 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 the ndirs argument is invalid.
  • ELOOP β€” More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the path argument.
  • 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>


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 πŸŽ€