Xinqi Bao's Git

uptime: OS split
[slstatus.git] / components / OpenBSD / uptime.c
1 /* See LICENSE file for copyright and license details. */
2 #include <errno.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/sysctl.h>
6 #include <sys/time.h>
7
8 #include "../../util.h"
9
10 const char *
11 uptime(void)
12 {
13 int h;
14 int m;
15 int uptime = 0;
16
17 int mib[2];
18 size_t size;
19 time_t now;
20 struct timeval boottime;
21
22 time(&now);
23
24 mib[0] = CTL_KERN;
25 mib[1] = KERN_BOOTTIME;
26
27 size = sizeof(boottime);
28
29 if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
30 uptime = now - boottime.tv_sec;
31 else {
32 fprintf(stderr, "sysctl 'KERN_BOOTTIME': %s\n", strerror(errno));
33 return NULL;
34 }
35
36 h = uptime / 3600;
37 m = (uptime - h * 3600) / 60;
38
39 return bprintf("%dh %dm", h, m);
40 }