summaryrefslogtreecommitdiffstats
path: root/UptimeMeter.c
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2014-11-27 19:34:47 -0200
committerHisham Muhammad <hisham@gobolinux.org>2014-11-27 19:34:47 -0200
commit2dcdd87658cdc38b2df5535cce85976e963fbd30 (patch)
tree78e8bb3cfe410e06b7728afa853697b742603186 /UptimeMeter.c
parent357e7a3243a0e6fbc937354a03639f0cc34b966c (diff)
Move UptimeMeter back into cross-platform code.
We'll just isolate the platform-dependent bit.
Diffstat (limited to 'UptimeMeter.c')
-rw-r--r--UptimeMeter.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/UptimeMeter.c b/UptimeMeter.c
new file mode 100644
index 00000000..9e288171
--- /dev/null
+++ b/UptimeMeter.c
@@ -0,0 +1,64 @@
+/*
+htop - UptimeMeter.c
+(C) 2004-2011 Hisham H. Muhammad
+Released under the GNU GPL, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#include "UptimeMeter.h"
+
+#include "ProcessList.h"
+#include "CRT.h"
+
+#include <math.h>
+
+/*{
+#include "Meter.h"
+}*/
+
+int UptimeMeter_attributes[] = {
+ UPTIME
+};
+
+static void UptimeMeter_setValues(Meter* this, char* buffer, int len) {
+ double uptime = 0;
+ FILE* fd = fopen(PROCDIR "/uptime", "r");
+ if (fd) {
+ fscanf(fd, "%64lf", &uptime);
+ fclose(fd);
+ }
+ int totalseconds = (int) ceil(uptime);
+ int seconds = totalseconds % 60;
+ int minutes = (totalseconds/60) % 60;
+ int hours = (totalseconds/3600) % 24;
+ int days = (totalseconds/86400);
+ this->values[0] = days;
+ if (days > this->total) {
+ this->total = days;
+ }
+ char daysbuf[15];
+ if (days > 100) {
+ sprintf(daysbuf, "%d days(!), ", days);
+ } else if (days > 1) {
+ sprintf(daysbuf, "%d days, ", days);
+ } else if (days == 1) {
+ sprintf(daysbuf, "1 day, ");
+ } else {
+ daysbuf[0] = '\0';
+ }
+ snprintf(buffer, len, "%s%02d:%02d:%02d", daysbuf, hours, minutes, seconds);
+}
+
+MeterClass UptimeMeter_class = {
+ .super = {
+ .extends = Class(Meter),
+ .delete = Meter_delete
+ },
+ .setValues = UptimeMeter_setValues,
+ .defaultMode = TEXT_METERMODE,
+ .total = 100.0,
+ .attributes = UptimeMeter_attributes,
+ .name = "Uptime",
+ .uiName = "Uptime",
+ .caption = "Uptime: "
+};

© 2014-2024 Faster IT GmbH | imprint | privacy policy