How can I read keyboard without stopping
John Silva
jsilva at cogsci.berkeley.edu
Tue Aug 16 17:03:14 AEST 1988
Here's a little demo program which should work fine on System V or Xenix
systems. (doesn't work under BSD, due to the use of the termio structure).
---------------------------- CUT HERE -------------------------------
#include<stdio.h>
#include<termio.h>
#include<signal.h>
void Finish();
static struct termio term, saveterm;
static long chars = 0;
static long counter = 0;
main(argc, argv)
int argc;
char *argv[];
{
int old, i, timeout;
char ch;
timeout = 1;
ioctl(fileno(stdin), TCGETA, &saveterm);
for (i = 0; i < NSIG; i++)
signal(i, Finish);
if (--argc) {
if (i = (0xFF & atoi(*(++argv)))) timeout = i;
}
printf("Using timeout of %.2fs\n", (float)timeout/10);
printf("You may begin typing....\n");
ioctl(fileno(stdin), TCGETA, &term);
term.c_lflag &= ~ICANON;
term.c_cc[VMIN]='\0';
term.c_cc[VTIME]=(char)timeout;
ioctl(fileno(stdin), TCSETA, &term);
while(1) {
if((ch = getchar()) != (char)EOF) chars++;
counter++;
}
ioctl(fileno(stdin), TCSETA, &saveterm);
exit(0);
}
void
Finish(sig)
int sig;
{
ioctl(fileno(stdin), TCSETA, &saveterm);
printf("\nExiting, signal #%d\n", sig);
printf("chars: %ld\ncounter: %ld\n",chars,counter);
exit(0);
}
------------------------------ CUT HERE -----------------------------
John Silva
---
UUCP: ucbvax!cogsci!jsilva
DOMAIN: jsilva at cogsci.berkeley.edu
More information about the Comp.unix.wizards
mailing list