fdopendir()

Updated: April 19, 2023

Open the directory associated with a file descriptor

Synopsis:

#include <dirent.h>

DIR * fdopendir( int fd );

Arguments:

fd
The file descriptor associated with the directory (DIR stream) to open.
Note: You must use a file descriptor obtained from an open() call with the O_DIRECTORY flag set. Or, you can set this argument to AT_FDCWD to open the current working directory. Otherwise, the function fails and sets errno to ENOTDIR.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The fdopendir() function is equivalent to the opendir() function except that the directory is specified by a file descriptor instead of a name. The file offset associated with the file descriptor (at the time fdopendir() is called) determines the entries to return.

After fdopendir() successfully returns, the file descriptor is controlled by the system. If an attempt is made to close the file descriptor or modify the state of the associated description, other than by calling the functions closedir(), readdir(), readdir_r(), rewinddir(), or seekdir(), the behavior is undefined.

Call closedir() to close the DIR stream; this also closes the file descriptor.

Returns:

A pointer to the DIR stream, or NULL if an error occurred (errno is set).

Errors:

EBADF
The fd argument isn't a file descriptor valid for reading.
ENOTDIR
The fd argument isn't AT_FDCWD and the file descriptor was obtained from an open() call made without the O_DIRECTORY flag set.

Classification:

POSIX 1003.1

Safety:  
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes