summaryrefslogtreecommitdiff
path: root/slstatus.c
diff options
context:
space:
mode:
authorLaslo Hunhold <dev@frign.de>2017-08-11 00:17:49 +0200
committerAaron Marcher <me@drkhsh.at>2017-08-11 00:20:47 +0200
commit178c23e3d2b75a99010987bf3e17a82ff7b83b6a (patch)
tree93ac7d9d66f881a01a3da5e36a42d14d7fdd3bf2 /slstatus.c
parent6b8384ef2f7328580425ac2cc75e0c65a04aa942 (diff)
Refactor battery_state()
Diffstat (limited to 'slstatus.c')
-rw-r--r--slstatus.c29
1 files changed, 17 insertions, 12 deletions
diff --git a/slstatus.c b/slstatus.c
index 052af87..c4a9e92 100644
--- a/slstatus.c
+++ b/slstatus.c
@@ -142,10 +142,19 @@ battery_power(const char *bat)
static const char *
battery_state(const char *bat)
{
- char path[PATH_MAX];
- char state[12];
FILE *fp;
+ struct {
+ char *state;
+ char *symbol;
+ } map[] = {
+ { "Charging", "+" },
+ { "Discharging", "-" },
+ { "Full", "=" },
+ { "Unknown", "/" },
+ };
+ size_t i;
int n;
+ char path[PATH_MAX], state[12];
snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/status");
fp = fopen(path, "r");
@@ -158,17 +167,13 @@ battery_state(const char *bat)
if (n != 1)
return UNKNOWN_STR;
- if (strcmp(state, "Charging") == 0) {
- return "+";
- } else if (strcmp(state, "Discharging") == 0) {
- return "-";
- } else if (strcmp(state, "Full") == 0) {
- return "=";
- } else if (strcmp(state, "Unknown") == 0) {
- return "/";
- } else {
- return "?";
+ for (i = 0; i < sizeof(map) / sizeof(*map); i++) {
+ if (!strcmp(map[i].state, state)) {
+ break;
+ }
}
+
+ return (i == sizeof(map) / sizeof(*map)) ? "?" : map[i].symbol;
}
static const char *