main.c 1.6 KB

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