#define SYS_DISK_BUFF_LEN 256
#define SYS_DISK_NAME_LEN 80
#define SYS_100_PERSENT 100
int
get_sysDiskUsage()
{
FILE * fp;
char filesystem[SYS_DISK_NAME_LEN],available[SYS_DISK_NAME_LEN],use[SYS_DISK_NAME_LEN],mounted[SYS_DISK_NAME_LEN],buf[SYS_DISK_BUFF_LEN];
double used,blocks,used_rate;
fp=popen("df","r");
fgets(buf,SYS_DISK_BUFF_LEN,fp);
double dev_total = 0,dev_used = 0;
while(6 == fscanf(fp,"%s %lf %lf %s %s %s",filesystem,&blocks,&used,available,use,mounted))
{
dev_total += blocks;
dev_used += used;
}
used_rate = (dev_used/dev_total)*SYS_100_PERSENT;
pclose(fp);
return used_rate;
}
原文地址:http://blog.51cto.com/qiaopeng688/2140582
时间: 2024-11-15 00:59:58