xref: /freebsd/lib/libc/gen/ftw.3 (revision 271c3a9060f2ee55607ebe146523f888e1db2654)
1.\"	$OpenBSD: ftw.3,v 1.5 2004/01/25 14:48:32 jmc Exp $
2.\"
3.\" Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
4.\"
5.\" Permission to use, copy, modify, and distribute this software for any
6.\" purpose with or without fee is hereby granted, provided that the above
7.\" copyright notice and this permission notice appear in all copies.
8.\"
9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.\"
17.\" Sponsored in part by the Defense Advanced Research Projects
18.\" Agency (DARPA) and Air Force Research Laboratory, Air Force
19.\" Materiel Command, USAF, under agreement number F39502-99-1-0512.
20.\"
21.\" $FreeBSD$
22.\"
23.Dd July 5, 2004
24.Dt FTW 3
25.Os
26.Sh NAME
27.Nm ftw , nftw
28.Nd traverse (walk) a file tree
29.Sh SYNOPSIS
30.In ftw.h
31.Ft int
32.Fo ftw
33.Fa "const char *path"
34.Fa "int \*[lp]*fn\*[rp]\*[lp]const char *, const struct stat *, int\*[rp]"
35.Fa "int maxfds"
36.Fc
37.Ft int
38.Fo nftw
39.Fa "const char *path"
40.Fa "int \*[lp]*fn\*[rp]\*[lp]const char *, const struct stat *, int, struct FTW *\*[rp]"
41.Fa "int maxfds"
42.Fa "int flags"
43.Fc
44.Sh DESCRIPTION
45The
46.Fn ftw
47and
48.Fn nftw
49functions traverse (walk) the directory hierarchy rooted in
50.Fa path .
51For each object in the hierarchy, these functions call the function
52pointed to by
53.Fa fn .
54The
55.Fn ftw
56function passes this function a pointer to a
57.Dv NUL Ns
58-terminated string containing
59the name of the object, a pointer to a
60.Vt stat
61structure corresponding to the
62object, and an integer flag.
63The
64.Fn nftw
65function passes the aforementioned arguments plus a pointer to a
66.Vt FTW
67structure as defined by
68.In ftw.h
69(shown below):
70.Bd -literal
71struct FTW {
72    int base;	/* offset of basename into pathname */
73    int level;	/* directory depth relative to starting point */
74};
75.Ed
76.Pp
77Possible values for the flag passed to
78.Fa fn
79are:
80.Bl -tag -width ".Dv FTW_DNR"
81.It Dv FTW_F
82A regular file.
83.It Dv FTW_D
84A directory being visited in pre-order.
85.It Dv FTW_DNR
86A directory which cannot be read.
87The directory will not be descended into.
88.It Dv FTW_DP
89A directory being visited in post-order
90.Fn ( nftw
91only).
92.It Dv FTW_NS
93A file for which no
94.Xr stat 2
95information was available.
96The contents of the
97.Vt stat
98structure are undefined.
99.It Dv FTW_SL
100A symbolic link.
101.It Dv FTW_SLN
102A symbolic link with a non-existent target
103.Fn ( nftw
104only).
105.El
106.Pp
107The
108.Fn ftw
109function traverses the tree in pre-order.
110That is, it processes the directory before the directory's contents.
111.Pp
112The
113.Fa maxfds
114argument specifies the maximum number of file descriptors
115to keep open while traversing the tree.
116It has no effect in this implementation.
117.Pp
118The
119.Fn nftw
120function has an additional
121.Fa flags
122argument with the following possible values:
123.Bl -tag -width ".Dv FTW_MOUNT"
124.It Dv FTW_PHYS
125Physical walk, do not follow symbolic links.
126.It Dv FTW_MOUNT
127The walk will not cross a mount point.
128.It FTW_DEPTH
129Process directories in post-order.
130Contents of a directory are visited before the directory itself.
131By default,
132.Fn nftw
133traverses the tree in pre-order.
134.It FTW_CHDIR
135Change to a directory before reading it.
136By default,
137.Fn nftw
138will change its starting directory.
139The current working directory will be restored to its original value before
140.Fn nftw
141returns.
142.El
143.Sh RETURN VALUES
144If the tree was traversed successfully, the
145.Fn ftw
146and
147.Fn nftw
148functions return 0.
149If the function pointed to by
150.Fa fn
151returns a non-zero value,
152.Fn ftw
153and
154.Fn nftw
155will stop processing the tree and return the value from
156.Fa fn .
157Both functions return \-1 if an error is detected.
158.Sh ERRORS
159The
160.Fn ftw
161and
162.Fn nftw
163functions may fail and set
164.Va errno
165for any of the errors specified for the library functions
166.Xr close 2 ,
167.Xr open 2 ,
168.Xr stat 2 ,
169.Xr malloc 3 ,
170.Xr opendir 3
171and
172.Xr readdir 3 .
173If the
174.Dv FTW_CHDIR
175flag is set, the
176.Fn nftw
177function may fail and set
178.Va errno
179for any of the errors specified for
180.Xr chdir 2 .
181In addition, either function may fail and set
182.Va errno
183as follows:
184.Bl -tag -width Er
185.It Bq Er EINVAL
186The
187.Fa maxfds
188argument is less than 1.
189.El
190.Sh SEE ALSO
191.Xr chdir 2 ,
192.Xr close 2 ,
193.Xr open 2 ,
194.Xr stat 2 ,
195.Xr fts 3 ,
196.Xr malloc 3 ,
197.Xr opendir 3 ,
198.Xr readdir 3
199.Sh STANDARDS
200The
201.Fn ftw
202and
203.Fn nftw
204functions conform to
205.St -p1003.1-2001 .
206.Sh HISTORY
207These functions first appeared in
208.At V.3 .
209Their first
210.Fx
211appearance was in
212.Fx 5.3 .
213.Sh BUGS
214The
215.Fa maxfds
216argument is currently ignored.
217