#include <dirent.h>DIR *opendir(const char *filename);
struct dirent *readdir(DIR *dirp);
struct dirent64 *readdir64(DIR *dirp);
int readdir_r(DIR *dirp, struct dirent *buf, struct dirent **resp);
int readdir64_r(DIR *dirp, struct dirent64 *buf, struct dirent64 **resp;
long telldir(DIR *dirp);
void seekdir(DIR *dirp, long loc);
void rewinddir(DIR *dirp);
int closedir(DIR *dirp);
readdir and readdir_r return pointers to the next active
directory entry and position the directory stream (dirp) at the next entry.
Both functions will not return inactive directory entries.
NULL is returned upon reaching the end of the directory.
resp is a pointer to a pointer to a dirent structure which
readdir_r uses to return the directory entry information.
If a directory entry is found, resp will be set to point
to the structure pointed to by buf.
Since the size of the structure may vary depending
on the length of d_name, one should provide, through
buf, a struct dirent
with at least {NAME_MAX} bytes for d_name.
On successful completion, readdir_r returns zero.
A non-zero return indicates an error and is set to the appropriate
errno.
readdir and readdir_r typically buffer several directory entries
per actual read operation; both functions mark for update the st_atime
field of the directory each time the directory is actually read.
They return zero with resp set to a null pointer when the
end of the directory is reached.
The readdir64 and readdir64_r functions are identical to their counterparts, except that they handle large files.
telldir returns the current location associated with the named directory stream.
seekdir sets the position of the next read operation on the directory stream. The new position reverts to the position associated with the directory stream at the time the telldir operation that provides loc was performed. Values returned by telldir are valid only if the directory has not changed because of compaction or expansion. This situation is not a problem with System V, but it may be a problem with some file system types.
rewinddir resets the position of the named directory stream to the beginning of the directory. It also causes the directory stream to refer to the current state of the corresponding directory, as a call to opendir would.
closedir closes the named directory stream and frees the DIR structure.
opendir returns NULL on failure and sets errno to one of the following values:
Under the following conditions readdir, readdir64, readdir_r and readdir64_r return one of the following values on failure:
Under the following condition readdir fails and sets errno to
closedir returns -1 on failure and sets errno to the following value:
These functions overwrite a buffer per DIR as needed, so applications should copy data or use readdir_r to preserve it.
The older readdir_r interface:
struct dirent *readdir_r(DIR *dirp, struct dirent *buf);is available if _SIMPLE_R is defined.
#include <stdio.h> #include <dirent.h>main() { DIR *dirp; struct dirent *direntp;
dirp = opendir("."); while ((direntp = readdir(dirp)) != NULL) (void)printf("%s\n", direntp->d_name); closedir(dirp); return (0); }