From 2278df1493e064c197913e49b5d1935942d83448 Mon Sep 17 00:00:00 2001 From: daniel Date: Tue, 6 May 2025 16:57:32 -0700 Subject: initial import --- src/time.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/time.c (limited to 'src/time.c') diff --git a/src/time.c b/src/time.c new file mode 100644 index 0000000..5338cb9 --- /dev/null +++ b/src/time.c @@ -0,0 +1,35 @@ +#include +#include +#include + +#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; +} -- cgit v1.2.3