| 1 | #include <signal.h> |
| 2 | #include <assert.h> |
| 3 | #include <ctype.h> |
| 4 | #include <errno.h> |
| 5 | #include <limits.h> |
| 6 | #include <signal.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include <time.h> |
| 11 | #include <unistd.h> |
| 12 | |
| 13 | #include "khlib_log.h" |
| 14 | #include "khlib_time.h" |
| 15 | |
| 16 | #define usage(...) {print_usage(); fprintf(stderr, "Error:\n " __VA_ARGS__); exit(EXIT_FAILURE);} |
| 17 | |
| 18 | #define MAX_LEN 20 |
| 19 | |
| 20 | char *argv0; |
| 21 | |
| 22 | double opt_interval = 1.0; |
| 23 | char *opt_battery = "BAT0"; |
| 24 | |
| 25 | void |
| 26 | print_usage() |
| 27 | { |
| 28 | printf( |
| 29 | "%s: [OPT ...]\n" |
| 30 | "\n" |
| 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", |
| 34 | argv0); |
| 35 | } |
| 36 | |
| 37 | void |
| 38 | opt_parse(int argc, char **argv) |
| 39 | { |
| 40 | char c; |
| 41 | |
| 42 | while ((c = getopt(argc, argv, "b:i:h")) != -1) |
| 43 | switch (c) { |
| 44 | case 'b': |
| 45 | opt_battery = calloc(strlen(optarg), sizeof(char)); |
| 46 | strcpy(opt_battery, optarg); |
| 47 | break; |
| 48 | case 'i': |
| 49 | opt_interval = atof(optarg); |
| 50 | break; |
| 51 | case 'h': |
| 52 | print_usage(); |
| 53 | exit(EXIT_SUCCESS); |
| 54 | case '?': |
| 55 | if (optopt == 'b' || optopt == 'i') |
| 56 | fprintf(stderr, |
| 57 | "Option -%c requires an argument.\n", |
| 58 | optopt); |
| 59 | else if (isprint(optopt)) |
| 60 | fprintf (stderr, |
| 61 | "Unknown option `-%c'.\n", |
| 62 | optopt); |
| 63 | else |
| 64 | fprintf(stderr, |
| 65 | "Unknown option character `\\x%x'.\n", |
| 66 | optopt); |
| 67 | exit(EXIT_FAILURE); |
| 68 | default: |
| 69 | assert(0); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | int |
| 74 | get_capacity(char *buf, char *path) |
| 75 | { |
| 76 | FILE *fp; |
| 77 | int cap; |
| 78 | |
| 79 | if (!(fp = fopen(path, "r"))) |
| 80 | khlib_fatal( |
| 81 | "Failed to open %s. errno: %d, msg: %s\n", |
| 82 | path, |
| 83 | errno, |
| 84 | strerror(errno) |
| 85 | ); |
| 86 | |
| 87 | switch (fscanf(fp, "%d", &cap)) { |
| 88 | case -1: khlib_fatal("EOF\n"); |
| 89 | case 0: khlib_fatal("Read 0\n"); |
| 90 | case 1: break; |
| 91 | default: assert(0); |
| 92 | } |
| 93 | fclose(fp); |
| 94 | return snprintf(buf, 6, "%3d%%", cap); |
| 95 | } |
| 96 | |
| 97 | int |
| 98 | main(int argc, char **argv) |
| 99 | { |
| 100 | argv0 = argv[0]; |
| 101 | |
| 102 | char buf[10]; |
| 103 | char path[PATH_MAX]; |
| 104 | char *path_fmt = "/sys/class/power_supply/%s/capacity"; |
| 105 | struct timespec ti; |
| 106 | |
| 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); |
| 115 | |
| 116 | for (;;) { |
| 117 | get_capacity(buf, path); |
| 118 | puts(buf); |
| 119 | fflush(stdout); |
| 120 | khlib_sleep(&ti); |
| 121 | } |
| 122 | return EXIT_SUCCESS; |
| 123 | } |