File system

File system stat

// INCLUDE
#include <sys/stat.h>

// PROTOTYPE
int stat(const char* pathname, struct stat* buf)

Get information about a file by pathname. The file can be open or closed.

  • pathname: The pathname to the file (Unix-style, with forward slash as the directory separator).
  • buf: Filled in with file information

Returns 0 on success. On error, returns -1 and sets errno. Some possible errno values include:

  • ENOENT: File does not exist.
  • ENOTDIR: A directory component of the path is not a directory.

Only a subset of the struct stat fields are filled in. In particular:

  • st_size: file size in bytes.
  • st_mode:
    • For files, the S_IFREG bit is set.
    • For directories, the S_IFDIR bit is set.
    • Be sure to check for the bit, not equality, as other bits may be set (like S_IRWXU | S_IRWXG | S_IRWXO) may be set.

The file system does not store file times (creation, modification, or access).