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 man-pages (unreleased)" "Linux Programmer's Manual"
14 fts, fts_open, fts_read, fts_children, fts_set, fts_close \- \
15 traverse a file hierarchy
18 .RI ( libc ", " \-lc )
21 .B #include <sys/types.h>
22 .B #include <sys/stat.h>
25 .BI "FTS *fts_open(char * const *" path_argv ", int " options ,
26 .BI " int (*" compar ")(const FTSENT **, const FTSENT **));"
28 .BI "FTSENT *fts_read(FTS *" ftsp );
30 .BI "FTSENT *fts_children(FTS *" ftsp ", int " instr );
32 .BI "int fts_set(FTS *" ftsp ", FTSENT *" f ", int " instr );
34 .BI "int fts_close(FTS *" ftsp );
38 fts functions are provided for traversing
40 A simple overview is that the
42 function returns a "handle" (of type
44 that refers to a file hierarchy "stream".
45 This handle is then supplied to the other
49 returns a pointer to a structure describing one of the files in the file
53 returns a pointer to a linked list of structures, each of which describes
54 one of the files contained in a directory in the hierarchy.
56 In general, directories are visited two distinguishable times; in preorder
57 (before any of their descendants are visited) and in postorder (after all
58 of their descendants have been visited).
59 Files are visited once.
60 It is possible to walk the hierarchy "logically" (visiting the files that
61 symbolic links point to)
62 or physically (visiting the symbolic links themselves),
63 order the walk of the hierarchy or
64 prune and/or revisit portions of the hierarchy.
66 Two structures (and associated types) are defined in the include file
70 the structure that represents the file hierarchy itself.
73 the structure that represents a file in the file
77 structure is returned for every file in the file
79 In this manual page, "file" and
81 are generally interchangeable.
85 structure contains fields describing a file.
86 The structure contains at least the following fields
87 (there are additional fields that
88 should be considered private to the implementation):
92 typedef struct _ftsent {
93 unsigned short fts_info; /* flags for FTSENT structure */
94 char *fts_accpath; /* access path */
95 char *fts_path; /* root path */
96 short fts_pathlen; /* strlen(fts_path) +
98 char *fts_name; /* filename */
99 short fts_namelen; /* strlen(fts_name) */
100 short fts_level; /* depth (\-1 to N) */
101 int fts_errno; /* file errno */
102 long fts_number; /* local numeric value */
103 void *fts_pointer; /* local address value */
104 struct _ftsent *fts_parent; /* parent directory */
105 struct _ftsent *fts_link; /* next file structure */
106 struct _ftsent *fts_cycle; /* cycle structure */
107 struct stat *fts_statp; /* [l]stat(2) information */
109 .\" ino_t fts_ino; /* inode (only for directories)*/
110 .\" dev_t fts_dev; /* device (only for directories)*/
111 .\" nlink_t fts_nlink; /* link count (only for directories)*/
112 .\" u_short fts_flags; /* private flags for FTSENT structure */
113 .\" u_short fts_instr; /* fts_set() instructions */
118 These fields are defined as follows:
119 .\" .Bl -tag -width "fts_namelen"
122 One of the following values describing the returned
125 the file it represents.
126 With the exception of directories without errors
129 entries are terminal, that is, they will not be revisited, nor will any
130 of their descendants be visited.
131 .\" .Bl -tag -width FTS_DEFAULT
135 A directory being visited in preorder.
138 A directory that causes a cycle in the tree.
143 structure will be filled in as well.)
148 structure that represents a file type not explicitly described
154 A directory which cannot be read.
155 This is an error return, and the
157 field will be set to indicate what caused the error.
164 which was not specified as a filename to
170 A directory being visited in postorder.
173 structure will be unchanged from when
174 it was returned in preorder, that is, with the
180 This is an error return, and the
182 field will be set to indicate what caused the error.
190 information was available.
194 This is an error return, and the
196 field will be set to indicate what caused the error.
201 information was requested.
210 A symbolic link with a nonexistent target.
213 field reference the file characteristic information for the symbolic link
219 A path for accessing the file from the current directory.
222 The path for the file relative to the root of the traversal.
223 This path contains the path specified to
228 The sum of the lengths of the strings referenced by
234 The name of the file.
237 The length of the string referenced by
241 The depth of the traversal, numbered from \-1 to N, where this file
245 structure representing the parent of the starting point (or root)
246 of the traversal is numbered \-1, and the
248 structure for the root
249 itself is numbered 0.
267 field contains the error number (i.e., the
270 specifying the cause of the error.
271 Otherwise, the contents of the
276 This field is provided for the use of the application program and is
279 It is initialized to 0.
282 This field is provided for the use of the application program and is
291 structure referencing the file in the hierarchy
292 immediately above the current file, that is, the directory of which this
294 A parent structure for the initial entry point is provided as well,
300 fields are guaranteed to be initialized.
307 field points to the next structure in the NULL-terminated linked list of
309 Otherwise, the contents of the
314 If a directory causes a cycle in the hierarchy (see
317 of a hard link between two directories, or a symbolic link pointing to a
320 field of the structure will point to the
322 structure in the hierarchy that references the same file as the current
325 Otherwise, the contents of the
332 information for the file.
335 A single buffer is used for all of the paths of all of the files in the
341 fields are guaranteed to be
344 for the file most recently returned by
346 To use these fields to reference any files represented by other
348 structures will require that the path buffer be modified using the
349 information contained in that
354 Any such modifications should be undone before further calls to
364 function takes a pointer to an array of character pointers naming one
365 or more paths which make up a logical file hierarchy to be traversed.
366 The array must be terminated by a
370 a number of options, at least one of which (either
375 The options are selected by ORing
376 the following values:
377 .\" .Bl -tag -width "FTS_PHYSICAL"
380 This option causes the
381 fts routines to return
383 structures for the targets of symbolic links
384 instead of the symbolic links themselves.
385 If this option is set, the only symbolic links for which
388 are returned to the application are those referencing nonexistent files:
391 field is obtained via
397 This option causes the
398 fts routines to return
400 structures for symbolic links themselves instead
401 of the target files they point to.
402 If this option is set,
404 structures for all symbolic links in the
405 hierarchy are returned to the application:
408 field is obtained via
412 This option causes any symbolic link specified as a root path to be
413 followed immediately, as if via
415 regardless of the primary mode.
418 As a performance optimization, the
419 fts functions change directories as they walk the file hierarchy.
420 This has the side-effect that an application cannot rely on being
421 in any particular directory during the traversal.
423 option turns off this optimization, and the
424 fts functions will not change the current directory.
425 Note that applications should not themselves change their current directory
426 and try to access files unless
428 is specified and absolute
429 pathnames were provided as arguments to
435 structures reference file characteristic information (the
437 field) for each file visited.
438 This option relaxes that requirement as a performance optimization,
440 fts functions to set the
444 and leave the contents of the
449 By default, unless they are specified as path arguments to
455 encountered in the file hierarchy are ignored.
456 This option causes the
457 fts routines to return
463 fts from descending into directories that have a different device number
464 than the file from which the descent began.
469 specifies a user-defined function which may be used to order the traversal
472 takes two pointers to pointers to
474 structures as arguments and
475 should return a negative value, zero, or a positive value to indicate
476 if the file referenced by its first argument comes before, in any order
477 with respect to, or after, the file referenced by its second argument.
487 be used in this comparison.
496 field may not either.
501 the directory traversal order is in the order listed in
503 for the root paths, and in the order listed in the directory for
508 function returns a pointer to an
510 structure describing a file in
512 Directories (that are readable and do not cause cycles) are visited at
513 least twice, once in preorder and once in postorder.
514 All other files are visited at least once.
515 (Hard links between directories that do not cause cycles or symbolic
516 links to symbolic links may cause files to be visited more than once,
517 or directories more than twice.)
519 If all the members of the hierarchy have been returned,
521 returns NULL and sets
524 If an error unrelated to a file in the hierarchy occurs,
530 to indicate the error.
531 If an error related to a returned file occurs, a pointer to an
533 structure is returned, and
535 may or may not have been set (see
540 structures returned by
542 may be overwritten after a call to
544 on the same file hierarchy stream, or, after a call to
546 on the same file hierarchy stream unless they represent a file of type
547 directory, in which case they will not be overwritten until after a call to
551 structure has been returned by the function
557 function returns a pointer to an
559 structure describing the first entry in a NULL-terminated linked list of
560 the files in the directory represented by the
562 structure most recently returned by
564 The list is linked through the
568 structure, and is ordered by the user-specified comparison function, if any.
571 will re-create this linked list.
573 As a special case, if
575 has not yet been called for a hierarchy,
577 will return a pointer to the files in the logical directory specified to
579 that is, the arguments specified to
583 structure most recently returned by
585 is not a directory being visited in preorder,
586 or the directory does not contain any files,
599 to indicate the error.
603 structures returned by
605 may be overwritten after a call to
610 on the same file hierarchy stream.
614 argument is either zero or the following value:
615 .\" .Bl -tag -width FTS_NAMEONLY
618 Only the names of the files are needed.
619 The contents of all the fields in the returned linked list of structures
620 are undefined with the exception of the
629 allows the user application to determine further processing for the
637 returns 0 on success, and \-1 if an error occurs.
641 argument is either 0 (meaning "do nothing") or one of the following values:
642 .\" .Bl -tag -width FTS_PHYSICAL
645 Revisit the file; any file type may be revisited.
648 will return the referenced file.
653 fields of the structure will be reinitialized at that time,
654 but no other fields will have been changed.
655 This option is meaningful only for the most recently returned
658 Normal use is for postorder directory visits, where it causes the
659 directory to be revisited (in both preorder and postorder) as well as all
663 The referenced file must be a symbolic link.
664 If the referenced file is the one most recently returned by
668 returns the file with the
672 fields reinitialized to reflect the target of the symbolic link instead
673 of the symbolic link itself.
674 If the file is one of those most recently returned by
680 fields of the structure, when returned by
682 will reflect the target of the symbolic link instead of the symbolic link
684 In either case, if the target of the symbolic link does not exist, the
685 fields of the returned structure will be unchanged and the
690 If the target of the link is a directory, the preorder return, followed
691 by the return of all of its descendants, followed by a postorder return,
695 No descendants of this file are visited.
696 The file may be one of those most recently returned by either
704 function closes the file hierarchy stream referred to by
706 and restores the current directory to the directory from which
713 returns 0 on success, and \-1 if an error occurs.
719 for any of the errors specified for
728 for any of the errors specified for
739 for any of the errors specified for
762 These functions are available in Linux since glibc2.
764 For an explanation of the terms used in this section, see
772 Interface Attribute Value
777 T} Thread safety MT-Safe
781 T} Thread safety MT-Unsafe
789 In versions of glibc before 2.23,
790 .\" Fixed by commit 8b7b7f75d91f7bac323dd6a370aeb3e9c5c4a7d5
791 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=15838
792 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=11460
793 all of the APIs described in this man page are not safe when compiling
794 a program using the LFS APIs (e.g., when compiling with
795 .IR \-D_FILE_OFFSET_BITS=64 ).
797 .\" The following statement is years old, and seems no closer to
798 .\" being true -- mtk
801 .\" utility is expected to be included in a future