Debian Codegraph @

upstream/debian-unstable/man3/fts.3

raw
1 '\" t
2 .\" Copyright 1989-1994, The Regents of the University of California.
3 .\" Copyright, the authors of the Linux man-pages project
4 .\"
5 .\" SPDX-License-Identifier: BSD-4-Clause-UC
6 .\"
7 .TH fts 3 2025-09-21 "Linux man-pages 6.18"
8 .SH NAME
9 fts, fts_open, fts_read, fts_children, fts_set, fts_close \- \
10 traverse a file hierarchy
11 .SH LIBRARY
12 Standard C library
13 .RI ( libc ,\~ \-lc )
14 .SH SYNOPSIS
15 .nf
16 .B #include <sys/types.h>
17 .B #include <sys/stat.h>
18 .B #include <fts.h>
19 .P
20 .BI "FTS *fts_open(char *const *" path_argv ", int " options ,
21 .B " typeof(int (const FTSENT **, const FTSENT **))"
22 .BI " *_Nullable " compar );
23 .P
24 .BI "FTSENT *fts_read(FTS *" ftsp );
25 .P
26 .BI "FTSENT *fts_children(FTS *" ftsp ", int " instr );
27 .P
28 .BI "int fts_set(FTS *" ftsp ", FTSENT *" f ", int " instr );
29 .P
30 .BI "int fts_close(FTS *" ftsp );
31 .fi
32 .SH DESCRIPTION
33 The
34 fts functions are provided for traversing
35 file hierarchies.
36 A simple overview is that the
37 .BR fts_open ()
38 function returns a "handle" (of type
39 .IR "FTS\ *" )
40 that refers to a file hierarchy "stream".
41 This handle is then supplied to the other
42 fts functions.
43 The function
44 .BR fts_read ()
45 returns a pointer to a structure describing one of the files in the file
46 hierarchy.
47 The function
48 .BR fts_children ()
49 returns a pointer to a linked list of structures, each of which describes
50 one of the files contained in a directory in the hierarchy.
51 .P
52 In general,
53 directories are visited two distinguishable times;
54 in preorder
55 (before any of their descendants are visited)
56 and in postorder
57 (after all of their descendants have been visited).
58 Files are visited once.
59 It is possible to walk the hierarchy
60 "logically"
61 (visiting the files that symbolic links point to)
62 or physically
63 (visiting the symbolic links themselves),
64 order the walk of the hierarchy or
65 prune and/or revisit portions of the hierarchy.
66 .P
67 Two structures (and associated types) are defined in the include file
68 .IR <fts.h> .
69 The first type is
70 .IR FTS ,
71 the structure that represents the file hierarchy itself.
72 The second type is
73 .IR FTSENT ,
74 the structure that represents a file in the file
75 hierarchy.
76 Normally, an
77 .I FTSENT
78 structure is returned for every file in the file
79 hierarchy.
80 In this manual page, "file" and
81 "FTSENT structure"
82 are generally interchangeable.
83 .P
84 The
85 .I FTSENT
86 structure contains fields describing a file.
87 The structure contains at least the following fields
88 (there are additional fields that
89 should be considered private to the implementation):
90 .P
91 .in +4n
92 .EX
93 typedef struct _ftsent {
94 unsigned short fts_info; /* flags for FTSENT structure */
95 char *fts_accpath; /* access path */
96 char *fts_path; /* root path */
97 short fts_pathlen; /* strlen(fts_path) +
98 strlen(fts_name) */
99 char *fts_name; /* filename */
100 short fts_namelen; /* strlen(fts_name) */
101 short fts_level; /* depth (\-1 to N) */
102 int fts_errno; /* file errno */
103 long fts_number; /* local numeric value */
104 void *fts_pointer; /* local address value */
105 struct _ftsent *fts_parent; /* parent directory */
106 struct _ftsent *fts_link; /* next file structure */
107 struct _ftsent *fts_cycle; /* cycle structure */
108 struct stat *fts_statp; /* [l]stat(2) information */
109 .\" Also:
110 .\" ino_t fts_ino; /* inode (only for directories)*/
111 .\" dev_t fts_dev; /* device (only for directories)*/
112 .\" nlink_t fts_nlink; /* link count (only for directories)*/
113 .\" u_short fts_flags; /* private flags for FTSENT structure */
114 .\" u_short fts_instr; /* fts_set() instructions */
115 } FTSENT;
116 .EE
117 .in
118 .P
119 These fields are defined as follows:
120 .\" .Bl -tag -width "fts_namelen"
121 .TP
122 .I fts_info
123 One of the following values describing the returned
124 .I FTSENT
125 structure and
126 the file it represents.
127 With the exception of directories without errors
128 .RB ( FTS_D ),
129 all of these
130 entries are terminal, that is, they will not be revisited, nor will any
131 of their descendants be visited.
132 .\" .Bl -tag -width FTS_DEFAULT
133 .RS
134 .TP
135 .B FTS_D
136 A directory being visited in preorder.
137 .TP
138 .B FTS_DC
139 A directory that causes a cycle in the tree.
140 (The
141 .I fts_cycle
142 field of the
143 .I FTSENT
144 structure will be filled in as well.)
145 .TP
146 .B FTS_DEFAULT
147 Any
148 .I FTSENT
149 structure that represents a file type not explicitly described
150 by one of the other
151 .I fts_info
152 values.
153 .TP
154 .B FTS_DNR
155 A directory which cannot be read.
156 This is an error return, and the
157 .I fts_errno
158 field will be set to indicate what caused the error.
159 .TP
160 .B FTS_DOT
161 A file named
162 "."
163 or
164 ".."
165 which was not specified as a filename to
166 .BR fts_open ()
167 (see
168 .BR FTS_SEEDOT ).
169 .TP
170 .B FTS_DP
171 A directory being visited in postorder.
172 The contents of the
173 .I FTSENT
174 structure will be unchanged from when
175 it was returned in preorder, that is, with the
176 .I fts_info
177 field set to
178 .BR FTS_D .
179 .TP
180 .B FTS_ERR
181 This is an error return, and the
182 .I fts_errno
183 field will be set to indicate what caused the error.
184 .TP
185 .B FTS_F
186 A regular file.
187 .TP
188 .B FTS_NS
189 A file for which no
190 .RB [ l ]\c
191 .BR stat (2)
192 information was available.
193 The contents of the
194 .I fts_statp
195 field are undefined.
196 This is an error return, and the
197 .I fts_errno
198 field will be set to indicate what caused the error.
199 .TP
200 .B FTS_NSOK
201 A file for which no
202 .RB [ l ]\c
203 .BR stat (2)
204 information was requested.
205 The contents of the
206 .I fts_statp
207 field are undefined.
208 .TP
209 .B FTS_SL
210 A symbolic link.
211 .TP
212 .B FTS_SLNONE
213 A symbolic link with a nonexistent target.
214 The contents of the
215 .I fts_statp
216 field reference the file characteristic information for the symbolic link
217 itself.
218 .\" .El
219 .RE
220 .TP
221 .I fts_accpath
222 A path for accessing the file from the current directory.
223 .TP
224 .I fts_path
225 The path for the file relative to the root of the traversal.
226 This path contains the path specified to
227 .BR fts_open ()
228 as a prefix.
229 .TP
230 .I fts_pathlen
231 The sum of the lengths of the strings referenced by
232 .I fts_path
233 and
234 .IR fts_name .
235 .TP
236 .I fts_name
237 The name of the file.
238 .TP
239 .I fts_namelen
240 The length of the string referenced by
241 .IR fts_name .
242 .TP
243 .I fts_level
244 The depth of the traversal, numbered from \-1 to N, where this file
245 was found.
246 The
247 .I FTSENT
248 structure representing the parent of the starting point (or root)
249 of the traversal is numbered \-1, and the
250 .I FTSENT
251 structure for the root
252 itself is numbered 0.
253 .TP
254 .I fts_errno
255 If
256 .BR fts_children ()
257 or
258 .BR fts_read ()
259 returns an
260 .I FTSENT
261 structure whose
262 .I fts_info
263 field is set to
264 .BR FTS_DNR ,
265 .BR FTS_ERR ,
266 or
267 .BR FTS_NS ,
268 the
269 .I fts_errno
270 field contains the error number (i.e., the
271 .I errno
272 value)
273 specifying the error.
274 Otherwise, the contents of the
275 .I fts_errno
276 field are undefined.
277 .TP
278 .I fts_number
279 This field is provided for the use of the application program and is
280 not modified by the
281 fts functions.
282 It is initialized to 0.
283 .TP
284 .I fts_pointer
285 This field is provided for the use of the application program and is
286 not modified by the
287 fts functions.
288 It is initialized to
289 NULL.
290 .TP
291 .I fts_parent
292 A pointer to the
293 .I FTSENT
294 structure referencing the file in the hierarchy
295 immediately above the current file, that is, the directory of which this
296 file is a member.
297 A parent structure for the initial entry point is provided as well,
298 however, only the
299 .IR fts_level ,
300 .IR fts_number ,
301 and
302 .I fts_pointer
303 fields are guaranteed to be initialized.
304 .TP
305 .I fts_link
306 Upon return from the
307 .BR fts_children ()
308 function, the
309 .I fts_link
310 field points to the next structure in the NULL-terminated linked list of
311 directory members.
312 Otherwise, the contents of the
313 .I fts_link
314 field are undefined.
315 .TP
316 .I fts_cycle
317 If a directory causes a cycle in the hierarchy (see
318 .BR FTS_DC ),
319 either because
320 of a hard link between two directories, or a symbolic link pointing to a
321 directory, the
322 .I fts_cycle
323 field of the structure will point to the
324 .I FTSENT
325 structure in the hierarchy that references the same file as the current
326 .I FTSENT
327 structure.
328 Otherwise, the contents of the
329 .I fts_cycle
330 field are undefined.
331 .TP
332 .I fts_statp
333 A pointer to
334 .RB [ l ]\c
335 .BR stat (2)
336 information for the file.
337 .\" .El
338 .P
339 A single buffer is used for all of the paths of all of the files in the
340 file hierarchy.
341 Therefore, the
342 .I fts_path
343 and
344 .I fts_accpath
345 fields are guaranteed to be
346 null-terminated
347 .I only
348 for the file most recently returned by
349 .BR fts_read ().
350 To use these fields to reference any files represented by other
351 .I FTSENT
352 structures will require that the path buffer be modified using the
353 information contained in that
354 .I FTSENT
355 structure's
356 .I fts_pathlen
357 field.
358 Any such modifications should be undone before further calls to
359 .BR fts_read ()
360 are attempted.
361 The
362 .I fts_name
363 field is always
364 null-terminated.
365 .SS fts_open()
366 The
367 .BR fts_open ()
368 function takes a pointer to an array of character pointers naming one
369 or more paths which make up a logical file hierarchy to be traversed.
370 The array must be terminated by a
371 null pointer.
372 .P
373 There are
374 a number of options, at least one of which (either
375 .B FTS_LOGICAL
376 or
377 .BR FTS_PHYSICAL )
378 must be specified.
379 The options are selected by ORing
380 the following values:
381 .\" .Bl -tag -width "FTS_PHYSICAL"
382 .TP
383 .B FTS_LOGICAL
384 This option causes the
385 fts routines to return
386 .I FTSENT
387 structures for the targets of symbolic links
388 instead of the symbolic links themselves.
389 If this option is set, the only symbolic links for which
390 .I FTSENT
391 structures
392 are returned to the application are those referencing nonexistent files:
393 the
394 .I fts_statp
395 field is obtained via
396 .BR stat (2)
397 with a fallback to
398 .BR lstat (2).
399 .TP
400 .B FTS_PHYSICAL
401 This option causes the
402 fts routines to return
403 .I FTSENT
404 structures for symbolic links themselves instead
405 of the target files they point to.
406 If this option is set,
407 .I FTSENT
408 structures for all symbolic links in the
409 hierarchy are returned to the application:
410 the
411 .I fts_statp
412 field is obtained via
413 .BR lstat (2).
414 .TP
415 .B FTS_COMFOLLOW
416 This option causes any symbolic link specified as a root path to be
417 followed immediately, as if via
418 .BR FTS_LOGICAL ,
419 regardless of the primary mode.
420 .TP
421 .B FTS_NOCHDIR
422 As a performance optimization, the
423 fts functions change directories as they walk the file hierarchy.
424 This has the side-effect that an application cannot rely on being
425 in any particular directory during the traversal.
426 This
427 option turns off this optimization, and the
428 fts functions will not change the current directory.
429 Note that applications should not themselves change their current directory
430 and try to access files unless
431 .B FTS_NOCHDIR
432 is specified and absolute
433 pathnames were provided as arguments to
434 .BR fts_open ().
435 .TP
436 .B FTS_NOSTAT
437 By default, returned
438 .I FTSENT
439 structures reference file characteristic information (the
440 .I fts_statp
441 field) for each file visited.
442 This option relaxes that requirement as a performance optimization,
443 allowing the
444 fts functions to set the
445 .I fts_info
446 field to
447 .B FTS_NSOK
448 and leave the contents of the
449 .I fts_statp
450 field undefined.
451 .TP
452 .B FTS_SEEDOT
453 By default, unless they are specified as path arguments to
454 .BR fts_open (),
455 any files named
456 "."
457 or
458 ".."
459 encountered in the file hierarchy are ignored.
460 This option causes the
461 fts routines to return
462 .I FTSENT
463 structures for them.
464 .TP
465 .B FTS_XDEV
466 This option prevents
467 fts from descending into directories that have a different device number
468 than the file from which the descent began.
469 .\" .El
470 .P
471 The argument
472 .BR compar ()
473 specifies a user-defined function which may be used to order the traversal
474 of the hierarchy.
475 It
476 takes two pointers to pointers to
477 .I FTSENT
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.
482 The
483 .IR fts_accpath ,
484 .IR fts_path ,
485 and
486 .I fts_pathlen
487 fields of the
488 .I FTSENT
489 structures may
490 .I never
491 be used in this comparison.
492 If the
493 .I fts_info
494 field is set to
495 .B FTS_NS
496 or
497 .BR FTS_NSOK ,
498 the
499 .I fts_statp
500 field may not either.
501 If the
502 .BR compar ()
503 argument is
504 NULL,
505 the directory traversal order is in the order listed in
506 .I path_argv
507 for the root paths, and in the order listed in the directory for
508 everything else.
509 .SS fts_read()
510 The
511 .BR fts_read ()
512 function returns a pointer to an
513 .I FTSENT
514 structure describing a file in
515 the hierarchy.
516 Directories (that are readable and do not cause cycles) are visited at
517 least twice, once in preorder and once in postorder.
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.)
522 .P
523 If all the members of the hierarchy have been returned,
524 .BR fts_read ()
525 returns NULL and sets
526 .I errno
527 to 0.
528 If an error unrelated to a file in the hierarchy occurs,
529 .BR fts_read ()
530 returns
531 NULL
532 and sets
533 .I errno
534 to indicate the error.
535 If an error related to a returned file occurs, a pointer to an
536 .I FTSENT
537 structure is returned, and
538 .I errno
539 may or may not have been set (see
540 .IR fts_info ).
541 .P
542 The
543 .I FTSENT
544 structures returned by
545 .BR fts_read ()
546 may be overwritten after a call to
547 .BR fts_close ()
548 on the same file hierarchy stream, or, after a call to
549 .BR fts_read ()
550 on the same file hierarchy stream unless they represent a file of type
551 directory, in which case they will not be overwritten until after a call to
552 .BR fts_read ()
553 after the
554 .I FTSENT
555 structure has been returned by the function
556 .BR fts_read ()
557 in postorder.
558 .SS fts_children()
559 The
560 .BR fts_children ()
561 function returns a pointer to an
562 .I FTSENT
563 structure describing the first entry in a NULL-terminated linked list of
564 the files in the directory represented by the
565 .I FTSENT
566 structure most recently returned by
567 .BR fts_read ().
568 The list is linked through the
569 .I fts_link
570 field of the
571 .I FTSENT
572 structure, and is ordered by the user-specified comparison function, if any.
573 Repeated calls to
574 .BR fts_children ()
575 will re-create this linked list.
576 .P
577 As a special case, if
578 .BR fts_read ()
579 has not yet been called for a hierarchy,
580 .BR fts_children ()
581 will return a pointer to the files in the logical directory specified to
582 .BR fts_open (),
583 that is, the arguments specified to
584 .BR fts_open ().
585 Otherwise, if the
586 .I FTSENT
587 structure most recently returned by
588 .BR fts_read ()
589 is not a directory being visited in preorder,
590 or the directory does not contain any files,
591 .BR fts_children ()
592 returns
593 NULL
594 and sets
595 .I errno
596 to zero.
597 If an error occurs,
598 .BR fts_children ()
599 returns
600 NULL
601 and sets
602 .I errno
603 to indicate the error.
604 .P
605 The
606 .I FTSENT
607 structures returned by
608 .BR fts_children ()
609 may be overwritten after a call to
610 .BR fts_children (),
611 .BR fts_close (),
612 or
613 .BR fts_read ()
614 on the same file hierarchy stream.
615 .P
616 The
617 .I instr
618 argument is either zero or the following value:
619 .\" .Bl -tag -width FTS_NAMEONLY
620 .TP
621 .B FTS_NAMEONLY
622 Only the names of the files are needed.
623 The contents of all the fields in the returned linked list of structures
624 are undefined with the exception of the
625 .I fts_name
626 and
627 .I fts_namelen
628 fields.
629 .\" .El
630 .SS fts_set()
631 The function
632 .BR fts_set ()
633 allows the user application to determine further processing for the
634 file
635 .I f
636 of the stream
637 .IR ftsp .
638 The
639 .BR fts_set ()
640 function
641 returns 0 on success, and \-1 if an error occurs.
642 .P
643 The
644 .I instr
645 argument is either 0 (meaning "do nothing") or one of the following values:
646 .\" .Bl -tag -width FTS_PHYSICAL
647 .TP
648 .B FTS_AGAIN
649 Revisit the file;
650 any file type may be revisited.
651 The next call to
652 .BR fts_read ()
653 will return the referenced file.
654 The
655 .I fts_stat
656 and
657 .I fts_info
658 fields of the structure will be reinitialized at that time,
659 but no other fields will have been changed.
660 This option is meaningful only for the most recently returned
661 file from
662 .BR fts_read ().
663 Normal use is for postorder directory visits, where it causes the
664 directory to be revisited (in both preorder and postorder) as well as all
665 of its descendants.
666 .TP
667 .B FTS_FOLLOW
668 The referenced file must be a symbolic link.
669 If the referenced file is the one most recently returned by
670 .BR fts_read (),
671 the next call to
672 .BR fts_read ()
673 returns the file with the
674 .I fts_info
675 and
676 .I fts_statp
677 fields reinitialized to reflect the target of the symbolic link instead
678 of the symbolic link itself.
679 If the file is one of those most recently returned by
680 .BR fts_children (),
681 the
682 .I fts_info
683 and
684 .I fts_statp
685 fields of the structure, when returned by
686 .BR fts_read (),
687 will reflect the target of the symbolic link instead of the symbolic link
688 itself.
689 In either case, if the target of the symbolic link does not exist, the
690 fields of the returned structure will be unchanged and the
691 .I fts_info
692 field will be set to
693 .BR FTS_SLNONE .
694 .IP
695 If the target of the link is a directory, the preorder return, followed
696 by the return of all of its descendants, followed by a postorder return,
697 is done.
698 .TP
699 .B FTS_SKIP
700 No descendants of this file are visited.
701 The file may be one of those most recently returned by either
702 .BR fts_children ()
703 or
704 .BR fts_read ().
705 .\" .El
706 .SS fts_close()
707 The
708 .BR fts_close ()
709 function closes the file hierarchy stream referred to by
710 .I ftsp
711 and restores the current directory to the directory from which
712 .BR fts_open ()
713 was called to open
714 .IR ftsp .
715 The
716 .BR fts_close ()
717 function
718 returns 0 on success, and \-1 if an error occurs.
719 .SH ERRORS
720 The function
721 .BR fts_open ()
722 may fail and set
723 .I errno
724 for any of the errors specified for
725 .BR open (2)
726 and
727 .BR malloc (3).
728 .P
729 In addition,
730 .BR fts_open ()
731 may fail and set
732 .I errno
733 as follows:
734 .TP
735 .B ENOENT
736 Any element of
737 .I path_argv
738 was an empty string.
739 .P
740 The function
741 .BR fts_close ()
742 may fail and set
743 .I errno
744 for any of the errors specified for
745 .BR chdir (2)
746 and
747 .BR close (2).
748 .P
749 The functions
750 .BR fts_read ()
751 and
752 .BR fts_children ()
753 may fail and set
754 .I errno
755 for any of the errors specified for
756 .BR chdir (2),
757 .BR malloc (3),
758 .BR opendir (3),
759 .BR readdir (3),
760 and
761 .RB [ l ]\c
762 .BR stat (2).
763 .P
764 In addition,
765 .BR fts_children (),
766 .BR fts_open (),
767 and
768 .BR fts_set ()
769 may fail and set
770 .I errno
771 as follows:
772 .TP
773 .B EINVAL
774 .I options
775 or
776 .I instr
777 was invalid.
778 .SH ATTRIBUTES
779 For an explanation of the terms used in this section, see
780 .BR attributes (7).
781 .TS
782 allbox;
783 lbx lb lb
784 l l l.
785 Interface Attribute Value
786 T{
787 .na
788 .nh
789 .BR fts_open (),
790 .BR fts_set (),
791 .BR fts_close ()
792 T} Thread safety MT-Safe
793 T{
794 .na
795 .nh
796 .BR fts_read (),
797 .BR fts_children ()
798 T} Thread safety MT-Unsafe
799 .TE
800 .SH STANDARDS
801 None.
802 .SH HISTORY
803 glibc 2.
804 4.4BSD.
805 .SH BUGS
806 Before glibc 2.23,
807 .\" Fixed by commit 8b7b7f75d91f7bac323dd6a370aeb3e9c5c4a7d5
808 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=15838
809 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=11460
810 all of the APIs described in this man page are not safe when compiling
811 a program using the LFS APIs (e.g., when compiling with
812 .IR \-D_FILE_OFFSET_BITS=64 ).
813 .\"
814 .\" The following statement is years old, and seems no closer to
815 .\" being true -- mtk
816 .\" The
817 .\" .I fts
818 .\" utility is expected to be included in a future
819 .\" POSIX.1
820 .\" revision.
821 .SH SEE ALSO
822 .BR find (1),
823 .BR chdir (2),
824 .BR lstat (2),
825 .BR stat (2),
826 .BR ftw (3),
827 .BR qsort (3)