summaryrefslogtreecommitdiff
path: root/src/time.c
blob: 5338cb9984565f2d73f4f4699f567f1926f0f354 (plain)
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
#include <stdio.h>
#include <stddef.h>
#include <sys/time.h>

#include "error.h"

double timestamp(void) {
	struct timeval  tv;

	if (gettimeofday(&tv, NULL) == -1) {
		return -1;
	}

	return tv.tv_sec + (tv.tv_usec * 1e-6);
}

unsigned long get_boot_time(void) {
	FILE *fp = fopen("/proc/stat", "r");
	if (!fp) {
		error("fopen /proc/stat: %s", strerror(errno));
		return 0;
	}

	char           buf[256];
	unsigned long  btime = 0;

	while (fgets(buf, sizeof(buf), fp)) {
		if (sscanf(buf, "btime %lu", &btime) == 1) {
			break;
		}
	}

	fclose(fp);
	return btime;
}