From 764813c0c1a83630ccb7ea589bcc595a78c4a08d Mon Sep 17 00:00:00 2001 From: Sravan Balaji Date: Fri, 7 Aug 2020 00:05:13 -0400 Subject: [PATCH] No Battery Fix - Check if battery information directory exists before displaying info - Fix divider spacing in battery-all-in-one --- components/battery.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/components/battery.c b/components/battery.c index e1eeff7..b26f1c5 100644 --- a/components/battery.c +++ b/components/battery.c @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include "../util.h" @@ -121,18 +123,28 @@ battery_all_in_one(const char *bat) { char path[PATH_MAX]; + char *div = ""; - // Handle case where battery isn't present if (esnprintf(path, sizeof(path), - "/sys/class/power_supply/%s/capacity", bat) < 0) { - return ""; + "/sys/class/power_supply/%s", bat) < 0) { + return bprintf(" %s ", div); + } + + DIR* dir = opendir(path); + + if (dir) { + // Battery exists + closedir(dir); + } + else if (ENOENT == errno) { + // Battery doesn't exist + return bprintf(" %s ", div); } int percentage = atoi(battery_perc(bat)); char *bat_icon = ""; char *bat_status = ""; char *bat_rem = ""; - char *div = ""; // Add battery percentage icon if (percentage < 20) { @@ -178,7 +190,7 @@ sprintf(bat_rem, " (%s)", temp); } - return bprintf("%s %s%s %i%%%s %s ", div, bat_icon, bat_status, percentage, bat_rem, div); + return bprintf(" %s %s%s %i%%%s %s ", div, bat_icon, bat_status, percentage, bat_rem, div); } #elif defined(__OpenBSD__) #include