Blame src/lib/libast/man/ftwalk.3

Packit Service a8c26c
.fp 5 CW
Packit Service a8c26c
.TH FTWALK 3
Packit Service a8c26c
.SH NAME
Packit Service a8c26c
\fBftwalk\fR \- file tree walker
Packit Service a8c26c
.SH SYNOPSIS
Packit Service a8c26c
.ta .75i 1.5i 2.25i 3i 3.75i 4.5i 5.25i 6i
Packit Service a8c26c
.PP
Packit Service a8c26c
.nf
Packit Service a8c26c
\f5
Packit Service a8c26c
#include <ftwalk.h>
Packit Service a8c26c
Packit Service a8c26c
int ftwalk(char* path, int (*userf)(struct FTW* ftw), int options,
Packit Service a8c26c
       int (*comparf)(struct FTW* ftw1, struct FTW* ftw2));
Packit Service a8c26c
Packit Service a8c26c
Packit Service a8c26c
int ftwflags(void);
Packit Service a8c26c
\fR
Packit Service a8c26c
.fi
Packit Service a8c26c
.SH DESCRIPTION
Packit Service a8c26c
.PP
Packit Service a8c26c
\fIFtwalk\fR traverses a directory hierarchy using depth-first search.
Packit Service a8c26c
Upon visiting each file or directory in the hierarchy, it calls
Packit Service a8c26c
the user function \fIuserf\fP to process that file or directory.
Packit Service a8c26c
On a directory object, \fIuserf\fR may be called twice, once in preorder
Packit Service a8c26c
and once in postorder.
Packit Service a8c26c
On a terminal object such as a file or an unreadable directory,
Packit Service a8c26c
\fIuserf\fP is called only once.
Packit Service a8c26c
Cycles due to hard links or symbolic links are detected
Packit Service a8c26c
to avoid infinite loops.
Packit Service a8c26c
.PP
Packit Service a8c26c
\fIPath\fR is the starting point of the search.
Packit Service a8c26c
It may be an absolute path name or a path name relative to
Packit Service a8c26c
the current directory.
Packit Service a8c26c
If \fIpath\fR is a null pointer or points to an empty string, it is treated
Packit Service a8c26c
as if it points to the current (dot) directory.
Packit Service a8c26c
.PP
Packit Service a8c26c
\fIOptions\fR consists of zero or more of the following bits:
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_CHILDREN:
Packit Service a8c26c
This implies preorder calls to \fIuserf\fR on directory objects.
Packit Service a8c26c
On such a call to \fIuserf\fR,
Packit Service a8c26c
the field \fIftw->link\fR (below) points to a link list of
Packit Service a8c26c
the children of the respective directory.
Packit Service a8c26c
Upon returning from \fIuserf\fP,
Packit Service a8c26c
if the field \fIftw->status\fR of any child object
Packit Service a8c26c
is set to FTW_SKIP (below), that child is pruned from the search.
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_DELAY: When \fBFTW_CHILDREN\fP is turned on,
Packit Service a8c26c
the fields \fIftw->statb\fP (\fIstruct stat\fP) of children objects
Packit Service a8c26c
remain undefined until these objects are visited.
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_DOT: Do not use \fIchdir\fR(2) during the traversal.
Packit Service a8c26c
Normally \fIchdir\fR is used so that
Packit Service a8c26c
the base name of the object about to be processed can be used
Packit Service a8c26c
in accessing its data.
Packit Service a8c26c
This can enhance \fIftwalk\fR efficiency but certain program effects
Packit Service a8c26c
such as core dumps may be generated in unexpected places
Packit Service a8c26c
or may not even be generated at all.
Packit Service a8c26c
Whenever \fIchdir\fR generates an error, if possible,
Packit Service a8c26c
the current directory is restored to the starting directory
Packit Service a8c26c
(see FTW_NAME and FTW_PATH).
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_MULTIPLE: The \fIpath\fP argument is treated as a \fIchar**\fP
Packit Service a8c26c
pointer to a null-terminated array of path names.
Packit Service a8c26c
All hierarchies rooted at these paths will be searched
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_POST: Calls to the user function are issued only in postorder.
Packit Service a8c26c
That is, \fIuserf\fP is called on a directory only after its descendants have
Packit Service a8c26c
been processed.
Packit Service a8c26c
The absence of this bit indicates that calls to the user functions
Packit Service a8c26c
are issued in preorder. That is, \fIuserf\fP is
Packit Service a8c26c
called on a directory before its descendants  are processed.
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_PHYSICAL: Use \fIlstat\fR(2) instead of \fIstat\fR(2) to get
Packit Service a8c26c
file status and allow detection of symbolic links.
Packit Service a8c26c
In addition, if each component
Packit Service a8c26c
of the absolute path to the starting object has search permission,
Packit Service a8c26c
the absolute path is used for early detection of cycles.
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_META|FTW_PHYSICAL: Use \fIstat\fR(2) for top level file status and
Packit Service a8c26c
\fIlstat\fR(2) for all other files.
Packit Service a8c26c
Used to implement the POSIX
Packit Service a8c26c
.B \-H
Packit Service a8c26c
option for commands like
Packit Service a8c26c
.IR ls .
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_TWICE: Calls to the user function are issued in both preorder and postorder
Packit Service a8c26c
for directory objects.
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_USER: The first of 6 user defined option bits.
Packit Service a8c26c
These bits are ignored by \fIftwalk\fP.
Packit Service a8c26c
.PP
Packit Service a8c26c
\fIUserf\fR is a user supplied function that is
Packit Service a8c26c
called upon different visits of an object.
Packit Service a8c26c
If the return value of \fIuserf\fR is non-zero,
Packit Service a8c26c
\fIftwalk\fR returns immediately with the same value.
Packit Service a8c26c
The \fIuserf\fP prototype is:
Packit Service a8c26c
.PP
Packit Service a8c26c
.nf
Packit Service a8c26c
	int userf(struct FTW* ftw)
