diff --git a/LICENSE b/LICENSE index b88a9cb..8bee9c8 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ ISC License -Copyright 2016-2020 Aaron Marcher +Copyright 2016-2022 Aaron Marcher Copyright 2016 Roy Freytag Copyright 2016 Vincent Loupmon @@ -9,23 +9,26 @@ Copyright 2016-2018 Ali H. Fardan Copyright 2016 Jody Leonard Copyright 2016-2018 Quentin Rameau Copyright 2016 Mike Coddington -Copyright 2016-2018 parazyd +Copyright 2016-2018 Ivan J. Copyright 2017 Tobias Stoeckmann Copyright 2017-2018 Laslo Hunhold Copyright 2018 Darron Anderson Copyright 2018 Josuah Demangeon Copyright 2018 Tobias Tschinkowitz Copyright 2018 David Demelier -Copyright 2018-2019 Michael Buch +Copyright 2018-2012 Michael Buch Copyright 2018 Ian Remmler Copyright 2016-2019 Joerg Jung Copyright 2019 Ryan Kes Copyright 2019 Cem Keylan -Copyright 2019 dsp -Copyright 2019-2020 Ingo Feinerer +Copyright 2019 Dimitris Papastamos +Copyright 2019-2022 Ingo Feinerer Copyright 2020 Alexandre Ratchov Copyright 2020 Mart Lubbers Copyright 2020 Daniel Moch +Copyright 2022 Nickolas Raymond Kaczynski +Copyright 2022 Patrick Iacob +Copyright 2021-2022 Steven Ward Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above diff --git a/Makefile b/Makefile index 2f93b87..7a18274 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ include config.mk REQ = util COM =\ components/battery\ + components/cat\ components/cpu\ components/datetime\ components/disk\ @@ -21,7 +22,6 @@ COM =\ components/num_files\ components/ram\ components/run_command\ - components/separator\ components/swap\ components/temperature\ components/uptime\ @@ -31,7 +31,7 @@ COM =\ all: slstatus -$(COM:=.o): config.mk $(REQ:=.h) +$(COM:=.o): config.mk $(REQ:=.h) slstatus.h slstatus.o: slstatus.c slstatus.h arg.h config.h config.mk $(REQ:=.h) .c.o: @@ -44,14 +44,15 @@ slstatus: slstatus.o $(COM:=.o) $(REQ:=.o) $(CC) -o $@ $(LDFLAGS) $(COM:=.o) $(REQ:=.o) slstatus.o $(LDLIBS) clean: - rm -f slstatus slstatus.o $(COM:=.o) $(REQ:=.o) + rm -f slstatus slstatus.o $(COM:=.o) $(REQ:=.o) slstatus-${VERSION}.tar.gz dist: rm -rf "slstatus-$(VERSION)" mkdir -p "slstatus-$(VERSION)/components" cp -R LICENSE Makefile README config.mk config.def.h \ - arg.h slstatus.c $(COM:=.c) $(REQ:=.c) $(REQ:=.h) \ + arg.h slstatus.h slstatus.c $(REQ:=.c) $(REQ:=.h) \ slstatus.1 "slstatus-$(VERSION)" + cp -R $(COM:=.c) "slstatus-$(VERSION)/components" tar -cf - "slstatus-$(VERSION)" | gzip -c > "slstatus-$(VERSION).tar.gz" rm -rf "slstatus-$(VERSION)" diff --git a/README b/README index 86fe988..12d38bf 100644 --- a/README +++ b/README @@ -1,12 +1,15 @@ slstatus - suckless status ========================== -slstatus is a suckless status monitor for window managers that use WM_NAME -(e.g. dwm) or stdin to fill the status bar. +slstatus is a small tool for providing system status information to other +programs over the EWMH property of the root window (used by dwm(1)) or +standard input/output. It is designed to be as efficient as possible by +only issuing the minimum of system calls required. Features -------- - Battery percentage/state/time left +- Cat (read file) - CPU usage - CPU frequency - Custom shell commands @@ -35,6 +38,10 @@ Requirements Currently slstatus works on FreeBSD, Linux and OpenBSD. In order to build slstatus you need the Xlib header files. +- For volume percentage on Linux the kernel module `snd-mixer-oss` must be + loaded. +- For volume percentage on FreeBSD, `sndio` must be installed. + Installation ------------ @@ -56,10 +63,3 @@ Configuration ------------- slstatus can be customized by creating a custom config.h and (re)compiling the source code. This keeps it fast, secure and simple. - - -Upcoming --------- - -A release (v1.0) will come soon... ;) -After a long phase of inactivity, development has been continued! diff --git a/arg.h b/arg.h index e0adb9f..5f1f408 100644 --- a/arg.h +++ b/arg.h @@ -13,7 +13,7 @@ extern char *argv0; break; \ } \ for (i_ = 1, argused_ = 0; (*argv)[i_]; i_++) { \ - switch((*argv)[i_]) + switch ((*argv)[i_]) #define ARGEND if (argused_) { \ if ((*argv)[i_ + 1]) { \ break; \ @@ -24,7 +24,7 @@ extern char *argv0; } \ } \ } -#define ARGC() ((*argv)[i_]) +#define ARGC() ((*argv)[i_]) #define ARGF_(x) (((*argv)[i_ + 1]) ? (argused_ = 1, &((*argv)[i_ + 1])) : \ (*(argv + 1)) ? (argused_ = 1, *(argv + 1)) : (x)) #define EARGF(x) ARGF_(((x), exit(1), (char *)0)) diff --git a/components/battery.c b/components/battery.c index f2b0f14..1c753f9 100644 --- a/components/battery.c +++ b/components/battery.c @@ -2,26 +2,35 @@ #include #include +#include "../slstatus.h" #include "../util.h" #if defined(__linux__) +/* + * https://www.kernel.org/doc/html/latest/power/power_supply_class.html + */ #include #include #include + #define POWER_SUPPLY_CAPACITY "/sys/class/power_supply/%s/capacity" + #define POWER_SUPPLY_STATUS "/sys/class/power_supply/%s/status" + #define POWER_SUPPLY_CHARGE "/sys/class/power_supply/%s/charge_now" + #define POWER_SUPPLY_ENERGY "/sys/class/power_supply/%s/energy_now" + #define POWER_SUPPLY_CURRENT "/sys/class/power_supply/%s/current_now" + #define POWER_SUPPLY_POWER "/sys/class/power_supply/%s/power_now" + 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) { + access(path, R_OK) == 0) return f1; - } if (esnprintf(path, length, f2, bat) > 0 && - access(path, R_OK) == 0) { + access(path, R_OK) == 0) return f2; - } return NULL; } @@ -29,18 +38,15 @@ const char * battery_perc(const char *bat) { - int perc; + int cap_perc; char path[PATH_MAX]; - if (esnprintf(path, sizeof(path), - "/sys/class/power_supply/%s/capacity", bat) < 0) { + if (esnprintf(path, sizeof(path), POWER_SUPPLY_CAPACITY, bat) < 0) return NULL; - } - if (pscanf(path, "%d", &perc) != 1) { + if (pscanf(path, "%d", &cap_perc) != 1) return NULL; - } - return bprintf("%d", perc); + return bprintf("%d", cap_perc); } const char * @@ -53,23 +59,20 @@ { "Charging", "+" }, { "Discharging", "-" }, { "Full", "o" }, + { "Not charging", "o" }, }; size_t i; char path[PATH_MAX], state[12]; - if (esnprintf(path, sizeof(path), - "/sys/class/power_supply/%s/status", bat) < 0) { + if (esnprintf(path, sizeof(path), POWER_SUPPLY_STATUS, bat) < 0) return NULL; - } - if (pscanf(path, "%12s", state) != 1) { + if (pscanf(path, "%12[a-zA-Z ]", state) != 1) return NULL; - } - for (i = 0; i < LEN(map); i++) { - if (!strcmp(map[i].state, state)) { + for (i = 0; i < LEN(map); i++) + if (!strcmp(map[i].state, state)) break; - } - } + return (i == LEN(map)) ? "?" : map[i].symbol; } @@ -80,32 +83,24 @@ double timeleft; char path[PATH_MAX], state[12]; - if (esnprintf(path, sizeof(path), - "/sys/class/power_supply/%s/status", bat) < 0) { + if (esnprintf(path, sizeof(path), POWER_SUPPLY_STATUS, bat) < 0) return NULL; - } - if (pscanf(path, "%12s", state) != 1) { + if (pscanf(path, "%12[a-zA-Z ]", state) != 1) return NULL; - } - if (!pick(bat, "/sys/class/power_supply/%s/charge_now", - "/sys/class/power_supply/%s/energy_now", path, + if (!pick(bat, POWER_SUPPLY_CHARGE, POWER_SUPPLY_ENERGY, path, sizeof(path)) || - pscanf(path, "%ju", &charge_now) < 0) { + pscanf(path, "%ju", &charge_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, + if (!pick(bat, POWER_SUPPLY_CURRENT, POWER_SUPPLY_POWER, path, sizeof(path)) || - pscanf(path, "%ju", ¤t_now) < 0) { + pscanf(path, "%ju", ¤t_now) < 0) return NULL; - } - if (current_now == 0) { + if (current_now == 0) return NULL; - } timeleft = (double)charge_now / (double)current_now; h = timeleft; @@ -147,9 +142,8 @@ { struct apm_power_info apm_info; - if (load_apm_power_info(&apm_info)) { + if (load_apm_power_info(&apm_info)) return bprintf("%d", apm_info.battery_life); - } return NULL; } @@ -168,11 +162,10 @@ 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) { + for (i = 0; i < LEN(map); i++) + if (map[i].state == apm_info.ac_state) break; - } - } + return (i == LEN(map)) ? "?" : map[i].symbol; } @@ -183,12 +176,13 @@ battery_remaining(const char *unused) { struct apm_power_info apm_info; + unsigned int h, m; 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); + h = apm_info.minutes_left / 60; + m = apm_info.minutes_left % 60; + return bprintf("%uh %02um", h, m); } else { return ""; } @@ -199,18 +193,21 @@ #elif defined(__FreeBSD__) #include + #define BATTERY_LIFE "hw.acpi.battery.life" + #define BATTERY_STATE "hw.acpi.battery.state" + #define BATTERY_TIME "hw.acpi.battery.time" + const char * battery_perc(const char *unused) { - int cap; + int cap_perc; size_t len; - len = sizeof(cap); - if (sysctlbyname("hw.acpi.battery.life", &cap, &len, NULL, 0) == -1 - || !len) + len = sizeof(cap_perc); + if (sysctlbyname(BATTERY_LIFE, &cap_perc, &len, NULL, 0) < 0 || !len) return NULL; - return bprintf("%d", cap); + return bprintf("%d", cap_perc); } const char * @@ -220,18 +217,17 @@ size_t len; len = sizeof(state); - if (sysctlbyname("hw.acpi.battery.state", &state, &len, NULL, 0) == -1 - || !len) + if (sysctlbyname(BATTERY_STATE, &state, &len, NULL, 0) < 0 || !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 "?"; } } @@ -242,9 +238,8 @@ size_t len; len = sizeof(rem); - if (sysctlbyname("hw.acpi.battery.time", &rem, &len, NULL, 0) == -1 - || !len - || rem == -1) + if (sysctlbyname(BATTERY_TIME, &rem, &len, NULL, 0) < 0 || !len + || rem < 0) return NULL; return bprintf("%uh %02um", rem / 60, rem % 60); diff --git a/components/cat.c b/components/cat.c new file mode 100644 index 0000000..07944cc --- /dev/null +++ b/components/cat.c @@ -0,0 +1,32 @@ +/* See LICENSE file for copyright and license details. */ +#include +#include + +#include "../slstatus.h" +#include "../util.h" + +const char * +cat(const char *path) +{ + char *f; + FILE *fp; + + if (!(fp = fopen(path, "r"))) { + warn("fopen '%s':", path); + return NULL; + } + + f = fgets(buf, sizeof(buf) - 1, fp); + if (fclose(fp) < 0) { + warn("fclose '%s':", path); + return NULL; + } + if (!f) + return NULL; + + if ((f = strrchr(buf, '\n'))) + f[0] = '\0'; + + return buf[0] ? buf : NULL; +} + diff --git a/components/cpu.c b/components/cpu.c index 9e28003..d0d03c7 100644 --- a/components/cpu.c +++ b/components/cpu.c @@ -3,25 +3,26 @@ #include #include +#include "../slstatus.h" #include "../util.h" #if defined(__linux__) + #define CPU_FREQ "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq" + const char * - cpu_freq(void) + cpu_freq(const char *unused) { uintmax_t freq; /* in kHz */ - if (pscanf("/sys/devices/system/cpu/cpu0/cpufreq/" - "scaling_cur_freq", "%ju", &freq) != 1) { + if (pscanf(CPU_FREQ, "%ju", &freq) != 1) return NULL; - } return fmt_human(freq * 1000, 1000); } const char * - cpu_perc(void) + cpu_perc(const char *unused) { static long double a[7]; long double b[7], sum; @@ -30,19 +31,17 @@ /* 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) { + != 7) return NULL; - } - if (b[0] == 0) { + + 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) { + if (sum == 0) return NULL; - } return bprintf("%d", (int)(100 * ((b[0] + b[1] + b[2] + b[5] + b[6]) - @@ -54,7 +53,7 @@ #include const char * - cpu_freq(void) + cpu_freq(const char *unused) { int freq, mib[2]; size_t size; @@ -74,7 +73,7 @@ } const char * - cpu_perc(void) + cpu_perc(const char *unused) { int mib[2]; static uintmax_t a[CPUSTATES]; @@ -91,16 +90,14 @@ warn("sysctl 'KERN_CPTIME':"); return NULL; } - if (b[0] == 0) { + 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) { + if (sum == 0) return NULL; - } return bprintf("%d", 100 * ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + @@ -109,20 +106,19 @@ b[CP_INTR])) / sum); } #elif defined(__FreeBSD__) + #include #include #include - #include const char * - cpu_freq(void) + cpu_freq(const char *unused) { int freq; size_t size; size = sizeof(freq); /* in MHz */ - if (sysctlbyname("hw.clockrate", &freq, &size, NULL, 0) == -1 - || !size) { + if (sysctlbyname("hw.clockrate", &freq, &size, NULL, 0) < 0 || !size) { warn("sysctlbyname 'hw.clockrate':"); return NULL; } @@ -131,7 +127,7 @@ } const char * - cpu_perc(void) + cpu_perc(const char *unused) { size_t size; static long a[CPUSTATES]; @@ -139,21 +135,18 @@ size = sizeof(a); memcpy(b, a, sizeof(b)); - if (sysctlbyname("kern.cp_time", &a, &size, NULL, 0) == -1 - || !size) { + if (sysctlbyname("kern.cp_time", &a, &size, NULL, 0) < 0 || !size) { warn("sysctlbyname 'kern.cp_time':"); return NULL; } - if (b[0] == 0) { + 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) { + if (sum == 0) return NULL; - } return bprintf("%d", 100 * ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + diff --git a/components/datetime.c b/components/datetime.c index c3efae3..5b10daf 100644 --- a/components/datetime.c +++ b/components/datetime.c @@ -2,6 +2,7 @@ #include #include +#include "../slstatus.h" #include "../util.h" const char * diff --git a/components/disk.c b/components/disk.c index 15a221b..e19a693 100644 --- a/components/disk.c +++ b/components/disk.c @@ -2,6 +2,7 @@ #include #include +#include "../slstatus.h" #include "../util.h" const char * @@ -28,7 +29,7 @@ disk_perc(const char *path) } return bprintf("%d", (int)(100 * - (1.0f - ((float)fs.f_bavail / (float)fs.f_blocks)))); + (1 - ((double)fs.f_bavail / (double)fs.f_blocks)))); } const char * diff --git a/components/entropy.c b/components/entropy.c index 2a485de..65010b0 100644 --- a/components/entropy.c +++ b/components/entropy.c @@ -1,27 +1,29 @@ /* See LICENSE file for copyright and license details. */ +#include "../slstatus.h" #if defined(__linux__) #include #include #include "../util.h" + #define ENTROPY_AVAIL "/proc/sys/kernel/random/entropy_avail" + const char * - entropy(void) + entropy(const char *unused) { uintmax_t num; - if (pscanf("/proc/sys/kernel/random/entropy_avail", "%ju", &num) - != 1) { + if (pscanf(ENTROPY_AVAIL, "%ju", &num) != 1) return NULL; - } return bprintf("%ju", num); } #elif defined(__OpenBSD__) | defined(__FreeBSD__) const char * - entropy(void) + entropy(const char *unused) { + // https://www.unicode.org/charts/PDF/U2200.pdf /* Unicode Character 'INFINITY' (U+221E) */ - return "\xe2\x88\x9e"; + return "\u221E"; } #endif diff --git a/components/hostname.c b/components/hostname.c index 23da677..dab8b63 100644 --- a/components/hostname.c +++ b/components/hostname.c @@ -2,10 +2,11 @@ #include #include +#include "../slstatus.h" #include "../util.h" const char * -hostname(void) +hostname(const char *unused) { if (gethostname(buf, sizeof(buf)) < 0) { warn("gethostbyname:"); diff --git a/components/ip.c b/components/ip.c index 70724eb..9476549 100644 --- a/components/ip.c +++ b/components/ip.c @@ -4,13 +4,14 @@ #include #include #if defined(__OpenBSD__) - #include #include + #include #elif defined(__FreeBSD__) #include #include #endif +#include "../slstatus.h" #include "../util.h" static const char * @@ -26,9 +27,9 @@ 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) && diff --git a/components/kernel_release.c b/components/kernel_release.c index 0457301..36a6a44 100644 --- a/components/kernel_release.c +++ b/components/kernel_release.c @@ -1,11 +1,12 @@ /* See LICENSE file for copyright and license details. */ -#include #include +#include +#include "../slstatus.h" #include "../util.h" const char * -kernel_release(void) +kernel_release(const char *unused) { struct utsname udata; diff --git a/components/keyboard_indicators.c b/components/keyboard_indicators.c index b35eba1..5f62bb7 100644 --- a/components/keyboard_indicators.c +++ b/components/keyboard_indicators.c @@ -4,6 +4,7 @@ #include #include +#include "../slstatus.h" #include "../util.h" /* @@ -32,17 +33,18 @@ 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..f8a2a47 100644 --- a/components/keymap.c +++ b/components/keymap.c @@ -5,6 +5,7 @@ #include #include +#include "../slstatus.h" #include "../util.h" static int @@ -14,11 +15,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; } @@ -46,12 +45,13 @@ get_layout(char *syms, int grp_num) } const char * -keymap(void) +keymap(const char *unused) { Display *dpy; XkbDescRec *desc; XkbStateRec state; - char *symbols, *layout; + char *symbols; + const char *layout; layout = NULL; @@ -75,13 +75,12 @@ keymap(void) warn("XGetAtomName: Failed to get atom name"); goto end; } - layout = (char *)bprintf("%s", get_layout(symbols, state.group)); + layout = bprintf("%s", get_layout(symbols, state.group)); 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/load_avg.c b/components/load_avg.c index 5c2e252..f278a40 100644 --- a/components/load_avg.c +++ b/components/load_avg.c @@ -2,10 +2,11 @@ #include #include +#include "../slstatus.h" #include "../util.h" const char * -load_avg(void) +load_avg(const char *unused) { double avgs[3]; diff --git a/components/netspeeds.c b/components/netspeeds.c index 0029177..cde6fa9 100644 --- a/components/netspeeds.c +++ b/components/netspeeds.c @@ -1,12 +1,16 @@ /* See LICENSE file for copyright and license details. */ -#include #include +#include +#include "../slstatus.h" #include "../util.h" #if defined(__linux__) #include + #define NET_RX_BYTES "/sys/class/net/%s/statistics/rx_bytes" + #define NET_TX_BYTES "/sys/class/net/%s/statistics/tx_bytes" + const char * netspeed_rx(const char *interface) { @@ -17,17 +21,12 @@ oldrxbytes = rxbytes; - if (esnprintf(path, sizeof(path), - "/sys/class/net/%s/statistics/rx_bytes", - interface) < 0) { + if (esnprintf(path, sizeof(path), NET_RX_BYTES, interface) < 0) return NULL; - } - if (pscanf(path, "%ju", &rxbytes) != 1) { + if (pscanf(path, "%ju", &rxbytes) != 1) return NULL; - } - if (oldrxbytes == 0) { + if (oldrxbytes == 0) return NULL; - } return fmt_human((rxbytes - oldrxbytes) * 1000 / interval, 1024); @@ -43,27 +42,22 @@ oldtxbytes = txbytes; - if (esnprintf(path, sizeof(path), - "/sys/class/net/%s/statistics/tx_bytes", - interface) < 0) { + if (esnprintf(path, sizeof(path), NET_TX_BYTES, interface) < 0) return NULL; - } - if (pscanf(path, "%ju", &txbytes) != 1) { + if (pscanf(path, "%ju", &txbytes) != 1) return NULL; - } - if (oldtxbytes == 0) { + if (oldtxbytes == 0) return NULL; - } return fmt_human((txbytes - oldtxbytes) * 1000 / interval, 1024); } #elif defined(__OpenBSD__) | defined(__FreeBSD__) - #include #include + #include + #include #include #include - #include const char * netspeed_rx(const char *interface) @@ -77,25 +71,23 @@ oldrxbytes = rxbytes; - if (getifaddrs(&ifal) == -1) { + if (getifaddrs(&ifal) < 0) { warn("getifaddrs failed"); return NULL; } rxbytes = 0; - for (ifa = ifal; ifa; ifa = ifa->ifa_next) { + for (ifa = ifal; ifa; ifa = ifa->ifa_next) if (!strcmp(ifa->ifa_name, interface) && - (ifd = (struct if_data *)ifa->ifa_data)) { + (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) { + if (oldrxbytes == 0) return NULL; - } return fmt_human((rxbytes - oldrxbytes) * 1000 / interval, 1024); @@ -113,25 +105,23 @@ oldtxbytes = txbytes; - if (getifaddrs(&ifal) == -1) { + if (getifaddrs(&ifal) < 0) { warn("getifaddrs failed"); return NULL; } txbytes = 0; - for (ifa = ifal; ifa; ifa = ifa->ifa_next) { + for (ifa = ifal; ifa; ifa = ifa->ifa_next) if (!strcmp(ifa->ifa_name, interface) && - (ifd = (struct if_data *)ifa->ifa_data)) { + (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) { + if (oldtxbytes == 0) return NULL; - } return fmt_human((txbytes - oldtxbytes) * 1000 / interval, 1024); diff --git a/components/num_files.c b/components/num_files.c index fb55df9..df0acd1 100644 --- a/components/num_files.c +++ b/components/num_files.c @@ -3,29 +3,30 @@ #include #include +#include "../slstatus.h" #include "../util.h" 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 index 47e6fda..15c4b74 100644 --- a/components/ram.c +++ b/components/ram.c @@ -1,13 +1,14 @@ /* See LICENSE file for copyright and license details. */ #include +#include "../slstatus.h" #include "../util.h" #if defined(__linux__) #include const char * - ram_free(void) + ram_free(const char *unused) { uintmax_t free; @@ -15,17 +16,17 @@ "MemTotal: %ju kB\n" "MemFree: %ju kB\n" "MemAvailable: %ju kB\n", - &free, &free, &free) != 3) { + &free, &free, &free) != 3) return NULL; - } return fmt_human(free * 1024, 1024); } const char * - ram_perc(void) + ram_perc(const char *unused) { uintmax_t total, free, buffers, cached; + int percent; if (pscanf("/proc/meminfo", "MemTotal: %ju kB\n" @@ -33,35 +34,32 @@ "MemAvailable: %ju kB\n" "Buffers: %ju kB\n" "Cached: %ju kB\n", - &total, &free, &buffers, &buffers, &cached) != 5) { + &total, &free, &buffers, &buffers, &cached) != 5) return NULL; - } - if (total == 0) { + if (total == 0) return NULL; - } - return bprintf("%d", 100 * ((total - free) - (buffers + cached)) - / total); + percent = 100 * ((total - free) - (buffers + cached)) / total; + return bprintf("%d", percent); } const char * - ram_total(void) + ram_total(const char *unused) { uintmax_t total; if (pscanf("/proc/meminfo", "MemTotal: %ju kB\n", &total) - != 1) { + != 1) return NULL; - } return fmt_human(total * 1024, 1024); } const char * - ram_used(void) + ram_used(const char *unused) { - uintmax_t total, free, buffers, cached; + uintmax_t total, free, buffers, cached, used; if (pscanf("/proc/meminfo", "MemTotal: %ju kB\n" @@ -69,12 +67,11 @@ "MemAvailable: %ju kB\n" "Buffers: %ju kB\n" "Cached: %ju kB\n", - &total, &free, &buffers, &buffers, &cached) != 5) { + &total, &free, &buffers, &buffers, &cached) != 5) return NULL; - } - return fmt_human((total - free - buffers - cached) * 1024, - 1024); + used = (total - free - buffers - cached); + return fmt_human(used * 1024, 1024); } #elif defined(__OpenBSD__) #include @@ -93,68 +90,61 @@ size = sizeof(*uvmexp); - if (sysctl(uvmexp_mib, 2, uvmexp, &size, NULL, 0) >= 0) { + if (sysctl(uvmexp_mib, 2, uvmexp, &size, NULL, 0) >= 0) return 1; - } return 0; } const char * - ram_free(void) + ram_free(const char *unused) { 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); - } + if (!load_uvmexp(&uvmexp)) + return NULL; - return NULL; + free_pages = uvmexp.npages - uvmexp.active; + return fmt_human(pagetok(free_pages, uvmexp.pageshift) * + 1024, 1024); } const char * - ram_perc(void) + ram_perc(const char *unused) { struct uvmexp uvmexp; int percent; - if (load_uvmexp(&uvmexp)) { - percent = uvmexp.active * 100 / uvmexp.npages; - return bprintf("%d", percent); - } + if (!load_uvmexp(&uvmexp)) + return NULL; - return NULL; + percent = uvmexp.active * 100 / uvmexp.npages; + return bprintf("%d", percent); } const char * - ram_total(void) + ram_total(const char *unused) { struct uvmexp uvmexp; - if (load_uvmexp(&uvmexp)) { - return fmt_human(pagetok(uvmexp.npages, - uvmexp.pageshift) * 1024, - 1024); - } + if (!load_uvmexp(&uvmexp)) + return NULL; - return NULL; + return fmt_human(pagetok(uvmexp.npages, + uvmexp.pageshift) * 1024, 1024); } const char * - ram_used(void) + ram_used(const char *unused) { struct uvmexp uvmexp; - if (load_uvmexp(&uvmexp)) { - return fmt_human(pagetok(uvmexp.active, - uvmexp.pageshift) * 1024, - 1024); - } + if (!load_uvmexp(&uvmexp)) + return NULL; - return NULL; + return fmt_human(pagetok(uvmexp.active, + uvmexp.pageshift) * 1024, 1024); } #elif defined(__FreeBSD__) #include @@ -163,58 +153,58 @@ #include const char * - ram_free(void) { + ram_free(const char *unused) { 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) + if (sysctl(mib, 2, &vm_stats, &len, NULL, 0) < 0 + || !len) return NULL; return fmt_human(vm_stats.t_free * getpagesize(), 1024); } const char * - ram_total(void) { - long npages; + ram_total(const char *unused) { + unsigned int npages; size_t len; len = sizeof(npages); - if (sysctlbyname("vm.stats.vm.v_page_count", &npages, &len, NULL, 0) == -1 - || !len) + if (sysctlbyname("vm.stats.vm.v_page_count", + &npages, &len, NULL, 0) < 0 || !len) return NULL; return fmt_human(npages * getpagesize(), 1024); } const char * - ram_perc(void) { - long npages; - long active; + ram_perc(const char *unused) { + unsigned int npages; + unsigned int active; size_t len; len = sizeof(npages); - if (sysctlbyname("vm.stats.vm.v_page_count", &npages, &len, NULL, 0) == -1 - || !len) + if (sysctlbyname("vm.stats.vm.v_page_count", + &npages, &len, NULL, 0) < 0 || !len) return NULL; - if (sysctlbyname("vm.stats.vm.v_active_count", &active, &len, NULL, 0) == -1 - || !len) + if (sysctlbyname("vm.stats.vm.v_active_count", + &active, &len, NULL, 0) < 0 || !len) return NULL; return bprintf("%d", active * 100 / npages); } const char * - ram_used(void) { - long active; + ram_used(const char *unused) { + unsigned int active; size_t len; len = sizeof(active); - if (sysctlbyname("vm.stats.vm.v_active_count", &active, &len, NULL, 0) == -1 - || !len) + if (sysctlbyname("vm.stats.vm.v_active_count", + &active, &len, NULL, 0) < 0 || !len) return NULL; return fmt_human(active * getpagesize(), 1024); diff --git a/components/run_command.c b/components/run_command.c index e00b478..93bf6da 100644 --- a/components/run_command.c +++ b/components/run_command.c @@ -2,6 +2,7 @@ #include #include +#include "../slstatus.h" #include "../util.h" const char * @@ -14,17 +15,17 @@ run_command(const char *cmd) warn("popen '%s':", cmd); return NULL; } + p = fgets(buf, sizeof(buf) - 1, fp); if (pclose(fp) < 0) { 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 deleted file mode 100644 index 40fec52..0000000 --- a/components/separator.c +++ /dev/null @@ -1,10 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include - -#include "../util.h" - -const char * -separator(const char *separator) -{ - return separator; -} diff --git a/components/swap.c b/components/swap.c index 2509db1..f270d93 100644 --- a/components/swap.c +++ b/components/swap.c @@ -4,6 +4,7 @@ #include #include +#include "../slstatus.h" #include "../util.h" #if defined(__linux__) @@ -24,11 +25,9 @@ char *line = NULL; /* get number of fields we want to extract */ - for (i = 0, left = 0; i < LEN(ent); i++) { - if (ent[i].var) { + for (i = 0, left = 0; i < LEN(ent); i++) + if (ent[i].var) left++; - } - } if (!(fp = fopen("/proc/meminfo", "r"))) { warn("fopen '/proc/meminfo':"); @@ -58,49 +57,45 @@ } const char * - swap_free(void) + swap_free(const char *unused) { long free; - if (get_swap_info(NULL, &free, NULL)) { + if (get_swap_info(NULL, &free, NULL)) return NULL; - } return fmt_human(free * 1024, 1024); } const char * - swap_perc(void) + swap_perc(const char *unused) { long total, free, cached; - if (get_swap_info(&total, &free, &cached) || total == 0) { + if (get_swap_info(&total, &free, &cached) || total == 0) return NULL; - } return bprintf("%d", 100 * (total - free - cached) / total); } const char * - swap_total(void) + swap_total(const char *unused) { long total; - if (get_swap_info(&total, NULL, NULL)) { + if (get_swap_info(&total, NULL, NULL)) return NULL; - } return fmt_human(total * 1024, 1024); } const char * - swap_used(void) + swap_used(const char *unused) { long total, free, cached; - if (get_swap_info(&total, &free, &cached)) { + if (get_swap_info(&total, &free, &cached)) return NULL; - } return fmt_human((total - free - cached) * 1024, 1024); } @@ -147,74 +142,69 @@ } const char * - swap_free(void) + swap_free(const char *unused) { int total, used; - if (getstats(&total, &used)) { + if (getstats(&total, &used)) return NULL; - } return fmt_human((total - used) * 1024, 1024); } const char * - swap_perc(void) + swap_perc(const char *unused) { int total, used; - if (getstats(&total, &used)) { + if (getstats(&total, &used)) return NULL; - } - if (total == 0) { + if (total == 0) return NULL; - } return bprintf("%d", 100 * used / total); } const char * - swap_total(void) + swap_total(const char *unused) { int total, used; - if (getstats(&total, &used)) { + if (getstats(&total, &used)) return NULL; - } return fmt_human(total * 1024, 1024); } const char * - swap_used(void) + swap_used(const char *unused) { int total, used; - if (getstats(&total, &used)) { + if (getstats(&total, &used)) return NULL; - } return fmt_human(used * 1024, 1024); } #elif defined(__FreeBSD__) + #include + #include #include #include - #include #include - #include 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) { + if (kd == NULL) { warn("kvm_openfiles '/dev/null':"); return 0; } - if(kvm_getswapinfo(kd, swap_info, size, 0 /* Unused flags */) == -1) { + if (kvm_getswapinfo(kd, swap_info, size, 0 /* Unused flags */) < 0) { warn("kvm_getswapinfo:"); kvm_close(kd); return 0; @@ -225,12 +215,12 @@ } const char * - swap_free(void) + swap_free(const char *unused) { struct kvm_swap swap_info[1]; long used, total; - if(!getswapinfo(swap_info, 1)) + if (!getswapinfo(swap_info, 1)) return NULL; total = swap_info[0].ksw_total; @@ -240,12 +230,12 @@ } const char * - swap_perc(void) + swap_perc(const char *unused) { struct kvm_swap swap_info[1]; long used, total; - if(!getswapinfo(swap_info, 1)) + if (!getswapinfo(swap_info, 1)) return NULL; total = swap_info[0].ksw_total; @@ -255,12 +245,12 @@ } const char * - swap_total(void) + swap_total(const char *unused) { struct kvm_swap swap_info[1]; long total; - if(!getswapinfo(swap_info, 1)) + if (!getswapinfo(swap_info, 1)) return NULL; total = swap_info[0].ksw_total; @@ -269,12 +259,12 @@ } const char * - swap_used(void) + swap_used(const char *unused) { struct kvm_swap swap_info[1]; long used; - if(!getswapinfo(swap_info, 1)) + if (!getswapinfo(swap_info, 1)) return NULL; used = swap_info[0].ksw_used; diff --git a/components/temperature.c b/components/temperature.c index 8e1f222..7cf1394 100644 --- a/components/temperature.c +++ b/components/temperature.c @@ -1,6 +1,7 @@ /* See LICENSE file for copyright and license details. */ #include +#include "../slstatus.h" #include "../util.h" @@ -12,9 +13,8 @@ { uintmax_t temp; - if (pscanf(file, "%ju", &temp) != 1) { + if (pscanf(file, "%ju", &temp) != 1) return NULL; - } return bprintf("%ju", temp / 1000); } @@ -52,6 +52,8 @@ #include #include + #define ACPI_TEMP "hw.acpi.thermal.%s.temperature" + const char * temp(const char *zone) { @@ -60,8 +62,8 @@ size_t len; len = sizeof(temp); - snprintf(buf, sizeof(buf), "hw.acpi.thermal.%s.temperature", zone); - if (sysctlbyname(buf, &temp, &len, NULL, 0) == -1 + snprintf(buf, sizeof(buf), ACPI_TEMP, zone); + if (sysctlbyname(buf, &temp, &len, NULL, 0) < 0 || !len) return NULL; diff --git a/components/uptime.c b/components/uptime.c index 7c23c98..6227f73 100644 --- a/components/uptime.c +++ b/components/uptime.c @@ -3,6 +3,7 @@ #include #include +#include "../slstatus.h" #include "../util.h" #if defined(CLOCK_BOOTTIME) @@ -14,14 +15,14 @@ #endif const char * -uptime(void) +uptime(const char *unused) { char warn_buf[256]; uintmax_t h, m; struct timespec uptime; if (clock_gettime(UPTIME_FLAG, &uptime) < 0) { - snprintf(warn_buf, 256, "clock_gettime %d", UPTIME_FLAG); + snprintf(warn_buf, sizeof(warn_buf), "clock_gettime %d", UPTIME_FLAG); warn(warn_buf); return NULL; } diff --git a/components/user.c b/components/user.c index 71a0c9d..3517495 100644 --- a/components/user.c +++ b/components/user.c @@ -4,16 +4,17 @@ #include #include +#include "../slstatus.h" #include "../util.h" const char * -gid(void) +gid(const char *unused) { return bprintf("%d", getgid()); } const char * -username(void) +username(const char *unused) { struct passwd *pw; @@ -26,7 +27,7 @@ username(void) } const char * -uid(void) +uid(const char *unused) { return bprintf("%d", geteuid()); } diff --git a/components/volume.c b/components/volume.c index b6665da..6cec556 100644 --- a/components/volume.c +++ b/components/volume.c @@ -5,13 +5,14 @@ #include #include +#include "../slstatus.h" #include "../util.h" -#if defined(__OpenBSD__) - #include +#if defined(__OpenBSD__) | defined(__FreeBSD__) #include #include #include + #include struct control { LIST_ENTRY(control) next; @@ -161,6 +162,7 @@ if (sioctl_revents(hdl, pfds) & POLLHUP) { warn("sndio: disconnected"); cleanup(); + initialized = 0; return NULL; } } diff --git a/components/wifi.c b/components/wifi.c index 92c252e..4543d32 100644 --- a/components/wifi.c +++ b/components/wifi.c @@ -6,6 +6,7 @@ #include #include +#include "../slstatus.h" #include "../util.h" #define RSSI_TO_PERC(rssi) \ @@ -17,6 +18,8 @@ #include #include + #define NET_OPERSTATE "/sys/class/net/%s/operstate" + const char * wifi_perc(const char *interface) { @@ -27,37 +30,32 @@ char status[5]; FILE *fp; - if (esnprintf(path, sizeof(path), "/sys/class/net/%s/operstate", - interface) < 0) { + if (esnprintf(path, sizeof(path), NET_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) { + if (!p || strcmp(status, "up\n") != 0) return NULL; - } if (!(fp = fopen("/proc/net/wireless", "r"))) { warn("fopen '/proc/net/wireless':"); return NULL; } - for (i = 0; i < 3; i++) { + 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))) { + fclose(fp); + if (i < 2 || !p) + return NULL; + + if (!(datastart = strstr(buf, interface))) return NULL; - } datastart = (datastart+(strlen(interface)+1)); sscanf(datastart + 1, " %*d %d %*d %*d\t\t %*d\t " @@ -77,9 +75,8 @@ 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) { + interface) < 0) return NULL; - } if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { warn("socket 'AF_INET':"); @@ -94,9 +91,8 @@ close(sockfd); - if (!strcmp(id, "")) { + if (!strcmp(id, "")) return NULL; - } return id; } @@ -152,11 +148,11 @@ int q; if (load_ieee80211_nodereq(interface, &nr)) { - if (nr.nr_max_rssi) { + if (nr.nr_max_rssi) q = IEEE80211_NODEREQ_RSSI(&nr); - } else { + else q = RSSI_TO_PERC(nr.nr_rssi); - } + return bprintf("%d", q); } @@ -168,9 +164,8 @@ { struct ieee80211_nodereq nr; - if (load_ieee80211_nodereq(interface, &nr)) { + if (load_ieee80211_nodereq(interface, &nr)) return bprintf("%s", nr.nr_nwid); - } return NULL; } @@ -256,7 +251,7 @@ fmt = NULL; len = sizeof(ssid); memset(&ssid, 0, len); - if (load_ieee80211req(sockfd, interface, &ssid, IEEE80211_IOC_SSID, &len )) { + if (load_ieee80211req(sockfd, interface, &ssid, IEEE80211_IOC_SSID, &len)) { if (len < sizeof(ssid)) len += 1; else diff --git a/config.def.h b/config.def.h index 93a875a..d805331 100644 --- a/config.def.h +++ b/config.def.h @@ -14,16 +14,17 @@ static const char unknown_str[] = "n/a"; * * battery_perc battery percentage battery name (BAT0) * NULL on OpenBSD/FreeBSD - * battery_state battery charging state battery name (BAT0) - * NULL on OpenBSD/FreeBSD * battery_remaining battery remaining HH:MM battery name (BAT0) * NULL on OpenBSD/FreeBSD - * cpu_perc cpu usage in percent NULL + * battery_state battery charging state battery name (BAT0) + * NULL on OpenBSD/FreeBSD + * cat read arbitrary file path * cpu_freq cpu frequency in MHz NULL + * cpu_perc cpu usage in percent NULL * 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_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 @@ -45,7 +46,6 @@ static const char unknown_str[] = "n/a"; * ram_total total memory size in GB NULL * ram_used used memory in GB NULL * run_command custom shell command command (echo foo) - * separator string to echo NULL * swap_free free swap in GB NULL * swap_perc swap usage in percent NULL * swap_total total swap size in GB NULL @@ -59,9 +59,9 @@ static const char unknown_str[] = "n/a"; * uptime system uptime NULL * username username of current user NULL * vol_perc OSS/ALSA volume in percent mixer file (/dev/mixer) - * NULL on OpenBSD - * wifi_perc WiFi signal in percent interface name (wlan0) + * NULL on OpenBSD/FreeBSD * wifi_essid WiFi ESSID interface name (wlan0) + * wifi_perc WiFi signal in percent interface name (wlan0) */ static const struct arg args[] = { /* function format argument */ diff --git a/config.mk b/config.mk index 2516e6e..07af883 100644 --- a/config.mk +++ b/config.mk @@ -1,5 +1,5 @@ # slstatus version -VERSION = 0 +VERSION = 1.0 # customize below to fit your system @@ -11,11 +11,11 @@ X11INC = /usr/X11R6/include X11LIB = /usr/X11R6/lib # flags -CPPFLAGS = -I$(X11INC) -D_DEFAULT_SOURCE -CFLAGS = -std=c99 -pedantic -Wall -Wextra -Os +CPPFLAGS = -I$(X11INC) -D_DEFAULT_SOURCE -DVERSION=\"${VERSION}\" +CFLAGS = -std=c99 -pedantic -Wall -Wextra -Wno-unused-parameter -Os LDFLAGS = -L$(X11LIB) -s # OpenBSD: add -lsndio -# FreeBSD: add -lkvm +# FreeBSD: add -lkvm -lsndio LDLIBS = -lX11 # compiler and linker diff --git a/slstatus.1 b/slstatus.1 index d802037..73e7a60 100644 --- a/slstatus.1 +++ b/slstatus.1 @@ -1,22 +1,29 @@ -.Dd 2020-06-23 +.Dd 2023-04-23 .Dt SLSTATUS 1 .Os .Sh NAME .Nm slstatus -.Nd suckless status monitor +.Nd suckless status .Sh SYNOPSIS .Nm .Op Fl s .Op Fl 1 .Sh DESCRIPTION .Nm -is a suckless status monitor for window managers that use WM_NAME (e.g. dwm) or -stdin to fill the status bar. +is a small tool for providing system status information to other programs +over the EWMH +.Em WM_NAME +property of the root window (used by +.Xr dwm 1 ) or standard input/output. It is designed to be as efficient as possible by +only issuing the minimum of system calls required. +.P By default, .Nm outputs to WM_NAME. .Sh OPTIONS .Bl -tag -width Ds +.It Fl v +Print version information to stderr, then exit. .It Fl s Write to stdout instead of WM_NAME. .It Fl 1 @@ -26,3 +33,15 @@ Write once to stdout and quit. .Nm can be customized by creating a custom config.h and (re)compiling the source code. This keeps it fast, secure and simple. +.Sh SIGNALS +.Nm +responds to the following signals: +.Pp +.Bl -tag -width TERM -compact +.It USR1 +Triggers an instant redraw. +.El +.Sh AUTHORS +See the LICENSE file for the authors. +.Sh SEE ALSO +.Xr dwm 1 diff --git a/slstatus.c b/slstatus.c index 64da5cb..fd31313 100644 --- a/slstatus.c +++ b/slstatus.c @@ -12,7 +12,7 @@ #include "util.h" struct arg { - const char *(*func)(); + const char *(*func)(const char *); const char *fmt; const char *args; }; @@ -41,7 +41,7 @@ difftimespec(struct timespec *res, struct timespec *a, struct timespec *b) static void usage(void) { - die("usage: %s [-s] [-1]", argv0); + die("usage: %s [-v] [-s] [-1]", argv0); } int @@ -56,19 +56,20 @@ main(int argc, char *argv[]) sflag = 0; ARGBEGIN { - case '1': - done = 1; - /* fallthrough */ - case 's': - sflag = 1; - break; - default: - usage(); + case 'v': + die("slstatus-"VERSION); + case '1': + done = 1; + /* FALLTHROUGH */ + case 's': + sflag = 1; + break; + default: + usage(); } ARGEND - if (argc) { + if (argc) usage(); - } memset(&act, 0, sizeof(act)); act.sa_handler = terminate; @@ -77,24 +78,22 @@ 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 +103,31 @@ 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 && + 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/slstatus.h b/slstatus.h index b0f2564..8ef5874 100644 --- a/slstatus.h +++ b/slstatus.h @@ -2,12 +2,15 @@ /* battery */ const char *battery_perc(const char *); -const char *battery_state(const char *); const char *battery_remaining(const char *); +const char *battery_state(const char *); + +/* cat */ +const char *cat(const char *path); /* cpu */ -const char *cpu_freq(void); -const char *cpu_perc(void); +const char *cpu_freq(const char *unused); +const char *cpu_perc(const char *unused); /* datetime */ const char *datetime(const char *fmt); @@ -19,26 +22,26 @@ const char *disk_total(const char *path); const char *disk_used(const char *path); /* entropy */ -const char *entropy(void); +const char *entropy(const char *unused); /* hostname */ -const char *hostname(void); +const char *hostname(const char *unused); /* ip */ const char *ipv4(const char *interface); const char *ipv6(const char *interface); /* kernel_release */ -const char *kernel_release(void); +const char *kernel_release(const char *unused); /* keyboard_indicators */ -const char *keyboard_indicators(void); +const char *keyboard_indicators(const char *fmt); /* keymap */ -const char *keymap(void); +const char *keymap(const char *unused); /* load_avg */ -const char *load_avg(void); +const char *load_avg(const char *unused); /* netspeeds */ const char *netspeed_rx(const char *interface); @@ -48,37 +51,34 @@ const char *netspeed_tx(const char *interface); const char *num_files(const char *path); /* ram */ -const char *ram_free(void); -const char *ram_perc(void); -const char *ram_total(void); -const char *ram_used(void); +const char *ram_free(const char *unused); +const char *ram_perc(const char *unused); +const char *ram_total(const char *unused); +const char *ram_used(const char *unused); /* run_command */ const char *run_command(const char *cmd); -/* separator */ -const char *separator(const char *separator); - /* swap */ -const char *swap_free(void); -const char *swap_perc(void); -const char *swap_total(void); -const char *swap_used(void); +const char *swap_free(const char *unused); +const char *swap_perc(const char *unused); +const char *swap_total(const char *unused); +const char *swap_used(const char *unused); /* temperature */ const char *temp(const char *); /* uptime */ -const char *uptime(void); +const char *uptime(const char *unused); /* user */ -const char *gid(void); -const char *username(void); -const char *uid(void); +const char *gid(const char *unused); +const char *uid(const char *unused); +const char *username(const char *unused); /* volume */ const char *vol_perc(const char *card); /* wifi */ -const char *wifi_perc(const char *interface); const char *wifi_essid(const char *interface); +const char *wifi_perc(const char *interface); diff --git a/util.c b/util.c index 85366bf..bca9b2e 100644 --- a/util.c +++ b/util.c @@ -13,10 +13,6 @@ char *argv0; static void verr(const char *fmt, va_list ap) { - if (argv0 && strncmp(fmt, "usage", sizeof("usage") - 1)) { - fprintf(stderr, "%s: ", argv0); - } - vfprintf(stderr, fmt, ap); if (fmt[0] && fmt[strlen(fmt) - 1] == ':') { @@ -119,9 +115,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]); } diff --git a/util.h b/util.h index 7f1f26c..cf4b027 100644 --- a/util.h +++ b/util.h @@ -3,7 +3,7 @@ extern char buf[1024]; -#define LEN(x) (sizeof (x) / sizeof *(x)) +#define LEN(x) (sizeof(x) / sizeof((x)[0])) extern char *argv0;