The FreeBSD `ram_*' functions in `components/ram.c' were printing completely wrong results (especially `ram_perc'). This was fixed by changing the `active' and `npages' variables' data types from `long' to `int'.
`config.mk' was lacking the appropriate `X11INC' and `X11LIB' values to build on FreeBSD, so I added them (commented out by default). I also fixed the coding style across all files to match https://suckless.org/coding_style/ -- Kind regards, Christos
>From a1b363ffac89a73060f1fe693ceb921b9c8311fe Mon Sep 17 00:00:00 2001 From: Christos Margiolis <[email protected]> Date: Sat, 8 May 2021 23:12:27 +0300 Subject: [PATCH] Fixed bugs in FreeBSD's ram_* functions. Added appropriate FreeBSD config.mk options. Coding style fixes across all files. --- components/battery.c | 409 ++++++++++++++-------------- components/cpu.c | 274 +++++++++---------- components/disk.c | 2 +- components/entropy.c | 36 ++- components/ip.c | 10 +- components/keyboard_indicators.c | 8 +- components/keymap.c | 14 +- components/netspeeds.c | 238 ++++++++--------- components/num_files.c | 12 +- components/ram.c | 336 +++++++++++------------ components/run_command.c | 6 +- components/separator.c | 2 +- components/swap.c | 426 ++++++++++++++--------------- components/temperature.c | 98 ++++--- components/uptime.c | 6 +- components/volume.c | 356 ++++++++++++------------- components/wifi.c | 441 +++++++++++++++---------------- config.mk | 4 + slstatus.c | 35 +-- util.c | 6 +- 20 files changed, 1289 insertions(+), 1430 deletions(-) mode change 100644 => 100755 components/ram.c diff --git a/components/battery.c b/components/battery.c index f2b0f14..a3e2812 100644 --- a/components/battery.c +++ b/components/battery.c @@ -5,248 +5,229 @@ #include "../util.h" #if defined(__linux__) - #include <limits.h> - #include <stdint.h> - #include <unistd.h> - - static const char * - pick(const char *bat, const char *f1, const char *f2, char *path, - size_t length) - { - if (esnprintf(path, length, f1, bat) > 0 && - access(path, R_OK) == 0) { - return f1; - } - - if (esnprintf(path, length, f2, bat) > 0 && - access(path, R_OK) == 0) { - return f2; - } +#include <limits.h> +#include <stdint.h> +#include <unistd.h> + +static const char * +pick(const char *bat, const char *f1, const char *f2, char *path, + size_t length) +{ + if (esnprintf(path, length, f1, bat) > 0 + && access(path, R_OK) == 0) + return f1; + + if (esnprintf(path, length, f2, bat) > 0 + && access(path, R_OK) == 0) + return f2; + + return NULL; +} + +const char * +battery_perc(const char *bat) +{ + int perc; + char path[PATH_MAX]; + + if (esnprintf(path, sizeof(path), + "/sys/class/power_supply/%s/capacity", bat) < 0) + return NULL; + if (pscanf(path, "%d", &perc) != 1) + return NULL; + return bprintf("%d", perc); +} + +const char * +battery_state(const char *bat) +{ + static struct { + char *state; + char *symbol; + } map[] = { + { "Charging", "+" }, + { "Discharging", "-" }, + { "Full", "o" }, + }; + size_t i; + char path[PATH_MAX], state[12]; + + if (esnprintf(path, sizeof(path), + "/sys/class/power_supply/%s/status", bat) < 0) + return NULL; + if (pscanf(path, "%12s", state) != 1) return NULL; - } - const char * - battery_perc(const char *bat) - { - int perc; - char path[PATH_MAX]; + for (i = 0; i < LEN(map); i++) + if (!strcmp(map[i].state, state)) + break; - if (esnprintf(path, sizeof(path), - "/sys/class/power_supply/%s/capacity", bat) < 0) { - return NULL; - } - if (pscanf(path, "%d", &perc) != 1) { - return NULL; - } + return (i == LEN(map)) ? "?" : map[i].symbol; +} - return bprintf("%d", perc); - } +const char * +battery_remaining(const char *bat) +{ + uintmax_t charge_now, current_now, m, h; + double timeleft; + char path[PATH_MAX], state[12]; - const char * - battery_state(const char *bat) - { - static struct { - char *state; - char *symbol; - } map[] = { - { "Charging", "+" }, - { "Discharging", "-" }, - { "Full", "o" }, - }; - size_t i; - char path[PATH_MAX], state[12]; - - if (esnprintf(path, sizeof(path), - "/sys/class/power_supply/%s/status", bat) < 0) { - return NULL; - } - if (pscanf(path, "%12s", state) != 1) { - return NULL; - } - - for (i = 0; i < LEN(map); i++) { - if (!strcmp(map[i].state, state)) { - break; - } - } - return (i == LEN(map)) ? "?" : map[i].symbol; - } + if (esnprintf(path, sizeof(path), + "/sys/class/power_supply/%s/status", bat) < 0) + return NULL; + if (pscanf(path, "%12s", state) != 1) + return NULL; - const char * - battery_remaining(const char *bat) - { - uintmax_t charge_now, current_now, m, h; - double timeleft; - char path[PATH_MAX], state[12]; + if (!pick(bat, "/sys/class/power_supply/%s/charge_now", + "/sys/class/power_supply/%s/energy_now", path, sizeof(path)) + || pscanf(path, "%ju", &charge_now) < 0) + return NULL; - if (esnprintf(path, sizeof(path), - "/sys/class/power_supply/%s/status", bat) < 0) { + if (!strcmp(state, "Discharging")) { + if (!pick(bat, "/sys/class/power_supply/%s/current_now", + "/sys/class/power_supply/%s/power_now", path, sizeof(path)) + || pscanf(path, "%ju", ¤t_now) < 0) return NULL; - } - if (pscanf(path, "%12s", state) != 1) { - return NULL; - } - if (!pick(bat, "/sys/class/power_supply/%s/charge_now", - "/sys/class/power_supply/%s/energy_now", path, - sizeof(path)) || - pscanf(path, "%ju", &charge_now) < 0) { + if (current_now == 0) return NULL; - } - - if (!strcmp(state, "Discharging")) { - if (!pick(bat, "/sys/class/power_supply/%s/current_now", - "/sys/class/power_supply/%s/power_now", path, - sizeof(path)) || - pscanf(path, "%ju", ¤t_now) < 0) { - return NULL; - } - - if (current_now == 0) { - return NULL; - } - timeleft = (double)charge_now / (double)current_now; - h = timeleft; - m = (timeleft - (double)h) * 60; + timeleft = (double)charge_now / (double)current_now; + h = timeleft; + m = (timeleft - (double)h) * 60; - return bprintf("%juh %jum", h, m); - } - - return ""; + return bprintf("%juh %jum", h, m); } + + return ""; +} #elif defined(__OpenBSD__) - #include <fcntl.h> - #include <machine/apmvar.h> - #include <sys/ioctl.h> - #include <unistd.h> - - static int - load_apm_power_info(struct apm_power_info *apm_info) - { - int fd; - - fd = open("/dev/apm", O_RDONLY); - if (fd < 0) { - warn("open '/dev/apm':"); - return 0; - } - - memset(apm_info, 0, sizeof(struct apm_power_info)); - if (ioctl(fd, APM_IOC_GETPOWER, apm_info) < 0) { - warn("ioctl 'APM_IOC_GETPOWER':"); - close(fd); - return 0; - } - return close(fd), 1; +#include <fcntl.h> +#include <machine/apmvar.h> +#include <sys/ioctl.h> +#include <unistd.h> + +static int +load_apm_power_info(struct apm_power_info *apm_info) +{ + int fd; + + if ((fd = open("/dev/apm", O_RDONLY)) == -1) + warn("open '/dev/apm':"); + return 0; } - - const char * - battery_perc(const char *unused) - { - struct apm_power_info apm_info; - - if (load_apm_power_info(&apm_info)) { - return bprintf("%d", apm_info.battery_life); - } - - return NULL; + memset(apm_info, 0, sizeof(struct apm_power_info)); + if (ioctl(fd, APM_IOC_GETPOWER, apm_info) < 0) { + warn("ioctl 'APM_IOC_GETPOWER':"); + close(fd); + return 0; } - const char * - battery_state(const char *unused) - { - struct { - unsigned int state; - char *symbol; - } map[] = { - { APM_AC_ON, "+" }, - { APM_AC_OFF, "-" }, - }; - struct apm_power_info apm_info; - size_t i; - - if (load_apm_power_info(&apm_info)) { - for (i = 0; i < LEN(map); i++) { - if (map[i].state == apm_info.ac_state) { - break; - } - } - return (i == LEN(map)) ? "?" : map[i].symbol; - } - - return NULL; + return close(fd), 1; +} + +const char * +battery_perc(const char *unused) +{ + struct apm_power_info apm_info; + + if (load_apm_power_info(&apm_info)) + return bprintf("%d", apm_info.battery_life); + + return NULL; +} + +const char * +battery_state(const char *unused) +{ + struct { + unsigned int state; + char *symbol; + } map[] = { + { APM_AC_ON, "+" }, + { APM_AC_OFF, "-" }, + }; + struct apm_power_info apm_info; + size_t i; + + if (load_apm_power_info(&apm_info)) { + for (i = 0; i < LEN(map); i++) + if (map[i].state == apm_info.ac_state) + break; + return (i == LEN(map)) ? "?" : map[i].symbol; } - const char * - battery_remaining(const char *unused) - { - struct apm_power_info apm_info; - - if (load_apm_power_info(&apm_info)) { - if (apm_info.ac_state != APM_AC_ON) { - return bprintf("%uh %02um", - apm_info.minutes_left / 60, - apm_info.minutes_left % 60); - } else { - return ""; - } - } - - return NULL; + return NULL; +} + +const char * +battery_remaining(const char *unused) +{ + struct apm_power_info apm_info; + + if (load_apm_power_info(&apm_info)) { + if (apm_info.ac_state != APM_AC_ON) + return bprintf("%uh %02um", + apm_info.minutes_left / 60, + apm_info.minutes_left % 60); + else + return ""; } + + return NULL; +} #elif defined(__FreeBSD__) - #include <sys/sysctl.h> +#include <sys/sysctl.h> - const char * - battery_perc(const char *unused) - { - int cap; - size_t len; +const char * +battery_perc(const char *unused) +{ + int cap; + size_t len; - len = sizeof(cap); - if (sysctlbyname("hw.acpi.battery.life", &cap, &len, NULL, 0) == -1 - || !len) - return NULL; + len = sizeof(cap); + if (sysctlbyname("hw.acpi.battery.life", &cap, &len, NULL, 0) == -1 + || !len) + return NULL; - return bprintf("%d", cap); - } + return bprintf("%d", cap); +} - const char * - battery_state(const char *unused) - { - int state; - size_t len; +const char * +battery_state(const char *unused) +{ + int state; + size_t len; - len = sizeof(state); - if (sysctlbyname("hw.acpi.battery.state", &state, &len, NULL, 0) == -1 - || !len) - return NULL; + len = sizeof(state); + if (sysctlbyname("hw.acpi.battery.state", &state, &len, NULL, 0) == -1 + || !len) + return NULL; - switch(state) { - case 0: - case 2: - return "+"; - case 1: - return "-"; - default: - return "?"; - } + switch(state) { + case 0: /* FALLTHROUGH */ + case 2: + return "+"; + case 1: + return "-"; + default: + return "?"; } +} + +const char * +battery_remaining(const char *unused) +{ + int rem; + size_t len; + + len = sizeof(rem); + if (sysctlbyname("hw.acpi.battery.time", &rem, &len, NULL, 0) == -1 + || !len + || rem == -1) + return NULL; - const char * - battery_remaining(const char *unused) - { - int rem; - size_t len; - - len = sizeof(rem); - if (sysctlbyname("hw.acpi.battery.time", &rem, &len, NULL, 0) == -1 - || !len - || rem == -1) - return NULL; - - return bprintf("%uh %02um", rem / 60, rem % 60); - } + return bprintf("%uh %02um", rem / 60, rem % 60); +} #endif diff --git a/components/cpu.c b/components/cpu.c index 9e28003..59aadf0 100644 --- a/components/cpu.c +++ b/components/cpu.c @@ -6,159 +6,143 @@ #include "../util.h" #if defined(__linux__) - const char * - cpu_freq(void) - { - uintmax_t freq; - - /* in kHz */ - if (pscanf("/sys/devices/system/cpu/cpu0/cpufreq/" - "scaling_cur_freq", "%ju", &freq) != 1) { - return NULL; - } - - return fmt_human(freq * 1000, 1000); +const char * +cpu_freq(void) +{ + uintmax_t freq; + + /* in kHz */ + if (pscanf("/sys/devices/system/cpu/cpu0/cpufreq/" + "scaling_cur_freq", "%ju", &freq) != 1) + return NULL; + + return fmt_human(freq * 1000, 1000); +} + +const char * +cpu_perc(void) +{ + static long double a[7]; + long double b[7], sum; + + memcpy(b, a, sizeof(b)); + /* cpu user nice system idle iowait irq softirq */ + if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf", + &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6]) != 7) + return NULL; + if (b[0] == 0) + return NULL; + + sum = (b[0] + b[1] + b[2] + b[3] + b[4] + b[5] + b[6]) - + (a[0] + a[1] + a[2] + a[3] + a[4] + a[5] + a[6]); + + if (sum == 0) + return NULL; + + return bprintf("%d", (int)(100 * ((b[0] + b[1] + b[2] + b[5] + b[6]) - + (a[0] + a[1] + a[2] + a[5] + a[6])) / sum)); +} +#elif defined(__OpenBSD__) +#include <sys/param.h> +#include <sys/sched.h> +#include <sys/sysctl.h> + +const char * +cpu_freq(void) +{ + int freq, mib[2]; + size_t size; + + mib[0] = CTL_HW; + mib[1] = HW_CPUSPEED; + + size = sizeof(freq); + /* in MHz */ + if (sysctl(mib, 2, &freq, &size, NULL, 0) < 0) { + warn("sysctl 'HW_CPUSPEED':"); + return NULL; } - const char * - cpu_perc(void) - { - static long double a[7]; - long double b[7], sum; - - memcpy(b, a, sizeof(b)); - /* cpu user nice system idle iowait irq softirq */ - if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf", - &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6]) - != 7) { - return NULL; - } - if (b[0] == 0) { - return NULL; - } - - sum = (b[0] + b[1] + b[2] + b[3] + b[4] + b[5] + b[6]) - - (a[0] + a[1] + a[2] + a[3] + a[4] + a[5] + a[6]); - - if (sum == 0) { - return NULL; - } - - return bprintf("%d", (int)(100 * - ((b[0] + b[1] + b[2] + b[5] + b[6]) - - (a[0] + a[1] + a[2] + a[5] + a[6])) / sum)); + return fmt_human(freq * 1E6, 1000); +} + +const char * +cpu_perc(void) +{ + int mib[2]; + static uintmax_t a[CPUSTATES]; + uintmax_t b[CPUSTATES], sum; + size_t size; + + mib[0] = CTL_KERN; + mib[1] = KERN_CPTIME; + + size = sizeof(a); + memcpy(b, a, sizeof(b)); + if (sysctl(mib, 2, &a, &size, NULL, 0) < 0) { + warn("sysctl 'KERN_CPTIME':"); + return NULL; } -#elif defined(__OpenBSD__) - #include <sys/param.h> - #include <sys/sched.h> - #include <sys/sysctl.h> - - const char * - cpu_freq(void) - { - int freq, mib[2]; - size_t size; - - mib[0] = CTL_HW; - mib[1] = HW_CPUSPEED; - - size = sizeof(freq); + if (b[0] == 0) + return NULL; - /* in MHz */ - if (sysctl(mib, 2, &freq, &size, NULL, 0) < 0) { - warn("sysctl 'HW_CPUSPEED':"); - return NULL; - } + sum = (a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR] + a[CP_IDLE]) - + (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR] + b[CP_IDLE]); - return fmt_human(freq * 1E6, 1000); - } + if (sum == 0) + return NULL; - const char * - cpu_perc(void) - { - int mib[2]; - static uintmax_t a[CPUSTATES]; - uintmax_t b[CPUSTATES], sum; - size_t size; - - mib[0] = CTL_KERN; - mib[1] = KERN_CPTIME; - - size = sizeof(a); - - memcpy(b, a, sizeof(b)); - if (sysctl(mib, 2, &a, &size, NULL, 0) < 0) { - warn("sysctl 'KERN_CPTIME':"); - return NULL; - } - if (b[0] == 0) { - return NULL; - } - - sum = (a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR] + a[CP_IDLE]) - - (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR] + b[CP_IDLE]); - - if (sum == 0) { - return NULL; - } - - return bprintf("%d", 100 * - ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + - a[CP_INTR]) - - (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + - b[CP_INTR])) / sum); - } + return bprintf("%d", 100 * ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + + a[CP_INTR]) - (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + + b[CP_INTR])) / sum); +} #elif defined(__FreeBSD__) - #include <sys/param.h> - #include <sys/sysctl.h> - #include <devstat.h> - - const char * - cpu_freq(void) - { - int freq; - size_t size; - - size = sizeof(freq); - /* in MHz */ - if (sysctlbyname("hw.clockrate", &freq, &size, NULL, 0) == -1 - || !size) { - warn("sysctlbyname 'hw.clockrate':"); - return NULL; - } - - return fmt_human(freq * 1E6, 1000); +#include <sys/param.h> +#include <sys/sysctl.h> +#include <devstat.h> + +const char * +cpu_freq(void) +{ + int freq; + size_t size; + + size = sizeof(freq); + /* in MHz */ + if (sysctlbyname("hw.clockrate", &freq, &size, NULL, 0) == -1 + || !size) { + warn("sysctlbyname 'hw.clockrate':"); + return NULL; } - const char * - cpu_perc(void) - { - size_t size; - static long a[CPUSTATES]; - long b[CPUSTATES], sum; - - size = sizeof(a); - memcpy(b, a, sizeof(b)); - if (sysctlbyname("kern.cp_time", &a, &size, NULL, 0) == -1 - || !size) { - warn("sysctlbyname 'kern.cp_time':"); - return NULL; - } - if (b[0] == 0) { - return NULL; - } - - sum = (a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR] + a[CP_IDLE]) - - (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR] + b[CP_IDLE]); - - if (sum == 0) { - return NULL; - } - - return bprintf("%d", 100 * - ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + - a[CP_INTR]) - - (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + - b[CP_INTR])) / sum); + return fmt_human(freq * 1E6, 1000); +} + +const char * +cpu_perc(void) +{ + size_t size; + static long a[CPUSTATES]; + long b[CPUSTATES], sum; + + size = sizeof(a); + memcpy(b, a, sizeof(b)); + if (sysctlbyname("kern.cp_time", &a, &size, NULL, 0) == -1 + || !size) { + warn("sysctlbyname 'kern.cp_time':"); + return NULL; } + if (b[0] == 0) + return NULL; + + sum = (a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR] + a[CP_IDLE]) - + (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR] + b[CP_IDLE]); + + if (sum == 0) + return NULL; + + return bprintf("%d", 100 * ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + + a[CP_INTR]) - (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + + b[CP_INTR])) / sum); +} #endif diff --git a/components/disk.c b/components/disk.c index 15a221b..117fab2 100644 --- a/components/disk.c +++ b/components/disk.c @@ -28,7 +28,7 @@ disk_perc(const char *path) } return bprintf("%d", (int)(100 * - (1.0f - ((float)fs.f_bavail / (float)fs.f_blocks)))); + (1.0f - ((float)fs.f_bavail / (float)fs.f_blocks)))); } const char * diff --git a/components/entropy.c b/components/entropy.c index 2a485de..c221d19 100644 --- a/components/entropy.c +++ b/components/entropy.c @@ -1,27 +1,25 @@ /* See LICENSE file for copyright and license details. */ #if defined(__linux__) - #include <stdint.h> - #include <stdio.h> +#include <stdint.h> +#include <stdio.h> - #include "../util.h" +#include "../util.h" - const char * - entropy(void) - { - uintmax_t num; +const char * +entropy(void) +{ + uintmax_t num; - if (pscanf("/proc/sys/kernel/random/entropy_avail", "%ju", &num) - != 1) { - return NULL; - } + if (pscanf("/proc/sys/kernel/random/entropy_avail", "%ju", &num) != 1) + return NULL; - return bprintf("%ju", num); - } + return bprintf("%ju", num); +} #elif defined(__OpenBSD__) | defined(__FreeBSD__) - const char * - entropy(void) - { - /* Unicode Character 'INFINITY' (U+221E) */ - return "\xe2\x88\x9e"; - } +const char * +entropy(void) +{ + /* Unicode Character 'INFINITY' (U+221E) */ + return "\xe2\x88\x9e"; +} #endif diff --git a/components/ip.c b/components/ip.c index 70724eb..8293a52 100644 --- a/components/ip.c +++ b/components/ip.c @@ -26,13 +26,12 @@ ip(const char *interface, unsigned short sa_family) } for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { - if (!ifa->ifa_addr) { + if (!ifa->ifa_addr) continue; - } s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), - host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); - if (!strcmp(ifa->ifa_name, interface) && - (ifa->ifa_addr->sa_family == sa_family)) { + host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); + if (!strcmp(ifa->ifa_name, interface) + && (ifa->ifa_addr->sa_family == sa_family)) { freeifaddrs(ifaddr); if (s != 0) { warn("getnameinfo: %s", gai_strerror(s)); @@ -41,7 +40,6 @@ ip(const char *interface, unsigned short sa_family) return bprintf("%s", host); } } - freeifaddrs(ifaddr); return NULL; diff --git a/components/keyboard_indicators.c b/components/keyboard_indicators.c index b35eba1..6902db1 100644 --- a/components/keyboard_indicators.c +++ b/components/keyboard_indicators.c @@ -32,16 +32,14 @@ keyboard_indicators(const char *fmt) fmtlen = strnlen(fmt, 4); for (i = n = 0; i < fmtlen; i++) { key = tolower(fmt[i]); - if (key != 'c' && key != 'n') { + if (key != 'c' && key != 'n') continue; - } togglecase = (i + 1 >= fmtlen || fmt[i + 1] != '?'); isset = (state.led_mask & (1 << (key == 'n'))); - if (togglecase) { + if (togglecase) buf[n++] = isset ? toupper(key) : key; - } else if (isset) { + else if (isset) buf[n++] = fmt[i]; - } } buf[n] = 0; return buf; diff --git a/components/keymap.c b/components/keymap.c index ddf7a15..1ae9c78 100644 --- a/components/keymap.c +++ b/components/keymap.c @@ -14,11 +14,9 @@ valid_layout_or_variant(char *sym) /* invalid symbols from xkb rules config */ static const char *invalid[] = { "evdev", "inet", "pc", "base" }; - for (i = 0; i < LEN(invalid); i++) { - if (!strncmp(sym, invalid[i], strlen(invalid[i]))) { + for (i = 0; i < LEN(invalid); i++) + if (!strncmp(sym, invalid[i], strlen(invalid[i]))) return 0; - } - } return 1; } @@ -32,12 +30,11 @@ get_layout(char *syms, int grp_num) layout = NULL; tok = strtok(syms, "+:"); for (grp = 0; tok && grp <= grp_num; tok = strtok(NULL, "+:")) { - if (!valid_layout_or_variant(tok)) { + if (!valid_layout_or_variant(tok)) continue; - } else if (strlen(tok) == 1 && isdigit(tok[0])) { + else if (strlen(tok) == 1 && isdigit(tok[0])) /* ignore :2, :3, :4 (additional layout groups) */ continue; - } layout = tok; grp++; } @@ -79,9 +76,8 @@ keymap(void) XFree(symbols); end: XkbFreeKeyboard(desc, XkbSymbolsNameMask, 1); - if (XCloseDisplay(dpy)) { + if (XCloseDisplay(dpy)) warn("XCloseDisplay: Failed to close display"); - } return layout; } diff --git a/components/netspeeds.c b/components/netspeeds.c index 0029177..3491ace 100644 --- a/components/netspeeds.c +++ b/components/netspeeds.c @@ -5,135 +5,119 @@ #include "../util.h" #if defined(__linux__) - #include <stdint.h> - - const char * - netspeed_rx(const char *interface) - { - uintmax_t oldrxbytes; - static uintmax_t rxbytes; - extern const unsigned int interval; - char path[PATH_MAX]; - - oldrxbytes = rxbytes; - - if (esnprintf(path, sizeof(path), - "/sys/class/net/%s/statistics/rx_bytes", - interface) < 0) { - return NULL; - } - if (pscanf(path, "%ju", &rxbytes) != 1) { - return NULL; - } - if (oldrxbytes == 0) { - return NULL; - } - - return fmt_human((rxbytes - oldrxbytes) * 1000 / interval, - 1024); +#include <stdint.h> + +const char * +netspeed_rx(const char *interface) +{ + uintmax_t oldrxbytes; + static uintmax_t rxbytes; + extern const unsigned int interval; + char path[PATH_MAX]; + + oldrxbytes = rxbytes; + + if (esnprintf(path, sizeof(path), "/sys/class/net/%s/statistics/rx_bytes", + interface) < 0) + return NULL; + if (pscanf(path, "%ju", &rxbytes) != 1) + return NULL; + if (oldrxbytes == 0) + return NULL; + + return fmt_human((rxbytes - oldrxbytes) * 1000 / interval, 1024); +} + +const char * +netspeed_tx(const char *interface) +{ + uintmax_t oldtxbytes; + static uintmax_t txbytes; + extern const unsigned int interval; + char path[PATH_MAX]; + + oldtxbytes = txbytes; + + if (esnprintf(path, sizeof(path), "/sys/class/net/%s/statistics/tx_bytes", + interface) < 0) + return NULL; + if (pscanf(path, "%ju", &txbytes) != 1) + return NULL; + if (oldtxbytes == 0) + return NULL; + + return fmt_human((txbytes - oldtxbytes) * 1000 / interval, 1024); +} +#elif defined(__OpenBSD__) | defined(__FreeBSD__) +#include <string.h> +#include <ifaddrs.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <net/if.h> + +const char * +netspeed_rx(const char *interface) +{ + struct ifaddrs *ifal, *ifa; + struct if_data *ifd; + uintmax_t oldrxbytes; + static uintmax_t rxbytes; + extern const unsigned int interval; + int if_ok = 0; + + oldrxbytes = rxbytes; + + if (getifaddrs(&ifal) == -1) { + warn("getifaddrs failed"); + return NULL; } - - const char * - netspeed_tx(const char *interface) - { - uintmax_t oldtxbytes; - static uintmax_t txbytes; - extern const unsigned int interval; - char path[PATH_MAX]; - - oldtxbytes = txbytes; - - if (esnprintf(path, sizeof(path), - "/sys/class/net/%s/statistics/tx_bytes", - interface) < 0) { - return NULL; - } - if (pscanf(path, "%ju", &txbytes) != 1) { - return NULL; - } - if (oldtxbytes == 0) { - return NULL; - } - - return fmt_human((txbytes - oldtxbytes) * 1000 / interval, - 1024); + rxbytes = 0; + for (ifa = ifal; ifa; ifa = ifa->ifa_next) { + if (!strcmp(ifa->ifa_name, interface) + && (ifd = (struct if_data *)ifa->ifa_data)) + rxbytes += ifd->ifi_ibytes, if_ok = 1; } -#elif defined(__OpenBSD__) | defined(__FreeBSD__) - #include <string.h> - #include <ifaddrs.h> - #include <sys/types.h> - #include <sys/socket.h> - #include <net/if.h> - - const char * - netspeed_rx(const char *interface) - { - struct ifaddrs *ifal, *ifa; - struct if_data *ifd; - uintmax_t oldrxbytes; - static uintmax_t rxbytes; - extern const unsigned int interval; - int if_ok = 0; - - oldrxbytes = rxbytes; - - if (getifaddrs(&ifal) == -1) { - warn("getifaddrs failed"); - return NULL; - } - rxbytes = 0; - for (ifa = ifal; ifa; ifa = ifa->ifa_next) { - if (!strcmp(ifa->ifa_name, interface) && - (ifd = (struct if_data *)ifa->ifa_data)) { - rxbytes += ifd->ifi_ibytes, if_ok = 1; - } - } - freeifaddrs(ifal); - if (!if_ok) { - warn("reading 'if_data' failed"); - return NULL; - } - if (oldrxbytes == 0) { - return NULL; - } - - return fmt_human((rxbytes - oldrxbytes) * 1000 / interval, - 1024); + freeifaddrs(ifal); + if (!if_ok) { + warn("reading 'if_data' failed"); + return NULL; } - - const char * - netspeed_tx(const char *interface) - { - struct ifaddrs *ifal, *ifa; - struct if_data *ifd; - uintmax_t oldtxbytes; - static uintmax_t txbytes; - extern const unsigned int interval; - int if_ok = 0; - - oldtxbytes = txbytes; - - if (getifaddrs(&ifal) == -1) { - warn("getifaddrs failed"); - return NULL; - } - txbytes = 0; - for (ifa = ifal; ifa; ifa = ifa->ifa_next) { - if (!strcmp(ifa->ifa_name, interface) && - (ifd = (struct if_data *)ifa->ifa_data)) { - txbytes += ifd->ifi_obytes, if_ok = 1; - } - } - freeifaddrs(ifal); - if (!if_ok) { - warn("reading 'if_data' failed"); - return NULL; - } - if (oldtxbytes == 0) { - return NULL; - } - - return fmt_human((txbytes - oldtxbytes) * 1000 / interval, - 1024); + if (oldrxbytes == 0) + return NULL; + + return fmt_human((rxbytes - oldrxbytes) * 1000 / interval, 1024); +} + +const char * +netspeed_tx(const char *interface) +{ + struct ifaddrs *ifal, *ifa; + struct if_data *ifd; + uintmax_t oldtxbytes; + static uintmax_t txbytes; + extern const unsigned int interval; + int if_ok = 0; + + oldtxbytes = txbytes; + + if (getifaddrs(&ifal) == -1) { + warn("getifaddrs failed"); + return NULL; } + txbytes = 0; + for (ifa = ifal; ifa; ifa = ifa->ifa_next) { + if (!strcmp(ifa->ifa_name, interface) + && (ifd = (struct if_data *)ifa->ifa_data)) + txbytes += ifd->ifi_obytes, if_ok = 1; + } + freeifaddrs(ifal); + if (!if_ok) { + warn("reading 'if_data' failed"); + return NULL; + } + if (oldtxbytes == 0) + return NULL; + + return fmt_human((txbytes - oldtxbytes) * 1000 / interval, 1024); +} #endif diff --git a/components/num_files.c b/components/num_files.c index fb55df9..6458cca 100644 --- a/components/num_files.c +++ b/components/num_files.c @@ -9,23 +9,21 @@ const char * num_files(const char *path) { struct dirent *dp; - DIR *fd; + DIR *dir; int num; - if (!(fd = opendir(path))) { + if (!(dir = opendir(path))) { warn("opendir '%s':", path); return NULL; } num = 0; - while ((dp = readdir(fd))) { - if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) { + while ((dp = readdir(dir))) { + if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) continue; /* skip self and parent */ - } num++; } - - closedir(fd); + closedir(dir); return bprintf("%d", num); } diff --git a/components/ram.c b/components/ram.c old mode 100644 new mode 100755 index 47e6fda..8b077f3 --- a/components/ram.c +++ b/components/ram.c @@ -4,219 +4,195 @@ #include "../util.h" #if defined(__linux__) - #include <stdint.h> - - const char * - ram_free(void) - { - uintmax_t free; - - if (pscanf("/proc/meminfo", - "MemTotal: %ju kB\n" - "MemFree: %ju kB\n" - "MemAvailable: %ju kB\n", - &free, &free, &free) != 3) { - return NULL; - } - - return fmt_human(free * 1024, 1024); - } +#include <stdint.h> - const char * - ram_perc(void) - { - uintmax_t total, free, buffers, cached; - - if (pscanf("/proc/meminfo", - "MemTotal: %ju kB\n" - "MemFree: %ju kB\n" - "MemAvailable: %ju kB\n" - "Buffers: %ju kB\n" - "Cached: %ju kB\n", - &total, &free, &buffers, &buffers, &cached) != 5) { - return NULL; - } - - if (total == 0) { - return NULL; - } - - return bprintf("%d", 100 * ((total - free) - (buffers + cached)) - / total); - } +const char * +ram_free(void) +{ + uintmax_t free; - const char * - ram_total(void) - { - uintmax_t total; + if (pscanf("/proc/meminfo", "MemTotal: %ju kB\n" "MemFree: %ju kB\n" + "MemAvailable: %ju kB\n", &free, &free, &free) != 3) + return NULL; - if (pscanf("/proc/meminfo", "MemTotal: %ju kB\n", &total) - != 1) { - return NULL; - } + return fmt_human(free * 1024, 1024); +} - return fmt_human(total * 1024, 1024); - } - - const char * - ram_used(void) - { - uintmax_t total, free, buffers, cached; - - if (pscanf("/proc/meminfo", - "MemTotal: %ju kB\n" - "MemFree: %ju kB\n" - "MemAvailable: %ju kB\n" - "Buffers: %ju kB\n" - "Cached: %ju kB\n", - &total, &free, &buffers, &buffers, &cached) != 5) { - return NULL; - } - - return fmt_human((total - free - buffers - cached) * 1024, - 1024); - } -#elif defined(__OpenBSD__) - #include <stdlib.h> - #include <sys/sysctl.h> - #include <sys/types.h> - #include <unistd.h> +const char * +ram_perc(void) +{ + uintmax_t total, free, buffers, cached; - #define LOG1024 10 - #define pagetok(size, pageshift) (size_t)(size << (pageshift - LOG1024)) + if (pscanf("/proc/meminfo", "MemTotal: %ju kB\n" "MemFree: %ju kB\n" + "MemAvailable: %ju kB\n" "Buffers: %ju kB\n" "Cached: %ju kB\n", + &total, &free, &buffers, &buffers, &cached) != 5) + return NULL; - inline int - load_uvmexp(struct uvmexp *uvmexp) - { - int uvmexp_mib[] = {CTL_VM, VM_UVMEXP}; - size_t size; + if (total == 0) + return NULL; - size = sizeof(*uvmexp); + return bprintf("%d", 100 * ((total - free) - (buffers + cached)) / total); +} - if (sysctl(uvmexp_mib, 2, uvmexp, &size, NULL, 0) >= 0) { - return 1; - } +const char * +ram_total(void) +{ + uintmax_t total; - return 0; - } + if (pscanf("/proc/meminfo", "MemTotal: %ju kB\n", &total) != 1) + return NULL; - const char * - ram_free(void) - { - struct uvmexp uvmexp; - int free_pages; + return fmt_human(total * 1024, 1024); +} - if (load_uvmexp(&uvmexp)) { - free_pages = uvmexp.npages - uvmexp.active; - return fmt_human(pagetok(free_pages, uvmexp.pageshift) * - 1024, 1024); - } +const char * +ram_used(void) +{ + uintmax_t total, free, buffers, cached; + if (pscanf("/proc/meminfo", "MemTotal: %ju kB\n" "MemFree: %ju kB\n" + "MemAvailable: %ju kB\n" "Buffers: %ju kB\n" "Cached: %ju kB\n", + &total, &free, &buffers, &buffers, &cached) != 5) return NULL; + + return fmt_human((total - free - buffers - cached) * 1024, 1024); +} +#elif defined(__OpenBSD__) +#include <stdlib.h> +#include <sys/sysctl.h> +#include <sys/types.h> +#include <unistd.h> + +#define LOG1024 10 +#define pagetok(size, pageshift) (size_t)(size << (pageshift - LOG1024)) + +inline int +load_uvmexp(struct uvmexp *uvmexp) +{ + int uvmexp_mib[] = {CTL_VM, VM_UVMEXP}; + size_t size; + + size = sizeof(*uvmexp); + if (sysctl(uvmexp_mib, 2, uvmexp, &size, NULL, 0) >= 0) + return 1; + + return 0; +} + +const char * +ram_free(void) +{ + struct uvmexp uvmexp; + int free_pages; + + if (load_uvmexp(&uvmexp)) { + free_pages = uvmexp.npages - uvmexp.active; + return fmt_human(pagetok(free_pages, uvmexp.pageshift) * 1024, 1024); } - const char * - ram_perc(void) - { - struct uvmexp uvmexp; - int percent; + return NULL; +} - if (load_uvmexp(&uvmexp)) { - percent = uvmexp.active * 100 / uvmexp.npages; - return bprintf("%d", percent); - } +const char * +ram_perc(void) +{ + struct uvmexp uvmexp; + int percent; - return NULL; + if (load_uvmexp(&uvmexp)) { + percent = uvmexp.active * 100 / uvmexp.npages; + return bprintf("%d", percent); } - const char * - ram_total(void) - { - struct uvmexp uvmexp; + return NULL; +} - if (load_uvmexp(&uvmexp)) { - return fmt_human(pagetok(uvmexp.npages, - uvmexp.pageshift) * 1024, - 1024); - } +const char * +ram_total(void) +{ + struct uvmexp uvmexp; - return NULL; - } + if (load_uvmexp(&uvmexp)) + return fmt_human(pagetok(uvmexp.npages, + uvmexp.pageshift) * 1024, 1024); - const char * - ram_used(void) - { - struct uvmexp uvmexp; + return NULL; +} - if (load_uvmexp(&uvmexp)) { - return fmt_human(pagetok(uvmexp.active, - uvmexp.pageshift) * 1024, - 1024); - } +const char * +ram_used(void) +{ + struct uvmexp uvmexp; - return NULL; - } + if (load_uvmexp(&uvmexp)) + return fmt_human(pagetok(uvmexp.active, + uvmexp.pageshift) * 1024, 1024); + + return NULL; +} #elif defined(__FreeBSD__) - #include <sys/sysctl.h> - #include <sys/vmmeter.h> - #include <unistd.h> - #include <vm/vm_param.h> - - const char * - ram_free(void) { - struct vmtotal vm_stats; - int mib[] = {CTL_VM, VM_TOTAL}; - size_t len; - - len = sizeof(struct vmtotal); - if (sysctl(mib, 2, &vm_stats, &len, NULL, 0) == -1 - || !len) - return NULL; - - return fmt_human(vm_stats.t_free * getpagesize(), 1024); - } +#include <sys/sysctl.h> +#include <sys/vmmeter.h> +#include <unistd.h> +#include <vm/vm_param.h> + +const char * +ram_free(void) +{ + struct vmtotal vm_stats; + int mib[] = {CTL_VM, VM_TOTAL}; + size_t len; + + len = sizeof(struct vmtotal); + if (sysctl(mib, 2, &vm_stats, &len, NULL, 0) == -1 || !len) + return NULL; - const char * - ram_total(void) { - long npages; - size_t len; + return fmt_human(vm_stats.t_free * getpagesize(), 1024); +} - len = sizeof(npages); - if (sysctlbyname("vm.stats.vm.v_page_count", &npages, &len, NULL, 0) == -1 - || !len) - return NULL; +const char * +ram_total(void) +{ + size_t len; + int npages; - return fmt_human(npages * getpagesize(), 1024); - } + len = sizeof(npages); + if (sysctlbyname("vm.stats.vm.v_page_count", &npages, &len, NULL, 0) == -1 + || !len) + return NULL; - const char * - ram_perc(void) { - long npages; - long active; - size_t len; + return fmt_human(npages * getpagesize(), 1024); +} - len = sizeof(npages); - if (sysctlbyname("vm.stats.vm.v_page_count", &npages, &len, NULL, 0) == -1 - || !len) - return NULL; +const char * +ram_perc(void) +{ + size_t len; + int npages; + int active; - if (sysctlbyname("vm.stats.vm.v_active_count", &active, &len, NULL, 0) == -1 - || !len) - return NULL; + len = sizeof(npages); + if (sysctlbyname("vm.stats.vm.v_page_count", &npages, &len, NULL, 0) == -1 + || !len) + return NULL; + if (sysctlbyname("vm.stats.vm.v_active_count", &active, &len, NULL, 0) == -1 + || !len) + return NULL; - return bprintf("%d", active * 100 / npages); - } + return bprintf("%d", active * 100 / npages); +} - const char * - ram_used(void) { - long active; - size_t len; +const char * +ram_used(void) +{ + size_t len; + int active; - len = sizeof(active); - if (sysctlbyname("vm.stats.vm.v_active_count", &active, &len, NULL, 0) == -1 - || !len) - return NULL; + len = sizeof(active); + if (sysctlbyname("vm.stats.vm.v_active_count", &active, &len, NULL, 0) == -1 + || !len) + return NULL; - return fmt_human(active * getpagesize(), 1024); - } + return fmt_human(active * getpagesize(), 1024); +} #endif diff --git a/components/run_command.c b/components/run_command.c index e00b478..0be6138 100644 --- a/components/run_command.c +++ b/components/run_command.c @@ -19,12 +19,10 @@ run_command(const char *cmd) warn("pclose '%s':", cmd); return NULL; } - if (!p) { + if (!p) return NULL; - } - if ((p = strrchr(buf, '\n'))) { + if ((p = strrchr(buf, '\n'))) p[0] = '\0'; - } return buf[0] ? buf : NULL; } diff --git a/components/separator.c b/components/separator.c index 40fec52..04808b5 100644 --- a/components/separator.c +++ b/components/separator.c @@ -6,5 +6,5 @@ const char * separator(const char *separator) { - return separator; + return separator; } diff --git a/components/swap.c b/components/swap.c index 2509db1..b7044a1 100644 --- a/components/swap.c +++ b/components/swap.c @@ -7,278 +7,260 @@ #include "../util.h" #if defined(__linux__) - static int - get_swap_info(long *s_total, long *s_free, long *s_cached) - { - FILE *fp; - struct { - const char *name; - const size_t len; - long *var; - } ent[] = { - { "SwapTotal", sizeof("SwapTotal") - 1, s_total }, - { "SwapFree", sizeof("SwapFree") - 1, s_free }, - { "SwapCached", sizeof("SwapCached") - 1, s_cached }, - }; - size_t line_len = 0, i, left; - char *line = NULL; - - /* get number of fields we want to extract */ - for (i = 0, left = 0; i < LEN(ent); i++) { - if (ent[i].var) { - left++; - } - } - - if (!(fp = fopen("/proc/meminfo", "r"))) { - warn("fopen '/proc/meminfo':"); - return 1; - } +static int +get_swap_info(long *s_total, long *s_free, long *s_cached) +{ + FILE *fp; + struct { + const char *name; + const size_t len; + long *var; + } ent[] = { + { "SwapTotal", sizeof("SwapTotal") - 1, s_total }, + { "SwapFree", sizeof("SwapFree") - 1, s_free }, + { "SwapCached", sizeof("SwapCached") - 1, s_cached }, + }; + size_t line_len = 0, i, left; + char *line = NULL; + + /* get number of fields we want to extract */ + for (i = 0, left = 0; i < LEN(ent); i++) + if (ent[i].var) + left++; + + if (!(fp = fopen("/proc/meminfo", "r"))) { + warn("fopen '/proc/meminfo':"); + return 1; + } - /* read file line by line and extract field information */ - while (left > 0 && getline(&line, &line_len, fp) >= 0) { - for (i = 0; i < LEN(ent); i++) { - if (ent[i].var && - !strncmp(line, ent[i].name, ent[i].len)) { - sscanf(line + ent[i].len + 1, - "%ld kB\n", ent[i].var); - left--; - break; - } + /* read file line by line and extract field information */ + while (left > 0 && getline(&line, &line_len, fp) >= 0) { + for (i = 0; i < LEN(ent); i++) { + if (ent[i].var && !strncmp(line, ent[i].name, ent[i].len)) + sscanf(line + ent[i].len + 1, + "%ld kB\n", ent[i].var); + left--; + break; } } - free(line); - if (ferror(fp)) { - warn("getline '/proc/meminfo':"); - return 1; - } - - fclose(fp); - return 0; + } + free(line); + if (ferror(fp)) { + warn("getline '/proc/meminfo':"); + return 1; } - const char * - swap_free(void) - { - long free; + fclose(fp); + return 0; +} - if (get_swap_info(NULL, &free, NULL)) { - return NULL; - } +const char * +swap_free(void) +{ + long free; - return fmt_human(free * 1024, 1024); - } - - const char * - swap_perc(void) - { - long total, free, cached; + if (get_swap_info(NULL, &free, NULL)) + return NULL; - if (get_swap_info(&total, &free, &cached) || total == 0) { - return NULL; - } + return fmt_human(free * 1024, 1024); +} - return bprintf("%d", 100 * (total - free - cached) / total); - } +const char * +swap_perc(void) +{ + long total, free, cached; - const char * - swap_total(void) - { - long total; + if (get_swap_info(&total, &free, &cached) || total == 0) + return NULL; - if (get_swap_info(&total, NULL, NULL)) { - return NULL; - } + return bprintf("%d", 100 * (total - free - cached) / total); +} - return fmt_human(total * 1024, 1024); - } +const char * +swap_total(void) +{ + long total; - const char * - swap_used(void) - { - long total, free, cached; + if (get_swap_info(&total, NULL, NULL)) + return NULL; - if (get_swap_info(&total, &free, &cached)) { - return NULL; - } + return fmt_human(total * 1024, 1024); +} - return fmt_human((total - free - cached) * 1024, 1024); - } -#elif defined(__OpenBSD__) - #include <stdlib.h> - #include <sys/swap.h> - #include <sys/types.h> - #include <unistd.h> - - static int - getstats(int *total, int *used) - { - struct swapent *sep, *fsep; - int rnswap, nswap, i; - - if ((nswap = swapctl(SWAP_NSWAP, 0, 0)) < 1) { - warn("swaptctl 'SWAP_NSWAP':"); - return 1; - } - if (!(fsep = sep = calloc(nswap, sizeof(*sep)))) { - warn("calloc 'nswap':"); - return 1; - } - if ((rnswap = swapctl(SWAP_STATS, (void *)sep, nswap)) < 0) { - warn("swapctl 'SWAP_STATA':"); - return 1; - } - if (nswap != rnswap) { - warn("getstats: SWAP_STATS != SWAP_NSWAP"); - return 1; - } +const char * +swap_used(void) +{ + long total, free, cached; - *total = 0; - *used = 0; + if (get_swap_info(&total, &free, &cached)) + return NULL; - for (i = 0; i < rnswap; i++) { - *total += sep->se_nblks >> 1; - *used += sep->se_inuse >> 1; - } + return fmt_human((total - free - cached) * 1024, 1024); +} +#elif defined(__OpenBSD__) +#include <stdlib.h> +#include <sys/swap.h> +#include <sys/types.h> +#include <unistd.h> + +static int +getstats(int *total, int *used) +{ + struct swapent *sep, *fsep; + int rnswap, nswap, i; + + if ((nswap = swapctl(SWAP_NSWAP, 0, 0)) < 1) { + warn("swaptctl 'SWAP_NSWAP':"); + return 1; + } + if (!(fsep = sep = calloc(nswap, sizeof(*sep)))) { + warn("calloc 'nswap':"); + return 1; + } + if ((rnswap = swapctl(SWAP_STATS, (void *)sep, nswap)) < 0) { + warn("swapctl 'SWAP_STATA':"); + return 1; + } + if (nswap != rnswap) { + warn("getstats: SWAP_STATS != SWAP_NSWAP"); + return 1; + } - free(fsep); + *total = 0; + *used = 0; - return 0; + for (i = 0; i < rnswap; i++) { + *total += sep->se_nblks >> 1; + *used += sep->se_inuse >> 1; } - const char * - swap_free(void) - { - int total, used; + free(fsep); - if (getstats(&total, &used)) { - return NULL; - } + return 0; +} - return fmt_human((total - used) * 1024, 1024); - } +const char * +swap_free(void) +{ + int total, used; - const char * - swap_perc(void) - { - int total, used; + if (getstats(&total, &used)) + return NULL; - if (getstats(&total, &used)) { - return NULL; - } + return fmt_human((total - used) * 1024, 1024); +} - if (total == 0) { - return NULL; - } +const char * +swap_perc(void) +{ + int total, used; - return bprintf("%d", 100 * used / total); - } + if (getstats(&total, &used)) + return NULL; + if (total == 0) + return NULL; - const char * - swap_total(void) - { - int total, used; + return bprintf("%d", 100 * used / total); +} - if (getstats(&total, &used)) { - return NULL; - } +const char * +swap_total(void) +{ + int total, used; - return fmt_human(total * 1024, 1024); - } + if (getstats(&total, &used)) + return NULL; - const char * - swap_used(void) - { - int total, used; + return fmt_human(total * 1024, 1024); +} - if (getstats(&total, &used)) { - return NULL; - } +const char * +swap_used(void) +{ + int total, used; - return fmt_human(used * 1024, 1024); - } + if (getstats(&total, &used)) + return NULL; + + return fmt_human(used * 1024, 1024); +} #elif defined(__FreeBSD__) - #include <stdlib.h> - #include <sys/types.h> - #include <fcntl.h> - #include <unistd.h> - #include <kvm.h> - - static int getswapinfo(struct kvm_swap *swap_info, size_t size) - { - kvm_t *kd; - - kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, NULL); - if(kd == NULL) { - warn("kvm_openfiles '/dev/null':"); - return 0; - } +#include <stdlib.h> +#include <sys/types.h> +#include <fcntl.h> +#include <unistd.h> +#include <kvm.h> - if(kvm_getswapinfo(kd, swap_info, size, 0 /* Unused flags */) == -1) { - warn("kvm_getswapinfo:"); - kvm_close(kd); - return 0; - } +static int getswapinfo(struct kvm_swap *swap_info, size_t size) +{ + kvm_t *kd; + if ((kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, NULL)) == NULL) { + warn("kvm_openfiles '/dev/null':"); + return 0; + } + if (kvm_getswapinfo(kd, swap_info, size, 0 /* Unused flags */) == -1) { + warn("kvm_getswapinfo:"); kvm_close(kd); - return 1; + return 0; } + kvm_close(kd); - const char * - swap_free(void) - { - struct kvm_swap swap_info[1]; - long used, total; + return 1; +} - if(!getswapinfo(swap_info, 1)) - return NULL; +const char * +swap_free(void) +{ + struct kvm_swap swap_info[1]; + long used, total; - total = swap_info[0].ksw_total; - used = swap_info[0].ksw_used; + if(!getswapinfo(swap_info, 1)) + return NULL; + total = swap_info[0].ksw_total; + used = swap_info[0].ksw_used; - return fmt_human((total - used) * getpagesize(), 1024); - } + return fmt_human((total - used) * getpagesize(), 1024); +} - const char * - swap_perc(void) - { - struct kvm_swap swap_info[1]; - long used, total; +const char * +swap_perc(void) +{ + struct kvm_swap swap_info[1]; + long used, total; - if(!getswapinfo(swap_info, 1)) - return NULL; + if(!getswapinfo(swap_info, 1)) + return NULL; - total = swap_info[0].ksw_total; - used = swap_info[0].ksw_used; + total = swap_info[0].ksw_total; + used = swap_info[0].ksw_used; - return bprintf("%d", used * 100 / total); - } + return bprintf("%d", used * 100 / total); +} - const char * - swap_total(void) - { - struct kvm_swap swap_info[1]; - long total; +const char * +swap_total(void) +{ + struct kvm_swap swap_info[1]; + long total; - if(!getswapinfo(swap_info, 1)) - return NULL; + if(!getswapinfo(swap_info, 1)) + return NULL; + total = swap_info[0].ksw_total; - total = swap_info[0].ksw_total; + return fmt_human(total * getpagesize(), 1024); +} - return fmt_human(total * getpagesize(), 1024); - } +const char * +swap_used(void) +{ + struct kvm_swap swap_info[1]; + long used; - const char * - swap_used(void) - { - struct kvm_swap swap_info[1]; - long used; + if(!getswapinfo(swap_info, 1)) + return NULL; + used = swap_info[0].ksw_used; - if(!getswapinfo(swap_info, 1)) - return NULL; - - used = swap_info[0].ksw_used; - - return fmt_human(used * getpagesize(), 1024); - } + return fmt_human(used * getpagesize(), 1024); +} #endif diff --git a/components/temperature.c b/components/temperature.c index 8e1f222..59ac323 100644 --- a/components/temperature.c +++ b/components/temperature.c @@ -5,67 +5,65 @@ #if defined(__linux__) - #include <stdint.h> +#include <stdint.h> - const char * - temp(const char *file) - { - uintmax_t temp; +const char * +temp(const char *file) +{ + uintmax_t temp; - if (pscanf(file, "%ju", &temp) != 1) { - return NULL; - } + if (pscanf(file, "%ju", &temp) != 1) + return NULL; - return bprintf("%ju", temp / 1000); - } + return bprintf("%ju", temp / 1000); +} #elif defined(__OpenBSD__) - #include <stdio.h> - #include <sys/time.h> /* before <sys/sensors.h> for struct timeval */ - #include <sys/sensors.h> - #include <sys/sysctl.h> - - const char * - temp(const char *unused) - { - int mib[5]; - size_t size; - struct sensor temp; +#include <stdio.h> +#include <sys/time.h> /* before <sys/sensors.h> for struct timeval */ +#include <sys/sensors.h> +#include <sys/sysctl.h> - mib[0] = CTL_HW; - mib[1] = HW_SENSORS; - mib[2] = 0; /* cpu0 */ - mib[3] = SENSOR_TEMP; - mib[4] = 0; /* temp0 */ +const char * +temp(const char *unused) +{ + int mib[5]; + size_t size; + struct sensor temp; - size = sizeof(temp); + mib[0] = CTL_HW; + mib[1] = HW_SENSORS; + mib[2] = 0; /* cpu0 */ + mib[3] = SENSOR_TEMP; + mib[4] = 0; /* temp0 */ - if (sysctl(mib, 5, &temp, &size, NULL, 0) < 0) { - warn("sysctl 'SENSOR_TEMP':"); - return NULL; - } + size = sizeof(temp); - /* kelvin to celsius */ - return bprintf("%d", (int)((float)(temp.value-273150000) / 1E6)); + if (sysctl(mib, 5, &temp, &size, NULL, 0) < 0) { + warn("sysctl 'SENSOR_TEMP':"); + return NULL; } + + /* kelvin to celsius */ + return bprintf("%d", (int)((float)(temp.value-273150000) / 1E6)); +} #elif defined(__FreeBSD__) - #include <stdio.h> - #include <stdlib.h> - #include <sys/sysctl.h> +#include <stdio.h> +#include <stdlib.h> +#include <sys/sysctl.h> - const char * - temp(const char *zone) - { - char buf[256]; - int temp; - size_t len; +const char * +temp(const char *zone) +{ + char buf[256]; + int temp; + size_t len; - len = sizeof(temp); - snprintf(buf, sizeof(buf), "hw.acpi.thermal.%s.temperature", zone); - if (sysctlbyname(buf, &temp, &len, NULL, 0) == -1 - || !len) - return NULL; + len = sizeof(temp); + snprintf(buf, sizeof(buf), "hw.acpi.thermal.%s.temperature", zone); + if (sysctlbyname(buf, &temp, &len, NULL, 0) == -1 || !len) + return NULL; - /* kelvin to decimal celcius */ - return bprintf("%d.%d", (temp - 2731) / 10, abs((temp - 2731) % 10)); - } + /* kelvin to decimal celcius */ + return bprintf("%d.%d", (temp - 2731) / 10, abs((temp - 2731) % 10)); +} #endif diff --git a/components/uptime.c b/components/uptime.c index 7c23c98..3480121 100644 --- a/components/uptime.c +++ b/components/uptime.c @@ -6,11 +6,11 @@ #include "../util.h" #if defined(CLOCK_BOOTTIME) - #define UPTIME_FLAG CLOCK_BOOTTIME +#define UPTIME_FLAG CLOCK_BOOTTIME #elif defined(CLOCK_UPTIME) - #define UPTIME_FLAG CLOCK_UPTIME +#define UPTIME_FLAG CLOCK_UPTIME #else - #define UPTIME_FLAG CLOCK_MONOTONIC +#define UPTIME_FLAG CLOCK_MONOTONIC #endif const char * diff --git a/components/volume.c b/components/volume.c index b6665da..8277866 100644 --- a/components/volume.c +++ b/components/volume.c @@ -8,210 +8,204 @@ #include "../util.h" #if defined(__OpenBSD__) - #include <sys/queue.h> - #include <poll.h> - #include <sndio.h> - #include <stdlib.h> - - struct control { - LIST_ENTRY(control) next; - unsigned int addr; - #define CTRL_NONE 0 - #define CTRL_LEVEL 1 - #define CTRL_MUTE 2 - unsigned int type; - unsigned int maxval; - unsigned int val; - }; - - static LIST_HEAD(, control) controls = LIST_HEAD_INITIALIZER(controls); - static struct pollfd *pfds; - static struct sioctl_hdl *hdl; - static int initialized; - - /* - * Call-back to obtain the description of all audio controls. - */ - static void - ondesc(void *unused, struct sioctl_desc *desc, int val) - { - struct control *c, *ctmp; - unsigned int type = CTRL_NONE; - - if (desc == NULL) - return; - - /* Delete existing audio control with the same address. */ - LIST_FOREACH_SAFE(c, &controls, next, ctmp) { - if (desc->addr == c->addr) { - LIST_REMOVE(c, next); - free(c); - break; - } - } - - /* Only match output.level and output.mute audio controls. */ - if (desc->group[0] != 0 || - strcmp(desc->node0.name, "output") != 0) - return; - if (desc->type == SIOCTL_NUM && - strcmp(desc->func, "level") == 0) - type = CTRL_LEVEL; - else if (desc->type == SIOCTL_SW && - strcmp(desc->func, "mute") == 0) - type = CTRL_MUTE; - else - return; - - c = malloc(sizeof(struct control)); - if (c == NULL) { - warn("sndio: failed to allocate audio control\n"); - return; - } - - c->addr = desc->addr; - c->type = type; - c->maxval = desc->maxval; - c->val = val; - LIST_INSERT_HEAD(&controls, c, next); - } - - /* - * Call-back invoked whenever an audio control changes. - */ - static void - onval(void *unused, unsigned int addr, unsigned int val) - { - struct control *c; - - LIST_FOREACH(c, &controls, next) { - if (c->addr == addr) - break; - } - c->val = val; - } - - static void - cleanup(void) - { - struct control *c; - - if (hdl) { - sioctl_close(hdl); - hdl = NULL; - } - - free(pfds); - pfds = NULL; - - while (!LIST_EMPTY(&controls)) { - c = LIST_FIRST(&controls); +#include <sys/queue.h> +#include <poll.h> +#include <sndio.h> +#include <stdlib.h> + +struct control { + LIST_ENTRY(control) next; + unsigned int addr; +#define CTRL_NONE 0 +#define CTRL_LEVEL 1 +#define CTRL_MUTE 2 + unsigned int type; + unsigned int maxval; + unsigned int val; +}; + +static LIST_HEAD(, control) controls = LIST_HEAD_INITIALIZER(controls); +static struct pollfd *pfds; +static struct sioctl_hdl *hdl; +static int initialized; + +/* + * Call-back to obtain the description of all audio controls. + */ +static void +ondesc(void *unused, struct sioctl_desc *desc, int val) +{ + struct control *c, *ctmp; + unsigned int type = CTRL_NONE; + + if (desc == NULL) + return; + + /* Delete existing audio control with the same address. */ + LIST_FOREACH_SAFE(c, &controls, next, ctmp) { + if (desc->addr == c->addr) { LIST_REMOVE(c, next); free(c); + break; } } - static int - init(void) - { - hdl = sioctl_open(SIO_DEVANY, SIOCTL_READ, 0); - if (hdl == NULL) { - warn("sndio: cannot open device"); - goto failed; - } + /* Only match output.level and output.mute audio controls. */ + if (desc->group[0] != 0 || strcmp(desc->node0.name, "output") != 0) + return; + if (desc->type == SIOCTL_NUM && strcmp(desc->func, "level") == 0) + type = CTRL_LEVEL; + else if (desc->type == SIOCTL_SW && strcmp(desc->func, "mute") == 0) + type = CTRL_MUTE; + else + return; + + c = malloc(sizeof(struct control)); + if (c == NULL) { + warn("sndio: failed to allocate audio control\n"); + return; + } - if (!sioctl_ondesc(hdl, ondesc, NULL)) { - warn("sndio: cannot set control description call-back"); - goto failed; - } + c->addr = desc->addr; + c->type = type; + c->maxval = desc->maxval; + c->val = val; + LIST_INSERT_HEAD(&controls, c, next); +} + +/* + * Call-back invoked whenever an audio control changes. + */ +static void +onval(void *unused, unsigned int addr, unsigned int val) +{ + struct control *c; + + LIST_FOREACH(c, &controls, next) { + if (c->addr == addr) + break; + } + c->val = val; +} - if (!sioctl_onval(hdl, onval, NULL)) { - warn("sndio: cannot set control values call-back"); - goto failed; - } +static void +cleanup(void) +{ + struct control *c; - pfds = calloc(sioctl_nfds(hdl), sizeof(struct pollfd)); - if (pfds == NULL) { - warn("sndio: cannot allocate pollfd structures"); - goto failed; - } + if (hdl) { + sioctl_close(hdl); + hdl = NULL; + } + free(pfds); + pfds = NULL; - return 1; - failed: - cleanup(); - return 0; + while (!LIST_EMPTY(&controls)) { + c = LIST_FIRST(&controls); + LIST_REMOVE(c, next); + free(c); + } +} + +static int +init(void) +{ + hdl = sioctl_open(SIO_DEVANY, SIOCTL_READ, 0); + if (hdl == NULL) { + warn("sndio: cannot open device"); + goto failed; } - const char * - vol_perc(const char *unused) - { - struct control *c; - int n, v, value; + if (!sioctl_ondesc(hdl, ondesc, NULL)) { + warn("sndio: cannot set control description call-back"); + goto failed; + } - if (!initialized) - initialized = init(); + if (!sioctl_onval(hdl, onval, NULL)) { + warn("sndio: cannot set control values call-back"); + goto failed; + } - if (hdl == NULL) - return NULL; + pfds = calloc(sioctl_nfds(hdl), sizeof(struct pollfd)); + if (pfds == NULL) { + warn("sndio: cannot allocate pollfd structures"); + goto failed; + } - n = sioctl_pollfd(hdl, pfds, POLLIN); + return 1; +failed: + cleanup(); + return 0; +} + +const char * +vol_perc(const char *unused) +{ + struct control *c; + int n, v, value; + + if (!initialized) + initialized = init(); + if (hdl == NULL) + return NULL; + + n = sioctl_pollfd(hdl, pfds, POLLIN); + if (n > 0) { + n = poll(pfds, n, 0); if (n > 0) { - n = poll(pfds, n, 0); - if (n > 0) { - if (sioctl_revents(hdl, pfds) & POLLHUP) { - warn("sndio: disconnected"); - cleanup(); - return NULL; - } + if (sioctl_revents(hdl, pfds) & POLLHUP) { + warn("sndio: disconnected"); + cleanup(); + return NULL; } } + } - value = 100; - LIST_FOREACH(c, &controls, next) { - if (c->type == CTRL_MUTE && c->val == 1) - value = 0; - else if (c->type == CTRL_LEVEL) { - v = (c->val * 100 + c->maxval / 2) / c->maxval; - /* For multiple channels return the minimum. */ - if (v < value) - value = v; - } + value = 100; + LIST_FOREACH(c, &controls, next) { + if (c->type == CTRL_MUTE && c->val == 1) + value = 0; + else if (c->type == CTRL_LEVEL) { + v = (c->val * 100 + c->maxval / 2) / c->maxval; + /* For multiple channels return the minimum. */ + if (v < value) + value = v; } - - return bprintf("%d", value); } + + return bprintf("%d", value); +} #else - #include <sys/soundcard.h> - - const char * - vol_perc(const char *card) - { - size_t i; - int v, afd, devmask; - char *vnames[] = SOUND_DEVICE_NAMES; - - if ((afd = open(card, O_RDONLY | O_NONBLOCK)) < 0) { - warn("open '%s':", card); - return NULL; - } +#include <sys/soundcard.h> + +const char * +vol_perc(const char *card) +{ + size_t i; + int v, afd, devmask; + char *vnames[] = SOUND_DEVICE_NAMES; + + if ((afd = open(card, O_RDONLY | O_NONBLOCK)) < 0) { + warn("open '%s':", card); + return NULL; + } - if (ioctl(afd, (int)SOUND_MIXER_READ_DEVMASK, &devmask) < 0) { - warn("ioctl 'SOUND_MIXER_READ_DEVMASK':"); - close(afd); - return NULL; - } - for (i = 0; i < LEN(vnames); i++) { - if (devmask & (1 << i) && !strcmp("vol", vnames[i])) { - if (ioctl(afd, MIXER_READ(i), &v) < 0) { - warn("ioctl 'MIXER_READ(%ld)':", i); - close(afd); - return NULL; - } + if (ioctl(afd, (int)SOUND_MIXER_READ_DEVMASK, &devmask) < 0) { + warn("ioctl 'SOUND_MIXER_READ_DEVMASK':"); + close(afd); + return NULL; + } + for (i = 0; i < LEN(vnames); i++) { + if (devmask & (1 << i) && !strcmp("vol", vnames[i])) { + if (ioctl(afd, MIXER_READ(i), &v) < 0) { + warn("ioctl 'MIXER_READ(%ld)':", i); + close(afd); + return NULL; } } - - close(afd); - - return bprintf("%d", v & 0xff); } + close(afd); + + return bprintf("%d", v & 0xff); +} #endif diff --git a/components/wifi.c b/components/wifi.c index 92c252e..2dce709 100644 --- a/components/wifi.c +++ b/components/wifi.c @@ -9,264 +9,249 @@ #include "../util.h" #define RSSI_TO_PERC(rssi) \ - rssi >= -50 ? 100 : \ - (rssi <= -100 ? 0 : \ - (2 * (rssi + 100))) + rssi >= -50 ? 100 : \ + (rssi <= -100 ? 0 : \ + (2 * (rssi + 100))) #if defined(__linux__) - #include <limits.h> - #include <linux/wireless.h> +#include <limits.h> +#include <linux/wireless.h> + +const char * +wifi_perc(const char *interface) +{ + int cur; + size_t i; + char *p, *datastart; + char path[PATH_MAX]; + char status[5]; + FILE *fp; + + if (esnprintf(path, sizeof(path), "/sys/class/net/%s/operstate", + interface) < 0) + return NULL; + if (!(fp = fopen(path, "r"))) { + warn("fopen '%s':", path); + return NULL; + } + p = fgets(status, 5, fp); + fclose(fp); + if (!p || strcmp(status, "up\n") != 0) + return NULL; - const char * - wifi_perc(const char *interface) - { - int cur; - size_t i; - char *p, *datastart; - char path[PATH_MAX]; - char status[5]; - FILE *fp; - - if (esnprintf(path, sizeof(path), "/sys/class/net/%s/operstate", - interface) < 0) { - return NULL; - } - if (!(fp = fopen(path, "r"))) { - warn("fopen '%s':", path); - return NULL; - } - p = fgets(status, 5, fp); - fclose(fp); - if (!p || strcmp(status, "up\n") != 0) { - return NULL; - } + if (!(fp = fopen("/proc/net/wireless", "r"))) { + warn("fopen '/proc/net/wireless':"); + return NULL; + } - if (!(fp = fopen("/proc/net/wireless", "r"))) { - warn("fopen '/proc/net/wireless':"); - return NULL; - } + for (i = 0; i < 3; i++) + if (!(p = fgets(buf, sizeof(buf) - 1, fp))) + break; + fclose(fp); + if (i < 2 || !p) + return NULL; + if (!(datastart = strstr(buf, interface))) + return NULL; - for (i = 0; i < 3; i++) { - if (!(p = fgets(buf, sizeof(buf) - 1, fp))) - break; - } - fclose(fp); - if (i < 2 || !p) { - return NULL; - } + datastart = (datastart+(strlen(interface)+1)); + sscanf(datastart + 1, " %*d %d %*d %*d\t\t %*d\t " + "%*d\t\t%*d\t\t %*d\t %*d\t\t %*d", &cur); - if (!(datastart = strstr(buf, interface))) { - return NULL; - } + /* 70 is the max of /proc/net/wireless */ + return bprintf("%d", (int)((float)cur / 70 * 100)); +} - datastart = (datastart+(strlen(interface)+1)); - sscanf(datastart + 1, " %*d %d %*d %*d\t\t %*d\t " - "%*d\t\t%*d\t\t %*d\t %*d\t\t %*d", &cur); +const char * +wifi_essid(const char *interface) +{ + static char id[IW_ESSID_MAX_SIZE+1]; + int sockfd; + struct iwreq wreq; - /* 70 is the max of /proc/net/wireless */ - return bprintf("%d", (int)((float)cur / 70 * 100)); + memset(&wreq, 0, sizeof(struct iwreq)); + wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; + if (esnprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", interface) < 0) + return NULL; + if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + warn("socket 'AF_INET':"); + return NULL; } - - const char * - wifi_essid(const char *interface) - { - static char id[IW_ESSID_MAX_SIZE+1]; - int sockfd; - struct iwreq wreq; - - memset(&wreq, 0, sizeof(struct iwreq)); - wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; - if (esnprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", - interface) < 0) { - return NULL; - } - - if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - warn("socket 'AF_INET':"); - return NULL; - } - wreq.u.essid.pointer = id; - if (ioctl(sockfd,SIOCGIWESSID, &wreq) < 0) { - warn("ioctl 'SIOCGIWESSID':"); - close(sockfd); - return NULL; - } - + wreq.u.essid.pointer = id; + if (ioctl(sockfd,SIOCGIWESSID, &wreq) < 0) { + warn("ioctl 'SIOCGIWESSID':"); close(sockfd); + return NULL; + } - if (!strcmp(id, "")) { - return NULL; - } + close(sockfd); + if (!strcmp(id, "")) + return NULL; - return id; - } + return id; +} #elif defined(__OpenBSD__) - #include <net/if.h> - #include <net/if_media.h> - #include <net80211/ieee80211.h> - #include <sys/select.h> /* before <sys/ieee80211_ioctl.h> for NBBY */ - #include <net80211/ieee80211_ioctl.h> - #include <stdlib.h> - #include <sys/types.h> - - static int - load_ieee80211_nodereq(const char *interface, struct ieee80211_nodereq *nr) - { - struct ieee80211_bssid bssid; - int sockfd; - uint8_t zero_bssid[IEEE80211_ADDR_LEN]; - - memset(&bssid, 0, sizeof(bssid)); - memset(nr, 0, sizeof(struct ieee80211_nodereq)); - if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - warn("socket 'AF_INET':"); - return 0; - } - strlcpy(bssid.i_name, interface, sizeof(bssid.i_name)); - if ((ioctl(sockfd, SIOCG80211BSSID, &bssid)) < 0) { - warn("ioctl 'SIOCG80211BSSID':"); - close(sockfd); - return 0; - } - memset(&zero_bssid, 0, sizeof(zero_bssid)); - if (memcmp(bssid.i_bssid, zero_bssid, - IEEE80211_ADDR_LEN) == 0) { - close(sockfd); - return 0; - } - strlcpy(nr->nr_ifname, interface, sizeof(nr->nr_ifname)); - memcpy(&nr->nr_macaddr, bssid.i_bssid, sizeof(nr->nr_macaddr)); - if ((ioctl(sockfd, SIOCG80211NODE, nr)) < 0 && nr->nr_rssi) { - warn("ioctl 'SIOCG80211NODE':"); - close(sockfd); - return 0; - } +#include <net/if.h> +#include <net/if_media.h> +#include <net80211/ieee80211.h> +#include <sys/select.h> /* before <sys/ieee80211_ioctl.h> for NBBY */ +#include <net80211/ieee80211_ioctl.h> +#include <stdlib.h> +#include <sys/types.h> + +static int +load_ieee80211_nodereq(const char *interface, struct ieee80211_nodereq *nr) +{ + struct ieee80211_bssid bssid; + int sockfd; + uint8_t zero_bssid[IEEE80211_ADDR_LEN]; + + memset(&bssid, 0, sizeof(bssid)); + memset(nr, 0, sizeof(struct ieee80211_nodereq)); + if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + warn("socket 'AF_INET':"); + return 0; + } + strlcpy(bssid.i_name, interface, sizeof(bssid.i_name)); + if ((ioctl(sockfd, SIOCG80211BSSID, &bssid)) < 0) { + warn("ioctl 'SIOCG80211BSSID':"); + close(sockfd); + return 0; + } + memset(&zero_bssid, 0, sizeof(zero_bssid)); + if (memcmp(bssid.i_bssid, zero_bssid, IEEE80211_ADDR_LEN) == 0) { + close(sockfd); + return 0; + } + strlcpy(nr->nr_ifname, interface, sizeof(nr->nr_ifname)); + memcpy(&nr->nr_macaddr, bssid.i_bssid, sizeof(nr->nr_macaddr)); + if ((ioctl(sockfd, SIOCG80211NODE, nr)) < 0 && nr->nr_rssi) { + warn("ioctl 'SIOCG80211NODE':"); + close(sockfd); + return 0; + } - return close(sockfd), 1; + return close(sockfd), 1; +} + +const char * +wifi_perc(const char *interface) +{ + struct ieee80211_nodereq nr; + int q; + + if (load_ieee80211_nodereq(interface, &nr)) { + if (nr.nr_max_rssi) + q = IEEE80211_NODEREQ_RSSI(&nr); + else + q = RSSI_TO_PERC(nr.nr_rssi); + return bprintf("%d", q); } - const char * - wifi_perc(const char *interface) - { - struct ieee80211_nodereq nr; - int q; - - if (load_ieee80211_nodereq(interface, &nr)) { - if (nr.nr_max_rssi) { - q = IEEE80211_NODEREQ_RSSI(&nr); - } else { - q = RSSI_TO_PERC(nr.nr_rssi); - } - return bprintf("%d", q); - } + return NULL; +} - return NULL; - } +const char * +wifi_essid(const char *interface) +{ + struct ieee80211_nodereq nr; - const char * - wifi_essid(const char *interface) - { - struct ieee80211_nodereq nr; + if (load_ieee80211_nodereq(interface, &nr)) + return bprintf("%s", nr.nr_nwid); - if (load_ieee80211_nodereq(interface, &nr)) { - return bprintf("%s", nr.nr_nwid); - } + return NULL; +} +#elif defined(__FreeBSD__) +#include <net/if.h> +#include <net80211/ieee80211_ioctl.h> + +int +load_ieee80211req(int sock, const char *interface, void *data, int type, size_t *len) +{ + char warn_buf[256]; + struct ieee80211req ireq; + memset(&ireq, 0, sizeof(ireq)); + ireq.i_type = type; + ireq.i_data = (caddr_t) data; + ireq.i_len = *len; + + strlcpy(ireq.i_name, interface, sizeof(ireq.i_name)); + if (ioctl(sock, SIOCG80211, &ireq) < 0) { + snprintf(warn_buf, sizeof(warn_buf), + "ioctl: 'SIOCG80211': %d", type); + warn(warn_buf); + return 0; + } + *len = ireq.i_len; + return 1; +} + +const char * +wifi_perc(const char *interface) +{ + union { + struct ieee80211req_sta_req sta; + uint8_t buf[24 * 1024]; + } info; + uint8_t bssid[IEEE80211_ADDR_LEN]; + int rssi_dbm; + int sockfd; + size_t len; + const char *fmt; + + if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + warn("socket 'AF_INET':"); return NULL; } -#elif defined(__FreeBSD__) - #include <net/if.h> - #include <net80211/ieee80211_ioctl.h> - int - load_ieee80211req(int sock, const char *interface, void *data, int type, size_t *len) + /* Retreive MAC address of interface */ + len = IEEE80211_ADDR_LEN; + fmt = NULL; + if (load_ieee80211req(sockfd, interface, &bssid, IEEE80211_IOC_BSSID, &len)) { - char warn_buf[256]; - struct ieee80211req ireq; - memset(&ireq, 0, sizeof(ireq)); - ireq.i_type = type; - ireq.i_data = (caddr_t) data; - ireq.i_len = *len; - - strlcpy(ireq.i_name, interface, sizeof(ireq.i_name)); - if (ioctl(sock, SIOCG80211, &ireq) < 0) { - snprintf(warn_buf, sizeof(warn_buf), - "ioctl: 'SIOCG80211': %d", type); - warn(warn_buf); - return 0; + /* Retrieve info on station with above BSSID */ + memset(&info, 0, sizeof(info)); + memcpy(info.sta.is_u.macaddr, bssid, sizeof(bssid)); + + len = sizeof(info); + if (load_ieee80211req(sockfd, interface, &info, + IEEE80211_IOC_STA_INFO, &len)) { + rssi_dbm = info.sta.info[0].isi_noise + + info.sta.info[0].isi_rssi / 2; + fmt = bprintf("%d", RSSI_TO_PERC(rssi_dbm)); } - - *len = ireq.i_len; - return 1; } - const char * - wifi_perc(const char *interface) - { - union { - struct ieee80211req_sta_req sta; - uint8_t buf[24 * 1024]; - } info; - uint8_t bssid[IEEE80211_ADDR_LEN]; - int rssi_dbm; - int sockfd; - size_t len; - const char *fmt; - - if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - warn("socket 'AF_INET':"); - return NULL; - } + close(sockfd); + return fmt; +} - /* Retreive MAC address of interface */ - len = IEEE80211_ADDR_LEN; - fmt = NULL; - if (load_ieee80211req(sockfd, interface, &bssid, IEEE80211_IOC_BSSID, &len)) - { - /* Retrieve info on station with above BSSID */ - memset(&info, 0, sizeof(info)); - memcpy(info.sta.is_u.macaddr, bssid, sizeof(bssid)); - - len = sizeof(info); - if (load_ieee80211req(sockfd, interface, &info, IEEE80211_IOC_STA_INFO, &len)) { - rssi_dbm = info.sta.info[0].isi_noise + - info.sta.info[0].isi_rssi / 2; - - fmt = bprintf("%d", RSSI_TO_PERC(rssi_dbm)); - } - } +const char * +wifi_essid(const char *interface) +{ + char ssid[IEEE80211_NWID_LEN + 1]; + size_t len; + int sockfd; + const char *fmt; - close(sockfd); - return fmt; + if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + warn("socket 'AF_INET':"); + return NULL; } - const char * - wifi_essid(const char *interface) - { - char ssid[IEEE80211_NWID_LEN + 1]; - size_t len; - int sockfd; - const char *fmt; - - if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - warn("socket 'AF_INET':"); - return NULL; - } - - fmt = NULL; - len = sizeof(ssid); - memset(&ssid, 0, len); - if (load_ieee80211req(sockfd, interface, &ssid, IEEE80211_IOC_SSID, &len )) { - if (len < sizeof(ssid)) - len += 1; - else - len = sizeof(ssid); - - ssid[len - 1] = '\0'; - fmt = bprintf("%s", ssid); - } - - close(sockfd); - return fmt; + fmt = NULL; + len = sizeof(ssid); + memset(&ssid, 0, len); + if (load_ieee80211req(sockfd, interface, &ssid, IEEE80211_IOC_SSID, &len)) { + if (len < sizeof(ssid)) + len += 1; + else + len = sizeof(ssid); + ssid[len - 1] = '\0'; + fmt = bprintf("%s", ssid); } + + close(sockfd); + return fmt; +} #endif diff --git a/config.mk b/config.mk index 2516e6e..6fc46a8 100644 --- a/config.mk +++ b/config.mk @@ -7,6 +7,10 @@ VERSION = 0 PREFIX = /usr/local MANPREFIX = $(PREFIX)/share/man +# FreeBSD (uncomment) +#X11INC = /usr/local/include +#X11LIB = /usr/local/lib + X11INC = /usr/X11R6/include X11LIB = /usr/X11R6/lib diff --git a/slstatus.c b/slstatus.c index 64da5cb..ea33e74 100644 --- a/slstatus.c +++ b/slstatus.c @@ -34,8 +34,7 @@ static void difftimespec(struct timespec *res, struct timespec *a, struct timespec *b) { res->tv_sec = a->tv_sec - b->tv_sec - (a->tv_nsec < b->tv_nsec); - res->tv_nsec = a->tv_nsec - b->tv_nsec + - (a->tv_nsec < b->tv_nsec) * 1E9; + res->tv_nsec = a->tv_nsec - b->tv_nsec + (a->tv_nsec < b->tv_nsec) * 1E9; } static void @@ -66,9 +65,8 @@ main(int argc, char *argv[]) usage(); } ARGEND - if (argc) { + if (argc) usage(); - } memset(&act, 0, sizeof(act)); act.sa_handler = terminate; @@ -77,24 +75,20 @@ main(int argc, char *argv[]) act.sa_flags |= SA_RESTART; sigaction(SIGUSR1, &act, NULL); - if (!sflag && !(dpy = XOpenDisplay(NULL))) { + if (!sflag && !(dpy = XOpenDisplay(NULL))) die("XOpenDisplay: Failed to open display"); - } do { - if (clock_gettime(CLOCK_MONOTONIC, &start) < 0) { + if (clock_gettime(CLOCK_MONOTONIC, &start) < 0) die("clock_gettime:"); - } status[0] = '\0'; for (i = len = 0; i < LEN(args); i++) { - if (!(res = args[i].func(args[i].args))) { + if (!(res = args[i].func(args[i].args))) res = unknown_str; - } if ((ret = esnprintf(status + len, sizeof(status) - len, - args[i].fmt, res)) < 0) { + args[i].fmt, res)) < 0) break; - } len += ret; } @@ -104,37 +98,30 @@ main(int argc, char *argv[]) if (ferror(stdout)) die("puts:"); } else { - if (XStoreName(dpy, DefaultRootWindow(dpy), status) - < 0) { + if (XStoreName(dpy, DefaultRootWindow(dpy), status) < 0) die("XStoreName: Allocation failed"); - } XFlush(dpy); } if (!done) { - if (clock_gettime(CLOCK_MONOTONIC, ¤t) < 0) { + if (clock_gettime(CLOCK_MONOTONIC, ¤t) < 0) die("clock_gettime:"); - } difftimespec(&diff, ¤t, &start); intspec.tv_sec = interval / 1000; intspec.tv_nsec = (interval % 1000) * 1E6; difftimespec(&wait, &intspec, &diff); - if (wait.tv_sec >= 0) { - if (nanosleep(&wait, NULL) < 0 && - errno != EINTR) { + if (wait.tv_sec >= 0) + if (nanosleep(&wait, NULL) < 0 && errno != EINTR) die("nanosleep:"); - } - } } } while (!done); if (!sflag) { XStoreName(dpy, DefaultRootWindow(dpy), NULL); - if (XCloseDisplay(dpy) < 0) { + if (XCloseDisplay(dpy) < 0) die("XCloseDisplay: Failed to close display"); - } } return 0; diff --git a/util.c b/util.c index 85366bf..d1f6077 100644 --- a/util.c +++ b/util.c @@ -13,9 +13,8 @@ char *argv0; static void verr(const char *fmt, va_list ap) { - if (argv0 && strncmp(fmt, "usage", sizeof("usage") - 1)) { + if (argv0 && strncmp(fmt, "usage", sizeof("usage") - 1)) fprintf(stderr, "%s: ", argv0); - } vfprintf(stderr, fmt, ap); @@ -119,9 +118,8 @@ fmt_human(uintmax_t num, int base) } scaled = num; - for (i = 0; i < prefixlen && scaled >= base; i++) { + for (i = 0; i < prefixlen && scaled >= base; i++) scaled /= base; - } return bprintf("%.1f %s", scaled, prefix[i]); } -- 2.31.1
