added -u option (unix time stamp)

master
Alessandro Mauri 5 years ago
parent 8e4d061022
commit c40b33fb97
  1. 17
      bmon.c

@ -8,13 +8,14 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include <time.h>
const char *bat_base_path = "/sys/class/power_supply/"; const char *bat_base_path = "/sys/class/power_supply/";
int dead = 0; int dead = 0;
void usage (void) void usage (void)
{ {
printf("usage: bmon [-h] [-w] [-f file] [-t interval]\n"); printf("usage: bmon [-wuh] [-f file] [-t interval]\n");
} }
void int_handler (int signum) void int_handler (int signum)
@ -34,10 +35,11 @@ int main (int argc, char *argv[])
int wflag = 0; int wflag = 0;
int wait_time = 10; int wait_time = 10;
int fflag = 0; int fflag = 0;
int uflag = 0;
char ofile_path[256]; char ofile_path[256];
int opc; int opc;
while ((opc = getopt(argc, argv, "hwf:t:")) != -1) { while ((opc = getopt(argc, argv, "uhwf:t:")) != -1) {
switch (opc) { switch (opc) {
case 'w': case 'w':
wflag = 1; wflag = 1;
@ -48,11 +50,14 @@ int main (int argc, char *argv[])
break; break;
case 't': case 't':
wait_time = atoi(optarg); wait_time = atoi(optarg);
if (wait_time < 0) { if (wait_time < 1) {
usage(); usage();
exit(1); exit(1);
} }
break; break;
case 'u':
uflag = 1;
break;
case 'h': case 'h':
default: /* '?' */ default: /* '?' */
usage(); usage();
@ -129,7 +134,7 @@ int main (int argc, char *argv[])
} }
} }
long long elapsed_time = 0; long int elapsed_time = 0;
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++) {
@ -146,13 +151,13 @@ int main (int argc, char *argv[])
printf("%s\t", buf); printf("%s\t", buf);
} }
if (fflag) { if (fflag) {
fprintf(ofile_ptr, "%lld\n", elapsed_time); fprintf(ofile_ptr, "%ld\n", uflag ? time(NULL) : elapsed_time);
if (fflush(ofile_ptr)) { if (fflush(ofile_ptr)) {
perror("error writing to file"); perror("error writing to file");
dead = 1; dead = 1;
} }
} else { } else {
printf("%lld\n", elapsed_time); printf("%ld\n", uflag ? time(NULL) : elapsed_time);
} }
elapsed_time += wait_time; elapsed_time += wait_time;
} }

Loading…
Cancel
Save