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 2012-10-25 "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
380 a number of options, at least one of which (either
385 The options are selected by ORing
386 the following values:
387 .\" .Bl -tag -width "FTS_PHYSICAL"
390 This option causes any symbolic link specified as a root path to be
391 followed immediately whether or not
396 This option causes the
397 fts routines to return
399 structures for the targets of symbolic links
400 instead of the symbolic links themselves.
401 If this option is set, the only symbolic links for which
404 are returned to the application are those referencing nonexistent files.
415 As a performance optimization, the
416 fts functions change directories as they walk the file hierarchy.
417 This has the side-effect that an application cannot rely on being
418 in any particular directory during the traversal.
421 option turns off this optimization, and the
422 fts functions will not change the current directory.
423 Note that applications should not themselves change their current directory
424 and try to access files unless
426 is specified and absolute
427 pathnames were provided as arguments to
433 structures reference file characteristic information (the
435 field) for each file visited.
436 This option relaxes that requirement as a performance optimization,
438 fts functions to set the
442 and leave the contents of the
447 This option causes the
448 fts routines to return
450 structures for symbolic links themselves instead
451 of the target files they point to.
452 If this option is set,
454 structures for all symbolic links in the
455 hierarchy are returned to the application.
466 By default, unless they are specified as path arguments to
472 encountered in the file hierarchy are ignored.
473 This option causes the
474 fts routines to return
480 fts from descending into directories that have a different device number
481 than the file from which the descent began.
486 specifies a user-defined function which may be used to order the traversal
489 takes two pointers to pointers to
491 structures as arguments and
492 should return a negative value, zero, or a positive value to indicate
493 if the file referenced by its first argument comes before, in any order
494 with respect to, or after, the file referenced by its second argument.
504 be used in this comparison.
513 field may not either.
518 the directory traversal order is in the order listed in
520 for the root paths, and in the order listed in the directory for
525 function returns a pointer to an
527 structure describing a file in
529 Directories (that are readable and do not cause cycles) are visited at
530 least twice, once in preorder and once in postorder.
531 All other files are visited at least once.
532 (Hard links between directories that do not cause cycles or symbolic
533 links to symbolic links may cause files to be visited more than once,
534 or directories more than twice.)
536 If all the members of the hierarchy have been returned,
540 and sets the external variable
543 If an error unrelated to a file in the hierarchy occurs,
550 If an error related to a returned file occurs, a pointer to an
552 structure is returned, and
554 may or may not have been set (see
559 structures returned by
561 may be overwritten after a call to
563 on the same file hierarchy stream, or, after a call to
565 on the same file hierarchy stream unless they represent a file of type
566 directory, in which case they will not be overwritten until after a call to
570 structure has been returned by the function
576 function returns a pointer to an
578 structure describing the first entry in a NULL-terminated linked list of
579 the files in the directory represented by the
581 structure most recently returned by
583 The list is linked through the
587 structure, and is ordered by the user-specified comparison function, if any.
590 will recreate this linked list.
592 As a special case, if
594 has not yet been called for a hierarchy,
596 will return a pointer to the files in the logical directory specified to
598 that is, the arguments specified to
602 structure most recently returned by
604 is not a directory being visited in preorder,
605 or the directory does not contain any files,
622 structures returned by
624 may be overwritten after a call to
629 on the same file hierarchy stream.
632 may be set to the following value:
633 .\" .Bl -tag -width FTS_NAMEONLY
636 Only the names of the files are needed.
637 The contents of all the fields in the returned linked list of structures
638 are undefined with the exception of the
647 allows the user application to determine further processing for the
655 returns 0 on success, and \-1 if an error occurs.
657 must be set to one of the following values:
658 .\" .Bl -tag -width FTS_PHYSICAL
661 Revisit the file; any file type may be revisited.
664 will return the referenced file.
669 fields of the structure will be reinitialized at that time,
670 but no other fields will have been changed.
671 This option is meaningful only for the most recently returned
674 Normal use is for postorder directory visits, where it causes the
675 directory to be revisited (in both preorder and postorder) as well as all
679 The referenced file must be a symbolic link.
680 If the referenced file is the one most recently returned by
684 returns the file with the
688 fields reinitialized to reflect the target of the symbolic link instead
689 of the symbolic link itself.
690 If the file is one of those most recently returned by
696 fields of the structure, when returned by
698 will reflect the target of the symbolic link instead of the symbolic link
700 In either case, if the target of the symbolic link does not exist the
701 fields of the returned structure will be unchanged and the
706 If the target of the link is a directory, the preorder return, followed
707 by the return of all of its descendants, followed by a postorder return,
711 No descendants of this file are visited.
712 The file may be one of those most recently returned by either
720 function closes a file hierarchy stream
722 and restores the current directory to the directory from which
729 returns 0 on success, and \-1 if an error occurs.
735 for any of the errors specified for
744 for any of the errors specified for
755 for any of the errors specified for
773 The options were invalid.
775 These functions are available in Linux since glibc2.
778 .\" The following statement is years old, and seems no closer to
779 .\" being true -- mtk
782 .\" utility is expected to be included in a future