1 .\" $NetBSD: fts.3,v 1.13.2.1 1997/11/14 02:09:32 mrg 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. All advertising materials mentioning features or use of this software
15 .\" must display the following acknowledgement:
16 .\" This product includes software developed by the University of
17 .\" California, Berkeley and its contributors.
18 .\" 4. Neither the name of the University nor the names of its contributors
19 .\" may be used to endorse or promote products derived from this software
20 .\" without specific prior written permission.
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .\" @(#)fts.3 8.5 (Berkeley) 4/16/94
36 .\" 2007-12-08, mtk, Converted from mdoc to man macros
38 .TH FTS 3 2007-12-28 "Linux" "Linux Programmer's Manual"
40 fts, fts_open, fts_read, fts_children, fts_set, fts_close \- \
41 traverse a file hierarchy
44 .B #include <sys/types.h>
45 .B #include <sys/stat.h>
48 .BI "FTS *fts_open(char * const *" path_argv ", int " options ", "
49 .BI " int (*" compar ")(const FTSENT **, const FTSENT **));"
51 .BI "FTSENT *fts_read(FTS *" ftsp );
53 .BI "FTSENT *fts_children(FTS *" ftsp ", int " options );
55 .BI "int fts_set(FTS *" ftsp ", FTSENT *" f ", int " options );
57 .BI "int fts_close(FTS *" ftsp );
61 fts functions are provided for traversing
63 A simple overview is that the
65 function returns a "handle" on a file hierarchy, which is then supplied to
70 returns a pointer to a structure describing one of the files in the file
74 returns a pointer to a linked list of structures, each of which describes
75 one of the files contained in a directory in the hierarchy.
76 In general, directories are visited two distinguishable times; in preorder
77 (before any of their descendants are visited) and in postorder (after all
78 of their descendants have been visited).
79 Files are visited once.
80 It is possible to walk the hierarchy "logically" (ignoring symbolic links)
81 or physically (visiting symbolic links), order the walk of the hierarchy or
82 prune and/or revisit portions of the hierarchy.
84 Two structures are defined (and typedef'd) in the include file
88 the structure that represents the file hierarchy itself.
91 the structure that represents a file in the file
95 structure is returned for every file in the file
97 In this manual page, "file" and
99 are generally interchangeable.
102 structure contains at least the following fields, which are
103 described in greater detail below:
107 typedef struct _ftsent {
108 unsigned short fts_info; /* flags for FTSENT structure */
109 char *fts_accpath; /* access path */
110 char *fts_path; /* root path */
111 short fts_pathlen; /* strlen(fts_path) */
112 char *fts_name; /* filename */
113 short fts_namelen; /* strlen(fts_name) */
114 short fts_level; /* depth (\-1 to N) */
115 int fts_errno; /* file errno */
116 long fts_number; /* local numeric value */
117 void *fts_pointer; /* local address value */
118 struct ftsent *fts_parent; /* parent directory */
119 struct ftsent *fts_link; /* next file structure */
120 struct ftsent *fts_cycle; /* cycle structure */
121 struct stat *fts_statp; /* stat(2) information */
126 These fields are defined as follows:
127 .\" .Bl -tag -width "fts_namelen"
130 One of the following flags describing the returned
133 the file it represents.
134 With the exception of directories without errors
137 entries are terminal, that is, they will not be revisited, nor will any
138 of their descendants be visited.
139 .\" .Bl -tag -width FTS_DEFAULT
143 A directory being visited in preorder.
146 A directory that causes a cycle in the tree.
151 structure will be filled in as well.)
156 structure that represents a file type not explicitly described
162 A directory which cannot be read.
163 This is an error return, and the
165 field will be set to indicate what caused the error.
172 which was not specified as a filename to
178 A directory being visited in postorder.
181 structure will be unchanged from when
182 it was returned in preorder, that is, with the
188 This is an error return, and the
190 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.
209 information was requested.
218 A symbolic link with a nonexistent target.
221 field reference the file characteristic information for the symbolic link
227 A path for accessing the file from the current directory.
230 The path for the file relative to the root of the traversal.
231 This path contains the path specified to
236 The length of the string referenced by
240 The name of the file.
243 The length of the string referenced by
247 The depth of the traversal, numbered from \-1 to N, where this file
251 structure representing the parent of the starting point (or root)
252 of the traversal is numbered \-1, and the
254 structure for the root
255 itself is numbered 0.
273 field contains the value of the external variable
275 specifying the cause of the error.
276 Otherwise, the contents of the
281 This field is provided for the use of the application program and is
284 It is initialized to 0.
287 This field is provided for the use of the application program and is
296 structure referencing the file in the hierarchy
297 immediately above the current file, that is, the directory of which this
299 A parent structure for the initial entry point is provided as well,
305 fields are guaranteed to be initialized.
312 field points to the next structure in the NULL-terminated linked list of
314 Otherwise, the contents of the
319 If a directory causes a cycle in the hierarchy (see
322 of a hard link between two directories, or a symbolic link pointing to a
325 field of the structure will point to the
327 structure in the hierarchy that references the same file as the current
330 Otherwise, the contents of the
337 information for the file.
340 A single buffer is used for all of the paths of all of the files in the
346 fields are guaranteed to be
349 for the file most recently returned by
351 To use these fields to reference any files represented by other
353 structures will require that the path buffer be modified using the
354 information contained in that
359 Any such modifications should be undone before further calls to
369 function takes a pointer to an array of character pointers naming one
370 or more paths which make up a logical file hierarchy to be traversed.
371 The array must be terminated by a
376 a number of options, at least one of which (either
381 The options are selected by ORing
382 the following values:
383 .\" .Bl -tag -width "FTS_PHYSICAL"
386 This option causes any symbolic link specified as a root path to be
387 followed immediately whether or not
392 This option causes the
393 fts routines to return
395 structures for the targets of symbolic links
396 instead of the symbolic links themselves.
397 If this option is set, the only symbolic links for which
400 are returned to the application are those referencing nonexistent files.
411 As a performance optimization, the
412 fts functions change directories as they walk the file hierarchy.
413 This has the side-effect that an application cannot rely on being
414 in any particular directory during the traversal.
417 option turns off this optimization, and the
418 fts functions will not change the current directory.
419 Note that applications should not themselves change their current directory
420 and try to access files unless
422 is specified and absolute
423 pathnames were provided as arguments to
429 structures reference file characteristic information (the
431 field) for each file visited.
432 This option relaxes that requirement as a performance optimization,
434 fts functions to set the
438 and leave the contents of the
443 This option causes the
444 fts routines to return
446 structures for symbolic links themselves instead
447 of the target files they point to.
448 If this option is set,
450 structures for all symbolic links in the
451 hierarchy are returned to the application.
462 By default, unless they are specified as path arguments to
468 encountered in the file hierarchy are ignored.
469 This option causes the
470 fts routines to return
476 fts from descending into directories that have a different device number
477 than the file from which the descent began.
482 specifies a user-defined function which may be used to order the traversal
485 takes two pointers to pointers to
487 structures as arguments and
488 should return a negative value, zero, or a positive value to indicate
489 if the file referenced by its first argument comes before, in any order
490 with respect to, or after, the file referenced by its second argument.
500 be used in this comparison.
509 field may not either.
514 the directory traversal order is in the order listed in
516 for the root paths, and in the order listed in the directory for
521 function returns a pointer to an
523 structure describing a file in
525 Directories (that are readable and do not cause cycles) are visited at
526 least twice, once in preorder and once in postorder.
527 All other files are visited at least once.
528 (Hard links between directories that do not cause cycles or symbolic
529 links to symbolic links may cause files to be visited more than once,
530 or directories more than twice.)
532 If all the members of the hierarchy have been returned,
536 and sets the external variable
539 If an error unrelated to a file in the hierarchy occurs,
546 If an error related to a returned file occurs, a pointer to an
548 structure is returned, and
550 may or may not have been set (see
555 structures returned by
557 may be overwritten after a call to
559 on the same file hierarchy stream, or, after a call to
561 on the same file hierarchy stream unless they represent a file of type
562 directory, in which case they will not be overwritten until after a call to
566 structure has been returned by the function
572 function returns a pointer to an
574 structure describing the first entry in a NULL-terminated linked list of
575 the files in the directory represented by the
577 structure most recently returned by
579 The list is linked through the
583 structure, and is ordered by the user-specified comparison function, if any.
586 will recreate this linked list.
588 As a special case, if
590 has not yet been called for a hierarchy,
592 will return a pointer to the files in the logical directory specified to
594 that is, the arguments specified to
598 structure most recently returned by
600 is not a directory being visited in preorder,
601 or the directory does not contain any files,
618 structures returned by
620 may be overwritten after a call to
625 on the same file hierarchy stream.
628 may be set to the following value:
629 .\" .Bl -tag -width FTS_NAMEONLY
632 Only the names of the files are needed.
633 The contents of all the fields in the returned linked list of structures
634 are undefined with the exception of the
643 allows the user application to determine further processing for the
651 returns 0 on success, and \-1 if an error occurs.
653 must be set to one of the following values:
654 .\" .Bl -tag -width FTS_PHYSICAL
657 Re-visit the file; any file type may be revisited.
660 will return the referenced file.
665 fields of the structure will be reinitialized at that time,
666 but no other fields will have been changed.
667 This option is meaningful only for the most recently returned
670 Normal use is for postorder directory visits, where it causes the
671 directory to be revisited (in both preorder and postorder) as well as all
675 The referenced file must be a symbolic link.
676 If the referenced file is the one most recently returned by
680 returns the file with the
684 fields reinitialized to reflect the target of the symbolic link instead
685 of the symbolic link itself.
686 If the file is one of those most recently returned by
692 fields of the structure, when returned by
694 will reflect the target of the symbolic link instead of the symbolic link
696 In either case, if the target of the symbolic link does not exist the
697 fields of the returned structure will be unchanged and the
702 If the target of the link is a directory, the preorder return, followed
703 by the return of all of its descendants, followed by a postorder return,
707 No descendants of this file are visited.
708 The file may be one of those most recently returned by either
716 function closes a file hierarchy stream
718 and restores the current directory to the directory from which
725 returns 0 on success, and \-1 if an error occurs.
731 for any of the errors specified for
740 for any of the errors specified for
751 for any of the errors specified for
769 The options were invalid.
771 These functions are available in Linux since glibc2.
774 .\" The following statement is years old, and seems no closer to
775 .\" being true -- mtk
778 .\" utility is expected to be included in a future