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
28 .\" @(#)fts.3 8.5 (Berkeley) 4/16/94
36 .Nd traverse a file hierarchy
42 .Fn fts_open "char * const *path_argv" "int options" "int (*compar)(const FTSENT * const *, const FTSENT * const *)"
44 .Fn fts_read "FTS *ftsp"
46 .Fn fts_children "FTS *ftsp" "int options"
48 .Fn fts_set "FTS *ftsp" "FTSENT *f" "int options"
50 .Fn fts_set_clientptr "FTS *ftsp" "void *clientdata"
52 .Fn fts_get_clientptr "FTS *ftsp"
54 .Fn fts_get_stream "FTSENT *f"
56 .Fn fts_close "FTS *ftsp"
60 functions are provided for traversing
63 A simple overview is that the
67 on a file hierarchy, which is then supplied to
73 returns a pointer to a structure describing one of the files in the file
77 returns a pointer to a linked list of structures, each of which describes
78 one of the files contained in a directory in 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 (ignoring symbolic links)
86 or physically (visiting symbolic links), order the walk of the hierarchy or
87 prune and/or re-visit portions of the hierarchy.
89 Two structures are defined (and typedef'd) in the include file
93 the structure that represents the file hierarchy itself.
96 the structure that represents a file in the file
100 structure is returned for every file in the file
105 .Dq Vt FTSENT No structure
111 structure contains space for a single pointer, which may be used to
112 store application data or per-hierarchy state.
114 .Fn fts_set_clientptr
116 .Fn fts_get_clientptr
117 functions may be used to set and retrieve this pointer.
118 This is likely to be useful only when accessed from the sort
119 comparison function, which can determine the original
121 stream of its arguments using the
126 functions are also available as macros of the same name.
130 structure contains at least the following fields, which are
131 described in greater detail below:
133 typedef struct _ftsent {
134 int fts_info; /* status for FTSENT structure */
135 char *fts_accpath; /* access path */
136 char *fts_path; /* root path */
137 size_t fts_pathlen; /* strlen(fts_path) */
138 char *fts_name; /* file name */
139 size_t fts_namelen; /* strlen(fts_name) */
140 long fts_level; /* depth (\-1 to N) */
141 int fts_errno; /* file errno */
142 long long fts_number; /* local numeric value */
143 void *fts_pointer; /* local address value */
144 struct ftsent *fts_parent; /* parent directory */
145 struct ftsent *fts_link; /* next file structure */
146 struct ftsent *fts_cycle; /* cycle structure */
147 struct stat *fts_statp; /* stat(2) information */
151 These fields are defined as follows:
152 .Bl -tag -width "fts_namelen"
154 One of the following values describing the returned
157 the file it represents.
158 With the exception of directories without errors
161 entries are terminal, that is, they will not be revisited, nor will any
162 of their descendants be visited.
163 .Bl -tag -width FTS_DEFAULT
165 A directory being visited in pre-order.
167 A directory that causes a cycle in the tree.
172 structure will be filled in as well.)
176 structure that represents a file type not explicitly described
181 A directory which cannot be read.
182 This is an error return, and the
184 field will be set to indicate what caused the error.
190 which was not specified as a file name to
195 A directory being visited in post-order.
198 structure will be unchanged from when
199 the directory was visited in pre-order, except for the
203 This is an error return, and the
205 field will be set to indicate what caused the error.
211 information was available.
215 This is an error return, and the
217 field will be set to indicate what caused the error.
221 information was requested.
228 A symbolic link with a non-existent target.
231 field reference the file characteristic information for the symbolic link
235 A path for accessing the file from the current directory.
237 The path for the file relative to the root of the traversal.
238 This path contains the path specified to
242 The length of the string referenced by
245 The name of the file.
247 The length of the string referenced by
250 The depth of the traversal, numbered from \-1 to N, where this file
254 structure representing the parent of the starting point (or root)
255 of the traversal is numbered
256 .Dv FTS_ROOTPARENTLEVEL
259 structure for the root
279 field contains the value of the external variable
281 specifying the cause of the error.
282 Otherwise, the contents of the
286 This field is provided for the use of the application program and is
290 It is initialized to 0.
292 This field is provided for the use of the application program and is
301 structure referencing the file in the hierarchy
302 immediately above the current file, i.e., the directory of which this
304 A parent structure for the initial entry point is provided as well,
310 fields are guaranteed to be initialized.
316 field points to the next structure in the NULL-terminated linked list of
318 Otherwise, the contents of the
322 If a directory causes a cycle in the hierarchy (see
325 of a hard link between two directories, or a symbolic link pointing to a
328 field of the structure will point to the
330 structure in the hierarchy that references the same file as the current
333 Otherwise, the contents of the
339 information for the file.
342 A single buffer is used for all of the paths of all of the files in the
348 fields are guaranteed to be
349 .Dv NUL Ns -terminated
351 for the file most recently returned by
353 To use these fields to reference any files represented by other
355 structures will require that the path buffer be modified using the
356 information contained in that
361 Any such modifications should be undone before further calls to
367 .Dv NUL Ns -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
378 a number of options, at least one of which (either
383 The options are selected by
385 the following values:
386 .Bl -tag -width "FTS_PHYSICAL"
388 This option causes any symbolic link specified as a root path to be
389 followed immediately whether or not
393 This option causes the
397 structures for the targets of symbolic links
398 instead of the symbolic links themselves.
399 If this option is set, the only symbolic links for which
402 are returned to the application are those referencing non-existent files.
412 To allow descending to arbitrary depths
415 and improve performance, the
417 functions change directories as they walk the file hierarchy.
418 This has the side-effect that an application cannot rely on being
419 in any particular directory during the traversal.
422 option turns off this feature, and the
424 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
434 structures reference file characteristic information (the
436 field) for each file visited.
437 This option relaxes that requirement as a performance optimization,
444 and leave the contents of the
448 This option causes the
452 structures for symbolic links themselves instead
453 of the target files they point to.
454 If this option is set,
456 structures for all symbolic links in the
457 hierarchy are returned to the application.
467 By default, unless they are specified as path arguments to
473 encountered in the file hierarchy are ignored.
474 This option causes the
482 from descending into directories that have a different device number
483 than the file from which the descent began.
488 specifies a user-defined function which may be used to order the traversal
491 takes two pointers to pointers to
493 structures as arguments and
494 should return a negative value, zero, or a positive value to indicate
495 if the file referenced by its first argument comes before, in any order
496 with respect to, or after, the file referenced by its second argument.
506 be used in this comparison.
515 field may not either.
520 the directory traversal order is in the order listed in
522 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 linked list of
581 the files in the directory represented by the
583 structure most recently returned by
585 The list is linked through the
589 structure, and is ordered by the user-specified comparison function, if any.
592 will recreate this linked list.
594 As a special case, if
596 has not yet been called for a hierarchy,
598 will return a pointer to the files in the logical directory specified to
600 i.e., the arguments specified to
604 structure most recently returned by
606 is not a directory being visited in pre-order,
607 or the directory does not contain any files,
624 structures returned by
626 may be overwritten after a call to
631 on the same file hierarchy stream.
634 may be set to the following value:
635 .Bl -tag -width FTS_NAMEONLY
637 Only the names of the files are needed.
638 The contents of all the fields in the returned linked list of structures
639 are undefined with the exception of the
648 allows the user application to determine further processing for the
656 returns 0 on success, and \-1 if an error occurs.
658 must be set to one of the following values:
659 .Bl -tag -width FTS_PHYSICAL
661 Re-visit the file; any file type may be re-visited.
664 will return the referenced file.
669 fields of the structure will be reinitialized at that time,
670 but no other fields will have been changed.
671 This option is meaningful only for the most recently returned
674 Normal use is for post-order directory visits, where it causes the
675 directory to be re-visited (in both pre and post-order) as well as all
678 The referenced file must be a symbolic link.
679 If the referenced file is the one most recently returned by
683 returns the file with the
687 fields reinitialized to reflect the target of the symbolic link instead
688 of the symbolic link itself.
689 If the file is one of those most recently returned by
695 fields of the structure, when returned by
697 will reflect the target of the symbolic link instead of the symbolic link
699 In either case, if the target of the symbolic link does not exist the
700 fields of the returned structure will be unchanged and the
705 If the target of the link is a directory, the pre-order return, followed
706 by the return of all of its descendants, followed by a post-order return,
709 No descendants of this file are visited.
710 The file may be one of those most recently returned by either
718 function closes a file hierarchy stream
720 and restores the current directory to the directory from which
727 returns 0 on success, and \-1 if an error occurs.
733 for any of the errors specified for the library functions
742 for any of the errors specified for the library functions
753 for any of the errors specified for the library functions
771 The options were invalid, or the list were empty.
782 interface was first introduced in
785 .Fn fts_get_clientptr ,
788 .Fn fts_set_clientptr
789 functions were introduced in
791 principally to provide for alternative interfaces to the
793 functionality using different data structures.
797 function will automatically set the
801 option is provided, or if it cannot
803 the current directory.