| FTWALK(3) | C LIBRARY FUNCTIONS | FTWALK(3) |
|---|
#include <ftwalk.h> int ftwalk(char* path, int (*userf)(struct FTW* ftw), int options,int (*comparf)(struct FTW* ftw1, struct FTW* ftw2));int ftwflags(void);
Ftwalk traverses a directory hierarchy using depth-first search. Upon visiting each file or directory in the hierarchy, it calls the user function userf to process that file or directory. On a directory object, userf may be called twice, once in preorder and once in postorder. On a terminal object such as a file or an unreadable directory, userf is called only once. Cycles due to hard links or symbolic links are detected to avoid infinite loops.
Path is the starting point of the search. It may be an absolute path name or a path name relative to the current directory. If path is a null pointer or points to an empty string, it is treated as if it points to the current (dot) directory.
Options consists of zero or more of the following bits:
Userf is a user supplied function that is called upon different visits of an object. If the return value of userf is non-zero, ftwalk returns immediately with the same value. The userf prototype is:
int userf(struct FTW* ftw)
struct FTW contains at least the following elements:
struct FTW* link; /* link list of children */
struct FTW* parent; /* parent object on the search path */
union
{
long number; /* local number */
void* pointer; /* local pointer */
} local; /* user defined */
struct stat statb; /* stat buffer of this object */
char* path; /* full pathname */
short pathlen; /* strlen(path) */
unsigned short info; /* type of object */
unsigned short status; /* status of object */
short level; /* depth of object on the search path */
short namelen; /* strlen(name) */
char name[]; /* file name of object */
The link field is normally NULL. If the option FTW_CHILDREN was turned on, it points to the start of the list of children of the directory being visited in preorder. Finally, if the directory being visited causes a cycle, link points to the object on the search path that is identical to this directory. Note that if FTW_PHYSICAL was turned on, this may point to a directory that is an ancestor of the starting object.
The parent field points to the parent object on the search path. For convenience, a parent object is also supplied for the starting object. In this case, except for the local field which is initialized to 0 and the level field which contains a negative number, the rest of the structure may be undefined.
The info field indicates the type of the object being visited and the type of the visit. The types are:
The status field of struct FTW is used to communicate information between ftwalk and userf. On calls to userf, it has one of two values:
Upon returning, userf may set the status field to one of the following values:
Comparf, if not NULL, is a pointer to a function used to define a search ordering for children of a directory. If FTW_CHILDREN is turned on, the ordering of the children of a directory is done before the preorder call to userf on that directory. Therefore, in that case, ftw->link will point to the smallest child.
The comparf prototype is:
int comparf(struct FTW* ftw1, struct FTW* ftw2)
Comparf should return a value <0, 0, or >0 to indicate whether ftw1 is considered smaller, equal, or larger than ftw2.
Ftwalk normally returns 0. On hard errors such as running out of memory, it returns -1. Ftwalk may also return other values as discussed with respect to userf.
Ftwflags returns a combination of 0, FTW_META, FTW_PHYSICAL according to the preferences specified by astconf("PATH_RESOLVE",0,0):
| November 07, 2006 |