UptimeMeter.c 1.36 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
/*
htop
Hisham Muhammad's avatar
Hisham Muhammad committed
3
(C) 2004-2011 Hisham H. Muhammad
Hisham Muhammad's avatar
Hisham Muhammad committed
4
5
6
7
8
9
10
11
12
13
14
15
16
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#include "UptimeMeter.h"
#include "Meter.h"

#include "ProcessList.h"

#include "CRT.h"

#include "debug.h"

17
18
19
int UptimeMeter_attributes[] = {
   UPTIME
};
20

21
static void UptimeMeter_setValues(Meter* this, char* buffer, int len) {
Hisham Muhammad's avatar
Hisham Muhammad committed
22
   double uptime = 0;
Hisham Muhammad's avatar
Hisham Muhammad committed
23
   FILE* fd = fopen(PROCDIR "/uptime", "r");
Hisham Muhammad's avatar
Hisham Muhammad committed
24
25
26
27
   if (fd) {
      fscanf(fd, "%lf", &uptime);
      fclose(fd);
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
28
   int totalseconds = (int) ceil(uptime);
29
   int seconds = totalseconds % 60;
Hisham Muhammad's avatar
Hisham Muhammad committed
30
31
32
   int minutes = (totalseconds/60) % 60;
   int hours = (totalseconds/3600) % 24;
   int days = (totalseconds/86400);
33
34
35
   this->values[0] = days;
   if (days > this->total) {
      this->total = days;
Hisham Muhammad's avatar
Hisham Muhammad committed
36
   }
37
   char daysbuf[15];
38
39
40
41
42
43
44
45
   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';
Hisham Muhammad's avatar
Hisham Muhammad committed
46
   }
47
   snprintf(buffer, len, "%s%02d:%02d:%02d", daysbuf, hours, minutes, seconds);
Hisham Muhammad's avatar
Hisham Muhammad committed
48
}
49
50
51
52
53
54
55
56
57
58
59
60

MeterType UptimeMeter = {
   .setValues = UptimeMeter_setValues, 
   .display = NULL,
   .mode = TEXT_METERMODE,
   .items = 1,
   .total = 100.0,
   .attributes = UptimeMeter_attributes,
   .name = "Uptime",
   .uiName = "Uptime",
   .caption = "Uptime: "
};