COHERENT manpages

This page displays the COHERENT manpage for opendir() [Open a directory stream].

List of available manpages
Index


opendir() -- General Function (libc)

Open a directory stream
#include <sys/types.h>
#include <dirent.h>
DIR *opendir (dirname)
char *dirname;

The COHERENT function  opendir() is one of a set  of COHERENT routines that
manipulate  directories  in   a  device-independent  manner.   It  opens  a
directory stream and connects the directory dirname with it.

opendir() returns  a pointer  to the directory  stream it has  created.  It
returns NULL if it cannot access dirname, if dirname is not a directory, or
if  it cannot  create  the directory  stream (perhaps  due to  insufficient
memory).

If an error occurs, opendir() exits and sets errno to an appropriate value.

Example

The following example searches the current working directory for entry FOO:

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

    main()
    {
        DIR *dirp
        struct dirent *dp;

        dirp = opendir( "." );

        while ((dp = readdir( dirp )) != NULL ) {
            if ( strcmp( dp->d_name, "FOO" ) == 0 ) {
                printf("Found FOO\n");
                (void) closedir(dirp);
                return FOUND;
            }
        }

        (void) closedir( dirp );
        printf("FOO not found\n");
        return NOT_FOUND;
    }

See Also

closedir(),
dirent.h,
getdents(),
libc,
readdir(),
rewinddir(),
seekdir(),
telldir()
POSIX Standard, §5.1.2

Notes

The 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.

The COHERENT implementation of the dirent routines was written by D. Gwynn.