From a1751622782eb9d3d9bfae56b75b857d8cbd0c6c Mon Sep 17 00:00:00 2001 From: Christoph Lohmann <20h@r-36.net> Date: Sun, 10 Sep 2017 19:28:54 +0200 Subject: Initial commit. --- new-acpi-battery.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 new-acpi-battery.c (limited to 'new-acpi-battery.c') diff --git a/new-acpi-battery.c b/new-acpi-battery.c new file mode 100644 index 0000000..cda0084 --- /dev/null +++ b/new-acpi-battery.c @@ -0,0 +1,55 @@ +char * +readfile(char *base, char *file) +{ + char *path, line[513]; + FILE *fd; + + memset(line, 0, sizeof(line)); + + path = smprintf("%s/%s", base, file); + fd = fopen(path, "r"); + if (fd == NULL) { + perror("fopen"); + exit(1); + } + free(path); + + if (fgets(line, sizeof(line)-1, fd) == NULL) { + perror("fgets"); + exit(1); + } + fclose(fd); + + return smprintf("%s", line); +} + +char * +getbattery(char *base) +{ + char *co; + int descap, remcap; + + descap = -1; + remcap = -1; + + co = readfile(base, "present"); + if (co[0] != '1') { + free(co); + return smprintf("not present"); + } + free(co); + + co = readfile(base, "charge_full_design"); + sscanf(co, "%d", &descap); + free(co); + + co = readfile(base, "charge_now"); + sscanf(co, "%d", &remcap); + free(co); + + if (remcap < 0 || descap < 0) + return smprintf("invalid"); + + return smprintf("%.0f", ((float)remcap / (float)descap) * 100); +} + -- cgit v1.2.3