13 #include "khlib_log.h"
14 #include "khlib_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";
31 "OPT = -i int # interval\n"
32 " | -b string # battery file name from /sys/class/power_supply/\n"
33 " | -h # help message (i.e. what you're reading now :) )\n",
38 opt_parse(int argc
, char **argv
)
42 while ((c
= getopt(argc
, argv
, "b:i:h")) != -1)
45 opt_battery
= calloc(strlen(optarg
), sizeof(char));
46 strcpy(opt_battery
, optarg
);
49 opt_interval
= atof(optarg
);
55 if (optopt
== 'b' || optopt
== 'i')
57 "Option -%c requires an argument.\n",
59 else if (isprint(optopt
))
61 "Unknown option `-%c'.\n",
65 "Unknown option character `\\x%x'.\n",
74 get_capacity(char *buf
, char *path
)
79 if (!(fp
= fopen(path
, "r")))
81 "Failed to open %s. errno: %d, msg: %s\n",
87 switch (fscanf(fp
, "%d", &cap
)) {
88 case -1: khlib_fatal("EOF\n");
89 case 0: khlib_fatal("Read 0\n");
94 return snprintf(buf
, 6, "%3d%%", cap
);
98 main(int argc
, char **argv
)
104 char *path_fmt
= "/sys/class/power_supply/%s/capacity";
107 opt_parse(argc
, argv
);
108 ti
= khlib_timespec_of_float(opt_interval
);
109 khlib_debug("opt_battery: \"%s\"\n", opt_battery
);
110 khlib_debug("opt_interval: %f\n", opt_interval
);
111 khlib_debug("ti: {tv_sec = %ld, tv_nsec = %ld}\n",ti
.tv_sec
,ti
.tv_nsec
);
112 memset(path
, '\0', PATH_MAX
);
113 snprintf(path
, PATH_MAX
, path_fmt
, opt_battery
);
114 signal(SIGPIPE
, SIG_IGN
);
117 get_capacity(buf
, path
);
This page took 0.102898 seconds and 4 git commands to generate.