]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/fts.3
Various pages: [BSD-4-Clause-UC] Use SPDX-License-Identifier
[thirdparty/man-pages.git] / man3 / fts.3
1 .\" $NetBSD: fts.3,v 1.13.2.1 1997/11/14 02:09:32 mrg Exp $
2 .\"
3 .\" Copyright (c) 1989, 1991, 1993, 1994
4 .\" The Regents of the University of California. All rights reserved.
5 .\"
6 .\" SPDX-License-Identifier: BSD-4-Clause-UC
7 .\"
8 .\" @(#)fts.3 8.5 (Berkeley) 4/16/94
9 .\"
10 .\" 2007-12-08, mtk, Converted from mdoc to man macros
11 .\"
12 .TH FTS 3 2021-03-22 "Linux" "Linux Programmer's Manual"
13 .SH NAME
14 fts, fts_open, fts_read, fts_children, fts_set, fts_close \- \
15 traverse a file hierarchy
16 .SH SYNOPSIS
17 .nf
18 .B #include <sys/types.h>
19 .B #include <sys/stat.h>
20 .B #include <fts.h>
21 .PP
22 .BI "FTS *fts_open(char * const *" path_argv ", int " options ,
23 .BI " int (*" compar ")(const FTSENT **, const FTSENT **));"
24 .PP
25 .BI "FTSENT *fts_read(FTS *" ftsp );
26 .PP
27 .BI "FTSENT *fts_children(FTS *" ftsp ", int " instr );
28 .PP
29 .BI "int fts_set(FTS *" ftsp ", FTSENT *" f ", int " instr );
30 .PP
31 .BI "int fts_close(FTS *" ftsp );
32 .fi
33 .SH DESCRIPTION
34 The
35 fts functions are provided for traversing
36 file hierarchies.
37 A simple overview is that the
38 .BR fts_open ()
39 function returns a "handle" (of type
40 .IR "FTS\ *" )
41 that refers to a file hierarchy "stream".
42 This handle is then supplied to the other
43 fts functions.
44 The function
45 .BR fts_read ()
46 returns a pointer to a structure describing one of the files in the file
47 hierarchy.
48 The function
49 .BR fts_children ()
50 returns a pointer to a linked list of structures, each of which describes
51 one of the files contained in a directory in the hierarchy.
52 .PP
53 In general, directories are visited two distinguishable times; in preorder
54 (before any of their descendants are visited) and in postorder (after all
55 of their descendants have been visited).
56 Files are visited once.
57 It is possible to walk the hierarchy "logically" (visiting the files that
58 symbolic links point to)
59 or physically (visiting the symbolic links themselves),
60 order the walk of the hierarchy or
61 prune and/or revisit portions of the hierarchy.
62 .PP
63 Two structures (and associated types) are defined in the include file
64 .IR <fts.h> .
65 The first type is
66 .IR FTS ,
67 the structure that represents the file hierarchy itself.
68 The second type is
69 .IR FTSENT ,
70 the structure that represents a file in the file
71 hierarchy.
72 Normally, an
73 .I FTSENT
74 structure is returned for every file in the file
75 hierarchy.
76 In this manual page, "file" and
77 "FTSENT structure"
78 are generally interchangeable.
79 .PP
80 The
81 .I FTSENT
82 structure contains fields describing a file.
83 The structure contains at least the following fields
84 (there are additional fields that
85 should be considered private to the implementation):
86 .PP
87 .in +4n
88 .EX
89 typedef struct _ftsent {
90 unsigned short fts_info; /* flags for FTSENT structure */
91 char *fts_accpath; /* access path */
92 char *fts_path; /* root path */
93 short fts_pathlen; /* strlen(fts_path) +
94 strlen(fts_name) */
95 char *fts_name; /* filename */
96 short fts_namelen; /* strlen(fts_name) */
97 short fts_level; /* depth (\-1 to N) */
98 int fts_errno; /* file errno */
99 long fts_number; /* local numeric value */
100 void *fts_pointer; /* local address value */
101 struct _ftsent *fts_parent; /* parent directory */
102 struct _ftsent *fts_link; /* next file structure */
103 struct _ftsent *fts_cycle; /* cycle structure */
104 struct stat *fts_statp; /* stat(2) information */
105 .\" Also:
106 .\" ino_t fts_ino; /* inode (only for directories)*/
107 .\" dev_t fts_dev; /* device (only for directories)*/
108 .\" nlink_t fts_nlink; /* link count (only for directories)*/
109 .\" u_short fts_flags; /* private flags for FTSENT structure */
110 .\" u_short fts_instr; /* fts_set() instructions */
111 } FTSENT;
112 .EE
113 .in
114 .PP
115 These fields are defined as follows:
116 .\" .Bl -tag -width "fts_namelen"
117 .TP
118 .IR fts_info
119 One of the following values describing the returned
120 .I FTSENT
121 structure and
122 the file it represents.
123 With the exception of directories without errors
124 .RB ( FTS_D ),
125 all of these
126 entries are terminal, that is, they will not be revisited, nor will any
127 of their descendants be visited.
128 .\" .Bl -tag -width FTS_DEFAULT
129 .RS
130 .TP
131 .BR FTS_D
132 A directory being visited in preorder.
133 .TP
134 .BR FTS_DC
135 A directory that causes a cycle in the tree.
136 (The
137 .I fts_cycle
138 field of the
139 .I FTSENT
140 structure will be filled in as well.)
141 .TP
142 .BR FTS_DEFAULT
143 Any
144 .I FTSENT
145 structure that represents a file type not explicitly described
146 by one of the other
147 .I fts_info
148 values.
149 .TP
150 .BR FTS_DNR
151 A directory which cannot be read.
152 This is an error return, and the
153 .I fts_errno
154 field will be set to indicate what caused the error.
155 .TP
156 .BR FTS_DOT
157 A file named
158 "."
159 or
160 ".."
161 which was not specified as a filename to
162 .BR fts_open ()
163 (see
164 .BR FTS_SEEDOT ).
165 .TP
166 .BR FTS_DP
167 A directory being visited in postorder.
168 The contents of the
169 .I FTSENT
170 structure will be unchanged from when
171 it was returned in preorder, that is, with the
172 .I fts_info
173 field set to
174 .BR FTS_D .
175 .TP
176 .BR FTS_ERR
177 This is an error return, and the
178 .I fts_errno
179 field will be set to indicate what caused the error.
180 .TP
181 .BR FTS_F
182 A regular file.
183 .TP
184 .BR FTS_NS
185 A file for which no
186 .BR stat (2)
187 information was available.
188 The contents of the
189 .I fts_statp
190 field are undefined.
191 This is an error return, and the
192 .I fts_errno
193 field will be set to indicate what caused the error.
194 .TP
195 .BR FTS_NSOK
196 A file for which no
197 .BR stat (2)
198 information was requested.
199 The contents of the
200 .I fts_statp
201 field are undefined.
202 .TP
203 .BR FTS_SL
204 A symbolic link.
205 .TP
206 .BR FTS_SLNONE
207 A symbolic link with a nonexistent target.
208 The contents of the
209 .I fts_statp
210 field reference the file characteristic information for the symbolic link
211 itself.
212 .\" .El
213 .RE
214 .TP
215 .IR fts_accpath
216 A path for accessing the file from the current directory.
217 .TP
218 .IR fts_path
219 The path for the file relative to the root of the traversal.
220 This path contains the path specified to
221 .BR fts_open ()
222 as a prefix.
223 .TP
224 .IR fts_pathlen
225 The sum of the lengths of the strings referenced by
226 .IR fts_path
227 and
228 .IR fts_name .
229 .TP
230 .IR fts_name
231 The name of the file.
232 .TP
233 .IR fts_namelen
234 The length of the string referenced by
235 .IR fts_name .
236 .TP
237 .IR fts_level
238 The depth of the traversal, numbered from \-1 to N, where this file
239 was found.
240 The
241 .I FTSENT
242 structure representing the parent of the starting point (or root)
243 of the traversal is numbered \-1, and the
244 .I FTSENT
245 structure for the root
246 itself is numbered 0.
247 .TP
248 .IR fts_errno
249 If
250 .BR fts_children ()
251 or
252 .BR fts_read ()
253 returns an
254 .I FTSENT
255 structure whose
256 .I fts_info
257 field is set to
258 .BR FTS_DNR ,
259 .BR FTS_ERR ,
260 or
261 .BR FTS_NS ,
262 the
263 .I fts_errno
264 field contains the error number (i.e., the
265 .IR errno
266 value)
267 specifying the cause of the error.
268 Otherwise, the contents of the
269 .I fts_errno
270 field are undefined.
271 .TP
272 .IR fts_number
273 This field is provided for the use of the application program and is
274 not modified by the
275 fts functions.
276 It is initialized to 0.
277 .TP
278 .IR fts_pointer
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
283 NULL.
284 .TP
285 .IR fts_parent
286 A pointer to the
287 .I FTSENT
288 structure referencing the file in the hierarchy
289 immediately above the current file, that is, the directory of which this
290 file is a member.
291 A parent structure for the initial entry point is provided as well,
292 however, only the
293 .IR fts_level ,
294 .IR fts_number ,
295 and
296 .I fts_pointer
297 fields are guaranteed to be initialized.
298 .TP
299 .IR fts_link
300 Upon return from the
301 .BR fts_children ()
302 function, the
303 .I fts_link
304 field points to the next structure in the NULL-terminated linked list of
305 directory members.
306 Otherwise, the contents of the
307 .I fts_link
308 field are undefined.
309 .TP
310 .IR fts_cycle
311 If a directory causes a cycle in the hierarchy (see
312 .BR FTS_DC ),
313 either because
314 of a hard link between two directories, or a symbolic link pointing to a
315 directory, the
316 .I fts_cycle
317 field of the structure will point to the
318 .I FTSENT
319 structure in the hierarchy that references the same file as the current
320 .I FTSENT
321 structure.
322 Otherwise, the contents of the
323 .I fts_cycle
324 field are undefined.
325 .TP
326 .IR fts_statp
327 A pointer to
328 .BR stat (2)
329 information for the file.
330 .\" .El
331 .PP
332 A single buffer is used for all of the paths of all of the files in the
333 file hierarchy.
334 Therefore, the
335 .I fts_path
336 and
337 .I fts_accpath
338 fields are guaranteed to be
339 null-terminated
340 .I only
341 for the file most recently returned by
342 .BR fts_read ().
343 To use these fields to reference any files represented by other
344 .I FTSENT
345 structures will require that the path buffer be modified using the
346 information contained in that
347 .I FTSENT
348 structure's
349 .I fts_pathlen
350 field.
351 Any such modifications should be undone before further calls to
352 .BR fts_read ()
353 are attempted.
354 The
355 .I fts_name
356 field is always
357 null-terminated.
358 .SS fts_open()
359 The
360 .BR fts_open ()
361 function takes a pointer to an array of character pointers naming one
362 or more paths which make up a logical file hierarchy to be traversed.
363 The array must be terminated by a
364 null pointer.
365 .PP
366 There are
367 a number of options, at least one of which (either
368 .BR FTS_LOGICAL
369 or
370 .BR FTS_PHYSICAL )
371 must be specified.
372 The options are selected by ORing
373 the following values:
374 .\" .Bl -tag -width "FTS_PHYSICAL"
375 .TP
376 .BR FTS_COMFOLLOW
377 This option causes any symbolic link specified as a root path to be
378 followed immediately whether or not
379 .BR FTS_LOGICAL
380 is also specified.
381 .TP
382 .BR FTS_LOGICAL
383 This option causes the
384 fts routines to return
385 .I FTSENT
386 structures for the targets of symbolic links
387 instead of the symbolic links themselves.
388 If this option is set, the only symbolic links for which
389 .I FTSENT
390 structures
391 are returned to the application are those referencing nonexistent files.
392 Either
393 .BR FTS_LOGICAL
394 or
395 .BR FTS_PHYSICAL
396 .I must
397 be provided to the
398 .BR fts_open ()
399 function.
400 .TP
401 .BR FTS_NOCHDIR
402 As a performance optimization, the
403 fts functions change directories as they walk the file hierarchy.
404 This has the side-effect that an application cannot rely on being
405 in any particular directory during the traversal.
406 The
407 .BR FTS_NOCHDIR
408 option turns off this optimization, and the
409 fts functions will not change the current directory.
410 Note that applications should not themselves change their current directory
411 and try to access files unless
412 .BR FTS_NOCHDIR
413 is specified and absolute
414 pathnames were provided as arguments to
415 .BR fts_open ().
416 .TP
417 .BR FTS_NOSTAT
418 By default, returned
419 .I FTSENT
420 structures reference file characteristic information (the
421 .I statp
422 field) for each file visited.
423 This option relaxes that requirement as a performance optimization,
424 allowing the
425 fts functions to set the
426 .I fts_info
427 field to
428 .BR FTS_NSOK
429 and leave the contents of the
430 .I statp
431 field undefined.
432 .TP
433 .BR FTS_PHYSICAL
434 This option causes the
435 fts routines to return
436 .I FTSENT
437 structures for symbolic links themselves instead
438 of the target files they point to.
439 If this option is set,
440 .I FTSENT
441 structures for all symbolic links in the
442 hierarchy are returned to the application.
443 Either
444 .BR FTS_LOGICAL
445 or
446 .BR FTS_PHYSICAL
447 .I must
448 be provided to the
449 .BR fts_open ()
450 function.
451 .TP
452 .BR 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 .BR 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 .PP
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 .BR 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 .PP
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 .PP
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 .PP
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 .PP
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 .PP
616 The
617 .I instr
618 argument is either zero or the following value:
619 .\" .Bl -tag -width FTS_NAMEONLY
620 .TP
621 .BR 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 .PP
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 .BR FTS_AGAIN
649 Revisit the file; any file type may be revisited.
650 The next call to
651 .BR fts_read ()
652 will return the referenced file.
653 The
654 .I fts_stat
655 and
656 .I fts_info
657 fields of the structure will be reinitialized at that time,
658 but no other fields will have been changed.
659 This option is meaningful only for the most recently returned
660 file from
661 .BR fts_read ().
662 Normal use is for postorder directory visits, where it causes the
663 directory to be revisited (in both preorder and postorder) as well as all
664 of its descendants.
665 .TP
666 .BR FTS_FOLLOW
667 The referenced file must be a symbolic link.
668 If the referenced file is the one most recently returned by
669 .BR fts_read (),
670 the next call to
671 .BR fts_read ()
672 returns the file with the
673 .I fts_info
674 and
675 .I fts_statp
676 fields reinitialized to reflect the target of the symbolic link instead
677 of the symbolic link itself.
678 If the file is one of those most recently returned by
679 .BR fts_children (),
680 the
681 .I fts_info
682 and
683 .I fts_statp
684 fields of the structure, when returned by
685 .BR fts_read (),
686 will reflect the target of the symbolic link instead of the symbolic link
687 itself.
688 In either case, if the target of the symbolic link does not exist, the
689 fields of the returned structure will be unchanged and the
690 .I fts_info
691 field will be set to
692 .BR FTS_SLNONE .
693 .IP
694 If the target of the link is a directory, the preorder return, followed
695 by the return of all of its descendants, followed by a postorder return,
696 is done.
697 .TP
698 .BR FTS_SKIP
699 No descendants of this file are visited.
700 The file may be one of those most recently returned by either
701 .BR fts_children ()
702 or
703 .BR fts_read ().
704 .\" .El
705 .SS fts_close()
706 The
707 .BR fts_close ()
708 function closes the file hierarchy stream referred to by
709 .I ftsp
710 and restores the current directory to the directory from which
711 .BR fts_open ()
712 was called to open
713 .IR ftsp .
714 The
715 .BR fts_close ()
716 function
717 returns 0 on success, and \-1 if an error occurs.
718 .SH ERRORS
719 The function
720 .BR fts_open ()
721 may fail and set
722 .I errno
723 for any of the errors specified for
724 .BR open (2)
725 and
726 .BR malloc (3).
727 .PP
728 The function
729 .BR fts_close ()
730 may fail and set
731 .I errno
732 for any of the errors specified for
733 .BR chdir (2)
734 and
735 .BR close (2).
736 .PP
737 The functions
738 .BR fts_read ()
739 and
740 .BR fts_children ()
741 may fail and set
742 .I errno
743 for any of the errors specified for
744 .BR chdir (2),
745 .BR malloc (3),
746 .BR opendir (3),
747 .BR readdir (3),
748 and
749 .BR stat (2).
750 .PP
751 In addition,
752 .BR fts_children (),
753 .BR fts_open (),
754 and
755 .BR fts_set ()
756 may fail and set
757 .I errno
758 as follows:
759 .TP
760 .B EINVAL
761 .I options
762 or
763 .I instr
764 was invalid.
765 .SH VERSIONS
766 These functions are available in Linux since glibc2.
767 .SH ATTRIBUTES
768 For an explanation of the terms used in this section, see
769 .BR attributes (7).
770 .ad l
771 .nh
772 .TS
773 allbox;
774 lbx lb lb
775 l l l.
776 Interface Attribute Value
777 T{
778 .BR fts_open (),
779 .BR fts_set (),
780 .BR fts_close ()
781 T} Thread safety MT-Safe
782 T{
783 .BR fts_read (),
784 .BR fts_children ()
785 T} Thread safety MT-Unsafe
786 .TE
787 .hy
788 .ad
789 .sp 1
790 .SH CONFORMING TO
791 4.4BSD.
792 .SH BUGS
793 In versions of glibc before 2.23,
794 .\" Fixed by commit 8b7b7f75d91f7bac323dd6a370aeb3e9c5c4a7d5
795 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=15838
796 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=11460
797 all of the APIs described in this man page are not safe when compiling
798 a program using the LFS APIs (e.g., when compiling with
799 .IR \-D_FILE_OFFSET_BITS=64 ).
800 .\"
801 .\" The following statement is years old, and seems no closer to
802 .\" being true -- mtk
803 .\" The
804 .\" .I fts
805 .\" utility is expected to be included in a future
806 .\" POSIX.1
807 .\" revision.
808 .SH SEE ALSO
809 .BR find (1),
810 .BR chdir (2),
811 .BR stat (2),
812 .BR ftw (3),
813 .BR qsort (3)