Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

opendir, fdopendir - open directory

&pagelevel(4)&pagelevel

Syntax

#include <dirent.h>

Optional
#include <sys/types.h> (End)

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

Description

opendir() opens a directory stream corresponding to the directory named by dirname. The directory stream is positioned at the first entry. The type DIR, which is defined in dirent.h, represents a directory stream that is an ordered sequence of all directory entries in a special directory. Since the type DIR is implemented in POSIX using a file descriptor, applications can only open a maximum of {OPEN_MAX} files and directories.

If dirname cannot be accessed or is not a directory, or if not enough memory to hold a DIR structure or a buffer for the directory entries can be allocated with malloc(), a null pointer is returned.

The fdopendir() function is equivalent to the opendir() function, with the difference that the directory is specified by the file descriptor fd instead of by a pathname.

After a successful return from fdopendir(), the file descriptor is under system control. If an attempt is made to close the file descriptor or to change the status of the directory by functions other than closedir(), readdir(), rewinddir() or seekdir(), the behavior is undefined. closedir() also closes the file descriptor.

Return val.

Pointer to a DIR object


if successful.

Null pointer

if an error occurs. errno is set to indicate the error.

Errors

opendir() and fdopendir() will fail if:

 

EACCES

Search permission is denied for the component of dirname or read permission is denied for dirname.

 

Extension

 

EFAULT

dirname points outside the allocated address space of the process.

 

ELOOP

Too many symbolic links were encountered in resolving dirname. (End)

 

EMFILE

More than {OPEN_MAX} file descriptors are currently open in the calling process.

 

ENAMETOOLONG 

 

The length of the dirname argument exceeds {PATH_MAX} or a pathname
component is longer than {NAME_MAX}.


ENFILE

Too many file descriptors are currently open in the system.


ENOENT

dirname points to the name of a file that does not exist or to an empty string.


ENOTDIR

A component of dirname is not a directory.


In addition, fdopendir() fails if the following applies:

 

EBADF

The fd parameter contains no valid file descriptor which is opened for reading.

 

ENOTDIR

The file descriptor fd is not connected with a directory.

Notes

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

opendir() is executed only for POSIX files

See also

closedir(), readdir(), rewinddir(), dirent.h, sys/types.h, limits.h.