7871245f6853df1925cf1a47481feb689700c67a
12 #include "khatus_lib_log.h"
13 #include "khatus_lib_sensor.h"
14 #include "khatus_lib_time.h"
16 #define usage(...) {print_usage(); fprintf(stderr, "Error:\n " __VA_ARGS__); exit(EXIT_FAILURE);}
24 double opt_interval
= 1.0;
25 char *opt_battery
= "BAT0";
26 char *opt_fifo
= NULL
;
32 "%s: [OPT ...] FIFO\n"
34 "FIFO = string # path to fifo file\n"
35 "OPT = -i int # interval\n"
36 " | -b string # battery file name from /sys/class/power_supply/\n"
37 " | -h # help message (i.e. what you're reading now :) )\n",
42 opt_parse(int argc
, char **argv
)
46 while ((c
= getopt(argc
, argv
, "f:i:h")) != -1)
49 opt_battery
= calloc(strlen(optarg
), sizeof(char));
50 strcpy(opt_battery
, optarg
);
53 opt_interval
= atof(optarg
);
59 if (optopt
== 'f' || optopt
== 'i')
61 "Option -%c requires an argument.\n",
63 else if (isprint(optopt
))
65 "Unknown option `-%c'.\n",
69 "Unknown option character `\\x%x'.\n",
75 opt_fifo
= argv
[optind
];
76 debug("opt_fifo: %s\n", opt_fifo
);
78 usage("No filename was provided\n");
82 get_capacity(char *buf
)
87 if (!(fp
= fopen(path
, "r")))
88 fatal("Failed to open %s. errno: %d, msg: %s\n",
89 path
, errno
, strerror(errno
));
91 switch (fscanf(fp
, "%d", &cap
)) {
92 case -1: fatal("EOF\n");
93 case 0: fatal("Read 0\n");
98 return snprintf(buf
, 6, "%3d%%\n", cap
);
102 main(int argc
, char **argv
)
107 char *path_fmt
= "/sys/class/power_supply/%s/capacity";
108 struct timespec ti
= timespec_of_float(opt_interval
);
110 opt_parse(argc
, argv
);
112 memset(path
, '\0', PATH_MAX
);
113 snprintf(path
, PATH_MAX
, path_fmt
, opt_battery
);
114 loop(&ti
, opt_fifo
, buf
, &get_capacity
);
This page took 0.080663 seconds and 3 git commands to generate.