|
|
|
@ -57,26 +57,30 @@ int main (int argc, char *argv[]) |
|
|
|
|
default: /* '?' */ |
|
|
|
|
usage(); |
|
|
|
|
exit(1); |
|
|
|
|
break;
|
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
DIR *bdir = NULL; |
|
|
|
|
if (!(bdir = opendir(bat_base_path))) |
|
|
|
|
if (!(bdir = opendir(bat_base_path))) { |
|
|
|
|
perror("error opening base directory"); |
|
|
|
|
|
|
|
|
|
exit(1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
struct dirent *ent = NULL; |
|
|
|
|
FILE **fp_list = NULL; |
|
|
|
|
int fp_num = 0; |
|
|
|
|
for (;;) { |
|
|
|
|
errno = 0; |
|
|
|
|
if (!(ent = readdir(bdir))) { |
|
|
|
|
if (errno) |
|
|
|
|
if (errno) { |
|
|
|
|
perror("error reading base directory"); |
|
|
|
|
exit(1); |
|
|
|
|
} |
|
|
|
|
else break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!strstr(ent->d_name, "BAT1")) |
|
|
|
|
continue; |
|
|
|
|
|
|
|
|
@ -84,9 +88,19 @@ int main (int argc, char *argv[]) |
|
|
|
|
char path[sizeof(bat_base_path) + 256 + 64]; |
|
|
|
|
strcpy(path, bat_base_path); |
|
|
|
|
strcat(path, ent->d_name); |
|
|
|
|
if (wflag) |
|
|
|
|
if (wflag) { |
|
|
|
|
char *tmp = malloc(1024); |
|
|
|
|
strcpy(tmp, path); |
|
|
|
|
strcat(tmp, "/status"); |
|
|
|
|
FILE *tmf = fopen(tmp, "r"); |
|
|
|
|
fgets(tmp, 1024, tmf); |
|
|
|
|
if (!strstr(tmp, "Dis")) { |
|
|
|
|
fprintf(stderr, "currently not discharging, cannot read power draw\n"); |
|
|
|
|
dead = 1; |
|
|
|
|
} |
|
|
|
|
free(tmp); |
|
|
|
|
strcat(path, "/energy_now"); |
|
|
|
|
else strcat(path, "/capacity"); |
|
|
|
|
} else strcat(path, "/capacity"); |
|
|
|
|
|
|
|
|
|
if ((fp = fopen(path, "r"))) { |
|
|
|
|
FILE **tmp = NULL; |
|
|
|
@ -96,20 +110,22 @@ int main (int argc, char *argv[]) |
|
|
|
|
fp_list = tmp; |
|
|
|
|
fp_list[fp_num++] = fp; |
|
|
|
|
} else printf("err: %s\n", strerror(errno)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
closedir(bdir); |
|
|
|
|
if (!fp_num) { |
|
|
|
|
printf("no files were opened\n"); |
|
|
|
|
goto main_end; |
|
|
|
|
dead = 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
FILE * ofile_ptr; |
|
|
|
|
|
|
|
|
|
FILE * ofile_ptr = NULL; |
|
|
|
|
if (fflag) { |
|
|
|
|
if (!(ofile_ptr = fopen(ofile_path, "w"))) { |
|
|
|
|
char * tmp = malloc(1024); |
|
|
|
|
char *tmp = malloc(1024); |
|
|
|
|
snprintf(tmp, 1024, "could not open %s", ofile_path); |
|
|
|
|
perror(tmp); |
|
|
|
|
free(tmp); |
|
|
|
|
exit(1); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -117,8 +133,12 @@ int main (int argc, char *argv[]) |
|
|
|
|
char buf[64]; |
|
|
|
|
for (; !dead ; sleep(wait_time)) { |
|
|
|
|
for (int i = 0; i < fp_num; i++) { |
|
|
|
|
freopen(NULL, "r", fp_list[i]); |
|
|
|
|
fgets(buf, 64, fp_list[i]); |
|
|
|
|
if (!freopen(NULL, "r", fp_list[i])) { |
|
|
|
|
perror("error reading the file"); |
|
|
|
|
dead = 1; |
|
|
|
|
} |
|
|
|
|
if (!fgets(buf, 64, fp_list[i])) |
|
|
|
|
dead = 1; |
|
|
|
|
buf[strcspn(buf, "\r\n")] = 0; |
|
|
|
|
if (fflag) |
|
|
|
|
fprintf(ofile_ptr, "%s\t", buf); |
|
|
|
@ -127,14 +147,16 @@ int main (int argc, char *argv[]) |
|
|
|
|
} |
|
|
|
|
if (fflag) { |
|
|
|
|
fprintf(ofile_ptr, "%lld\n", elapsed_time); |
|
|
|
|
fflush(ofile_ptr); |
|
|
|
|
} else {
|
|
|
|
|
if (fflush(ofile_ptr)) { |
|
|
|
|
perror("error writing to file"); |
|
|
|
|
dead = 1; |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
printf("%lld\n", elapsed_time); |
|
|
|
|
} |
|
|
|
|
elapsed_time += wait_time; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
main_end: |
|
|
|
|
|
|
|
|
|
if (fflag) |
|
|
|
|
fclose(ofile_ptr); |
|
|
|
|
for (int i = 0; i < fp_num; i++) |
|
|
|
|