[comp.lang.c] true unbuffered input.

efinley%ug.utah.edu@cs.utah.edu (Elliott Finley) (01/16/90)

I can't seem to get true unbuffered input.  By unbuffered input I mean
that the user should be able to type a key and then the program should
be able to act on that key before <CR> is pressed.

I've tried the following:

int getchar(void)
{
   char c;

   return (read(0, &c, 1) == 1) ? (unsigned char) c : EOF;
}

But it still waits for a carriage return before the program actually sees
the data..

Any help would be appreciated.  (I running on a VAX with BSD 4.3)

--

Send email to efinley@ug.utah.edu

dwb@sppy00.UUCP (David Burhans) (01/17/90)

In article <1990Jan15.222652.21085@hellgate.utah.edu> efinley%ug.utah.edu@cs.utah.edu (Elliott Finley) writes:
>
>I can't seem to get true unbuffered input.   ...stuff deleted...

 To do this you meed to change the your mode from cooked to CBREAK or RAW.
 Use ioctl(2) to accomplish this change.

 Man pages of interest: ioctl(2), tty(4).
 Include files of interest: sgtty.h, sys/ioctl.h.
 An example program follows:
---------------------------<Cut Here>-------------------------------------
#include <stdio.h>
#include <sgtty.h>
main() {
  int c;
  struct sgttyb oldargs, newargs;

  printf("stdin:%d\n",fileno(stdin));
  ioctl(fileno(stdin), TIOCGETP, &oldargs);

  newargs = oldargs;
  newargs.sg_flags |= CBREAK;
  newargs.sg_flags &= ~(ECHO | CRMOD);
  ioctl(fileno(stdin), TIOCSETP, &newargs);

  while(c = getchar(), c != '\004') {  /* while c != ^D */
    switch (c) {
      case '\033': fputs("<Esc>", stdout); break;
      case '\n':   fputs("\\n\n\r", stdout); break;
      case '\r':   fputs("\\r\n\r", stdout); break;
      case '\t':   fputs("\\t", stdout); break;
      default:     fprintf(stdout,"%c",c); break;
    }
  }
  ioctl(fileno(stdin), TIOCSETP, &oldargs);
}
-- 
dwb@sppy00	{att|killer|pyramid}!osu-cis!sppy00!dwb	
David Burhans/6565 Frantz Rd./Columbus, Oh 43017
  ****  Views expressed above were randomly generated with a six
  	sided die and as such are not the views of my employer.   *****