]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/ftw.3
crypt.3: srcfix: rewrap source lines
[thirdparty/man-pages.git] / man3 / ftw.3
CommitLineData
fea681da 1.\" Copyright (c) 1993 Michael Haardt (michael@moria.de)
538b02cc
MK
2.\" and copyright (c) 1999 Andries Brouwer (aeb@cwi.nl)
3.\" and copyright (c) 2006 Justin Pryzby <justinpryzby@users.sf.net>
c11b1abf 4.\" and copyright (c) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 5.\"
1dd72f9c 6.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
fea681da
MK
7.\" This is free documentation; you can redistribute it and/or
8.\" modify it under the terms of the GNU General Public License as
9.\" published by the Free Software Foundation; either version 2 of
10.\" the License, or (at your option) any later version.
11.\"
12.\" The GNU General Public License's references to "object code"
13.\" and "executables" are to be interpreted as the output of any
14.\" document formatting or typesetting system, including
15.\" intermediate and printed output.
16.\"
17.\" This manual is distributed in the hope that it will be useful,
18.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
19.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20.\" GNU General Public License for more details.
21.\"
22.\" You should have received a copy of the GNU General Public
c715f741
MK
23.\" License along with this manual; if not, see
24.\" <http://www.gnu.org/licenses/>.
6a8d8745 25.\" %%%LICENSE_END
fea681da
MK
26.\"
27.\" Modified Sun Jul 25 11:02:22 1993 by Rik Faith (faith@cs.unc.edu)
538b02cc 28.\" 2006-05-24, Justin Pryzby <justinpryzby@users.sf.net>
d282bb24 29.\" document FTW_ACTIONRETVAL; include .SH RETURN VALUE;
538b02cc 30.\" 2006-05-24, Justin Pryzby <justinpryzby@users.sf.net> and
c11b1abf 31.\" Michael Kerrisk <mtk.manpages@gmail.com>
538b02cc 32.\" reorganized and rewrote much of the page
c11b1abf 33.\" 2006-05-24, Michael Kerrisk <mtk.manpages@gmail.com>
538b02cc 34.\" Added an example program.
3dd43f24 35.\"
4b8c67d9 36.TH FTW 3 2017-09-15 "Linux" "Linux Programmer's Manual"
fea681da
MK
37.SH NAME
38ftw, nftw \- file tree walk
39.SH SYNOPSIS
538b02cc 40.nf
fea681da 41.B #include <ftw.h>
f90f031e 42.PP
d954cea8
MK
43.BI "int nftw(const char *" dirpath ,
44.BI " int (*" fn ") (const char *" fpath ", const struct stat *" sb ,
45.BI " int " typeflag ", struct FTW *" ftwbuf ),
46.BI " int " nopenfd ", int " flags );
dbfe9c70 47.PP
d954cea8 48.B #include <ftw.h>
f90f031e 49.PP
c13182ef 50.BI "int ftw(const char *" dirpath ,
ca313a68 51.BI " int (*" fn ") (const char *" fpath ", const struct stat *" sb ,
538b02cc 52.BI " int " typeflag ),
8d3a1e4b 53.BI " int " nopenfd );
d954cea8 54.fi
68e4db0a 55.PP
d954cea8
MK
56.in -4n
57Feature Test Macro Requirements for glibc (see
58.BR feature_test_macros (7)):
59.in
68e4db0a 60.PP
d954cea8
MK
61.BR nftw ():
62_XOPEN_SOURCE >= 500
fea681da 63.SH DESCRIPTION
d954cea8 64.BR nftw ()
60a90ecd 65walks through the directory tree that is
538b02cc
MK
66located under the directory \fIdirpath\fP,
67and calls \fIfn\fP() once for each entry in the tree.
c13182ef 68By default, directories are handled before the files and
01d0a447 69subdirectories they contain (preorder traversal).
847e0d88 70.PP
c13182ef
MK
71To avoid using up all of the calling process's file descriptors,
72\fInopenfd\fP specifies the maximum number of directories that
d954cea8 73.BR nftw ()
60a90ecd 74will hold open simultaneously.
538b02cc 75When
60a90ecd 76the search depth exceeds this,
d954cea8 77.BR nftw ()
60a90ecd
MK
78will become slower because
79directories have to be closed and reopened.
d954cea8 80.BR nftw ()
60a90ecd 81uses at most
538b02cc 82one file descriptor for each level in the directory tree.
847e0d88 83.PP
c13182ef 84For each entry found in the tree,
d954cea8 85.BR nftw ()
538b02cc 86calls
d954cea8 87\fIfn\fP() with four arguments:
538b02cc
MK
88.IR fpath ,
89.IR sb ,
d954cea8 90.IR typeflag ,
538b02cc 91and
d954cea8 92.IR ftwbuf .
0daa9e92 93.I fpath
e409862f
MK
94is the pathname of the entry,
95and is expressed either as a pathname relative to the calling process's
96current working directory at the time of the call to
d954cea8 97.BR nftw (),
e409862f
MK
98if
99.IR dirpath
100was expressed as a relative pathname,
101or as an absolute pathname, if
102.I dirpath
103was expressed as an absolute pathname.
0daa9e92 104.I sb
538b02cc 105is a pointer to the
0daa9e92 106.I stat
c13182ef
MK
107structure returned by a call to
108.BR stat (2)
109for
538b02cc 110.IR fpath .
847e0d88 111.PP
cbc091e8 112The
0daa9e92 113.I typeflag
cbc091e8
MK
114argument passed to
115.IR fn ()
538b02cc 116is an integer that has one of the following values:
fea681da
MK
117.TP
118.B FTW_F
538b02cc 119.I fpath
21035964 120is a regular file.
fea681da
MK
121.TP
122.B FTW_D
538b02cc
MK
123.I fpath
124is a directory.
fea681da
MK
125.TP
126.B FTW_DNR
538b02cc
MK
127.I fpath
128is a directory which can't be read.
fea681da 129.TP
d954cea8
MK
130.B FTW_DP
131.I fpath
132is a directory, and \fBFTW_DEPTH\fP was specified in \fIflags\fP.
133(If
134.B FTW_DEPTH
135was not specified in
136.IR flags ,
137then directories will always be visited with
138.I typeflag
139set to
140.BR FTW_D .)
141All of the files
142and subdirectories within \fIfpath\fP have been processed.
143.TP
fea681da 144.B FTW_NS
c13182ef
MK
145The
146.BR stat (2)
147call failed on
538b02cc
MK
148.IR fpath ,
149which is not a symbolic link.
b466283a
MK
150The probable cause for this is that the caller had read permission
151on the parent directory, so that the filename
152.I fpath
153could be seen,
154but did not have execute permission,
155so that the file could not be reached for
156.BR stat (2).
d252ac9d
MK
157The contents of the buffer pointed to by
158.I sb
159are undefined.
d954cea8
MK
160.TP
161.B FTW_SL
538b02cc 162.I fpath
d954cea8
MK
163is a symbolic link, and \fBFTW_PHYS\fP was set in \fIflags\fP.
164.\" To obtain the definition of this constant from
165.\" .IR <ftw.h> ,
166.\" either
167.\" .B _BSD_SOURCE
168.\" must be defined, or
169.\" .BR _XOPEN_SOURCE
170.\" must be defined with a value of 500 or more.
171.TP
172.B FTW_SLN
173.I fpath
174is a symbolic link pointing to a nonexistent file.
175(This occurs only if \fBFTW_PHYS\fP is not set.)
687d31a4
MK
176On most implementations, in this case the
177.I sb
178argument passed to
179.IR fn ()
180contains information returned by performing
181.BR lstat (2)
182on the symbolic link.
183For the details on Linux, see BUGS.
d954cea8 184.PP
b8f9afd0
MK
185The fourth argument
186.RI ( ftwbuf )
187that
d954cea8
MK
188.BR nftw ()
189supplies when calling
190\fIfn\fP()
b8f9afd0 191is a pointer to a structure of type \fIFTW\fP:
e646a1ba 192.PP
d954cea8 193.in +4n
e646a1ba 194.EX
d954cea8
MK
195struct FTW {
196 int base;
197 int level;
198};
e646a1ba 199.EE
d954cea8 200.in
e646a1ba 201.PP
d954cea8
MK
202.I base
203is the offset of the filename (i.e., basename component)
204in the pathname given in
205.IR fpath .
206.I level
207is the depth of
208.I fpath
209in the directory tree, relative to the root of the tree
210.RI ( dirpath ,
211which has depth 0).
fea681da 212.PP
c7094399 213To stop the tree walk, \fIfn\fP() returns a nonzero value; this
60a90ecd 214value will become the return value of
d954cea8 215.BR nftw ().
538b02cc 216As long as \fIfn\fP() returns 0,
d954cea8 217.BR nftw ()
60a90ecd 218will continue either until it has traversed the entire tree,
c13182ef 219in which case it will return zero,
69515438 220or until it encounters an error (such as a
fea681da
MK
221.BR malloc (3)
222failure), in which case it will return \-1.
223.PP
60a90ecd 224Because
d954cea8 225.BR nftw ()
60a90ecd 226uses dynamic data structures, the only safe way to
c7094399 227exit out of a tree walk is to return a nonzero value from \fIfn\fP().
538b02cc
MK
228To allow a signal to terminate the walk without causing a memory leak,
229have the handler set a global flag that is checked by \fIfn\fP().
60a90ecd
MK
230\fIDon't\fP use
231.BR longjmp (3)
232unless the program is going to terminate.
847e0d88 233.PP
cd358be3 234The \fIflags\fP argument of
d954cea8
MK
235.BR nftw ()
236is formed by ORing zero or more of the
538b02cc 237following flags:
fea681da 238.TP
538b02cc
MK
239.BR FTW_ACTIONRETVAL " (since glibc 2.3.3)"
240If this glibc-specific flag is set, then
241.BR nftw ()
69515438 242handles the return value from
538b02cc
MK
243.IR fn ()
244differently.
245.IR fn ()
246should return one of the following values:
247.RS
248.TP
249.B FTW_CONTINUE
60a90ecd
MK
250Instructs
251.BR nftw ()
252to continue normally.
538b02cc 253.TP
5a967310
MK
254.B FTW_SKIP_SIBLINGS
255If \fIfn\fP() returns this value, then
256siblings of the current entry will be skipped,
257and processing continues in the parent.
258.\" If \fBFTW_DEPTH\fP
259.\" is set, the entry's parent directory is processed next (with
260.\" \fIflag\fP set to \fBFTW_DP\fP).
538b02cc
MK
261.TP
262.B FTW_SKIP_SUBTREE
c13182ef 263If \fIfn\fP() is called with an entry that is a directory
538b02cc
MK
264(\fItypeflag\fP is \fBFTW_D\fP), this return
265value will prevent objects within that directory from being passed as
266arguments to \fIfn\fP().
267.BR nftw ()
268continues processing with the next sibling of the directory.
269.TP
5a967310 270.B FTW_STOP
60a90ecd
MK
271Causes
272.BR nftw ()
273to return immediately with the return value
5a967310 274\fBFTW_STOP\fP.
538b02cc 275.PP
c13182ef 276Other return values could be associated with new actions in the future;
538b02cc 277\fIfn\fP() should not return values other than those listed above.
847e0d88 278.PP
c3dfd2c8
MK
279The feature test macro
280.B _GNU_SOURCE
e417acb0
MK
281must be defined
282(before including
283.I any
284header files)
285in order to
538b02cc
MK
286obtain the definition of \fBFTW_ACTIONRETVAL\fP from \fI<ftw.h>\fP.
287.RE
b7924f02
MK
288.TP
289.B FTW_CHDIR
290If set, do a
291.BR chdir (2)
292to each directory before handling its contents.
293This is useful if the program needs to perform some action
294in the directory in which \fIfpath\fP resides.
a00ca3d5
MK
295(Specifying this flag has no effect on the pathname that is passed in the
296.I fpath
297argument of
298.IR fn .)
b7924f02
MK
299.TP
300.B FTW_DEPTH
301If set, do a post-order traversal, that is, call \fIfn\fP() for
302the directory itself \fIafter\fP handling the contents of the directory
303and its subdirectories.
304(By default, each directory is handled \fIbefore\fP its contents.)
305.TP
306.B FTW_MOUNT
9ee4a2b6 307If set, stay within the same filesystem
0be9413e 308(i.e., do not cross mount points).
b7924f02
MK
309.TP
310.B FTW_PHYS
311If set, do not follow symbolic links.
312(This is what you want.)
313If not set, symbolic links are followed, but no file is reported twice.
bdd915e2 314.IP
c13182ef 315If \fBFTW_PHYS\fP is not set, but \fBFTW_DEPTH\fP is set,
b7924f02
MK
316then the function
317.IR fn ()
318is never called for a directory that would be a descendant of itself.
d954cea8
MK
319.SS ftw()
320.BR ftw ()
321is an older function that offers a subset of the functionality of
322.BR nftw ().
323The notable differences are as follows:
324.IP * 3
325.BR ftw ()
326has no
327.IR flags
328argument.
329It behaves the same as when
538b02cc 330.BR nftw ()
d954cea8
MK
331is called with
332.I flags
333specified as zero.
334.IP *
335The callback function,
336.IR fn (),
337is not supplied with a fourth argument.
338.IP *
339The range of values that is passed via the
538b02cc 340.I typeflag
d954cea8
MK
341argument supplied to
342.IR fn ()
343is smaller: just
344.BR FTW_F ,
345.BR FTW_D ,
346.BR FTW_DNR ,
347.BR FTW_NS ,
348and (possibly)
349.BR FTW_SL .
47297adb 350.SH RETURN VALUE
538b02cc 351These functions return 0 on success, and \-1 if an error occurs.
847e0d88 352.PP
c7094399 353If \fIfn\fP() returns nonzero,
538b02cc 354then the tree walk is terminated and the value returned by \fIfn\fP()
60a90ecd
MK
355is returned as the result of
356.BR ftw ()
357or
358.BR nftw ().
847e0d88 359.PP
60a90ecd
MK
360If
361.BR nftw ()
362is called with the \fBFTW_ACTIONRETVAL\fP flag,
c7094399 363then the only nonzero value that should be used by \fIfn\fP()
c13182ef 364to terminate the tree walk is \fBFTW_STOP\fP,
60a90ecd
MK
365and that value is returned as the result of
366.BR nftw ().
0b158300
MK
367.SH VERSIONS
368.BR nftw ()
369is available under glibc since version 2.1.
a83934bf
ZL
370.SH ATTRIBUTES
371For an explanation of the terms used in this section, see
372.BR attributes (7).
373.TS
374allbox;
375lb lb lb
376l l l.
377Interface Attribute Value
378T{
379.BR nftw ()
380T} Thread safety MT-Safe cwd
381T{
382.BR ftw ()
383T} Thread safety MT-Safe
384.TE
847e0d88 385.sp 1
47297adb 386.SH CONFORMING TO
c47fe813 387POSIX.1-2001, POSIX.1-2008, SVr4, SUSv1.
b7be83b7
MK
388POSIX.1-2008 marks
389.BR ftw ()
390as obsolete.
fea681da 391.SH NOTES
f6c19db0 392POSIX.1-2008 notes that the results are unspecified if
743e7eb3
MK
393.I fn
394does not preserve the current working directory.
395.PP
fea681da 396The function
31e9a9ec 397.BR nftw ()
8d3a1e4b 398and the use of \fBFTW_SL\fP with
31e9a9ec 399.BR ftw ()
68e1685c 400were introduced in SUSv1.
dd3568a1 401.PP
d954cea8 402In some implementations (e.g., glibc),
31e9a9ec 403.BR ftw ()
8d3a1e4b 404will never use \fBFTW_SL\fP, on other systems \fBFTW_SL\fP occurs only
fea681da
MK
405for symbolic links that do not point to an existing file,
406and again on other systems
31e9a9ec 407.BR ftw ()
c13182ef 408will use \fBFTW_SL\fP for each symbolic link.
d954cea8
MK
409If
410.I fpath
411is a symbolic link and
412.BR stat (2)
413failed, POSIX.1-2008 states
414that it is undefined whether \fBFTW_NS\fP or \fBFTW_SL\fP
415is passed in
416.IR typeflag .
417For predictable results, use
31e9a9ec 418.BR nftw ().
34a2ee18
MK
419.SH BUGS
420In the specification of
421.BR nftw (),
422POSIX.1 notes that when
423.B FTW_NS
424is passed as the
425.I typeflag
426argument of
427.IR fn (),
428then the contents of the buffer pointed to by the
429.I sb
430argument are undefined.
431The standard makes no such statement for the case where
432.B FTW_SLN
433is passed in
434.IR typeflag ,
435with the implication that the contents of the buffer pointed to by
436.I sb
437are defined.
438And indeed this is the case on most implementations: the buffer pointed to by
439.I sb
440contains the results produced by applying
441.BR lstat (2)
442to the symbolic link.
443In early glibc, the behavior was the same.
444However, since glibc 2.4, the contents of the buffer pointed to by
445.I sb
446are undefined when
447.B FTW_SLN
448is passed in
449.IR typeflag .
450This change
451.I appears
452to be an unintended regression,
453but it is not (yet) clear if the behavior will be restored to that
454provided in the original glibc implementation (and on other implementations).
455.\" FIXME .
456.\" https://bugzilla.redhat.com/show_bug.cgi?id=1422736
457.\" http://austingroupbugs.net/view.php?id=1121
538b02cc 458.SH EXAMPLE
69515438
MK
459The following program traverses the directory tree under the path named
460in its first command-line argument, or under the current directory
538b02cc
MK
461if no argument is supplied.
462It displays various information about each file.
76c44d83 463The second command-line argument can be used to specify characters that
c13182ef 464control the value assigned to the \fIflags\fP
60a90ecd
MK
465argument when calling
466.BR nftw ().
f30b7415 467.SS Program source
c7885256 468\&
e7d0bb47 469.EX
538b02cc
MK
470#define _XOPEN_SOURCE 500
471#include <ftw.h>
472#include <stdio.h>
473#include <stdlib.h>
474#include <string.h>
a15ae69a 475#include <stdint.h>
538b02cc
MK
476
477static int
c13182ef 478display_info(const char *fpath, const struct stat *sb,
538b02cc
MK
479 int tflag, struct FTW *ftwbuf)
480{
88f78c4a
MK
481 printf("%\-3s %2d ",
482 (tflag == FTW_D) ? "d" : (tflag == FTW_DNR) ? "dnr" :
483 (tflag == FTW_DP) ? "dp" : (tflag == FTW_F) ? "f" :
484 (tflag == FTW_NS) ? "ns" : (tflag == FTW_SL) ? "sl" :
485 (tflag == FTW_SLN) ? "sln" : "???",
486 ftwbuf\->level);
487
488 if (tflag == FTW_NS)
489 printf("\-\-\-\-\-\-\-");
490 else
491 printf("%7jd", (intmax_t) sb\->st_size);
492
493 printf(" %\-40s %d %s\\n",
494 fpath, ftwbuf\->base, fpath + ftwbuf\->base);
495
538b02cc
MK
496 return 0; /* To tell nftw() to continue */
497}
498
499int
500main(int argc, char *argv[])
501{
502 int flags = 0;
503
f81fb444 504 if (argc > 2 && strchr(argv[2], \(aqd\(aq) != NULL)
538b02cc 505 flags |= FTW_DEPTH;
f81fb444 506 if (argc > 2 && strchr(argv[2], \(aqp\(aq) != NULL)
538b02cc
MK
507 flags |= FTW_PHYS;
508
e4b7f997
MK
509 if (nftw((argc < 2) ? "." : argv[1], display_info, 20, flags)
510 == \-1) {
511 perror("nftw");
a15ae69a
MK
512 exit(EXIT_FAILURE);
513 }
88f78c4a 514
538b02cc
MK
515 exit(EXIT_SUCCESS);
516}
e7d0bb47 517.EE
47297adb 518.SH SEE ALSO
c2dd39b4 519.BR stat (2),
b7924f02 520.BR fts (3),
0a4f8b7b 521.BR readdir (3)