1 .\" $OpenBSD: fts.3,v 1.36 2016/06/28 17:25:08 millert Exp $
3 .\" Copyright (c) 1989, 1991, 1993, 1994
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" @(#)fts.3 8.5 (Berkeley) 4/16/94
32 .Dd $Mdocdate: June 28 2016 $
41 .Nd traverse a file hierarchy
47 .Fn fts_open "char * const *path_argv" "int options" "int (*compar)(const FTSENT **, const FTSENT **)"
49 .Fn fts_read "FTS *ftsp"
51 .Fn fts_children "FTS *ftsp" "int options"
53 .Fn fts_set "FTS *ftsp" "FTSENT *f" "int option"
55 .Fn fts_close "FTS *ftsp"
59 functions are provided for traversing
66 on a file hierarchy, which is then supplied to
72 returns a pointer to a structure describing one of the files in the file
76 returns a pointer to a linked list of structures, each of which describes
77 one of the files contained in a directory within the hierarchy.
79 In general, directories are visited two distinguishable times; in pre-order
80 (before any of their descendants are visited) and in post-order (after all
81 of their descendants have been visited).
82 Files are visited once.
83 It is possible to walk the hierarchy
85 (following symbolic links)
88 (not following symbolic links),
89 order the walk of the hierarchy, or
90 prune and/or re-visit portions of the hierarchy.
92 Two structures are defined (and typedef'd) in the include file
96 the structure that represents the file hierarchy itself.
99 the structure that represents a file in the file
103 structure is returned for every file in the file
108 .Dq Li FTSENT No structure
114 structure contains at least the following fields, which are
115 described in greater detail below:
117 typedef struct _ftsent {
118 unsigned short fts_info; /* flags for FTSENT structure */
119 char *fts_accpath; /* access path */
120 char *fts_path; /* root path */
121 size_t fts_pathlen; /* strlen(fts_path) */
122 char *fts_name; /* file name */
123 size_t fts_namelen; /* strlen(fts_name) */
124 int fts_level; /* depth (-1 to N) */
125 int fts_errno; /* file errno */
126 long fts_number; /* local numeric value */
127 void *fts_pointer; /* local address value */
128 struct _ftsent *fts_parent; /* parent directory */
129 struct _ftsent *fts_link; /* next file structure */
130 struct _ftsent *fts_cycle; /* cycle structure */
131 struct stat *fts_statp; /* stat(2) information */
135 These fields are defined as follows:
136 .Bl -tag -width "fts_namelen"
138 One of the following flags describing the returned
141 the file it represents.
142 With the exception of directories without errors
145 entries are terminal, that is, they will not be revisited, nor will any
146 of their descendants be visited.
147 .Bl -tag -width FTS_DEFAULT
149 A directory being visited in pre-order.
151 A directory that causes a cycle in the tree.
156 structure will be filled in as well.)
160 structure that represents a file type not explicitly described
165 A directory which cannot be read.
166 This is an error return, and the
168 field will be set to indicate what caused the error.
174 which was not specified as a file name to
179 A directory being visited in post-order.
182 structure will be unchanged from when
183 it was returned in pre-order, i.e., with the
188 This is an error return, and the
190 field will be set to indicate what caused the error.
196 information was available.
200 This is an error return, and the
202 field will be set to indicate what caused the error.
206 information was requested.
213 A symbolic link with a non-existent target.
216 field reference the file characteristic information for the symbolic link
220 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
227 The length of the string referenced by
230 The name of the file.
232 The length of the string referenced by
235 The depth of the traversal, numbered from \-1 to N, where this file
239 structure representing the parent of the starting point (or root)
240 of the traversal is numbered
241 .Dv FTS_ROOTPARENTLEVEL
244 structure for the root
250 cannot hold a number of levels greater than
254 functions themselves are not limited to a fixed number
256 Application code that inspects
258 should be written with this in mind.
275 field contains the value of the external variable
277 specifying the cause of the error.
278 Otherwise, the contents of the
282 This field is provided for the use of the application program and is
286 It is initialized to 0.
288 This field is provided for the use of the application program and is
297 structure referencing the file in the hierarchy
298 immediately above the current file, i.e., the directory of which this
300 A parent structure for the initial entry point is provided as well,
306 fields are guaranteed to be initialized.
312 field points to the next structure in the null-terminated
313 linked list of directory members.
314 Otherwise, the contents of the
318 If a directory causes a cycle in the hierarchy (see
321 of a hard link between two directories, or a symbolic link pointing to a
324 field of the structure will point to the
326 structure in the hierarchy that references the same file as the current
329 Otherwise, the contents of the
335 information for the file.
338 A single buffer is used for all of the paths of all of the files in the
344 fields are guaranteed to be NUL terminated
346 for the file most recently returned by
348 To use these fields to reference any files represented by other
350 structures will require that the path buffer be modified using the
351 information contained in that
356 Any such modifications should be undone before further calls to
361 field is always NUL terminated.
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 null pointer.
370 a number of options, at least one of which (either
379 the following values:
380 .Bl -tag -width "FTS_PHYSICAL"
382 This option causes any symbolic link specified as a root path to be
383 followed immediately whether or not
387 This option causes the
391 structures for the targets of symbolic links
392 instead of the symbolic links themselves.
393 If this option is set, the only symbolic links for which
396 are returned to the application are those referencing non-existent files.
406 As a performance optimization, the
408 functions change directories as they walk the file hierarchy.
409 This has the side-effect that an application cannot rely on being
410 in any particular directory during the traversal.
413 option turns off this optimization, and the
415 functions will not change the current directory.
416 Note that applications should not themselves change their current directory
417 and try to access files unless
419 is specified and absolute
420 pathnames were provided as arguments to
425 structures reference file characteristic information (the
427 field) for each file visited.
428 This option relaxes that requirement as a performance optimization,
435 and leave the contents of the
439 This option causes the
443 structures for symbolic links themselves instead
444 of the target files they point to.
445 If this option is set,
447 structures for all symbolic links in the
448 hierarchy are returned to the application.
458 By default, unless they are specified as path arguments to
464 encountered in the file hierarchy are ignored.
465 This option causes the
473 from descending into directories that have a different device number
474 than the file from which the descent began.
480 specifies a user-defined function which may be used to order the traversal
483 takes two pointers to pointers to
485 structures as arguments and
486 should return a negative value, zero, or a positive value to indicate
487 if the file referenced by its first argument comes before, in any order
488 with respect to, or after, the file referenced by its second argument.
498 be used in this comparison.
507 field may not either.
512 the directory traversal order is in the order listed in
514 for the root paths, and in the order listed in the directory for
527 function returns a pointer to an
529 structure describing a file in
531 Directories (that are readable and do not cause cycles) are visited at
532 least twice, once in pre-order and once in post-order.
533 All other files are visited at least once.
534 (Hard links between directories that do not cause cycles or symbolic
535 links to symbolic links may cause files to be visited more than once,
536 or directories more than twice.)
538 If all the members of the hierarchy have been returned,
542 and sets the external variable
545 If an error unrelated to a file in the hierarchy occurs,
552 If an error related to a returned file occurs, a pointer to an
554 structure is returned, and
556 may or may not have been set (see
561 structures returned by
563 may be overwritten after a call to
565 on the same file hierarchy stream or, after a call to
567 on the same file hierarchy stream unless they represent a file of type
568 directory, in which case they will not be overwritten until after a call to
572 structure has been returned by the function
578 function returns a pointer to an
580 structure describing the first entry in a null-terminated
582 the files in the directory represented by the
584 structure most recently returned by
586 The list is linked through the
590 structure, and is ordered by the user-specified comparison function, if any.
593 will recreate this linked list.
595 As a special case, if
597 has not yet been called for a hierarchy,
599 will return a pointer to the files in the logical directory specified to
601 i.e., the arguments specified to
605 structure most recently returned by
607 is not a directory being visited in pre-order,
608 or the directory does not contain any files,
625 structures returned by
627 may be overwritten after a call to
632 on the same file hierarchy stream.
635 may be set to the following value:
636 .Bl -tag -width FTS_NAMEONLY
638 Only the names of the files are needed.
639 The contents of all the fields in the returned linked list of structures
640 are undefined with the exception of the
649 allows the user application to determine further processing for the file
655 function returns 0 on success or \-1 if an error occurred.
657 must be set to one of the following values:
658 .Bl -tag -width FTS_PHYSICAL
660 Re-visit the file; any file type may be re-visited.
663 will return the referenced file.
668 fields of the structure will be reinitialized at that time,
669 but no other fields will have been changed.
670 This option is meaningful only for the most recently returned
673 Normal use is for post-order directory visits, where it causes the
674 directory to be re-visited (in both pre and post-order) as well as all
677 The referenced file must be a symbolic link.
678 If the referenced file is the one most recently returned by
682 returns the file with the
686 fields reinitialized to reflect the target of the symbolic link instead
687 of the symbolic link itself.
688 If the file is one of those most recently returned by
694 fields of the structure, when returned by
696 will reflect the target of the symbolic link instead of the symbolic link
698 In either case if the target of the symbolic link does not exist, the
699 fields of the returned structure will be unchanged and the
704 If the target of the link is a directory, the pre-order return, followed
705 by the return of all of its descendants, followed by a post-order return,
708 No descendants of this file are visited.
709 The file may be one of those most recently returned by either
717 function closes a file hierarchy stream
719 and restores the current directory to the directory from which
729 for any of the errors specified for the library functions
738 for any of the errors specified for the library function
747 for any of the errors specified for the library functions
765 A specified option is invalid or
777 functions first appeared in
779 The interface was revised in