directory(3C) INTERACTIVE UNIX System directory(3C)
NAME
directory: opendir, readdir, telldir, seekdir, rewinddir,
closedir - directory operations
SYNOPSIS
#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;
void closedir(dirp)
DIR *dirp;
DESCRIPTION
The opendir command opens the directory named by filename
and associates a directory stream with it. The opendir com-
mand returns a pointer to be used to identify the directory
stream in subsequent operations. The pointer NULL is
returned if filename cannot be accessed or is not a direc-
tory, or if it cannot malloc enough memory to hold a DIR
structure or a buffer for the directory entries.
readdir returns a pointer to the next active directory
entry. No inactive entries are returned. It returns NULL
upon reaching the end of the directory or upon detecting an
invalid location in the directory.
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 if the directory has not
changed due to compaction or expansion. This is not a prob-
lem with System V, but it may be with some file system
types.
Rev. 1.2 Page 1
directory(3C) INTERACTIVE UNIX System directory(3C)
rewinddir resets the position of the named directory stream
to the beginning of the directory.
closedir closes the named directory stream and frees the DIR
structure.
The following errors can occur as a result of these opera-
tions:
opendir:
[ENOTDIR] A component of filename is not a directory.
[EACCES] A component of filename denies search permis-
sion.
[EMFILE] The maximum number of file descriptors are
currently open.
[EFAULT] The filename variable points outside the
allocated address space.
[ENAMETOOLONG] (POSIX Only) The length of the path argument
exceeds {PATH_MAX}, or a path name component
is longer than {NAME_MAX} while
{_POSIX_NO_TRUNC} is in effect.
readdir:
[ENOENT] The current file pointer for the directory is
not located at a valid entry.
[EBADF] The file descriptor determined by the DIR
stream is no longer valid. This results if
the DIR stream has been closed.
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;
Rev. 1.2 Page 2
directory(3C) INTERACTIVE UNIX System directory(3C)
}
closedir( dirp );
return NOT_FOUND;
SEE ALSO
getdents(2), dirent(4).
WARNING
rewinddir is implemented as a macro, so its function address
cannot be taken.
Rev. 1.2 Page 3