DWM Status Bar
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

55 lines
867 B

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);
}