bug fixes
This commit is contained in:
parent
ce20f534f7
commit
8e4d061022
42
bmon.c
42
bmon.c
@ -63,8 +63,10 @@ int main (int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
@ -72,8 +74,10 @@ int main (int argc, char *argv[])
|
|||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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;
|
||||||
@ -101,15 +115,17 @@ int main (int argc, char *argv[])
|
|||||||
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)) {
|
||||||
|
perror("error writing to file");
|
||||||
|
dead = 1;
|
||||||
|
}
|
||||||
} else {
|
} 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…
Reference in New Issue
Block a user