bug fixes
This commit is contained in:
parent
ce20f534f7
commit
8e4d061022
44
bmon.c
44
bmon.c
@ -63,8 +63,10 @@ int main (int argc, char *argv[])
|
||||
}
|
||||
|
||||
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;
|
||||
@ -72,8 +74,10 @@ int main (int argc, char *argv[])
|
||||
for (;;) {
|
||||
errno = 0;
|
||||
if (!(ent = readdir(bdir))) {
|
||||
if (errno)
|
||||
if (errno) {
|
||||
perror("error reading base directory");
|
||||
exit(1);
|
||||
}
|
||||
else break;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
@ -101,15 +115,17 @@ int main (int argc, char *argv[])
|
||||
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);
|
||||
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++)
|
||||
|
Loading…
Reference in New Issue
Block a user