my little program

Link to this paste: http://bugs.dragonflybsd.org/pastes/2

Added by tuxillo over 2 years ago.
Syntax: C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <time.h>
#include <err.h>
#include <strings.h>

#include <sys/sysctl.h>
#include <sys/time.h>

void
print_uptime(time_t t)
{
        int days;
        int hours;
        int minutes;

        days = t / 86400;
        t %= 86400;
        hours = t / 3600;
        t %= 3600;
        minutes = t / 60;
        t %= 60;

        printf("UP: %d days, %dh %dm %lds\n", days, hours, minutes, t);


}
int
main(void)
{
        struct timeval now, boottime;
        struct timespec now1;
        time_t uptime;
        size_t len;
        int error;

        tzset();
        bzero(&uptime, sizeof(uptime));

        clock_gettime(CLOCK_MONOTONIC, &now1);
        error = gettimeofday(&now, NULL);
        if (error)
                err(1, "gettimeofday");

        len = sizeof(boottime);
        if (sysctlbyname("kern.boottime", &boottime, &len, NULL, 0) == -1)
                err(1, "sysctlbyname");

        uptime = now.tv_sec - boottime.tv_sec;
        printf("clock_gettime: %ld\n", now1.tv_sec);
        printf("gettimeofday: %ld\n", uptime);

        print_uptime(now1.tv_sec);
        print_uptime(uptime);

        return 0;
}

Download