xref: /freebsd/lib/libc/gen/fts.3 (revision 6990ffd8a95caaba6858ad44ff1b3157d1efba8f)
1.\" Copyright (c) 1989, 1991, 1993, 1994
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     @(#)fts.3	8.5 (Berkeley) 4/16/94
33.\" $FreeBSD$
34.\"
35.Dd April 16, 1994
36.Dt FTS 3
37.Os
38.Sh NAME
39.Nm fts
40.Nd traverse a file hierarchy
41.Sh LIBRARY
42.Lb libc
43.Sh SYNOPSIS
44.Fd #include <sys/types.h>
45.Fd #include <sys/stat.h>
46.Fd #include <fts.h>
47.Ft FTS *
48.Fn fts_open "char * const *path_argv" "int options" "int (*compar)(const FTSENT **, const FTSENT **)"
49.Ft FTSENT *
50.Fn fts_read "FTS *ftsp"
51.Ft FTSENT *
52.Fn fts_children "FTS *ftsp" "int options"
53.Ft int
54.Fn fts_set "FTS *ftsp" "FTSENT *f" "int options"
55.Ft int
56.Fn fts_close "FTS *ftsp"
57.Sh DESCRIPTION
58The
59.Nm
60functions are provided for traversing
61.Tn UNIX
62file hierarchies.
63A simple overview is that the
64.Fn fts_open
65function returns a
66.Dq handle
67on a file hierarchy, which is then supplied to
68the other
69.Nm
70functions.
71The function
72.Fn fts_read
73returns a pointer to a structure describing one of the files in the file
74hierarchy.
75The function
76.Fn fts_children
77returns a pointer to a linked list of structures, each of which describes
78one of the files contained in a directory in the hierarchy.
79In general, directories are visited two distinguishable times; in pre-order
80(before any of their descendants are visited) and in post-order (after all
81of their descendants have been visited).
82Files are visited once.
83It is possible to walk the hierarchy
84.Dq logically
85(ignoring symbolic links)
86or physically (visiting symbolic links), order the walk of the hierarchy or
87prune and/or re-visit portions of the hierarchy.
88.Pp
89Two structures are defined (and typedef'd) in the include file
90.Aq Pa fts.h .
91The first is
92.Fa FTS ,
93the structure that represents the file hierarchy itself.
94The second is
95.Fa FTSENT ,
96the structure that represents a file in the file
97hierarchy.
98Normally, an
99.Fa FTSENT
100structure is returned for every file in the file
101hierarchy.
102In this manual page,
103.Dq file
104and
105.Dq Fa FTSENT No structure
106are generally
107interchangeable.
108The
109.Fa FTSENT
110structure contains at least the following fields, which are
111described in greater detail below:
112.Bd -literal
113typedef struct _ftsent {
114	u_short fts_info;		/* flags for FTSENT structure */
115	char *fts_accpath;		/* access path */
116	char *fts_path;			/* root path */
117	u_short fts_pathlen;		/* strlen(fts_path) */
118	char *fts_name;			/* file name */
119	u_short fts_namelen;		/* strlen(fts_name) */
120	short fts_level;		/* depth (\-1 to N) */
121	int fts_errno;			/* file errno */
122	long fts_number;		/* local numeric value */
123	void *fts_pointer;		/* local address value */
124	struct ftsent *fts_parent;	/* parent directory */
125	struct ftsent *fts_link;	/* next file structure */
126	struct ftsent *fts_cycle;	/* cycle structure */
127	struct stat *fts_statp;		/* stat(2) information */
128} FTSENT;
129.Ed
130.Pp
131These fields are defined as follows:
132.Bl -tag -width "fts_namelen"
133.It Fa fts_info
134One of the following values describing the returned
135.Fa FTSENT
136structure and
137the file it represents.
138With the exception of directories without errors
139.Pq Dv FTS_D ,
140all of these
141entries are terminal, that is, they will not be revisited, nor will any
142of their descendants be visited.
143.Bl  -tag -width FTS_DEFAULT
144.It Dv FTS_D
145A directory being visited in pre-order.
146.It Dv FTS_DC
147A directory that causes a cycle in the tree.
148(The
149.Fa fts_cycle
150field of the
151.Fa FTSENT
152structure will be filled in as well.)
153.It Dv FTS_DEFAULT
154Any
155.Fa FTSENT
156structure that represents a file type not explicitly described
157by one of the other
158.Fa fts_info
159values.
160.It Dv FTS_DNR
161A directory which cannot be read.
162This is an error return, and the
163.Fa fts_errno
164field will be set to indicate what caused the error.
165.It Dv FTS_DOT
166A file named
167.Ql .\&
168or
169.Ql ..\&
170which was not specified as a file name to
171.Fn fts_open
172(see
173.Dv FTS_SEEDOT ) .
174.It Dv FTS_DP
175A directory being visited in post-order.
176The contents of the
177.Fa FTSENT
178structure will be unchanged from when
179it was returned in pre-order, i.e. with the
180.Fa fts_info
181field set to
182.Dv FTS_D .
183.It Dv FTS_ERR
184This is an error return, and the
185.Fa fts_errno
186field will be set to indicate what caused the error.
187.It Dv FTS_F
188A regular file.
189.It Dv FTS_NS
190A file for which no
191.Xr stat 2
192information was available.
193The contents of the
194.Fa fts_statp
195field are undefined.
196This is an error return, and the
197.Fa fts_errno
198field will be set to indicate what caused the error.
199.It Dv FTS_NSOK
200A file for which no
201.Xr stat 2
202information was requested.
203The contents of the
204.Fa fts_statp
205field are undefined.
206.It Dv FTS_SL
207A symbolic link.
208.It Dv FTS_SLNONE
209A symbolic link with a non-existent target.
210The contents of the
211.Fa fts_statp
212field reference the file characteristic information for the symbolic link
213itself.
214.El
215.It Fa fts_accpath
216A path for accessing the file from the current directory.
217.It Fa fts_path
218The path for the file relative to the root of the traversal.
219This path contains the path specified to
220.Fn fts_open
221as a prefix.
222.It Fa fts_pathlen
223The length of the string referenced by
224.Fa fts_path .
225.It Fa fts_name
226The name of the file.
227.It Fa fts_namelen
228The length of the string referenced by
229.Fa fts_name .
230.It Fa fts_level
231The depth of the traversal, numbered from \-1 to N, where this file
232was found.
233The
234.Fa FTSENT
235structure representing the parent of the starting point (or root)
236of the traversal is numbered \-1, and the
237.Fa FTSENT
238structure for the root
239itself is numbered 0.
240.It Fa fts_errno
241Upon return of a
242.Fa FTSENT
243structure from the
244.Fn fts_children
245or
246.Fn fts_read
247functions, with its
248.Fa fts_info
249field set to
250.Dv FTS_DNR ,
251.Dv FTS_ERR
252or
253.Dv FTS_NS ,
254the
255.Fa fts_errno
256field contains the value of the external variable
257.Va errno
258specifying the cause of the error.
259Otherwise, the contents of the
260.Fa fts_errno
261field are undefined.
262.It Fa fts_number
263This field is provided for the use of the application program and is
264not modified by the
265.Nm
266functions.
267It is initialized to 0.
268.It Fa fts_pointer
269This field is provided for the use of the application program and is
270not modified by the
271.Nm
272functions.
273It is initialized to
274.Dv NULL .
275.It Fa fts_parent
276A pointer to the
277.Fa FTSENT
278structure referencing the file in the hierarchy
279immediately above the current file, i.e. the directory of which this
280file is a member.
281A parent structure for the initial entry point is provided as well,
282however, only the
283.Fa fts_level ,
284.Fa fts_number
285and
286.Fa fts_pointer
287fields are guaranteed to be initialized.
288.It Fa fts_link
289Upon return from the
290.Fn fts_children
291function, the
292.Fa fts_link
293field points to the next structure in the NULL-terminated linked list of
294directory members.
295Otherwise, the contents of the
296.Fa fts_link
297field are undefined.
298.It Fa fts_cycle
299If a directory causes a cycle in the hierarchy (see
300.Dv FTS_DC ) ,
301either because
302of a hard link between two directories, or a symbolic link pointing to a
303directory, the
304.Fa fts_cycle
305field of the structure will point to the
306.Fa FTSENT
307structure in the hierarchy that references the same file as the current
308.Fa FTSENT
309structure.
310Otherwise, the contents of the
311.Fa fts_cycle
312field are undefined.
313.It Fa fts_statp
314A pointer to
315.Xr stat 2
316information for the file.
317.El
318.Pp
319A single buffer is used for all of the paths of all of the files in the
320file hierarchy.
321Therefore, the
322.Fa fts_path
323and
324.Fa fts_accpath
325fields are guaranteed to be
326.Dv NUL Ns -terminated
327.Em only
328for the file most recently returned by
329.Fn fts_read .
330To use these fields to reference any files represented by other
331.Fa FTSENT
332structures will require that the path buffer be modified using the
333information contained in that
334.Fa FTSENT
335structure's
336.Fa fts_pathlen
337field.
338Any such modifications should be undone before further calls to
339.Fn fts_read
340are attempted.
341The
342.Fa fts_name
343field is always
344.Dv NUL Ns -terminated .
345.Sh FTS_OPEN
346The
347.Fn fts_open
348function takes a pointer to an array of character pointers naming one
349or more paths which make up a logical file hierarchy to be traversed.
350The array must be terminated by a
351.Dv NULL
352pointer.
353.Pp
354There are
355a number of options, at least one of which (either
356.Dv FTS_LOGICAL
357or
358.Dv FTS_PHYSICAL )
359must be specified.
360The options are selected by
361.Em or Ns 'ing
362the following values:
363.Bl -tag -width "FTS_PHYSICAL"
364.It Dv FTS_COMFOLLOW
365This option causes any symbolic link specified as a root path to be
366followed immediately whether or not
367.Dv FTS_LOGICAL
368is also specified.
369.It Dv FTS_LOGICAL
370This option causes the
371.Nm
372routines to return
373.Fa FTSENT
374structures for the targets of symbolic links
375instead of the symbolic links themselves.
376If this option is set, the only symbolic links for which
377.Fa FTSENT
378structures
379are returned to the application are those referencing non-existent files.
380Either
381.Dv FTS_LOGICAL
382or
383.Dv FTS_PHYSICAL
384.Em must
385be provided to the
386.Fn fts_open
387function.
388.It Dv FTS_NOCHDIR
389As a performance optimization, the
390.Nm
391functions change directories as they walk the file hierarchy.
392This has the side-effect that an application cannot rely on being
393in any particular directory during the traversal.
394The
395.Dv FTS_NOCHDIR
396option turns off this optimization, and the
397.Nm
398functions will not change the current directory.
399Note that applications should not themselves change their current directory
400and try to access files unless
401.Dv FTS_NOCHDIR
402is specified and absolute
403pathnames were provided as arguments to
404.Fn fts_open .
405.It Dv FTS_NOSTAT
406By default, returned
407.Fa FTSENT
408structures reference file characteristic information (the
409.Fa statp
410field) for each file visited.
411This option relaxes that requirement as a performance optimization,
412allowing the
413.Nm
414functions to set the
415.Fa fts_info
416field to
417.Dv FTS_NSOK
418and leave the contents of the
419.Fa statp
420field undefined.
421.It Dv FTS_PHYSICAL
422This option causes the
423.Nm
424routines to return
425.Fa FTSENT
426structures for symbolic links themselves instead
427of the target files they point to.
428If this option is set,
429.Fa FTSENT
430structures for all symbolic links in the
431hierarchy are returned to the application.
432Either
433.Dv FTS_LOGICAL
434or
435.Dv FTS_PHYSICAL
436.Em must
437be provided to the
438.Fn fts_open
439function.
440.It Dv FTS_SEEDOT
441By default, unless they are specified as path arguments to
442.Fn fts_open ,
443any files named
444.Ql .\&
445or
446.Ql ..\&
447encountered in the file hierarchy are ignored.
448This option causes the
449.Nm
450routines to return
451.Fa FTSENT
452structures for them.
453.It Dv FTS_XDEV
454This option prevents
455.Nm
456from descending into directories that have a different device number
457than the file from which the descent began.
458.El
459.Pp
460The argument
461.Fn compar
462specifies a user-defined function which may be used to order the traversal
463of the hierarchy.
464It
465takes two pointers to pointers to
466.Fa FTSENT
467structures as arguments and
468should return a negative value, zero, or a positive value to indicate
469if the file referenced by its first argument comes before, in any order
470with respect to, or after, the file referenced by its second argument.
471The
472.Fa fts_accpath ,
473.Fa fts_path
474and
475.Fa fts_pathlen
476fields of the
477.Fa FTSENT
478structures may
479.Em never
480be used in this comparison.
481If the
482.Fa fts_info
483field is set to
484.Dv FTS_NS
485or
486.Dv FTS_NSOK ,
487the
488.Fa fts_statp
489field may not either.
490If the
491.Fn compar
492argument is
493.Dv NULL ,
494the directory traversal order is in the order listed in
495.Fa path_argv
496for the root paths, and in the order listed in the directory for
497everything else.
498.Sh FTS_READ
499The
500.Fn fts_read
501function returns a pointer to an
502.Fa FTSENT
503structure describing a file in
504the hierarchy.
505Directories (that are readable and do not cause cycles) are visited at
506least twice, once in pre-order and once in post-order.
507All other files are visited at least once.
508(Hard links between directories that do not cause cycles or symbolic
509links to symbolic links may cause files to be visited more than once,
510or directories more than twice.)
511.Pp
512If all the members of the hierarchy have been returned,
513.Fn fts_read
514returns
515.Dv NULL
516and sets the external variable
517.Va errno
518to 0.
519If an error unrelated to a file in the hierarchy occurs,
520.Fn fts_read
521returns
522.Dv NULL
523and sets
524.Va errno
525appropriately.
526If an error related to a returned file occurs, a pointer to an
527.Fa FTSENT
528structure is returned, and
529.Va errno
530may or may not have been set (see
531.Fa fts_info ) .
532.Pp
533The
534.Fa FTSENT
535structures returned by
536.Fn fts_read
537may be overwritten after a call to
538.Fn fts_close
539on the same file hierarchy stream, or, after a call to
540.Fn fts_read
541on the same file hierarchy stream unless they represent a file of type
542directory, in which case they will not be overwritten until after a call to
543.Fn fts_read
544after the
545.Fa FTSENT
546structure has been returned by the function
547.Fn fts_read
548in post-order.
549.Sh FTS_CHILDREN
550The
551.Fn fts_children
552function returns a pointer to an
553.Fa FTSENT
554structure describing the first entry in a NULL-terminated linked list of
555the files in the directory represented by the
556.Fa FTSENT
557structure most recently returned by
558.Fn fts_read .
559The list is linked through the
560.Fa fts_link
561field of the
562.Fa FTSENT
563structure, and is ordered by the user-specified comparison function, if any.
564Repeated calls to
565.Fn fts_children
566will recreate this linked list.
567.Pp
568As a special case, if
569.Fn fts_read
570has not yet been called for a hierarchy,
571.Fn fts_children
572will return a pointer to the files in the logical directory specified to
573.Fn fts_open ,
574i.e. the arguments specified to
575.Fn fts_open .
576Otherwise, if the
577.Fa FTSENT
578structure most recently returned by
579.Fn fts_read
580is not a directory being visited in pre-order,
581or the directory does not contain any files,
582.Fn fts_children
583returns
584.Dv NULL
585and sets
586.Va errno
587to zero.
588If an error occurs,
589.Fn fts_children
590returns
591.Dv NULL
592and sets
593.Va errno
594appropriately.
595.Pp
596The
597.Fa FTSENT
598structures returned by
599.Fn fts_children
600may be overwritten after a call to
601.Fn fts_children ,
602.Fn fts_close
603or
604.Fn fts_read
605on the same file hierarchy stream.
606.Pp
607.Em Option
608may be set to the following value:
609.Bl -tag -width FTS_NAMEONLY
610.It Dv FTS_NAMEONLY
611Only the names of the files are needed.
612The contents of all the fields in the returned linked list of structures
613are undefined with the exception of the
614.Fa fts_name
615and
616.Fa fts_namelen
617fields.
618.El
619.Sh FTS_SET
620The function
621.Fn fts_set
622allows the user application to determine further processing for the
623file
624.Fa f
625of the stream
626.Fa ftsp .
627The
628.Fn fts_set
629function
630returns 0 on success, and \-1 if an error occurs.
631.Em Option
632must be set to one of the following values:
633.Bl -tag -width FTS_PHYSICAL
634.It Dv FTS_AGAIN
635Re-visit the file; any file type may be re-visited.
636The next call to
637.Fn fts_read
638will return the referenced file.
639The
640.Fa fts_stat
641and
642.Fa fts_info
643fields of the structure will be reinitialized at that time,
644but no other fields will have been changed.
645This option is meaningful only for the most recently returned
646file from
647.Fn fts_read .
648Normal use is for post-order directory visits, where it causes the
649directory to be re-visited (in both pre and post-order) as well as all
650of its descendants.
651.It Dv FTS_FOLLOW
652The referenced file must be a symbolic link.
653If the referenced file is the one most recently returned by
654.Fn fts_read ,
655the next call to
656.Fn fts_read
657returns the file with the
658.Fa fts_info
659and
660.Fa fts_statp
661fields reinitialized to reflect the target of the symbolic link instead
662of the symbolic link itself.
663If the file is one of those most recently returned by
664.Fn fts_children ,
665the
666.Fa fts_info
667and
668.Fa fts_statp
669fields of the structure, when returned by
670.Fn fts_read ,
671will reflect the target of the symbolic link instead of the symbolic link
672itself.
673In either case, if the target of the symbolic link does not exist the
674fields of the returned structure will be unchanged and the
675.Fa fts_info
676field will be set to
677.Dv FTS_SLNONE .
678.Pp
679If the target of the link is a directory, the pre-order return, followed
680by the return of all of its descendants, followed by a post-order return,
681is done.
682.It Dv FTS_SKIP
683No descendants of this file are visited.
684The file may be one of those most recently returned by either
685.Fn fts_children
686or
687.Fn fts_read .
688.El
689.Sh FTS_CLOSE
690The
691.Fn fts_close
692function closes a file hierarchy stream
693.Fa ftsp
694and restores the current directory to the directory from which
695.Fn fts_open
696was called to open
697.Fa ftsp .
698The
699.Fn fts_close
700function
701returns 0 on success, and \-1 if an error occurs.
702.Sh ERRORS
703The function
704.Fn fts_open
705may fail and set
706.Va errno
707for any of the errors specified for the library functions
708.Xr open 2
709and
710.Xr malloc 3 .
711.Pp
712The function
713.Fn fts_close
714may fail and set
715.Va errno
716for any of the errors specified for the library functions
717.Xr chdir 2
718and
719.Xr close 2 .
720.Pp
721The functions
722.Fn fts_read
723and
724.Fn fts_children
725may fail and set
726.Va errno
727for any of the errors specified for the library functions
728.Xr chdir 2 ,
729.Xr malloc 3 ,
730.Xr opendir 3 ,
731.Xr readdir 3
732and
733.Xr stat 2 .
734.Pp
735In addition,
736.Fn fts_children ,
737.Fn fts_open
738and
739.Fn fts_set
740may fail and set
741.Va errno
742as follows:
743.Bl -tag -width Er
744.It Bq Er EINVAL
745The options were invalid.
746.El
747.Sh SEE ALSO
748.Xr find 1 ,
749.Xr chdir 2 ,
750.Xr stat 2 ,
751.Xr qsort 3
752.Sh STANDARDS
753The
754.Nm
755utility is expected to be included in a future
756.St -p1003.1-88
757revision.
758