home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / util / time / time_ma.lha / time.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-02  |  5.1 KB  |  201 lines

  1. /*
  2.  
  3.   --------------------------------------------------------------------------
  4.  
  5.   Time v1.2 Copyright © 1994 by Stefano Reksten of 3AM - The Three Amigos!!!
  6.   This piece of code is FREEWARE. Do anything with it but don't gain money
  7.   for my work! And please spread exe + source!
  8.  
  9.   What does it do? Simply tells you how much time did a command take to
  10.   complete.
  11.  
  12.   rekststef@unisi.it
  13.  
  14.   Stefano Reksten c/o Naimi,
  15.   v.le Cavour 40,
  16.   53100 Siena
  17.   Italy
  18.  
  19.   --------------------------------------------------------------------------
  20.  
  21.   Time v1.4 Copyright © February 2nd, 1997 by Matthias Andree
  22.  
  23.   mandree@sx1.hrz.uni-dortmund.de
  24.  
  25.   Matthias Andree
  26.   Stormstr. 14
  27.   58099 Hagen
  28.   Germany
  29.  
  30.   Motivation 1.2 -> 1.3
  31.  
  32.   GNU time did not start SEARCH properly
  33.   This program
  34.  
  35.  
  36.   Changes 1.2 -> 1.3
  37.  
  38.   - added Repeat option to improve accuracy for commands that last only a
  39.     very short period of time
  40.  
  41.   - changed from system to SystemTags, thus OS 2.04+ only.
  42.  
  43.   - suppresses requesters
  44.  
  45.   - added SMAKEFILE and README
  46.  
  47.  
  48.   Changes 1.3 -> 1.4
  49.  
  50.   - added FAILAT option, defaults to 10.
  51.  
  52.  
  53.  
  54.   Regards to Stefano Reksten, Oliver B. Warzecha
  55.  
  56.   --------------------------------------------------------------------------
  57.  
  58. */
  59.  
  60. #include <exec/types.h>
  61. #include <exec/io.h>
  62. #include <devices/timer.h>
  63.  
  64. #include <proto/exec.h>
  65. #include <proto/dos.h>
  66. #include <proto/timer.h>
  67. #include <clib/alib_protos.h>
  68. #include <dos.h>
  69. #include <dos/dostags.h>
  70. #include <stdlib.h>
  71. #include <stdio.h>
  72. #include <utility/tagitem.h>
  73. #include <string.h>
  74.  
  75. #define RES_SECS  0
  76. #define RES_MICRO 1
  77.  
  78. #ifndef __SASC
  79. #error This file needs autoinitialization and autotermination functions
  80. #error It is meant for use with SAS/C only, at this moment. Sorry.
  81. #endif
  82.  
  83. #define VERSION "1.4"
  84.  
  85. const char * const ver = "$VER: Time " VERSION " " __AMIGADATE__;
  86. static const char * const template = "R=REPEAT/K/N,F=FAILAT/K/N,C=COMMAND/A/F";
  87.  
  88. struct Library *TimerBase;
  89. static struct Window *oldWin = NULL;
  90.  
  91. __regargs int __STI_init(void)
  92. { /* save window pointer and suppress requesters */
  93.   struct Process *p = (struct Process *)FindTask(NULL);
  94.  
  95.   oldWin = p -> pr_WindowPtr;
  96.   p -> pr_WindowPtr = (void *)-1L;
  97.  
  98.   return 0;
  99. }
  100.  
  101. __regargs void __STD_exit(void)
  102. { /* restore old window pointer, re-enabling requesters */
  103.   struct Process *p = (struct Process *)FindTask(NULL);
  104.   if(oldWin) p -> pr_WindowPtr = oldWin;
  105. }
  106.  
  107.  
  108. int main( void )
  109. {
  110.   struct RDArgs *args;
  111.   struct timeval start_time, end_time;
  112.   struct timerequest *tIO;
  113.   struct MsgPort *mport;
  114.   LONG error;
  115.   struct {
  116.     LONG *repeat;
  117.     LONG *failat;
  118.     char  *cmd_line;
  119.   } myargs = { NULL, NULL, NULL };
  120.  
  121.   BPTR  output;
  122.   ULONG res[2];
  123.  
  124.   char myname[256];
  125.  
  126.   if(DOSBase -> dl_lib.lib_Version < 37L) {
  127.     char *warn = "This program requires OS 2.04 (V37) or newer.\n";
  128.     Write(Output(), warn, (LONG)strlen(warn));
  129.     return RETURN_FAIL;
  130.   }
  131.  
  132.   if(!GetProgramName(myname, sizeof(myname))) strcpy(myname, "");
  133.  
  134.   if ( args = ReadArgs((char *)template, (LONG *)&myargs, NULL ) ) {
  135.     if ( mport = CreateMsgPort() ) {
  136.       if ( tIO = (struct timerequest *)CreateExtIO(mport,sizeof(struct timerequest)) ) {
  137.         if ( !OpenDevice("timer.device",UNIT_VBLANK,(struct IORequest*)tIO,0L) ) {
  138.           ULONG count, i;
  139.           LONG failat;
  140.           BPTR nilhandle = Open("NIL:", MODE_NEWFILE); /* safe if NULL */
  141.  
  142.           TimerBase = (struct Library *)tIO->tr_node.io_Device;
  143.  
  144.           output = Output();
  145.           VFPrintf( output, "Time v" VERSION " © 1994/1997 by Stefano Reksten, Matthias Andree\n", NULL );
  146.           count  = myargs.repeat ? *myargs.repeat : 1;
  147.           failat = myargs.failat ? *myargs.failat : 10;
  148.           if(failat < 0) {
  149.             PrintFault(ERROR_BAD_NUMBER, myname);
  150.             return RETURN_FAIL;
  151.           }
  152.  
  153.           GetSysTime( &start_time );
  154.  
  155.           for(error = 0, i = 0;
  156.             error >= 0 && error < failat && i < count;
  157.             i++)
  158.           {
  159.             error = SystemTags( myargs.cmd_line,
  160.               SYS_Input, NULL,
  161.               SYS_Output, nilhandle, /* safe */
  162.               NP_WindowPtr, -1, /* suppress requestors */
  163.               TAG_DONE );
  164.           }
  165.  
  166.           GetSysTime( &end_time );
  167.  
  168.           if(error < 0 || error > failat) {
  169.             PrintFault(IoErr(), myname);
  170.           } else {
  171.             ULONG timediff;
  172.  
  173.             if ( end_time.tv_micro < start_time.tv_micro ) {
  174.               start_time.tv_secs--;
  175.               res[RES_MICRO] = (1000000 + end_time.tv_micro) - start_time.tv_micro;
  176.             } else {
  177.               res[RES_MICRO] = end_time.tv_micro - start_time.tv_micro;
  178.             }
  179.             res[RES_SECS] = end_time.tv_secs - start_time.tv_secs;
  180.  
  181.             timediff = res[RES_SECS] * 1000000 + res[RES_MICRO];
  182.  
  183.             timediff /= count;
  184.  
  185.             res[RES_SECS]  = timediff / 1000000;
  186.             res[RES_MICRO] = timediff % 1000000;
  187.  
  188.             VFPrintf( output, "Command took %lu,%06lu s.\n", (LONG *)res );
  189.           }
  190.           CloseDevice((struct IORequest *)tIO);
  191.         }
  192.         DeleteExtIO((struct IORequest *)tIO);
  193.       }
  194.       DeleteMsgPort(mport);
  195.     }
  196.     FreeArgs( args );
  197.   } else {
  198.     PrintFault( IoErr(), myname );
  199.   }
  200. }
  201.