1*f7d60097Sguenther.\" $OpenBSD: opendir.3,v 1.3 2024/03/23 16:30:01 guenther Exp $ 2647427d6Sderaadt.\" 3647427d6Sderaadt.\" Copyright (c) 1983, 1991, 1993 4647427d6Sderaadt.\" The Regents of the University of California. All rights reserved. 5647427d6Sderaadt.\" 6647427d6Sderaadt.\" Redistribution and use in source and binary forms, with or without 7647427d6Sderaadt.\" modification, are permitted provided that the following conditions 8647427d6Sderaadt.\" are met: 9647427d6Sderaadt.\" 1. Redistributions of source code must retain the above copyright 10647427d6Sderaadt.\" notice, this list of conditions and the following disclaimer. 11647427d6Sderaadt.\" 2. Redistributions in binary form must reproduce the above copyright 12647427d6Sderaadt.\" notice, this list of conditions and the following disclaimer in the 13647427d6Sderaadt.\" documentation and/or other materials provided with the distribution. 14647427d6Sderaadt.\" 3. Neither the name of the University nor the names of its contributors 15647427d6Sderaadt.\" may be used to endorse or promote products derived from this software 16647427d6Sderaadt.\" without specific prior written permission. 17647427d6Sderaadt.\" 18647427d6Sderaadt.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19647427d6Sderaadt.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20647427d6Sderaadt.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21647427d6Sderaadt.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22647427d6Sderaadt.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23647427d6Sderaadt.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24647427d6Sderaadt.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25647427d6Sderaadt.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26647427d6Sderaadt.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27647427d6Sderaadt.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28647427d6Sderaadt.\" SUCH DAMAGE. 29647427d6Sderaadt.\" 30*f7d60097Sguenther.Dd $Mdocdate: March 23 2024 $ 31647427d6Sderaadt.Dt OPENDIR 3 32647427d6Sderaadt.Os 33647427d6Sderaadt.Sh NAME 34647427d6Sderaadt.Nm opendir , 35647427d6Sderaadt.Nm fdopendir , 36647427d6Sderaadt.Nm readdir , 37647427d6Sderaadt.Nm readdir_r , 38647427d6Sderaadt.Nm telldir , 39647427d6Sderaadt.Nm seekdir , 40647427d6Sderaadt.Nm rewinddir , 41647427d6Sderaadt.Nm closedir , 42647427d6Sderaadt.Nm dirfd 43647427d6Sderaadt.Nd directory operations 44647427d6Sderaadt.Sh SYNOPSIS 45647427d6Sderaadt.In sys/types.h 46647427d6Sderaadt.In dirent.h 47647427d6Sderaadt.Ft DIR * 48647427d6Sderaadt.Fn opendir "const char *filename" 49647427d6Sderaadt.Ft DIR * 50647427d6Sderaadt.Fn fdopendir "int fd" 51647427d6Sderaadt.Ft struct dirent * 52647427d6Sderaadt.Fn readdir "DIR *dirp" 53647427d6Sderaadt.Ft int 54647427d6Sderaadt.Fn readdir_r "DIR *dirp" "struct dirent *entry" "struct dirent **result" 55647427d6Sderaadt.Ft long 56647427d6Sderaadt.Fn telldir "const DIR *dirp" 57647427d6Sderaadt.Ft void 58647427d6Sderaadt.Fn seekdir "DIR *dirp" "long loc" 59647427d6Sderaadt.Ft void 60647427d6Sderaadt.Fn rewinddir "DIR *dirp" 61647427d6Sderaadt.Ft int 62647427d6Sderaadt.Fn closedir "DIR *dirp" 63647427d6Sderaadt.Ft int 64647427d6Sderaadt.Fn dirfd "DIR *dirp" 65647427d6Sderaadt.Sh DESCRIPTION 66647427d6SderaadtThe 67647427d6Sderaadt.Fn opendir 68647427d6Sderaadtfunction opens the directory named by 69647427d6Sderaadt.Fa filename , 70647427d6Sderaadtassociates a directory stream with it, and returns a pointer to be used 71647427d6Sderaadtto identify the directory stream in subsequent operations. 72647427d6SderaadtOn failure, 73647427d6Sderaadt.Dv NULL 74647427d6Sderaadtis returned and 75647427d6Sderaadt.Va errno 76647427d6Sderaadtis set to indicate the error. 77647427d6Sderaadt.Pp 78647427d6SderaadtThe 79647427d6Sderaadt.Fn fdopendir 80647427d6Sderaadtfunction is equivalent to 81647427d6Sderaadt.Fn opendir 82647427d6Sderaadtexcept that the directory is specified by file descriptor rather than by name. 83647427d6SderaadtThe file offset associated with the file descriptor at the time of the call 84647427d6Sderaadtdetermines which entries are returned. 85647427d6Sderaadt.Pp 86647427d6SderaadtUpon successful return from 87647427d6Sderaadt.Fn fdopendir , 88647427d6Sderaadtthe file descriptor is under the control of the system, 89647427d6Sderaadtand if any attempt is made to close the file descriptor 90647427d6Sderaadtor to modify the state of the associated directory, 91647427d6Sderaadtother than by means of 92647427d6Sderaadt.Fn closedir , 93647427d6Sderaadt.Fn readdir , 94647427d6Sderaadt.Fn readdir_r , 95647427d6Sderaadtor 96647427d6Sderaadt.Fn rewinddir , 97647427d6Sderaadtthe behavior is undefined. 98647427d6SderaadtUpon calling 99647427d6Sderaadt.Fn closedir 100647427d6Sderaadtthe file descriptor shall be closed. 101647427d6Sderaadt.Pp 102647427d6SderaadtThe 103647427d6Sderaadt.Fn readdir 104647427d6Sderaadtfunction returns a pointer to the next directory entry in the named 105647427d6Sderaadtdirectory stream 106647427d6Sderaadt.Fa dirp . 107647427d6SderaadtIt returns 108647427d6Sderaadt.Dv NULL 109647427d6Sderaadtupon reaching the end of the directory or detecting an invalid 110647427d6Sderaadt.Fn seekdir 111647427d6Sderaadtoperation. 112647427d6Sderaadt.Pp 113647427d6SderaadtThe 114647427d6Sderaadt.Fn readdir_r 115*f7d60097Sguentherfunction is a deprecated variant of 116*f7d60097Sguenther.Fn readdir . 117*f7d60097SguentherLike 118*f7d60097Sguenther.Fn readdir , 119*f7d60097Sguentherit initializes the 120d32639f6Sjmc.Vt dirent 121647427d6Sderaadtstructure referenced by 122647427d6Sderaadt.Fa entry 123647427d6Sderaadtto represent the next directory entry in the named directory stream 124647427d6Sderaadt.Fa dirp , 125647427d6Sderaadtand stores a pointer to this structure at the location referenced by 126647427d6Sderaadt.Fa result . 127647427d6SderaadtThe storage pointed to by 128647427d6Sderaadt.Fa entry 129647427d6Sderaadtmust be large enough for a dirent with a 130647427d6Sderaadt.Fa d_name 131647427d6Sderaadtarray member containing at least 132647427d6Sderaadt.Dv NAME_MAX 133647427d6Sderaadtplus one elements. 134647427d6Sderaadt.Fn readdir_r 135647427d6Sderaadtreturns 0 on success, or an error number if an error occurs; see 136647427d6Sderaadt.Sx ERRORS . 137647427d6SderaadtOn successful return, the pointer returned at 138647427d6Sderaadt.Fa "*result" 139647427d6Sderaadtwill have the same value as the argument 140647427d6Sderaadt.Fa entry . 141647427d6SderaadtUpon reaching the end of the directory stream, this pointer shall have the value 142647427d6Sderaadt.Dv NULL . 143647427d6Sderaadt.Pp 144647427d6SderaadtThe 145647427d6Sderaadt.Fn telldir 146647427d6Sderaadtfunction returns the current location associated with the named 147647427d6Sderaadtdirectory stream 148647427d6Sderaadt.Fa dirp . 149647427d6SderaadtOn failure, \-1 is returned and 150647427d6Sderaadt.Va errno 151647427d6Sderaadtis set to indicate the error. 152647427d6Sderaadt.Pp 153647427d6SderaadtThe 154647427d6Sderaadt.Fn seekdir 155647427d6Sderaadtfunction sets the position of the next 156647427d6Sderaadt.Fn readdir 157647427d6Sderaadtoperation on the named directory stream 158647427d6Sderaadt.Fa dirp . 159647427d6SderaadtThe new position reverts to the one associated with the 160647427d6Sderaadtdirectory stream when the 161647427d6Sderaadt.Fn telldir 162647427d6Sderaadtoperation was performed. 163647427d6SderaadtValues returned by 164647427d6Sderaadt.Fn telldir 165647427d6Sderaadtare good only for the lifetime of the 166647427d6Sderaadt.Dv DIR 167647427d6Sderaadtpointer, 168647427d6Sderaadt.Fa dirp , 169647427d6Sderaadtfrom which they are derived. 170647427d6SderaadtIf the directory is closed and then reopened, the 171647427d6Sderaadt.Fn telldir 172647427d6Sderaadtvalue may be invalidated due to undetected directory compaction. 173647427d6Sderaadt.Pp 174647427d6SderaadtThe 175647427d6Sderaadt.Fn rewinddir 176647427d6Sderaadtfunction resets the position of the named directory stream 177647427d6Sderaadt.Fa dirp 178647427d6Sderaadtto the beginning of the directory. 179647427d6Sderaadt.Pp 180647427d6SderaadtThe 181647427d6Sderaadt.Fn closedir 182647427d6Sderaadtfunction closes the named directory stream and frees the structure 183647427d6Sderaadtassociated with the 184647427d6Sderaadt.Fa dirp 185647427d6Sderaadtpointer, returning 0 on success. 186647427d6SderaadtOn failure, \-1 is returned and the global variable 187647427d6Sderaadt.Va errno 188647427d6Sderaadtis set to indicate the error. 189647427d6Sderaadt.Pp 190647427d6SderaadtThe 191647427d6Sderaadt.Fn dirfd 192647427d6Sderaadtfunction returns the integer file descriptor associated with the named 193647427d6Sderaadtdirectory stream 194647427d6Sderaadt.Fa dirp 195647427d6Sderaadt(see 196647427d6Sderaadt.Xr open 2 ) . 197647427d6Sderaadt.Sh EXAMPLES 198647427d6SderaadtSample code which searches a directory for entry 199647427d6Sderaadt.Dq name 200647427d6Sderaadtis: 201647427d6Sderaadt.Bd -literal -offset indent 202647427d6Sderaadtlen = strlen(name); 203647427d6Sderaadtdirp = opendir("."); 204647427d6Sderaadtif (dirp) { 205647427d6Sderaadt while ((dp = readdir(dirp)) != NULL) 206647427d6Sderaadt if (dp->d_namlen == len && 207647427d6Sderaadt !strcmp(dp->d_name, name)) { 208647427d6Sderaadt closedir(dirp); 209647427d6Sderaadt return FOUND; 210647427d6Sderaadt } 211647427d6Sderaadt closedir(dirp); 212647427d6Sderaadt} 213647427d6Sderaadtreturn NOT_FOUND; 214647427d6Sderaadt.Ed 215647427d6Sderaadt.Sh ERRORS 216647427d6SderaadtThe 217647427d6Sderaadt.Fn opendir 218647427d6Sderaadtfunction will fail if: 219647427d6Sderaadt.Bl -tag -width Er 220647427d6Sderaadt.It Bq Er ENOTDIR 221647427d6SderaadtThe supplied 222647427d6Sderaadt.Fa filename 223647427d6Sderaadtis not a directory. 224647427d6Sderaadt.El 225647427d6Sderaadt.Pp 226647427d6SderaadtThe 227647427d6Sderaadt.Fn opendir 228647427d6Sderaadtfunction may also fail and set 229647427d6Sderaadt.Va errno 230647427d6Sderaadtfor any of the errors specified for the routines 231647427d6Sderaadt.Xr fcntl 2 , 232647427d6Sderaadt.Xr fstat 2 , 233647427d6Sderaadt.Xr open 2 , 234647427d6Sderaadtand 235647427d6Sderaadt.Xr malloc 3 . 236647427d6Sderaadt.Pp 237647427d6SderaadtThe 238647427d6Sderaadt.Fn fdopendir 239647427d6Sderaadtfunction will fail if: 240647427d6Sderaadt.Bl -tag -width Er 241647427d6Sderaadt.It Bq Er EBADF 242647427d6SderaadtThe 243647427d6Sderaadt.Fa fd 244647427d6Sderaadtargument is not a valid file descriptor open for reading. 245647427d6Sderaadt.It Bq Er ENOTDIR 246647427d6SderaadtThe descriptor 247647427d6Sderaadt.Fa fd 248647427d6Sderaadtis not associated with a directory. 249647427d6Sderaadt.El 250647427d6Sderaadt.Pp 251647427d6SderaadtThe 252647427d6Sderaadt.Fn readdir 253647427d6Sderaadtand 254647427d6Sderaadt.Fn readdir_r 255647427d6Sderaadtfunctions may also fail and set 256647427d6Sderaadt.Va errno 257647427d6Sderaadtfor any of the errors specified for the routine 258647427d6Sderaadt.Xr getdents 2 . 259647427d6Sderaadt.Pp 260647427d6SderaadtThe 261647427d6Sderaadt.Fn telldir 262647427d6Sderaadtfunction may also fail and set 263647427d6Sderaadt.Va errno 264647427d6Sderaadtfor any of the errors specified for the routine 265647427d6Sderaadt.Xr realloc 3 . 266647427d6Sderaadt.Pp 267647427d6SderaadtThe 268647427d6Sderaadt.Fn closedir 269647427d6Sderaadtfunction may also fail and set 270647427d6Sderaadt.Va errno 271647427d6Sderaadtfor any of the errors specified for the routine 272647427d6Sderaadt.Xr close 2 . 273647427d6Sderaadt.Sh SEE ALSO 274647427d6Sderaadt.Xr close 2 , 275647427d6Sderaadt.Xr getdents 2 , 276647427d6Sderaadt.Xr lseek 2 , 277647427d6Sderaadt.Xr open 2 , 278647427d6Sderaadt.Xr dir 5 279647427d6Sderaadt.Sh STANDARDS 280647427d6SderaadtThe 281647427d6Sderaadt.Fn opendir , 282647427d6Sderaadt.Fn fdopendir , 283647427d6Sderaadt.Fn readdir , 284647427d6Sderaadt.Fn readdir_r , 285647427d6Sderaadt.Fn telldir , 286647427d6Sderaadt.Fn seekdir , 287647427d6Sderaadt.Fn rewinddir , 288647427d6Sderaadt.Fn closedir , 289647427d6Sderaadtand 290647427d6Sderaadt.Fn dirfd 291647427d6Sderaadtfunctions conform to 292647427d6Sderaadt.St -p1003.1-2008 . 293647427d6Sderaadt.Sh HISTORY 294647427d6SderaadtThe 295647427d6Sderaadt.Fn opendir , 296647427d6Sderaadt.Fn readdir , 297647427d6Sderaadt.Fn telldir , 298647427d6Sderaadt.Fn seekdir , 299647427d6Sderaadt.Fn rewinddir , 300647427d6Sderaadt.Fn closedir , 301647427d6Sderaadtand 302647427d6Sderaadt.Fn dirfd 303647427d6Sderaadtfunctions appeared in 304647427d6Sderaadt.Bx 4.2 . 305647427d6SderaadtThe 306647427d6Sderaadt.Fn fdopendir 307647427d6Sderaadtfunction appeared in 308647427d6Sderaadt.Ox 5.0 . 309*f7d60097Sguenther.Sh CAVEATS 310*f7d60097SguentherThe 311*f7d60097Sguenther.Fn readdir_r 312*f7d60097Sguentherfunction was intended to provide a thread-safe version of 313*f7d60097Sguenther.Fn readdir . 314*f7d60097SguentherHowever, it was later found to be both unnecessary in the typical 315*f7d60097Sguentherusage and unportable due to insufficient buffer sizing guidance. 316*f7d60097SguentherIt was therefore officially deprecated in issue 8. 317