working prototype
This commit is contained in:
parent
f79c6988ee
commit
52300778dc
97
bmon.c
97
bmon.c
@ -4,30 +4,56 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <poll.h>
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <signal.h>
|
||||||
//char *bat_power_path = "/sys/class/power_supply/BAT1/energy_now";
|
|
||||||
//char *bat_capacity_path = "/sys/class/power_supply/BAT1/capacity";
|
|
||||||
|
|
||||||
const char *bat_base_path = "/sys/class/power_supply/";
|
const char *bat_base_path = "/sys/class/power_supply/";
|
||||||
|
int dead = 0;
|
||||||
|
|
||||||
void usage (void)
|
void usage (void)
|
||||||
{
|
{
|
||||||
printf("usage: bmon [-w]\n");
|
printf("usage: bmon [-h] [-w] [-f file] [-t interval]\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void int_handler (int signum)
|
||||||
|
{
|
||||||
|
dead = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main (int argc, char *argv[])
|
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 wflag = 0;
|
||||||
|
int wait_time = 10;
|
||||||
|
int fflag = 0;
|
||||||
|
char ofile_path[256];
|
||||||
|
|
||||||
int opc;
|
int opc;
|
||||||
while ((opc = getopt(argc, argv, "w")) != -1) {
|
while ((opc = getopt(argc, argv, "hwf:t:")) != -1) {
|
||||||
switch (opc) {
|
switch (opc) {
|
||||||
case 'w':
|
case 'w':
|
||||||
wflag = 1;
|
wflag = 1;
|
||||||
break;
|
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: /* '?' */
|
default: /* '?' */
|
||||||
usage();
|
usage();
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -41,8 +67,8 @@ int main (int argc, char *argv[])
|
|||||||
perror("error opening base directory");
|
perror("error opening base directory");
|
||||||
|
|
||||||
struct dirent *ent = NULL;
|
struct dirent *ent = NULL;
|
||||||
int *fd_list = NULL;
|
FILE **fp_list = NULL;
|
||||||
int fd_num = 0;
|
int fp_num = 0;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
errno = 0;
|
errno = 0;
|
||||||
if (!(ent = readdir(bdir))) {
|
if (!(ent = readdir(bdir))) {
|
||||||
@ -54,7 +80,7 @@ int main (int argc, char *argv[])
|
|||||||
if (!strstr(ent->d_name, "BAT1"))
|
if (!strstr(ent->d_name, "BAT1"))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
int fd;
|
FILE *fp;
|
||||||
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);
|
||||||
@ -62,22 +88,57 @@ int main (int argc, char *argv[])
|
|||||||
strcat(path, "/energy_now");
|
strcat(path, "/energy_now");
|
||||||
else strcat(path, "/capacity");
|
else strcat(path, "/capacity");
|
||||||
|
|
||||||
if ((fd = open(path, O_RDONLY | O_NONBLOCK)) != -1) {
|
if ((fp = fopen(path, "r"))) {
|
||||||
int *tmp = NULL;
|
FILE **tmp = NULL;
|
||||||
tmp = realloc(fd_list, sizeof(int) * (fd_num + 1));
|
tmp = realloc(fp_list, sizeof(FILE *) * (fp_num + 1));
|
||||||
if (!tmp)
|
if (!tmp)
|
||||||
perror("failed realloc");
|
perror("failed realloc");
|
||||||
fd_list = tmp;
|
fp_list = tmp;
|
||||||
fd_list[fd_num] = fd;
|
fp_list[fp_num++] = fp;
|
||||||
fd_num++;
|
|
||||||
} else printf("err: %s\n", strerror(errno));
|
} else printf("err: %s\n", strerror(errno));
|
||||||
|
|
||||||
}
|
}
|
||||||
closedir(bdir);
|
closedir(bdir);
|
||||||
if (!fd_num)
|
if (!fp_num) {
|
||||||
printf("no files were opened\n");
|
printf("no files were opened\n");
|
||||||
|
goto main_end;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < fd_num; i++)
|
FILE * ofile_ptr;
|
||||||
close(fd_list[i]);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user