home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 274.lha / ETime_v1.02 / etime.c < prev    next >
C/C++ Source or Header  |  1989-07-26  |  4KB  |  141 lines

  1. /******************************************************************************/
  2. /*                                                                            */
  3. /*      program: etime                                                        */
  4. /*   programmer: George Kerber                                                */
  5. /*      written:  07/16/89                                                    */
  6. /*                                                                            */
  7. /******************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <time.h>
  11. #include <string.h>
  12.  
  13. #define WRITTEN "07/16/89 - 07/23/89"
  14. #define VERSION "v1.02"
  15.  
  16.  
  17. FILE *tfile;
  18. char node[31];
  19. void main();
  20. void mistake();
  21. void helpscreen();
  22. void line();
  23.   
  24. void main(argc,argv)
  25. int argc;
  26. char *argv[];
  27. {
  28. char filename[80] = "env:" ;
  29. char storedtime[80];
  30. long time1,time2;
  31. int hour = 0, minute = 0, second = 0, filenumber = 1, optionsallowed = 3;
  32.  
  33. stcgfn(node,argv[0]);
  34.  
  35. if(argc == 1) { printf("\x0c\n"); helpscreen(); }
  36.  
  37. if(argc > 1 && argv[1][0] == '-') 
  38.    if(argv[1][1] != 's') mistake("Invalid Option"); 
  39.  
  40. if(argc > 1) {
  41.    if(argv[1][0] == '-') optionsallowed = 4;
  42.    if(argc > optionsallowed) mistake("Invalid option count");
  43.    if(strcmp(argv[1],"-s") == 0) {
  44.       if(argc == 2) mistake("Missing TIMER name"); 
  45.       filenumber = 2;
  46.       }
  47.    }
  48.  
  49. if(stricmp(argv[filenumber],"NOLINE") == 0) 
  50.    mistake("TIMER name can't be NOLINE");
  51.  
  52. if(stricmp(argv[argc - 1],"NOLINE") && (argc >= filenumber + 2))
  53.    mistake("Invalid final arguement, can only be NOLINE");
  54.    
  55.  
  56.  
  57. time(&time1);
  58. strcat(filename,node);
  59. strcat(filename,".");
  60. strcat(filename,argv[filenumber]);
  61. if(access(filename,0)) {
  62.    tfile = fopen(filename,"w");
  63.    if(tfile == (FILE *)NULL) mistake("Can't create etime file.");
  64.    fprintf(tfile,"%ld\n",time1);
  65.    fflush(tfile);
  66.    fclose(tfile);
  67.    exit(0);
  68.    }
  69.    else {
  70.    tfile = fopen(filename,"r+");
  71.    if(tfile == (FILE *)NULL) mistake("Can't open file");
  72.    fgets(storedtime,20,tfile);
  73.    fclose(tfile);
  74.    remove(filename);
  75.    time2 = atol(storedtime);  
  76.    if(time2 < 500000000) mistake("Invalid TIMER file format");
  77.    time2 = time1 - time2;
  78.    if(filenumber == 2) {
  79.       printf("%ld",time2);
  80.       }
  81.       else {
  82.       second = time2 % 60;
  83.       time2 = time2 - second;
  84.       minute = (time2 / 60) % 60;
  85.       time2 = time2 - (minute * 60);
  86.       hour = time2 / 3600;
  87.       printf("%02d:%02d:%02d",hour,minute,second);  
  88.       }
  89.    }
  90.  
  91. if(stricmp(argv[argc - 1],"NOLINE")) printf("\n");
  92. exit(0);  
  93.  
  94. }  /*  ending brace for main  */
  95.  
  96. /*----------------------------------------------------------------------------*/
  97.  
  98. /*  MISTAKE FUNCTION  */
  99.  
  100. void mistake(description)
  101. char description[80];
  102. {
  103. fprintf(stderr,"\x0c\n\07  ERROR:  %s.\n",description);
  104. helpscreen();
  105. exit(5);
  106. }
  107.  
  108. /*----------------------------------------------------------------------------*/
  109.  
  110. /*  HELPSCREEN FUNCTION  */
  111.  
  112. void helpscreen()
  113. {
  114. line();
  115. printf("     %s            George Kerber  %9s  %24s\n\n",node,VERSION,WRITTEN);
  116. printf("     SYNTAX:  %s [[-s] TIMERNAME [NOLINE]]\n\n",node);
  117. printf("                  -s   Elapsed time is returned in seconds.\n",node);
  118. printf("              NOLINE   A newline will not be output after the\n"); 
  119. printf("                       elapsed time is displayed.\n\n");
  120. printf("     %s will return an elapsed time between the first and second\n",node);
  121. printf("     execution of the program with the same argument.\n");
  122. printf("     Elapsed time will be returned as hh:mm:ss.\n\n");
  123. printf("     Execute %s with a TIMER name to start the timer.\n",node);
  124. printf("     Execute it a second time with the same TIMER name to display the\n");
  125. printf("     elapsed time.  You can have any number of timers in use at the\n");
  126. printf("     same time.\n");
  127. line();
  128. exit(0);
  129. }
  130.  
  131. /*----------------------------------------------------------------------------*/
  132.  
  133. /*  LINE FUNCTION  */
  134.  
  135. void line()
  136. {
  137. printf("  ___________________________________________________________________________\n\n");
  138. }
  139.  
  140. /*----------------------------------------------------------------------------*/
  141.