File: uptime.c

package info (click to toggle)
jackd2 1.9.17~dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,524 kB
  • sloc: cpp: 48,028; ansic: 30,427; python: 12,448; sh: 270; makefile: 60
file content (17 lines) | stat: -rw-r--r-- 375 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <time.h>
#include <errno.h>
#include <sys/sysctl.h>

long uptime(void)
{
    struct timeval boottime;
    size_t len = sizeof(boottime);
    int mib[2] = { CTL_KERN, KERN_BOOTTIME };
    if (sysctl(mib, 2, &boottime, &len, NULL, 0) < 0)
    {
        return -1L;
    }
    time_t bsec = boottime.tv_sec, csec = time(NULL);

    return (long) difftime(csec, bsec);
}