]> git.sur5r.net Git - i3/i3status/blob - src/print_disk_info.c
able to print percentage
[i3/i3status] / src / print_disk_info.c
1 // vim:ts=4:sw=4:expandtab
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <stdint.h>
7 #include <sys/stat.h>
8 #include <sys/statvfs.h>
9 #include <sys/types.h>
10 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
11 #include <sys/param.h>
12 #include <sys/mount.h>
13 #elif defined(__NetBSD__)
14 #else
15 #include <mntent.h>
16 #endif
17 #include <yajl/yajl_gen.h>
18 #include <yajl/yajl_version.h>
19
20 #include "i3status.h"
21
22 #define BINARY_BASE UINT64_C(1024)
23 #define DECIMAL_BASE UINT64_C(1000)
24
25 #define MAX_EXPONENT 4
26
27 static const char *const iec_symbols[MAX_EXPONENT + 1] = {"", "Ki", "Mi", "Gi", "Ti"};
28 static const char *const si_symbols[MAX_EXPONENT + 1] = {"", "k", "M", "G", "T"};
29 static const char *const custom_symbols[MAX_EXPONENT + 1] = {"", "K", "M", "G", "T"};
30
31 /*
32  * Formats bytes according to the given base and set of symbols.
33  *
34  */
35 static int format_bytes(char *outwalk, uint64_t bytes, uint64_t base, const char *const symbols[]) {
36     double size = bytes;
37     int exponent = 0;
38     while (size >= base && exponent < MAX_EXPONENT) {
39         size /= base;
40         exponent += 1;
41     }
42     return sprintf(outwalk, "%.1f %sB", size, symbols[exponent]);
43 }
44
45 /*
46  * Prints the given amount of bytes in a human readable manner.
47  *
48  */
49 static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_type) {
50     if (strcasecmp(prefix_type, "decimal") == 0) {
51         return format_bytes(outwalk, bytes, DECIMAL_BASE, si_symbols);
52     } else if (strcasecmp(prefix_type, "custom") == 0) {
53         return format_bytes(outwalk, bytes, BINARY_BASE, custom_symbols);
54     } else {
55         return format_bytes(outwalk, bytes, BINARY_BASE, iec_symbols);
56     }
57 }
58
59 /*
60  * Determines whether remaining bytes are below given threshold.
61  *
62  */
63 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
64 static bool below_threshold(struct statfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
65 #else
66 static bool below_threshold(struct statvfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
67 #endif
68     if (strcasecmp(threshold_type, "percentage_free") == 0) {
69         return 100.0 * (double)buf.f_bfree / (double)buf.f_blocks < low_threshold;
70     } else if (strcasecmp(threshold_type, "percentage_avail") == 0) {
71         return 100.0 * (double)buf.f_bavail / (double)buf.f_blocks < low_threshold;
72     } else if (strcasecmp(threshold_type, "bytes_free") == 0) {
73         return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold;
74     } else if (strcasecmp(threshold_type, "bytes_avail") == 0) {
75         return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold;
76     } else if (threshold_type[0] != '\0' && strncasecmp(threshold_type + 1, "bytes_", strlen("bytes_")) == 0) {
77         uint64_t base = strcasecmp(prefix_type, "decimal") == 0 ? DECIMAL_BASE : BINARY_BASE;
78         double factor = 1;
79
80         switch (threshold_type[0]) {
81             case 'T':
82             case 't':
83                 factor *= base;
84             case 'G':
85             case 'g':
86                 factor *= base;
87             case 'M':
88             case 'm':
89                 factor *= base;
90             case 'K':
91             case 'k':
92                 factor *= base;
93                 break;
94             default:
95                 return false;
96         }
97
98         if (strcasecmp(threshold_type + 1, "bytes_free") == 0) {
99             return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold * factor;
100         } else if (strcasecmp(threshold_type + 1, "bytes_avail") == 0) {
101             return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold * factor;
102         }
103     }
104
105     return false;
106 }
107
108 /*
109  * Does a statvfs and prints either free, used or total amounts of bytes in a
110  * human readable manner.
111  *
112  */
113 void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *format_below_threshold, const char *format_not_mounted, const char *prefix_type, const char *threshold_type, const double low_threshold) {
114     const char *selected_format = format;
115     const char *walk;
116     char *outwalk = buffer;
117     bool colorful_output = false;
118     bool mounted = false;
119
120     INSTANCE(path);
121
122 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
123     struct statfs buf;
124
125     if (statfs(path, &buf) == -1)
126         return;
127
128     mounted = true;
129 #elif defined(__NetBSD__)
130     struct statvfs buf;
131
132     if (statvfs(path, &buf) == -1)
133         return;
134
135     mounted = true;
136 #else
137     struct statvfs buf;
138
139     if (statvfs(path, &buf) == -1) {
140         /* If statvfs errors, e.g., due to the path not existing,
141          * we consider the device not mounted. */
142         mounted = false;
143     } else {
144         char *sanitized = sstrdup(path);
145         if (strlen(sanitized) > 1 && sanitized[strlen(sanitized) - 1] == '/')
146             sanitized[strlen(sanitized) - 1] = '\0';
147         FILE *mntentfile = setmntent("/etc/mtab", "r");
148         struct mntent *m;
149
150         while ((m = getmntent(mntentfile)) != NULL) {
151             if (strcmp(m->mnt_dir, sanitized) == 0) {
152                 mounted = true;
153                 break;
154             }
155         }
156         endmntent(mntentfile);
157         free(sanitized);
158     }
159 #endif
160
161     if (!mounted) {
162         if (format_not_mounted == NULL)
163             format_not_mounted = "";
164         selected_format = format_not_mounted;
165     } else if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) {
166         START_COLOR("color_bad");
167         colorful_output = true;
168         if (format_below_threshold != NULL)
169             selected_format = format_below_threshold;
170     }
171
172     for (walk = selected_format; *walk != '\0'; walk++) {
173         if (*walk != '%') {
174             *(outwalk++) = *walk;
175
176         } else if (BEGINS_WITH(walk + 1, "free")) {
177             outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type);
178             walk += strlen("free");
179
180         } else if (BEGINS_WITH(walk + 1, "used")) {
181             outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type);
182             walk += strlen("used");
183
184         } else if (BEGINS_WITH(walk + 1, "total")) {
185             outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type);
186             walk += strlen("total");
187
188         } else if (BEGINS_WITH(walk + 1, "avail")) {
189             outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type);
190             walk += strlen("avail");
191
192         } else if (BEGINS_WITH(walk + 1, "percentage_free")) {
193             outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks, pct_mark);
194             walk += strlen("percentage_free");
195
196         } else if (BEGINS_WITH(walk + 1, "percentage_used_of_avail")) {
197             outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks, pct_mark);
198             walk += strlen("percentage_used_of_avail");
199
200         } else if (BEGINS_WITH(walk + 1, "percentage_used")) {
201             outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks, pct_mark);
202             walk += strlen("percentage_used");
203
204         } else if (BEGINS_WITH(walk + 1, "percentage_avail")) {
205             outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)buf.f_bavail / (double)buf.f_blocks, pct_mark);
206             walk += strlen("percentage_avail");
207
208         } else {
209             *(outwalk++) = '%';
210         }
211     }
212
213     if (colorful_output)
214         END_COLOR;
215
216     *outwalk = '\0';
217     OUTPUT_FULL_TEXT(buffer);
218 }