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 โ€” fd is 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 โ€” name was too long.
  • ENFILE โ€” the system-wide limit on the total number of open files has been reached.
  • ENOENT โ€” directory does not exist, or name is an empty string.
  • ENOMEM โ€” insufficient memory to complete the operation.
  • ENOTDIR โ€” name is 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 the DIR *. meanwhile fdopendir() 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 to fdopendir() will set the close-on-exec flag for the file descriptor fd โ€” 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms