The
fts functions are provided for traversing
UNIX file hierarchies. A simple overview is that the
fts_open() function returns a “handle” on a file hierarchy, which is then supplied to the other
fts functions. The function
fts_read() returns a pointer to a structure describing one of the files in the file hierarchy. The function
fts_children() 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). Files are visited once. 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 itself. The second is
FTSENT, the structure that represents a file in the file hierarchy. Normally, an
FTSENT structure is returned for every file in the file hierarchy. In this manual page, “file” and “
FTSENT structure” are generally interchangeable. The
FTSENT structure contains at least the following fields, which are described in greater detail below:
typedef struct _ftsent {
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) */
int fts_errno; /* file errno */
long fts_number; /* local numeric value */
void *fts_pointer; /* local address value */
struct ftsent *fts_parent; /* parent directory */
struct ftsent *fts_link; /* next file structure */
struct ftsent *fts_cycle; /* cycle structure */
struct stat *fts_statp; /* stat(2) information */
} FTSENT;
These fields are defined as follows:
fts_info
One of the following flags describing the returned
FTSENT structure and the file it represents. 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.
FTS_D
A directory being visited in pre-order.
FTS_DC
A directory that causes a cycle in the tree. (The fts_cycle field of the FTSENT structure will be filled in as well).
FTS_DEFAULT
Any FTSENT structure that represents a file type not explicitly described by one of the other fts_info values.
FTS_DNR
A directory which cannot be read. This is an error return, and the fts_errno field will be set to indicate what caused the error.
FTS_DOT
A file named ‘.' or ‘..' which was not specified as a file name to fts_open() (see FTS_SEEDOT).
FTS_DP
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 fts_info field set to FTS_D.
FTS_ERR
This is an error return, and the fts_errno field will be set to indicate what caused the error.
FTS_NS
A file for which no
stat(2) information was available. The contents of the
fts_statp field are undefined. This is an error return, and the
fts_errno field will be set to indicate what caused the error.
FTS_NSOK
A file for which no
stat(2) information was requested. The contents of the
fts_statp field are undefined.
FTS_SLNONE
A symbolic link with a non-existent target. The contents of the fts_statp field reference the file characteristic information for the symbolic link itself.
fts_accpath
A path for accessing the file from the current directory.
fts_path
The path for the file relative to the root of the traversal. This path contains the path specified to fts_open() as a prefix.
fts_pathlen
The length of the string referenced by fts_path.
fts_name
The name of the file.
fts_namelen
The length of the string referenced by fts_name.
fts_level
The depth of the traversal, numbered from -1 to N, where this file was found. 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 itself is numbered 0.
fts_errno
Upon return of a FTSENT structure from the fts_children() or fts_read() functions, with its fts_info field set to FTS_DNR, FTS_ERR or FTS_NS, the fts_errno field contains the value of the external variable errno specifying the cause of the error. Otherwise, the contents of the fts_errno field are undefined.
fts_number
This field is provided for the use of the application program and is not modified by the fts functions. It is initialized to 0.
fts_pointer
This field is provided for the use of the application program and is not modified by the fts functions. It is initialized to NULL.
fts_parent
A pointer to the FTSENT structure referencing the file in the hierarchy immediately above the current file, i.e., the directory of which this file is a member. A parent structure for the initial entry point is provided as well, however, only the fts_level, fts_number and fts_pointer fields are guaranteed to be initialized.
fts_link
Upon return from the fts_children() function, the fts_link field points to the next structure in the NULL-terminated linked list of directory members. Otherwise, the contents of the fts_link field are undefined.
fts_cycle
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 directory, the fts_cycle field of the structure will point to the FTSENT structure in the hierarchy that references the same file as the current FTSENT structure. Otherwise, the contents of the fts_cycle field are undefined.
fts_statp
A pointer to
stat(2) information for the file.
A single buffer is used for all of the paths of all of the files in the file hierarchy. Therefore, the
fts_path and
fts_accpath fields are guaranteed to be
NULL-terminated
only for the file most recently returned by
fts_read(). 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
fts_pathlen field. Any such modifications should be undone before further calls to
fts_read() are attempted. The
fts_name field is always
NULL-terminated.