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 .\" 4. 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
44 .Fn fts_open "char * const *path_argv" "int options" "int (*compar)(const FTSENT * const *, const FTSENT * const *)"
46 .Fn fts_read "FTS *ftsp"
48 .Fn fts_children "FTS *ftsp" "int options"
50 .Fn fts_set "FTS *ftsp" "FTSENT *f" "int options"
52 .Fn fts_set_clientptr "FTS *ftsp" "void *clientdata"
54 .Fn fts_get_clientptr "FTS *ftsp"
56 .Fn fts_get_stream "FTSENT *f"
58 .Fn fts_close "FTS *ftsp"
62 functions are provided for traversing
65 A simple overview is that the
69 on a file hierarchy, which is then supplied to
75 returns a pointer to a structure describing one of the files in the file
79 returns a pointer to a linked list of structures, each of which describes
80 one of the files contained in a directory in the hierarchy.
81 In general, directories are visited two distinguishable times; in pre-order
82 (before any of their descendants are visited) and in post-order (after all
83 of their descendants have been visited).
84 Files are visited once.
85 It is possible to walk the hierarchy
87 (ignoring symbolic links)
88 or physically (visiting symbolic links), order the walk of the hierarchy or
89 prune and/or re-visit portions of the hierarchy.
91 Two structures are defined (and typedef'd) in the include file
95 the structure that represents the file hierarchy itself.
98 the structure that represents a file in the file
102 structure is returned for every file in the file
107 .Dq Vt FTSENT No structure
113 structure contains space for a single pointer, which may be used to
114 store application data or per-hierarchy state.
116 .Fn fts_set_clientptr
118 .Fn fts_get_clientptr
119 functions may be used to set and retrieve this pointer.
120 This is likely to be useful only when accessed from the sort
121 comparison function, which can determine the original
123 stream of its arguments using the
128 functions are also available as macros of the same name.
132 structure contains at least the following fields, which are
133 described in greater detail below:
135 typedef struct _ftsent {
136 int fts_info; /* status for FTSENT structure */
137 char *fts_accpath; /* access path */
138 char *fts_path; /* root path */
139 size_t fts_pathlen; /* strlen(fts_path) */
140 char *fts_name; /* file name */
141 size_t fts_namelen; /* strlen(fts_name) */
142 long fts_level; /* depth (\-1 to N) */
143 int fts_errno; /* file errno */
144 long long fts_number; /* local numeric value */
145 void *fts_pointer; /* local address value */
146 struct ftsent *fts_parent; /* parent directory */
147 struct ftsent *fts_link; /* next file structure */
148 struct ftsent *fts_cycle; /* cycle structure */
149 struct stat *fts_statp; /* stat(2) information */
153 These fields are defined as follows:
154 .Bl -tag -width "fts_namelen"
156 One of the following values describing the returned
159 the file it represents.
160 With the exception of directories without errors
163 entries are terminal, that is, they will not be revisited, nor will any
164 of their descendants be visited.
165 .Bl -tag -width FTS_DEFAULT
167 A directory being visited in pre-order.
169 A directory that causes a cycle in the tree.
174 structure will be filled in as well.)
178 structure that represents a file type not explicitly described
183 A directory which cannot be read.
184 This is an error return, and the
186 field will be set to indicate what caused the error.
192 which was not specified as a file name to
197 A directory being visited in post-order.
200 structure will be unchanged from when
201 the directory was visited in pre-order, except for the
205 This is an error return, and the
207 field will be set to indicate what caused the error.
213 information was available.
217 This is an error return, and the
219 field will be set to indicate what caused the error.
223 information was requested.
230 A symbolic link with a non-existent target.
233 field reference the file characteristic information for the symbolic link
237 A path for accessing the file from the current directory.
239 The path for the file relative to the root of the traversal.
240 This path contains the path specified to
244 The length of the string referenced by
247 The name of the file.
249 The length of the string referenced by
252 The depth of the traversal, numbered from \-1 to N, where this file
256 structure representing the parent of the starting point (or root)
257 of the traversal is numbered
258 .Dv FTS_ROOTPARENTLEVEL
261 structure for the root
281 field contains the value of the external variable
283 specifying the cause of the error.
284 Otherwise, the contents of the
288 This field is provided for the use of the application program and is
292 It is initialized to 0.
294 This field is provided for the use of the application program and is
303 structure referencing the file in the hierarchy
304 immediately above the current file, i.e., the directory of which this
306 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 linked list of
321 Otherwise, the contents of the
325 If a directory causes a cycle in the hierarchy (see
328 of a hard link between two directories, or a symbolic link pointing to a
331 field of the structure will point to the
333 structure in the hierarchy that references the same file as the current
336 Otherwise, the contents of the
342 information for the file.
345 A single buffer is used for all of the paths of all of the files in the
351 fields are guaranteed to be
352 .Dv NUL Ns -terminated
354 for the file most recently returned by
356 To use these fields to reference any files represented by other
358 structures will require that the path buffer be modified using the
359 information contained in that
364 Any such modifications should be undone before further calls to
370 .Dv NUL Ns -terminated .
374 is mutually exclusive with the use of
381 function takes a pointer to an array of character pointers naming one
382 or more paths which make up a logical file hierarchy to be traversed.
383 The array must be terminated by a
388 a number of options, at least one of which (either
393 The options are selected by
395 the following values:
396 .Bl -tag -width "FTS_PHYSICAL"
398 This option causes any symbolic link specified as a root path to be
399 followed immediately whether or not
403 This option causes the
407 structures for the targets of symbolic links
408 instead of the symbolic links themselves.
409 If this option is set, the only symbolic links for which
412 are returned to the application are those referencing non-existent files.
422 As a performance optimization, the
424 functions change directories as they walk the file hierarchy.
425 This has the side-effect that an application cannot rely on being
426 in any particular directory during the traversal.
429 option turns off this optimization, and the
431 functions will not change the current directory.
432 Note that applications should not themselves change their current directory
433 and try to access files unless
435 is specified and absolute
436 pathnames were provided as arguments to
441 structures reference file characteristic information (the
443 field) for each file visited.
444 This option relaxes that requirement as a performance optimization,
451 and leave the contents of the
455 This option causes the
459 structures for symbolic links themselves instead
460 of the target files they point to.
461 If this option is set,
463 structures for all symbolic links in the
464 hierarchy are returned to the application.
474 By default, unless they are specified as path arguments to
480 encountered in the file hierarchy are ignored.
481 This option causes the
489 from descending into directories that have a different device number
490 than the file from which the descent began.
495 specifies a user-defined function which may be used to order the traversal
498 takes two pointers to pointers to
500 structures as arguments and
501 should return a negative value, zero, or a positive value to indicate
502 if the file referenced by its first argument comes before, in any order
503 with respect to, or after, the file referenced by its second argument.
513 be used in this comparison.
522 field may not either.
527 the directory traversal order is in the order listed in
529 for the root paths, and in the order listed in the directory for
534 function returns a pointer to an
536 structure describing a file in
538 Directories (that are readable and do not cause cycles) are visited at
539 least twice, once in pre-order and once in post-order.
540 All other files are visited at least once.
541 (Hard links between directories that do not cause cycles or symbolic
542 links to symbolic links may cause files to be visited more than once,
543 or directories more than twice.)
545 If all the members of the hierarchy have been returned,
549 and sets the external variable
552 If an error unrelated to a file in the hierarchy occurs,
559 If an error related to a returned file occurs, a pointer to an
561 structure is returned, and
563 may or may not have been set (see
568 structures returned by
570 may be overwritten after a call to
572 on the same file hierarchy stream, or, after a call to
574 on the same file hierarchy stream unless they represent a file of type
575 directory, in which case they will not be overwritten until after a call to
579 structure has been returned by the function
585 function returns a pointer to an
587 structure describing the first entry in a NULL-terminated linked list of
588 the files in the directory represented by the
590 structure most recently returned by
592 The list is linked through the
596 structure, and is ordered by the user-specified comparison function, if any.
599 will recreate this linked list.
601 As a special case, if
603 has not yet been called for a hierarchy,
605 will return a pointer to the files in the logical directory specified to
607 i.e., the arguments specified to
611 structure most recently returned by
613 is not a directory being visited in pre-order,
614 or the directory does not contain any files,
631 structures returned by
633 may be overwritten after a call to
638 on the same file hierarchy stream.
641 may be set to the following value:
642 .Bl -tag -width FTS_NAMEONLY
644 Only the names of the files are needed.
645 The contents of all the fields in the returned linked list of structures
646 are undefined with the exception of the
655 allows the user application to determine further processing for the
663 returns 0 on success, and \-1 if an error occurs.
665 must be set to one of the following values:
666 .Bl -tag -width FTS_PHYSICAL
668 Re-visit the file; any file type may be re-visited.
671 will return the referenced file.
676 fields of the structure will be reinitialized at that time,
677 but no other fields will have been changed.
678 This option is meaningful only for the most recently returned
681 Normal use is for post-order directory visits, where it causes the
682 directory to be re-visited (in both pre and post-order) as well as all
685 The referenced file must be a symbolic link.
686 If the referenced file is the one most recently returned by
690 returns the file with the
694 fields reinitialized to reflect the target of the symbolic link instead
695 of the symbolic link itself.
696 If the file is one of those most recently returned by
702 fields of the structure, when returned by
704 will reflect the target of the symbolic link instead of the symbolic link
706 In either case, if the target of the symbolic link does not exist the
707 fields of the returned structure will be unchanged and the
712 If the target of the link is a directory, the pre-order return, followed
713 by the return of all of its descendants, followed by a post-order return,
716 No descendants of this file are visited.
717 The file may be one of those most recently returned by either
725 function closes a file hierarchy stream
727 and restores the current directory to the directory from which
734 returns 0 on success, and \-1 if an error occurs.
740 for any of the errors specified for the library functions
749 for any of the errors specified for the library functions
760 for any of the errors specified for the library functions
778 The options were invalid.
789 interface was first introduced in
792 .Fn fts_get_clientptr ,
795 .Fn fts_set_clientptr
796 functions were introduced in
798 principally to provide for alternative interfaces to the
800 functionality using different data structures.