#include #include #include #include #include #include #define PATH_APM "/dev/apm" #define TIME_FMT "%d:%02d:%02d" #define TIME_LEN 10 static void _time2str(u_int t, char *s) { snprintf(s, TIME_LEN, TIME_FMT, t / (60 * 60), (t % (60 * 60)) / 60, t % 60); } int main(int ac, char *av[]) { int afd; struct apm_info ai; char ts[TIME_LEN]; if ((afd = open(PATH_APM, O_RDONLY)) == -1) err(EX_NOINPUT, "Cannot open apm device %s", PATH_APM); if (ioctl(afd, APMIO_GETINFO, &ai) == -1) err(1, "Cannot retrieve APM Information"); close(afd); if (!ai.ai_acline) { _time2str(ai.ai_batt_time, ts); printf("%d%% (%s) Battery Life\n", ai.ai_batt_life, ts); } else printf("Machine is on AC Line power\n"); return 0; }