Compare commits

..

No commits in common. '419909e0ff207dab4eaaa3c6ff58c5b23eddffce' and '710b99c54e1dd62fe73241ae1eecf02e0a306fcb' have entirely different histories.

  1. 174
      bmon.c
  2. 7
      makefile

174
bmon.c

@ -1,10 +1,8 @@
#define _POSIX_C_SOURCE 200809L
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
@ -12,12 +10,8 @@
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <limits.h>
// TODO: handle multiple batteries
const char *CAPACITY_PATH = "/sys/class/power_supply/BAT1/capacity";
const char *STATUS_PATH = "/sys/class/power_supply/BAT1/status";
const char *POWER_PATH = "/sys/class/power_supply/BAT1/power_now";
const char *bat_base_path = "/sys/class/power_supply/";
int dead = 0;
void usage (void)
@ -32,26 +26,17 @@ void usage (void)
void int_handler (int signum)
{
(void)signum;
dead = 1;
}
int read_int (FILE *fp)
{
int x;
freopen(NULL, "r", fp);
fscanf(fp, "%d", &x);
return x;
}
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);
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;
@ -88,87 +73,100 @@ int main (int argc, char *argv[])
}
FILE *capacity_fp = NULL, *status_fp = NULL, *power_fp = NULL;
capacity_fp = fopen(CAPACITY_PATH, "r");
if (capacity_fp == NULL) {
fprintf(stderr, "Error opening %s: %s\n", CAPACITY_PATH, strerror(errno));
DIR *bdir = NULL;
if (!(bdir = opendir(bat_base_path))) {
perror("error opening base directory");
exit(1);
}
if (wflag) {
power_fp = fopen(POWER_PATH, "r");
if (power_fp == NULL) {
fprintf(stderr, "Error opening %s: %s\n", POWER_PATH, strerror(errno));
exit(1);
}
status_fp = fopen(STATUS_PATH, "r");
if (status_fp == NULL) {
fprintf(stderr, "Error opening %s: %s\n", STATUS_PATH, strerror(errno));
exit(1);
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");
exit(1);
}
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) {
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;
}
fclose(tmf);
free(tmp);
strcat(path, "/power_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));
FILE *output_fp = stdout;
}
closedir(bdir);
if (!fp_num) {
printf("no files were opened\n");
dead = 1;
}
FILE * ofile_ptr = stdout;
if (fflag) {
if ((output_fp = fopen(ofile_path, "w")) == NULL) {
fprintf(stderr, "Error opening %s: %s\n", ofile_path, strerror(errno));
if (!(ofile_ptr = fopen(ofile_path, "w"))) {
char *tmp = malloc(1024);
snprintf(tmp, 1024, "could not open %s", ofile_path);
perror(tmp);
free(tmp);
exit(1);
}
}
long int elapsed_time = 0;
fprintf(output_fp, "TIME\t");
fprintf(output_fp, "CAP\t");
if (wflag) fprintf(output_fp, "WATTS");
fprintf(output_fp, "\n");
while (!dead) {
int capacity = read_int(capacity_fp);
float power = 0;
if (capacity < 0) {
fprintf(stderr, "Error reading capacity: %s\n", strerror(errno));
break;
}
if (wflag) {
power = read_int(power_fp);
if (power < 0) {
fprintf(stderr, "Error reading power: %s\n", strerror(errno));
break;
}
static char b[128] = {0};
freopen(NULL, "r", status_fp);
if (fgets(b, sizeof(b), status_fp) == NULL) {
fprintf(stderr, "Error reading status: %s\n", strerror(errno));
long val;
char buf[64];
for (; !dead ; sleep(wait_time)) {
for (int i = 0; i < fp_num; i++) {
if (!freopen(NULL, "r", fp_list[i])) {
perror("error reading the file");
dead = 1;
}
if (strncmp(b, "Discharging\n", sizeof(b)) == 0) {
power *= -1;
}
// power is in microwatts
power /= 1e6;
if (!fgets(buf, 64, fp_list[i]))
dead = 1;
val = atol(buf);
fprintf(ofile_ptr, "%ld\t", wflag ? (long)(val / 1E3) : val);
}
fprintf(ofile_ptr, "%ld\n", uflag ? time(NULL) : elapsed_time);
if (fflush(ofile_ptr)) {
perror("error writing to file");
dead = 1;
}
fprintf(output_fp, "%ld\t", uflag ? time(NULL) : elapsed_time);
fprintf(output_fp, "%d\t", capacity);
if (wflag) fprintf(output_fp, "%.2f", power);
fprintf(output_fp, "\n");
sleep(wait_time);
elapsed_time += wait_time;
}
if (fflag) {
fclose(output_fp);
}
if (capacity_fp) {
fclose(capacity_fp);
}
if (status_fp) {
fclose(status_fp);
}
if (power_fp) {
fclose(power_fp);
}
if (fflag)
fclose(ofile_ptr);
for (int i = 0; i < fp_num; i++)
fclose(fp_list[i]);
free(fp_list);
return 0;
}

@ -1,11 +1,10 @@
CC = gcc
CFLAGS = -Wall -pedantic --std=c99 -O2
PREFIX = /usr/local
bmon: bmon.c
install:
cp bmon ${PREFIX}/bin/
dbg:
gcc -O0 -g bmon.c -o bmon-dbg
clean:
rm -f bmon
rm -f bmon bmon-dbg
Loading…
Cancel
Save