File System

File System fstat

fstat

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

// PROTOTYPE
int fstat(int fd, struct stat* buf)

Get information about a file that is open.

  • fd: The file descriptor for the file, return from the open call.
  • buf: Filled in with file information.

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

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.