1- .\" $NetBSD: directory.3,v 1.44 2026/01/22 04:01:14 dholland Exp $
1+ .\" $NetBSD: directory.3,v 1.45 2026/01/22 10:34:30 uwe Exp $
22.\"
33.\" Copyright (c) 1983, 1991, 1993
44.\" The Regents of the University of California. All rights reserved.
3232.Dd January 21, 2026
3333.Dt DIRECTORY 3
3434.Os
35+ .
3536.Sh NAME
3637.Nm fdopendir ,
3738.Nm opendir ,
4344.Nm closedir ,
4445.Nm dirfd
4546.Nd directory operations
47+ .
4648.Sh LIBRARY
4749.Lb libc
50+ .
4851.Sh SYNOPSIS
52+ .
4953.In dirent.h
54+ .
5055.Ft DIR *
5156.Fn opendir " const char *filename"
57+ .
5258.Ft DIR *
5359.Fn fdopendir " int fd"
60+ .
5461.Ft struct dirent *
5562.Fn readdir " DIR *dirp"
63+ .
5664.Ft int
57- .Fn readdir_r " DIR * restrict dirp" " struct dirent * restrict entry" " struct dirent ** restrict result"
65+ .Fo readdir_r
66+ .Fa " DIR * restrict dirp"
67+ .Fa " struct dirent * restrict entry"
68+ .Fa " struct dirent ** restrict result"
69+ .Fc
70+ .
5871.Ft long
5972.Fn telldir " DIR *dirp"
73+ .
6074.Ft void
6175.Fn seekdir " DIR *dirp" " long loc"
76+ .
6277.Ft void
6378.Fn rewinddir " DIR *dirp"
79+ .
6480.Ft int
6581.Fn closedir " DIR *dirp"
82+ .
6683.Ft int
6784.Fn dirfd " DIR *dirp"
85+ .
6886.Sh DESCRIPTION
6987The type
7088.Vt DIR
@@ -77,9 +95,11 @@ structure is similar to that of the
7795structure maintained by the
7896.Xr stdio 3
7997library functions.
98+ .
8099.Sh FUNCTIONS
81100The following standard directory operations are defined.
82- .Bl -tag -width XXX
101+ .Bl -tag -width Fn
102+ .
83103.It Fn opendir " filename"
84104The
85105.Fn opendir
@@ -94,6 +114,7 @@ Otherwise,
94114.Fn opendir
95115returns
96116.Dv NULL .
117+ .
97118.It Fn fdopendir " fd"
98119The
99120.Fn fdopendir
@@ -118,6 +139,7 @@ other than by means of
118139the behavior is undefined.
119140The file descriptor can be closed by calling
120141.Fn closedir .
142+ .
121143.It Fn readdir " dirp"
122144The
123145.Fn readdir
@@ -134,13 +156,14 @@ The returned structure is described in
134156.Xr dirent 3 .
135157.Pp
136158The returned pointer to the
137- .Em dirent
159+ .Vt dirent
138160structure points to data which may be overwritten by another call to
139161.Fn readdir
140162on the same directory stream.
141163This data is not however overwritten by another call to
142164.Fn readdir
143165on a different directory stream.
166+ .
144167.It Fn readdir_r " dirp" " entry" " result"
145168The
146169.Fn readdir_r
@@ -169,10 +192,14 @@ the
169192.Fn readdir_r
170193function may buffer several directory entries per actual read operation.
171194Both functions mark for update the
172- .Em st_atime
173- field (see
174- .Xr stat 2 )
195+ .Fa st_atime
196+ field
197+ .Po
198+ see
199+ .Xr stat 2
200+ .Pc
175201of the directory each time the directory is actually read.
202+ .
176203.It Fn telldir " dirp"
177204The
178205.Fn telldir
@@ -189,6 +216,7 @@ is the same as
189216supplied as an argument to the
190217.Fn seekdir
191218call.
219+ .
192220.It Fn seekdir " dirp" " loc"
193221The
194222.Fn seekdir
@@ -223,6 +251,7 @@ value becomes invalid.
223251.Fn seekdir
224252returns no value.
225253There is no supported way to detect if the operation succeeded.
254+ .
226255.It Fn rewinddir " dirp"
227256The
228257.Fn rewinddir
@@ -232,11 +261,14 @@ It also causes the directory stream to refer to the
232261current state of the corresponding directory, as if a call to
233262.Fn opendir
234263was made.
235- It is not specified whether this refers to the ``corresponding directory''
264+ It is not specified whether this refers to the
265+ .Dq corresponding directory
236266by name or by underlying object.
237- (These can differ if
267+ .Po
268+ These can differ if
238269.Xr rename 2
239- has been used.)
270+ has been used
271+ .Pc .
240272.\" Note: currently the underlying fd is reopened if and only if
241273.\" __DTF_READALL is in effect, which is true for union mounts and
242274.\" nfs; documenting that exactly seems inadvisable since it might
@@ -245,6 +277,7 @@ has been used.)
245277If
246278.Fa dirp
247279does not refer to a valid directory stream, the behavior is undefined.
280+ .
248281.It Fn closedir " dirp"
249282The
250283.Fn closedir
@@ -253,6 +286,7 @@ and frees the structure associated with the
253286.Fa dirp
254287pointer,
255288returning 0 on success and \- 1 on failure.
289+ .
256290.It Fn dirfd " dirp"
257291The
258292.Fn dirfd
@@ -294,13 +328,14 @@ if (dirp != NULL) {
294328}
295329return (NOT_FOUND);
296330.Ed
331+ .
297332.Sh COMPATIBILITY
298333The described directory operations have traditionally been problematic
299334in terms of portability.
300335A good example is the semantics of
301- .Sq \& .
336+ .Sq Li \& .
302337(dot) and
303- .Sq \& ..
338+ .Sq Li \& ..
304339(dot-dot).
305340Based on historical implementations,
306341the rules about file descriptors apply to directory streams as well.
@@ -312,7 +347,8 @@ implemented by using file descriptors.
312347The following additional remarks can be noted from the
313348.St -p1003.1-2008
314349standard.
315- .Bl -bullet -offset 2n
350+ .Bl -bullet
351+ .
316352.It
317353If the type
318354.Vt DIR
@@ -323,6 +359,7 @@ applications should be able to open only
323359.Dv OPEN_MAX
324360files and directories.
325361Otherwise the limit is left as unspecified.
362+ .
326363.It
327364When a file descriptor is used to implement the directory stream, the
328365.Fn closedir
@@ -332,12 +369,14 @@ flag had been set for the file descriptor.
332369In other words, it is mandatory that
333370.Fn closedir
334371deallocates the file descriptor.
372+ .
335373.It
336374If directory streams are not implemented by using file descriptors,
337375functions such as
338376.Fn dirfd
339377may fail with
340378.Er ENOTSUP .
379+ .
341380.It
342381If a file is removed from or added to the directory
343382after the most recent call to
347386it is unspecified whether a subsequent call to
348387.Fn readdir
349388returns an entry for that file.
389+ .
350390.It
351391When using the function
352392.Fn seekdir ,
363403the results of any subsequent call to
364404.Fn readdir
365405are unspecified, possibly resulting in undefined behavior.
406+ .
366407.It
367408After a call to
368409.Xr fork 2 ,
375416However, if both the parent and child processes use these functions,
376417the result is undefined.
377418.El
419+ .
378420.Sh ERRORS
379421.\"
380422.\" XXX: The errors should be enumerated.
381423.\"
382424All described functions may set
383425.Vt errno
384426to indicate the error.
427+ .
385428.Sh SEE ALSO
386429.Xr close 2 ,
387430.Xr lseek 2 ,
388431.Xr open 2 ,
389432.Xr read 2 ,
390433.Xr dirent 3
434+ .
391435.Sh STANDARDS
392436The
393437.Fn opendir ,
@@ -399,6 +443,7 @@ functions conform to
399443.St -p1003.1-90 .
400444The other functions conform to
401445.St -p1003.1-2008 .
446+ .
402447.Sh HISTORY
403448The
404449.Fn opendir ,
0 commit comments