#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)
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;
}
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;
}
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;
#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)
{
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);
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) {
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);
#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)
{
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;
}
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;
}