This utility is called wait.exe. I use this utility in batch files to allow a timed pause to occur. It will default to a delay of 10 seconds or it will take a parameter of seconds and it would do either of two things; wait for the number of seconds to elapse and return, or if a keyboard key is pressed, it would return.
I left all my "will this work" and debugging stuff in so people can play around with it like I did. I plan on adding the getopt ftn to process the argv options and adding the functionally of passing in a new string to replace the default message as an option. But, it may take a while, so thought I would go ahead and post the code now, since a while for me have in the past been between a few hours and a few years!
This utility currently works, so you can take it out for a drive. Can it be better, you bet, can't wait to see the code enhancements. Everybody loves to see the code the guru's code or enhance, so people (non-guru and guru) feel free to enhance the code to your hearts content. If you have enhancements, please don't keep them to yourself, post them. Remember, people always talk more about non-guru code than they do the guru's code!
Here is version 1 of wait.exe
#include <stdio.h> // For printf
#include <stdlib.h> // For EXIT_FAILURE, EXIT_SUCCESS
#include <time.h>
#include <sys/timeb.h>
#include <conio.h> // For _kbhit
#include <ctype.h>
#include <string.h> // For strcmp
void doUsage(void)
{
printf ("wait.exe utility v1.0\n" \
"Usage : wait.exe [delay in seconds]\n" \
"options: -h -H /h /H -? /? -help -HELP ?help ?HELP\n" \
"Notes: This default delay is 10 seconds.\n" \
" If a key is pressed before the end of the delay period\n" \
" the program will stop.\n" \
" Author: Fletcher Allen Date: 1/20/2015\n" \
" This program is provided as open source.\n");
}
int main (int argc, char* argv[])
{
struct _timeb now;
struct _timeb then;
int numofsecs = 10; //default delay in seconds
double timedifference = 0; //holds the time difference in seconds between the two times for the do-while loop
//int ireturn = 0; //captures return values
//int i; //used in for loops
/* printf("Number of arguments: %d\n\n",argc);
printf("Name of program %s\n\n",argv[0]);
for (i=1; i<argc && argc > 1; i++)
printf("User parameter = %d : %s\n",i,argv[i]);
printf("\n\n");
*/
/* if (argv[1]) {
if (strcmp ("-?", argv[1]) == 0)
{
printf ("1 User is asking for help.\n" );
doUsage ();
return EXIT_SUCCESS;
//ireturn = system("pause");
}
} */
if (argv[1])
{
// If no arguments are given in DOS command line, then print Usage.
if (argv[1] == "-h" || argv[1] == "/h" || argv[1] == "-H" || argv[1] == "/H" ||
argv[1] == "-?" || argv[1] == "/?" ||
argv[1] == "-help" || argv[1] == "/help" || argv[1] == "-HELP" || argv[1] == "/HELP" )
{
//printf ("1 User is asking for help.\n" );
doUsage ();
return EXIT_SUCCESS;
}
else
{
//printf("3 Argv[1] value: %s with int value of: %d.\n",argv[1], atoi(argv[1]));
if (atoi(argv[1]) != 0)
{
//printf("4 argv[1] %s is a digit value.\n",argv[1]);
numofsecs = atoi(argv[1]);
//ireturn = system("pause");
}
/*else
{
printf("5 argv[1] %s is a not a digit value.\n",argv[1]);
} */
/*
char *s;
char *t;
printf("*s value: %s.\n",s);
printf("*t value: %s.\n",t);
s = '\0';
t = argv[1];
printf("*s value: %s.\n",(char *)*s);
printf("*t value: %s.\n",(char *)*t);
while(*t != '\0')
*s++ = *t++;
*s = '\0';
printf("*s value: %s.\n",(char *)*s);
*/
/* for(i = 0; argv[1][i] != '\0'; i++)
{
//s[i] = argv[1][i];
if (isdigit(argv[1][i]) != 0)
{
printf("6 argv[1][i] %u is a digit value.\n",argv[1][i]);
}
else
{
printf("7 argv[1][i] %u is a not a digit value.\n",argv[1][i]);
}
} */
//s[i] = '\0';
/*printf("sizeof(argv[1]) value: %d.\n",strlen(argv[1]));
for (i=0; i<sizeof(argv[1]); i++)
{
if (isdigit(argv[1][i]) != 0)
{
printf("argv[1][i] %u is a digit value.\n",argv[1][i]);
}
else
{
printf("argv[1][i] %u is a not a digit value.\n",argv[1][i]);
}
} */
//ireturn = system("pause");
/* Once again, these code fragments are being written in a rather compressed way. To make it easier to see what's going on, here are alternate versions of strcpy, which don't bury the assignment in the loop test. First we'll use array notation:
void strcpy(char s[], char t[])
{
int i;
for(i = 0; t[i] != '\0'; i++)
s[i] = t[i];
s[i] = '\0';
}
Note that we have to manually append the '\0' to s after the loop. Note that in doing so we depend upon i retaining its final value after the loop, but this is guaranteed in C, as we learned in Chapter 3.
Here is a similar function, using pointer notation:
void strcpy(char *s, char *t)
{
while(*t != '\0')
*s++ = *t++;
*s = '\0';
}
Again, we have to manually append the '\0'. Yet another option might be to use a do/while loop.
*/
}
}
//ireturn = system("pause");
_ftime(&now); //get the current time
//_ftime(&then);
then = now; //set the later time
//printf ("8 Wait for %4d seconds.\n", numofsecs );
//printf ("9 xx: %10i now.time: %u then.time: %12u \n", (int)xx, now.time, then.time );
//ireturn = system("pause");
printf ("Waiting for %i seconds...\n", (int)numofsecs); //display the default wait.exe message.
do
{
timedifference = difftime(then.time, now.time);
//printf ("10 xx: %i now.time: %12d then.time: %12u \n", (int)numofsecs, now.time, then.time );
_ftime(&then);
if ( _kbhit() != 0)
return 3;
} while (timedifference <= numofsecs);
return 0;
}
I had help with my questions from czerny in the Beginner section, which also posted this code for me, so I thought I would post it here as well. czerny comments were, "This is another small example. If no parameter is given, this waits until keypressed."
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
int main(int argc, char **argv)
{
clock_t start;
double stop;
if (argc == 2)
{
stop = strtod(argv[1], NULL);
if (stop > 0.0) {
start = clock();
while (!_kbhit())
{
if ((double)(clock() - start)/CLOCKS_PER_SEC >= stop) break;
}
}
else puts("usage: wait [seconds]");
}
else while (!_kbhit());
return 0;
}