aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2024-09-02 08:00:00 +0800
committerRunxi Yu <me@runxiyu.org>2024-09-02 08:00:00 +0800
commit5ca6a785890fd3c9aeff3b67e09c57d319f2e18a (patch)
tree7afda4893b600b8b18a5d24af5d835a1ca5e26ff
parentFix buffer overflow (diff)
downloadrxbar-master.tar.gz
rxbar-master.zip
Show battery life tooHEADmaster
-rw-r--r--rxbar.c18
1 files changed, 12 insertions, 6 deletions
diff --git a/rxbar.c b/rxbar.c
index 10468df..43694ec 100644
--- a/rxbar.c
+++ b/rxbar.c
@@ -10,24 +10,27 @@
#include <fcntl.h>
#include <stdint.h>
-#define MAXLEN 4
+#define MAXLEN 7
time_t t;
char buf[MAXLEN + 2];
-size_t len;
+char buf2[MAXLEN + 2];
+size_t len, len2;
struct tm td;
-int fdc, fds;
+int fdt, fds, fdc;
time_t t1;
int main()
{
+ fdt = open("/sys/class/power_supply/macsmc-battery/time_to_empty_now", O_RDONLY);
fdc = open("/sys/class/power_supply/macsmc-battery/capacity", O_RDONLY);
fds = open("/sys/class/power_supply/macsmc-battery/status", O_RDONLY);
- if (fdc == -1 || fds == -1) {
+ if (fdt == -1 || fds == -1) {
+ fdt = open("/sys/class/power_supply/BAT0/time_to_empty_now", O_RDONLY);
fdc = open("/sys/class/power_supply/BAT0/capacity", O_RDONLY);
fds = open("/sys/class/power_supply/BAT0/status", O_RDONLY);
}
- if (fdc == -1 || fds == -1) {
+ if (fdt == -1 || fds == -1) {
// no battery
for (;;) {
t = time(NULL);
@@ -52,10 +55,13 @@ int main()
td = *localtime(&t);
len = pread(fdc, buf + 1, MAXLEN, 0);
pread(fds, buf, 1, 0);
+ len2 = pread(fdt, buf2, MAXLEN, 0);
buf[len] = '\0';
+ buf2[len2-1] = '\0';
dprintf(STDOUT_FILENO,
- "%s %d-%02d-%02d %02d:%02d:%02d\n",
+ "%s.%s %d-%02d-%02d %02d:%02d:%02d\n",
buf,
+ buf2,
td.tm_year + 1900,
td.tm_mon + 1,
td.tm_mday,