Packit Service a8c26c
.fi
Packit Service a8c26c
.PP
Packit Service a8c26c
\fBstruct FTW\fP contains at least the following elements:
Packit Service a8c26c
.PP
Packit Service a8c26c
.nf
Packit Service a8c26c
    struct FTW*    link;    /* link list of children */
Packit Service a8c26c
    struct FTW*    parent;  /* parent object on the search path */
Packit Service a8c26c
    union
Packit Service a8c26c
    {
Packit Service a8c26c
    long           number;  /* local number */
Packit Service a8c26c
    void*          pointer; /* local pointer */
Packit Service a8c26c
    }              local;   /* user defined */
Packit Service a8c26c
    struct stat    statb;   /* stat buffer of this object */
Packit Service a8c26c
    char*          path;    /* full pathname */
Packit Service a8c26c
    short          pathlen; /* strlen(path) */
Packit Service a8c26c
    unsigned short info;    /* type of object */
Packit Service a8c26c
    unsigned short status;  /* status of object */ 
Packit Service a8c26c
    short          level;   /* depth of object on the search path */
Packit Service a8c26c
    short          namelen; /* strlen(name) */
Packit Service a8c26c
    char           name[];  /* file name of object */
Packit Service a8c26c
.fi
Packit Service a8c26c
.PP
Packit Service a8c26c
The \fIlink\fR field is normally NULL.
Packit Service a8c26c
If the option FTW_CHILDREN was turned on,
Packit Service a8c26c
it points to the start of the list of children
Packit Service a8c26c
of the directory being visited in preorder.
Packit Service a8c26c
Finally, if the directory being visited causes a cycle,
Packit Service a8c26c
\fIlink\fR points to the object on the search path that is
Packit Service a8c26c
identical to this directory. Note that if FTW_PHYSICAL was turned on,
Packit Service a8c26c
this may point to a directory that is an ancestor of the starting
Packit Service a8c26c
object.
Packit Service a8c26c
.PP
Packit Service a8c26c
The \fIparent\fR field points to the parent object
Packit Service a8c26c
on the search path. For convenience, a parent object is also supplied for
Packit Service a8c26c
the starting object.
Packit Service a8c26c
In this case, except for the \fIlocal\fR field which is initialized
Packit Service a8c26c
to 0 and the \fIlevel\fR field which contains a negative number,
Packit Service a8c26c
the rest of the structure may be undefined.
Packit Service a8c26c
.PP
Packit Service a8c26c
The \fIinfo\fR field indicates the type of the object
Packit Service a8c26c
being visited and the type of the visit. The types are:
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_D: A directory being visited in preorder, i.e.,
Packit Service a8c26c
none of its children has been visited by the search.
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_DNX: A directory being visited in preorder that does not have
Packit Service a8c26c
search permission.
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_DP: A directory being visited in postorder, i.e., all of its
Packit Service a8c26c
descendants have been completely processed.
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_DC: A directory that causes cycles. This is a terminal object.
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_DNR: A directory that cannot be opened for reading. This is a terminal object.
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_F: An ordinary file.
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_SL: A symbolic link.
Packit Service a8c26c
Unless FTW_FOLLOW (below) is issued by the user function,
Packit Service a8c26c
this object is terminal.
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_NS: \fIStat\fR failed on this object.
Packit Service a8c26c
The stat buffer \fIstatb\fR is undefined.
Packit Service a8c26c
This object is terminal.
Packit Service a8c26c
.PP
Packit Service a8c26c
The \fIstatus\fR field of \fIstruct FTW\fR is used to communicate information
Packit Service a8c26c
between \fIftwalk\fR and \fIuserf\fR. On calls to \fIuserf\fR, it has one of
Packit Service a8c26c
two values:
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_NAME: The name of the object as defined in \fIftw->name\fR should be used for
Packit Service a8c26c
accessing its file information. This is because \fIchdir\fR(2) has been used
Packit Service a8c26c
to set the current directory to a suitable place (see FTW_CHDIR).
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_PATH: The argument \fIpath\fR of \fIuserf\fR should be used
Packit Service a8c26c
for accessing the file information of the object.
Packit Service a8c26c
.PP
Packit Service a8c26c
Upon returning, \fIuserf\fR may set the \fIstatus\fR field
Packit Service a8c26c
to one of the following values:
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_AGAIN: If this is a directory object being visited in postorder,
Packit Service a8c26c
it will be processed \fIagain\fR as if it had not been visited.
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_NOPOST: If this is a directory object being visited in preorder,
Packit Service a8c26c
the user function will not be called on its postorder visit.
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_SKIP: This object and its descendants are pruned from the search.
Packit Service a8c26c
.IP
Packit Service a8c26c
FTW_FOLLOW: If this object is a symbolic link,
Packit Service a8c26c
follow the link to its physical counterpart.
Packit Service a8c26c
.PP
Packit Service a8c26c
\fIComparf\fR, if not NULL, is a pointer to a function
Packit Service a8c26c
used to define a search ordering for children of a directory.
Packit Service a8c26c
If FTW_CHILDREN is turned on, the ordering of the children of
Packit Service a8c26c
a directory is done before the preorder call to \fIuserf\fR on that directory.
Packit Service a8c26c
Therefore, in that case, \fIftw->link\fR will point to the smallest child.
Packit Service a8c26c
.PP
Packit Service a8c26c
The \fIcomparf\fP prototype is:
Packit Service a8c26c
.PP
Packit Service a8c26c
.nf
Packit Service a8c26c
	int comparf(struct FTW* ftw1, struct FTW* ftw2)
