#include <stdio.h>#include<stdlib.h>#include<string.h>#include<errno.h>#ifnndef win32#include<sys/statfs.h>#include<sys/vfs.h>#endif#defineDefault_disk_path "/work"typedefstructStatfs disk,*Pdisk;//Gets the structure that contains disk space informationBOOLGetdiskinfo (Pdisk DiskInfo,Const Char*path) { Chardpath[ -]=Default_disk_path; intflag=0; if(null!=path) {strcpy (Dpath,path); } if(-1= = (Flag=statfs (dpath,diskinfo)))//Gets the structure that contains disk space information { return false; } return true;}//calculates the total disk space, disk free space, and calculates the unit of the result in byteintCaldiskinfo (unsignedLong Long&disktotal, unsignedLong Long&diskavail) {
Disk disk;
memset (&disk,0,sizeof(DISK));
Getdiskinfo (&disk,disk_path);
pdisk diskinfo = &disk;
unsigned long long blocksize=diskinfo->f_bsize; // each block contains a byte size
disktotal=diskinfo->f_blocks*blocksize; // Total disk space
diskavail=diskinfo->f_bavail*blocksize; // Disk free space }
Linux gets disk usage through function calls