[hackers] [slstatus] Fix memory leak || Aaron Marcher
commit 9be2be7197d9ba51abaeb98675fc81676a72b1df
Author: Aaron Marcher <me_AT_drkhsh.at>
AuthorDate: Fri May 18 16:52:10 2018 +0200
Commit: Aaron Marcher <me_AT_drkhsh.at>
CommitDate: Fri May 18 16:52:10 2018 +0200
Fix memory leak
diff --git a/components/battery.c b/components/battery.c
index 84b2c11..b0cd97b 100644
--- a/components/battery.c
+++ b/components/battery.c
_AT_@ -49,8 +49,32 @@
const char *
battery_remaining(const char *bat)
{
- /* TODO: Implement */
- return NULL;
+ int charge_now, current_now;
+ char path[PATH_MAX], state[12];
+
+ snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/",
+ bat, "/status");
+ if (pscanf(path, "%12s", state) != 1) {
+ return NULL;
+ }
+
+ if (!strcmp(state, "Discharging")) {
+ snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/",
+ bat, "/charge_now");
+ if (pscanf(path, "%i", charge_now) != 1) {
+ return NULL;
+ }
+ snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/",
+ bat, "/current_now");
+ if (pscanf(path, "%i", current_now) != 1) {
+ return NULL;
+ }
+
+ return bprintf("%d")
+ }
+
+ return (pscanf(path, "%i", &perc) == 1) ? bprintf("%d", perc) : NULL;
+
}
#elif defined(__OpenBSD__)
#include <fcntl.h>
_AT_@ -125,7 +149,7 @@
return bprintf("%u:%02u", apm_info.minutes_left / 60,
apm_info.minutes_left % 60);
} else {
- return strdup("");
+ return "";
}
}
Received on Fri May 18 2018 - 16:54:11 CEST
This archive was generated by hypermail 2.3.0
: Fri May 18 2018 - 17:00:26 CEST