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. All advertising materials mentioning features or use of this software
13 .\" must display the following acknowledgement:
14 .\" This product includes software developed by the University of
15 .\" California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" @(#)fts.3 8.5 (Berkeley) 4/16/94
33 .\" $FreeBSD: src/lib/libc/gen/fts.3,v 1.13 2001/09/20 12:32:45 ru Exp $
40 .Nd traverse a file hierarchy
48 .Fn fts_open "char * const *path_argv" "int options" "int (*compar)(const FTSENT **, const FTSENT **)"
50 .Fn fts_read "FTS *ftsp"
52 .Fn fts_children "FTS *ftsp" "int options"
54 .Fn fts_set "FTS *ftsp" "FTSENT *f" "int options"
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 Fa FTSENT No structure
110 structure contains at least the following fields, which are
111 described in greater detail below:
113 typedef struct _ftsent {
114 u_short fts_info; /* flags for FTSENT structure */
115 char *fts_accpath; /* access path */
116 char *fts_path; /* root path */
117 u_short fts_pathlen; /* strlen(fts_path) */
118 char *fts_name; /* file name */
119 u_short fts_namelen; /* strlen(fts_name) */
120 short fts_level; /* depth (\-1 to N) */
121 int fts_errno; /* file errno */
122 long fts_number; /* local numeric value */
123 void *fts_pointer; /* local address value */
124 struct ftsent *fts_parent; /* parent directory */
125 struct ftsent *fts_link; /* next file structure */
126 struct ftsent *fts_cycle; /* cycle structure */
127 struct stat *fts_statp; /* stat(2) information */
131 These fields are defined as follows:
132 .Bl -tag -width "fts_namelen"
134 One of the following values describing the returned
137 the file it represents.
138 With the exception of directories without errors
141 entries are terminal, that is, they will not be revisited, nor will any
142 of their descendants be visited.
143 .Bl -tag -width FTS_DEFAULT
145 A directory being visited in pre-order.
147 A directory that causes a cycle in the tree.
152 structure will be filled in as well.)
156 structure that represents a file type not explicitly described
161 A directory which cannot be read.
162 This is an error return, and the
164 field will be set to indicate what caused the error.
170 which was not specified as a file name to
175 A directory being visited in post-order.
178 structure will be unchanged from when
179 it was returned in pre-order, i.e. with the
184 This is an error return, and the
186 field will be set to indicate what caused the error.
192 information was available.
196 This is an error return, and the
198 field will be set to indicate what caused the error.
202 information was requested.
209 A symbolic link with a non-existent target.
212 field reference the file characteristic information for the symbolic link
216 A path for accessing the file from the current directory.
218 The path for the file relative to the root of the traversal.
219 This path contains the path specified to
223 The length of the string referenced by
226 The name of the file.
228 The length of the string referenced by
231 The depth of the traversal, numbered from \-1 to N, where this file
235 structure representing the parent of the starting point (or root)
236 of the traversal is numbered
237 .Dv FTS_ROOTPARENTLEVEL
240 structure for the root
260 field contains the value of the external variable
262 specifying the cause of the error.
263 Otherwise, the contents of the
267 This field is provided for the use of the application program and is
271 It is initialized to 0.
273 This field is provided for the use of the application program and is
282 structure referencing the file in the hierarchy
283 immediately above the current file, i.e. the directory of which this
285 A parent structure for the initial entry point is provided as well,
291 fields are guaranteed to be initialized.
297 field points to the next structure in the NULL-terminated linked list of
299 Otherwise, the contents of the
303 If a directory causes a cycle in the hierarchy (see
306 of a hard link between two directories, or a symbolic link pointing to a
309 field of the structure will point to the
311 structure in the hierarchy that references the same file as the current
314 Otherwise, the contents of the
320 information for the file.
323 A single buffer is used for all of the paths of all of the files in the
329 fields are guaranteed to be
330 .Dv NUL Ns -terminated
332 for the file most recently returned by
334 To use these fields to reference any files represented by other
336 structures will require that the path buffer be modified using the
337 information contained in that
342 Any such modifications should be undone before further calls to
348 .Dv NUL Ns -terminated .
352 function takes a pointer to an array of character pointers naming one
353 or more paths which make up a logical file hierarchy to be traversed.
354 The array must be terminated by a
359 a number of options, at least one of which (either
364 The options are selected by
366 the following values:
367 .Bl -tag -width "FTS_PHYSICAL"
369 This option causes any symbolic link specified as a root path to be
370 followed immediately whether or not
374 This option causes the
378 structures for the targets of symbolic links
379 instead of the symbolic links themselves.
380 If this option is set, the only symbolic links for which
383 are returned to the application are those referencing non-existent files.
393 As a performance optimization, the
395 functions change directories as they walk the file hierarchy.
396 This has the side-effect that an application cannot rely on being
397 in any particular directory during the traversal.
400 option turns off this optimization, and the
402 functions will not change the current directory.
403 Note that applications should not themselves change their current directory
404 and try to access files unless
406 is specified and absolute
407 pathnames were provided as arguments to
412 structures reference file characteristic information (the
414 field) for each file visited.
415 This option relaxes that requirement as a performance optimization,
422 and leave the contents of the
426 This option causes the
430 structures for symbolic links themselves instead
431 of the target files they point to.
432 If this option is set,
434 structures for all symbolic links in the
435 hierarchy are returned to the application.
445 By default, unless they are specified as path arguments to
451 encountered in the file hierarchy are ignored.
452 This option causes the
460 from descending into directories that have a different device number
461 than the file from which the descent began.
466 specifies a user-defined function which may be used to order the traversal
469 takes two pointers to pointers to
471 structures as arguments and
472 should return a negative value, zero, or a positive value to indicate
473 if the file referenced by its first argument comes before, in any order
474 with respect to, or after, the file referenced by its second argument.
484 be used in this comparison.
493 field may not either.
498 the directory traversal order is in the order listed in
500 for the root paths, and in the order listed in the directory for
505 function returns a pointer to an
507 structure describing a file in
509 Directories (that are readable and do not cause cycles) are visited at
510 least twice, once in pre-order and once in post-order.
511 All other files are visited at least once.
512 (Hard links between directories that do not cause cycles or symbolic
513 links to symbolic links may cause files to be visited more than once,
514 or directories more than twice.)
516 If all the members of the hierarchy have been returned,
520 and sets the external variable
523 If an error unrelated to a file in the hierarchy occurs,
530 If an error related to a returned file occurs, a pointer to an
532 structure is returned, and
534 may or may not have been set (see
539 structures returned by
541 may be overwritten after a call to
543 on the same file hierarchy stream, or, after a call to
545 on the same file hierarchy stream unless they represent a file of type
546 directory, in which case they will not be overwritten until after a call to
550 structure has been returned by the function
556 function returns a pointer to an
558 structure describing the first entry in a NULL-terminated linked list of
559 the files in the directory represented by the
561 structure most recently returned by
563 The list is linked through the
567 structure, and is ordered by the user-specified comparison function, if any.
570 will recreate this linked list.
572 As a special case, if
574 has not yet been called for a hierarchy,
576 will return a pointer to the files in the logical directory specified to
578 i.e. the arguments specified to
582 structure most recently returned by
584 is not a directory being visited in pre-order,
585 or the directory does not contain any files,
602 structures returned by
604 may be overwritten after a call to
609 on the same file hierarchy stream.
612 may be set to the following value:
613 .Bl -tag -width FTS_NAMEONLY
615 Only the names of the files are needed.
616 The contents of all the fields in the returned linked list of structures
617 are undefined with the exception of the
626 allows the user application to determine further processing for the
634 returns 0 on success, and \-1 if an error occurs.
636 must be set to one of the following values:
637 .Bl -tag -width FTS_PHYSICAL
639 Re-visit the file; any file type may be re-visited.
642 will return the referenced file.
647 fields of the structure will be reinitialized at that time,
648 but no other fields will have been changed.
649 This option is meaningful only for the most recently returned
652 Normal use is for post-order directory visits, where it causes the
653 directory to be re-visited (in both pre and post-order) as well as all
656 The referenced file must be a symbolic link.
657 If the referenced file is the one most recently returned by
661 returns the file with the
665 fields reinitialized to reflect the target of the symbolic link instead
666 of the symbolic link itself.
667 If the file is one of those most recently returned by
673 fields of the structure, when returned by
675 will reflect the target of the symbolic link instead of the symbolic link
677 In either case, if the target of the symbolic link does not exist the
678 fields of the returned structure will be unchanged and the
683 If the target of the link is a directory, the pre-order return, followed
684 by the return of all of its descendants, followed by a post-order return,
687 No descendants of this file are visited.
688 The file may be one of those most recently returned by either
696 function closes a file hierarchy stream
698 and restores the current directory to the directory from which
705 returns 0 on success, and \-1 if an error occurs.
711 for any of the errors specified for the library functions
720 for any of the errors specified for the library functions
731 for any of the errors specified for the library functions
749 The options were invalid.
759 utility is expected to be included in a future