new-acpi-battery.c (867B)
1 char * 2 readfile(char *base, char *file) 3 { 4 char *path, line[513]; 5 FILE *fd; 6 7 memset(line, 0, sizeof(line)); 8 9 path = smprintf("%s/%s", base, file); 10 fd = fopen(path, "r"); 11 if (fd == NULL) { 12 perror("fopen"); 13 exit(1); 14 } 15 free(path); 16 17 if (fgets(line, sizeof(line)-1, fd) == NULL) { 18 perror("fgets"); 19 exit(1); 20 } 21 fclose(fd); 22 23 return smprintf("%s", line); 24 } 25 26 char * 27 getbattery(char *base) 28 { 29 char *co; 30 int descap, remcap; 31 32 descap = -1; 33 remcap = -1; 34 35 co = readfile(base, "present"); 36 if (co[0] != '1') { 37 free(co); 38 return smprintf("not present"); 39 } 40 free(co); 41 42 co = readfile(base, "charge_full_design"); 43 sscanf(co, "%d", &descap); 44 free(co); 45 46 co = readfile(base, "charge_now"); 47 sscanf(co, "%d", &remcap); 48 free(co); 49 50 if (remcap < 0 || descap < 0) 51 return smprintf("invalid"); 52 53 return smprintf("%.0f", ((float)remcap / (float)descap) * 100); 54 } 55