working prototype

master
Alessandro Mauri 4 years ago
parent f79c6988ee
commit 52300778dc
  1. 97
      bmon.c

@ -4,30 +4,56 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <poll.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
//char *bat_power_path = "/sys/class/power_supply/BAT1/energy_now";
//char *bat_capacity_path = "/sys/class/power_supply/BAT1/capacity";
#include <signal.h>
const char *bat_base_path = "/sys/class/power_supply/";
int dead = 0;
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[])
{
/* 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, "w")) != -1) {
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);
@ -41,8 +67,8 @@ int main (int argc, char *argv[])
perror("error opening base directory");
struct dirent *ent = NULL;
int *fd_list = NULL;
int fd_num = 0;
FILE **fp_list = NULL;
int fp_num = 0;
for (;;) {
errno = 0;
if (!(ent = readdir(bdir))) {
@ -54,7 +80,7 @@ int main (int argc, char *argv[])
if (!strstr(ent->d_name, "BAT1"))
continue;
int fd;
FILE *fp;
char path[sizeof(bat_base_path) + 256 + 64];
strcpy(path, bat_base_path);
strcat(path, ent->d_name);
@ -62,22 +88,57 @@ int main (int argc, char *argv[])
strcat(path, "/energy_now");
else strcat(path, "/capacity");
if ((fd = open(path, O_RDONLY | O_NONBLOCK)) != -1) {
int *tmp = NULL;
tmp = realloc(fd_list, sizeof(int) * (fd_num + 1));
if ((fp = fopen(path, "r"))) {
FILE **tmp = NULL;
tmp = realloc(fp_list, sizeof(FILE *) * (fp_num + 1));
if (!tmp)
perror("failed realloc");
fd_list = tmp;
fd_list[fd_num] = fd;
fd_num++;
fp_list = tmp;
fp_list[fp_num++] = fp;
} else printf("err: %s\n", strerror(errno));
}
closedir(bdir);
if (!fd_num)
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;
}
for (int i = 0; i < fd_num; i++)
close(fd_list[i]);
main_end:
if (fflag)
fclose(ofile_ptr);
for (int i = 0; i < fp_num; i++)
fclose(fp_list[i]);
free(fp_list);
return 0;
}

Loading…
Cancel
Save