netstar pushed a commit to branch master.

http://git.enlightenment.org/apps/evisum.git/commit/?id=09e82496897191882c9d49de9fe77c230607d52e

commit 09e82496897191882c9d49de9fe77c230607d52e
Author: Alastair Poole <nets...@gmail.com>
Date:   Sat Jul 11 13:09:38 2020 +0100

    machine: Explain use of machine and tidy Linuxisms
    
    The functions machine.h provides are deliberately using standard
    functions from a base install of the operating system. This IS
    intentional. I'd like to be able to modify and reuse this anywhere
    without having to untangle additional dependencies from machine to
    machine.
---
 src/bin/system/machine.c             | 13 +++----
 src/bin/system/machine.h             | 27 +++++++++-----
 src/bin/system/machine/machine.bogox | 17 +++++++++
 src/bin/system/machine/sensors.bogox | 72 ++++++++++++------------------------
 4 files changed, 62 insertions(+), 67 deletions(-)

diff --git a/src/bin/system/machine.c b/src/bin/system/machine.c
index 9249c45..1877337 100644
--- a/src/bin/system/machine.c
+++ b/src/bin/system/machine.c
@@ -13,6 +13,7 @@
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
+
 #define _DEFAULT_SOURCE
 #include <stdio.h>
 #include <stdlib.h>
@@ -76,7 +77,7 @@ _network_transfer_get_thread_cb(void *arg)
 
    system_network_transfer_get(usage);
 
-   return (void *)0;
+   return NULL;
 }
 
 void
@@ -86,13 +87,10 @@ system_info_all_free(Sys_Info *info)
    int i;
 
    for (i = 0; i < info->cpu_count; i++)
-     {
-        free(info->cores[i]);
-     }
+     free(info->cores[i]);
    free(info->cores);
 
