Xinqi Bao's Git

wifi: fixed disconnected wifi status on openbsd
[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 <sys/sysctl.h>
33 #include <sys/time.h>
34
35 const char *
36 uptime(void)
37 {
38 int mib[2], uptime;
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) < 0) {
51 warn("sysctl 'KERN_BOOTTIME':");
52 return NULL;
53 }
54
55 uptime = now - boottime.tv_sec;
56
57 return format(uptime);
58 }
59 #endif