Skip to content

Instantly share code, notes, and snippets.

@allisio
Last active May 9, 2020 00:15
Show Gist options
  • Save allisio/1e850b93c81150124c2634716fbc4815 to your computer and use it in GitHub Desktop.
Save allisio/1e850b93c81150124c2634716fbc4815 to your computer and use it in GitHub Desktop.
static char *get_battery() {
int capacity;
FILE *capacity_file, *status_file;
char bat_status[12] = "Unknown";
if((capacity_file = fopen(BATTERY_DIRECTORY "/capacity", "r")) == NULL) {
status = ENOENT;
halt_and_catch_fire("unable to get battery information");
}
fscanf(capacity_file, "%d", &capacity);
fclose(capacity_file);
if((status_file = fopen(BATTERY_DIRECTORY "/status", "r")) != NULL) {
fscanf(status_file, "%s", bat_status);
fclose(status_file);
}
char *battery = malloc(BUF_SIZE);
snprintf(battery, BUF_SIZE, "%d%% [%s]", capacity, bat_status);
return battery;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment