Skip to main content

Directory Entry (dirent)

Functions

closedir(DIR *dirp)#

int closedir (DIR *dirp)

Type: int

Parameters:

  • DIR * dirp: A pointer to the open directory

Details#

Closes the directory stream specified by dirp.

Returns:

Zero or -1 with errno (see Errno) set to:

  • EINVAL: dirp does not refere to an open directory stream

opendir(const char *dirname)#

DIR * opendir (const char *dirname)

Type: DIR *

Parameters:

  • const char * dirname

Details#

Opens a directory.

Returns:

a pointer to the directory or NULL with errno (see Errno) set to:

  • ENOMEM: not enough memory
  • ENOENT: dirname was not found
  • EACCES: read access to dirname is not allowed
  • ENAMETOOLONG: dirname exceeds PATH_MAX or an element of dirname exceeds NAME_MAX

readdir(DIR *dirp)#

struct dirent * readdir (DIR *dirp)

Type: struct dirent *

Parameters:

  • DIR * dirp: a pointer to the directory structure

Details#

Reads the next directory entry in the open directory.


This function is not thread-safe nor re-entrant; use readdir_r() as a thread-safe, re-entrant alternative.

Returns:

a pointer to a dirent or NULL with errno (see Errno) set to:

  • EBADF: dirp is invalid
  • ENOENT: the current position of the directory stream is invalid

readdir_r(DIR dirp, struct dirent entry, struct dirent **result)#

int readdir_r (DIR *dirp, struct dirent *entry, struct dirent **result)

Type: int

Parameters:

  • DIR * dirp: a pointer to the directory structure
  • struct dirent * entry: a pointer to the destination memory
  • struct dirent * result: this value is assigned to entry* on success and NULL on failure

Details#

Reads the next directory entry in the open directory (reentrant version).

Returns:

a pointer to a dirent or NULL with errno (see Errno) set to:

  • EBADF: dirp is invalid
  • ENOENT: the current position of the directory stream is invalid

rewinddir(DIR *dirp)#

void rewinddir (DIR *dirp)

Type: void

Parameters:

  • DIR * dirp: a pointer to the directory structure

Details#

Rewinds dirp.

seekdir(DIR *dirp, long loc)#

void seekdir (DIR *dirp, long loc)

Type: void

Parameters:

  • DIR * dirp: a pointer to the directory structure
  • long loc: the target location

Details#

Seeks to the specified location in the directory.

telldir(DIR *dirp)#

long telldir (DIR *dirp)

Type: long

Parameters:

  • DIR * dirp: a pointer to the directory structure

Details#

Gets the current location in the directory.

Returns:

The current directory location

Details

This interface accesses directory entries (both files and folders). Here is an example of how to use this interface.

#include <[dirent.h](dirent_8h)>#include <stdio.h>
void show_directory(){     DIR * dirp;     struct [dirent](structdirent) entry;     struct [dirent](structdirent) * result;
     dirp = [opendir](group__directory#group__directory_1gae27c7f260a652b74c43296993d14ef0b)("/path/to/dir");     if ( dirp == NULL ){          perror("failed to open directory");          return;     }
     while( [readdir_r](group__directory#group__directory_1ga2219da481be06be4bf9f8f363b607492)(dirp, &entry, &result) == 0 ){          printf("Directory name (number): %s (%d)\n", entry.d_name, entry.d_ino);     }
     if ( [closedir](group__directory#group__directory_1gaaeac2b41e8c2c3a5f91c9bd511a8c0a6)(dirp) < 0 ){          perror("failed to close directory");     }
}