BSD 4_4 release
[unix-history] / usr / src / lib / libc / gen / fts.3
index e01b9f0..48d0c42 100644 (file)
-.\" Copyright (c) 1989 The Regents of the University of California.
-.\" All rights reserved.
+.\" Copyright (c) 1989, 1991, 1993
+.\"    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.
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\" 3. All advertising materials mentioning features or use of this software
+.\"    must display the following acknowledgement:
+.\"    This product includes software developed by the University of
+.\"    California, Berkeley and its contributors.
+.\" 4. Neither the name of the University nor the names of its contributors
+.\"    may be used to endorse or promote products derived from this software
+.\"    without specific prior written permission.
 .\"
 .\"
-.\"    @(#)fts.3       5.2 (Berkeley) %G%
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
 .\"
 .\"
-.TH FTS 3 ""
-.UC 7
-.SH NAME
-fts \- traverse a file hierarchy
-.SH SYNOPSIS
-.nf
-.ft B
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fts.h>
-
-FTS *
-ftsopen(path, options, compar)
-char *path;
-int options, (*compar)();
-
-FTS *
-ftsopen(path_argv, options | FTS_MULTIPLE, 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;
-.ft R
-.fi
-.SH DESCRIPTION
+.\"     @(#)fts.3      8.1 (Berkeley) 6/4/93
+.\"
+.Dd June 4, 1993
+.Dt FTS 3
+.Os
+.Sh NAME
+.Nm fts
+.Nd traverse a file hierarchy
+.Sh SYNOPSIS
+.Fd #include <sys/types.h>
+.Fd #include <sys/stat.h>
+.Fd #include <fts.h>
+.Ft FTS *
+.Fn fts_open "char * const *path_argv" "int options" "int *compar(const FTSENT **, const FTSENT **)"
+.Ft FTSENT *
+.Fn fts_read "FTS *ftsp"
+.Ft FTSENT *
+.Fn fts_children "FTS *ftsp" "int options"
+.Ft int
+.Fn fts_set "FTS ftsp" "FTSENT *f" "int options"
+.Ft int
+.Fn fts_close "FTS *ftsp"
+.Sh DESCRIPTION
 The
 The
-.IR fts (3)
-utilities are provided for traversing UNIX file hierarchies.
-Two structures are defined (and typedef'd) in the include file <\fIfts.h\fP>.
-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
+.Nm fts
+functions are provided for traversing
+.Tn UNIX
+file hierarchies.
+A simple overview is that the
+.Fn fts_open
+function returns a ``handle'' on a file hierarchy, which is then supplied to
+the other
+.Nm fts
+functions.
+The function
+.Fn fts_read
+returns a pointer to a structure describing one of the files in the file
 hierarchy.
 hierarchy.
-.PP
-The
-.I ftsopen
-routine returns a pointer to a file hierarchy rooted at
-.IR path .
-The 
-.I options
-specified are formed by
-.IR or 'ing
-one or more of the following values:
-.TP
-FTS_LOGICAL
-This option causes 
-.I ftsread
-to use the function
-.IR 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
-.B must
-be provided to the 
-.I ftsopen
-routine.
-.TP
-FTS_MULTIPLE
-.I Ftsopen
-takes a single file name as a path argument by default.
-If the FTS_MULTIPLE option is specified, the path argument is a pointer
-to an array of character pointers (``argv'') to the paths to be traversed.
-The array must be terminated by a pointer to a NULL string.
-In this case the ``logical'' hierarchy is traversed, i.e. it's as if
-there's a virtual directory that contains the list of paths supplied,
-and the hierarchy is rooted in that directory.
-.TP
-FTS_NOCHDIR
-As a performance optimization,
-.I 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
-.IR ftsopen .
-.TP
-FTS_NOSTAT
-By default,
-.I ftsread
-and
-.I ftschildren
-provide file characteristic information (the
-.I statb
-field) for each file they return.
-This option relaxes that requirement; the contents of the
-.I statb
-field may be undefined, and the
-.I info
-field may be set to FTS_NS.
-.TP
-FTS_PHYSICAL
-This option causes 
-.I ftsread
-to use the function
-.IR 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
-.B must
-be provided to the 
-.I ftsopen
-routine.
-.TP
-FTS_SEEDOT
-This option causes the routine
-.I ftsread
-to return structures for the directory entries ``.'' and ``..''.
-By default they are ignored unless specified as an argument to
-.IR ftsopen .
-.PP
-The argument
-.I compar
-specifies a user-defined routine which is used to order the traversal
-of directories.
-.I 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.
-Note, the
-.I accpath
-and
-.I path
-fields of the FTSENT structures may not be used in this comparison.
-If the
-.I compar
-argument is NULL the traversal order is undefined.
-.PP
-If both FTS_MULTIPLE and the user comparison routine are specified,
-the root paths are processed in sorted order.
-Otherwise, the root paths are processed in the order specified by
-the user.
-.PP
+The function
+.Fn 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.
+.Pp
+Two structures are defined (and typedef'd) in the include file
+.Aq Pa fts.h .
+The first is
+.Fa FTS ,
+the structure that represents the file hierarchy itself.
+The second is
+.Fa FTSENT ,
+the structure that represents a file in the file
+hierarchy.
+Normally, an
+.Fa FTSENT
+structure is returned for every file in the file
+hierarchy.
+In this manual page, ``file'' and
+.Dq Fa FTSENT No structure
+are generally
+interchangeable.
 The
 The
-.I ftsclose
-routine closes a file hierarchy stream and changes the current
-directory to the directory
-.I ftsopen
-was called from.
-.I Ftsclose
-returns 0 on success, and -1 if an error occurs.
-.PP
-Each call to the
-.I 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.
-.PP
-The FTSENT structure contains at least the following fields:
-.sp
-.RS
-.nf
-.ta .5i +.5i +\w'struct ftsent *parent;\0\0\0'u
-typedef struct ftsent {
-       struct ftsent *parent;          /* parent structure */
-       struct ftsent *link;            /* cycle or file structure */
-       union {
-               long number;            /* local numeric value */
-               void *pointer;          /* local address value */
-       } local;
-       char *accpath;                  /* path from current directory */
-       char *path;                     /* path from starting directory */
-       char *name;                     /* file name */
-       short pathlen;                  /* strlen(path) */
-       short namelen;                  /* strlen(name) */
-       short level;                    /* depth (\-1 to N) */
-       unsigned short info;            /* flags for entry */
-       struct stat statb;                      /* stat(2) information */
+.Fa FTSENT
+structure contains at least the following fields, which are
+described in greater detail below:
+.Bd -literal
+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_error;                  /* 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;
 } FTSENT;
-.fi
-.RE
-.PP
-The fields are as follows:
-.TP
-parent
-A pointer to a structure referencing the file in the hierarchy
-immediately above the current file/structure.
+.Ed
+.Pp
+These fields are defined as follows:
+.Bl -tag -width "fts_namelen"
+.It Fa fts_info
+One of the following flags describing the returned
+.Fa FTSENT
+structure and
+the file it represents.
+With the exception of directories without errors
+.Pq Dv FTS_D ,
+all of these
+entries are terminal, that is, they will not be revisited, nor will any
+of their descendants be visited.
+.Bl  -tag -width FTS_DEFAULT
+.It Dv FTS_D
+A directory being visited in pre-order.
+.It Dv FTS_DC
+A directory that causes a cycle in the tree.
+(The
+.Fa fts_cycle
+field of the
+.Fa FTSENT
+structure will be filled in as well.)
+.It Dv FTS_DEFAULT
+Any
+.Fa FTSENT
+structure that represents a file type not explicitly described
+by one of the other
+.Fa fts_info
+values.
+.It Dv FTS_DNR
+A directory which cannot be read.
+This is an error return, and the
+.Fa fts_errno
+field will be set to indicate what caused the error.
+.It Dv FTS_DOT
+A file named
+.Ql \&.
+or
+.Ql ..
+which was not specified as a file name to
+.Fn fts_open
+(see
+.Dv FTS_SEEDOT ) .
+.It Dv FTS_DP
+A directory being visited in post-order.
+The contents of the
+.Fa FTSENT
+structure will be unchanged from when
+it was returned in pre-order, i.e. with the
+.Fa fts_info
+field set to
+.Dv FTS_D .
+.It Dv FTS_ERR
+This is an error return, and the
+.Fa fts_errno
+field will be set to indicate what caused the error.
+.It Dv FTS_F
+A regular file.
+.It Dv FTS_NS
+A file for which no
+.Xr stat 2
+information was available.
+The contents of the
+.Fa fts_statp
+field are undefined.
+This is an error return, and the
+.Fa fts_errno
+field will be set to indicate what caused the error.
+.It Dv FTS_NSOK
+A file for which no
+.Xr stat 2
+information was requested.
+The contents of the
+.Fa fts_statp
+field are undefined.
+.It Dv FTS_SL
+A symbolic link.
+.It Dv FTS_SLNONE
+A symbolic link with a non-existent target.
+The contents of the
+.Fa fts_statp
+field reference the file characteristic information for the symbolic link
+itself.
+.El
+.It Fa fts_accpath
+A path for accessing the file from the current directory.
+.It Fa fts_path
+The path for the file relative to the root of the traversal.
+This path contains the path specified to
+.Fn fts_open
+as a prefix.
+.It Fa fts_pathlen
+The length of the string referenced by
+.Fa fts_path .
+.It Fa fts_name
+The name of the file.
+.It Fa fts_namelen
+The length of the string referenced by
+.Fa fts_name .
+.It Fa fts_level
+The depth of the traversal, numbered from \-1 to N, where this file
+was found.
+The
+.Fa FTSENT
+structure representing the parent of the starting point (or root)
+of the traversal is numbered \-1, and the
+.Fa FTSENT
+structure for the root
+itself is numbered 0.
+.It Fa fts_errno
+Upon return of a
+.Fa FTSENT
+structure from the
+.Fn fts_children
+or
+.Fn fts_read
+functions, with its
+.Fa fts_info
+field set to 
+.Dv FTS_DNR ,
+.Dv FTS_ERR
+or
+.Dv FTS_NS ,
+the
+.Fa fts_errno
+field contains the value of the external variable
+.Va errno
+specifying the cause of the error.
+Otherwise, the contents of the
+.Fa fts_errno
+field are undefined.
+.It Fa fts_number
+This field is provided for the use of the application program and is
+not modified by the
+.Nm fts
+functions.
+It is initialized to 0.
+.It Fa fts_pointer
+This field is provided for the use of the application program and is
+not modified by the
+.Nm fts
+functions.
+It is initialized to
+.Dv NULL .
+.It Fa fts_parent
+A pointer to the
+.Fa 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
 A parent structure for the initial entry point is provided as well,
 however, only the
-.I local
+.Fa fts_level ,
+.Fa fts_number
 and
 and
-.I level
+.Fa fts_pointer
 fields are guaranteed to be initialized.
 fields are guaranteed to be initialized.
-.TP
-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
-.I link
-field of the structure will point to the structure in the hierarchy 
-that references the same file as the current structure.
+.It Fa fts_link
 Upon return from the
 Upon return from the
-.I ftschildren
-routine, the
-.I link
-field points to the next structure in the linked list of entries
-from the directory.
-Otherwise, the contents of the link field are undefined.
-.TP
-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.
-.TP
-accpath
-A path that can be used to access the file.
-.TP
-path
-The path for the file relative to the root of the traversal.
-.TP
-name
-The basename of the file.
-.TP
-pathlen
-The length of the string referenced by
-.IR path .
-.TP
-namelen
-The length of the string referenced by
-.IR name .
-.TP 
-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.
-.TP 
-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).
-.RS
-.TP
-FTS_D
-A directory being visited in pre-order.
-.TP
-FTS_DC
-A directory that causes a cycle.
-The 
-.I link
-field of the structure will be filled in as well.
-.TP
-FTS_DEFAULT
-Anything that's not one of the others.
-.TP
-FTS_DNR
-A directory that cannot be read.
-.TP
-FTS_DNX
-A directory that cannot be searched.
-.TP
-FTS_DOT
-A file named ``.'' or ``..'' with a
-.I level
-field not equal to zero, i.e. one not specified as an argument to
-.IR ftsopen .
-(See FTS_SEEDOT.)
-.TP
-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.
-.TP
-FTS_ERR
-An error indicator; the external variable
-.I errno
-contains an error number indicating the reason for the error.
-.TP
-FTS_F
-A regular file.
-.TP
-FTS_NS
-No
-.IR stat (2)
-information is available at this time (see FTS_NOSTAT); the
-contents of the
-.I statb
+.Fn fts_children
+function, the
+.Fa fts_link
+field points to the next structure in the NULL-terminated linked list of
+directory members.
+Otherwise, the contents of the
+.Fa fts_link
 field are undefined.
 field are undefined.
-.TP
-FTS_SL
-A symbolic link.
-.TP
-FTS_SLNONE
-A symbolic link with a non-existent target.
-.RE
-.TP
-statb
-.IR Stat (2)
+.It Fa fts_cycle
+If a directory causes a cycle in the hierarchy (see
+.Dv FTS_DC ) ,
+either because
+of a hard link between two directories, or a symbolic link pointing to a
+directory, the
+.Fa fts_cycle
+field of the structure will point to the
+.Fa FTSENT
+structure in the hierarchy that references the same file as the current
+.Fa FTSENT
+structure.
+Otherwise, the contents of the
+.Fa fts_cycle
+field are undefined.
+.It Fa fts_statp
+A pointer to
+.Xr stat 2
 information for the file.
 information for the file.
-.PP
-The
-.IR accpath
+.El
+.Pp
+A single buffer is used for all of the paths of all of the files in the
+file hierarchy.
+Therefore, the
+.Fa fts_path
 and
 and
-.I path
-fields are guaranteed to be NULL-terminated
-.B only
+.Fa fts_accpath
+fields are guaranteed to be
+.Dv NULL Ns -terminated
+.Em only
 for the file most recently returned by
 for the file most recently returned by
-.IR ftsread .
-The
-.I 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
-.I pathlen
+.Fn fts_read .
+To use these fields to reference any files represented by other
+.Fa FTSENT
+structures will require that the path buffer be modified using the
+information contained in that
+.Fa FTSENT
+structure's
+.Fa fts_pathlen
 field.
 Any such modifications should be undone before further calls to
 field.
 Any such modifications should be undone before further calls to
-.I ftsread
+.Fn fts_read
 are attempted.
 are attempted.
-.PP
+The
+.Fa fts_name
+field is always
+.Dv NULL Ns -terminated.
+.Sh FTS_OPEN
+The
+.Fn fts_open
+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
+.Dv NULL
+pointer.
+.Pp
+There are
+a number of options, at least one of which (either
+.Dv FTS_LOGICAL
+or
+.Dv FTS_PHYSICAL )
+must be specified.
+The options are selected by
+.Em or Ns 'ing
+the following values:
+.Bl -tag -width "FTS_PHYSICAL"
+.It Dv FTS_COMFOLLOW
+This option causes any symbolic link specified as a root path to be
+followed immediately whether or not
+.Dv FTS_LOGICAL
+is also specified.
+.It Dv FTS_LOGICAL
+This option causes the
+.Nm fts
+routines to return
+.Fa 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
+.Fa FTSENT
+structures
+are returned to the application are those referencing non-existent files.
+Either
+.Dv FTS_LOGICAL
+or
+.Dv FTS_PHYSICAL
+.Em must
+be provided to the
+.Fn fts_open
+function.
+.It Dv FTS_NOCHDIR
+As a performance optimization, the
+.Nm fts
+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
+.Dv FTS_NOCHDIR
+option turns off this optimization, and the
+.Nm fts
+functions will not change the current directory.
+Note that applications should not themselves change their current directory
+and try to access files unless
+.Dv FTS_NOCHDIR
+is specified and absolute
+pathnames were provided as arguments to
+.Fn fts_open .
+.It Dv FTS_NOSTAT
+By default, returned
+.Fa FTSENT
+structures reference file characteristic information (the
+.Fa statp
+field) for each file visited.
+This option relaxes that requirement as a performance optimization,
+allowing the
+.Nm fts
+functions to set the
+.Fa fts_info
+field to
+.Dv FTS_NSOK
+and leave the contents of the
+.Fa statp
+field undefined.
+.It Dv FTS_PHYSICAL
+This option causes the
+.Nm fts
+routines to return
+.Fa FTSENT
+structures for symbolic links themselves instead
+of the target files they point to.
+If this option is set,
+.Fa FTSENT
+structures for all symbolic links in the
+hierarchy are returned to the application.
+Either
+.Dv FTS_LOGICAL
+or
+.Dv FTS_PHYSICAL
+.Em must
+be provided to the
+.Fn fts_open
+function.
+.It Dv FTS_SEEDOT
+By default, unless they are specified as path arguments to
+.Fn fts_open ,
+any files named
+.Ql \&.
+or
+.Ql ..
+encountered in the file hierarchy are ignored.
+This option causes the
+.Nm fts
+routines to return
+.Fa FTSENT
+structures for them.
+.It Dv FTS_XDEV
+This option prevents
+.Nm fts
+from descending into directories that have a different device number
+than the file from which the descent began.
+.El
+.Pp
+The argument
+.Fn compar
+specifies a user-defined function which may be used to order the traversal
+of the hierarchy.
+It
+takes two pointers to pointers to
+.Fa 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
+.Fa fts_accpath ,
+.Fa fts_path
+and
+.Fa fts_pathlen
+fields of the
+.Fa FTSENT
+structures may
+.Em never
+be used in this comparison.
+If the 
+.Fa fts_info
+field is set to
+.Dv FTS_NS
+or
+.Dv FTS_NSOK ,
+the
+.Fa fts_statp
+field may not either.
+If the
+.Fn compar
+argument is
+.Dv NULL ,
+the directory traversal order is in the order listed in
+.Fa path_argv
+for the root paths, and in the order listed in the directory for
+everything else.
+.Sh FTS_READ
+The
+.Fn fts_read
+function returns a pointer to an
+.Fa FTSENT
+structure describing a file in
+the hierarchy.
+Directories (that are readable 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.)
+.Pp
 If all the members of the hierarchy have been returned,
 If all the members of the hierarchy have been returned,
-.I ftsread
-returns NULL and sets the external variable
-.I errno
+.Fn fts_read
+returns
+.Dv NULL
+and sets the external variable
+.Va errno
 to 0.
 If an error unrelated to a file in the hierarchy occurs,
 to 0.
 If an error unrelated to a file in the hierarchy occurs,
-.I 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.
-.PP
-When the most recently returned file from 
-.I ftsread
-is a directory being visited in pre-order, 
-.I 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
-.I link
-field of the FTSENT structure.
-Each call to
-.I ftschildren
-recreates the list.
-Note, cycles are not detected until a directory is actually visited,
-therefore
-.I ftschildren
-will never return a structure with an
-.I 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
-.I ftschildren
-returns NULL.
+.Fn fts_read
+returns
+.Dv NULL
+and sets
+.Va errno
+appropriately.
+If an error related to a returned file occurs, a pointer to an
+.Fa FTSENT
+structure is returned, and
+.Va errno
+may or may not have been set (see
+.Fa fts_info ) .
+.Pp
+The
+.Fa FTSENT
+structures returned by
+.Fn fts_read
+may be overwritten after a call to
+.Fn fts_close
+on the same file hierarchy stream, or, after a call to
+.Fn fts_read
+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
+.Fn fts_read
+after the
+.Fa FTSENT
+structure has been returned by the function
+.Fn fts_read
+in post-order.
+.Sh FTS_CHILDREN
+The
+.Fn fts_children
+function returns a pointer to an
+.Fa FTSENT
+structure describing the first entry in a NULL-terminated linked list of
+the files in the directory represented by the
+.Fa FTSENT
+structure most recently returned by
+.Fn fts_read .
+The list is linked through the
+.Fa fts_link
+field of the
+.Fa FTSENT
+structure, and is ordered by the user-specified comparison function, if any.
+Repeated calls to
+.Fn fts_children
+will recreate this linked list.
+.Pp
+As a special case, if
+.Fn fts_read
+has not yet been called for a hierarchy,
+.Fn fts_children
+will return a pointer to the files in the logical directory specified to
+.Fn fts_open ,
+i.e. the arguments specified to
+.Fn fts_open .
+Otherwise, if the
+.Fa FTSENT
+structure most recently returned by
+.Fn fts_read
+is not a directory being visited in pre-order,
+or the directory does not contain any files,
+.Fn fts_children
+returns
+.Dv NULL
+and sets
+.Va errno
+to zero.
 If an error occurs,
 If an error occurs,
-.I ftschildren
-sets errno appropriately, otherwise it sets errno to zero.
-.PP
-The pointers returned by
-.I ftsread
-and
-.I ftschildren
-point to structures which may be overwritten.
-Structures returned by
-.I ftschildren
-and
-.I ftsread
+.Fn fts_children
+returns
+.Dv NULL
+and sets
+.Va errno
+appropriately.
+.Pp
+The
+.Fa FTSENT
+structures returned by
+.Fn fts_children
 may be overwritten after a call to
 may be overwritten after a call to
-.I ftsclose
-on the same file hierarchy.
-Otherwise, structures returned by
-.I ftschildren
-will not be overwritten until a subsequent call to either
-.I ftschildren
+.Fn fts_children ,
+.Fn fts_close
 or
 or
-.I ftsread
-on the same file hierarchy.
-Structures returned by
-.I ftsread
-will not not be overwritten until a subsequent call to
-.I 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
-.I ftsread
-after it has been returned by the function
-.I ftsread
-in post-order.
-.PP
-The routine
-.I ftsset
+.Fn fts_read
+on the same file hierarchy stream.
+.Pp
+.Em Option
+may be set to the following value:
+.Bl -tag -width FTS_NAMEONLY
+.It Dv FTS_NAMEONLY
+Only the names of the files are needed.
+The contents of all the fields in the returned linked list of structures
+are undefined with the exception of the
+.Fa fts_name
+and
+.Fa fts_namelen
+fields.
+.El
+.Sh FTS_SET
+The function
+.Fn fts_set
 allows the user application to determine further processing for the
 file
 allows the user application to determine further processing for the
 file
-.I f
+.Fa f
 of the stream
 of the stream
-.IR ftsp .
-.I Ftsset
-returns 0 on success, and -1 if an error occurs.
-.I Option
-may be set to any one of the following values.
-.TP
-FTS_AGAIN
+.Fa ftsp .
+The
+.Fn fts_set
+function
+returns 0 on success, and \-1 if an error occurs.
+.Em Option
+must be set to one of the following values:
+.Bl -tag -width FTS_PHYSICAL
+.It Dv FTS_AGAIN
 Re-visit the file; any file type may be re-visited.
 The next call to
 Re-visit the file; any file type may be re-visited.
 The next call to
-.I ftsread
+.Fn fts_read
 will return the referenced file.
 will return the referenced file.
-The 
-.stat
+The
+.Fa fts_stat
 and
 and
-.info
+.Fa fts_info
 fields of the structure will be reinitialized at that time,
 fields of the structure will be reinitialized at that time,
-but no other fields will have been modified.
+but no other fields will have been changed.
 This option is meaningful only for the most recently returned
 file from
 This option is meaningful only for the most recently returned
 file from
-.IR ftsread .
+.Fn fts_read .
 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.
 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.
-.TP
-FTS_FOLLOW
+.It Dv FTS_FOLLOW
 The referenced file must be a symbolic link.
 If the referenced file is the one most recently returned by
 The referenced file must be a symbolic link.
 If the referenced file is the one most recently returned by
-.IR ftsread ,
+.Fn fts_read ,
 the next call to
 the next call to
-.I ftsread
+.Fn fts_read
 returns the file with the
 returns the file with the
-.info
+.Fa fts_info
 and
 and
-.I statb
+.Fa fts_statp
 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 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
-.IR ftschildren ,
+.Fn fts_children ,
 the
 the
-.info
+.Fa fts_info
 and
 and
-.I statb
+.Fa fts_statp
 fields of the structure, when returned by
 fields of the structure, when returned by
-.IR ftsread ,
+.Fn fts_read ,
 will reflect the target of the symbolic link instead of the symbolic link
 itself.
 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.
-.TP
-FTS_SKIP
+In either case, if the target of the symbolic link does not exist the
+fields of the returned structure will be unchanged and the
+.Fa fts_info
+field will be set to
+.Dv FTS_SLNONE .
+.Pp
+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.
+.It Dv FTS_SKIP
 No descendants of this file are visited.
 No descendants of this file are visited.
-.PP
-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.
-.SH ERRORS
-.I Ftsopen
-may fail and set errno for any of the errors specified for the library
-routine
-.IR malloc (3).
-.PP
-.I Ftsclose
-may fail and set errno for any of the errors specified for the library
-routine
-.IR chdir (2).
-.PP
-.I Ftsread
+The file may be one of those most recently returned by either
+.Fn fts_children
+or
+.Fn fts_read .
+.El
+.Sh FTS_CLOSE
+The
+.Fn fts_close
+function closes a file hierarchy stream
+.Fa ftsp
+and restores the current directory to the directory from which
+.Fn fts_open
+was called to open
+.Fa ftsp .
+The
+.Fn fts_close
+function
+returns 0 on success, and \-1 if an error occurs.
+.Sh ERRORS
+The function
+.Fn fts_open
+may fail and set
+.Va errno
+for any of the errors specified for the library functions
+.Xr open 2
+and
+.Xr malloc 3 .
+.Pp
+The function
+.Fn fts_close
+may fail and set
+.Va errno
+for any of the errors specified for the library functions
+.Xr chdir 2
+and
+.Xr close 2 .
+.Pp
+The functions
+.Fn fts_read
+and
+.Fn fts_children
+may fail and set
+.Va errno
+for any of the errors specified for the library functions
+.Xr chdir 2 ,
+.Xr malloc 3 ,
+.Xr opendir 3 ,
+.Xr readdir 3
 and
 and
-.I ftschildren
-may fail and set errno for any of the errors specified for the library
-routines
-.IR chdir (2),
-.IR getgroups (2),
-.IR malloc (3),
-.IR opendir (3),
-.IR readdir (3)
+.Xr stat 2 .
+.Pp
+In addition,
+.Fn fts_children ,
+.Fn fts_open
 and
 and
-.IR stat (2).
-.SH SEE ALSO
-find(1), chdir(2), stat(2), qsort(3)
-.SH STANDARDS
+.Fn fts_set
+may fail and set
+.Va errno
+as follows:
+.Bl -tag -width Er
+.It Bq Er EINVAL
+The options were invalid.
+.EL
+.Sh SEE ALSO
+.Xr find 1 ,
+.Xr chdir 2 ,
+.Xr stat 2 ,
+.Xr qsort 3
+.Sh STANDARDS
 The
 The
-.I fts
-utility is expected to be POSIX 1003.1 compliant.
+.Nm fts
+utility is expected to be a superset of the
+.St -p1003.1-88
+specification.