char * load_str(struct load_context *ctx) { double la[3]; char la1[6], la5[6], la15[6]; assert(ctx != NULL); if (bsd_getloadavg(la, 3) == -1) errx(EXIT_FAILURE, "bsd_getloadavg"); loadstr(la[0], sizeof(la1), la1); loadstr(la[1], sizeof(la5), la5); loadstr(la[2], sizeof(la15), la15); tools_catitems(ctx->load_str, sizeof(ctx->load_str), la1, " ", la5, " ", la15, NULL); return (ctx->load_str); }
char * battery_str(struct battery_context *ctx) { union acpi_battery_ioctl_arg battio; const char *state; char cap[4]; assert(ctx != NULL); battio.unit = ACPI_BATTERY_ALL_UNITS; if (ioctl(ctx->fd, ACPIIO_BATT_GET_BATTINFO, &battio) == -1) err(EXIT_FAILURE, "ioctl(ACPIIO_BATT_GET_BATTINFO)"); if (battio.battinfo.cap == -1) { strncpy(ctx->battery_str, NOTAVAILABLE, sizeof(ctx->battery_str) - 1); ctx->battery_str[sizeof(ctx->battery_str) - 1] = '\0'; goto exit; } if (battio.battinfo.state == 0) state = "="; else if (battio.battinfo.state & ACPI_BATT_STAT_CRITICAL) state = "!"; else if (battio.battinfo.state & ACPI_BATT_STAT_DISCHARG) state = "-"; else if (battio.battinfo.state & ACPI_BATT_STAT_CHARGING) state = "+"; else state = "?"; assert(battio.battinfo.cap >= 0 && battio.battinfo.cap <= 100 && sizeof(cap) > 3); sprintf(cap, "%d", battio.battinfo.cap); tools_catitems(ctx->battery_str, sizeof(ctx->battery_str), cap, "% [", state, "]", NULL); exit: return (ctx->battery_str); }