master
Alessandro Mauri 5 years ago
parent ce20f534f7
commit 8e4d061022
  1. 60
      bmon.c

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

Loading…
Cancel
Save