NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |ATTRIBUTES |STANDARDS |HISTORY |SEE ALSO |COLOPHON | |
readdir_r(3) Library Functions Manualreaddir_r(3)readdir_r - read a directory
Standard C library (libc,-lc)
#include <dirent.h>[[deprecated]] int readdir_r(DIR *restrictdirp,struct dirent *restrictentry,struct dirent **restrictresult); Feature Test Macro Requirements for glibc (seefeature_test_macros(7)):readdir_r(): _POSIX_C_SOURCE || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
This function is deprecated; usereaddir(3) instead. Thereaddir_r() function was invented as a reentrant version ofreaddir(3). It reads the next directory entry from the directory streamdirp, and returns it in the caller-allocated buffer pointed to byentry. For details of thedirent structure, seereaddir(3). A pointer to the returned buffer is placed in*result; if the end of the directory stream was encountered, then NULL is instead returned in*result. It is recommended that applications usereaddir(3) instead ofreaddir_r(). Furthermore, since glibc 2.24, glibc deprecatesreaddir_r(). The reasons are as follows: • On systems whereNAME_MAXis undefined, callingreaddir_r() may be unsafe because the interface does not allow the caller to specify the length of the buffer used for the returned directory entry. • On some systems,readdir_r() can't read directory entries with very long names. When the glibc implementation encounters such a name,readdir_r() fails with the errorENAMETOOLONGafter thefinal directory entry has been read. On some other systems,readdir_r() may return a success status, but the returnedd_name field may not be null terminated or may be truncated. • In the current POSIX.1 specification (POSIX.1-2008),readdir(3) is not required to be thread-safe. However, in modern implementations (including the glibc implementation), concurrent calls toreaddir(3) that specify different directory streams are thread-safe. Therefore, the use ofreaddir_r() is generally unnecessary in multithreaded programs. In cases where multiple threads must read from the same directory stream, usingreaddir(3) with external synchronization is still preferable to the use ofreaddir_r(), for the reasons given in the points above. • It is expected that a future version of POSIX.1 will makereaddir_r() obsolete, and require thatreaddir(3) be thread- safe when concurrently employed on different directory streams.
Thereaddir_r() function returns 0 on success. On error, it returns a positive error number (listed under ERRORS). If the end of the directory stream is reached,readdir_r() returns 0, and returns NULL in*result.
EBADFInvalid directory stream descriptordirp.ENAMETOOLONG A directory entry whose name was too long to be read was encountered.
For an explanation of the terms used in this section, seeattributes(7). ┌──────────────────────────────────────┬───────────────┬─────────┐ │Interface│Attribute│Value│ ├──────────────────────────────────────┼───────────────┼─────────┤ │readdir_r() │ Thread safety │ MT-Safe │ └──────────────────────────────────────┴───────────────┴─────────┘
POSIX.1-2008.
POSIX.1-2001.
readdir(3)
This page is part of theman-pages (Linux kernel and C library user-space interface documentation) project. Information about the project can be found at ⟨https://www.kernel.org/doc/man-pages/⟩. If you have a bug report for this manual page, see ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩. This page was obtained from the tarball man-pages-6.15.tar.gz fetched from ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on 2025-08-11. If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up- to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgLinux man-pages 6.15 2025-05-17readdir_r(3)Pages that refer to this page:readdir(3)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |