1FTW(3)                     Linux Programmer's Manual                    FTW(3)
2
3
4

NAME

6       ftw, nftw - file tree walk
7

SYNOPSIS

9       #include <ftw.h>
10
11       int ftw(const char *dirpath,
12               int (*fn) (const char *fpath, const struct stat *sb,
13                          int typeflag),
14               int nopenfd);
15
16       #define _XOPEN_SOURCE 500
17       #include <ftw.h>
18
19       int nftw(const char *dirpath,
20               int (*fn) (const char *fpath, const struct stat *sb,
21                          int typeflag, struct FTW *ftwbuf),
22               int nopenfd, int flags);
23

DESCRIPTION

25       ftw() walks through the directory tree that is located under the direc‐
26       tory dirpath, and calls fn() once for  each  entry  in  the  tree.   By
27       default,  directories  are  handled before the files and subdirectories
28       they contain (preorder traversal).
29
30       To avoid using up  all  of  the  calling  process's  file  descriptors,
31       nopenfd  specifies  the  maximum  number of directories that ftw() will
32       hold open simultaneously.  When the search depth  exceeds  this,  ftw()
33       will  become slower because directories have to be closed and reopened.
34       ftw() uses at most one file descriptor for each level in the  directory
35       tree.
36
37       For  each  entry  found  in the tree, ftw() calls fn() with three argu‐
38       ments: fpath, sb, and typeflag.  fpath is the pathname  of  the  entry,
39       and is expressed either as a pathname relative to the calling process's
40       current working directory at the time of the call to ftw(), if  dirpath
41       was  expressed  as  a relative pathname, or as an absolute pathname, if
42       dirpath was expressed as an absolute pathname.  sb is a pointer to  the
43       stat structure returned by a call to stat(2) for fpath.  typeflag is an
44       integer that has one of the following values:
45
46       FTW_F  fpath is a regular file.
47
48       FTW_D  fpath is a directory.
49
50       FTW_DNR
51              fpath is a directory which can't be read.
52
53       FTW_NS The stat(2) call failed on fpath, which is not a symbolic link.
54
55              If fpath is a symbolic link  and  stat(2)  failed,  POSIX.1-2001
56              states that it is undefined whether FTW_NS or FTW_SL (see below)
57              is passed in typeflag.
58
59       To stop the tree walk, fn() returns a nonzero value;  this  value  will
60       become  the  return  value  of ftw().  As long as fn() returns 0, ftw()
61       will continue either until it has traversed the entire tree,  in  which
62       case  it  will  return zero, or until it encounters an error (such as a
63       malloc(3) failure), in which case it will return -1.
64
65       Because ftw() uses dynamic data structures, the only safe way  to  exit
66       out  of a tree walk is to return a nonzero value from fn().  To allow a
67       signal to terminate the walk without causing a memory  leak,  have  the
68       handler  set  a  global  flag  that  is  checked  by  fn().   Don't use
69       longjmp(3) unless the program is going to terminate.
70
71   nftw()
72       The function nftw() is the same as ftw(), except that it has one  addi‐
73       tional argument, flags, and calls fn() with one more argument, ftwbuf.
74
75       This  flags  argument  is formed by ORing zero or more of the following
76       flags:
77
78       FTW_ACTIONRETVAL (since glibc 2.3.3)
79              If this glibc-specific flag is  set,  then  nftw()  handles  the
80              return  value  from fn() differently.  fn() should return one of
81              the following values:
82
83              FTW_CONTINUE
84                     Instructs nftw() to continue normally.
85
86              FTW_SKIP_SIBLINGS
87                     If fn() returns this value, then siblings of the  current
88                     entry  will  be  skipped, and processing continues in the
89                     parent.
90
91              FTW_SKIP_SUBTREE
92                     If fn() is called with  an  entry  that  is  a  directory
93                     (typeflag  is  FTW_D),  this  return  value  will prevent
94                     objects within that directory from being passed as  argu‐
95                     ments to fn().  nftw() continues processing with the next
96                     sibling of the directory.
97
98              FTW_STOP
99                     Causes nftw() to return immediately with the return value
100                     FTW_STOP.
101
102              Other  return values could be associated with new actions in the
103              future; fn() should not return values other  than  those  listed
104              above.
105
106              The  feature  test macro _GNU_SOURCE must be defined in order to
107              obtain the definition of FTW_ACTIONRETVAL from <ftw.h>.
108
109       FTW_CHDIR
110              If set, do a chdir(2) to each directory before handling its con‐
111              tents.   This  is  useful  if  the program needs to perform some
112              action in the directory in which fpath resides.
113
114       FTW_DEPTH
115              If set, do a post-order traversal, that is, call  fn()  for  the
116              directory  itself  after  handling the contents of the directory
117              and its subdirectories.  (By default, each directory is  handled
118              before its contents.)
119
120       FTW_MOUNT
121              If  set,  stay  within  the same file system (i.e., do not cross
122              mount points).
123
124       FTW_PHYS
125              If set, do not follow symbolic links.  (This is what you  want.)
126              If not set, symbolic links are followed, but no file is reported
127              twice.
128
129              If FTW_PHYS is not set, but FTW_DEPTH is set, then the  function
130              fn()  is never called for a directory that would be a descendant
131              of itself.
132
133       For each entry in the directory tree, nftw() calls fn() with four argu‐
134       ments.  fpath and sb are as for ftw().  typeflag may receive any of the
135       same values as with ftw(), or any of the following values:
136
137       FTW_DP fpath is a directory, and FTW_DEPTH was specified in flags.  All
138              of  the  files  and  subdirectories  within fpath have been pro‐
139              cessed.
140
141       FTW_SL fpath is a symbolic link, and FTW_PHYS was set in flags.
142
143       FTW_SLN
144              fpath is a symbolic link pointing to a nonexistent file.   (This
145              occurs only if FTW_PHYS is not set.)
146
147       The  fourth argument that nftw() supplies when calling fn() is a struc‐
148       ture of type FTW:
149
150           struct FTW {
151               int base;
152               int level;
153           };
154
155       base is the offset of the filename (i.e., basename  component)  in  the
156       pathname  given in fpath.  level is the depth of fpath in the directory
157       tree, relative to the root of the tree (dirpath, which has depth 0).
158

