home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / unix / aix / 11764 < prev    next >
Encoding:
Text File  |  1992-11-20  |  1.7 KB  |  54 lines

  1. Newsgroups: comp.unix.aix
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!sun-barr!ames!purdue!yuma!sorensej
  3. From: sorensej@CS.ColoState.EDU (james a sorensen)
  4. Subject: Re: Non blocking reads from stdin
  5. Sender: news@yuma.ACNS.ColoState.EDU (News Account)
  6. Message-ID: <Nov20.200452.38395@yuma.ACNS.ColoState.EDU>
  7. Date: Fri, 20 Nov 1992 20:04:52 GMT
  8. References: <Nov.19.11.24.21.1992.26240@gauss.rutgers.edu>
  9. Nntp-Posting-Host: handel.cs.colostate.edu
  10. Organization: Sleuthworks, Inc.
  11. Keywords: non blocking getchar()
  12. Lines: 40
  13.  
  14. The following code is what I have used to reconfigure stdin for polling.  
  15. You have to store a copy of the current configuration and then restore it 
  16. before leaving your program.  The same applies if your program does interactive
  17. system calls or allows a escape to the shell.  You should also trap any
  18. signals that may cause an abrupt end to your program and restore the original
  19. terminal configuration before exiting.
  20.  
  21. For (a lot) more information, plow through the man page for termio.
  22. I use getc(stdin) instead of getchar but it should work the same.
  23.  
  24. You can also do this using curses.
  25.  
  26. James Sorensen
  27. Sleuthworks, Inc.
  28.  
  29.  
  30. {
  31.    struct termio orgterm, term;
  32.  
  33.    /* Get current io configuration */
  34.    if (ioctl (0,TCGETA, &term) == -1)
  35.    {
  36.       fprintf (stderr,"standard input is not a tty\n");
  37.       exit();
  38.    }
  39.  
  40.    /* Save current io configuration */
  41.    orgterm = term;
  42.  
  43.    /* Disable canonical processing and echoing */
  44.    term.c_lflag &= ~(ICANON | ECHO);    /* disable icanonical processing */
  45.  
  46.    term.c_cc[VMIN]  = 0;   /* Set minimum number of characters to 0 */
  47.    term.c_cc[VTIME] = 0;   /* Set minimum time to 0 */
  48.  
  49.    ioctl (0,TCSETA,&term);
  50. }
  51.  
  52.  
  53.  
  54.