7 #include <sys/statvfs.h>
12 #define TERABYTE (1024ULL * 1024 * 1024 * 1024)
13 #define GIGABYTE (1024ULL * 1024 * 1024)
14 #define MEGABYTE (1024ULL * 1024)
15 #define KILOBYTE (1024ULL)
18 * Prints the given amount of bytes in a human readable manner.
21 static void print_bytes_human(uint64_t bytes) {
23 printf("%.02f TB", (double)bytes / TERABYTE);
24 else if (bytes > GIGABYTE)
25 printf("%.01f GB", (double)bytes / GIGABYTE);
26 else if (bytes > MEGABYTE)
27 printf("%.01f MB", (double)bytes / MEGABYTE);
28 else if (bytes > KILOBYTE)
29 printf("%.01f KB", (double)bytes / KILOBYTE);
31 printf("%.01f B", (double)bytes);
36 * Does a statvfs and prints either free, used or total amounts of bytes in a
37 * human readable manner.
40 void print_disk_info(const char *path, const char *format) {
44 if (statvfs(path, &buf) == -1)
47 for (walk = format; *walk != '\0'; walk++) {
53 if (BEGINS_WITH(walk+1, "free")) {
54 print_bytes_human((uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree);
55 walk += strlen("free");
58 if (BEGINS_WITH(walk+1, "used")) {
59 print_bytes_human((uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree));
60 walk += strlen("used");
63 if (BEGINS_WITH(walk+1, "total")) {
64 print_bytes_human((uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks);
65 walk += strlen("total");
68 if (BEGINS_WITH(walk+1, "avail")) {
69 print_bytes_human((uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail);
70 walk += strlen("avail");