FAT | RFS | XFS | ZFS

fstat()

PROTOTYPE

#include <posix.h>

int fstat(int fid, struct stat *info);

DESCRIPTION

fstat() writes information about the open file specified by file descriptor fid to the stat structure indicated by info. The stat structure is defined in “posix.h” and reproduced below:

struct stat
{
  dev_t   st_dev;    /* ID of device containing this file */
  ino_t   st_ino;    /* file serial number */
  nlink_t st_nlink;  /* number of links */
  dev_t   st_rdev;   /* device ID (if inode device) */
  off_t   st_size;   /* the file size in bytes */
  time_t  st_atime;  /* last data access time (secs since Epoch) */
  time_t  st_mtime;  /* last data modification time (secs since Epoch) */
  time_t  st_ctime;  /* last status change time (secs since Epoch) */
  mode_t  st_mode;   /* file mode */
  uid_t   st_uid;    /* user ID of file's owner */
  gid_t   st_gid;    /* group ID of file's owner */
};

“posix.h” defines macros that can be applied to the st_mode field to determine the file’s access permissions and mode. The S_ISDIR() and S_ISREG() macros test whether the file is a directory or regular file.

For directories, the st_size field is set to zero. frstat() behaves like fstat() except if the file is a directory, the st_size field is the sum of the size of every regular file in the directory and its subdirectories. Because of the recursive size calculation, frstat() takes longer to execute than fstat().

If successful, fstat() returns 0. Otherwise, it sets errno and returns -1.

ERROR CODES

EBADF fid is not the descriptor of an open file.
EFAULT info equals NULL.

EXAMPLE

struct stat info;

  ...

  if (fstat(fid, &info))
    error("fstat() failed");
  else
  {
    printf("File data access time:   %s", ctime(&info.st_atime));
    printf("File data modified time: %s", ctime(&info.st_mtime));
    if (vol_type == FAT_VOL)
      printf("File creation time: %s", ctime(&info.st_ctime));
    else
      printf("File status change time: %s", ctime(&info.st_ctime));
    printf("File mode:          0%o\n", info.st_mode);
    printf("File serial number: %u\n", (uint)info.st_ino);
    printf("File num links:     %u\n", (uint)info.st_nlink);
    printf("File size (byte):   %'d\n", (int)info.st_size);
    printf("File user ID:       %u\n", info.st_uid);
    printf("File group ID:      %u\n", info.st_gid);
  }