X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fprint_disk_info.c;h=5f12e930074946943cf6b89edad02d0a8f1bb7e1;hb=0e4fd9ad4a4cc7d6c770134a077e91b02f6cd2f0;hp=b577d233ad56926e46418e0098ebc8c1f69a8be5;hpb=d6b8a4efc1c07f3bbf5f01f524986ecb496711a0;p=i3%2Fi3status diff --git a/src/print_disk_info.c b/src/print_disk_info.c index b577d23..5f12e93 100644 --- a/src/print_disk_info.c +++ b/src/print_disk_info.c @@ -1,113 +1,226 @@ -// vim:ts=8:expandtab +// vim:ts=4:sw=4:expandtab +#include #include #include #include #include #include +#include #include #include -#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || (__OpenBSD__) +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__) #include #include +#elif defined(__NetBSD__) +#else +#include #endif #include #include #include "i3status.h" -#define TERABYTE (1024ULL * 1024 * 1024 * 1024) -#define GIGABYTE (1024ULL * 1024 * 1024) -#define MEGABYTE (1024ULL * 1024) -#define KILOBYTE (1024ULL) +#define BINARY_BASE UINT64_C(1024) +#define DECIMAL_BASE UINT64_C(1000) + +#define MAX_EXPONENT 4 + +static const char *const iec_symbols[MAX_EXPONENT + 1] = {"", "Ki", "Mi", "Gi", "Ti"}; +static const char *const si_symbols[MAX_EXPONENT + 1] = {"", "k", "M", "G", "T"}; +static const char *const custom_symbols[MAX_EXPONENT + 1] = {"", "K", "M", "G", "T"}; /* - * Prints the given amount of bytes in a human readable manner. + * Formats bytes according to the given base and set of symbols. * */ -static int print_bytes_human(char *outwalk, uint64_t bytes) { - if (bytes > TERABYTE) - return sprintf(outwalk, "%.02f TB", (double)bytes / TERABYTE); - else if (bytes > GIGABYTE) - return sprintf(outwalk, "%.01f GB", (double)bytes / GIGABYTE); - else if (bytes > MEGABYTE) - return sprintf(outwalk, "%.01f MB", (double)bytes / MEGABYTE); - else if (bytes > KILOBYTE) - return sprintf(outwalk, "%.01f KB", (double)bytes / KILOBYTE); - else { - return sprintf(outwalk, "%.01f B", (double)bytes); - } +static int format_bytes(char *outwalk, uint64_t bytes, uint64_t base, const char *const symbols[]) { + double size = bytes; + int exponent = 0; + while (size >= base && exponent < MAX_EXPONENT) { + size /= base; + exponent += 1; + } + return sprintf(outwalk, "%.1f %sB", size, symbols[exponent]); } /* - * Does a statvfs and prints either free, used or total amounts of bytes in a - * human readable manner. + * Prints the given amount 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 *walk; - char *outwalk = buffer; - - INSTANCE(path); - -#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) - struct statfs buf; +static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_type) { + if (strcasecmp(prefix_type, "decimal") == 0) { + return format_bytes(outwalk, bytes, DECIMAL_BASE, si_symbols); + } 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); + } +} - if (statfs(path, &buf) == -1) - return; +/* + * Determines whether remaining bytes are below given threshold. + * + */ +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__) +static bool below_threshold(struct statfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) { #else - struct statvfs buf; - - if (statvfs(path, &buf) == -1) - return; +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; + } - for (walk = format; *walk != '\0'; walk++) { - if (*walk != '%') { - *(outwalk++) = *walk; - continue; - } + 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; + } + } - if (BEGINS_WITH(walk+1, "free")) { - outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree); - walk += strlen("free"); - } + return false; +} - if (BEGINS_WITH(walk+1, "used")) { - outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree)); - walk += strlen("used"); - } +/* + * 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 *format_below_threshold, const char *format_not_mounted, const char *prefix_type, const char *threshold_type, const double low_threshold) { + const char *selected_format = format; + const char *walk; + char *outwalk = buffer; + bool colorful_output = false; + bool mounted = false; - if (BEGINS_WITH(walk+1, "total")) { - outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks); - walk += strlen("total"); - } + INSTANCE(path); - if (BEGINS_WITH(walk+1, "avail")) { - outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail); - walk += strlen("avail"); - } +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__) + struct statfs buf; - if (BEGINS_WITH(walk+1, "percentage_free")) { - outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks); - walk += strlen("percentage_free"); - } + if (statfs(path, &buf) == -1) + return; - if (BEGINS_WITH(walk+1, "percentage_used_of_avail")) { - outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks); - walk += strlen("percentage_used_of_avail"); - } + mounted = true; +#elif defined(__NetBSD__) + struct statvfs buf; - if (BEGINS_WITH(walk+1, "percentage_used")) { - outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks); - walk += strlen("percentage_used"); - } + if (statvfs(path, &buf) == -1) + return; - if (BEGINS_WITH(walk+1, "percentage_avail")) { - outwalk += sprintf(outwalk, "%.01f%%", 100.0 * (double)buf.f_bavail / (double)buf.f_blocks); - walk += strlen("percentage_avail"); + mounted = true; +#else + struct statvfs buf; + + if (statvfs(path, &buf) == -1) { + /* If statvfs errors, e.g., due to the path not existing, + * we consider the device not mounted. */ + mounted = false; + } else { + char *sanitized = sstrdup(path); + if (strlen(sanitized) > 1 && sanitized[strlen(sanitized) - 1] == '/') + sanitized[strlen(sanitized) - 1] = '\0'; + FILE *mntentfile = setmntent("/etc/mtab", "r"); + if (mntentfile == NULL) { + mntentfile = setmntent("/proc/mounts", "r"); + } + if (mntentfile == NULL) { + fprintf(stderr, "i3status: files /etc/mtab and /proc/mounts aren't accessible\n"); + } else { + struct mntent *m; + + while ((m = getmntent(mntentfile)) != NULL) { + if (strcmp(m->mnt_dir, sanitized) == 0) { + mounted = true; + break; } + } + endmntent(mntentfile); } + free(sanitized); + } +#endif + + if (!mounted) { + if (format_not_mounted == NULL) + format_not_mounted = ""; + selected_format = format_not_mounted; + } else if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) { + START_COLOR("color_bad"); + colorful_output = true; + if (format_below_threshold != NULL) + selected_format = format_below_threshold; + } + + for (walk = selected_format; *walk != '\0'; walk++) { + if (*walk != '%') { + *(outwalk++) = *walk; + + } else if (BEGINS_WITH(walk + 1, "free")) { + outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type); + walk += strlen("free"); + + } else if (BEGINS_WITH(walk + 1, "used")) { + outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type); + walk += strlen("used"); + + } else if (BEGINS_WITH(walk + 1, "total")) { + outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type); + walk += strlen("total"); + + } else if (BEGINS_WITH(walk + 1, "avail")) { + outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type); + walk += strlen("avail"); + + } else if (BEGINS_WITH(walk + 1, "percentage_free")) { + outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks, pct_mark); + walk += strlen("percentage_free"); + + } else if (BEGINS_WITH(walk + 1, "percentage_used_of_avail")) { + outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks, pct_mark); + walk += strlen("percentage_used_of_avail"); + + } else if (BEGINS_WITH(walk + 1, "percentage_used")) { + outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks, pct_mark); + walk += strlen("percentage_used"); + + } else if (BEGINS_WITH(walk + 1, "percentage_avail")) { + outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)buf.f_bavail / (double)buf.f_blocks, pct_mark); + walk += strlen("percentage_avail"); + + } else { + *(outwalk++) = '%'; + } + } + + if (colorful_output) + END_COLOR; - *outwalk = '\0'; - OUTPUT_FULL_TEXT(buffer); + *outwalk = '\0'; + OUTPUT_FULL_TEXT(buffer); }