UptimeMeter.c 1.46 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
/*
Hisham Muhammad's avatar
Hisham Muhammad committed
2
htop - UptimeMeter.c
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
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"

Hisham Muhammad's avatar
Hisham Muhammad committed
13
14
15
16
17
18
#include <math.h>

/*{
#include "Meter.h"
}*/

19
20
21
int UptimeMeter_attributes[] = {
   UPTIME
};
22

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

52
53
54
55
56
MeterClass UptimeMeter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete
   },
57
   .setValues = UptimeMeter_setValues, 
58
   .defaultMode = TEXT_METERMODE,
59
60
61
62
63
64
65
   .items = 1,
   .total = 100.0,
   .attributes = UptimeMeter_attributes,
   .name = "Uptime",
   .uiName = "Uptime",
   .caption = "Uptime: "
};