This section describes how to open a directory stream. All the symbols are declared in the header file dirent.h.
You shouldn't ever allocate objects of the
struct dirent or
DIR data types, since the directory access functions do that for
you. Instead, you refer to these objects using the pointers returned by
the following functions.
opendirfunction opens and returns a directory stream for reading the directory whose file name is dirname. The stream has type
opendirreturns a null pointer. In addition to the usual file name errors (see File Name Errors), the following
errnoerror conditions are defined for this function:
- Read permission is denied for the directory named by
- The process has too many files open.
- The entire system, or perhaps the file system which contains the directory, cannot support any additional open files at the moment. (This problem cannot happen on the GNU system.)
DIRtype is typically implemented using a file descriptor, and the
opendirfunction in terms of the
openfunction. See Low-Level I/O. Directory streams and the underlying file descriptors are closed on
exec(see Executing a File).
In some situations it can be desirable to get hold of the file
descriptor which is created by the
opendir call. For instance,
to switch the current working directory to the directory just read the
fchdir function could be used. Historically the
was exposed and programs could access the fields. This does not happen
in the GNU C library. Instead a separate function is provided to allow