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, anderrnogets 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βdirpdoes 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
- Copyright 2002, Andries E. Brouwer <aeb@cwi.nl>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms