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