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