b589bef27891612428ff7da18494ebd0673c735e
12 #include "khatus_lib_log.h"
13 #include "khatus_lib_time.h"
15 #define usage(...) {print_usage(); fprintf(stderr, "Error:\n " __VA_ARGS__); exit(EXIT_FAILURE);}
18 #define END_OF_MESSAGE '\n'
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 loop(struct timespec
*ti
, char *fifo
, char *buf
, int fun(char *))
85 int w
= -1; /* written */
86 int r
= -1; /* remaining */
87 int i
= -1; /* buffer position */
90 debug("openning \"%s\"\n", fifo
);
91 fd
= open(fifo
, O_WRONLY
);
93 fatal("Failed to open FIFO file: \"%s\". Error: %s\n",
96 debug("openned. fd: %d\n", fd
);
98 buf
[r
] = END_OF_MESSAGE
;
99 for (i
= 0; (w
= write(fd
, buf
+ i
++, 1)) && r
; r
--)
102 fatal("Failed to write to %s. Err num: %d, Err msg: %s\n",
107 fatal("Failed to close %s. Err num: %d, Err msg: %s\n",
112 debug("closed. fd: %d\n", fd
);
118 read_capacity(char *buf
)
123 if (!(fp
= fopen(path
, "r")))
124 fatal("Failed to open %s. errno: %d, msg: %s\n",
125 path
, errno
, strerror(errno
));
127 switch (fscanf(fp
, "%d", &cap
)) {
128 case -1: fatal("EOF\n");
129 case 0: fatal("Read 0\n");
134 return snprintf(buf
, 6, "%3d%%\n", cap
);
138 main(int argc
, char **argv
)
143 char *path_fmt
= "/sys/class/power_supply/%s/capacity";
144 struct timespec ti
= timespec_of_float(opt_interval
);
146 opt_parse(argc
, argv
);
148 memset(path
, '\0', PATH_MAX
);
149 snprintf(path
, PATH_MAX
, path_fmt
, opt_battery
);
150 loop(&ti
, opt_fifo
, buf
, &read_capacity
);
This page took 0.073459 seconds and 4 git commands to generate.