1
0

Compare commits

..

No commits in common. "4bd78c94ba90d67a268c092231b85037c94a339a" and "826a5dc86202624b00ad2a51d781758a7f7ca1fa" have entirely different histories.

24 changed files with 224 additions and 164 deletions

5
README
View File

@ -35,9 +35,8 @@ Requirements
Currently slstatus works on FreeBSD, Linux and OpenBSD. Currently slstatus works on FreeBSD, Linux and OpenBSD.
In order to build slstatus you need the Xlib header files. In order to build slstatus you need the Xlib header files.
- For volume percentage on Linux the kernel module `snd-mixer-oss` must be For volume percentage on Linux the kernel module `snd-mixer-oss` must be
loaded. loaded.
- For volume percentage on FreeBSD, `sndio` must be installed.
Installation Installation

2
arg.h
View File

@ -13,7 +13,7 @@ extern char *argv0;
break; \ break; \
} \ } \
for (i_ = 1, argused_ = 0; (*argv)[i_]; i_++) { \ for (i_ = 1, argused_ = 0; (*argv)[i_]; i_++) { \
switch ((*argv)[i_]) switch((*argv)[i_])
#define ARGEND if (argused_) { \ #define ARGEND if (argused_) { \
if ((*argv)[i_ + 1]) { \ if ((*argv)[i_ + 1]) { \
break; \ break; \

View File

@ -2,8 +2,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "../slstatus.h"
#include "../util.h" #include "../util.h"
#include "../slstatus.h"
#if defined(__linux__) #if defined(__linux__)
#include <limits.h> #include <limits.h>
@ -22,12 +22,14 @@
size_t length) size_t length)
{ {
if (esnprintf(path, length, f1, bat) > 0 && if (esnprintf(path, length, f1, bat) > 0 &&
access(path, R_OK) == 0) access(path, R_OK) == 0) {
return f1; return f1;
}
if (esnprintf(path, length, f2, bat) > 0 && if (esnprintf(path, length, f2, bat) > 0 &&
access(path, R_OK) == 0) access(path, R_OK) == 0) {
return f2; return f2;
}
return NULL; return NULL;
} }
@ -40,8 +42,9 @@
if (esnprintf(path, sizeof(path), POWER_SUPPLY_CAPACITY, bat) < 0) if (esnprintf(path, sizeof(path), POWER_SUPPLY_CAPACITY, bat) < 0)
return NULL; return NULL;
if (pscanf(path, "%d", &perc) != 1) if (pscanf(path, "%d", &perc) != 1) {
return NULL; return NULL;
}
return bprintf("%d", perc); return bprintf("%d", perc);
} }
@ -63,13 +66,15 @@
if (esnprintf(path, sizeof(path), POWER_SUPPLY_STATUS, bat) < 0) if (esnprintf(path, sizeof(path), POWER_SUPPLY_STATUS, bat) < 0)
return NULL; return NULL;
if (pscanf(path, "%12[a-zA-Z ]", state) != 1) if (pscanf(path, "%12[a-zA-Z ]", state) != 1) {
return NULL; return NULL;
}
for (i = 0; i < LEN(map); i++) for (i = 0; i < LEN(map); i++) {
if (!strcmp(map[i].state, state)) if (!strcmp(map[i].state, state)) {
break; break;
}
}
return (i == LEN(map)) ? "?" : map[i].symbol; return (i == LEN(map)) ? "?" : map[i].symbol;
} }
@ -82,8 +87,9 @@
if (esnprintf(path, sizeof(path), POWER_SUPPLY_STATUS, bat) < 0) if (esnprintf(path, sizeof(path), POWER_SUPPLY_STATUS, bat) < 0)
return NULL; return NULL;
if (pscanf(path, "%12[a-zA-Z ]", state) != 1) if (pscanf(path, "%12[a-zA-Z ]", state) != 1) {
return NULL; return NULL;
}
if (!pick(bat, POWER_SUPPLY_CHARGE, POWER_SUPPLY_ENERGY, path, if (!pick(bat, POWER_SUPPLY_CHARGE, POWER_SUPPLY_ENERGY, path,
sizeof(path)) || sizeof(path)) ||
@ -96,8 +102,9 @@
pscanf(path, "%ju", &current_now) < 0) pscanf(path, "%ju", &current_now) < 0)
return NULL; return NULL;
if (current_now == 0) if (current_now == 0) {
return NULL; return NULL;
}
timeleft = (double)charge_now / (double)current_now; timeleft = (double)charge_now / (double)current_now;
h = timeleft; h = timeleft;
@ -139,8 +146,9 @@
{ {
struct apm_power_info apm_info; 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 bprintf("%d", apm_info.battery_life);
}
return NULL; return NULL;
} }
@ -159,10 +167,11 @@
size_t i; size_t i;
if (load_apm_power_info(&apm_info)) { if (load_apm_power_info(&apm_info)) {
for (i = 0; i < LEN(map); i++) for (i = 0; i < LEN(map); i++) {
if (map[i].state == apm_info.ac_state) if (map[i].state == apm_info.ac_state) {
break; break;
}
}
return (i == LEN(map)) ? "?" : map[i].symbol; return (i == LEN(map)) ? "?" : map[i].symbol;
} }
@ -217,8 +226,8 @@
if (sysctlbyname(BATTERY_STATE, &state, &len, NULL, 0) < 0 || !len) if (sysctlbyname(BATTERY_STATE, &state, &len, NULL, 0) < 0 || !len)
return NULL; return NULL;
switch (state) { switch(state) {
case 0: /* FALLTHROUGH */ case 0:
case 2: case 2:
return "+"; return "+";
case 1: case 1:

View File

@ -3,8 +3,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "../slstatus.h"
#include "../util.h" #include "../util.h"
#include "../slstatus.h"
#if defined(__linux__) #if defined(__linux__)
#define CPU_FREQ "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq" #define CPU_FREQ "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"
@ -31,17 +31,19 @@
/* cpu user nice system idle iowait irq softirq */ /* cpu user nice system idle iowait irq softirq */
if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf", 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]) &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6])
!= 7) != 7) {
return NULL; return NULL;
}
if (b[0] == 0) if (b[0] == 0) {
return NULL; return NULL;
}
sum = (b[0] + b[1] + b[2] + b[3] + b[4] + b[5] + b[6]) - 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]); (a[0] + a[1] + a[2] + a[3] + a[4] + a[5] + a[6]);
if (sum == 0) if (sum == 0) {
return NULL; return NULL;
}
return bprintf("%d", (int)(100 * return bprintf("%d", (int)(100 *
((b[0] + b[1] + b[2] + b[5] + b[6]) - ((b[0] + b[1] + b[2] + b[5] + b[6]) -
@ -90,14 +92,16 @@
warn("sysctl 'KERN_CPTIME':"); warn("sysctl 'KERN_CPTIME':");
return NULL; return NULL;
} }
if (b[0] == 0) if (b[0] == 0) {
return NULL; return NULL;
}
sum = (a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR] + a[CP_IDLE]) - 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]); (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR] + b[CP_IDLE]);
if (sum == 0) if (sum == 0) {
return NULL; return NULL;
}
return bprintf("%d", 100 * return bprintf("%d", 100 *
((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] +
@ -106,9 +110,9 @@
b[CP_INTR])) / sum); b[CP_INTR])) / sum);
} }
#elif defined(__FreeBSD__) #elif defined(__FreeBSD__)
#include <devstat.h>
#include <sys/param.h> #include <sys/param.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <devstat.h>
const char * const char *
cpu_freq(const char *unused) cpu_freq(const char *unused)
@ -118,7 +122,8 @@
size = sizeof(freq); size = sizeof(freq);
/* in MHz */ /* in MHz */
if (sysctlbyname("hw.clockrate", &freq, &size, NULL, 0) < 0 || !size) { if (sysctlbyname("hw.clockrate", &freq, &size, NULL, 0) == -1
|| !size) {
warn("sysctlbyname 'hw.clockrate':"); warn("sysctlbyname 'hw.clockrate':");
return NULL; return NULL;
} }
@ -135,18 +140,21 @@
size = sizeof(a); size = sizeof(a);
memcpy(b, a, sizeof(b)); memcpy(b, a, sizeof(b));
if (sysctlbyname("kern.cp_time", &a, &size, NULL, 0) < 0 || !size) { if (sysctlbyname("kern.cp_time", &a, &size, NULL, 0) == -1
|| !size) {
warn("sysctlbyname 'kern.cp_time':"); warn("sysctlbyname 'kern.cp_time':");
return NULL; return NULL;
} }
if (b[0] == 0) if (b[0] == 0) {
return NULL; return NULL;
}
sum = (a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR] + a[CP_IDLE]) - 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]); (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR] + b[CP_IDLE]);
if (sum == 0) if (sum == 0) {
return NULL; return NULL;
}
return bprintf("%d", 100 * return bprintf("%d", 100 *
((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] +

View File

@ -2,8 +2,8 @@
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include "../slstatus.h"
#include "../util.h" #include "../util.h"
#include "../slstatus.h"
const char * const char *
datetime(const char *fmt) datetime(const char *fmt)

View File

@ -2,8 +2,8 @@
#include <stdio.h> #include <stdio.h>
#include <sys/statvfs.h> #include <sys/statvfs.h>
#include "../slstatus.h"
#include "../util.h" #include "../util.h"
#include "../slstatus.h"
const char * const char *
disk_free(const char *path) disk_free(const char *path)

View File

@ -2,8 +2,8 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include "../slstatus.h"
#include "../util.h" #include "../util.h"
#include "../slstatus.h"
const char * const char *
hostname(const char *unused) hostname(const char *unused)

View File

@ -4,15 +4,15 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#if defined(__OpenBSD__) #if defined(__OpenBSD__)
#include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h>
#elif defined(__FreeBSD__) #elif defined(__FreeBSD__)
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/socket.h> #include <sys/socket.h>
#endif #endif
#include "../slstatus.h"
#include "../util.h" #include "../util.h"
#include "../slstatus.h"
static const char * static const char *
ip(const char *interface, unsigned short sa_family) ip(const char *interface, unsigned short sa_family)
@ -27,9 +27,9 @@ ip(const char *interface, unsigned short sa_family)
} }
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (!ifa->ifa_addr) if (!ifa->ifa_addr) {
continue; continue;
}
s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6),
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (!strcmp(ifa->ifa_name, interface) && if (!strcmp(ifa->ifa_name, interface) &&

View File

@ -1,9 +1,9 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <sys/utsname.h> #include <sys/utsname.h>
#include <stdio.h>
#include "../slstatus.h"
#include "../util.h" #include "../util.h"
#include "../slstatus.h"
const char * const char *
kernel_release(const char *unused) kernel_release(const char *unused)

View File

@ -4,8 +4,8 @@
#include <string.h> #include <string.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include "../slstatus.h"
#include "../util.h" #include "../util.h"
#include "../slstatus.h"
/* /*
* fmt consists of uppercase or lowercase 'c' for caps lock and/or 'n' for num * fmt consists of uppercase or lowercase 'c' for caps lock and/or 'n' for num
@ -33,18 +33,17 @@ keyboard_indicators(const char *fmt)
fmtlen = strnlen(fmt, 4); fmtlen = strnlen(fmt, 4);
for (i = n = 0; i < fmtlen; i++) { for (i = n = 0; i < fmtlen; i++) {
key = tolower(fmt[i]); key = tolower(fmt[i]);
if (key != 'c' && key != 'n') if (key != 'c' && key != 'n') {
continue; continue;
}
togglecase = (i + 1 >= fmtlen || fmt[i + 1] != '?'); togglecase = (i + 1 >= fmtlen || fmt[i + 1] != '?');
isset = (state.led_mask & (1 << (key == 'n'))); isset = (state.led_mask & (1 << (key == 'n')));
if (togglecase) {
if (togglecase)
buf[n++] = isset ? toupper(key) : key; buf[n++] = isset ? toupper(key) : key;
else if (isset) } else if (isset) {
buf[n++] = fmt[i]; buf[n++] = fmt[i];
} }
}
buf[n] = 0; buf[n] = 0;
return buf; return buf;
} }

View File

@ -5,8 +5,8 @@
#include <X11/XKBlib.h> #include <X11/XKBlib.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include "../slstatus.h"
#include "../util.h" #include "../util.h"
#include "../slstatus.h"
static int static int
valid_layout_or_variant(char *sym) valid_layout_or_variant(char *sym)
@ -15,9 +15,11 @@ valid_layout_or_variant(char *sym)
/* invalid symbols from xkb rules config */ /* invalid symbols from xkb rules config */
static const char *invalid[] = { "evdev", "inet", "pc", "base" }; static const char *invalid[] = { "evdev", "inet", "pc", "base" };
for (i = 0; i < LEN(invalid); i++) for (i = 0; i < LEN(invalid); i++) {
if (!strncmp(sym, invalid[i], strlen(invalid[i]))) if (!strncmp(sym, invalid[i], strlen(invalid[i]))) {
return 0; return 0;
}
}
return 1; return 1;
} }
@ -78,8 +80,9 @@ keymap(const char *unused)
XFree(symbols); XFree(symbols);
end: end:
XkbFreeKeyboard(desc, XkbSymbolsNameMask, 1); XkbFreeKeyboard(desc, XkbSymbolsNameMask, 1);
if (XCloseDisplay(dpy)) if (XCloseDisplay(dpy)) {
warn("XCloseDisplay: Failed to close display"); warn("XCloseDisplay: Failed to close display");
}
return layout; return layout;
} }

