/*-------------------------------------------------------------------------*/
/*      Program:  etime                                                    */
/*   Programmer:  George Kerber                                            */
/*      Written:  07/16/89                                                 */
/*     Compiler:  Lattice 5.04                                             */
/*-------------------------------------------------------------------------*/

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <dos.h>
#include <stdlib.h>
#include <ctype.h>

#define WRITTEN "07/16/89 - 01/01/90"
#define VERSION "v1.05c"


void main();
void mistake();
void helpscreen();
void makeline();
void exitcode();
  
void main(int argc, char *argv[])
{
FILE *tfile;
char node1[FNSIZE];
char spacer[80] = "", *odata, option, opts[] = "";
char filename[80] = "Multiple Color Options";
char storedtime[80] = "Can't RETAIN and DELETE/START";
char drive[FNSIZE], path[FMSIZE], node[FNSIZE], ext[FESIZE];
long time1,time2;
int i, next, hour = 0, minute = 0, second = 0;
int rflag = 1, sflag = 0, nflag = 1, dflag = 0, cflag = 0;

stcgfn(node1,argv[0]);
if(argc == 1 || (argc == 2 && argv[1][0] == '?')) {
   printf("\x0c\n\n\n"); helpscreen(node1); }


for( next = 1 ; (odata = argopt(argc,argv,opts,&next,&option)) != NULL ; ) {
   switch(option) {
      case 'r':   rflag = 0;
                  if(dflag) mistake(storedtime,node1);
                  break ;                          /*  retain file         */
      case 's':   sflag++ ; break ;                /*  seconds             */
      case 'n':   nflag = 0; break ;               /*  noline              */
      case 'd':   dflag++;
                  if(!rflag) mistake(storedtime,node1);
                  break ;                          /*  delete/start only   */
      case '3':   printf("[33m"); 
                  if(cflag) mistake(filename,node1); cflag++;
                  break ;                          /*  orange              */
      case '2':   printf("[32m");
                  if(cflag) mistake(filename,node1); cflag++;
                  break ;                          /*  black               */
      case '1':   printf("[31m");
                  if(cflag) mistake(filename,node1); cflag++;
                  break ;                          /*  white (default)     */
      case '5':   printf("[1m");  break ;        /*  bold                */
      case '6':   printf("[4m");  break ;        /*  underline    [0m  */
      case '7':   printf("[3m");  break ;        /*  italics      [0m  */

      case 'm':   if(!strcmp(odata,"")) 
                     mistake("Requires # of Spaces to Add",node1);
                  for(i = (strlen(odata)) ; i >= 1 ; i--) {    
                  if(!isdigit(odata[i - 1]))
                     mistake("Only Numeric Entries",node1);
                  } 
                  i = atoi(odata);
                  if(!i || i > 69) mistake("Entry must be 1 - 69",node1);
                  for( ; i > 0 ;strcat(spacer," "), i--) ;
                  break;
        default:  mistake("Invalid Option",node1);
      }
   }

/*----- Begin error & option checking.-------------------------------------*/
if(next >= argc) mistake("Missing TIMER Name",node1);
if(argv[next][0] == '-') mistake("Missing TIMER Name",node1);
if(argc > (next + 1)) mistake("Invalid Option Count",node1);

/*----- End error checking ------------------------------------------------*/

strsfn(argv[next],drive,path,node,ext);

if(strcmp(ext,"") && strcmp(ext,"etime")) 
   mistake("Filename extension defaults to \".etime\"",node1);

strcpy(ext,"etime");
if(drive[0] == '\0' && path[0] == '\0') { 
   strcpy(drive,"ram:");
   strcpy(path,"etime");
   if(access("ram:etime",0)) mkdir("ram:etime"); }

if(drive[0] == '\0' && path[0] != '\0') 
   mistake("Full path must be specified",node1);

strmfn(filename,drive,path,node,ext);   /*  create destination file name   */

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

if(dflag) remove(filename);
 
time(&time1);
if(access(filename,0)) {
   tfile = fopen(filename,"w");
   if(tfile == (FILE *)NULL) mistake("Can't create etime file.",node1);
   fprintf(tfile,"%ld\n",time1);
   fflush(tfile);
   fclose(tfile);
   exitcode(0);
   }
   else {
   tfile = fopen(filename,"r+");
   if(tfile == (FILE *)NULL) mistake("Can't open file",node1);
   fgets(storedtime,20,tfile);
   fclose(tfile);
   if(rflag) remove(filename);      /*  remove file unless -r (retain)     */
   time2 = atol(storedtime);        /*  insure stored data is a long value */
   if(time2 < 500000000) mistake("Invalid TIMER file format",node1);
   time2 = time1 - time2;
   if(time2 < 0) mistake("Invalid ETIME, check system clock",node1);
   if(sflag) {
      printf("%s%ld",spacer,time2); /*   output in total seconds           */
      }
      else {
      second = time2 % 60;          /*   Calculate hours, minutes & seconds*/
      time2 = time2 - second;
      minute = (time2 / 60) % 60;
      time2 = time2 - (minute * 60);
      hour = time2 / 3600;
      printf("%s%02d:%02d:%02d",spacer,hour,minute,second);  /* hh:mm:ss  */
      }
   }

if(nflag) printf("\n");             /*  output newline unless NOLINE       */
exitcode(0);  

}  /*  ending brace for main  */

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

/*  EXITCODE FUNCTION  */

void exitcode(int x)
{
printf("[0m");
exit(x);
}

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

/*  HELPSCREEN FUNCTION  */

void helpscreen(char node1[])
{
printf("     [33m[1m%s[0m            George Kerber  %9s  %24s[0m\n\n",node1,VERSION,WRITTEN);
printf("     SYNTAX:  [33m%s [[-s -r -n -d -1 -2 -3 -5 -6 -7] TIMERNAME]][0m\n\n",node1);
printf("                  -s   Elapsed time is returned in seconds.\n");
printf("                  -r   Stored time is not removed.\n");
printf("                  -n   Newline will not be output.\n");
printf("                  -d   Unconditionally deletes etime file & resets.\n");
printf("                  -#   Color, boldface, underline & italics.\n\n");
printf("     [33m%s[0m will return an elapsed time between the first and second\n",node1);
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.  Execute it\n",node1);
printf("     a 2nd time with the same TIMER name to display the elapsed time.\n\n");
exitcode(0);
}

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

/*  MAKELINE FUNCTION  */

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

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

/*  MISTAKE FUNCTION  */

void mistake(char description[],char node1[])
{
printf("\n\07  [0mERROR:  %s --> [33m%s.[0m\n\n",node1,description);
exitcode(5);
}

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

08/05/89 v1.03:   Changed default directory to ram:etime instead of env:.
                  Added -r -n -d options.
11/01/89 v1.05b:  Re-compiled with Lattice 5.04
01/01/90 v1.05c:  Removed all global variables.

*/
