home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!gatech!emory!sol.ctr.columbia.edu!The-Star.honeywell.com!umn.edu!news.d.umn.edu!ub.d.umn.edu!not-for-mail
- From: cbusch@ub.d.umn.edu (Chris)
- Newsgroups: comp.unix.questions
- Subject: I need help
- Date: 12 Jan 1993 02:42:29 -0600
- Organization: University of Minnesota, Duluth
- Lines: 70
- Message-ID: <1iu09lINNa3h@ub.d.umn.edu>
- NNTP-Posting-Host: ub.d.umn.edu
-
- I am just trying to do something simple. I need a way to determine if
- keys are present in the input buffer from the console. I did read the
- FAQ. I tried ioctl and select, both to no avail. This is largely due
- to poor exampless manuals.
- Please help me create a working function. I am not a newbie when it comes
- to C programming (or C++ for that matter!), however, most of my
- experience lies with IBM PCs.
-
- Basically, all I need is: if(kbhit()) ch=getchar();
-
- I tried ioctl like this:
- int count;
- ioctl(stdin/*not sure what goes here*/,FIONREAD,&count);
- But of course this doesnt work.
-
- What follows is what I tried with select.
-
-
-
- --------cut-----
- #include <stdio.h>
- #include <sys/time.h>
-
- int nfound, nfds, readfds, writefds, exceptfds;
- struct timeval timeout;
-
- /* How many seconds between queue scans to start off */
- #define DEFAULT_PERIOD 300
- void main()
- {
- int z,ch;
- nfds = 1;
- writefds = 0;
- readfds = 1;
- exceptfds = 0;
- timeout.tv_sec = 0;/*DEFAULT_PERIOD*/;
- timeout.tv_usec = 0;
-
-
- /* this is a bitmap and the 1 specifies to look at stdin only */
- readfds = 1;
-
- /* the select call will wait for the specified period of time if
- no characters are available on stdin. It will return as soon
- as any characters are available. If the timeout struct was
- set to 0 it would return immediately even if nothing was available,
- but then it would return a 0. */
- puts("Running:");
- for(ch=0;ch!=27;){
- nfound = select(nfds,&readfds,&writefds,&exceptfds,&timeout);
- printf("nfound: %d:4",nfound);
- if (nfound) {
- printf("read %c\n",ch=getchar());
- /* something was typed *{
- } /* the stuff in this if..else statement is just test BS*/
- else {
- printf("%d10:\r",z++);
- /* nothing was typed */
- }
- }
- }
-
- /*I realize the indenting is off...*/
-
-
- --
- cbusch@ub.d.umn.edu
-
-
-
-