use getchar() for queryuser function; lots of minor lint
[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.\"
91cff1e1 6.\" @(#)fts.3 5.6 (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
19FTS *
9a0aa1ee 20ftsopen(path_argv, options, compar)
c4e1185d
KB
21char *path_argv[];
22int options, (*compar)();
23
24FTSENT *
25ftsread(ftsp);
26FTS *ftsp;
27
28FTSENT *
29ftschildren(ftsp)
30FTS *ftsp;
31
32ftsset(ftsp, f, options)
33FTS *ftsp;
34FTSENT *f;
35int options;
36
37ftsclose(ftsp)
38FTS *ftsp;
39.ft R
40.fi
41.SH DESCRIPTION
42The
43.IR fts (3)
44utilities are provided for traversing UNIX file hierarchies.
45Two structures are defined (and typedef'd) in the include file <\fIfts.h\fP>.
46The first is FTS, the structure that defines the file hierarchy stream.
47The second is FTSENT, the structure that defines a file in the file
48hierarchy.
49.PP
50The
51.I ftsopen
9a0aa1ee
KB
52routine takes a pointer to an array of character pointers (``argv'')
53naming the file hierarchies to be traversed.
54The array must be terminated by a pointer to a NULL string.
55.PP
c4e1185d
KB
56The
57.I options
58specified are formed by
59.IR or 'ing
60one or more of the following values:
61.TP
62FTS_LOGICAL
63This option causes
64.I ftsread
65to use the function
66.IR stat (2),
67by default, to determine the status of each file.
68If this option is set, the only symbolic links returned to the application
69are those referencing non-existent files.
70Either FTS_LOGICAL or FTS_PHYSICAL
71.B must
72be provided to the
73.I ftsopen
74routine.
75.TP
c4e1185d
KB
76FTS_NOCHDIR
77As a performance optimization,
78.I ftsread
79changes directories as it descends the hierarchy.
80This has the side-effect that applications cannot rely on being
81in any particular directory.
82The FTS_NOCHDIR option turns off this optimization.
83Note that applications should not change the current directory
84(even if FTS_NOCHDIR is specified) unless absolute pathnames were
85provided as arguments to
86.IR ftsopen .
87.TP
88FTS_NOSTAT
89By default,
90.I ftsread
91and
92.I ftschildren
93provide file characteristic information (the
94.I statb
95field) for each file they return.
96This option relaxes that requirement; the contents of the
97.I statb
98field may be undefined, and the
99.I info
100field may be set to FTS_NS.
101.TP
102FTS_PHYSICAL
103This option causes
104.I ftsread
105to use the function
106.IR lstat (2),
107by default, to determine the status of each file.
108If this option is set, all symbolic links are returned to the application
109program.
110Either FTS_LOGICAL or FTS_PHYSICAL
111.B must
112be provided to the
113.I ftsopen
114routine.
115.TP
116FTS_SEEDOT
117This option causes the routine
118.I ftsread
119to return structures for the directory entries ``.'' and ``..''.
120By default they are ignored unless specified as an argument to
121.IR ftsopen .
97bb157b
KB
122.TP
123FTS_XDEV
124This option keeps
125.I fts
126from descending into directories that have a different device number
127than the file the descent began from.
c4e1185d
KB
128.PP
129The argument
130.I compar
131specifies a user-defined routine which is used to order the traversal
132of directories.
133.I Compar
134takes two pointers to pointers to FTSENT structures as arguments and
135should return a negative value, zero, or a positive value to indicate
136if the file referenced by its first argument comes before, in any order
137with respect to, or after, the file referenced by its second argument.
9a0aa1ee
KB
138.PP
139The
df589a0d 140.I fts_accpath
c4e1185d 141and
df589a0d 142.I fts_path
c4e1185d 143fields of the FTSENT structures may not be used in this comparison.
9a0aa1ee
KB
144If the option
145.I FTS_NOSTAT
146is specified, the
147.I fts_stab
148field may not be used as well.
b42bc245
KB
149If the
150.I compar
9a0aa1ee
KB
151argument is NULL the directory traversal order is unspecified except
152for the root paths which are traversed in the order listed in
153.IR path_argv .
c4e1185d
KB
154.PP
155The
156.I ftsclose
157routine closes a file hierarchy stream and changes the current
158directory to the directory
159.I ftsopen
160was called from.
161.I Ftsclose
162returns 0 on success, and -1 if an error occurs.
163.PP
164Each call to the
165.I ftsread
166routine returns a pointer to an FTSENT structure describing a file in
167the hierarchy.
168Directories (that are readable, searchable and do not cause cycles)
169are visited at least twice, before any of their descendants have been
170visited (pre-order) and after all their descendants have been visited
171(post-order).
172All other files are visited at least once.
173.PP
174The FTSENT structure contains at least the following fields:
175.sp
176.RS
177.nf
178.ta .5i +.5i +\w'struct ftsent *parent;\0\0\0'u
179typedef struct ftsent {
df589a0d
KB
180 struct ftsent *fts_parent; /* parent structure */
181 struct ftsent *fts_link; /* cycle or file structure */
c4e1185d
KB
182 union {
183 long number; /* local numeric value */
184 void *pointer; /* local address value */
df589a0d
KB
185 } fts_local;
186 char *fts_accpath; /* path from current directory */
187 char *fts_path; /* path from starting directory */
188 char *fts_name; /* file name */
189 short fts_pathlen; /* strlen(path) */
190 short fts_namelen; /* strlen(name) */
191 short fts_level; /* depth (\-1 to N) */
192 unsigned short fts_info; /* flags for entry */
193 struct stat fts_statb; /* stat(2) information */
c4e1185d
KB
194} FTSENT;
195.fi
196.RE
197.PP
198The fields are as follows:
199.TP
df589a0d 200fts_parent
c4e1185d
KB
201A pointer to a structure referencing the file in the hierarchy
202immediately above the current file/structure.
203A parent structure for the initial entry point is provided as well,
204however, only the
df589a0d 205.I fts_local
c4e1185d 206and
df589a0d 207.I fts_level
c4e1185d
KB
208fields are guaranteed to be initialized.
209.TP
df589a0d 210fts_link
c4e1185d
KB
211If a directory causes a cycle in the hierarchy, either because of a
212hard link between two directories, or a symbolic link pointing to a
213directory, the
df589a0d 214.I fts_link
c4e1185d
KB
215field of the structure will point to the structure in the hierarchy
216that references the same file as the current structure.
217Upon return from the
218.I ftschildren
219routine, the
df589a0d 220.I fts_link
c4e1185d
KB
221field points to the next structure in the linked list of entries
222from the directory.
df589a0d
KB
223Otherwise, the contents of the
224.I fts_link
225field are undefined.
c4e1185d 226.TP
df589a0d 227fts_local
c4e1185d
KB
228This field is provided for the use of the application program.
229It can be used to store either a long value or an address.
230.TP
df589a0d 231fts_accpath
c4e1185d
KB
232A path that can be used to access the file.
233.TP
df589a0d 234fts_path
c4e1185d
KB
235The path for the file relative to the root of the traversal.
236.TP
df589a0d 237fts_name
c4e1185d
KB
238The basename of the file.
239.TP
df589a0d 240fts_pathlen
c4e1185d 241The length of the string referenced by
df589a0d 242.IR fts_path .
c4e1185d 243.TP
df589a0d 244fts_namelen
c4e1185d 245The length of the string referenced by
df589a0d 246.IR fts_name .
c4e1185d 247.TP
df589a0d 248fts_level
c4e1185d
KB
249The depth of the traversal, numbered from \-1 to N.
250The parent structure of the root of the traversal is numbered \-1, and
251the structure for the root of the traversal is numbered 0.
252.TP
df589a0d 253fts_info
c4e1185d
KB
254Information describing the file.
255With the exception of directories (FTS_D), all of these entries are
256terminal, i.e. they will not be revisited, nor will their descendants
257be visited (if they have not been visited already).
258.RS
259.TP
260FTS_D
261A directory being visited in pre-order.
262.TP
263FTS_DC
264A directory that causes a cycle.
265The
df589a0d 266.I fts_link
c4e1185d
KB
267field of the structure will be filled in as well.
268.TP
269FTS_DEFAULT
270Anything that's not one of the others.
271.TP
272FTS_DNR
273A directory that cannot be read.
274.TP
275FTS_DNX
276A directory that cannot be searched.
277.TP
278FTS_DOT
279A file named ``.'' or ``..'' with a
df589a0d 280.I fts_level
c4e1185d
KB
281field not equal to zero, i.e. one not specified as an argument to
282.IR ftsopen .
283(See FTS_SEEDOT.)
284.TP
285FTS_DP
286A directory that is being visited in post-order.
287The contents of the structure will be unchanged from when the
288directory was visited in pre-order.
289.TP
290FTS_ERR
291An error indicator; the external variable
292.I errno
293contains an error number indicating the reason for the error.
294.TP
295FTS_F
296A regular file.
297.TP
298FTS_NS
299No
300.IR stat (2)
301information is available at this time (see FTS_NOSTAT); the
302contents of the
df589a0d 303.I fts_statb
c4e1185d
KB
304field are undefined.
305.TP
306FTS_SL
307A symbolic link.
308.TP
309FTS_SLNONE
310A symbolic link with a non-existent target.
311.RE
312.TP
df589a0d 313fts_statb
c4e1185d
KB
314.IR Stat (2)
315information for the file.
316.PP
317The
df589a0d 318.I fts_accpath
c4e1185d 319and
df589a0d 320.I fts_path
c4e1185d
KB
321fields are guaranteed to be NULL-terminated
322.B only
323for the file most recently returned by
324.IR ftsread .
325The
df589a0d 326.I fts_name
c4e1185d
KB
327field is always NULL-terminated.
328To use these fields to reference any other active files may require
329that they be modified using the information contained in the
df589a0d 330.I fts_pathlen
c4e1185d
KB
331field.
332Any such modifications should be undone before further calls to
333.I ftsread
334are attempted.
335.PP
336If all the members of the hierarchy have been returned,
337.I ftsread
338returns NULL and sets the external variable
339.I errno
340to 0.
341If an error unrelated to a file in the hierarchy occurs,
342.I ftsread
343returns NULL and sets errno appropriately.
344Otherwise, a pointer to an FTSENT structure is returned, and an
345error may or may not have occurred; see FTS_ERR above.
346.PP
347When the most recently returned file from
348.I ftsread
349is a directory being visited in pre-order,
350.I ftschildren
351returns a pointer to the first entry in a linked list (sorted by
352the comparison routine, if provided) of the directory entries
353it contains.
354The linked list is linked through the
df589a0d 355.I fts_link
c4e1185d
KB
356field of the FTSENT structure.
357Each call to
358.I ftschildren
359recreates the list.
360Note, cycles are not detected until a directory is actually visited,
361therefore
362.I ftschildren
363will never return a structure with an
df589a0d 364.I fts_info
c4e1185d
KB
365field set to FTS_DC.
366If the current file is not a directory being visited in pre-order,
367or if an error occurs, or the file does not contain any entries
368.I ftschildren
369returns NULL.
370If an error occurs,
371.I ftschildren
372sets errno appropriately, otherwise it sets errno to zero.
373.PP
374The pointers returned by
375.I ftsread
376and
377.I ftschildren
378point to structures which may be overwritten.
379Structures returned by
380.I ftschildren
381and
382.I ftsread
383may be overwritten after a call to
384.I ftsclose
385on the same file hierarchy.
386Otherwise, structures returned by
387.I ftschildren
388will not be overwritten until a subsequent call to either
389.I ftschildren
390or
391.I ftsread
392on the same file hierarchy.
393Structures returned by
394.I ftsread
395will not not be overwritten until a subsequent call to
396.I ftsread
397on the same file hierarchy stream, unless it represents a file of type
398directory, in which case it will not be overwritten until after a
399subsequent call to
400.I ftsread
401after it has been returned by the function
402.I ftsread
403in post-order.
404.PP
405The routine
406.I ftsset
407allows the user application to determine further processing for the
408file
409.I f
410of the stream
411.IR ftsp .
412.I Ftsset
413returns 0 on success, and -1 if an error occurs.
414.I Option
415may be set to any one of the following values.
416.TP
417FTS_AGAIN
418Re-visit the file; any file type may be re-visited.
419The next call to
420.I ftsread
421will return the referenced file.
422The
df589a0d 423.I fts_stat
c4e1185d 424and
df589a0d 425.I fts_info
c4e1185d
KB
426fields of the structure will be reinitialized at that time,
427but no other fields will have been modified.
428This option is meaningful only for the most recently returned
429file from
430.IR ftsread .
431Normal use is for post-order directory visits, where it causes the
432directory to be re-visited (in both pre and post-order) as well as all
433of its descendants.
434.TP
435FTS_FOLLOW
436The referenced file must be a symbolic link.
437If the referenced file is the one most recently returned by
438.IR ftsread ,
439the next call to
440.I ftsread
441returns the file with the
df589a0d 442.I fts_info
c4e1185d 443and
df589a0d 444.I fts_statb
c4e1185d
KB
445fields reinitialized to reflect the target of the symbolic link instead
446of the symbolic link itself.
447If the file is one of those most recently returned by
448.IR ftschildren ,
449the
df589a0d 450.I fts_info
c4e1185d 451and
df589a0d 452.I fts_statb
c4e1185d
KB
453fields of the structure, when returned by
454.IR ftsread ,
455will reflect the target of the symbolic link instead of the symbolic link
456itself.
457In either case, if the target of the link is a directory, the pre-order
458return, followed by the return of all of its descendants, followed by a
459post-order return, is done.
460.TP
461FTS_SKIP
462No descendants of this file are visited.
463.PP
464Hard links between directories that do not cause cycles or symbolic
465links to symbolic links may cause files to be visited more than once,
466or directories more than twice.
467.SH ERRORS
468.I Ftsopen
469may fail and set errno for any of the errors specified for the library
470routine
471.IR malloc (3).
472.PP
473.I Ftsclose
474may fail and set errno for any of the errors specified for the library
475routine
476.IR chdir (2).
477.PP
478.I Ftsread
479and
480.I ftschildren
481may fail and set errno for any of the errors specified for the library
482routines
483.IR chdir (2),
484.IR getgroups (2),
485.IR malloc (3),
486.IR opendir (3),
487.IR readdir (3)
488and
489.IR stat (2).
490.SH SEE ALSO
491find(1), chdir(2), stat(2), qsort(3)
492.SH STANDARDS
493The
494.I fts
df589a0d 495utility is expected to be a superset of the POSIX 1003.1 specification.