fstatvfs() — Get file system information

Standards

Standards / Extensions C or C++ Dependencies
XPG4.2
Single UNIX Specification, Version 3
both  

Format

#define _XOPEN_SOURCE_EXTENDED 1
#include <sys/statvfs.h>

int fstatvfs(int fildes, struct statvfs *fsinfo);

General description

The fstatvfs() function obtains information about the file system containing the file referenced by fildes and stores it in the area of memory pointed to by the fsinfo argument.

The information is returned in a statvfs structure, as defined in the sys/statvfs.h header file. The elements of this structure are described in statvfs() — Get file system information. If fstatvfs() successfully determines this information, it stores it in the area indicated by the fsinfo argument. The size of the buffer determines how much information is stored; data that exceeds the size of the buffer is truncated.

Returned value

If successful, fstatvfs() returns 0.

If unsuccessful, fstatvfs() returns -1 and sets errno to one of the following values:
Error Code
Description
EBADF
fildes is not a valid open file descriptor.
EINTR
A signal was caught during the execution of the function.
EIO
An I/O error has occurred while reading the file system.

Example


#define _XOPEN_SOURCE_EXTENDED 1
#include <sys/statvfs.h>
#include <stdio.h>

main()
{
  char fn[]="temp.file";
  int fd;
  struct statvfs buf;

  if ((fd = creat(fn,S_IWUSR)) < 0)
    perror("creat() error");
  else {
    if (fstatvfs(fd, &buf) == -1)
      perror("fstatvfs() error");
    else {
      printf("each block is %d bytes big\n", buf.f_frsize);
      printf("there are %d blocks available\n", buf.f_bavail);
      printf("out of a total of %d in bytes,\n", buf.f_blocks);
      printf("that's %.0f bytes free out of a total of %.0f\n",
           ((double)buf.f_bavail * buf.f_frsize),
           ((double)buf.f_blocks * buf.f_frsize));
    }
    close(fd);
    unlink(fn);
  }
}
Output
each block is 4096 bytes big
there are 2089 blocks available
out of a total of 2400 in bytes,
that's 8556544 bytes free out of a total of 9830400

Related information