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