fts(3) Library Functions Manual fts(3) fts, fts_open, fts_read, fts_children, fts_set, fts_close - C (libc, -lc) #include #include #include FTS *fts_open(char *const *path_argv, int options, int (*_Nullable compar)(const FTSENT **, const FTSENT **)); FTSENT *fts_read(FTS *ftsp); FTSENT *fts_children(FTS *ftsp, int instr); int fts_set(FTS *ftsp, FTSENT *f, int instr); int fts_close(FTS *ftsp); fts . : fts_open() <<>> ( FTS *), <<>> . fts. fts_read() , . fts_children() , , . , -- ( , ) ( , ). . <<>> ( , ) <<>> ( ), - . ( ). -- FTS, . -- FTSENT, . , FTSENT . <<>> << FTSENT>> . FTSENT , . , , ( , ): typedef struct _ftsent { unsigned short fts_info; /* flags for FTSENT structure */ char *fts_accpath; /* access path */ char *fts_path; /* root path */ short fts_pathlen; /* strlen(fts_path) + strlen(fts_name) */ char *fts_name; /* filename */ 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; /* [l]stat(2) information */ } FTSENT; : fts_info FTSENT , . (FTS_D), , , . FTS_D , . FTS_DC , ( fts_cycle FTSENT). FTS_DEFAULT FTSENT, , fts_info. FTS_DNR , . , fts_errno , . FTS_DOT <<.>> <<..>>, fts_open() ( FTS_SEEDOT). FTS_DP , . FTSENT , , fts_info FTS_D. FTS_ERR , fts_errno , . FTS_F . FTS_NS A file for which no [l]stat(2) information was available. The contents of the fts_statp field are undefined. This is an error return, and the fts_errno field will be set to indicate what caused the error. FTS_NSOK A file for which no [l]stat(2) information was requested. The contents of the fts_statp field are undefined. FTS_SL . FTS_SLNONE , . fts_statp . fts_accpath . fts_path . ( ), fts_open(). fts_pathlen , fts_path fts_name. fts_name . fts_namelen , fts_name. fts_level , -1 N, . FTSENT, ( ), -1, FTSENT 0. fts_errno FTSENT fts_children() fts_read() fts_info FTS_DNR, FTS_ERR FTS_NS, fts_errno ( errno), . , fts_errno . fts_number fts. 0. fts_pointer fts. NULL. fts_parent FTSENT, , , . , fts_level, fts_number fts_pointer. fts_link fts_children() fts_link ( NULL) . fts_link . fts_cycle ( FTS_DC), - , - , , fts_cycle FTSENT , , FTSENT. fts_cycle . fts_statp A pointer to [l]stat(2) information for the file. . , fts_path fts_accpath null , fts_read() . , FTSENT , , fts_pathlen FTSENT. fts_read(). fts_name null. fts_open() fts_open() , , , . null. , ( FTS_LOGICAL, FTS_PHYSICAL). , , : FTS_LOGICAL This option causes the fts routines to return 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 FTSENT structures are returned to the application are those referencing nonexistent files: the fts_statp field is obtained via stat(2) with a fallback to lstat(2). FTS_PHYSICAL This option causes the fts routines to return FTSENT structures for symbolic links themselves instead of the target files they point to. If this option is set, FTSENT structures for all symbolic links in the hierarchy are returned to the application: the fts_statp field is obtained via lstat(2). FTS_COMFOLLOW This option causes any symbolic link specified as a root path to be followed immediately, as if via FTS_LOGICAL, regardless of the primary mode. FTS_NOCHDIR As a performance optimization, the 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. This option turns off this optimization, and the fts functions will not change the current directory. Note that applications should not themselves change their current directory and try to access files unless FTS_NOCHDIR is specified and absolute pathnames were provided as arguments to fts_open(). FTS_NOSTAT By default, returned FTSENT structures reference file characteristic information (the fts_statp field) for each file visited. This option relaxes that requirement as a performance optimization, allowing the fts functions to set the fts_info field to FTS_NSOK and leave the contents of the fts_statp field undefined. FTS_SEEDOT , <<.>> <<..>>, , , fts_open(). fts FTSENT. FTS_XDEV fts , , , . compar() , . FTSENT, , , , , , ( ), , . fts_accpath, fts_path fts_pathlen FTSENT . fts_info FTS_NS FTS_NSOK, fts_statp . compar() NULL, , path_argv , , , . fts_read() fts_read() FTSENT, . ( ), -- . ( , , , , ). If all the members of the hierarchy have been returned, fts_read() returns NULL and sets errno to 0. If an error unrelated to a file in the hierarchy occurs, fts_read() returns NULL and sets errno to indicate the error. If an error related to a returned file occurs, a pointer to an FTSENT structure is returned, and errno may or may not have been set (see fts_info). FTSENT, fts_read(), fts_close() fts_read() , <<>>; , fts_read() FTSENT . fts_children() fts_children() FTSENT, ( NULL) , FTSENT, fts_read() . fts_link FTSENT, , . fts_children() . As a special case, if fts_read() has not yet been called for a hierarchy, fts_children() will return a pointer to the files in the logical directory specified to fts_open(), that is, the arguments specified to fts_open(). Otherwise, if the FTSENT structure most recently returned by fts_read() is not a directory being visited in preorder, or the directory does not contain any files, fts_children() returns NULL and sets errno to zero. If an error occurs, fts_children() returns NULL and sets errno to indicate the error. FTSENT, fts_children(), fts_children(), fts_close() fts_read() . instr : FTS_NAMEONLY . , fts_name fts_namelen. fts_set() fts_set() f ftsp. fts_set() 0 -1 . instr 0 (<< >>) : FTS_AGAIN ; . fts_read() , . fts_stat fts_info , . fts_read(). ; ( ), . FTS_FOLLOW . -- fts_read(), fts_read() fts_info fts_statp, , . -- fts_children(), fts_info fts_statp fts_read() , . , , , fts_info FTS_SLNONE. -- , , , . FTS_SKIP . fts_children(), fts_read(). fts_close() fts_close() , ftsp, , fts_open() ftsp. fts_close() 0 -1 . fts_open() errno , open(2) malloc(3). In addition, fts_open() may fail and set errno as follows: ENOENT Any element of path_argv was an empty string. fts_close() errno , chdir(2) close(2). The functions fts_read() and fts_children() may fail and set errno for any of the errors specified for chdir(2), malloc(3), opendir(3), readdir(3), and [l]stat(2). , fts_children(), fts_open() fts_set() errno : EINVAL options instr. attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |fts_open(), fts_set(), | | MT-Safe | |fts_close() | | | +----------------------------+----------------------------------------------------------+--------------------------+ |fts_read(), fts_children() | | MT-Unsafe | +----------------------------+----------------------------------------------------------+--------------------------+ . glibc 2. 4.4BSD. Before glibc 2.23, all of the APIs described in this man page are not safe when compiling a program using the LFS APIs (e.g., when compiling with -D_FILE_OFFSET_BITS=64). find(1), chdir(2), lstat(2), stat(2), ftw(3), qsort(3) () Azamat Hackimov , Dmitry Bolkhovskikh , Yuri Kozlov , Kirill Rekhov ; GNU (GNU General Public License - GPL, 3 ) , - . - , , () () () <>. Linux 6.9.1 2 2024 . fts(3)