readdir(3)

๐ŸŽ€ NAME ๐ŸŽ€

readdir โ€” read a directory. That's the whole job, and she does it flawlessly every single time. This is readdir(3) from the Library Functions Manual, here to hand you directory entries one at a time like your bestie passing you fries. ๐ŸŸ

๐ŸŒˆ LIBRARY ๐ŸŒˆ

Standard C library (libc, -lc). Yes bestie, link the -lc and let's go.

โœจ SYNOPSIS โœจ

#include <dirent.h>

struct dirent *readdir(DIR *dirp);

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

Okay so readdir() hands you back a pointer to a dirent structure, and that struct represents the next directory entry in the directory stream that dirp points to. It returns NULL when you hit the end of the directory stream, or if an error happened. That's the vibe.

In the glibc implementation, the dirent structure is defined like this:

struct dirent {
    ino_t          d_ino;       /* Inode number */
    off_t          d_off;       /* Not an offset; see below */
    unsigned short d_reclen;    /* Length of this record */
    unsigned char  d_type;      /* Type of file; not supported
                                   by all filesystem types */
    char           d_name[256]; /* Null-terminated filename */
};

The only fields in the dirent structure that POSIX.1 actually mandates are .d_name and .d_ino. Everything else? Unstandardized, not present on all systems, lowkey doing its own thing. See VERSIONS.

The fields of the dirent structure, one by one, because we are thorough in this house ๐Ÿ’…:

.d_ino โ€” this is the inode number of the file in the filesystem containing the directory on which readdir() was called. But here's the tea: if the directory entry is a mount point, then .d_ino differs from .st_ino returned by stat(2) on this file. .d_ino is the inode number of the mount point, while .st_ino is the inode number of the root directory of the mounted filesystem. Not the same thing, don't get it twisted.

.d_off โ€” the value returned in .d_off is the same as would be returned by calling telldir(3) at the current position in the directory stream. But bestie, hear me on this: despite its type and its name, the .d_off field is seldom any kind of directory offset on modern filesystems. Applications should treat this field as an opaque value, making no assumptions about its contents. Also go read telldir(3).

.d_reclen โ€” this is the size (in bytes) of the returned record. This may not match the size of the structure definition shown above. Yeah. See VERSIONS.

.d_type โ€” this field contains a value indicating the file type, which makes it possible to avoid the expense of calling lstat(2) if your next steps depend on the type of the file. Slay.

When a suitable feature test macro is defined (_DEFAULT_SOURCE since glibc 2.19, or _BSD_SOURCE on glibc 2.19 and earlier), glibc defines the following macro constants for the value returned in .d_type:

  • DT_BLK โ€” this is a block device.
  • DT_CHR โ€” this is a character device.
  • DT_DIR โ€” this is a directory.
  • DT_FIFO โ€” this is a named pipe (FIFO).
  • DT_LNK โ€” this is a symbolic link.
  • DT_REG โ€” this is a regular file.
  • DT_SOCK โ€” this is a UNIX domain socket.
  • DT_UNKNOWN โ€” the file type could not be determined.

Currently, only some filesystems (among them: Btrfs, ext2, ext3, and ext4) have full support for returning the file type in .d_type. All applications must properly handle a return of DT_UNKNOWN. Non-negotiable, no cap.

.d_name โ€” this field contains the null terminated filename. See VERSIONS.

And the big one, the one that gets people every time: the data returned by readdir() may be overwritten by subsequent calls to readdir() for the same directory stream. So if you're holding onto that pointer, you are holding onto sand, bestie. ๐Ÿ˜ญ

๐Ÿ’… RETURN VALUE ๐Ÿ’…

On success, readdir() returns a pointer to a dirent structure. (This structure may be statically allocated; do not attempt to free(3) it. Do not. Put the free() down. ๐Ÿ™ˆ)

If the end of the directory stream is reached, NULL is returned and errno is not changed. If an error occurs, NULL is returned and errno is set to indicate the error. So how do you tell end of stream apart from an error? Set errno to zero before calling readdir(), then check the value of errno if NULL is returned. That's the whole trick. Iconic. ๐Ÿ‘‘

๐Ÿ”ฅ ERRORS ๐Ÿ”ฅ

  • EBADF โ€” Invalid directory stream descriptor dirp.

๐Ÿ‘‘ ATTRIBUTES ๐Ÿ‘‘

For an explanation of the terms used in this section, see attributes(7).

Here's the table, unfurled into a list so it fits the vibe (facts stay facts โœจ):

  • Interface: readdir()
  • Attribute: Thread safety
  • Value: MT-Unsafe race:dirstream

In the current POSIX.1 specification (POSIX.1-2008), readdir() is not required to be thread-safe. However โ€” and this is the good part โ€” in modern implementations (including the glibc implementation), concurrent calls to readdir() that specify different directory streams are thread-safe. In cases where multiple threads must read from the same directory stream, using readdir() with external synchronization is still preferable to the use of the deprecated readdir_r(3) function. And it is expected that a future version of POSIX.1 will require that readdir() be thread-safe when concurrently employed on different directory streams.

๐ŸŒธ VERSIONS ๐ŸŒธ

Only the fields .d_name and (as an XSI extension) .d_ino are specified in POSIX.1. Other than Linux, the .d_type field is available mainly only on BSD systems. The remaining fields are available on many, but not all systems. Under glibc, programs can check for the availability of the fields not defined in POSIX.1 by testing whether the macros _DIRENT_HAVE_D_NAMLEN, _DIRENT_HAVE_D_RECLEN, _DIRENT_HAVE_D_OFF, or _DIRENT_HAVE_D_TYPE are defined.

๐ŸŽ€ The .d_name field ๐ŸŽ€

The dirent structure definition shown above is taken from the glibc headers, and shows the .d_name field with a fixed size.

Warning: applications should avoid any dependence on the size of the .d_name field. POSIX defines it as char d_name[], a character array of unspecified size, with at most NAME_MAX characters preceding the terminating null byte ('\0').

POSIX.1 explicitly notes that this field should not be used as an lvalue. The standard also notes that the use of sizeof(.d_name) is incorrect; use strlen(.d_name) instead. (On some systems, this field is defined as char d_name[1]!) By implication, the use of sizeof(struct dirent) to capture the size of the record including the size of .d_name is also incorrect. Yes, really. ๐Ÿ˜ญ

Note that while the call

fpathconf(fd, _PC_NAME_MAX)

returns the value 255 for most filesystems, on some filesystems (e.g., CIFS, Windows SMB servers), the null-terminated filename that is (correctly) returned in .d_name can actually exceed this size. In such cases, the .d_reclen field will contain a value that exceeds the size of the glibc dirent structure shown above. Unhinged behavior, but it's real. ๐ŸŒˆ

โญ STANDARDS โญ

POSIX.1-2008.

๐Ÿงš HISTORY ๐Ÿงš

POSIX.1-2001, SVr4, 4.3BSD. She's been around. She has history. Respect the elders โœจ

๐Ÿ’œ NOTES ๐Ÿ’œ

A directory stream is opened using opendir(3). (You kinda need one before you can read from it, bestie. Just saying. ๐Ÿซถ)

The order in which filenames are read by successive calls to readdir() depends on the filesystem implementation; it is unlikely that the names will be sorted in any fashion. So if you were expecting alphabetical, uh, no. Not happening. Sort it yourself if you care. ๐Ÿ’…


๐Ÿ”ญ SEE ALSO ๐Ÿ”ญ

getdents(2), read(2), closedir(3), dirfd(3), ftw(3), offsetof(3), opendir(3), readdir_r(3), rewinddir(3), scandir(3), seekdir(3), telldir(3)


And that's the whole page, bestie! This was readdir(3) straight out of Linux man-pages 6.20, dated 2026-02-08. Now go read a directory. You got this. ๐Ÿ’–โœจ๐Ÿฆ‹๐ŸŽ€

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms