dirfd
dirfd is a POSIX function that returns the integer file descriptor associated with a directory stream. It is declared in dirent.h and is used with a DIR* object obtained from opendir or fdopendir. The function provides access to the underlying directory file descriptor without altering the current working directory, enabling subsequent relative-file operations such as openat, fstatat, or other fd-based calls.
The typical usage is to obtain a DIR* for a directory and then query its descriptor with
If the call succeeds, dirfd returns the directory’s file descriptor. If an error occurs, the function returns
Portability and notes: dirfd is defined by POSIX and is widely supported on modern Unix-like systems, including
See also: opendir, readdir, fdopendir, fstatat, openat, dirent.