readdir_r

Reads the directory entries of a directory reentrantly.

Library:LibC
Classification:POSIX
Service:File and Directory I/O

Syntax

  #include <dirent.h> 
   
  int readdir_r (
     DIR              *dirp,
     struct dirent    *entry,
     struct dirent   **result);
  

Parameters

dirp

(IN) Specifies the directory to enumerate. For information about the structure, see DIR.

entry

(OUT) Points to the structure to receive information about the next directory entry. For information about the structure, see DIR.

result

(IN/OUT) Points to the directory entry pointer, which was returned in the entry parameter. When the operation encounters the end of the directory, this parameter is set to NULL. For information about the structure, see DIR.

Return Values

If successful, returns 0. When the operation encounters the end of the directory, returns 0 and sets the result parameter to NULL. If the dirp parameter does not refer to a directory, returns a nonzero error code.

Decimal

Constant

Description

4

EBADF

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

Remarks

The readdir_r function can be called repeatedly to obtain the list of file and directory names contained in the directory specified by the dirp parameter, which was obtain from a call to the opendir function.

The closedir function must be called to close the directory and free the memory allocated by the opendir function.

The readdir_r function does not return entries containing empty names. If entries for dot or dot-dot exist, one entry is returned for dot and one entry is returned for dot-dot; otherwise, they are not returned.

If a file is removed or added to the directory after the most recent call to opendir or rewinddir, a subsequent call to readdir_r might or might not return an entry for the file; the behavior is unspecified.

The readdir_r function marks for update the st_atime field of the directory each time the directory is actually read.

The readdir_r function is thread-safe. It returns values in a user-supplied buffer instead of a static data area that might be overwritten by each call.

See Also