char *get_apm_battery_life() { int fd; u_int batt_life; struct apm_power_info info; char *out; out = (char *) calloc(16, sizeof(char)); fd = open(APMDEV, O_RDONLY); if (fd < 0) { strncpy(out, "ERR", 16); return out; } if (apm_getinfo(fd, &info) != 0) { close(fd); strncpy(out, "ERR", 16); return out; } close(fd); batt_life = info.battery_life; if (batt_life <= 100) { snprintf(out, 16, "%d%%", batt_life); return out; } else { strncpy(out, "ERR", 16); } return out; }
char *get_apm_battery_time() { int fd; int batt_time; int h, m; struct apm_power_info info; char *out; out = (char *) calloc(16, sizeof(char)); fd = open(APMDEV, O_RDONLY); if (fd < 0) { strncpy(out, "ERR", 16); return out; } if (apm_getinfo(fd, &info) != 0) { close(fd); strncpy(out, "ERR", 16); return out; } close(fd); batt_time = info.minutes_left; if (batt_time == -1) { strncpy(out, "unknown", 16); } else { h = batt_time / 60; m = batt_time % 60; snprintf(out, 16, "%2d:%02d", h, m); } return out; }
char *get_apm_adapter() { int fd; struct apm_power_info info; char *out; out = (char *) calloc(16, sizeof(char)); fd = open(APMDEV, O_RDONLY); if (fd < 0) { strncpy(out, "ERR", 16); return out; } if (apm_getinfo(fd, &info) != 0) { close(fd); strncpy(out, "ERR", 16); return out; } close(fd); switch (info.ac_state) { case APM_AC_OFF: strncpy(out, "off-line", 16); return out; break; case APM_AC_ON: if (info.battery_state == APM_BATT_CHARGING) { strncpy(out, "charging", 16); return out; } else { strncpy(out, "on-line", 16); return out; } break; default: strncpy(out, "unknown", 16); return out; break; } }
int main(int argc, char *argv[]) { int c, fd; int dosleep = 0, all_info = 1, apm_status = 0, batt_status = 0; int display = -1, batt_life = 0, ac_status = 0, standby = 0; int batt_time = 0, delta = 0, enable = -1, haltcpu = -1; int bioscall_available = 0; size_t cmos_wall_len = sizeof(cmos_wall); if (sysctlbyname("machdep.wall_cmos_clock", &cmos_wall, &cmos_wall_len, NULL, 0) == -1) err(1, "sysctlbyname(machdep.wall_cmos_clock)"); while ((c = getopt(argc, argv, "abe:h:lRr:stzd:Z")) != -1) { switch (c) { case 'a': ac_status = 1; all_info = 0; break; case 'b': batt_status = 1; all_info = 0; break; case 'd': display = is_true(optarg); all_info = 0; break; case 'l': batt_life = 1; all_info = 0; break; case 'R': delta = -1; break; case 'r': delta = atoi(optarg); break; case 's': apm_status = 1; all_info = 0; break; case 'e': enable = is_true(optarg); all_info = 0; break; case 'h': haltcpu = is_true(optarg); all_info = 0; break; case 't': batt_time = 1; all_info = 0; break; case 'z': dosleep = 1; all_info = 0; break; case 'Z': standby = 1; all_info = 0; break; case '?': default: usage(); } argc -= optind; argv += optind; } if (haltcpu != -1 || enable != -1 || display != -1 || delta || dosleep || standby) { fd = open(APMDEV, O_RDWR); bioscall_available = 1; } else if ((fd = open(APMDEV, O_RDWR)) >= 0) bioscall_available = 1; else fd = open(APMDEV, O_RDONLY); if (fd == -1) err(1, "can't open %s", APMDEV); if (enable != -1) apm_enable(fd, enable); if (haltcpu != -1) apm_haltcpu(fd, haltcpu); if (delta) apm_set_timer(fd, delta); if (dosleep) apm_suspend(fd); else if (standby) apm_standby(fd); else if (delta == 0) { struct apm_info info; apm_getinfo(fd, &info); if (all_info) print_all_info(fd, &info, bioscall_available); if (ac_status) printf("%d\n", info.ai_acline); if (batt_status) printf("%d\n", info.ai_batt_stat); if (batt_life) printf("%d\n", info.ai_batt_life); if (apm_status) printf("%d\n", info.ai_status); if (batt_time) printf("%d\n", info.ai_batt_time); if (display != -1) apm_display(fd, display); } close(fd); exit(0); }