Hi,
I wanna write some small battery-indicator in C using acpi. With bash I would simply do:
Code:
$ cat /proc/acpi/battery/BAT0/state
present: yes
capacity state: ok
charging state: charged
present rate: 0 mA
remaining capacity: 3403 mAh
present voltage: 15000 mV
$ grep remain /proc/acpi/battery/BAT0/state|sed 's/^.* //'|sed 's/mAh$//'
3403
But how in C? I already did:
Code:
$ cat bat.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *fp;
char str[128];
char value[20];
int i;
fp = fopen("/proc/acpi/battery/BAT0/state", "r");
while(fgets(str, 126, fp)) {
if (strstr(str,"remaining capacity:") != NULL){
printf("%s",str);
}
}
fclose(fp);
return 0;
}
$ gcc -o bat bat.c
$ ./bat
remaining capacity: 3403 mAh
But how can I now remove all unneeded things of this line? I just need all the numbers. So is it best to loop through the entire string char by char taking a look if the ascii-value is a number and if it is using strcat to create a new string?
Or do you have a better solution for me?