RETURN VALUE

160       These functions return 0 on success, and -1 if an error occurs.
161
162       If fn() returns nonzero, then the tree walk is terminated and the value
163       returned by fn() is returned as the result of ftw() or nftw().
164
165       If  nftw()  is  called  with  the  FTW_ACTIONRETVAL flag, then the only
166       nonzero value that should be used by fn() to terminate the tree walk is
167       FTW_STOP, and that value is returned as the result of nftw().
168

CONFORMING TO

170       POSIX.1-2001, SVr4, SUSv1.  POSIX.1-2008 marks ftw() as obsolete.
171

NOTES

173       POSIX.1-2001  note that the results are unspecified if fn does not pre‐
174       serve the current working directory.
175
176       The function nftw() and the use of FTW_SL with ftw() were introduced in
177       SUSv1.
178
179       On  some  systems  ftw() will never use FTW_SL, on other systems FTW_SL
180       occurs only for symbolic links that do not point to an  existing  file,
181       and  again  on  other  systems  ftw() will use FTW_SL for each symbolic
182       link.  For predictable control, use nftw().
183
184       Under Linux, libc4 and libc5 and glibc 2.0.6 will  use  FTW_F  for  all
185       objects  (files,  symbolic links, FIFOs, etc.)  that can be stat'ed but
186       are not a directory.
187
188       The function nftw() is available since glibc 2.1.
189
190       FTW_ACTIONRETVAL is glibc-specific.
191

EXAMPLE

193       The following program traverses the directory tree under the path named
194       in  its  first command-line argument, or under the current directory if
195       no argument is supplied.  It displays various  information  about  each
196       file.   The second command-line argument can be used to specify charac‐
197       ters that control the value assigned to the flags argument when calling
198       nftw().
199
200       #define _XOPEN_SOURCE 500
201       #include <ftw.h>
202       #include <stdio.h>
203       #include <stdlib.h>
204       #include <string.h>
205       #include <stdint.h>
206
207       static int
208       display_info(const char *fpath, const struct stat *sb,
209                    int tflag, struct FTW *ftwbuf)
210       {
211           printf("%-3s %2d %7jd   %-40s %d %s\n",
212               (tflag == FTW_D) ?   "d"   : (tflag == FTW_DNR) ? "dnr" :
213               (tflag == FTW_DP) ?  "dp"  : (tflag == FTW_F) ?   "f" :
214               (tflag == FTW_NS) ?  "ns"  : (tflag == FTW_SL) ?  "sl" :
215               (tflag == FTW_SLN) ? "sln" : "???",
216               ftwbuf->level, (intmax_t) sb->st_size,
217               fpath, ftwbuf->base, fpath + ftwbuf->base);
218           return 0;           /* To tell nftw() to continue */
219       }
220
221       int
222       main(int argc, char *argv[])
223       {
224           int flags = 0;
225
226           if (argc > 2 && strchr(argv[2], 'd') != NULL)
227               flags |= FTW_DEPTH;
228           if (argc > 2 && strchr(argv[2], 'p') != NULL)
229               flags |= FTW_PHYS;
230
231           if (nftw((argc < 2) ? "." : argv[1], display_info, 20, flags)
232                   == -1) {
233               perror("nftw");
234               exit(EXIT_FAILURE);
235           }
236           exit(EXIT_SUCCESS);
237       }
238

SEE ALSO

240       stat(2), fts(3), readdir(3), feature_test_macros(7)
241

COLOPHON

243       This  page  is  part of release 3.25 of the Linux man-pages project.  A
244       description of the project, and information about reporting  bugs,  can
245       be found at http://www.kernel.org/doc/man-pages/.
246
247
248
249Linux                             2010-06-10                            FTW(3)
Impressum