sts: show time stamps added
authorMarkus Brökers <mbroeker@largo.homelinux.org>
Sun, 29 Aug 2010 15:29:22 +0200
changeset 144 f064cd793f8d
parent 143 88d9b8bfa7a5
child 145 63a5269fb113
sts: show time stamps added
Makefile
sts.c
--- a/Makefile
+++ b/Makefile
@@ -68,6 +68,7 @@
 TARGET += compliteral
 TARGET += getbits
 TARGET += fun
+TARGET += sts
 
 # needs ncursesw
 TARGET  += ncurses
@@ -356,6 +357,10 @@
 	@echo Linking $<...
 	@$(CC) -Wall -O2 -g -ggdb $(LDFLAGS) $< -o $@
 
+sts: sts.o
+	@echo Linking $<...
+	@$(CC) -Wall -O2 -lm -g -ggdb $(LDFLAGS) $< -o $@
+
 .PHONY: beauty clean uninstall
 
 clean:
new file mode 100644
--- /dev/null
+++ b/sts.c
@@ -0,0 +1,69 @@
+/**
+ * sts.c: show time stamp
+ * Copyright (C) 2010 Markus Broeker
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <time.h>
+#include <errno.h>
+
+void usage (char *name)
+{
+    printf ("Usage: %s STAMP [BOOT-STAMP]\n", name);
+    exit (EXIT_FAILURE);
+}
+
+time_t parse (char *buf)
+{
+    time_t sec = 0;
+    time_t milli = 0;
+    double value;
+
+    if (sscanf (buf, "%ld.%ld", &sec, &milli) != 2) {
+        fprintf (stderr, "  sec: %ld, ", sec);
+        fprintf (stderr, "milli: %ld\n", milli);
+    }
+
+    value = (milli / 1000000);
+    return sec + (time_t) (floor (value));
+}
+
+time_t getboottime ()
+{
+    char buf[80];
+    time_t now = time (NULL);
+
+    FILE *f = fopen ("/proc/uptime", "r");
+    if (fgets (buf, sizeof (buf), f) == NULL) {
+        perror ("FOPEN");
+        exit (errno);
+    }
+
+    fclose (f);
+    return now - parse (buf);
+}
+
+int main (int argc, char **argv)
+{
+    time_t boot;
+    time_t stamp = 0;
+    time_t diff;
+
+    if (argc < 2)
+        usage (argv[0]);
+
+    stamp = parse (argv[1]);
+
+    if (argc == 3) {
+        boot = parse (argv[2]);
+    } else
+        boot = getboottime ();
+
+    diff = boot + stamp;
+    printf ("%s: %s", argv[1], ctime (&diff));
+
+    return EXIT_SUCCESS;
+}