]> git.sur5r.net Git - i3/i3status/blob - src/print_disk_info.c
Merge pull request #2 from pfsmorigo/master
[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 (strcasecmp(prefix_type, "decimal") == 0) {
47                 return format_bytes(outwalk, bytes, DECIMAL_BASE, si_symbols);
48         } else if (strcasecmp(prefix_type, "custom") == 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  * Determines whether remaining bytes are below given threshold.
57  *
58  */
59 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
60 static bool below_threshold(struct statfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
61 #else
62 static bool below_threshold(struct statvfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
63 #endif
64         if (strcasecmp(threshold_type, "percentage_free") == 0) {
65                 return 100.0 * (double)buf.f_bfree / (double)buf.f_blocks < low_threshold;
66         } else if (strcasecmp(threshold_type, "percentage_avail") == 0) {
67                 return 100.0 * (double)buf.f_bavail / (double)buf.f_blocks < low_threshold;
68         } else if (strcasecmp(threshold_type, "bytes_free") == 0) {
69                 return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold;
70         } else if (strcasecmp(threshold_type, "bytes_avail") == 0) {
71                 return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold;
72         } else if (threshold_type[0] != '\0' && strncasecmp(threshold_type+1, "bytes_", strlen("bytes_")) == 0) {
73                 uint64_t base = strcasecmp(prefix_type, "decimal") == 0 ? DECIMAL_BASE : BINARY_BASE;
74                 double factor = 1;
75
76                 switch (threshold_type[0]) {
77                 case 'T':
78                 case 't':
79                         factor *= base;
80                 case 'G':
81                 case 'g':
82                         factor *= base;
83                 case 'M':
84                 case 'm':
85                         factor *= base;
86                 case 'K':
87                 case 'k':
88                         factor *= base;
89                         break;
90                 default:
91                         return false;
92                 }
93
94                 if (strcasecmp(threshold_type+1, "bytes_free") == 0) {
95                         return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold * factor;
96                 } else if (strcasecmp(threshold_type+1, "bytes_avail") == 0) {
97                         return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold * factor;
98                 }
99         }
100
101         return false;
102 }
103
104 /*
105  * Does a statvfs and prints either free, used or total amounts of bytes in a
106  * human readable manner.
107  *
108  */
109 void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type, const char *threshold_type, const double low_threshold) {
110         const char *walk;
111         char *outwalk = buffer;
112         bool colorful_output = false;
113
114         INSTANCE(path);
115
116 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
117         struct statfs buf;
118
119         if (statfs(path, &buf) == -1)
120                 return;
121 #else
122         struct statvfs buf;
123
124         if (statvfs(path, &buf) == -1)
125                 return;
126 #endif
127
128         if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) {
129                 START_COLOR("color_bad");
130                 colorful_output = true;
131         }
132
133         for (walk = format; *walk != '\0'; walk++) {
134                 if (*walk != '%') {
135                         *(outwalk++) = *walk;
136                         continue;
137                 }
138
139                 if (BEGINS_WITH(walk+1, "free")) {
140                         outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type);
141                         walk += strlen("free");
142                 }
143
144                 if (BEGINS_WITH(walk+1, "used")) {
145                         outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type);
146                         walk += strlen("used");
147                 }
148
149                 if (BEGINS_WITH(walk+1, "total")) {
150                         outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type);
151                         walk += strlen("total");
152                 }
153
154                 if (BEGINS_WITH(walk+1, "avail")) {
155                         outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type);
156                         walk += strlen("avail");
157                 }
158
159                 if (BEGINS_WITH(walk+1, "percentage_free")) {
160                         outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks);
161                         walk += strlen("percentage_free");
162                 }
163
164                 if (BEGINS_WITH(walk+1, "percentage_used_of_avail")) {
165                         outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks);
166                         walk += strlen("percentage_used_of_avail");
167                 }
168
169                 if (BEGINS_WITH(walk+1, "percentage_used")) {
170                         outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks);
171                         walk += strlen("percentage_used");
172                 }
173
174                 if (BEGINS_WITH(walk+1, "percentage_avail")) {
175                         outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bavail / (double)buf.f_blocks);
176                         walk += strlen("percentage_avail");
177                 }
178         }
179
180         if (colorful_output)
181                 END_COLOR;
182
183         *outwalk = '\0';
184         OUTPUT_FULL_TEXT(buffer);
185 }