Initial import.
authorlace <>
Mon, 20 Aug 2007 18:21:19 +0000 (18:21 +0000)
committerlace <>
Mon, 20 Aug 2007 18:21:19 +0000 (18:21 +0000)
src/timeprec.c [new file with mode: 0644]

diff --git a/src/timeprec.c b/src/timeprec.c
new file mode 100644 (file)
index 0000000..64699ca
--- /dev/null
@@ -0,0 +1,57 @@
+/* time(1) with a better precision. */
+/* $Id$ */
+
+#include <sys/time.h>
+#include <stdlib.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <stdio.h>
+
+int main (int argc, char **argv)
+{
+  struct timeval tv_start, tv_end, tv_total;
+  pid_t child, pid_got;
+  int status;
+
+  if (gettimeofday (&tv_start, NULL) != 0)
+    {
+      perror ("gettimeofday ()");
+      exit (EXIT_FAILURE);
+    }
+
+  child = fork ();
+  switch (child)
+    {
+    case -1:
+      perror ("fork ()");
+      exit (EXIT_FAILURE);
+    case 0:
+      execvp (argv[1], argv + 1);
+      perror ("execvp ()");
+      exit (EXIT_FAILURE);
+    default:
+      pid_got = waitpid (child, &status, 0);
+      if (pid_got != child)
+        {
+         perror ("waitpid ()");
+         exit (EXIT_FAILURE);
+       }
+      if (!WIFEXITED (status))
+        {
+         fputs ("!WIFEXITED", stderr);
+         exit (EXIT_FAILURE);
+       }
+      break;
+    }
+
+  if (gettimeofday (&tv_end, NULL) != 0)
+    {
+      perror ("gettimeofday ()");
+      exit (EXIT_FAILURE);
+    }
+
+  timersub (&tv_end, &tv_start, &tv_total);
+  printf ("%ld.%06ld\n", tv_total.tv_sec, tv_total.tv_usec);
+
+  return WEXITSTATUS (status);
+}