home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 1 / FFMCD01.bin / bbs / libdisks / d700t799 / disk774.lha / ExtraCmds / src / TimeCom.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-05  |  5.5 KB  |  205 lines

  1. /*
  2.  * TimeCom - Time a command.
  3.  *
  4.  * Version 37.1 =TP= 16-Jan-92
  5.  * Compile with SAS/C 5.10 and link without startup code:
  6.  *      lc -cqfist -v -b0 -rr -O -ms TimeCom
  7.  *      blink TimeCom.o to TimeCom sd sc
  8.  *      protect TimeCom +p
  9.  *
  10.  * Copyright (c) 1992 Torsten Poulin
  11.  *
  12.  * Torsten Poulin
  13.  * Banebrinken 99, 2, lejlighed 77
  14.  * DK 2400  København NV
  15.  * DENMARK
  16.  */
  17. /* section: User Environment Utilities */
  18.  
  19. /****** English:TIMECOM ***************************************************
  20. *
  21. *   FORMAT
  22. *       TIMECOM [TO <log file> [APPEND]] [COMMAND] <command line>
  23. *
  24. *   TEMPLATE
  25. *       TO/K,APPEND/S,COMMAND/F/A
  26. *
  27. *   PURPOSE
  28. *       To time a command.
  29. *
  30. *   SPECIFICATION
  31. *       The <command line> is executed; after it is complete, TimeCom
  32. *       prints the elapsed time during the command.  The time is
  33. *       reported in seconds.
  34. *
  35. *       The time is printed on CONSOLE: unless the TO option is used
  36. *       to specify an output file.  The APPEND switch adds the output
  37. *       to the end of the specified output file.  This is useful for
  38. *       keeping a log of the execution times of several commands.
  39. *
  40. *   EXAMPLES
  41. *           1> TIMECOM MyProgram
  42. *           Elapsed time: 23 seconds.
  43. *
  44. *           1> TIMECOM TO timelog APPEND MyProgram
  45. *           1> TYPE timelog
  46. *           Elapsed time: 23 seconds. Command: MyProgram
  47. *
  48. *           1> ALIAS MyProgram "TIMECOM TO log APPEND MyProgram"
  49. *           1> MyProgram
  50. *
  51. *   WARNING
  52. *       The time value is not extremely accurate.
  53. *
  54. *   SEE ALSO
  55. *       intuition.library/CurrentTime
  56. *       in `ROM Kernel Reference Manual: Includes and Autodocs.'
  57. *
  58. ***************************************************************************
  59. *
  60. */
  61. /****** dansk:TIMECOM *****************************************************
  62. *
  63. *   FORMAT
  64. *       TIMECOM [TO <logfil> [APPEND]] [COMMAND] <kommandolinje>
  65. *
  66. *   SKABELON
  67. *       TO/K,APPEND/S,COMMAND/F/A
  68. *
  69. *   FORMÅL
  70. *       At tage tid på en kommando.
  71. *
  72. *   SPECIFIKATION
  73. *       Argumentet <kommandolinje> udføres.  Når det er færdig
  74. *       udskriver TimeCom den forløbne tid.  Tiden rapporteres i
  75. *       sekunder.
  76. *
  77. *       Tiden udskrives på CONSOLE: medmindre argumentet TO bruges
  78. *       til at angive en uddatafil.  Kontakten APPEND tilføjer
  79. *       uddata til slutningen uddatafilen.  Dette er nyttigt til
  80. *       at føre en logfil over kørselstiderne for flere
  81. *       kommandoer.
  82. *
  83. *   EKSEMPLER
  84. *           1> TIMECOM MitProgram
  85. *           Elapsed time: 23 seconds.
  86. *
  87. *           1> TIMECOM TO tidslog APPEND MitProgram
  88. *           1> TYPE tidslog
  89. *           Elapsed time: 23 seconds. Command: MitProgram
  90. *
  91. *           1> ALIAS MitProgram "TIMECOM TO logfil APPEND MitProgram"
  92. *           1> MitProgram
  93. *
  94. *   ADVARSEL
  95. *       Tiderne er ikke ekstremt nøjagtige.
  96. *
  97. *   SE OGSÅ
  98. *       intuition.library/CurrentTime
  99. *       i `ROM Kernel Reference Manual: Includes and Autodocs.'
  100. *
  101. ***************************************************************************
  102. *
  103. */
  104.  
  105.  
  106. #include <exec/types.h>
  107. #include <libraries/dos.h>
  108. #include <dos/dos.h>
  109. #include <dos/dostags.h>
  110. #include <proto/exec.h>
  111. #include <proto/dos.h>
  112. #include <proto/intuition.h>
  113.  
  114. #define OPT_TO      0
  115. #define OPT_APPEND  1
  116. #define OPT_COMMAND 2
  117.  
  118.  
  119. static LONG MySystemTags(struct DosLibrary *DOSBase,
  120.                          UBYTE *command, ULONG firsttag, ...);
  121.  
  122. char const *version = "\0$VER: TimeCom 37.1 (16.1.92) ©1992 Torsten Poulin";
  123.  
  124. int entrypoint(void)
  125. {
  126.     struct RDArgs        *args;
  127.  
  128.     struct IntuitionBase *IntuitionBase;
  129.     struct DosLibrary    *DOSBase;
  130.     struct Library       *SysBase;
  131.  
  132.     BPTR  out;
  133.     ULONG secs1 = 0, secs2 = 0, not_used = 0;
  134.     LONG  arg[3];
  135.     LONG  rc = RETURN_OK;
  136.  
  137.  
  138.     SysBase = *(struct Library **) 4L;
  139.     if(!(DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37L)))
  140.     {
  141.         rc = RETURN_FAIL;
  142.         goto exit1;
  143.     }
  144.     if(!(IntuitionBase = (struct IntuitionBase *)
  145.                             OpenLibrary("intuition.library", 33L)))
  146.     {
  147.         rc = RETURN_FAIL;
  148.         goto exit2;
  149.     }
  150.  
  151.     arg[OPT_TO] = arg[OPT_APPEND] = arg[OPT_COMMAND] = 0L;
  152.     
  153.     if(args = ReadArgs("TO/K,APPEND/S,COMMAND/F/A", arg, NULL))
  154.     {
  155.         CurrentTime(&secs1, ¬_used);
  156.         rc = MySystemTags(DOSBase, (UBYTE *) arg[OPT_COMMAND],
  157.                           SYS_UserShell, TRUE,
  158.                           TAG_DONE);
  159.         CurrentTime(&secs2, ¬_used);
  160.         if(rc == -1)
  161.             rc = RETURN_ERROR;
  162.         secs2 -= secs1;
  163.  
  164.         if(out = Open(arg[OPT_TO] ? (UBYTE *) arg[OPT_TO] : "CONSOLE:",
  165.                  (BOOL) arg[OPT_APPEND] ? MODE_READWRITE : MODE_NEWFILE))
  166.         {
  167.             if((BOOL) arg[OPT_APPEND] && arg[OPT_TO])
  168.                 Seek(out, 0L, OFFSET_END);
  169.             VFPrintf(out, "Elapsed time: %ld second", (LONG *) &secs2);
  170.             FPuts(out, secs2 == 1 ? "." : "s.");
  171.             if(arg[OPT_TO])
  172.                 VFPrintf(out, " Command: %s", (LONG *) &arg[OPT_COMMAND]);
  173.             FPutC(out, '\n');
  174.             Close(out);
  175.         }
  176.         else
  177.         {
  178.             LONG err = IoErr();
  179.             PrintFault(err, "TimeCom");
  180.             rc = RETURN_ERROR;
  181.         }
  182.  
  183.         FreeArgs(args);
  184.     }
  185.     else
  186.     {
  187.         LONG err = IoErr();
  188.         PrintFault(err, "TimeCom");
  189.         rc = RETURN_ERROR;
  190.     }
  191.  
  192.     CloseLibrary((struct Library *) IntuitionBase);
  193.  exit2:
  194.     CloseLibrary((struct Library *) DOSBase);
  195.  exit1:
  196.     return rc;
  197. }
  198.  
  199.  
  200. static LONG MySystemTags(struct DosLibrary *DOSBase,
  201.                          UBYTE *command, ULONG firsttag, ...)
  202. {
  203.     return SystemTagList(command, (struct TagItem *) &firsttag);
  204. }
  205.