1 .\" $NetBSD: fts.3,v 1.13.2.1 1997/11/14 02:09:32 mrg Exp $
3 .\" Copyright (c) 1989, 1991, 1993, 1994
4 .\" The Regents of the University of California. All rights reserved.
6 .\" SPDX-License-Identifier: BSD-4-Clause-UC
8 .\" @(#)fts.3 8.5 (Berkeley) 4/16/94
10 .\" 2007-12-08, mtk, Converted from mdoc to man macros
12 .TH FTS 3 2021-03-22 "Linux" "Linux Programmer's Manual"
14 fts, fts_open, fts_read, fts_children, fts_set, fts_close \- \
15 traverse a file hierarchy
18 .B #include <sys/types.h>
19 .B #include <sys/stat.h>
22 .BI "FTS *fts_open(char * const *" path_argv ", int " options ,
23 .BI " int (*" compar ")(const FTSENT **, const FTSENT **));"
25 .BI "FTSENT *fts_read(FTS *" ftsp );
27 .BI "FTSENT *fts_children(FTS *" ftsp ", int " instr );
29 .BI "int fts_set(FTS *" ftsp ", FTSENT *" f ", int " instr );
31 .BI "int fts_close(FTS *" ftsp );
35 fts functions are provided for traversing
37 A simple overview is that the
39 function returns a "handle" (of type
41 that refers to a file hierarchy "stream".
42 This handle is then supplied to the other
46 returns a pointer to a structure describing one of the files in the file
50 returns a pointer to a linked list of structures, each of which describes
51 one of the files contained in a directory in the hierarchy.
53 In general, directories are visited two distinguishable times; in preorder
54 (before any of their descendants are visited) and in postorder (after all
55 of their descendants have been visited).
56 Files are visited once.
57 It is possible to walk the hierarchy "logically" (visiting the files that
58 symbolic links point to)
59 or physically (visiting the symbolic links themselves),
60 order the walk of the hierarchy or
61 prune and/or revisit portions of the hierarchy.
63 Two structures (and associated types) are defined in the include file
67 the structure that represents the file hierarchy itself.
70 the structure that represents a file in the file
74 structure is returned for every file in the file
76 In this manual page, "file" and
78 are generally interchangeable.
82 structure contains fields describing a file.
83 The structure contains at least the following fields
84 (there are additional fields that
85 should be considered private to the implementation):
89 typedef struct _ftsent {
90 unsigned short fts_info; /* flags for FTSENT structure */
91 char *fts_accpath; /* access path */
92 char *fts_path; /* root path */
93 short fts_pathlen; /* strlen(fts_path) +
95 char *fts_name; /* filename */
96 short fts_namelen; /* strlen(fts_name) */
97 short fts_level; /* depth (\-1 to N) */
98 int fts_errno; /* file errno */
99 long fts_number; /* local numeric value */
100 void *fts_pointer; /* local address value */
101 struct _ftsent *fts_parent; /* parent directory */
102 struct _ftsent *fts_link; /* next file structure */
103 struct _ftsent *fts_cycle; /* cycle structure */
104 struct stat *fts_statp; /* stat(2) information */
106 .\" ino_t fts_ino; /* inode (only for directories)*/
107 .\" dev_t fts_dev; /* device (only for directories)*/
108 .\" nlink_t fts_nlink; /* link count (only for directories)*/
109 .\" u_short fts_flags; /* private flags for FTSENT structure */
110 .\" u_short fts_instr; /* fts_set() instructions */
115 These fields are defined as follows:
116 .\" .Bl -tag -width "fts_namelen"
119 One of the following values describing the returned
122 the file it represents.
123 With the exception of directories without errors
126 entries are terminal, that is, they will not be revisited, nor will any
127 of their descendants be visited.
128 .\" .Bl -tag -width FTS_DEFAULT
132 A directory being visited in preorder.
135 A directory that causes a cycle in the tree.
140 structure will be filled in as well.)
145 structure that represents a file type not explicitly described
151 A directory which cannot be read.
152 This is an error return, and the
154 field will be set to indicate what caused the error.
161 which was not specified as a filename to
167 A directory being visited in postorder.
170 structure will be unchanged from when
171 it was returned in preorder, that is, with the
177 This is an error return, and the
179 field will be set to indicate what caused the error.
187 information was available.
191 This is an error return, and the
193 field will be set to indicate what caused the error.
198 information was requested.
207 A symbolic link with a nonexistent target.
210 field reference the file characteristic information for the symbolic link
216 A path for accessing the file from the current directory.
219 The path for the file relative to the root of the traversal.
220 This path contains the path specified to
225 The sum of the lengths of the strings referenced by
231 The name of the file.
234 The length of the string referenced by
238 The depth of the traversal, numbered from \-1 to N, where this file
242 structure representing the parent of the starting point (or root)
243 of the traversal is numbered \-1, and the
245 structure for the root
246 itself is numbered 0.
264 field contains the error number (i.e., the
267 specifying the cause of the error.
268 Otherwise, the contents of the
273 This field is provided for the use of the application program and is
276 It is initialized to 0.
279 This field is provided for the use of the application program and is
288 structure referencing the file in the hierarchy
289 immediately above the current file, that is, the directory of which this
291 A parent structure for the initial entry point is provided as well,
297 fields are guaranteed to be initialized.
304 field points to the next structure in the NULL-terminated linked list of
306 Otherwise, the contents of the
311 If a directory causes a cycle in the hierarchy (see
314 of a hard link between two directories, or a symbolic link pointing to a
317 field of the structure will point to the
319 structure in the hierarchy that references the same file as the current
322 Otherwise, the contents of the
329 information for the file.
332 A single buffer is used for all of the paths of all of the files in the
338 fields are guaranteed to be
341 for the file most recently returned by
343 To use these fields to reference any files represented by other
345 structures will require that the path buffer be modified using the
346 information contained in that
351 Any such modifications should be undone before further calls to
361 function takes a pointer to an array of character pointers naming one
362 or more paths which make up a logical file hierarchy to be traversed.
363 The array must be terminated by a
367 a number of options, at least one of which (either
372 The options are selected by ORing
373 the following values:
374 .\" .Bl -tag -width "FTS_PHYSICAL"
377 This option causes any symbolic link specified as a root path to be
378 followed immediately whether or not
383 This option causes the
384 fts routines to return
386 structures for the targets of symbolic links
387 instead of the symbolic links themselves.
388 If this option is set, the only symbolic links for which
391 are returned to the application are those referencing nonexistent files.
402 As a performance optimization, the
403 fts functions change directories as they walk the file hierarchy.
404 This has the side-effect that an application cannot rely on being
405 in any particular directory during the traversal.
408 option turns off this optimization, and the
409 fts functions will not change the current directory.
410 Note that applications should not themselves change their current directory
411 and try to access files unless
413 is specified and absolute
414 pathnames were provided as arguments to
420 structures reference file characteristic information (the
422 field) for each file visited.
423 This option relaxes that requirement as a performance optimization,
425 fts functions to set the
429 and leave the contents of the
434 This option causes the
435 fts routines to return
437 structures for symbolic links themselves instead
438 of the target files they point to.
439 If this option is set,
441 structures for all symbolic links in the
442 hierarchy are returned to the application.
453 By default, unless they are specified as path arguments to
459 encountered in the file hierarchy are ignored.
460 This option causes the
461 fts routines to return
467 fts from descending into directories that have a different device number
468 than the file from which the descent began.
473 specifies a user-defined function which may be used to order the traversal
476 takes two pointers to pointers to
478 structures as arguments and
479 should return a negative value, zero, or a positive value to indicate
480 if the file referenced by its first argument comes before, in any order
481 with respect to, or after, the file referenced by its second argument.
491 be used in this comparison.
500 field may not either.
505 the directory traversal order is in the order listed in
507 for the root paths, and in the order listed in the directory for
512 function returns a pointer to an
514 structure describing a file in
516 Directories (that are readable and do not cause cycles) are visited at
517 least twice, once in preorder and once in postorder.
518 All other files are visited at least once.
519 (Hard links between directories that do not cause cycles or symbolic
520 links to symbolic links may cause files to be visited more than once,
521 or directories more than twice.)
523 If all the members of the hierarchy have been returned,
525 returns NULL and sets
528 If an error unrelated to a file in the hierarchy occurs,
534 to indicate the error.
535 If an error related to a returned file occurs, a pointer to an
537 structure is returned, and
539 may or may not have been set (see
544 structures returned by
546 may be overwritten after a call to
548 on the same file hierarchy stream, or, after a call to
550 on the same file hierarchy stream unless they represent a file of type
551 directory, in which case they will not be overwritten until after a call to
555 structure has been returned by the function
561 function returns a pointer to an
563 structure describing the first entry in a NULL-terminated linked list of
564 the files in the directory represented by the
566 structure most recently returned by
568 The list is linked through the
572 structure, and is ordered by the user-specified comparison function, if any.
575 will re-create this linked list.
577 As a special case, if
579 has not yet been called for a hierarchy,
581 will return a pointer to the files in the logical directory specified to
583 that is, the arguments specified to
587 structure most recently returned by
589 is not a directory being visited in preorder,
590 or the directory does not contain any files,
603 to indicate the error.
607 structures returned by
609 may be overwritten after a call to
614 on the same file hierarchy stream.
618 argument is either zero or the following value:
619 .\" .Bl -tag -width FTS_NAMEONLY
622 Only the names of the files are needed.
623 The contents of all the fields in the returned linked list of structures
624 are undefined with the exception of the
633 allows the user application to determine further processing for the
641 returns 0 on success, and \-1 if an error occurs.
645 argument is either 0 (meaning "do nothing") or one of the following values:
646 .\" .Bl -tag -width FTS_PHYSICAL
649 Revisit the file; any file type may be revisited.
652 will return the referenced file.
657 fields of the structure will be reinitialized at that time,
658 but no other fields will have been changed.
659 This option is meaningful only for the most recently returned
662 Normal use is for postorder directory visits, where it causes the
663 directory to be revisited (in both preorder and postorder) as well as all
667 The referenced file must be a symbolic link.
668 If the referenced file is the one most recently returned by
672 returns the file with the
676 fields reinitialized to reflect the target of the symbolic link instead
677 of the symbolic link itself.
678 If the file is one of those most recently returned by
684 fields of the structure, when returned by
686 will reflect the target of the symbolic link instead of the symbolic link
688 In either case, if the target of the symbolic link does not exist, the
689 fields of the returned structure will be unchanged and the
694 If the target of the link is a directory, the preorder return, followed
695 by the return of all of its descendants, followed by a postorder return,
699 No descendants of this file are visited.
700 The file may be one of those most recently returned by either
708 function closes the file hierarchy stream referred to by
710 and restores the current directory to the directory from which
717 returns 0 on success, and \-1 if an error occurs.
723 for any of the errors specified for
732 for any of the errors specified for
743 for any of the errors specified for
766 These functions are available in Linux since glibc2.
768 For an explanation of the terms used in this section, see
776 Interface Attribute Value
781 T} Thread safety MT-Safe
785 T} Thread safety MT-Unsafe
793 In versions of glibc before 2.23,
794 .\" Fixed by commit 8b7b7f75d91f7bac323dd6a370aeb3e9c5c4a7d5
795 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=15838
796 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=11460
797 all of the APIs described in this man page are not safe when compiling
798 a program using the LFS APIs (e.g., when compiling with
799 .IR \-D_FILE_OFFSET_BITS=64 ).
801 .\" The following statement is years old, and seems no closer to
802 .\" being true -- mtk
805 .\" utility is expected to be included in a future