machine: fix index

This commit is contained in:
Alastair Poole 2020-06-18 12:45:06 +01:00
parent ef961b275a
commit d5c10b939d
1 changed files with 7 additions and 6 deletions

View File

@ -797,7 +797,7 @@ _power_battery_count_get(power_t *power)
char *type; char *type;
char path[PATH_MAX]; char path[PATH_MAX];
struct dirent **names; struct dirent **names;
int i, n; int i, n, id;
n = scandir("/sys/class/power_supply", &names, 0, alphasort); n = scandir("/sys/class/power_supply", &names, 0, alphasort);
if (n < 0) return power->battery_count; if (n < 0) return power->battery_count;
@ -812,11 +812,12 @@ _power_battery_count_get(power_t *power)
{ {
if (!strncmp(type, "Battery", 7)) if (!strncmp(type, "Battery", 7))
{ {
power->batteries = realloc(power->batteries, 1 + id = power->battery_count;
power->battery_count * sizeof(bat_t **)); power->batteries = realloc(power->batteries, (1 +
power->batteries[i] = calloc(1, sizeof(bat_t)); power->battery_count) * sizeof(bat_t **));
power->batteries[i]->name = strdup(names[i]->d_name); power->batteries[id] = calloc(1, sizeof(bat_t));
power->batteries[i]->present = true; power->batteries[id]->name = strdup(names[i]->d_name);
power->batteries[id]->present = true;
power->battery_count++; power->battery_count++;
} }
free(type); free(type);