+option pie
[nethome.git] / src / timeprec.c
1 /* time(1) with a better precision. */
2 /* $Id$ */
3
4 #include <sys/time.h>
5 #include <stdlib.h>
6 #include <sys/wait.h>
7 #include <unistd.h>
8 #include <stdio.h>
9
10 int main (int argc, char **argv)
11 {
12   struct timeval tv_start, tv_end, tv_total;
13   pid_t child, pid_got;
14   int status;
15
16   if (gettimeofday (&tv_start, NULL) != 0)
17     {
18       perror ("gettimeofday ()");
19       exit (EXIT_FAILURE);
20     }
21
22   child = fork ();
23   switch (child)
24     {
25     case -1:
26       perror ("fork ()");
27       exit (EXIT_FAILURE);
28     case 0:
29       execvp (argv[1], argv + 1);
30       perror ("execvp ()");
31       exit (EXIT_FAILURE);
32     default:
33       pid_got = waitpid (child, &status, 0);
34       if (pid_got != child)
35         {
36           perror ("waitpid ()");
37           exit (EXIT_FAILURE);
38         }
39       if (!WIFEXITED (status))
40         {
41           fputs ("!WIFEXITED", stderr);
42           exit (EXIT_FAILURE);
43         }
44       break;
45     }
46
47   if (gettimeofday (&tv_end, NULL) != 0)
48     {
49       perror ("gettimeofday ()");
50       exit (EXIT_FAILURE);
51     }
52
53   timersub (&tv_end, &tv_start, &tv_total);
54   printf ("%ld.%06ld\n", tv_total.tv_sec, tv_total.tv_usec);
55
56   return WEXITSTATUS (status);
57 }