.\" Copyright (c) 1989 The Regents of the University of California.
.\" %sccs.include.redist.man%
.\" @(#)fts.3 5.7 (Berkeley) %G%
fts \- traverse a file hierarchy
fts_open(path_argv, options, compar)
int options, (*compar)();
fts_set(ftsp, f, options)
functions are provided for traversing UNIX file hierarchies.
The simple overview is that the function
returns a ``handle'' on a file hierarchy, which is supplied to the other
functions to determine which hierarchy they operate on.
returns a pointer to a structure describing one of the files in the file
returns a pointer to a linked list of structures, each of which describes
one of the files contained in a directory in the hierarchy.
In general, directories are visited two distinguishable times; in pre-order
(before any of their descendants are visited) and in post-order (after all
of their descendants have been visited).
It is possible to walk the hierarchy ``logically'' (ignoring symbolic links)
or physically (visiting symbolic links), order the walk of the hierarchy or
prune and/or re-visit portions of the hierarchy.
Two structures are defined (and typedef'd) in the include file <fts.h>.
The first is FTS, the structure that represents the file hierarchy stream.
The second is FTSENT, the structure that represents a file in the file
Normally, an FTSENT structure is returned for every file in the file
In this manual page, ``file'' and ``FTSENT structure'' are generally
The FTSENT structure contains at least the following fields, which are
described in greater detail below:
.ta .5i +.5i +\w'struct ftsent *parent;\0\0\0'u
u_short fts_info; /* flags for FTSENT structure */
char *fts_accpath; /* access path */
char *fts_path; /* root path */
short fts_pathlen; /* strlen(fts_path) */
char *fts_name; /* file name */
short fts_namelen; /* strlen(fts_name) */
short fts_level; /* depth (\-1 to N) */
long fts_number /* local numeric value */
void *fts_pointer; /* local address value */
struct ftsent *fts_parent; /* parent directory */
struct ftsent *fts_link; /* cycle or next file structure */
struct stat fts_statb; /* stat(2) information */
These fields are defined as follows:
One of the following flags describing the returned FTSENT structure and
With the exception of directories without errors (FTS_D), all of these
entries are terminal, that is, they will not be revisited, nor will any
of their descendants be visited.
A directory being visited in pre-order.
A directory that causes a cycle in the tree.
field of the FTSENT structure will be filled in as well.)
Any FTSENT structure that represents a file type not explicitly described
A directory which cannot be read.
Directory readability is checked before directory searchability
A directory which cannot be searched.
Directory readability is checked before directory searchability
A file named ``.'' or ``..'' which was not specified as a file name to
A directory being visited in post-order.
The contents of the FTSENT structure will be unchanged from when
it was returned in pre-order, i.e. with the
An error return; the external variable
will be set to indicate the error.
information was available (see FTS_NOSTAT).
In this case the contents of the
A symbolic link with a non-existent target.
A path for accessing the file.
functions are changing the current working directory or not (see FTS_NOCHDIR).
The path for the file relative to the root of the traversal.
This path contains the path specified to
The length of the string referenced by
The length of the string referenced by
The depth of the traversal, numbered from \-1 to N, where this file
The FTSENT structure representing the parent of the starting point (or root)
of the traversal is numbered \-1, and the FTSENT structure for the root
This field is provided for the use of the application program and is
occupy the same physical location; using both may cause undefined results.
This field is provided for the use of the application program and is
It is initialized to NULL.
occupy the same physical location; using both may cause undefined results.
A pointer to the FTSENT structure referencing the file in the hierarchy
immediately above the current file, i.e. the directory of which this
A parent structure for the initial entry point is provided as well,
fields are guaranteed to be initialized.
field has two separate uses.
If a directory causes a cycle in the hierarchy (see FTS_DC), either because
of a hard link between two directories, or a symbolic link pointing to a
field of the structure will point to the FTSENT structure in the hierarchy
that references the same file as the current FTSENT structure.
Also, upon return from the
field points to the next structure in the linked list of directory members.
Otherwise, the contents of the
information for the file.
function takes a pointer to an array of character pointers naming one
or more paths which make up a logical file hierarchy to be traversed.
The array must be terminated by a NULL pointer.
has a number of options, at least one of which (either FTS_LOGICAL or
FTS_PHYSICAL) must be specified.
The options are selected by
routines to return FTSENT structures for the targets of symbolic links
instead of the symbolic links themselves.
If this option is set, the only symbolic links for which FTSENT structures
are returned to the application are those referencing non-existent files.
Either FTS_LOGICAL or FTS_PHYSICAL
As a performance optimization, the
functions change directories as they walk the file hierarchy.
This has the side-effect that an application cannot rely on being
in any particular directory during the traversal.
The FTS_NOCHDIR option turns off this optimization, and the
functions will not change the current directory.
Note that applications should not themselves change their current directory
and try to access files unless FTS_NOCHDIR is specified and absolute
pathnames were provided as arguments to
By default, returned FTSENT structures contain file characteristic
field) for each file visited.
This option relaxes that requirement as a performance optimization,
field to FTS_NS and leave the contents of the
routines to return FTSENT structures for symbolic links themselves instead
of the target files they point to.
If this option is set, FTSENT structures for all symbolic links in the
hierarchy are returned to the application.
Either FTS_LOGICAL or FTS_PHYSICAL
By default, unless they are specified as path arguments to
any files named ``.'' and ``..'' encountered in the file hierarchy are
routines to return FTSENT structures for them.
from descending into directories that have a different device number
than the file from which the descent began.
specifies a user-defined function which may be used to order the traversal
takes two pointers to pointers to FTSENT structures as arguments and
should return a negative value, zero, or a positive value to indicate
if the file referenced by its first argument comes before, in any order
with respect to, or after, the file referenced by its second argument.
fields of the FTSENT structures may
be used in this comparison.
If the option FTS_NOSTAT was specified, or the
field was set to FTS_NS, the
argument is NULL, the directory traversal order is unspecified except
for the root paths which are traversed in the order listed in
function returns a pointer to an FTSENT structure describing a file in
Directories (that are readable, searchable and do not cause cycles) are
visited at least twice, once in pre-order and once in post-order.
All other files are visited at least once.
(Hard links between directories that do not cause cycles or symbolic
links to symbolic links may cause files to be visited more than once,
or directories more than twice.)
If all the members of the hierarchy have been returned,
returns NULL and sets the external variable
If an error unrelated to a file in the hierarchy occurs,
If an error related to the returned file occurs, a pointer to an FTSENT
structure is returned, and
may or may not have been set (see
The FTSENT structures returned by
may be overwritten after a call to
on the same file hierarchy stream, or, after a call to
on the same file hierarchy stream unless they represent a file of type
directory, in which case they will not be overwritten until after a call to
after the FTSENT structure has been returned by the function
function returns a pointer to an FTSENT structure describing the first
entry in a linked list of the files in the directory represented by the
FTSENT structure most recently returned by
The list is linked through the
field of the FTSENT structure, and is ordered by the user-specified
comparison function, if any.
will recreate this linked list.
If the directory returned by
is readable but not searchable (see FTS_DNR and FTS_DNX) the contents
of the directory may be retrieved using the
Pathnames to the files may be built as well, as there is guaranteed
to be sufficient space in the path buffer to construct them as follows:
for (p = ftsent->fts_path; *p; ++p);
bcopy(ftsent->fts_name, p, ftsent->fts_namelen + 1);
If the FTSENT structure most recently returned by
is not a directory being visited in pre-order,
or the directory does not contain any files,
The FTSENT structures returned by
may be overwritten after a call to
on the same file hierarchy stream, or after a call to
on the same file hierarchy stream.
A single buffer is used for all of the paths of all of the files in the
fields are guaranteed to be NULL-terminated
for the file most recently returned by
To use these fields to reference any files represented by other FTSENT
structures will require that the path buffer be modified using the
information contained in that FTSENT structure's
Any such modifications should be undone before further calls to
field is always NULL-terminated.
allows the user application to determine further processing for the
returns 0 on success, and -1 if an error occurs.
must be set to one of the following values:
Re-visit the file; any file type may be re-visited.
will return the referenced file.
fields of the structure will be reinitialized at that time,
but no other fields will have been changed.
This option is meaningful only for the most recently returned
Normal use is for post-order directory visits, where it causes the
directory to be re-visited (in both pre and post-order) as well as all
The referenced file must be a symbolic link.
If the referenced file is the one most recently returned by
returns the file with the
fields reinitialized to reflect the target of the symbolic link instead
of the symbolic link itself.
If the file is one of those most recently returned by
fields of the structure, when returned by
will reflect the target of the symbolic link instead of the symbolic link
In either case, if the target of the link is a directory, the pre-order
return, followed by the return of all of its descendants, followed by a
post-order return, is done.
No descendants of this file are visited.
The file may be one of those most recently returned by either
function closes a file hierarchy stream
and restores the current directory to the directory from which
returns 0 on success, and -1 if an error occurs.
may fail and set errno for any of the errors specified for the library
may fail and set errno for any of the errors specified for the library
may fail and set errno for any of the errors specified for the library
find(1), chdir(2), stat(2), qsort(3)
utility is expected to be a superset of the POSIX 1003.1 specification.