1 .\" Copyright (c) 1993 Michael Haardt (michael@moria.de)
2 .\" and copyright (c) 1999 Andries Brouwer (aeb@cwi.nl)
3 .\" and copyright (c) 2006 Justin Pryzby <justinpryzby@users.sf.net>
4 .\" and copyright (c) 2006 Michael Kerrisk <mtk-manpages@gmx.net>
6 .\" This is free documentation; you can redistribute it and/or
7 .\" modify it under the terms of the GNU General Public License as
8 .\" published by the Free Software Foundation; either version 2 of
9 .\" the License, or (at your option) any later version.
11 .\" The GNU General Public License's references to "object code"
12 .\" and "executables" are to be interpreted as the output of any
13 .\" document formatting or typesetting system, including
14 .\" intermediate and printed output.
16 .\" This manual is distributed in the hope that it will be useful,
17 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
18 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 .\" GNU General Public License for more details.
21 .\" You should have received a copy of the GNU General Public
22 .\" License along with this manual; if not, write to the Free
23 .\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
26 .\" Modified Sun Jul 25 11:02:22 1993 by Rik Faith (faith@cs.unc.edu)
27 .\" 2006-05-24, Justin Pryzby <justinpryzby@users.sf.net>
28 .\" document FTW_ACTIONRETVAL; include .SH "RETURN VALUE";
29 .\" 2006-05-24, Justin Pryzby <justinpryzby@users.sf.net> and
30 .\" Michael Kerrisk <mtk-manpages@gmx.net>
31 .\" reorganized and rewrote much of the page
32 .\" 2006-05-24, Michael Kerrisk <mtk-manpages@gmx.net>
33 .\" Added an example program.
34 .TH FTW 3 2006-05-24 "Linux" "Linux Programmer's Manual"
36 ftw, nftw \- file tree walk
41 .BI "int ftw(const char *" dirpath ,
42 .BR " int (*" fn ") (const char *" fpath ", const struct stat *" sb ,
43 .BI " int " typeflag ),
44 .BI " int " nopenfd );
46 .B #define _XOPEN_SOURCE 500
49 .BI "int nftw(const char *" dirpath ,
50 .BI " int (*" fn ") (const char *" fpath ", const struct stat *" sb ,
51 .BI " int " typeflag ", struct FTW *" ftwbuf ),
52 .BI " int " nopenfd ", int " flags );
55 \fBftw\fP() walks through the directory tree that is
56 located under the directory \fIdirpath\fP,
57 and calls \fIfn\fP() once for each entry in the tree.
58 By default, directories are handled before the files and
59 subdirectories they contain (pre-order traversal).
61 To avoid using up all of the calling process's file descriptors,
62 \fInopenfd\fP specifies the maximum number of directories that
63 \fBftw\fP() will hold open simultaneously.
65 the search depth exceeds this, \fBftw\fP() will become slower because
66 directories have to be closed and reopened. \fBftw\fP() uses at most
67 one file descriptor for each level in the directory tree.
69 For each entry found in the tree,
72 \fIfn\fP() with three arguments:
78 is the pathname of the entry relative to
83 structure returned by a call to
88 is an integer that has one of the following values:
100 is a directory which can't be read.
107 which is not a symbolic link.
111 is a symbolic link and
113 failed, POSIX.1 states
114 that it is undefined whether \fBFTW_NS\fP or \fBFTW_SL\fP (see below)
118 To stop the tree walk, \fIfn\fP() returns a non-zero value; this
119 value will become the return value of \fBftw\fP().
120 As long as \fIfn\fP() returns 0,
121 \fBftw\fP() will continue either until it has traversed the entire tree,
122 in which case it will return zero,
123 or until it encounters an error (such as a
125 failure), in which case it will return \-1.
127 Because \fBftw\fP() uses dynamic data structures, the only safe way to
128 exit out of a tree walk is to return a non-zero value from \fIfn\fP().
129 To allow a signal to terminate the walk without causing a memory leak,
130 have the handler set a global flag that is checked by \fIfn\fP().
131 \fIDon't\fP use \fBlongjmp\fP(3) unless the program is going to terminate.
133 The function \fBnftw\fP() is the same as \fBftw\fP(),
134 except that it has one additional argument, \fIflags\fP,
135 and calls \fIfn\fP() with one more argument, \fIftwbuf\fP.
137 This \fIflags\fP argument is formed by ORing zero or more of the
140 .BR FTW_ACTIONRETVAL " (since glibc 2.3.3)"
141 If this glibc-specific flag is set, then
143 handles the return value from
147 should return one of the following values:
151 Instructs \fBnftw\fP() to continue normally.
154 If \fIfn\fP() returns this value, then
155 siblings of the current entry will be skipped,
156 and processing continues in the parent.
157 .\" If \fBFTW_DEPTH\fP
158 .\" is set, the entry's parent directory is processed next (with
159 .\" \fIflag\fP set to \fBFTW_DP\fP).
162 If \fIfn\fP() is called with an entry that is a directory
163 (\fItypeflag\fP is \fBFTW_D\fP), this return
164 value will prevent objects within that directory from being passed as
165 arguments to \fIfn\fP().
167 continues processing with the next sibling of the directory.
170 Causes \fBnftw\fP() to return immediately with the return value
173 Other return values could be associated with new actions in the future;
174 \fIfn\fP() should not return values other than those listed above.
176 The feature test macro _GNU_SOURCE must be defined in order to
177 obtain the definition of \fBFTW_ACTIONRETVAL\fP from \fI<ftw.h>\fP.
183 to each directory before handling its contents.
184 This is useful if the program needs to perform some action
185 in the directory in which \fIfpath\fP resides.
188 If set, do a post-order traversal, that is, call \fIfn\fP() for
189 the directory itself \fIafter\fP handling the contents of the directory
190 and its subdirectories.
191 (By default, each directory is handled \fIbefore\fP its contents.)
194 If set, stay within the same file system
195 (i.e., do not cross mount points).
198 If set, do not follow symbolic links.
199 (This is what you want.)
200 If not set, symbolic links are followed, but no file is reported twice.
202 If \fBFTW_PHYS\fP is not set, but \fBFTW_DEPTH\fP is set,
205 is never called for a directory that would be a descendant of itself.
207 For each entry in the directory tree,
218 may receive any of the same values as with
220 or any of the following values:
224 is a directory, and \fBFTW_DEPTH\fP was specified in \fIflags\fP.
226 and subdirectories within \fIfpath\fP have been processed.
230 is a symbolic link, and \fBFTW_PHYS\fP was set in \fIflags\fP.
234 is a symbolic link pointing to a nonexistent file.
235 (This occurs only if \fBFTW_PHYS\fP is not set.)
237 The fourth argument that
239 supplies when calling
241 is a structure of type \fIFTW\fP:
253 is the offset of the filename (i.e., basename component)
254 in the pathname given in
259 in the directory tree, relative to the root of the tree
263 These functions return 0 on success, and \-1 if an error occurs.
265 .\" FIXME check the following
266 If \fIfn\fP() returns non-zero,
267 then the tree walk is terminated and the value returned by \fIfn\fP()
268 is returned as the result of \fBftw\fP() or \fBnftw\fP().
270 If \fBnftw\fP() is called with the \fBFTW_ACTIONRETVAL\fP flag,
271 then the only non-zero value that should be used by \fIfn\fP()
272 to terminate the tree walk is \fBFTW_STOP\fP,
273 and that value is returned as the result of \fBnftw\fP().
277 and the use of \fBFTW_SL\fP with
279 were introduced in XPG4v2.
283 will never use \fBFTW_SL\fP, on other systems \fBFTW_SL\fP occurs only
284 for symbolic links that do not point to an existing file,
285 and again on other systems
287 will use \fBFTW_SL\fP for each symbolic link. For predictable control, use
290 Under Linux, libc4 and libc5 and glibc 2.0.6 will
291 use \fBFTW_F\fP for all objects (files, symbolic links, fifos, etc)
292 that can be stat'ed but are not a directory.
296 is available since glibc 2.1.
298 \fBFTW_ACTIONRETVAL\fP is glibc specific.
300 POSIX.1-2001, SVID3, XPG4v2.
302 The following program traverses the directory tree under the path named
303 in its first command-line argument, or under the current directory
304 if no argument is supplied.
305 It displays various information about each file.
306 The second-command line argument can be used to specify characters that
307 control the value assigned to the \fIflags\fP
308 argument when calling \fBnftw\fP().
311 #define _XOPEN_SOURCE 500
318 display_info(const char *fpath, const struct stat *sb,
319 int tflag, struct FTW *ftwbuf)
321 printf("%-3s %2d %7lld %-40s %d %s\\n",
322 (tflag == FTW_D) ? "d" : (tflag == FTW_DNR) ? "dnr" :
323 (tflag == FTW_DP) ? "dp" : (tflag == FTW_F) ? "f" :
324 (tflag == FTW_DP) ? "dp" : (tflag == FTW_SL) ? "sl" :
325 (tflag == FTW_SLN) ? "sln" : "???",
326 ftwbuf->level, (long long) sb->st_size,
327 fpath, ftwbuf->base, fpath + ftwbuf->base);
328 return 0; /* To tell nftw() to continue */
332 main(int argc, char *argv[])
336 if (argc > 2 && strchr(argv[2], 'd') != NULL)
338 if (argc > 2 && strchr(argv[2], 'p') != NULL)
341 nftw((argc < 2) ? "." : argv[1], display_info, 20, flags);