DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

directory(S)


directory: closedir, opendir, readdir, rewinddir, seekdir, telldir -- directory operations

Syntax

cc . . . -lc

#include <dirent.h>

int closedir(DIR *dirp);

DIR *opendir(const char *dirname);

struct dirent *readdir(DIR *dirp);

void rewinddir(DIR *dirp);

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

long int telldir(DIR *dirp);

Description

opendir opens the directory named by pathname and associates a directory stream with it. opendir returns a pointer to be used to identify the directory stream in subsequent operations. The pointer NULL is returned if pathname cannot be accessed or is not a directory, 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, loc, is the position associated with the directory stream when the telldir operation was performed. Values returned by telldir are good only if the directory has not changed due to compaction or expansion. This is not a problem with System V, but it may be with some file system types.

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

opendir:


[EACCES]
A component of pathname denies search permission.

[EFAULT]
pathname points outside the allocated address space.

[ELOOP]
Too many symbolic links were encountered in translating pathname.

[EMFILE]
The maximum number of file descriptors are currently open.

[ENAMETOOLONG]
The length of the pathname argument exceeds {PATH_MAX}, or the length of a pathname component exceeds {NAME_MAX} while {_POSIX_NO_TRUNC} is in effect.

[ENFILE]
The system file table is full.

[ENOENT]
A component of pathname does not name an existing directory or pathname is an empty string.

[ENOTDIR]
A component of pathname 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.

closedir:


[EBADF]
The dirp argument does not refer to an open directory stream.

[EINTR]
The closedir function was interrupted by a signal.

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;

Warnings

rewinddir is implemented as a macro, so its function address cannot be taken.

See also

dirent(FP)

Standards conformance

closedir, opendir, readdir and rewinddir are conformant with:

X/Open Portability Guide, Issue 3, 1989 ;
IEEE POSIX Std 1003.1-1990 System Application Program Interface (API) [C Language] (ISO/IEC 9945-1) ;
and NIST FIPS 151-1 .

seekdir and telldir are conformant with:

X/Open Portability Guide, Issue 3, 1989 .


© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003