File System

File System readdir_r

readdir_r

// INCLUDE
#include <dirent.h>

// PROTOTYPE
int readdir_r(DIR* pdir, struct dirent* dentry, struct dirent** out_dirent)

Reads the next entry from a directory. Used to find the names of all of the files and directories within a directory. See also readdir.

  • dirp: The DIR* returned by opendir.
  • dentry: Pass in a pointer to a struct dirent to be filled in with the current directory entry.
  • out_dirent: If not NULL, filled in with dentry if a directory entry was retrieved, or NULL if at the end of the directory.

Not all fields of dentry are filled in. You should only rely on:

  • d_type: Type of entry:
    • DT_REG: File
    • DT_DIR: Directory
  • d_name: Name of the file or directory. Just the name, not the whole path.

Returns 0 on success. On error, returns -1 and sets errno.