From cd0bc01101a02ad10132cee9fa5d49e02f70d5a9 Mon Sep 17 00:00:00 2001 From: Alastair Poole Date: Thu, 18 Jun 2020 13:11:56 +0100 Subject: [PATCH] linux: read sensors (alphasort). Otherwise it's a sh*tshow --- src/bin/system/machine.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/bin/system/machine.c b/src/bin/system/machine.c index 157a90a..f0beffe 100644 --- a/src/bin/system/machine.c +++ b/src/bin/system/machine.c @@ -686,20 +686,19 @@ _sensors_thermal_get(Sys_Info *info) } #elif defined(__linux__) sensor_t *sensor; - struct dirent *dh; - DIR *dir; - char *value, *type; + char *type, *value; char path[PATH_MAX]; + struct dirent **names; + int i, n; - dir = opendir("/sys/class/thermal"); - if (!dir) return; + n = scandir("/sys/class/thermal", &names, 0, alphasort); + if (n < 0) return; - while ((dh = readdir(dir)) != NULL) + for (i = 0; i < n; i++) { - if (strncmp(dh->d_name, "thermal_zone", 12)) - continue; + snprintf(path, sizeof(path), "/sys/class/thermal/%s/type", + names[i]->d_name); - snprintf(path, sizeof(path), "/sys/class/thermal/%s/type", dh->d_name); type = file_contents(path); if (type) { @@ -708,9 +707,9 @@ _sensors_thermal_get(Sys_Info *info) sensors[info->sensor_count++] = sensor = calloc(1, sizeof(sensor_t)); - sensor->name = strdup(dh->d_name); + sensor->name = strdup(names[i]->d_name); snprintf(path, sizeof(path), "/sys/class/thermal/%s/temp", - dh->d_name); + names[i]->d_name); value = file_contents(path); if (!value) @@ -722,9 +721,11 @@ _sensors_thermal_get(Sys_Info *info) } free(type); } + + free(names[i]); } - closedir(dir); + free(names); #elif defined(__MacOS__) #endif info->sensors = sensors;