下面的程序使用statfs函数实现硬盘大小数据提取,及剩余空间大小的提取,并把硬盘大小及剩余空间打印出来。
#include ;
#include ;
#include ;
#define Gsize (1024.00*1024.00*1024.00)
#define Msize (1024.00*1024.00)
#ifndef EXT2_SUPER_MAGIC
#define EXT2_SUPER_MAGIC 0xef53
#endif
int main()
{
long long blocks,bfree;
struct statfs fs;
if(statfs("/",&fs)<0)
{
perror("statfs");
exit(0);
}
printf("%x\n",fs.f_type); /* type of filesystem (see below) */
printf("%ld\n",fs.f_bsize); /* optimal transfer block size */
printf("%ld\n",fs.f_blocks); /* total data blocks in file system */
printf("%ld\n",fs.f_bfree); /* free blocks in fs */
printf("%ld\n",fs.f_bavail); /* free blocks avail to non-superuser */
printf("%ld\n",fs.f_files); /* total file nodes in file system */
printf("%ld\n",fs.f_ffree); /* free file nodes in fs */
printf("%d\n",fs.f_fsid); /* file system id */
printf("%ld\n",fs.f_namelen); /* maximum length of filenames */
blocks=fs.f_blocks;
bfree=fs.f_bfree;
printf(" %lld\n",blocks);
if(fs.f_type==EXT2_SUPER_MAGIC)
{
printf("Total size of / is %f G\n",blocks*fs.f_bsize/Gsize);
printf("Free size of / is %f G\n",bfree*fs.f_bsize/Gsize);
}
}