#include <dirent.h>
DIR *opendir(dirname) char *dirname;
struct dirent *readdir(dirp) DIR *dirp;
long telldir(dirp) DIR *dirp;
void seekdir(dirp, loc) DIR *dirp; long loc;
void rewinddir(dirp) DIR *dirp;
int closedir(dirp) DIR *dirp;
For XPG2 conformance, use:
#include <sys/dirent.h>
opendir() opens the directory named by dirname and associates a directory stream with it. opendir() returns a pointer to be used to identify the directory stream in subsequent operations. A NULL pointer is returned if dirname cannot be accessed or is not a directory, or if it cannot malloc.3v enough memory to hold the whole thing.
readdir() returns a pointer to the next directory entry. It returns NULL upon reaching 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() operation 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. I also causes the directory stream to refer to the current state of the corresponding directory, as a call to opendir() would have done.
closedir()
closes the named
directory stream
and frees the structure associated with the
DIR
pointer.
opendir() returns a pointer to an object of type DIR on success. On failure, it returns NULL and sets errno to indicate the error.
readdir() returns a pointer to an object of type struct dirent on success. On failure, it returns NULL and sets errno to indicate the error. When the end of the directory is encountered, readdir() returns NULL and leaves errno unchanged.
closedir() returns:
telldir() returns the current location associated with the specified directory stream.
Read permission is denied for dirname.
A pathname component is longer than {NAME_MAX} (see sysconf.2v while {_POSIX_NO_TRUNC} is in effect (see pathconf.2v
for each of the following conditions, when the condition is detected, opendir() sets errno to one of the following:
For each of the following conditions, when the condition is detected, readdir() sets errno to the following:
For each of the following conditions, when the condition is detected, closedir() sets errno to the following:
Sample code which searchs a directory for entry ``name'' is:
dirp = opendir("."); for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) if (!strcmp(dp->d_name, name)) { closedir (dirp); return FOUND; } closedir (dirp); return NOT_FOUND;
The directory library routines now use a new include file, <dirent.h>. This replaces the file, <sys/dir.h>, used in previous releases. Furthermore, with the use of this new file, the readdir() routine returns directory entries whose structure is named struct dirent rather than struct direct as before. The file <sys/dir.h> is retained in the current SunOS release for purposes of backwards source code compatibility; programs which use the directory() library and <sys/dir.h> will continue to compile and run without source code modifications. However, existing programs should convert to the use of the new include file, <dirent.h>, as <sys/dir.h> will be removed in a future major release.
The X/Open Portability Guide, issue 2 (XPG2) requires <sys/dirent.h> rather than <dirent.h>. /usr/xpg2include/sys/dirent.h is functionally equivalent to /usr/include/dirent.h. In future SunOS releases, X/Open conformance will require <dirent.h>.
Created by unroff & hp-tools. © somebody (See intro for details). All Rights Reserved. Last modified 11/5/97