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
383 the following values:
384 .\" .Bl -tag -width "FTS_PHYSICAL"
387 This option causes any symbolic link specified as a root path to be
388 followed immediately whether or not
393 This option causes the
394 fts routines to return
396 structures for the targets of symbolic links
397 instead of the symbolic links themselves.
398 If this option is set, the only symbolic links for which
401 are returned to the application are those referencing nonexistent files.
412 As a performance optimization, the
413 fts functions change directories as they walk the file hierarchy.
414 This has the side-effect that an application cannot rely on being
415 in any particular directory during the traversal.
418 option turns off this optimization, and the
419 fts functions will not change the current directory.
420 Note that applications should not themselves change their current directory
421 and try to access files unless
423 is specified and absolute
424 pathnames were provided as arguments to
430 structures reference file characteristic information (the
432 field) for each file visited.
433 This option relaxes that requirement as a performance optimization,
435 fts functions to set the
439 and leave the contents of the
444 This option causes the
445 fts routines to return
447 structures for symbolic links themselves instead
448 of the target files they point to.
449 If this option is set,
451 structures for all symbolic links in the
452 hierarchy are returned to the application.
463 By default, unless they are specified as path arguments to
469 encountered in the file hierarchy are ignored.
470 This option causes the
471 fts routines to return
477 fts from descending into directories that have a different device number
478 than the file from which the descent began.
483 specifies a user-defined function which may be used to order the traversal
486 takes two pointers to pointers to
488 structures as arguments and
489 should return a negative value, zero, or a positive value to indicate
490 if the file referenced by its first argument comes before, in any order
491 with respect to, or after, the file referenced by its second argument.
501 be used in this comparison.
510 field may not either.
515 the directory traversal order is in the order listed in
517 for the root paths, and in the order listed in the directory for
522 function returns a pointer to an
524 structure describing a file in
526 Directories (that are readable and do not cause cycles) are visited at
527 least twice, once in preorder and once in postorder.
528 All other files are visited at least once.
529 (Hard links between directories that do not cause cycles or symbolic
530 links to symbolic links may cause files to be visited more than once,
531 or directories more than twice.)
533 If all the members of the hierarchy have been returned,
537 and sets the external variable
540 If an error unrelated to a file in the hierarchy occurs,
547 If an error related to a returned file occurs, a pointer to an
549 structure is returned, and
551 may or may not have been set (see
556 structures returned by
558 may be overwritten after a call to
560 on the same file hierarchy stream, or, after a call to
562 on the same file hierarchy stream unless they represent a file of type
563 directory, in which case they will not be overwritten until after a call to
567 structure has been returned by the function
573 function returns a pointer to an
575 structure describing the first entry in a NULL-terminated linked list of
576 the files in the directory represented by the
578 structure most recently returned by
580 The list is linked through the
584 structure, and is ordered by the user-specified comparison function, if any.
587 will recreate this linked list.
589 As a special case, if
591 has not yet been called for a hierarchy,
593 will return a pointer to the files in the logical directory specified to
595 that is, the arguments specified to
599 structure most recently returned by
601 is not a directory being visited in preorder,
602 or the directory does not contain any files,
619 structures returned by
621 may be overwritten after a call to
626 on the same file hierarchy stream.
629 may be set to the following value:
630 .\" .Bl -tag -width FTS_NAMEONLY
633 Only the names of the files are needed.
634 The contents of all the fields in the returned linked list of structures
635 are undefined with the exception of the
644 allows the user application to determine further processing for the
652 returns 0 on success, and \-1 if an error occurs.
654 must be set to one of the following values:
655 .\" .Bl -tag -width FTS_PHYSICAL
658 Re-visit the file; any file type may be revisited.
661 will return the referenced file.
666 fields of the structure will be reinitialized at that time,
667 but no other fields will have been changed.
668 This option is meaningful only for the most recently returned
671 Normal use is for postorder directory visits, where it causes the
672 directory to be revisited (in both preorder and postorder) as well as all
676 The referenced file must be a symbolic link.
677 If the referenced file is the one most recently returned by
681 returns the file with the
685 fields reinitialized to reflect the target of the symbolic link instead
686 of the symbolic link itself.
687 If the file is one of those most recently returned by
693 fields of the structure, when returned by
695 will reflect the target of the symbolic link instead of the symbolic link
697 In either case, if the target of the symbolic link does not exist the
698 fields of the returned structure will be unchanged and the
703 If the target of the link is a directory, the preorder return, followed
704 by the return of all of its descendants, followed by a postorder return,
708 No descendants of this file are visited.
709 The file may be one of those most recently returned by either
717 function closes a file hierarchy stream
719 and restores the current directory to the directory from which
726 returns 0 on success, and \-1 if an error occurs.
732 for any of the errors specified for
741 for any of the errors specified for
752 for any of the errors specified for
770 The options were invalid.
772 These functions are available in Linux since glibc2.
775 .\" The following statement is years old, and seems no closer to
776 .\" being true -- mtk
779 .\" utility is expected to be included in a future