-   for (i = 0; i < info->sensor_count; i++)
-     {
+   for (i = 0; i < info->sensor_count; i++) {
         snsr = info->sensors[i];
         if (snsr->name)
           free(snsr->name);
@@ -101,8 +99,7 @@ system_info_all_free(Sys_Info *info)
    if (info->sensors)
      free(info->sensors);
 
-   for (i = 0; i < info->power.battery_count; i++)
-     {
+   for (i = 0; i < info->power.battery_count; i++) {
         if (info->power.batteries[i]->name)
           free(info->power.batteries[i]->name);
 #if defined(__OpenBSD__)
diff --git a/src/bin/system/machine.h b/src/bin/system/machine.h
index 96728df..5b1d6a6 100644
--- a/src/bin/system/machine.h
+++ b/src/bin/system/machine.h
@@ -1,6 +1,12 @@
 #ifndef __MACHINE_H__
 #define __MACHINE_H__
 
+/* All functions and data types implementing these APIs have no additional
+ * system dependencies deliberately.
+ *
+ * See machine.c and the files includes in machine/ sub directory.
+ */
+
 #include <stdint.h>
 #include <stdbool.h>
 
@@ -13,15 +19,15 @@ typedef struct
 
 typedef struct
 {
-   unsigned long long total;
-   unsigned long long used;
-   unsigned long long cached;
-   unsigned long long buffered;
-   unsigned long long shared;
-   unsigned long long swap_total;
-   unsigned long long swap_used;
-
-   unsigned long long zfs_arc_used;
+   uint64_t total;
+   uint64_t used;
+   uint64_t cached;
+   uint64_t buffered;
+   uint64_t shared;
+   uint64_t swap_total;
+   uint64_t swap_used;
+
+   uint64_t zfs_arc_used;
 } meminfo_t;
 
 typedef struct
@@ -63,11 +69,12 @@ struct Sys_Info
 {
    int           cpu_count;
    cpu_core_t  **cores;
+
    meminfo_t     memory;
    power_t       power;
 
    int           sensor_count;
-   sensor_t     **sensors;
+   sensor_t    **sensors;
 
    network_t     network_usage;
 };
diff --git a/src/bin/system/machine/machine.bogox 
b/src/bin/system/machine/machine.bogox
index 3f690bc..595b9d9 100644
--- a/src/bin/system/machine/machine.bogox
+++ b/src/bin/system/machine/machine.bogox
@@ -15,6 +15,9 @@
  */
 
 #if defined(__linux__)
+
+#include <stdarg.h>
+
 char *
 file_contents(const char *path)
 {
@@ -49,6 +52,20 @@ file_contents(const char *path)
    return buf;
 }
 
+char *
+strsli_printf(const char *fmt, ...)
+{
+   static char buf[4096];
+   va_list ap;
+
+   buf[0] = 0x00;
+   va_start(ap, fmt);
+   vsnprintf(buf, sizeof(buf), fmt, ap);
+   va_end(ap);
+
+   return buf;
+}
+
 #endif
 
 #if defined(__FreeBSD__) || defined(__DragonFly__)
diff --git a/src/bin/system/machine/sensors.bogox 
b/src/bin/system/machine/sensors.bogox
index d326bd4..0e5cba0 100644
--- a/src/bin/system/machine/sensors.bogox
+++ b/src/bin/system/machine/sensors.bogox
@@ -71,7 +71,6 @@ system_sensors_thermal_get(int *sensor_count)
 #elif defined(__linux__)
    sensor_t *sensor;
    char *type, *value;
-   char path[PATH_MAX];
    struct dirent **names;
    int i, n;
 
@@ -84,10 +83,8 @@ system_sensors_thermal_get(int *sensor_count)
              free(names[i]);
              continue;
           }
-        snprintf(path, sizeof(path), "/sys/class/thermal/%s/type",
-                 names[i]->d_name);
 
-        type = file_contents(path);
+        type = file_contents(strsli_printf("/sys/class/thermal/%s/type", 
names[i]->d_name));
         if (type)
           {
              sensors =
@@ -95,11 +92,9 @@ system_sensors_thermal_get(int *sensor_count)
              sensors[(*sensor_count)++] =
                  sensor = calloc(1, sizeof(sensor_t));
 
-             sensor->name = strdup(type);
-             snprintf(path, sizeof(path), "/sys/class/thermal/%s/temp",
-                      names[i]->d_name);
+             sensor->name = type;
 
-             value = file_contents(path);
+             value = file_contents(strsli_printf("/sys/class/thermal/%s/temp", 
names[i]->d_name));
              if (!value)
                sensor->invalid = true;
              else
@@ -107,7 +102,6 @@ system_sensors_thermal_get(int *sensor_count)
                   sensor->value = (float)atoi(value) / 1000.0;
                   free(value);
                }
-             free(type);
           }
 
         free(names[i]);
@@ -180,7 +174,6 @@ _power_battery_count_get(power_t *power)
      }
 #elif defined(__linux__)
    char *type;
-   char path[PATH_MAX];
    struct dirent **names;
    int i, n, id;
 
@@ -188,10 +181,7 @@ _power_battery_count_get(power_t *power)
    if (n < 0) return power->battery_count;
 
    for (i = 0; i < n; i++) {
-        snprintf(path, sizeof(path), "/sys/class/power_supply/%s/type",
-                 names[i]->d_name);
-
-        type = file_contents(path);
+        type = file_contents(strsli_printf("/sys/class/power_supply/%s/type", 
names[i]->d_name));
         if (type)
           {
              if (!strncmp(type, "Battery", 7))
@@ -287,24 +277,20 @@ _battery_state_get(power_t *power)
    close(fd);
 
 #elif defined(__linux__)
-   char path[PATH_MAX];
+   const char *path;
    struct dirent *dh;
    struct stat st;
    DIR *dir;
    char *model, *vendor;
    char *buf, *naming = NULL;
-   int i = 0;
-   unsigned long charge_full = 0;
-   unsigned long charge_current = 0;
 
-   for (i = 0; i < power->battery_count; i++) {
+   for (int i = 0; i < power->battery_count; i++) {
         naming = NULL;
-        snprintf(path, sizeof(path), "/sys/class/power_supply/%s",
-                 power->batteries[i]->name);
 
-        if (stat(path, &st) < 0) continue;
-        if (S_ISLNK(st.st_mode)) continue;
-        if (!S_ISDIR(st.st_mode)) continue;
+        path = strsli_printf("/sys/class/power_suppy/%s", 
power->batteries[i]->name);
+
+        if (((stat(path, &st) < 0)) || (S_ISLNK(st.st_mode)) || 
(!S_ISDIR(st.st_mode)))
+          continue;
 
         dir = opendir(path);
         if (!dir) return;
@@ -324,34 +310,28 @@ _battery_state_get(power_t *power)
         if (!naming)
           continue;
 
-        snprintf(path, sizeof(path), "/sys/class/power_supply/%s/%s_full",
-                 power->batteries[i]->name, naming);
-        buf = file_contents(path);
+        buf = file_contents(strsli_printf("/sys/class/power_supply/%s/%s_full",
+                            power->batteries[i]->name, naming));
         if (buf)
           {
-             charge_full = atol(buf);
+             power->batteries[i]->charge_full = atol(buf);
              free(buf);
           }
-        snprintf(path, sizeof(path), "/sys/class/power_supply/%s/%s_now",
-                 power->batteries[i]->name, naming);
-        buf = file_contents(path);
+        buf = file_contents(strsli_printf("/sys/class/power_supply/%s/%s_now",
+                            power->batteries[i]->name, naming));
         if (buf)
           {
-             charge_current = atol(buf);
+             power->batteries[i]->charge_current = atol(buf);
              free(buf);
           }
 
-        snprintf(path, sizeof(path), "/sys/class/power_supply/%s/manufacturer",
-                 power->batteries[i]->name);
-        vendor = file_contents(path);
-
-        snprintf(path, sizeof(path), "/sys/class/power_supply/%s/model_name",
-                 power->batteries[i]->name);
-        model = file_contents(path);
+        vendor = 
file_contents(strsli_printf("/sys/class/power_supply/%s/manufacturer",
+                               power->batteries[i]->name));
+        model = 
file_contents(strsli_printf("/sys/class/power_supply/%s/model_name",
+                              power->batteries[i]->name));
 
         if (vendor && vendor[0] && model && model[0])
           {
-             char name[256];
              int len;
 
              len = strlen(vendor) - 1;
@@ -363,13 +343,8 @@ _battery_state_get(power_t *power)
                model[len] = '\0';
 
              free(power->batteries[i]->name);;
-             snprintf(name, sizeof(name), "%s %s", vendor, model);
-             power->batteries[i]->name = strdup(name);
+             power->batteries[i]->name = strdup(strsli_printf("%s %s", vendor, 
model));
           }
-
-        power->batteries[i]->charge_full = charge_full;
-        power->batteries[i]->charge_current = charge_current;
-
         if (model)
           free(model);
         if (vendor)
@@ -417,13 +392,12 @@ system_power_state_get(power_t *power)
      }
 #endif
 
-  _battery_state_get(power);
+   _battery_state_get(power);
 
    for (i = 0; i < power->battery_count; i++) {
-        double percent = 100 *
+        power->batteries[i]->percent = 100 *
            (power->batteries[i]->charge_current /
                                     power->batteries[i]->charge_full);
-        power->batteries[i]->percent = percent;
      }
 }
 

-- 


Reply via email to