Xinqi Bao's Git

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