More cleanup, 'temp' and 'uptime' use fileRead().
[sysstatus.git] / statuses / uptime.c
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include "uptime.h"
6 #include "../config.h"
7
8 void status_uptime()
9 {
10         char stline[16];
11         int stfile;
12         ssize_t stlen;
13         int i;
14         int upts, uptm, upth, uptd;
15
16         fputs(" ^fg(#AAAAAA)up: ", stdout);
17
18         stlen = fileRead(stline, sizeof(stline), "/proc/uptime");
19         if (stlen < 0)
20         {
21                 fputs(" ^fg(red)ERROR ", stdout);
22                 return;
23         }
24
25         // Cut first element
26         for(i = 0; i < stlen; i++)
27         {
28                 if (stline[i] == ' ')
29                 {
30                         stline[i] = '\0';
31                         break;
32                 }
33         }
34
35         // Split time into days, hours, mins, secs
36         upts = atoi(stline);
37         uptd = upts / (24 * 60 * 60);
38         upts -= uptd * (24 * 60 * 60);
39         upth = upts / (60 * 60);
40         upts -= upth * (60 * 60);
41         uptm = upts / (60);
42         upts -= uptm * (60);
43
44         if (uptd > 0)
45                 printf("%dd ", uptd);
46
47         printf("%d:%.2d"
48
49                 #ifdef SHOW_SECONDS
50                 ":%.2d"
51                 #endif
52
53                 " "
54                 ,upth
55                 ,uptm
56
57                 #ifdef SHOW_SECONDS
58                 ,upts
59                 #endif
60                 );
61 }