View File

@ -2,8 +2,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "../slstatus.h"
#include "../util.h" #include "../util.h"
#include "../slstatus.h"
const char * const char *
load_avg(const char *unused) load_avg(const char *unused)

View File

@ -1,9 +1,9 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <limits.h>
#include "../slstatus.h"
#include "../util.h" #include "../util.h"
#include "../slstatus.h"
#if defined(__linux__) #if defined(__linux__)
#include <stdint.h> #include <stdint.h>
@ -23,10 +23,12 @@
if (esnprintf(path, sizeof(path), NET_RX_BYTES, interface) < 0) if (esnprintf(path, sizeof(path), NET_RX_BYTES, interface) < 0)
return NULL; return NULL;
if (pscanf(path, "%ju", &rxbytes) != 1) if (pscanf(path, "%ju", &rxbytes) != 1) {
return NULL; return NULL;
if (oldrxbytes == 0) }
if (oldrxbytes == 0) {
return NULL; return NULL;
}
return fmt_human((rxbytes - oldrxbytes) * 1000 / interval, return fmt_human((rxbytes - oldrxbytes) * 1000 / interval,
1024); 1024);
@ -44,20 +46,22 @@
if (esnprintf(path, sizeof(path), NET_TX_BYTES, interface) < 0) if (esnprintf(path, sizeof(path), NET_TX_BYTES, interface) < 0)
return NULL; return NULL;
if (pscanf(path, "%ju", &txbytes) != 1) if (pscanf(path, "%ju", &txbytes) != 1) {
return NULL; return NULL;
if (oldtxbytes == 0) }
if (oldtxbytes == 0) {
return NULL; return NULL;
}
return fmt_human((txbytes - oldtxbytes) * 1000 / interval, return fmt_human((txbytes - oldtxbytes) * 1000 / interval,
1024); 1024);
} }
#elif defined(__OpenBSD__) | defined(__FreeBSD__) #elif defined(__OpenBSD__) | defined(__FreeBSD__)
#include <ifaddrs.h>
#include <net/if.h>
#include <string.h> #include <string.h>
#include <ifaddrs.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <net/if.h>
const char * const char *
netspeed_rx(const char *interface) netspeed_rx(const char *interface)
@ -71,23 +75,25 @@
oldrxbytes = rxbytes; oldrxbytes = rxbytes;
if (getifaddrs(&ifal) < 0) { if (getifaddrs(&ifal) == -1) {
warn("getifaddrs failed"); warn("getifaddrs failed");
return NULL; return NULL;
} }
rxbytes = 0; rxbytes = 0;
for (ifa = ifal; ifa; ifa = ifa->ifa_next) for (ifa = ifal; ifa; ifa = ifa->ifa_next) {
if (!strcmp(ifa->ifa_name, interface) && 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; rxbytes += ifd->ifi_ibytes, if_ok = 1;
}
}
freeifaddrs(ifal); freeifaddrs(ifal);
if (!if_ok) { if (!if_ok) {
warn("reading 'if_data' failed"); warn("reading 'if_data' failed");
return NULL; return NULL;
} }
if (oldrxbytes == 0) if (oldrxbytes == 0) {
return NULL; return NULL;
}
return fmt_human((rxbytes - oldrxbytes) * 1000 / interval, return fmt_human((rxbytes - oldrxbytes) * 1000 / interval,
1024); 1024);
@ -105,23 +111,25 @@
oldtxbytes = txbytes; oldtxbytes = txbytes;
if (getifaddrs(&ifal) < 0) { if (getifaddrs(&ifal) == -1) {
warn("getifaddrs failed"); warn("getifaddrs failed");
return NULL; return NULL;
} }
txbytes = 0; txbytes = 0;
for (ifa = ifal; ifa; ifa = ifa->ifa_next) for (ifa = ifal; ifa; ifa = ifa->ifa_next) {
if (!strcmp(ifa->ifa_name, interface) && 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; txbytes += ifd->ifi_obytes, if_ok = 1;
}
}
freeifaddrs(ifal); freeifaddrs(ifal);
if (!if_ok) { if (!if_ok) {
warn("reading 'if_data' failed"); warn("reading 'if_data' failed");
return NULL; return NULL;
} }
if (oldtxbytes == 0) if (oldtxbytes == 0) {
return NULL; return NULL;
}
return fmt_human((txbytes - oldtxbytes) * 1000 / interval, return fmt_human((txbytes - oldtxbytes) * 1000 / interval,
1024); 1024);

