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
35 .Nd traverse a file hierarchy
41 .Fn fts_open "char * const *path_argv" "int options" "int (*compar)(const FTSENT * const *, const FTSENT * const *)"
43 .Fn fts_read "FTS *ftsp"
45 .Fn fts_children "FTS *ftsp" "int options"
47 .Fn fts_set "FTS *ftsp" "FTSENT *f" "int options"
49 .Fn fts_set_clientptr "FTS *ftsp" "void *clientdata"
51 .Fn fts_get_clientptr "FTS *ftsp"
53 .Fn fts_get_stream "FTSENT *f"
55 .Fn fts_close "FTS *ftsp"
59 functions are provided for traversing
62 A simple overview is that the
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 in the hierarchy.
78 In general, directories are visited two distinguishable times; in pre-order
79 (before any of their descendants are visited) and in post-order (after all
80 of their descendants have been visited).
81 Files are visited once.
82 It is possible to walk the hierarchy
84 (ignoring symbolic links)
85 or physically (visiting symbolic links), order the walk of the hierarchy or
86 prune and/or re-visit portions of the hierarchy.
88 Two structures are defined (and typedef'd) in the include file
92 the structure that represents the file hierarchy itself.
95 the structure that represents a file in the file
99 structure is returned for every file in the file
104 .Dq Vt FTSENT No structure
110 structure contains space for a single pointer, which may be used to
111 store application data or per-hierarchy state.
113 .Fn fts_set_clientptr
115 .Fn fts_get_clientptr
116 functions may be used to set and retrieve this pointer.
117 This is likely to be useful only when accessed from the sort
118 comparison function, which can determine the original
120 stream of its arguments using the
125 functions are also available as macros of the same name.
129 structure contains at least the following fields, which are
130 described in greater detail below:
132 typedef struct _ftsent {
133 int fts_info; /* status for FTSENT structure */
134 char *fts_accpath; /* access path */
135 char *fts_path; /* root path */
136 size_t fts_pathlen; /* strlen(fts_path) */
137 char *fts_name; /* file name */
138 size_t fts_namelen; /* strlen(fts_name) */
139 long fts_level; /* depth (\-1 to N) */
140 int fts_errno; /* file errno */
141 long long fts_number; /* local numeric value */
142 void *fts_pointer; /* local address value */
143 struct ftsent *fts_parent; /* parent directory */
144 struct ftsent *fts_link; /* next file structure */
145 struct ftsent *fts_cycle; /* cycle structure */
146 struct stat *fts_statp; /* stat(2) information */
150 These fields are defined as follows:
151 .Bl -tag -width "fts_namelen"
153 One of the following values describing the returned
156 the file it represents.
157 With the exception of directories without errors
160 entries are terminal, that is, they will not be revisited, nor will any
161 of their descendants be visited.
162 .Bl -tag -width FTS_DEFAULT
164 A directory being visited in pre-order.
166 A directory that causes a cycle in the tree.
171 structure will be filled in as well.)
175 structure that represents a file type not explicitly described
180 A directory which cannot be read.
181 This is an error return, and the
183 field will be set to indicate what caused the error.
189 which was not specified as a file name to
194 A directory being visited in post-order.
197 structure will be unchanged from when
198 the directory was visited in pre-order, except for the
202 This is an error return, and the
204 field will be set to indicate what caused the error.
210 information was available.
214 This is an error return, and the
216 field will be set to indicate what caused the error.
220 information was requested.
227 A symbolic link with a non-existent target.
230 field reference the file characteristic information for the symbolic link
234 A path for accessing the file from the current directory.
236 The path for the file relative to the root of the traversal.
237 This path contains the path specified to
241 The length of the string referenced by
244 The name of the file.
246 The length of the string referenced by
249 The depth of the traversal, numbered from \-1 to N, where this file
253 structure representing the parent of the starting point (or root)
254 of the traversal is numbered
255 .Dv FTS_ROOTPARENTLEVEL
258 structure for the root
278 field contains the value of the external variable
280 specifying the cause of the error.
281 Otherwise, the contents of the
285 This field is provided for the use of the application program and is
289 It is initialized to 0.
291 This field is provided for the use of the application program and is
300 structure referencing the file in the hierarchy
301 immediately above the current file, i.e., the directory of which this
303 A parent structure for the initial entry point is provided as well,
309 fields are guaranteed to be initialized.
315 field points to the next structure in the NULL-terminated linked list of
317 Otherwise, the contents of the
321 If a directory causes a cycle in the hierarchy (see
324 of a hard link between two directories, or a symbolic link pointing to a
327 field of the structure will point to the
329 structure in the hierarchy that references the same file as the current
332 Otherwise, the contents of the
338 information for the file.
341 A single buffer is used for all of the paths of all of the files in the
347 fields are guaranteed to be
348 .Dv NUL Ns -terminated
350 for the file most recently returned by
352 To use these fields to reference any files represented by other
354 structures will require that the path buffer be modified using the
355 information contained in that
360 Any such modifications should be undone before further calls to
366 .Dv NUL Ns -terminated .
370 function takes a pointer to an array of character pointers naming one
371 or more paths which make up a logical file hierarchy to be traversed.
372 The array must be terminated by a
377 a number of options, at least one of which (either
382 The options are selected by
384 the following values:
385 .Bl -tag -width "FTS_PHYSICAL"
387 This option causes any symbolic link specified as a root path to be
388 followed immediately whether or not
392 This option causes the
396 structures for the targets of symbolic links
397 instead of the symbolic links themselves.
398 If this option is set, the only symbolic links for which
401 are returned to the application are those referencing non-existent files.
411 To allow descending to arbitrary depths
414 and improve performance, 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 feature, 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 (the
435 field) for each file visited.
436 This option relaxes that requirement as a performance optimization,
443 and leave the contents of the
447 This option causes the
451 structures for symbolic links themselves instead
452 of the target files they point to.
453 If this option is set,
455 structures for all symbolic links in the
456 hierarchy are returned to the application.
466 By default, unless they are specified as path arguments to
472 encountered in the file hierarchy are ignored.
473 This option causes the
481 from descending into directories that have a different device number
482 than the file from which the descent began.
487 specifies a user-defined function which may be used to order the traversal
490 takes two pointers to pointers to
492 structures as arguments and
493 should return a negative value, zero, or a positive value to indicate
494 if the file referenced by its first argument comes before, in any order
495 with respect to, or after, the file referenced by its second argument.
505 be used in this comparison.
514 field may not either.
519 the directory traversal order is in the order listed in
521 for the root paths, and in the order listed in the directory for
526 function returns a pointer to an
528 structure describing a file in
530 Directories (that are readable and do not cause cycles) are visited at
531 least twice, once in pre-order and once in post-order.
532 All other files are visited at least once.
533 (Hard links between directories that do not cause cycles or symbolic
534 links to symbolic links may cause files to be visited more than once,
535 or directories more than twice.)
537 If all the members of the hierarchy have been returned,
541 and sets the external variable
544 If an error unrelated to a file in the hierarchy occurs,
551 If an error related to a returned file occurs, a pointer to an
553 structure is returned, and
555 may or may not have been set (see
560 structures returned by
562 may be overwritten after a call to
564 on the same file hierarchy stream, or, after a call to
566 on the same file hierarchy stream unless they represent a file of type
567 directory, in which case they will not be overwritten until after a call to
571 structure has been returned by the function
577 function returns a pointer to an
579 structure describing the first entry in a NULL-terminated linked list of
580 the files in the directory represented by the
582 structure most recently returned by
584 The list is linked through the
588 structure, and is ordered by the user-specified comparison function, if any.
591 will recreate this linked list.
593 As a special case, if
595 has not yet been called for a hierarchy,
597 will return a pointer to the files in the logical directory specified to
599 i.e., the arguments specified to
603 structure most recently returned by
605 is not a directory being visited in pre-order,
606 or the directory does not contain any files,
623 structures returned by
625 may be overwritten after a call to
630 on the same file hierarchy stream.
633 may be set to the following value:
634 .Bl -tag -width FTS_NAMEONLY
636 Only the names of the files are needed.
637 The contents of all the fields in the returned linked list of structures
638 are undefined with the exception of the
647 allows the user application to determine further processing for the
655 returns 0 on success, and \-1 if an error occurs.
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
726 returns 0 on success, and \-1 if an error occurs.
732 for any of the errors specified for the library functions
741 for any of the errors specified for the library functions
752 for any of the errors specified for the library functions
770 The options were invalid, or the list were empty.
781 interface was first introduced in
784 .Fn fts_get_clientptr ,
787 .Fn fts_set_clientptr
788 functions were introduced in
790 principally to provide for alternative interfaces to the
792 functionality using different data structures.
796 function will automatically set the
800 option is provided, or if it cannot
802 the current directory.