获取硬盘大小和使用情况
时间:2010-11-23 来源:空灵静世
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <mntent.h>
#include <sys/statfs.h>
#include <string.h>
int get_blk_size(unsigned int *blksize,unsigned int *blkuse){
unsigned int blkfree = 0;
FILE *fp = fopen("/etc/mtab","rb");
if (NULL == fp){
return 0;
}
while(1){
struct mntent *mt = getmntent(fp);
if(NULL==mt){
break;
}
printf("fs_name:[%s]\n",mt->mnt_fsname);
if (NULL != strstr(mt->mnt_fsname,"/dev/")){
struct statfs buf;
// printf("fsname:[%s]\n",mt->mnt_fsname);
statfs(mt->mnt_dir,&buf);
*blksize+= (buf.f_bsize/1024*buf.f_blocks);
blkfree+=(buf.f_bsize/1024*buf.f_bfree);
}
}
*blkuse = *blksize - blkfree;
fclose(fp);
return 0;
}
int main(){
int blksize =0 ;
int blkuse = 0;
get_blk_size(&blksize,&blkuse) ;
printf("blk_size:[%d] blkuse:[%d]\n",blksize,blkuse);
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <mntent.h>
#include <sys/statfs.h>
#include <string.h>
int get_blk_size(unsigned int *blksize,unsigned int *blkuse){
unsigned int blkfree = 0;
FILE *fp = fopen("/etc/mtab","rb");
if (NULL == fp){
return 0;
}
while(1){
struct mntent *mt = getmntent(fp);
if(NULL==mt){
break;
}
printf("fs_name:[%s]\n",mt->mnt_fsname);
if (NULL != strstr(mt->mnt_fsname,"/dev/")){
struct statfs buf;
// printf("fsname:[%s]\n",mt->mnt_fsname);
statfs(mt->mnt_dir,&buf);
*blksize+= (buf.f_bsize/1024*buf.f_blocks);
blkfree+=(buf.f_bsize/1024*buf.f_bfree);
}
}
*blkuse = *blksize - blkfree;
fclose(fp);
return 0;
}
int main(){
int blksize =0 ;
int blkuse = 0;
get_blk_size(&blksize,&blkuse) ;
printf("blk_size:[%d] blkuse:[%d]\n",blksize,blkuse);
return 0;
}
相关阅读 更多 +