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
29 .\" $FreeBSD: src/lib/libc/gen/fts.3,v 1.24 2008/01/26 17:09:40 yar Exp $
30 .\" $DragonFly: src/lib/libc/gen/fts.3,v 1.4 2007/07/30 22:11:32 swildner Exp $
37 .Nd traverse a file hierarchy
45 .Fn fts_open "char * const *path_argv" "int options" "int (*compar)(const FTSENT * const *, const FTSENT * const *)"
47 .Fn fts_read "FTS *ftsp"
49 .Fn fts_children "FTS *ftsp" "int options"
51 .Fn fts_set "FTS *ftsp" "FTSENT *f" "int options"
53 .Fn fts_set_clientptr "FTS *ftsp" "void *clientdata"
55 .Fn fts_get_clientptr "FTS *ftsp"
57 .Fn fts_get_stream "FTSENT *f"
59 .Fn fts_close "FTS *ftsp"
63 functions are provided for traversing
66 A simple overview is that the
70 on a file hierarchy, which is then supplied to
76 returns a pointer to a structure describing one of the files in the file
80 returns a pointer to a linked list of structures, each of which describes
81 one of the files contained in a directory in the hierarchy.
82 In general, directories are visited two distinguishable times; in pre-order
83 (before any of their descendants are visited) and in post-order (after all
84 of their descendants have been visited).
85 Files are visited once.
86 It is possible to walk the hierarchy
88 (ignoring symbolic links)
89 or physically (visiting symbolic links), order the walk of the hierarchy or
90 prune and/or re-visit portions of the hierarchy.
92 Two structures are defined (and typedef'd) in the include file
96 the structure that represents the file hierarchy itself.
99 the structure that represents a file in the file
103 structure is returned for every file in the file
108 .Dq Vt FTSENT No structure
114 structure contains space for a single pointer, which may be used to
115 store application data or per-hierarchy state.
117 .Fn fts_set_clientptr
119 .Fn fts_get_clientptr
120 functions may be used to set and retrieve this pointer.
121 This is likely to be useful only when accessed from the sort
122 comparison function, which can determine the original
124 stream of its arguments using the
129 functions are also available as macros of the same name.
133 structure contains at least the following fields, which are
134 described in greater detail below:
136 typedef struct _ftsent {
137 int fts_info; /* status for FTSENT structure */
138 char *fts_accpath; /* access path */
139 char *fts_path; /* root path */
140 size_t fts_pathlen; /* strlen(fts_path) */
141 char *fts_name; /* file name */
142 size_t fts_namelen; /* strlen(fts_name) */
143 long fts_level; /* depth (\-1 to N) */
144 int fts_errno; /* file errno */
145 long long fts_number; /* local numeric value */
146 void *fts_pointer; /* local address value */
147 struct ftsent *fts_parent; /* parent directory */
148 struct ftsent *fts_link; /* next file structure */
149 struct ftsent *fts_cycle; /* cycle structure */
150 struct stat *fts_statp; /* stat(2) information */
154 These fields are defined as follows:
155 .Bl -tag -width "fts_namelen"
157 One of the following values describing the returned
160 the file it represents.
161 With the exception of directories without errors
164 entries are terminal, that is, they will not be revisited, nor will any
165 of their descendants be visited.
166 .Bl -tag -width FTS_DEFAULT
168 A directory being visited in pre-order.
170 A directory that causes a cycle in the tree.
175 structure will be filled in as well.)
179 structure that represents a file type not explicitly described
184 A directory which cannot be read.
185 This is an error return, and the
187 field will be set to indicate what caused the error.
193 which was not specified as a file name to
198 A directory being visited in post-order.
201 structure will be unchanged from when
202 it was returned in pre-order, i.e., with the
207 This is an error return, and the
209 field will be set to indicate what caused the error.
215 information was available.
219 This is an error return, and the
221 field will be set to indicate what caused the error.
225 information was requested.
232 A symbolic link with a non-existent target.
235 field reference the file characteristic information for the symbolic link
239 A path for accessing the file from the current directory.
241 The path for the file relative to the root of the traversal.
242 This path contains the path specified to
246 The length of the string referenced by
249 The name of the file.
251 The length of the string referenced by
254 The depth of the traversal, numbered from \-1 to N, where this file
258 structure representing the parent of the starting point (or root)
259 of the traversal is numbered
260 .Dv FTS_ROOTPARENTLEVEL
263 structure for the root
283 field contains the value of the external variable
285 specifying the cause of the error.
286 Otherwise, the contents of the
290 This field is provided for the use of the application program and is
294 It is initialized to 0.
296 This field is provided for the use of the application program and is
305 structure referencing the file in the hierarchy
306 immediately above the current file, i.e., the directory of which this
308 A parent structure for the initial entry point is provided as well,
315 fields are guaranteed to be initialized.
321 field points to the next structure in the NULL-terminated linked list of
323 Otherwise, the contents of the
327 If a directory causes a cycle in the hierarchy (see
330 of a hard link between two directories, or a symbolic link pointing to a
333 field of the structure will point to the
335 structure in the hierarchy that references the same file as the current
338 Otherwise, the contents of the
344 information for the file.
347 A single buffer is used for all of the paths of all of the files in the
353 fields are guaranteed to be
354 .Dv NUL Ns -terminated
356 for the file most recently returned by
358 To use these fields to reference any files represented by other
360 structures will require that the path buffer be modified using the
361 information contained in that
366 Any such modifications should be undone before further calls to
372 .Dv NUL Ns -terminated .
376 is mutually exclusive with the use of
383 function takes a pointer to an array of character pointers naming one
384 or more paths which make up a logical file hierarchy to be traversed.
385 The array must be terminated by a
390 a number of options, at least one of which (either
395 The options are selected by
397 the following values:
398 .Bl -tag -width "FTS_PHYSICAL"
400 This option causes any symbolic link specified as a root path to be
401 followed immediately whether or not
405 This option causes the
409 structures for the targets of symbolic links
410 instead of the symbolic links themselves.
411 If this option is set, the only symbolic links for which
414 are returned to the application are those referencing non-existent files.
424 As a performance optimization, the
426 functions change directories as they walk the file hierarchy.
427 This has the side-effect that an application cannot rely on being
428 in any particular directory during the traversal.
431 option turns off this optimization, and the
433 functions will not change the current directory.
434 Note that applications should not themselves change their current directory
435 and try to access files unless
437 is specified and absolute
438 pathnames were provided as arguments to
443 structures reference file characteristic information (the
445 field) for each file visited.
446 This option relaxes that requirement as a performance optimization,
453 and leave the contents of the
457 This option causes the
461 structures for symbolic links themselves instead
462 of the target files they point to.
463 If this option is set,
465 structures for all symbolic links in the
466 hierarchy are returned to the application.
476 By default, unless they are specified as path arguments to
482 encountered in the file hierarchy are ignored.
483 This option causes the
491 from descending into directories that have a different device number
492 than the file from which the descent began.
497 specifies a user-defined function which may be used to order the traversal
500 takes two pointers to pointers to
502 structures as arguments and
503 should return a negative value, zero, or a positive value to indicate
504 if the file referenced by its first argument comes before, in any order
505 with respect to, or after, the file referenced by its second argument.
515 be used in this comparison.
524 field may not either.
529 the directory traversal order is in the order listed in
531 for the root paths, and in the order listed in the directory for
536 function returns a pointer to an
538 structure describing a file in
540 Directories (that are readable and do not cause cycles) are visited at
541 least twice, once in pre-order and once in post-order.
542 All other files are visited at least once.
543 (Hard links between directories that do not cause cycles or symbolic
544 links to symbolic links may cause files to be visited more than once,
545 or directories more than twice.)
547 If all the members of the hierarchy have been returned,
551 and sets the external variable
554 If an error unrelated to a file in the hierarchy occurs,
561 If an error related to a returned file occurs, a pointer to an
563 structure is returned, and
565 may or may not have been set (see
570 structures returned by
572 may be overwritten after a call to
574 on the same file hierarchy stream, or, after a call to
576 on the same file hierarchy stream unless they represent a file of type
577 directory, in which case they will not be overwritten until after a call to
581 structure has been returned by the function
587 function returns a pointer to an
589 structure describing the first entry in a NULL-terminated linked list of
590 the files in the directory represented by the
592 structure most recently returned by
594 The list is linked through the
598 structure, and is ordered by the user-specified comparison function, if any.
601 will recreate this linked list.
603 As a special case, if
605 has not yet been called for a hierarchy,
607 will return a pointer to the files in the logical directory specified to
609 i.e., the arguments specified to
613 structure most recently returned by
615 is not a directory being visited in pre-order,
616 or the directory does not contain any files,
633 structures returned by
635 may be overwritten after a call to
640 on the same file hierarchy stream.
643 may be set to the following value:
644 .Bl -tag -width FTS_NAMEONLY
646 Only the names of the files are needed.
647 The contents of all the fields in the returned linked list of structures
648 are undefined with the exception of the
657 allows the user application to determine further processing for the
665 returns 0 on success, and \-1 if an error occurs.
667 must be set to one of the following values:
668 .Bl -tag -width FTS_PHYSICAL
670 Re-visit the file; any file type may be re-visited.
673 will return the referenced file.
678 fields of the structure will be reinitialized at that time,
679 but no other fields will have been changed.
680 This option is meaningful only for the most recently returned
683 Normal use is for post-order directory visits, where it causes the
684 directory to be re-visited (in both pre and post-order) as well as all
687 The referenced file must be a symbolic link.
688 If the referenced file is the one most recently returned by
692 returns the file with the
696 fields reinitialized to reflect the target of the symbolic link instead
697 of the symbolic link itself.
698 If the file is one of those most recently returned by
704 fields of the structure, when returned by
706 will reflect the target of the symbolic link instead of the symbolic link
708 In either case, if the target of the symbolic link does not exist the
709 fields of the returned structure will be unchanged and the
714 If the target of the link is a directory, the pre-order return, followed
715 by the return of all of its descendants, followed by a post-order return,
718 No descendants of this file are visited.
719 The file may be one of those most recently returned by either
727 function closes a file hierarchy stream
729 and restores the current directory to the directory from which
736 returns 0 on success, and \-1 if an error occurs.
742 for any of the errors specified for the library functions
751 for any of the errors specified for the library functions
762 for any of the errors specified for the library functions
780 The options were invalid.
791 interface was first introduced in
794 .Fn fts_get_clientptr ,
797 .Fn fts_set_clientptr
798 functions were introduced in
800 principally to provide for alternative interfaces to the
802 functionality using different data structures.