/****************************************************************************/ /* */ /* Module: time.c - Time execution of a program */ /* */ /* Programmer: George R. Woodside */ /* */ /* Date: January 8, 1987 */ /* */ /* Function: Time the execution of a program. */ /* Works the same as the unix function of the same name. */ /* If "prog -parm -parm parm parm" executes, then */ /* "time prog -parm -parm parm parm" executes the same, with */ /* the execution time printed after completion. */ /* */ /* Warning: Stack size can be critical here. Generally, this program */ /* is set to keep very little memory, and return the rest. */ /* If you re-compile it, be sure to keep the stack size */ /* at something minimal. As supplied, it keeps 8K. */ /* */ /* Since the current TOS is not able to pass path names, */ /* the program to be executed must either be in the current, */ /* active directory, or somewhere along the PATH specified in */ /* the environment area. */ /* */ /****************************************************************************/ #include #include #include #ifdef MWC /* if Mark Williams shell, */ #define ESEP ',' /* environment separator is a comma */ #define PSEP "\\" /* path name separator required */ char *dpath = ","; /* default path */ #else /* if Micro C-Shell, */ #define ESEP ';' /* env separator is a semicolon */ #define PSEP "\0" /* path name separator built in */ char *dpath = ".\\"; /* default path */ #endif #define GOT_ONE 0L extern char * getenv(); /* getenv returns a char pointer */ struct DTA { unsigned char resrv[21]; /* used by system */ unsigned char attr; /* attributes */ unsigned int ctime; /* creation time */ unsigned int cdate; /* creation date */ unsigned long fsize; /* file size */ unsigned char fname[14]; /* file name */ } wdta ; struct DTA *dta = &wdta; /* pointer to structure */ char comm[128]; /* copy command here */ char tail[128]; /* and command tail here */ char tpath[128]; /* temporary path */ char prog[16]; /* search program name */ char p1[16]; /* .tos program name */ char p2[16]; /* .ttp program name */ char p3[16]; /* .prg program name */ int lsflag = 0; /* print path names during searches */ int dkflag = 0; /* hold screen for desktop */ /****************************************************************************/ /* Begin here. Parse command line for our own arguments, -d or -l, */ /* preceeding the target program name. All other arguments are */ /* passed to the target program. */ /****************************************************************************/ main(argc,argv) int argc; char *argv[]; { long start_time; /* timer at initiation */ long end_time; /* timer at termination */ long sup_stk; /* saved supervisor stack */ long *clock = (long *) 0x4ba; /* address of system 200hz counter */ long mins; /* computed elapsed minutes */ long secs; /* computed elapsed seconds */ long fracs; /* computed elapsed remainder */ register int i; /* generic counter */ register int carg = 1; /* argument counter */ register int ours = 1; /* argument is for time */ while(ours) { ours = 0; /* set argument not used */ if(strcmp(argv[carg],"-d") == 0) /* if desktop request */ { dkflag = 1; /* the flag should be set */ ours = 1; /* argument is now used */ } if(strcmp(argv[carg],"-l") == 0) /* if list request */ { lsflag = 1; /* the flag should be set */ ours = 1; /* argument is now used */ } if(strcmp(argv[carg],"-dl") == 0) /* if desktop and list request */ { dkflag = 1; /* desktop flag should be set */ lsflag = 1; /* list flag should be set */ ours = 1; /* argument is now used */ } if(strcmp(argv[carg],"-ld") == 0) /* if list and desktop request */ { lsflag = 1; /* list flag should be set */ dkflag = 1; /* desktop flag should be set */ ours = 1; /* argument is now used */ } if(ours) /* if we used this argument, */ carg++; /* move to next one */ } /* end test arguments for time */ if(locate(argv[carg]) == 0) /* if we can find the program */ { tail[1] = '\0'; /* terminate command tail */ for(i = carg + 1; i < argc; i++) { strcat(&tail[1],argv[i]); /* add in the arguments */ strcat(&tail[1]," "); /* and a separator */ } i = strlen(&tail[1]); /* compute tail length */ tail[0] = (char) i & 0xff; /* insert length byte */ sup_stk = Super(0L); /* enter supervisor mode */ start_time = *clock; /* get the time */ Super(sup_stk); /* back to user mode */ Pexec(0,comm,tail,0L); /* launch the program */ sup_stk = Super(0L); /* enter supervisor mode */ end_time = *clock; /* get the time */ Super(sup_stk); /* back to user mode */ fracs = end_time - start_time; /* elapsed time in ticks */ mins = fracs / (200L * 60L); /* elapsed minutes */ fracs -= mins * (200L * 60L); /* take them out */ secs = fracs / 200L; /* elapsed seconds */ fracs -= secs * 200L; /* take them out */ fracs /= 2L; /* make them hundredths of seconds */ printf("time: %D:%02D.%02D\n",mins,secs,fracs); } /* end got program */ else /* if we can not find the program, */ printf("time: Program %s not found.\n",argv[carg]); /* say we failed. */ if(dkflag) /* if we are to hold the screen, */ { printf("time: Press any key\n"); /* give a prompt */ Crawcin(); /* and wait for a key */ } } /* end main */ /****************************************************************************/ /* Try the environment path first. If no matching program, try the */ /* current directory, just in case the simple minded command processor */ /* is still around. */ /****************************************************************************/ locate(pname) char *pname; { register int i = 0; register int hit; /* "got it" flag */ register int flag = 0; /* file type specified flag */ register char *path; /* the path we will search */ register char *ptr = pname; /* the program we will search for */ Fsetdta(dta); /* set system dta address */ while(*ptr) /* copy until end of string */ { if(islower(*ptr) ) /* if character is lower case, */ prog[i++] = toupper(*ptr); /* make it upper case */ else /* otherwise, */ prog[i++] = *ptr; /* just copy it */ if( *ptr++ == '.') /* if there is a type */ flag++; /* set the type specified flag */ } strcpy(p1,prog); /* make first copy */ strcpy(p2,prog); /* make second copy */ strcpy(p3,prog); /* make third copy */ if(flag == 0) /* if no file type, */ { strcat(prog,".*"); /* make wild card search */ strcat(p1,".TOS"); /* make .tos name */ strcat(p2,".TTP"); /* make .ttp name */ strcat(p3,".PRG"); /* make .prg name */ } /* end no file type specified */ if( (path = getenv("PATH") ) != (char *) NULL) /* if a path string, */ hit = search(path,prog); /* check it out */ if(hit) /* if we didn't get it, */ hit = search(dpath,prog); /* default to current directory */ return(hit); /* say if we found the file, */ } /****************************************************************************/ /* Start searching the set paths for an executable program */ /* with the name argv[1].prg, argv[1].ttp, or argv[1].tos. */ /* If there is a dot in argv[1], search only for argv[1] exactly. */ /* Substitute current drive and path when the set path is ".\" */ /****************************************************************************/ search(spath,sname) char *spath; char *sname; { register int i; register int match = 1; /* string match flag */ register long moref; /* response from searches */ register char *path = spath; /* the paths we will search */ /**************************************************************************/ /* For each entry in the PATH variable, search for a file */ /* that matches the program name. */ /**************************************************************************/ while( *path && match ) { i = 0; /* reset the array index */ while( *path && (*path != ESEP) ) /* until end of path entry, */ { if(islower(*path) ) /* if path is lower case, */ tpath[i++] = toupper(*path++); /* make work field upper case */ else /* if not lower case, */ tpath[i++] = *path++; /* copy directly to work field */ } /* end of path copy */ tpath[i] = '\0'; /* now terminate the path */ if(strlen(tpath)) /* if there is anything in the path */ strcat(tpath,PSEP); /* tack on path separator */ if(*path) /* if path is not exhausted, */ path++; /* skip the separator */ if(strcmp(tpath,".\\") == 0) /* if it's the current directory, */ tpath[0] = '\0'; /* change to no path */ strcpy(comm,tpath); /* keep the proper path */ strcat(tpath,sname); /* add search name to path */ if(lsflag) /* if debugging, */ printf("Searching %s\n",tpath); /* log the paths as searched */ moref = Fsfirst(tpath,0); /* do the first search */ while( (moref == GOT_ONE) && match ) /* until a match, or no files */ { match = strcmp(p1,dta->fname);/* is .tos a hit? */ if(match) /* if that did not match, */ match = strcmp(p2,dta->fname); /* try .ttp */ if(match) /* if that did not match, */ match = strcmp(p3,dta->fname); /* try .prg */ if(match) /* if that didn't match either, */ moref = Fsnext(); /* look for another file */ } /* end search for this path */ } /* end search all paths */ if(match == 0) /* if we got it, */ strcat(comm,dta->fname); /* add the program name */ return(match); /* report the result */ }