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