From: lace <> Date: Mon, 20 Aug 2007 18:21:19 +0000 (+0000) Subject: Initial import. X-Git-Url: https://git.jankratochvil.net/?a=commitdiff_plain;ds=sidebyside;h=757dfc8ae5b7661665eded0e1434084106ae9219;p=nethome.git Initial import. --- diff --git a/src/timeprec.c b/src/timeprec.c new file mode 100644 index 0000000..64699ca --- /dev/null +++ b/src/timeprec.c @@ -0,0 +1,57 @@ +/* time(1) with a better precision. */ +/* $Id$ */ + +#include +#include +#include +#include +#include + +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); +}