]> git.sur5r.net Git - i3/i3status/blob - src/print_disk_info.c
1ac22d0f3a51d453f9c70f17b1c19a93a3e95197
[i3/i3status] / src / print_disk_info.c
1 // vim:ts=8:expandtab
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <stdint.h>
7 #include <sys/statvfs.h>
8 #include <sys/types.h>
9 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
10 #include <sys/param.h>
11 #include <sys/mount.h>
12 #endif
13
14
15 #include "i3status.h"
16
17 #define TERABYTE (1024ULL * 1024 * 1024 * 1024)
18 #define GIGABYTE (1024ULL * 1024 * 1024)
19 #define MEGABYTE (1024ULL * 1024)
20 #define KILOBYTE (1024ULL)
21
22 /*
23  * Prints the given amount of bytes in a human readable manner.
24  *
25  */
26 static void print_bytes_human(uint64_t bytes) {
27         if (bytes > TERABYTE)
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);
35         else {
36                 printf("%.01f B", (double)bytes);
37         }
38 }
39
40 /*
41  * Does a statvfs and prints either free, used or total amounts of bytes in a
42  * human readable manner.
43  *
44  */
45 void print_disk_info(const char *path, const char *format) {
46         const char *walk;
47
48         if (output_format == O_I3BAR)
49                 printf("{\"name\":\"disk_info\", \"instance\": \"%s\", \"full_text\":\"", path);
50
51 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
52         struct statfs buf;
53
54         if (statfs(path, &buf) == -1)
55                 return;
56 #else
57         struct statvfs buf;
58
59         if (statvfs(path, &buf) == -1)
60                 return;
61 #endif
62
63         for (walk = format; *walk != '\0'; walk++) {
64                 if (*walk != '%') {
65                         putchar(*walk);
66                         continue;
67                 }
68
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");
72                 }
73
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");
77                 }
78
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");
82                 }
83
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");
87                 }
88         }
89
90         if (output_format == O_I3BAR)
91                 printf("\"}");
92 }