disk.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* See LICENSE file for copyright and license details. */
  2. #include <err.h>
  3. #include <stdio.h>
  4. #include <sys/statvfs.h>
  5. #include "../lib/util.h"
  6. void
  7. disk_free(char *out, const char *path, uint32_t __unused _i, void __unused *_p)
  8. {
  9. struct statvfs fs;
  10. if (statvfs(path, &fs) < 0) {
  11. warn("statvfs '%s'", path);
  12. ERRRET(out);
  13. }
  14. fmt_human(out, fs.f_frsize * fs.f_bavail);
  15. }
  16. void
  17. disk_perc(char *out, const char *path, uint32_t __unused _i, void __unused *_p)
  18. {
  19. struct statvfs fs;
  20. if (statvfs(path, &fs) < 0) {
  21. warn("statvfs '%s'", path);
  22. ERRRET(out);
  23. }
  24. bprintf(
  25. out,
  26. "%" PRIperc,
  27. (percent_t)(100
  28. * (1.0f - ((float)fs.f_bavail / (float)fs.f_blocks))));
  29. }
  30. void
  31. disk_total(char *out,
  32. const char *path,
  33. uint32_t __unused _i,
  34. void __unused *_p)
  35. {
  36. struct statvfs fs;
  37. if (statvfs(path, &fs) < 0) {
  38. warn("statvfs '%s'", path);
  39. ERRRET(out);
  40. }
  41. fmt_human(out, fs.f_frsize * fs.f_blocks);
  42. }
  43. void
  44. disk_used(char *out, const char *path, uint32_t __unused _i, void __unused *_p)
  45. {
  46. struct statvfs fs;
  47. if (statvfs(path, &fs) < 0) {
  48. warn("statvfs '%s'", path);
  49. ERRRET(out);
  50. }
  51. fmt_human(out, fs.f_frsize * (fs.f_blocks - fs.f_bfree));
  52. }