1FTW(3P)                    POSIX Programmer's Manual                   FTW(3P)
2
3
4

PROLOG

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

NAME

12       ftw - traverse (walk) a file tree
13

SYNOPSIS

15       #include <ftw.h>
16
17       int ftw(const char *path, int (*fn)(const char *,
18              const struct stat *ptr, int flag), int ndirs);
19
20

DESCRIPTION

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,  and  an  integer.
27       Possible values of the integer, defined in the <ftw.h> header, are:
28
29       FTW_D  For a directory.
30
31       FTW_DNR
32              For a directory that cannot be read.
33
34       FTW_F  For a 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() could
39              not successfully be executed. If the object is a  symbolic  link
40              and stat() failed, it is unspecified whether ftw() passes FTW_SL
41              or FTW_NS to the user-supplied function.
42
43
44       If the integer is FTW_DNR, descendants of that directory shall  not  be
45       processed.  If the integer is FTW_NS, the stat structure contains unde‐
46       fined values. An example of an object that would  cause  FTW_NS  to  be
47       passed  to the function pointed to by fn would be a file in a directory
48       with read but without execute (search) permission.
49
50       The ftw() function shall visit a directory before visiting any  of  its
51       descendants.
52
53       The ftw() function shall use at most one file descriptor for each level
54       in the tree.
55
56       The argument ndirs should be in the range [1, {OPEN_MAX}].
57
58       The tree traversal shall continue until either the tree  is  exhausted,
59       an invocation of fn returns a non-zero value, or some error, other than
60       [EACCES], is detected within ftw().
61
62       The ndirs argument  shall  specify  the  maximum  number  of  directory
63       streams  or  file  descriptors or both available for use by ftw() while
64       traversing the tree. When ftw() returns it shall  close  any  directory
65       streams  and  file  descriptors  it uses not counting any opened by the
66       application-supplied fn function.
67
68       The results are unspecified if  the  application-supplied  fn  function
69       does not preserve the current working directory.
70
71       The  ftw()  function  need  not  be  reentrant.  A function that is not
72       required to be reentrant is not required to be thread-safe.
73

RETURN VALUE

75       If the tree is exhausted, ftw() shall return 0. If the function pointed
76       to  by fn returns a non-zero value, ftw() shall stop its tree traversal
77       and return whatever value was returned by the function  pointed  to  by
78       fn.  If  ftw()  detects  an  error, it shall return -1 and set errno to
79       indicate the error.
80
81       If ftw() encounters an error  other  than  [EACCES]  (see  FTW_DNR  and
82       FTW_NS  above), it shall return -1 and set errno to indicate the error.
83       The external variable errno may contain any error value that is  possi‐
84       ble  when  a  directory  is opened or when one of the stat functions is
85       executed on a directory or file.
86

ERRORS

88       The ftw() function shall fail if:
89
90       EACCES Search permission is denied for any component of  path  or  read
91              permission is denied for path.
92
93       ELOOP  A loop exists in symbolic links encountered during resolution of
94              the path argument.
95
96       ENAMETOOLONG
97              The length of the path argument exceeds {PATH_MAX} or a pathname
98              component is longer than {NAME_MAX}.
99
100       ENOENT A component of path does not name an existing file or path is an
101              empty string.
102
103       ENOTDIR
104              A component of path is not a directory.
105
106       EOVERFLOW
107              A field in the stat structure cannot be represented correctly in
108              the  current programming environment for one or more files found
109              in the file hierarchy.
110
111
112       The ftw() function may fail if:
113
114       EINVAL The value of the ndirs argument is invalid.
115
116       ELOOP  More than {SYMLOOP_MAX} symbolic links were  encountered  during
117              resolution of the path argument.
118
119       ENAMETOOLONG
120              Pathname  resolution of a symbolic link produced an intermediate
121              result whose length exceeds {PATH_MAX}.
122
123
124       In addition, if the function pointed to by fn encounters system errors,
125       errno may be set accordingly.
126
127       The following sections are informative.
128

EXAMPLES

130   Walking a Directory Structure
131       The  following  example  walks the current directory structure, calling
132       the fn function for every  directory  entry,  using  at  most  10  file
133       descriptors:
134
135
136              #include <ftw.h>
137              ...
138              if (ftw(".", fn, 10) != 0) {
139                  perror("ftw"); exit(2);
140              }
141

APPLICATION USAGE

143       The  ftw()  function may allocate dynamic storage during its operation.
144       If ftw() is forcibly terminated, such as by longjmp()  or  siglongjmp()
145       being  executed  by  the function pointed to by fn or an interrupt rou‐
146       tine, ftw() does not have a chance to free that storage, so it  remains
147       permanently  allocated. A safe way to handle interrupts is to store the
148       fact that an interrupt has occurred, and arrange to have  the  function
149       pointed to by fn return a non-zero value at its next invocation.
150

RATIONALE

152       None.
153

FUTURE DIRECTIONS

155       None.
156

SEE ALSO

158       longjmp(),  lstat(), malloc(), nftw(), opendir(), siglongjmp(), stat(),
159       the  Base  Definitions   volume   of   IEEE Std 1003.1-2001,   <ftw.h>,
160       <sys/stat.h>
161
163       Portions  of  this text are reprinted and reproduced in electronic form
164       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
165       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
166       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
167       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
168       event of any discrepancy between this version and the original IEEE and
169       The  Open Group Standard, the original IEEE and The Open Group Standard
170       is the referee document. The original Standard can be  obtained  online
171       at http://www.opengroup.org/unix/online.html .
172
173
174
175IEEE/The Open Group                  2003                              FTW(3P)
Impressum