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