Xinqi Bao's Git

Update README todo
[slstatus.git] / components / uptime.c
1 /* See LICENSE file for copyright and license details. */
2 #include <errno.h>
3 #include <stdio.h>
4 #include <string.h>
5
6 #include "../util.h"
7
8 #if defined(__linux__)
9 #include <sys/sysinfo.h>
10
11 const char *
12 uptime(void)
13 {
14 int h;
15 int m;
16 int uptime = 0;
17 struct sysinfo info;
18
19 sysinfo(&info);
20 uptime = info.uptime;
21
22 h = uptime / 3600;
23 m = (uptime - h * 3600) / 60;
24
25 return bprintf("%dh %dm", h, m);
26 }
27 #elif defined(__OpenBSD__)
28 #include <sys/sysctl.h>
29 #include <sys/time.h>
30
31 const char *
32 uptime(void)
33 {
34 int h;
35 int m;
36 int uptime = 0;
37
38 int mib[2];
39 size_t size;
40 time_t now;
41 struct timeval boottime;
42
43 time(&now);
44
45 mib[0] = CTL_KERN;
46 mib[1] = KERN_BOOTTIME;
47
48 size = sizeof(boottime);
49
50 if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
51 uptime = now - boottime.tv_sec;
52 else {
53 fprintf(stderr, "sysctl 'KERN_BOOTTIME': %s\n", strerror(errno));
54 return NULL;
55 }
56
57 h = uptime / 3600;
58 m = (uptime - h * 3600) / 60;
59
60 return bprintf("%dh %dm", h, m);
61 }
62 #endif