summaryrefslogtreecommitdiff
path: root/src/status/uptime.c
blob: ef5b2e5d90f1879005ae0d847ccb4926b1c0ba1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

#include "common.h"
#include "tools.h"
#include "config.h"

void status_uptime(GlobalData *g)
{
  StatusItem s;
  char text[16] = { 0 };

  char stline[16];
  ssize_t stlen;
  int i;
  int upts, uptm, upth, uptd;


  statusitem_init(&s);
  s.text = text;

  stlen = fileRead(stline, sizeof(stline), "/proc/uptime");
  if (stlen < 0) {
    s.color = "red";
    s.text = "up: ERROR";
  } else {
    unsigned textlen = 0;

    /* Cut first element */
    for(i = 0; i < stlen; i++) {
      if (stline[i] == ' ') {
        stline[i] = '\0';
        break;
      }
    }

    // Split time into days, hours, mins, secs
    upts = atoi(stline);
    uptd = upts / (24 * 60 * 60);
    upts -= uptd * (24 * 60 * 60);
    upth = upts / (60 * 60);
    upts -= upth * (60 * 60);
    uptm = upts / (60);
    upts -= uptm * (60);

    s.color = "#AAAAAA";
    textlen = snprintf(text, sizeof(text), "up: ");
    if (uptd > 0) {
      textlen += snprintf(&text[textlen], sizeof(text) - textlen, "%dd ", uptd);
    }


    snprintf(&text[textlen], sizeof(text) - textlen,
              "%d:%.2d"
              #ifdef SHOW_SECONDS
              ":%.2d"
              #endif

              ""
              ,upth
              ,uptm

              #ifdef SHOW_SECONDS
              ,upts
              #endif
            );

    line_append_item(g, &s);
  }
}