1FTW(3P) POSIX Programmer's Manual FTW(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 ftw — traverse (walk) a file tree
14
16 #include <ftw.h>
17
18 int ftw(const char *path, int (*fn)(const char *,
19 const struct stat *ptr, int flag), int ndirs);
20
22 The ftw() function shall recursively descend the directory hierarchy
23 rooted in path. For each object in the hierarchy, ftw() shall call the
24 function pointed to by fn, passing it a pointer to a null-terminated
25 character string containing the name of the object, a pointer to a stat
26 structure containing information about the object, filled in as if
27 stat() or lstat() had been called to retrieve the information. Possible
28 values of the integer, defined in the <ftw.h> header, are:
29
30 FTW_D For a directory.
31
32 FTW_DNR For a directory that cannot be read.
33
34 FTW_F For a non-directory file.
35
36 FTW_SL For a symbolic link (but see also FTW_NS below).
37
38 FTW_NS For an object other than a symbolic link on which stat()
39 could not successfully be executed. If the object is a sym‐
40 bolic link and stat() failed, it is unspecified whether ftw()
41 passes FTW_SL or FTW_NS to the user-supplied function.
42
43 If the integer is FTW_DNR, descendants of that directory shall not be
44 processed. If the integer is FTW_NS, the stat structure contains unde‐
45 fined values. An example of an object that would cause FTW_NS to be
46 passed to the function pointed to by fn would be a file in a directory
47 with read but without execute (search) permission.
48
49 The ftw() function shall visit a directory before visiting any of its
50 descendants.
51
52 The ftw() function shall use at most one file descriptor for each level
53 in the tree.
54
55 The argument ndirs should be in the range [1,{OPEN_MAX}].
56
57 The tree traversal shall continue until either the tree is exhausted,
58 an invocation of fn returns a non-zero value, or some error, other than
59 [EACCES], is detected within ftw().
60
61 The ndirs argument shall specify the maximum number of directory
62 streams or file descriptors or both available for use by ftw() while
63 traversing the tree. When ftw() returns it shall close any directory
64 streams and file descriptors it uses not counting any opened by the
65 application-supplied fn function.
66
67 The results are unspecified if the application-supplied fn function
68 does not preserve the current working directory.
69
70 The ftw() function need not be thread-safe.
71
73 If the tree is exhausted, ftw() shall return 0. If the function pointed
74 to by fn returns a non-zero value, ftw() shall stop its tree traversal
75 and return whatever value was returned by the function pointed to by
76 fn. If ftw() detects an error, it shall return −1 and set errno to
77 indicate the error.
78
79 If ftw() encounters an error other than [EACCES] (see FTW_DNR and
80 FTW_NS above), it shall return −1 and set errno to indicate the error.
81 The external variable errno may contain any error value that is possi‐
82 ble when a directory is opened or when one of the stat functions is
83 executed on a directory or file.
84
86 The ftw() function shall fail if:
87
88 EACCES Search permission is denied for any component of path or read
89 permission is denied for path.
90
91 ELOOP A loop exists in symbolic links encountered during resolution of
92 the path argument.
93
94 ENAMETOOLONG
95 The length of a component of a pathname is longer than
96 {NAME_MAX}.
97
98 ENOENT A component of path does not name an existing file or path is an
99 empty string.
100
101 ENOTDIR
102 A component of path names an existing file that is neither a
103 directory nor a symbolic link to a directory.
104
105 EOVERFLOW
106 A field in the stat structure cannot be represented correctly in
107 the current programming environment for one or more files found
108 in the file hierarchy.
109
110 The ftw() function may fail if:
111
112 EINVAL The value of the ndirs argument is invalid.
113
114 ELOOP More than {SYMLOOP_MAX} symbolic links were encountered during
115 resolution of the path argument.
116
117 ENAMETOOLONG
118 The length of a pathname exceeds {PATH_MAX}, or pathname resolu‐
119 tion of a symbolic link produced an intermediate result with a
120 length that exceeds {PATH_MAX}.
121
122 In addition, if the function pointed to by fn encounters system errors,
123 errno may be set accordingly.
124
125 The following sections are informative.
126
128 Walking a Directory Structure
129 The following example walks the current directory structure, calling
130 the fn function for every directory entry, using at most 10 file
131 descriptors:
132
133 #include <ftw.h>
134 ...
135 if (ftw(".", fn, 10) != 0) {
136 perror("ftw"); exit(2);
137 }
138
140 The ftw() function may allocate dynamic storage during its operation.
141 If ftw() is forcibly terminated, such as by longjmp() or siglongjmp()
142 being executed by the function pointed to by fn or an interrupt rou‐
143 tine, ftw() does not have a chance to free that storage, so it remains
144 permanently allocated. A safe way to handle interrupts is to store the
145 fact that an interrupt has occurred, and arrange to have the function
146 pointed to by fn return a non-zero value at its next invocation.
147
148 Applications should use the nftw() function instead of the obsolescent
149 ftw() function.
150
152 None.
153
155 The ftw() function may be removed in a future version.
156
158 fdopendir(), fstatat(), longjmp(), nftw(), siglongjmp()
159
160 The Base Definitions volume of POSIX.1‐2008, <ftw.h>, <sys_stat.h>
161
163 Portions of this text are reprinted and reproduced in electronic form
164 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
165 -- Portable Operating System Interface (POSIX), The Open Group Base
166 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
167 cal and Electronics Engineers, Inc and The Open Group. (This is
168 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
169 event of any discrepancy between this version and the original IEEE and
170 The Open Group Standard, the original IEEE and The Open Group Standard
171 is the referee document. The original Standard can be obtained online
172 at http://www.unix.org/online.html .
173
174 Any typographical or formatting errors that appear in this page are
175 most likely to have been introduced during the conversion of the source
176 files to man page format. To report such errors, see https://www.ker‐
177 nel.org/doc/man-pages/reporting_bugs.html .
178
179
180
181IEEE/The Open Group 2013 FTW(3P)