diff options
author | Rob Landley <rob@landley.net> | 2023-12-04 04:05:46 -0600 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2023-12-04 04:05:46 -0600 |
commit | 26883294d524c69aaf007629607232bc3c0fff8b (patch) | |
tree | 320b150ee98f189679ff8d253bf83df1505e3279 | |
parent | b6d1d9068c41a0dec960f723b66555c0d96e1535 (diff) | |
download | toybox-26883294d524c69aaf007629607232bc3c0fff8b.tar.gz |
Cleanup pass: FLAG() macros, yank unnecessary globals, whitespace/indent.
-rw-r--r-- | toys/other/acpi.c | 90 |
1 files changed, 41 insertions, 49 deletions
diff --git a/toys/other/acpi.c b/toys/other/acpi.c index 3e19141c..193ac28a 100644 --- a/toys/other/acpi.c +++ b/toys/other/acpi.c @@ -24,11 +24,6 @@ config ACPI #define FOR_acpi #include "toys.h" -GLOBALS( - int ac, bat, therm, cool; - char *cpath; -) - static int read_int_at(int dirfd, char *name) { int fd, ret=0; @@ -43,101 +38,98 @@ static int read_int_at(int dirfd, char *name) static int acpi_callback(struct dirtree *tree) { - int dfd, fd, len, on; + int dfd, fd, len, on, bat = 0, ac = 0; + char *cpath; errno = 0; - if (tree->name[0]=='.') return 0; - - if (!tree->parent) - return DIRTREE_RECURSE|DIRTREE_SYMFOLLOW; + if (*tree->name=='.') return 0; + if (!tree->parent) return DIRTREE_RECURSE|DIRTREE_SYMFOLLOW; - if (0 <= (dfd = open((TT.cpath=dirtree_path(tree, NULL)), O_RDONLY))) { + if (0<=(dfd = open((cpath = dirtree_path(tree, 0)), O_RDONLY))) { if ((fd = openat(dfd, "type", O_RDONLY)) < 0) goto done; len = readall(fd, toybuf, sizeof(toybuf)); close(fd); - if (len < 1) goto done; + if (len<1) goto done; if (!strncmp(toybuf, "Battery", 7)) { - if ((toys.optflags & FLAG_b) || (!toys.optflags)) { + if (FLAG(b) || !toys.optflags) { int cap = 0, curr = 0, max = 0; - if ((cap = read_int_at(dfd, "capacity")) < 0) { - if ((max = read_int_at(dfd, "charge_full")) > 0) + if ((cap = read_int_at(dfd, "capacity"))<0) { + if ((max = read_int_at(dfd, "charge_full"))>0 || + (max = read_int_at(dfd, "energy_full"))>0) curr = read_int_at(dfd, "charge_now"); - else if ((max = read_int_at(dfd, "energy_full")) > 0) - curr = read_int_at(dfd, "energy_now"); - if (max > 0 && curr >= 0) cap = 100 * curr / max; + if (max>0 && curr>=0) cap = 100*curr/max; } - if (cap >= 0) printf("Battery %d: %d%%\n", TT.bat++, cap); + if (cap>=0) printf("Battery %d: %d%%\n", bat++, cap); } - } else if (toys.optflags & FLAG_a) { - if ((on = read_int_at(dfd, "online")) >= 0) - printf("Adapter %d: %s-line\n", TT.ac++, (on ? "on" : "off")); - } + } else if (FLAG(a) && (on = read_int_at(dfd, "online"))>=0) + printf("Adapter %d: %s-line\n", ac++, on ? "on" : "off"); done: close(dfd); } - free(TT.cpath); + free(cpath); return 0; } static int temp_callback(struct dirtree *tree) { - int dfd, temp; + int dfd, temp, therm = 0; + char *cpath; if (*tree->name=='.') return 0; if (!tree->parent || !tree->parent->parent) return DIRTREE_RECURSE|DIRTREE_SYMFOLLOW; errno = 0; - if (0 <= (dfd = open((TT.cpath=dirtree_path(tree, NULL)), O_RDONLY))) { + if (0<=(dfd = open((cpath = dirtree_path(tree, 0)), O_RDONLY))) { if ((0 < (temp = read_int_at(dfd, "temp"))) || !errno) { - //some tempertures are in milli-C, some in deci-C - //reputedly some are in deci-K, but I have not seen them - if (((temp >= 1000) || (temp <= -1000)) && (temp%100 == 0)) temp /= 100; - printf("Thermal %d: %d.%d degrees C\n", TT.therm++, temp/10, temp%10); + // some tempertures are in milli-C, some in deci-C + // reputedly some are in deci-K, but I have not seen them + if ((temp>=1000 || temp<=-1000) && !(temp%100)) temp /= 100; + printf("Thermal %d: %d.%d degrees C\n", therm++, temp/10, temp%10); } close(dfd); } - free(TT.cpath); + free(cpath); return 0; } static int cool_callback(struct dirtree *tree) { - int dfd=5, cur, max; + int dfd = 5, cur, max, cool = 0; + char *cpath; errno = 0; - memset(toybuf, 0, sizeof(toybuf)); + memset(toybuf, 0, 257); if (*tree->name == '.') return 0; if (!tree->parent) return DIRTREE_RECURSE|DIRTREE_SYMFOLLOW; - - if (0 <= (dfd = open((TT.cpath=dirtree_path(tree, &dfd)), O_RDONLY))) { - TT.cpath = strcat(TT.cpath, "/type"); - if (readfile(TT.cpath, toybuf, 256) && !errno) { - toybuf[strlen(toybuf) -1] = 0; - cur=read_int_at(dfd, "cur_state"); - max=read_int_at(dfd, "max_state"); - if (errno) - printf("Cooling %d: %s no state information\n", TT.cool++, toybuf); - else printf("Cooling %d: %s %d of %d\n", TT.cool++, toybuf, cur, max); + if (0<=(dfd = open((cpath = dirtree_path(tree, &dfd)), O_RDONLY))) { + strcat(cpath, "/type"); + if (readfile(cpath, toybuf, 256) && !errno) { + chomp(toybuf); + cur = read_int_at(dfd, "cur_state"); + max = read_int_at(dfd, "max_state"); + printf("Cooling %d: %s ", cool++, toybuf); + if (errno) printf("no state information\n"); + else printf("%d of %d\n", cur, max); } close(dfd); } - free(TT.cpath); + free(cpath); + return 0; } void acpi_main(void) { - if (toys.optflags & FLAG_V) toys.optflags = FLAG_a|FLAG_b|FLAG_c|FLAG_t; + if (FLAG(V)) toys.optflags = FLAG_a|FLAG_b|FLAG_c|FLAG_t; if (!toys.optflags) toys.optflags = FLAG_b; - if (toys.optflags & (FLAG_a|FLAG_b)) - dirtree_read("/sys/class/power_supply", acpi_callback); - if (toys.optflags & FLAG_t) dirtree_read("/sys/class", temp_callback); - if (toys.optflags & FLAG_c) dirtree_read("/sys/class/thermal", cool_callback); + if (FLAG(a)|FLAG(b)) dirtree_read("/sys/class/power_supply", acpi_callback); + if (FLAG(t)) dirtree_read("/sys/class", temp_callback); + if (FLAG(c)) dirtree_read("/sys/class/thermal", cool_callback); } |