Packit Service a8c26c
.fi
Packit Service a8c26c
.PP
Packit Service a8c26c
\fIComparf\fR should return a value <0, 0, or >0 to indicate whether
Packit Service a8c26c
\fIftw1\fR is considered smaller, equal, or larger than \fIftw2\fR.
Packit Service a8c26c
.PP
Packit Service a8c26c
\fIFtwalk\fR normally returns 0.
Packit Service a8c26c
On hard errors such as running out of memory, it returns -1.
Packit Service a8c26c
\fIFtwalk\fR may also return other values as discussed with respect
Packit Service a8c26c
to \fIuserf\fR. 
Packit Service a8c26c
.PP
Packit Service a8c26c
\fIFtwflags\fR returns a combination of \fB0, FTW_META, FTW_PHYSICAL\fR
Packit Service a8c26c
according to the
Packit Service a8c26c
preferences specified by
Packit Service a8c26c
\fBastconf("PATH_RESOLVE",0,0)\fR:
Packit Service a8c26c
.TP
Packit Service a8c26c
.B logical
Packit Service a8c26c
0
Packit Service a8c26c
.TP
Packit Service a8c26c
.B metaphysical
Packit Service a8c26c
.B "FTW_META|FTW_PHYSICAL"
Packit Service a8c26c
.TP
Packit Service a8c26c
.B physical
Packit Service a8c26c
.B FTW_PHYSICAL
Packit Service a8c26c
.SH HISTORY
Packit Service a8c26c
\fIFtwalk\fR performs similar functions as that of
Packit Service a8c26c
the routine \fIftw\fR provided in System V.
Packit Service a8c26c
However, it is more general than \fIftw\fR
Packit Service a8c26c
and suitable for use as a base in implementing
Packit Service a8c26c
popular tools such as \fIls, find, tar, du,\fR and \fIrm\fR.
Packit Service a8c26c
\fIFtwalk\fR also handles symbolic links and hard links gracefully.
Packit Service a8c26c
.SH AUTHORS
Packit Service a8c26c
Phong Vo, Glenn Fowler, Dave Korn
Packit Service a8c26c
.SH SEE ALSO
Packit Service a8c26c
find(1), rm(1), du(1), ls(1), tar(1), stat(2), symlink(2),
Packit Service a8c26c
astfeature(3), ftw(3), pathcd(3)