View File

@ -3,8 +3,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "../slstatus.h"
#include "../util.h" #include "../util.h"
#include "../slstatus.h"
const char * const char *
num_files(const char *path) num_files(const char *path)
@ -20,9 +20,9 @@ num_files(const char *path)
num = 0; num = 0;
while ((dp = readdir(fd))) { while ((dp = readdir(fd))) {
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) {
continue; /* skip self and parent */ continue; /* skip self and parent */
}
num++; num++;
} }

View File

@ -1,8 +1,8 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <stdio.h> #include <stdio.h>
#include "../slstatus.h"
#include "../util.h" #include "../util.h"
#include "../slstatus.h"
#if defined(__linux__) #if defined(__linux__)
#include <stdint.h> #include <stdint.h>
@ -16,8 +16,9 @@
"MemTotal: %ju kB\n" "MemTotal: %ju kB\n"
"MemFree: %ju kB\n" "MemFree: %ju kB\n"
"MemAvailable: %ju kB\n", "MemAvailable: %ju kB\n",
&free, &free, &free) != 3) &free, &free, &free) != 3) {
return NULL; return NULL;
}
return fmt_human(free * 1024, 1024); return fmt_human(free * 1024, 1024);
} }
@ -34,11 +35,13 @@
"MemAvailable: %ju kB\n" "MemAvailable: %ju kB\n"
"Buffers: %ju kB\n" "Buffers: %ju kB\n"
"Cached: %ju kB\n", "Cached: %ju kB\n",
&total, &free, &buffers, &buffers, &cached) != 5) &total, &free, &buffers, &buffers, &cached) != 5) {
return NULL; return NULL;
}
if (total == 0) if (total == 0) {
return NULL; return NULL;
}
percent = 100 * ((total - free) - (buffers + cached)) / total; percent = 100 * ((total - free) - (buffers + cached)) / total;
return bprintf("%d", percent); return bprintf("%d", percent);
@ -50,8 +53,9 @@
uintmax_t total; uintmax_t total;
if (pscanf("/proc/meminfo", "MemTotal: %ju kB\n", &total) if (pscanf("/proc/meminfo", "MemTotal: %ju kB\n", &total)
!= 1) != 1) {
return NULL; return NULL;
}
return fmt_human(total * 1024, 1024); return fmt_human(total * 1024, 1024);
} }
@ -90,8 +94,9 @@
size = sizeof(*uvmexp); 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 1;
}
return 0; return 0;
} }
@ -159,7 +164,7 @@
size_t len; size_t len;
len = sizeof(struct vmtotal); len = sizeof(struct vmtotal);
if (sysctl(mib, 2, &vm_stats, &len, NULL, 0) < 0 if (sysctl(mib, 2, &vm_stats, &len, NULL, 0) == -1
|| !len) || !len)
return NULL; return NULL;
@ -172,8 +177,8 @@
size_t len; size_t len;
len = sizeof(npages); len = sizeof(npages);
if (sysctlbyname("vm.stats.vm.v_page_count", if (sysctlbyname("vm.stats.vm.v_page_count", &npages, &len, NULL, 0) == -1
&npages, &len, NULL, 0) < 0 || !len) || !len)
return NULL; return NULL;
return fmt_human(npages * getpagesize(), 1024); return fmt_human(npages * getpagesize(), 1024);
@ -186,12 +191,12 @@
size_t len; size_t len;
len = sizeof(npages); len = sizeof(npages);
if (sysctlbyname("vm.stats.vm.v_page_count", if (sysctlbyname("vm.stats.vm.v_page_count", &npages, &len, NULL, 0) == -1
&npages, &len, NULL, 0) < 0 || !len) || !len)
return NULL; return NULL;
if (sysctlbyname("vm.stats.vm.v_active_count", if (sysctlbyname("vm.stats.vm.v_active_count", &active, &len, NULL, 0) == -1
&active, &len, NULL, 0) < 0 || !len) || !len)
return NULL; return NULL;
return bprintf("%d", active * 100 / npages); return bprintf("%d", active * 100 / npages);
@ -203,8 +208,8 @@
size_t len; size_t len;
len = sizeof(active); len = sizeof(active);
if (sysctlbyname("vm.stats.vm.v_active_count", if (sysctlbyname("vm.stats.vm.v_active_count", &active, &len, NULL, 0) == -1
&active, &len, NULL, 0) < 0 || !len) || !len)
return NULL; return NULL;
return fmt_human(active * getpagesize(), 1024); return fmt_human(active * getpagesize(), 1024);

