opendir(3)
โจ opendir(3) โจ
okay bestie, pull up a chair, because today we're opening directories ๐ this is a page from the Library Functions Manual and it's giving serious "let me into your filesystem" energy ๐
๐ธ NAME ๐ธ
opendir, fdopendir โ open a directory
๐ LIBRARY ๐
Standard C library (libc, -lc)
๐ SYNOPSIS ๐
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
DIR *fdopendir(int fd);
๐ฆ Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
fdopendir():
Since glibc 2.10:
_POSIX_C_SOURCE >= 200809L
Before glibc 2.10:
_GNU_SOURCE
๐ DESCRIPTION ๐
so opendir() opens a directory stream that corresponds to the directory called name, and it hands you back a pointer to that directory stream. the stream is positioned at the very first entry in the directory. first in line, main character energy, no cutting ๐
fdopendir() is literally opendir()'s sibling, except it returns a directory stream for the directory that the open file descriptor fd is referring to. watch out though bestie ๐ซถ after a successful call to fdopendir(), fd gets used internally by the implementation, and your application should not be using it for anything else. it's taken. it's booked. it's in a relationship with the library now ๐ญ
๐ฅ RETURN VALUE ๐ฅ
both opendir() and fdopendir() return a pointer to the directory stream. on error they return NULL, and errno is set to tell you what went wrong. not a slay, but at least it tells you why ๐
๐ ERRORS ๐
EACCESโ permission denied. the door said no ๐EBADFโfdis not a valid file descriptor opened for reading.EMFILEโ the per-process limit on the number of open file descriptors has been reached. you opened too many doors, bestie ๐ชENAMETOOLONGโnamewas too long.ENFILEโ the system-wide limit on the total number of open files has been reached.ENOENTโ directory does not exist, ornameis an empty string.ENOMEMโ insufficient memory to complete the operation.ENOTDIRโnameis not a directory. it's giving "wrong door" ๐ฅฒ
โญ ATTRIBUTES โญ
for an explanation of the terms used in this section, see attributes(7). (no tables allowed in my group chat era so here it is as a cute lil list โจ)
- Interface:
opendir(),fdopendir() - Attribute: Thread safety
- Value:
MT-Safe๐ซถ
๐ง STANDARDS ๐ง
POSIX.1-2008.
๐ธ STANDARDS ๐ธ
(yes bestie there are two STANDARDS sections in this manpage, and I am not skipping a single one โ we keep every crumb ๐)
opendir()โ SVr4, 4.3BSD, POSIX.1-2001.fdopendir()โ POSIX.1-2008. glibc 2.4.
๐ NOTES ๐
- filename entries can be read from a directory stream using
readdir(3). - the underlying file descriptor of the directory stream can be obtained using
dirfd(3). opendir()sets the close-on-exec flag for the file descriptor underlying theDIR *. meanwhilefdopendir()leaves the setting of the close-on-exec flag unchanged for the file descriptor,fd. and POSIX.1-200x leaves it unspecified whether a successful call tofdopendir()will set the close-on-exec flag for the file descriptorfdโ so like, nobody knows, it's a mystery, it's the vibe ๐ฎ
๐ฅ SEE ALSO ๐ฅ
open(2), closedir(3), dirfd(3), readdir(3), rewinddir(3), scandir(3), seekdir(3), telldir(3)
and that's the whole thing bestie, you ate it all up ๐ this lil page came from Linux man-pages 6.20, dated 2026-02-08 ๐ go forth and open some directories, ily, periodt ๐โจ๐
๐ฆ 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 1993, David Metcalfe <david@prism.demon.co.uk>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms