1 .\" Copyright (c) 1989, 1991, 1993, 1994
2 .\" The Regents of the University of California. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\" must display the following acknowledgement:
14 .\" This product includes software developed by the University of
15 .\" California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" @(#)fts.3 8.5 (Berkeley) 4/16/94
40 .Nd traverse a file hierarchy
42 .Fd #include <sys/types.h>
43 .Fd #include <sys/stat.h>
46 .Fn fts_open "char * const *path_argv" "int options" "int (*compar)(const FTSENT **, const FTSENT **)"
48 .Fn fts_read "FTS *ftsp"
50 .Fn fts_children "FTS *ftsp" "int options"
52 .Fn fts_set "FTS *ftsp" "FTSENT *f" "int options"
54 .Fn fts_close "FTS *ftsp"
58 functions are provided for traversing
61 A simple overview is that the
63 function returns a ``handle'' on a file hierarchy, which is then supplied to
69 returns a pointer to a structure describing one of the files in the file
73 returns a pointer to a linked list of structures, each of which describes
74 one of the files contained in a directory in the hierarchy.
75 In general, directories are visited two distinguishable times; in pre-order
76 (before any of their descendants are visited) and in post-order (after all
77 of their descendants have been visited).
78 Files are visited once.
79 It is possible to walk the hierarchy ``logically'' (ignoring symbolic links)
80 or physically (visiting symbolic links), order the walk of the hierarchy or
81 prune and/or re-visit portions of the hierarchy.
83 Two structures are defined (and typedef'd) in the include file
87 the structure that represents the file hierarchy itself.
90 the structure that represents a file in the file
94 structure is returned for every file in the file
96 In this manual page, ``file'' and
97 .Dq Fa FTSENT No structure
102 structure contains at least the following fields, which are
103 described in greater detail below:
105 typedef struct _ftsent {
106 u_short fts_info; /* flags for FTSENT structure */
107 char *fts_accpath; /* access path */
108 char *fts_path; /* root path */
109 short fts_pathlen; /* strlen(fts_path) */
110 char *fts_name; /* file name */
111 short fts_namelen; /* strlen(fts_name) */
112 short fts_level; /* depth (\-1 to N) */
113 int fts_errno; /* file errno */
114 long fts_number; /* local numeric value */
115 void *fts_pointer; /* local address value */
116 struct ftsent *fts_parent; /* parent directory */
117 struct ftsent *fts_link; /* next file structure */
118 struct ftsent *fts_cycle; /* cycle structure */
119 struct stat *fts_statp; /* stat(2) information */
123 These fields are defined as follows:
124 .Bl -tag -width "fts_namelen"
126 One of the following values describing the returned
129 the file it represents.
130 With the exception of directories without errors
133 entries are terminal, that is, they will not be revisited, nor will any
134 of their descendants be visited.
135 .Bl -tag -width FTS_DEFAULT
137 A directory being visited in pre-order.
139 A directory that causes a cycle in the tree.
144 structure will be filled in as well.)
148 structure that represents a file type not explicitly described
153 A directory which cannot be read.
154 This is an error return, and the
156 field will be set to indicate what caused the error.
162 which was not specified as a file name to
167 A directory being visited in post-order.
170 structure will be unchanged from when
171 it was returned in pre-order, i.e. with the
176 This is an error return, and the
178 field will be set to indicate what caused the error.
184 information was available.
188 This is an error return, and the
190 field will be set to indicate what caused the error.
194 information was requested.
201 A symbolic link with a non-existent target.
204 field reference the file characteristic information for the symbolic link
208 A path for accessing the file from the current directory.
210 The path for the file relative to the root of the traversal.
211 This path contains the path specified to
215 The length of the string referenced by
218 The name of the file.
220 The length of the string referenced by
223 The depth of the traversal, numbered from \-1 to N, where this file
227 structure representing the parent of the starting point (or root)
228 of the traversal is numbered \-1, and the
230 structure for the root
231 itself is numbered 0.
248 field contains the value of the external variable
250 specifying the cause of the error.
251 Otherwise, the contents of the
255 This field is provided for the use of the application program and is
259 It is initialized to 0.
261 This field is provided for the use of the application program and is
270 structure referencing the file in the hierarchy
271 immediately above the current file, i.e. the directory of which this
273 A parent structure for the initial entry point is provided as well,
279 fields are guaranteed to be initialized.
285 field points to the next structure in the NULL-terminated linked list of
287 Otherwise, the contents of the
291 If a directory causes a cycle in the hierarchy (see
294 of a hard link between two directories, or a symbolic link pointing to a
297 field of the structure will point to the
299 structure in the hierarchy that references the same file as the current
302 Otherwise, the contents of the
308 information for the file.
311 A single buffer is used for all of the paths of all of the files in the
317 fields are guaranteed to be
318 .Dv NUL Ns -terminated
320 for the file most recently returned by
322 To use these fields to reference any files represented by other
324 structures will require that the path buffer be modified using the
325 information contained in that
330 Any such modifications should be undone before further calls to
336 .Dv NUL Ns -terminated.
340 function takes a pointer to an array of character pointers naming one
341 or more paths which make up a logical file hierarchy to be traversed.
342 The array must be terminated by a
347 a number of options, at least one of which (either
352 The options are selected by
354 the following values:
355 .Bl -tag -width "FTS_PHYSICAL"
357 This option causes any symbolic link specified as a root path to be
358 followed immediately whether or not
362 This option causes the
366 structures for the targets of symbolic links
367 instead of the symbolic links themselves.
368 If this option is set, the only symbolic links for which
371 are returned to the application are those referencing non-existent files.
381 As a performance optimization, the
383 functions change directories as they walk the file hierarchy.
384 This has the side-effect that an application cannot rely on being
385 in any particular directory during the traversal.
388 option turns off this optimization, and the
390 functions will not change the current directory.
391 Note that applications should not themselves change their current directory
392 and try to access files unless
394 is specified and absolute
395 pathnames were provided as arguments to
400 structures reference file characteristic information (the
402 field) for each file visited.
403 This option relaxes that requirement as a performance optimization,
410 and leave the contents of the
414 This option causes the
418 structures for symbolic links themselves instead
419 of the target files they point to.
420 If this option is set,
422 structures for all symbolic links in the
423 hierarchy are returned to the application.
433 By default, unless they are specified as path arguments to
439 encountered in the file hierarchy are ignored.
440 This option causes the
448 from descending into directories that have a different device number
449 than the file from which the descent began.
454 specifies a user-defined function which may be used to order the traversal
457 takes two pointers to pointers to
459 structures as arguments and
460 should return a negative value, zero, or a positive value to indicate
461 if the file referenced by its first argument comes before, in any order
462 with respect to, or after, the file referenced by its second argument.
472 be used in this comparison.
481 field may not either.
486 the directory traversal order is in the order listed in
488 for the root paths, and in the order listed in the directory for
493 function returns a pointer to an
495 structure describing a file in
497 Directories (that are readable and do not cause cycles) are visited at
498 least twice, once in pre-order and once in post-order.
499 All other files are visited at least once.
500 (Hard links between directories that do not cause cycles or symbolic
501 links to symbolic links may cause files to be visited more than once,
502 or directories more than twice.)
504 If all the members of the hierarchy have been returned,
508 and sets the external variable
511 If an error unrelated to a file in the hierarchy occurs,
518 If an error related to a returned file occurs, a pointer to an
520 structure is returned, and
522 may or may not have been set (see
527 structures returned by
529 may be overwritten after a call to
531 on the same file hierarchy stream, or, after a call to
533 on the same file hierarchy stream unless they represent a file of type
534 directory, in which case they will not be overwritten until after a call to
538 structure has been returned by the function
544 function returns a pointer to an
546 structure describing the first entry in a NULL-terminated linked list of
547 the files in the directory represented by the
549 structure most recently returned by
551 The list is linked through the
555 structure, and is ordered by the user-specified comparison function, if any.
558 will recreate this linked list.
560 As a special case, if
562 has not yet been called for a hierarchy,
564 will return a pointer to the files in the logical directory specified to
566 i.e. the arguments specified to
570 structure most recently returned by
572 is not a directory being visited in pre-order,
573 or the directory does not contain any files,
590 structures returned by
592 may be overwritten after a call to
597 on the same file hierarchy stream.
600 may be set to the following value:
601 .Bl -tag -width FTS_NAMEONLY
603 Only the names of the files are needed.
604 The contents of all the fields in the returned linked list of structures
605 are undefined with the exception of the
614 allows the user application to determine further processing for the
622 returns 0 on success, and \-1 if an error occurs.
624 must be set to one of the following values:
625 .Bl -tag -width FTS_PHYSICAL
627 Re-visit the file; any file type may be re-visited.
630 will return the referenced file.
635 fields of the structure will be reinitialized at that time,
636 but no other fields will have been changed.
637 This option is meaningful only for the most recently returned
640 Normal use is for post-order directory visits, where it causes the
641 directory to be re-visited (in both pre and post-order) as well as all
644 The referenced file must be a symbolic link.
645 If the referenced file is the one most recently returned by
649 returns the file with the
653 fields reinitialized to reflect the target of the symbolic link instead
654 of the symbolic link itself.
655 If the file is one of those most recently returned by
661 fields of the structure, when returned by
663 will reflect the target of the symbolic link instead of the symbolic link
665 In either case, if the target of the symbolic link does not exist the
666 fields of the returned structure will be unchanged and the
671 If the target of the link is a directory, the pre-order return, followed
672 by the return of all of its descendants, followed by a post-order return,
675 No descendants of this file are visited.
676 The file may be one of those most recently returned by either
684 function closes a file hierarchy stream
686 and restores the current directory to the directory from which
693 returns 0 on success, and \-1 if an error occurs.
699 for any of the errors specified for the library functions
708 for any of the errors specified for the library functions
719 for any of the errors specified for the library functions
737 The options were invalid.
747 utility is expected to be included in a future