简体   繁体   中英

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?

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.

Have you checked the source of df from Coreutils ?

It uses statvfs() from sys/statvfs.h on Linux as well as on other POSIX-like systems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM