directory

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

#include <dirent.h>
#include <stdio.h>

void show_directory(){
     DIR * dirp;
     struct dirent entry;
     struct dirent * result;

     dirp = opendir("/path/to/dir");
     if ( dirp == NULL ){
          perror("failed to open directory");
          return;
     }

     while( readdir_r(dirp, &entry, &result) == 0 ){
          printf("Directory name (number): %s (%d)\n", entry.d_name, entry.d_ino);
     }

     if ( closedir(dirp) < 0 ){
          perror("failed to close directory");
     }

}

Prototypes

Details

structdirent

This defines the data structure for a directory entry.


structDIR


public intclosedir(DIR * dirp)

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

public DIR *opendir(const char * dirname)

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


public structdirent*readdir(DIR * dirp)

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


public intreaddir_r(DIR * dirp,structdirent* entry,structdirent** result)

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


public voidrewinddir(DIR * dirp)

Rewinds dirp.


public voidseekdir(DIR * dirp,long loc)

Seeks to the specified location in the directory.


public longtelldir(DIR * dirp)

Gets the current location in the directory.

Returns

The current directory location


struct dirent

This defines the data structure for a directory entry.

Methods

Details

public ino_td_ino


public chard_name


struct DIR

Methods

Details

public const void *fs


public void *handle


public longloc


public unsigned intchecksum