mchetan@hawk.ulowell.edu (Munisuvratha Chetan) (10/18/90)
Hi UNIX-C fellows, I need a C routine, "readchar ()" that reads one char from the stdin, WITHOUT waiting for a '\n' on the stream. How do I do this with "ioctl()" ? Thanx in advace for any help. Cheers.
nieters@eagle.crd.ge.com (coolbean) (10/19/90)
hi there A readchar() program to suit your needs follows. I wasn't sure what version OS you were working on so I wrote a version to work on Sun OS 3.5 and one to work on Sun OS 4.0. A sample main() is included too. hope this helps. --ed -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= main() { int x; printf("enter a character: "); x = readchar(); printf("\nthe letter %c was entered.\n", x); } >>>>> Sun OS 3.5 Version: <<<<< #include <stdio.h> #include <sys/ioctl.h> int readchar() { struct sgttyb old, new; int x; ioctl(0, TIOCGETP, &old); /* get the current tty settings */ new = old; new.sg_flags |= CBREAK; ioctl(0, TIOCSETP, &new); /* set the new tty options */ x=getchar(); ioctl(0, TIOCSETP, &old); /* return the tty to its original state */ return(x); } >>>>> Sun OS 4.0 Version: <<<<< #include <stdio.h> #include <sys/termios.h> #include <fcntl.h> int readchar() { struct termios old, new; int x; ioctl(0, TCGETS, &old); /* get the current tty settings */ new=old; new.c_lflag &= ~ICANON; new.c_cc[VMIN]=1; new.c_cc[VTIME]=0; ioctl(0, TCSETS, &new); /* set the new tty options */ x=getchar(); ioctl(0, TCSETS, &old); /* return the tty to its original state */ return(x); } -- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Ed Nieters INTERnet: nieters@crd.ge.com GE Corporate R&D UUCPnet: uunet!crd.ge.com!nieters Schenectady, NY 12301 BELLnet: (518) 387-5187