diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/print_disk_info.c | 12 | ||||
-rw-r--r-- | src/print_mem.c | 5 | ||||
-rw-r--r-- | src/print_path_exists.c | 1 | ||||
-rw-r--r-- | src/print_wireless_info.c | 140 |
4 files changed, 83 insertions, 75 deletions
diff --git a/src/print_disk_info.c b/src/print_disk_info.c index 5f510a0..4c23c7a 100644 --- a/src/print_disk_info.c +++ b/src/print_disk_info.c @@ -73,9 +73,9 @@ static bool below_threshold(struct statvfs buf, const char *prefix_type, const c } 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; + return (double)buf.f_frsize * (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; + return (double)buf.f_frsize * (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; @@ -187,10 +187,10 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch char string_percentage_used[STRING_SIZE]; char string_percentage_avail[STRING_SIZE]; - print_bytes_human(string_free, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type); - print_bytes_human(string_used, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type); - print_bytes_human(string_total, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type); - print_bytes_human(string_avail, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type); + print_bytes_human(string_free, (uint64_t)buf.f_frsize * (uint64_t)buf.f_bfree, prefix_type); + print_bytes_human(string_used, (uint64_t)buf.f_frsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type); + print_bytes_human(string_total, (uint64_t)buf.f_frsize * (uint64_t)buf.f_blocks, prefix_type); + print_bytes_human(string_avail, (uint64_t)buf.f_frsize * (uint64_t)buf.f_bavail, prefix_type); snprintf(string_percentage_free, STRING_SIZE, "%.01f%s", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks, pct_mark); snprintf(string_percentage_used_of_avail, STRING_SIZE, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks, pct_mark); snprintf(string_percentage_used, STRING_SIZE, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks, pct_mark); diff --git a/src/print_mem.c b/src/print_mem.c index cbe42a9..941c9d9 100644 --- a/src/print_mem.c +++ b/src/print_mem.c @@ -23,7 +23,7 @@ static const char *const iec_symbols[] = {"B", "KiB", "MiB", "GiB", "TiB"}; */ static int print_bytes_human(char *outwalk, unsigned long bytes, const char *unit, const int decimals) { double base = bytes; - int exponent = 0; + size_t exponent = 0; while (base >= BINARY_BASE && exponent < MAX_EXPONENT) { if (strcasecmp(unit, iec_symbols[exponent]) == 0) { break; @@ -86,7 +86,6 @@ void print_memory(yajl_gen json_gen, char *buffer, const char *format, const cha #if defined(linux) const char *selected_format = format; - const char *walk; const char *output_color = NULL; int unread_fields = 6; @@ -140,6 +139,8 @@ void print_memory(yajl_gen json_gen, char *buffer, const char *format, const cha ram_used = ram_total - ram_available; } else if (BEGINS_WITH(memory_used_method, "classical")) { ram_used = ram_total - ram_free - ram_buffers - ram_cached; + } else { + die("Unexpected value: memory_used_method = %s", memory_used_method); } if (threshold_degraded) { diff --git a/src/print_path_exists.c b/src/print_path_exists.c index 504eb6c..a57cc7b 100644 --- a/src/print_path_exists.c +++ b/src/print_path_exists.c @@ -1,6 +1,7 @@ // vim:ts=4:sw=4:expandtab #include <config.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <yajl/yajl_gen.h> #include <yajl/yajl_version.h> diff --git a/src/print_wireless_info.c b/src/print_wireless_info.c index 49421bb..cfb51b6 100644 --- a/src/print_wireless_info.c +++ b/src/print_wireless_info.c @@ -68,6 +68,8 @@ #include "i3status.h" +#define STRING_SIZE 30 + #define WIRELESS_INFO_FLAG_HAS_ESSID (1 << 0) #define WIRELESS_INFO_FLAG_HAS_QUALITY (1 << 1) #define WIRELESS_INFO_FLAG_HAS_SIGNAL (1 << 2) @@ -95,7 +97,7 @@ typedef struct { double frequency; } wireless_info_t; -#ifdef __linux__ +#if defined(__linux__) || defined(__FreeBSD__) // Like iw_print_bitrate, but without the dependency on libiw. static void print_bitrate(char *buffer, int buflen, int bitrate, const char *format_bitrate) { const int kilo = 1e3; @@ -118,7 +120,9 @@ static void print_bitrate(char *buffer, int buflen, int bitrate, const char *for } snprintf(buffer, buflen, format_bitrate, rate / divisor, scale); } +#endif +#ifdef __linux__ // Based on NetworkManager/src/platform/wifi/wifi-utils-nl80211.c static uint32_t nl80211_xbm_to_percent(int32_t xbm, int32_t divisor) { #define NOISE_FLOOR_DBM -90 @@ -406,6 +410,9 @@ error1: info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL; info->noise_level = u.req.info[0].isi_noise; info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE; + // isi_txmbps is specified in units of 500 Kbit/s + // Convert them to bit/s + info->bitrate = u.req.info[0].isi_txmbps * 500 * 1000; } return 1; @@ -517,7 +524,12 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, if (ipv6_address == NULL) { START_COLOR("color_bad"); outwalk += sprintf(outwalk, "%s", format_down); - goto out; + + END_COLOR; + free(ipv4_address); + free(ipv6_address); + OUTPUT_FULL_TEXT(buffer); + return; } else { prefer_ipv4 = false; } @@ -542,81 +554,75 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, } } - for (; *walk != '\0'; walk++) { - if (*walk != '%') { - *(outwalk++) = *walk; + char string_quality[STRING_SIZE]; + char string_signal[STRING_SIZE]; + char string_noise[STRING_SIZE]; + char string_essid[STRING_SIZE]; + char string_frequency[STRING_SIZE]; + char string_ip[STRING_SIZE]; + char string_bitrate[STRING_SIZE]; + + if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY) { + if (info.quality_max) + snprintf(string_quality, STRING_SIZE, format_quality, PERCENT_VALUE(info.quality, info.quality_max), pct_mark); + else + snprintf(string_quality, STRING_SIZE, "%d", info.quality); + } else { + snprintf(string_quality, STRING_SIZE, "?"); + } - } else if (BEGINS_WITH(walk + 1, "quality")) { - if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY) { - if (info.quality_max) - outwalk += sprintf(outwalk, format_quality, PERCENT_VALUE(info.quality, info.quality_max), pct_mark); - else - outwalk += sprintf(outwalk, "%d", info.quality); - } else { - *(outwalk++) = '?'; - } - walk += strlen("quality"); - - } else if (BEGINS_WITH(walk + 1, "signal")) { - if (info.flags & WIRELESS_INFO_FLAG_HAS_SIGNAL) { - if (info.signal_level_max) - outwalk += sprintf(outwalk, format_signal, PERCENT_VALUE(info.signal_level, info.signal_level_max), pct_mark); - else - outwalk += sprintf(outwalk, "%d dBm", info.signal_level); - } else { - *(outwalk++) = '?'; - } - walk += strlen("signal"); - - } else if (BEGINS_WITH(walk + 1, "noise")) { - if (info.flags & WIRELESS_INFO_FLAG_HAS_NOISE) { - if (info.noise_level_max) - outwalk += sprintf(outwalk, format_noise, PERCENT_VALUE(info.noise_level, info.noise_level_max), pct_mark); - else - outwalk += sprintf(outwalk, "%d dBm", info.noise_level); - } else { - *(outwalk++) = '?'; - } - walk += strlen("noise"); + if (info.flags & WIRELESS_INFO_FLAG_HAS_SIGNAL) { + if (info.signal_level_max) + snprintf(string_signal, STRING_SIZE, format_signal, PERCENT_VALUE(info.signal_level, info.signal_level_max), pct_mark); + else + snprintf(string_signal, STRING_SIZE, "%d dBm", info.signal_level); + } else { + snprintf(string_signal, STRING_SIZE, "?"); + } + + if (info.flags & WIRELESS_INFO_FLAG_HAS_NOISE) { + if (info.noise_level_max) + snprintf(string_noise, STRING_SIZE, format_noise, PERCENT_VALUE(info.noise_level, info.noise_level_max), pct_mark); + else + snprintf(string_noise, STRING_SIZE, "%d dBm", info.noise_level); + } else { + snprintf(string_noise, STRING_SIZE, "?"); + } - } else if (BEGINS_WITH(walk + 1, "essid")) { + char *tmp = string_essid; #ifdef IW_ESSID_MAX_SIZE - if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID) - maybe_escape_markup(info.essid, &outwalk); - else + if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID) + maybe_escape_markup(info.essid, &tmp); + else #endif - *(outwalk++) = '?'; - walk += strlen("essid"); - - } else if (BEGINS_WITH(walk + 1, "frequency")) { - if (info.flags & WIRELESS_INFO_FLAG_HAS_FREQUENCY) - outwalk += sprintf(outwalk, "%1.1f GHz", info.frequency / 1e9); - else - *(outwalk++) = '?'; - walk += strlen("frequency"); - - } else if (BEGINS_WITH(walk + 1, "ip")) { - outwalk += sprintf(outwalk, "%s", ip_address); - walk += strlen("ip"); - } -#ifdef __linux__ - else if (BEGINS_WITH(walk + 1, "bitrate")) { - char br_buffer[128]; + snprintf(string_essid, STRING_SIZE, "?"); - print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate, format_bitrate); + if (info.flags & WIRELESS_INFO_FLAG_HAS_FREQUENCY) + snprintf(string_frequency, STRING_SIZE, "%1.1f GHz", info.frequency / 1e9); + else + snprintf(string_frequency, STRING_SIZE, "?"); - outwalk += sprintf(outwalk, "%s", br_buffer); - walk += strlen("bitrate"); - } + snprintf(string_ip, STRING_SIZE, "%s", ip_address); + +#if defined(__linux__) || defined(__FreeBSD__) + print_bitrate(string_bitrate, sizeof(string_bitrate), info.bitrate, format_bitrate); #endif - else { - *(outwalk++) = '%'; - } - } -out: + placeholder_t placeholders[] = { + {.name = "%quality", .value = string_quality}, + {.name = "%signal", .value = string_signal}, + {.name = "%noise", .value = string_noise}, + {.name = "%essid", .value = string_essid}, + {.name = "%frequency", .value = string_frequency}, + {.name = "%ip", .value = string_ip}, + {.name = "%bitrate", .value = string_bitrate}}; + + const size_t num = sizeof(placeholders) / sizeof(placeholder_t); + buffer = format_placeholders(walk, &placeholders[0], num); + END_COLOR; free(ipv4_address); free(ipv6_address); OUTPUT_FULL_TEXT(buffer); + free(buffer); } |