home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / TIMER.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  99 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*_ timer.c   Mon Feb 29 1988   Modified by: Jerry Coffin   */
  4. /* Written by Walter Bright                                 */
  5. /* To compile (with Zortech C):                             */
  6. /*    ZTC -mti timer                                        */
  7.  
  8. #include    <stdio.h>
  9. #include    <time.h>
  10. #include    <process.h>
  11. #include    <stdlib.h>
  12. #include    <string.h>
  13. #include    "extkword.h"
  14.  
  15. #if defined(_QC) || defined(_MSC_VER)
  16.  #include    <malloc.h>
  17. #endif
  18.  
  19. #ifdef __SC__
  20.  #define SVP_CAST(x) (const char * const *)(x)
  21. #else
  22.  #define SVP_CAST(x) (x)
  23. #endif
  24.  
  25. #ifdef __ZTC__
  26. int _okbigbuf = 0;      /* Use as little memory as possible */
  27. #endif
  28.  
  29. main(int argc, char *argv[])
  30. {
  31.       clock_t clock(),starttime;
  32.       int status, i;
  33.  
  34.       if (argc < 2)
  35.       {     printf("Time execution of a command.\nUse:\n\ttimer command\n");
  36.             exit(1);
  37.       }
  38.       argv[argc] = 0;         /* terminate with a 0 (unportable method) */
  39.       starttime = clock();
  40.  
  41. /*
  42. ** This block added to keep arguments with embedded whitespace from getting
  43. ** broken up when timing is done.  A *nix version should be a bit different.
  44. ** Also, the assignment to argv[i] technically isn't portable, but I don't
  45. ** know of an implementation where it causes a problem. <JC>
  46. */
  47.       for (i=1;i<argc;i++)
  48.       {
  49.             if (NULL != strchr(argv[i], ' '))
  50.             {
  51.                   size_t len = strlen(argv[i]+3);
  52.                   char *temp = malloc(len);
  53.  
  54.                   strcpy(temp+1, argv[i]);
  55.                   temp[0] = '"';
  56.                   temp[len-2] = '"';
  57.                   temp[len-1] = '\0';
  58.                   argv[i] = temp;
  59.             }
  60.       }
  61.  
  62. /* This reduces memory usage with MS compilers by releasing unused heap.
  63.  * Watcom may support it as well, but I'm not sure.
  64.  */
  65. #if defined(_QC) || defined(_MSC_VER)
  66.       _heapmin();
  67. #endif
  68.       status = spawnvp(0,argv[1],SVP_CAST(argv + 1));
  69.       starttime = clock() - starttime;
  70.       if (status == -1)
  71.       {     printf("'%s' failed to execute\n",argv[1]);
  72.             exit(1);
  73.       }
  74. #if !defined(__TURBOC__)
  75.       printf("Elapsed time = %d.%02d seconds\n",(int) (starttime/CLK_TCK),
  76.             (int) (starttime%CLK_TCK));
  77. #else
  78.       printf("Elapsed time = %.2f seconds\n", starttime/CLK_TCK);
  79. #endif
  80.       if (status != 0)
  81.             printf("--- errorlevel %d\n",status);
  82.       /* exit(0); changed to `return 0;' to reduce program size (minutely)
  83.        * and possibility or warnings that main doesn't return a value.
  84.        */
  85.       return 0;
  86. }
  87.  
  88. #ifdef __ZTC__
  89.  
  90. /* Prevent exit() and fclose() from being linked in from library  */
  91. /* (to conserve the size of the output file).                     */
  92.  
  93. void exit(int exitstatus)
  94. {
  95.       _exit(exitstatus);
  96. }
  97.  
  98. #endif
  99.