NAME

fdopendir, opendir — open directory associated with file descriptor

SYNOPSIS

#include <dirent.h>

DIR *fdopendir(int
fd);
DIR *opendir(const char *
dirname);

DESCRIPTION

The fdopendir() function shall be equivalent to the opendir() function except that the directory is specified by a file descriptor rather than by a name. The file offset associated with the file descriptor at the time of the call determines which entries are returned.

Upon successful return from fdopendir(), the file descriptor is under the control of the system, and if any attempt is made to close the file descriptor, or to modify the state of the associated description, other than by means of closedir(), readdir(), readdir_r(), rewinddir(), or [XSI] [Option Start] seekdir(), [Option End] the behavior is undefined. Upon calling closedir() the file descriptor shall be closed.

It is unspecified whether the FD_CLOEXEC flag will be set on the file descriptor by a successful call to fdopendir() if it was not previously set. However, the flag shall not be cleared if it was previously set.

The opendir() function shall open a directory stream corresponding to the directory named by the dirname argument. The directory stream shall be positioned at the first entry. If opendir() opens a file descriptor for dirname to associate with the returned stream:

RETURN VALUE

Upon successful completion, these functions shall return a pointer to an object of type DIR. Otherwise, these functions shall return a null pointer and set errno to indicate the error.

ERRORS

The fdopendir() function shall fail if:

[EBADF]
The fd argument is not a valid file descriptor open for reading.
[ENOTDIR]
The descriptor fd is not associated with a directory.

The opendir() function shall fail if:

[EACCES]
Search permission is denied for the component of the path prefix of dirname or read permission is denied for dirname.
[ELOOP]
A loop exists in symbolic links encountered during resolution of the dirname argument.
[ENAMETOOLONG]
The length of a component of a pathname is longer than {NAME_MAX}.
[ENOENT]
A component of dirname does not name an existing directory or dirname is an empty string.
[ENOTDIR]
A component of dirname names an existing file that is neither a directory nor a symbolic link to a directory.

The opendir() function may fail if:

[ELOOP]
More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the dirname argument.
[EMFILE]
All file descriptors available to the process are currently open.
[ENAMETOOLONG]
The length of a pathname exceeds {PATH_MAX}, or pathname resolution of a symbolic link produced an intermediate result with a length that exceeds {PATH_MAX}.
[ENFILE]
Too many files are currently open in the system.

The following sections are informative.

EXAMPLES

Open a Directory Stream

The following program fragment demonstrates how the opendir() function is used.

#include <dirent.h>
...
    DIR *dir;
    struct dirent *dp;
...
    if ((dir = opendir (".")) == NULL) {
        perror ("Cannot open .");
        exit (1);
    }

while ((dp = readdir (dir)) != NULL) { ...
Find And Open a File

The following program searches through a given directory looking for files whose name does not begin with a dot and whose size is larger than 1 MiB.

#include <stdio.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[]) { struct stat statbuf; DIR *d; struct dirent *dp; int dfd, ffd;
if ((d = fdopendir((dfd = open("./tmp", O_RDONLY)))) == NULL) { fprintf(stderr, "Cannot open ./tmp directory\n"); exit(1); } while ((dp = readdir(d)) != NULL) { if (dp->d_name[0] == '.') continue; /* there is a possible race condition here as the file * could be renamed between the readdir and the open */ if ((ffd = openat(dfd, dp->d_name, O_RDONLY)) == -1) { perror(dp->d_name); continue; } if (fstat(ffd, &statbuf) == 0 && statbuf.st_size > (1024*1024)) { /* found it ... */ printf("%s: %jdK\n", dp->d_name, (intmax_t)(statbuf.st_size / 1024)); } close(ffd); } closedir(d); // note this implicitly closes dfd return 0; }

APPLICATION USAGE

The opendir() function should be used in conjunction with readdir(), closedir(), and rewinddir() to examine the contents of the directory (see the EXAMPLES section in readdir ). This method is recommended for portability.

RATIONALE

The purpose of the fdopendir() function is to enable opening files in directories other than the current working directory without exposure to race conditions. Any part of the path of a file could be changed in parallel to a call to opendir(), resulting in unspecified behavior.

Based on historical implementations, the rules about file descriptors apply to directory streams as well. However, this volume of POSIX.1-2024 does not mandate that opendir() opens a file descriptor to associate with the stream; this may instead be done by the first call to dirfd(), thus avoiding the need to allocate a file descriptor if dirfd() is never called. Once a file descriptor has been associated with the stream, it is mandatory that closedir() deallocate the file descriptor. If opendir() opens a file descriptor to associate with the stream, it behaves as if the O_CLOEXEC flag for open() had been used, so that the FD_CLOEXEC flag is set for the file descriptor. If fdopendir() is used to create a directory stream, it is unspecified whether the FD_CLOEXEC flag on the file descriptor specified by the fd argument is set or left unchanged.

The directory entries for dot and dot-dot are optional. This volume of POSIX.1-2024 does not provide a way to test a priori for their existence because an application that is portable must be written to look for (and usually ignore) those entries. Writing code that presumes that they are the first two entries does not always work, as many implementations permit them to be other than the first two entries, with a "normal" entry preceding them. There is negligible value in providing a way to determine what the implementation does because the code to deal with dot and dot-dot must be written in any case and because such a flag would add to the list of those flags (which has proven in itself to be objectionable) and might be abused.

Since the structure and buffer allocation, if any, for directory operations are defined by the implementation, this volume of POSIX.1-2024 imposes no portability requirements for erroneous program constructs, erroneous data, or the use of unspecified values such as the use or referencing of a dirp value or a dirent structure value after a directory stream has been closed or after a fork() or one of the exec function calls.

FUTURE DIRECTIONS

None.

SEE ALSO

closedir , dirfd , fstatat , open , posix_getdents , readdir , rewinddir , symlink

XBD <dirent.h> , <fcntl.h> , <sys/types.h>

CHANGE HISTORY

First released in Issue 2.

Issue 6

In the SYNOPSIS, the optional include of the <sys/types.h> header is removed.

The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:

The following changes were made to align with the IEEE P1003.1a draft standard:

Issue 7

Austin Group Interpretation 1003.1-2001 #143 is applied.

SD5-XBD-ERN-4 is applied, changing the definition of the [EMFILE] error.

The fdopendir() function is added from The Open Group Technical Standard, 2006, Extended API Set Part 2.

An additional example is added.

POSIX.1-2008, Technical Corrigendum 1, XSH/TC1-2008/0122 [422] and XSH/TC1-2008/0123 [324] are applied.

Issue 8

Austin Group Defect 368 is applied, adding a requirement for FD_CLOEXEC to be set by opendir() if it associates a file descriptor with the returned stream.

Austin Group Defect 411 is applied, clarifying that FD_CLOEXEC is not cleared by fdopendir() if it was previously set.

Austin Group Defect 697 is applied, adding posix_getdents() to the SEE ALSO section.

Austin Group Defect 1360 is applied, clarifying that type DIR always has the ability to store a file descriptor; what is optional is whether one is opened by opendir().

End of informative text.