p The .Fn fdopendir function associates a .Em directory stream with the directory file descriptor .Fa fd . The file descriptor .Fa fd must not be used further by the caller in any way.
p Both functions return a pointer to be used to identify the .Em directory stream in subsequent operations. The pointer .Dv NULL is returned if .Fa filename cannot be accessed, or if it cannot .Xr malloc 3 enough memory to hold the whole thing.
p The .Fn readdir function returns a pointer to the next directory entry. It returns .Dv NULL upon reaching the end of the directory or detecting an invalid .Fn seekdir operation.
p The .Fn readdir_r function provides the same functionality as .Fn readdir , but the caller must provide a directory .Fa entry buffer to store the results in. If the read succeeds, .Fa result is pointed at the .Fa entry ; upon reaching the end of the directory .Fa result is set to .Dv NULL . The .Fn readdir_r function returns 0 on success or an error number to indicate failure.
p The .Fn telldir function returns the current location associated with the named .Em directory stream .
p The .Fn seekdir function sets the position of the next .Fn readdir operation on the .Em directory stream . The new position reverts to the one associated with the .Em directory stream when the .Fn telldir operation was performed. Values returned by .Fn telldir are good only for the lifetime of the .Dv DIR pointer, .Fa dirp , from which they are derived. If the directory is closed and then reopened, the .Fn telldir value cannot be re-used.
p The .Fn rewinddir function resets the position of the named .Em directory stream to the beginning of the directory.
p The .Fn closedir function closes the named .Em directory stream and frees the structure associated with the .Fa dirp pointer, returning 0 on success. On failure, -1 is returned and the global variable .Va errno is set to indicate the error.
p The .Fn dirfd function returns the integer file descriptor associated with the named .Em directory stream , see .Xr open 2 . .Sh EXAMPLES Sample code which searches a directory for entry .Dq name is: d -literal -offset indent len = strlen(name); dirp = opendir("."); if (dirp != NULL) { while ((dp = readdir(dirp)) != NULL) if (dp-\*[Gt]d_namlen == len \*[Am]\*[Am] !strcmp(dp-\*[Gt]d_name, name)) { (void)closedir(dirp); return (FOUND); } (void)closedir(dirp); } return (NOT_FOUND); .Ed .Sh SEE ALSO .Xr close 2 , .Xr lseek 2 , .Xr open 2 , .Xr read 2 , .Xr dir 5 .Sh STANDARDS The .Fn opendir , .Fn readdir , .Fn rewinddir and .Fn closedir functions conform to .St -p1003.1-90 . .Sh HISTORY The .Fn opendir , .Fn readdir , .Fn telldir , .Fn seekdir , .Fn rewinddir , .Fn closedir , and .Fn dirfd functions appeared in x 4.2 .