Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

close(2)

getdirentries(2)

lseek(2)

open(2)

read(2)

dir(5)

directory(3)

NAME

opendir, readdir, telldir, seekdir, rewinddir, closedir − directory operations

SYNTAX

#include <sys/dir.h>

DIR *opendir(filename)
char *filename;

struct direct *readdir(dirp)
DIR *dirp;

long telldir(dirp)
DIR *dirp;

seekdir(dirp, loc)
DIR *dirp;
long loc;

rewinddir(dirp)
DIR *dirp;

int closedir(dirp)
DIR *dirp;

DESCRIPTION

The opendir library routine opens the directory named by filename and associates a directory stream with it.  A pointer is returned to identify the directory stream in subsequent operations.  The pointer NULL is returned if the specified filename can not be accessed, or if insufficient memory is available to open the directory file. 

The readdir routine returns a pointer to the next directory entry.  It returns NULL upon reaching the end of the directory or on detecting an invalid seekdir operation. The readdir routine uses the getdirentries system call to read directories. Since the readdir routine returns NULL upon reaching the end of the directory or on detecting an error, an application which wishes to detect the difference must set errno to 0 prior to calling readdir.

The telldir routine returns the current location associated with the named directory stream.  Values returned by telldir are good only for the lifetime of the DIR pointer from which they are derived. If the directory is closed and then reopened, the telldir value may be invalidated due to undetected directory compaction.

The seekdir routine sets the position of the next readdir operation on the directory stream.  Only values returned by telldir should be used with seekdir.

The rewinddir routine resets the position of the named directory stream to the beginning of the directory. 

The closedir routine closes the named directory stream and returns a value of 0 if successful. Otherwise, a value of −1 is returned and errno is set to indicate the error.  All resources associated with this directory stream are released. 

EXAMPLE

The following sample code searches a directory for the entry name. 

len = strlen(name);
 dirp = opendir(".");
 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
 if (dp->d_namlen == len && !strcmp(dp->d_name, name)) {
 closedir(dirp);
 return FOUND;
 }
 closedir(dirp);
 return NOT_FOUND;

SEE ALSO

close(2), getdirentries(2), lseek(2), open(2), read(2), dir(5)

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026