disk.c 992 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* See LICENSE file for copyright and license details. */
  2. #include <stdio.h>
  3. #include <sys/statvfs.h>
  4. #include "../util.h"
  5. const char *
  6. disk_free(const char *path)
  7. {
  8. struct statvfs fs;
  9. if (statvfs(path, &fs) < 0) {
  10. warn("statvfs '%s':", path);
  11. return NULL;
  12. }
  13. return fmt_human(fs.f_frsize * fs.f_bavail, 1024);
  14. }
  15. const char *
  16. disk_perc(const char *path)
  17. {
  18. struct statvfs fs;
  19. if (statvfs(path, &fs) < 0) {
  20. warn("statvfs '%s':", path);
  21. return NULL;
  22. }
  23. return bprintf("%d", (int)(100 *
  24. (1.0f - ((float)fs.f_bavail / (float)fs.f_blocks))));
  25. }
  26. const char *
  27. disk_total(const char *path)
  28. {
  29. struct statvfs fs;
  30. if (statvfs(path, &fs) < 0) {
  31. warn("statvfs '%s':", path);
  32. return NULL;
  33. }
  34. return fmt_human(fs.f_frsize * fs.f_blocks, 1024);
  35. }
  36. const char *
  37. disk_used(const char *path)
  38. {
  39. struct statvfs fs;
  40. if (statvfs(path, &fs) < 0) {
  41. warn("statvfs '%s':", path);
  42. return NULL;
  43. }
  44. return fmt_human(fs.f_frsize * (fs.f_blocks - fs.f_bfree), 1024);
  45. }