various: Put paths into defines to avoid line wraps

Long, wrapped, multi-line if statements suck to read.
This fixes readability in the worst places by packing format strings for
paths into defines.
This commit is contained in:
drkhsh 2022-10-27 23:44:52 +02:00
parent c46c1487a9
commit 69b2487650
5 changed files with 36 additions and 41 deletions

View File

@ -10,6 +10,13 @@
#include <stdint.h> #include <stdint.h>
#include <unistd.h> #include <unistd.h>
#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"
#define POWER_SUPPLY_POWER "/sys/class/power_supply/%s/power"
static const char * static const char *
pick(const char *bat, const char *f1, const char *f2, char *path, pick(const char *bat, const char *f1, const char *f2, char *path,
size_t length) size_t length)
@ -33,10 +40,8 @@
int perc; int perc;
char path[PATH_MAX]; char path[PATH_MAX];
if (esnprintf(path, sizeof(path), if (esnprintf(path, sizeof(path), POWER_SUPPLY_CAPACITY, bat) < 0)
"/sys/class/power_supply/%s/capacity", bat) < 0) {
return NULL; return NULL;
}
if (pscanf(path, "%d", &perc) != 1) { if (pscanf(path, "%d", &perc) != 1) {
return NULL; return NULL;
} }
@ -59,10 +64,8 @@
size_t i; size_t i;
char path[PATH_MAX], state[12]; char path[PATH_MAX], state[12];
if (esnprintf(path, sizeof(path), if (esnprintf(path, sizeof(path), POWER_SUPPLY_STATUS, bat) < 0)
"/sys/class/power_supply/%s/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;
} }
@ -82,28 +85,22 @@
double timeleft; double timeleft;
char path[PATH_MAX], state[12]; char path[PATH_MAX], state[12];
if (esnprintf(path, sizeof(path), if (esnprintf(path, sizeof(path), POWER_SUPPLY_STATUS, bat) < 0)
"/sys/class/power_supply/%s/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, "/sys/class/power_supply/%s/charge_now", if (!pick(bat, POWER_SUPPLY_CHARGE, POWER_SUPPLY_ENERGY, path,
"/sys/class/power_supply/%s/energy_now", path,
sizeof(path)) || sizeof(path)) ||
pscanf(path, "%ju", &charge_now) < 0) { pscanf(path, "%ju", &charge_now) < 0)
return NULL; return NULL;
}
if (!strcmp(state, "Discharging")) { if (!strcmp(state, "Discharging")) {
if (!pick(bat, "/sys/class/power_supply/%s/current_now", if (!pick(bat, POWER_SUPPLY_CURRENT, POWER_SUPPLY_POWER, path,
"/sys/class/power_supply/%s/power_now", path,
sizeof(path)) || sizeof(path)) ||
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;
@ -201,6 +198,10 @@
#elif defined(__FreeBSD__) #elif defined(__FreeBSD__)
#include <sys/sysctl.h> #include <sys/sysctl.h>
#define BATTERY_LIFE "hw.acpi.battery.life"
#define BATTERY_STATE "hw.acpi.battery.state"
#define BATTERY_TIME "hw.acpi.battery.time"
const char * const char *
battery_perc(const char *unused) battery_perc(const char *unused)
{ {
@ -208,8 +209,7 @@
size_t len; size_t len;
len = sizeof(cap); len = sizeof(cap);
if (sysctlbyname("hw.acpi.battery.life", &cap, &len, NULL, 0) == -1 if (sysctlbyname(BATTERY_LIFE, &cap, &len, NULL, 0) < 0 || !len)
|| !len)
return NULL; return NULL;
return bprintf("%d", cap); return bprintf("%d", cap);
@ -222,8 +222,7 @@
size_t len; size_t len;
len = sizeof(state); len = sizeof(state);
if (sysctlbyname("hw.acpi.battery.state", &state, &len, NULL, 0) == -1 if (sysctlbyname(BATTERY_STATE, &state, &len, NULL, 0) < 0 || !len)
|| !len)
return NULL; return NULL;
switch(state) { switch(state) {
@ -244,9 +243,8 @@
size_t len; size_t len;
len = sizeof(rem); len = sizeof(rem);
if (sysctlbyname("hw.acpi.battery.time", &rem, &len, NULL, 0) == -1 if (sysctlbyname(BATTERY_TIME, &rem, &len, NULL, 0) < 0 || !len
|| !len || rem < 0)
|| rem == -1)
return NULL; return NULL;
return bprintf("%uh %02um", rem / 60, rem % 60); return bprintf("%uh %02um", rem / 60, rem % 60);

View File

@ -7,16 +7,16 @@
#include "../slstatus.h" #include "../slstatus.h"
#if defined(__linux__) #if defined(__linux__)
#define CPU_FREQ "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"
const char * const char *
cpu_freq(const char *unused) cpu_freq(const char *unused)
{ {
uintmax_t freq; uintmax_t freq;
/* in kHz */ /* in kHz */
if (pscanf("/sys/devices/system/cpu/cpu0/cpufreq/" if (pscanf(CPU_FREQ, "%ju", &freq) != 1)
"scaling_cur_freq", "%ju", &freq) != 1) {
return NULL; return NULL;
}
return fmt_human(freq * 1000, 1000); return fmt_human(freq * 1000, 1000);
} }

View File

@ -6,15 +6,15 @@
#include "../util.h" #include "../util.h"
#define ENTROPY_AVAIL "/proc/sys/kernel/random/entropy_avail"
const char * const char *
entropy(const char *unused) entropy(const char *unused)
{ {
uintmax_t num; uintmax_t num;
if (pscanf("/proc/sys/kernel/random/entropy_avail", "%ju", &num) if (pscanf(ENTROPY_AVAIL, "%ju", &num) != 1)
!= 1) {
return NULL; return NULL;
}
return bprintf("%ju", num); return bprintf("%ju", num);
} }

View File

@ -8,6 +8,9 @@
#if defined(__linux__) #if defined(__linux__)
#include <stdint.h> #include <stdint.h>
#define NET_RX_BYTES "/sys/class/net/%s/statistics/rx_bytes"
#define NET_TX_BYTES "/sys/class/net/%s/statistics/tx_bytes"
const char * const char *
netspeed_rx(const char *interface) netspeed_rx(const char *interface)
{ {
@ -18,11 +21,8 @@
oldrxbytes = rxbytes; oldrxbytes = rxbytes;
if (esnprintf(path, sizeof(path), if (esnprintf(path, sizeof(path), NET_RX_BYTES, interface) < 0)
"/sys/class/net/%s/statistics/rx_bytes",
interface) < 0) {
return NULL; return NULL;
}
if (pscanf(path, "%ju", &rxbytes) != 1) { if (pscanf(path, "%ju", &rxbytes) != 1) {
return NULL; return NULL;
} }
@ -44,11 +44,8 @@
oldtxbytes = txbytes; oldtxbytes = txbytes;
if (esnprintf(path, sizeof(path), if (esnprintf(path, sizeof(path), NET_TX_BYTES, interface) < 0)
"/sys/class/net/%s/statistics/tx_bytes",
interface) < 0) {
return NULL; return NULL;
}
if (pscanf(path, "%ju", &txbytes) != 1) { if (pscanf(path, "%ju", &txbytes) != 1) {
return NULL; return NULL;
} }

View File

@ -18,6 +18,8 @@
#include <limits.h> #include <limits.h>
#include <linux/wireless.h> #include <linux/wireless.h>
#define NET_OPERSTATE "/sys/class/net/%s/operstate"
const char * const char *
wifi_perc(const char *interface) wifi_perc(const char *interface)
{ {
@ -28,10 +30,8 @@
char status[5]; char status[5];
FILE *fp; FILE *fp;
if (esnprintf(path, sizeof(path), "/sys/class/net/%s/operstate", if (esnprintf(path, sizeof(path), NET_OPERSTATE, interface) < 0)
interface) < 0) {
return NULL; return NULL;
}
if (!(fp = fopen(path, "r"))) { if (!(fp = fopen(path, "r"))) {
warn("fopen '%s':", path); warn("fopen '%s':", path);
return NULL; return NULL;