.\" and copyright (c) 2006 Justin Pryzby <justinpryzby@users.sf.net>
.\" and copyright (c) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
.\"
-.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
-.\" This is free documentation; you can redistribute it and/or
-.\" modify it under the terms of the GNU General Public License as
-.\" published by the Free Software Foundation; either version 2 of
-.\" the License, or (at your option) any later version.
-.\"
-.\" The GNU General Public License's references to "object code"
-.\" and "executables" are to be interpreted as the output of any
-.\" document formatting or typesetting system, including
-.\" intermediate and printed output.
-.\"
-.\" This manual is distributed in the hope that it will be useful,
-.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
-.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-.\" GNU General Public License for more details.
-.\"
-.\" You should have received a copy of the GNU General Public
-.\" License along with this manual; if not, see
-.\" <http://www.gnu.org/licenses/>.
-.\" %%%LICENSE_END
+.\" SPDX-License-Identifier: GPL-2.0-or-later
.\"
.\" Modified Sun Jul 25 11:02:22 1993 by Rik Faith (faith@cs.unc.edu)
.\" 2006-05-24, Justin Pryzby <justinpryzby@users.sf.net>
.\" 2006-05-24, Michael Kerrisk <mtk.manpages@gmail.com>
.\" Added an example program.
.\"
-.TH FTW 3 2015-04-19 "Linux" "Linux Programmer's Manual"
+.TH FTW 3 2021-03-22 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
.SH NAME
ftw, nftw \- file tree walk
+.SH LIBRARY
+Standard C library
+.RI ( libc ", " \-lc )
.SH SYNOPSIS
.nf
.B #include <ftw.h>
-
+.PP
.BI "int nftw(const char *" dirpath ,
-.BI " int (*" fn ") (const char *" fpath ", const struct stat *" sb ,
-.BI " int " typeflag ", struct FTW *" ftwbuf ),
+.BI " int (*" fn ")(const char *" fpath ", const struct stat *" sb ,
+.BI " int " typeflag ", struct FTW *" ftwbuf ),
.BI " int " nopenfd ", int " flags );
-
-.B #include <ftw.h>
-
+.PP
.BI "int ftw(const char *" dirpath ,
-.BI " int (*" fn ") (const char *" fpath ", const struct stat *" sb ,
-.BI " int " typeflag ),
+.BI " int (*" fn ")(const char *" fpath ", const struct stat *" sb ,
+.BI " int " typeflag ),
.BI " int " nopenfd );
.fi
-.sp
-.in -4n
+.PP
+.RS -4
Feature Test Macro Requirements for glibc (see
.BR feature_test_macros (7)):
-.in
-.sp
+.RE
+.PP
.BR nftw ():
-_XOPEN_SOURCE >= 500
+.nf
+ _XOPEN_SOURCE >= 500
+.fi
.SH DESCRIPTION
.BR nftw ()
walks through the directory tree that is
and calls \fIfn\fP() once for each entry in the tree.
By default, directories are handled before the files and
subdirectories they contain (preorder traversal).
-
+.PP
To avoid using up all of the calling process's file descriptors,
\fInopenfd\fP specifies the maximum number of directories that
.BR nftw ()
.BR nftw ()
uses at most
one file descriptor for each level in the directory tree.
-
+.PP
For each entry found in the tree,
.BR nftw ()
calls
current working directory at the time of the call to
.BR nftw (),
if
-.IR dirpath
+.I dirpath
was expressed as a relative pathname,
or as an absolute pathname, if
.I dirpath
.BR stat (2)
for
.IR fpath .
+.PP
+The
.I typeflag
+argument passed to
+.IR fn ()
is an integer that has one of the following values:
.TP
.B FTW_F
but did not have execute permission,
so that the file could not be reached for
.BR stat (2).
+The contents of the buffer pointed to by
+.I sb
+are undefined.
.TP
.B FTW_SL
.I fpath
.I fpath
is a symbolic link pointing to a nonexistent file.
(This occurs only if \fBFTW_PHYS\fP is not set.)
+In this case the
+.I sb
+argument passed to
+.IR fn ()
+contains information returned by performing
+.BR lstat (2)
+on the "dangling" symbolic link.
+(But see BUGS.)
.PP
-The fourth argument that
+The fourth argument
+.RI ( ftwbuf )
+that
.BR nftw ()
supplies when calling
\fIfn\fP()
-is a structure of type \fIFTW\fP:
+is a pointer to a structure of type \fIFTW\fP:
+.PP
.in +4n
-.nf
-
+.EX
struct FTW {
int base;
int level;
};
-
-.fi
+.EE
.in
+.PP
.I base
is the offset of the filename (i.e., basename component)
in the pathname given in
\fIDon't\fP use
.BR longjmp (3)
unless the program is going to terminate.
-
+.PP
The \fIflags\fP argument of
.BR nftw ()
is formed by ORing zero or more of the
.PP
Other return values could be associated with new actions in the future;
\fIfn\fP() should not return values other than those listed above.
-
+.PP
The feature test macro
.B _GNU_SOURCE
must be defined
If set, do not follow symbolic links.
(This is what you want.)
If not set, symbolic links are followed, but no file is reported twice.
-.sp
+.IP
If \fBFTW_PHYS\fP is not set, but \fBFTW_DEPTH\fP is set,
then the function
.IR fn ()
.IP * 3
.BR ftw ()
has no
-.IR flags
+.I flags
argument.
It behaves the same as when
.BR nftw ()
.BR FTW_SL .
.SH RETURN VALUE
These functions return 0 on success, and \-1 if an error occurs.
-
+.PP
If \fIfn\fP() returns nonzero,
then the tree walk is terminated and the value returned by \fIfn\fP()
is returned as the result of
.BR ftw ()
or
.BR nftw ().
-
+.PP
If
.BR nftw ()
is called with the \fBFTW_ACTIONRETVAL\fP flag,
.SH VERSIONS
.BR nftw ()
is available under glibc since version 2.1.
-.SH CONFORMING TO
+.SH ATTRIBUTES
+For an explanation of the terms used in this section, see
+.BR attributes (7).
+.ad l
+.nh
+.TS
+allbox;
+lbx lb lb
+l l l.
+Interface Attribute Value
+T{
+.BR nftw ()
+T} Thread safety MT-Safe cwd
+T{
+.BR ftw ()
+T} Thread safety MT-Safe
+.TE
+.hy
+.ad
+.sp 1
+.SH STANDARDS
POSIX.1-2001, POSIX.1-2008, SVr4, SUSv1.
POSIX.1-2008 marks
.BR ftw ()
and the use of \fBFTW_SL\fP with
.BR ftw ()
were introduced in SUSv1.
-.LP
+.PP
In some implementations (e.g., glibc),
.BR ftw ()
will never use \fBFTW_SL\fP, on other systems \fBFTW_SL\fP occurs only
.IR typeflag .
For predictable results, use
.BR nftw ().
-.SH EXAMPLE
+.SH BUGS
+According to POSIX.1-2008, when the
+.I typeflag
+argument passed to
+.IR fn ()
+contains
+.BR FTW_SLN ,
+the buffer pointed to by
+.I sb
+should contain information about the dangling symbolic link
+(obtained by calling
+.BR 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
+.I sb
+were undefined when
+.B FTW_SLN
+is passed in
+.IR typeflag .
+(More precisely, the contents of the buffer were left unchanged in this case.)
+This regression was eventually fixed in glibc 2.30,
+.\" https://bugzilla.redhat.com/show_bug.cgi?id=1422736
+.\" http://austingroupbugs.net/view.php?id=1121
+.\" glibc commit 6ba205b2c35e3e024c8c12d2ee1b73363e84da87
+.\" https://sourceware.org/bugzilla/show_bug.cgi?id=23501
+so that the glibc implementation (once more) follows the POSIX specification.
+.SH 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.
argument when calling
.BR nftw ().
.SS Program source
-.nf
+\&
+.EX
#define _XOPEN_SOURCE 500
#include <ftw.h>
#include <stdio.h>
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) ? "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);
+ 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\en",
+ fpath, ftwbuf\->base, fpath + ftwbuf\->base);
+
return 0; /* To tell nftw() to continue */
}
perror("nftw");
exit(EXIT_FAILURE);
}
+
exit(EXIT_SUCCESS);
}
-.fi
+.EE
.SH SEE ALSO
.BR stat (2),
.BR fts (3),