.TH opendir() "" "" "General Function (libc)"
.PC "Open a directory stream"
.B "#include <sys/types.h>"
.B "#include <dirent.h>"
\fBDIR *opendir (\fIdirname\^\fB)\fR
\fBchar *\fIdirname\^\fB;\fR
.PP
The \*(CO function
.B opendir()
is one of a set of \*(CO routines that manipulate directories in a
device-independent manner.
It opens a directory stream and connects the directory
.I dirname
with it.
.PP
.B opendir()
returns a pointer to the directory stream it has created.
It returns NULL if it cannot access
.IR dirname ,
if
.I dirname
is not a directory,
or if it cannot create the directory stream
(perhaps due to insufficient memory).
.PP
If an error occurs,
.B opendir()
exits and sets
.B errno
to an appropriate value.
.SH Example
The following example
searches the current working directory for entry \fBFOO\fR:
.DM
#include <stddef.h>
#include <sys/types.h>
#include <dirent.h>
.DE
.DM
	main()
	{
		DIR *dirp
		struct dirent *dp;
.DE
.DM
		dirp = opendir( "." );
.DE
.DM
		while ((dp = readdir( dirp )) != NULL ) {
			if ( strcmp( dp->d_name, "FOO" ) == 0 ) {
				printf("Found FOO\en");
				(void) closedir(dirp);
				return FOUND;
			}
		}
.DE
.DM
		(void) closedir( dirp );
		printf("FOO not found\en");
		return NOT_FOUND;
	}
.DE
.SH "See Also"
.Xr "closedir()," closedir
.Xr "dirent.h," dirent.h
.Xr "getdents()," getdents
.Xr "libc," libc
.Xr "readdir()," readdir
.Xr "rewinddir()," rewinddir
.Xr "seekdir()," seekdir
.Xr "telldir()" telldir
.br
\*(PX Standard, \(sc5.1.2
.SH Notes
The
.B dirent
routines buffer directories; and because directory entries
can appear and disappear as other users manipulate the directory,
your application should continually rescan
a directory to keep an accurate picture of its active entries.
.PP
.II "Gwynn, D."
The \*(CO implementation of the \fBdirent\fR routines
was written by D. Gwynn.
