Change named colors to hex values
[sysstatus.git] / src / status / uptime.c
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5
6 #include "common.h"
7 #include "tools.h"
8 #include "config.h"
9
10 void status_uptime(GlobalData *g)
11 {
12   StatusItem s;
13   char text[16] = { 0 };
14
15   char stline[16];
16   ssize_t stlen;
17   int i;
18   int upts, uptm, upth, uptd;
19
20
21   statusitem_init(&s);
22   s.text = text;
23
24   stlen = fileRead(stline, sizeof(stline), "/proc/uptime");
25   if (stlen < 0) {
26     s.color = "#FF0000";  // red
27     s.text = "up: ERROR";
28   } else {
29     unsigned textlen = 0;
30
31     /* Cut first element */
32     for(i = 0; i < stlen; i++) {
33       if (stline[i] == ' ') {
34         stline[i] = '\0';
35         break;
36       }
37     }
38
39     // Split time into days, hours, mins, secs
40     upts = atoi(stline);
41     uptd = upts / (24 * 60 * 60);
42     upts -= uptd * (24 * 60 * 60);
43     upth = upts / (60 * 60);
44     upts -= upth * (60 * 60);
45     uptm = upts / (60);
46     upts -= uptm * (60);
47
48     s.color = "#AAAAAA";
49     textlen = snprintf(text, sizeof(text), "up: ");
50     if (uptd > 0) {
51       textlen += snprintf(&text[textlen], sizeof(text) - textlen, "%dd ", uptd);
52     }
53
54
55     snprintf(&text[textlen], sizeof(text) - textlen,
56               "%d:%.2d"
57               #ifdef SHOW_SECONDS
58               ":%.2d"
59               #endif
60
61               ""
62               ,upth
63               ,uptm
64
65               #ifdef SHOW_SECONDS
66               ,upts
67               #endif
68             );
69
70     line_append_item(g, &s);
71   }
72 }