]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - lib/libc/gen/directory.3
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / lib / libc / gen / directory.3
1 .\" Copyright (c) 1983, 1991, 1993
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 .\" 4. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)directory.3 8.1 (Berkeley) 6/4/93
29 .\" $FreeBSD$
30 .\"
31 .Dd May 6, 2015
32 .Dt DIRECTORY 3
33 .Os
34 .Sh NAME
35 .Nm opendir ,
36 .Nm fdopendir ,
37 .Nm readdir ,
38 .Nm readdir_r ,
39 .Nm telldir ,
40 .Nm seekdir ,
41 .Nm rewinddir ,
42 .Nm closedir ,
43 .Nm fdclosedir ,
44 .Nm dirfd
45 .Nd directory operations
46 .Sh LIBRARY
47 .Lb libc
48 .Sh SYNOPSIS
49 .In sys/types.h
50 .In dirent.h
51 .Ft DIR *
52 .Fn opendir "const char *filename"
53 .Ft DIR *
54 .Fn fdopendir "int fd"
55 .Ft struct dirent *
56 .Fn readdir "DIR *dirp"
57 .Ft int
58 .Fn readdir_r "DIR *dirp" "struct dirent *entry" "struct dirent **result"
59 .Ft long
60 .Fn telldir "DIR *dirp"
61 .Ft void
62 .Fn seekdir "DIR *dirp" "long loc"
63 .Ft void
64 .Fn rewinddir "DIR *dirp"
65 .Ft int
66 .Fn closedir "DIR *dirp"
67 .Ft int
68 .Fn fdclosedir "DIR *dirp"
69 .Ft int
70 .Fn dirfd "DIR *dirp"
71 .Sh DESCRIPTION
72 The
73 .Fn opendir
74 function
75 opens the directory named by
76 .Fa filename ,
77 associates a
78 .Em directory stream
79 with it
80 and
81 returns a pointer to be used to identify the
82 .Em directory stream
83 in subsequent operations.
84 The pointer
85 .Dv NULL
86 is returned if
87 .Fa filename
88 cannot be accessed, or if it cannot
89 .Xr malloc 3
90 enough memory to hold the whole thing.
91 .Pp
92 The
93 .Fn fdopendir
94 function is equivalent to the
95 .Fn opendir
96 function except that the directory is specified by a file descriptor
97 .Fa fd
98 rather than by a name.
99 The file offset associated with the file descriptor at the time of the call
100 determines which entries are returned.
101 .Pp
102 Upon successful return from
103 .Fn fdopendir ,
104 the file descriptor is under the control of the system,
105 and if any attempt is made to close the file descriptor,
106 or to modify the state of the associated description other than by means
107 of
108 .Fn closedir ,
109 .Fn readdir ,
110 .Fn readdir_r ,
111 or
112 .Fn rewinddir ,
113 the behavior is undefined.
114 Upon calling
115 .Fn closedir
116 the file descriptor is closed.
117 The
118 .Dv FD_CLOEXEC
119 flag is set on the file descriptor by a successful call to
120 .Fn fdopendir .
121 .Pp
122 The
123 .Fn readdir
124 function
125 returns a pointer to the next directory entry.
126 It returns
127 .Dv NULL
128 upon reaching the end of the directory or on error.
129 In the event of an error,
130 .Va errno
131 may be set to any of the values documented for the
132 .Xr getdirentries 2
133 system call.
134 .Pp
135 The
136 .Fn readdir_r
137 function
138 provides the same functionality as
139 .Fn readdir ,
140 but the caller must provide a directory
141 .Fa entry
142 buffer to store the results in.
143 If the read succeeds,
144 .Fa result
145 is pointed at the
146 .Fa entry ;
147 upon reaching the end of the directory
148 .Fa result
149 is set to
150 .Dv NULL .
151 The
152 .Fn readdir_r
153 function
154 returns 0 on success or an error number to indicate failure.
155 .Pp
156 The
157 .Fn telldir
158 function
159 returns a token representing the current location associated with the named
160 .Em directory stream .
161 Values returned by
162 .Fn telldir
163 are good only for the lifetime of the
164 .Dv DIR
165 pointer,
166 .Fa dirp ,
167 from which they are derived.
168 If the directory is closed and then
169 reopened, prior values returned by
170 .Fn telldir
171 will no longer be valid.
172 Values returned by
173 .Fn telldir
174 are also invalidated by a call to
175 .Fn rewinddir .
176 .Pp
177 The
178 .Fn seekdir
179 function
180 sets the position of the next
181 .Fn readdir
182 operation on the
183 .Em directory stream .
184 The new position reverts to the one associated with the
185 .Em directory stream
186 when the
187 .Fn telldir
188 operation was performed.
189 .Pp
190 The
191 .Fn rewinddir
192 function
193 resets the position of the named
194 .Em directory stream
195 to the beginning of the directory.
196 .Pp
197 The
198 .Fn closedir
199 function
200 closes the named
201 .Em directory stream
202 and frees the structure associated with the
203 .Fa dirp
204 pointer,
205 returning 0 on success.
206 On failure, \-1 is returned and the global variable
207 .Va errno
208 is set to indicate the error.
209 .Pp
210 The
211 .Fn fdclosedir
212 function is equivalent to the
213 .Fn closedir
214 function except that this function returns directory file descriptor instead of
215 closing it.
216 .Pp
217 The
218 .Fn dirfd
219 function
220 returns the integer file descriptor associated with the named
221 .Em directory stream ,
222 see
223 .Xr open 2 .
224 .Pp
225 Sample code which searches a directory for entry ``name'' is:
226 .Bd -literal -offset indent
227 dirp = opendir(".");
228 if (dirp == NULL)
229         return (ERROR);
230 len = strlen(name);
231 while ((dp = readdir(dirp)) != NULL) {
232         if (dp->d_namlen == len && strcmp(dp->d_name, name) == 0) {
233                 (void)closedir(dirp);
234                 return (FOUND);
235         }
236 }
237 (void)closedir(dirp);
238 return (NOT_FOUND);
239 .Ed
240 .Sh SEE ALSO
241 .Xr close 2 ,
242 .Xr lseek 2 ,
243 .Xr open 2 ,
244 .Xr read 2 ,
245 .Xr dir 5
246 .Sh HISTORY
247 The
248 .Fn opendir ,
249 .Fn readdir ,
250 .Fn telldir ,
251 .Fn seekdir ,
252 .Fn rewinddir ,
253 .Fn closedir ,
254 and
255 .Fn dirfd
256 functions appeared in
257 .Bx 4.2 .
258 The
259 .Fn fdopendir
260 function appeared in
261 .Fx 8.0 .
262 .Fn fdclosedir
263 function appeared in
264 .Fx 10.0 .
265 .Sh BUGS
266 The behaviour of
267 .Fn telldir
268 and
269 .Fn seekdir
270 is likely to be wrong if there are parallel unlinks happening
271 and the directory is larger than one page.
272 There is code to ensure that a
273 .Fn seekdir
274 to the location given by a 
275 .Fn telldir
276 immediately before the last 
277 .Fn readdir
278 will always set the correct location to return the same value as that last
279 .Fn readdir
280 performed.
281 This is enough for some applications which want to "push back the last entry read" E.g. Samba. 
282 Seeks back to any other location,
283 other than the beginning of the directory,
284 may result in unexpected behaviour if deletes are present.
285 It is hoped that this situation will be resolved with changes to
286 .Fn getdirentries
287 and the VFS.