OPENDIR(3C) Standard C Library Functions OPENDIR(3C)

opendir, fdopendiropen directory stream

#include <sys/types.h>
#include <dirent.h>

DIR *
opendir(dirname);

DIR *
fdopendir(int filedes);

The () and fdopendir() functions are used to create seekable directory streams that can be used to iterate over the contents of a directory, most commonly with readdir(3C). One can traverse and seek the stream with functions such as seekdir(3C), telldir(3C), and rewinddir(3C).

The () function creates a directory stream from the path named by dirname. The fdopendir() function creates a directory stream from an already opened file descriptor, filedes, that refers to a directory. After successfully calling fdopendir(), filedes belongs to the system and the application must not modify or close it in any way.

The new directory stream is positioned at the first entry. When finished with the directory stream, the caller is responsible for releasing its resources by calling the closedir(3C) function. This will close the directory stream's underlying file descriptor, including filedes if () was used to create it. In addition, memory associated with the directory stream, such as the struct dirent returned from readdir(3C) will be invalid once a call to closedir(3C) is completed.

All directory streams are closed upon a successful call to any of the exec(2) family of functions. The underlying file descriptors behave as though the FD_CLOEXEC flag was set upon them.

Directory streams created by the () function require an underlying file descriptor. As a result, applications are only able to open up to a total of {OPEN_MAX} files and directories.

Upon successful completion, the opendir() and fdopendir() functions return a pointer to an object of type Ft DIR . Otherwise, a null pointer is returned and errno is set to indicate the error.

The opendir() function will fail if:

Search permission is denied for any component of the path prefix of dirname or read permission is denied for Idirname.
Too many symbolic links were encountered in resolving path.
The length of the dirname argument exceeds {PATH_MAX}, or a path name component is longer than {NAME_MAX} while {_POSIX_NO_TRUNC} is in effect.
A component of dirname does not name an existing directory or dirname is an empty string.
A component of dirname is not a directory.

The fdopendir() function will fail if:

The file descriptor filedes does not reference a directory.

The opendir() function may fail if:

There are already {OPEN_MAX} file descriptors currently open in the calling process.
Pathname resolution of a symbolic link produced an intermediate result whose length exceeds PATH_MAX.
Too many files are currently open on the system.

lstat(2), symlink(2), closedir(3C), readdir(3C), rewinddir(3C), seekdir(3C), telldir(3C), attributes(7)

February 25, 2021 OmniOS