/* EMACS_MODES: !fill */

#include <stdio.h>
#define NAME "yn"
#define VERSION "2.0"

main(argc,argv)
int argc;
char *argv[];
{

	long maxwait;
	long t0,t1;
	int c;
	if (argc<2){
		printf("By Andrew Gaunt, AKA Quantum, #1@6300 - May be freely shared.\n\n");
		printf("usage: yn timeout\n\n");
		printf("yn will wait timeout seconds for a key to be pressed.\n");
		printf("If a \"y\" or \"n\" is pressed, it will exit, setting the error level\n");
		printf("as follows.\n\n");
		printf("  If y is pressed the error level is set to 1.\n");
		printf("  If n is pressed the error level is set to 2.\n");
		printf("  If timeout seconds is exceeded, the error level will be 0.\n");
		printf("\n[%s v%s]\n",NAME,VERSION);
	}
	maxwait=atol(argv[1]);
	while(1){
		time(&t0);
		while (!kbhit()) {
			time(&t1);
			if(t1>t0+maxwait){
				exit(0);
			}
		}
		c=getch();
		if (tolower(c)=='y')
			exit(1);
		if (tolower(c)=='n')
			exit(2);
	}
}

