1 .\" $NetBSD: fts.3,v 1.29 2010/04/29 17:39:03 jruoho Exp $
3 .\" Copyright (c) 1989, 1991, 1993, 1994
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" @(#)fts.3 8.5 (Berkeley) 4/16/94
42 .Nd traverse a file hierarchy
51 .Fa "char * const *path_argv"
53 .Fa "int (*compar)(const FTSENT **, const FTSENT **)"
56 .Fn fts_read "FTS *ftsp"
58 .Fn fts_children "FTS *ftsp" "int options"
60 .Fn fts_set "FTS *ftsp" "FTSENT *f" "int options"
62 .Fn fts_close "FTS *ftsp"
66 functions are provided for traversing
69 A simple overview is that the
73 on a file hierarchy, which is then supplied to
79 returns a pointer to a structure describing one of the files in the file
83 returns a pointer to a linked list of structures, each of which describes
84 one of the files contained in a directory in the hierarchy.
85 In general, directories are visited two distinguishable times; in pre-order
86 (before any of their descendants are visited) and in post-order (after all
87 of their descendants have been visited).
88 Files are visited once.
89 It is possible to walk the hierarchy
91 (ignoring symbolic links)
92 or physically (visiting symbolic links), order the walk of the hierarchy or
93 prune and/or re-visit portions of the hierarchy.
95 Two structures are defined (and typedef'd) in the include file
99 the structure that represents the file hierarchy itself.
102 the structure that represents a file in the file
106 structure is returned for every file in the file
111 .Dq Fa FTSENT No structure
116 structure contains at least the following fields, which are
117 described in greater detail below:
119 typedef struct _ftsent {
120 u_short fts_info; /* flags for FTSENT structure */
121 char *fts_accpath; /* access path */
122 char *fts_path; /* root path */
123 short fts_pathlen; /* strlen(fts_path) */
124 char *fts_name; /* file name */
125 short fts_namelen; /* strlen(fts_name) */
126 short fts_level; /* depth (\-1 to N) */
127 int fts_errno; /* file errno */
128 long fts_number; /* local numeric value */
129 void *fts_pointer; /* local address value */
130 struct ftsent *fts_parent; /* parent directory */
131 struct ftsent *fts_link; /* next file structure */
132 struct ftsent *fts_cycle; /* cycle structure */
133 struct stat *fts_statp; /* stat(2) information */
137 These fields are defined as follows:
138 .Bl -tag -width "fts_namelen"
140 One of the following flags describing the returned
143 the file it represents.
144 With the exception of directories without errors
147 entries are terminal, that is, they will not be revisited, nor will any
148 of their descendants be visited.
149 .Bl -tag -width FTS_DEFAULT
151 A directory being visited in pre-order.
153 A directory that causes a cycle in the tree.
158 structure will be filled in as well).
162 structure that represents a file type not explicitly described
167 A directory which cannot be read.
168 This is an error return, and the
170 field will be set to indicate what caused the error.
176 which was not specified as a file name to
181 A directory being visited in post-order.
184 structure will be unchanged from when
185 it was returned in pre-order, i.e., with the
190 This is an error return, and the
192 field will be set to indicate what caused the error.
198 information was available.
202 This is an error return, and the
204 field will be set to indicate what caused the error.
208 information was requested.
215 A symbolic link with a non-existent target.
218 field reference the file characteristic information for the symbolic link
224 A path for accessing the file from the current directory.
226 The path for the file relative to the root of the traversal.
227 This path contains the path specified to
231 The length of the string referenced by
234 The name of the file.
236 The length of the string referenced by
239 The depth of the traversal, numbered from \-1 to N, where this file
243 structure representing the parent of the starting point (or root)
244 of the traversal is numbered \-1, and the
246 structure for the root
247 itself is numbered 0.
264 field contains the value of the external variable
266 specifying the cause of the error.
267 Otherwise, the contents of the
271 This field is provided for the use of the application program and is
275 It is initialized to 0.
277 This field is provided for the use of the application program and is
286 structure referencing the file in the hierarchy
287 immediately above the current file, i.e., the directory of which this
289 A parent structure for the initial entry point is provided as well,
295 fields are guaranteed to be initialized.
301 field points to the next structure in the
302 .Dv NULL Ns -terminated
303 linked list of directory members.
304 Otherwise, the contents of the
308 If a directory causes a cycle in the hierarchy (see
311 of a hard link between two directories, or a symbolic link pointing to a
314 field of the structure will point to the
316 structure in the hierarchy that references the same file as the current
319 Otherwise, the contents of the
325 information for the file.
328 A single buffer is used for all of the paths of all of the files in the
334 fields are guaranteed to be
335 .Dv NULL Ns -terminated
337 for the file most recently returned by
339 To use these fields to reference any files represented by other
341 structures will require that the path buffer be modified using the
342 information contained in that
347 Any such modifications should be undone before further calls to
353 .Dv NULL Ns -terminated .
357 function takes a pointer to an array of character pointers naming one
358 or more paths which make up a logical file hierarchy to be traversed.
359 The array must be terminated by a
364 a number of options, at least one of which (either
369 The options are selected by
371 the following values:
372 .Bl -tag -width "FTS_PHYSICAL"
374 This option causes any symbolic link specified as a root path to be
375 followed immediately whether or not
379 This option causes the
383 structures for the targets of symbolic links
384 instead of the symbolic links themselves.
385 If this option is set, the only symbolic links for which
388 are returned to the application are those referencing non-existent files.
398 As a performance optimization, the
400 functions change directories as they walk the file hierarchy.
401 This has the side-effect that an application cannot rely on being
402 in any particular directory during the traversal.
405 option turns off this optimization, and the
407 functions will not change the current directory.
408 Note that applications should not themselves change their current directory
409 and try to access files unless
411 is specified and absolute
412 pathnames were provided as arguments to
417 structures reference file characteristic information (the
419 field) for each file visited.
420 This option relaxes that requirement as a performance optimization,
427 and leave the contents of the
431 This option causes the
435 structures for symbolic links themselves instead
436 of the target files they point to.
437 If this option is set,
439 structures for all symbolic links in the
440 hierarchy are returned to the application.
450 By default, unless they are specified as path arguments to
456 encountered in the file hierarchy are ignored.
457 This option causes the
463 Return whiteout entries, which are normally hidden.
467 from descending into directories that have a different device number
468 than the file from which the descent began.
473 specifies a user-defined function which may be used to order the traversal
476 takes two pointers to pointers to
478 structures as arguments and
479 should return a negative value, zero, or a positive value to indicate
480 if the file referenced by its first argument comes before, in any order
481 with respect to, or after, the file referenced by its second argument.
491 be used in this comparison.
500 field may not either.
505 the directory traversal order is in the order listed in
507 for the root paths, and in the order listed in the directory for
512 function returns a pointer to an
514 structure describing a file in
516 Directories (that are readable and do not cause cycles) are visited at
517 least twice, once in pre-order and once in post-order.
518 All other files are visited at least once.
519 (Hard links between directories that do not cause cycles or symbolic
520 links to symbolic links may cause files to be visited more than once,
521 or directories more than twice.)
523 If all the members of the hierarchy have been returned,
527 and sets the external variable
530 If an error unrelated to a file in the hierarchy occurs,
537 If an error related to a returned file occurs, a pointer to an
539 structure is returned, and
541 may or may not have been set (see
546 structures returned by
548 may be overwritten after a call to
550 on the same file hierarchy stream, or, after a call to
552 on the same file hierarchy stream unless they represent a file of type
553 directory, in which case they will not be overwritten until after a call to
557 structure has been returned by the function
563 function returns a pointer to an
565 structure describing the first entry in a
566 .Dv NULL Ns -terminated
567 linked list of the files in the directory represented by the
569 structure most recently returned by
571 The list is linked through the
575 structure, and is ordered by the user-specified comparison function, if any.
578 will recreate this linked list.
580 As a special case, if
582 has not yet been called for a hierarchy,
584 will return a pointer to the files in the logical directory specified to
586 i.e., the arguments specified to
590 structure most recently returned by
592 is not a directory being visited in pre-order,
593 or the directory does not contain any files,
610 structures returned by
612 may be overwritten after a call to
617 on the same file hierarchy stream.
620 may be set to the following value:
621 .Bl -tag -width FTS_NAMEONLY
623 Only the names of the files are needed.
624 The contents of all the fields in the returned linked list of structures
625 are undefined with the exception of the
634 allows the user application to determine further processing for the
642 returns 0 on success, and \-1 if an error occurs.
644 must be set to one of the following values:
645 .Bl -tag -width FTS_PHYSICAL
647 Re-visit the file; any file type may be re-visited.
650 will return the referenced file.
655 fields of the structure will be reinitialized at that time,
656 but no other fields will have been changed.
657 This option is meaningful only for the most recently returned
660 Normal use is for post-order directory visits, where it causes the
661 directory to be re-visited (in both pre and post-order) as well as all
664 The referenced file must be a symbolic link.
665 If the referenced file is the one most recently returned by
669 returns the file with the
673 fields reinitialized to reflect the target of the symbolic link instead
674 of the symbolic link itself.
675 If the file is one of those most recently returned by
681 fields of the structure, when returned by
683 will reflect the target of the symbolic link instead of the symbolic link
685 In either case, if the target of the symbolic link does not exist the
686 fields of the returned structure will be unchanged and the
691 If the target of the link is a directory, the pre-order return, followed
692 by the return of all of its descendants, followed by a post-order return,
695 No descendants of this file are visited.
696 The file may be one of those most recently returned by either
704 function closes a file hierarchy stream
706 and restores the current directory to the directory from which
713 returns 0 on success, and \-1 if an error occurs.
719 for any of the errors specified for the library functions
728 for any of the errors specified for the library functions
739 for any of the errors specified for the library functions
757 The options were invalid.
768 utility was expected to be included in the
771 But twenty years later, it still was not included in the