summaryrefslogtreecommitdiff
path: root/src/time.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/time.c')
-rw-r--r--src/time.c35
1 files changed, 35 insertions, 0 deletions
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 <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;
+}