xref: /original-bsd/lib/libc/gen/fts.3 (revision f0203ecd)
Copyright (c) 1989 The Regents of the University of California.
All rights reserved.

Redistribution and use in source and binary forms are permitted
provided that the above copyright notice and this paragraph are
duplicated in all such forms and that any documentation,
advertising materials, and other materials related to such
distribution and use acknowledge that the software was developed
by the University of California, Berkeley. The name of the
University may not be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

@(#)fts.3 5.5 (Berkeley) 06/09/90

FTS 3 ""
C 7
NAME
fts - traverse a file hierarchy
SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <fts.h>

FTS *
ftsopen(path_argv, options, compar)
char *path_argv[];
int options, (*compar)();

FTSENT *
ftsread(ftsp);
FTS *ftsp;

FTSENT *
ftschildren(ftsp)
FTS *ftsp;

ftsset(ftsp, f, options)
FTS *ftsp;
FTSENT *f;
int options;

ftsclose(ftsp)
FTS *ftsp;
DESCRIPTION
The fts (3) utilities are provided for traversing UNIX file hierarchies. Two structures are defined (and typedef'd) in the include file <fts.h>. The first is FTS, the structure that defines the file hierarchy stream. The second is FTSENT, the structure that defines a file in the file hierarchy.

The ftsopen routine takes a pointer to an array of character pointers (``argv'') naming the file hierarchies to be traversed. The array must be terminated by a pointer to a NULL string.

The options specified are formed by or 'ing one or more of the following values:

FTS_LOGICAL This option causes ftsread to use the function stat (2), by default, to determine the status of each file. If this option is set, the only symbolic links returned to the application are those referencing non-existent files. Either FTS_LOGICAL or FTS_PHYSICAL must be provided to the ftsopen routine.

FTS_NOCHDIR As a performance optimization, ftsread changes directories as it descends the hierarchy. This has the side-effect that applications cannot rely on being in any particular directory. The FTS_NOCHDIR option turns off this optimization. Note that applications should not change the current directory (even if FTS_NOCHDIR is specified) unless absolute pathnames were provided as arguments to ftsopen .

FTS_NOSTAT By default, ftsread and ftschildren provide file characteristic information (the statb field) for each file they return. This option relaxes that requirement; the contents of the statb field may be undefined, and the info field may be set to FTS_NS.

FTS_PHYSICAL This option causes ftsread to use the function lstat (2), by default, to determine the status of each file. If this option is set, all symbolic links are returned to the application program. Either FTS_LOGICAL or FTS_PHYSICAL must be provided to the ftsopen routine.

FTS_SEEDOT This option causes the routine ftsread to return structures for the directory entries ``.'' and ``..''. By default they are ignored unless specified as an argument to ftsopen .

FTS_XDEV This option keeps fts from descending into directories that have a different device number than the file the descent began from.

The argument compar specifies a user-defined routine which is used to order the traversal of directories. Compar 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.

The fts_accpath and fts_path fields of the FTSENT structures may not be used in this comparison. If the option FTS_NOSTAT is specified, the fts_stab field may not be used as well. If the compar argument is NULL the directory traversal order is unspecified except for the root paths which are traversed in the order listed in path_argv .

The ftsclose routine closes a file hierarchy stream and changes the current directory to the directory ftsopen was called from. Ftsclose returns 0 on success, and -1 if an error occurs.

Each call to the ftsread routine returns a pointer to an FTSENT structure describing a file in the hierarchy. Directories (that are readable, searchable and do not cause cycles) are visited at least twice, before any of their descendants have been visited (pre-order) and after all their descendants have been visited (post-order). All other files are visited at least once.

The FTSENT structure contains at least the following fields:

typedef struct ftsent {
 struct ftsent *fts_parent; /* parent structure */
 struct ftsent *fts_link; /* cycle or file structure */
 union {
 long number; /* local numeric value */
 void *pointer; /* local address value */
 } fts_local;
 char *fts_accpath; /* path from current directory */
 char *fts_path; /* path from starting directory */
 char *fts_name; /* file name */
 short fts_pathlen; /* strlen(path) */
 short fts_namelen; /* strlen(name) */
 short fts_level; /* depth (-1 to N) */
 unsigned short fts_info; /* flags for entry */
 struct stat fts_statb; /* stat(2) information */
} FTSENT;

The fields are as follows:

fts_parent A pointer to a structure referencing the file in the hierarchy immediately above the current file/structure. A parent structure for the initial entry point is provided as well, however, only the fts_local and fts_level fields are guaranteed to be initialized.

fts_link If a directory causes a cycle in the hierarchy, either because of a hard link between two directories, or a symbolic link pointing to a directory, the fts_link field of the structure will point to the structure in the hierarchy that references the same file as the current structure. Upon return from the ftschildren routine, the fts_link field points to the next structure in the linked list of entries from the directory. Otherwise, the contents of the fts_link field are undefined.

fts_local This field is provided for the use of the application program. It can be used to store either a long value or an address.

fts_accpath A path that can be used to access the file.

fts_path The path for the file relative to the root of the traversal.

fts_name The basename of the file.

fts_pathlen The length of the string referenced by fts_path .

fts_namelen The length of the string referenced by fts_name .

fts_level The depth of the traversal, numbered from -1 to N. The parent structure of the root of the traversal is numbered -1, and the structure for the root of the traversal is numbered 0.

fts_info Information describing the file. With the exception of directories (FTS_D), all of these entries are terminal, i.e. they will not be revisited, nor will their descendants be visited (if they have not been visited already).

FTS_D A directory being visited in pre-order.

FTS_DC A directory that causes a cycle. The fts_link field of the structure will be filled in as well.

FTS_DEFAULT Anything that's not one of the others.

FTS_DNR A directory that cannot be read.

FTS_DNX A directory that cannot be searched.

FTS_DOT A file named ``.'' or ``..'' with a fts_level field not equal to zero, i.e. one not specified as an argument to ftsopen . (See FTS_SEEDOT.)

FTS_DP A directory that is being visited in post-order. The contents of the structure will be unchanged from when the directory was visited in pre-order.

FTS_ERR An error indicator; the external variable errno contains an error number indicating the reason for the error.

FTS_F A regular file.

FTS_NS No stat (2) information is available at this time (see FTS_NOSTAT); the contents of the fts_statb field are undefined.

FTS_SL A symbolic link.

FTS_SLNONE A symbolic link with a non-existent target.

fts_statb Stat (2) information for the file.

The fts_accpath and fts_path fields are guaranteed to be NULL-terminated only for the file most recently returned by ftsread . The fts_name field is always NULL-terminated. To use these fields to reference any other active files may require that they be modified using the information contained in the fts_pathlen field. Any such modifications should be undone before further calls to ftsread are attempted.

If all the members of the hierarchy have been returned, ftsread returns NULL and sets the external variable errno to 0. If an error unrelated to a file in the hierarchy occurs, ftsread returns NULL and sets errno appropriately. Otherwise, a pointer to an FTSENT structure is returned, and an error may or may not have occurred; see FTS_ERR above.

When the most recently returned file from ftsread is a directory being visited in pre-order, ftschildren returns a pointer to the first entry in a linked list (sorted by the comparison routine, if provided) of the directory entries it contains. The linked list is linked through the fts_link field of the FTSENT structure. Each call to ftschildren recreates the list. Note, cycles are not detected until a directory is actually visited, therefore ftschildren will never return a structure with an fts_info field set to FTS_DC. If the current file is not a directory being visited in pre-order, or if an error occurs, or the file does not contain any entries ftschildren returns NULL. If an error occurs, ftschildren sets errno appropriately, otherwise it sets errno to zero.

The pointers returned by ftsread and ftschildren point to structures which may be overwritten. Structures returned by ftschildren and ftsread may be overwritten after a call to ftsclose on the same file hierarchy. Otherwise, structures returned by ftschildren will not be overwritten until a subsequent call to either ftschildren or ftsread on the same file hierarchy. Structures returned by ftsread will not not be overwritten until a subsequent call to ftsread on the same file hierarchy stream, unless it represents a file of type directory, in which case it will not be overwritten until after a subsequent call to ftsread after it has been returned by the function ftsread in post-order.

The routine ftsset allows the user application to determine further processing for the file f of the stream ftsp . Ftsset returns 0 on success, and -1 if an error occurs. Option may be set to any one of the following values.

FTS_AGAIN Re-visit the file; any file type may be re-visited. The next call to ftsread will return the referenced file. The fts_stat and fts_info fields of the structure will be reinitialized at that time, but no other fields will have been modified. This option is meaningful only for the most recently returned file from ftsread . 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 of its descendants.

FTS_FOLLOW The referenced file must be a symbolic link. If the referenced file is the one most recently returned by ftsread , the next call to ftsread returns the file with the fts_info and fts_statb 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 ftschildren , the fts_info and fts_statb fields of the structure, when returned by ftsread , will reflect the target of the symbolic link instead of the symbolic link itself. 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.

FTS_SKIP No descendants of this file are visited.

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.

ERRORS
Ftsopen may fail and set errno for any of the errors specified for the library routine malloc (3).

Ftsclose may fail and set errno for any of the errors specified for the library routine chdir (2).

Ftsread and ftschildren may fail and set errno for any of the errors specified for the library routines chdir (2), getgroups (2), malloc (3), opendir (3), readdir (3) and stat (2).

SEE ALSO
find(1), chdir(2), stat(2), qsort(3)
STANDARDS
The fts utility is expected to be a superset of the POSIX 1003.1 specification.