Copyright (C) 2000-2012 |
Manpages FTSSection: C Library Functions (3)Index Return to Main Contents BSD mandoc NAMEfts fts_open fts_read fts_children fts_set fts_close - traverse a file hierarchySYNOPSISFd #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 *ftspDESCRIPTIONThe fts functions are provided for traversing 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 fts functions. The function Fn fts_read returns a pointer to a structure describing one of the files in the file hierarchy. 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.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 ``Fa FTSENT structure '' are generally interchangeable. The Fa 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:
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 Fa fts_accpath fields are guaranteed to be NULL -terminated only for the file most recently returned by 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 Fn fts_read are attempted. The Fa fts_name field is always NULL -terminated. FTS_OPENThe 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 NULL pointer.There are a number of options, at least one of which (either FTS_LOGICAL or FTS_PHYSICAL must be specified. The options are selected by or 'ing the following values:
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 never be used in this comparison. If the Fa fts_info field is set to FTS_NS or FTS_NSOK the Fa fts_statp field may not either. If the Fn compar argument is 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. FTS_READThe 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.)If all the members of the hierarchy have been returned, Fn fts_read returns NULL and sets the external variable errno to 0. If an error unrelated to a file in the hierarchy occurs, Fn fts_read returns NULL and sets errno appropriately. If an error related to a returned file occurs, a pointer to an Fa FTSENT structure is returned, and errno may or may not have been set (see Fa fts_info ) . 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. FTS_CHILDRENThe 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.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 NULL and sets errno to zero. If an error occurs, Fn fts_children returns NULL and sets errno appropriately. The Fa FTSENT structures returned by Fn fts_children may be overwritten after a call to Fn fts_children , Fn fts_close or Fn fts_read on the same file hierarchy stream. Option may be set to the following value:
FTS_SETThe function Fn fts_set allows the user application to determine further processing for the file Fa f of the stream Fa ftsp . The Fn fts_set function returns 0 on success, and -1 if an error occurs. Option must be set to one of the following values:
FTS_CLOSEThe 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.ERRORSThe function Fn fts_open may fail and set errno for any of the errors specified for the library functions open(2) and malloc(3).The function Fn fts_close may fail and set errno for any of the errors specified for the library functions chdir(2) and close(2). The functions Fn fts_read and Fn fts_children may fail and set errno for any of the errors specified for the library functions chdir(2), malloc(3), opendir(3), readdir(3) and stat(2). In addition, Fn fts_children , Fn fts_open and Fn fts_set may fail and set errno as follows:
SEE ALSOfind(1), chdir(2), stat(2), qsort(3)CONFORMING TOBSD 4.4. The fts utility is expected to be included in a future St -p1003.1-88 revision.AVAILABILITYThese functions are available in Linux since glibc2.
Index
This document was created by man2html, using the manual pages. Time: 11:08:59 GMT, March 29, 2024 |