2 .\" $NetBSD: fts.3,v 1.13.2.1 1997/11/14 02:09:32 mrg Exp $
4 .\" Copyright (c) 1989, 1991, 1993, 1994
5 .\" The Regents of the University of California. All rights reserved.
7 .\" SPDX-License-Identifier: BSD-4-Clause-UC
9 .\" @(#)fts.3 8.5 (Berkeley) 4/16/94
11 .\" 2007-12-08, mtk, Converted from mdoc to man macros
13 .TH fts 3 (date) "Linux man-pages (unreleased)"
15 fts, fts_open, fts_read, fts_children, fts_set, fts_close \- \
16 traverse a file hierarchy
19 .RI ( libc ", " \-lc )
22 .B #include <sys/types.h>
23 .B #include <sys/stat.h>
26 .BI "FTS *fts_open(char *const *" path_argv ", int " options ,
27 .BI " int (*_Nullable " compar ")(const FTSENT **, const FTSENT **));"
29 .BI "FTSENT *fts_read(FTS *" ftsp );
31 .BI "FTSENT *fts_children(FTS *" ftsp ", int " instr );
33 .BI "int fts_set(FTS *" ftsp ", FTSENT *" f ", int " instr );
35 .BI "int fts_close(FTS *" ftsp );
39 fts functions are provided for traversing
41 A simple overview is that the
43 function returns a "handle" (of type
45 that refers to a file hierarchy "stream".
46 This handle is then supplied to the other
50 returns a pointer to a structure describing one of the files in the file
54 returns a pointer to a linked list of structures, each of which describes
55 one of the files contained in a directory in the hierarchy.
57 In general, directories are visited two distinguishable times; in preorder
58 (before any of their descendants are visited) and in postorder (after all
59 of their descendants have been visited).
60 Files are visited once.
61 It is possible to walk the hierarchy "logically" (visiting the files that
62 symbolic links point to)
63 or physically (visiting the symbolic links themselves),
64 order the walk of the hierarchy or
65 prune and/or revisit portions of the hierarchy.
67 Two structures (and associated types) are defined in the include file
71 the structure that represents the file hierarchy itself.
74 the structure that represents a file in the file
78 structure is returned for every file in the file
80 In this manual page, "file" and
82 are generally interchangeable.
86 structure contains fields describing a file.
87 The structure contains at least the following fields
88 (there are additional fields that
89 should be considered private to the implementation):
93 typedef struct _ftsent {
94 unsigned short fts_info; /* flags for FTSENT structure */
95 char *fts_accpath; /* access path */
96 char *fts_path; /* root path */
97 short fts_pathlen; /* strlen(fts_path) +
99 char *fts_name; /* filename */
100 short fts_namelen; /* strlen(fts_name) */
101 short fts_level; /* depth (\-1 to N) */
102 int fts_errno; /* file errno */
103 long fts_number; /* local numeric value */
104 void *fts_pointer; /* local address value */
105 struct _ftsent *fts_parent; /* parent directory */
106 struct _ftsent *fts_link; /* next file structure */
107 struct _ftsent *fts_cycle; /* cycle structure */
108 struct stat *fts_statp; /* [l]stat(2) information */
110 .\" ino_t fts_ino; /* inode (only for directories)*/
111 .\" dev_t fts_dev; /* device (only for directories)*/
112 .\" nlink_t fts_nlink; /* link count (only for directories)*/
113 .\" u_short fts_flags; /* private flags for FTSENT structure */
114 .\" u_short fts_instr; /* fts_set() instructions */
119 These fields are defined as follows:
120 .\" .Bl -tag -width "fts_namelen"
123 One of the following values describing the returned
126 the file it represents.
127 With the exception of directories without errors
130 entries are terminal, that is, they will not be revisited, nor will any
131 of their descendants be visited.
132 .\" .Bl -tag -width FTS_DEFAULT
136 A directory being visited in preorder.
139 A directory that causes a cycle in the tree.
144 structure will be filled in as well.)
149 structure that represents a file type not explicitly described
155 A directory which cannot be read.
156 This is an error return, and the
158 field will be set to indicate what caused the error.
165 which was not specified as a filename to
171 A directory being visited in postorder.
174 structure will be unchanged from when
175 it was returned in preorder, that is, with the
181 This is an error return, and the
183 field will be set to indicate what caused the error.
191 information was available.
195 This is an error return, and the
197 field will be set to indicate what caused the error.
202 information was requested.
211 A symbolic link with a nonexistent target.
214 field reference the file characteristic information for the symbolic link
220 A path for accessing the file from the current directory.
223 The path for the file relative to the root of the traversal.
224 This path contains the path specified to
229 The sum of the lengths of the strings referenced by
235 The name of the file.
238 The length of the string referenced by
242 The depth of the traversal, numbered from \-1 to N, where this file
246 structure representing the parent of the starting point (or root)
247 of the traversal is numbered \-1, and the
249 structure for the root
250 itself is numbered 0.
268 field contains the error number (i.e., the
271 specifying the cause of the error.
272 Otherwise, the contents of the
277 This field is provided for the use of the application program and is
280 It is initialized to 0.
283 This field is provided for the use of the application program and is
292 structure referencing the file in the hierarchy
293 immediately above the current file, that is, the directory of which this
295 A parent structure for the initial entry point is provided as well,
301 fields are guaranteed to be initialized.
308 field points to the next structure in the NULL-terminated linked list of
310 Otherwise, the contents of the
315 If a directory causes a cycle in the hierarchy (see
318 of a hard link between two directories, or a symbolic link pointing to a
321 field of the structure will point to the
323 structure in the hierarchy that references the same file as the current
326 Otherwise, the contents of the
333 information for the file.
336 A single buffer is used for all of the paths of all of the files in the
342 fields are guaranteed to be
345 for the file most recently returned by
347 To use these fields to reference any files represented by other
349 structures will require that the path buffer be modified using the
350 information contained in that
355 Any such modifications should be undone before further calls to
365 function takes a pointer to an array of character pointers naming one
366 or more paths which make up a logical file hierarchy to be traversed.
367 The array must be terminated by a
371 a number of options, at least one of which (either
376 The options are selected by ORing
377 the following values:
378 .\" .Bl -tag -width "FTS_PHYSICAL"
381 This option causes the
382 fts routines to return
384 structures for the targets of symbolic links
385 instead of the symbolic links themselves.
386 If this option is set, the only symbolic links for which
389 are returned to the application are those referencing nonexistent files:
392 field is obtained via
398 This option causes the
399 fts routines to return
401 structures for symbolic links themselves instead
402 of the target files they point to.
403 If this option is set,
405 structures for all symbolic links in the
406 hierarchy are returned to the application:
409 field is obtained via
413 This option causes any symbolic link specified as a root path to be
414 followed immediately, as if via
416 regardless of the primary mode.
419 As a performance optimization, the
420 fts functions change directories as they walk the file hierarchy.
421 This has the side-effect that an application cannot rely on being
422 in any particular directory during the traversal.
424 option turns off this optimization, and the
425 fts functions will not change the current directory.
426 Note that applications should not themselves change their current directory
427 and try to access files unless
429 is specified and absolute
430 pathnames were provided as arguments to
436 structures reference file characteristic information (the
438 field) for each file visited.
439 This option relaxes that requirement as a performance optimization,
441 fts functions to set the
445 and leave the contents of the
450 By default, unless they are specified as path arguments to
456 encountered in the file hierarchy are ignored.
457 This option causes the
458 fts routines to return
464 fts from descending into directories that have a different device number
465 than the file from which the descent began.
470 specifies a user-defined function which may be used to order the traversal
473 takes two pointers to pointers to
475 structures as arguments and
476 should return a negative value, zero, or a positive value to indicate
477 if the file referenced by its first argument comes before, in any order
478 with respect to, or after, the file referenced by its second argument.
488 be used in this comparison.
497 field may not either.
502 the directory traversal order is in the order listed in
504 for the root paths, and in the order listed in the directory for
509 function returns a pointer to an
511 structure describing a file in
513 Directories (that are readable and do not cause cycles) are visited at
514 least twice, once in preorder and once in postorder.
515 All other files are visited at least once.
516 (Hard links between directories that do not cause cycles or symbolic
517 links to symbolic links may cause files to be visited more than once,
518 or directories more than twice.)
520 If all the members of the hierarchy have been returned,
522 returns NULL and sets
525 If an error unrelated to a file in the hierarchy occurs,
531 to indicate the error.
532 If an error related to a returned file occurs, a pointer to an
534 structure is returned, and
536 may or may not have been set (see
541 structures returned by
543 may be overwritten after a call to
545 on the same file hierarchy stream, or, after a call to
547 on the same file hierarchy stream unless they represent a file of type
548 directory, in which case they will not be overwritten until after a call to
552 structure has been returned by the function
558 function returns a pointer to an
560 structure describing the first entry in a NULL-terminated linked list of
561 the files in the directory represented by the
563 structure most recently returned by
565 The list is linked through the
569 structure, and is ordered by the user-specified comparison function, if any.
572 will re-create this linked list.
574 As a special case, if
576 has not yet been called for a hierarchy,
578 will return a pointer to the files in the logical directory specified to
580 that is, the arguments specified to
584 structure most recently returned by
586 is not a directory being visited in preorder,
587 or the directory does not contain any files,
600 to indicate the error.
604 structures returned by
606 may be overwritten after a call to
611 on the same file hierarchy stream.
615 argument is either zero or the following value:
616 .\" .Bl -tag -width FTS_NAMEONLY
619 Only the names of the files are needed.
620 The contents of all the fields in the returned linked list of structures
621 are undefined with the exception of the
630 allows the user application to determine further processing for the
638 returns 0 on success, and \-1 if an error occurs.
642 argument is either 0 (meaning "do nothing") or one of the following values:
643 .\" .Bl -tag -width FTS_PHYSICAL
646 Revisit the file; any file type may be revisited.
649 will return the referenced file.
654 fields of the structure will be reinitialized at that time,
655 but no other fields will have been changed.
656 This option is meaningful only for the most recently returned
659 Normal use is for postorder directory visits, where it causes the
660 directory to be revisited (in both preorder and postorder) as well as all
664 The referenced file must be a symbolic link.
665 If the referenced file is the one most recently returned by
669 returns the file with the
673 fields reinitialized to reflect the target of the symbolic link instead
674 of the symbolic link itself.
675 If the file is one of those most recently returned by
681 fields of the structure, when returned by
683 will reflect the target of the symbolic link instead of the symbolic link
685 In either case, if the target of the symbolic link does not exist, the
686 fields of the returned structure will be unchanged and the
691 If the target of the link is a directory, the preorder return, followed
692 by the return of all of its descendants, followed by a postorder return,
696 No descendants of this file are visited.
697 The file may be one of those most recently returned by either
705 function closes the file hierarchy stream referred to by
707 and restores the current directory to the directory from which
714 returns 0 on success, and \-1 if an error occurs.
720 for any of the errors specified for
740 for any of the errors specified for
751 for any of the errors specified for
774 For an explanation of the terms used in this section, see
782 Interface Attribute Value
787 T} Thread safety MT-Safe
791 T} Thread safety MT-Unsafe
803 .\" Fixed by commit 8b7b7f75d91f7bac323dd6a370aeb3e9c5c4a7d5
804 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=15838
805 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=11460
806 all of the APIs described in this man page are not safe when compiling
807 a program using the LFS APIs (e.g., when compiling with
808 .IR \-D_FILE_OFFSET_BITS=64 ).
810 .\" The following statement is years old, and seems no closer to
811 .\" being true -- mtk
814 .\" utility is expected to be included in a future