From f5f3808f361a4278a60124e0c26721538ea400d2 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Tue, 1 May 2018 21:05:50 +0200 Subject: [PATCH 01/16] Update README todo --- README | 1 - 1 file changed, 1 deletion(-) diff --git a/README b/README index 30cacd4..c0977aa 100644 --- a/README +++ b/README @@ -65,4 +65,3 @@ The following functions are not portable at the moment: - cpu_{perc,iowait} - entropy - swap_{free,perc,total,used} -- battery_{power,state} -- 2.20.1 From 2bd581a47b8e355a398b87bb82fdcff547b8073a Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Wed, 2 May 2018 08:16:21 +0200 Subject: [PATCH 02/16] battery_state: Unify unknown state with "?" symbol --- components/battery.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/components/battery.c b/components/battery.c index 86ea2a1..e997e4c 100644 --- a/components/battery.c +++ b/components/battery.c @@ -30,7 +30,6 @@ { "Charging", "+" }, { "Discharging", "-" }, { "Full", "=" }, - { "Unknown", "/" }, }; size_t i; char path[PATH_MAX], state[12]; @@ -89,7 +88,6 @@ } map[] = { { APM_AC_ON, "+" }, { APM_AC_OFF, "-" }, - { APM_AC_UNKNOWN, "/" }, }; fd = open("/dev/apm", O_RDONLY); -- 2.20.1 From b2b6eb638eadcdd5ea091d74f65b1c0a97e0df0a Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Wed, 2 May 2018 08:21:32 +0200 Subject: [PATCH 03/16] ip: Sort headers alphabetically --- components/ip.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/components/ip.c b/components/ip.c index 422b448..b1f8a05 100644 --- a/components/ip.c +++ b/components/ip.c @@ -1,14 +1,13 @@ /* See LICENSE file for copyright and license details. */ -#if defined(__OpenBSD__) -#include -#include -#endif - #include #include #include #include #include +#if defined(__OpenBSD__) +#include +#include +#endif #include "../util.h" -- 2.20.1 From 7c11f890a5296ddd527ca956503bb33ceacd3ca9 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Wed, 2 May 2018 08:26:23 +0200 Subject: [PATCH 04/16] keyboard_indicators: Clean up opening display --- components/keyboard_indicators.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/keyboard_indicators.c b/components/keyboard_indicators.c index 76cf17e..ed4bc17 100644 --- a/components/keyboard_indicators.c +++ b/components/keyboard_indicators.c @@ -7,10 +7,10 @@ const char * keyboard_indicators(void) { - Display *dpy = XOpenDisplay(NULL); + Display *dpy; XKeyboardState state; - if (dpy == NULL) { + if (!(dpy = XOpenDisplay(NULL))) { fprintf(stderr, "Cannot open display\n"); return NULL; } -- 2.20.1 From f0070071566ad27fa36c49312e1ba3946cd472b5 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Wed, 2 May 2018 08:29:36 +0200 Subject: [PATCH 05/16] run_command: Clean up --- components/run_command.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/components/run_command.c b/components/run_command.c index 2041ae4..e356620 100644 --- a/components/run_command.c +++ b/components/run_command.c @@ -11,8 +11,7 @@ run_command(const char *cmd) char *p; FILE *fp; - fp = popen(cmd, "r"); - if (fp == NULL) { + if (!(fp = popen(cmd, "r"))) { fprintf(stderr, "popen '%s': %s\n", cmd, strerror(errno)); return NULL; } -- 2.20.1 From e43c3a16f06ccd0c35814e8b47a6f30e81f9c981 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Wed, 2 May 2018 08:41:06 +0200 Subject: [PATCH 06/16] uptime: Simplifiy and clean up --- components/uptime.c | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/components/uptime.c b/components/uptime.c index 25f904c..19908b9 100644 --- a/components/uptime.c +++ b/components/uptime.c @@ -1,41 +1,43 @@ /* See LICENSE file for copyright and license details. */ -#include #include -#include #include "../util.h" +const char * +format(int uptime) +{ + int h, m; + + h = uptime / 3600; + m = (uptime - h * 3600) / 60; + + return bprintf("%dh %dm", h, m); +} + #if defined(__linux__) #include const char * uptime(void) { - int h; - int m; - int uptime = 0; + int uptime; struct sysinfo info; sysinfo(&info); uptime = info.uptime; - h = uptime / 3600; - m = (uptime - h * 3600) / 60; - - return bprintf("%dh %dm", h, m); + return format(uptime); } #elif defined(__OpenBSD__) + #include + #include #include #include const char * uptime(void) { - int h; - int m; - int uptime = 0; - - int mib[2]; + int mib[2], uptime; size_t size; time_t now; struct timeval boottime; @@ -47,16 +49,13 @@ size = sizeof(boottime); - if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1) - uptime = now - boottime.tv_sec; - else { + if (sysctl(mib, 2, &boottime, &size, NULL, 0) == -1) fprintf(stderr, "sysctl 'KERN_BOOTTIME': %s\n", strerror(errno)); return NULL; } - h = uptime / 3600; - m = (uptime - h * 3600) / 60; + uptime = now - boottime.tv_sec; - return bprintf("%dh %dm", h, m); + return format(uptime); } #endif -- 2.20.1 From 709549b4bd9fee0e0054c0dfa75fff9ac18f2862 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Wed, 2 May 2018 08:42:55 +0200 Subject: [PATCH 07/16] user: Only declare variables in the beginning --- components/user.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/user.c b/components/user.c index b335dc3..8ab6db9 100644 --- a/components/user.c +++ b/components/user.c @@ -17,9 +17,9 @@ gid(void) const char * username(void) { - struct passwd *pw = getpwuid(geteuid()); + struct passwd *pw; - if (pw == NULL) { + if (!(pw = getpwuid(geteuid()))) { fprintf(stderr, "getpwuid '%d': %s\n", geteuid(), strerror(errno)); return NULL; } -- 2.20.1 From f7a6d6c8f7d621fe7b5a9c37a130f8bf3b9bd892 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Wed, 2 May 2018 08:49:06 +0200 Subject: [PATCH 08/16] wifi: Various cleanups --- components/wifi.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/components/wifi.c b/components/wifi.c index 33e09b7..13b630a 100644 --- a/components/wifi.c +++ b/components/wifi.c @@ -25,20 +25,17 @@ snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface, "/operstate"); - fp = fopen(path, "r"); - if (fp == NULL) { + if (!(fp = fopen(path, "r"))) { fprintf(stderr, "fopen '%s': %s\n", path, strerror(errno)); return NULL; } - p = fgets(status, 5, fp); - fclose(fp); - if(!p || strcmp(status, "up\n") != 0) { + if(!(p = fgets(status, 5, fp)) || strcmp(status, "up\n") != 0) { return NULL; } + fclose(fp); - fp = fopen("/proc/net/wireless", "r"); - if (fp == NULL) { + if (!(fp = fopen("/proc/net/wireless", "r"))) { fprintf(stderr, "fopen '/proc/net/wireless': %s\n", strerror(errno)); return NULL; @@ -92,8 +89,8 @@ if (strcmp(id, "") == 0) return NULL; - else - return id; + + return id; } #elif defined(__OpenBSD__) /* unimplemented */ -- 2.20.1 From 34010907a6fdcb2b3fbd09eb06740e5f9f8d1e4f Mon Sep 17 00:00:00 2001 From: parazyd Date: Wed, 2 May 2018 11:38:27 +0200 Subject: [PATCH 09/16] volume: Cast SOUND_MIXER_READ_DEVMASK to int to avoid warning. --- components/volume.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/volume.c b/components/volume.c index 7c831c9..41a112d 100644 --- a/components/volume.c +++ b/components/volume.c @@ -26,7 +26,7 @@ vol_perc(const char *card) return NULL; } - if (ioctl(afd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) { + if (ioctl(afd, (int)SOUND_MIXER_READ_DEVMASK, &devmask) == -1) { fprintf(stderr, "ioctl 'SOUND_MIXER_READ_DEVMASK': %s\n", strerror(errno)); close(afd); return NULL; -- 2.20.1 From 54ebd0dabe3594072c1920ecbbebae390aa186ce Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Wed, 2 May 2018 12:08:54 +0200 Subject: [PATCH 10/16] uptime: Add missing brace --- components/uptime.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/uptime.c b/components/uptime.c index 19908b9..f662b40 100644 --- a/components/uptime.c +++ b/components/uptime.c @@ -49,7 +49,7 @@ format(int uptime) size = sizeof(boottime); - if (sysctl(mib, 2, &boottime, &size, NULL, 0) == -1) + if (sysctl(mib, 2, &boottime, &size, NULL, 0) == -1) { fprintf(stderr, "sysctl 'KERN_BOOTTIME': %s\n", strerror(errno)); return NULL; } -- 2.20.1 From af3cdfbbc8dfbdb723fdfef8ac08a89fdee25f2e Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Wed, 2 May 2018 12:30:16 +0200 Subject: [PATCH 11/16] Add examples to config.def.h --- config.def.h | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/config.def.h b/config.def.h index d1cb995..f94e978 100644 --- a/config.def.h +++ b/config.def.h @@ -10,43 +10,46 @@ static const char unknown_str[] = "n/a"; #define MAXLEN 2048 /* - * function description argument + * function description argument (example) * - * battery_perc battery percentage battery name - * battery_state battery charging state battery name + * battery_perc battery percentage battery name (BAT0) + * battery_state battery charging state battery name (BAT0) * cpu_perc cpu usage in percent NULL * cpu_iowait cpu iowait in percent NULL * cpu_freq cpu frequency in MHz NULL - * datetime date and time format string - * disk_free free disk space in GB mountpoint path - * disk_perc disk usage in percent mountpoint path - * disk_total total disk space in GB mountpoint path - * disk_used used disk space in GB mountpoint path + * datetime date and time format string (%F %T) + * disk_free free disk space in GB mountpoint path (/) + * disk_perc disk usage in percent mountpoint path (/) + * disk_total total disk space in GB mountpoint path (/") + * disk_used used disk space in GB mountpoint path (/) * entropy available entropy NULL * gid GID of current user NULL * hostname hostname NULL - * ipv4 IPv4 address interface name - * ipv6 IPv6 address interface name + * ipv4 IPv4 address interface name (eth0) + * ipv6 IPv6 address interface name (eth0) * kernel_release `uname -r` NULL * keyboard_indicators caps/num lock indicators NULL * load_avg load average format string + * (%.2f %.2f %.2f) * num_files number of files in a directory path + * (/home/foo/Inbox/cur) * ram_free free memory in GB NULL * ram_perc memory usage in percent NULL * ram_total total memory size in GB NULL * ram_used used memory in GB NULL - * run_command custom shell command command + * run_command custom shell command command (echo foo) * swap_free free swap in GB NULL * swap_perc swap usage in percent NULL * swap_total total swap size in GB NULL * swap_used used swap in GB NULL * temp temperature in degree celsius sensor file + * (/sys/class/thermal/...) * uid UID of current user NULL * uptime system uptime NULL * username username of current user NULL - * vol_perc OSS/ALSA volume in percent "/dev/mixer" - * wifi_perc WiFi signal in percent interface name - * wifi_essid WiFi ESSID interface name + * vol_perc OSS/ALSA volume in percent mixer file (/dev/mixer) + * wifi_perc WiFi signal in percent interface name (wlan0) + * wifi_essid WiFi ESSID interface name (wlan0) */ static const struct arg args[] = { /* function format argument */ -- 2.20.1 From 699f6734d99ac67f9f1dceae2b7aeee8cb105d2f Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Wed, 2 May 2018 19:41:53 +0200 Subject: [PATCH 12/16] wifi_perc: Fix file descriptor leak --- components/wifi.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/components/wifi.c b/components/wifi.c index 13b630a..7cd2702 100644 --- a/components/wifi.c +++ b/components/wifi.c @@ -30,10 +30,11 @@ strerror(errno)); return NULL; } - if(!(p = fgets(status, 5, fp)) || strcmp(status, "up\n") != 0) { + p = fgets(status, 5, fp); + fclose(fp); + if(!p || strcmp(status, "up\n") != 0) { return NULL; } - fclose(fp); if (!(fp = fopen("/proc/net/wireless", "r"))) { fprintf(stderr, "fopen '/proc/net/wireless': %s\n", -- 2.20.1 From 417f473fe2c5b54543d57d97450b8e2c2ebeba50 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Fri, 4 May 2018 18:04:53 +0200 Subject: [PATCH 13/16] battery: Remove full indicator It does not respect charging thresholds and it is not trivial to implement the indicator in OpenBSD. --- components/battery.c | 1 - 1 file changed, 1 deletion(-) diff --git a/components/battery.c b/components/battery.c index e997e4c..fe17a68 100644 --- a/components/battery.c +++ b/components/battery.c @@ -29,7 +29,6 @@ } map[] = { { "Charging", "+" }, { "Discharging", "-" }, - { "Full", "=" }, }; size_t i; char path[PATH_MAX], state[12]; -- 2.20.1 From efb41724b5499bf61ab65ff65908d686e59ecdc0 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Sun, 6 May 2018 01:20:46 +0200 Subject: [PATCH 14/16] swap_*: Port to OpenBSD --- README | 1 - components/swap.c | 90 +++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 84 insertions(+), 7 deletions(-) diff --git a/README b/README index c0977aa..0961030 100644 --- a/README +++ b/README @@ -64,4 +64,3 @@ The following functions are not portable at the moment: - wifi_{perc,essid} - cpu_{perc,iowait} - entropy -- swap_{free,perc,total,used} diff --git a/components/swap.c b/components/swap.c index fe779db..f6f8993 100644 --- a/components/swap.c +++ b/components/swap.c @@ -1,11 +1,11 @@ /* See LICENSE file for copyright and license details. */ -#if defined(__linux__) - #include - #include - #include +#include +#include +#include - #include "../util.h" +#include "../util.h" +#if defined(__linux__) static size_t pread(const char *path, char *buf, size_t bufsiz) { @@ -118,5 +118,83 @@ return bprintf("%f", (float)(total - free - cached) / 1024 / 1024); } #elif defined(__OpenBSD__) - /* unimplemented */ + #include + #include /* dbtob */ + #include + #include + #include + + #define dbtoqb(b) dbtob((int64_t)(b)) + + static void + getstats(int *total, int *used) + { + struct swapent *sep, *fsep; + int rnswap, nswap, i; + + nswap = swapctl(SWAP_NSWAP, 0, 0); + if (nswap < 1) + fprintf(stderr, "swaptctl 'SWAP_NSWAP': %s\n", strerror(errno)); + + fsep = sep = calloc(nswap, sizeof(*sep)); + if (sep == NULL) + fprintf(stderr, "calloc 'nswap': %s\n", strerror(errno)); + + rnswap = swapctl(SWAP_STATS, (void *)sep, nswap); + if (rnswap < 0) + fprintf(stderr, "swapctl 'SWAP_STATA': %s\n", strerror(errno)); + + if (nswap != rnswap) + fprintf(stderr, "SWAP_STATS != SWAP_NSWAP\n"); + + *total = 0; + *used = 0; + + for (i = 0; i < rnswap; i++) { + *total += dbtoqb(sep->se_nblks); + *used += dbtoqb(sep->se_inuse); + } + + free(fsep); + } + + const char * + swap_free(void) + { + int total, used; + + getstats(&total, &used); + + return bprintf("%f", (float)(total - used) / 1024 / 1024 / 1024); + } + + const char * + swap_perc(void) + { + int total, used; + + getstats(&total, &used); + + return bprintf("%d", 100 * used / total); + } + + const char * + swap_total(void) + { + int total, used; + + getstats(&total, &used); + + return bprintf("%f", (float)total / 1024 / 1024 / 1024); + } + + const char * + swap_used(void) + { + int total, used; + + getstats(&total, &used); + + return bprintf("%f", (float)used / 1024 / 1024 / 1024); + } #endif -- 2.20.1 From 2b82bf02488147a3e69a715267934f015fe29a43 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Sun, 6 May 2018 17:48:37 +0200 Subject: [PATCH 15/16] cpu_perc: Port to OpenBSD In OpenBSD the CPU usage in percent is now computed using KERN_CPTIME sysctl. --- README | 2 +- components/cpu.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/README b/README index 0961030..805f541 100644 --- a/README +++ b/README @@ -62,5 +62,5 @@ Porting to OpenBSD is the current goal before thinking about a release. The following functions are not portable at the moment: - wifi_{perc,essid} -- cpu_{perc,iowait} +- cpu_iowait - entropy diff --git a/components/cpu.c b/components/cpu.c index e7f4cd9..79bb881 100644 --- a/components/cpu.c +++ b/components/cpu.c @@ -68,6 +68,8 @@ return bprintf("%d", perc); } #elif defined(__OpenBSD__) + #include + #include #include const char * @@ -89,4 +91,37 @@ return bprintf("%d", freq); } + + const char * + cpu_perc(void) + { + int mib[2], perc; + static int valid; + static long int a[CPUSTATES]; + long int b[CPUSTATES]; + 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) == -1) { + fprintf(stderr, "sysctl 'KERN_CPTIME': %s\n", strerror(errno)); + return NULL; + } + if (!valid) { + valid = 1; + return NULL; + } + + perc = 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])) / + ((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 bprintf("%d", perc); + } #endif -- 2.20.1 From 235fa3c3065a93d24efb06d0dc8a9fffae5d32cd Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Sun, 6 May 2018 21:10:02 +0200 Subject: [PATCH 16/16] cpu_perc: Documentation and readbility improvements --- components/cpu.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/components/cpu.c b/components/cpu.c index 79bb881..661c27c 100644 --- a/components/cpu.c +++ b/components/cpu.c @@ -25,6 +25,7 @@ long double b[7]; 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; @@ -34,11 +35,10 @@ return NULL; } - perc = 100 * - ((b[0]+b[1]+b[2]+b[5]+b[6]) - - (a[0]+a[1]+a[2]+a[5]+a[6])) / - ((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])); + perc = 100 * ((b[0] + b[1] + b[2] + b[5] + b[6]) - + (a[0] + a[1] + a[2] + a[5] + a[6])) / + ((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])); return bprintf("%d", perc); } @@ -117,10 +117,10 @@ } perc = 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])) / - ((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])); + ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR]) - + (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR])) / + ((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 bprintf("%d", perc); } -- 2.20.1