summaryrefslogtreecommitdiff
path: root/statuses/uptime.c
blob: d512cdb84512f143a5fc2a3e0a4d91385e5e54c4 (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
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include "uptime.h"
#include "../config.h"

void status_uptime()
{
	char stline[16];
	int stfile;
	ssize_t stlen;
	int i;
	int upts, uptm, upth, uptd;

	fputs(" ^fg(#AAAAAA)up: ", stdout);

	stlen = fileRead(stline, sizeof(stline), "/proc/uptime");
	if (stlen < 0)
	{
		fputs(" ^fg(red)ERROR ", stdout);
		return;
	}

	// 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);

	if (uptd > 0)
		printf("%dd ", uptd);

	printf("%d:%.2d"

		#ifdef SHOW_SECONDS
		":%.2d"
		#endif

		" "
		,upth
		,uptm

		#ifdef SHOW_SECONDS
		,upts
		#endif
		);
}