home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / t / time14.zip / time-1.4 / time.c < prev    next >
C/C++ Source or Header  |  1992-10-28  |  20KB  |  702 lines

  1. /* `time' utility to display resource usage of processes.
  2.    Copyright (C) 1989, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Most information is collected from the rusage struct.
  19.  
  20.    Usage: time [-avp] [-f format] [-o file] [--append] [--portability]
  21.           [--verbose] [--format format] [--output-file file] command [arg...]
  22.  
  23.    Options:
  24.    -a, --append            Append to output instead of overwriting.
  25.    -f, --format STRING        Use STRING to format the statistics.
  26.    -o, --output-file NAME    Send summary to NAME instead of stderr.
  27.    -p, --portability        Use the POSIX.2 output format.
  28.    -v, --verbose        Use a verbose format to print the statistics.
  29.  
  30.    Written by David Keppel, pardo@cs.washington.edu.
  31.    Modified by David MacKenzie, djm@gnu.ai.mit.edu. */
  32.  
  33. #include <stdio.h>
  34. #include <sys/types.h>
  35. #include <signal.h>
  36. #include <errno.h>
  37. #include <getopt.h>
  38.  
  39. #ifdef HAVE_SYS_TIME_H
  40. #include <sys/time.h>
  41. #else
  42. #include "timeval.h"
  43. #endif
  44.  
  45. #ifdef HAVE_SYS_RESOURCE_H
  46. #include <sys/resource.h>
  47. #include <sys/wait.h>
  48. #else
  49. #include "rusage.h"
  50. #endif
  51.  
  52. #if defined(__STDC__)
  53. # define P_(s) s
  54. # define PTR void *
  55. #else
  56. # define P_(s) ()
  57. # define PTR char *
  58. # define const
  59. #endif
  60.  
  61. #if defined(STDC_HEADERS) || defined(USG)
  62. #include <string.h>
  63. #else
  64. #include <strings.h>
  65. #endif
  66.  
  67. #ifdef STDC_HEADERS
  68. #include <limits.h>
  69. #include <stdlib.h>
  70. #else
  71. #define    LONG_MAX (~(1 << (sizeof (long) * 8 - 1)))
  72. void abort P_((void));
  73. void exit P_((int));
  74. char *getenv P_((const char *));
  75. PTR malloc P_((size_t));
  76. extern int errno;
  77. #endif
  78.  
  79. #ifdef HAVE_UNISTD_H
  80. #include <unistd.h>
  81. #endif
  82.  
  83. #ifndef _POSIX_VERSION
  84. int execvp P_((const char *, const char **));
  85. int fork P_((void));
  86. void _exit P_((int));
  87. #endif /* !_POSIX_VERSION */
  88.  
  89. void error P_((int status, int errnum, char *message, ...));
  90.  
  91. int gettimeofday P_((struct timeval *tp, struct timezone *tz));
  92. #ifdef HAVE_SYS_RESOURCE_H
  93. int wait3 P_((union wait *status, int options, struct rusage *rusage));
  94. #else
  95. int wait3 P_((int *status, int options, struct rusage *rusage));
  96. #endif
  97.  
  98. #include "getpagesize.h"
  99. #ifndef getpagesize
  100. int getpagesize P_((void));
  101. #endif
  102.  
  103. void usage P_((void));
  104.  
  105. #ifndef RETSIGTYPE
  106. #define RETSIGTYPE void
  107. #endif
  108. typedef RETSIGTYPE (*sighand_t) ();
  109.  
  110. /* The time in msec of one `tick' as used by the `getrusage' structure.
  111.    At least one Unix man page has an error, as it reports
  112.    units for average sizes in kb*sec.  Judging by the
  113.    output of `time' (all versions), this looks like it
  114.    ought to be in kb*ticks. */
  115. #define ONETICK (20)        /* 20 msec/tick, 1/50th sec. */
  116.  
  117. /* Convert T msec to a number of ticks. */
  118. #define TICK(t) ((t) / ONETICK)
  119.  
  120. typedef enum
  121. {
  122.   NO = 0, YES
  123. } bool;
  124.  
  125. #define UL unsigned long
  126.  
  127. char *version_string = "GNU time version 1.4";
  128.  
  129. /* The default output format.  */
  130. static const char *const default_format =
  131. "%Uuser %Ssystem %Eelapsed %PCPU (%Xavgtext+%Davgdata %Mmaxresident)k\n\
  132. %Iinputs+%Ooutputs (%Fmajor+%Rminor)pagefaults %Wswaps";
  133.  
  134. /* The output format for the -p option .*/
  135. static const char *const posix_format = "real %e\nuser %U\nsys %S";
  136.  
  137. /* Format string for printing all statistics verbosely.
  138.    The format string is used two ways: as a format string, and in
  139.    verbose mode, to document all the possible formatting possiblities.
  140.    When `longstats' is used as a format string, it has to be put into
  141.    one contiguous string (e.g., into a `char[]').  We could alternatively
  142.    store it as a `char *' and convert it into a `*char[]' when we need
  143.    it as documentation, but most compilers choke on very long strings. */
  144. static const char *const longstats[] =
  145. {
  146.   "\nSummary statistics:\n",
  147.   "\tCommand being timed: \"%C\"\n",
  148.   "\n",
  149.   "\tUser time (seconds): %U\n",
  150.   "\tSystem time (seconds): %S\n",
  151.   "\tPercent of CPU this job got: %P\n",
  152.   "\tElapsed (wall clock) time (h:mm:ss or m:ss): %E\n",
  153.   "\n",
  154.   "\tAverage shared text size (kbytes): %X\n",
  155.   "\tAverage unshared data size (kbytes): %D\n",
  156.   "\tAverage stack size (kbytes): %p\n",
  157.   "\tAverage total size (kbytes): %K\n",
  158.   "\tMaximum resident set size (kbytes): %M\n",
  159.   "\tAverage resident set size (kbytes): %t\n",
  160.   "\n",
  161.   "\tMajor (requiring I/O) page faults: %F\n",
  162.   "\tMinor (reclaiming a frame) page faults: %R\n",
  163.   "\tVoluntary context switches: %w\n",
  164.   "\tInvoluntary context switches: %c\n",
  165.   "\tSwaps: %W\n",
  166.   "\tFile system inputs: %I\n",
  167.   "\tFile system outputs: %O\n",
  168.   "\n",
  169.   "\tSocket messages sent: %s\n",
  170.   "\tSocket messages received: %r\n",
  171.   "\tSignals delivered: %k\n",
  172.   "\n",
  173.   "\tPage size (bytes): %Z\n",
  174.   "\tExit status: %x\n",
  175.   NULL
  176. };
  177.  
  178. /* If YES, show an English description next to each statistic. */
  179. static bool verbose;
  180.  
  181. /* Name of output file.  Only used if -o option is given. */
  182. static char const *outfile;
  183.  
  184. /* Output stream, stderr by default. */
  185. static FILE *outfp;
  186.  
  187. /* If YES, append to `outfile' rather than truncating it. */
  188. static bool append;
  189.  
  190. /* Controls the output format. */
  191. static char const *output_format;
  192.  
  193. /* The command line to run and gather statistics on. */
  194. static char const **command_line;
  195.  
  196. /* The name this program was invoked by. */
  197. char *program_name;
  198.  
  199. static struct option longopts[] =
  200. {
  201.   {"append", 0, NULL, 'a'},
  202.   {"format", 1, NULL, 'f'},
  203.   {"output-file", 1, NULL, 'o'},
  204.   {"portability", 0, NULL, 'p'},
  205.   {"verbose", 0, NULL, 'v'},
  206.   {"version", 0, NULL, 'V'},
  207.   {NULL, 0, NULL, 0}
  208. };
  209.  
  210. /* Print ARGV to FP, with each entry in ARGV separated by FILLER. */
  211.  
  212. void
  213. fprintargv (fp, argv, filler)
  214.      FILE *fp;
  215.      char const *const argv[];
  216.      char const *filler;
  217. {
  218.   char const *const *av;
  219.  
  220.   av = argv;
  221.   fputs (*av, fp);
  222.   while (*++av)
  223.     {
  224.       fputs (filler, fp);
  225.       fputs (*av, fp);
  226.     }
  227.   if (ferror (fp))
  228.     error (1, errno, "write error");
  229. }
  230.  
  231. /* Return the number of kilobytes corresponding to a number of pages PAGES.
  232.    Try to do arithmetic so that the risk of overflow errors is minimized.
  233.    This is funky since the pagesize could be less than 1K.
  234.    Note: Some machines express getrusage statistics in terms of K,
  235.    others in terms of pages. */
  236.  
  237. unsigned long
  238. tok (pages)
  239.      unsigned long pages;
  240. {
  241.   static unsigned long ps = 0;
  242.   unsigned long tmp;
  243.   static long size = LONG_MAX;
  244.  
  245.   /* Initialization. */
  246.   if (ps == 0)
  247.     ps = (long) getpagesize ();
  248.  
  249.   /* Conversion. */
  250.   if (pages > (LONG_MAX / ps))
  251.     {                /* Could overflow.  */
  252.       tmp = pages / 1024;    /* Smaller first, */
  253.       size = tmp * ps;        /* then larger.  */
  254.     }
  255.   else
  256.     {                /* Could underflow.  */
  257.       tmp = pages * ps;        /* Larger first, */
  258.       size = tmp / 1024;    /* then smaller.  */
  259.     }
  260.   return size;
  261. }
  262.  
  263. /* summarize: Report on the system use of a command.
  264.  
  265.    Copy the FMT argument to FP except that `%' sequences
  266.    have special meaning, and `\n' and `\t' are translated into
  267.    newline and tab, respectively, and `\\' is translated into `\'.
  268.  
  269.    The character following a `%' can be:
  270.    (* means the tcsh time builtin also recognizes it)
  271.    % == a literal `%'
  272.    C == command name
  273. *  D == average unshared data size in K (ru_idrss+ru_isrss)
  274. *  E == elapsed real (wall clock) time in [hour:]min:sec.usec
  275. *  F == major page faults (required physical I/O) (ru_majflt)
  276. *  I == file system inputs (ru_inblock)
  277. *  K == average total mem usage (ru_idrss+ru_isrss+ru_ixrss)
  278. *  M == maximum resident set size in K (ru_maxrss)
  279. *  O == file system outputs (ru_oublock)
  280. *  P == percent of CPU this job got (total cpu time / elapsed time)
  281. *  R == minor page faults (reclaims; no physical I/O involved) (ru_minflt)
  282. *  S == system (kernel) time (seconds:usec) (ru_stime)
  283. *  U == user time (seconds:usec) (ru_utime)
  284. *  W == times swapped out (ru_nswap)
  285. *  X == average amount of shared text in K (ru_ixrss)
  286.    Z == page size
  287. *  c == involuntary context switches (ru_nivcsw)
  288. *  k == signals delivered (ru_nsignals)
  289.    p == average unshared stack size in K (ru_isrss)
  290. *  r == socket messages received (ru_msgrcv)
  291. *  s == socket messages sent (ru_msgsnd)
  292.    t == average resident set size in K (ru_idrss)
  293. *  w == voluntary context switches (ru_nvcsw)
  294.    x == exit status of command
  295.  
  296.    Various memory usages are found by converting from page-seconds
  297.    to kbytes by multiplying by the page size, dividing by 1024,
  298.    and dividing by elapsed real time.
  299.  
  300.    FP is the stream to print to.
  301.    FMT is the format string, interpreted as described above.
  302.    COMMAND is the command and args that are being summarized.
  303.    RUP is information on the command.
  304.    TV is the elapsed time of the command.
  305.    EXITSTAT is the command's exit status. */
  306.  
  307. void
  308. summarize (fp, fmt, command, rup, tv, exitstat)
  309.      FILE *fp;
  310.      char const *fmt;
  311.      char const *command[];
  312.      struct rusage *rup;
  313.      struct timeval *tv;
  314.      int exitstat;
  315. {
  316.   unsigned long r;        /* Elapsed real milliseconds. */
  317.   unsigned long v;        /* Elapsed virtual (CPU) time. */
  318.  
  319.   if (exitstat)
  320.     fprintf (fp, "Command had non-zero exit status %d\n", exitstat);
  321.  
  322.   /* All times converted to milliseconds.  `r' is real, or wallclock
  323.      time.  `v' is virtual, or cpu-seconds time.  Occasionally, one
  324.      of these values comes out as zero.  Dividing by zero causes
  325.      problems, so we first need to check the time value.  If it is
  326.      zero, then we take `evasive action' instead of printing a
  327.      value. */
  328.   r = tv->tv_sec * 1000 + tv->tv_usec / 1000;
  329.   v = rup->ru_utime.tv_sec * 1000 + rup->ru_utime.tv_usec / 1000 +
  330.     rup->ru_stime.tv_sec * 1000 + rup->ru_stime.tv_usec / 1000;
  331.  
  332.   while (*fmt)
  333.     {
  334.       switch (*fmt)
  335.     {
  336.     case '%':
  337.       switch (*++fmt)
  338.         {
  339.         case '%':        /* Literal '%'.  */
  340.           putc ('%', fp);
  341.           break;
  342.         case 'C':        /* The command that got timed.  */
  343.           /* If no command, print nothing.  */
  344.           if (*command)
  345.         fprintargv (fp, command, " ");
  346.           break;
  347.         case 'D':        /* Total unshared data size.  */
  348.           fprintf (fp, "%lu", (TICK (v) == 0)
  349.                ? 0
  350.                : tok ((UL) rup->ru_idrss) / TICK (v) +
  351.                tok ((UL) rup->ru_isrss) / TICK (v));
  352.           break;
  353.         case 'E':        /* Elapsed real (wall clock) time.  */
  354.           if (tv->tv_sec >= 3600)
  355.         fprintf (fp, "%ld:%02ld:%02ld", tv->tv_sec / 3600,
  356.              (tv->tv_sec % 3600) / 60, tv->tv_sec % 60);
  357.           else
  358.         fprintf (fp, "%ld:%02ld.%02ld", tv->tv_sec / 60,
  359.              tv->tv_sec % 60, tv->tv_usec / 10000);
  360.           break;
  361.         case 'F':        /* Major page faults.  */
  362.           fprintf (fp, "%ld", rup->ru_majflt);
  363.           break;
  364.         case 'I':        /* Inputs.  */
  365.           fprintf (fp, "%ld", rup->ru_inblock);
  366.           break;
  367.         case 'K':        /* Average mem usage == data+stack+text.  */
  368.           fprintf (fp, "%lu",
  369.                (TICK (v) == 0) ? 0 :
  370.                tok ((UL) rup->ru_idrss) / TICK (v) +
  371.                tok ((UL) rup->ru_isrss) / TICK (v) +
  372.                tok ((UL) rup->ru_ixrss) / TICK (v));
  373.           break;
  374.         case 'M':        /* Maximum resident set size.  */
  375.           fprintf (fp, "%lu", tok ((UL) rup->ru_maxrss));
  376.           break;
  377.         case 'O':        /* Outputs.  */
  378.           fprintf (fp, "%ld", rup->ru_oublock);
  379.           break;
  380.         case 'P':        /* Percent of CPU this job got.  */
  381.           /* % cpu is (total cpu time)/(elapsed time).  */
  382.           if (r > 0)
  383.         fprintf (fp, "%lu%%", (v * 100 / r));
  384.           else
  385.         fprintf (fp, "?%%");
  386.           break;
  387.         case 'R':        /* Minor page faults (reclaims).  */
  388.           fprintf (fp, "%ld", rup->ru_minflt);
  389.           break;
  390.         case 'S':        /* System time.  */
  391.           fprintf (fp, "%ld.%ld",
  392.                rup->ru_stime.tv_sec, rup->ru_stime.tv_usec / 10000);
  393.           break;
  394.         case 'U':        /* User time.  */
  395.           fprintf (fp, "%ld.%ld",
  396.                rup->ru_utime.tv_sec, rup->ru_utime.tv_usec / 10000);
  397.           break;
  398.         case 'W':        /* Times swapped out.  */
  399.           fprintf (fp, "%ld", rup->ru_nswap);
  400.           break;
  401.         case 'X':        /* Average shared text size.  */
  402.           fprintf (fp, "%lu", (TICK (v) == 0) ? 0
  403.                : tok ((UL) rup->ru_ixrss) / TICK (v));
  404.           break;
  405.         case 'Z':        /* Page size.  */
  406.           fprintf (fp, "%d", getpagesize ());
  407.           break;
  408.         case 'c':        /* Involuntary context switches.  */
  409.           fprintf (fp, "%ld", rup->ru_nivcsw);
  410.           break;
  411.         case 'e':        /* Elapsed real time in seconds.  */
  412.           fprintf (fp, "%ld.%ld", tv->tv_sec, tv->tv_usec / 10000);
  413.           break;
  414.         case 'k':        /* Signals delivered.  */
  415.           fprintf (fp, "%ld", rup->ru_nsignals);
  416.           break;
  417.         case 'p':        /* Stack segment.  */
  418.           fprintf (fp, "%lu", (TICK (v) == 0) ? 0
  419.                : tok ((UL) rup->ru_isrss) / TICK (v));
  420.           break;
  421.         case 'r':        /* Incoming socket messages received.  */
  422.           fprintf (fp, "%ld", rup->ru_msgrcv);
  423.           break;
  424.         case 's':        /* Outgoing socket messages sent.  */
  425.           fprintf (fp, "%ld", rup->ru_msgsnd);
  426.           break;
  427.         case 't':        /* Average resident set size.  */
  428.           fprintf (fp, "%lu",
  429.          (TICK (v) == 0) ? 0 : tok ((UL) rup->ru_idrss) / TICK (v));
  430.           break;
  431.         case 'w':        /* Voluntary context switches.  */
  432.           fprintf (fp, "%ld", rup->ru_nvcsw);
  433.           break;
  434.         case 'x':        /* Exit status.  */
  435.           fprintf (fp, "%d", exitstat);
  436.           break;
  437.         case '\0':
  438.           putc ('?', fp);
  439.           return;
  440.         default:
  441.           putc ('?', fp);
  442.           putc (*fmt, fp);
  443.         }
  444.       ++fmt;
  445.       break;
  446.  
  447.     case '\\':        /* Format escape. */
  448.       switch (*++fmt)
  449.         {
  450.         case 't':
  451.           putc ('\t', fp);
  452.           break;
  453.         case 'n':
  454.           putc ('\n', fp);
  455.           break;
  456.         case '\\':
  457.           putc ('\\', fp);
  458.           break;
  459.         default:
  460.           putc ('?', fp);
  461.           putc ('\\', fp);
  462.           putc (*fmt, fp);
  463.         }
  464.       ++fmt;
  465.       break;
  466.  
  467.     default:
  468.       putc (*fmt++, fp);
  469.     }
  470.  
  471.       if (ferror (fp))
  472.     error (1, errno, "write error");
  473.     }
  474.   putc ('\n', fp);
  475.  
  476.   if (ferror (fp))
  477.     error (1, errno, "write error");
  478. }
  479.  
  480. /* Return a null-terminated string containing the concatenation,
  481.    in order, of all of the elements of ARGV.
  482.    The '\0' at the end of each ARGV-element is not copied.
  483.    Example:    char *argv[] = {"12", "ab", ".,"};
  484.          linear_argv(argv) == "12ab.,"
  485.    Print a message and return NULL if memory allocation failed. */
  486.  
  487. static char *
  488. linear_argv (argv)
  489.      char const *const *argv;
  490. {
  491.   char const *const *s;        /* Each string in ARGV. */
  492.   char *new;            /* Allocated space. */
  493.   char *dp;            /* Copy in to destination. */
  494.   char const *sp;        /* Copy from source. */
  495.   int size;
  496.  
  497.   /* Find length of ARGV and allocate. */
  498.   size = 1;
  499.   for (s = argv; *s; ++s)
  500.     size += strlen (*s);
  501.   new = (char *) malloc (size);
  502.   if (new == NULL)
  503.     {
  504.       fprintf (stderr, "%s: virtual memory exhausted\n", program_name);
  505.       return NULL;
  506.     }
  507.  
  508.   /* Copy each string in ARGV to the new string.  At the end of
  509.      each string copy, back up `dp' so that on the next string,
  510.      the `\0' will be overwritten. */
  511.   for (s = argv, sp = *s, dp = new; *s; ++s)
  512.     {
  513.       sp = *s;
  514.       while ((*dp++ = *sp++) != '\0')
  515.     /* Do nothing. */ ;
  516.       --dp;
  517.     }
  518.  
  519.   return new;
  520. }
  521.  
  522. /* Initialize the options and parse the command line arguments.
  523.    Also note the position in ARGV where the command to time starts.
  524.  
  525.    By default, output is to stderr.
  526.  
  527.    ARGV is the array of command line arguments.
  528.    ARGC is the number of command line arguments. */
  529.  
  530. void
  531. getargs (argc, argv)
  532.      int argc;
  533.      char *argv[];
  534. {
  535.   int optc, longind;
  536.   char *format;            /* Format found in environment. */
  537.  
  538.   /* Initialize the option flags. */
  539.   verbose = NO;
  540.   outfile = NULL;
  541.   outfp = stderr;
  542.   append = NO;
  543.   output_format = default_format;
  544.   program_name = argv[0];
  545.  
  546.   /* Set the format string from the environment.  Do this before checking
  547.      the args so that we won't clobber a user-specified format. */
  548.   format = getenv ("TIME");
  549.   if (format)
  550.     output_format = format;
  551.  
  552.   while ((optc = getopt_long (argc, argv, "+af:o:pvV", longopts, &longind))
  553.      != EOF)
  554.     {
  555.       switch (optc)
  556.     {
  557.     case 'a':
  558.       append = YES;
  559.       break;
  560.     case 'f':
  561.       output_format = optarg;
  562.       break;
  563.     case 'o':
  564.       outfile = optarg;
  565.       break;
  566.     case 'p':
  567.       output_format = posix_format;
  568.       break;
  569.     case 'v':
  570.       verbose = YES;
  571.       break;
  572.     case 'V':
  573.       fprintf (stderr, "%s\n", version_string);
  574.       break;
  575.     default:
  576.       usage ();
  577.     }
  578.     }
  579.  
  580.   if (optind == argc)
  581.     usage ();
  582.  
  583.   command_line = (char const **) &argv[optind];
  584.  
  585.   if (outfile)
  586.     {
  587.       if (append)
  588.     outfp = fopen (outfile, "a");
  589.       else
  590.     outfp = fopen (outfile, "w");
  591.       if (outfp == NULL)
  592.     error (1, errno, "%s", outfile);
  593.     }
  594.  
  595.   /* If the user specified verbose output, we need to convert
  596.      `longstats' to a `char *'. */
  597.   if (verbose)
  598.     {
  599.       output_format = (char const *) linear_argv (longstats);
  600.       if (output_format == NULL)
  601.     exit (1);        /* Out of memory. */
  602.     }
  603. }
  604.  
  605. /* Run command CMD and return statistics on it.
  606.    *RUSE gets most of the statistics.
  607.    *FINISH gets the elapsed time.
  608.    *EXITCODE gets the exit code of the child. */
  609.  
  610. void
  611. run_command (ruse, finish, exitcode, cmd)
  612.      struct rusage *ruse;
  613.      struct timeval *finish;
  614.      int *exitcode;
  615.      char const *const cmd[];
  616. {
  617. #ifdef HAVE_SYS_RESOURCE_H
  618.   union wait status;        /* Exit status of child. */
  619. #else
  620.   int status;
  621. #endif
  622.   int pid;            /* Pid of child. */
  623.   int caught;            /* Pid got back from wait. */
  624.   struct timeval start;        /* When job starts. */
  625.   sighand_t interrupt_signal, quit_signal;
  626.  
  627.   /* Set time the command started.  Since this is BEFORE the
  628.      fork/exec sequence, this number is probably too large. */
  629.   gettimeofday (&start, (struct timezone *) NULL);
  630.  
  631.   pid = fork ();        /* Run CMD as child process. */
  632.   if (pid < 0)
  633.     error (1, errno, "cannot fork");
  634.   else if (pid == 0)
  635.     {                /* If child. */
  636.       execvp (cmd[0], (const char **) cmd);
  637.       error (0, errno, "cannot run %s", cmd[0]);
  638.       _exit (errno == ENOENT ? 127 : 126);
  639.     }
  640.  
  641.   /* Have signals kill the child but not self (if possible). */
  642.   interrupt_signal = signal (SIGINT, SIG_IGN);
  643.   quit_signal = signal (SIGQUIT, SIG_IGN);
  644.  
  645.   /* Ignore signals, but don't ignore the children.  When wait3
  646.      returns the child process, set the time the command finished. */
  647.   while ((caught = wait3 (&status, 0, ruse)) != pid)
  648.     {
  649.       if (caught == -1)
  650.     error (1, errno, "error waiting for child process");
  651.     }
  652.  
  653.   /* Re-enable signals. */
  654.   signal (SIGINT, interrupt_signal);
  655.   signal (SIGQUIT, quit_signal);
  656.  
  657.   gettimeofday (finish, (struct timezone *) NULL);
  658.  
  659.   /* Set `finish' to hold the elapsed "real" (wallclock) time of
  660.      the command.  If finish.usec < start.usec, manually carry a one
  661.      from the seconds field. */
  662.   finish->tv_sec -= start.tv_sec;
  663.   if (finish->tv_usec < start.tv_usec)
  664.     {
  665.       finish->tv_usec += 1000000;
  666.       --finish->tv_sec;
  667.     }
  668.   finish->tv_usec -= start.tv_usec;
  669.  
  670. #ifdef HAVE_SYS_RESOURCE_H
  671.   *exitcode = (int) status.w_T.w_Retcode;
  672. #else
  673.   *exitcode = status >> 8;
  674. #endif
  675. }
  676.  
  677. void
  678. main (argc, argv)
  679.      int argc;
  680.      char **argv;
  681. {
  682.   int exitcode;            /* Exit status of job we ran. */
  683.   struct timeval elapsed;    /* Wallclock time of job. */
  684.   struct rusage ruse;        /* Use by children. */
  685.  
  686.   getargs (argc, argv);
  687.   run_command (&ruse, &elapsed, &exitcode, command_line);
  688.   summarize (outfp, output_format, command_line, &ruse, &elapsed, exitcode);
  689.   exit (exitcode);        /* Exit with child's status. */
  690. }
  691.  
  692. void
  693. usage ()
  694. {
  695.   fprintf (stderr, "\
  696. Usage: %s [-apvV] [-f format] [-o file] [--append] [--portability]\n\
  697.        [--verbose] [--format=format] [--output-file=file] [--version]\n\
  698.        command [arg...]\n",
  699.        program_name);
  700.   exit (1);
  701. }
  702.