View File

@ -2,8 +2,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "../slstatus.h"
#include "../util.h" #include "../util.h"
#include "../slstatus.h"
const char * const char *
run_command(const char *cmd) run_command(const char *cmd)
@ -15,17 +15,17 @@ run_command(const char *cmd)
warn("popen '%s':", cmd); warn("popen '%s':", cmd);
return NULL; return NULL;
} }
p = fgets(buf, sizeof(buf) - 1, fp); p = fgets(buf, sizeof(buf) - 1, fp);
if (pclose(fp) < 0) { if (pclose(fp) < 0) {
warn("pclose '%s':", cmd); warn("pclose '%s':", cmd);
return NULL; return NULL;
} }
if (!p) if (!p) {
return NULL; return NULL;
}
if ((p = strrchr(buf, '\n'))) if ((p = strrchr(buf, '\n'))) {
p[0] = '\0'; p[0] = '\0';
}
return buf[0] ? buf : NULL; return buf[0] ? buf : NULL;
} }

View File

@ -4,8 +4,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "../slstatus.h"
#include "../util.h" #include "../util.h"
#include "../slstatus.h"
#if defined(__linux__) #if defined(__linux__)
static int static int
@ -25,9 +25,11 @@
char *line = NULL; char *line = NULL;
/* get number of fields we want to extract */ /* get number of fields we want to extract */
for (i = 0, left = 0; i < LEN(ent); i++) for (i = 0, left = 0; i < LEN(ent); i++) {
if (ent[i].var) if (ent[i].var) {
left++; left++;
}
}
if (!(fp = fopen("/proc/meminfo", "r"))) { if (!(fp = fopen("/proc/meminfo", "r"))) {
warn("fopen '/proc/meminfo':"); warn("fopen '/proc/meminfo':");
@ -61,8 +63,9 @@
{ {
long free; long free;
if (get_swap_info(NULL, &free, NULL)) if (get_swap_info(NULL, &free, NULL)) {
return NULL; return NULL;
}
return fmt_human(free * 1024, 1024); return fmt_human(free * 1024, 1024);
} }
@ -72,8 +75,9 @@
{ {
long total, free, cached; 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 NULL;
}
return bprintf("%d", 100 * (total - free - cached) / total); return bprintf("%d", 100 * (total - free - cached) / total);
} }
@ -83,8 +87,9 @@
{ {
long total; long total;
if (get_swap_info(&total, NULL, NULL)) if (get_swap_info(&total, NULL, NULL)) {
return NULL; return NULL;
}
return fmt_human(total * 1024, 1024); return fmt_human(total * 1024, 1024);
} }
@ -94,8 +99,9 @@
{ {
long total, free, cached; long total, free, cached;
if (get_swap_info(&total, &free, &cached)) if (get_swap_info(&total, &free, &cached)) {
return NULL; return NULL;
}
return fmt_human((total - free - cached) * 1024, 1024); return fmt_human((total - free - cached) * 1024, 1024);
} }
@ -146,8 +152,9 @@
{ {
int total, used; int total, used;
if (getstats(&total, &used)) if (getstats(&total, &used)) {
return NULL; return NULL;
}
return fmt_human((total - used) * 1024, 1024); return fmt_human((total - used) * 1024, 1024);
} }
@ -157,11 +164,13 @@
{ {
int total, used; int total, used;
if (getstats(&total, &used)) if (getstats(&total, &used)) {
return NULL; return NULL;
}
if (total == 0) if (total == 0) {
return NULL; return NULL;
}
return bprintf("%d", 100 * used / total); return bprintf("%d", 100 * used / total);
} }
@ -171,8 +180,9 @@
{ {
int total, used; int total, used;
if (getstats(&total, &used)) if (getstats(&total, &used)) {
return NULL; return NULL;
}
return fmt_human(total * 1024, 1024); return fmt_human(total * 1024, 1024);
} }
@ -182,29 +192,30 @@
{ {
int total, used; int total, used;
if (getstats(&total, &used)) if (getstats(&total, &used)) {
return NULL; return NULL;
}
return fmt_human(used * 1024, 1024); return fmt_human(used * 1024, 1024);
} }
#elif defined(__FreeBSD__) #elif defined(__FreeBSD__)
#include <fcntl.h>
#include <kvm.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <kvm.h>
static int getswapinfo(struct kvm_swap *swap_info, size_t size) static int getswapinfo(struct kvm_swap *swap_info, size_t size)
{ {
kvm_t *kd; kvm_t *kd;
kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, NULL); kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, NULL);
if (kd == NULL) { if(kd == NULL) {
warn("kvm_openfiles '/dev/null':"); warn("kvm_openfiles '/dev/null':");
return 0; return 0;
} }
if (kvm_getswapinfo(kd, swap_info, size, 0 /* Unused flags */) < 0) { if(kvm_getswapinfo(kd, swap_info, size, 0 /* Unused flags */) == -1) {
warn("kvm_getswapinfo:"); warn("kvm_getswapinfo:");
kvm_close(kd); kvm_close(kd);
return 0; return 0;
@ -220,7 +231,7 @@
struct kvm_swap swap_info[1]; struct kvm_swap swap_info[1];
long used, total; long used, total;
if (!getswapinfo(swap_info, 1)) if(!getswapinfo(swap_info, 1))
return NULL; return NULL;
total = swap_info[0].ksw_total; total = swap_info[0].ksw_total;
@ -235,7 +246,7 @@
struct kvm_swap swap_info[1]; struct kvm_swap swap_info[1];
long used, total; long used, total;
if (!getswapinfo(swap_info, 1)) if(!getswapinfo(swap_info, 1))
return NULL; return NULL;
total = swap_info[0].ksw_total; total = swap_info[0].ksw_total;
@ -250,7 +261,7 @@
struct kvm_swap swap_info[1]; struct kvm_swap swap_info[1];
long total; long total;
if (!getswapinfo(swap_info, 1)) if(!getswapinfo(swap_info, 1))
return NULL; return NULL;
total = swap_info[0].ksw_total; total = swap_info[0].ksw_total;
@ -264,7 +275,7 @@
struct kvm_swap swap_info[1]; struct kvm_swap swap_info[1];
long used; long used;
if (!getswapinfo(swap_info, 1)) if(!getswapinfo(swap_info, 1))
return NULL; return NULL;
used = swap_info[0].ksw_used; used = swap_info[0].ksw_used;

View File

@ -1,8 +1,8 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <stddef.h> #include <stddef.h>
#include "../slstatus.h"
#include "../util.h" #include "../util.h"
#include "../slstatus.h"
#if defined(__linux__) #if defined(__linux__)
@ -13,8 +13,9 @@
{ {
uintmax_t temp; uintmax_t temp;
if (pscanf(file, "%ju", &temp) != 1) if (pscanf(file, "%ju", &temp) != 1) {
return NULL; return NULL;
}
return bprintf("%ju", temp / 1000); return bprintf("%ju", temp / 1000);
} }

View File

@ -3,8 +3,8 @@
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include "../slstatus.h"
#include "../util.h" #include "../util.h"
#include "../slstatus.h"
#if defined(CLOCK_BOOTTIME) #if defined(CLOCK_BOOTTIME)
#define UPTIME_FLAG CLOCK_BOOTTIME #define UPTIME_FLAG CLOCK_BOOTTIME

View File

@ -4,8 +4,8 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include "../slstatus.h"
#include "../util.h" #include "../util.h"
#include "../slstatus.h"
const char * const char *
gid(const char *unused) gid(const char *unused)

View File

@ -5,14 +5,14 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <unistd.h> #include <unistd.h>
#include "../slstatus.h"
#include "../util.h" #include "../util.h"
#include "../slstatus.h"
#if defined(__OpenBSD__) | defined(__FreeBSD__) #if defined(__OpenBSD__) | defined(__FreeBSD__)
#include <sys/queue.h>
#include <poll.h> #include <poll.h>
#include <sndio.h> #include <sndio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/queue.h>
struct control { struct control {
LIST_ENTRY(control) next; LIST_ENTRY(control) next;

View File

@ -6,8 +6,8 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h> #include <unistd.h>
#include "../slstatus.h"
#include "../util.h" #include "../util.h"
#include "../slstatus.h"
#define RSSI_TO_PERC(rssi) \ #define RSSI_TO_PERC(rssi) \
rssi >= -50 ? 100 : \ rssi >= -50 ? 100 : \
@ -38,24 +38,27 @@
} }
p = fgets(status, 5, fp); p = fgets(status, 5, fp);
fclose(fp); fclose(fp);
if (!p || strcmp(status, "up\n") != 0) if (!p || strcmp(status, "up\n") != 0) {
return NULL; return NULL;
}
if (!(fp = fopen("/proc/net/wireless", "r"))) { if (!(fp = fopen("/proc/net/wireless", "r"))) {
warn("fopen '/proc/net/wireless':"); warn("fopen '/proc/net/wireless':");
return NULL; return NULL;
} }
for (i = 0; i < 3; i++) for (i = 0; i < 3; i++) {
if (!(p = fgets(buf, sizeof(buf) - 1, fp))) if (!(p = fgets(buf, sizeof(buf) - 1, fp)))
break; break;
}
fclose(fp); fclose(fp);
if (i < 2 || !p) if (i < 2 || !p) {
return NULL; return NULL;
}
if (!(datastart = strstr(buf, interface))) if (!(datastart = strstr(buf, interface))) {
return NULL; return NULL;
}
datastart = (datastart+(strlen(interface)+1)); datastart = (datastart+(strlen(interface)+1));
sscanf(datastart + 1, " %*d %d %*d %*d\t\t %*d\t " sscanf(datastart + 1, " %*d %d %*d %*d\t\t %*d\t "
@ -75,8 +78,9 @@
memset(&wreq, 0, sizeof(struct iwreq)); memset(&wreq, 0, sizeof(struct iwreq));
wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
if (esnprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", if (esnprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s",
interface) < 0) interface) < 0) {
return NULL; return NULL;
}
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
warn("socket 'AF_INET':"); warn("socket 'AF_INET':");
@ -91,8 +95,9 @@
close(sockfd); close(sockfd);
if (!strcmp(id, "")) if (!strcmp(id, "")) {
return NULL; return NULL;
}
return id; return id;
} }
@ -148,11 +153,11 @@
int q; int q;
if (load_ieee80211_nodereq(interface, &nr)) { if (load_ieee80211_nodereq(interface, &nr)) {
if (nr.nr_max_rssi) if (nr.nr_max_rssi) {
q = IEEE80211_NODEREQ_RSSI(&nr); q = IEEE80211_NODEREQ_RSSI(&nr);
else } else {
q = RSSI_TO_PERC(nr.nr_rssi); q = RSSI_TO_PERC(nr.nr_rssi);
}
return bprintf("%d", q); return bprintf("%d", q);
} }
@ -164,8 +169,9 @@
{ {
struct ieee80211_nodereq nr; struct ieee80211_nodereq nr;
if (load_ieee80211_nodereq(interface, &nr)) if (load_ieee80211_nodereq(interface, &nr)) {
return bprintf("%s", nr.nr_nwid); return bprintf("%s", nr.nr_nwid);
}
return NULL; return NULL;
} }
@ -251,7 +257,7 @@
fmt = NULL; fmt = NULL;
len = sizeof(ssid); len = sizeof(ssid);
memset(&ssid, 0, len); 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)) if (len < sizeof(ssid))
len += 1; len += 1;
else else

