mirror of git://git.suckless.org/slstatus
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:
parent
c46c1487a9
commit
69b2487650
|
@ -10,6 +10,13 @@
|
|||
#include <stdint.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 *
|
||||
pick(const char *bat, const char *f1, const char *f2, char *path,
|
||||
size_t length)
|
||||
|
@ -33,10 +40,8 @@
|
|||
int 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) {
|
||||
return NULL;
|
||||
}
|
||||
|
@ -59,10 +64,8 @@
|
|||
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, "%12[a-zA-Z ]", state) != 1) {
|
||||
return NULL;
|
||||
}
|
||||
|
@ -82,28 +85,22 @@
|
|||
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, "%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) {
|
||||
return NULL;
|
||||
|
@ -201,6 +198,10 @@
|
|||
#elif defined(__FreeBSD__)
|
||||
#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 *
|
||||
battery_perc(const char *unused)
|
||||
{
|
||||
|
@ -208,8 +209,7 @@
|
|||
size_t len;
|
||||
|
||||
len = sizeof(cap);
|
||||
if (sysctlbyname("hw.acpi.battery.life", &cap, &len, NULL, 0) == -1
|
||||
|| !len)
|
||||
if (sysctlbyname(BATTERY_LIFE, &cap, &len, NULL, 0) < 0 || !len)
|
||||
return NULL;
|
||||
|
||||
return bprintf("%d", cap);
|
||||
|
@ -222,8 +222,7 @@
|
|||
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) {
|
||||
|
@ -244,9 +243,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);
|
||||
|
|
|
@ -7,16 +7,16 @@
|
|||
#include "../slstatus.h"
|
||||
|
||||
#if defined(__linux__)
|
||||
#define CPU_FREQ "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"
|
||||
|
||||
const char *
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -6,15 +6,15 @@
|
|||
|
||||
#include "../util.h"
|
||||
|
||||
#define ENTROPY_AVAIL "/proc/sys/kernel/random/entropy_avail"
|
||||
|
||||
const char *
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -8,6 +8,9 @@
|
|||
#if defined(__linux__)
|
||||
#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 *
|
||||
netspeed_rx(const char *interface)
|
||||
{
|
||||
|
@ -18,11 +21,8 @@
|
|||
|
||||
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) {
|
||||
return NULL;
|
||||
}
|
||||
|
@ -44,11 +44,8 @@
|
|||
|
||||
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) {
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
@ -18,6 +18,8 @@
|
|||
#include <limits.h>
|
||||
#include <linux/wireless.h>
|
||||
|
||||
#define NET_OPERSTATE "/sys/class/net/%s/operstate"
|
||||
|
||||
const char *
|
||||
wifi_perc(const char *interface)
|
||||
{
|
||||
|
@ -28,10 +30,8 @@
|
|||
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;
|
||||
|
|
Loading…
Reference in New Issue