1FTS(3) BSD Library Functions Manual FTS(3)
2
4 fts, fts_open, fts_read, fts_children, fts_set, fts_close — traverse a
5 file hierarchy
6
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fts.h>
11
12 FTS *
13 fts_open(char * const *path_argv, int options,
14 int (*compar)(const FTSENT **, const FTSENT **));
15
16 FTSENT *
17 fts_read(FTS *ftsp);
18
19 FTSENT *
20 fts_children(FTS *ftsp, int options);
21
22 int
23 fts_set(FTS *ftsp, FTSENT *f, int options);
24
25 int
26 fts_close(FTS *ftsp);
27
29 The fts functions are provided for traversing UNIX file hierarchies. A
30 simple overview is that the fts_open() function returns a ``handle'' on a
31 file hierarchy, which is then supplied to the other fts functions. The
32 function fts_read() returns a pointer to a structure describing one of
33 the files in the file hierarchy. The function fts_children() returns a
34 pointer to a linked list of structures, each of which describes one of
35 the files contained in a directory in the hierarchy. In general, direc‐
36 tories are visited two distinguishable times; in pre-order (before any of
37 their descendants are visited) and in post-order (after all of their
38 descendants have been visited). Files are visited once. It is possible
39 to walk the hierarchy ``logically'' (ignoring symbolic links) or physi‐
40 cally (visiting symbolic links), order the walk of the hierarchy or prune
41 and/or re-visit portions of the hierarchy.
42
43 Two structures are defined (and typedef'd) in the include file ⟨fts.h⟩.
44 The first is FTS, the structure that represents the file hierarchy
45 itself. The second is FTSENT, the structure that represents a file in
46 the file hierarchy. Normally, an FTSENT structure is returned for every
47 file in the file hierarchy. In this manual page, ``file'' and “FTSENT
48 structure” are generally interchangeable. The FTSENT structure contains
49 at least the following fields, which are described in greater detail
50 below:
51
52 typedef struct _ftsent {
53 u_short fts_info; /* flags for FTSENT structure */
54 char *fts_accpath; /* access path */
55 char *fts_path; /* root path */
56 short fts_pathlen; /* strlen(fts_path) */
57 char *fts_name; /* filename */
58 short fts_namelen; /* strlen(fts_name) */
59 short fts_level; /* depth (-1 to N) */
60 int fts_errno; /* file errno */
61 long fts_number; /* local numeric value */
62 void *fts_pointer; /* local address value */
63 struct ftsent *fts_parent; /* parent directory */
64 struct ftsent *fts_link; /* next file structure */
65 struct ftsent *fts_cycle; /* cycle structure */
66 struct stat *fts_statp; /* stat(2) information */
67 } FTSENT;
68
69 These fields are defined as follows:
70
71 fts_info One of the following flags describing the returned FTSENT
72 structure and the file it represents. With the exception of
73 directories without errors (FTS_D), all of these entries are
74 terminal, that is, they will not be revisited, nor will any
75 of their descendants be visited.
76
77 FTS_D A directory being visited in pre-order.
78
79 FTS_DC A directory that causes a cycle in the tree.
80 (The fts_cycle field of the FTSENT structure
81 will be filled in as well.)
82
83 FTS_DEFAULT Any FTSENT structure that represents a file
84 type not explicitly described by one of the
85 other fts_info values.
86
87 FTS_DNR A directory which cannot be read. This is an
88 error return, and the fts_errno field will be
89 set to indicate what caused the error.
90
91 FTS_DOT A file named ‘.’ or ‘..’ which was not speci‐
92 fied as a filename to fts_open() (see
93 FTS_SEEDOT).
94
95 FTS_DP A directory being visited in post-order. The
96 contents of the FTSENT structure will be
97 unchanged from when it was returned in pre-
98 order, i.e. with the fts_info field set to
99 FTS_D.
100
101 FTS_ERR This is an error return, and the fts_errno
102 field will be set to indicate what caused the
103 error.
104
105 FTS_F A regular file.
106
107 FTS_NS A file for which no stat(2) information was
108 available. The contents of the fts_statp field
109 are undefined. This is an error return, and
110 the fts_errno field will be set to indicate
111 what caused the error.
112
113 FTS_NSOK A file for which no stat(2) information was
114 requested. The contents of the fts_statp field
115 are undefined.
116
117 FTS_SL A symbolic link.
118
119 FTS_SLNONE A symbolic link with a non-existent target.
120 The contents of the fts_statp field reference
121 the file characteristic information for the
122 symbolic link itself.
123
124 fts_accpath A path for accessing the file from the current directory.
125
126 fts_path The path for the file relative to the root of the traversal.
127 This path contains the path specified to fts_open() as a
128 prefix.
129
130 fts_pathlen The length of the string referenced by fts_path.
131
132 fts_name The name of the file.
133
134 fts_namelen The length of the string referenced by fts_name.
135
136 fts_level The depth of the traversal, numbered from -1 to N, where
137 this file was found. The FTSENT structure representing the
138 parent of the starting point (or root) of the traversal is
139 numbered -1, and the FTSENT structure for the root itself is
140 numbered 0.
141
142 fts_errno Upon return of a FTSENT structure from the fts_children() or
143 fts_read() functions, with its fts_info field set to
144 FTS_DNR, FTS_ERR or FTS_NS, the fts_errno field contains the
145 value of the external variable errno specifying the cause of
146 the error. Otherwise, the contents of the fts_errno field
147 are undefined.
148
149 fts_number This field is provided for the use of the application pro‐
150 gram and is not modified by the fts functions. It is ini‐
151 tialized to 0.
152
153 fts_pointer This field is provided for the use of the application pro‐
154 gram and is not modified by the fts functions. It is ini‐
155 tialized to NULL.
156
157 fts_parent A pointer to the FTSENT structure referencing the file in
158 the hierarchy immediately above the current file, i.e. the
159 directory of which this file is a member. A parent struc‐
160 ture for the initial entry point is provided as well, how‐
161 ever, only the fts_level, fts_number and fts_pointer fields
162 are guaranteed to be initialized.
163
164 fts_link Upon return from the fts_children() function, the fts_link
165 field points to the next structure in the NULL-terminated
166 linked list of directory members. Otherwise, the contents
167 of the fts_link field are undefined.
168
169 fts_cycle If a directory causes a cycle in the hierarchy (see FTS_DC),
170 either because of a hard link between two directories, or a
171 symbolic link pointing to a directory, the fts_cycle field
172 of the structure will point to the FTSENT structure in the
173 hierarchy that references the same file as the current
174 FTSENT structure. Otherwise, the contents of the fts_cycle
175 field are undefined.
176
177 fts_statp A pointer to stat(2) information for the file.
178
179 A single buffer is used for all of the paths of all of the files in the
180 file hierarchy. Therefore, the fts_path and fts_accpath fields are guar‐
181 anteed to be NULL-terminated only for the file most recently returned by
182 fts_read(). To use these fields to reference any files represented by
183 other FTSENT structures will require that the path buffer be modified
184 using the information contained in that FTSENT structure's fts_pathlen
185 field. Any such modifications should be undone before further calls to
186 fts_read() are attempted. The fts_name field is always NULL-terminated.
187
189 The fts_open() function takes a pointer to an array of character pointers
190 naming one or more paths which make up a logical file hierarchy to be
191 traversed. The array must be terminated by a NULL pointer.
192
193 There are a number of options, at least one of which (either FTS_LOGICAL
194 or FTS_PHYSICAL) must be specified. The options are selected by or'ing
195 the following values:
196
197 FTS_COMFOLLOW
198 This option causes any symbolic link specified as a root
199 path to be followed immediately whether or not FTS_LOGICAL
200 is also specified.
201
202 FTS_LOGICAL This option causes the fts routines to return FTSENT struc‐
203 tures for the targets of symbolic links instead of the sym‐
204 bolic links themselves. If this option is set, the only
205 symbolic links for which FTSENT structures are returned to
206 the application are those referencing non-existent files.
207 Either FTS_LOGICAL or FTS_PHYSICAL must be provided to the
208 fts_open() function.
209
210 FTS_NOCHDIR As a performance optimization, the fts functions change
211 directories as they walk the file hierarchy. This has the
212 side-effect that an application cannot rely on being in any
213 particular directory during the traversal. The FTS_NOCHDIR
214 option turns off this optimization, and the fts functions
215 will not change the current directory. Note that applica‐
216 tions should not themselves change their current directory
217 and try to access files unless FTS_NOCHDIR is specified and
218 absolute pathnames were provided as arguments to
219 fts_open().
220
221 FTS_NOSTAT By default, returned FTSENT structures reference file char‐
222 acteristic information (the statp field) for each file vis‐
223 ited. This option relaxes that requirement as a perfor‐
224 mance optimization, allowing the fts functions to set the
225 fts_info field to FTS_NSOK and leave the contents of the
226 statp field undefined.
227
228 FTS_PHYSICAL This option causes the fts routines to return FTSENT struc‐
229 tures for symbolic links themselves instead of the target
230 files they point to. If this option is set, FTSENT struc‐
231 tures for all symbolic links in the hierarchy are returned
232 to the application. Either FTS_LOGICAL or FTS_PHYSICAL
233 must be provided to the fts_open() function.
234
235 FTS_SEEDOT By default, unless they are specified as path arguments to
236 fts_open(), any files named ‘.’ or ‘..’ encountered in the
237 file hierarchy are ignored. This option causes the fts
238 routines to return FTSENT structures for them.
239
240 FTS_XDEV This option prevents fts from descending into directories
241 that have a different device number than the file from
242 which the descent began.
243
244 The argument compar() specifies a user-defined function which may be used
245 to order the traversal of the hierarchy. It takes two pointers to point‐
246 ers to FTSENT structures as arguments and should return a negative value,
247 zero, or a positive value to indicate if the file referenced by its first
248 argument comes before, in any order with respect to, or after, the file
249 referenced by its second argument. The fts_accpath, fts_path and
250 fts_pathlen fields of the FTSENT structures may never be used in this
251 comparison. If the fts_info field is set to FTS_NS or FTS_NSOK, the
252 fts_statp field may not either. If the compar() argument is NULL, the
253 directory traversal order is in the order listed in path_argv for the
254 root paths, and in the order listed in the directory for everything else.
255
257 The fts_read() function returns a pointer to an FTSENT structure describ‐
258 ing a file in the hierarchy. Directories (that are readable and do not
259 cause cycles) are visited at least twice, once in pre-order and once in
260 post-order. All other files are visited at least once. (Hard links
261 between directories that do not cause cycles or symbolic links to sym‐
262 bolic links may cause files to be visited more than once, or directories
263 more than twice.)
264
265 If all the members of the hierarchy have been returned, fts_read()
266 returns NULL and sets the external variable errno to 0. If an error
267 unrelated to a file in the hierarchy occurs, fts_read() returns NULL and
268 sets errno appropriately. If an error related to a returned file occurs,
269 a pointer to an FTSENT structure is returned, and errno may or may not
270 have been set (see fts_info).
271
272 The FTSENT structures returned by fts_read() may be overwritten after a
273 call to fts_close() on the same file hierarchy stream, or, after a call
274 to fts_read() on the same file hierarchy stream unless they represent a
275 file of type directory, in which case they will not be overwritten until
276 after a call to fts_read() after the FTSENT structure has been returned
277 by the function fts_read() in post-order.
278
280 The fts_children() function returns a pointer to an FTSENT structure
281 describing the first entry in a NULL-terminated linked list of the files
282 in the directory represented by the FTSENT structure most recently
283 returned by fts_read(). The list is linked through the fts_link field of
284 the FTSENT structure, and is ordered by the user-specified comparison
285 function, if any. Repeated calls to fts_children() will recreate this
286 linked list.
287
288 As a special case, if fts_read() has not yet been called for a hierarchy,
289 fts_children() will return a pointer to the files in the logical direc‐
290 tory specified to fts_open(), i.e. the arguments specified to fts_open().
291 Otherwise, if the FTSENT structure most recently returned by fts_read()
292 is not a directory being visited in pre-order, or the directory does not
293 contain any files, fts_children() returns NULL and sets errno to zero.
294 If an error occurs, fts_children() returns NULL and sets errno appropri‐
295 ately.
296
297 The FTSENT structures returned by fts_children() may be overwritten after
298 a call to fts_children(), fts_close() or fts_read() on the same file
299 hierarchy stream.
300
301 Option may be set to the following value:
302
303 FTS_NAMEONLY Only the names of the files are needed. The contents of
304 all the fields in the returned linked list of structures
305 are undefined with the exception of the fts_name and
306 fts_namelen fields.
307
309 The function fts_set() allows the user application to determine further
310 processing for the file f of the stream ftsp. The fts_set() function
311 returns 0 on success, and -1 if an error occurs. Option must be set to
312 one of the following values:
313
314 FTS_AGAIN Re-visit the file; any file type may be re-visited. The
315 next call to fts_read() will return the referenced file.
316 The fts_stat and fts_info fields of the structure will be
317 reinitialized at that time, but no other fields will have
318 been changed. This option is meaningful only for the most
319 recently returned file from fts_read(). Normal use is for
320 post-order directory visits, where it causes the directory
321 to be re-visited (in both pre and post-order) as well as
322 all of its descendants.
323
324 FTS_FOLLOW The referenced file must be a symbolic link. If the refer‐
325 enced file is the one most recently returned by fts_read(),
326 the next call to fts_read() returns the file with the
327 fts_info and fts_statp fields reinitialized to reflect the
328 target of the symbolic link instead of the symbolic link
329 itself. If the file is one of those most recently returned
330 by fts_children(), the fts_info and fts_statp fields of the
331 structure, when returned by fts_read(), will reflect the
332 target of the symbolic link instead of the symbolic link
333 itself. In either case, if the target of the symbolic link
334 does not exist the fields of the returned structure will be
335 unchanged and the fts_info field will be set to FTS_SLNONE.
336
337 If the target of the link is a directory, the pre-order
338 return, followed by the return of all of its descendants,
339 followed by a post-order return, is done.
340
341 FTS_SKIP No descendants of this file are visited. The file may be
342 one of those most recently returned by either
343 fts_children() or fts_read().
344
346 The fts_close() function closes a file hierarchy stream ftsp and restores
347 the current directory to the directory from which fts_open() was called
348 to open ftsp. The fts_close() function returns 0 on success, and -1 if
349 an error occurs.
350
352 The function fts_open() may fail and set errno for any of the errors
353 specified for the library functions open(2) and malloc(3).
354
355 The function fts_close() may fail and set errno for any of the errors
356 specified for the library functions chdir(2) and close(2).
357
358 The functions fts_read() and fts_children() may fail and set errno for
359 any of the errors specified for the library functions chdir(2),
360 malloc(3), opendir(3), readdir(3) and stat(2).
361
362 In addition, fts_children(), fts_open() and fts_set() may fail and set
363 errno as follows:
364
365 [EINVAL] The options were invalid.
366
368 find(1), chdir(2), stat(2), ftw(3), qsort(3)
369
371 4.4BSD. The fts utility is expected to be included in a future IEEE Std
372 1003.1-1988 (“POSIX.1”) revision.
373
375 These functions are available in Linux since glibc2.
376
377BSD April 16, 1994 BSD