]> git.sur5r.net Git - i3/i3status/blob - src/print_disk_info.c
23083058ee979f14e9a828321a88e00ba685ec65
[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__) || (__OpenBSD__) || defined(__DragonFly__)
10 #include <sys/param.h>
11 #include <sys/mount.h>
12 #endif
13 #include <yajl/yajl_gen.h>
14 #include <yajl/yajl_version.h>
15
16 #include "i3status.h"
17
18 #define BINARY_BASE UINT64_C(1024)
19 #define DECIMAL_BASE UINT64_C(1000)
20
21 #define MAX_EXPONENT 4
22
23 static const char * const iec_symbols[MAX_EXPONENT+1] = {"", "Ki", "Mi", "Gi", "Ti"};
24 static const char * const si_symbols[MAX_EXPONENT+1] = {"", "k", "M", "G", "T"};
25 static const char * const custom_symbols[MAX_EXPONENT+1] = {"", "K", "M", "G", "T"};
26
27 /*
28  * Formats bytes according to the given base and set of symbols.
29  *
30  */
31 static int format_bytes(char *outwalk, uint64_t bytes, uint64_t base, const char * const symbols[]) {
32         double size = bytes;
33         int exponent = 0;
34         while (size >= base && exponent < MAX_EXPONENT) {
35                 size /= base;
36                 exponent += 1;
37         }
38         return sprintf(outwalk, "%.1f %sB", size, symbols[exponent]);
39 }
40
41 /*
42  * Prints the given amount of bytes in a human readable manner.
43  *
44  */
45 static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_type) {
46         if (strncmp(prefix_type, "decimal", strlen(prefix_type)) == 0) {
47                 return format_bytes(outwalk, bytes, DECIMAL_BASE, si_symbols);
48         } else if (strncmp(prefix_type, "custom", strlen(prefix_type)) == 0) {
49                 return format_bytes(outwalk, bytes, BINARY_BASE, custom_symbols);
50         } else {
51                 return format_bytes(outwalk, bytes, BINARY_BASE, iec_symbols);
52         }
53 }
54
55 /*
56  * Does a statvfs and prints either free, used or total amounts of bytes in a
57  * human readable manner.
58  *
59  */
60 void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type) {
61         const char *walk;
62         char *outwalk = buffer;
63
64         INSTANCE(path);
65
66 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
67         struct statfs buf;
68
69         if (statfs(path, &buf) == -1)
70                 return;
71 #else
72         struct statvfs buf;
73
74         if (statvfs(path, &buf) == -1)
75                 return;
76 #endif
77
78         for (walk = format; *walk != '\0'; walk++) {
79                 if (*walk != '%') {
80                         *(outwalk++) = *walk;
81                         continue;
82                 }
83
84                 if (BEGINS_WITH(walk+1, "free")) {
85                         outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type);
86                         walk += strlen("free");
87                 }
88
89                 if (BEGINS_WITH(walk+1, "used")) {
90                         outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type);
91                         walk += strlen("used");
92                 }
93
94                 if (BEGINS_WITH(walk+1, "total")) {
95                         outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type);
96                         walk += strlen("total");
97                 }
98
99                 if (BEGINS_WITH(walk+1, "avail")) {
100                         outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type);
101                         walk += strlen("avail");
102                 }
103
104                 if (BEGINS_WITH(walk+1, "percentage_free")) {
105                         outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks);
106                         walk += strlen("percentage_free");
107                 }
108
109                 if (BEGINS_WITH(walk+1, "percentage_used_of_avail")) {
110                         outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks);
111                         walk += strlen("percentage_used_of_avail");
112                 }
113
114                 if (BEGINS_WITH(walk+1, "percentage_used")) {
115                         outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks);
116                         walk += strlen("percentage_used");
117                 }
118
119                 if (BEGINS_WITH(walk+1, "percentage_avail")) {
120                         outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bavail / (double)buf.f_blocks);
121                         walk += strlen("percentage_avail");
122                 }
123         }
124
125         *outwalk = '\0';
126         OUTPUT_FULL_TEXT(buffer);
127 }