sts.c
changeset 144 f064cd793f8d
child 145 63a5269fb113
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;
+}