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   /* See feature_test_macros(7) */
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 (before
107              including any header files) in order to obtain the definition of
108              FTW_ACTIONRETVAL from <ftw.h>.
109
110       FTW_CHDIR
111              If set, do a chdir(2) to each directory before handling its con‐
112              tents.  This is useful if the  program  needs  to  perform  some
113              action in the directory in which fpath resides.
114
115       FTW_DEPTH
116              If  set,  do  a post-order traversal, that is, call fn() for the
117              directory itself after handling the contents  of  the  directory
118              and  its subdirectories.  (By default, each directory is handled
119              before its contents.)
120
121       FTW_MOUNT
122              If set, stay within the same file system  (i.e.,  do  not  cross
123              mount points).
124
125       FTW_PHYS
126              If  set, do not follow symbolic links.  (This is what you want.)
127              If not set, symbolic links are followed, but no file is reported
128              twice.
129
130              If  FTW_PHYS is not set, but FTW_DEPTH is set, then the function
131              fn() is never called for a directory that would be a  descendant
132              of itself.
133
134       For each entry in the directory tree, nftw() calls fn() with four argu‐
135       ments.  fpath and sb are as for ftw().  typeflag may receive any of the
136       same values as with ftw(), or any of the following values:
137
138       FTW_DP fpath is a directory, and FTW_DEPTH was specified in flags.  All
139              of the files and subdirectories  within  fpath  have  been  pro‐
140              cessed.
141
142       FTW_SL fpath is a symbolic link, and FTW_PHYS was set in flags.
143
144       FTW_SLN
145              fpath  is a symbolic link pointing to a nonexistent file.  (This
146              occurs only if FTW_PHYS is not set.)
147
148       The fourth argument that nftw() supplies when calling fn() is a  struc‐
149       ture of type FTW:
150
151           struct FTW {
152               int base;
153               int level;
154           };
155
156       base  is  the  offset of the filename (i.e., basename component) in the
157       pathname given in fpath.  level is the depth of fpath in the  directory
158       tree, relative to the root of the tree (dirpath, which has depth 0).
159

RETURN VALUE

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

CONFORMING TO

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

NOTES

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

EXAMPLE

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

SEE ALSO

241       stat(2), fts(3), readdir(3)
242

COLOPHON

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