Movatterモバイル変換


[0]ホーム

URL:


man7.org > Linux >man-pages

Linux/UNIX system programming training


fts(3) — Linux manual page

NAME |LIBRARY |SYNOPSIS |DESCRIPTION |ERRORS |ATTRIBUTES |STANDARDS |HISTORY |BUGS |SEE ALSO |COLOPHON

fts(3)                   Library Functions Manualfts(3)

NAME        top

       fts, fts_open, fts_read, fts_children, fts_set, fts_close -       traverse a file hierarchy

LIBRARY        top

       Standard C library (libc,-lc)

SYNOPSIS        top

#include <sys/types.h>#include <sys/stat.h>#include <fts.h>FTS *fts_open(char *const *path_argv, intoptions,typeof(int (const FTSENT **, const FTSENT **))*_Nullablecompar);FTSENT *fts_read(FTS *ftsp);FTSENT *fts_children(FTS *ftsp, intinstr);int fts_set(FTS *ftsp, FTSENT *f, intinstr);int fts_close(FTS *ftsp);

DESCRIPTION        top

       The fts functions are provided for traversing file hierarchies.  A       simple overview is that thefts_open() function returns a "handle"       (of typeFTS *) that refers to a file hierarchy "stream".  This       handle is then supplied to the other fts functions.  The functionfts_read() returns a pointer to a structure describing one of the       files in the file hierarchy.  The functionfts_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       preorder (before any of their descendants are visited) and in       postorder (after all of their descendants have been visited).       Files are visited once.  It is possible to walk the hierarchy       "logically" (visiting the files that symbolic links point to) or       physically (visiting the symbolic links themselves), order the       walk of the hierarchy or prune and/or revisit portions of the       hierarchy.       Two structures (and associated types) are defined in the include       file<fts.h>.  The first type isFTS, the structure that       represents the file hierarchy itself.  The second type isFTSENT,       the structure that represents a file in the file hierarchy.       Normally, anFTSENT structure is returned for every file in the       file hierarchy.  In this manual page, "file" and "FTSENT       structure" are generally interchangeable.       TheFTSENT structure contains fields describing a file.  The       structure contains at least the following fields (there are       additional fields that should be considered private to the       implementation):           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;       These fields are defined as follows:fts_info              One of the following values describing the returnedFTSENT              structure and the file it represents.  With the exception              of directories without errors (FTS_D), all of these entries              are terminal, that is, they will not be revisited, nor will              any of their descendants be visited.FTS_DA directory being visited in preorder.FTS_DCA directory that causes a cycle in the tree.  (Thefts_cycle field of theFTSENT structure will be                     filled in as well.)FTS_DEFAULT                     AnyFTSENT structure that represents a file type not                     explicitly described by one of the otherfts_info                     values.FTS_DNR                     A directory which cannot be read.  This is an error                     return, and thefts_errno field will be set to                     indicate what caused the error.FTS_DOT                     A file named "."  or ".."  which was not specified                     as a filename tofts_open() (seeFTS_SEEDOT).FTS_DPA directory being visited in postorder.  The                     contents of theFTSENT structure will be unchanged                     from when it was returned in preorder, that is, with                     thefts_info field set toFTS_D.FTS_ERR                     This is an error return, and thefts_errno field                     will be set to indicate what caused the error.FTS_FA regular file.FTS_NSA file for which no [l]stat(2) information was                     available.  The contents of thefts_statp field are                     undefined.  This is an error return, and thefts_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 thefts_statp field are                     undefined.FTS_SLA symbolic link.FTS_SLNONE                     A symbolic link with a nonexistent target.  The                     contents of thefts_statp field reference the file                     characteristic information for the symbolic link                     itself.fts_accpath              A path for accessing the file from the current directory.fts_path              The path for the file relative to the root of the              traversal.  This path contains the path specified tofts_open() as a prefix.fts_pathlen              The sum of the lengths of the strings referenced byfts_path andfts_name.fts_name              The name of the file.fts_namelen              The length of the string referenced byfts_name.fts_level              The depth of the traversal, numbered from -1 to N, where              this file was found.  TheFTSENT structure representing the              parent of the starting point (or root) of the traversal is              numbered -1, and theFTSENT structure for the root itself              is numbered 0.fts_errno              Iffts_children() orfts_read() returns anFTSENT structure              whosefts_info field is set toFTS_DNR,FTS_ERR, orFTS_NS,              thefts_errno field contains the error number (i.e., theerrno value) specifying the error.  Otherwise, the contents              of thefts_errno field are undefined.fts_number              This field is provided for the use of the application              program and is not modified by the fts functions.  It is              initialized to 0.fts_pointer              This field is provided for the use of the application              program and is not modified by the fts functions.  It is              initialized to NULL.fts_parent              A pointer to theFTSENT structure referencing the file in              the hierarchy immediately above the current file, that is,              the directory of which this file is a member.  A parent              structure for the initial entry point is provided as well,              however, only thefts_level,fts_number, andfts_pointer              fields are guaranteed to be initialized.fts_link              Upon return from thefts_children() function, thefts_link              field points to the next structure in the NULL-terminated              linked list of directory members.  Otherwise, the contents              of thefts_link field are undefined.fts_cycle              If a directory causes a cycle in the hierarchy (seeFTS_DC), either because of a hard link between two              directories, or a symbolic link pointing to a directory,              thefts_cycle field of the structure will point to theFTSENT structure in the hierarchy that references the same              file as the currentFTSENT structure.  Otherwise, the              contents of thefts_cycle field are undefined.fts_statp              A pointer to [l]stat(2) information for the file.       A single buffer is used for all of the paths of all of the files       in the file hierarchy.  Therefore, thefts_path andfts_accpath       fields are guaranteed to be null-terminatedonly for the file most       recently returned byfts_read().  To use these fields to reference       any files represented by otherFTSENT structures will require that       the path buffer be modified using the information contained in       thatFTSENT structure'sfts_pathlen field.  Any such modifications       should be undone before further calls tofts_read() are attempted.       Thefts_name field is always null-terminated.fts_open()       Thefts_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 (eitherFTS_LOGICALorFTS_PHYSICAL) must be specified.  The options are       selected by ORing the following values:FTS_LOGICAL              This option causes the fts routines to returnFTSENT              structures for the targets of symbolic links instead of the              symbolic links themselves.  If this option is set, the only              symbolic links for whichFTSENT structures are returned to              the application are those referencing nonexistent files:              thefts_statp field is obtained viastat(2) with a fallback              tolstat(2).FTS_PHYSICAL              This option causes the fts routines to returnFTSENT              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: thefts_statp field is              obtained vialstat(2).FTS_COMFOLLOW              This option causes any symbolic link specified as a root              path to be followed immediately, as if viaFTS_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 unlessFTS_NOCHDIRis specified and              absolute pathnames were provided as arguments tofts_open().FTS_NOSTAT              By default, returnedFTSENT structures reference file              characteristic information (thefts_statp field) for each              file visited.  This option relaxes that requirement as a              performance optimization, allowing the fts functions to set              thefts_info field toFTS_NSOKand leave the contents of              thefts_statp field undefined.FTS_SEEDOT              By default, unless they are specified as path arguments tofts_open(), any files named "."  or ".."  encountered in              the file hierarchy are ignored.  This option causes the fts              routines to returnFTSENT structures for them.FTS_XDEV              This option prevents fts from descending into directories              that have a different device number than the file from              which the descent began.       The argumentcompar() specifies a user-defined function which may       be used to order the traversal of the hierarchy.  It takes two       pointers to pointers toFTSENT 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.  Thefts_accpath,fts_path, andfts_pathlen fields of       theFTSENT structures maynever be used in this comparison.  If       thefts_info field is set toFTS_NSorFTS_NSOK, thefts_statp       field may not either.  If thecompar() argument is NULL, the       directory traversal order is in the order listed inpath_argv for       the root paths, and in the order listed in the directory for       everything else.fts_read()       Thefts_read() function returns a pointer to anFTSENT structure       describing a file in the hierarchy.  Directories (that are       readable and do not cause cycles) are visited at least twice, once       in preorder and once in postorder.  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,fts_read()       returns NULL and setserrno to 0.  If an error unrelated to a file       in the hierarchy occurs,fts_read() returns NULL and setserrno to       indicate the error.  If an error related to a returned file       occurs, a pointer to anFTSENT structure is returned, anderrno       may or may not have been set (seefts_info).       TheFTSENT structures returned byfts_read() may be overwritten       after a call tofts_close() on the same file hierarchy stream, or,       after a call tofts_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 tofts_read() after theFTSENT structure has been returned by the functionfts_read() in       postorder.fts_children()       Thefts_children() function returns a pointer to anFTSENT       structure describing the first entry in a NULL-terminated linked       list of the files in the directory represented by theFTSENT       structure most recently returned byfts_read().  The list is       linked through thefts_link field of theFTSENT structure, and is       ordered by the user-specified comparison function, if any.       Repeated calls tofts_children() will re-create this linked list.       As a special case, iffts_read() has not yet been called for a       hierarchy,fts_children() will return a pointer to the files in       the logical directory specified tofts_open(), that is, the       arguments specified tofts_open().  Otherwise, if theFTSENT       structure most recently returned byfts_read() is not a directory       being visited in preorder, or the directory does not contain any       files,fts_children() returns NULL and setserrno to zero.  If an       error occurs,fts_children() returns NULL and setserrno to       indicate the error.       TheFTSENT structures returned byfts_children() may be       overwritten after a call tofts_children(),fts_close(), orfts_read() on the same file hierarchy stream.       Theinstr argument is either zero or the following value: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 thefts_name andfts_namelen fields.fts_set()       The functionfts_set() allows the user application to determine       further processing for the filef of the streamftsp.  Thefts_set() function returns 0 on success, and -1 if an error       occurs.       Theinstr argument is either 0 (meaning "do nothing") or one of       the following values:FTS_AGAIN              Revisit the file; any file type may be revisited.  The next              call tofts_read() will return the referenced file.  Thefts_stat andfts_info fields of the structure will be              reinitialized at that time, but no other fields will have              been changed.  This option is meaningful only for the most              recently returned file fromfts_read().  Normal use is for              postorder directory visits, where it causes the directory              to be revisited (in both preorder and postorder) as well as              all of its descendants.FTS_FOLLOW              The referenced file must be a symbolic link.  If the              referenced file is the one most recently returned byfts_read(), the next call tofts_read() returns the file              with thefts_info andfts_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 byfts_children(), thefts_info andfts_statp fields of the structure, when returned byfts_read(), will reflect the target of the symbolic link              instead of the symbolic link itself.  In either case, if              the target of the symbolic link does not exist, the fields              of the returned structure will be unchanged and thefts_info field will be set toFTS_SLNONE.              If the target of the link is a directory, the preorder              return, followed by the return of all of its descendants,              followed by a postorder return, is done.FTS_SKIP              No descendants of this file are visited.  The file may be              one of those most recently returned by eitherfts_children() orfts_read().fts_close()       Thefts_close() function closes the file hierarchy stream referred       to byftsp and restores the current directory to the directory       from whichfts_open() was called to openftsp.  Thefts_close()       function returns 0 on success, and -1 if an error occurs.

