Skip to content
Advertisement

C Linux Check free space in mount

When I running df -h I can see that in /dev I use 6M and the size is 40M , and Available size is 34M .

How can I get this information with c code?

Advertisement

Answer

From here:

Use the statvfs API:

// header for statvfs
#include <sys/statvfs.h>

and the prototype of the statvfs is

int statvfs(const char *path, struct statvfs *buf);

The results will be filled to the buf statvfs struct:

struct statvfs {
    unsigned long  f_bsize;    /* filesystem block size */
    unsigned long  f_frsize;   /* fragment size */
    fsblkcnt_t     f_blocks;   /* size of fs in f_frsize units */
    fsblkcnt_t     f_bfree;    /* # free blocks */
    fsblkcnt_t     f_bavail;   /* # free blocks for unprivileged users */
    fsfilcnt_t     f_files;    /* # inodes */
    fsfilcnt_t     f_ffree;    /* # free inodes */
    fsfilcnt_t     f_favail;   /* # free inodes for unprivileged users */
    unsigned long  f_fsid;     /* filesystem ID */
    unsigned long  f_flag;     /* mount flags */
    unsigned long  f_namemax;  /* maximum filename length */
};

The return type is:

On success, zero is returned. On error, -1 is returned, and errno is set appropriately.

Also have a look at the man3 manpage of the statvfs command for more and detailed information.

Advertisement