1.\" $NetBSD: directory.3,v 1.41 2017/07/03 21:32:49 wiz Exp $ 2.\" 3.\" Copyright (c) 1983, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)directory.3 8.1 (Berkeley) 6/4/93 31.\" 32.Dd January 22, 2016 33.Dt DIRECTORY 3 34.Os 35.Sh NAME 36.Nm fdopendir , 37.Nm opendir , 38.Nm readdir , 39.Nm readdir_r , 40.Nm telldir , 41.Nm seekdir , 42.Nm rewinddir , 43.Nm closedir , 44.Nm dirfd 45.Nd directory operations 46.Sh LIBRARY 47.Lb libc 48.Sh SYNOPSIS 49.In dirent.h 50.Ft DIR * 51.Fn opendir "const char *filename" 52.Ft DIR * 53.Fn fdopendir "int fd" 54.Ft struct dirent * 55.Fn readdir "DIR *dirp" 56.Ft int 57.Fn readdir_r "DIR * restrict dirp" "struct dirent * restrict entry" "struct dirent ** restrict result" 58.Ft long 59.Fn telldir "DIR *dirp" 60.Ft void 61.Fn seekdir "DIR *dirp" "long loc" 62.Ft void 63.Fn rewinddir "DIR *dirp" 64.Ft int 65.Fn closedir "DIR *dirp" 66.Ft int 67.Fn dirfd "DIR *dirp" 68.Sh DESCRIPTION 69The type 70.Vt DIR 71represents a directory stream; 72an ordered sequence of all directory entries in a particular directory. 73The purpose of the 74.Vt DIR 75structure is similar to that of the 76.Vt FILE 77structure maintained by the 78.Xr stdio 3 79library functions. 80.Sh FUNCTIONS 81The following standard directory operations are defined. 82.Bl -tag -width XXX 83.It Fn opendir "filename" 84The 85.Fn opendir 86function opens the directory named by 87.Fa filename 88and associates a directory stream with it. 89The directory stream is positioned at the first entry. 90Upon successful completion, a pointer to 91.Vt DIR 92type is returned. 93Otherwise, 94.Fn opendir 95returns 96.Dv NULL . 97.It Fn fdopendir "fd" 98The 99.Fn fdopendir 100function associates a directory stream with the directory file descriptor 101.Fa fd . 102The file offset associated with 103.Fa fd 104at the time of the call determines which entries are returned. 105.Pp 106Upon failure, 107.Fn fdopendir 108returns 109.Dv NULL . 110Otherwise the file descriptor is under the control of the system, 111and if any attempt is made to close the file descriptor, 112or to modify the state of the associated description, 113other than by means of 114.Fn closedir , 115.Fn readdir , 116.Fn readdir_r , 117.Fn rewinddir , 118the behavior is undefined. 119The file descriptor can be closed by calling 120.Fn closedir . 121.It Fn readdir "dirp" 122The 123.Fn readdir 124function returns a pointer to the directory entry at the current position 125in the directory stream specified by 126.Fa dirp , 127and positions the directory stream at the next entry. 128It returns 129.Dv NULL 130upon reaching the end of the directory or detecting an invalid 131.Fn seekdir 132operation. 133The returned structure is described in 134.Xr dirent 3 . 135.Pp 136The returned pointer to the 137.Em dirent 138structure points to data which may be overwritten by another call to 139.Fn readdir 140on the same directory stream. 141This data is not however overwritten by another call to 142.Fn readdir 143on a different directory stream. 144.It Fn readdir_r "dirp" "entry" "result" 145The 146.Fn readdir_r 147function 148provides the same functionality as 149.Fn readdir , 150but the caller must provide a directory 151.Fa entry 152buffer to store the results in. 153If the read succeeds, 154.Fa result 155is pointed at the 156.Fa entry ; 157upon reaching the end of the directory 158.Fa result 159is set to 160.Dv NULL . 161The 162.Fn readdir_r 163function 164returns 0 on success or an error number to indicate failure. 165.Pp 166Like 167.Fn readdir , 168the 169.Fn readdir_r 170function may buffer several directory entries per actual read operation. 171Both functions mark for update the 172.Em st_atime 173field (see 174.Xr stat 2 ) 175of the directory each time the directory is actually read. 176.It Fn telldir "dirp" 177The 178.Fn telldir 179function returns the current location associated 180with the directory stream specified by 181.Fa dirp . 182.Pp 183If the most recent operation on the particular directory stream was a 184.Fn seekdir , 185the directory position returned from 186.Fn telldir 187is the same as 188.Fa loc 189supplied as an argument to the 190.Fn seekdir 191call. 192.It Fn seekdir "dirp" "loc" 193The 194.Fn seekdir 195function sets the position of the next 196.Fn readdir 197operation on the directory stream specified by 198.Fa dirp . 199The value of 200.Fa loc 201should come from a previous call to 202.Fn telldir 203using the same directory stream. 204.Pp 205The new position reverts to the one associated 206with the directory stream when the 207.Fn telldir 208operation was performed. 209Values returned by 210.Fn telldir 211are good only for the lifetime of the 212.Vt DIR 213pointer, 214.Fa dirp , 215from which they are derived. 216If the directory is closed and then reopened, the 217.Fn telldir 218value cannot be re-used. 219.It Fn rewinddir "dirp" 220The 221.Fn rewinddir 222function resets the position of the named directory 223stream to the beginning of the directory. 224It also causes the directory stream to refer to the 225current state of the corresponding directory, as if a call to 226.Fn opendir 227would have been made. 228.Pp 229If 230.Fa dirp 231does not refer to a valid directory stream, the behavior is undefined. 232.It Fn closedir "dirp" 233The 234.Fn closedir 235function closes the directory stream 236and frees the structure associated with the 237.Fa dirp 238pointer, 239returning 0 on success and \-1 on failure. 240.It Fn dirfd "dirp" 241The 242.Fn dirfd 243function returns the integer file descriptor 244associated with the directory stream specified by 245.Fa dirp . 246Upon failure, 247.Fn dirfd 248returns \-1. 249The returned file descriptor should not be closed by 250.Xr close 2 , 251it will be released when 252.Fa dirp 253is closed with 254.Fn closedir . 255.Pp 256The rationale of 257.Fn dirfd 258is to provide a mechanism by which a file descriptor 259can be obtained for the use of the 260.Xr fchdir 2 261function. 262.El 263.Sh EXAMPLES 264Sample code which searches a directory for entry 265.Dq name 266is: 267.Bd -literal -offset indent 268len = strlen(name); 269dirp = opendir("."); 270if (dirp != NULL) { 271 while ((dp = readdir(dirp)) != NULL) 272 if (dp->d_namlen == len && 273 !strcmp(dp->d_name, name)) { 274 (void)closedir(dirp); 275 return (FOUND); 276 } 277 (void)closedir(dirp); 278} 279return (NOT_FOUND); 280.Ed 281.Sh COMPATIBILITY 282The described directory operations have traditionally been problematic 283in terms of portability. 284A good example is the semantics around 285.Sq \&. 286(dot) and 287.Sq \&.. 288(dot-dot). 289Based on historical implementations, 290the rules about file descriptors apply to directory streams as well. 291The 292.St -p1003.1-2008 293standard no longer mandates that directory streams be 294implemented by using file descriptors. 295.Pp 296The following additional remarks can be noted from the 297.St -p1003.1-2008 298standard. 299.Bl -bullet -offset 2n 300.It 301If the type 302.Vt DIR 303is implemented using a file descriptor, 304like in 305.Nx , 306applications should be able to open only 307.Dv OPEN_MAX 308files and directories. 309Otherwise the limit is left as unspecified. 310.It 311When a file descriptor is used to implement the directory stream, the 312.Fn closedir 313function behaves as if the 314.Dv FD_CLOEXEC 315had been set for the file descriptor. 316In another words, it is mandatory that 317.Fn closedir 318deallocates the file descriptor. 319.It 320If directory streams are not implemented by using file descriptors, 321functions such as 322.Fn dirfd 323may fail with 324.Er ENOTSUP . 325.It 326If a file is removed from or added to the directory 327after the most recent call to 328.Fn opendir 329or 330.Fn rewinddir , 331it is unspecified whether a subsequent call to 332.Fn readdir 333returns an entry for that file. 334.It 335When using the function 336.Fn seekdir , 337note that if the value of 338.Fa loc 339was not obtained from an earlier call to 340.Fn telldir , 341or if a call to 342.Fn rewinddir 343occurred between the calls to 344.Fn telldir 345and 346.Fn seekdir , 347any subsequent call to 348.Fn readdir 349is unspecified, possibly resulting in undefined behavior. 350.It 351After a call to 352.Xr fork 2 , 353either the parent or child (but not both) can continue processing the 354directory stream using 355.Fn readdir , 356.Fn rewinddir , 357or 358.Fn seekdir . 359However, if both the parent and child processes use these functions, 360the result is undefined. 361.El 362.Sh ERRORS 363.\" 364.\" XXX: The errors should be enumerated. 365.\" 366All described functions may set 367.Vt errno 368to indicate the error. 369.Sh SEE ALSO 370.Xr close 2 , 371.Xr lseek 2 , 372.Xr open 2 , 373.Xr read 2 , 374.Xr dirent 3 375.Sh STANDARDS 376The 377.Fn opendir , 378.Fn readdir , 379.Fn rewinddir 380and 381.Fn closedir 382functions conform to 383.St -p1003.1-90 . 384The other functions conform to 385.St -p1003.1-2008 . 386.Sh HISTORY 387The 388.Fn opendir , 389.Fn readdir , 390.Fn telldir , 391.Fn seekdir , 392.Fn rewinddir , 393.Fn closedir , 394and 395.Fn dirfd 396functions appeared in 397.Bx 4.2 . 398The 399.Fn fdopendir 400function appeared in 401.Nx 6.0 . 402