1.\" 2.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for 3.\" permission to reproduce portions of its copyrighted documentation. 4.\" Original documentation from The Open Group can be obtained online at 5.\" http://www.opengroup.org/bookstore/. 6.\" 7.\" The Institute of Electrical and Electronics Engineers and The Open 8.\" Group, have given us permission to reprint portions of their 9.\" documentation. 10.\" 11.\" In the following statement, the phrase ``this text'' refers to portions 12.\" of the system documentation. 13.\" 14.\" Portions of this text are reprinted and reproduced in electronic form 15.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition, 16.\" Standard for Information Technology -- Portable Operating System 17.\" Interface (POSIX), The Open Group Base Specifications Issue 6, 18.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics 19.\" Engineers, Inc and The Open Group. In the event of any discrepancy 20.\" between these versions and the original IEEE and The Open Group 21.\" Standard, the original IEEE and The Open Group Standard is the referee 22.\" document. The original Standard can be obtained online at 23.\" http://www.opengroup.org/unix/online.html. 24.\" 25.\" This notice shall appear on any product containing this material. 26.\" 27.\" The contents of this file are subject to the terms of the 28.\" Common Development and Distribution License (the "License"). 29.\" You may not use this file except in compliance with the License. 30.\" 31.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 32.\" or http://www.opensolaris.org/os/licensing. 33.\" See the License for the specific language governing permissions 34.\" and limitations under the License. 35.\" 36.\" When distributing Covered Code, include this CDDL HEADER in each 37.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE. 38.\" If applicable, add the following below this CDDL HEADER, with the 39.\" fields enclosed by brackets "[]" replaced with your own identifying 40.\" information: Portions Copyright [yyyy] [name of copyright owner] 41.\" 42.\" 43.\" Portions Copyright (c) 1992, X/Open Company Limited. All Rights Reserved. 44.\" Copyright (c) 2007, Sun Microsystems, Inc. All Rights Reserved. 45.\" Copyright 2021 Oxide Computer Company 46.\" 47.Dd February 25, 2021 48.Dt READDIR 3C 49.Os 50.Sh NAME 51.Nm readdir , 52.Nm readdir_r 53.Nd read directory 54.Sh SYNOPSIS 55.In sys/types.h 56.In dirent.h 57.Ft "struct dirent *" 58.Fo readdir 59.Fa "DIR *dirp" 60.Fc 61.Ft "struct dirent *" 62.Fo readdir_r 63.Fa "DIR *dirp" 64.Fa "struct dirent *entry" 65.Fc 66.Ss Standard Conforming 67.Fd #define _POSIX_PTHREAD_SEMANTICS 68.Ft int 69.Fo readdir_r 70.Fa "DIR *restrict dirp" 71.Fa "struct dirent *restrict entry" 72.Fa "struct dirent **restrict result" 73.Fc 74.Sh DESCRIPTION 75The type 76.Ft DIR , 77which is defined in the header 78.In dirent.h , 79represents a 80.Em directory stream , 81which is an ordered sequence of all the directory entries in a particular 82directory. 83Directory entries represent files. 84Files can be removed from a directory or added to a directory asynchronously 85to the operation of 86.Fn readdir 87and 88.Fn readdir_r . 89.Ss Fn readdir 90The 91.Fn readdir 92function returns a pointer to a structure representing the directory entry at 93the current position in the directory stream specified by the argument 94.Fa dirp , 95and positions the directory stream at the next entry. 96It returns a null pointer upon reaching the end of the directory stream. 97The structure 98.Ft dirent 99defined by the 100.In dirent.h 101header describes a directory entry. 102.Pp 103The 104.Fn readdir 105function will not return directory entries containing empty names. 106If entries for 107.No \&. 108.Pq dot 109.No \&.. 110.Pq dot-dot 111exist, one entry will be returned for dot and one entry will be returned for 112dot-dot; otherwise they will not be returned. 113.Pp 114The pointer returned by 115.Fn readdir 116points to data that can be overwritten by another call to 117.Fn readdir 118on the same directory stream. 119It will not be overwritten by another call to 120.Fn readdir 121on a different directory stream. 122The returned pointer will remain valid until the directory stream is 123freed with a call to 124.Xr closedir 3C . 125It is safe to use 126.Fn readdir 127in a threaded application, so long as only one thread reads from the directory 128stream at any given time. 129.Pp 130If a file is removed from or added to the directory after the most recent call 131to 132.Xr opendir 3C 133or 134.Xr rewinddir 3C , 135whether a subsequent call to 136.Fn readdir 137returns an entry for that file is unspecified. 138.Pp 139The 140.Fn readdir 141function can buffer several directory entries per actual read operation. 142It marks for update the 143.Ft st_atime 144field of the directory each time the directory is actually read. 145.Pp 146After a call to 147.Xr fork 2 , 148either the parent or child 149.Pq but not both 150can continue processing the directory stream using 151.Fn readdir , 152.Xr rewinddir 3C , 153or 154.Xr seekdir 3C . 155If both the parent and child processes use these functions, the result is 156undefined. 157.Pp 158If the entry names a symbolic link, the value of the 159.Ft d_ino 160member is unspecified. 161.Ss Fn readdir_r 162Unless the end of the directory stream has been reached or an error occurred, 163the 164.Fn readdir_r 165function initializes the 166.Ft dirent 167structure referenced by 168.Fa entry 169to represent the directory entry at the current position in the directory 170stream referred to by 171.Fa dirp , 172and positions the directory stream at the next entry. 173.Pp 174The caller must allocate storage pointed to by 175.Fa entry 176to be large enough for a 177.Ft dirent 178structure with an array of 179.Ft char 180.Fa d_name 181member containing at least 182.Dv NAME_MAX 183.Po 184that is, 185.Fo pathconf 186.Fa directory , 187.Dv _PC_NAME_MAX 188.Fc 189plus one element. 190.Po 191.Dv _PC_NAME_MAX 192is defined in 193.In unistd.h 194.Pc 195.Pc 196.Pp 197While the 198.Fn readdir_r 199function was originally added as a re-entrant version of 200.Fn readdir , 201it is not recommended that 202.Fn readdir_r 203be used in new applications and existing software should instead use 204.Fn readdir . 205The 206.Fn readdir_r 207function has been deprecated in many systems. 208Historically, the data returned from 209.Fn readdir 210was not specific to the directory stream making it unsafe in a multi-threaded 211environment; however, that is no longer the case. 212.Pp 213The 214.Fn readdir_r 215function will not return directory entries containing empty names. 216It is unspecified whether entries are returned for 217.No \&. 218.Pq dot 219or 220.No \&.. 221.Pq dot-dot . 222.Pp 223If a file is removed from or added to the directory after the most recent call 224to 225.Xr opendir 3C or 226.Xr rewinddir 3C , whether a subsequent call to 227.Fn readdir_r 228returns an entry for that file is unspecified. 229.Pp 230The 231.Fn readdir_r 232function can buffer several directory entries per actual read operation. 233It marks for update the 234.Ft st_atime 235field of the directory each time the directory is actually read. 236.Pp 237The standard-conforming version 238.Po see 239.Xr standards 7 240.Pc 241of the 242.Fn readdir_r 243function performs all of the actions described above for 244.Fn readdir_r 245and sets the pointer pointed to by 246.Fa result . 247If a directory entry is returned, the pointer will be set to the same value 248as the 249.Fa entry 250argument; otherwise, it will be set to 251.Dv NULL . 252.Sh RETURN VALUES 253Upon successful completion, 254.Fn readdir 255and the default 256.Fn readdir_r 257return a pointer to an object of type 258.Ft struct dirent . 259When an error is encountered, a null pointer is returned and 260.Va errno 261is set to indicate the error. 262When the end of the directory is encountered, a null pointer is returned and 263.Va errno 264is not changed. 265.Pp 266The standard-conforming 267.Fn readdir_r 268returns 269.Sy 0 270if the end of the directory is encountered or a directory entry is stored in 271the structure referenced by 272.Fa entry . 273Otherwise, an error number is returned to indicate the failure. 274.Sh EXAMPLES 275.Sy Example 1 276Search the current directory for the entry 277.Fa name . 278.Pp 279The following sample program will search the current directory for each of the 280arguments supplied on the command line: 281.Bd -literal 282#include <sys/types.h> 283#include <dirent.h> 284#include <errno.h> 285#include <stdio.h> 286#include <strings.h> 287 288static void 289lookup(const char *arg) 290{ 291 DIR *dirp; 292 struct dirent *dp; 293 294 if ((dirp = opendir(".")) == NULL) { 295 perror("couldn't open '.'"); 296 return; 297 } 298 299 do { 300 errno = 0; 301 if ((dp = readdir(dirp)) != NULL) { 302 if (strcmp(dp->d_name, arg) != 0) 303 continue; 304 305 (void) printf("found %s\en", arg); 306 (void) closedir(dirp); 307 return; 308 } 309 } while (dp != NULL); 310 311 if (errno != 0) 312 perror("error reading directory"); 313 else 314 (void) printf("failed to find %s\en", arg); 315 (void) closedir(dirp); 316} 317 318int 319main(int argc, char *argv[]) 320{ 321 int i; 322 for (i = 1; i < argc; i++) 323 lookup(argv[i]); 324 return (0); 325} 326.Ed 327.Sh ERRORS 328The 329.Fn readdir 330and 331.Fn readdir_r 332functions will fail if: 333.Bl -tag -width Er 334.It Er EOVERFLOW 335One of the values in the structure to be returned cannot be represented 336correctly. 337.El 338.Pp 339The 340.Fn readdir 341and 342.Fn readdir_r 343functions may fail if: 344.Bl -tag -width Er 345.It Er EBADF 346The 347.Fa dirp 348argument does not refer to an open directory stream. 349.It Er ENOENT 350The current position of the directory stream is invalid. 351.El 352.Sh USAGE 353The 354.Fn readdir 355and 356.Fn readdir_r 357functions should be used in conjunction with 358.Xr opendir 3C , 359.Xr closedir 3C , 360and 361.Xr rewinddir 3C 362to examine the contents of the directory. 363Since 364.Fn readdir 365and the default 366.Fn readdir_r 367return a null pointer both at the end of the directory and on error, an 368application wanting to check for error situations should set 369.Va errno 370to 0 before calling either of these functions. 371If 372.Va errno 373is set to non-zero on return, an error occurred. 374.Pp 375The standard-conforming 376.Fn readdir_r 377returns the error number if an error occurred. 378It returns 0 on success 379.Pq including reaching the end of the directory stream . 380.Pp 381The 382.Fn readdir 383and 384.Fn readdir_r 385functions have transitional interfaces for 64-bit file offsets. 386See 387.Xr lf64 7 . 388.Sh INTERFACE STABILITY 389.Sy Committed 390.Sh MT-LEVEL 391The 392.Fn readdir 393function is 394.Sy Unsafe ; 395however, it is 396.Sy Safe 397if different directory streams are used concurrently. 398The 399.Fn readdir_r 400function is 401.Sy Safe . 402.Sh SEE ALSO 403.Xr fork 2 , 404.Xr lstat 2 , 405.Xr symlink 2 , 406.Xr Intro 3 , 407.Xr closedir 3C , 408.Xr opendir 3C , 409.Xr rewinddir 3C , 410.Xr scandir 3C , 411.Xr seekdir 3C , 412.Xr attributes 7 , 413.Xr lf64 7 , 414.Xr standards 7 415.Sh NOTES 416When compiling multithreaded programs, see the 417.Sy MULTITHREADED APPLICATIONS 418section of 419.Xr Intro 3 . 420.Pp 421Solaris 2.4 and earlier releases provided a 422.Fn readdir_r 423interface as specified in POSIX.1c Draft 6. 424The final POSIX.1c standard changed the interface as described above. 425Support for the Draft 6 interface is provided for compatibility only. 426New applications and libraries should use the standard-conforming interface, 427though preferably 428.Fn readdir . 429.Pp 430For POSIX.1c-conforming applications, the 431.Dv _POSIX_PTHREAD_SEMANTICS 432and 433.Dv _REENTRANT 434flags are automatically turned on by defining the 435.Dv _POSIX_C_SOURCE 436flag with a value >= 199506L. 437