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 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\" notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\" notice, this list of conditions and the following disclaimer in the
14 .\" documentation and/or other materials provided with the distribution.
15 .\" 3. All advertising materials mentioning features or use of this software
16 .\" must display the following acknowledgement:
17 .\" This product includes software developed by the University of
18 .\" California, Berkeley and its contributors.
19 .\" 4. Neither the name of the University nor the names of its contributors
20 .\" may be used to endorse or promote products derived from this software
21 .\" without specific prior written permission.
23 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 .\" @(#)fts.3 8.5 (Berkeley) 4/16/94
38 .\" 2007-12-08, mtk, Converted from mdoc to man macros
40 .TH FTS 3 2014-03-18 "Linux" "Linux Programmer's Manual"
42 fts, fts_open, fts_read, fts_children, fts_set, fts_close \- \
43 traverse a file hierarchy
46 .B #include <sys/types.h>
47 .B #include <sys/stat.h>
50 .BI "FTS *fts_open(char * const *" path_argv ", int " options ", "
51 .BI " int (*" compar ")(const FTSENT **, const FTSENT **));"
53 .BI "FTSENT *fts_read(FTS *" ftsp );
55 .BI "FTSENT *fts_children(FTS *" ftsp ", int " options );
57 .BI "int fts_set(FTS *" ftsp ", FTSENT *" f ", int " options );
59 .BI "int fts_close(FTS *" ftsp );
63 fts functions are provided for traversing
65 A simple overview is that the
67 function returns a "handle" on a file hierarchy, which is then supplied to
72 returns a pointer to a structure describing one of the files in the file
76 returns a pointer to a linked list of structures, each of which describes
77 one of the files contained in a directory in the hierarchy.
78 In general, directories are visited two distinguishable times; in preorder
79 (before any of their descendants are visited) and in postorder (after all
80 of their descendants have been visited).
81 Files are visited once.
82 It is possible to walk the hierarchy "logically" (visiting the files that
83 symbolic links point to)
84 or physically (visiting the symbolic links themselves),
85 order the walk of the hierarchy or
86 prune and/or revisit portions of the hierarchy.
88 Two structures are defined (and typedef'd) in the include file
92 the structure that represents the file hierarchy itself.
95 the structure that represents a file in the file
99 structure is returned for every file in the file
101 In this manual page, "file" and
103 are generally interchangeable.
106 structure contains at least the following fields, which are
107 described in greater detail below:
111 typedef struct _ftsent {
112 unsigned short fts_info; /* flags for FTSENT structure */
113 char *fts_accpath; /* access path */
114 char *fts_path; /* root path */
115 short fts_pathlen; /* strlen(fts_path) */
116 char *fts_name; /* filename */
117 short fts_namelen; /* strlen(fts_name) */
118 short fts_level; /* depth (\-1 to N) */
119 int fts_errno; /* file errno */
120 long fts_number; /* local numeric value */
121 void *fts_pointer; /* local address value */
122 struct ftsent *fts_parent; /* parent directory */
123 struct ftsent *fts_link; /* next file structure */
124 struct ftsent *fts_cycle; /* cycle structure */
125 struct stat *fts_statp; /* stat(2) information */
130 These fields are defined as follows:
131 .\" .Bl -tag -width "fts_namelen"
134 One of the following flags 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
147 A directory being visited in preorder.
150 A directory that causes a cycle in the tree.
155 structure will be filled in as well.)
160 structure that represents a file type not explicitly described
166 A directory which cannot be read.
167 This is an error return, and the
169 field will be set to indicate what caused the error.
176 which was not specified as a filename to
182 A directory being visited in postorder.
185 structure will be unchanged from when
186 it was returned in preorder, that is, with the
192 This is an error return, and the
194 field will be set to indicate what caused the error.
202 information was available.
206 This is an error return, and the
208 field will be set to indicate what caused the error.
213 information was requested.
222 A symbolic link with a nonexistent target.
225 field reference the file characteristic information for the symbolic link
231 A path for accessing the file from the current directory.
234 The path for the file relative to the root of the traversal.
235 This path contains the path specified to
240 The length of the string referenced by
244 The name of the file.
247 The length of the string referenced by
251 The depth of the traversal, numbered from \-1 to N, where this file
255 structure representing the parent of the starting point (or root)
256 of the traversal is numbered \-1, and the
258 structure for the root
259 itself is numbered 0.
277 field contains the value of the external variable
279 specifying the cause of the error.
280 Otherwise, the contents of the
285 This field is provided for the use of the application program and is
288 It is initialized to 0.
291 This field is provided for the use of the application program and is
300 structure referencing the file in the hierarchy
301 immediately above the current file, that is, the directory of which this
303 A parent structure for the initial entry point is provided as well,
309 fields are guaranteed to be initialized.
316 field points to the next structure in the NULL-terminated linked list of
318 Otherwise, the contents of the
323 If a directory causes a cycle in the hierarchy (see
326 of a hard link between two directories, or a symbolic link pointing to a
329 field of the structure will point to the
331 structure in the hierarchy that references the same file as the current
334 Otherwise, the contents of the
341 information for the file.
344 A single buffer is used for all of the paths of all of the files in the
350 fields are guaranteed to be
353 for the file most recently returned by
355 To use these fields to reference any files represented by other
357 structures will require that the path buffer be modified using the
358 information contained in that
363 Any such modifications should be undone before further calls to
373 function takes a pointer to an array of character pointers naming one
374 or more paths which make up a logical file hierarchy to be traversed.
375 The array must be terminated by a
379 a number of options, at least one of which (either
384 The options are selected by ORing
385 the following values:
386 .\" .Bl -tag -width "FTS_PHYSICAL"
389 This option causes any symbolic link specified as a root path to be
390 followed immediately whether or not
395 This option causes the
396 fts routines to return
398 structures for the targets of symbolic links
399 instead of the symbolic links themselves.
400 If this option is set, the only symbolic links for which
403 are returned to the application are those referencing nonexistent files.
414 As a performance optimization, the
415 fts functions change directories as they walk the file hierarchy.
416 This has the side-effect that an application cannot rely on being
417 in any particular directory during the traversal.
420 option turns off this optimization, and the
421 fts functions will not change the current directory.
422 Note that applications should not themselves change their current directory
423 and try to access files unless
425 is specified and absolute
426 pathnames were provided as arguments to
432 structures reference file characteristic information (the
434 field) for each file visited.
435 This option relaxes that requirement as a performance optimization,
437 fts functions to set the
441 and leave the contents of the
446 This option causes the
447 fts routines to return
449 structures for symbolic links themselves instead
450 of the target files they point to.
451 If this option is set,
453 structures for all symbolic links in the
454 hierarchy are returned to the application.
465 By default, unless they are specified as path arguments to
471 encountered in the file hierarchy are ignored.
472 This option causes the
473 fts routines to return
479 fts from descending into directories that have a different device number
480 than the file from which the descent began.
485 specifies a user-defined function which may be used to order the traversal
488 takes two pointers to pointers to
490 structures as arguments and
491 should return a negative value, zero, or a positive value to indicate
492 if the file referenced by its first argument comes before, in any order
493 with respect to, or after, the file referenced by its second argument.
503 be used in this comparison.
512 field may not either.
517 the directory traversal order is in the order listed in
519 for the root paths, and in the order listed in the directory for
524 function returns a pointer to an
526 structure describing a file in
528 Directories (that are readable and do not cause cycles) are visited at
529 least twice, once in preorder and once in postorder.
530 All other files are visited at least once.
531 (Hard links between directories that do not cause cycles or symbolic
532 links to symbolic links may cause files to be visited more than once,
533 or directories more than twice.)
535 If all the members of the hierarchy have been returned,
539 and sets the external variable
542 If an error unrelated to a file in the hierarchy occurs,
549 If an error related to a returned file occurs, a pointer to an
551 structure is returned, and
553 may or may not have been set (see
558 structures returned by
560 may be overwritten after a call to
562 on the same file hierarchy stream, or, after a call to
564 on the same file hierarchy stream unless they represent a file of type
565 directory, in which case they will not be overwritten until after a call to
569 structure has been returned by the function
575 function returns a pointer to an
577 structure describing the first entry in a NULL-terminated linked list of
578 the files in the directory represented by the
580 structure most recently returned by
582 The list is linked through the
586 structure, and is ordered by the user-specified comparison function, if any.
589 will re-create this linked list.
591 As a special case, if
593 has not yet been called for a hierarchy,
595 will return a pointer to the files in the logical directory specified to
597 that is, the arguments specified to
601 structure most recently returned by
603 is not a directory being visited in preorder,
604 or the directory does not contain any files,
621 structures returned by
623 may be overwritten after a call to
628 on the same file hierarchy stream.
631 may be set to the following value:
632 .\" .Bl -tag -width FTS_NAMEONLY
635 Only the names of the files are needed.
636 The contents of all the fields in the returned linked list of structures
637 are undefined with the exception of the
646 allows the user application to determine further processing for the
654 returns 0 on success, and \-1 if an error occurs.
656 must be set to one of the following values:
657 .\" .Bl -tag -width FTS_PHYSICAL
660 Revisit the file; any file type may be revisited.
663 will return the referenced file.
668 fields of the structure will be reinitialized at that time,
669 but no other fields will have been changed.
670 This option is meaningful only for the most recently returned
673 Normal use is for postorder directory visits, where it causes the
674 directory to be revisited (in both preorder and postorder) as well as all
678 The referenced file must be a symbolic link.
679 If the referenced file is the one most recently returned by
683 returns the file with the
687 fields reinitialized to reflect the target of the symbolic link instead
688 of the symbolic link itself.
689 If the file is one of those most recently returned by
695 fields of the structure, when returned by
697 will reflect the target of the symbolic link instead of the symbolic link
699 In either case, if the target of the symbolic link does not exist, the
700 fields of the returned structure will be unchanged and the
705 If the target of the link is a directory, the preorder return, followed
706 by the return of all of its descendants, followed by a postorder return,
710 No descendants of this file are visited.
711 The file may be one of those most recently returned by either
719 function closes a file hierarchy stream
721 and restores the current directory to the directory from which
728 returns 0 on success, and \-1 if an error occurs.
734 for any of the errors specified for
743 for any of the errors specified for
754 for any of the errors specified for
772 The options were invalid.
774 These functions are available in Linux since glibc2.
778 All of the APIs described in this man page are not safe when compiling
779 a program using the LFS APIs (e.g., when compiling with
780 .IR -D_FILE_OFFSET_BITS=64 ).
781 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=15838
782 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=11460
783 .\" The following statement is years old, and seems no closer to
784 .\" being true -- mtk
787 .\" utility is expected to be included in a future