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