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

DESCRIPTION

21       The ftw() function shall recursively descend  the  directory  hierarchy
22       rooted in path.  For each object in the hierarchy, ftw() shall call the
23       function pointed to by fn, passing it a pointer  to  a  null-terminated
24       character string containing the name of the object, a pointer to a stat
25       structure containing information about the  object,  filled  in  as  if
26       stat() or lstat() had been called to retrieve the information. Possible
27       values of the integer, defined in the <ftw.h> header, are:
28
29       FTW_D     For a directory.
30
31       FTW_DNR   For a directory that cannot be read.
32
33       FTW_F     For a non-directory file.
34
35       FTW_SL    For a symbolic link (but see also FTW_NS below).
36
37       FTW_NS    For an object other than a  symbolic  link  on  which  stat()
38                 could  not  successfully be executed. If the object is a sym‐
39                 bolic link and stat() failed, it is unspecified whether ftw()
40                 passes FTW_SL or FTW_NS to the user-supplied function.
41
42       If  the  integer is FTW_DNR, descendants of that directory shall not be
43       processed. If the integer is FTW_NS, the stat structure contains  unde‐
44       fined  values.  An  example  of an object that would cause FTW_NS to be
45       passed to the function pointed to by fn would be a file in a  directory
46       with read but without execute (search) permission.
47
48       The  ftw()  function shall visit a directory before visiting any of its
49       descendants.
50
51       The ftw() function shall use at most one file descriptor for each level
52       in the tree.
53
54       The argument ndirs should be in the range [1,{OPEN_MAX}].
55
56       The  tree  traversal shall continue until either the tree is exhausted,
57       an invocation of fn returns a non-zero value, or some error, other than
58       [EACCES], is detected within ftw().
59
60       The  ndirs  argument  shall  specify  the  maximum  number of directory
61       streams or file descriptors or both available for use  by  ftw()  while
62       traversing  the  tree.  When ftw() returns it shall close any directory
63       streams and file descriptors it uses not counting  any  opened  by  the
64       application-supplied fn function.
65
66       The  results  are  unspecified  if the application-supplied fn function
67       does not preserve the current working directory.
68
69       The ftw() function need not be thread-safe.
70

RETURN VALUE

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

ERRORS

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

EXAMPLES

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

APPLICATION USAGE

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

RATIONALE

152       None.
153

FUTURE DIRECTIONS

155       The ftw() function may be removed in a future version.
156

SEE ALSO

158       fdopendir(), fstatat(), longjmp(), nftw(), siglongjmp()
159
160       The Base Definitions volume of POSIX.1‐2017, <ftw.h>, <sys_stat.h>
161
163       Portions of this text are reprinted and reproduced in  electronic  form
164       from  IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
165       table Operating System Interface (POSIX), The Open Group Base  Specifi‐
166       cations  Issue  7, 2018 Edition, Copyright (C) 2018 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       Any typographical or formatting errors that appear  in  this  page  are
174       most likely to have been introduced during the conversion of the source
175       files to man page format. To report such errors,  see  https://www.ker
176       nel.org/doc/man-pages/reporting_bugs.html .
177
178
179
180IEEE/The Open Group                  2017                              FTW(3P)
Impressum