NFTW(3P)
π NFTW(3P) β walk a file tree π
okay so bestie, we're reading NFTW(3P) from the POSIX Programmer's Manual today, and honestly? she ate. π¦ let's go.
πΈ PROLOG πΈ
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, for real β if you're on Linux, go check the Linux page too, because bestie might be acting a little different over there. π
π NAME π
nftw β walk a file tree. that's it, that's the whole girl. main character energy, no cap. β¨
π SYNOPSIS π
#include <ftw.h>
int nftw(const char *path, int (*fn)(const char *,
const struct stat *, int, struct FTW *), int fd_limit, int flags);
π DESCRIPTION π
okay so nftw() recursively descends the directory hierarchy rooted in path. she's basically ftw()'s cooler older sister, except she takes one extra argument: flags, which is a bitwise-inclusive OR of zero or more of these absolute icons:
FTW_CHDIRβ if set,nftw()shall change the current working directory to each directory as it reports files in that directory. if clear,nftw()shall not change the current working directory. (she stays put, respectfully)FTW_DEPTHβ if set,nftw()shall report all files in a directory before reporting the directory itself. if clear,nftw()shall report any directory before reporting the files in that directory.FTW_MOUNTβ if set,nftw()shall only report files in the same file system as path. if clear,nftw()shall report all files encountered during the walk. (no hopping the fence, bestie)FTW_PHYSβ if set,nftw()shall perform a physical walk and shall not follow symbolic links.
and now the combo lore, because this part is important:
- if
FTW_PHYSis clear andFTW_DEPTHis set,nftw()shall follow links instead of reporting them, but shall not report any directory that would be a descendant of itself. - if
FTW_PHYSis clear andFTW_DEPTHis clear,nftw()shall follow links instead of reporting them, but shall not report the contents of any directory that would be a descendant of itself.
At each file it encounters, nftw() shall call the user-supplied function fn with four arguments:
- The first argument is the pathname of the object.
- The second argument is a pointer to the
statbuffer containing information on the object, filled in as iffstatat(),stat(), orlstat()had been called to retrieve the information. - The third argument is an integer giving additional information. Its value is one of the following:
FTW_Dβ the object is a directory.FTW_DNRβ the object is a directory that cannot be read. The fn function shall not be called for any of its descendants.FTW_DPβ the object is a directory and subdirectories have been visited. (this condition shall only occur if theFTW_DEPTHflag is included in flags.)FTW_Fβ the object is a non-directory file.FTW_NSβ thestat()function failed on the object because of lack of appropriate permission. thestatbuffer passed to fn is undefined. failure ofstat()for any other reason is considered an error andnftw()shall return-1. (yes, undefined. be scared.)FTW_SLβ the object is a symbolic link. (this condition shall only occur if theFTW_PHYSflag is included in flags.)FTW_SLNβ the object is a symbolic link that does not name an existing file. (this condition shall only occur if theFTW_PHYSflag is not included in flags.)
- The fourth argument is a pointer to an
FTWstructure. The value ofbaseis the offset of the object's filename in the pathname passed as the first argument to fn. The value oflevelindicates depth relative to the root of the walk, where the root level is0.
β οΈ bestie listen. The results are unspecified if the application-supplied fn function does not preserve the current working directory. so likeβ¦ don't do that. π
The argument fd_limit sets the maximum number of file descriptors that shall be used by nftw() while traversing the file tree. At most one file descriptor shall be used for each directory level. (budget queen π)
The nftw() function need not be thread-safe. β¨
β RETURN VALUE β
nftw() shall continue until the first of the following conditions occurs:
- An invocation of fn shall return a non-zero value, in which case
nftw()shall return that value. - The
nftw()function detects an error other than[EACCES](seeFTW_DNRandFTW_NSabove), in which casenftw()shall return-1and seterrnoto indicate the error. - The tree is exhausted, in which case
nftw()shall return0.
π«Ά ERRORS π«Ά
The nftw() function shall fail if:
EACCESβ search permission is denied for any component of path or read permission is denied for path, or fn returns-1and does not reseterrno.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 thestatstructure cannot be represented correctly in the current programming environment for one or more files found in the file hierarchy.
The nftw() function may fail if:
ELOOPβ more than{SYMLOOP_MAX}symbolic links were encountered during resolution of the path argument.EMFILEβ all file descriptors available to the process are currently open.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}.ENFILEβ too many files are currently open in the system.
In addition, errno may be set if the function pointed to by fn causes errno to be set. (yes bestie, your callback can leak chaos into errno. she's messy like that. π
)
The following sections are informative.
π EXAMPLES π
The following program traverses the directory tree under the path named in its first command-line argument, or under the current directory if no argument is supplied. It displays various information about each file. The second command-line argument can be used to specify characters that control the value assigned to the flags argument when calling nftw().
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
static int
display_info(const char *fpath, const struct stat *sb,
int tflag, struct FTW *ftwbuf)
{
printf("%-3s %2d %7jd %-40s %d %s\n",
(tflag == FTW_D) ? "d" : (tflag == FTW_DNR) ? "dnr" :
(tflag == FTW_DP) ? "dp" : (tflag == FTW_F) ?
(S_ISBLK(sb->st_mode) ? "f b" :
S_ISCHR(sb->st_mode) ? "f c" :
S_ISFIFO(sb->st_mode) ? "f p" :
S_ISREG(sb->st_mode) ? "f r" :
S_ISSOCK(sb->st_mode) ? "f s" : "f ?") :
(tflag == FTW_NS) ? "ns" : (tflag == FTW_SL) ? "sl" :
(tflag == FTW_SLN) ? "sln" : "?",
ftwbuf->level, (intmax_t) sb->st_size,
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);
}
look at her go. passing d in that second arg gets you FTW_DEPTH, p gets you FTW_PHYS, and 20 is the fd_limit. periodt.
π APPLICATION USAGE π
The nftw() function may allocate dynamic storage during its operation. If nftw() is forcibly terminated, such as by longjmp() or siglongjmp() being executed by the function pointed to by fn or an interrupt routine, nftw() does not have a chance to free that storage, so it remains permanently allocated. π
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. do that instead, bestie. your memory will thank you. π«Ά
π§ RATIONALE π§
None. (she said nothing, your honor.)
πΈ FUTURE DIRECTIONS πΈ
None. (and she plans to keep saying nothing.)
π¦ SEE ALSO π¦
fdopendir()fstatat()readdir()
The Base Definitions volume of POSIX.1β2017, <ftw.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 page, bestie!! π mwah. this one came to you from IEEE/The Open Group, 2017 β yeah, the NFTW(3P) of it all. stay recursive, stay slay. π
β¨π¦
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms