dirfd(3)

✨ dirfd(3) β€” Library Functions Manual ✨

okay bestie, gather round, because today we're talking about dirfd from the library functions club πŸ’… this one's a little cutie that scoops the file descriptor out of a directory stream. yes, that file descriptor. let's go πŸ¦‹


🌸 NAME 🌸

dirfd β€” get directory stream file descriptor


πŸ’– LIBRARY πŸ’–

Standard C library (libc, -lc) β€” the classic, the icon, the one that's already linked in.


✨ SYNOPSIS ✨

#include <sys/types.h>
#include <dirent.h>

int dirfd(DIR *dirp);

πŸŽ€ Feature Test Macro Requirements for glibc πŸŽ€

(psst, go check feature_test_macros(7) if you're lost πŸ’œ)

dirfd():
    /* Since glibc 2.10: */ _POSIX_C_SOURCE >= 200809L
        || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE

so it's either the POSIX one on newer glibc, or _BSD_SOURCE / _SVID_SOURCE if you're on glibc 2.19 or older. vintage behavior, honestly iconic.


🫢 DESCRIPTION 🫢

dirfd() hands you back the file descriptor that's chilling inside the directory stream dirp. that's it, that's the whole job, and it ate πŸ”₯

here's the tea though: this file descriptor is the one used internally by the directory stream. so it's only useful for functions that don't depend on, or mess with, the file position β€” think fstat(2) and fchdir(2). and the moment you call closedir(3), it gets closed automatically. no take-backs, no manual cleanup, it just goes 🌈

not me reminding you again: do not touch the file position with this fd. it's not yours, it's the directory stream's. respect the boundary bestie 😭

πŸ’œ RETURN VALUE πŸ’œ

  • slay β€” on success, dirfd() returns a file descriptor, which is a nonnegative integer ✨
  • not slay β€” on error it returns -1, and errno gets set to tell you what went wrong πŸ’€

πŸ‘‘ ERRORS πŸ‘‘

POSIX.1-2008 specifies two errors here... and plot twist: neither of them is returned by the current implementation. so these are basically the lore, not the reality πŸ™ˆ

  • EINVAL β€” dirp does not refer to a valid directory stream.
  • ENOTSUP β€” the implementation does not support the association of a file descriptor with a directory.

🌈 ATTRIBUTES 🌈

for the whole explanation of what these terms even mean, go peek at attributes(7). here's the rundown:

  • Interface: dirfd()
  • Attribute: Thread safety
  • Value: MT-Safe

so yeah, MT-Safe, meaning she's thread-safe and thriving 🧚


⭐ STANDARDS ⭐

POSIX.1-2008.


πŸ¦‹ HISTORY πŸ¦‹

4.3BSD-Reno β€” and not in 4.2BSD. a glow-up era, we love to see it πŸ’–


πŸ”₯ SEE ALSO πŸ”₯

open(2), openat(2), closedir(3), opendir(3), readdir(3), rewinddir(3), scandir(3), seekdir(3), telldir(3)


and that's the whole page, no cap 🫢 this was brought to you by Linux man-pages 6.20, dated 2026-02-08, serving dirfd(3) realness. go call her, she's waiting πŸ’…βœ¨

πŸ“¦ 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