View File

@ -58,7 +58,7 @@ main(int argc, char *argv[])
ARGBEGIN { ARGBEGIN {
case '1': case '1':
done = 1; done = 1;
/* FALLTHROUGH */ /* fallthrough */
case 's': case 's':
sflag = 1; sflag = 1;
break; break;
@ -66,8 +66,9 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND } ARGEND
if (argc) if (argc) {
usage(); usage();
}
memset(&act, 0, sizeof(act)); memset(&act, 0, sizeof(act));
act.sa_handler = terminate; act.sa_handler = terminate;
@ -76,22 +77,24 @@ main(int argc, char *argv[])
act.sa_flags |= SA_RESTART; act.sa_flags |= SA_RESTART;
sigaction(SIGUSR1, &act, NULL); sigaction(SIGUSR1, &act, NULL);
if (!sflag && !(dpy = XOpenDisplay(NULL))) if (!sflag && !(dpy = XOpenDisplay(NULL))) {
die("XOpenDisplay: Failed to open display"); die("XOpenDisplay: Failed to open display");
}
do { do {
if (clock_gettime(CLOCK_MONOTONIC, &start) < 0) if (clock_gettime(CLOCK_MONOTONIC, &start) < 0) {
die("clock_gettime:"); die("clock_gettime:");
}
status[0] = '\0'; status[0] = '\0';
for (i = len = 0; i < LEN(args); i++) { 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; res = unknown_str;
}
if ((ret = esnprintf(status + len, sizeof(status) - len, if ((ret = esnprintf(status + len, sizeof(status) - len,
args[i].fmt, res)) < 0) args[i].fmt, res)) < 0) {
break; break;
}
len += ret; len += ret;
} }
@ -101,32 +104,38 @@ main(int argc, char *argv[])
if (ferror(stdout)) if (ferror(stdout))
die("puts:"); die("puts:");
} else { } else {
if (XStoreName(dpy, DefaultRootWindow(dpy), status) < 0) if (XStoreName(dpy, DefaultRootWindow(dpy), status)
< 0) {
die("XStoreName: Allocation failed"); die("XStoreName: Allocation failed");
}
XFlush(dpy); XFlush(dpy);
} }
if (!done) { if (!done) {
if (clock_gettime(CLOCK_MONOTONIC, &current) < 0) if (clock_gettime(CLOCK_MONOTONIC, &current) < 0) {
die("clock_gettime:"); die("clock_gettime:");
}
difftimespec(&diff, &current, &start); difftimespec(&diff, &current, &start);
intspec.tv_sec = interval / 1000; intspec.tv_sec = interval / 1000;
intspec.tv_nsec = (interval % 1000) * 1E6; intspec.tv_nsec = (interval % 1000) * 1E6;
difftimespec(&wait, &intspec, &diff); difftimespec(&wait, &intspec, &diff);
if (wait.tv_sec >= 0 && if (wait.tv_sec >= 0) {
nanosleep(&wait, NULL) < 0 && if (nanosleep(&wait, NULL) < 0 &&
errno != EINTR) errno != EINTR) {
die("nanosleep:"); die("nanosleep:");
} }
}
}
} while (!done); } while (!done);
if (!sflag) { if (!sflag) {
XStoreName(dpy, DefaultRootWindow(dpy), NULL); XStoreName(dpy, DefaultRootWindow(dpy), NULL);
if (XCloseDisplay(dpy) < 0) if (XCloseDisplay(dpy) < 0) {
die("XCloseDisplay: Failed to close display"); die("XCloseDisplay: Failed to close display");
} }
}
return 0; return 0;
} }

6
util.c
View File

@ -13,8 +13,9 @@ char *argv0;
static void static void
verr(const char *fmt, va_list ap) 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); fprintf(stderr, "%s: ", argv0);
}
vfprintf(stderr, fmt, ap); vfprintf(stderr, fmt, ap);
@ -118,8 +119,9 @@ fmt_human(uintmax_t num, int base)
} }
scaled = num; scaled = num;
for (i = 0; i < prefixlen && scaled >= base; i++) for (i = 0; i < prefixlen && scaled >= base; i++) {
scaled /= base; scaled /= base;
}
return bprintf("%.1f %s", scaled, prefix[i]); return bprintf("%.1f %s", scaled, prefix[i]);
} }