#include <dirent.h>
DIR *opendir(const char *filename);
struct dirent *readdir(DIR *dirp);
long telldir(DIR *dirp);
void seekdir(DIR *dirp, long loc);
void rewinddir(DIR *dirp);
int closedir(DIR *dirp);
struct dirent *readdir_r(DIR *dirp, struct dirent *entry);
cc [ flag... ] file ... -D_POSIX_PTHREAD_SEMANTICS [ library... ]
int *readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
See the NOTES section of this page.
readdir() returns a pointer to a structure representing the directory entry at the current position in the directory stream to which dirp refers, and positions the directory stream at the next entry, except on read-only filesystems. It returns a NULL pointer upon reaching the end of the directory stream, or upon detecting an invalid location in the directory. readdir() shall not return directory entries containing empty names. It is unspecified whether entries are returned for dot or dot-dot. The pointer returned by readdir() points to data that may be overwritten by another call to readdir() on the same directory stream. This data shall not be overwritten by another call to readdir() on a different directory stream. readdir() may buffer several directory entries per actual read operation; readdir() marks for update the st_atime field of the directory each time the directory is actually read.
readdir_r() has the equivalent functionality as readdir() except that a buffer result must be supplied by the caller to store the result. The size should be sizeof(struct dirent) + {NAME_MAX} (that is, pathconf(_PC_NAME_MAX )) + 1. _PC_NAME_MAX is defined in <unistd.h>.
The POSIX readdir_r() function initializes the structure referenced by entry and stores a pointer to this structure in result.
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 position associated with the directory stream at the time the telldir() operation that provides loc 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. 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() will fail if one or more of the following are true:
readdir() and readdir_r() will fail if one or more of the following are true:
telldir(), seekdir(), and closedir() return 0 on success and will fail if one or more of the following are true:
#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 ); (void)closedir( dirp ); return (0); }
readdir() is unsafe in multithread applications. readdir_r() is safe, and should be used instead. closedir(), directory(), opendir(), rewinddir(), seekdir(), and telldir() are safe in multithread applications.
Solaris 2.4 and earlier releases provided a readdir_r() interface as specified in POSIX.1c Draft 6. The final POSIX.1c standard changed the interface as described above. Support for the Draft 6 interface is provided for compatibility only and may not be supported in future releases. New applications and libraries should use the POSIX standard interface.
For POSIX.1c complaint applications, the _POSIX_PTHREAD_SEMANTICS and _REENTRANT flags are automatically turned on by defining the _POSIX_C_SOURCE flag with a value >= 199506L.