memusage.c: Read 64-bit integers
[sysstatus.git] / src / status / memusage.c
index 6a6cb21441923529851da25a67df433e11e3511a..d8a9de5f96906316b010f0edd8a00049e8032bca 100644 (file)
@@ -1,36 +1,46 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
-#include "status/memusage.h"
+#include "common.h"
 #include "tools.h"
 
 
-void status_memusage()
+void status_memusage(GlobalData *g)
 {
+  StatusItem s;
+  char text[16] = { 0 };
+
   char *stline = NULL;
   size_t stlen;
   FILE *stfile;
 
-  int memtotal = 0;
-  int memfree = 0;
-  int memused;
-  int membuffers = 0;
-  int memcached = 0;
+  long long memtotal = 0;
+  long long memfree = 0;
+  long long memused;
+  long long membuffers = 0;
+  long long memcached = 0;
+
 
+  statusitem_init(&s);
+  s.text = text;
 
   stfile = fopen("/proc/meminfo", "r");
   if (stfile != NULL) {
     stlen = getline(&stline, &stlen, stfile);
-    memtotal = atoi(&stline[17]);
+    memtotal = atoll(&stline[16]);
 
     stlen = getline(&stline, &stlen, stfile);
-    memfree = atoi(&stline[17]);
+    memfree = atoll(&stline[16]);
 
     stlen = getline(&stline, &stlen, stfile);
-    membuffers = atoi(&stline[17]);
+    if (stlen > 13 && !memcmp(stline, "MemAvailable:", 13)) {
+      stlen = getline(&stline, &stlen, stfile);
+    }
+    membuffers = atoll(&stline[16]);
 
     stlen = getline(&stline, &stlen, stfile);
-    memcached = atoi(&stline[17]);
+    memcached = atoll(&stline[16]);
     free(stline);
 
     fclose(stfile);
@@ -41,11 +51,13 @@ void status_memusage()
 
     /* Change color based on % of RAM used */
     if ((float)memused / (float)memtotal < 0.85) {
-      fputs("^fg(green)", stdout);
+      s.color = "#22FF22";  // green
     } else {
-      fputs("^fg(red)", stdout);
+      s.color = "#FF0000";  // red
     }
 
-    printf(" Mem: %d M ", memused);
+    snprintf(text, sizeof(text), "Mem: %lld M", memused);
+
+    line_append_item(g, &s);
   }
 }