1 // vim:ts=4:sw=4:expandtab
9 #include <sys/statvfs.h>
10 #include <sys/types.h>
11 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
12 #include <sys/param.h>
13 #include <sys/mount.h>
14 #elif defined(__NetBSD__)
18 #include <yajl/yajl_gen.h>
19 #include <yajl/yajl_version.h>
23 #define BINARY_BASE UINT64_C(1024)
24 #define DECIMAL_BASE UINT64_C(1000)
26 #define MAX_EXPONENT 4
28 static const char *const iec_symbols[MAX_EXPONENT + 1] = {"", "Ki", "Mi", "Gi", "Ti"};
29 static const char *const si_symbols[MAX_EXPONENT + 1] = {"", "k", "M", "G", "T"};
30 static const char *const custom_symbols[MAX_EXPONENT + 1] = {"", "K", "M", "G", "T"};
33 * Formats bytes according to the given base and set of symbols.
36 static int format_bytes(char *outwalk, uint64_t bytes, uint64_t base, const char *const symbols[]) {
39 while (size >= base && exponent < MAX_EXPONENT) {
43 return sprintf(outwalk, "%.1f %sB", size, symbols[exponent]);
47 * Prints the given amount of bytes in a human readable manner.
50 static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_type) {
51 if (strcasecmp(prefix_type, "decimal") == 0) {
52 return format_bytes(outwalk, bytes, DECIMAL_BASE, si_symbols);
53 } else if (strcasecmp(prefix_type, "custom") == 0) {
54 return format_bytes(outwalk, bytes, BINARY_BASE, custom_symbols);
56 return format_bytes(outwalk, bytes, BINARY_BASE, iec_symbols);
61 * Determines whether remaining bytes are below given threshold.
64 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
65 static bool below_threshold(struct statfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
67 static bool below_threshold(struct statvfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
69 if (strcasecmp(threshold_type, "percentage_free") == 0) {
70 return 100.0 * (double)buf.f_bfree / (double)buf.f_blocks < low_threshold;
71 } else if (strcasecmp(threshold_type, "percentage_avail") == 0) {
72 return 100.0 * (double)buf.f_bavail / (double)buf.f_blocks < low_threshold;
73 } else if (strcasecmp(threshold_type, "bytes_free") == 0) {
74 return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold;
75 } else if (strcasecmp(threshold_type, "bytes_avail") == 0) {
76 return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold;
77 } else if (threshold_type[0] != '\0' && strncasecmp(threshold_type + 1, "bytes_", strlen("bytes_")) == 0) {
78 uint64_t base = strcasecmp(prefix_type, "decimal") == 0 ? DECIMAL_BASE : BINARY_BASE;
81 switch (threshold_type[0]) {
99 if (strcasecmp(threshold_type + 1, "bytes_free") == 0) {
100 return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold * factor;
101 } else if (strcasecmp(threshold_type + 1, "bytes_avail") == 0) {
102 return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold * factor;
110 * Does a statvfs and prints either free, used or total amounts of bytes in a
111 * human readable manner.
114 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) {
115 const char *selected_format = format;
117 char *outwalk = buffer;
118 bool colorful_output = false;
119 bool mounted = false;
123 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
126 if (statfs(path, &buf) == -1)
130 #elif defined(__NetBSD__)
133 if (statvfs(path, &buf) == -1)
140 if (statvfs(path, &buf) == -1) {
141 /* If statvfs errors, e.g., due to the path not existing,
142 * we consider the device not mounted. */
145 char *sanitized = sstrdup(path);
146 if (strlen(sanitized) > 1 && sanitized[strlen(sanitized) - 1] == '/')
147 sanitized[strlen(sanitized) - 1] = '\0';
148 FILE *mntentfile = setmntent("/etc/mtab", "r");
149 if (mntentfile == NULL) {
150 mntentfile = setmntent("/proc/mounts", "r");
152 if (mntentfile == NULL) {
153 fprintf(stderr, "i3status: files /etc/mtab and /proc/mounts aren't accessible\n");
157 while ((m = getmntent(mntentfile)) != NULL) {
158 if (strcmp(m->mnt_dir, sanitized) == 0) {
163 endmntent(mntentfile);
170 if (format_not_mounted == NULL)
171 format_not_mounted = "";
172 selected_format = format_not_mounted;
173 } else if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) {
174 START_COLOR("color_bad");
175 colorful_output = true;
176 if (format_below_threshold != NULL)
177 selected_format = format_below_threshold;
180 for (walk = selected_format; *walk != '\0'; walk++) {
182 *(outwalk++) = *walk;
184 } else if (BEGINS_WITH(walk + 1, "free")) {
185 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type);
186 walk += strlen("free");
188 } else if (BEGINS_WITH(walk + 1, "used")) {
189 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type);
190 walk += strlen("used");
192 } else if (BEGINS_WITH(walk + 1, "total")) {
193 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type);
194 walk += strlen("total");
196 } else if (BEGINS_WITH(walk + 1, "avail")) {
197 outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type);
198 walk += strlen("avail");
200 } else if (BEGINS_WITH(walk + 1, "percentage_free")) {
201 outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks, pct_mark);
202 walk += strlen("percentage_free");
204 } else if (BEGINS_WITH(walk + 1, "percentage_used_of_avail")) {
205 outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks, pct_mark);
206 walk += strlen("percentage_used_of_avail");
208 } else if (BEGINS_WITH(walk + 1, "percentage_used")) {
209 outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks, pct_mark);
210 walk += strlen("percentage_used");
212 } else if (BEGINS_WITH(walk + 1, "percentage_avail")) {
213 outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)buf.f_bavail / (double)buf.f_blocks, pct_mark);
214 walk += strlen("percentage_avail");
225 OUTPUT_FULL_TEXT(buffer);