group::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");
     }

}

Details

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



date: “2019-06-27” layout: post title: struct::dirent katex: true

categories: struct

This defines the data structure for a directory entry.

Methods

Details

public ino_td_ino


public chard_name



date: “2019-06-27” layout: post title: struct::DIR katex: true

categories: struct

Methods

Details

public const void *fs


public void *handle


public longloc


public unsigned intchecksum


X

Thanks for Coming!

Subscribe to news and updates