directory(3C) directory(3C)
NAME
opendir, readdir, telldir, seekdir, rewinddir, closedir -
directory operations
SYNOPSIS (AT&T System V)
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(filename)
char *filename;
struct dirent *readdir(dirp)
DIR *dirp;
long telldir(dirp)
DIR *dirp;
void seekdir(dirp, loc)
DIR *dirp;
long loc;
void rewinddir(dirp)
DIR *dirp;
closedir(dirp)
DIR *dirp;
SYNOPSIS (4 BSD)
#include <sys/dir.h>
DIR *opendir(filename)
char *filename;
struct direct *readdir(dirp)
DIR *dirp;
long telldir(dirp)
DIR *dirp;
void seekdir(dirp, loc)
DIR *dirp;
long loc;
void rewinddir(dirp)
DIR *dirp;
void closedir(dirp)
DIR *dirp;
DESCRIPTION
Opendir opens the directory named by filename and associates
a directory stream with it. Opendir returns a pointer to be
Page 1 CX/UX Programmer's Reference Manual
directory(3C) directory(3C)
used to identify the directory stream in subsequent opera-
tions. The pointer NULL is returned if filename cannot be
accessed or is not a directory, or if it cannot malloc(3C)
enough memory to hold a DIR structure or a buffer for the
directory entries.
Readdir returns a pointer to the next directory entry. No
inactive entries are returned. It returns NULL upon reach-
ing the end of the directory or detecting an invalid seekdir
operation.
Telldir returns the current location associated with the
named directory stream.
Seekdir sets the position of the next readdir operation on
the directory stream. The new position reverts to the one
associated with the directory stream when the telldir opera-
tion from which loc was obtained was performed. 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. It is
safe to use a previous telldir value immediately after a
call to opendir and before any calls to readdir.
Rewinddir resets the position of the named directory stream
to the beginning of the directory.
Closedir closes the named directory stream.
The following errors can occur as a result of these opera-
tions.
opendir:
[EACCES] A component of filename denies search permis-
sion.
[EFAULT] Filename points outside the allocated address
space.
[EMFILE] The maximum number of file descriptors for
the process are currently open.
[ENAMETOOLONG] The length of the filename argument exceeds
{PATH_MAX}, or a pathname component is longer
that {NAME_MAX} (see pathconf(2)).
[ENFILE] The maximum number of file descriptors in the
system are currently open.
[ENOENT] The named directory does not exist or
Page 2 CX/UX Programmer's Reference Manual
directory(3C) directory(3C)
filename points to an empty string.
[ENOTDIR] A component of filename is not a directory.
readdir:
[EBADF] The file descriptor determined by the DIR
stream is no longer valid. This results if
the DIR stream has been closed.
[ENOENT] The current file pointer for the directory is
not located at a valid entry.
telldir, seekdir, and closedir:
[EBADF] The file descriptor determined by the DIR
stream is no longer valid. This results if
the DIR stream has been closed.
EXAMPLE
Sample code which searches a directory for entry name:
dirp = opendir( "." );
while ( (dp = readdir( dirp )) != NULL )
if ( strcmp( dp->d_name, name ) == 0 )
{
closedir( dirp );
return FOUND;
}
closedir( dirp );
return NOT_FOUND;
SEE ALSO
open(2), close(2), read(2), lseek(2), getdirentries(2),
dir(4)
Page 3 CX/UX Programmer's Reference Manual