Linux C 读取磁盘大小

发布于 2020-07-02  121 次阅读


#include <sys/vfs.h>

struct statfs diskInfo;
statfs("/", &diskInfo);
unsigned long long totalBlocks = diskInfo.f_bsize;
unsigned long long totalSize = totalBlocks * diskInfo.f_blocks;
size_t mbTotalsize = totalSize >> 20;
unsigned long long freeDisk = diskInfo.f_bfree * totalBlocks;
size_t mbFreedisk = freeDisk >> 20;
printf ("/  total=%dMB, free=%dMB\n", mbTotalsize, mbFreedisk);

statfs("/boot", &diskInfo);
totalBlocks = diskInfo.f_bsize;
totalSize = totalBlocks * diskInfo.f_blocks;
mbTotalsize = totalSize >> 20;
freeDisk = diskInfo.f_bfree*totalBlocks;
mbFreedisk = freeDisk >> 20;
printf ("/boot  total=%dMB, free=%dMB\n", mbTotalsize, mbFreedisk);

statfs("/dev/shm", &diskInfo);
totalBlocks = diskInfo.f_bsize;
totalSize = totalBlocks * diskInfo.f_blocks;
mbTotalsize = totalSize >> 20;
freeDisk = diskInfo.f_bfree*totalBlocks;
mbFreedisk = freeDisk >> 20;
printf ("/dev/shm  total=%dMB, free=%dMB\n", mbTotalsize, mbFreedisk);