2 .\" Copyright (c) 1989, 1991, 1993C, 1994
3 .\" The Regents of the University of California. All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
13 .\" 3C. Neither the name of the University nor the names of its contributors
14 .\" may be used to endorse or promote products derived from this software
15 .\" without specific prior written permission.
17 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 .Nd traverse a file hierarchy
46 .Fa "char * const *path_argv"
48 .Fa "int (*compar)(const FTSENT **, const FTSENT **)"
51 .Fn fts_read "FTS *ftsp"
53 .Fn fts_children "FTS *ftsp" "int options"
55 .Fn fts_set "FTS *ftsp" "FTSENT *f" "int option"
57 .Fn fts_close "FTS *ftsp"
61 functions are provided for traversing
68 on a file hierarchy, which is then supplied to
74 returns a pointer to a structure describing one of the files in the file
78 returns a pointer to a linked list of structures, each of which describes
79 one of the files contained in a directory within the hierarchy.
81 In general, directories are visited two distinguishable times; in pre-order
82 .Pq before any of their descendants are visited
84 .Pq after all of their descendants have been visited .
85 Files are visited once.
86 It is possible to walk the hierarchy
88 .Pq following symbolic links
91 .Pq not following symbolic links ,
92 order the walk of the hierarchy, or
93 prune and/or re-visit portions of the hierarchy.
95 Two structures are defined
101 the structure that represents the file hierarchy itself.
104 the structure that represents a file in the file
108 structure is returned for every file in the file
113 .Dq Li FTSENT No structure
119 structure contains at least the following fields, which are
120 described in greater detail below:
122 typedef struct _ftsent {
123 unsigned short fts_info; /* flags for FTSENT structure */
124 char *fts_accpath; /* access path */
125 char *fts_path; /* root path */
126 size_t fts_pathlen; /* strlen(fts_path) */
127 char *fts_name; /* file name */
128 size_t fts_namelen; /* strlen(fts_name) */
129 int fts_level; /* depth (-1 to N) */
130 int fts_errno; /* file errno */
131 long fts_number; /* local numeric value */
132 void *fts_pointer; /* local address value */
133 struct _ftsent *fts_parent; /* parent directory */
134 struct _ftsent *fts_link; /* next file structure */
135 struct _ftsent *fts_cycle; /* cycle structure */
136 struct stat *fts_statp; /* stat(2) information */
140 These fields are defined as follows:
141 .Bl -tag -width "fts_namelen"
143 One of the following flags describing the returned
146 the file it represents.
147 With the exception of directories without errors
150 entries are terminal, that is, they will not be revisited, nor will any
151 of their descendants be visited.
152 .Bl -tag -width FTS_DEFAULT
154 A directory being visited in pre-order.
156 A directory that causes a cycle in the tree.
161 structure will be filled in as well.
166 structure that represents a file type not explicitly described
171 A directory which cannot be read.
172 This is an error return, and the
174 field will be set to indicate what caused the error.
180 which was not specified as a file name to
182 .Pq see Dv FTS_SEEDOT .
184 A directory being visited in post-order.
187 structure will be unchanged from when
188 it was returned in pre-order, i.e., with the
193 This is an error return, and the
195 field will be set to indicate what caused the error.
201 information was available.
205 This is an error return, and the
207 field will be set to indicate what caused the error.
211 information was requested.
218 A symbolic link with a non-existent target.
221 field reference the file characteristic information for the symbolic link
225 A path for accessing the file from the current directory.
227 The path for the file relative to the root of the traversal.
228 This path contains the path specified to
232 The length of the string referenced by
235 The name of the file.
237 The length of the string referenced by
240 The depth of the traversal, numbered from \-1 to N, where this file
244 structure representing the parent of the starting point
246 of the traversal is numbered
247 .Dv FTS_ROOTPARENTLEVEL
251 structure for the root
257 cannot hold a number of levels greater than
261 functions themselves are not limited to a fixed number
263 Application code that inspects
265 should be written with this in mind.
282 field contains the value of the external variable
284 specifying the cause of the error.
285 Otherwise, the contents of the
289 This field is provided for the use of the application program and is
293 It is initialized to 0.
295 This field is provided for the use of the application program and is
304 structure referencing the file in the hierarchy
305 immediately above the current file, i.e., the directory of which this
307 A parent structure for the initial entry point is provided as well,
313 fields are guaranteed to be initialized.
319 field points to the next structure in the null-terminated
320 linked list of directory members.
321 Otherwise, the contents of the
325 If a directory causes a cycle in the hierarchy
327 either because of a hard link between two directories, or a symbolic link
328 pointing to a directory, the
330 field of the structure will point to the
332 structure in the hierarchy that references the same file as the current
335 Otherwise, the contents of the
341 information for the file.
344 A single buffer is used for all of the paths of all of the files in the
350 fields are guaranteed to be NUL terminated
352 for the file most recently returned by
354 To use these fields to reference any files represented by other
356 structures will require that the path buffer be modified using the
357 information contained in that
362 Any such modifications should be undone before further calls to
367 field is always NUL terminated.
371 function takes a pointer to an array of character pointers naming one
372 or more paths which make up a logical file hierarchy to be traversed.
373 The array must be terminated by a null pointer.
376 a number of options, at least one of which
387 the following values:
388 .Bl -tag -width "FTS_COMFOLLOW"
390 This option causes any symbolic link specified as a root path to be
391 followed immediately whether or not
395 This option causes the
399 structures for the targets of symbolic links
400 instead of the symbolic links themselves.
401 If this option is set, the only symbolic links for which
404 are returned to the application are those referencing non-existent files.
414 As a performance optimization, the
416 functions change directories as they walk the file hierarchy.
417 This has the side-effect that an application cannot rely on being
418 in any particular directory during the traversal.
421 option turns off this optimization, and the
423 functions will not change the current directory.
424 Note that applications should not themselves change their current directory
425 and try to access files unless
427 is specified and absolute
428 pathnames were provided as arguments to
433 structures reference file characteristic information
438 for each file visited.
439 This option relaxes that requirement as a performance optimization,
446 and leave the contents of the
450 This option causes the
454 structures for symbolic links themselves instead
455 of the target files they point to.
456 If this option is set,
458 structures for all symbolic links in the
459 hierarchy are returned to the application.
469 By default, unless they are specified as path arguments to
475 encountered in the file hierarchy are ignored.
476 This option causes the
484 from descending into directories that have a different device number
485 than the file from which the descent began.
491 specifies a user-defined function which may be used to order the traversal
494 takes two pointers to pointers to
496 structures as arguments and
497 should return a negative value, zero, or a positive value to indicate
498 if the file referenced by its first argument comes before, in any order
499 with respect to, or after, the file referenced by its second argument.
509 be used in this comparison.
518 field may not either.
523 the directory traversal order is in the order listed in
525 for the root paths, and in the order listed in the directory for
538 function returns a pointer to an
540 structure describing a file in
543 .Pq that are readable and do not cause cycles
544 are visited at least twice, once in pre-order and once in post-order.
545 All other files are visited at least once.
546 .Po Hard links between directories that do not cause cycles or symbolic
547 links to symbolic links may cause files to be visited more than once,
548 or directories more than twice.
551 If all the members of the hierarchy have been returned,
555 and sets the external variable
558 If an error unrelated to a file in the hierarchy occurs,
565 If an error related to a returned file occurs, a pointer to an
567 structure is returned, and
569 may or may not have been set
570 .Pq see Fa fts_info .
574 structures returned by
576 may be overwritten after a call to
578 on the same file hierarchy stream or, after a call to
580 on the same file hierarchy stream unless they represent a file of type
581 directory, in which case they will not be overwritten until after a call to
585 structure has been returned by the function
591 function returns a pointer to an
593 structure describing the first entry in a null-terminated
595 the files in the directory represented by the
597 structure most recently returned by
599 The list is linked through the
603 structure, and is ordered by the user-specified comparison function, if any.
606 will recreate this linked list.
608 As a special case, if
610 has not yet been called for a hierarchy,
612 will return a pointer to the files in the logical directory specified to
614 i.e., the arguments specified to
618 structure most recently returned by
620 is not a directory being visited in pre-order,
621 or the directory does not contain any files,
638 structures returned by
640 may be overwritten after a call to
645 on the same file hierarchy stream.
648 may be set to the following value:
649 .Bl -tag -width FTS_NAMEONLY
651 Only the names of the files are needed.
652 The contents of all the fields in the returned linked list of structures
653 are undefined with the exception of the
662 allows the user application to determine further processing for the file
668 function returns 0 on success or \-1 if an error occurred.
670 must be set to one of the following values:
671 .Bl -tag -width FTS_PHYSICAL
673 Re-visit the file; any file type may be re-visited.
676 will return the referenced file.
681 fields of the structure will be reinitialized at that time,
682 but no other fields will have been changed.
683 This option is meaningful only for the most recently returned
686 Normal use is for post-order directory visits, where it causes the
687 directory to be re-visited
688 .Pq in both pre and post-order
689 as well as all of its descendants.
691 The referenced file must be a symbolic link.
692 If the referenced file is the one most recently returned by
696 returns the file with the
700 fields reinitialized to reflect the target of the symbolic link instead
701 of the symbolic link itself.
702 If the file is one of those most recently returned by
708 fields of the structure, when returned by
710 will reflect the target of the symbolic link instead of the symbolic link
712 In either case if the target of the symbolic link does not exist, the
713 fields of the returned structure will be unchanged and the
718 If the target of the link is a directory, the pre-order return, followed
719 by the return of all of its descendants, followed by a post-order return,
722 No descendants of this file are visited.
723 The file may be one of those most recently returned by either
731 function closes a file hierarchy stream
733 and restores the current directory to the directory from which
739 The fts routines provide no locking.
744 and can be called from multiple threads simultaneously, the individual
745 handles returned from the
747 function are not thread-safe.
748 If callers need to operate on a single
750 structure, then it is their responsibility to ensure that none of the
751 other functions are called from multiple threads simultaneously.
752 This implies that the
761 These routines are not
762 .Sy Async-Signal-Safe
763 and callers should not assume that the implementation of these functions
766 If callers implement their own locking structures around the use of
767 these routines, they must ensure that those locks are accounted for when
768 forking by the use of routines such as
769 .Xr pthread_atfork 3C .
775 for any of the errors specified for the library functions
784 for any of the errors specified for the library function
793 for any of the errors specified for the library functions
811 A specified option is invalid or
815 .Sh INTERFACE STABILITY