main.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #include <sys/statfs.h>
  2. #include <stdio.h>
  3. int main(int argc,char **argv)
  4. {
  5. struct statfs diskInfo;
  6. statfs("/bin/about.elf", &diskInfo);
  7. unsigned long long blocksize1 = diskInfo.f_bsize; //每个block里包含的字节数
  8. unsigned long long totalsize = blocksize1 * diskInfo.f_blocks;//总的字节数,f_blocks为block的数目
  9. printf("Total_size=%llu B =%llu KB =%llu MB = %llu GB\n",
  10. totalsize,totalsize>>10,totalsize>>20, totalsize>>30);
  11. /* 2.获取一下剩余空间和可用空间的大小 */
  12. unsigned long long freeDisk = diskInfo.f_bfree * blocksize1; //剩余空间的大小
  13. unsigned long long availableDisk = diskInfo.f_bavail * blocksize1; //可用空间大小
  14. printf("Disk_free=%llu MB =%llu GB Disk_available=%llu MB = %llu GB\n",
  15. freeDisk>>20,freeDisk>>30,availableDisk>>20, availableDisk>>30);
  16. printf("====================\n");
  17. printf("diskInfo address: %p\n", diskInfo);
  18. printf("f_type= %lu\n", diskInfo.f_type);
  19. printf("f_bsize = %lu\n", diskInfo.f_bsize);
  20. printf("f_blocks = %d\n", diskInfo.f_blocks);
  21. printf("f_bfree = %lu\n", diskInfo.f_bfree);
  22. printf("b_avail = %d\n", diskInfo.f_bavail);
  23. printf("f_files = %d\n", diskInfo.f_files);
  24. printf("f_ffree = %lu\n", diskInfo.f_ffree);
  25. printf("f_fsid = %ld\n", diskInfo.f_fsid);
  26. printf("f_namelen = %ld\n", diskInfo.f_namelen);
  27. printf("f_frsize = %ld\n", diskInfo.f_frsize);
  28. printf("f_flags = %ld\n", diskInfo.f_flags);
  29. return 0;
  30. }