You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
2.7 KiB
144 lines
2.7 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
#include <dirent.h>
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <signal.h>
|
|
|
|
const char *bat_base_path = "/sys/class/power_supply/";
|
|
int dead = 0;
|
|
|
|
void usage (void)
|
|
{
|
|
printf("usage: bmon [-h] [-w] [-f file] [-t interval]\n");
|
|
}
|
|
|
|
void int_handler (int signum)
|
|
{
|
|
dead = 1;
|
|
}
|
|
|
|
int main (int argc, char *argv[])
|
|
{
|
|
/* Handle SIGINT */
|
|
dead = 0;
|
|
struct sigaction action;
|
|
memset(&action, 0, sizeof(action));
|
|
action.sa_handler = int_handler;
|
|
sigaction(SIGINT, &action, NULL);
|
|
|
|
int wflag = 0;
|
|
int wait_time = 10;
|
|
int fflag = 0;
|
|
char ofile_path[256];
|
|
|
|
int opc;
|
|
while ((opc = getopt(argc, argv, "hwf:t:")) != -1) {
|
|
switch (opc) {
|
|
case 'w':
|
|
wflag = 1;
|
|
break;
|
|
case 'f':
|
|
fflag = 1;
|
|
strncpy(ofile_path, optarg, 255);
|
|
break;
|
|
case 't':
|
|
wait_time = atoi(optarg);
|
|
if (wait_time < 0) {
|
|
usage();
|
|
exit(1);
|
|
}
|
|
break;
|
|
case 'h':
|
|
default: /* '?' */
|
|
usage();
|
|
exit(1);
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
DIR *bdir = NULL;
|
|
if (!(bdir = opendir(bat_base_path)))
|
|
perror("error opening base directory");
|
|
|
|
struct dirent *ent = NULL;
|
|
FILE **fp_list = NULL;
|
|
int fp_num = 0;
|
|
for (;;) {
|
|
errno = 0;
|
|
if (!(ent = readdir(bdir))) {
|
|
if (errno)
|
|
perror("error reading base directory");
|
|
else break;
|
|
}
|
|
|
|
if (!strstr(ent->d_name, "BAT1"))
|
|
continue;
|
|
|
|
FILE *fp;
|
|
char path[sizeof(bat_base_path) + 256 + 64];
|
|
strcpy(path, bat_base_path);
|
|
strcat(path, ent->d_name);
|
|
if (wflag)
|
|
strcat(path, "/energy_now");
|
|
else strcat(path, "/capacity");
|
|
|
|
if ((fp = fopen(path, "r"))) {
|
|
FILE **tmp = NULL;
|
|
tmp = realloc(fp_list, sizeof(FILE *) * (fp_num + 1));
|
|
if (!tmp)
|
|
perror("failed realloc");
|
|
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;
|
|
}
|
|
|
|
FILE * ofile_ptr;
|
|
if (fflag) {
|
|
if (!(ofile_ptr = fopen(ofile_path, "w"))) {
|
|
char * tmp = malloc(1024);
|
|
snprintf(tmp, 1024, "could not open %s", ofile_path);
|
|
perror(tmp);
|
|
}
|
|
}
|
|
|
|
long long elapsed_time = 0;
|
|
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]);
|
|
buf[strcspn(buf, "\r\n")] = 0;
|
|
if (fflag)
|
|
fprintf(ofile_ptr, "%s\t", buf);
|
|
else
|
|
printf("%s\t", buf);
|
|
}
|
|
if (fflag) {
|
|
fprintf(ofile_ptr, "%lld\n", elapsed_time);
|
|
fflush(ofile_ptr);
|
|
} 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++)
|
|
fclose(fp_list[i]);
|
|
free(fp_list);
|
|
return 0;
|
|
}
|
|
|