home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / unix / question / 15444 < prev    next >
Encoding:
Text File  |  1993-01-12  |  2.2 KB  |  81 lines

  1. 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
  2. From: cbusch@ub.d.umn.edu (Chris)
  3. Newsgroups: comp.unix.questions
  4. Subject: I need help
  5. Date: 12 Jan 1993 02:42:29 -0600
  6. Organization: University of Minnesota, Duluth
  7. Lines: 70
  8. Message-ID: <1iu09lINNa3h@ub.d.umn.edu>
  9. NNTP-Posting-Host: ub.d.umn.edu
  10.  
  11. I am just trying to do something simple.  I need a way to determine if
  12. keys are present in the input buffer from the console.  I did read the
  13. FAQ.  I tried ioctl and select, both to no avail.  This is largely due
  14. to poor exampless manuals.
  15. Please help me create a working function.  I am not a newbie when it comes
  16. to C programming (or C++ for that matter!), however, most of my
  17. experience lies with IBM PCs.
  18.  
  19. Basically, all I need is: if(kbhit()) ch=getchar();
  20.  
  21. I tried ioctl like this:
  22.    int count;
  23.    ioctl(stdin/*not sure what goes here*/,FIONREAD,&count);
  24. But of course this doesnt work.
  25.  
  26. What follows is what I tried with select.
  27.  
  28.  
  29.  
  30. --------cut-----
  31. #include <stdio.h>
  32. #include <sys/time.h>
  33.  
  34. int     nfound, nfds, readfds, writefds, exceptfds;
  35. struct timeval timeout;
  36.  
  37. /* How many seconds between queue scans to start off */
  38. #define DEFAULT_PERIOD 300
  39. void main()
  40. {
  41.    int z,ch;
  42.    nfds = 1;
  43.    writefds = 0;
  44.    readfds = 1;
  45.    exceptfds = 0;
  46.    timeout.tv_sec = 0;/*DEFAULT_PERIOD*/;
  47.    timeout.tv_usec = 0;
  48.    
  49.  
  50.    /* this is a bitmap and the 1 specifies to look at stdin only */
  51.    readfds = 1;
  52.  
  53.    /* the select call will wait for the specified period of time if
  54.       no characters are available on stdin.  It will return as soon
  55.       as any characters are available.  If the timeout struct was
  56.       set to 0 it would return immediately even if nothing was available,
  57.       but then it would return a 0.  */
  58. puts("Running:");
  59. for(ch=0;ch!=27;){
  60.    nfound = select(nfds,&readfds,&writefds,&exceptfds,&timeout);
  61.    printf("nfound: %d:4",nfound);
  62.    if (nfound) {
  63.       printf("read %c\n",ch=getchar());
  64.       /* something was typed *{
  65.       }        /* the stuff in this if..else statement is just test BS*/
  66.    else {
  67.       printf("%d10:\r",z++);
  68.       /* nothing was typed */
  69.       }
  70. }
  71. }
  72.  
  73. /*I realize the indenting is off...*/
  74.  
  75.  
  76. --
  77. cbusch@ub.d.umn.edu
  78.  
  79.  
  80.  
  81.