e4625c676ef8b387a3747fa96924ec68a424163a
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);}
22 double opt_interval
= 1.0;
23 char *opt_battery
= "BAT0";
24 char *opt_fifo
= NULL
;
30 "%s: [OPT ...] FIFO\n"
32 "FIFO = string # path to fifo file\n"
33 "OPT = -i int # interval\n"
34 " | -b string # battery file name from /sys/class/power_supply/\n"
35 " | -h # help message (i.e. what you're reading now :) )\n",
40 opt_parse(int argc
, char **argv
)
44 while ((c
= getopt(argc
, argv
, "f:i:h")) != -1)
47 opt_battery
= calloc(strlen(optarg
), sizeof(char));
48 strcpy(opt_battery
, optarg
);
51 opt_interval
= atof(optarg
);
57 if (optopt
== 'f' || optopt
== 'i')
59 "Option -%c requires an argument.\n",
61 else if (isprint(optopt
))
63 "Unknown option `-%c'.\n",
67 "Unknown option character `\\x%x'.\n",
73 opt_fifo
= argv
[optind
];
74 debug("opt_fifo: %s\n", opt_fifo
);
76 usage("No filename was provided\n");
80 get_capacity(char *buf
, char *path
)
85 if (!(fp
= fopen(path
, "r")))
86 fatal("Failed to open %s. errno: %d, msg: %s\n",
87 path
, errno
, strerror(errno
));
89 switch (fscanf(fp
, "%d", &cap
)) {
90 case -1: fatal("EOF\n");
91 case 0: fatal("Read 0\n");
96 return snprintf(buf
, 6, "%3d%%\n", cap
);
100 main(int argc
, char **argv
)
106 char *path_fmt
= "/sys/class/power_supply/%s/capacity";
107 struct timespec ti
= timespec_of_float(opt_interval
);
109 opt_parse(argc
, argv
);
111 memset(path
, '\0', PATH_MAX
);
112 snprintf(path
, PATH_MAX
, path_fmt
, opt_battery
);
117 (SENSOR_FUN_T
) get_capacity
,
118 (SENSOR_PARAMS_T
) path
This page took 0.081198 seconds and 3 git commands to generate.