/******************************************************************************/
/*                                                                            */
/*      program: etime                                                        */
/*   programmer: George Kerber                                                */
/*      written:  07/16/89                                                    */
/*                                                                            */
/******************************************************************************/

#include <stdio.h>
#include <time.h>
#include <string.h>

#define WRITTEN "07/16/89 - 07/23/89"
#define VERSION "v1.02"


FILE *tfile;
char node[31];
void main();
void mistake();
void helpscreen();
void line();
  
void main(argc,argv)
int argc;
char *argv[];
{
char filename[80] = "env:" ;
char storedtime[80];
long time1,time2;
int hour = 0, minute = 0, second = 0, filenumber = 1, optionsallowed = 3;

stcgfn(node,argv[0]);

if(argc == 1) { printf("\x0c\n"); helpscreen(); }
 
if(argc > 1 && argv[1][0] == '-') 
   if(argv[1][1] != 's') mistake("Invalid Option"); 

if(argc > 1) {
   if(argv[1][0] == '-') optionsallowed = 4;
   if(argc > optionsallowed) mistake("Invalid option count");
   if(strcmp(argv[1],"-s") == 0) {
      if(argc == 2) mistake("Missing TIMER name"); 
      filenumber = 2;
      }
   }

if(stricmp(argv[filenumber],"NOLINE") == 0) 
   mistake("TIMER name can't be NOLINE");

if(stricmp(argv[argc - 1],"NOLINE") && (argc >= filenumber + 2))
   mistake("Invalid final arguement, can only be NOLINE");
   


time(&time1);
strcat(filename,node);
strcat(filename,".");
strcat(filename,argv[filenumber]);
if(access(filename,0)) {
   tfile = fopen(filename,"w");
   if(tfile == (FILE *)NULL) mistake("Can't create etime file.");
   fprintf(tfile,"%ld\n",time1);
   fflush(tfile);
   fclose(tfile);
   exit(0);
   }
   else {
   tfile = fopen(filename,"r+");
   if(tfile == (FILE *)NULL) mistake("Can't open file");
   fgets(storedtime,20,tfile);
   fclose(tfile);
   remove(filename);
   time2 = atol(storedtime);  
   if(time2 < 500000000) mistake("Invalid TIMER file format");
   time2 = time1 - time2;
   if(filenumber == 2) {
      printf("%ld",time2);
      }
      else {
      second = time2 % 60;
      time2 = time2 - second;
      minute = (time2 / 60) % 60;
      time2 = time2 - (minute * 60);
      hour = time2 / 3600;
      printf("%02d:%02d:%02d",hour,minute,second);  
      }
   }

if(stricmp(argv[argc - 1],"NOLINE")) printf("\n");
exit(0);  

}  /*  ending brace for main  */

/*----------------------------------------------------------------------------*/

/*  MISTAKE FUNCTION  */

void mistake(description)
char description[80];
{
fprintf(stderr,"\x0c\n\07  [0mERROR:  [33m%s.[0m\n",description);
helpscreen();
exit(5);
}

/*----------------------------------------------------------------------------*/

/*  HELPSCREEN FUNCTION  */

void helpscreen()
{
line();
printf("     [33m[1m%s[0m            George Kerber  %9s  %24s[0m\n\n",node,VERSION,WRITTEN);
printf("     SYNTAX:  [33m%s [[-s] TIMERNAME [NOLINE]][0m\n\n",node);
printf("                  -s   Elapsed time is returned in seconds.\n",node);
printf("              NOLINE   A newline will not be output after the\n"); 
printf("                       elapsed time is displayed.\n\n");
printf("     [33m%s[0m will return an elapsed time between the first and second\n",node);
printf("     execution of the program with the same argument.\n");
printf("     Elapsed time will be returned as [33mhh:mm:ss[0m.\n\n");
printf("     Execute [33m%s[0m with a TIMER name to start the timer.\n",node);
printf("     Execute it a second time with the same TIMER name to display the\n");
printf("     elapsed time.  You can have any number of timers in use at the\n");
printf("     same time.\n");
line();
exit(0);
}

/*----------------------------------------------------------------------------*/

/*  LINE FUNCTION  */

void line()
{
printf("[32m[1m  ___________________________________________________________________________[0m\n\n");
}

/*----------------------------------------------------------------------------*/
