On Mon, 2012-05-21 at 14:44 +0300, Eugene V. Lyubimkin wrote: 
> Ack, thanks, I forwarded your report upstream.
> 
less sucky version of patch 1

-- 
-Shawn Landden
>From 3a07e3cc9c1d85e42b883abc64a43a5eaccf4aa2 Mon Sep 17 00:00:00 2001
From: Shawn Landden <[email protected]>
Date: Sun, 20 May 2012 13:52:27 -0700
Subject: [PATCH 1/6] record btime from /proc/stat on bootup as semi-global

---
 Process.c |    2 ++
 htop.c    |   19 +++++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/Process.c b/Process.c
index 1ae96e1..cc8305c 100644
--- a/Process.c
+++ b/Process.c
@@ -177,6 +177,8 @@ char* PROCESS_CLASS = "Process";
 #define PROCESS_CLASS NULL
 #endif
 
+extern long long int btime;  /* boot time, gets set in htop.c:record_btime() */
+
 const char *Process_fieldNames[] = {
    "", "PID", "Command", "STATE", "PPID", "PGRP", "SESSION",
    "TTY_NR", "TPGID", "FLAGS", "MINFLT", "CMINFLT", "MAJFLT", "CMAJFLT",
diff --git a/htop.c b/htop.c
index b9092d5..1647cca 100644
--- a/htop.c
+++ b/htop.c
@@ -41,6 +41,8 @@ in the source distribution for its full text.
 
 #define COPYRIGHT "(C) 2004-2011 Hisham Muhammad"
 
+long long int btime; /* boot time, gets set in record_btime() */
+
 static void printVersionFlag() {
    fputs("htop " VERSION " - " COPYRIGHT "\n"
          "Released under the GNU GPL.\n\n",
@@ -262,6 +264,21 @@ static void IncBuffer_reset(IncBuffer* inc) {
    inc->buffer[0] = 0;
 }
 
+static void record_btime() {
+  FILE* file = fopen(PROCSTATFILE, "r");
+  assert(file != NULL);
+  do {
+    char buffer[4096];
+    if (fgets(buffer, 4096, file) == NULL)
+      assert(false); /* got to end of file without getting btime */
+    if (strncmp(buffer, "btime ", 6) == 0) {
+      sscanf(buffer, "btime %lld\n", &btime);
+      break;
+    }
+  } while(true);
+  return;
+}
+
 int main(int argc, char** argv) {
 
    int delay = -1;
@@ -350,6 +367,8 @@ int main(int argc, char** argv) {
    bool doRefresh = true;
    bool doRecalculate = false;
    Settings* settings;
+
+   record_btime();
    
    Panel* killPanel = NULL;
    
-- 
1.7.9.5

Reply via email to