]> git.sur5r.net Git - i3/i3status/blobdiff - src/print_disk_info.c
Make the code more readable.
[i3/i3status] / src / print_disk_info.c
index 23083058ee979f14e9a828321a88e00ba685ec65..ef028bdf3bd710c8d4ae9896fec35744be28c524 100644 (file)
@@ -3,7 +3,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
+#include <mntent.h>
 #include <stdint.h>
+#include <sys/stat.h>
 #include <sys/statvfs.h>
 #include <sys/types.h>
 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || (__OpenBSD__) || defined(__DragonFly__)
@@ -43,23 +45,73 @@ static int format_bytes(char *outwalk, uint64_t bytes, uint64_t base, const char
  *
  */
 static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_type) {
-        if (strncmp(prefix_type, "decimal", strlen(prefix_type)) == 0) {
+        if (strcasecmp(prefix_type, "decimal") == 0) {
                 return format_bytes(outwalk, bytes, DECIMAL_BASE, si_symbols);
-        } else if (strncmp(prefix_type, "custom", strlen(prefix_type)) == 0) {
+        } else if (strcasecmp(prefix_type, "custom") == 0) {
                 return format_bytes(outwalk, bytes, BINARY_BASE, custom_symbols);
         } else {
                 return format_bytes(outwalk, bytes, BINARY_BASE, iec_symbols);
         }
 }
 
+/*
+ * Determines whether remaining bytes are below given threshold.
+ *
+ */
+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__)
+static bool below_threshold(struct statfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
+#else
+static bool below_threshold(struct statvfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
+#endif
+        if (strcasecmp(threshold_type, "percentage_free") == 0) {
+                return 100.0 * (double)buf.f_bfree / (double)buf.f_blocks < low_threshold;
+        } else if (strcasecmp(threshold_type, "percentage_avail") == 0) {
+                return 100.0 * (double)buf.f_bavail / (double)buf.f_blocks < low_threshold;
+        } else if (strcasecmp(threshold_type, "bytes_free") == 0) {
+                return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold;
+        } else if (strcasecmp(threshold_type, "bytes_avail") == 0) {
+                return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold;
+        } else if (threshold_type[0] != '\0' && strncasecmp(threshold_type+1, "bytes_", strlen("bytes_")) == 0) {
+                uint64_t base = strcasecmp(prefix_type, "decimal") == 0 ? DECIMAL_BASE : BINARY_BASE;
+                double factor = 1;
+
+                switch (threshold_type[0]) {
+                case 'T':
+                case 't':
+                        factor *= base;
+                case 'G':
+                case 'g':
+                        factor *= base;
+                case 'M':
+                case 'm':
+                        factor *= base;
+                case 'K':
+                case 'k':
+                        factor *= base;
+                        break;
+                default:
+                        return false;
+                }
+
+                if (strcasecmp(threshold_type+1, "bytes_free") == 0) {
+                        return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold * factor;
+                } else if (strcasecmp(threshold_type+1, "bytes_avail") == 0) {
+                        return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold * factor;
+                }
+        }
+
+        return false;
+}
+
 /*
  * Does a statvfs and prints either free, used or total amounts of bytes in a
  * human readable manner.
  *
  */
-void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type) {
+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) {
         const char *walk;
         char *outwalk = buffer;
+        bool colorful_output = false;
 
         INSTANCE(path);
 
@@ -75,6 +127,29 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch
                 return;
 #endif
 
+        FILE *mntentfile = setmntent("/etc/mtab", "r");
+        struct mntent *m;
+        bool found = false;
+
+        while (NULL != (m = getmntent(mntentfile))) {
+                if (strcmp(m->mnt_dir, path) == 0) {
+                        found = true;
+                        break;
+                }
+        }
+        endmntent(mntentfile);
+
+        if (!found) {
+                *buffer = '\0';
+                OUTPUT_FULL_TEXT(buffer);
+                return;
+        }
+
+        if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) {
+                START_COLOR("color_bad");
+                colorful_output = true;
+        }
+
         for (walk = format; *walk != '\0'; walk++) {
                 if (*walk != '%') {
                         *(outwalk++) = *walk;
@@ -122,6 +197,9 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch
                 }
         }
 
+        if (colorful_output)
+                END_COLOR;
+
         *outwalk = '\0';
         OUTPUT_FULL_TEXT(buffer);
 }