1 .\" Copyright (c) 1989, 1991, 1993, 1994
2 .\" The Regents of the University of California. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 3. Neither the name of the University nor the names of its contributors
13 .\" may be used to endorse or promote products derived from this software
14 .\" without specific prior written permission.
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 .Nd traverse a file hierarchy
39 .Fn fts_open "char * const *path_argv" "int options" "int (*compar)(const FTSENT * const *, const FTSENT * const *)"
41 .Fn fts_read "FTS *ftsp"
43 .Fn fts_children "FTS *ftsp" "int options"
45 .Fn fts_set "FTS *ftsp" "FTSENT *f" "int options"
47 .Fn fts_set_clientptr "FTS *ftsp" "void *clientdata"
49 .Fn fts_get_clientptr "FTS *ftsp"
51 .Fn fts_get_stream "FTSENT *f"
53 .Fn fts_close "FTS *ftsp"
57 functions are provided for traversing
60 A simple overview is that the
64 on a file hierarchy, which is then supplied to
70 returns a pointer to a structure describing one of the files in the file
74 returns a pointer to a linked list of structures, each of which describes
75 one of the files contained in a directory in the hierarchy.
76 In general, directories are visited two distinguishable times; in pre-order
77 (before any of their descendants are visited) and in post-order (after all
78 of their descendants have been visited).
79 Files are visited once.
80 It is possible to walk the hierarchy
82 (ignoring symbolic links)
83 or physically (visiting symbolic links), order the walk of the hierarchy or
84 prune and/or re-visit portions of the hierarchy.
86 Two structures are defined (and typedef'd) in the include file
90 the structure that represents the file hierarchy itself.
93 the structure that represents a file in the file
97 structure is returned for every file in the file
102 .Dq Vt FTSENT No structure
108 structure contains space for a single pointer, which may be used to
109 store application data or per-hierarchy state.
111 .Fn fts_set_clientptr
113 .Fn fts_get_clientptr
114 functions may be used to set and retrieve this pointer.
115 This is likely to be useful only when accessed from the sort
116 comparison function, which can determine the original
118 stream of its arguments using the
123 functions are also available as macros of the same name.
127 structure contains at least the following fields, which are
128 described in greater detail below:
130 typedef struct _ftsent {
131 int fts_info; /* status for FTSENT structure */
132 char *fts_accpath; /* access path */
133 char *fts_path; /* root path */
134 size_t fts_pathlen; /* strlen(fts_path) */
135 char *fts_name; /* file name */
136 size_t fts_namelen; /* strlen(fts_name) */
137 long fts_level; /* depth (\-1 to N) */
138 int fts_errno; /* file errno */
139 long long fts_number; /* local numeric value */
140 void *fts_pointer; /* local address value */
141 struct ftsent *fts_parent; /* parent directory */
142 struct ftsent *fts_link; /* next file structure */
143 struct ftsent *fts_cycle; /* cycle structure */
144 struct stat *fts_statp; /* stat(2) information */
148 These fields are defined as follows:
149 .Bl -tag -width "fts_namelen"
151 One of the following values describing the returned
154 the file it represents.
155 With the exception of directories without errors
158 entries are terminal, that is, they will not be revisited, nor will any
159 of their descendants be visited.
160 .Bl -tag -width FTS_DEFAULT
162 A directory being visited in pre-order.
164 A directory that causes a cycle in the tree.
169 structure will be filled in as well.)
173 structure that represents a file type not explicitly described
178 A directory which cannot be read.
179 This is an error return, and the
181 field will be set to indicate what caused the error.
187 which was not specified as a file name to
192 A directory being visited in post-order.
195 structure will be unchanged from when
196 the directory was visited in pre-order, except for the
200 This is an error return, and the
202 field will be set to indicate what caused the error.
208 information was available.
212 This is an error return, and the
214 field will be set to indicate what caused the error.
218 information was requested.
225 A symbolic link with a non-existent target.
228 field reference the file characteristic information for the symbolic link
232 A path for accessing the file from the current directory.
234 The path for the file relative to the root of the traversal.
235 This path contains the path specified to
239 The length of the string referenced by
242 The name of the file.
244 The length of the string referenced by
247 The depth of the traversal, numbered from \-1 to N, where this file
251 structure representing the parent of the starting point (or root)
252 of the traversal is numbered
253 .Dv FTS_ROOTPARENTLEVEL
256 structure for the root
276 field contains the value of the external variable
278 specifying the cause of the error.
279 Otherwise, the contents of the
283 This field is provided for the use of the application program and is
287 It is initialized to 0.
289 This field is provided for the use of the application program and is
298 structure referencing the file in the hierarchy
299 immediately above the current file, i.e., the directory of which this
301 A parent structure for the initial entry point is provided as well,
307 fields are guaranteed to be initialized.
313 field points to the next structure in the NULL-terminated linked list of
315 Otherwise, the contents of the
319 If a directory causes a cycle in the hierarchy (see
322 of a hard link between two directories, or a symbolic link pointing to a
325 field of the structure will point to the
327 structure in the hierarchy that references the same file as the current
330 Otherwise, the contents of the
336 information for the file.
339 A single buffer is used for all of the paths of all of the files in the
345 fields are guaranteed to be
346 .Dv NUL Ns -terminated
348 for the file most recently returned by
350 To use these fields to reference any files represented by other
352 structures will require that the path buffer be modified using the
353 information contained in that
358 Any such modifications should be undone before further calls to
364 .Dv NUL Ns -terminated .
368 function takes a pointer to an array of character pointers naming one
369 or more paths which make up a logical file hierarchy to be traversed.
370 The array must be terminated by a
375 a number of options, at least one of which (either
380 The options are selected by
382 the following values:
383 .Bl -tag -width "FTS_PHYSICAL"
385 This option causes any symbolic link specified as a root path to be
386 followed immediately whether or not
390 This option causes the
394 structures for the targets of symbolic links
395 instead of the symbolic links themselves.
396 If this option is set, the only symbolic links for which
399 are returned to the application are those referencing non-existent files.
409 To allow descending to arbitrary depths
412 and improve performance, the
414 functions change directories as they walk the file hierarchy.
415 This has the side-effect that an application cannot rely on being
416 in any particular directory during the traversal.
419 option turns off this feature, and the
421 functions will not change the current directory.
422 Note that applications should not themselves change their current directory
423 and try to access files unless
425 is specified and absolute
426 pathnames were provided as arguments to
431 structures reference file characteristic information (the
433 field) for each file visited.
434 This option relaxes that requirement as a performance optimization,
441 and leave the contents of the
445 This option causes the
449 structures for symbolic links themselves instead
450 of the target files they point to.
451 If this option is set,
453 structures for all symbolic links in the
454 hierarchy are returned to the application.
464 By default, unless they are specified as path arguments to
470 encountered in the file hierarchy are ignored.
471 This option causes the
479 from descending into directories that have a different device number
480 than the file from which the descent began.
485 specifies a user-defined function which may be used to order the traversal
488 takes two pointers to pointers to
490 structures as arguments and
491 should return a negative value, zero, or a positive value to indicate
492 if the file referenced by its first argument comes before, in any order
493 with respect to, or after, the file referenced by its second argument.
503 be used in this comparison.
512 field may not either.
517 the directory traversal order is in the order listed in
519 for the root paths, and in the order listed in the directory for
524 function returns a pointer to an
526 structure describing a file in
528 Directories (that are readable and do not cause cycles) are visited at
529 least twice, once in pre-order and once in post-order.
530 All other files are visited at least once.
531 (Hard links between directories that do not cause cycles or symbolic
532 links to symbolic links may cause files to be visited more than once,
533 or directories more than twice.)
535 If all the members of the hierarchy have been returned,
539 and sets the external variable
542 If an error unrelated to a file in the hierarchy occurs,
549 If an error related to a returned file occurs, a pointer to an
551 structure is returned, and
553 may or may not have been set (see
558 structures returned by
560 may be overwritten after a call to
562 on the same file hierarchy stream, or, after a call to
564 on the same file hierarchy stream unless they represent a file of type
565 directory, in which case they will not be overwritten until after a call to
569 structure has been returned by the function
575 function returns a pointer to an
577 structure describing the first entry in a NULL-terminated linked list of
578 the files in the directory represented by the
580 structure most recently returned by
582 The list is linked through the
586 structure, and is ordered by the user-specified comparison function, if any.
589 will recreate this linked list.
591 As a special case, if
593 has not yet been called for a hierarchy,
595 will return a pointer to the files in the logical directory specified to
597 i.e., the arguments specified to
601 structure most recently returned by
603 is not a directory being visited in pre-order,
604 or the directory does not contain any files,
621 structures returned by
623 may be overwritten after a call to
628 on the same file hierarchy stream.
631 may be set to the following value:
632 .Bl -tag -width FTS_NAMEONLY
634 Only the names of the files are needed.
635 The contents of all the fields in the returned linked list of structures
636 are undefined with the exception of the
645 allows the user application to determine further processing for the
653 returns 0 on success, and \-1 if an error occurs.
655 must be set to one of the following values:
656 .Bl -tag -width FTS_PHYSICAL
658 Re-visit the file; any file type may be re-visited.
661 will return the referenced file.
666 fields of the structure will be reinitialized at that time,
667 but no other fields will have been changed.
668 This option is meaningful only for the most recently returned
671 Normal use is for post-order directory visits, where it causes the
672 directory to be re-visited (in both pre and post-order) as well as all
675 The referenced file must be a symbolic link.
676 If the referenced file is the one most recently returned by
680 returns the file with the
684 fields reinitialized to reflect the target of the symbolic link instead
685 of the symbolic link itself.
686 If the file is one of those most recently returned by
692 fields of the structure, when returned by
694 will reflect the target of the symbolic link instead of the symbolic link
696 In either case, if the target of the symbolic link does not exist the
697 fields of the returned structure will be unchanged and the
702 If the target of the link is a directory, the pre-order return, followed
703 by the return of all of its descendants, followed by a post-order return,
706 No descendants of this file are visited.
707 The file may be one of those most recently returned by either
715 function closes a file hierarchy stream
717 and restores the current directory to the directory from which
724 returns 0 on success, and \-1 if an error occurs.
730 for any of the errors specified for the library functions
739 for any of the errors specified for the library functions
750 for any of the errors specified for the library functions
768 The options were invalid, or the list were empty.
779 interface was first introduced in
782 .Fn fts_get_clientptr ,
785 .Fn fts_set_clientptr
786 functions were introduced in
788 principally to provide for alternative interfaces to the
790 functionality using different data structures.
794 function will automatically set the
798 option is provided, or if it cannot
800 the current directory.