ERRORS        top

       The functionfts_open() may fail and seterrno for any of the       errors specified foropen(2) andmalloc(3).       In addition,fts_open() may fail and seterrno as follows:ENOENTAny element ofpath_argv was an empty string.       The functionfts_close() may fail and seterrno for any of the       errors specified forchdir(2) andclose(2).       The functionsfts_read() andfts_children() may fail and seterrno       for any of the errors specified forchdir(2),malloc(3),opendir(3),readdir(3), and [l]stat(2).       In addition,fts_children(),fts_open(), andfts_set() may fail       and seterrno as follows:EINVALoptions orinstr was invalid.

ATTRIBUTES        top

       For an explanation of the terms used in this section, seeattributes(7).       ┌────────────────────────────────────┬───────────────┬───────────┐       │InterfaceAttributeValue│       ├────────────────────────────────────┼───────────────┼───────────┤       │fts_open(),fts_set(),fts_close() │ Thread safety │ MT-Safe   │       ├────────────────────────────────────┼───────────────┼───────────┤       │fts_read(),fts_children()         │ Thread safety │ MT-Unsafe │       └────────────────────────────────────┴───────────────┴───────────┘

STANDARDS        top

       None.

HISTORY        top

       glibc 2.  4.4BSD.

BUGS        top

       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).

SEE ALSO        top

find(1),chdir(2),lstat(2),stat(2),ftw(3),qsort(3)

COLOPHON        top

       This page is part of theman-pages (Linux kernel and C library       user-space interface documentation) project.  Information about       the project can be found at        ⟨https://www.kernel.org/doc/man-pages/⟩.  If you have a bug report       for this manual page, see       ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.       This page was obtained from the tarball man-pages-6.15.tar.gz       fetched from       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on       2025-08-11.  If you discover any rendering problems in this HTML       version of the page, or you believe there is a better or more up-       to-date source for the page, or you have corrections or       improvements to the information in this COLOPHON (which isnot       part of the original manual page), send a mail to       man-pages@man7.orgLinux man-pages 6.15            2025-05-17fts(3)

Pages that refer to this page:ftw(3)



HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface.

For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere.

Hosting byjambit GmbH.

Cover of TLPI


[8]ページ先頭

©2009-2025 Movatter.jp