C Linux检查安装的可用空间

问题描述 投票:0回答:2

[当我运行df -h时,我看到在/dev中我使用6M,大小为40M,可用大小为34M。

如何使用C代码获取此信息?

c linux mount
2个回答
1
投票

来自here

使用statvfs API:

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

[statvfs的原型是

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

结果将填充到buf statvfs结构:

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 */
};

也请查看statvfs命令的man3联机帮助页。


0
投票

您是否已检查source of df from Coreutils

在Linux以及其他类似POSIX的系统上,它使用statvfs()中的sys/statvfs.h

© www.soinside.com 2019 - 2024. All rights reserved.