disk.c 1016 B

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