7 #include <sys/statvfs.h>
9 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
10 #include <sys/param.h>
11 #include <sys/mount.h>
17 #define TERABYTE (1024ULL * 1024 * 1024 * 1024)
18 #define GIGABYTE (1024ULL * 1024 * 1024)
19 #define MEGABYTE (1024ULL * 1024)
20 #define KILOBYTE (1024ULL)
23 * Prints the given amount of bytes in a human readable manner.
26 static void print_bytes_human(uint64_t bytes) {
28 printf("%.02f TB", (double)bytes / TERABYTE);
29 else if (bytes > GIGABYTE)
30 printf("%.01f GB", (double)bytes / GIGABYTE);
31 else if (bytes > MEGABYTE)
32 printf("%.01f MB", (double)bytes / MEGABYTE);
33 else if (bytes > KILOBYTE)
34 printf("%.01f KB", (double)bytes / KILOBYTE);
36 printf("%.01f B", (double)bytes);
41 * Does a statvfs and prints either free, used or total amounts of bytes in a
42 * human readable manner.
45 void print_disk_info(const char *path, const char *format) {
48 if (output_format == O_I3BAR)
49 printf("{\"name\":\"disk_info\", \"instance\": \"%s\", \"full_text\":\"", path);
51 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
54 if (statfs(path, &buf) == -1)
59 if (statvfs(path, &buf) == -1)
63 for (walk = format; *walk != '\0'; walk++) {
69 if (BEGINS_WITH(walk+1, "free")) {
70 print_bytes_human((uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree);
71 walk += strlen("free");
74 if (BEGINS_WITH(walk+1, "used")) {
75 print_bytes_human((uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree));
76 walk += strlen("used");
79 if (BEGINS_WITH(walk+1, "total")) {
80 print_bytes_human((uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks);
81 walk += strlen("total");
84 if (BEGINS_WITH(walk+1, "avail")) {
85 print_bytes_human((uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail);
86 walk += strlen("avail");
90 if (output_format == O_I3BAR)