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