Posted by 1esproc on Tue 20th Jan 06:22 (modification of post by 1esproc view diff)
diff | download | new post
- // Based on http://crunchbanglinux.org/pastebin/98
- // Converted by 1esproc
- // i'm tired (that's my excuse for the prev. vers) :P
- #include <stdio.h>
- #include <string.h>
- #include <malloc.h>
- int main(int argc, char *argv[]) {
- FILE *fp;
- float rate, voltage, wdrawn, adrawn;
- char *battery, *path, *substr, *substr_e;
- char line[256];
- int blen;
- rate = voltage = wdrawn = adrawn = 0.0;
- bzero(line, 256);
- if (argc < 2) {
- battery = malloc(5);
- strncpy(battery, "BAT0", 5);
- blen = 5;
- } else {
- if ((blen = strlen(argv[1])) > 8) {
- return -1;
- } else {
- battery = malloc(blen+1);
- strncpy(battery, argv[1], blen+1);
- }
- }
- path = malloc(26 + blen);
- strncpy(path, "/proc/acpi/battery/", 20);
- strncat(path, battery, blen);
- strncat(path, "/state", 6);
- if ((fp = fopen(path, "r")) == NULL) {
- return -1;
- } else {
- while (!feof(fp)) {
- if (fgets(line, 256, fp)) {
- if (strstr(line, "charging state") != NULL) {
- if (strstr(line, "discharging") == NULL) {
- return -1;
- }
- } else if ((substr = strstr(line, "present voltage")) != NULL) {
- substr += 17;
- substr_e = strchr(substr, ' ');
- *substr_e = '\0';
- voltage = atoi(substr) / 1000;
- } else if ((substr = strstr(line, "present rate")) != NULL) {
- substr += 14;
- substr_e = strchr(substr, ' ');
- if (strstr(line, "mW") != NULL) {
- *substr_e = '\0';
- wdrawn = atoi(substr) / 1000;
- } else if (strstr(line, "mA") != NULL) {
- *substr_e = '\0';
- adrawn = atoi(substr) / 1000;
- }
- }
- }
- }
- fclose(fp);
- rate = wdrawn + voltage * adrawn;
- }
- return 0;
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.